-
Notifications
You must be signed in to change notification settings - Fork 787
/
chunk.go
74 lines (62 loc) · 1.69 KB
/
chunk.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
65
66
67
68
69
70
71
72
73
74
package batch
import (
"github.com/prometheus/common/model"
promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
)
// chunkIterator implement batchIterator over a chunk. Its is designed to be
// reused by calling reset() with a fresh chunk.
type chunkIterator struct {
chunk GenericChunk
it promchunk.Iterator
batch promchunk.Batch
}
func (i *chunkIterator) reset(chunk GenericChunk) {
i.chunk = chunk
i.it = chunk.Iterator(i.it)
i.batch.Length = 0
i.batch.Index = 0
}
func (i *chunkIterator) MaxCurrentChunkTime() int64 {
return i.chunk.MaxTime
}
// Seek advances the iterator forward to the value at or after
// the given timestamp.
func (i *chunkIterator) Seek(t int64, size int) bool {
// We assume seeks only care about a specific window; if this chunk doesn't
// contain samples in that window, we can shortcut.
if i.chunk.MaxTime < t {
return false
}
// If the seek is to the middle of the current batch, and size fits, we can
// shortcut.
if i.batch.Length > 0 && t >= i.batch.Timestamps[0] && t <= i.batch.Timestamps[i.batch.Length-1] {
i.batch.Index = 0
for i.batch.Index < i.batch.Length && t > i.batch.Timestamps[i.batch.Index] {
i.batch.Index++
}
if i.batch.Index+size < i.batch.Length {
return true
}
}
if i.it.FindAtOrAfter(model.Time(t)) {
i.batch = i.it.Batch(size)
return i.batch.Length > 0
}
return false
}
func (i *chunkIterator) Next(size int) bool {
if i.it.Scan() {
i.batch = i.it.Batch(size)
return i.batch.Length > 0
}
return false
}
func (i *chunkIterator) AtTime() int64 {
return i.batch.Timestamps[0]
}
func (i *chunkIterator) Batch() promchunk.Batch {
return i.batch
}
func (i *chunkIterator) Err() error {
return i.it.Err()
}