@@ -17,13 +17,13 @@ var errOutOfBounds = errors.New("out of bounds")
17
17
type smallChunk struct {
18
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
24
23
// upperbound on number of samples it can contain.
25
24
type bigchunk struct {
26
25
chunks []smallChunk
26
+ end int64
27
27
28
28
appender chunkenc.Appender
29
29
remainingSamples int
@@ -45,7 +45,7 @@ func (b *bigchunk) Add(sample model.SamplePair) ([]Chunk, error) {
45
45
46
46
b .appender .Append (int64 (sample .Timestamp ), float64 (sample .Value ))
47
47
b .remainingSamples --
48
- b .chunks [ len ( b . chunks ) - 1 ]. end = int64 (sample .Timestamp )
48
+ b .end = int64 (sample .Timestamp )
49
49
return []Chunk {b }, nil
50
50
}
51
51
@@ -70,7 +70,6 @@ func (b *bigchunk) addNextChunk(start model.Time) error {
70
70
b .chunks = append (b .chunks , smallChunk {
71
71
XORChunk : * chunkenc .NewXORChunk (),
72
72
start : int64 (start ),
73
- end : int64 (start ),
74
73
})
75
74
76
75
appender , err := b .chunks [len (b .chunks )- 1 ].Appender ()
@@ -128,15 +127,14 @@ func (b *bigchunk) UnmarshalFromBuf(buf []byte) error {
128
127
return err
129
128
}
130
129
131
- start , end , err := firstAndLastTimes (chunk )
130
+ start , err := firstTime (chunk )
132
131
if err != nil {
133
132
return err
134
133
}
135
134
136
135
b .chunks = append (b .chunks , smallChunk {
137
136
XORChunk : * chunk .(* chunkenc.XORChunk ),
138
137
start : int64 (start ),
139
- end : int64 (end ),
140
138
})
141
139
}
142
140
return nil
@@ -183,8 +181,8 @@ func (b *bigchunk) NewIterator() Iterator {
183
181
func (b * bigchunk ) Slice (start , end model.Time ) Chunk {
184
182
i , j := 0 , len (b .chunks )
185
183
for k := 0 ; k < len (b .chunks ); k ++ {
186
- if b .chunks [k ].end < int64 (start ) {
187
- i = k + 1
184
+ if b .chunks [k ].start <= int64 (start ) {
185
+ i = k
188
186
}
189
187
if b .chunks [k ].start > int64 (end ) {
190
188
j = k
@@ -238,22 +236,19 @@ type bigchunkIterator struct {
238
236
}
239
237
240
238
func (it * bigchunkIterator ) FindAtOrAfter (target model.Time ) bool {
241
- if it .i >= len (it .chunks ) {
239
+ if it .i >= len (it .chunks ) || int64 ( target ) > it . end {
242
240
return false
243
241
}
244
242
245
243
// If the seek is outside the current chunk, use the index to find the right
246
244
// chunk.
247
- if int64 (target ) < it .chunks [it .i ].start || int64 (target ) > it .chunks [it .i ].end {
245
+ if int64 (target ) < it .chunks [it .i ].start ||
246
+ (it .i + 1 < len (it .chunks ) && int64 (target ) >= it .chunks [it .i + 1 ].start ) {
248
247
it .curr = nil
249
- for it .i = 0 ; it .i < len (it .chunks ) && int64 (target ) > it .chunks [it .i ]. end ; it .i ++ {
248
+ for it .i = 0 ; it .i + 1 < len (it .chunks ) && int64 (target ) >= it .chunks [it .i + 1 ]. start ; it .i ++ {
250
249
}
251
250
}
252
251
253
- if it .i >= len (it .chunks ) {
254
- return false
255
- }
256
-
257
252
if it .curr == nil {
258
253
it .curr = it .chunks [it .i ].Iterator ()
259
254
} else if t , _ := it .curr .At (); int64 (target ) <= t {
@@ -319,20 +314,13 @@ func (it *bigchunkIterator) Err() error {
319
314
return nil
320
315
}
321
316
322
- func firstAndLastTimes (c chunkenc.Chunk ) (int64 , int64 , error ) {
317
+ func firstTime (c chunkenc.Chunk ) (int64 , error ) {
323
318
var (
324
- first int64
325
- last int64
326
- firstSet bool
327
- iter = c .Iterator ()
319
+ first int64
320
+ iter = c .Iterator ()
328
321
)
329
- for iter .Next () {
330
- t , _ := iter .At ()
331
- if ! firstSet {
332
- first = t
333
- firstSet = true
334
- }
335
- last = t
322
+ if iter .Next () {
323
+ first , _ = iter .At ()
336
324
}
337
- return first , last , iter .Err ()
325
+ return first , iter .Err ()
338
326
}
0 commit comments