Skip to content

Commit

Permalink
Merge pull request #61 from Level/greenkeeper/initial
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers authored Apr 5, 2019
2 parents f06ad45 + 3981790 commit 9c59c60
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.nyc_output/
coverage/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sub(db, 'one', { valueEncoding: 'json' })
sub(db, 'two', { keyEncoding: 'binary' })
```

There is one limitation, however: keys must _encode to_ either strings or Buffers. This is not likely to affect you, unless you use custom encodings or the `id` encoding (which bypasses encodings and thus makes it your responsibility to ensure keys are either strings or Buffers).
There is one limitation, however: keys must _encode to_ either strings or Buffers. This is not likely to affect you, unless you use custom encodings or the `id` encoding (which bypasses encodings and thus makes it your responsibility to ensure keys are either strings or Buffers). If in that case you do pass in a key that is not a string or Buffer, it will be irreversibly converted to a string.

Authored by [@mafintosh](https://github.com/mafintosh) and inspired by [`level-sublevel`][level-sublevel] by [@dominictarr](https://github.com/dominictarr), `subleveldown` has become an official part of [Level][level-org]. As `level-sublevel` is no longer under active development, we recommend switching to `subleveldown` to get the latest and greatest of the Level ecosystem. These two modules largely offer the same functionality, except for [hooks](https://github.com/dominictarr/level-sublevel#hooks) and [per-batch prefixes](https://github.com/dominictarr/level-sublevel#batches).

Expand Down
6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ var sub = require('./')
var levelup = require('levelup')
var memdown = require('memdown')

var db = levelup('test', {db: memdown})
var db = levelup('test', { db: memdown })

var test = sub(db, 'test', {valueEncoding: 'utf-8', separator: '@'})
var test2 = sub(test, 'tester', {valueEncoding: 'utf-8', separator: '@'})
var test = sub(db, 'test', { valueEncoding: 'utf-8', separator: '@' })
var test2 = sub(test, 'tester', { valueEncoding: 'utf-8', separator: '@' })

test.put('hi', 'der')
test.put('hello', ['world'], function () {
Expand Down
26 changes: 13 additions & 13 deletions leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function concat (prefix, key, force) {
return key
}

function SubIterator (ite, prefix) {
function SubIterator (db, ite, prefix) {
this.iterator = ite
this.prefix = prefix

abstract.AbstractIterator.call(this)
abstract.AbstractIterator.call(this, db)
}

inherits(SubIterator, abstract.AbstractIterator)
Expand All @@ -36,7 +36,7 @@ SubIterator.prototype._end = function (cb) {

function SubDown (db, prefix, opts) {
if (!(this instanceof SubDown)) return new SubDown(db, prefix, opts)
if (typeof opts === 'string') opts = {separator: opts}
if (typeof opts === 'string') opts = { separator: opts }
if (!opts) opts = {}

var separator = opts.separator
Expand All @@ -63,7 +63,7 @@ function SubDown (db, prefix, opts) {
}
}

abstract.AbstractLevelDOWN.call(this, 'no-location')
abstract.AbstractLevelDOWN.call(this)
}

inherits(SubDown, abstract.AbstractLevelDOWN)
Expand Down Expand Up @@ -94,6 +94,10 @@ SubDown.prototype._close = function (cb) {
this.leveldown.close(cb)
}

SubDown.prototype._serializeKey = function (key) {
return Buffer.isBuffer(key) ? key : String(key)
}

SubDown.prototype._put = function (key, value, opts, cb) {
this.leveldown.put(concat(this.prefix, key), value, opts, cb)
}
Expand All @@ -107,16 +111,12 @@ SubDown.prototype._del = function (key, opts, cb) {
}

SubDown.prototype._batch = function (operations, opts, cb) {
if (arguments.length === 0) return new abstract.AbstractChainedBatch(this)
if (!Array.isArray(operations)) return this.leveldown.batch.apply(null, arguments)

var subops = new Array(operations.length)
// No need to make a copy of the array, abstract-leveldown does that
for (var i = 0; i < operations.length; i++) {
var o = operations[i]
subops[i] = {type: o.type, key: concat(this.prefix, o.key), value: o.value}
operations[i].key = concat(this.prefix, operations[i].key)
}

this.leveldown.batch(subops, opts, cb)
this.leveldown.batch(operations, opts, cb)
}

function extend (xopts, opts) {
Expand All @@ -137,12 +137,12 @@ function extend (xopts, opts) {
}

function fixRange (opts) {
return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start: opts.end, end: opts.start}
return (!opts.reverse || (!opts.end && !opts.start)) ? opts : { start: opts.end, end: opts.start }
}

SubDown.prototype._iterator = function (opts) {
var xopts = extend(wrap(fixRange(opts), this._wrap), opts)
return new SubIterator(this.leveldown.iterator(xopts), this.prefix)
return new SubIterator(this, this.leveldown.iterator(xopts), this.prefix)
}

module.exports = SubDown
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
"test": "test"
},
"dependencies": {
"abstract-leveldown": "^5.0.0",
"encoding-down": "^5.0.3",
"abstract-leveldown": "^6.0.2",
"encoding-down": "^6.0.1",
"inherits": "^2.0.3",
"level-option-wrap": "^1.1.0",
"levelup": "^3.0.1"
"levelup": "^4.0.1"
},
"devDependencies": {
"coveralls": "^3.0.2",
"dependency-check": "^3.3.0",
"hallmark": "^0.1.0",
"level-community": "^3.0.0",
"memdown": "^3.0.0",
"nyc": "^12.0.2",
"standard": "^11.0.1",
"memdown": "^4.0.0",
"nyc": "^13.3.0",
"standard": "^12.0.1",
"tape": "^4.9.0"
},
"hallmark": {
Expand Down
51 changes: 0 additions & 51 deletions test/common.js

This file was deleted.

94 changes: 77 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
var test = require('tape')
var suite = require('abstract-leveldown/test')
var memdown = require('memdown')
var encoding = require('encoding-down')
var subdown = require('../leveldown')
var subdb = require('..')
var levelup = require('levelup')
var testCommon = require('./common')

require('abstract-leveldown/abstract/open-test').args(down, test, testCommon)
require('abstract-leveldown/abstract/open-test').open(down, test, testCommon)
require('abstract-leveldown/abstract/del-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/get-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/put-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/put-get-del-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/batch-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/chained-batch-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/close-test').close(down, test, testCommon)
require('abstract-leveldown/abstract/iterator-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/iterator-range-test').all(down, test, testCommon)

// Test abstract-leveldown compliance
suite({
test: test,
factory: function () {
return subdown(levelup(memdown()), 'test')
},

// Unsupported features
seek: false,
createIfMissing: false,
errorIfExists: false
})

// Test without a user-provided levelup layer
suite({
test: test,
factory: function () {
return subdown(memdown(), 'test')
},

// Unsupported features
seek: false,
createIfMissing: false,
errorIfExists: false
})

// Additional tests for this implementation
test('SubDown constructor', function (t) {
t.test('can be called without new', function (t) {
var sub = subdown()
Expand Down Expand Up @@ -72,6 +87,24 @@ test('SubDb main function', function (t) {
})
})

t.test('error from open() bubbles up', function (t) {
t.plan(1)

var mockdb = {
open: function (cb) {
process.nextTick(cb, new Error('error from underlying store'))
}
}

subdb(mockdb, 'test')

// Awkward: we don't pass a callback to levelup() so levelup goes
// into "promise mode" which we can't catch properly
process.once('unhandledRejection', (err) => {
t.is(err.message, 'error from underlying store')
})
})

t.test('levelup *down is set to subdown which has correct storage', function (t) {
var db = levelup(memdown())
var sub = subdb(db, 'test')
Expand Down Expand Up @@ -184,8 +217,35 @@ test('SubDb main function', function (t) {
var sub = subdb(db, { valueEncoding: 'json' })
t.equal(sub.db._db.db.prefix, '!!')
})
})

function down (loc) {
return subdown(levelup(memdown()), 'test')
}
t.test('errors from iterator bubble up', function (t) {
t.plan(2)

var mockdb = {
open: function (cb) {
process.nextTick(cb)
},
iterator: function () {
return {
next: function (cb) {
process.nextTick(cb, new Error('next() error from underlying store'))
},
end: function (cb) {
process.nextTick(cb, new Error('end() error from underlying store'))
}
}
}
}

var sub = subdb(mockdb, 'test')
var it = sub.iterator()

it.next(function (err) {
t.is(err.message, 'next() error from underlying store')

it.end(function (err) {
t.is(err.message, 'end() error from underlying store')
})
})
})
})

0 comments on commit 9c59c60

Please sign in to comment.