Skip to content

Commit

Permalink
Replace source with N-API binding and JS from leveldown
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed May 22, 2019
1 parent fab2d10 commit c6957d0
Show file tree
Hide file tree
Showing 40 changed files with 2,196 additions and 2,749 deletions.
1,852 changes: 1,852 additions & 0 deletions binding.cc

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,10 @@
"<(module_root_dir)/deps/leveldb/leveldb.gyp:leveldb"
]
, "include_dirs" : [
"<!(node -e \"require('nan')\")"
"<!(node -e \"require('napi-macros')\")"
]
, "sources": [
"src/batch.cc"
, "src/batch_async.cc"
, "src/database.cc"
, "src/database_async.cc"
, "src/iterator.cc"
, "src/iterator_async.cc"
, "src/leveldown.cc"
, "src/leveldown_async.cc"
"binding.cc"
]
}]
}
1 change: 1 addition & 0 deletions binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('node-gyp-build')(__dirname)
11 changes: 6 additions & 5 deletions chained-batch.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const util = require('util')
const AbstractChainedBatch = require('abstract-leveldown').AbstractChainedBatch
const binding = require('./binding')

function ChainedBatch (db) {
AbstractChainedBatch.call(this, db)
this.binding = db.binding.batch()
this.context = binding.batch_init(db.context)
}

ChainedBatch.prototype._put = function (key, value) {
this.binding.put(key, value)
binding.batch_put(this.context, key, value)
}

ChainedBatch.prototype._del = function (key) {
this.binding.del(key)
binding.batch_del(this.context, key)
}

ChainedBatch.prototype._clear = function () {
this.binding.clear()
binding.batch_clear(this.context)
}

ChainedBatch.prototype._write = function (options, callback) {
this.binding.write(callback)
binding.batch_write(this.context, options, callback)
}

util.inherits(ChainedBatch, AbstractChainedBatch)
Expand Down
9 changes: 5 additions & 4 deletions iterator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const util = require('util')
const AbstractIterator = require('abstract-leveldown').AbstractIterator
const fastFuture = require('fast-future')
const binding = require('./binding')

function Iterator (db, options) {
AbstractIterator.call(this, db)

this.binding = db.binding.iterator(options)
this.context = binding.iterator_init(db.context, options)
this.cache = null
this.finished = false
this.fastFuture = fastFuture()
Expand All @@ -19,7 +20,7 @@ Iterator.prototype._seek = function (target) {
}

this.cache = null
this.binding.seek(target)
binding.iterator_seek(this.context, target)
this.finished = false
}

