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

Commit

Permalink
feat: add getMany()
Browse files Browse the repository at this point in the history
With `getMany()` it's possible to get several blocks with one call.

Closes #132.
  • Loading branch information
vmx committed Nov 7, 2018
1 parent 240c868 commit 0b84dd9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ const ipld = new Ipld({
- `value` - the value that resulted from the get
- `remainderPath` - If it didn't manage to successfully resolve the whole path through or if simply the `localResolve` option was passed.

### `.getMany(cids, callback)`

> Retrieve several nodes at once
`callback` should be a function with the signature `function (err, result)`, the result is an array with the nodes corresponding to the CIDs.


### `.getStream(cid [, path] [, options])`

> Same as get, but returns a source pull-stream that is used to pass the fetched node.
Expand Down
33 changes: 33 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ class IPLDResolver {
return deferred
}

/**
* Get multiple nodes back from an array of CIDs.
*
* @param {Array<CID>} cids
* @param {function(Error, Array)} callback
* @returns {void}
*/
getMany (cids, callback) {
if (!Array.isArray(cids)) {
return callback(new Error('Argument must be an array of CIDs'))
}
this.bs.getMany(cids, (err, blocks) => {
if (err) {
return callback(err)
}
map(blocks, (block, mapCallback) => {
const resolver = this.resolvers[block.cid.codec]
if (!resolver) {
return mapCallback(
new Error('No resolver found for codec "' + block.cid.codec + '"'))
}

resolver.util.deserialize(block.data, (err, deserialized) => {
if (err) {
return mapCallback(err)
}
return mapCallback(null, deserialized)
})
},
callback)
})
}

put (node, options, callback) {
if (typeof options === 'function') {
callback = options
Expand Down
42 changes: 42 additions & 0 deletions test/ipld-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const dagPB = require('ipld-dag-pb')
const dagCBOR = require('ipld-dag-cbor')
const each = require('async/each')
const waterfall = require('async/waterfall')
const CID = require('cids')

const IPLDResolver = require('../src')

Expand Down Expand Up @@ -66,4 +67,45 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => {
done()
})
})

describe('getMany', () => {
it('should return nodes correctly', () => {
resolver.getMany([cidCbor, cidPb], (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.equal(2)
expect(result).to.deep.equal([nodeCbor, nodePb])
})
})

it('should return nodes in input order', () => {
resolver.getMany([cidPb, cidCbor], (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.equal(2)
expect(result).to.deep.equal([nodePb, nodeCbor])
})
})

it('should return error on invalid CID', () => {
resolver.getMany([cidCbor, 'invalidcid'], (err, result) => {
expect(err.message).to.equal('Not a valid cid')
expect(result).to.be.undefined()
})
})

it('should return error on non-existent CID', () => {
const nonExistentCid = new CID(
'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
resolver.getMany([cidCbor, nonExistentCid], (err, result) => {
expect(err.message).to.equal('Not Found')
expect(result).to.be.undefined()
})
})

it('should return error on invalid input', () => {
resolver.getMany('astring', (err, result) => {
expect(err.message).to.equal('Argument must be an array of CIDs')
expect(result).to.be.undefined()
})
})
})
})

0 comments on commit 0b84dd9

Please sign in to comment.