[Pkg-javascript-commits] [node-leveldown] 28/492: 'start' and 'end' options for ReadStreams

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:41 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 699a0f118f51498cafce0e2ea694fc6b8ab0c32c
Author: Rod Vagg <rod at vagg.org>
Date:   Sat Aug 18 16:55:37 2012 +1000

    'start' and 'end' options for ReadStreams
---
 lib/levelup.js           |  6 ++++--
 lib/read-stream.js       |  6 ++++++
 src/database.h           |  3 +--
 src/iterator.cc          | 28 ++++++++++++++++---------
 src/iterator.h           |  5 +++--
 src/levelup.h            |  3 +++
 test/read-stream-test.js | 54 ++++++++++++++++++++++++++++++++++++++++++++----
 7 files changed, 85 insertions(+), 20 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 7b29ace..e0f7869 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -203,11 +203,13 @@ var bridge       = require('bindings')('levelup.node')
         }
 
       , readStream: function (options) {
+          if (typeof options != 'object')
+            options = {}
           return readStream.create(
-              options || {}
+              options
             , this
             , function () {
-                return bridge.createIterator(this.db)
+                return bridge.createIterator(this.db, options)
               }.bind(this)
           )
         }
diff --git a/lib/read-stream.js b/lib/read-stream.js
index b86e554..a597d81 100644
--- a/lib/read-stream.js
+++ b/lib/read-stream.js
@@ -4,6 +4,7 @@ var Stream       = require("stream").Stream
   , BufferStream = require('bufferstream')
 
   , toEncoding   = require('./util').toEncoding
+  , toBuffer     = require('./util').toBuffer
   , Creator      = require('./creator').Creator
 
   , defaultOptions = {}
@@ -16,6 +17,11 @@ var Stream       = require("stream").Stream
       this.readable = true
       this.writable = false
 
