Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Breaking: bump abstract-leveldown
Browse files Browse the repository at this point in the history
Removes legacy ranges options (Level/community#86).

Cherry-picked from Level/leveldown@15d5a9e
  • Loading branch information
vweevers committed Apr 17, 2021
1 parent 9d36809 commit 271894f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 64 deletions.
85 changes: 26 additions & 59 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -528,8 +526,6 @@ struct Iterator {
uint32_t highWaterMark)
: database_(database),
id_(id),
start_(start),
end_(end),
reverse_(reverse),
keys_(keys),
values_(values),
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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();
Expand All @@ -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 )
Expand All @@ -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<std::pair<std::string, std::string> >& result) {
Expand Down Expand Up @@ -716,8 +693,6 @@ struct Iterator {

Database* database_;
uint32_t id_;
std::string* start_;
std::string* end_;
bool reverse_;
bool keys_;
bool values_;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion test/empty-range-option-test.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 4 additions & 2 deletions test/iterator-recursion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/stack-blower.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (process.argv[2] === 'run') {

db.open(function () {
function recurse () {
db.iterator({ start: '0' })
db.iterator({ gte: '0' })
depth++
recurse()
}
Expand Down

0 comments on commit 271894f

Please sign in to comment.