Skip to content

Commit

Permalink
Move from level-sublevel to subleveldown
Browse files Browse the repository at this point in the history
  • Loading branch information
mhart committed Feb 11, 2019
1 parent 8bab506 commit ffd80ea
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 209 deletions.
14 changes: 9 additions & 5 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ var crypto = require('crypto'),
lazy = require('lazy'),
levelup = require('levelup'),
memdown = require('memdown'),
sublevel = require('level-sublevel'),
sub = require('subleveldown'),
lock = require('lock'),
BigNumber = require('bignumber.js')
BigNumber = require('bignumber.js'),
once = require('once')

exports.create = create
exports.lazy = lazyStream
Expand All @@ -29,8 +30,7 @@ function create(options) {
if (options.shardLimit == null) options.shardLimit = 10

var db = levelup(options.path ? require('leveldown')(options.path) : memdown()),
sublevelDb = sublevel(db),
metaDb = sublevelDb.sublevel('meta', {valueEncoding: 'json'}),
metaDb = sub(db, 'meta', {valueEncoding: 'json'}),
streamDbs = []

metaDb.lock = lock.Lock()
Expand All @@ -41,13 +41,14 @@ function create(options) {

function getStreamDb(name) {
if (!streamDbs[name]) {
streamDbs[name] = sublevelDb.sublevel('stream-' + name, {valueEncoding: 'json'})
streamDbs[name] = sub(db, 'stream-' + name, {valueEncoding: 'json'})
streamDbs[name].lock = lock.Lock()
}
return streamDbs[name]
}

function deleteStreamDb(name, cb) {
cb = once(cb)
var streamDb = getStreamDb(name)
delete streamDbs[name]
lazyStream(streamDb.createKeyStream(), cb).join(function(keys) {
Expand All @@ -56,6 +57,7 @@ function create(options) {
}

function getStream(name, cb) {
cb = once(cb)
metaDb.get(name, function(err, stream) {
if (err) {
if (err.name == 'NotFoundError') {
Expand Down Expand Up @@ -96,6 +98,8 @@ function create(options) {
function lazyStream(stream, errHandler) {
if (errHandler) stream.on('error', errHandler)
var streamAsLazy = lazy(stream)
stream.removeAllListeners('readable')
stream.on('data', streamAsLazy.emit.bind(streamAsLazy, 'data'))
if (stream.destroy) streamAsLazy.on('pipe', stream.destroy.bind(stream))
return streamAsLazy
}
Expand Down
Loading

0 comments on commit ffd80ea

Please sign in to comment.