Skip to content

Commit 6074f0f

Browse files
authored
feat: add black hole stores (#227)
Sometimes it's useful to not store anything, for example calculating the CID of a datastructure without actually storing any of the data.
1 parent 61f56da commit 6074f0f

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

packages/blockstore-core/README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [Usage](#usage)
1616
- [BaseBlockstore](#baseblockstore)
1717
- [MemoryBlockstore](#memoryblockstore)
18+
- [BlackHoleBlockstore](#blackholeblockstore)
1819
- [API Docs](#api-docs)
1920
- [License](#license)
2021
- [Contribute](#contribute)
@@ -35,8 +36,9 @@ Loading this module through a script tag will make it's exports available as `Bl
3536

3637
## Implementations
3738

38-
- Base: [`src/base`](src/base.js)
39-
- Memory: [`src/memory`](src/memory.js)
39+
- Base: [`src/base`](src/base.ts)
40+
- Memory: [`src/memory`](src/memory.ts)
41+
- BlackHole: ['src/blackhole](src/blackhole.ts)
4042

4143
## Usage
4244

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

75+
### BlackHoleBlockstore
76+
77+
A Blockstore that does not store any blocks.
78+
79+
```js
80+
import { BlackHoleBlockstore } from 'blockstore-core/black-hole'
81+
82+
const store = new BlackHoleBlockstore()
83+
```
84+
7385
## API Docs
7486

7587
- <https://ipfs.github.io/js-stores/modules/blockstore_core.html>

packages/blockstore-core/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
"types": "./dist/src/base.d.ts",
5656
"import": "./dist/src/base.js"
5757
},
58+
"./black-hole": {
59+
"types": "./dist/src/black-hole.d.ts",
60+
"import": "./dist/src/black-hole.js"
61+
},
5862
"./errors": {
5963
"types": "./dist/src/errors.d.ts",
6064
"import": "./dist/src/errors.js"
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { BaseBlockstore } from './base.js'
2+
import * as Errors from './errors.js'
3+
import type { Pair } from 'interface-blockstore'
4+
import type { Await, AwaitIterable } from 'interface-store'
5+
import type { CID } from 'multiformats/cid'
6+
7+
export class BlackHoleBlockstore extends BaseBlockstore {
8+
put (key: CID): Await<CID> {
9+
return key
10+
}
11+
12+
get (): Await<Uint8Array> {
13+
throw Errors.notFoundError()
14+
}
15+
16+
has (): Await<boolean> {
17+
return false
18+
}
19+
20+
async delete (): Promise<void> {
21+
22+
}
23+
24+
async * getAll (): AwaitIterable<Pair> {
25+
26+
}
27+
}

packages/datastore-core/README.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ Loading this module through a script tag will make it's exports available as `Da
3737
## Implementations
3838

3939
- Wrapper Implementations
40-
- Mount: [`src/mount`](src/mount.js)
41-
- Keytransform: [`src/keytransform`](src/keytransform.js)
42-
- Sharding: [`src/sharding`](src/sharding.js)
43-
- Tiered: [`src/tiered`](src/tirered.js)
44-
- Namespace: [`src/namespace`](src/namespace.js)
40+
- Mount: [`src/mount`](src/mount.ts)
41+
- Keytransform: [`src/keytransform`](src/keytransform.ts)
42+
- Sharding: [`src/sharding`](src/sharding.ts)
43+
- Tiered: [`src/tiered`](src/tirered.ts)
44+
- Namespace: [`src/namespace`](src/namespace.ts)
45+
- BlackHole: [`src/black-hole`](src/black-hole.ts)
4546

4647
## Usage
4748

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

87+
### BlackHoleDatastore
88+
89+
A datastore that does not store any data.
90+
91+
```js
92+
import { BlackHoleDatastore } from 'datastore-core/black-hole'
93+
94+
const store = new BlackHoleDatastore()
95+
```
96+
8697
## Contribute
8798

8899
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs-unixfs-importer/issues)!

packages/datastore-core/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
"types": "./dist/src/base.d.ts",
5656
"import": "./dist/src/base.js"
5757
},
58+
"./black-hole": {
59+
"types": "./dist/src/black-hole.d.ts",
60+
"import": "./dist/src/black-hole.js"
61+
},
5862
"./errors": {
5963
"types": "./dist/src/errors.d.ts",
6064
"import": "./dist/src/errors.js"
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { BaseDatastore } from './base.js'
2+
import * as Errors from './errors.js'
3+
import type { Pair } from 'interface-datastore'
4+
import type { Key } from 'interface-datastore/key'
5+
import type { Await, AwaitIterable } from 'interface-store'
6+
7+
export class BlackHoleDatastore extends BaseDatastore {
8+
put (key: Key): Await<Key> {
9+
return key
10+
}
11+
12+
get (): Await<Uint8Array> {
13+
throw Errors.notFoundError()
14+
}
15+
16+
has (key: Key): Await<boolean> {
17+
return false
18+
}
19+
20+
delete (key: Key): Await<void> {
21+
22+
}
23+
24+
* _all (): AwaitIterable<Pair> {
25+
26+
}
27+
28+
* _allKeys (): AwaitIterable<Key> {
29+
30+
}
31+
}

0 commit comments

Comments
 (0)