[Pkg-javascript-commits] [node-leveldown] 116/492: return Strings not Buffers from C++ when possible
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:50 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 24effd6a804ecfdd9d54a21a924c9e74ad2006d0
Author: Rod Vagg <rod at vagg.org>
Date: Sun Dec 16 13:59:47 2012 +1100
return Strings not Buffers from C++ when possible
---
lib/levelup.js | 9 ++++++---
lib/read-stream.js | 17 ++++++++++-------
lib/util.js | 3 ++-
src/database.cc | 16 ++++++++++------
src/database_async.cc | 7 ++++++-
src/database_async.h | 3 +++
src/iterator.cc | 4 +++-
src/iterator.h | 25 ++++++++++++++++++-------
src/iterator_async.cc | 14 ++++++++++++--
9 files changed, 70 insertions(+), 28 deletions(-)
diff --git a/lib/levelup.js b/lib/levelup.js
index 3fc3ad4..483c257 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -117,7 +117,7 @@ LevelUP.prototype = {
, get: function (key_, options_, callback_) {
var open = this.isOpen()
- , callback, options, key, err
+ , callback, options, key, keyEnc, valueEnc, err
if (!open && !this.isClosed()) {
// limbo, defer the operation
@@ -130,7 +130,10 @@ LevelUP.prototype = {
if (open) {
options = getOptions(options_, this._options)
- key = toSlice[options.keyEncoding || options.encoding](key_)
+ keyEnc = options.keyEncoding || options.encoding
+ valueEnc = options.valueEncoding || options.encoding
+ key = toSlice[keyEnc](key_)
+ options.asBuffer = valueEnc != 'utf8' && valueEnc != 'json'
this._db.get(key, options, function (err, value) {
if (err) {
err = new errors.NotFoundError('Key not found in database [' + key_ + ']')
@@ -138,7 +141,7 @@ LevelUP.prototype = {
return callback(err)
throw err
}
- callback && callback(null, toEncoding[options.valueEncoding || options.encoding](value), key_)
+ callback && callback(null, toEncoding[valueEnc](value), key_)
})
} else {
err = new errors.ReadError('Database is not open')
diff --git a/lib/read-stream.js b/lib/read-stream.js
index 1e64e06..eea8e65 100644
--- a/lib/read-stream.js
+++ b/lib/read-stream.js
@@ -11,15 +11,15 @@ var Stream = require('stream').Stream
, makeKeyValueData = function (key, value) {
return {
- key: toEncoding[this._options.keyEncoding || this._options.encoding](key)
- , value: toEncoding[this._options.valueEncoding || this._options.encoding](value)
+ key: toEncoding[this._keyEncoding](key)
+ , value: toEncoding[this._valueEncoding](value)
}
}
, makeKeyData = function (key) {
- return toEncoding[this._options.keyEncoding || this._options.encoding](key)
+ return toEncoding[this._keyEncoding](key)
}
, makeValueData = function (key, value) {
- return toEncoding[this._options.valueEncoding || this._options.encoding](value)
+ return toEncoding[this._valueEncoding](value)
}
, makeNoData = function () { return null }
@@ -37,13 +37,16 @@ function ReadStream (options, db, iteratorFactory) {
this._db = db
options = this._options = extend(extend({}, defaultOptions), options)
- var keyEncoding = options.keyEncoding || options.encoding
+ this._keyEncoding = options.keyEncoding || options.encoding
+ this._valueEncoding = options.valueEncoding || options.encoding
if (typeof this._options.start != 'undefined')
- this._options.start = toSlice[keyEncoding](this._options.start)
+ this._options.start = toSlice[this._keyEncoding](this._options.start)
if (typeof this._options.end != 'undefined')
- this._options.end = toSlice[keyEncoding](this._options.end)
+ this._options.end = toSlice[this._keyEncoding](this._options.end)
if (typeof this._options.limit != 'number')
this._options.limit = -1
+ this._options.keyAsBuffer = this._keyEncoding != 'utf8' && this._keyEncoding != 'json'
+ this._options.valueAsBuffer = this._valueEncoding != 'utf8' && this._valueEncoding != 'json'
this._makeData = this._options.keys && this._options.values
? makeKeyValueData.bind(this) : this._options.keys
diff --git a/lib/util.js b/lib/util.js
index 2efe8d7..ed70030 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -19,7 +19,8 @@ var encodings = 'hex utf8 utf-8 ascii binary base64 ucs2 ucs-2 utf16le utf-16le'
, toEncoding = (function () {
var encoders = {}
- encoders.json = function (buffer) { return JSON.parse(buffer.toString('utf8')) }
+ encoders.json = function (str) { return JSON.parse(str) }
+ encoders.utf8 = function (str) { return str }
encoders.binary = function (buffer) { return buffer }
encodings.forEach(function (enc) {
if (encoders[enc]) return
diff --git a/src/database.cc b/src/database.cc
index 711ff3b..bab5198 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -24,12 +24,13 @@ using namespace leveldb;
LU_OPTION ( createIfMissing ); // for open()
LU_OPTION ( errorIfExists ); // for open()
LU_OPTION ( compression ); // for open()
-LU_OPTION ( sync ); // for write() and delete()
-LU_STR ( key );
+LU_OPTION ( sync ); // for put() and delete()
+LU_OPTION ( asBuffer ); // for get()
+LU_STR ( key );
LU_STR ( value );
-LU_STR ( type );
-LU_STR ( del );
-LU_STR ( put );
+LU_STR ( type );
+LU_STR ( del );
+LU_STR ( put );
Database::Database () {
db = NULL;
@@ -118,10 +119,10 @@ Handle<Value> Database::Open (const Arguments& args) {
Database* database = ObjectWrap::Unwrap<Database>(args.This());
String::Utf8Value location(args[0]->ToString());
Local<Object> optionsObj = Local<Object>::Cast(args[1]);
- Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
BOOLEAN_OPTION_VALUE(optionsObj, createIfMissing)
BOOLEAN_OPTION_VALUE(optionsObj, errorIfExists)
BOOLEAN_OPTION_VALUE(optionsObj, compression)
+ Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
OpenWorker* worker = new OpenWorker(
database
@@ -193,11 +194,14 @@ Handle<Value> Database::Get (const Arguments& args) {
Persistent<Value> keyBuffer = Persistent<Value>::New(args[0]);
STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
+ Local<Object> optionsObj = Local<Object>::Cast(args[1]);
+ BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, asBuffer)
ReadWorker* worker = new ReadWorker(
database
, callback
, key
+ , asBuffer
, keyBuffer
);
AsyncQueueWorker(worker);
diff --git a/src/database_async.cc b/src/database_async.cc
index 17039b1..55b167d 100644
--- a/src/database_async.cc
+++ b/src/database_async.cc
@@ -62,9 +62,14 @@ void ReadWorker::Execute () {
}
void ReadWorker::HandleOKCallback () {
+ Local<Value> returnValue;
+ if (asBuffer)
+ returnValue = Local<Value>::New(Buffer::New((char*)value.data(), value.size())->handle_);
+ else
+ returnValue = String::New((char*)value.data(), value.size());
Local<Value> argv[] = {
Local<Value>::New(Null())
- , Local<Value>::New(Buffer::New((char*)value.data(), value.size())->handle_)
+ , returnValue
};
RunCallback(callback, argv, 2);
}
diff --git a/src/database_async.h b/src/database_async.h
index 5f22116..76cbe94 100644
--- a/src/database_async.h
+++ b/src/database_async.h
@@ -78,8 +78,10 @@ public:
Database* database
, Persistent<Function> callback
, Slice key
+ , bool asBuffer
, Persistent<Value> keyPtr
) : IOWorker(database, callback, key, keyPtr)
+ , asBuffer(asBuffer)
{
options = new ReadOptions();
};
@@ -89,6 +91,7 @@ public:
virtual void HandleOKCallback ();
private:
+ bool asBuffer;
ReadOptions* options;
string value;
};
diff --git a/src/iterator.cc b/src/iterator.cc
index 7ce8997..b37ed91 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -137,11 +137,13 @@ Handle<Value> levelup::Iterator::New (const Arguments& args) {
BOOLEAN_OPTION_VALUE(optionsObj, reverse)
BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, keys)
BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, values)
+ BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, keyAsBuffer)
+ BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, valueAsBuffer)
int limit = -1;
if (args[1]->ToObject()->Has(option_limit)) {
limit = Local<Integer>::Cast(args[1]->ToObject()->Get(option_limit))->Value();
}
- Iterator* iterator = new Iterator(database, start, end, reverse, keys, values, limit);
+ Iterator* iterator = new Iterator(database, start, end, reverse, keys, values, limit, keyAsBuffer, valueAsBuffer);
iterator->Wrap(args.This());
return args.This();
diff --git a/src/iterator.h b/src/iterator.h
index 4970350..3e48fd1 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -15,12 +15,14 @@ using namespace leveldb;
namespace levelup {
-LU_OPTION ( start );
-LU_OPTION ( end );
-LU_OPTION ( limit );
-LU_OPTION ( reverse );
-LU_OPTION ( keys );
-LU_OPTION ( values );
+LU_OPTION ( start );
+LU_OPTION ( end );
+LU_OPTION ( limit );
+LU_OPTION ( reverse );
+LU_OPTION ( keys );
+LU_OPTION ( values );
+LU_OPTION ( keyAsBuffer );
+LU_OPTION ( valueAsBuffer );
Handle<Value> CreateIterator (const Arguments& args);
@@ -33,7 +35,6 @@ public:
Status IteratorStatus ();
void IteratorEnd ();
-private:
Iterator (
Database* database
, Slice* start
@@ -42,6 +43,8 @@ private:
, bool keys
, bool values
, int limit
+ , bool keyAsBuffer
+ , bool valueAsBuffer
) : database(database)
, start(start)
, end(end)
@@ -49,6 +52,8 @@ private:
, keys(keys)
, values(values)
, limit(limit)
+ , keyAsBuffer(keyAsBuffer)
+ , valueAsBuffer(valueAsBuffer)
{
options = new ReadOptions();
dbIterator = NULL;
@@ -63,6 +68,7 @@ private:
delete end;
};
+private:
Database* database;
leveldb::Iterator* dbIterator;
ReadOptions* options;
@@ -74,6 +80,11 @@ private:
int limit;
int count;
+public:
+ bool keyAsBuffer;
+ bool valueAsBuffer;
+
+private:
bool GetIterator ();
static v8::Persistent<v8::Function> constructor;
diff --git a/src/iterator_async.cc b/src/iterator_async.cc
index 2a4788c..af3a701 100644
--- a/src/iterator_async.cc
+++ b/src/iterator_async.cc
@@ -26,11 +26,21 @@ void NextWorker::Execute () {
}
void NextWorker::HandleOKCallback () {
+ Local<Value> returnKey;
+ if (iterator->keyAsBuffer)
+ returnKey = Local<Value>::New(Buffer::New((char*)key.data(), key.size())->handle_);
+ else
+ returnKey = String::New((char*)key.data(), key.size());
+ Local<Value> returnValue;
+ if (iterator->valueAsBuffer)
+ returnValue = Local<Value>::New(Buffer::New((char*)value.data(), value.size())->handle_);
+ else
+ returnValue = String::New((char*)value.data(), value.size());
if (ok) {
Local<Value> argv[] = {
Local<Value>::New(Null())
- , Local<Value>::New(Buffer::New((char*)key.data(), key.size())->handle_)
- , Local<Value>::New(Buffer::New((char*)value.data(), value.size())->handle_)
+ , returnKey
+ , returnValue
};
RunCallback(callback, argv, 3);
} else {
--
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