Skip to content

Commit

Permalink
Make use of nextv(), keys() and values() (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Jan 30, 2022
1 parent b5d5e56 commit 7c4fc44
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12, 14]
node: [12, 14, 16]
name: Node ${{ matrix.node }}
steps:
- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -81,11 +81,11 @@ Upon stream end or having called `stream.destroy()` the underlying iterator will

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

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.
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.

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

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.
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.

### `stream`

Expand Down
64 changes: 30 additions & 34 deletions index.js
Expand Up @@ -3,19 +3,19 @@
const { Readable } = require('readable-stream')

const kIterator = Symbol('iterator')
const kNext = Symbol('next')
const kNextv = Symbol('nextv')

class LevelReadStream extends Readable {
constructor (db, options) {
const { highWaterMark, ...rest } = options
constructor (db, method, options) {
const { highWaterMark, ...rest } = options || {}

super({
objectMode: true,
highWaterMark: highWaterMark || 16
highWaterMark: highWaterMark || 1000
})

this[kIterator] = db.iterator(rest)
this[kNext] = this[kNext].bind(this)
this[kIterator] = db[method](rest)
this[kNextv] = this[kNextv].bind(this)

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

_read () {
_read (size) {
if (this.destroyed) return
this[kIterator].next(this[kNext])
this[kIterator].nextv(size, this[kNextv])
}

[kNextv] (err, items) {
if (this.destroyed) return
if (err) return this.destroy(err)

if (items.length === 0) {
this.push(null)
} else {
for (const item of items) {
this.push(item)
}
}
}

_destroy (err, callback) {
this[kIterator].end(function (err2) {
this[kIterator].close(function (err2) {
callback(err || err2)
})
}
}

class Entry {
constructor (key, value) {
this.key = key
this.value = value
}
}

class EntryStream extends LevelReadStream {
constructor (db, options) {
super(db, { ...options, keys: true, values: true })
super(db, 'iterator', { ...options, keys: true, values: true })
}

[kNext] (err, key, value) {
[kNextv] (err, entries) {
if (this.destroyed) return
if (err) return this.destroy(err)

if (key === undefined && value === undefined) {
if (entries.length === 0) {
this.push(null)
} else {
this.push(new Entry(key, value))
for (const [key, value] of entries) {
this.push({ key, value })
}
}
}
}

class KeyStream extends LevelReadStream {
constructor (db, options) {
super(db, { ...options, keys: true, values: false })
}

[kNext] (err, key) {
if (this.destroyed) return
if (err) return this.destroy(err)
this.push(key === undefined ? null : key)
super(db, 'keys', options)
}
}

class ValueStream extends LevelReadStream {
constructor (db, options) {
super(db, { ...options, keys: false, values: true })
}

[kNext] (err, _, value) {
if (this.destroyed) return
if (err) return this.destroy(err)
this.push(value === undefined ? null : value)
super(db, 'values', options)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"airtap-playwright": "^1.0.1",
"faucet": "^0.0.1",
"hallmark": "^4.0.0",
"memdown": "^6.0.0",
"memory-level": "^1.0.0",
"nyc": "^15.1.0",
"secret-event-listener": "^1.0.0",
"standard": "^16.0.3",
Expand Down

0 comments on commit 7c4fc44

Please sign in to comment.