Skip to content

Commit

Permalink
Implement destroy_db()
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphtheninja committed Dec 15, 2018
1 parent 0790470 commit ad079a5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
63 changes: 46 additions & 17 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1004,15 +1004,15 @@ NAPI_METHOD(db_get) {
}

/**
* Worker class for getting a value from a database.
* Worker class for deleting a value from a database.
*/
struct DelWorker : public BaseWorker {
DelWorker (napi_env env,
Database* database,
napi_value callback,
napi_value key,
bool sync)
: BaseWorker(env, database, callback, "leveldown.db.get"),
: BaseWorker(env, database, callback, "leveldown.db.del"),
key_(ToSlice(env, key)) {
options_.sync = sync;
}
Expand All @@ -1031,7 +1031,7 @@ struct DelWorker : public BaseWorker {
};

/**
* Gets a value from a database.
* Delete a value from a database.
*/
NAPI_METHOD(db_del) {
NAPI_ARGV(4);
Expand All @@ -1052,7 +1052,7 @@ NAPI_METHOD(db_del) {
}

/**
* Worker class for getting a value from a database.
* Worker class for calculating the size of a range.
*/
struct ApproximateSizeWorker : public BaseWorker {
ApproximateSizeWorker (napi_env env,
Expand Down Expand Up @@ -1115,7 +1115,7 @@ NAPI_METHOD(db_approximate_size) {
}

/**
* Worker class for getting a value from a database.
* Worker class for compacting a range in a database.
*/
struct CompactRangeWorker : public BaseWorker {
CompactRangeWorker (napi_env env,
Expand All @@ -1140,7 +1140,7 @@ struct CompactRangeWorker : public BaseWorker {
};

/**
* Compacts a range.
* Compacts a range in a database.
*/
NAPI_METHOD(db_compact_range) {
NAPI_ARGV(4);
Expand All @@ -1160,6 +1160,44 @@ NAPI_METHOD(db_compact_range) {
NAPI_RETURN_UNDEFINED();
}

/**
* Worker class for compacting a range in a database.
*/
struct DestroyWorker : public BaseWorker {
DestroyWorker (napi_env env,
const std::string* location,
napi_value callback)
// TODO turns out we don't even need database in BaseWorker()
: BaseWorker(env, NULL, callback, "leveldown.destroy_db"),
location_(location) {}

virtual ~DestroyWorker () {}

virtual void DoExecute () {
leveldb::Options options;
SetStatus(leveldb::DestroyDB(location_, options));
}

std::string location_;
};

/**
* Destroys a database.
*/
NAPI_METHOD(destroy_db) {
NAPI_ARGV(2);
// TODO replace with new
NAPI_ARGV_UTF8_MALLOC(location, 0);
napi_value callback = argv[1];

DestroyWorker* worker = new DestroyWorker(env, location, callback);
worker->Queue();

free(location);

NAPI_RETURN_UNDEFINED();
}

/**
* Runs when an Iterator is garbage collected.
*/
Expand Down Expand Up @@ -1396,8 +1434,7 @@ struct EndWorker : public BaseWorker {
EndWorker (napi_env env,
Iterator* iterator,
napi_value callback)
: BaseWorker(env, iterator->database_, callback,
"leveldown.iterator.end"),
: BaseWorker(env, iterator->database_, callback, "leveldown.iterator.end"),
iterator_(iterator) {}

virtual ~EndWorker () {}
Expand Down Expand Up @@ -1818,9 +1855,6 @@ NAPI_METHOD(batch_write) {
* All exported functions.
*/
NAPI_INIT() {
/**
* Database related functions.
*/
NAPI_EXPORT_FUNCTION(db_init);
NAPI_EXPORT_FUNCTION(db_open);
NAPI_EXPORT_FUNCTION(db_close);
Expand All @@ -1829,18 +1863,13 @@ NAPI_INIT() {
NAPI_EXPORT_FUNCTION(db_del);
NAPI_EXPORT_FUNCTION(db_approximate_size);
NAPI_EXPORT_FUNCTION(db_compact_range);
NAPI_EXPORT_FUNCTION(destroy_db);

/**
* Iterator related functions.
*/
NAPI_EXPORT_FUNCTION(iterator_init);
NAPI_EXPORT_FUNCTION(iterator_seek);
NAPI_EXPORT_FUNCTION(iterator_end);
NAPI_EXPORT_FUNCTION(iterator_next);

/**
* Batch related functions.
*/
NAPI_EXPORT_FUNCTION(batch_do);
NAPI_EXPORT_FUNCTION(batch_init);
NAPI_EXPORT_FUNCTION(batch_put);
Expand Down
2 changes: 1 addition & 1 deletion leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down
6 changes: 3 additions & 3 deletions test/destroy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('test callback-less, 1-arg, destroy() throws', function (t) {
t.end()
})

test('test destroy non-existent directory', function (t) {
test.only('test destroy non-existent directory', function (t) {
t.plan(4)

var location = tempy.directory()
Expand All @@ -37,8 +37,8 @@ test('test destroy non-existent directory', function (t) {
rimraf(location, { glob: false }, function (err) {
t.ifError(err, 'no error from rimraf()')

leveldown.destroy(location, function () {
t.is(arguments.length, 0, 'no arguments returned on callback')
leveldown.destroy(location, function (err) {
t.error(err, 'no error')

// Assert that destroy() didn't inadvertently create the directory.
// Or if it did, that it was at least cleaned up afterwards.
Expand Down

0 comments on commit ad079a5

Please sign in to comment.