Skip to content

Commit

Permalink
Breaking: remove callbacks and LEVEL_NOT_FOUND
Browse files Browse the repository at this point in the history
Category: change
  • Loading branch information
vweevers committed Oct 20, 2024
1 parent 4b02bde commit d5bad80
Show file tree
Hide file tree
Showing 30 changed files with 949 additions and 1,092 deletions.
425 changes: 223 additions & 202 deletions binding.cc

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions chained-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ class ChainedBatch extends AbstractChainedBatch {
binding.batch_clear(this[kContext])
}

_write (options, callback) {
binding.batch_write(this[kContext], options, callback)
async _write (options) {
return binding.batch_write(this[kContext], options)
}

_close (callback) {
async _close () {
// TODO: close native batch (currently done on GC)
process.nextTick(callback)
}
}

Expand Down
23 changes: 1 addition & 22 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
AbstractKeyIteratorOptions,
AbstractValueIterator,
AbstractValueIteratorOptions,
Transcoder,
NodeCallback
Transcoder
} from 'abstract-level'

/**
Expand Down Expand Up @@ -48,33 +47,21 @@ declare class ClassicLevel<KDefault = string, VDefault = string>

open (): Promise<void>
open (options: OpenOptions): Promise<void>
open (callback: NodeCallback<void>): void
open (options: OpenOptions, callback: NodeCallback<void>): void

get (key: KDefault): Promise<VDefault>
get (key: KDefault, callback: NodeCallback<VDefault>): void
get<K = KDefault, V = VDefault> (key: K, options: GetOptions<K, V>): Promise<V>
get<K = KDefault, V = VDefault> (key: K, options: GetOptions<K, V>, callback: NodeCallback<V>): void

getMany (keys: KDefault[]): Promise<VDefault[]>
getMany (keys: KDefault[], callback: NodeCallback<VDefault[]>): void
getMany<K = KDefault, V = VDefault> (keys: K[], options: GetManyOptions<K, V>): Promise<V[]>
getMany<K = KDefault, V = VDefault> (keys: K[], options: GetManyOptions<K, V>, callback: NodeCallback<V[]>): void

put (key: KDefault, value: VDefault): Promise<void>
put (key: KDefault, value: VDefault, callback: NodeCallback<void>): void
put<K = KDefault, V = VDefault> (key: K, value: V, options: PutOptions<K, V>): Promise<void>
put<K = KDefault, V = VDefault> (key: K, value: V, options: PutOptions<K, V>, callback: NodeCallback<void>): void

del (key: KDefault): Promise<void>
del (key: KDefault, callback: NodeCallback<void>): void
del<K = KDefault> (key: K, options: DelOptions<K>): Promise<void>
del<K = KDefault> (key: K, options: DelOptions<K>, callback: NodeCallback<void>): void

batch (operations: Array<BatchOperation<typeof this, KDefault, VDefault>>): Promise<void>
batch (operations: Array<BatchOperation<typeof this, KDefault, VDefault>>, callback: NodeCallback<void>): void
batch<K = KDefault, V = VDefault> (operations: Array<BatchOperation<typeof this, K, V>>, options: BatchOptions<K, V>): Promise<void>
batch<K = KDefault, V = VDefault> (operations: Array<BatchOperation<typeof this, K, V>>, options: BatchOptions<K, V>, callback: NodeCallback<void>): void
batch (): ChainedBatch<typeof this, KDefault, VDefault>

iterator (): Iterator<typeof this, KDefault, VDefault>
Expand All @@ -91,17 +78,13 @@ declare class ClassicLevel<KDefault = string, VDefault = string>
* `[start..end)`.
*/
approximateSize (start: KDefault, end: KDefault): Promise<number>
approximateSize (start: KDefault, end: KDefault, callback: NodeCallback<number>): void
approximateSize<K = KDefault> (start: K, end: K, options: StartEndOptions<K>): Promise<number>
approximateSize<K = KDefault> (start: K, end: K, options: StartEndOptions<K>, callback: NodeCallback<number>): void

/**
* Manually trigger a database compaction in the range `[start..end)`.
*/
compactRange (start: KDefault, end: KDefault): Promise<void>
compactRange (start: KDefault, end: KDefault, callback: NodeCallback<void>): void
compactRange<K = KDefault> (start: K, end: K, options: StartEndOptions<K>): Promise<void>
compactRange<K = KDefault> (start: K, end: K, options: StartEndOptions<K>, callback: NodeCallback<void>): void

/**
* Get internal details from LevelDB.
Expand All @@ -113,14 +96,12 @@ declare class ClassicLevel<KDefault = string, VDefault = string>
* place of a full directory removal to only remove LevelDB-related files.
*/
static destroy (location: string): Promise<void>
static destroy (location: string, callback: NodeCallback<void>): void

/**
* Attempt a restoration of a damaged database. Can also be used to perform
* a compaction of the LevelDB log into table files.
*/
static repair (location: string): Promise<void>
static repair (location: string, callback: NodeCallback<void>): void
}

