Skip to content

Commit

Permalink
feat: add black hole stores (#227)
Browse files Browse the repository at this point in the history
Sometimes it's useful to not store anything, for example calculating
the CID of a datastructure without actually storing any of the data.
  • Loading branch information
achingbrain committed Jun 3, 2023
1 parent 61f56da commit 6074f0f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 7 deletions.
16 changes: 14 additions & 2 deletions packages/blockstore-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Usage](#usage)
- [BaseBlockstore](#baseblockstore)
- [MemoryBlockstore](#memoryblockstore)
- [BlackHoleBlockstore](#blackholeblockstore)
- [API Docs](#api-docs)
- [License](#license)
- [Contribute](#contribute)
Expand All @@ -35,8 +36,9 @@ Loading this module through a script tag will make it's exports available as `Bl

## Implementations

- Base: [`src/base`](src/base.js)
- Memory: [`src/memory`](src/memory.js)
- Base: [`src/base`](src/base.ts)
- Memory: [`src/memory`](src/memory.ts)
- BlackHole: ['src/blackhole](src/blackhole.ts)

## Usage

Expand Down Expand Up @@ -70,6 +72,16 @@ import { MemoryBlockstore } from 'blockstore-core/memory'
const store = new MemoryBlockstore()
```

### BlackHoleBlockstore

A Blockstore that does not store any blocks.

```js
import { BlackHoleBlockstore } from 'blockstore-core/black-hole'

const store = new BlackHoleBlockstore()
```

## API Docs

- <https://ipfs.github.io/js-stores/modules/blockstore_core.html>
Expand Down
4 changes: 4 additions & 0 deletions packages/blockstore-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"types": "./dist/src/base.d.ts",
"import": "./dist/src/base.js"
},
"./black-hole": {
"types": "./dist/src/black-hole.d.ts",
"import": "./dist/src/black-hole.js"
},
"./errors": {
"types": "./dist/src/errors.d.ts",
"import": "./dist/src/errors.js"
Expand Down
27 changes: 27 additions & 0 deletions packages/blockstore-core/src/black-hole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BaseBlockstore } from './base.js'
import * as Errors from './errors.js'
import type { Pair } from 'interface-blockstore'
import type { Await, AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'

export class BlackHoleBlockstore extends BaseBlockstore {
put (key: CID): Await<CID> {
return key
}

get (): Await<Uint8Array> {
throw Errors.notFoundError()
}

has (): Await<boolean> {
return false
}

async delete (): Promise<void> {

}

async * getAll (): AwaitIterable<Pair> {

}
}
21 changes: 16 additions & 5 deletions packages/datastore-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ Loading this module through a script tag will make it's exports available as `Da
## Implementations

- Wrapper Implementations
- Mount: [`src/mount`](src/mount.js)
- Keytransform: [`src/keytransform`](src/keytransform.js)
- Sharding: [`src/sharding`](src/sharding.js)
- Tiered: [`src/tiered`](src/tirered.js)
- Namespace: [`src/namespace`](src/namespace.js)
- Mount: [`src/mount`](src/mount.ts)
- Keytransform: [`src/keytransform`](src/keytransform.ts)
- Sharding: [`src/sharding`](src/sharding.ts)
- Tiered: [`src/tiered`](src/tirered.ts)
- Namespace: [`src/namespace`](src/namespace.ts)
- BlackHole: [`src/black-hole`](src/black-hole.ts)

## Usage

Expand Down Expand Up @@ -83,6 +84,16 @@ import {
const store = new MountStore({prefix: new Key('/a'), datastore: new MemoryStore()})
```

### BlackHoleDatastore

A datastore that does not store any data.

```js
import { BlackHoleDatastore } from 'datastore-core/black-hole'

const store = new BlackHoleDatastore()
```

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs-unixfs-importer/issues)!
Expand Down
4 changes: 4 additions & 0 deletions packages/datastore-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"types": "./dist/src/base.d.ts",
"import": "./dist/src/base.js"
},
"./black-hole": {
"types": "./dist/src/black-hole.d.ts",
"import": "./dist/src/black-hole.js"
},
"./errors": {
"types": "./dist/src/errors.d.ts",
"import": "./dist/src/errors.js"
Expand Down
31 changes: 31 additions & 0 deletions packages/datastore-core/src/black-hole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { BaseDatastore } from './base.js'
import * as Errors from './errors.js'
import type { Pair } from 'interface-datastore'
import type { Key } from 'interface-datastore/key'
import type { Await, AwaitIterable } from 'interface-store'

export class BlackHoleDatastore extends BaseDatastore {
put (key: Key): Await<Key> {
return key
}

get (): Await<Uint8Array> {
throw Errors.notFoundError()
}

has (key: Key): Await<boolean> {
return false
}

delete (key: Key): Await<void> {

}

* _all (): AwaitIterable<Pair> {

}

* _allKeys (): AwaitIterable<Key> {

}
}

0 comments on commit 6074f0f

Please sign in to comment.