11const util = require ( 'util' )
22const AbstractLevelDOWN = require ( 'abstract-leveldown' ) . AbstractLevelDOWN
3- const binding = require ( 'bindings' ) ( 'leveldown' ) . leveldown
3+ const binding = require ( './binding' )
44const ChainedBatch = require ( './chained-batch' )
55const Iterator = require ( './iterator' )
6- const fs = require ( 'fs' )
76
87function 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
2322util . inherits ( LevelDOWN , AbstractLevelDOWN )
2423
2524LevelDOWN . prototype . _open = function ( options , callback ) {
26- this . binding . open ( options , callback )
25+ binding . db_open ( this . context , this . location , options , callback )
2726}
2827
2928LevelDOWN . prototype . _close = function ( callback ) {
30- this . binding . close ( callback )
29+ binding . db_close ( this . context , callback )
3130}
3231
3332LevelDOWN . prototype . _serializeKey = function ( key ) {
@@ -39,23 +38,23 @@ LevelDOWN.prototype._serializeValue = function (value) {
3938}
4039
4140LevelDOWN . 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
4544LevelDOWN . prototype . _get = function ( key , options , callback ) {
46- this . binding . get ( key , options , callback )
45+ binding . db_get ( this . context , key , options , callback )
4746}
4847
4948LevelDOWN . prototype . _del = function ( key , options , callback ) {
50- this . binding . del ( key , options , callback )
49+ binding . db_del ( this . context , key , options , callback )
5150}
5251
5352LevelDOWN . prototype . _chainedBatch = function ( ) {
5453 return new ChainedBatch ( this )
5554}
5655
5756LevelDOWN . 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
6160LevelDOWN . 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
7978LevelDOWN . 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
9796LevelDOWN . 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
103104LevelDOWN . 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
147127LevelDOWN . 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
161141module . exports = LevelDOWN . default = LevelDOWN
0 commit comments