Skip to content

Commit 9c06303

Browse files
achingbrainjacobheun
authored andcommitted
feat: accept async iterators into blockstore.putMany (#209)
Allows streaming into the blockstore. The blockstore currently uses the batch functionality of the underlying datatore, so we might need to change `interface-datastore` next to add streaming primitives but this at least allows streaming this far down the stack. BREAKING CHANGE: you must pass an iterable or async iterable to putMany - this should be relatively painless as the current API is to pass an array which is iterable, but it does change the API. * chore: remove CI commitlint * chore: add node 12 to CI * docs: update docs with new api
1 parent 9b85b16 commit 9c06303

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ stages:
77

88
node_js:
99
- '10'
10+
- '12'
1011

1112
os:
1213
- linux
@@ -20,7 +21,6 @@ jobs:
2021
include:
2122
- stage: check
2223
script:
23-
- npx aegir commitlint --travis
2424
- npx aegir dep-check
2525
- npm run lint
2626

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Get a value at the root of the repo.
189189

190190
Put many blocks.
191191

192-
* `block` should be an array of type [Block](https://github.com/ipfs/js-ipfs-block#readme).
192+
* `block` should be an Iterable or AsyncIterable that yields entries of type [Block](https://github.com/ipfs/js-ipfs-block#readme).
193193

194194
#### `Promise<Buffer> repo.blocks.get (cid)`
195195

src/blockstore.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,21 @@ function createBaseStore (store) {
8585
/**
8686
* Like put, but for more.
8787
*
88-
* @param {Array<Block>} blocks
88+
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
8989
* @returns {Promise<void>}
9090
*/
9191
async putMany (blocks) {
92-
const keys = blocks.map((b) => ({
93-
key: cidToKey(b.cid),
94-
block: b
95-
}))
96-
9792
const batch = store.batch()
9893

99-
await Promise.all(
100-
keys.map(async k => {
101-
if (await store.has(k.key)) {
102-
return
103-
}
94+
for await (const block of blocks) {
95+
const key = cidToKey(block.cid)
10496

105-
batch.put(k.key, k.block.data)
106-
})
107-
)
97+
if (await store.has(key)) {
98+
continue
99+
}
100+
101+
batch.put(key, block.data)
102+
}
108103

109104
return batch.commit()
110105
},

0 commit comments

Comments
 (0)