/**
Expand Down Expand Up @@ -311,8 +292,6 @@ export interface ChainedBatchWriteOptions extends AbstractChainedBatchWriteOptio
export class ChainedBatch<TDatabase, KDefault, VDefault> extends AbstractChainedBatch<TDatabase, KDefault, VDefault> {
write (): Promise<void>
write (options: ChainedBatchWriteOptions): Promise<void>
write (callback: NodeCallback<void>): void
write (options: ChainedBatchWriteOptions, callback: NodeCallback<void>): void
}

/**
Expand Down
98 changes: 40 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

const { AbstractLevel } = require('abstract-level')
const ModuleError = require('module-error')
const { fromCallback } = require('catering')
const fs = require('fs')
const fsp = require('fs/promises')
const binding = require('./binding')
const { ChainedBatch } = require('./chained-batch')
const { Iterator } = require('./iterator')

const kPromise = Symbol('promise')
const kContext = Symbol('context')
const kLocation = Symbol('location')

class ClassicLevel extends AbstractLevel {
constructor (location, options, _) {
// To help migrating to abstract-level
// TODO (v2): remove
if (typeof options === 'function' || typeof _ === 'function') {
throw new ModuleError('The levelup-style callback argument has been removed', {
code: 'LEVEL_LEGACY'
Expand Down Expand Up @@ -48,103 +47,88 @@ class ClassicLevel extends AbstractLevel {
return this[kLocation]
}

_open (options, callback) {
async _open (options) {
if (options.createIfMissing) {
fs.mkdir(this[kLocation], { recursive: true }, (err) => {
if (err) return callback(err)
binding.db_open(this[kContext], this[kLocation], options, callback)
})
} else {
binding.db_open(this[kContext], this[kLocation], options, callback)
await fsp.mkdir(this[kLocation], { recursive: true })
}

return binding.db_open(this[kContext], this[kLocation], options)
}

_close (callback) {
binding.db_close(this[kContext], callback)
async _close () {
return binding.db_close(this[kContext])
}

_put (key, value, options, callback) {
binding.db_put(this[kContext], key, value, options, callback)
async _put (key, value, options) {
return binding.db_put(this[kContext], key, value, options)
}

_get (key, options, callback) {
binding.db_get(this[kContext], key, options, callback)
async _get (key, options) {
return binding.db_get(this[kContext], key, options)
}

_getMany (keys, options, callback) {
binding.db_get_many(this[kContext], keys, options, callback)
async _getMany (keys, options) {
return binding.db_get_many(this[kContext], keys, options)
}

_del (key, options, callback) {
binding.db_del(this[kContext], key, options, callback)
async _del (key, options) {
return binding.db_del(this[kContext], key, options)
}

_clear (options, callback) {
binding.db_clear(this[kContext], options, callback)
async _clear (options) {
return binding.db_clear(this[kContext], options)
}

_chainedBatch () {
return new ChainedBatch(this, this[kContext])
}

_batch (operations, options, callback) {
binding.batch_do(this[kContext], operations, options, callback)
async _batch (operations, options) {
return binding.batch_do(this[kContext], operations, options)
}

approximateSize (start, end, options, callback) {
if (arguments.length < 2 || typeof start === 'function' || typeof end === 'function') {
// TODO (v2): update docs
async approximateSize (start, end, options) {
if (arguments.length < 2) {
throw new TypeError("The arguments 'start' and 'end' are required")
} else if (typeof options === 'function') {
callback = options
options = null
} else if (typeof options !== 'object') {
options = null
}

callback = fromCallback(callback, kPromise)

if (this.status === 'opening') {
this.defer(() => this.approximateSize(start, end, options, callback))
return this.deferAsync(() => this.approximateSize(start, end, options))
} else if (this.status !== 'open') {
this.nextTick(callback, new ModuleError('Database is not open: cannot call approximateSize()', {
throw new ModuleError('Database is not open: cannot call approximateSize()', {
code: 'LEVEL_DATABASE_NOT_OPEN'
}))
})
} else {
const keyEncoding = this.keyEncoding(options && options.keyEncoding)
start = keyEncoding.encode(start)
end = keyEncoding.encode(end)
binding.db_approximate_size(this[kContext], start, end, callback)
return binding.db_approximate_size(this[kContext], start, end)
}

return callback[kPromise]
}

compactRange (start, end, options, callback) {
if (arguments.length < 2 || typeof start === 'function' || typeof end === 'function') {
// TODO (v2): update docs
compactRange (start, end, options) {
if (arguments.length < 2) {
throw new TypeError("The arguments 'start' and 'end' are required")
} else if (typeof options === 'function') {
callback = options
options = null
} else if (typeof options !== 'object') {
options = null
}

callback = fromCallback(callback, kPromise)

if (this.status === 'opening') {
this.defer(() => this.compactRange(start, end, options, callback))
return this.deferAsync(() => this.compactRange(start, end, options))
} else if (this.status !== 'open') {
this.nextTick(callback, new ModuleError('Database is not open: cannot call compactRange()', {
throw new ModuleError('Database is not open: cannot call compactRange()', {
code: 'LEVEL_DATABASE_NOT_OPEN'
}))
})
} else {
const keyEncoding = this.keyEncoding(options && options.keyEncoding)
start = keyEncoding.encode(start)
end = keyEncoding.encode(end)
binding.db_compact_range(this[kContext], start, end, callback)
return binding.db_compact_range(this[kContext], start, end)
}

return callback[kPromise]
}

getProperty (property) {
Expand All @@ -166,24 +150,22 @@ class ClassicLevel extends AbstractLevel {
return new Iterator(this, this[kContext], options)
}

static destroy (location, callback) {
// TODO (v2): update docs
static async destroy (location) {
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}

callback = fromCallback(callback, kPromise)
binding.destroy_db(location, callback)
return callback[kPromise]
return binding.destroy_db(location)
}

static repair (location, callback) {
// TODO (v2): update docs
static async repair (location) {
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}

callback = fromCallback(callback, kPromise)
binding.repair_db(location, callback)
return callback[kPromise]
return binding.repair_db(location)
}
}

Expand Down
Loading

0 comments on commit d5bad80

Please sign in to comment.