github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/querier/batch/chunk.go (about)

     1  package batch
     2  
     3  import (
     4  	"github.com/prometheus/common/model"
     5  
     6  	promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
     7  )
     8  
     9  // chunkIterator implement batchIterator over a chunk.  Its is designed to be
    10  // reused by calling reset() with a fresh chunk.
    11  type chunkIterator struct {
    12  	chunk GenericChunk
    13  	it    promchunk.Iterator
    14  	batch promchunk.Batch
    15  }
    16  
    17  func (i *chunkIterator) reset(chunk GenericChunk) {
    18  	i.chunk = chunk
    19  	i.it = chunk.Iterator(i.it)
    20  	i.batch.Length = 0
    21  	i.batch.Index = 0
    22  }
    23  
    24  // Seek advances the iterator forward to the value at or after
    25  // the given timestamp.
    26  func (i *chunkIterator) Seek(t int64, size int) bool {
    27  	// We assume seeks only care about a specific window; if this chunk doesn't
    28  	// contain samples in that window, we can shortcut.
    29  	if i.chunk.MaxTime < t {
    30  		return false
    31  	}
    32  
    33  	// If the seek is to the middle of the current batch, and size fits, we can
    34  	// shortcut.
    35  	if i.batch.Length > 0 && t >= i.batch.Timestamps[0] && t <= i.batch.Timestamps[i.batch.Length-1] {
    36  		i.batch.Index = 0
    37  		for i.batch.Index < i.batch.Length && t > i.batch.Timestamps[i.batch.Index] {
    38  			i.batch.Index++
    39  		}
    40  		if i.batch.Index+size < i.batch.Length {
    41  			return true
    42  		}
    43  	}
    44  
    45  	if i.it.FindAtOrAfter(model.Time(t)) {
    46  		i.batch = i.it.Batch(size)
    47  		return i.batch.Length > 0
    48  	}
    49  	return false
    50  }
    51  
    52  func (i *chunkIterator) Next(size int) bool {
    53  	if i.it.Scan() {
    54  		i.batch = i.it.Batch(size)
    55  		return i.batch.Length > 0
    56  	}
    57  	return false
    58  }
    59  
    60  func (i *chunkIterator) AtTime() int64 {
    61  	return i.batch.Timestamps[0]
    62  }
    63  
    64  func (i *chunkIterator) Batch() promchunk.Batch {
    65  	return i.batch
    66  }
    67  
    68  func (i *chunkIterator) Err() error {
    69  	return i.it.Err()
    70  }