Skip to content

feat: use Level getSync when possible #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "1.0.0",
"description": "Blockstores and datastores used by IP-JS internals",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/ipfs/js-stores#readme",
"homepage": "https://github.com/dao-xyz/js-stores#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-stores.git"
"url": "git+https://github.com//dao-xyz/js-stores.git"
},
"bugs": {
"url": "https://github.com/ipfs/js-stores/issues"
"url": "https://github.com//dao-xyz/js-stores/issues"
},
"private": true,
"scripts": {
Expand Down
18 changes: 8 additions & 10 deletions packages/datastore-level/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
{
"name": "datastore-level",
"version": "11.0.1",
"name": "@dao-xyz/datastore-level",
"version": "11.1.0",
"description": "Datastore implementation with level(up|down) backend",
"author": "Friedel Ziegelmayer<[email protected]>",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/ipfs/js-stores/tree/main/packages/datastore-level#readme",
"homepage": "https://github.com/dao-xyz/js-stores/tree/main/packages/datastore-level#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-stores.git"
"url": "git+https://github.com/dao-xyz/js-stores.git"
},
"bugs": {
"url": "https://github.com/ipfs/js-stores/issues"
"url": "https://github.com/dao-xyz/js-stores/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
"access": "public"
},
"keywords": [
"datastore",
Expand Down Expand Up @@ -153,12 +151,12 @@
"it-map": "^3.1.1",
"it-sort": "^3.0.6",
"it-take": "^3.0.6",
"level": "^8.0.1"
"level": "^10.0.0"
},
"devDependencies": {
"aegir": "^44.1.1",
"interface-datastore-tests": "^6.0.0",
"ipfs-utils": "^9.0.14",
"memory-level": "^1.0.0"
"memory-level": "^3.1.0"
}
}
44 changes: 23 additions & 21 deletions packages/datastore-level/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* More information: [https://github.com/Level/level-js/blob/master/UPGRADING.md#new-database-prefix](https://github.com/Level/level-js/blob/99831913e905d19e5f6dee56d512b7264fbed7bd/UPGRADING.md#new-database-prefix)
*/


import { BaseDatastore } from 'datastore-core'
import { type Batch, Key, type KeyQuery, type Pair, type Query } from 'interface-datastore'
import { DeleteFailedError, GetFailedError, NotFoundError, OpenFailedError, PutFailedError } from 'interface-store'
Expand All @@ -54,6 +55,20 @@ import take from 'it-take'
import { Level } from 'level'
import type { DatabaseOptions, OpenOptions, IteratorOptions } from 'level'

type GetFn = (key: string)=> (Uint8Array | undefined | Promise<Uint8Array | undefined > )
const getOrGetSync = (level: Level<string, Uint8Array>): GetFn=> {
const canGetSync = (level.supports as any)["getSync"] !== false;
if (!canGetSync) {
return ( key: string): Promise<Uint8Array | undefined> => {
return level.get(key)
}
} else {
return ( key: string): Uint8Array | undefined => {
return level.getSync(key)
}
}
}

interface BatchPut {
type: 'put'
key: string
Expand All @@ -73,7 +88,7 @@ type BatchOp = BatchPut | BatchDel
export class LevelDatastore extends BaseDatastore {
public db: Level<string, Uint8Array>
private readonly opts: OpenOptions

private readonly getFn: GetFn
constructor (path: string | Level<string, Uint8Array>, opts: DatabaseOptions<string, Uint8Array> & OpenOptions = {}) {
super()

Expand All @@ -90,6 +105,7 @@ export class LevelDatastore extends BaseDatastore {
compression: false, // same default as go
...opts
}
this.getFn = getOrGetSync(this.db)
}

async open (): Promise<void> {
Expand All @@ -110,31 +126,17 @@ export class LevelDatastore extends BaseDatastore {
}
}

async get (key: Key): Promise<Uint8Array> {
let data
try {
data = await this.db.get(key.toString())
} catch (err: any) {
if (err.notFound != null) {
throw new NotFoundError(String(err))
}

throw new GetFailedError(String(err))
async get (key: Key): Promise<Uint8Array> {
let data = await this.getFn(key.toString())
if(data === undefined)
{
throw new NotFoundError("Data with key " + key.toString() + " not found")
}
return data
}

async has (key: Key): Promise<boolean> {
try {
await this.db.get(key.toString())
} catch (err: any) {
if (err.notFound != null) {
return false
}

throw err
}
return true
return (await this.getFn(key.toString())) !== undefined;
}

async delete (key: Key): Promise<void> {
Expand Down