Skip to content

Commit

Permalink
Implement db_compact_range()
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphtheninja committed Dec 15, 2018
1 parent fc46554 commit 0790470
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
52 changes: 52 additions & 0 deletions binding.cc
Expand Up @@ -381,6 +381,11 @@ struct Database {
return size;
}

void CompactRange (const leveldb::Slice* start,
const leveldb::Slice* end) {
db_->CompactRange(start, end);
}

const leveldb::Snapshot* NewSnapshot () {
return db_->GetSnapshot();
}
Expand Down Expand Up @@ -1109,6 +1114,52 @@ NAPI_METHOD(db_approximate_size) {
NAPI_RETURN_UNDEFINED();
}

/**
* Worker class for getting a value from a database.
*/
struct CompactRangeWorker : public BaseWorker {
CompactRangeWorker (napi_env env,
Database* database,
napi_value callback,
leveldb::Slice start,
leveldb::Slice end)
: BaseWorker(env, database, callback, "leveldown.db.compact_range"),
start_(start), end_(end) {}

virtual ~CompactRangeWorker () {
// TODO clean up start_ and end_ slices
// See DisposeStringOrBufferFromSlice()
}

virtual void DoExecute () {
database_->CompactRange(&start_, &end_);
}

leveldb::Slice start_;
leveldb::Slice end_;
};

/**
* Compacts a range.
*/
NAPI_METHOD(db_compact_range) {
NAPI_ARGV(4);
NAPI_DB_CONTEXT();

leveldb::Slice start = ToSlice(env, argv[1]);
leveldb::Slice end = ToSlice(env, argv[2]);
napi_value callback = argv[3];

CompactRangeWorker* worker = new CompactRangeWorker(env,
database,
callback,
start,
end);
worker->Queue();

NAPI_RETURN_UNDEFINED();
}

/**
* Runs when an Iterator is garbage collected.
*/
Expand Down Expand Up @@ -1777,6 +1828,7 @@ NAPI_INIT() {
NAPI_EXPORT_FUNCTION(db_get);
NAPI_EXPORT_FUNCTION(db_del);
NAPI_EXPORT_FUNCTION(db_approximate_size);
NAPI_EXPORT_FUNCTION(db_compact_range);

/**
* Iterator related functions.
Expand Down
17 changes: 3 additions & 14 deletions leveldown.js
Expand Up @@ -22,12 +22,7 @@ function LevelDOWN (location) {
util.inherits(LevelDOWN, AbstractLevelDOWN)

LevelDOWN.prototype._open = function (options, callback) {
binding.db_open(
this.context,
this.location,
options,
callback
)
binding.db_open(this.context, this.location, options, callback)
}

LevelDOWN.prototype._close = function (callback) {
Expand All @@ -43,13 +38,7 @@ LevelDOWN.prototype._serializeValue = function (value) {
}

LevelDOWN.prototype._put = function (key, value, options, callback) {
binding.db_put(
this.context,
key,
value,
options,
callback
)
binding.db_put(this.context, key, value, options, callback)
}

LevelDOWN.prototype._get = function (key, options, callback) {
Expand Down Expand Up @@ -101,7 +90,7 @@ LevelDOWN.prototype.compactRange = function (start, end, callback) {
start = this._serializeKey(start)
end = this._serializeKey(end)

this.binding.compactRange(start, end, callback)
binding.db_compact_range(this.context, start, end, callback)
}

LevelDOWN.prototype.getProperty = function (property) {
Expand Down

0 comments on commit 0790470

Please sign in to comment.