-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
WindowState.js
146 lines (122 loc) · 3.21 KB
/
WindowState.js
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
import {createMeasure, compileMeasures, measureName} from './AggregateOps';
import TupleStore from './TupleStore';
import {WindowOp, WindowOps} from './WindowOps';
import {accessorFields, accessorName, array, error, hasOwnProperty} from 'vega-util';
export default function WindowState(_) {
let self = this,
ops = array(_.ops),
fields = array(_.fields),
params = array(_.params),
as = array(_.as),
outputs = self.outputs = [],
windows = self.windows = [],
inputs = {},
map = {},
countOnly = true,
counts = [],
measures = [];
function visitInputs(f) {
array(accessorFields(f)).forEach(_ => inputs[_] = 1);
}
visitInputs(_.sort);
ops.forEach(function(op, i) {
let field = fields[i],
mname = accessorName(field),
name = measureName(op, mname, as[i]);
visitInputs(field);
outputs.push(name);
// Window operation
if (hasOwnProperty(WindowOps, op)) {
windows.push(WindowOp(op, fields[i], params[i], name));
}
// Aggregate operation
else {
if (field == null && op !== 'count') {
error('Null aggregate field specified.');
}
if (op === 'count') {
counts.push(name);
return;
}
countOnly = false;
let m = map[mname];
if (!m) {
m = (map[mname] = []);
m.field = field;
measures.push(m);
}
m.push(createMeasure(op, name));
}
});
if (counts.length || measures.length) {
self.cell = cell(measures, counts, countOnly);
}
self.inputs = Object.keys(inputs);
}
const prototype = WindowState.prototype;
prototype.init = function() {
this.windows.forEach(_ => _.init());
if (this.cell) this.cell.init();
};
prototype.update = function(w, t) {
let self = this,
cell = self.cell,
wind = self.windows,
data = w.data,
m = wind && wind.length,
j;
if (cell) {
for (j=w.p0; j<w.i0; ++j) cell.rem(data[j]);
for (j=w.p1; j<w.i1; ++j) cell.add(data[j]);
cell.set(t);
}
for (j=0; j<m; ++j) wind[j].update(w, t);
};
function cell(measures, counts, countOnly) {
measures = measures.map(m => compileMeasures(m, m.field));
let cell = {
num: 0,
agg: null,
store: false,
count: counts
};
if (!countOnly) {
var n = measures.length,
a = cell.agg = Array(n),
i = 0;
for (; i<n; ++i) a[i] = new measures[i](cell);
}
if (cell.store) {
var store = cell.data = new TupleStore();
}
cell.add = function(t) {
cell.num += 1;
if (countOnly) return;
if (store) store.add(t);
for (let i=0; i<n; ++i) {
a[i].add(a[i].get(t), t);
}
};
cell.rem = function(t) {
cell.num -= 1;
if (countOnly) return;
if (store) store.rem(t);
for (let i=0; i<n; ++i) {
a[i].rem(a[i].get(t), t);
}
};
cell.set = function(t) {
let i, n;
// consolidate stored values
if (store) store.values();
// update tuple properties
for (i=0, n=counts.length; i<n; ++i) t[counts[i]] = cell.num;
if (!countOnly) for (i=0, n=a.length; i<n; ++i) a[i].set(t);
};
cell.init = function() {
cell.num = 0;
if (store) store.reset();
for (let i=0; i<n; ++i) a[i].init();
};
return cell;
}