Skip to content

Commit

Permalink
Implement dc_approximate_size()
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphtheninja committed Dec 15, 2018
1 parent 60b7982 commit 8aba0c3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
70 changes: 70 additions & 0 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ struct Database {
return db_->Write(options, batch);
}

uint64_t ApproximateSize (const leveldb::Range* range) {
uint64_t size = 0;
db_->GetApproximateSizes(range, 1, &size);
return size;
}

const leveldb::Snapshot* NewSnapshot () {
return db_->GetSnapshot();
}
Expand Down Expand Up @@ -763,6 +769,69 @@ NAPI_METHOD(db_del) {
NAPI_RETURN_UNDEFINED();
}

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

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

virtual void DoExecute () {
leveldb::Range range(start_, end_);
size_ = database_->ApproximateSize(&range);
}

virtual void HandleOKCallback() {
const int argc = 2;
napi_value argv[argc];
napi_get_null(env_, &argv[0]);
napi_create_uint32(env_, (uint32_t)size_, &argv[1]);

// TODO move to base class
napi_value global;
napi_get_global(env_, &global);
napi_value callback;
napi_get_reference_value(env_, callbackRef_, &callback);

napi_call_function(env_, global, callback, argc, argv, NULL);
}

leveldb::Slice start_;
leveldb::Slice end_;
uint64_t size_;
};

/**
* Calculates the approximate size of a range in a database.
*/
NAPI_METHOD(db_approximate_size) {
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];

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

NAPI_RETURN_UNDEFINED();
}

/**
* Owns a leveldb iterator.
*/
Expand Down Expand Up @@ -1702,6 +1771,7 @@ NAPI_INIT() {
NAPI_EXPORT_FUNCTION(db_put);
NAPI_EXPORT_FUNCTION(db_get);
NAPI_EXPORT_FUNCTION(db_del);
NAPI_EXPORT_FUNCTION(db_approximate_size);

/**
* Iterator related functions.
Expand Down
2 changes: 1 addition & 1 deletion leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ LevelDOWN.prototype.approximateSize = function (start, end, callback) {
start = this._serializeKey(start)
end = this._serializeKey(end)

this.binding.approximateSize(start, end, callback)
binding.db_approximate_size(this.context, start, end, callback)
}

LevelDOWN.prototype.compactRange = function (start, end, callback) {
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require('./leveldown-test')
require('./abstract-leveldown-test')
// require('./approximate-size-test')
//require('./leveldown-test')
//require('./abstract-leveldown-test')
require('./approximate-size-test')
// require('./cleanup-hanging-iterators-test')
// require('./compact-range-test')
// require('./compression-test')
Expand Down

0 comments on commit 8aba0c3

Please sign in to comment.