Skip to content

Commit

Permalink
feat: accept async iterators into blockstore.putMany (#209)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored and jacobheun committed Sep 20, 2019
1 parent 9b85b16 commit 9c06303
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
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

0 comments on commit 9c06303

Please sign in to comment.