forked from nezroy/EVEoj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDD.Table.js
More file actions
298 lines (272 loc) · 7.8 KB
/
Copy pathSDD.Table.js
File metadata and controls
298 lines (272 loc) · 7.8 KB
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
var extend = require("node.extend"),
Utils = require("./Utils"),
Promise = require("./Promise");
var P = exports.P = {}; // public methods
// default object properties
exports.D = {
src: null, // the EVEoj.SDD.Source that owns this table
name: null, // the name of this table
keyname: null, // the primary key name
columns: [], // the list of columns
colmap: {}, // a reverse lookup map for column indexes
c: null, // shortcut to colmap
colmeta: {}, // a map of metainfo about each complex column
subkeys: [], // any subkeys (this implies a nested entry structure)
data: {}, // the data for this table (shallow references into raw data from source)
segments: [], // the segment information for this table
length: 0, // the total number of entries in this table
loaded: 0 // the total number of currently loaded entries
};
exports.Create = function(name, src, meta) {
var obj,
i,
keyarr;
obj = Utils.create(P);
extend(true, obj, exports.D);
// sort out relevant metadata details
obj.src = src;
obj.name = name;
// determine the source(s) of this table's data
if (meta.hasOwnProperty("j")) {
// only one segment and it is stored with other stuff
obj.segments.push({
min: 0,
max: -1,
tag: meta.j,
loaded: false,
p: null
});
} else if (meta.hasOwnProperty("s")) {
// at least one segment that is stored independently
for (i = 0; i < meta.s.length; i++) {
obj.segments.push({
min: meta.s[i][1],
max: meta.s[i][2],
tag: name + "_" + meta.s[i][0],
loaded: false,
p: null
});
}
}
// find out the key info for this table
if (meta.hasOwnProperty("k")) {
keyarr = meta.k.split(":");
obj.keyname = keyarr.shift();
for (i = 0; i < keyarr.length; i++) obj.subkeys.push(keyarr[i]);
}
// add keys to the column definition
if (obj.keyname) obj.columns.push(obj.keyname);
else obj.columns.push("index");
for (i = 0; i < obj.subkeys.length; i++) {
obj.columns.push(obj.subkeys[i]);
}
// add meta columns to column definition
if (meta.hasOwnProperty("c")) {
for (i = 0; i < meta.c.length; i++) obj.columns.push(meta.c[i]);
}
// create a reverse lookup map for columns
for (i = 0; i < obj.columns.length; i++) obj.colmap[obj.columns[i]] = i;
obj.colmap.index = 0;
obj.c = obj.colmap;
// grab the colmeta extra info
if (meta.hasOwnProperty("m")) {
extend(true, obj.colmeta, meta.m);
}
// grab the length
if (meta.hasOwnProperty("l")) {
obj.length = meta.l;
}
return obj;
};
// get the entry for the key provided; all keys must be numeric values for segmentation
P.GetEntry = function(key) {
var i,
nkey,
skey;
// get a guaranteed numeric and guaranteed string version of the key; numeric
// is for segment comparison, string is for object property lookup
nkey = parseInt(key, 10);
if (isNaN(nkey)) return null;
skey = nkey.toString(10);
if (this.data.hasOwnProperty(skey)) return this.data[skey];
// if we don't have this key, determine if we ought to by now
for (i = 0; i < this.segments.length; i++) {
if (nkey >= this.segments[i].min && (nkey <= this.segments[i].max || this.segments[i].max == -1)) {
if (this.segments[i].loaded) return null; // the key should be in this segment
else return false; // the segment isn't loaded yet
}
}
return null;
};
// get the value for the key (or entry array) and column provided
P.GetValue = function(key, col) {
var entry;
if (key instanceof Array) entry = key;
else entry = this.GetEntry(key);
if (entry === null || entry === false) return entry;
if (isNaN(col)) {
if (!this.colmap.hasOwnProperty(col)) return null;
col = this.colmap[col];
}
return entry[col];
};
function UnshiftIndexes(data, indexes) {
var key, i;
for (key in data) {
if (!data.hasOwnProperty(key)) return;
if (!data[key]) return;
indexes.push(parseInt(key, 10));
if (data[key] instanceof Array) {
for (i = indexes.length - 1; i >= 0; i--) {
data[key].unshift(indexes[i]);
}
} else UnshiftIndexes(data[key], indexes);
indexes.pop();
}
}
function SegLoadDone(res, rej, tag, data, done, ctx, progress) {
var i;
done.has++;
for (i = 0; i < this.segments.length; i++) {
if (this.segments[i].tag != tag) continue;
if (data.tables.hasOwnProperty(this.name) && data.tables[this.name].hasOwnProperty("d")) {
if (!data.tables[this.name].hasOwnProperty("U")) {
// put the indexes into the first columns of every row
UnshiftIndexes(data.tables[this.name].d, []);
data.tables[this.name].U = true;
}
extend(this.data, data.tables[this.name].d);
if (data.tables[this.name].hasOwnProperty("L")) {
this.loaded += data.tables[this.name].L;
} else if (done.needs == 1) {
this.loaded = this.length;
}
this.segments[i].loaded = true;
}
break;
}
if (progress !== null) progress({
context: ctx,
table: this,
has: done.has,
needs: done.needs
});
if (done.has >= done.needs) res({
context: ctx,
table: this
});
}
// load data for this table; returns a deferred promise object as this is an async thing
// if key is provided, loads ONLY the segment containing that key
P.Load = function(opts) {
var self = this,
all_needs = [],
nkey,
skey,
i,
segment,
o = {
context: null,
key: null,
progress: null
};
extend(o, opts);
// figure out which segments need loading
if (o.key === null) {
// no key specified; load all segments
for (i = 0; i < this.segments.length; i++) {
if (!this.segments[i].loaded) {
// this segment not yet loaded
all_needs.push(i);
}
}
} else {
// determine which segment the key is in
nkey = parseInt(o.key, 10);
if (isNaN(nkey)) {
return Promise.reject(new Error("invalid key; not numeric"));
}
skey = nkey.toString(10);
segment = -1;
for (i = 0; i < this.segments.length; i++) {
if (nkey >= this.segments[i].min && (nkey <= this.segments[i].max || this.segments[i].max == -1)) {
// the key should be in this segment
segment = i;
break;
}
}
if (segment === -1) {
return Promise.reject(new Error("invalid key; no segment contains it"));
}
if (!this.segments[segment].loaded) {
all_needs.push(segment);
}
}
if (all_needs.length < 1) {
// all required data already loaded
return Promise.resolve({
context: o.context,
table: this
});
}
return new Promise(function(res, rej) {
var done = {
needs: all_needs.length,
has: 0
};
var thenDone = function(arg) {
SegLoadDone.apply(self, [res, rej, arg.tag, arg.data, done, o.context, o.progress]);
};
for (i = 0; i < all_needs.length; i++) {
segment = self.segments[all_needs[i]];
if (!segment.p) {
// this segment not pending load
segment.p = self.src.LoadTag(segment.tag);
}
segment.p.then(thenDone);
}
});
};
P.ColIter = function(colname) {
var colnum;
if (this.colmap.hasOwnProperty(colname)) {
colnum = this.colmap[colname];
return function(e) {
return e[colnum];
};
} else return function() {
return undefined;
};
};
P.ColPred = function(colname, compare, value) {
var colnum;
if (this.colmap.hasOwnProperty(colname)) {
colnum = this.colmap[colname];
if (compare == "==" || compare == "eq") return function(e) {
return e[colnum] == value;
};
if (compare == "!=" || compare == "ne") return function(e) {
return e[colnum] != value;
};
if (compare == "===" || compare == "seq") return function(e) {
return e[colnum] === value;
};
if (compare == "!==" || compare == "sne") return function(e) {
return e[colnum] !== value;
};
else if (compare == ">" || compare == "gt") return function(e) {
return e[colnum] > value;
};
else if (compare == ">=" || compare == "gte") return function(e) {
return e[colnum] >= value;
};
else if (compare == "<" || compare == "lt") return function(e) {
return e[colnum] < value;
};
else if (compare == "<=" || compare == "lte") return function(e) {
return e[colnum] < value;
};
} else return function() {
return false;
};
};