Skip to content

Commit

Permalink
Merge pull request #540 from ralphtheninja/napi
Browse files Browse the repository at this point in the history
Napi rewrite
  • Loading branch information
ralphtheninja authored Dec 15, 2018
2 parents 0cf69ae + e80be43 commit c2fefcf
Show file tree
Hide file tree
Showing 26 changed files with 1,817 additions and 2,506 deletions.
1,777 changes: 1,777 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 @@ -39,17 +39,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)
17 changes: 12 additions & 5 deletions chained-batch.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
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)
// TODO (ensure docs covers the following)
// Note that we're passing in options here, which we didn't do before. We
// must do this so we can use the `sync` property, which we didn't handle before
// since we never passed in an object at time of creation (bug) (the previous c++
// used to assume we did this from the js side from the ChainedBatch()
// constructor).
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
26 changes: 13 additions & 13 deletions leveldown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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')

Expand All @@ -16,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 @@ -38,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 @@ -72,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 @@ -90,15 +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')
}

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

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

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

LevelDOWN.repair = function (location, callback) {
Expand All @@ -135,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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"abstract-leveldown": "~6.0.0",
"bindings": "~1.3.0",
"fast-future": "~1.0.2",
"nan": "~2.11.0",
"napi-macros": "^1.8.1",
"node-gyp-build": "^3.5.1",
"prebuild-install": "^5.0.0"
},
"devDependencies": {
Expand All @@ -43,7 +44,7 @@
"verify-travis-appveyor": "^3.0.0"
},
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
"install": "node-gyp-build",
"test": "standard && verify-travis-appveyor && nyc tape test/*-test.js && prebuild-ci",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"rebuild": "prebuild --compile",
Expand Down
32 changes: 0 additions & 32 deletions src/async.h

This file was deleted.

Loading

0 comments on commit c2fefcf

Please sign in to comment.