This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking: only defer operations while db is opening
In other states (besides 'open') a 'Database is not open' error is now thrown. This reduces the scope of `deferred-leveldown` to what's strictly needed in `levelup` and aligns behavior. In addition, override public methods of `abstract-leveldown` instead of private methods. This has one downside: they need to do the same callback to promise conversion that `abstract-leveldown` does. The upside is that operation callbacks are not called before the db has finished opening, including in cases where `abstract-leveldown` has a fast-path, like on `db.batch([])` which because the array is empty bypasses `_batch()`. Closes #91 Closes #90
- Loading branch information
Showing
6 changed files
with
659 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
const { AbstractChainedBatch } = require('abstract-leveldown') | ||
const kOperations = Symbol('operations') | ||
|
||
module.exports = class DeferredChainedBatch extends AbstractChainedBatch { | ||
constructor (db) { | ||
super(db) | ||
this[kOperations] = [] | ||
} | ||
|
||
_put (key, value, options) { | ||
this[kOperations].push({ ...options, type: 'put', key, value }) | ||
} | ||
|
||
_del (key, options) { | ||
this[kOperations].push({ ...options, type: 'del', key }) | ||
} | ||
|
||
_clear () { | ||
this[kOperations] = [] | ||
} | ||
|
||
_write (options, callback) { | ||
// AbstractChainedBatch would call _batch(), we call batch() | ||
this.db.batch(this[kOperations], options, callback) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,85 @@ | ||
'use strict' | ||
|
||
const AbstractIterator = require('abstract-leveldown').AbstractIterator | ||
const { AbstractIterator } = require('abstract-leveldown') | ||
const inherits = require('inherits') | ||
const getCallback = require('./util').getCallback | ||
|
||
const kOptions = Symbol('options') | ||
const kIterator = Symbol('iterator') | ||
const kOperations = Symbol('operations') | ||
const kPromise = Symbol('promise') | ||
|
||
function DeferredIterator (db, options) { | ||
AbstractIterator.call(this, db) | ||
|
||
this._options = options | ||
this._iterator = null | ||
this._operations = [] | ||
this[kOptions] = options | ||
this[kIterator] = null | ||
this[kOperations] = [] | ||
} | ||
|
||
inherits(DeferredIterator, AbstractIterator) | ||
|
||
DeferredIterator.prototype.setDb = function (db) { | ||
const it = this._iterator = db.iterator(this._options) | ||
this[kIterator] = db.iterator(this[kOptions]) | ||
|
||
for (const op of this._operations) { | ||
it[op.method](...op.args) | ||
for (const op of this[kOperations].splice(0, this[kOperations].length)) { | ||
this[kIterator][op.method](...op.args) | ||
} | ||
} | ||
|
||
DeferredIterator.prototype._operation = function (method, args) { | ||
if (this._iterator) return this._iterator[method](...args) | ||
this._operations.push({ method, args }) | ||
} | ||
DeferredIterator.prototype.next = function (...args) { | ||
if (this.db.status === 'open') { | ||
return this[kIterator].next(...args) | ||
} | ||
|
||
const callback = getCallback(args, kPromise, function map (key, value) { | ||
if (key === undefined && value === undefined) { | ||
return undefined | ||
} else { | ||
return [key, value] | ||
} | ||
}) | ||
|
||
for (const m of ['next', 'end']) { | ||
DeferredIterator.prototype['_' + m] = function (...args) { | ||
this._operation(m, args) | ||
if (this.db.status === 'opening') { | ||
this[kOperations].push({ method: 'next', args }) | ||
} else { | ||
this._nextTick(callback, new Error('Database is not open')) | ||
} | ||
|
||
return callback[kPromise] || this | ||
} | ||
|
||
// Must defer seek() rather than _seek() because it requires db._serializeKey to be available | ||
DeferredIterator.prototype.seek = function (...args) { | ||
this._operation('seek', args) | ||
if (this.db.status === 'open') { | ||
this[kIterator].seek(...args) | ||
} else if (this.db.status === 'opening') { | ||
this[kOperations].push({ method: 'seek', args }) | ||
} else { | ||
throw new Error('Database is not open') | ||
} | ||
} | ||
|
||
DeferredIterator.prototype.end = function (...args) { | ||
if (this.db.status === 'open') { | ||
return this[kIterator].end(...args) | ||
} | ||
|
||
const callback = getCallback(args, kPromise) | ||
|
||
if (this.db.status === 'opening') { | ||
this[kOperations].push({ method: 'end', args }) | ||
} else { | ||
this._nextTick(callback, new Error('Database is not open')) | ||
} | ||
|
||
return callback[kPromise] || this | ||
} | ||
|
||
for (const method of ['next', 'seek', 'end']) { | ||
DeferredIterator.prototype['_' + method] = function () { | ||
/* istanbul ignore next: assertion */ | ||
throw new Error('Did not expect private method to be called: ' + method) | ||
} | ||
} | ||
|
||
module.exports = DeferredIterator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.