From 9c063036a6e0023ce7e3625682ba0227bdcd0e4c Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 20 Sep 2019 12:26:50 +0100 Subject: [PATCH] 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 --- .travis.yml | 2 +- README.md | 2 +- src/blockstore.js | 23 +++++++++-------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index d2e373b0..8507920f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ stages: node_js: - '10' + - '12' os: - linux @@ -20,7 +21,6 @@ jobs: include: - stage: check script: - - npx aegir commitlint --travis - npx aegir dep-check - npm run lint diff --git a/README.md b/README.md index f5e46902..c48600eb 100644 --- a/README.md +++ b/README.md @@ -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 repo.blocks.get (cid)` diff --git a/src/blockstore.js b/src/blockstore.js index 27fc5415..7d3a4bc0 100644 --- a/src/blockstore.js +++ b/src/blockstore.js @@ -85,26 +85,21 @@ function createBaseStore (store) { /** * Like put, but for more. * - * @param {Array} blocks + * @param {AsyncIterable|Iterable} blocks * @returns {Promise} */ 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() },