Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy range options and node versions #761

Merged
merged 7 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ updates:
interval: monthly
ignore:
- dependency-name: dependency-check
- dependency-name: cross-env
- dependency-name: node-gyp
- dependency-name: standard
- dependency-name: tempy
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ language: node_js
jobs:
include:
- os: linux
node_js: 8
node_js: 10
env: [TEST=1]
- os: linux
node_js: 10
node_js: 12
env: [TEST=1]
- os: linux
node_js: 14
env: [TEST=1]
- os: linux
node_js: node
Expand All @@ -19,10 +22,13 @@ jobs:
- export DISPLAY=':99.0'
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
- os: osx
node_js: 8
node_js: 10
env: [TEST=1]
- os: osx
node_js: 10
node_js: 12
env: [TEST=1]
- os: osx
node_js: 14
env: [TEST=1]
- os: osx
node_js: node
Expand Down
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
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ skip_branch_with_pr: true

environment:
matrix:
- nodejs_version: "8"
- nodejs_version: "10"
- nodejs_version: "12"
- nodejs_version: "Current"

configuration: Release
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: 2 additions & 0 deletions chained-batch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const util = require('util')
const AbstractChainedBatch = require('abstract-leveldown').AbstractChainedBatch
const binding = require('./binding')
Expand Down
12 changes: 6 additions & 6 deletions iterator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const util = require('util')
const AbstractIterator = require('abstract-leveldown').AbstractIterator
const binding = require('./binding')
Expand All @@ -23,19 +25,17 @@ Iterator.prototype._seek = function (target) {
}

Iterator.prototype._next = function (callback) {
var that = this

if (this.cache && this.cache.length) {
process.nextTick(callback, null, this.cache.pop(), this.cache.pop())
} else if (this.finished) {
process.nextTick(callback)
} else {
binding.iterator_next(this.context, function (err, array, finished) {
binding.iterator_next(this.context, (err, array, finished) => {
if (err) return callback(err)

that.cache = array
that.finished = finished
that._next(callback)
this.cache = array
this.finished = finished
this._next(callback)
})
}

Expand Down
2 changes: 2 additions & 0 deletions leveldown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const util = require('util')
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const binding = require('./binding')
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"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"
},
"devDependencies": {
"async-each": "^1.0.3",
"coveralls": "^3.0.2",
"cross-env": "^6.0.0",
"cross-env": "^7.0.3",
"delayed": "^2.0.0",
"dependency-check": "^4.1.0",
"du": "^1.0.0",
Expand All @@ -40,18 +40,18 @@
"glob": "^7.1.3",
"hallmark": "^3.0.0",
"level-community": "^3.0.0",
"level-concat-iterator": "^2.0.0",
"level-concat-iterator": "^3.0.0",
"mkfiletree": "^2.0.0",
"node-gyp": "^6.0.0",
"node-gyp": "^7.1.2",
"nyc": "^15.0.0",
"prebuildify": "^4.1.0",
"prebuildify-ci": "^1.0.4",
"prebuildify-cross": "^4.0.0",
"readfiletree": "^1.0.0",
"rimraf": "^3.0.0",
"standard": "^15.0.0",
"standard": "^16.0.3",
"tape": "^5.0.1",
"tempy": "^0.3.0"
"tempy": "^1.0.1"
},
"hallmark": {
"community": "level-community"
Expand All @@ -67,6 +67,6 @@
"level"
],
"engines": {
"node": ">=8.6.0"
"node": ">=10.12.0"
}
}
6 changes: 3 additions & 3 deletions test/approximate-size-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('tape')
const testCommon = require('./common')

var db
let db

test('setUp common for approximate size', testCommon.setUp)

Expand Down Expand Up @@ -66,7 +66,7 @@ test('test 1-arg + callback approximateSize() throws', function (t) {

test('test custom _serialize*', function (t) {
t.plan(4)
var db = testCommon.factory()
const db = testCommon.factory()
db._serializeKey = function (data) { return data }
db.approximateSize = function (start, end, callback) {
t.deepEqual(start, { foo: 'bar' })
Expand All @@ -82,7 +82,7 @@ test('test custom _serialize*', function (t) {
})

test('test approximateSize()', function (t) {
var data = Array.apply(null, Array(10000)).map(function () {
const data = Array.apply(null, Array(10000)).map(function () {
return 'aaaaaaaaaa'
}).join('')

Expand Down
10 changes: 5 additions & 5 deletions test/cleanup-hanging-iterators-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const repeats = 200

makeTest('test ended iterator', function (db, t, done) {
// First test normal and proper usage: calling it.end() before db.close()
var it = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })
const it = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })

it.next(function (err, key, value) {
t.ifError(err, 'no error from next()')
Expand All @@ -19,7 +19,7 @@ makeTest('test ended iterator', function (db, t, done) {
makeTest('test likely-ended iterator', function (db, t, done) {
// Test improper usage: not calling it.end() before db.close(). Cleanup of the
// database will crash Node if not handled properly.
var it = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })
const it = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })

it.next(function (err, key, value) {
t.ifError(err, 'no error from next()')
Expand All @@ -33,7 +33,7 @@ makeTest('test non-ended iterator', function (db, t, done) {
// Same as the test above but with a highWaterMark of 0 so that we don't
// preemptively fetch all records, to ensure that the iterator is still
// active when we (attempt to) close the database.
var it = db.iterator({
const it = db.iterator({
highWaterMark: 0,
keyAsBuffer: false,
valueAsBuffer: false
Expand Down Expand Up @@ -84,10 +84,10 @@ global.gc && makeTest('test multiple non-ended iterators with forced gc', functi

makeTest('test ending iterators', function (db, t, done) {
// At least one end() should be in progress when we try to close the db.
var it1 = db.iterator().next(function () {
const it1 = db.iterator().next(function () {
it1.end(function () {})
})
var it2 = db.iterator().next(function () {
const it2 = db.iterator().next(function () {
it2.end(function () {})
done()
})
Expand Down
12 changes: 6 additions & 6 deletions test/compact-range-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ test('setUp db', function (t) {
})

test('test compactRange() frees disk space after key deletion', function (t) {
var key1 = '000000'
var key2 = '000001'
var val1 = Buffer.allocUnsafe(64).fill(1)
var val2 = Buffer.allocUnsafe(64).fill(1)
const key1 = '000000'
const key2 = '000001'
const val1 = Buffer.allocUnsafe(64).fill(1)
const val2 = Buffer.allocUnsafe(64).fill(1)

db.batch().put(key1, val1).put(key2, val2).write(function (err) {
t.ifError(err, 'no batch put error')
Expand Down Expand Up @@ -46,8 +46,8 @@ test('test compactRange() frees disk space after key deletion', function (t) {
test('test compactRange() serializes start and end', function (t) {
t.plan(3)

var clone = Object.create(db)
var count = 0
const clone = Object.create(db)
let count = 0

clone._serializeKey = function (key) {
t.is(key, count++)
Expand Down
Loading