Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
deps: update interface-store to 5.x.x (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Mar 23, 2023
1 parent ba79dca commit 2820b0d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 80 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"dependencies": {
"@libp2p/logger": "^2.0.0",
"err-code": "^3.0.1",
"interface-store": "^4.0.0",
"interface-store": "^5.0.1",
"it-all": "^2.0.0",
"it-drain": "^2.0.0",
"it-filter": "^2.0.0",
Expand All @@ -210,7 +210,7 @@
},
"devDependencies": {
"aegir": "^38.1.7",
"interface-datastore": "^8.0.0",
"interface-datastore-tests": "^4.0.0"
"interface-datastore": "^8.1.2",
"interface-datastore-tests": "^5.0.0"
}
}
49 changes: 22 additions & 27 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,43 @@ import sort from 'it-sort'
import drain from 'it-drain'
import filter from 'it-filter'
import take from 'it-take'
import type { Batch, Datastore, Key, KeyQuery, Options, Pair, Query } from 'interface-datastore'
import type { AwaitIterable } from 'interface-store'
import type { Batch, Datastore, Key, KeyQuery, Pair, Query } from 'interface-datastore'
import type { AbortOptions, Await, AwaitIterable } from 'interface-store'

export class BaseDatastore implements Datastore {
async open (): Promise<void> {

}

async close (): Promise<void> {

put (key: Key, val: Uint8Array, options?: AbortOptions): Await<Key> {
return Promise.reject(new Error('.put is not implemented'))
}

async put (key: Key, val: Uint8Array, options?: Options): Promise<void> {
await Promise.reject(new Error('.put is not implemented'))
get (key: Key, options?: AbortOptions): Await<Uint8Array> {
return Promise.reject(new Error('.get is not implemented'))
}

async get (key: Key, options?: Options): Promise<Uint8Array> {
return await Promise.reject(new Error('.get is not implemented'))
has (key: Key, options?: AbortOptions): Await<boolean> {
return Promise.reject(new Error('.has is not implemented'))
}

async has (key: Key, options?: Options): Promise<boolean> {
return await Promise.reject(new Error('.has is not implemented'))
delete (key: Key, options?: AbortOptions): Await<void> {
return Promise.reject(new Error('.delete is not implemented'))
}

async delete (key: Key, options?: Options): Promise<void> {
await Promise.reject(new Error('.delete is not implemented'))
}

async * putMany (source: AwaitIterable<Pair>, options: Options = {}): AsyncIterable<Pair> {
async * putMany (source: AwaitIterable<Pair>, options: AbortOptions = {}): AwaitIterable<Key> {
for await (const { key, value } of source) {
await this.put(key, value, options)
yield { key, value }
yield key
}
}

async * getMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Uint8Array> {
async * getMany (source: AwaitIterable<Key>, options: AbortOptions = {}): AwaitIterable<Pair> {
for await (const key of source) {
yield this.get(key, options)
yield {
key,
value: await this.get(key, options)
}
}
}

async * deleteMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Key> {
async * deleteMany (source: AwaitIterable<Key>, options: AbortOptions = {}): AwaitIterable<Key> {
for await (const key of source) {
await this.delete(key, options)
yield key
Expand Down Expand Up @@ -75,19 +70,19 @@ export class BaseDatastore implements Datastore {
* Extending classes should override `query` or implement this method
*/
// eslint-disable-next-line require-yield
async * _all (q: Query, options?: Options): AsyncIterable<Pair> {
async * _all (q: Query, options?: AbortOptions): AwaitIterable<Pair> {
throw new Error('._all is not implemented')
}

/**
* Extending classes should override `queryKeys` or implement this method
*/
// eslint-disable-next-line require-yield
async * _allKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
async * _allKeys (q: KeyQuery, options?: AbortOptions): AwaitIterable<Key> {
throw new Error('._allKeys is not implemented')
}

query (q: Query, options?: Options): AsyncIterable<Pair> {
query (q: Query, options?: AbortOptions): AwaitIterable<Pair> {
let it = this._all(q, options)

if (q.prefix != null) {
Expand Down Expand Up @@ -116,7 +111,7 @@ export class BaseDatastore implements Datastore {
return it
}

queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
queryKeys (q: KeyQuery, options?: AbortOptions): AwaitIterable<Key> {
let it = this._allKeys(q, options)

if (q.prefix != null) {
Expand Down
35 changes: 20 additions & 15 deletions src/keytransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { BaseDatastore } from './base.js'
import map from 'it-map'
import { pipe } from 'it-pipe'
import type { KeyTransform } from './index.js'
import type { Batch, Datastore, Key, KeyQuery, Options, Pair, Query } from 'interface-datastore'
import type { AwaitIterable } from 'interface-store'
import type { Batch, Datastore, Key, KeyQuery, Pair, Query } from 'interface-datastore'
import type { AbortOptions, AwaitIterable } from 'interface-store'

/**
* A datastore shim, that wraps around a given datastore, changing
Expand All @@ -21,23 +21,25 @@ export class KeyTransformDatastore extends BaseDatastore {
this.transform = transform
}

async put (key: Key, val: Uint8Array, options?: Options): Promise<void> {
async put (key: Key, val: Uint8Array, options?: AbortOptions): Promise<Key> {
await this.child.put(this.transform.convert(key), val, options)

return key
}

async get (key: Key, options?: Options): Promise<Uint8Array> {
async get (key: Key, options?: AbortOptions): Promise<Uint8Array> {
return await this.child.get(this.transform.convert(key), options)
}

async has (key: Key, options?: Options): Promise<boolean> {
async has (key: Key, options?: AbortOptions): Promise<boolean> {
return await this.child.has(this.transform.convert(key), options)
}

async delete (key: Key, options?: Options): Promise<void> {
async delete (key: Key, options?: AbortOptions): Promise<void> {
await this.child.delete(this.transform.convert(key), options)
}

async * putMany (source: AwaitIterable<Pair>, options: Options = {}): AsyncIterable<Pair> {
async * putMany (source: AwaitIterable<Pair>, options: AbortOptions = {}): AsyncIterable<Key> {
const transform = this.transform
const child = this.child

Expand All @@ -53,15 +55,12 @@ export class KeyTransformDatastore extends BaseDatastore {
yield * child.putMany(source, options)
},
async function * (source) {
yield * map(source, ({ key, value }) => ({
key: transform.invert(key),
value
}))
yield * map(source, key => transform.invert(key))
}
)
}

async * getMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Uint8Array> {
async * getMany (source: AwaitIterable<Key>, options: AbortOptions = {}): AsyncIterable<Pair> {
const transform = this.transform
const child = this.child

Expand All @@ -72,11 +71,17 @@ export class KeyTransformDatastore extends BaseDatastore {
},
async function * (source) {
yield * child.getMany(source, options)
},
async function * (source) {
yield * map(source, ({ key, value }) => ({
key: transform.invert(key),
value
}))
}
)
}

async * deleteMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Key> {
async * deleteMany (source: AwaitIterable<Key>, options: AbortOptions = {}): AsyncIterable<Key> {
const transform = this.transform
const child = this.child

Expand Down Expand Up @@ -109,7 +114,7 @@ export class KeyTransformDatastore extends BaseDatastore {
}
}

query (q: Query, options?: Options): AsyncIterable<Pair> {
query (q: Query, options?: AbortOptions): AsyncIterable<Pair> {
const query: Query = {
...q
}
Expand Down Expand Up @@ -143,7 +148,7 @@ export class KeyTransformDatastore extends BaseDatastore {
})
}

queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
queryKeys (q: KeyQuery, options?: AbortOptions): AsyncIterable<Key> {
const query = {
...q
}
Expand Down
15 changes: 9 additions & 6 deletions src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseDatastore } from './base.js'
import { Key } from 'interface-datastore/key'
import * as Errors from './errors.js'
import type { Pair } from 'interface-datastore'
import type { Await, AwaitIterable } from 'interface-store'

export class MemoryDatastore extends BaseDatastore {
private readonly data: Map<string, Uint8Array>
Expand All @@ -12,11 +13,13 @@ export class MemoryDatastore extends BaseDatastore {
this.data = new Map()
}

async put (key: Key, val: Uint8Array): Promise<void> { // eslint-disable-line require-await
put (key: Key, val: Uint8Array): Await<Key> { // eslint-disable-line require-await
this.data.set(key.toString(), val)

return key
}

async get (key: Key): Promise<Uint8Array> {
get (key: Key): Await<Uint8Array> {
const result = this.data.get(key.toString())

if (result == null) {
Expand All @@ -26,21 +29,21 @@ export class MemoryDatastore extends BaseDatastore {
return result
}

async has (key: Key): Promise<boolean> { // eslint-disable-line require-await
has (key: Key): Await<boolean> { // eslint-disable-line require-await
return this.data.has(key.toString())
}

async delete (key: Key): Promise<void> { // eslint-disable-line require-await
delete (key: Key): Await<void> { // eslint-disable-line require-await
this.data.delete(key.toString())
}

async * _all (): AsyncIterable<Pair> {
* _all (): AwaitIterable<Pair> {
for (const [key, value] of this.data.entries()) {
yield { key: new Key(key), value }
}
}

async * _allKeys (): AsyncIterable<Key> {
* _allKeys (): AwaitIterable<Key> {
for (const key of this.data.keys()) {
yield new Key(key)
}
Expand Down
16 changes: 9 additions & 7 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BaseDatastore } from './base.js'
import * as Errors from './errors.js'
import sort from 'it-sort'
import type { Batch, Datastore, Key, KeyQuery, Pair, Query } from 'interface-datastore'
import type { Options } from 'interface-store'
import type { AbortOptions } from 'interface-store'

/**
* A datastore that can combine multiple stores inside various
Expand Down Expand Up @@ -34,36 +34,38 @@ export class MountDatastore extends BaseDatastore {
}
}

async put (key: Key, value: Uint8Array, options?: Options): Promise<void> {
async put (key: Key, value: Uint8Array, options?: AbortOptions): Promise<Key> {
const match = this._lookup(key)
if (match == null) {
throw Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
}

await match.datastore.put(key, value, options)

return key
}

/**
* @param {Key} key
* @param {Options} [options]
*/
async get (key: Key, options: Options = {}): Promise<Uint8Array> {
async get (key: Key, options: AbortOptions = {}): Promise<Uint8Array> {
const match = this._lookup(key)
if (match == null) {
throw Errors.notFoundError(new Error('No datastore mounted for this key'))
}
return await match.datastore.get(key, options)
}

async has (key: Key, options?: Options): Promise<boolean> {
async has (key: Key, options?: AbortOptions): Promise<boolean> {
const match = this._lookup(key)
if (match == null) {
return await Promise.resolve(false)
}
return await match.datastore.has(key, options)
}

async delete (key: Key, options?: Options): Promise<void> {
async delete (key: Key, options?: AbortOptions): Promise<void> {
const match = this._lookup(key)
if (match == null) {
throw Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
Expand Down Expand Up @@ -106,7 +108,7 @@ export class MountDatastore extends BaseDatastore {
}
}

query (q: Query, options?: Options): AsyncIterable<Pair> {
query (q: Query, options?: AbortOptions): AsyncIterable<Pair> {
const qs = this.mounts.map(m => {
return m.datastore.query({
prefix: q.prefix,
Expand All @@ -127,7 +129,7 @@ export class MountDatastore extends BaseDatastore {
return it
}

queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
queryKeys (q: KeyQuery, options?: AbortOptions): AsyncIterable<Key> {
const qs = this.mounts.map(m => {
return m.datastore.queryKeys({
prefix: q.prefix,
Expand Down
Loading

0 comments on commit 2820b0d

Please sign in to comment.