Skip to content

Commit

Permalink
Implement db_close()
Browse files Browse the repository at this point in the history
This concludes the factory-test.js in abstract-leveldown
  • Loading branch information
ralphtheninja committed Dec 15, 2018
1 parent 043508a commit 57b0be4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ LevelDOWN.prototype._open = function (options, callback) {
}

LevelDOWN.prototype._close = function (callback) {
this.binding.close(callback)
binding.db_close(this.context, callback)
}

LevelDOWN.prototype._serializeKey = function (key) {
Expand Down
56 changes: 50 additions & 6 deletions napi/leveldown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ struct Database {
return leveldb::DB::Open(options, location, &db_);
}

void CloseDatabase () {
delete db_;
db_ = NULL;
if (blockCache_) {
delete blockCache_;
blockCache_ = NULL;
}
if (filterPolicy_) {
delete filterPolicy_;
filterPolicy_ = NULL;
}
}

leveldb::Status Put (const leveldb::WriteOptions& options,
leveldb::Slice key,
leveldb::Slice value) {
Expand Down Expand Up @@ -302,7 +315,7 @@ static int Int32Property (napi_env env,
}

/**
* Worker class for opening the database
* Worker class for opening a database
*/
struct OpenWorker : public BaseWorker {
OpenWorker (napi_env env,
Expand All @@ -317,7 +330,7 @@ struct OpenWorker : public BaseWorker {
uint32_t maxOpenFiles,
uint32_t blockRestartInterval,
uint32_t maxFileSize)
: BaseWorker(env, database, callback, "leveldown::open"),
: BaseWorker(env, database, callback, "leveldown.db.open"),
location_(location) {
options_.block_cache = database->blockCache_;
options_.filter_policy = database->filterPolicy_;
Expand Down Expand Up @@ -346,7 +359,7 @@ struct OpenWorker : public BaseWorker {
};

/**
* Opens a database.
* Open a database.
*/
NAPI_METHOD(db_open) {
NAPI_ARGV(4);
Expand Down Expand Up @@ -390,6 +403,36 @@ NAPI_METHOD(db_open) {
NAPI_RETURN_UNDEFINED();
}

/**
* Worker class for closing a database
*/
struct CloseWorker : public BaseWorker {
CloseWorker (napi_env env,
Database* database,
napi_value callback)
: BaseWorker(env, database, callback, "leveldown.db.close") {}

virtual ~CloseWorker () {}

virtual void DoExecute () {
database_->CloseDatabase();
}
};

/**
* Close a database.
*/
NAPI_METHOD(db_close) {
NAPI_ARGV(2);
NAPI_DB_CONTEXT();

napi_value callback = argv[1];
CloseWorker* worker = new CloseWorker(env, database, callback);
worker->Queue();

NAPI_RETURN_UNDEFINED();
}

/**
* Returns true if 'value' is a string otherwise false.
*/
Expand Down Expand Up @@ -446,7 +489,7 @@ struct PutWorker : public BaseWorker {
napi_value key,
napi_value value,
bool sync)
: BaseWorker(env, database, callback, "leveldown::put"),
: BaseWorker(env, database, callback, "leveldown.db.put"),
key_(ToSlice(env, key)), value_(ToSlice(env, value)) {
options_.sync = sync;
}
Expand Down Expand Up @@ -945,7 +988,7 @@ struct EndWorker : public BaseWorker {
Iterator* iterator,
napi_value callback)
: BaseWorker(env, iterator->database_, callback,
"leveldown::iterator.end"),
"leveldown.iterator.end"),
iterator_(iterator) {}

virtual ~EndWorker () {}
Expand Down Expand Up @@ -1005,7 +1048,7 @@ struct NextWorker : public BaseWorker {
napi_value callback,
void (*localCallback)(Iterator*))
: BaseWorker(env, iterator->database_, callback,
"leveldown::iterator.next"),
"leveldown.iterator.next"),
iterator_(iterator),
localCallback_(localCallback) {}

Expand Down Expand Up @@ -1103,6 +1146,7 @@ NAPI_METHOD(iterator_next) {
NAPI_INIT() {
NAPI_EXPORT_FUNCTION(db);
NAPI_EXPORT_FUNCTION(db_open);
NAPI_EXPORT_FUNCTION(db_close);
NAPI_EXPORT_FUNCTION(db_put);

NAPI_EXPORT_FUNCTION(iterator);
Expand Down

0 comments on commit 57b0be4

Please sign in to comment.