-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstrelki.js
executable file
·342 lines (303 loc) · 10 KB
/
strelki.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
"use strict";
/**
* StrelkiJS module
* @module StrelkiJS
* @author A. Maximov amaksr
*/
(function(exports){
/**
* Create new IndexArray object
* @class
* IndexedArray is a hybrid array with multiple hash indexes.
* It can have additional "indexes": hash objects that point to elements and that can be quickly accessed by index value.
* Elements also can be accessed by position.
* @name IndexedArray
* @constructor
*/
var IndexedArray = function() {
this.data = {};
this.data_ids = null;
this.indexData = {};
this.ordered = false;
}
/**
* Set flag that indicates whether array should be ordered by "id" or not
* @param {boolean} ordered
*/
IndexedArray.prototype.setOrdered = function(ordered) {
this.ordered = ordered;
if(this.ordered && this.data_ids)
this.data_ids.sort();
};
IndexedArray.prototype._getDataIds = function() {
if(!this.data_ids) {
this.data_ids = Object.keys(this.data);
if(this.ordered)
this.data_ids.sort();
}
return this.data_ids;
};
/**
* Creates hash index on array using fieldName property of the elements
* @param {String} fieldName
*/
IndexedArray.prototype.createIndex = function(fieldName) {
if(fieldName === 'id')
return;
var idxData = {};
var dataKeys = this._getDataIds();
for(var i=0; i<dataKeys.length; i++) {
var id = dataKeys[i];
var e = this.data[id];
var fv = e[fieldName];
if(!idxData[fv])
idxData[fv] = {};
idxData[fv][id] = 1;
}
this.indexData[fieldName] = idxData;
};
/**
* Returns hash array of objects
* @returns {{}|*}
*/
IndexedArray.prototype.getData = function() {
return this.data;
};
/**
* Puts all the elements from array
* @param {Array} array
*/
IndexedArray.prototype.loadArray = function(array) {
if(!array || !array.length)
return;
for(var i=0; i<array.length; i++)
this.put(array[i]);
};
/**
* Drops index fieldName
* @param {String} fieldName
*/
IndexedArray.prototype.dropIndex = function(fieldName) {
delete this.indexData[fieldName];
};
/**
* Drops all indexes
* @param fieldName
*/
IndexedArray.prototype.dropAllIndexes = function(fieldName) {
this.indexData={};
};
/**
* Returns number of elements in the array
* @returns {Number}
*/
IndexedArray.prototype.length = function() {
return this._getDataIds().length;
};
/**
* Delete all data in the array
*/
IndexedArray.prototype.empty = function() {
this.data = {};
this.data_ids = null;
var indexes = Object.keys(this.indexData);
for(var i = 0; i < indexes.length; i++)
this.indexData[indexes[i]] = {};
};
/**
* Returns record by "id" field
* @param id
* @returns {*}
*/
IndexedArray.prototype.get = function(id) {
return this.data[id];
};
/**
* Returns record at given position
* @param {Number} pos - position of element
* @returns {*}
*/
IndexedArray.prototype.getAt = function(pos) {
return this.data[this._getDataIds()[pos]];
};
/**
* Stores element with unique "id" to array. If element with given "id" was already there, it will be overriden with the new element
* @param element
*/
IndexedArray.prototype.put = function(element) {
this.del(element.id);
this.data_ids = null;
this.data[element.id] = element;
var indexes = Object.keys(this.indexData);
for(var i = 0; i < indexes.length; i++) {
var fld = element[indexes[i]];
var idxData = this.indexData[indexes[i]];
if(!idxData[fld])
idxData[fld] = {};
idxData[fld][element.id] = 1;
}
};
/**
* Delete element by "id"
* @param id
*/
IndexedArray.prototype.del = function(id) {
var e = this.data[id];
if(!e)
return;
var indexes = Object.keys(this.indexData);
for(var i = 0; i < indexes.length; i++) {
var idxData = this.indexData[indexes[i]];
var idxElements = idxData[e[indexes[i]]];
if(idxElements) {
delete idxElements[id];
if(Object.keys(idxElements).length === 0)
delete idxData[e[indexes[i]]];
}
}
delete this.data[id];
this.data_ids = null;
};
/**
* Delete multimple elements by array of ids
* @param {Array} ids
*/
IndexedArray.prototype.delMany = function(ids) {
for(var i=0; i<ids.length; i++)
this.del(ids[i]);
};
/**
* Returns IndexedArray converted to regular Array
* @returns {Array}
*/
IndexedArray.prototype.toArray = function() {
var res = [];
var ids = this._getDataIds();
for(var i =0; i<ids.length; i++)
res.push(this.get(ids[i]));
return res;
};
/**
* Find elements by index value, and return their ids
* @param {String} filedName - index name
* @param {String} value - index value
* @returns {Array} - array of ids, empty array if not found
*/
IndexedArray.prototype.findIdsByIndex = function(filedName,value) {
if(filedName === 'id') {
var val = this.data[value];
if(val)
return [val.id];
else
return [];
}
else {
var idxData = this.indexData[filedName];
if(!idxData)
throw "Index " + filedName + " does not exist";
if(!idxData[value])
return [];
return Object.keys(idxData[value]);
}
};
IndexedArray.prototype.dump = function() {
console.log("dump: indexes="+JSON.stringify(this.indexData));
console.log("dump: data="+JSON.stringify(this.data));
};
/**
* For element el perform lookups according to joinInfoArray, and return all related records
* @param el - element
* @param {Array} joinInfoArray
*/
IndexedArray.doLookups = function(el, joinInfoArray) {
var res=[];
for(var i=0; i<joinInfoArray.length; i++) {
var ji = joinInfoArray[i];
var joins = IndexedArray.doLookup(el, ji);
res = IndexedArray._merge(res,joins);
}
return res;
};
IndexedArray.doLookup = function(el, joinInfo) {
var res = [];
var from_col = joinInfo.from_col;
var to_table = joinInfo.to_table;
var to_col = joinInfo.to_col;
var to_ids = el?to_table.findIdsByIndex(to_col,el[from_col]):[];
if(to_ids.length == 0 && joinInfo.type === 'outer')
to_ids.push(null);
for(var i=0; i < to_ids.length; i++) {
var to_id = to_ids[i];
var to_el = to_id?to_table.getData()[to_id]:null;
if(joinInfo['join']) {
var joins = IndexedArray.doLookups(to_el,joinInfo['join']);
for(var j=0; j<joins.length; j++)
res.push([to_el].concat(joins[j]));
}
else
res.push([to_el]);
}
return res;
};
/**
* Join other IndexedArrays according to joinInfoArray
* <pre>
* joinInfoArray = [
* {
* from_col: mandatory field name in 'el' element
* to_table: mandatory reference to IndexedArray
* to_col: mandatory id or other indexed field in referenced IndexedArray
* type: 'outer' for outer join, null for inner join
* join: optional nested joinInfoArray structure
* },
*
* ]
* </pre>
* @param joinInfoArray
* @returns {Array} - array of records, where each record is an array of joined elements
*/
IndexedArray.prototype.query = function(joinInfoArray) {
var res = [];
var mainKeys = this._getDataIds();
for(var i=0; i < mainKeys.length; i++) {
var el = this.data[mainKeys[i]];
var joins = IndexedArray.doLookups(el,joinInfoArray);
for(var j=0; j<joins.length; j++)
res.push([el].concat(joins[j]));
}
return res;
};
/**
* Search array by index, and then by filterCallback function
* @param fieldName - name of the index, null to select all elements
* @param value - value of the index
* @param filterCallback - filter function that eccepts element as a parameter, and returns true or false
* @returns {IndexedArray} - new IndexedArray with selected records
*/
IndexedArray.prototype.where = function(fieldName, value, filterCallback) {
var res = new IndexedArray();
var ids;
if(fieldName)
ids = this.findIdsByIndex(fieldName, value);
else
ids = this._getDataIds();
for(var i = 0; i < ids.length; i++) {
var el = this.get(ids[i]);
if(!filterCallback || filterCallback && filterCallback(el))
res.put(el);
}
return res;
};
IndexedArray._merge = function(arr1, arr2) {
if(arr1.length === 0)
return arr2;
var res=[];
for(var i=0; i < arr1.length; i++) {
for(var j=0; j < arr2.length; j++) {
res.push(arr1[i].concat(arr2[j]));
}
}
return res;
};
exports.IndexedArray = IndexedArray;
})(typeof exports === 'undefined'? this['StrelkiJS']={} : exports);