[Pkg-javascript-commits] [node-leveldown] 125/492: speed up batch() & allow non-Strings to C++

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:51 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 6ed429ecbb66200d9618a726a852d5e34a1f34a4
Author: Rod Vagg <rod at vagg.org>
Date:   Sun Dec 16 23:36:56 2012 +1100

    speed up batch() & allow non-Strings to C++
---
 lib/levelup.js                 | 30 +++++++++++++++++++-----------
 src/database.cc                | 11 ++---------
 src/levelup.h                  | 21 ++++++---------------
 test/benchmarks/tests/index.js |  2 +-
 4 files changed, 28 insertions(+), 36 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 483c257..e48547a 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -242,18 +242,26 @@ LevelUP.prototype = {
       options       = getOptions(options_, this._options)
       keyEncoding   = options.keyEncoding   || options.encoding
       valueEncoding = options.valueEncoding || options.encoding
-      arr           = arr_.map(function (e) {
-        if (e.type !== undefined && e.key !== undefined) {
-          var o = {
-              type  : e.type
-            , key   : toSlice[keyEncoding](e.key)
+
+      // If we're not dealing with plain utf8 strings or plain
+      // Buffers then we have to do some work on the array to
+      // encode the keys and/or values. This includes JSON types.
+      if ((keyEncoding != 'utf8' && keyEncoding != 'binary')
+          || (valueEncoding != 'utf8' && valueEncoding != 'binary')) {
+        arr = arr_.map(function (e) {
+          if (e.type !== undefined && e.key !== undefined) {
+            var o = {
+                type  : e.type
+              , key   : toSlice[keyEncoding](e.key)
+            }
+            if (e.value !== undefined)
+              o.value = toSlice[valueEncoding](e.value)
+            return o
           }
-          if (e.value !== undefined)
-            o.value = toSlice[valueEncoding](e.value)
-          return o
-        }
-        return {}
-      })
+          return {}
+        })
+      } else
+        arr = arr_
       this._db.batch(arr, options, function (err) {
         if (err) {
           err = new errors.WriteError(err)
diff --git a/src/database.cc b/src/database.cc
index bab5198..a68a1c6 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -158,9 +158,7 @@ Handle<Value> Database::Put (const Arguments& args) {
   Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[3]));
 
   CB_ERR_IF_NULL_OR_UNDEFINED(0, "Key")
-  CB_ERR_IF_NOT_BUFFER_OR_STRING(0, "Key")
   CB_ERR_IF_NULL_OR_UNDEFINED(1, "Value")
-  CB_ERR_IF_NOT_BUFFER_OR_STRING(1, "Value")
 
   Persistent<Value> keyBuffer = Persistent<Value>::New(args[0]);
   STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
@@ -190,7 +188,6 @@ Handle<Value> Database::Get (const Arguments& args) {
   Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
 
   CB_ERR_IF_NULL_OR_UNDEFINED(0, "Key")
-  CB_ERR_IF_NOT_BUFFER_OR_STRING(0, "Key")
 
   Persistent<Value> keyBuffer = Persistent<Value>::New(args[0]);
   STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
@@ -216,7 +213,6 @@ Handle<Value> Database::Delete (const Arguments& args) {
   Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
 
   CB_ERR_IF_NULL_OR_UNDEFINED(0, "Key")
-  CB_ERR_IF_NOT_BUFFER_OR_STRING(0, "Key")
 
   Persistent<Value> keyBuffer = Persistent<Value>::New(args[0]);
   STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
@@ -254,18 +250,15 @@ Handle<Value> Database::Batch (const Arguments& args) {
       continue;
 
     Local<Value> keyBuffer = obj->Get(str_key);
-    if (!keyBuffer->IsString() && !Buffer::HasInstance(keyBuffer))
-      continue;
-    STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
 
     if (obj->Get(str_type)->StrictEquals(str_del)) {
+      STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
       operations->push_back(new BatchDelete(key, Persistent<Value>::New(keyBuffer)));
     } else if (obj->Get(str_type)->StrictEquals(str_put) && obj->Has(str_value)) {
       if (!obj->Has(str_value))
         continue;
       Local<Value> valueBuffer = obj->Get(str_value);
-      if (!valueBuffer->IsString() && !Buffer::HasInstance(valueBuffer))
-        continue;
+      STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
       STRING_OR_BUFFER_TO_SLICE(value, valueBuffer)
       operations->push_back(new BatchWrite(key, value, Persistent<Value>::New(keyBuffer), Persistent<Value>::New(valueBuffer)));
     }
diff --git a/src/levelup.h b/src/levelup.h
index 8fa74ce..f61becb 100644
--- a/src/levelup.h
+++ b/src/levelup.h
@@ -21,26 +21,17 @@
     return Undefined(); \
   }
 
-#define CB_ERR_IF_NOT_BUFFER_OR_STRING(index, name) \
-  if (!args[index]->IsString() && !Buffer::HasInstance(args[index])) { \
-    Local<Value> argv[] = { \
-      Local<Value>::New(Exception::Error(String::New("name must be a Buffer or a String"))) \
-    }; \
-    RunCallback(callback, argv, 1); \
-    return Undefined(); \
-  }
-
 #define STRING_OR_BUFFER_TO_SLICE(to, from) \
   size_t to ## Sz_; \
   char* to ## Ch_; \
-  if (from->IsString()) { \
-    to ## Sz_ = from->ToString()->Utf8Length(); \
-    to ## Ch_ = new char[to ## Sz_]; \
-    from->ToString()->WriteUtf8(to ## Ch_, -1, NULL, String::NO_NULL_TERMINATION); \
-  } else { \
-    assert(Buffer::HasInstance(from->ToObject())); \
+  if (Buffer::HasInstance(from->ToObject())) { \
     to ## Sz_ = Buffer::Length(from->ToObject()); \
     to ## Ch_ = Buffer::Data(from->ToObject()); \
+  } else { \
+    Local<String> to ## Str = from->ToString(); \
+    to ## Sz_ = to ## Str->Utf8Length(); \
+    to ## Ch_ = new char[to ## Sz_]; \
+    to ## Str->WriteUtf8(to ## Ch_, -1, NULL, String::NO_NULL_TERMINATION); \
   } \
   Slice to(to ## Ch_, to ## Sz_);
 
diff --git a/test/benchmarks/tests/index.js b/test/benchmarks/tests/index.js
index 1bfc45c..55c1bb4 100644
--- a/test/benchmarks/tests/index.js
+++ b/test/benchmarks/tests/index.js
@@ -43,7 +43,7 @@ module.exports = {
       , 'SQLite3'             : require('./get_int_string_x1000_sqlite3')
     }
 
-  , '=>batch(int, string) x 1000': {
+  , 'batch(int, string) x 1000': {
         'LevelUP'             : require('./batch_int_string_x1000_levelup')
       , 'LevelUP (release)'   : require('./batch_int_string_x1000_levelup')
       , 'LevelUP (no Snappy)' : require('./batch_int_string_x1000_levelup')

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