Skip to content

Commit

Permalink
start working on exposing compactRange from leveldb
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Hall committed Jan 30, 2017
1 parent 710d190 commit e31f433
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ LevelDOWN.prototype._approximateSize = function (start, end, callback) {
}


LevelDOWN.prototype.compactRange = function (start, end, callback) {
this.binding.compactRange(start, end, callback)
}


LevelDOWN.prototype.getProperty = function (property) {
if (typeof property != 'string')
throw new Error('getProperty() requires a valid `property` argument')
Expand Down
29 changes: 29 additions & 0 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ uint64_t Database::ApproximateSizeFromDatabase (const leveldb::Range* range) {
return size;
}

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

void Database::GetPropertyFromDatabase (
const leveldb::Slice& property
, std::string* value) {
Expand Down Expand Up @@ -143,6 +148,7 @@ void Database::Init () {
Nan::SetPrototypeMethod(tpl, "del", Database::Delete);
Nan::SetPrototypeMethod(tpl, "batch", Database::Batch);
Nan::SetPrototypeMethod(tpl, "approximateSize", Database::ApproximateSize);
Nan::SetPrototypeMethod(tpl, "compactRange", Database::CompactRange);
Nan::SetPrototypeMethod(tpl, "getProperty", Database::GetProperty);
Nan::SetPrototypeMethod(tpl, "iterator", Database::Iterator);
}
Expand Down Expand Up @@ -430,6 +436,29 @@ NAN_METHOD(Database::ApproximateSize) {
Nan::AsyncQueueWorker(worker);
}

NAN_METHOD(Database::CompactRange) {
v8::Local<v8::Object> startHandle = info[0].As<v8::Object>();
v8::Local<v8::Object> endHandle = info[1].As<v8::Object>();

LD_METHOD_SETUP_COMMON(compactRange, -1, 2)

LD_STRING_OR_BUFFER_TO_SLICE(start, startHandle, start)
LD_STRING_OR_BUFFER_TO_SLICE(end, endHandle, end)

CompactRangeWorker* worker = new CompactRangeWorker(
database
, new Nan::Callback(callback)
, start
, end
, startHandle
, endHandle
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}

NAN_METHOD(Database::GetProperty) {
v8::Local<v8::Value> propertyHandle = info[0].As<v8::Object>();
v8::Local<v8::Function> callback; // for LD_STRING_OR_BUFFER_TO_SLICE
Expand Down
2 changes: 2 additions & 0 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Database : public Nan::ObjectWrap {
, leveldb::WriteBatch* batch
);
uint64_t ApproximateSizeFromDatabase (const leveldb::Range* range);
void CompactRangeFromDatabase (const leveldb::Slice* start, const leveldb::Slice* end);
void GetPropertyFromDatabase (const leveldb::Slice& property, std::string* value);
leveldb::Iterator* NewIterator (leveldb::ReadOptions* options);
const leveldb::Snapshot* NewSnapshot ();
Expand Down Expand Up @@ -101,6 +102,7 @@ class Database : public Nan::ObjectWrap {
static NAN_METHOD(Write);
static NAN_METHOD(Iterator);
static NAN_METHOD(ApproximateSize);
static NAN_METHOD(CompactRange);
static NAN_METHOD(GetProperty);
};

Expand Down
40 changes: 40 additions & 0 deletions src/database_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,44 @@ void ApproximateSizeWorker::HandleOKCallback () {
callback->Call(2, argv);
}

/** COMPACT RANGE WORKER **/

CompactRangeWorker::CompactRangeWorker (
Database *database
, Nan::Callback *callback
, leveldb::Slice start
, leveldb::Slice end
, v8::Local<v8::Object> &startHandle
, v8::Local<v8::Object> &endHandle
) : AsyncWorker(database, callback)
{
Nan::HandleScope scope;

SaveToPersistent("start", startHandle);
SaveToPersistent("end", endHandle);
};

CompactRangeWorker::~CompactRangeWorker () {}

void CompactRangeWorker::Execute () {
database->CompactRangeFromDatabase(&start, &end);
}

void CompactRangeWorker::WorkComplete() {
Nan::HandleScope scope;

DisposeStringOrBufferFromSlice(GetFromPersistent("start"), start);
DisposeStringOrBufferFromSlice(GetFromPersistent("end"), end);
AsyncWorker::WorkComplete();
}

void CompactRangeWorker::HandleOKCallback () {
Nan::HandleScope scope;

v8::Local<v8::Value> argv[] = {
Nan::Null()
};
callback->Call(1, argv);
}

} // namespace leveldown
22 changes: 22 additions & 0 deletions src/database_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ class ApproximateSizeWorker : public AsyncWorker {
uint64_t size;
};

class CompactRangeWorker : public AsyncWorker {
public:
CompactRangeWorker (
Database *database
, Nan::Callback *callback
, leveldb::Slice start
, leveldb::Slice end
, v8::Local<v8::Object> &startHandle
, v8::Local<v8::Object> &endHandle
);

virtual ~CompactRangeWorker ();
virtual void Execute ();
virtual void HandleOKCallback ();
virtual void WorkComplete ();

private:
leveldb::Slice start;
leveldb::Slice end;
};


} // namespace leveldown

#endif

0 comments on commit e31f433

Please sign in to comment.