Skip to content

Commit bc075a3

Browse files
committed
Fix Path handling.
1 parent 8c09112 commit bc075a3

File tree

1 file changed

+82
-78
lines changed

1 file changed

+82
-78
lines changed

neo4j.js

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var MAX_CHUNK_SIZE = 16383,
2424

25-
// Signature bytes for each message type
25+
// Signature bytes for each message type
2626
INIT = 0x01, // 0000 0001 // INIT <user_agent>
2727
ACK_FAILURE = 0x0F, // 0000 1111 // ACK_FAILURE
2828
RUN = 0x10, // 0001 0000 // RUN <statement> <parameters>
@@ -61,19 +61,19 @@
6161
MAP_32 = 0xDA,
6262
STRUCT_8 = 0xDC,
6363
STRUCT_16 = 0xDD,
64-
64+
6565
USER_AGENT = "neo4j-javascript/0.0";
6666

6767
function Structure(signature, fields) {
6868
this.signature = signature;
6969
this.fields = fields;
7070
}
71-
71+
7272
function Node(identity, labels, properties) {
7373
this.identity = identity;
7474
this.labels = labels;
7575
this.properties = properties;
76-
76+
7777
this.toString = function toString() {
7878
var s = "(" + this.identity.split('/')[1];
7979
for (var i = 0; i < this.labels.length; i++) {
@@ -99,7 +99,7 @@
9999
this.end = end;
100100
this.type = type;
101101
this.properties = properties;
102-
102+
103103
this.toString = function toString() {
104104
var s = "(" + this.start.split('/')[1] + ")-[:" + this.type;
105105
var keys = Object.keys(this.properties);
@@ -115,16 +115,16 @@
115115
return s;
116116
}
117117
}
118-
118+
119119
function UnboundRelationship(identity, type, properties) {
120120
this.identity = identity;
121121
this.type = type;
122122
this.properties = properties;
123-
123+
124124
this.bind = function bind(start, end) {
125125
return new Relationship(identity, start, end, type, properties);
126126
}
127-
127+
128128
this.toString = function toString() {
129129
var s = "-[:" + this.type;
130130
var keys = Object.keys(this.properties);
@@ -140,33 +140,38 @@
140140
return s;
141141
}
142142
}
143-
143+
144144
function Path(nodes, rels, sequence) {
145+
for(var i = 0; i < nodes.length; i++) {
146+
nodes[i] = hydrate(nodes[i]);
147+
}
145148
var last_node = nodes[0],
146149
entities = [last_node];
147150
for (var i = 0; i < sequence.length; i += 2) {
148151
var rel_index = sequence[i],
149-
next_node = nodes[sequence[2 * i + 1]],
152+
next_node = nodes[sequence[i + 1]],
150153
rel;
151154
if (rel_index > 0) {
152155
rel = hydrate(rels[rel_index - 1]);
156+
entities.push(rel.bind(last_node.identity, next_node.identity));
153157
} else {
154158
rel = hydrate(rels[-rel_index - 1]);
159+
entities.push(rel.bind(next_node.identity, last_node.identity));
155160
}
156-
entities.push(rel.bind(next_node, last_node))
157-
entities.push(next_node)
161+
entities.push(next_node);
162+
last_node = next_node;
158163
}
159-
164+
160165
this.nodes = [entities[0]];
161166
this.relationships = [];
162167
for (var i = 1; i < entities.length; i++) {
163-
if (i % 2 == 1) {
168+
if (i % 2 == 0) {
164169
this.nodes.push(entities[i]);
165170
} else {
166171
this.relationships.push(entities[i]);
167172
}
168173
}
169-
174+
170175
this.toString = function toString() {
171176
return "<Path>";
172177
}
@@ -180,18 +185,18 @@
180185
} else if (x instanceof Structure) {
181186
fields = x.fields;
182187
switch(x.signature) {
183-
case NODE:
184-
x = new Node(fields[0], fields[1], fields[2]);
185-
break;
186-
case RELATIONSHIP:
187-
x = new Relationship(fields[0], fields[1], fields[2], fields[3], fields[4]);
188-
break;
189-
case UNBOUND_RELATIONSHIP:
190-
x = new UnboundRelationship(fields[0], fields[1], fields[2]);
191-
break;
192-
case PATH:
193-
x = new Path(fields[0], fields[1], fields[2]);
194-
break;
188+
case NODE:
189+
x = new Node(fields[0], fields[1], fields[2]);
190+
break;
191+
case RELATIONSHIP:
192+
x = new Relationship(fields[0], fields[1], fields[2], fields[3], fields[4]);
193+
break;
194+
case UNBOUND_RELATIONSHIP:
195+
x = new UnboundRelationship(fields[0], fields[1], fields[2]);
196+
break;
197+
case PATH:
198+
x = new Path(fields[0], fields[1], fields[2]);
199+
break;
195200
}
196201
}
197202
return x;
@@ -213,11 +218,11 @@
213218
this.summaryHandler = summaryHandler;
214219
this.detailHandler = detailHandler;
215220
}
216-
221+
217222
function Message(ws) {
218223
var data = [],
219224
size = 0;
220-
225+
221226
function flush() {
222227
var header = new Uint8Array([size/256>>0, size%256]);
223228
ws.send(header);
@@ -227,7 +232,7 @@
227232
data = [];
228233
size = 0;
229234
}
230-
235+
231236
this.write = function write(b) {
232237
// TODO: when b > MAX_CHUNK_SIZE
233238
var newSize = size + b.length;
@@ -243,7 +248,7 @@
243248
var zero = new Uint8Array([0, 0]);
244249
ws.send(zero);
245250
}
246-
251+
247252
}
248253

249254
function Packer(msg) {
@@ -282,26 +287,26 @@
282287
console.log(x);
283288
}
284289
}
285-
290+
286291
function packNull() {
287292
msg.write(new Uint8Array([NULL]));
288293
}
289-
294+
290295
function packTrue() {
291296
msg.write(new Uint8Array([TRUE]));
292297
}
293-
298+
294299
function packFalse() {
295300
msg.write(new Uint8Array([FALSE]));
296301
}
297-
302+
298303
function packNumber(x) {
299304
if (x == x>>0)
300305
packInteger(x);
301306
else
302307
packFloat(x);
303308
}
304-
309+
305310
function packInteger(x) {
306311
if (-0x10 <= x && x < 0x80) {
307312
var a = new Uint8Array(1);
@@ -331,14 +336,14 @@
331336
packFloat(x);
332337
}
333338
}
334-
339+
335340
function packFloat(x) {
336341
var a = new Uint8Array(9);
337342
a[0] = FLOAT_64;
338343
new DataView(a.buffer).setFloat64(1, x);
339344
msg.write(a);
340345
}
341-
346+
342347
function packText(x) {
343348
var bytes = encoder.encode(x);
344349
var size = bytes.length;
@@ -358,7 +363,7 @@
358363
throw new ProtocolError("UTF-8 strings of size " + size + " are not supported");
359364
}
360365
}
361-
366+
362367
function packListHeader(size) {
363368
if (size < 0x10) {
364369
msg.write(new Uint8Array([TINY_LIST | size]));
@@ -372,7 +377,7 @@
372377
throw new ProtocolError("Lists of size " + size + " are not supported");
373378
}
374379
}
375-
380+
376381
function packMapHeader(size) {
377382
if (size < 0x10) {
378383
msg.write(new Uint8Array([TINY_MAP | size]));
@@ -386,7 +391,7 @@
386391
throw new ProtocolError("Maps of size " + size + " are not supported");
387392
}
388393
}
389-
394+
390395
function packStructHeader(size, signature) {
391396
if (size < 0x10) {
392397
msg.write(new Uint8Array([TINY_STRUCT | size, signature]));
@@ -398,7 +403,7 @@
398403
throw new ProtocolError("Structures of size " + size + " are not supported");
399404
}
400405
}
401-
406+
402407
var bytes = function bytes() {
403408
return Uint8Array(data);
404409
}
@@ -409,31 +414,31 @@
409414
var p = 0,
410415
view = new DataView(data.buffer),
411416
decoder = new TextDecoder();
412-
417+
413418
function read() {
414419
var ch = data[p];
415420
p += 1;
416421
return ch;
417422
}
418-
423+
419424
function readUint16() {
420425
var q = p;
421426
readBytes(2);
422427
return view.getUint16(q);
423428
}
424-
429+
425430
function readUint32() {
426431
var q = p;
427432
readBytes(4);
428433
return view.getUint32(q);
429434
}
430-
435+
431436
function readUint64() {
432437
var q = p;
433438
readBytes(8);
434439
return view.getUint64(q);
435440
}
436-
441+
437442
function readBytes(n) {
438443
var q = p + n,
439444
s = data.subarray(p, q);
@@ -558,7 +563,7 @@
558563
packer.pack(new Structure(signature, fields));
559564
msg.end();
560565
}
561-
566+
562567
function recv(data) {
563568
var b = new Uint8Array(data), h = [];
564569
for(var i = 0; i < b.length; i++) {
@@ -572,32 +577,32 @@
572577
var unpacker = new Unpacker(data),
573578
message = unpacker.unpack();
574579
switch(message.signature) {
575-
case SUCCESS:
576-
debug("S: SUCCESS " + JSON.stringify(message.fields[0]));
577-
var handler = responseHandlers.shift().summaryHandler;
578-
if(handler) handler(true, message.fields[0]);
579-
break;
580-
case FAILURE:
581-
debug("S: FAILURE " + JSON.stringify(message.fields[0]));
582-
console.log(message.fields[0]);
583-
var handler = responseHandlers.shift().summaryHandler;
584-
if(handler) handler(false, message.fields[0]);
585-
debug("C: ACK_FAILURE");
586-
responseHandlers.unshift(new ResponseHandler());
587-
packer.pack(new Structure(ACK_FAILURE, []));
588-
msg.end();
589-
break;
590-
case IGNORED:
591-
debug("S: IGNORED");
592-
responseHandlers.shift();
593-
break;
594-
case RECORD:
595-
debug("S: RECORD " + JSON.stringify(message.fields[0]));
596-
var handler = responseHandlers[0].detailHandler;
597-
if(handler) handler(hydrate(message.fields[0]));
598-
break;
599-
default:
600-
debug("WTF");
580+
case SUCCESS:
581+
debug("S: SUCCESS " + JSON.stringify(message.fields[0]));
582+
var handler = responseHandlers.shift().summaryHandler;
583+
if(handler) handler(true, message.fields[0]);
584+
break;
585+
case FAILURE:
586+
debug("S: FAILURE " + JSON.stringify(message.fields[0]));
587+
console.log(message.fields[0]);
588+
var handler = responseHandlers.shift().summaryHandler;
589+
if(handler) handler(false, message.fields[0]);
590+
debug("C: ACK_FAILURE");
591+
responseHandlers.unshift(new ResponseHandler());
592+
packer.pack(new Structure(ACK_FAILURE, []));
593+
msg.end();
594+
break;
595+
case IGNORED:
596+
debug("S: IGNORED");
597+
responseHandlers.shift();
598+
break;
599+
case RECORD:
600+
debug("S: RECORD " + JSON.stringify(message.fields[0]));
601+
var handler = responseHandlers[0].detailHandler;
602+
if(handler) handler(hydrate(message.fields[0]));
603+
break;
604+
default:
605+
debug("WTF");
601606
}
602607
}
603608

@@ -613,7 +618,7 @@
613618
messageData = newData;
614619
}
615620
}
616-
621+
617622
function receiverV1(data) {
618623
var p = 0;
619624
while (p < data.byteLength) {
@@ -622,7 +627,7 @@
622627
p = q + chunkSize;
623628
onChunk(new Uint8Array(data.slice(q, p)));
624629
}
625-
630+
626631
}
627632

628633
function init() {
@@ -639,7 +644,7 @@
639644
requests.push(new RunRequest(statement, parameters, onHeader, onRecord, onFooter));
640645
runNext();
641646
}
642-
647+
643648
function runNext() {
644649
if (ready) {
645650
while (requests.length > 0) {
@@ -691,4 +696,3 @@
691696
window.Path = Path;
692697

693698
}());
694-

0 commit comments

Comments
 (0)