Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Updates getBlocks API.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed Apr 18, 2016
1 parent 6779e25 commit 58f1191
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,18 @@ If the block could not be found, expect `err.code` to be `'ENOENT'`.

Asynchronously returns the blocks whose content multihashes match the array
`multihashes`.
Returns an error (`err.code === 'ENOENT'`) if *any* the blocks do not exist.

`blocks` is an object that maps each `multihash` to an object of the form

```js
{
err: Error
block: Block
}
```

Expect `blocks[multihash].err.code === 'ENOENT'` and `blocks[multihash].block
=== null` if a block did not exist.

*Does not guarantee atomicity.*

Expand Down
10 changes: 5 additions & 5 deletions src/block-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ function BlockService (ipfsRepo, exchange) {
return callback(new Error('Invalid batch of multihashes'))
}

const blocks = []
var results = {}

async.each(multihashes, (multihash, next) => {
this.getBlock(multihash, extension, (err, block) => {
if (err) { return next(err) }
if (block) {
blocks.push(block)
results[multihash] = {
err: err,
block: block
}
next()
})
}, (err) => {
callback(err, blocks)
callback(err, results)
})
}

Expand Down
24 changes: 21 additions & 3 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ module.exports = (repo) => {

bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
expect(err).to.not.exist
expect(blocks).to.have.lengthOf(3)
expect(Object.keys(blocks)).to.have.lengthOf(3)
expect(blocks[b1.key]).to.exist
expect(blocks[b1.key].err).to.not.exist
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
expect(blocks[b2.key]).to.exist
expect(blocks[b2.key].err).to.not.exist
expect(blocks[b2.key].block.data).to.deep.equal(b2.data)
expect(blocks[b3.key]).to.exist
expect(blocks[b3.key].err).to.not.exist
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
done()
})
})
Expand All @@ -108,8 +117,17 @@ module.exports = (repo) => {
expect(err).to.not.exist

bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
expect(err).to.exist
expect(blocks).to.have.length.below(3)
expect(err).to.not.exist
expect(Object.keys(blocks)).to.have.lengthOf(3)
expect(blocks[b1.key]).to.exist
expect(blocks[b1.key].err).to.not.exist
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
expect(blocks[b2.key]).to.exist
expect(blocks[b2.key].err).to.exist
expect(blocks[b2.key].block).to.not.exist
expect(blocks[b3.key]).to.exist
expect(blocks[b3.key].err).to.not.exist
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
done()
})
})
Expand Down

0 comments on commit 58f1191

Please sign in to comment.