@@ -17,13 +17,13 @@ var errOutOfBounds = errors.New("out of bounds")
1717type smallChunk struct {
1818 chunkenc.XORChunk
1919 start int64
20- end int64
2120}
2221
2322// bigchunk is a set of prometheus/tsdb chunks. It grows over time and has no
2423// upperbound on number of samples it can contain.
2524type bigchunk struct {
2625 chunks []smallChunk
26+ end int64
2727
2828 appender chunkenc.Appender
2929 remainingSamples int
@@ -45,7 +45,7 @@ func (b *bigchunk) Add(sample model.SamplePair) ([]Chunk, error) {
4545
4646 b .appender .Append (int64 (sample .Timestamp ), float64 (sample .Value ))
4747 b .remainingSamples --
48- b .chunks [ len ( b . chunks ) - 1 ]. end = int64 (sample .Timestamp )
48+ b .end = int64 (sample .Timestamp )
4949 return []Chunk {b }, nil
5050}
5151
@@ -70,7 +70,6 @@ func (b *bigchunk) addNextChunk(start model.Time) error {
7070 b .chunks = append (b .chunks , smallChunk {
7171 XORChunk : * chunkenc .NewXORChunk (),
7272 start : int64 (start ),
73- end : int64 (start ),
7473 })
7574
7675 appender , err := b .chunks [len (b .chunks )- 1 ].Appender ()
@@ -129,16 +128,15 @@ func (b *bigchunk) UnmarshalFromBuf(buf []byte) error {
129128 return err
130129 }
131130
132- var start , end int64
133- start , end , reuseIter , err = firstAndLastTimes (chunk , reuseIter )
131+ var start int64
132+ start , reuseIter , err = firstTime (chunk , reuseIter )
134133 if err != nil {
135134 return err
136135 }
137136
138137 b .chunks = append (b .chunks , smallChunk {
139138 XORChunk : * chunk .(* chunkenc.XORChunk ),
140139 start : int64 (start ),
141- end : int64 (end ),
142140 })
143141 }
144142 return nil
@@ -195,8 +193,8 @@ func (b *bigchunk) NewIterator(reuseIter Iterator) Iterator {
195193func (b * bigchunk ) Slice (start , end model.Time ) Chunk {
196194 i , j := 0 , len (b .chunks )
197195 for k := 0 ; k < len (b .chunks ); k ++ {
198- if b .chunks [k ].end < int64 (start ) {
199- i = k + 1
196+ if b .chunks [k ].start <= int64 (start ) {
197+ i = k
200198 }
201199 if b .chunks [k ].start > int64 (end ) {
202200 j = k
@@ -250,22 +248,19 @@ type bigchunkIterator struct {
250248}
251249
252250func (it * bigchunkIterator ) FindAtOrAfter (target model.Time ) bool {
253- if it .i >= len (it .chunks ) {
251+ if it .i >= len (it .chunks ) || int64 ( target ) > it . end {
254252 return false
255253 }
256254
257255 // If the seek is outside the current chunk, use the index to find the right
258256 // chunk.
259- if int64 (target ) < it .chunks [it .i ].start || int64 (target ) > it .chunks [it .i ].end {
257+ if int64 (target ) < it .chunks [it .i ].start ||
258+ (it .i + 1 < len (it .chunks ) && int64 (target ) >= it .chunks [it .i + 1 ].start ) {
260259 it .curr = nil
261- for it .i = 0 ; it .i < len (it .chunks ) && int64 (target ) > it .chunks [it .i ]. end ; it .i ++ {
260+ for it .i = 0 ; it .i + 1 < len (it .chunks ) && int64 (target ) >= it .chunks [it .i + 1 ]. start ; it .i ++ {
262261 }
263262 }
264263
265- if it .i >= len (it .chunks ) {
266- return false
267- }
268-
269264 if it .curr == nil {
270265 it .curr = it .chunks [it .i ].Iterator (it .curr )
271266 } else if t , _ := it .curr .At (); int64 (target ) <= t {
@@ -331,20 +326,11 @@ func (it *bigchunkIterator) Err() error {
331326 return nil
332327}
333328
334- func firstAndLastTimes (c chunkenc.Chunk , iter chunkenc.Iterator ) (int64 , int64 , chunkenc.Iterator , error ) {
335- var (
336- first int64
337- last int64
338- firstSet bool
339- )
329+ func firstTime (c chunkenc.Chunk , iter chunkenc.Iterator ) (int64 , chunkenc.Iterator , error ) {
330+ var first int64
340331 iter = c .Iterator (iter )
341- for iter .Next () {
342- t , _ := iter .At ()
343- if ! firstSet {
344- first = t
345- firstSet = true
346- }
347- last = t
332+ if iter .Next () {
333+ first , _ = iter .At ()
348334 }
349- return first , last , iter , iter .Err ()
335+ return first , iter , iter .Err ()
350336}
0 commit comments