Skip to content

Commit

Permalink
switch do neo4j node driver called neo4j
Browse files Browse the repository at this point in the history
  • Loading branch information
weinberger committed Jun 9, 2015
1 parent 005bf9c commit eac3794
Showing 2 changed files with 72 additions and 95 deletions.
164 changes: 71 additions & 93 deletions neo4j/description.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
'use strict';

var opts = {maxSockets: 25, keepAlive: true, keepAliveMsecs: 1000};
var Agent = require('http').Agent;
var neo4j = require('neo4j');

module.exports = {
name: 'Neo4J',

startup: function (host, cb) {
var neo4j = require('seraph')({
server: 'http://' + host + ':7474',
user: 'neo4j',
pass: 'abc'
});
var db = new neo4j.GraphDatabase({
url: 'http://neo4j:abc@' + host + ':7474',
agent: new Agent(opts)});

cb(neo4j);
cb(db);
},

warmup: function (db, cb) {
@@ -24,7 +26,7 @@ module.exports = {

for (var i = 1; i <= 50; ++i) {
for (var j = 51; j <= 100; ++j) {
module.exports.shortestPath(db, 'profiles', 'relations',
module.exports.shortestPath(db, 'PROFILES', 'RELATIONS',
{from: String(i), to: String(j)}, i,
function (err) {
if (err) return cb(err);
@@ -53,115 +55,91 @@ module.exports = {
dropCollection: function (db, name, cb) {
name = name.toUpperCase();

db.nodesWithLabel(name, function (err, results) {
if (err) cb(err);

console.log('INFO clearing %d documents in %s', results.length, name);

var i = -1;
var next = function (err) {
// if (err) console.log('ERROR %s', err);
if (err) cb(err);

++i;

if (i === results.length) {
cb();
}
else {
var obj = {id: results[i].id};
db.delete(obj, function (err) {
next(err);
});
}
};

next();
});
db.cypher({query: 'MATCH (n:' + name + ') DELETE n'}, cb);
},

createCollection: function (db, name, cb) {
cb();
},

getDocument: function (db, coll, id, cb) {
var obj = {_key: 'P/' + id};
db.find(obj, false, coll, cb);
db.cypher({query: 'MATCH (f:' + coll + ' {_key:{key}}) RETURN f',
params: {key: 'P/' + id},
headers: {Connection: 'keep-alive'},
lean: true}, cb);
},

saveDocument: function (db, coll, doc, cb) {
db.save(doc, coll, cb);
db.cypher({query: 'CREATE (f:' + coll + ' {doc})',
params: {doc: doc},
headers: {Connection: 'keep-alive'},
lean: true}, cb);
},

aggregate: function (db, coll, cb) {
db.query('MATCH (f:' + coll + ') WITH f.AGE as AGE RETURN AGE, count(*)',
function (err, result) {
if (err) return cb(err);
db.cypher({query: 'MATCH (f:' + coll + ') WITH f.AGE as AGE RETURN AGE, count(*)',
headers: {Connection: 'keep-alive'},
lean: true},

cb(null, result.length);
}
);
},

neighbors: function (db, collP, collR, id, i, cb) {
db.query('MATCH (s:' + collP + ' {_key:{key}})-->(n:' + collP + ') RETURN n._key', {key: 'P/' + id},
function (err, result) {
if (err) return cb(err);
function (err, result) {
if (err) return cb(err);

if (result.length === undefined) cb(null, 1);
else cb(null, result.length);
}
);
cb(null, result.length);
});
},

neighbors2: function (db, collP, collR, id, i, cb) {
db.query('MATCH (s:' + collP + ' {_key:{key}})-[*1..2]->(n:' + collP + ') RETURN DISTINCT n._key', {key: 'P/' + id},
function (err, result) {
if (err) return cb(err);
neighbors: function (db, collP, collR, id, i, cb) {
db.cypher({query: 'MATCH (s:' + collP + ' {_key:{key}})-->(n:' + collP + ') RETURN n._key',
params: {key: 'P/' + id},
headers: {Connection: 'keep-alive'},
lean: true},

if (result.map === undefined) {
result = [result['n._key']];
}
else {
result = result.map(function (x) { return x['n._key']; });
}
function (err, result) {
if (err) return cb(err);

if (result.indexOf('P/' + id) === -1) {
cb(null, result.length);
}
else {
cb(null, result.length - 1);
}
}
);
if (result.length === undefined) cb(null, 1);
else cb(null, result.length);
});
},

neighbors3: function (db, collP, collR, id, i, cb) {
db.query('MATCH (s:' + collP + ' {_key:{key}})-[*1..2]->(n:' + collP + ') RETURN DISTINCT n._key', {key: 'P/' + id},
function (err, result) {
if (err) return cb(err);

result = result.map(function (x) { return x['n._key']; });

if (result.indexOf('P/' + id) === -1) {
cb(null, result.length);
}
else {
cb(null, result.length - 1);
}
}
);
neighbors2: function (db, collP, collR, id, i, cb) {
db.cypher({query: 'MATCH (s:' + collP + ' {_key:{key}})-[*1..2]->(n:'
+ collP + ') RETURN DISTINCT n._key',
params: {key: 'P/' + id},
headers: {Connection: 'keep-alive'},
lean: true},

function (err, result) {
if (err) return cb(err);

if (result.map === undefined) {
result = [result['n._key']];
}
else {
result = result.map(function (x) { return x['n._key']; });
}

if (result.indexOf('P/' + id) === -1) {
cb(null, result.length);
}
else {
cb(null, result.length - 1);
}
});
},

shortestPath: function (db, collP, collR, path, i, cb) {
db.query('MATCH (s:' + collP + ' {_key:{from}}),(t:' + collP + ' {_key:{to}}), p = shortestPath((s)-[*..15]->(t)) RETURN p',
{from: 'P/' + path.from, to: 'P/' + path.to},
function (err, result) {
if (err) return cb(err);

if (result.length === 0) { return cb(null, 0); }
else { return cb(null, result[0].length); }
}
);
db.cypher({query: 'MATCH (s:' + collP + ' {_key:{from}}),(t:'
+ collP + ' {_key:{to}}), p = shortestPath((s)-[*..15]->(t)) RETURN p',
params: {from: 'P/' + path.from, to: 'P/' + path.to},
headers: {Connection: 'keep-alive'},
lean: true},

function (err, result) {
if (err) return cb(err);

if (result.length === 0) {cb(null, 0);}
else {cb(null, (result[0].p.length - 1) / 2);}
});
}
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@
"underscore": "^1.8.3",
"arangojs": "^3.8.1",
"mongodb": "^2.0.33",
"neo4j": "^2.0.0-RC1",
"seraph": "^0.11.2"
"neo4j": "^2.0.0-RC1"
}
}

0 comments on commit eac3794

Please sign in to comment.