Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to abstract-leveldown 4 and levelup 2 #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
language: node_js
node_js:
- "0.10"
- 4
- 6
- 8
- 9
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ module.exports = function (db, prefix, opts) {
if (typeof prefix === 'object' && !opts) return module.exports(db, null, prefix)
if (!opts) opts = {}

opts.db = function () {
return subdown(db, prefix, opts)
}

return levelup(opts)
return levelup(subdown(db, prefix, opts), opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vweevers What if we did levelup(encoding(subdown(db, prefix, opts)), opts) here instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work, yeah, with one addition: also pass opts to encoding().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens with deferred-leveldown though? Wouldn't every (sub)db be wrapped needlessly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not following you. That's already the case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already the case?

It is? OK, then never mind, we can optimize that later if we want

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye, so each levelup has its own deferred-leveldown and subleveldown just returns a new levelup so nothing has changed there.

But I agree, it's a bit overhead maybe. Not sure what we can do about it since it's part of levelup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vweevers What if we did levelup(encoding(subdown(db, prefix, opts)), opts) here instead?

Commenting on my own comment 😄

This is exactly what level does, so we could probably do:

return level(subdown(db, prefix, opts), opts)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch my last comment. I meant using level-packager which wraps with levelup and encoding-down.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hurts my brain :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hurts my brain :)

You're not alone :)

}
43 changes: 13 additions & 30 deletions leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ var util = require('util')
var abstract = require('abstract-leveldown')
var wrap = require('level-option-wrap')

var END = new Buffer([0xff])
var END = Buffer.from([0xff])

var concat = function (prefix, key, force) {
if (typeof key === 'string' && (force || key.length)) return prefix + key
if (Buffer.isBuffer(key) && (force || key.length)) return Buffer.concat([new Buffer(prefix), key])
if (Buffer.isBuffer(key) && (force || key.length)) return Buffer.concat([Buffer.from(prefix), key])
return key
}

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

abstract.AbstractIterator.call(this)
}

SubIterator.prototype.next = function (cb) {
util.inherits(SubIterator, abstract.AbstractIterator)

SubIterator.prototype._next = function (cb) {
var self = this
this.iterator.next(cb && function (err, key, value) {
if (err) return cb(err)
Expand All @@ -24,7 +28,7 @@ SubIterator.prototype.next = function (cb) {
})
}

SubIterator.prototype.end = function (cb) {
SubIterator.prototype._end = function (cb) {
this.iterator.end(cb)
}

Expand Down Expand Up @@ -85,27 +89,22 @@ SubDown.prototype._open = function (opts, cb) {
}
}

SubDown.prototype.close = function () {
SubDown.prototype._close = function () {
this.leveldown.close.apply(this.leveldown, arguments)
}

SubDown.prototype.setDb = function () {
this.leveldown.setDb.apply(this.leveldown, arguments)
}

SubDown.prototype.put = function (key, value, opts, cb) {
SubDown.prototype._put = function (key, value, opts, cb) {
this.leveldown.put(concat(this.prefix, key), value, opts, cb)
}

SubDown.prototype.get = function (key, opts, cb) {
SubDown.prototype._get = function (key, opts, cb) {
this.leveldown.get(concat(this.prefix, key), opts, cb)
}

SubDown.prototype.del = function (key, opts, cb) {
SubDown.prototype._del = function (key, opts, cb) {
this.leveldown.del(concat(this.prefix, key), opts, cb)
}

SubDown.prototype.batch =
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)
Expand All @@ -119,22 +118,6 @@ SubDown.prototype._batch = function (operations, opts, cb) {
this.leveldown.batch(subops, opts, cb)
}

SubDown.prototype.approximateSize = function (start, end, cb) {
this.leveldown.approximateSize.apply(this.leveldown, arguments)
}

SubDown.prototype.getProperty = function () {
return this.leveldown.getProperty.apply(this.leveldown, arguments)
}

SubDown.prototype.destroy = function () {
return this.leveldown.destroy.apply(this.leveldown, arguments)
}

SubDown.prototype.repair = function () {
return this.leveldown.repair.apply(this.leveldown, arguments)
}

var extend = function (xopts, opts) {
xopts.keys = opts.keys
xopts.values = opts.values
Expand All @@ -155,7 +138,7 @@ var fixRange = function (opts) {
return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start: opts.end, end: opts.start}
}

SubDown.prototype.iterator = function (opts) {
SubDown.prototype._iterator = function (opts) {
if (!opts) opts = {}
var xopts = extend(wrap(fixRange(opts), this._wrap), opts)
return new SubIterator(this.leveldown.iterator(xopts), this.prefix)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "sublevels implemented using leveldowns",
"main": "index.js",
"dependencies": {
"abstract-leveldown": "^2.4.1",
"abstract-leveldown": "^4.0.2",
"level-option-wrap": "^1.1.0",
"levelup": "^1.2.1"
"levelup": "^2.0.1"
},
"devDependencies": {
"memdown": "^1.1.0",
"standard": "^5.3.1",
"memdown": "^2.0.0",
"standard": "^10.0.3",
"tape": "^4.2.2"
},
"repository": {
Expand Down
8 changes: 3 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ var memdown = require('memdown')
var subdown = require('../leveldown')
var levelup = require('levelup')
var testCommon = require('./common')
var testBuffer = new Buffer('this-is-test-data')

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, testBuffer)
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/ranges-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/iterator-range-test').all(down, test, testCommon)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@finnp Did you have any help of the upgrade guide in abstract-leveldown?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I had a look at it, that was quite helpful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's awesome to hear! It paid off :)


function down (loc) {
return subdown(levelup(loc, {db: memdown}), 'test')
return subdown(levelup(memdown()), 'test')
}