Skip to content

Commit

Permalink
Implement db_del()
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphtheninja committed Dec 15, 2018
1 parent 2acd4b6 commit 8de6618
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion leveldown.js
Expand Up @@ -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 () {
Expand Down
56 changes: 55 additions & 1 deletion napi/leveldown.cc
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8de6618

Please sign in to comment.