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

Napi rewrite #540

Merged
merged 42 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
11797d8
Implement leveldown function (test/leveldown-test.js pass)
ralphtheninja Dec 6, 2018
71e7823
Implement open()
ralphtheninja Dec 6, 2018
9f03290
Implement put() (not yet handling buffers)
ralphtheninja Dec 7, 2018
1fba29c
Refactor common logic from OpenWorker and PutWorker into BaseWorker
ralphtheninja Dec 7, 2018
e37cda0
Rename binding functions
ralphtheninja Dec 7, 2018
085ef08
Stub out iterator methods
ralphtheninja Dec 7, 2018
3ede7b3
Lets adopt same naming as for nan case, DbContext -> Database
ralphtheninja Dec 7, 2018
20f2aea
Move leveldb specific calls to Database
ralphtheninja Dec 7, 2018
6889722
Implement iterator()
ralphtheninja Dec 7, 2018
182a58b
Reuse LD_STRING_OR_BUFFER_TO_COPY() macro in ToSlice() function
ralphtheninja Dec 7, 2018
043508a
Implement iterator.next() and iterator.end()
ralphtheninja Dec 8, 2018
57b0be4
Implement db_close()
ralphtheninja Dec 8, 2018
5613fb5
Fix error messages (needs to be copied)
ralphtheninja Dec 8, 2018
2acd4b6
Implement db_get()
ralphtheninja Dec 8, 2018
8de6618
Implement db_del()
ralphtheninja Dec 8, 2018
6f09aaf
Reorganize, macros at top, helpers etc
ralphtheninja Dec 8, 2018
f7c6d93
napi/leveldown.cc -> binding.cc
ralphtheninja Dec 8, 2018
64ff8f6
Add TODO for db_close() (when open iterators)
ralphtheninja Dec 8, 2018
52ccc82
Implement batch_do() for immediate write operation
ralphtheninja Dec 8, 2018
79966af
Implement chained batch (+ rename some functions)
ralphtheninja Dec 8, 2018
60b7982
Implement the rest of iterator_seek()
ralphtheninja Dec 8, 2018
8aba0c3
Implement dc_approximate_size()
ralphtheninja Dec 8, 2018
023deae
Fix ending open iterators while closing database (+ reorder Iterator …
ralphtheninja Dec 8, 2018
fc46554
Fix "iterator has ended" error
ralphtheninja Dec 8, 2018
0790470
Implement db_compact_range()
ralphtheninja Dec 8, 2018
ad079a5
Implement destroy_db()
ralphtheninja Dec 8, 2018
a1f94da
Implement repair_db()
ralphtheninja Dec 8, 2018
ce049dd
Implement db_get_property()
ralphtheninja Dec 8, 2018
50062ad
Accidentally disabled all tests but one :)
ralphtheninja Dec 8, 2018
1dfe434
.destroy() now calls back with null
ralphtheninja Dec 8, 2018
02cceda
Reset back to normal testing
ralphtheninja Dec 8, 2018
cb15d6e
Clean up debug printf
ralphtheninja Dec 9, 2018
d6001e1
Fix some napi commands + clean up old comments and reduce some if-sta…
ralphtheninja Dec 9, 2018
8855749
Dispose data pointed to by instances of leveldb::Slice
ralphtheninja Dec 9, 2018
6aca8c8
Only need to set filterPolicy once when Database object is created
ralphtheninja Dec 9, 2018
cb34bf0
Use new instead of malloc to keep similar memory handling everywhere
ralphtheninja Dec 9, 2018
41c19a2
Refactor calling code into CallFunction()
ralphtheninja Dec 9, 2018
4bcffea
Refactor some of the hideous iterator property code
ralphtheninja Dec 9, 2018
647c3dd
Compress constructor arguments a little bit
ralphtheninja Dec 9, 2018
42da176
Remove nan/v8 based glue code
ralphtheninja Dec 9, 2018
d4dd7f0
Tweak back if-statements to be more clear
ralphtheninja Dec 9, 2018
e80be43
Pass in leveldb::Slice into workers and not napi values
ralphtheninja Dec 9, 2018
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
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')\")"
Copy link
Member Author

Choose a reason for hiding this comment

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

❤️ @mafintosh

],
"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)
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's how we do it, regardless of where we are. We use an _init method to get a 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)
vweevers marked this conversation as resolved.
Show resolved Hide resolved
}

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",
vweevers marked this conversation as resolved.
Show resolved Hide resolved
"install": "node-gyp-build",
Copy link
Member Author

Choose a reason for hiding this comment

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

node-gyp-build checks if the module can be require()'d, if it fails it will recompile.

"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