| 1 |
package tsi1_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/influxdata/influxdb/models" |
| 8 |
"github.com/influxdata/influxdb/tsdb/index/tsi1" |
| 9 |
) |
| 10 |
|
| 11 |
// Ensure multiple index files can be compacted together. |
| 12 |
func TestIndexFiles_WriteTo(t *testing.T) { |
| 13 |
sfile := MustOpenSeriesFile() |
| 14 |
defer sfile.Close() |
| 15 |
|
| 16 |
// Write first file. |
| 17 |
f0, err := CreateIndexFile(sfile.SeriesFile, []Series{ |
| 18 |
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "east"})}, |
| 19 |
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "west"})}, |
| 20 |
{Name: []byte("mem"), Tags: models.NewTags(map[string]string{"region": "east"})}, |
| 21 |
}) |
| 22 |
if err != nil { |
| 23 |
t.Fatal(err) |
| 24 |
} |
| 25 |
|
| 26 |
// Write second file. |
| 27 |
f1, err := CreateIndexFile(sfile.SeriesFile, []Series{ |
| 28 |
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "west"})}, |
| 29 |
{Name: []byte("disk"), Tags: models.NewTags(map[string]string{"region": "east"})}, |
| 30 |
}) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
|
| 35 |
// Compact the two together and write out to a buffer. |
| 36 |
var buf bytes.Buffer |
| 37 |
a := tsi1.IndexFiles{f0, f1} |
| 38 |
if n, err := a.CompactTo(&buf, sfile.SeriesFile, M, K, nil); err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} else if n == 0 { |
| 41 |
t.Fatal("expected data written") |
| 42 |
} |
| 43 |
|
| 44 |
// Unmarshal buffer into a new index file. |
| 45 |
f := tsi1.NewIndexFile(sfile.SeriesFile) |
| 46 |
if err := f.UnmarshalBinary(buf.Bytes()); err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
|
| 50 |
// Verify data in compacted file. |
| 51 |
if e := f.TagValueElem([]byte("cpu"), []byte("region"), []byte("west")); e == nil { |
| 52 |
t.Fatal("expected element") |
| 53 |
} else if n := e.(*tsi1.TagBlockValueElem).SeriesN(); n != 1 { |
| 54 |
t.Fatalf("unexpected series count: %d", n) |
| 55 |
} |
| 56 |
} |