Skip to content

Commit

Permalink
Merge 5014912 into d939ab1
Browse files Browse the repository at this point in the history
  • Loading branch information
staticskies committed Oct 25, 2015
2 parents d939ab1 + 5014912 commit 71316e3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
16 changes: 15 additions & 1 deletion lib/levelgraph.js
Expand Up @@ -110,6 +110,18 @@ module.exports = function levelgraph(leveldb, options) {
return db;
};

// patch variables in a query
function patchQuery(query) {
query.forEach(function(e) {
utilities.defs.spo.forEach(function(key) {
if (typeof e[key] === 'object' &&
e[key].constructor !== Variable) {
e[key] = new Variable(e[key].name);
}
});
});
}

searchStream = function(db, options) {
options = extend({ joinAlgorithm: 'sort' }, options);

Expand All @@ -119,6 +131,7 @@ searchStream = function(db, options) {
var that = this
, result = new PassThrough({ objectMode: true });

patchQuery(query);

options = extend(joinDefaults, options);

Expand Down Expand Up @@ -192,7 +205,8 @@ doAction = function(action, leveldb) {

doActionStream = function(type, leveldb) {

if (!leveldb.createWriteStream) {
// also check leveldb.isClient for multilevel compatibility
if (!leveldb.createWriteStream || leveldb.isClient) {
leveldb = levelWriteStream(leveldb);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/queryplanner.js
Expand Up @@ -48,7 +48,7 @@ function queryplanner(db, options) {
var newq = queryMask(q)
, range = utilities.createQuery(newq);

if (db.db.approximateSize) {
if (db.db && db.db.approximateSize) {
db.db.approximateSize(range.start, range.end, function(err, size) {
if (err) {
size = Object.keys(variablesMask(q)).length;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -41,11 +41,12 @@
"coveralls": "^2.11.2",
"istanbul": "~0.3.2",
"jshint": "~2.5.6",
"level-browserify": "^0.19.1",
"level-browserify": "^1.0.1",
"level-subkey": "^7.7.1",
"level-sublevel": "^6.4.6",
"level-test": "^2.0.0",
"mocha": "^2.2.5",
"multilevel": "^7.2.0",
"osenv": "^0.1.0",
"pre-commit": "1.0.7",
"setimmediate": "^1.0.2",
Expand Down
54 changes: 54 additions & 0 deletions test/multilevel_spec.js
@@ -0,0 +1,54 @@
var levelgraph = require('../lib/levelgraph')
, multilevel = require('multilevel')
, level = require('level-test')()
, osenv = require('osenv');

describe('a multileveled triple store', function() {

var db, graph, leveldb, server, client;

beforeEach(function() {
db = level();
server = multilevel.server(db);
client = multilevel.client();
graph = levelgraph(client);

server.pipe(client.createRpcStream()).pipe(server);
});

it('should put a triple', function(done) {
var triple = { subject: 'a', predicate: 'b', object: 'c' };
graph.put(triple, done);
});

it('should get a triple', function(done) {
var triple = { subject: 'a', predicate: 'b', object: 'c' };
graph.put(triple, function() {
graph.get({ subject: 'a' }, function(err, list) {
expect(list).to.eql([triple]);
done();
});
});
});

it('should search a triple', function(done) {
var triple = { subject: 'a', predicate: 'b', object: 'c' };
graph.put(triple, function() {
graph.search([{ subject: { name: 'x' }, predicate: 'b', object: 'c' }], function(err, list) {
expect(list).to.eql([{ x: 'a' }]);
done();
});
});
});

it('should put a triple with a stream', function(done) {
var triple = { subject: 'a', predicate: 'b', object: 'c' };
var stream = graph.putStream();
stream.on('end', done);
stream.end(triple);
});

it('should not close anything', function(done) {
graph.close(done);
});
});

0 comments on commit 71316e3

Please sign in to comment.