Skip to content

Commit daf2a88

Browse files
authored
Breaking: make iterator.seek() a mandatory feature (#105)
All first-party implementations already support it (`classic-level`, `memory-level`, `browser-level`, `many-level` and `rave-level`). Category: change
1 parent ca3c368 commit daf2a88

File tree

5 files changed

+10
-16
lines changed

5 files changed

+10
-16
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,6 @@ The optional `options` object may contain:
597597

598598
If range options like `gt` were passed to `db.iterator()` and `target` does not fall within that range, the iterator will reach its natural end.
599599

600-
**Note:** Not all implementations support `seek()`. Consult `db.supports.seek` or the [support matrix](https://github.com/Level/supports#seek-boolean).
601-
602600
#### `iterator.close()`
603601

604602
Free up underlying resources. Returns a promise. Closing the iterator is an idempotent operation, such that calling `close()` more than once is allowed and makes no difference.
@@ -1349,14 +1347,14 @@ When a sublevel prefix contains characters outside of the supported byte range.
13491347

13501348
#### `LEVEL_NOT_SUPPORTED`
13511349

1352-
When a module needs a certain feature, typically as indicated by `db.supports`, but that feature is not available on a database argument or other. For example, some kind of plugin may depend on `seek()`:
1350+
When a module needs a certain feature, typically as indicated by `db.supports`, but that feature is not available on a database argument or other. For example, some kind of plugin may depend on snapshots:
13531351

13541352
```js
13551353
const ModuleError = require('module-error')
13561354

13571355
module.exports = function plugin (db) {
1358-
if (!db.supports.seek) {
1359-
throw new ModuleError('Database must support seeking', {
1356+
if (!db.supports.explicitSnapshots) {
1357+
throw new ModuleError('Database must support snapshots', {
13601358
code: 'LEVEL_NOT_SUPPORTED'
13611359
})
13621360
}
@@ -1767,7 +1765,7 @@ The default `_all()` is a functional default that makes repeated calls to `_next
17671765

17681766
#### `iterator._seek(target, options)`
17691767

1770-
Seek to the key closest to `target`. The `options` object will always have the following properties: `keyEncoding`. This method is optional. The default will throw an error with code [`LEVEL_NOT_SUPPORTED`](#errors). If supported, set `db.supports.seek` to `true` (via the manifest passed to the database constructor) which also enables relevant tests in the [test suite](#test-suite).
1768+
Seek to the key closest to `target`. The `options` object will always have the following properties: `keyEncoding`. The default `_seek()` will throw an error with code [`LEVEL_NOT_SUPPORTED`](#errors) and must be overridden.
17711769

17721770
#### `iterator._close()`
17731771

@@ -1921,7 +1919,7 @@ test('custom test', function (t) {
19211919
// ..
19221920
})
19231921

1924-
testCommon.supports.seek && test('another test', function (t) {
1922+
testCommon.supports.explicitSnapshots && test('another test', function (t) {
19251923
const db = testCommon.factory()
19261924
// ..
19271925
})

abstract-iterator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class CommonIterator {
226226
}
227227

228228
_seek (target, options) {
229-
throw new ModuleError('Iterator does not support seek()', {
229+
throw new ModuleError('Iterator does not implement seek()', {
230230
code: 'LEVEL_NOT_SUPPORTED'
231231
})
232232
}

abstract-level.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class AbstractLevel extends EventEmitter {
5252
this.hooks = new DatabaseHooks()
5353
this.supports = supports(manifest, {
5454
deferredOpen: true,
55-
56-
// TODO (next major): add seek
55+
seek: true,
5756
implicitSnapshots,
5857
permanence: manifest.permanence !== false,
5958

test/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function suite (options) {
3636
require('./iterator-test').all(test, testCommon)
3737
require('./iterator-range-test').all(test, testCommon)
3838
require('./async-iterator-test').all(test, testCommon)
39+
require('./iterator-seek-test').all(test, testCommon)
3940

4041
require('./deferred-open-test').all(test, testCommon)
4142
require('./encoding-test').all(test, testCommon)
@@ -44,10 +45,6 @@ function suite (options) {
4445
require('./encoding-buffer-test').all(test, testCommon)
4546
require('./encoding-decode-error-test').all(test, testCommon)
4647

47-
if (testCommon.supports.seek) {
48-
require('./iterator-seek-test').all(test, testCommon)
49-
}
50-
5148
if (testCommon.supports.implicitSnapshots) {
5249
require('./iterator-snapshot-test').all(test, testCommon)
5350
} else {

test/self/sublevel-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ test('sublevel manifest and parent db', function (t) {
251251
t.test('sublevel inherits manifest from parent db', function (t) {
252252
const parent = new AbstractLevel({
253253
encodings: { utf8: true },
254-
seek: true,
254+
explicitSnapshots: true,
255255
foo: true
256256
})
257257
const sub = parent.sublevel('')
258258
t.is(sub.supports.foo, true, 'AbstractSublevel inherits from parent')
259-
t.is(sub.supports.seek, true, 'AbstractSublevel inherits from parent')
259+
t.is(sub.supports.explicitSnapshots, true, 'AbstractSublevel inherits from parent')
260260
t.end()
261261
})
262262

0 commit comments

Comments
 (0)