Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit c6957d0

Browse files
committed
Replace source with N-API binding and JS from leveldown
1 parent fab2d10 commit c6957d0

40 files changed

+2196
-2749
lines changed

binding.cc

Lines changed: 1852 additions & 0 deletions
Large diffs are not rendered by default.

binding.gyp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,10 @@
5555
"<(module_root_dir)/deps/leveldb/leveldb.gyp:leveldb"
5656
]
5757
, "include_dirs" : [
58-
"<!(node -e \"require('nan')\")"
58+
"<!(node -e \"require('napi-macros')\")"
5959
]
6060
, "sources": [
61-
"src/batch.cc"
62-
, "src/batch_async.cc"
63-
, "src/database.cc"
64-
, "src/database_async.cc"
65-
, "src/iterator.cc"
66-
, "src/iterator_async.cc"
67-
, "src/leveldown.cc"
68-
, "src/leveldown_async.cc"
61+
"binding.cc"
6962
]
7063
}]
7164
}

binding.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('node-gyp-build')(__dirname)

chained-batch.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
const util = require('util')
22
const AbstractChainedBatch = require('abstract-leveldown').AbstractChainedBatch
3+
const binding = require('./binding')
34

45
function ChainedBatch (db) {
56
AbstractChainedBatch.call(this, db)
6-
this.binding = db.binding.batch()
7+
this.context = binding.batch_init(db.context)
78
}
89

910
ChainedBatch.prototype._put = function (key, value) {
10-
this.binding.put(key, value)
11+
binding.batch_put(this.context, key, value)
1112
}
1213

1314
ChainedBatch.prototype._del = function (key) {
14-
this.binding.del(key)
15+
binding.batch_del(this.context, key)
1516
}
1617

1718
ChainedBatch.prototype._clear = function () {
18-
this.binding.clear()
19+
binding.batch_clear(this.context)
1920
}
2021

2122
ChainedBatch.prototype._write = function (options, callback) {
22-
this.binding.write(callback)
23+
binding.batch_write(this.context, options, callback)
2324
}
2425

2526
util.inherits(ChainedBatch, AbstractChainedBatch)

iterator.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const util = require('util')
22
const AbstractIterator = require('abstract-leveldown').AbstractIterator
33
const fastFuture = require('fast-future')
4+
const binding = require('./binding')
45

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

8-
this.binding = db.binding.iterator(options)
9+
this.context = binding.iterator_init(db.context, options)
910
this.cache = null
1011
this.finished = false
1112
this.fastFuture = fastFuture()
@@ -19,7 +20,7 @@ Iterator.prototype._seek = function (target) {
1920
}
2021

2122
this.cache = null
22-
this.binding.seek(target)
23+
binding.iterator_seek(this.context, target)
2324
this.finished = false
2425
}
2526

