Skip to content

Commit c6970e8

Browse files
author
DvirDukhan
committed
added path support
1 parent 4eda4c4 commit c6970e8

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"url": "git://github.com/redislabs/redisgraph.js.git"
1010
},
1111
"dependencies": {
12+
"deep-equal": "^1.1.0",
1213
"redis": "^2.8.0"
1314
},
1415
"devDependencies": {

src/path.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Path {
2+
constructor(nodes, edges){
3+
this.nodes = nodes;
4+
this.edges = edges;
5+
}
6+
7+
getNodes(){
8+
return this.nodes;
9+
}
10+
11+
getEdges(){
12+
return this.edges;
13+
}
14+
15+
getNode(index){
16+
return this.nodes[index];
17+
}
18+
19+
getEdge(index){
20+
return this.edges[index];
21+
}
22+
23+
firstNode(){
24+
return this.nodes[0];
25+
}
26+
27+
lastNode(){
28+
return this.nodes[this.nodes.length -1];
29+
}
30+
31+
nodeCount(){
32+
return this.nodes.length;
33+
}
34+
35+
edgeCount(){
36+
return this.edges.length;
37+
}
38+
39+
toString() {
40+
return JSON.stringify(this);
41+
}
42+
43+
}
44+
45+
module.exports = Path;

src/resultSet.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Statistics = require("./statistics"),
22
Record = require("./record");
33
Node = require("./node");
44
Edge = require("./edge");
5+
Path = require("./path");
56
ReplyError = require("redis").ReplyError
67

78
const ResultSetColumnTypes = {
@@ -20,7 +21,8 @@ const ResultSetValueTypes = {
2021
VALUE_DOUBLE: 5,
2122
VALUE_ARRAY: 6,
2223
VALUE_EDGE: 7,
23-
VALUE_NODE: 8
24+
VALUE_NODE: 8,
25+
VALUE_PATH: 9
2426
}
2527

2628
/**
@@ -190,6 +192,12 @@ class ResultSet {
190192
return rawArray;
191193
}
192194

195+
async parsePath(rawPath) {
196+
let nodes = await this.parseScalar(rawPath[0]);
197+
let edges = await this.parseScalar(rawPath[1]);
198+
return new Path(nodes, edges);
199+
}
200+
193201
async parseScalar(cell) {
194202
let scalar_type = cell[0];
195203
let value = cell[1];
@@ -224,6 +232,9 @@ class ResultSet {
224232
case ResultSetValueTypes.VALUE_EDGE:
225233
scalar = await this.parseEdge(value);
226234
break;
235+
case ResultSetValueTypes.VALUE_PATH:
236+
scalar = await this.parsePath(value);
237+
break;
227238
case ResultSetValueTypes.VALUE_UNKNOWN:
228239
console.log("Unknown scalar type\n");
229240
break;

test/pathBuilder.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Node = require("../src/node");
2+
Edge = require("../src/edge");
3+
Path = require("../src/path");
4+
5+
class PathBuilder {
6+
constructor(nodeCount){
7+
this.nodes = new Array();
8+
this.edges = new Array();
9+
this.currentAppendClass = Node;
10+
}
11+
12+
append(obj){
13+
if(! obj instanceof this.currentAppendClass) throw "Error in path build insertion order and types."
14+
if(obj instanceof Node) return this._appendNode(obj);
15+
else return this._appendEdge(obj);
16+
}
17+
18+
build(){
19+
return new Path(this.nodes, this.edges);
20+
}
21+
22+
_appendNode(node){
23+
this.nodes.push(node);
24+
this.currentAppendClass = Edge;
25+
return this;
26+
}
27+
28+
_appendEdge(edge){
29+
this.edges.push(edge);
30+
this.currentAppendClass = Node;
31+
return this;
32+
}
33+
}
34+
35+
module.exports = PathBuilder;

test/redisGraphAPITest.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const assert = require("assert"),
22
redis = require("redis"),
33
Label = require("../src/label"),
44
RedisGraph = require("../src/graph");
5+
PathBuilder = require("./pathBuilder");
6+
deepEqual = require('deep-equal');
57

68
describe('RedisGraphAPI Test', function () {
79
const api = new RedisGraph("social");
@@ -288,4 +290,44 @@ describe('RedisGraphAPI Test', function () {
288290
})
289291
})
290292

293+
it('testPath', (done)=>{
294+
api.query("CREATE (:L1)-[:R1]->(:L1)-[:R1]->(:L1)").then(response => {
295+
api.query("MATCH p = (:L1)-[:R1*]->(:L1) RETURN p").then(response =>{
296+
let node0 = new Node("L1", {});
297+
node0.setId(0);
298+
let node1 = new Node("L1", {});
299+
node1.setId(1);
300+
let node2 = new Node("L1", {});
301+
node2.setId(2);
302+
let edge01 = new Edge(0, "R1", 1, {});
303+
edge01.setId(0);
304+
let edge12 = new Edge(1, "R1", 2, {});
305+
edge12.setId(1);
306+
307+
let path01 = new PathBuilder().append(node0).append(edge01).append(node1).build();
308+
let path12 = new PathBuilder().append(node1).append(edge12).append(node2).build();
309+
let path02 = new PathBuilder().append(node0).append(edge01).append(node1).append(edge12).append(node2).build();
310+
311+
let paths = new Set([path01, path12, path02]);
312+
while(response.hasNext()){
313+
let p = response.next().get("p");
314+
let pInPaths = false;
315+
let path = null;
316+
for( pathsIterator = paths.values(); path = pathsIterator.next().value;){
317+
if(deepEqual(p ,path)){
318+
pInPaths = true;
319+
break;
320+
}
321+
}
322+
assert(pInPaths);
323+
paths.delete(path);
324+
}
325+
assert.equal(0, paths.size);
326+
done();
327+
})
328+
}).catch(error => {
329+
console.log(error);
330+
})
331+
})
332+
291333
});

0 commit comments

Comments
 (0)