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
249 lines (223 loc) · 7.29 KB
/
Copy pathSDD.Table.js
File metadata and controls
249 lines (223 loc) · 7.29 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
EVEoj.SDD.Table = EVEoj.SDD.Table || {};
(function () {
var ME = EVEoj.SDD.Table,
// namespace quick refs
E = EVEoj,
SDD = EVEoj.data,
_P = {}, // private methods
P = {} // public methods
;
// default object properties
ME.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
'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
};
ME.Create = function (name, src, meta) {
var obj,
i,
keyarr
;
obj = E.create(P);
E.extend(true, obj, ME.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;
// grab the colmeta extra info
if (meta.hasOwnProperty('m')) {
E.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);
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];
};
_P.UnshiftIndexes = function(data, indexes) {
var key, i;
for (key in data) {
if (!data.hasOwnProperty(key)) return;
if (!data[key]) return;
indexes.push(key);
if (data[key] instanceof Array) {
for (i = indexes.length - 1; i >= 0; i--) {
data[key].unshift(indexes[i]);
}
indexes.pop();
}
else _P.UnshiftIndexes(data[key], indexes);
}
};
_P.SegLoadDone = function(tag, data, done, p, ctx) {
var i, key;
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
_P.UnshiftIndexes(data['tables'][this.name]['d'], []);
data['tables'][this.name]['U'] = true;
}
E.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;
}
}
break;
}
if (done.has >= done.needs) p.resolveWith(ctx, [this]);
else p.notifyWith(ctx, [this, done.has, done.needs]);
};
_P.SegLoadFail = function(tag, status, error, p, ctx) {
p.rejectWith(ctx, [this, status, error]);
};
// 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 p = E.deferred(),
self = this,
all_needs,
done,
nkey,
skey,
i,
segment,
o = {'ctx': null, 'key': null}
;
E.extend(o, opts);
if (o.key === null) {
// load all segments
all_needs = [];
for (i = 0; i < this.segments.length; i++) {
if (!this.segments[i].loaded) {
// this segment not yet loaded
all_needs.push(i);
}
}
done = {'needs': all_needs.length, 'has': 0};
if (all_needs.length > 0) {
for (i = 0; i < all_needs.length; i++) {
if (!this.segments[all_needs[i]].p) {
// this segment not pending load
this.segments[all_needs[i]].p = this.src.LoadTag(this.segments[i].tag);
}
this.segments[all_needs[i]].p
.done(function (tag, data) { _P.SegLoadDone.apply(self, [tag, data, done, p, o.ctx]) })
.fail(function (tag, status, error) { _P.SegLoadFail.apply(self, [tag, status, error, p, o.ctx]) });
}
return p.promise();
}
else {
p.resolveWith(o.ctx, [this]);
return p.promise();
}
}
else {
// determine which segment the key is in
nkey = parseInt(o.key);
if (isNaN(nkey)) {
p.rejectWith(o.ctx, [this, 'badkey', 'invalid key; not numeric']);
return this.p.promise();
}
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 = this.segments[i];
break;
}
}
if (segment === -1) return p.rejectWith(o.ctx, [this, 'badkey', 'invalid key; no segment contains it']).promise();
if (segment.loaded) return p.resolveWith(o.ctx, [this]).promise();
if (segment.p == null) segment.p = this.src.LoadTag(segment.tag);
done = {'needs': 1, 'has': 0};
segment.p
.done(function (tag, data) { _P.SegLoadDone.apply(self, [tag, data, done, p, o.ctx]) })
.fail(function (tag, status, error) { _P.SegLoadFail.apply(self, [tag, status, error, p, o.ctx]) });
return p.promise();
}
};
P.ColIter = function (colname) {
var colnum;
if (this.colmap.hasOwnProperty(colname)) {
colnum = this.colmap[colname];
return function (e) { return e[colnum] };
}
else return function (e) { return undefined };
};
})();