Skip to content

Commit

Permalink
failing sublevel test
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 25, 2014
1 parent decff64 commit 3f17d79
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
54 changes: 52 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ var levelProxy = require('level-proxy');
var net = require('net');
var fs = require('fs');
var path = require('path');
var has = require('has');
var manifest = require('level-manifest')({
methods: {
_iteratorCreate: { type: 'async' },
_iteratorNext: { type: 'async' },
_iteratorEnd: { type: 'async' }
}
});

module.exports = function (dir, opts) {
var proxy = levelProxy();
Expand All @@ -27,8 +35,40 @@ function withProxy (proxy, dir, opts) {
db.removeListener('error', onerror);

var server = net.createServer(function (stream) {
stream.on('error', function (err) {});
var iterators = {};
if (!db.methods) db.methods = {};

db.methods._iteratorCreate = { type: 'async' };
db._iteratorCreate = function (ix, opts) {
iterators[ix] = (db.iterator && db.iterator(opts))
|| (db.db && db.db.iterator && db.db.iterator(opts))
;
};

db.methods._iteratorNext = { type: 'async' };
db._iteratorNext = function (ix, cb) {
if (!has(iterators, ix)) cb(new Error('no such iterator'))
else iterators[ix].next(function (err, key, value) {
cb(err, key, value);
})
};

db.methods._iteratorEnd = { type: 'async' };
db._iteratorEnd = function (ix, cb) {
if (!has(iterators, ix)) cb(new Error('no such iterator'))
else iterators[ix].end(cb)
};

stream.on('error', function (err) { cleanup() });
stream.once('end', cleanup);
stream.pipe(multilevel.server(db)).pipe(stream);

function cleanup () {
Object.keys(iterators).forEach(function (ix) {
iterators[ix].end();
});
iterators = null;
}
});
server.listen(sockfile);

Expand Down Expand Up @@ -58,7 +98,17 @@ function withProxy (proxy, dir, opts) {
}

function createStream () {
var xdb = multilevel.client();
var xdb = multilevel.client(manifest);

var iteratorIx = 0;
xdb.iterator = function (opts) {
var ix = iteratorIx ++;
xdb._iteratorCreate(ix, opts);

return { next: next, end: end };
function next (cb) { xdb._iteratorNext(ix, cb) }
function end (cb) { xdb._iteratorEnd(ix, cb) }
};

(function connect () {
var stream = net.connect(sockfile);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"description": "open a leveldb handle multiple times",
"main": "index.js",
"dependencies": {
"has": "0.0.1",
"level": "~0.18.0",
"level-proxy": "^1.0.0",
"multilevel": "~5.5.0"
"multilevel": "^6.0.0"
},
"devDependencies": {
"tape": "~2.3.2"
Expand Down
41 changes: 41 additions & 0 deletions test/sublevel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var test = require('tape');
var level = require('../');
var path = require('path');
var sub = require('level-sublevel');
var os = require('os');
var tmpdir = os.tmpdir ? os.tmpdir() : os.tmpDir();
var datadir = path.join(tmpdir, 'level-party-' + Math.random());

test('sublevel', function (t) {
t.plan(6);
var db = level(datadir);
var sdb = sub(db);
var a = sdb.sublevel('aaa', { valueEncoding: 'json' });
var b = sdb.sublevel('bbb', { valueEncoding: 'json' });

var value = Math.floor(Math.random() * 100000);

a.put('x', value, function (err) {
t.ifError(err);

a.get('x', function (err, x) {
t.ifError(err);
t.equal(x, value);
});
b.get('x', function (err, x) {
t.ok(err);
});

a.createReadStream().on('data', function (row) {
t.deepEqual(row.key, 'x');
t.deepEqual(row.value, value);
});
b.createReadStream().on('data', function (row) {
t.fail('b should not have rows');
});
});

t.on('end', function () {
db.close();
});
});

0 comments on commit 3f17d79

Please sign in to comment.