Skip to content

Commit

Permalink
Fixed deferred search in Level-js/IndexedDB.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jan 17, 2014
1 parent 8683aa1 commit 950d1e1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/levelgraph.js
Expand Up @@ -56,6 +56,8 @@ module.exports = function levelgraph(leveldb, options, readyCallback) {

}

utilities.patchApproximateSize(leveldb);

// it may be an empty object if we are on browserify.
// we are not patching it up if a sublevel is passed in.
if (typeof levelWriteStream === 'function' && leveldb.parent) {
Expand Down
22 changes: 8 additions & 14 deletions lib/queryplanner.js
Expand Up @@ -22,21 +22,15 @@ function queryplanner(db, options) {

async.each(query, function(q, cb) {
var newq = queryMask(q)
, range = utilities.createQuery(newq)
, result = function(err, size) {
if (err) {
return cb(err);
}
q.size = size;
async.nextTick(cb);
};

try {
db.approximateSize(range.start, range.end, result);
} catch(err) {
result(null, Object.keys(variablesMask(q)).length);
}
, range = utilities.createQuery(newq);

db.approximateSize(range.start, range.end, function(err, size) {
if (err) {
size = Object.keys(variablesMask(q)).length;
}
q.size = size;
async.nextTick(cb);
});
}, function(err) {
if (err) {
callback(err);
Expand Down
33 changes: 33 additions & 0 deletions lib/utilities.js
Expand Up @@ -231,3 +231,36 @@ function matcher(pattern) {
}

module.exports.matcher = matcher;

function patchApproximateSize(leveldb) {
// we need to be sublevel-aware
var db = leveldb.db || leveldb._root.db;

// monkey patching _approximateSize
// see https://github.com/maxogden/level.js/pull/21
function doPatch(db) {
if (db.constructor.name === 'Level') {
// we are in Level-js
db._approximateSize = function(a, b, callback) {
var err = new Error('Not implemented');
if (callback) {
return callback(err);
}

throw err;
};
}
}

if (db.constructor.name === 'DeferredLevelDOWN') {
db.setDb = function() {
doPatch(arguments[0]);
var result = db.constructor.prototype.setDb.apply(db, arguments);
return result;
};
} else {
doPatch(db);
}
}

module.exports.patchApproximateSize = patchApproximateSize;
6 changes: 5 additions & 1 deletion test/queryplanner_spec.js
Expand Up @@ -503,7 +503,11 @@ describe('query planner', function() {

describe('without approximateSize', function() {
beforeEach(function() {
db = {};
db = {
approximateSize: function(a, b, cb) {
cb(new Error('not implemented'));
}
};
planner = queryplanner(db, { joinAlgorithm: 'sort' });
});

Expand Down
7 changes: 7 additions & 0 deletions test/triple_store_spec.js
Expand Up @@ -385,4 +385,11 @@ describe('deferred open support', function() {
done();
});
});

it('should support deferred search', function(done) {
db = levelgraph(level());
db.search([{ predicate: 'likes' }], function() {
done();
});
});
});

0 comments on commit 950d1e1

Please sign in to comment.