Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 220c3dd

Browse files
committed
Added tests to xor iterator seek.
Signed-off-by: Bartek Plotka <[email protected]>
1 parent f09fb89 commit 220c3dd

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

chunkenc/chunk.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ type Appender interface {
5858

5959
// Iterator iterates over the data of a time series.
6060
type Iterator interface {
61-
// Seek advances the iterator forward to the given timestamp.
62-
// If there's no value exactly at t, it advances to the first value
63-
// after t.
61+
// Seek advances the iterator forward to the sample with the timestamp t or first value after t.
62+
// If the current iterator points to the sample with timestamp after t already,
63+
// Seek should not advance the iterator.
64+
// Seek returns false if there is no such sample with the timestamp equal or larger than t.
6465
Seek(t int64) bool
6566
// At returns the current timestamp/value pair.
6667
At() (int64, float64)

chunkenc/chunk_test.go

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"fmt"
1818
"io"
1919
"math/rand"
20-
"reflect"
2120
"testing"
2221

2322
"github.com/prometheus/tsdb/testutil"
@@ -35,28 +34,23 @@ func TestChunk(t *testing.T) {
3534
t.Run(fmt.Sprintf("%v", enc), func(t *testing.T) {
3635
for range make([]struct{}, 1) {
3736
c := nc()
38-
if err := testChunk(c); err != nil {
39-
t.Fatal(err)
40-
}
37+
testChunk(t, c)
4138
}
4239
})
4340
}
4441
}
4542

46-
func testChunk(c Chunk) error {
43+
func testChunk(t *testing.T, c Chunk) {
4744
app, err := c.Appender()
48-
if err != nil {
49-
return err
50-
}
45+
testutil.Ok(t, err)
5146

52-
var exp []pair
47+
var all []pair
5348
var (
5449
ts = int64(1234123324)
5550
v = 1243535.123
5651
)
5752
for i := 0; i < 300; i++ {
5853
ts += int64(rand.Intn(10000) + 1)
59-
// v = rand.Float64()
6054
if i%2 == 0 {
6155
v += float64(rand.Intn(1000000))
6256
} else {
@@ -67,29 +61,53 @@ func testChunk(c Chunk) error {
6761
// appending to a partially filled chunk.
6862
if i%10 == 0 {
6963
app, err = c.Appender()
70-
if err != nil {
71-
return err
72-
}
64+
testutil.Ok(t, err)
7365
}
7466

7567
app.Append(ts, v)
76-
exp = append(exp, pair{t: ts, v: v})
77-
// fmt.Println("appended", len(c.Bytes()), c.Bytes())
68+
all = append(all, pair{t: ts, v: v})
7869
}
7970

80-
it := c.Iterator(nil)
81-
var res []pair
82-
for it.Next() {
83-
ts, v := it.At()
84-
res = append(res, pair{t: ts, v: v})
71+
// 1. Expand iterator in simple case.
72+
it1 := c.Iterator(nil)
73+
var res1 []pair
74+
for it1.Next() {
75+
ts, v := it1.At()
76+
res1 = append(res1, pair{t: ts, v: v})
8577
}
86-
if it.Err() != nil {
87-
return it.Err()
78+
testutil.Ok(t, it1.Err())
79+
testutil.Equals(t, all, res1)
80+
81+
// 2. Expand second iterator while reusing first one.
82+
it2 := c.Iterator(it1)
83+
var res2 []pair
84+
for it2.Next() {
85+
ts, v := it2.At()
86+
res2 = append(res2, pair{t: ts, v: v})
8887
}
89-
if !reflect.DeepEqual(exp, res) {
90-
return fmt.Errorf("unexpected result\n\ngot: %v\n\nexp: %v", res, exp)
88+
testutil.Ok(t, it2.Err())
89+
testutil.Equals(t, all, res2)
90+
91+
// 3. Test Iterator Seek.
92+
mid := len(all) / 2
93+
94+
it3 := c.Iterator(nil)
95+
var res3 []pair
96+
testutil.Equals(t, true, it3.Seek(all[mid].t))
97+
// Below ones should not matter.
98+
testutil.Equals(t, true, it3.Seek(all[mid].t))
99+
testutil.Equals(t, true, it3.Seek(all[mid].t))
100+
ts, v = it3.At()
101+
res3 = append(res3, pair{t: ts, v: v})
102+
103+
for it3.Next() {
104+
ts, v := it3.At()
105+
res3 = append(res3, pair{t: ts, v: v})
91106
}
92-
return nil
107+
testutil.Ok(t, it3.Err())
108+
testutil.Equals(t, all[mid:], res3)
109+
110+
testutil.Equals(t, false, it3.Seek(all[len(all)-1].t + 1))
93111
}
94112

95113
func benchmarkIterator(b *testing.B, newChunk func() Chunk) {

chunkenc/xor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,16 @@ type xorIterator struct {
242242
}
243243

244244
func (it *xorIterator) Seek(t int64) bool {
245-
if t >= it.t {
245+
if it.err != nil {
246246
return false
247247
}
248248

249-
for it.Next() {
250-
if t >= it.t {
251-
return true
249+
for t > it.t || it.numRead == 0 {
250+
if !it.Next() {
251+
return false
252252
}
253253
}
254-
return false
254+
return true
255255
}
256256

257257
func (it *xorIterator) At() (int64, float64) {

querier_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ func TestSeriesIterator(t *testing.T) {
924924
if tc.success {
925925
// Init the list and then proceed to check.
926926
remaining := exp.Next()
927-
testutil.Assert(t, remaining == true, "")
927+
testutil.Assert(t, remaining, "")
928928

929929
for remaining {
930930
sExp, eExp := exp.At()
@@ -1034,7 +1034,7 @@ func TestSeriesIterator(t *testing.T) {
10341034
if tc.success {
10351035
// Init the list and then proceed to check.
10361036
remaining := exp.Next()
1037-
testutil.Assert(t, remaining == true, "")
1037+
testutil.Assert(t, remaining, "")
10381038

10391039
for remaining {
10401040
sExp, eExp := exp.At()
@@ -1101,8 +1101,8 @@ func TestChunkSeriesIterator_DoubleSeek(t *testing.T) {
11011101
}
11021102

11031103
res := newChunkSeriesIterator(chkMetas, nil, 2, 8)
1104-
testutil.Assert(t, res.Seek(1) == true, "")
1105-
testutil.Assert(t, res.Seek(2) == true, "")
1104+
testutil.Assert(t, res.Seek(1), "")
1105+
testutil.Assert(t, res.Seek(2), "")
11061106
ts, v := res.At()
11071107
testutil.Equals(t, int64(2), ts)
11081108
testutil.Equals(t, float64(2), v)
@@ -1119,12 +1119,12 @@ func TestChunkSeriesIterator_SeekInCurrentChunk(t *testing.T) {
11191119

11201120
it := newChunkSeriesIterator(metas, nil, 1, 7)
11211121

1122-
testutil.Assert(t, it.Next() == true, "")
1122+
testutil.Assert(t, it.Next(), "")
11231123
ts, v := it.At()
11241124
testutil.Equals(t, int64(1), ts)
11251125
testutil.Equals(t, float64(2), v)
11261126

1127-
testutil.Assert(t, it.Seek(4) == true, "")
1127+
testutil.Assert(t, it.Seek(4), "")
11281128
ts, v = it.At()
11291129
testutil.Equals(t, int64(5), ts)
11301130
testutil.Equals(t, float64(6), v)

0 commit comments

Comments
 (0)