Skip to content

Commit 7c4fc44

Browse files
authored
Make use of nextv(), keys() and values() (#2)
Ref Level/community#70 Closes #1
1 parent b5d5e56 commit 7c4fc44

File tree

5 files changed

+84
-113
lines changed

5 files changed

+84
-113
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
node: [10, 12, 14]
10+
node: [12, 14, 16]
1111
name: Node ${{ matrix.node }}
1212
steps:
1313
- name: Checkout

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ Upon stream end or having called `stream.destroy()` the underlying iterator will
8181

8282
### `stream = new KeyStream(db[, options])`
8383

84-
Same as `EntryStream` but yields keys instead of entries. If only keys are needed, using `KeyStream` may increase performance because values won't have to be fetched.
84+
Same as `EntryStream` but yields keys instead of entries, using `db.keys()` instead of `db.iterator()`. If only keys are needed, using `KeyStream` may increase performance because values won't have to be fetched.
8585

8686
### `stream = new ValueStream(db[, options])`
8787

88-
Same as `EntryStream` but yields values instead of entries. If only values are needed, using `ValueStream` may increase performance because keys won't have to be fetched.
88+
Same as `EntryStream` but yields values instead of entries, using `db.values()` instead of `db.iterator()`. If only values are needed, using `ValueStream` may increase performance because keys won't have to be fetched.
8989

9090
### `stream`
9191

index.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
const { Readable } = require('readable-stream')
44

55
const kIterator = Symbol('iterator')
6-
const kNext = Symbol('next')
6+
const kNextv = Symbol('nextv')
77

88
class LevelReadStream extends Readable {
9-
constructor (db, options) {
10-
const { highWaterMark, ...rest } = options
9+
constructor (db, method, options) {
10+
const { highWaterMark, ...rest } = options || {}
1111

1212
super({
1313
objectMode: true,
14-
highWaterMark: highWaterMark || 16
14+
highWaterMark: highWaterMark || 1000
1515
})
1616

17-
this[kIterator] = db.iterator(rest)
18-
this[kNext] = this[kNext].bind(this)
17+
this[kIterator] = db[method](rest)
18+
this[kNextv] = this[kNextv].bind(this)
1919

2020
// NOTE: use autoDestroy option when it lands in readable-stream
2121
this.once('end', this.destroy.bind(this, null, null))
@@ -25,63 +25,59 @@ class LevelReadStream extends Readable {
2525
return this[kIterator].db
2626
}
2727

28-
_read () {
28+
_read (size) {
2929
if (this.destroyed) return
30-
this[kIterator].next(this[kNext])
30+
this[kIterator].nextv(size, this[kNextv])
31+
}
32+
33+
[kNextv] (err, items) {
34+
if (this.destroyed) return
35+
if (err) return this.destroy(err)
36+
37+
if (items.length === 0) {
38+
this.push(null)
39+
} else {
40+
for (const item of items) {
41+
this.push(item)
42+
}
43+
}
3144
}
3245

3346
_destroy (err, callback) {
34-
this[kIterator].end(function (err2) {
47+
this[kIterator].close(function (err2) {
3548
callback(err || err2)
3649
})
3750
}
3851
}
3952

40-
class Entry {
41-
constructor (key, value) {
42-
this.key = key
43-
this.value = value
44-
}
45-
}
46-
4753
class EntryStream extends LevelReadStream {
4854
constructor (db, options) {
49-
super(db, { ...options, keys: true, values: true })
55+
super(db, 'iterator', { ...options, keys: true, values: true })
5056
}
5157

52-
[kNext] (err, key, value) {
58+
[kNextv] (err, entries) {
5359
if (this.destroyed) return
5460
if (err) return this.destroy(err)
5561

56-
if (key === undefined && value === undefined) {
62+
if (entries.length === 0) {
5763
this.push(null)
5864
} else {
59-
this.push(new Entry(key, value))
65+
for (const [key, value] of entries) {
66+
this.push({ key, value })
67+
}
6068
}
6169
}
6270
}
6371

6472
class KeyStream extends LevelReadStream {
6573
constructor (db, options) {
66-
super(db, { ...options, keys: true, values: false })
67-
}
68-
69-
[kNext] (err, key) {
70-
if (this.destroyed) return
71-
if (err) return this.destroy(err)
72-
this.push(key === undefined ? null : key)
74+
super(db, 'keys', options)
7375
}
7476
}
7577

7678
class ValueStream extends LevelReadStream {
7779
constructor (db, options) {
78-
super(db, { ...options, keys: false, values: true })
79-
}
80-
81-
[kNext] (err, _, value) {
82-
if (this.destroyed) return
83-
if (err) return this.destroy(err)
84-
this.push(value === undefined ? null : value)
80+
super(db, 'values', options)
8581
}
8682
}
8783

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"airtap-playwright": "^1.0.1",
2323
"faucet": "^0.0.1",
2424
"hallmark": "^4.0.0",
25-
"memdown": "^6.0.0",
25+
"memory-level": "^1.0.0",
2626
"nyc": "^15.1.0",
2727
"secret-event-listener": "^1.0.0",
2828
"standard": "^16.0.3",

0 commit comments

Comments
 (0)