Skip to content
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

fix: update blocks interface to align with interface-blockstore #54

Merged
merged 1 commit into from
Mar 15, 2023
Merged
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
13 changes: 12 additions & 1 deletion packages/helia/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import filter from 'it-filter'
import type { Blockstore } from 'interface-blockstore'
import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks'
import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents } from '@helia/interface/blocks'
import type { Bitswap } from 'ipfs-bitswap'
import type { CID } from 'multiformats/cid'
import type { AbortOptions } from '@libp2p/interfaces'
Expand Down Expand Up @@ -196,4 +196,15 @@ export class BlockStorage implements Blocks {
releaseLock()
}
}

async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncIterable<Pair> {
const releaseLock = await this.lock.readLock()

try {
options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'))
yield * this.child.getAll(options)
} finally {
releaseLock()
}
}
}
4 changes: 2 additions & 2 deletions packages/helia/test/fixtures/create-block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Blocks } from '@helia/interface/blocks'
import type { Blockstore } from 'interface-blockstore'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'

Expand All @@ -9,7 +9,7 @@ export async function createBlock <Codec extends number> (codec: Codec, block: U
return { cid, block }
}

export async function createAndPutBlock <Codec extends number> (codec: Codec, block: Uint8Array, blockstore: Blocks): Promise<CID<unknown, Codec, 18>> {
export async function createAndPutBlock <Codec extends number> (codec: Codec, block: Uint8Array, blockstore: Blockstore): Promise<CID<unknown, Codec, 18>> {
const result = await createBlock(codec, block)

await blockstore.put(result.cid, block)
Expand Down
6 changes: 3 additions & 3 deletions packages/helia/test/fixtures/create-dag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Blocks } from '@helia/interface/blocks'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { createAndPutBlock } from './create-block.js'
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface DAGNode {
* }
* ```
*/
export async function createDag (codec: number, blocks: Blocks, depth: number, children: number): Promise<Record<string, DAGNode>> {
export async function createDag (codec: number, blocks: Blockstore, depth: number, children: number): Promise<Record<string, DAGNode>> {
const dag: Record<string, DAGNode> = {}
const root = await createAndPutBlock(codec, uint8arrayFromString('level-0'), blocks)

Expand All @@ -67,7 +67,7 @@ export async function createDag (codec: number, blocks: Blocks, depth: number, c
return dag
}

async function addChildren (cid: CID, name: string, level: number, index: number, depth: number, children: number, dag: Record<string, DAGNode>, codec: number, blocks: Blocks): Promise<void> {
async function addChildren (cid: CID, name: string, level: number, index: number, depth: number, children: number, dag: Record<string, DAGNode>, codec: number, blocks: Blockstore): Promise<void> {
if (depth === 0) {
return
}
Expand Down
90 changes: 54 additions & 36 deletions packages/interface/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,70 +33,49 @@ export type GetManyBlocksProgressEvents =
ProgressEvent<'blocks:get-many:blockstore:put', CID> |
BitswapWantProgressEvents

export type GetAllBlocksProgressEvents =
ProgressEvent<'blocks:get-all:blockstore:get-many'>

export type DeleteBlockProgressEvents =
ProgressEvent<'blocks:delete:blockstore:delete', CID>

export type DeleteManyBlocksProgressEvents =
ProgressEvent<'blocks:delete-many:blockstore:delete-many'>

export interface Blocks {
/**
* Store the passed block under the passed CID
*
* @example
*
* ```js
* await store.put([{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }])
* ```
*/
put: (key: CID, val: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>) => Promise<void>

/**
* Retrieve the value stored under the given key
*
* @example
* ```js
* const value = await store.get(new Key('awesome'))
* console.log('got content: %s', value.toString('utf8'))
* // => got content: datastore
* ```
*/
get: (key: CID, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>) => Promise<Uint8Array>

/**
* Check for the existence of a value for the passed key
*
* @example
* ```js
*const exists = await store.has(new Key('awesome'))
* const exists = await store.has(CID('bafyfoo'))
*
*if (exists) {
* console.log('it is there')
*} else {
* console.log('it is not there')
*}
* if (exists) {
* console.log('it is there')
* } else {
* console.log('it is not there')
* }
*```
*/
has: (key: CID, options?: AbortOptions) => Promise<boolean>

/**
* Remove the record for the passed key
* Store the passed block under the passed CID
*
* @example
*
* ```js
* await store.delete(new Key('awesome'))
* console.log('deleted awesome content :(')
* await store.put(CID('bafyfoo'), new Uint8Array([0, 1, 2, 3]))
* ```
*/
delete: (key: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>) => Promise<void>
put: (key: CID, val: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>) => Promise<void>

/**
* Store the given key/value pairs
*
* @example
* ```js
* const source = [{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }]
* const source = [{ cid: CID('bafyfoo'), block: new Uint8Array([0, 1, 2, 3]) }]
*
* for await (const { key, value } of store.putMany(source)) {
* console.info(`put content for key ${key}`)
Expand All @@ -108,12 +87,24 @@ export interface Blocks {
options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>
) => AsyncIterable<Pair>

/**
* Retrieve the value stored under the given key
*
* @example
* ```js
* const value = await store.get(CID('bafyfoo'))
* console.log('got content: %s', value.toString('utf8'))
* // => got content: datastore
* ```
*/
get: (key: CID, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>) => Promise<Uint8Array>

/**
* Retrieve values for the passed keys
*
* @example
* ```js
* for await (const value of store.getMany([new Key('awesome')])) {
* for await (const value of store.getMany([CID('bafyfoo')])) {
* console.log('got content:', new TextDecoder('utf8').decode(value))
* // => got content: datastore
* }
Expand All @@ -124,13 +115,40 @@ export interface Blocks {
options?: AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>
) => AsyncIterable<Uint8Array>

/**
* Retrieve all blocks in the blockstore
*
* @example
* ```js
* for await (const value of store.getAll()) {
* console.log('got content:', new TextDecoder('utf8').decode(value))
* // => got content: datastore
* }
* ```
*/
getAll: (
options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>
) => AsyncIterable<Pair>

/**
* Remove the record for the passed key
*
* @example
*
* ```js
* await store.delete(CID('bafyfoo'))
* console.log('deleted awesome content :(')
* ```
*/
delete: (key: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>) => Promise<void>

/**
* Remove values for the passed keys
*
* @example
*
* ```js
* const source = [new Key('awesome')]
* const source = [CID('bafyfoo')]
*
* for await (const key of store.deleteMany(source)) {
* console.log(`deleted content with key ${key}`)
Expand Down