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).
  • Loading branch information
vweevers committed Apr 10, 2021
1 parent 8bc5696 commit 15d5a9e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 66 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ Returns a new [`iterator`](#iterator) instance. The optional `options` object ma

- `lt` (less than), `lte` (less than or equal) define the higher bound of the range to be fetched and will determine the starting point where `reverse` is _not_ `true`. Only records where the key is less than (or equal to) this option will be included in the range. When `reverse` is `true` the order will be reversed, but the records returned will be the same.

- `start, end` legacy ranges - instead use `gte, lte`

- `reverse` _(boolean, default: `false`)_: a boolean, set to `true` if you want the stream to go in reverse order. Beware that due to the way LevelDB works, a reverse seek will be slower than a forward seek.

- `keys` (boolean, default: `true`): whether the callback to the `next()` method should receive a non-null `key`. There is a small efficiency gain if you ultimately don't care what the keys are as they don't need to be converted and copied into JavaScript.
Expand Down
85 changes: 26 additions & 59 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,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 @@ -515,8 +513,6 @@ struct Iterator {
uint32_t highWaterMark)
: database_(database),
id_(id),
start_(start),
end_(end),
reverse_(reverse),
keys_(keys),
values_(values),
Expand Down Expand Up @@ -544,8 +540,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 @@ -588,32 +582,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 @@ -638,12 +629,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 @@ -665,20 +652,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 @@ -711,8 +688,6 @@ struct Iterator {

Database* database_;
uint32_t id_;
std::string* start_;
std::string* end_;
bool reverse_;
bool keys_;
bool values_;
Expand Down Expand Up @@ -1251,21 +1226,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 @@ -24,7 +24,7 @@
"prebuild-darwin-x64": "prebuildify -t 8.14.0 --napi --strip"
},
"dependencies": {
"abstract-leveldown": "^6.3.0",
"abstract-leveldown": "^7.0.0",
"napi-macros": "~2.0.0",
"node-gyp-build": "~4.2.1"
},
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 15d5a9e

Please sign in to comment.