@@ -40,7 +41,7 @@ Iterator.prototype._next = function (callback) {
4041
callback()
4142
})
4243
} else {
43-
this.binding.next(function (err, array, finished) {
44+
binding.iterator_next(this.context, function (err, array, finished) {
4445
if (err) return callback(err)
4546

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

5556
Iterator.prototype._end = function (callback) {
5657
delete this.cache
57-
this.binding.end(callback)
58+
binding.iterator_end(this.context, callback)
5859
}
5960

6061
module.exports = Iterator

leveldown.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const util = require('util')
22
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
3-
const binding = require('bindings')('leveldown').leveldown
3+
const binding = require('./binding')
44
const ChainedBatch = require('./chained-batch')
55
const Iterator = require('./iterator')
6-
const fs = require('fs')
76

87
function LevelDOWN (location) {
98
if (!(this instanceof LevelDOWN)) {
@@ -17,17 +16,17 @@ function LevelDOWN (location) {
1716
AbstractLevelDOWN.call(this)
1817

1918
this.location = location
20-
this.binding = binding(location)
19+
this.context = binding.db_init()
2120
}
2221

2322
util.inherits(LevelDOWN, AbstractLevelDOWN)
2423

2524
LevelDOWN.prototype._open = function (options, callback) {
26-
this.binding.open(options, callback)
25+
binding.db_open(this.context, this.location, options, callback)
2726
}
2827

2928
LevelDOWN.prototype._close = function (callback) {
30-
this.binding.close(callback)
29+
binding.db_close(this.context, callback)
3130
}
3231

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

4140
LevelDOWN.prototype._put = function (key, value, options, callback) {
42-
this.binding.put(key, value, options, callback)
41+
binding.db_put(this.context, key, value, options, callback)
4342
}
4443

4544
LevelDOWN.prototype._get = function (key, options, callback) {
46-
this.binding.get(key, options, callback)
45+
binding.db_get(this.context, key, options, callback)
4746
}
4847

4948
LevelDOWN.prototype._del = function (key, options, callback) {
50-
this.binding.del(key, options, callback)
49+
binding.db_del(this.context, key, options, callback)
5150
}
5251

5352
LevelDOWN.prototype._chainedBatch = function () {
5453
return new ChainedBatch(this)
5554
}
5655

5756
LevelDOWN.prototype._batch = function (operations, options, callback) {
58-
return this.binding.batch(operations, options, callback)
57+
binding.batch_do(this.context, operations, options, callback)
5958
}
6059

6160
LevelDOWN.prototype.approximateSize = function (start, end, callback) {
@@ -73,7 +72,7 @@ LevelDOWN.prototype.approximateSize = function (start, end, callback) {
7372
start = this._serializeKey(start)
7473
end = this._serializeKey(end)
7574

76-
this.binding.approximateSize(start, end, callback)
75+
binding.db_approximate_size(this.context, start, end, callback)
7776
}
7877

7978
LevelDOWN.prototype.compactRange = function (start, end, callback) {
@@ -91,13 +90,15 @@ LevelDOWN.prototype.compactRange = function (start, end, callback) {
9190
start = this._serializeKey(start)
9291
end = this._serializeKey(end)
9392

94-
this.binding.compactRange(start, end, callback)
93+
binding.db_compact_range(this.context, start, end, callback)
9594
}
9695

9796
LevelDOWN.prototype.getProperty = function (property) {
98-
if (typeof property !== 'string') { throw new Error('getProperty() requires a valid `property` argument') }
97+
if (typeof property !== 'string') {
98+
throw new Error('getProperty() requires a valid `property` argument')
99+
}
99100

100-
return this.binding.getProperty(property)
101+
return binding.db_get_property(this.context, property)
101102
}
102103

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

123-
binding.destroy(location, function (err) {
124-
if (err) return callback(err)
125-
126-
// On Windows, RocksDB silently fails to remove the directory because its
127-
// Logger, which is instantiated on destroy(), has an open file handle on a
128-
// LOG file. Destroy() removes this file but Windows won't actually delete
129-
// it until the handle is released. This happens when destroy() goes out of
130-
// scope, which disposes the Logger. So back in JS-land, we can again
131-
// attempt to remove the directory. This is merely a workaround because
132-
// arguably RocksDB should not instantiate a Logger or open a file at all.
133-
fs.rmdir(location, function (err) {
134-
if (err) {
135-
// Ignore this error in case there are non-RocksDB files left.
136-
if (err.code === 'ENOTEMPTY') return callback()
137-
if (err.code === 'ENOENT') return callback()
138-
139-
return callback(err)
140-
}
141-
142-
callback()
143-
})
144-
})
124+
binding.destroy_db(location, callback)
145125
}
146126

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

158-
binding.repair(location, callback)
138+
binding.repair_db(location, callback)
159139
}
160140

161141
module.exports = LevelDOWN.default = LevelDOWN

package.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
"license": "MIT",
66
"main": "leveldown.js",
77
"scripts": {
8-
"install": "prebuild-install || node-gyp rebuild",
9-
"test": "standard && hallmark && verify-travis-appveyor && nyc tape test/*-test.js && prebuild-ci",
8+
"install": "node-gyp-build",
9+
"test": "standard && hallmark && verify-travis-appveyor && nyc tape test/*-test.js",
1010
"coverage": "nyc report --reporter=text-lcov | coveralls",
11-
"rebuild": "prebuild --compile",
1211
"hallmark": "hallmark --fix",
1312
"dependency-check": "dependency-check . test/*.js",
1413
"prepublishOnly": "npm run dependency-check"
1514
},
1615
"dependencies": {
1716
"abstract-leveldown": "~6.0.3",
18-
"bindings": "~1.5.0",
1917
"fast-future": "~1.0.2",
20-
"nan": "~2.14.0",
21-
"prebuild-install": "^5.0.0"
18+
"napi-macros": "~1.8.2",
19+
"node-gyp-build": "~4.1.0"
2220
},
2321
"devDependencies": {
2422
"async": "^2.0.1",
@@ -27,19 +25,15 @@
2725
"dependency-check": "^3.3.0",
2826
"du": "~0.1.0",
2927
"hallmark": "^0.1.0",
30-
"level-concat-iterator": "^2.0.0",
3128
"level-community": "^3.0.0",
29+
"level-concat-iterator": "^2.0.0",
3230
"mkfiletree": "^1.0.1",
33-
"monotonic-timestamp": "~0.0.8",
3431
"nyc": "^14.0.0",
35-
"prebuild": "^8.0.0",
36-
"prebuild-ci": "^2.0.0",
3732
"readfiletree": "~0.0.1",
3833
"rimraf": "^2.6.1",
3934
"standard": "^12.0.0",
4035
"tape": "^4.10.0",
4136
"tempy": "^0.2.1",
42-
"uuid": "^3.2.1",
4337
"verify-travis-appveyor": "^3.0.0"
4438
},
4539
"standard": {

src/async.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)