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