This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
query.go
570 lines (512 loc) · 11.4 KB
/
query.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package main
import (
"errors"
"fmt"
"log"
"math"
"strconv"
"sync/atomic"
"time"
"github.com/dustin/go-jsonpointer"
"github.com/dustin/gojson"
"github.com/mschoch/gouchstore"
)
var errTimeout = errors.New("query timed out")
type ptrval struct {
di *gouchstore.DocumentInfo
val interface{}
included bool
}
type reducer func(input chan ptrval) interface{}
type processOut struct {
cacheKey string
key int64
value []interface{}
err error
cacheOpaque uint32
}
func (p processOut) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{"v": p.value})
}
type processIn struct {
cacheKey string
dbname string
key int64
infos []*gouchstore.DocumentInfo
nextInfo *gouchstore.DocumentInfo
ptrs []string
reds []string
before time.Time
filters []string
filtervals []string
out chan<- *processOut
}
type queryIn struct {
dbname string
from string
to string
group int
ptrs []string
reds []string
start time.Time
before time.Time
filters []string
filtervals []string
started int32
totalKeys int32
out chan *processOut
cherr chan error
}
func resolveFetch(j []byte, keys []string) map[string]interface{} {
rv := map[string]interface{}{}
found, err := jsonpointer.FindMany(j, keys)
if err != nil {
return rv
}
for k, v := range found {
var val interface{}
err = json.Unmarshal(v, &val)
if err == nil {
rv[k] = val
}
}
return rv
}
func processDoc(di *gouchstore.DocumentInfo, chs []chan ptrval,
doc []byte, ptrs []string,
filters []string, filtervals []string,
included bool) {
pv := ptrval{di, nil, included}
// Find all keys for filters and comparisons so we can do a
// single pass through the document.
keys := make([]string, 0, len(filters)+len(ptrs))
seen := map[string]bool{}
for _, f := range filters {
if !seen[f] {
keys = append(keys, f)
seen[f] = true
}
}
for _, f := range ptrs {
if !seen[f] {
keys = append(keys, f)
seen[f] = true
}
}
fetched := resolveFetch(doc, keys)
for i, p := range filters {
val := fetched[p]
checkVal := filtervals[i]
switch val.(type) {
case string:
if val != checkVal {
return
}
case int, uint, int64, float64, uint64, bool:
v := fmt.Sprintf("%v", val)
if v != checkVal {
return
}
default:
return
}
}
for i, p := range ptrs {
val := fetched[p]
if p == "_id" {
val = di.ID
}
switch x := val.(type) {
case int, uint, int64, float64, uint64, bool:
v := fmt.Sprintf("%v", val)
pv.val = v
chs[i] <- pv
default:
pv.val = x
chs[i] <- pv
}
}
}
func processDocs(pi *processIn) {
result := processOut{pi.cacheKey, pi.key, nil, nil, 0}
if len(pi.ptrs) == 0 {
log.Panicf("No pointers specified in query: %#v", *pi)
}
db, err := dbopen(pi.dbname)
if err != nil {
result.err = err
pi.out <- &result
return
}
defer closeDBConn(db)
chans := make([]chan ptrval, 0, len(pi.ptrs))
resultchs := make([]chan interface{}, 0, len(pi.ptrs))
for i, r := range pi.reds {
chans = append(chans, make(chan ptrval))
resultchs = append(resultchs, make(chan interface{}))
go func(fi int, fr string) {
resultchs[fi] <- reducers[fr](chans[fi])
}(i, r)
}
go func() {
defer closeAll(chans)
dodoc := func(di *gouchstore.DocumentInfo, included bool) {
doc, err := db.DocumentByDocumentInfo(di)
if err == nil {
processDoc(di, chans, doc.Body, pi.ptrs,
pi.filters, pi.filtervals, included)
} else {
for i := range pi.ptrs {
chans[i] <- ptrval{di, nil, included}
}
}
}
for _, di := range pi.infos {
dodoc(di, true)
}
if pi.nextInfo != nil {
dodoc(pi.nextInfo, false)
}
}()
results := make([]interface{}, len(pi.ptrs))
for i := 0; i < len(pi.ptrs); i++ {
results[i] = <-resultchs[i]
if f, fok := results[i].(float64); fok &&
(math.IsNaN(f) || math.IsInf(f, 0)) {
results[i] = nil
}
}
result.value = results
if result.cacheOpaque == 0 && result.cacheKey != "" {
// It's OK if we can't store our newly pulled item in
// the cache, but it's most definitely not OK to stop
// here because of this.
select {
case cacheInputSet <- &result:
default:
}
}
pi.out <- &result
}
func docProcessor(ch <-chan *processIn) {
for pi := range ch {
if time.Now().Before(pi.before) {
processDocs(pi)
} else {
pi.out <- &processOut{"", pi.key, nil, errTimeout, 0}
}
}
}
func fetchDocs(dbname string, key int64, infos []*gouchstore.DocumentInfo,
nextInfo *gouchstore.DocumentInfo, ptrs []string, reds []string,
filters []string, filtervals []string,
before time.Time, out chan<- *processOut) {
i := processIn{"", dbname, key, infos, nextInfo,
ptrs, reds, before, filters, filtervals, out}
cacheInput <- &i
}
func runQuery(q *queryIn) {
if len(q.ptrs) == 0 {
q.cherr <- fmt.Errorf("at least one pointer is required")
return
}
if q.group == 0 {
q.cherr <- fmt.Errorf("group level cannot be zero")
return
}
db, err := dbopen(q.dbname)
if err != nil {
log.Printf("Error opening db: %v - %v", q.dbname, err)
q.cherr <- err
return
}
defer closeDBConn(db)
chunk := int64(time.Duration(q.group) * time.Millisecond)
infos := []*gouchstore.DocumentInfo{}
g := int64(0)
nextg := ""
err = db.AllDocuments(q.from, q.to, func(db *gouchstore.Gouchstore, di *gouchstore.DocumentInfo, userContext interface{}) error {
kstr := di.ID
var err error
atomic.AddInt32(&q.totalKeys, 1)
if kstr >= nextg {
if len(infos) > 0 {
atomic.AddInt32(&q.started, 1)
fetchDocs(q.dbname, g, infos, di,
q.ptrs, q.reds, q.filters, q.filtervals,
q.before, q.out)
infos = make([]*gouchstore.DocumentInfo, 0, len(infos))
}
k := parseKey(kstr)
g = (k / chunk) * chunk
nextgi := g + chunk
nextgt := time.Unix(nextgi/1e9, nextgi%1e9).UTC()
nextg = nextgt.Format(time.RFC3339Nano)
}
infos = append(infos, di)
return err
}, nil)
if err == nil && len(infos) > 0 {
atomic.AddInt32(&q.started, 1)
fetchDocs(q.dbname, g, infos, nil,
q.ptrs, q.reds, q.filters, q.filtervals,
q.before, q.out)
}
q.cherr <- err
}
func queryExecutor() {
for q := range queryInput {
if time.Now().Before(q.before) {
runQuery(q)
} else {
log.Printf("Timed out query that's %v late",
time.Since(q.before))
q.cherr <- errTimeout
}
}
}
func executeQuery(dbname, from, to string, group int,
ptrs, reds, filters, filtervals []string) *queryIn {
now := time.Now()
rv := &queryIn{
dbname: dbname,
from: from,
to: to,
group: group,
ptrs: ptrs,
reds: reds,
start: now,
before: now.Add(*queryTimeout),
filters: filters,
filtervals: filtervals,
out: make(chan *processOut),
cherr: make(chan error),
}
queryInput <- rv
return rv
}
var processorInput chan *processIn
var queryInput chan *queryIn
func convertTofloat64(in chan ptrval) chan float64 {
ch := make(chan float64)
go func() {
defer close(ch)
for v := range in {
if v.included && v.val != nil {
switch value := v.val.(type) {
case string:
x, err := strconv.ParseFloat(value, 64)
if err == nil {
ch <- x
}
}
}
}
}()
return ch
}
func convertTofloat64Rate(in chan ptrval) chan float64 {
ch := make(chan float64)
go func() {
defer close(ch)
var prevts int64
var preval float64
// First, find a part of the stream that has usable data.
FIND_USABLE:
for v := range in {
if v.di != nil && v.val != nil {
switch value := v.val.(type) {
case string:
x, err := strconv.ParseFloat(value, 64)
if err == nil {
prevts = parseKey(v.di.ID)
preval = x
break FIND_USABLE
}
}
}
}
// Then emit floats based on deltas from previous values.
for v := range in {
if v.di != nil && v.val != nil {
switch value := v.val.(type) {
case string:
x, err := strconv.ParseFloat(value, 64)
if err == nil {
thists := parseKey(v.di.ID)
val := ((x - preval) /
(float64(thists-prevts) / 1e9))
if !(math.IsNaN(val) || math.IsInf(val, 0)) {
ch <- val
}
prevts = thists
preval = x
}
}
}
}
}()
return ch
}
var reducers = map[string]reducer{
"identity": func(input chan ptrval) interface{} {
rv := []interface{}{}
for s := range input {
if s.included {
rv = append(rv, s.val)
}
}
return rv
},
"any": func(input chan ptrval) interface{} {
var rv interface{}
for v := range input {
if rv == nil && v.included && v.val != nil {
rv = v.val
}
}
return rv
},
"distinct": func(input chan ptrval) interface{} {
uvm := map[interface{}]bool{}
for v := range input {
if v.included {
switch value := v.val.(type) {
case map[string]interface{}:
case []interface{}:
//unhashable
continue
default:
uvm[value] = true
}
}
}
rv := make([]interface{}, 0, len(uvm))
for k := range uvm {
rv = append(rv, k)
}
return rv
},
"count": func(input chan ptrval) interface{} {
rv := 0
for v := range input {
if v.included && v.val != nil {
rv++
}
}
return rv
},
"sum": func(input chan ptrval) interface{} {
rv := float64(0)
for v := range convertTofloat64(input) {
rv += v
}
return rv
},
"sumsq": func(input chan ptrval) interface{} {
rv := float64(0)
for v := range convertTofloat64(input) {
rv += (v * v)
}
return rv
},
"max": func(input chan ptrval) interface{} {
rv := math.NaN()
for v := range convertTofloat64(input) {
if v > rv || math.IsNaN(rv) || math.IsInf(rv, 0) {
rv = v
}
}
return rv
},
"min": func(input chan ptrval) interface{} {
rv := math.NaN()
for v := range convertTofloat64(input) {
if v < rv || math.IsNaN(rv) || math.IsInf(rv, 0) {
rv = v
}
}
return rv
},
"avg": func(input chan ptrval) interface{} {
nums := float64(0)
sum := float64(0)
for v := range convertTofloat64(input) {
nums++
sum += v
}
if nums > 0 {
return sum / nums
}
return math.NaN()
},
"c": func(input chan ptrval) interface{} {
sum := float64(0)
for v := range convertTofloat64Rate(input) {
sum += v
}
return sum
},
"c_min": func(input chan ptrval) interface{} {
rv := math.NaN()
for v := range convertTofloat64Rate(input) {
if v < rv || math.IsNaN(rv) || math.IsInf(rv, 0) {
rv = v
}
}
return rv
},
"c_avg": func(input chan ptrval) interface{} {
nums := float64(0)
sum := float64(0)
for v := range convertTofloat64Rate(input) {
nums++
sum += v
}
if nums > 0 {
return sum / nums
}
return math.NaN()
},
"c_max": func(input chan ptrval) interface{} {
rv := math.NaN()
for v := range convertTofloat64Rate(input) {
if v > rv || math.IsNaN(rv) || math.IsInf(rv, 0) {
rv = v
}
}
return rv
},
"obj_keys": func(input chan ptrval) interface{} {
rv := []string{}
for v := range input {
if v.included {
switch value := v.val.(type) {
case map[string]interface{}:
for mapk := range value {
rv = append(rv, mapk)
}
}
}
}
return rv
},
"obj_distinct_keys": func(input chan ptrval) interface{} {
ukm := map[string]bool{}
for v := range input {
if v.included {
switch value := v.val.(type) {
case map[string]interface{}:
for mapk := range value {
ukm[mapk] = true
}
}
}
}
rv := make([]string, 0, len(ukm))
for k := range ukm {
rv = append(rv, k)
}
return rv
},
}