-
Notifications
You must be signed in to change notification settings - Fork 0
/
canal.js
3790 lines (3430 loc) · 69.8 KB
/
canal.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! canal.js v1.0.59 2024-08-21 */
/**
* Functional Programming Framework of Data Processing in Javascript.
* https://github.com/KerneLab/Canal
*/
(function(global, factory)
{
typeof exports === "object" && typeof module !== "undefined" ? (module.exports = factory()) : typeof define === "function" && define.amd ? define(factory) : (global.Canal = factory());
}(this, (function()
{
"use strict";
// Constant of an empty array which MUST not be changed
var emptyArray = [];
// Constant of the end of data flow which MUST NOT be changed or in any
// data
var endOfData = {};
// Default value of a data which returns data itself
var valOfData = function(d)
{
return d;
};
// Default key of a pair is the 1st element of Array[2]
var keyOfPair = function(p)
{
return p[0];
};
// Default value of a pair is the 2nd element of Array[2]
var valOfPair = function(p)
{
return p[1];
};
// The function which always returns null
var nullData = function()
{
return null;
};
// The function which always returns undefined
var undefineData = function()
{
return undefined;
};
// Return the second argument if the first one is null or undefined
var nvl = function(val, bak)
{
return val == null ? bak : val;
};
var nvl0 = function(val)
{
return nvl(val, 0);
};
// The function which doesn't do anything
var voidAction = function()
{
};
// Make an item stand for pick all columns of the object
var pickAllColumns = function(obj)
{
var pick = function()
{
return obj;
};
pick.all = true;
return pick;
};
// Make a Spring source according to given range
var range = function(begin, until, step)
{
if (step == null)
{
step = 1;
}
var index = begin - step;
return new Spring(function()
{
var has = false;
if (begin < until)
{
has = step > 0 && index + step < until;
}
else if (begin > until)
{
has = step < 0 && index + step > until;
}
if (has)
{
return index += step;
}
else
{
return endOfData;
}
});
};
// Signum function which could compare LVal1 vs RVal1, LVal2 vs RVal2 ...
var signum = function()
{
var l, r;
for (var i = 0; i < arguments.length - 1; i += 2)
{
l = arguments[i];
r = arguments[i + 1];
if (l < r)
{
return -1;
}
else if (l > r)
{
return 1;
}
}
return 0;
};
// Compare two value by given order
var cmpval = function(a, b, asc, nullsFirst)
{
if (a == b)
{
return 0;
}
if (a == null || b == null)
{
if (nullsFirst === true)
{
return a == null ? -1 : 1;
}
else
{
return a == null ? 1 : -1;
}
}
return a < b ? -1 * asc : 1 * asc;
};
// Make a comparator to compare to two object according to given fields
var comparator = function()
{
var fields = [];
for (var i = 0; i < arguments.length; i++)
{
fields.push(arguments[i]);
}
return function(a, b)
{
if (a == b)
{
return 0;
}
else if (a == null)
{
return -1;
}
else if (b == null)
{
return 1;
}
else
{
var c = 0, f = null;
for (var i = 0; i < fields.length; i++)
{
f = fields[i];
c = signum(a[f], b[f]);
if (c != 0)
{
return c;
}
}
return 0;
}
};
};
// Default equality
var equality = function(a, b)
{
return a === b ? 0 : signum(a, b);
};
// Decide whether the value is a plain object or not
var isObject = function(val)
{
return val != null && val.constructor == Object;
};
// Decide whether the value is a plain array or not
var isArray = function(val)
{
return val != null && val.constructor == Array;
};
// Flatten the hierarchical array
var flatten = function(arr, lev, res)
{
if (res == null)
{
res = [];
}
var e = null;
for (var i = 0; i < arr.length; i++)
{
e = arr[i];
if ((lev > 0 || lev == null) && (e instanceof Array))
{
flatten(e, (lev == null ? null : lev - 1), res);
}
else
{
res.push(e);
}
}
return res;
};
// Collect the orders array
var collectOrders = function(kops, ascs, orders)
{
var asc = null;
for (var i = 0; i < orders.length; i++)
{
var arg = orders[i];
if (typeof arg === "boolean")
{
asc = arg === false ? -1 : 1;
}
else
{
if (typeof arg === "function")
{
kops.push(arg);
if (asc != null)
{
ascs.push(asc);
}
asc = arg.order === false ? -1 : 1;
}
}
}
if (asc != null)
{
ascs.push(asc);
}
};
// Generate the comparator according to the orders array
var generateComparator = function(orders)
{
var kops = [];
var ascs = [];
collectOrders(kops, ascs, orders);
return function(a, b)
{
var cmp = 0;
for (var i = 0; i < kops.length; i++)
{
var kop = kops[i];
cmp = cmpval(kop(a), kop(b), ascs[i], kop.nulls == "first");
if (cmp != 0)
{
break;
}
}
return cmp;
};
};
// Sort the data and collect the "same" data into each array
var sortCollect = function(data, cmp, asc)
{
if (cmp != null)
{
data.sort(cmp);
var last = null, next = null;
var collect = null;
var result = [];
if (asc)
{
for (var i = 0; i < data.length; i++)
{
next = data[i];
if (collect == null || cmp(last, next) != 0)
{
collect = [];
result.push(collect);
}
collect.push(next);
last = next;
}
}
else
{
for (var i = data.length - 1; i >= 0; i--)
{
next = data[i];
if (collect == null || cmp(last, next) != 0)
{
collect = [];
result.push(collect);
}
collect.push(next);
last = next;
}
}
return result;
}
else
{
return [ data ];
}
};
// Seek an index when the condition is satisfied
var seekIndexWhen = function(rows, kop, from, direct, last, cmp, val)
{
var toTail = direct > 0 ? true : false;
var length = rows.length;
var index = null, cur = null;
for (var i = from; toTail && i < length || !toTail && i >= 0; i += direct)
{
cur = kop(rows[i]);
if (last)
{
if (!cmp(cur, val))
{
break;
}
index = i;
}
else
{
if (cmp(cur, val))
{
index = i;
break;
}
}
}
return index;
};
var addWindowItem = function(c, aggr, updt, expr, alias, partBy, orderBy, between, byRows)
{
// Aggregation on each partition
aggr = aggr != null ? aggr : undefineData;
// Updater for each level
updt = updt != null ? updt : function(agg)
{
return agg;
};
// Expression for each row
expr = expr != null ? expr : function(pos, upd)
{
return upd;
};
var preced = null, follow = null;
if (between != null)
{
preced = between[0];
follow = between[1];
byRows = byRows == null ? true : byRows;
}
else
{
byRows = null;
}
var last = true, first = false;
var le = function(a, b)
{
return a <= b && a != null && b != null || a == null && b == null;
}, ge = function(a, b)
{
return a >= b && a != null && b != null || a == null && b == null;
};
var kops = [], ascs = [], kop = null, asc = null;
if (orderBy != null && orderBy.length > 0)
{
collectOrders(kops, ascs, orderBy);
kop = kops[0];
asc = ascs[0];
}
var makeRangeBeginSeeker = function(preced, kop, asc)
{
var dir = preced < 0 ? -1 : 1;
var lst = preced < 0 ? last : first;
var cmp = asc > 0 ? ge : le;
return function(rows, current, preced, levelBegin, levelEnd)
{
var begin = null;
if (preced == null)
{
begin = 0;
}
else if (preced == 0)
{
begin = levelBegin;
}
else
{
var from = preced < 0 ? levelBegin : (levelEnd - 1);
var far = current != null ? current + asc * preced : null;
begin = seekIndexWhen(rows, kop, from, dir, lst, cmp, far);
}
return begin;
};
};
var makeRangeEndSeeker = function(follow, kop, asc)
{
var dir = follow > 0 ? 1 : -1;
var lst = follow > 0 ? last : first;
var cmp = asc > 0 ? le : ge;
return function(rows, current, follow, levelBegin, levelEnd)
{
var end = null;
if (follow == null)
{
end = rows.length - 1;
}
else if (follow == 0)
{
end = levelEnd - 1;
}
else
{
var from = follow > 0 ? (levelEnd - 1) : levelBegin;
var far = current != null ? current + asc * follow : null;
end = seekIndexWhen(rows, kop, from, dir, lst, cmp, far);
}
if (end != null)
{
end++;
}
return end;
};
};
var seeker = null;
if (between != null)
{
if (byRows)
{
seeker = [ //
function(rows, current, preced, levelBegin, levelEnd)
{
return Math.max(preced == null ? 0 : (current + preced), 0);
}, //
function(rows, current, follow, levelBegin, levelEnd)
{
return Math.min(follow == null ? rows.length : (current + follow + 1), rows.length);
} ];
}
else
{
seeker = [ makeRangeBeginSeeker(preced, kop, asc), makeRangeEndSeeker(follow, kop, asc) ];
}
}
return c.stratifyBy.apply(c, partBy) //
.flatMap(function(part)
{
var partCanal = Canal.of(part);
var ordered = partCanal.stratifyBy.apply(partCanal, orderBy).collect();
var aggRes = aggr(ordered);
var partRows = [], levelLength = [];
var layer = null, res = undefined, length = null, last = null;
for (var l = 0; l < ordered.length; l++)
{
layer = ordered[l];
levelLength.push(layer.length);
last = partRows.length;
partRows.push.apply(partRows, layer);
if (between == null)
{
length = partRows.length;
// aggRes,partRows,winBegin,winEnd,lvlBegin,lvlEnd
res = updt(aggRes, partRows, 0, length, length - layer.length, length);
for (var k = 0; k < layer.length; k++)
{
// curntPos,updRes,partRows,winBegin,winEnd,lvlBegin,lvlEnd
layer[k][alias] = expr(last + k, res, partRows, 0, length, length - layer.length, length);
}
}
}
ordered = null;
if (between != null)
{
var begin = null, end = null;
length = partRows.length;
if (byRows)
{
var levelBegin = 0, levelEnd = 0;
for (var i = 0; i < levelLength.length; i++)
{
levelEnd += levelLength[i];
for (var j = levelBegin; j < levelEnd; j++)
{
begin = seeker[0](partRows, j, preced, levelBegin, levelEnd);
end = seeker[1](partRows, j, follow, levelBegin, levelEnd);
res = updt(aggRes, partRows, begin, end, levelBegin, levelEnd);
partRows[j][alias] = expr(j, res, partRows, begin, end, levelBegin, levelEnd);
}
levelBegin = levelEnd;
}
}
else
{
var levelBegin = 0, levelEnd = 0, val = undefined;
for (var i = 0; i < levelLength.length; i++)
{
levelEnd += levelLength[i];
val = kop(partRows[levelBegin]); // Level Value
begin = seeker[0](partRows, val, preced, levelBegin, levelEnd);
end = seeker[1](partRows, val, follow, levelBegin, levelEnd);
if (begin != null && end != null)
{
res = updt(aggRes, partRows, begin, end, levelBegin, levelEnd);
}
else
{ // Window does not exist
res = undefined;
}
for (var j = levelBegin; j < levelEnd; j++)
{
partRows[j][alias] = expr(j, res, partRows, begin, end, levelBegin, levelEnd);
}
levelBegin = levelEnd;
}
}
}
return partRows;
});
};
function Pond()
{
this.downstream = null;
}
Pond.prototype.begin = function() // Void
{
if (this.downstream != null)
{
this.downstream.begin();
}
};
Pond.prototype.accept = undefined; // (data) => accept or not
Pond.prototype.done = function() // Void
{
if (this.downstream != null)
{
this.downstream.done();
}
};
function Wheel()
{
this.index = undefined;
}
Wheel.prototype = new Pond();
Wheel.prototype.begin = function() // Void
{
if (this.index === undefined)
{
this.index = 0;
}
if (this.downstream != null)
{
this.downstream.begin();
}
};
function Desilter()
{
this.sediment = undefined;
}
Desilter.prototype = new Pond();
Desilter.prototype.settling = null; // () => new Sediment
Desilter.prototype.begin = function()
{
if (this.settle() === undefined && this.settling != null)
{
this.settle(this.settling());
}
if (this.downstream != null)
{
this.downstream.begin();
}
};
Desilter.prototype.settle = function() // access Sediment
{
if (arguments.length > 0)
{
this.sediment = arguments[0];
return this;
}
else
{
return this.sediment;
}
};
function Heaper()
{
}
Heaper.prototype = new Desilter();
Heaper.prototype.settling = function()
{
return [];
};
Heaper.prototype.accept = function(d)
{
this.settle().push(d);
return true;
};
function Grouper()
{
}
Grouper.prototype = new Desilter();
Grouper.prototype.settling = function()
{
return {};
};
Grouper.prototype.keyOf = keyOfPair;
Grouper.prototype.valOf = valOfPair;
Grouper.prototype.accept = function(d)
{
var key = this.keyOf(d);
var settle = this.settle();
var group = settle[key];
if (group == null)
{
group = [];
settle[key] = group;
}
group.push(this.valOf(d));
return true;
};
Grouper.prototype.done = function()
{
if (this.downstream != null)
{
var settle = this.settle();
for ( var group in settle)
{
if (settle.hasOwnProperty(group) //
&& !this.downstream.accept([ group, settle[group] ]))
{
break;
}
}
this.downstream.done();
}
};
function Dam()
{
this.branch = undefined;
}
Dam.prototype = new Pond();
Dam.prototype.that = undefined; // () => that data
Dam.prototype.begin = function()
{
if (this.branch === undefined)
{
this.branch = this.that().collect();
}
if (this.downstream != null)
{
this.downstream.begin();
}
};
function Joiner()
{
this.keyR = keyOfPair;
this.valR = valOfPair;
}
Joiner.prototype = new Grouper();
// () => that data
Joiner.prototype.that = undefined; // () => that data
Joiner.prototype.base = undefined; // (left,right) => base data
// (down,key,lefts,rights) => continue or not
Joiner.prototype.join = undefined;
Joiner.prototype.done = function()
{
if (this.downstream != null)
{
var left = this.settle();
var right = this.that().groupByKey(this.keyR, this.valR).collectAsMap();
var base = this.base(left, right);
var down = this.downstream;
for ( var k in base)
{
if (base.hasOwnProperty(k) && !this.join(down, k, left[k], right[k]))
{
break;
}
}
down.done();
}
};
// Operators
function Operator()
{
}
Operator.prototype.newPond = undefined; // () => Pond
// Intermediate Operators
function CartesianOp(that)
{
function CartesianPond()
{
}
CartesianPond.prototype = new Dam();
CartesianPond.prototype.that = function()
{
return that;
};
CartesianPond.prototype.accept = function(d)
{
var branch = this.branch;
for (var i = 0; i < branch.length; i++)
{
if (!this.downstream.accept([ d, branch[i] ]))
{
return false;
}
}
return true;
};
this.newPond = function()
{
return new CartesianPond();
};
}
CartesianOp.prototype = new Operator();
function ChooseOp(fn)
{
function ChoosePond()
{
}
ChoosePond.prototype = new Pond();
ChoosePond.prototype.accept = function(arr)
{
if (fn.apply(null, arr))
{
return this.downstream.accept(arr);
}
else
{
return true;
}
};
this.newPond = function()
{
return new ChoosePond();
};
}
ChooseOp.prototype = new Operator();
function CogroupOp(those)
{
function CogroupPond()
{
}
CogroupPond.prototype = new Grouper();
CogroupPond.prototype.done = function()
{
if (this.downstream != null)
{
var settle = this.settle();
var groups = [ settle ];
var keys = {};
for ( var k in settle)
{
if (settle.hasOwnProperty(k))
{
keys[k] = null;
}
}
for (var i = 0; i < those.length; i++)
{
settle = those[i].groupByKey().collectAsMap();
for ( var k in settle)
{
if (settle.hasOwnProperty(k))
{
keys[k] = null;
}
}
groups.push(settle);
}
for ( var key in keys)
{
if (keys.hasOwnProperty(key))
{
var comb = [];
for (var g = 0; g < groups.length; g++)
{
var group = groups[g][key];
comb.push(group != null ? group : []);
}
if (!this.downstream.accept([ key, comb ]))
{
break;
}
}
}
this.downstream.done();
}
};
this.newPond = function()
{
return new CogroupPond();
};
}
CogroupOp.prototype = new Operator();
function DistinctOp(cmp)
{
cmp = cmp != null ? cmp : equality;
function DistinctPond()
{
}
DistinctPond.prototype = new Heaper();
DistinctPond.prototype.done = function()
{
if (this.downstream != null)
{
var settle = this.settle();
var sorts = [];
for (var i = 0; i < settle.length; i++)
{
sorts.push([ i, settle[i] ]);
}
sorts.sort(function(a, b)
{
var c = cmp(a[1], b[1]);
if (c != 0)
{
return c;
}
return a[0] - b[0];
});
var need = {}, last = endOfData, next = null;
for (var i = 0; i < sorts.length; i++)
{
next = sorts[i][1];
if (last !== endOfData && cmp(last, next) === 0)
{
continue;
}
need[sorts[i][0]] = true;
last = next;
}
for (var i = 0; i < settle.length; i++)
{
if (need[i] && !this.downstream.accept(settle[i]))
{
break;
}
}
this.downstream.done();
}
};
this.newPond = function()
{
return new DistinctPond();
};
}
DistinctOp.prototype = new Operator();
function FilterOp(pred)
{
function FilterPond()
{
}
FilterPond.prototype = new Wheel();
FilterPond.prototype.accept = function(d)
{
if (pred(d, this.index++))
{
return this.downstream.accept(d);
}
else
{
return true;
}
};
this.newPond = function()
{
return new FilterPond();
};
}
FilterOp.prototype = new Operator();
function FlatMapOp(fn)
{
fn = fn != null ? fn : function(d)
{
return d;
};
function FlatMapPond()
{
}
FlatMapPond.prototype = new Wheel();
FlatMapPond.prototype.accept = function(d)