From 271894f334b320b010ee72d3f74bf7fd9a73d2a8 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 10 Apr 2021 00:31:43 +0200 Subject: [PATCH] Breaking: bump abstract-leveldown Removes legacy ranges options (Level/community#86). Cherry-picked from Level/leveldown@15d5a9e --- binding.cc | 85 ++++++++++----------------------- package.json | 2 +- test/empty-range-option-test.js | 2 +- test/iterator-recursion-test.js | 6 ++- test/stack-blower.js | 2 +- 5 files changed, 33 insertions(+), 64 deletions(-) diff --git a/binding.cc b/binding.cc index 9ead8fac..1899b3a9 100644 --- a/binding.cc +++ b/binding.cc @@ -512,8 +512,6 @@ struct PriorityWorker : public BaseWorker { struct Iterator { Iterator (Database* database, uint32_t id, - std::string* start, - std::string* end, bool reverse, bool keys, bool values, @@ -528,8 +526,6 @@ struct Iterator { uint32_t highWaterMark) : database_(database), id_(id), - start_(start), - end_(end), reverse_(reverse), keys_(keys), values_(values), @@ -558,8 +554,6 @@ struct Iterator { ~Iterator () { assert(ended_); - if (start_ != NULL) delete start_; - if (end_ != NULL) delete end_; if (lt_ != NULL) delete lt_; if (gt_ != NULL) delete gt_; if (lte_ != NULL) delete lte_; @@ -593,32 +587,29 @@ struct Iterator { dbIterator_ = database_->NewIterator(options_); - if (start_ != NULL) { - dbIterator_->Seek(*start_); + if (!reverse_ && gte_ != NULL) { + dbIterator_->Seek(*gte_); + } else if (!reverse_ && gt_ != NULL) { + dbIterator_->Seek(*gt_); - if (reverse_) { - if (!dbIterator_->Valid()) { - dbIterator_->SeekToLast(); - } else { - leveldb::Slice key = dbIterator_->key(); - - if ((lt_ != NULL && key.compare(*lt_) >= 0) || - (lte_ != NULL && key.compare(*lte_) > 0) || - (start_ != NULL && key.compare(*start_) > 0)) { - dbIterator_->Prev(); - } - } + if (dbIterator_->Valid() && dbIterator_->key().compare(*gt_) == 0) { + dbIterator_->Next(); + } + } else if (reverse_ && lte_ != NULL) { + dbIterator_->Seek(*lte_); - // TODO: this looks like dead code. Remove it in a - // next major release together with Level/community#86. - if (dbIterator_->Valid() && lt_ != NULL) { - if (dbIterator_->key().compare(*lt_) >= 0) - dbIterator_->Prev(); - } - } else { - if (dbIterator_->Valid() && gt_ != NULL - && dbIterator_->key().compare(*gt_) == 0) - dbIterator_->Next(); + if (!dbIterator_->Valid()) { + dbIterator_->SeekToLast(); + } else if (dbIterator_->key().compare(*lte_) > 0) { + dbIterator_->Prev(); + } + } else if (reverse_ && lt_ != NULL) { + dbIterator_->Seek(*lt_); + + if (!dbIterator_->Valid()) { + dbIterator_->SeekToLast(); + } else if (dbIterator_->key().compare(*lt_) >= 0) { + dbIterator_->Prev(); } } else if (reverse_) { dbIterator_->SeekToLast(); @@ -643,12 +634,8 @@ struct Iterator { if (dbIterator_->Valid()) { std::string keyStr = dbIterator_->key().ToString(); - const int isEnd = end_ == NULL ? 1 : end_->compare(keyStr); if ((limit_ < 0 || ++count_ <= limit_) - && (end_ == NULL - || (reverse_ && (isEnd <= 0)) - || (!reverse_ && (isEnd >= 0))) && ( lt_ != NULL ? (lt_->compare(keyStr) > 0) : lte_ != NULL ? (lte_->compare(keyStr) >= 0) : true ) @@ -670,20 +657,10 @@ struct Iterator { } bool OutOfRange (leveldb::Slice& target) { - if ((lt_ != NULL && target.compare(*lt_) >= 0) || - (lte_ != NULL && target.compare(*lte_) > 0) || - (start_ != NULL && reverse_ && target.compare(*start_) > 0)) { - return true; - } - - if (end_ != NULL) { - int d = target.compare(*end_); - if (reverse_ ? d < 0 : d > 0) return true; - } - - return ((gt_ != NULL && target.compare(*gt_) <= 0) || - (gte_ != NULL && target.compare(*gte_) < 0) || - (start_ != NULL && !reverse_ && target.compare(*start_) < 0)); + return ((lt_ != NULL && target.compare(*lt_) >= 0) || + (lte_ != NULL && target.compare(*lte_) > 0) || + (gt_ != NULL && target.compare(*gt_) <= 0) || + (gte_ != NULL && target.compare(*gte_) < 0)); } bool IteratorNext (std::vector >& result) { @@ -716,8 +693,6 @@ struct Iterator { Database* database_; uint32_t id_; - std::string* start_; - std::string* end_; bool reverse_; bool keys_; bool values_; @@ -1305,21 +1280,13 @@ NAPI_METHOD(iterator_init) { uint32_t highWaterMark = Uint32Property(env, options, "highWaterMark", 16 * 1024); - std::string* start = NULL; - std::string* end = RangeOption(env, options, "end"); std::string* lt = RangeOption(env, options, "lt"); std::string* lte = RangeOption(env, options, "lte"); std::string* gt = RangeOption(env, options, "gt"); std::string* gte = RangeOption(env, options, "gte"); - if (!reverse && gte != NULL) start = new std::string(*gte); - else if (!reverse && gt != NULL) start = new std::string(*gt); - else if (reverse && lte != NULL) start = new std::string(*lte); - else if (reverse && lt != NULL) start = new std::string(*lt); - else start = RangeOption(env, options, "start"); - uint32_t id = database->currentIteratorId_++; - Iterator* iterator = new Iterator(database, id, start, end, reverse, keys, + Iterator* iterator = new Iterator(database, id, reverse, keys, values, limit, lt, lte, gt, gte, fillCache, keyAsBuffer, valueAsBuffer, highWaterMark); napi_value result; diff --git a/package.json b/package.json index 10bde1d0..c70fa089 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "prepublishOnly": "npm run dependency-check" }, "dependencies": { - "abstract-leveldown": "^6.3.0", + "abstract-leveldown": "^7.0.0", "napi-macros": "^2.0.0", "node-gyp-build": "^4.2.3" }, diff --git a/test/empty-range-option-test.js b/test/empty-range-option-test.js index 6f6974de..d6a2fdec 100644 --- a/test/empty-range-option-test.js +++ b/test/empty-range-option-test.js @@ -1,7 +1,7 @@ const test = require('tape') const concat = require('level-concat-iterator') const testCommon = require('./common') -const rangeOptions = 'start end gt gte lt lte'.split(' ') +const rangeOptions = ['gt', 'gte', 'lt', 'lte'] test('empty range options are ignored', function (t) { const db = testCommon.factory() diff --git a/test/iterator-recursion-test.js b/test/iterator-recursion-test.js index aa4b96e1..fbdc4293 100644 --- a/test/iterator-recursion-test.js +++ b/test/iterator-recursion-test.js @@ -68,8 +68,10 @@ test('iterate over a large iterator with a large watermark', function (t) { highWaterMark: 10000000 }) const read = function () { - iterator.next(function () { - if (!arguments.length) { + iterator.next(function (err, key, value) { + if (err) throw err + + if (key === undefined && value === undefined) { t.end() } else { read() diff --git a/test/stack-blower.js b/test/stack-blower.js index 24f0a18c..390bcbb5 100644 --- a/test/stack-blower.js +++ b/test/stack-blower.js @@ -13,7 +13,7 @@ if (process.argv[2] === 'run') { db.open(function () { function recurse () { - db.iterator({ start: '0' }) + db.iterator({ gte: '0' }) depth++ recurse() }