Skip to content

feat: accept async iterators into blockstore.putMany #209

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

Merged
merged 4 commits into from
Sep 20, 2019
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ stages:

node_js:
- '10'
- '12'

os:
- linux
Expand All @@ -20,7 +21,6 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Get a value at the root of the repo.

Put many blocks.

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

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

Expand Down
23 changes: 9 additions & 14 deletions src/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,21 @@ function createBaseStore (store) {
/**
* Like put, but for more.
*
* @param {Array<Block>} blocks
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
* @returns {Promise<void>}
*/
async putMany (blocks) {
const keys = blocks.map((b) => ({
key: cidToKey(b.cid),
block: b
}))

const batch = store.batch()

await Promise.all(
keys.map(async k => {
if (await store.has(k.key)) {
return
}
for await (const block of blocks) {
const key = cidToKey(block.cid)

batch.put(k.key, k.block.data)
})
)
if (await store.has(key)) {
continue
}

batch.put(key, block.data)
}

return batch.commit()
},
Expand Down