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

     1  package iterators
     2  
     3  import (
     4  	"github.com/prometheus/common/model"
     5  
     6  	"github.com/cortexproject/cortex/pkg/chunk"
     7  	promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
     8  )
     9  
    10  type chunkIterator struct {
    11  	chunk.Chunk
    12  	it promchunk.Iterator
    13  
    14  	// At() is called often in the heap code, so caching its result seems like
    15  	// a good idea.
    16  	cacheValid  bool
    17  	cachedTime  int64
    18  	cachedValue float64
    19  }
    20  
    21  // Seek advances the iterator forward to the value at or after
    22  // the given timestamp.
    23  func (i *chunkIterator) Seek(t int64) bool {
    24  	i.cacheValid = false
    25  
    26  	// We assume seeks only care about a specific window; if this chunk doesn't
    27  	// contain samples in that window, we can shortcut.
    28  	if int64(i.Through) < t {
    29  		return false
    30  	}
    31  
    32  	return i.it.FindAtOrAfter(model.Time(t))
    33  }
    34  
    35  func (i *chunkIterator) AtTime() int64 {
    36  	if i.cacheValid {
    37  		return i.cachedTime
    38  	}
    39  
    40  	v := i.it.Value()
    41  	i.cachedTime, i.cachedValue = int64(v.Timestamp), float64(v.Value)
    42  	i.cacheValid = true
    43  	return i.cachedTime
    44  }
    45  
    46  func (i *chunkIterator) At() (int64, float64) {
    47  	if i.cacheValid {
    48  		return i.cachedTime, i.cachedValue
    49  	}
    50  
    51  	v := i.it.Value()
    52  	i.cachedTime, i.cachedValue = int64(v.Timestamp), float64(v.Value)
    53  	i.cacheValid = true
    54  	return i.cachedTime, i.cachedValue
    55  }
    56  
    57  func (i *chunkIterator) Next() bool {
    58  	i.cacheValid = false
    59  	return i.it.Scan()
    60  }
    61  
    62  func (i *chunkIterator) Err() error {
    63  	return i.it.Err()
    64  }