-
Notifications
You must be signed in to change notification settings - Fork 66
/
merger.go
266 lines (249 loc) · 5.46 KB
/
merger.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2021 Michael Andersen
// Copyright (c) 2021 Regents of the University Of California
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package btrdb
import (
"sort"
"github.com/BTrDB/btrdb-server/bte"
"github.com/BTrDB/btrdb-server/qtree"
)
func mergeChangedRanges(parent chan ChangedRange, parentCE chan bte.BTE,
pqbuffer []ChangedRange) (chan ChangedRange, chan bte.BTE) {
var left, right *ChangedRange
pqbufferIndex := 0
//Fill left and right with an element
refill := func() bool {
found := false
if left == nil {
cr, ok := <-parent
if ok {
left = &cr
found = true
}
}
if right == nil {
if pqbufferIndex < len(pqbuffer) {
right = &pqbuffer[pqbufferIndex]
pqbufferIndex += 1
found = true
}
}
return found || (left != nil) || right != nil
}
rv := make(chan ChangedRange, 100)
rve := make(chan bte.BTE, 100)
intersect := func(l *ChangedRange, r *ChangedRange) (bool, *ChangedRange) {
if l.End < r.Start || r.End < l.Start {
return false, nil
}
// They intersect
minStart := l.Start
maxEnd := l.End
if r.Start < minStart {
minStart = r.Start
}
if r.End > maxEnd {
maxEnd = r.End
}
rv := &ChangedRange{minStart, maxEnd}
return true, rv
}
go func() {
var cur *ChangedRange
for {
//Check for errors
select {
case err := <-parentCE:
rve <- err
return
default:
}
//Refill left and right
more := refill()
if !more {
if cur != nil {
rv <- *cur
//end
close(rv)
return
}
}
//If no current element, fill it
if cur == nil {
if left != nil && right != nil {
//Select first from left/right
if left.Start < right.Start {
cur = left
left = nil
} else {
cur = right
right = nil
}
} else if left != nil {
//Must be left
cur = left
left = nil
} else {
//Must be right
cur = right
right = nil
}
//Refill left and right
continue
}
//We have a current
if left != nil {
bIs, Is := intersect(cur, left)
if bIs {
cur = Is
left = nil
continue
}
}
if right != nil {
bIs, Is := intersect(cur, right)
if bIs {
cur = Is
right = nil
continue
}
}
//Current does not intersect
rv <- *cur
cur = nil
continue
}
}() //end go()
return rv, rve
}
func mergeStatisticalWindowChannels(parent chan qtree.StatRecord, parentCE chan bte.BTE, pqbuffer []qtree.StatRecord) (chan qtree.StatRecord,
chan bte.BTE) {
rvc := make(chan qtree.StatRecord, 1000)
rve := make(chan bte.BTE, 2)
var parentValue qtree.StatRecord
var parentError bte.BTE
var parentExhausted bool
var hasCached bool
peekparent := func() (qtree.StatRecord, bool, bte.BTE) {
if hasCached {
return parentValue, parentExhausted, parentError
}
var ok bool
select {
case parentValue, ok = <-parent:
if !ok {
parentExhausted = true
}
case parentError = <-parentCE:
}
hasCached = true
return parentValue, parentExhausted, parentError
}
popparent := func() {
if parentExhausted {
return
}
hasCached = false
}
go func() {
for {
pv, fin, pe := peekparent()
if pe != nil {
rve <- pe
return
}
if fin {
//No more parent values, just return pqbuffer
if len(pqbuffer) > 0 {
rvc <- pqbuffer[0]
pqbuffer = pqbuffer[1:]
continue
} else {
close(rvc)
return
}
}
//There is a parent value
if len(pqbuffer) == 0 || pv.Time < pqbuffer[0].Time {
//return parent unmodified
rvc <- pv
popparent()
} else if pqbuffer[0].Time < pv.Time {
//return pq unmodified
rvc <- pqbuffer[0]
pqbuffer = pqbuffer[1:]
} else if pqbuffer[0].Time == pv.Time {
//Need to merge
v := pqbuffer[0]
pqbuffer = pqbuffer[1:]
if v.Count == 0 {
v.Max = pv.Max
v.Min = pv.Min
}
if pv.Max > v.Max {
v.Max = pv.Max
}
if pv.Min < v.Min {
v.Min = pv.Min
}
v.Mean = (v.Mean*float64(v.Count) + pv.Mean*float64(pv.Count)) / float64((v.Count + pv.Count))
v.Count += pv.Count
rvc <- v
popparent()
} else {
panic("PQM done messed up")
}
}
}()
return rvc, rve
}
//For all points in rz >= tCutoffStart and < tEnd, align into windows of width w starting from tStart
//END IS INCLUSIVE, so you need to subtract one for unaligned windows
func CreateStatWindows(rz []qtree.Record, tCutoffStart int64, tStart int64, tEnd int64, w uint64) []qtree.StatRecord {
wz := make(map[int64]qtree.StatRecord)
for _, r := range rz {
if r.Time < tCutoffStart {
continue
}
if r.Time > tEnd {
continue
}
windowIdx := (r.Time - tStart) / int64(w)
ex := wz[windowIdx]
ex.Time = windowIdx*int64(w) + tStart
if ex.Count == 0 {
ex.Min = r.Val
ex.Max = r.Val
ex.Mean = r.Val
ex.Count = 1
} else {
if r.Val < ex.Min {
ex.Min = r.Val
}
if r.Val > ex.Max {
ex.Max = r.Val
}
ex.Mean = (ex.Mean*float64(ex.Count) + r.Val) / float64(ex.Count+1)
ex.Count++
}
wz[windowIdx] = ex
}
rv := make([]qtree.StatRecord, 0, len(wz))
for _, sr := range wz {
rv = append(rv, sr)
}
sort.Sort(StatRecordSlice(rv))
return rv
}
type StatRecordSlice []qtree.StatRecord
func (srs StatRecordSlice) Len() int {
return len(srs)
}
func (srs StatRecordSlice) Less(i, j int) bool {
return srs[i].Time < srs[j].Time
}
func (srs StatRecordSlice) Swap(i, j int) {
srs[i], srs[j] = srs[j], srs[i]
}