[Pkg-javascript-commits] [node-leveldown] 134/492: Get approximate size for a range
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:52 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit 4e6f878bd56c70f95a3386586653ceba098175a0
Author: David Björklund <david.bjorklund at gmail.com>
Date: Sun Dec 30 03:27:29 2012 +0100
Get approximate size for a range
---
lib/levelup.js | 28 +++++++++++++++++++++++
src/database.cc | 34 ++++++++++++++++++++++++++++
src/database.h | 2 ++
src/database_async.cc | 21 ++++++++++++++++++
src/database_async.h | 26 ++++++++++++++++++++++
test/simple-test.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 172 insertions(+)
diff --git a/lib/levelup.js b/lib/levelup.js
index e854e39..91ddde2 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -275,6 +275,34 @@ LevelUP.prototype = {
}.bind(this))
}
+ , approximateSize: function(start, end, callback) {
+ var err
+
+ if (!this.isOpen() && !this.isClosed()) {
+ return this.once('ready', function () {
+ this.approximateSize(start, end, callback)
+ })
+ }
+
+ if (this.isClosed()) {
+ err = new errors.WriteError('Database is not open')
+ if (callback)
+ return callback(err)
+ throw err
+ }
+
+ this._db.approximateSize(start, end, function(err, size) {
+ if (err) {
+ err = new errors.OpenError(err)
+ if (callback)
+ return callback(err)
+ this.emit('error', err)
+ } else {
+ callback && callback(null, size)
+ }
+ });
+ }
+
, readStream: function (options) {
options = extend(extend({}, this._options), typeof options == 'object' ? options : {})
return readStream.create(
diff --git a/src/database.cc b/src/database.cc
index 4900519..2929a60 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -52,6 +52,12 @@ Status Database::WriteBatchToDatabase (WriteOptions* options, WriteBatch* batch)
return db->Write(*options, batch);
}
+uint64_t Database::ApproximateSizeFromDatabase (const Range* range) {
+ uint64_t size;
+ db->GetApproximateSizes(range, 1, &size);
+ return size;
+}
+
leveldb::Iterator* Database::NewIterator (ReadOptions* options) {
return db->NewIterator(*options);
}
@@ -88,6 +94,7 @@ void Database::Init () {
tpl->PrototypeTemplate()->Set(String::NewSymbol("get") , FunctionTemplate::New(Get)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("del") , FunctionTemplate::New(Delete)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("batch") , FunctionTemplate::New(Batch)->GetFunction());
+ tpl->PrototypeTemplate()->Set(String::NewSymbol("approximateSize") , FunctionTemplate::New(ApproximateSize)->GetFunction());
constructor = Persistent<Function>::New(tpl->GetFunction());
}
@@ -269,3 +276,30 @@ Handle<Value> Database::Batch (const Arguments& args) {
return Undefined();
}
+
+Handle<Value> Database::ApproximateSize (const Arguments& args) {
+ HandleScope scope;
+
+ Database* database = ObjectWrap::Unwrap<Database>(args.This());
+ Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
+
+ CB_ERR_IF_NULL_OR_UNDEFINED(0, "start")
+ CB_ERR_IF_NULL_OR_UNDEFINED(1, "end")
+
+ Persistent<Value> startBuffer = Persistent<Value>::New(args[0]);
+ STRING_OR_BUFFER_TO_SLICE(start, startBuffer)
+ Persistent<Value> endBuffer = Persistent<Value>::New(args[1]);
+ STRING_OR_BUFFER_TO_SLICE(end, endBuffer)
+
+ ApproximateSizeWorker* worker = new ApproximateSizeWorker(
+ database
+ , callback
+ , start
+ , end
+ , startBuffer
+ , endBuffer
+ );
+ AsyncQueueWorker(worker);
+
+ return Undefined();
+}
diff --git a/src/database.h b/src/database.h
index dc707cc..cdfcfb2 100644
--- a/src/database.h
+++ b/src/database.h
@@ -39,6 +39,7 @@ public:
Status GetFromDatabase (ReadOptions* options, Slice key, string& value);
Status DeleteFromDatabase (WriteOptions* options, Slice key);
Status WriteBatchToDatabase (WriteOptions* options, WriteBatch* batch);
+ uint64_t ApproximateSizeFromDatabase (const Range* range);
leveldb::Iterator* NewIterator (ReadOptions* options);
const leveldb::Snapshot* NewSnapshot ();
void ReleaseSnapshot (const leveldb::Snapshot* snapshot);
@@ -59,6 +60,7 @@ private:
LU_V8_METHOD( Delete )
LU_V8_METHOD( Get )
LU_V8_METHOD( Batch )
+ LU_V8_METHOD( ApproximateSize)
};
#endif
diff --git a/src/database_async.cc b/src/database_async.cc
index 55b167d..555713e 100644
--- a/src/database_async.cc
+++ b/src/database_async.cc
@@ -113,3 +113,24 @@ void BatchWorker::Execute () {
}
status = database->WriteBatchToDatabase(options, &batch);
}
+
+/** APPROXIMATE SIZE WORKER **/
+
+void ApproximateSizeWorker::Execute () {
+ size = database->ApproximateSizeFromDatabase(&range);
+}
+
+void ApproximateSizeWorker::WorkComplete() {
+ AsyncWorker::WorkComplete();
+ startPtr.Dispose();
+ endPtr.Dispose();
+}
+
+void ApproximateSizeWorker::HandleOKCallback () {
+ Local<Value> returnValue = Number::New((double) size);
+ Local<Value> argv[] = {
+ Local<Value>::New(Null())
+ , returnValue
+ };
+ RunCallback(callback, argv, 2);
+}
diff --git a/src/database_async.h b/src/database_async.h
index 76cbe94..50d2213 100644
--- a/src/database_async.h
+++ b/src/database_async.h
@@ -162,4 +162,30 @@ private:
vector<BatchOp*>* operations;
};
+class ApproximateSizeWorker : public AsyncWorker {
+public:
+ ApproximateSizeWorker (
+ Database* database
+ , Persistent<Function> callback
+ , Slice start
+ , Slice end
+ , Persistent<Value> startPtr
+ , Persistent<Value> endPtr
+ ) : AsyncWorker(database, callback)
+ , range(start, end)
+ , startPtr(startPtr)
+ , endPtr(endPtr)
+ {};
+
+ virtual void Execute ();
+ virtual void HandleOKCallback ();
+ virtual void WorkComplete ();
+
+ private:
+ Range range;
+ Persistent<Value> startPtr;
+ Persistent<Value> endPtr;
+ uint64_t size;
+};
+
#endif
diff --git a/test/simple-test.js b/test/simple-test.js
index 6c77af3..4b79361 100644
--- a/test/simple-test.js
+++ b/test/simple-test.js
@@ -409,6 +409,67 @@ buster.testCase('Basic API', {
}
}
+ , 'approximateSize()': {
+ 'approximateSize() works on empty database': function (done) {
+ this.openTestDatabase(function (db) {
+ db.approximateSize('a', 'z', function(err, size) {
+ refute(err) // sanity
+ assert.equals(size, 0)
+ done()
+ })
+ })
+ }
+ , 'apporximateSize() work on none-empty database': function(done) {
+ var location = common.nextLocation()
+ var db
+ async.series(
+ [
+ function (callback) {
+ levelup(location, { createIfMissing: true, errorIfExists: true }, function (err, _db) {
+ refute(err)
+ db = _db
+ this.closeableDatabases.push(db)
+ this.cleanupDirs.push(location)
+ callback()
+ }.bind(this))
+ }.bind(this)
+ , function (callback) {
+ var batch = [];
+ for(var i = 0; i < 10; ++i) {
+ batch.push({
+ type: 'put', key: String(i), value: 'afoovalue'
+ });
+ }
+ db.batch(
+ batch
+ , { sync: true }
+ , callback
+ )
+ }
+ , function (callback) {
+ // close db to make sure stuff gets written to disc
+ db.close(callback)
+ }
+ , function (callback) {
+ levelup(location, function (err, _db) {
+ refute(err)
+ db = _db
+ callback()
+ })
+ }
+ , function (callback) {
+ db.approximateSize('', '99', function(err, size) {
+ refute(err) // sanity
+ refute.equals(size, 0)
+ done()
+ })
+ }
+ ]
+ , done
+ )
+ }
+ }
+
, 'null and undefined': {
'setUp': function (done) {
levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: true }, function (err, db) {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list