From 8de66184ebc688c42400a44f039e7a45f463d2ef Mon Sep 17 00:00:00 2001 From: Lars-Magnus Skog Date: Sat, 8 Dec 2018 03:46:24 +0100 Subject: [PATCH] Implement db_del() --- leveldown.js | 2 +- napi/leveldown.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/leveldown.js b/leveldown.js index 077a00a6..0fa445f0 100644 --- a/leveldown.js +++ b/leveldown.js @@ -57,7 +57,7 @@ LevelDOWN.prototype._get = function (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 () { diff --git a/napi/leveldown.cc b/napi/leveldown.cc index 63a72396..3455bb8a 100644 --- a/napi/leveldown.cc +++ b/napi/leveldown.cc @@ -188,6 +188,11 @@ struct Database { return db_->Get(options, key, &value); } + leveldb::Status Del (const leveldb::WriteOptions& options, + leveldb::Slice key) { + return db_->Delete(options, key); + } + const leveldb::Snapshot* NewSnapshot () { return db_->GetSnapshot(); } @@ -560,7 +565,7 @@ NAPI_METHOD(db_put) { } /** - * Worker class for getting values from a database + * Worker class for getting a value from a database. */ struct GetWorker : public BaseWorker { GetWorker (napi_env env, @@ -634,6 +639,54 @@ NAPI_METHOD(db_get) { NAPI_RETURN_UNDEFINED(); } +/** + * Worker class for getting 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"), + key_(ToSlice(env, key)) { + options_.sync = sync; + } + + virtual ~DelWorker () { + // TODO clean up key_ if not empty? + // See DisposeStringOrBufferFromSlice() + } + + virtual void DoExecute () { + SetStatus(database_->Del(options_, key_)); + } + + leveldb::WriteOptions options_; + leveldb::Slice key_; +}; + +/** + * Gets a value from a database. + */ +NAPI_METHOD(db_del) { + NAPI_ARGV(4); + NAPI_DB_CONTEXT(); + + napi_value key = argv[1]; + bool sync = BooleanProperty(env, argv[2], "sync", false); + napi_value callback = argv[3]; + + DelWorker* worker = new DelWorker(env, + database, + callback, + key, + sync); + worker->Queue(); + + NAPI_RETURN_UNDEFINED(); +} + /** * Owns a leveldb iterator. */ @@ -1254,6 +1307,7 @@ NAPI_INIT() { NAPI_EXPORT_FUNCTION(db_close); NAPI_EXPORT_FUNCTION(db_put); NAPI_EXPORT_FUNCTION(db_get); + NAPI_EXPORT_FUNCTION(db_del); NAPI_EXPORT_FUNCTION(iterator); NAPI_EXPORT_FUNCTION(iterator_seek);