+      if (this._options.start)
+        this._options.start = toBuffer(this._options.start)
+      if (this._options.end)
+        this._options.end = toBuffer(this._options.end)
+
       var ready = function () {
         if (this._status == 'ended')
           return
diff --git a/src/database.h b/src/database.h
index a16cc9f..1c333bb 100644
--- a/src/database.h
+++ b/src/database.h
@@ -8,8 +8,7 @@
 
 #include "leveldb/db.h"
 
-#define LU_V8_METHOD(name) \
-  static v8::Handle<v8::Value> name (const v8::Arguments& args);
+#include "levelup.h"
 
 using namespace std;
 using namespace v8;
diff --git a/src/iterator.cc b/src/iterator.cc
index 4be1f28..fa018cb 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -15,12 +15,8 @@ using namespace v8;
 using namespace node;
 using namespace levelup;
 
-/*
-void dataCallbackProxy (void* ctx, Slice key, Slice value) {
-  IteratorWorker* worker = static_cast<IteratorWorker*>(ctx);
-  worker->DataCallback(key, value);
-}
-*/
+LU_OPTION ( start );
+LU_OPTION ( end );
 
 bool levelup::Iterator::GetIterator () {
   if (dbIterator == NULL) {
@@ -36,7 +32,8 @@ bool levelup::Iterator::GetIterator () {
 
 bool levelup::Iterator::IteratorNext (string& key, string& value) {
   if (!GetIterator()) dbIterator->Next();
-  if (dbIterator->Valid()) { // && (end == NULL || dbIterator->key().data() < end->data())) {
+  // 'end' here is an inclusive test
+  if (dbIterator->Valid() && (end == NULL || end->compare(dbIterator->key().ToString()) > 0)) {
     key.assign(dbIterator->key().data(), dbIterator->key().size());
     value.assign(dbIterator->value().data(), dbIterator->value().size());
     return true;
@@ -96,10 +93,11 @@ void levelup::Iterator::Init () {
 Handle<Value> levelup::Iterator::NewInstance (const Arguments& args) {
   HandleScope scope;
 
-  Handle<Value> argv[1] = {
+  Handle<Value> argv[2] = {
       args[0]->ToObject()
+    , args[1]->ToObject()
   };
-  Local<Object> instance = constructor->NewInstance(1, argv);
+  Local<Object> instance = constructor->NewInstance(2, argv);
 
   return scope.Close(instance);
 }
@@ -108,7 +106,17 @@ Handle<Value> levelup::Iterator::New (const Arguments& args) {
   HandleScope scope;
 
   Database* database = ObjectWrap::Unwrap<Database>(args[0]->ToObject());
-  Iterator* iterator = new Iterator(database, NULL, NULL);
+  Slice* start = NULL;
+  if (args[1]->ToObject()->Has(option_start)) {
+    Persistent<Object> startBuffer = Persistent<Object>::New(args[1]->ToObject()->Get(option_start)->ToObject());
+    start = new Slice(Buffer::Data(startBuffer), Buffer::Length(startBuffer));
+  }
+  string* end = NULL;
+  if (args[1]->ToObject()->Has(option_end)) {
+    Persistent<Object> endBuffer = Persistent<Object>::New(args[1]->ToObject()->Get(option_end)->ToObject());
+    end = new string(Buffer::Data(endBuffer), Buffer::Length(endBuffer));
+  }
+  Iterator* iterator = new Iterator(database, start, end);
   iterator->Wrap(args.This());
 
   return args.This();
diff --git a/src/iterator.h b/src/iterator.h
index 0b4f4ad..8b3be29 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -6,6 +6,7 @@
 #include <cstdlib>
 #include <node.h>
 
+#include "levelup.h"
 #include "database.h"
 
 using namespace std;
@@ -29,7 +30,7 @@ private:
   Iterator (
       Database*            database
     , Slice*               start
-    , Slice*               end
+    , string*              end
   ) : database(database)
     , start(start)
     , end(end)
@@ -50,7 +51,7 @@ private:
   leveldb::Iterator*   dbIterator;
   ReadOptions*         options;
   Slice*               start;
-  Slice*               end;
+  string*              end;
 
   bool GetIterator ();
 
diff --git a/src/levelup.h b/src/levelup.h
index dc7aeb3..83e03c1 100644
--- a/src/levelup.h
+++ b/src/levelup.h
@@ -9,6 +9,9 @@
 #define LU_OPTION(key) \
   static Persistent<String> option_ ## key = Persistent<String>::New(String::New(#key));
 
+#define LU_V8_METHOD(name) \
+  static v8::Handle<v8::Value> name (const v8::Arguments& args);
+
 const char* ToCString(const v8::String::Utf8Value& value);
 const char* ToCString(const v8::String::AsciiValue& value);
 
diff --git a/test/read-stream-test.js b/test/read-stream-test.js
index b4290a9..17de606 100644
--- a/test/read-stream-test.js
+++ b/test/read-stream-test.js
@@ -208,13 +208,59 @@ buster.testCase('ReadStream', {
       }.bind(this))
     }
 
-/*
   , 'test readStream() with "start"': function (done) {
       this.openTestDatabase(function (db) {
         db.batch(this.sourceData.slice(), function (err) {
           refute(err)
-        })
-      })
+
+          var rs = db.readStream({ start: '50' })
+          assert.isFalse(rs.writable)
+          assert.isTrue(rs.readable)
+          rs.on('ready', this.readySpy)
+          rs.on('data' , this.dataSpy)
+          rs.on('end'  , this.endSpy)
+          rs.on('close', this.verify.bind(this, rs, done))
+
+          // slice off the first 50 so verify() expects only the last 50 even though all 100 are in the db
+          this.sourceData = this.sourceData.slice(50)
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test readStream() with "end"': function (done) {
+      this.openTestDatabase(function (db) {
+        db.batch(this.sourceData.slice(), function (err) {
+          refute(err)
+
+          var rs = db.readStream({ end: '50' })
+          assert.isFalse(rs.writable)
+          assert.isTrue(rs.readable)
+          rs.on('ready', this.readySpy)
+          rs.on('data' , this.dataSpy)
+          rs.on('end'  , this.endSpy)
+          rs.on('close', this.verify.bind(this, rs, done))
+
+          // slice off the last 50 so verify() expects only the first 50 even though all 100 are in the db
+          this.sourceData = this.sourceData.slice(0, 50)
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test readStream() with both "start" and "end"': function (done) {
+      this.openTestDatabase(function (db) {
+        db.batch(this.sourceData.slice(), function (err) {
+          refute(err)
+
+          var rs = db.readStream({ start: 30, end: 70 })
+          assert.isFalse(rs.writable)
+          assert.isTrue(rs.readable)
+          rs.on('ready', this.readySpy)
+          rs.on('data' , this.dataSpy)
+          rs.on('end'  , this.endSpy)
+          rs.on('close', this.verify.bind(this, rs, done))
+
+          this.sourceData = this.sourceData.slice(30, 70)
+        }.bind(this))
+      }.bind(this))
     }
-*/
 })
\ 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