[Pkg-javascript-commits] [node-leveldown] 84/492: implement readStream() `limit` option

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:47 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 2fd94d7604f3c663e4f5bc4e1f8416be0beb5cc0
Author: Rod Vagg <rod at vagg.org>
Date:   Wed Nov 21 19:57:14 2012 +1100

    implement readStream() `limit` option
---
 README.md                |  2 ++
 lib/read-stream.js       |  8 ++++--
 src/iterator.cc          |  8 +++++-
 src/iterator.h           |  5 ++++
 test/read-stream-test.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 91 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index e9cdec7..94bfba6 100644
--- a/README.md
+++ b/README.md
@@ -252,6 +252,8 @@ Additionally, you can supply an options object as the first parameter to `readSt
 
 * `'values'`: a boolean (defaults to `true`) to indicate whether the `'data'` event should contain values. If set to `true` and `'keys'` set to `false` then `'data'` events will simply be values, rather than objects with a `'value'` property. Used internally by the `valueStream()` method.
 
+* `'limit'`: a number (defaults to -1) to limit the number of results collected by this stream. This number represents a *maximum* number of results and may not be reached if you get to the end of the store or your `'end'` value first. A value of -1 means there is no limit.
+
 --------------------------------------------------------
 <a name="keyStream"></a>
 ### db.keyStream()
diff --git a/lib/read-stream.js b/lib/read-stream.js
index ce434f5..1091b1d 100644
--- a/lib/read-stream.js
+++ b/lib/read-stream.js
@@ -38,10 +38,12 @@ function ReadStream (options, db, iteratorFactory) {
 
   options = this._options = extend(extend({}, defaultOptions), options)
   var keyEncoding = options.keyEncoding || options.encoding
-  if (typeof this._options.start !== 'undefined')
+  if (typeof this._options.start != 'undefined')
     this._options.start = toBuffer(this._options.start, keyEncoding)
-  if (typeof this._options.end !== 'undefined')
+  if (typeof this._options.end != 'undefined')
     this._options.end = toBuffer(this._options.end, keyEncoding)
+  if (typeof this._options.limit != 'number')
+    this._options.limit = -1
 
   this._makeData = this._options.keys && this._options.values
     ? makeKeyValueData.bind(this) : this._options.keys
@@ -147,4 +149,4 @@ ReadStream.prototype = {
 
 module.exports.create = function (options, db, iteratorFactory) {
   return new ReadStream(options, db, iteratorFactory)
-}
+}
\ No newline at end of file
diff --git a/src/iterator.cc b/src/iterator.cc
index 9e85314..371e460 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -17,6 +17,7 @@ using namespace levelup;
 
 LU_OPTION ( start );
 LU_OPTION ( end );
+LU_OPTION ( limit );
 LU_OPTION ( reverse );
 LU_OPTION ( keys );
 LU_OPTION ( values );
@@ -45,6 +46,7 @@ bool levelup::Iterator::IteratorNext (string& key, string& value) {
 
   // 'end' here is an inclusive test
   if (dbIterator->Valid()
+      && (limit < 0 || ++count <= limit)
       && (end == NULL
           || (reverse && end->compare(dbIterator->key().ToString()) <= 0)
           || (!reverse && end->compare(dbIterator->key().ToString()) >= 0))) {
@@ -146,7 +148,11 @@ Handle<Value> levelup::Iterator::New (const Arguments& args) {
   if (args[1]->ToObject()->Has(option_values)) {
     values = args[1]->ToObject()->Get(option_values)->BooleanValue();
   }
-  Iterator* iterator = new Iterator(database, start, end, reverse, keys, values);
+  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->Wrap(args.This());
 
   return args.This();
diff --git a/src/iterator.h b/src/iterator.h
index 968a2e2..bf844d6 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -34,15 +34,18 @@ private:
     , bool                 reverse
     , bool                 keys
     , bool                 values
+    , int                  limit
   ) : database(database)
     , start(start)
     , end(end)
     , reverse(reverse)
     , keys(keys)
     , values(values)
+    , limit(limit)
   {
     options = new ReadOptions();
     dbIterator = NULL;
+    count = 0;
   };
 
   ~Iterator () {
@@ -61,6 +64,8 @@ private:
   bool                 reverse;
   bool                 keys;
   bool                 values;
+  int                  limit;
+  int                  count;
 
   bool GetIterator ();
 
diff --git a/test/read-stream-test.js b/test/read-stream-test.js
index 444ebf6..f0c2ca7 100644
--- a/test/read-stream-test.js
+++ b/test/read-stream-test.js
@@ -592,4 +592,76 @@ buster.testCase('ReadStream', {
 
       this.openTestDatabase(setup)
     }
+
+  , 'test readStream() with "limit"': function (done) {
+      this.openTestDatabase(function (db) {
+        db.batch(this.sourceData.slice(), function (err) {
+          refute(err)
+
+          var rs = db.readStream({ limit: 20 })
+          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(0, 20)
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test readStream() with "start" and "limit"': function (done) {
+      this.openTestDatabase(function (db) {
+        db.batch(this.sourceData.slice(), function (err) {
+          refute(err)
+
+          var rs = db.readStream({ start: '20', limit: 20 })
+          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(20, 40)
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test readStream() with "end" after "limit"': function (done) {
+      this.openTestDatabase(function (db) {
+        db.batch(this.sourceData.slice(), function (err) {
+          refute(err)
+
+          var rs = db.readStream({ end: '50', limit: 20 })
+          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(0, 20)
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test readStream() with "end" before "limit"': function (done) {
+      this.openTestDatabase(function (db) {
+        db.batch(this.sourceData.slice(), function (err) {
+          refute(err)
+
+          var rs = db.readStream({ end: '30', limit: 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))
+
+          this.sourceData = this.sourceData.slice(0, 31)
+        }.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