2016-10-27 15:47:41 +00:00
|
|
|
package tsi1_test
|
2016-11-02 16:09:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/models"
|
2016-11-15 16:20:00 +00:00
|
|
|
"github.com/influxdata/influxdb/tsdb/index/tsi1"
|
2016-11-02 16:09:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure multiple index files can be compacted together.
|
|
|
|
func TestIndexFiles_WriteTo(t *testing.T) {
|
|
|
|
// Write first file.
|
|
|
|
f0, err := CreateIndexFile([]Series{
|
|
|
|
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "east"})},
|
|
|
|
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "west"})},
|
|
|
|
{Name: []byte("mem"), Tags: models.NewTags(map[string]string{"region": "east"})},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write second file.
|
|
|
|
f1, err := CreateIndexFile([]Series{
|
|
|
|
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "west"})},
|
|
|
|
{Name: []byte("disk"), Tags: models.NewTags(map[string]string{"region": "east"})},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compact the two together and write out to a buffer.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
a := tsi1.IndexFiles{f0, f1}
|
|
|
|
if n, err := a.WriteTo(&buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if n == 0 {
|
|
|
|
t.Fatal("expected data written")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal buffer into a new index file.
|
|
|
|
var f tsi1.IndexFile
|
|
|
|
if err := f.UnmarshalBinary(buf.Bytes()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:05:48 +00:00
|
|
|
// Verify data in compacted file.
|
2016-11-29 18:09:33 +00:00
|
|
|
if e := f.TagValueElem([]byte("cpu"), []byte("region"), []byte("west")); e == nil {
|
2016-11-27 16:34:03 +00:00
|
|
|
t.Fatal("expected element")
|
|
|
|
} else if n := e.(*tsi1.TagBlockValueElem).SeriesN(); n != 1 {
|
|
|
|
t.Fatalf("unexpected series count: %d", n)
|
2016-11-03 17:05:48 +00:00
|
|
|
}
|
2016-11-02 16:09:49 +00:00
|
|
|
}
|