feature(tsdb): decode name back into IDs

Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
pull/10616/head
Leonardo Di Donato 2018-10-10 17:02:26 +02:00 committed by Chris Goller
parent 473f4e986c
commit 21475c22cd
2 changed files with 49 additions and 1 deletions

View File

@ -18,10 +18,16 @@ var (
MeasurementTagKeyBytes = []byte(MeasurementTagKey)
)
// DecodeName converts tsdb internal serialization back to organization and bucket IDs.
func DecodeName(name [16]byte) (org, bucket platform.ID) {
org = platform.ID(binary.BigEndian.Uint64(name[0:8]))
bucket = platform.ID(binary.BigEndian.Uint64(name[8:16]))
return
}
// EncodeName converts org/bucket pairs to the tsdb internal serialization
func EncodeName(org, bucket platform.ID) [16]byte {
var nameBytes [16]byte
binary.BigEndian.PutUint64(nameBytes[0:8], uint64(org))
binary.BigEndian.PutUint64(nameBytes[8:16], uint64(bucket))
return nameBytes

42
tsdb/explode_test.go Normal file
View File

@ -0,0 +1,42 @@
package tsdb_test
import (
"fmt"
"testing"
"github.com/influxdata/platform"
"github.com/influxdata/platform/tsdb"
)
func TestNames(t *testing.T) {
goodExamples := []struct {
Org uint64
Bucket uint64
Name [16]byte
}{
{Org: 12345678, Bucket: 87654321, Name: [16]byte{0, 0, 0, 0, 0, 188, 97, 78, 0, 0, 0, 0, 5, 57, 127, 177}},
{Org: 1234567891011, Bucket: 87654321, Name: [16]byte{0, 0, 1, 31, 113, 251, 8, 67, 0, 0, 0, 0, 5, 57, 127, 177}},
{Org: 12345678, Bucket: 8765432100000, Name: [16]byte{0, 0, 0, 0, 0, 188, 97, 78, 0, 0, 7, 248, 220, 119, 116, 160}},
{Org: 123456789929, Bucket: 8765432100000, Name: [16]byte{0, 0, 0, 28, 190, 153, 29, 169, 0, 0, 7, 248, 220, 119, 116, 160}},
}
for _, example := range goodExamples {
t.Run(fmt.Sprintf("%d%d", example.Org, example.Bucket), func(t *testing.T) {
name := tsdb.EncodeName(platform.ID(example.Org), platform.ID(example.Bucket))
if got, exp := name, example.Name; got != exp {
t.Errorf("got name %q, expected %q", got, exp)
}
org, bucket := tsdb.DecodeName(name)
if gotOrg, expOrg := org, example.Org; gotOrg != platform.ID(expOrg) {
t.Errorf("got organization ID %q, expected %q", gotOrg, expOrg)
}
if gotBucket, expBucket := bucket, example.Bucket; gotBucket != platform.ID(expBucket) {
t.Errorf("got organization ID %q, expected %q", gotBucket, expBucket)
}
})
}
}