Expand All @@ -40,7 +41,7 @@ Iterator.prototype._next = function (callback) {
callback()
})
} else {
this.binding.next(function (err, array, finished) {
binding.iterator_next(this.context, function (err, array, finished) {
if (err) return callback(err)

that.cache = array
Expand All @@ -54,7 +55,7 @@ Iterator.prototype._next = function (callback) {

Iterator.prototype._end = function (callback) {
delete this.cache
this.binding.end(callback)
binding.iterator_end(this.context, callback)
}

module.exports = Iterator
52 changes: 16 additions & 36 deletions leveldown.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const util = require('util')
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const binding = require('bindings')('leveldown').leveldown
const binding = require('./binding')
const ChainedBatch = require('./chained-batch')
const Iterator = require('./iterator')
const fs = require('fs')

function LevelDOWN (location) {
if (!(this instanceof LevelDOWN)) {
Expand All @@ -17,17 +16,17 @@ function LevelDOWN (location) {
AbstractLevelDOWN.call(this)

this.location = location
this.binding = binding(location)
this.context = binding.db_init()
}

util.inherits(LevelDOWN, AbstractLevelDOWN)

LevelDOWN.prototype._open = function (options, callback) {
this.binding.open(options, callback)
binding.db_open(this.context, this.location, options, callback)
}

LevelDOWN.prototype._close = function (callback) {
this.binding.close(callback)
binding.db_close(this.context, callback)
}

LevelDOWN.prototype._serializeKey = function (key) {
Expand All @@ -39,23 +38,23 @@ LevelDOWN.prototype._serializeValue = function (value) {
}

LevelDOWN.prototype._put = function (key, value, options, callback) {
this.binding.put(key, value, options, callback)
binding.db_put(this.context, key, value, options, callback)
}

LevelDOWN.prototype._get = function (key, options, callback) {
this.binding.get(key, options, callback)
binding.db_get(this.context, key, options, callback)
}

LevelDOWN.prototype._del = function (key, options, callback) {
this.binding.del(key, options, callback)
binding.db_del(this.context, key, options, callback)
}

LevelDOWN.prototype._chainedBatch = function () {
return new ChainedBatch(this)
}

LevelDOWN.prototype._batch = function (operations, options, callback) {
return this.binding.batch(operations, options, callback)
binding.batch_do(this.context, operations, options, callback)
}

LevelDOWN.prototype.approximateSize = function (start, end, callback) {
Expand All @@ -73,7 +72,7 @@ LevelDOWN.prototype.approximateSize = function (start, end, callback) {
start = this._serializeKey(start)
end = this._serializeKey(end)

this.binding.approximateSize(start, end, callback)
binding.db_approximate_size(this.context, start, end, callback)
}

LevelDOWN.prototype.compactRange = function (start, end, callback) {
Expand All @@ -91,13 +90,15 @@ LevelDOWN.prototype.compactRange = function (start, end, callback) {
start = this._serializeKey(start)
end = this._serializeKey(end)

this.binding.compactRange(start, end, callback)
binding.db_compact_range(this.context, start, end, callback)
}

LevelDOWN.prototype.getProperty = function (property) {
if (typeof property !== 'string') { throw new Error('getProperty() requires a valid `property` argument') }
if (typeof property !== 'string') {
throw new Error('getProperty() requires a valid `property` argument')
}

return this.binding.getProperty(property)
return binding.db_get_property(this.context, property)
}

LevelDOWN.prototype._iterator = function (options) {
Expand All @@ -120,28 +121,7 @@ LevelDOWN.destroy = function (location, callback) {
throw new Error('destroy() requires a callback function argument')
}

binding.destroy(location, function (err) {
if (err) return callback(err)

// On Windows, RocksDB silently fails to remove the directory because its
// Logger, which is instantiated on destroy(), has an open file handle on a
// LOG file. Destroy() removes this file but Windows won't actually delete
// it until the handle is released. This happens when destroy() goes out of
// scope, which disposes the Logger. So back in JS-land, we can again
// attempt to remove the directory. This is merely a workaround because
// arguably RocksDB should not instantiate a Logger or open a file at all.
fs.rmdir(location, function (err) {
if (err) {
// Ignore this error in case there are non-RocksDB files left.
if (err.code === 'ENOTEMPTY') return callback()
if (err.code === 'ENOENT') return callback()

return callback(err)
}

callback()
})
})
binding.destroy_db(location, callback)
}

LevelDOWN.repair = function (location, callback) {
Expand All @@ -155,7 +135,7 @@ LevelDOWN.repair = function (location, callback) {
throw new Error('repair() requires a callback function argument')
}

binding.repair(location, callback)
binding.repair_db(location, callback)
}

module.exports = LevelDOWN.default = LevelDOWN
16 changes: 5 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
"license": "MIT",
"main": "leveldown.js",
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
"test": "standard && hallmark && verify-travis-appveyor && nyc tape test/*-test.js && prebuild-ci",
"install": "node-gyp-build",
"test": "standard && hallmark && verify-travis-appveyor && nyc tape test/*-test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"rebuild": "prebuild --compile",
"hallmark": "hallmark --fix",
"dependency-check": "dependency-check . test/*.js",
"prepublishOnly": "npm run dependency-check"
},
"dependencies": {
"abstract-leveldown": "~6.0.3",
"bindings": "~1.5.0",
"fast-future": "~1.0.2",
"nan": "~2.14.0",
"prebuild-install": "^5.0.0"
"napi-macros": "~1.8.2",
"node-gyp-build": "~4.1.0"
},
"devDependencies": {
"async": "^2.0.1",
Expand All @@ -27,19 +25,15 @@
"dependency-check": "^3.3.0",
"du": "~0.1.0",
"hallmark": "^0.1.0",
"level-concat-iterator": "^2.0.0",
"level-community": "^3.0.0",
"level-concat-iterator": "^2.0.0",
"mkfiletree": "^1.0.1",
"monotonic-timestamp": "~0.0.8",
"nyc": "^14.0.0",
"prebuild": "^8.0.0",
"prebuild-ci": "^2.0.0",
"readfiletree": "~0.0.1",
"rimraf": "^2.6.1",
"standard": "^12.0.0",
"tape": "^4.10.0",
"tempy": "^0.2.1",
"uuid": "^3.2.1",
"verify-travis-appveyor": "^3.0.0"
},
"standard": {
Expand Down
33 changes: 0 additions & 33 deletions src/async.h

This file was deleted.

0 comments on commit c6957d0

Please sign in to comment.