[Pkg-javascript-commits] [node-leveldown] 301/492: Iterator: Ignore start/end if it's the empty string (or empty buffer)
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:11 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 a7b37d2b3f35850c2db512354ce8099a6af3985d
Author: David Björklund <david.bjorklund at gmail.com>
Date: Sat Apr 6 20:04:32 2013 +0200
Iterator: Ignore start/end if it's the empty string (or empty buffer)
---
src/iterator.cc | 17 +++++++++++++----
src/leveldown.h | 7 +++++++
test/iterator-test.js | 40 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 60 insertions(+), 4 deletions(-)
diff --git a/src/iterator.cc b/src/iterator.cc
index e5fdd22..235636d 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -233,6 +233,7 @@ v8::Handle<v8::Value> Iterator::New (const v8::Arguments& args) {
v8::Local<v8::Value> startBuffer;
leveldb::Slice* start = NULL;
+ size_t length;
std::string* end = NULL;
int limit = -1;
@@ -248,8 +249,12 @@ v8::Handle<v8::Value> Iterator::New (const v8::Arguments& args) {
|| optionsObj->Get(option_start)->IsString())) {
startBuffer = v8::Local<v8::Value>::New(optionsObj->Get(option_start));
- LD_STRING_OR_BUFFER_TO_SLICE(_start, startBuffer, start)
- start = new leveldb::Slice(_start.data(), _start.size());
+ LD_STRING_OR_BUFFER_LENGTH(startBuffer, length)
+ // ignore start if it has size 0 since a Slice can't have length 0
+ if (length > 0) {
+ LD_STRING_OR_BUFFER_TO_SLICE(_start, startBuffer, start)
+ start = new leveldb::Slice(_start.data(), _start.size());
+ }
}
if (optionsObj->Has(option_end)
@@ -258,8 +263,12 @@ v8::Handle<v8::Value> Iterator::New (const v8::Arguments& args) {
v8::Local<v8::Value> endBuffer =
v8::Local<v8::Value>::New(optionsObj->Get(option_end));
- LD_STRING_OR_BUFFER_TO_SLICE(_end, endBuffer, end)
- end = new std::string(_end.data(), _end.size());
+ LD_STRING_OR_BUFFER_LENGTH(endBuffer, length)
+ // ignore end if it has size 0 since a Slice can't have length 0
+ if (length > 0) {
+ LD_STRING_OR_BUFFER_TO_SLICE(_end, endBuffer, end)
+ end = new std::string(_end.data(), _end.size());
+ }
}
if (!optionsObj.IsEmpty() && optionsObj->Has(option_limit)) {
diff --git a/src/leveldown.h b/src/leveldown.h
index 43377dd..c4ad00a 100644
--- a/src/leveldown.h
+++ b/src/leveldown.h
@@ -60,6 +60,13 @@
} \
leveldb::Slice to(to ## Ch_, to ## Sz_);
+#define LD_STRING_OR_BUFFER_LENGTH(from, length) \
+ if (node::Buffer::HasInstance(from->ToObject())) { \
+ length = node::Buffer::Length(from->ToObject()); \
+ } else { \
+ length = from->ToString()->Utf8Length(); \
+ }
+
#define LD_BOOLEAN_OPTION_VALUE(optionsObj, opt) \
bool opt = !optionsObj.IsEmpty() \
&& optionsObj->Has(option_ ## opt) \
diff --git a/test/iterator-test.js b/test/iterator-test.js
index 3a4e1a6..14eceac 100644
--- a/test/iterator-test.js
+++ b/test/iterator-test.js
@@ -365,6 +365,46 @@ module.exports.iterator = function (leveldown) {
t.end()
})
})
+
+ test('test iterator with start as empty string', function(t) {
+ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: ''}), function (err, data) {
+ t.notOk(err, 'no error')
+ t.equal(data.length, 100, 'correct number of entries')
+ var expected = sourceData.map(transformSource)
+ t.deepEqual(data, expected)
+ t.end()
+ })
+ })
+
+ test('test iterator with start as empty buffer', function(t) {
+ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: new Buffer(0)}), function (err, data) {
+ t.notOk(err, 'no error')
+ t.equal(data.length, 100, 'correct number of entries')
+ var expected = sourceData.map(transformSource)
+ t.deepEqual(data, expected)
+ t.end()
+ })
+ })
+
+ test('test iterator with end as empty string', function(t) {
+ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: ''}), function (err, data) {
+ t.notOk(err, 'no error')
+ t.equal(data.length, 100, 'correct number of entries')
+ var expected = sourceData.map(transformSource)
+ t.deepEqual(data, expected)
+ t.end()
+ })
+ })
+
+ test('test iterator with end as empty string', function(t) {
+ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: new Buffer(0)}), function (err, data) {
+ t.notOk(err, 'no error')
+ t.equal(data.length, 100, 'correct number of entries')
+ var expected = sourceData.map(transformSource)
+ t.deepEqual(data, expected)
+ t.end()
+ })
+ })
}
module.exports.tearDown = function () {
--
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