forked from couchbaselabs/cbgb
/
reduction.go
174 lines (150 loc) · 3.44 KB
/
reduction.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
package main
import (
"fmt"
"math"
"github.com/robertkrimen/otto"
)
func newReducer() *otto.Otto {
o := otto.New()
must(o.Set("_sum", javascriptReduceSum))
must(o.Set("_count", javascriptReduceCount))
must(o.Set("_stats", javascriptReduceStats))
return o
}
// Convert interface{} to float with NaN and infs to 0 for summing
func zeroate(i interface{}) float64 {
f, ok := i.(float64)
if !ok {
return 0
}
if math.IsNaN(f) || math.IsInf(f, 1) || math.IsInf(f, -1) {
return 0
}
return f
}
func ottoMust(v otto.Value, err error) otto.Value {
if err != nil {
panic(err)
}
return v
}
func javascriptReduceSum(call otto.FunctionCall) otto.Value {
ob, err := call.Argument(1).Export()
if err != nil {
return ottoMust(otto.ToValue(fmt.Sprintf("Error getting stuff: %v", err)))
}
l, ok := ob.([]interface{})
if !ok {
return ottoMust(otto.ToValue(fmt.Sprintf("unhandled %v/%T", ob, ob)))
}
rv := float64(0)
for _, i := range l {
rv += zeroate(i)
}
return ottoMust(otto.ToValue(rv))
}
func javascriptReduceCount(call otto.FunctionCall) otto.Value {
rere, err := call.Argument(2).ToBoolean()
if err != nil {
return ottoMust(otto.ToValue(fmt.Sprintf("Error getting rere flag: %v", err)))
}
ob, err := call.Argument(1).Export()
if err != nil {
return ottoMust(otto.ToValue(fmt.Sprintf("Error getting stuff: %v", err)))
}
l, ok := ob.([]interface{})
if !ok {
return ottoMust(otto.ToValue(fmt.Sprintf("unhandled %v/%T", ob, ob)))
}
if !rere {
return ottoMust(otto.ToValue(len(l)))
}
rv := float64(0)
for _, i := range l {
rv += zeroate(i)
}
return ottoMust(otto.ToValue(rv))
}
type statsResult struct {
sum, min, max, sumsqr float64
count int
}
func (s statsResult) toMap() map[string]float64 {
return map[string]float64{
"sum": s.sum,
"count": float64(s.count),
"min": s.min,
"max": s.max,
"sumsqr": s.sumsqr,
}
}
func (s statsResult) toOtto() otto.Value {
o := otto.New()
return ottoMust(o.ToValue(s.toMap()))
}
func (s *statsResult) load(ob interface{}) {
m, ok := ob.(map[string]interface{})
if !ok {
return
}
s.sum = zeroate(m["sum"])
s.count = int(zeroate(m["count"]))
s.min = zeroate(m["min"])
s.max = zeroate(m["max"])
s.sumsqr = zeroate(m["sumsqr"])
}
func (s *statsResult) Add(from statsResult) {
s.sum += from.sum
s.count += from.count
s.min = math.Min(s.min, from.min)
s.max = math.Max(s.max, from.max)
s.sumsqr += from.sumsqr
}
func javascriptReduceStats(call otto.FunctionCall) otto.Value {
rere, err := call.Argument(2).ToBoolean()
if err != nil {
return ottoMust(otto.ToValue(fmt.Sprintf("Error getting rere flag: %v", err)))
}
ob, err := call.Argument(1).Export()
if err != nil {
return ottoMust(otto.ToValue(fmt.Sprintf("Error getting stuff: %v", err)))
}
l, ok := ob.([]interface{})
if !ok {
return ottoMust(otto.ToValue(fmt.Sprintf("unhandled %v/%T", ob, ob)))
}
rv := statsResult{}
if len(l) == 0 {
return rv.toOtto()
}
if rere {
// Rereduction goes here.
rv.load(l[0])
for i := 1; i < len(l); i++ {
ob := statsResult{}
ob.load(l[i])
rv.Add(ob)
}
return rv.toOtto()
}
// Initial reduction
rv.count = 1
rv.sum = zeroate(l[0])
rv.min = rv.sum
rv.max = rv.sum
rv.sumsqr = rv.sum * rv.sum
for i := 1; i < len(l); i++ {
v := zeroate(l[i])
rv.count++
rv.sum += v
rv.min = math.Min(rv.min, v)
rv.max = math.Max(rv.max, v)
rv.sumsqr += (v * v)
}
return rv.toOtto()
}
func must(err error) {
if err != nil {
panic(err)
}
}