-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathresultSet.js
413 lines (373 loc) · 10.2 KB
/
resultSet.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
"use strict";
/**
* @typedef {import('./graph')} Graph
*/
const Statistics = require("./statistics"),
Record = require("./record"),
Node = require("./node"),
Edge = require("./edge"),
Path = require("./path"),
ReplyError = require("redis").ReplyError;
/**
* @enum {number}
* @readonly
*/
const ResultSetColumnTypes = {
COLUMN_UNKNOWN: 0,
COLUMN_SCALAR: 1,
COLUMN_NODE: 2,
COLUMN_RELATION: 3,
};
/**
* @enum {number}
* @readonly
*/
const ResultSetValueTypes = {
VALUE_UNKNOWN: 0,
VALUE_NULL: 1,
VALUE_STRING: 2,
VALUE_INTEGER: 3,
VALUE_BOOLEAN: 4,
VALUE_DOUBLE: 5,
VALUE_ARRAY: 6,
VALUE_EDGE: 7,
VALUE_NODE: 8,
VALUE_PATH: 9,
VALUE_MAP: 10,
VALUE_POINT: 11,
};
/**
* Hold a query result
*/
class ResultSet {
/**
* Builds an empty ResultSet object.
*
* @param {Graph} graph
*/
constructor(graph) {
this._graph = graph; //_graph is graph api
this._position = 0; //allowing iterator like behevior
this._resultsCount = 0; //total number of records in this result set
this._header = []; //reponse schema columns labels
this._results = []; //result records
}
/**
* Parse raw response data to ResultSet object.
* @async
* @param {object[]} resp - raw response representation - the raw representation of response is at most 3 lists of objects.
* The last list is the statistics list.
*/
async parseResponse(resp) {
if (Array.isArray(resp)) {
let statistics = resp[resp.length - 1];
if (statistics instanceof ReplyError) throw statistics;
if (resp.length < 3) {
this._statistics = new Statistics(statistics);
} else {
await this.parseResults(resp);
this._resultsCount = this._results.length;
this._statistics = new Statistics(resp[2]);
}
} else {
this._statistics = new Statistics(resp);
}
return this;
}
/**
* Parse a raw response body into header an records.
* @async
* @param {object[]} resp raw response
*/
async parseResults(resp) {
this.parseHeader(resp[0]);
await this.parseRecords(resp);
}
/**
* A raw representation of a header (query response schema) is a list.
* Each entry in the list is a tuple (list of size 2).
* tuple[0] represents the type of the column, and tuple[1] represents the name of the column.
* @param {object[]} rawHeader raw header
*/
parseHeader(rawHeader) {
// An array of column name/column type pairs.
this._header = rawHeader;
// Discard header types.
this._typelessHeader = new Array(this._header.length);
for (var i = 0; i < this._header.length; i++) {
this._typelessHeader[i] = this._header[i][1];
}
}
/**
* The raw representation of response is at most 3 lists of objects. rawResultSet[1] contains the data records.
* Each entry in the record can be either a node, an edge or a scalar
* @async
* @param {object[]} rawResultSet raw result set representation
*/
async parseRecords(rawResultSet) {
let result_set = rawResultSet[1];
this._results = new Array(result_set.length);
for (var i = 0; i < result_set.length; i++) {
let row = result_set[i];
let record = new Array(row.length);
for (var j = 0; j < row.length; j++) {
let cell = row[j];
let cellType = this._header[j][0];
switch (cellType) {
case ResultSetColumnTypes.COLUMN_SCALAR:
record[j] = await this.parseScalar(cell);
break;
case ResultSetColumnTypes.COLUMN_NODE:
record[j] = await this.parseNode(cell);
break;
case ResultSetColumnTypes.COLUMN_RELATION:
record[j] = await this.parseEdge(cell);
break;
default:
console.log("Unknown column type.\n" + cellType);
break;
}
}
this._results[i] = new Record(this._typelessHeader, record);
}
}
/**
* Parse raw entity properties representation into a Map
* @async
* @param {object[]} props raw properties representation
* @returns {Promise<object>} Map with the parsed properties.
*/
async parseEntityProperties(props) {
// [[name, value, value type] X N]
let properties = {};
for (var i = 0; i < props.length; i++) {
let prop = props[i];
var propIndex = prop[0];
let prop_name = this._graph.getProperty(propIndex);
// will try to get the right property for at most 10 times
var tries = 0;
while (prop_name == undefined && tries < 10) {
prop_name = await this._graph.fetchAndGetProperty(propIndex);
tries++;
}
if (prop_name == undefined) {
console.warn(
"unable to retrive property name value for propety index " +
propIndex
);
}
let prop_value = await this.parseScalar(prop.slice(1, prop.length));
properties[prop_name] = prop_value;
}
return properties;
}
/**
* Parse label index into a label string.
* @async
* @param {number} label_idx index of label.
* @returns {Promise<string>} string representation of label.
*/
async parseNodeLabel(label_idx) {
let label = this._graph.getLabel(label_idx);
// will try to get the right label for at most 10 times
var tries = 0;
while (label == undefined && tries < 10) {
label = await this._graph.fetchAndGetLabel(label_idx);
tries++;
}
if (label == undefined) {
console.warn(
"unable to retrieve label value for label index " + label_idx
);
}
return label;
}
/**
* Parse raw node representation into a Node object.
* @async
* @param {object[]} cell raw node representation.
* @returns {Promise<Node>} Node object.
*/
async parseNode(cell) {
// Node ID (integer),
// [label string offset (integer) X N],
// [[name, value, value type] X N]
let node_id = cell[0];
var labels = undefined;
if (cell[1].length == 1) {
labels = await this.parseNodeLabel(cell[1][0]);
} else {
labels = await Promise.all(cell[1].map(x => this.parseNodeLabel(x)));
}
let properties = await this.parseEntityProperties(cell[2]);
let node = new Node(labels, properties);
node.setId(node_id);
return node;
}
/**
* Parse a raw edge representation into an Edge object.
* @async
* @param {object[]} cell raw edge representation
* @returns {Promise<Edge>} Edge object.
*/
async parseEdge(cell) {
// Edge ID (integer),
// reltype string offset (integer),
// src node ID offset (integer),
// dest node ID offset (integer),
// [[name, value, value type] X N]
let edge_id = cell[0];
let relation = this._graph.getRelationship(cell[1]);
// will try to get the right relationship type for at most 10 times
var tries = 0;
while (relation == undefined && tries < 10) {
relation = await this._graph.fetchAndGetRelationship(cell[1]);
tries++;
}
if (relation == undefined) {
console.warn(
"unable to retrive relationship type value for relationship index " +
cell[1]
);
}
let src_node_id = cell[2];
let dest_node_id = cell[3];
let properties = await this.parseEntityProperties(cell[4]);
let edge = new Edge(src_node_id, relation, dest_node_id, properties);
edge.setId(edge_id);
return edge;
}
/**
* Parse and in-place replace raw array into an array of values or objects.
* @async
* @param {object[]} rawArray raw array representation
* @returns {Promise<object[]>} Parsed array.
*/
async parseArray(rawArray) {
for (var i = 0; i < rawArray.length; i++) {
rawArray[i] = await this.parseScalar(rawArray[i]);
}
return rawArray;
}
/**
* Parse a raw path representation into Path object.
* @async
* @param {object[]} rawPath raw path representation
* @returns {Promise<Path>} Path object.
*/
async parsePath(rawPath) {
let nodes = await this.parseScalar(rawPath[0]);
let edges = await this.parseScalar(rawPath[1]);
return new Path(nodes, edges);
}
/**
* Parse a raw map representation into Map object.
* @async
* @param {object[]} rawMap raw map representation
* @returns {Promise<object>} Map object.
*/
async parseMap(rawMap) {
let m = {};
for (var i = 0; i < rawMap.length; i+=2) {
var key = rawMap[i];
m[key] = await this.parseScalar(rawMap[i+1]);
}
return m;
}
/**
* Parse a raw Point representation into a lat-lon Map object.
* @param {object[]} rawPoint 2-valued lat-lon array representation
* @returns {{ latitude: number, longitude: number }} Map object with latitude and longitude keys.
*/
parsePoint(rawPoint) {
let m = {};
m["latitude"] = Number(rawPoint[0])
m["longitude"] = Number(rawPoint[1])
return m;
}
/**
* Parse a raw value into its actual value.
* @async
* @param {object[]} cell raw value representation
* @returns {Promise<object>} Actual value - scalar, array, Node, Edge, Path
*/
async parseScalar(cell) {
let scalar_type = cell[0];
let value = cell[1];
let scalar = undefined;
switch (scalar_type) {
case ResultSetValueTypes.VALUE_NULL:
scalar = null;
break;
case ResultSetValueTypes.VALUE_STRING:
scalar = String(value);
break;
case ResultSetValueTypes.VALUE_INTEGER:
case ResultSetValueTypes.VALUE_DOUBLE:
scalar = Number(value);
break;
case ResultSetValueTypes.VALUE_BOOLEAN:
if (value === "true") {
scalar = true;
} else if (value === "false") {
scalar = false;
} else {
console.log("Unknown boolean type\n");
}
break;
case ResultSetValueTypes.VALUE_ARRAY:
scalar = this.parseArray(value);
break;
case ResultSetValueTypes.VALUE_NODE:
scalar = await this.parseNode(value);
break;
case ResultSetValueTypes.VALUE_EDGE:
scalar = await this.parseEdge(value);
break;
case ResultSetValueTypes.VALUE_PATH:
scalar = await this.parsePath(value);
break;
case ResultSetValueTypes.VALUE_MAP:
scalar = await this.parseMap(value);
break;
case ResultSetValueTypes.VALUE_POINT:
scalar = this.parsePoint(value);
break;
case ResultSetValueTypes.VALUE_UNKNOWN:
console.log("Unknown scalar type\n");
break;
}
return scalar;
}
/**
* @returns {string[] }ResultSet's header.
*/
getHeader() {
return this._typelessHeader;
}
/**
* @returns {boolean} If the ResultSet object can return additional records.
*/
hasNext() {
return this._position < this._resultsCount;
}
/**
* @returns {Record} The current record.
*/
next() {
return this._results[this._position++];
}
/**
* @returns {Statistics} ResultsSet's statistics.
*/
getStatistics() {
return this._statistics;
}
/**
* @returns {number} Result set size. (integer)
*/
size() {
return this._resultsCount;
}
}
module.exports = ResultSet;