@@ -15,9 +15,8 @@ const samplesPerChunk = 120
15
15
var errOutOfBounds = errors .New ("out of bounds" )
16
16
17
17
type smallChunk struct {
18
- * chunkenc.XORChunk
18
+ chunkenc.XORChunk
19
19
start int64
20
- end int64
21
20
}
22
21
23
22
// bigchunk is a set of prometheus/tsdb chunks. It grows over time and has no
@@ -45,7 +44,6 @@ func (b *bigchunk) Add(sample model.SamplePair) ([]Chunk, error) {
45
44
46
45
b .appender .Append (int64 (sample .Timestamp ), float64 (sample .Value ))
47
46
b .remainingSamples --
48
- b .chunks [len (b .chunks )- 1 ].end = int64 (sample .Timestamp )
49
47
return []Chunk {b }, nil
50
48
}
51
49
@@ -63,22 +61,19 @@ func (b *bigchunk) addNextChunk(start model.Time) error {
63
61
if err != nil {
64
62
return err
65
63
}
66
- b .chunks [l - 1 ].XORChunk = compacted .(* chunkenc.XORChunk )
64
+ b .chunks [l - 1 ].XORChunk = * compacted .(* chunkenc.XORChunk )
67
65
}
68
66
}
69
67
70
- chunk := chunkenc .NewXORChunk ()
71
- appender , err := chunk .Appender ()
72
- if err != nil {
73
- return err
74
- }
75
-
76
68
b .chunks = append (b .chunks , smallChunk {
77
- XORChunk : chunk ,
69
+ XORChunk : * chunkenc . NewXORChunk () ,
78
70
start : int64 (start ),
79
- end : int64 (start ),
80
71
})
81
72
73
+ appender , err := b .chunks [len (b .chunks )- 1 ].Appender ()
74
+ if err != nil {
75
+ return err
76
+ }
82
77
b .appender = appender
83
78
b .remainingSamples = samplesPerChunk
84
79
return nil
@@ -131,16 +126,15 @@ func (b *bigchunk) UnmarshalFromBuf(buf []byte) error {
131
126
return err
132
127
}
133
128
134
- var start , end int64
135
- start , end , reuseIter , err = firstAndLastTimes (chunk , reuseIter )
129
+ var start int64
130
+ start , reuseIter , err = firstTime (chunk , reuseIter )
136
131
if err != nil {
137
132
return err
138
133
}
139
134
140
135
b .chunks = append (b .chunks , smallChunk {
141
- XORChunk : chunk .(* chunkenc.XORChunk ),
136
+ XORChunk : * chunk .(* chunkenc.XORChunk ),
142
137
start : int64 (start ),
143
- end : int64 (end ),
144
138
})
145
139
}
146
140
return nil
@@ -197,8 +191,8 @@ func (b *bigchunk) NewIterator(reuseIter Iterator) Iterator {
197
191
func (b * bigchunk ) Slice (start , end model.Time ) Chunk {
198
192
i , j := 0 , len (b .chunks )
199
193
for k := 0 ; k < len (b .chunks ); k ++ {
200
- if b .chunks [k ].end < int64 (start ) {
201
- i = k + 1
194
+ if b .chunks [k ].start <= int64 (start ) {
195
+ i = k
202
196
}
203
197
if b .chunks [k ].start > int64 (end ) {
204
198
j = k
@@ -258,16 +252,13 @@ func (it *bigchunkIterator) FindAtOrAfter(target model.Time) bool {
258
252
259
253
// If the seek is outside the current chunk, use the index to find the right
260
254
// chunk.
261
- if int64 (target ) < it .chunks [it .i ].start || int64 (target ) > it .chunks [it .i ].end {
255
+ if int64 (target ) < it .chunks [it .i ].start ||
256
+ (it .i + 1 < len (it .chunks ) && int64 (target ) >= it .chunks [it .i + 1 ].start ) {
262
257
it .curr = nil
263
- for it .i = 0 ; it .i < len (it .chunks ) && int64 (target ) > it .chunks [it .i ]. end ; it .i ++ {
258
+ for it .i = 0 ; it .i + 1 < len (it .chunks ) && int64 (target ) >= it .chunks [it .i + 1 ]. start ; it .i ++ {
264
259
}
265
260
}
266
261
267
- if it .i >= len (it .chunks ) {
268
- return false
269
- }
270
-
271
262
if it .curr == nil {
272
263
it .curr = it .chunks [it .i ].Iterator (it .curr )
273
264
} else if t , _ := it .curr .At (); int64 (target ) <= t {
@@ -280,6 +271,14 @@ func (it *bigchunkIterator) FindAtOrAfter(target model.Time) bool {
280
271
return true
281
272
}
282
273
}
274
+ // Timestamp is after the end of that chunk - if there is another chunk
275
+ // then the position we need is at the beginning of it.
276
+ if it .i + 1 < len (it .chunks ) {
277
+ it .i ++
278
+ it .curr = it .chunks [it .i ].Iterator (it .curr )
279
+ it .curr .Next ()
280
+ return true
281
+ }
283
282
return false
284
283
}
285
284
@@ -333,20 +332,11 @@ func (it *bigchunkIterator) Err() error {
333
332
return nil
334
333
}
335
334
336
- func firstAndLastTimes (c chunkenc.Chunk , iter chunkenc.Iterator ) (int64 , int64 , chunkenc.Iterator , error ) {
337
- var (
338
- first int64
339
- last int64
340
- firstSet bool
341
- )
335
+ func firstTime (c chunkenc.Chunk , iter chunkenc.Iterator ) (int64 , chunkenc.Iterator , error ) {
336
+ var first int64
342
337
iter = c .Iterator (iter )
343
- for iter .Next () {
344
- t , _ := iter .At ()
345
- if ! firstSet {
346
- first = t
347
- firstSet = true
348
- }
349
- last = t
338
+ if iter .Next () {
339
+ first , _ = iter .At ()
350
340
}
351
- return first , last , iter , iter .Err ()
341
+ return first , iter , iter .Err ()
352
342
}
0 commit comments