Exemple #1
0
// Ensure the multi-cursor can correctly iterate across multiple non-overlapping subcursors.
func TestMultiCursor_Multiple_NonOverlapping_Reverse(t *testing.T) {
	mc := tsdb.MultiCursor(
		NewCursor([]CursorItem{
			{Key: 0, Value: 0},
			{Key: 3, Value: 30},
			{Key: 4, Value: 40},
		}, false),
		NewCursor([]CursorItem{
			{Key: 1, Value: 10},
			{Key: 2, Value: 20},
		}, false),
	)

	if k, v := mc.SeekTo(4); k != 4 || v.(int) != 40 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 3 || v.(int) != 30 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 2 || v.(int) != 20 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 1 || v.(int) != 10 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 0 || v.(int) != 00 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != tsdb.EOF {
		t.Fatalf("expected eof, got: %x / %x", k, v)
	}
}
Exemple #2
0
// Ensure the multi-cursor can correctly iterate across multiple overlapping subcursors.
func TestMultiCursor_Multiple_Overlapping(t *testing.T) {
	mc := tsdb.MultiCursor(
		NewCursor([]CursorItem{
			{Key: 0, Value: 0},
			{Key: 3, Value: 3},
			{Key: 4, Value: 4},
		}, true),
		NewCursor([]CursorItem{
			{Key: 0, Value: 0xF0},
			{Key: 2, Value: 0xF2},
			{Key: 4, Value: 0xF4},
		}, true),
	)

	if k, v := mc.SeekTo(0); k != 0 || v.(int) != 0 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 2 || v.(int) != 0xF2 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 3 || v.(int) != 3 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 4 || v.(int) != 4 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != tsdb.EOF {
		t.Fatalf("expected eof, got: %x / %x", k, v)
	}
}
Exemple #3
0
// Ensure the multi-cursor can correctly iterate across a single subcursor in reverse order.
func TestMultiCursor_Single_Reverse(t *testing.T) {
	mc := tsdb.MultiCursor(NewCursor([]CursorItem{
		{Key: 0, Value: 0},
		{Key: 1, Value: 10},
		{Key: 2, Value: 20},
	}, false))

	if k, v := mc.SeekTo(2); k != 2 || v.(int) != 20 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 1 || v.(int) != 10 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != 0 || v.(int) != 0 {
		t.Fatalf("unexpected key/value: %x / %x", k, v)
	} else if k, v = mc.Next(); k != tsdb.EOF {
		t.Fatalf("expected eof, got: %x / %x", k, v)
	}
}
Exemple #4
0
// Ensure the multi-cursor can handle randomly generated data.
func TestMultiCursor_Quick(t *testing.T) {
	quick.Check(func(useek uint64, cursors []Cursor) bool {
		var got, exp []CursorItem
		seek := int64(useek) % 100

		// Merge all cursor data to determine expected output.
		// First seen key overrides all other items with the same key.
		m := make(map[int64]CursorItem)
		for _, c := range cursors {
			for _, item := range c.items {
				if item.Key < seek {
					continue
				}
				if _, ok := m[item.Key]; ok {
					continue
				}
				m[item.Key] = item
			}
		}

		// Convert map back to single item list.
		for _, item := range m {
			exp = append(exp, item)
		}
		sort.Sort(CursorItems(exp))

		// Create multi-cursor and iterate over all items.
		mc := tsdb.MultiCursor(tsdbCursorSlice(cursors)...)
		for k, v := mc.SeekTo(seek); k != tsdb.EOF; k, v = mc.Next() {
			got = append(got, CursorItem{k, v.(int)})
		}

		// Verify results.
		if !reflect.DeepEqual(got, exp) {
			t.Fatalf("mismatch: seek=%d\n\ngot=%+v\n\nexp=%+v", seek, got, exp)
		}

		return true
	}, nil)
}