-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathozone.js
3251 lines (3251 loc) · 147 KB
/
ozone.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
/**
* Copyright 2013-2014 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='_all.ts' />
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='_all.ts' />
/**
* Contains public functions and tiny classes that are too small to merit their own file.
*/
var ozone;
(function (ozone) {
/**
* Minimum and maximum values (inclusive), and whether every number is an integer. For our purposes, an integer is
* defined according to Mozilla's Number.isInteger polyfill and ECMA Harmony specification, namely:
*
* typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
*
* ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger )
*
* JSON.stringify(range) produces clean JSON that can be parsed back into an identical Range.
*/
var Range = (function () {
function Range(min, max, integerOnly) {
this.min = min;
this.max = max;
this.integerOnly = integerOnly;
}
/**
* Build from JSON; in most cases you could just use the AJAX directly, but calling this provides
* instanceof and toString().
*/
Range.build = function (ajax) {
return new Range(ajax.min, ajax.max, ajax.integerOnly);
};
Range.prototype.toString = function () {
var intStr = (this.integerOnly) ? "integer" : "decimal";
return this.min + " to " + this.max + " " + intStr;
};
return Range;
})();
ozone.Range = Range;
var AbstractReducer = (function () {
function AbstractReducer() {
this.reset();
}
AbstractReducer.prototype.onItem = function (item) {
throw new Error("Subclasses must implement");
};
/** Default implementation does nothing. */
AbstractReducer.prototype.reset = function () {
};
AbstractReducer.prototype.yieldResult = function () {
throw new Error("Subclasses must implement");
};
AbstractReducer.prototype.onEnd = function () {
var result = this.yieldResult();
this.reset();
return result;
};
return AbstractReducer;
})();
ozone.AbstractReducer = AbstractReducer;
/** Wraps an OrderedIterator so you can peek at the next item without modifying it. */
var BufferedOrderedIterator = (function () {
function BufferedOrderedIterator(inner) {
this.inner = inner;
if (inner.hasNext()) {
this.current = inner.next();
}
}
/** Returns the next value to be returned by next(), or undefined if hasNext() returns false. */
BufferedOrderedIterator.prototype.peek = function () { return this.current; };
BufferedOrderedIterator.prototype.skipTo = function (item) {
if (this.current < item) {
this.inner.skipTo(item);
this.current = this.inner.hasNext() ? this.inner.next() : undefined;
}
};
BufferedOrderedIterator.prototype.next = function () {
var result = this.current;
this.current = this.inner.hasNext() ? this.inner.next() : undefined;
return result;
};
BufferedOrderedIterator.prototype.hasNext = function () {
return typeof (this.current) !== 'undefined';
};
return BufferedOrderedIterator;
})();
ozone.BufferedOrderedIterator = BufferedOrderedIterator;
/**
* Combine all descriptors, with later ones overwriting values provided by earlier ones. All non-inherited
* properties are copied over, plus all FieldDescribing (inherited or otherwise).
* If range and distinctValueEstimate are functions, the result's function calls the original object's function.
* If they are not functions, the result's function returns the value.
*/
function mergeFieldDescriptors() {
var descriptors = [];
for (var _i = 0; _i < arguments.length; _i++) {
descriptors[_i - 0] = arguments[_i];
}
return mergeObjects(["identifier", "displayName", "typeOfValue", "typeConstructor", "aggregationRule"], ["range", "distinctValueEstimate"], descriptors);
}
ozone.mergeFieldDescriptors = mergeFieldDescriptors;
function mergeObjects(fields, functions, items) {
if (items.length === 0) {
return null;
}
var result = {};
for (var i = 0; i < items.length; i++) {
var item = items[i];
for (var k in item) {
if (item.hasOwnProperty(k)) {
result[k] = k;
}
}
for (var j = 0; j < fields.length; j++) {
var key = fields[j];
if (typeof item[key] !== "undefined") {
result[key] = item[key];
}
}
for (j = 0; j < functions.length; j++) {
var key = functions[j];
if (typeof item[key] === "function") {
(function (f) {
result[key] = function () { f(); };
})(item[key]);
}
else if (typeof item[key] !== "undefined") {
(function (value) {
result[key] = function () { return value; };
})(item[key]);
}
}
}
return result;
}
function convert(item, descriptor) {
if (item === null) {
return null;
}
if (descriptor.typeOfValue === "number") {
if (typeof item === "string") {
return Number(item);
}
}
else if (descriptor.typeOfValue === "string") {
if (typeof item === "number") {
return "" + item;
}
}
return item;
}
ozone.convert = convert;
})(ozone || (ozone = {}));
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='_all.ts' />
var ozone;
(function (ozone) {
/**
* Selects rows where a specific field has a specific value. Note: ColumnStore typically uses indexes to filter by
* value, so this class is generally used only to trigger that code.
*/
var ValueFilter = (function () {
function ValueFilter(fieldDescriptor, value, displayName) {
if (displayName === void 0) { displayName = null; }
this.fieldDescriptor = fieldDescriptor;
this.value = value;
this.displayName = displayName;
if (displayName === null) {
this.displayName = value + " (" + fieldDescriptor.displayName + ")";
}
}
/**
* Returns true if the row has the given value. Note: ColumnStore typically uses indexes to filter by
* value, bypassing this method.
*/
ValueFilter.prototype.matches = function (store, rowToken) {
var field = store.field(this.fieldDescriptor.identifier);
return field.rowHasValue(rowToken, this.value);
};
ValueFilter.prototype.equals = function (f) {
if (f === this) {
return true;
}
if (Object.getPrototypeOf(f) !== Object.getPrototypeOf(this)) {
return false;
}
var vf = f;
if (vf.fieldDescriptor.identifier !== this.fieldDescriptor.identifier) {
return false;
}
if (vf.value === this.value) {
return true;
}
return vf.value.toString() === this.value.toString(); // non-primitive values, such as dates
};
return ValueFilter;
})();
ozone.ValueFilter = ValueFilter;
/**
* Selects rows which match all of several values. Note: because this is a fundamental set operation, ColumnStore
* generally uses its internal union operation when given a UnionFilter.
*
* As currently implemented, this works with an array of Filters, and makes no attempt to remove redundant filters.
* In the future, the constructor might remove redundant filters, and the other methods might make assumptions
* based on that.
*/
var UnionFilter = (function () {
function UnionFilter() {
var of = [];
for (var _i = 0; _i < arguments.length; _i++) {
of[_i - 0] = arguments[_i];
}
this.filters = of;
this.displayName = "All of { ";
for (var i = 0; i < this.filters.length; i++) {
if (i > 0) {
this.displayName += ", ";
}
this.displayName += this.filters[i].displayName;
}
this.displayName += " }";
}
/**
* True if f is a UnionFilter, each of f's filters is equal to one of this's filters, and each of this's
* filters is equal to one of f's filters.
*/
UnionFilter.prototype.equals = function (f) {
if (f === this) {
return true;
}
if (Object.getPrototypeOf(f) !== Object.getPrototypeOf(this)) {
return false;
}
var that = f;
var matched = [];
var unmatched = that.filters.concat();
function checkForMatch(thisItem) {
for (var unmatchedIndex = unmatched.length; unmatchedIndex >= 0; unmatchedIndex--) {
var thatItem = unmatched[unmatchedIndex];
if (thisItem.equals(thatItem)) {
unmatched.splice(unmatchedIndex, 1);
matched.push(thatItem);
return true;
}
}
return matched.some(function (thatItem) { return thisItem.equals(thatItem); });
}
for (var thisIndex = this.filters.length - 1; thisIndex >= 0; thisIndex--) {
var thisItem = this.filters[thisIndex];
if (!checkForMatch(thisItem)) {
return false;
}
}
return unmatched.length === 0;
};
/**
* Returns false if (and only if) any of the filters don't match. NOTE: ColumnStore will typically bypass
* this method and use column indexes to compute a union.
*/
UnionFilter.prototype.matches = function (store, rowToken) {
return this.filters.every(function (f) { return f.matches(store, rowToken); });
};
return UnionFilter;
})();
ozone.UnionFilter = UnionFilter;
})(ozone || (ozone = {}));
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='_all.ts' />
var ozone;
(function (ozone) {
var StoreProxy = (function () {
function StoreProxy(source) {
this.source = source;
}
StoreProxy.prototype.fields = function () { return this.source.fields(); };
StoreProxy.prototype.field = function (key) { return this.source.field(key); };
StoreProxy.prototype.sizeField = function () { return this.source.sizeField(); };
StoreProxy.prototype.eachRow = function (rowAction) { this.source.eachRow(rowAction); };
return StoreProxy;
})();
ozone.StoreProxy = StoreProxy;
})(ozone || (ozone = {}));
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='../_all.ts' />
var ozone;
(function (ozone) {
var columnStore;
(function (columnStore) {
/**
* This is the recommended way to generate a ColumnStore.
*
* @params provides optional arguments:
*
* fields: maps from field identifiers in the source to field-specific params. All FieldDescribing
* properties and Builder parameters can be specified here.
*
* class: (within fields:) a Field class, such as UnIndexedField, or other object with a "builder" method.
*
* buildAllFields: boolean, default is true. If false, any fields not listed under 'Fields' are ignored.
*/
function buildFromStore(source, params) {
if (params === void 0) { params = {}; }
var builders = {};
var sourceFields = source.fields();
var buildAllFields = !(params.buildAllFields === false);
for (var i = 0; i < sourceFields.length; i++) {
var sourceField = sourceFields[i];
var sourceFieldIsUnary = typeof (sourceField["value"]) === "function";
var newBuilder = null;
var fieldParams = {};
var buildThisField = buildAllFields;
if (params.fields && params.fields[sourceField.identifier]) {
buildThisField = true;
fieldParams = params.fields[sourceField.identifier];
if (fieldParams["class"]) {
newBuilder = fieldParams["class"]["builder"](sourceField, fieldParams);
}
}
if (newBuilder === null && buildThisField) {
if (sourceFieldIsUnary && sourceField.distinctValueEstimate() > 500) {
newBuilder = columnStore.UnIndexedField.builder(sourceField, fieldParams);
}
else {
newBuilder = columnStore.IndexedField.builder(sourceField, fieldParams);
}
}
if (newBuilder !== null) {
builders[sourceField.identifier] = newBuilder;
}
}
var length = 0;
source.eachRow(function (rowToken) {
var indexedToken = { index: length, rowToken: rowToken };
length++;
for (var id in builders) {
if (builders.hasOwnProperty(id)) {
builders[id].onItem(indexedToken);
}
}
});
var resultFields = [];
for (i = 0; i < sourceFields.length; i++) {
sourceField = sourceFields[i];
var builder = builders[sourceField.identifier];
if (builder) {
resultFields.push(builder.onEnd());
}
}
var sizeFieldId = source.sizeField() ? source.sizeField().identifier : null;
return new columnStore.ColumnStore(length, resultFields, sizeFieldId);
}
columnStore.buildFromStore = buildFromStore;
/** Used to implement ColumnStore.sum(). */
function sum(store, fieldOrId) {
var result = 0;
var field = ((typeof fieldOrId === 'string') ? store.field(fieldOrId) : fieldOrId);
if (!field || field.typeOfValue !== 'number') {
return 0;
}
if (typeof field['value'] === 'function') {
store.eachRow(function (r) { result += field.value(r); });
}
else {
var sumFunc = function (a, b) { return a + b; };
store.eachRow(function (r) { result += field.values(r).reduce(sumFunc, 0); });
}
return result;
}
columnStore.sum = sum;
})(columnStore = ozone.columnStore || (ozone.columnStore = {}));
})(ozone || (ozone = {}));
/**
* Copyright 2013-2014 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='../_all.ts' />
var ozone;
(function (ozone) {
var columnStore;
(function (columnStore) {
/**
* A Field which is inefficient for filtering; intended for columns where distinctValueEstimate is so large that
* an IndexedField would use an unreasonable amount of memory. Stores the entire column in a single dense array.
*/
var UnIndexedField = (function () {
function UnIndexedField(descriptor, array, offset, nullProxy) {
if (offset === void 0) { offset = 0; }
if (nullProxy === void 0) { nullProxy = null; }
this.array = array;
this.offset = offset;
this.nullProxy = nullProxy;
this.identifier = descriptor.identifier;
this.displayName = descriptor.displayName;
this.typeOfValue = descriptor.typeOfValue;
this.typeConstructor = descriptor.typeConstructor;
this.valueEstimate = descriptor.distinctValueEstimate();
this.aggregationRule = (descriptor.aggregationRule) ? descriptor.aggregationRule : null;
var range = descriptor.range();
if (typeof range === 'undefined' || descriptor.typeOfValue !== 'number') {
range = null;
}
this.rangeValue = range;
if (typeof (this.valueEstimate) !== "number" || isNaN(this.valueEstimate) || this.valueEstimate > array.length) {
this.valueEstimate = array.length;
}
if (array.length > 0 && (array[0] === nullProxy || array[array.length - 1] === nullProxy)) {
throw new Error("Array must be trimmed (Field: " + this.identifier + ")");
}
}
/**
* Returns a reducer that can be run on a source DataStore to reproduce a sourceField.
*
* @param sourceField the field which will be replicated
* @param params may override any FieldDescribing field, plus additional parameters:
* nullValues -- if provided, this is a list of values equivalent to null.
* nullProxy -- if provided, this is used instead of null for storing null values. This
* may allow the JavaScript implementation to use an array of primitives.
* (Haven't yet checked to see if any JS implementations actually do this.)
*/
UnIndexedField.builder = function (sourceField, params) {
if (params === void 0) { params = {}; }
var array = [];
var offset = 0;
var nullValues = (typeof (params["nullValues"]) === "object") ? params["nullValues"] : [];
var nullProxy = (typeof (params["nullProxy"]) === "undefined") ? null : params["nullProxy"];
var nullMap = {};
for (var i = 0; i < nullValues.length; i++) {
var nv = nullValues[i];
nullMap["" + nv] = nv;
}
var descriptor = ozone.mergeFieldDescriptors(sourceField, params);
return {
onItem: function (indexedRowToken) {
var value = ozone.convert(sourceField.value(indexedRowToken.rowToken), descriptor);
if (nullValues.length > 0 && nullMap["" + value] === value) {
value = nullProxy;
}
if (array.length === 0) {
if (value !== null) {
array[0] = value;
offset = indexedRowToken.index;
}
}
else {
var newIndex = indexedRowToken.index - offset;
while (array.length < newIndex) {
array.push(nullProxy);
}
array[newIndex] = value;
}
},
onEnd: function () {
while ((array.length > 0) && (array[array.length - 1] === nullProxy)) {
array.pop(); // Trim the end
}
return new UnIndexedField(descriptor, array, offset, nullProxy);
}
};
};
UnIndexedField.prototype.value = function (rowToken) {
var index = rowToken - this.offset;
var result = this.array[index];
return (this.isNull(result)) ? null : result;
};
UnIndexedField.prototype.isNull = function (item) {
return (typeof (item) === null || typeof (item) === 'undefined' || item === this.nullProxy);
};
UnIndexedField.prototype.values = function (rowToken) {
var result = this.value(rowToken);
return (result === null) ? [] : [result];
};
UnIndexedField.prototype.range = function () {
return this.rangeValue;
};
UnIndexedField.prototype.distinctValueEstimate = function () {
return this.valueEstimate;
};
UnIndexedField.prototype.rowHasValue = function (rowToken, value) {
var actualValue = this.value(rowToken);
return actualValue === value;
};
/** Returns the first rowToken; this is for serialization and not intended for queries. */
UnIndexedField.prototype.firstRowToken = function () {
return this.offset;
};
/** Returns a copy of the data array for serialization; not intended for queries. */
UnIndexedField.prototype.dataArray = function () {
return this.array.concat();
};
return UnIndexedField;
})();
columnStore.UnIndexedField = UnIndexedField;
})(columnStore = ozone.columnStore || (ozone.columnStore = {}));
})(ozone || (ozone = {}));
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='../_all.ts' />
var ozone;
(function (ozone) {
var columnStore;
(function (columnStore) {
/**
* This is the native internal format for Ozone DataStores. The ColumnStore is little more than a container for
* Fields. IndexedFields are generally more efficient than UnIndexedFields-- with the assumption that
* Field.distinctValueEstimate() is usually low.
*
* <p>
* Conceptually the DataStore represents a dense array of rows, and each row is identified by its array index.
* In fact there is no such array; the index exists to identify records across Fields.
* </p>
*
* <p>
* Confusingly, "index" refers both to the map of values to row identifiers (i.e. a database index) and an
* individual row identifier, since conceptually (but not literally) the DataStore is a dense array of rows.
* </p>
*/
var ColumnStore = (function () {
function ColumnStore(theRowCount, fieldArray, sizeFieldId) {
if (sizeFieldId === void 0) { sizeFieldId = null; }
this.theRowCount = theRowCount;
this.fieldArray = fieldArray;
this.sizeFieldId = sizeFieldId;
this.cachedSize = null;
this.fieldMap = {};
for (var i = 0; i < fieldArray.length; i++) {
var field = fieldArray[i];
this.fieldMap[field.identifier] = field;
}
if (sizeFieldId) {
var rcField = this.fieldMap[sizeFieldId];
if (rcField === null) {
throw new Error("No field named '" + sizeFieldId + "' for record count");
}
else if (rcField.typeOfValue !== 'number') {
throw new Error(sizeFieldId + " can't be used as a record count, it isn't numerical");
}
else if (typeof rcField.value !== 'function') {
throw new Error(sizeFieldId + " can't be used as a record count, it isn't unary");
}
}
}
ColumnStore.prototype.size = function () {
if (this.cachedSize === null) {
this.cachedSize = (this.sizeFieldId) ? this.sum(this.sizeFieldId) : this.theRowCount;
}
return this.cachedSize;
};
ColumnStore.prototype.rowCount = function () { return this.theRowCount; };
ColumnStore.prototype.sum = function (field) { return ozone.columnStore.sum(this, field); };
ColumnStore.prototype.intSet = function () { return new ozone.intSet.RangeIntSet(0, this.size()); };
ColumnStore.prototype.fields = function () { return this.fieldArray; };
ColumnStore.prototype.field = function (key) { return (this.fieldMap.hasOwnProperty(key)) ? this.fieldMap[key] : null; };
ColumnStore.prototype.filter = function (fieldNameOrFilter, value) {
return columnStore.filterColumnStore(this, this, columnStore.createFilter(this, fieldNameOrFilter, value));
};
ColumnStore.prototype.filters = function () { return []; };
ColumnStore.prototype.simplifiedFilters = function () { return []; };
ColumnStore.prototype.removeFilter = function (filter) { return this; };
ColumnStore.prototype.partition = function (fieldAny) {
var key = (typeof fieldAny === 'string') ? fieldAny : fieldAny.identifier;
return columnStore.partitionColumnStore(this, this.field(key));
};
ColumnStore.prototype.eachRow = function (rowAction) {
var max = this.rowCount();
for (var i = 0; i < max; i++) {
rowAction(i);
}
};
ColumnStore.prototype.sizeField = function () { return this.field(this.sizeFieldId); };
return ColumnStore;
})();
columnStore.ColumnStore = ColumnStore;
})(columnStore = ozone.columnStore || (ozone.columnStore = {}));
})(ozone || (ozone = {}));
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='../_all.ts' />
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var ozone;
(function (ozone) {
var columnStore;
(function (columnStore) {
function createFilter(store, fieldNameOrFilter, value) {
if (typeof fieldNameOrFilter === "string") {
return new ozone.ValueFilter(store.field(fieldNameOrFilter), value);
}
else if (typeof fieldNameOrFilter === "object") {
if (typeof fieldNameOrFilter.distinctValueEstimate === "function" && typeof fieldNameOrFilter.identifier === "string") {
return new ozone.ValueFilter(fieldNameOrFilter, value);
}
if (typeof fieldNameOrFilter.matches === "function") {
return fieldNameOrFilter;
}
}
throw "Not a filter: " + fieldNameOrFilter;
}
columnStore.createFilter = createFilter;
/**
* Used by ColumnStores to implement filtering
*
* @param source the top-level ColumnStore
* @param oldStore the ColumnStore being filtered, which is source or a subset of source
* @param filtersToAdd the new filters
* @returns a ColumnStore with all of oldStore's filters and filtersToAdd applied
*/
function filterColumnStore(source, oldStore) {
var filtersToAdd = [];
for (var _i = 2; _i < arguments.length; _i++) {
filtersToAdd[_i - 2] = arguments[_i];
}
if (filtersToAdd.length === 0) {
return oldStore;
}
var oldFilters = oldStore.filters();
var filtersForIteration = [];
var indexedValueFilters = [];
var unionFilters = [];
var numNewFilters = 0; // the total size of the buckets above, tracked separately to avoid bugs
deduplicate: for (var i = 0; i < filtersToAdd.length; i++) {
var newFilter = filtersToAdd[i];
for (var j = 0; j < oldFilters.length; j++) {
var oldFilter = oldFilters[j];
if (oldFilter.equals(newFilter)) {
continue deduplicate;
}
}
var filterTarget = filtersForIteration; // Determines which bucket this filter belongs in
if (newFilter instanceof ozone.UnionFilter) {
filterTarget = unionFilters;
}
else if (newFilter instanceof ozone.ValueFilter) {
var fieldId = newFilter.fieldDescriptor.identifier;
if (source.field(fieldId) instanceof columnStore.IndexedField) {
filterTarget = indexedValueFilters;
}
}
filterTarget.push(newFilter);
numNewFilters++;
}
if (numNewFilters === 0) {
return oldStore;
}
// IntSet intersection filtering
var set = oldStore.intSet();
if (indexedValueFilters.length > 0) {
for (var i = 0; i < indexedValueFilters.length; i++) {
var intSetFilter = indexedValueFilters[i];
var fieldId = intSetFilter.fieldDescriptor.identifier;
var field = source.field(fieldId);
var fieldIntSet = field.intSetForValue(intSetFilter.value);
set = ozone.intSet.intersectionOfIntSets(set, fieldIntSet);
}
}
// Iterative filtering
if (filtersForIteration.length > 0) {
var setBuilder = ozone.intSet.builder(set.min(), set.max());
set.each(function (rowToken) {
for (var i = 0; i < filtersForIteration.length; i++) {
var filter = filtersForIteration[i];
if (!filter.matches(oldStore, rowToken)) {
return;
}
setBuilder.onItem(rowToken);
}
});
set = setBuilder.onEnd();
}
// Unions, done last because they are slowest
if (unionFilters.length > 0) {
unionFilters.forEach(function (f) {
set = unionColumnStore(source, set, f.filters);
});
}
var newFilters = oldStore.filters().concat(filtersForIteration, indexedValueFilters);
newFilters.sort(compareFilterByName);
return new FilteredColumnStore(source, newFilters, set);
}
columnStore.filterColumnStore = filterColumnStore;
function applyFilter(source, initialSet, filter) {
if (filter instanceof ozone.UnionFilter) {
return unionColumnStore(source, initialSet, filter.filters);
}
if (filter instanceof ozone.ValueFilter) {
var intSetFilter = filter;
var fieldId = intSetFilter.fieldDescriptor.identifier;
var field = source.field(fieldId);
if (source.field(fieldId) instanceof columnStore.IndexedField) {
var fieldIntSet = field.intSetForValue(intSetFilter.value);
return ozone.intSet.intersectionOfIntSets(initialSet, fieldIntSet);
}
}
var setBuilder = ozone.intSet.builder(initialSet.min(), initialSet.max());
initialSet.each(function (rowToken) {
if (filter.matches(source, rowToken)) {
setBuilder.onItem(rowToken);
}
});
return setBuilder.onEnd();
}
function unionColumnStore(source, initialSet, filters) {
if (filters.length === 0) {
return initialSet;
}
var toUnion = [];
for (var i = 0; i < filters.length; i++) {
toUnion.push(applyFilter(source, initialSet, filters[i]));
}
return initialSet.intersectionOfUnion(toUnion);
}
function compareFilterByName(a, b) {
if (a.displayName < b.displayName)
return -1;
if (a.displayName > b.displayName)
return 1;
return 0;
}
function partitionColumnStore(store, field) {
if (store.rowCount() === 0) {
return {};
}
var indexedField;
if (field instanceof columnStore.IndexedField) {
indexedField = field;
}
else {
var indexedFieldBuilder = columnStore.IndexedField.builder(field);
store.eachRow(function (row) {
indexedFieldBuilder.onItem({ index: row, rowToken: row });
});
indexedField = indexedFieldBuilder.onEnd();
}
var result = {};
var allValues = indexedField.allValues();
for (var i = 0; i < allValues.length; i++) {
var value = allValues[i];
var filtered = store.filter(new ozone.ValueFilter(field, value));
if (filtered.size() > 0) {
result["" + value] = filtered;
}
}
return result;
}
columnStore.partitionColumnStore = partitionColumnStore;
var FilteredColumnStore = (function (_super) {
__extends(FilteredColumnStore, _super);
function FilteredColumnStore(source, filterArray, filterBits) {
_super.call(this, source);
this.source = source;
this.filterArray = filterArray;
this.filterBits = filterBits;
this.cachedSize = null;
}
FilteredColumnStore.prototype.size = function () {
if (this.cachedSize === null) {
this.cachedSize = (this.sizeField()) ? this.sum(this.sizeField()) : this.rowCount();
}
return this.cachedSize;
};
FilteredColumnStore.prototype.rowCount = function () { return this.filterBits.size(); };
FilteredColumnStore.prototype.sum = function (field) { return columnStore.sum(this, field); };
FilteredColumnStore.prototype.intSet = function () { return this.filterBits; };
FilteredColumnStore.prototype.eachRow = function (rowAction) { this.filterBits.each(rowAction); };
FilteredColumnStore.prototype.filter = function (fieldNameOrFilter, value) {
return filterColumnStore(this.source, this, createFilter(this, fieldNameOrFilter, value));
};
FilteredColumnStore.prototype.filters = function () { return this.filterArray; };
FilteredColumnStore.prototype.simplifiedFilters = function () { return this.filterArray; };
FilteredColumnStore.prototype.removeFilter = function (filter) {
var newFilters = [];
for (var i = 0; i < this.filterArray.length; i++) {
var f = this.filterArray[i];
if (!f.equals(filter)) {
newFilters.push(f);
}
}
var result = this.source;
for (var i = 0; i < newFilters.length; i++) {
result = result.filter(newFilters[i]);
}
return result;
};
FilteredColumnStore.prototype.partition = function (fieldAny) {
var key = (typeof fieldAny === 'string') ? fieldAny : fieldAny.identifier;
return partitionColumnStore(this, this.field(key));
};
return FilteredColumnStore;
})(ozone.StoreProxy);
columnStore.FilteredColumnStore = FilteredColumnStore;
})(columnStore = ozone.columnStore || (ozone.columnStore = {}));
})(ozone || (ozone = {}));
/**
* Copyright 2013 by Vocal Laboratories, Inc. Distributed under the Apache License 2.0.
*/
/// <reference path='../_all.ts' />
var ozone;
(function (ozone) {
var columnStore;
(function (columnStore) {
/**
* A Field which stores values in an index, and each value is mapped to a list of row identifiers. This is similar
* to an SQL index on a column, except that SQL databases store both a row and (optionally) an index, whereas this
* Field only stores the index-- the row itself is nothing more than an identifying row number.
*
* <p><b>Although values need not be strings, they are identified internally by their toString method.</b></p>
*
* It is legal for values to have empty intSets; for example, a Month
* field might contain all the months of the year in order, even if only a few have any values, to guarantee that
* the UI looks right.
*/
var IndexedField = (function () {
function IndexedField(descriptor, valueList, valueMap) {
this.valueList = valueList;
this.valueMap = valueMap;
var range = descriptor.range();
if (typeof range === 'undefined' || descriptor.typeOfValue === 'number') {
range = null;
}
this.identifier = descriptor.identifier;
this.displayName = descriptor.displayName;
this.typeOfValue = descriptor.typeOfValue;
this.typeConstructor = descriptor.typeConstructor;
this.rangeVal = range;
this.aggregationRule = (descriptor.aggregationRule) ? descriptor.aggregationRule : null;
}
/**
* Returns a reducer that can be run on a source DataStore to reproduce a sourceField.
*
* @param sourceField the field which will be replicated
* @param params additional parameters:
* values -- if provided, this is the list of values used and any values not in this
* list are ignored. This also defines the order of values.
* intSetSource -- if provided, a IntSetBuilding to override the default. The default may
* change, and may be browser specific or determined based on the
* characteristics of sourceField.
*/
IndexedField.builder = function (sourceField, params) {
if (params === void 0) { params = {}; }
var descriptor = ozone.mergeFieldDescriptors(sourceField, params);
var addValues = !params.values;
var valueList = []; // (addValues) ? [] : params.values.concat();
if (params.values) {
for (var i = 0; i < params.values.length; i++) {
valueList.push(ozone.convert(params.values[i], descriptor));
}
}
var intSetSource = (params.intSetSource)
? params.intSetSource
: ozone.intSet.ArrayIndexIntSet;
var intSetBuilders = {};
for (var i = 0; i < valueList.length; i++) {
var value = ozone.convert(valueList[i], descriptor);
intSetBuilders[value.toString()] = intSetSource.builder();
}
return {
onItem: function (indexedRowToken) {
var values = sourceField.values(indexedRowToken.rowToken);
for (var i = 0; i < values.length; i++) {
var value = ozone.convert(values[i], descriptor);
var builder = intSetBuilders[value.toString()];
if (typeof (builder) === "undefined" && addValues) {
builder = intSetSource.builder();
intSetBuilders[value.toString()] = builder;
valueList.push(value);
}
if (typeof (builder) === "object") {
builder.onItem(indexedRowToken.index);
}
}
},
onEnd: function () {
var valueMap = {};
if (addValues && valueList.length > 0) {
var firstValue = valueList[0];
if (firstValue instanceof Date) {
valueList.sort(function (a, b) { return a.getTime() - b.getTime(); });
}
else {
valueList.sort();
}
}
for (var i = 0; i < valueList.length; i++) {
var valueStr = valueList[i].toString();
valueMap[valueStr] = intSetBuilders[valueStr].onEnd();
}
return new IndexedField(descriptor, valueList, valueMap);
}
};
};
IndexedField.prototype.allValues = function () {
return this.valueList.concat();
};
IndexedField.prototype.values = function (rowToken) {
var index = rowToken;
var result = [];
for (var i = 0; i < this.valueList.length; i++) {
var value = this.valueList[i];
var intSet = this.valueMap[value.toString()];
if (intSet.has(index)) {
result.push(value);
}
}
return result;
};
IndexedField.prototype.range = function () {
return this.rangeVal;
};
/** Equivalent to allValues().length. */
IndexedField.prototype.distinctValueEstimate = function () {
return this.valueList.length;
};
IndexedField.prototype.rowHasValue = function (index, value) {
var intSet = this.valueMap[value.toString()];
if (intSet) {
return intSet.has(index);
}
return false;
};
/** Return the intSet matching value.toString(), or an empty intSet if the value is not found. */
IndexedField.prototype.intSetForValue = function (value) {
var set = this.valueMap[value.toString()];
return (set) ? set : ozone.intSet.empty;
};
return IndexedField;
})();
columnStore.IndexedField = IndexedField;
})(columnStore = ozone.columnStore || (ozone.columnStore = {}));
})(ozone || (ozone = {}));
/**
* Copyright 2014 by Vocal Laboratories, Inc. All rights reserved.
*/
/// <reference path='../_all.ts' />
/**
* Bitwise operations on 32-bit numbers. These match the asm.js standard for "int": 32-bit, unknown sign,
* intended for bitwise use only. In practice, JavaScript bitwise operators convert numbers to 32-bit two's-complement,
* so that's what we use here. We might actually use asm.js at some point, but hand coding it is a pain (see
* https://github.com/zbjornson/human-asmjs).
*
* See: http://asmjs.org/spec/latest/
*/
var ozone;
(function (ozone) {
var intSet;
(function (intSet) {
var bits;
(function (bits) {
function singleBitMask(bitPos) {
return 1 << (bitPos % 32);
}
bits.singleBitMask = singleBitMask;
/** Return a number with the bit at num%32 set to true. */
function setBit(num, word) {
word = word | 0; // JIT hint, same one used by asm.js to signify a bitwise int. Also clears high bits.
var mask = singleBitMask(num);
var result = 0;
result = word | mask;
return result;
}
bits.setBit = setBit;
/** Return a number with the bit at num%32 set to false. */
function unsetBit(num, word) {
word = word | 0;
var mask = ~singleBitMask(num);
return word & mask;
}
bits.unsetBit = unsetBit;
/** Return true if the bit num%32 is set*/
function hasBit(num, word) {
if (word == null)
return false;
if (word & singleBitMask(num)) {
return true;
}
else {
return false;
}
}
bits.hasBit = hasBit;
/** Returns the number of 1's set within the first 32-bits of this number. */
function countBits(word) {
if (word == null)
return 0;
word = word | 0; // This is not just a JIT hint: clears the high bits
var result = 0;
result = word & 1;