[Pkg-javascript-commits] [node-leveldown] 343/492: implement db.getProperty() closes #23

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:15 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 56b283c2d66eba0cb5d8b6c555a092986d8c214d
Author: Rod Vagg <rod at vagg.org>
Date:   Sat May 18 16:12:01 2013 +1000

    implement db.getProperty() closes #23
---
 README.md                | 17 ++++++++++++++-
 src/database.cc          | 37 +++++++++++++++++++++++++++++++
 src/database.h           |  2 ++
 test/getproperty-test.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index bd9ccbe..3d4365d 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,7 @@ Tested & supported platforms
   * <a href="#leveldown_del"><code><b>leveldown#del()</b></code></a>
   * <a href="#leveldown_batch"><code><b>leveldown#batch()</b></code></a>
   * <a href="#leveldown_approximateSize"><code><b>leveldown#approximateSize()</b></code></a>
+  * <a href="#leveldown_getProperty"><code><b>leveldown#getProperty()</b></code></a>
   * <a href="#leveldown_iterator"><code><b>leveldown#iterator()</b></code></a>
   * <a href="#iterator_next"><code><b>iterator#next()</b></code></a>
   * <a href="#iterator_end"><code><b>iterator#end()</b></code></a>
@@ -158,6 +159,20 @@ The `callback` function will be called with no arguments if the operation is suc
 
 
 --------------------------------------------------------
+<a name="leveldown_getProperty"></a>
+### leveldown#getProperty(property)
+<code>getProperty</code> can be used to get internal details from LevelDB. When issued with a valid property string, a readable string will be returned (this method is synchronous).
+
+Currently, the only valid properties are:
+
+* <b><code>'leveldb.num-files-at-levelN'</code></b>: return the number of files at level *N*, where N is an integer representing a valid level (e.g. "0").
+
+* <b><code>'leveldb.stats'</code></b>: returns a multi-line string describing statistics about LevelDB's internal operation.
+
+* <b><code>'leveldb.sstables'</code></b>: returns a multi-line string describing all of the *sstables* that make up contents of the current database.
+
+
+--------------------------------------------------------
 <a name="createIterator"></a>
 ### leveldown#iterator([options])
 <code>iterator()</code> is an instance method on an existing database object. It returns a new **Iterator** instance.
@@ -294,4 +309,4 @@ Copyright (c) 2012-2013 LevelDOWN contributors (listed above).
 
 LevelDOWN is licensed under an MIT +no-false-attribs license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
 
