-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See Level/community#118. Depends on Level/supports#32. Category: addition
- Loading branch information
Showing
9 changed files
with
215 additions
and
10 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
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
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,89 @@ | ||
'use strict' | ||
|
||
const ModuleError = require('module-error') | ||
const { noop } = require('./lib/common') | ||
|
||
const kStatus = Symbol('status') | ||
const kReferenceCount = Symbol('referenceCount') | ||
const kPendingClose = Symbol('pendingClose') | ||
const kClosePromise = Symbol('closePromise') | ||
const kRootDatabase = Symbol('rootDatabase') | ||
|
||
const STATUS_OPEN = 1 | ||
const STATUS_CLOSING = 2 | ||
|
||
class AbstractSnapshot { | ||
constructor (db) { | ||
if (typeof db !== 'object' || db === null) { | ||
const hint = db === null ? 'null' : typeof db | ||
throw new TypeError(`The first argument must be an abstract-level database, received ${hint}`) | ||
} | ||
|
||
// Get root database in case this is a sublevel | ||
// TODO: add db property to AbstractLevel too | ||
if (db.parent) { | ||
db = db.db | ||
} | ||
|
||
this[kStatus] = STATUS_OPEN | ||
this[kReferenceCount] = 0 | ||
this[kPendingClose] = null | ||
this[kClosePromise] = null | ||
this[kRootDatabase] = db | ||
|
||
db.attachResource(this) | ||
} | ||
|
||
get db () { | ||
return this[kRootDatabase] | ||
} | ||
|
||
ref () { | ||
if (this[kStatus] !== STATUS_OPEN) { | ||
throw new ModuleError('Snapshot is not open: cannot use snapshot after close()', { | ||
code: 'LEVEL_SNAPSHOT_NOT_OPEN' | ||
}) | ||
} | ||
|
||
this[kReferenceCount]++ | ||
} | ||
|
||
unref () { | ||
if (--this[kReferenceCount] === 0 && this[kPendingClose] !== null) { | ||
this[kPendingClose]() | ||
} | ||
} | ||
|
||
async close () { | ||
if (this[kClosePromise] !== null) { | ||
// First caller of close() is responsible for error | ||
return this[kClosePromise].catch(noop) | ||
} | ||
|
||
// Wrap to avoid race issues on recursive calls | ||
this[kClosePromise] = new Promise((resolve, reject) => { | ||
this[kPendingClose] = () => { | ||
this[kPendingClose] = null | ||
privateClose(this).then(resolve, reject) | ||
} | ||
}) | ||
|
||
// If working we'll delay closing, but still handle the close error (if any) here | ||
if (this[kReferenceCount] === 0) { | ||
this[kPendingClose]() | ||
} | ||
|
||
return this[kClosePromise] | ||
} | ||
|
||
async _close () {} | ||
} | ||
|
||
const privateClose = async function (snapshot) { | ||
// There's no need for a closed status atm (after _close) | ||
snapshot[kStatus] = STATUS_CLOSING | ||
await snapshot._close() | ||
snapshot[kRootDatabase].detachResource(snapshot) | ||
} | ||
|
||
exports.AbstractSnapshot = AbstractSnapshot |
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
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
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,30 @@ | ||
/** | ||
* A lightweight token that represents a version of a database at a particular point in | ||
* time. | ||
* | ||
* @template TDatabase Type of the database that created this snapshot. | ||
*/ | ||
export class AbstractSnapshot<TDatabase> { | ||
/** | ||
* A reference to the database that created this snapshot. | ||
*/ | ||
db: TDatabase | ||
|
||
/** | ||
* Increment reference count, to register work that should delay closing until | ||
* {@link unref} is called an equal amount of times. The promise that will be returned | ||
* by {@link close} will not resolve until the reference count returns to 0. This | ||
* prevents prematurely closing underlying resources while the snapshot is in use. | ||
*/ | ||
ref (): void | ||
|
||
/** | ||
* Decrement reference count, to indicate that the work has finished. | ||
*/ | ||
unref (): void | ||
|
||
/** | ||
* Close the snapshot. | ||
*/ | ||
close (): Promise<void> | ||
} |