-
Notifications
You must be signed in to change notification settings - Fork 810
/
Copy pathmerge.go
188 lines (151 loc) · 4.1 KB
/
merge.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package batch
import (
"container/heap"
"sort"
promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
)
type mergeIterator struct {
its []*nonOverlappingIterator
h iteratorHeap
// Store the current sorted batchStream
batches batchStream
// Buffers to merge in.
batchesBuf batchStream
nextBatchBuf [1]promchunk.Batch
currErr error
}
func newMergeIterator(cs []GenericChunk) *mergeIterator {
css := partitionChunks(cs)
its := make([]*nonOverlappingIterator, 0, len(css))
for _, cs := range css {
its = append(its, newNonOverlappingIterator(cs))
}
c := &mergeIterator{
its: its,
h: make(iteratorHeap, 0, len(its)),
batches: make(batchStream, 0, len(its)*2*promchunk.BatchSize),
batchesBuf: make(batchStream, len(its)*2*promchunk.BatchSize),
}
for _, iter := range c.its {
if iter.Next(1) {
c.h = append(c.h, iter)
continue
}
if err := iter.Err(); err != nil {
c.currErr = err
}
}
heap.Init(&c.h)
return c
}
func (c *mergeIterator) Seek(t int64, size int) bool {
// Optimisation to see if the seek is within our current caches batches.
found:
for len(c.batches) > 0 {
batch := &c.batches[0]
if t >= batch.Timestamps[0] && t <= batch.Timestamps[batch.Length-1] {
batch.Index = 0
for batch.Index < batch.Length && t > batch.Timestamps[batch.Index] {
batch.Index++
}
break found
}
copy(c.batches, c.batches[1:])
c.batches = c.batches[:len(c.batches)-1]
}
// If we didn't find anything in the current set of batches, reset the heap
// and seek.
if len(c.batches) == 0 {
c.h = c.h[:0]
c.batches = c.batches[:0]
for _, iter := range c.its {
if iter.Seek(t, size) {
c.h = append(c.h, iter)
continue
}
if err := iter.Err(); err != nil {
c.currErr = err
return false
}
}
heap.Init(&c.h)
}
return c.buildNextBatch(size)
}
func (c *mergeIterator) Next(size int) bool {
// Pop the last built batch in a way that doesn't extend the slice.
if len(c.batches) > 0 {
copy(c.batches, c.batches[1:])
c.batches = c.batches[:len(c.batches)-1]
}
return c.buildNextBatch(size)
}
func (c *mergeIterator) nextBatchEndTime() int64 {
batch := &c.batches[0]
return batch.Timestamps[batch.Length-1]
}
func (c *mergeIterator) buildNextBatch(size int) bool {
// All we need to do is get enough batches that our first batch's last entry
// is before all iterators next entry.
for len(c.h) > 0 && (len(c.batches) == 0 || c.nextBatchEndTime() >= c.h[0].AtTime()) {
c.nextBatchBuf[0] = c.h[0].Batch()
c.batchesBuf = mergeStreams(c.batches, c.nextBatchBuf[:], c.batchesBuf, size)
copy(c.batches[:len(c.batchesBuf)], c.batchesBuf)
c.batches = c.batches[:len(c.batchesBuf)]
if c.h[0].Next(size) {
heap.Fix(&c.h, 0)
} else {
heap.Pop(&c.h)
}
}
return len(c.batches) > 0
}
func (c *mergeIterator) AtTime() int64 {
return c.batches[0].Timestamps[0]
}
func (c *mergeIterator) Batch() promchunk.Batch {
return c.batches[0]
}
func (c *mergeIterator) Err() error {
return c.currErr
}
type iteratorHeap []iterator
func (h *iteratorHeap) Len() int { return len(*h) }
func (h *iteratorHeap) Swap(i, j int) { (*h)[i], (*h)[j] = (*h)[j], (*h)[i] }
func (h *iteratorHeap) Less(i, j int) bool {
iT := (*h)[i].AtTime()
jT := (*h)[j].AtTime()
return iT < jT
}
func (h *iteratorHeap) Push(x interface{}) {
*h = append(*h, x.(iterator))
}
func (h *iteratorHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// Build a list of lists of non-overlapping chunks.
func partitionChunks(cs []GenericChunk) [][]GenericChunk {
sort.Sort(byMinTime(cs))
css := [][]GenericChunk{}
outer:
for _, c := range cs {
for i, cs := range css {
if cs[len(cs)-1].MaxTime < c.MinTime {
css[i] = append(css[i], c)
continue outer
}
}
cs := make([]GenericChunk, 0, len(cs)/(len(css)+1))
cs = append(cs, c)
css = append(css, cs)
}
return css
}
type byMinTime []GenericChunk
func (b byMinTime) Len() int { return len(b) }
func (b byMinTime) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byMinTime) Less(i, j int) bool { return b[i].MinTime < b[j].MinTime }