-*LevelDOWN builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the [New BSD Licence](http://opensource.org/licenses/BSD-3-Clause).*
+*LevelDOWN builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the [New BSD Licence](http://opensource.org/licenses/BSD-3-Clause).*
\ No newline at end of file
diff --git a/src/database.cc b/src/database.cc
index dc0522a..bf74015 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -78,6 +78,12 @@ uint64_t Database::ApproximateSizeFromDatabase (const leveldb::Range* range) {
   return size;
 }
 
+void Database::GetPropertyFromDatabase (
+      const leveldb::Slice& property
+    , std::string* value) {
+  db->GetProperty(property, value);
+}
+
 leveldb::Iterator* Database::NewIterator (leveldb::ReadOptions* options) {
   return db->NewIterator(*options);
 }
@@ -150,6 +156,10 @@ void Database::Init () {
     , v8::FunctionTemplate::New(ApproximateSize)->GetFunction()
   );
   tpl->PrototypeTemplate()->Set(
+      v8::String::NewSymbol("getProperty")
+    , v8::FunctionTemplate::New(GetProperty)->GetFunction()
+  );
+  tpl->PrototypeTemplate()->Set(
       v8::String::NewSymbol("iterator")
     , v8::FunctionTemplate::New(Iterator)->GetFunction()
   );
@@ -522,6 +532,33 @@ v8::Handle<v8::Value> Database::ApproximateSize (const v8::Arguments& args) {
   return v8::Undefined();
 }
 
+v8::Handle<v8::Value> Database::GetProperty (const v8::Arguments& args) {
+  v8::HandleScope scope;
+
+  v8::Local<v8::Value> propertyV = args[0];
+  v8::Local<v8::Function> callback; // for LD_CB_ERR_IF_NULL_OR_UNDEFINED
+
+  if (!propertyV->IsString()) {
+    LD_THROW_RETURN(getProperty() requires a valid `property` argument)
+  }
+
+  LD_CB_ERR_IF_NULL_OR_UNDEFINED(propertyV, property)
+
+  LD_STRING_OR_BUFFER_TO_SLICE(property, propertyV, property)
+
+  leveldown::Database* database =
+      node::ObjectWrap::Unwrap<leveldown::Database>(args.This());
+
+  std::string* value = new std::string();
+  database->GetPropertyFromDatabase(property, value);
+  v8::Local<v8::String> returnValue
+      = v8::String::New(value->c_str(), value->length());
+  delete value;
+  delete property.data();
+
+  return returnValue;
+}
+
 v8::Handle<v8::Value> Database::Iterator (const v8::Arguments& args) {
   v8::HandleScope scope;
 
diff --git a/src/database.h b/src/database.h
index 5a5a32a..d9874b8 100644
--- a/src/database.h
+++ b/src/database.h
@@ -75,6 +75,7 @@ public:
     , leveldb::WriteBatch* batch
   );
   uint64_t ApproximateSizeFromDatabase (const leveldb::Range* range);
+  void GetPropertyFromDatabase (const leveldb::Slice& property, std::string* value);
   leveldb::Iterator* NewIterator (leveldb::ReadOptions* options);
   const leveldb::Snapshot* NewSnapshot ();
   void ReleaseSnapshot (const leveldb::Snapshot* snapshot);
@@ -107,6 +108,7 @@ private:
   LD_V8_METHOD( Write    )
   LD_V8_METHOD( Iterator )
   LD_V8_METHOD( ApproximateSize )
+  LD_V8_METHOD( GetProperty )
 };
 
 } // namespace leveldown
diff --git a/test/getproperty-test.js b/test/getproperty-test.js
new file mode 100644
index 0000000..db48946
--- /dev/null
+++ b/test/getproperty-test.js
@@ -0,0 +1,57 @@
+const test       = require('tap').test
+    , testCommon = require('abstract-leveldown/testCommon')
+    , leveldown  = require('../')
+
+var db
+
+test('setUp common', testCommon.setUp)
+
+test('setUp db', function (t) {
+  db = leveldown(testCommon.location())
+  db.open(t.end.bind(t))
+})
+
+test('test argument-less getProperty() throws', function (t) {
+  t.throws(
+      db.getProperty.bind(db)
+    , { name: 'Error', message: 'getProperty() requires a valid `property` argument' }
+    , 'no-arg getProperty() throws'
+  )
+  t.end()
+})
+
+test('test non-string getProperty() throws', function (t) {
+  t.throws(
+      db.getProperty.bind(db, {})
+    , { name: 'Error', message: 'getProperty() requires a valid `property` argument' }
+    , 'no-arg getProperty() throws'
+  )
+  t.end()
+})
+
+test('test invalid getProperty() returns empty string', function (t) {
+  t.equal(db.getProperty('foo'), '', 'invalid property')
+  t.equal(db.getProperty('leveldb.foo'), '', 'invalid leveldb.* property')
+  t.end()
+})
+
+test('test invalid getProperty("leveldb.num-files-at-levelN") returns numbers', function (t) {
+  for (var i = 0; i < 7; i++)
+    t.equal(db.getProperty('leveldb.num-files-at-level' + i), '0', '"leveldb.num-files-at-levelN" === "0"')
+  t.end()
+})
+
+test('test invalid getProperty("leveldb.stats")', function (t) {
+  t.ok(db.getProperty('leveldb.stats').split('\n').length > 3, 'leveldb.stats has > 3 newlines')
+  t.end()
+})
+
+test('test invalid getProperty("leveldb.sstables")', function (t) {
+  var expected = [0,1,2,3,4,5,6].map(function (l) { return '--- level ' + l + ' ---' }).join('\n') + '\n'
+  t.equal(db.getProperty('leveldb.sstables'), expected, 'leveldb.sstables')
+  t.end()
+})
+
+test('tearDown', function (t) {
+  db.close(testCommon.tearDown.bind(null, t))
+})
\ No newline at end of file

-- 
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