diff --git a/chunks/chunk.go b/chunks/chunk.go index eac7c06f57..ee0b4c5972 100644 --- a/chunks/chunk.go +++ b/chunks/chunk.go @@ -44,7 +44,7 @@ func FromData(e Encoding, d []byte) (Chunk, error) { switch e { case EncXOR: return &XORChunk{ - b: &bstream{count: 8}, + b: &bstream{count: 0, stream: d}, num: binary.BigEndian.Uint16(d), }, nil } diff --git a/querier.go b/querier.go index 4581480da3..26f839be1f 100644 --- a/querier.go +++ b/querier.go @@ -2,6 +2,7 @@ package tsdb import ( "fmt" + "math" "sort" "strings" @@ -657,14 +658,17 @@ func (it *chunkSeriesIterator) Err() error { type BufferedSeriesIterator struct { it SeriesIterator buf *sampleRing + + lastTime int64 } // NewBuffer returns a new iterator that buffers the values within the time range // of the current element and the duration of delta before. func NewBuffer(it SeriesIterator, delta int64) *BufferedSeriesIterator { return &BufferedSeriesIterator{ - it: it, - buf: newSampleRing(delta, 16), + it: it, + buf: newSampleRing(delta, 16), + lastTime: math.MinInt64, } } @@ -676,14 +680,13 @@ func (b *BufferedSeriesIterator) PeekBack() (t int64, v float64, ok bool) { // Seek advances the iterator to the element at time t or greater. func (b *BufferedSeriesIterator) Seek(t int64) bool { - tcur, _ := b.it.Values() - t0 := t - b.buf.delta + // If the delta would cause us to seek backwards, preserve the buffer // and just continue regular advancment while filling the buffer on the way. - if t0 <= tcur { + if t0 <= b.lastTime { for b.Next() { - if tcur, _ = b.it.Values(); tcur >= t { + if tcur, _ := b.it.Values(); tcur >= t { return true } } @@ -711,7 +714,9 @@ func (b *BufferedSeriesIterator) Next() bool { // Add current element to buffer before advancing. b.buf.add(b.it.Values()) - return b.it.Next() + ok := b.it.Next() + b.lastTime, _ = b.Values() + return ok } // Values returns the current element of the iterator. diff --git a/reader.go b/reader.go index a9d933b6f9..4621e87d4b 100644 --- a/reader.go +++ b/reader.go @@ -247,9 +247,9 @@ func (r *indexReader) Series(ref uint32, mint, maxt int64) (Series, error) { } b := r.b[int(ref)+n:] - offsets := make([]uint32, 0, k) + offsets := make([]uint32, 0, 2*k) - for i := 0; i < int(k); i++ { + for i := 0; i < 2*int(k); i++ { o, n := binary.Uvarint(b) if n < 1 { return nil, errors.Wrap(errInvalidSize, "symbol offset")