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 ebd2d1b commit db7dc8b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ It may contain any of the following:
- `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 @@ -166,6 +166,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
46 changes: 46 additions & 0 deletions test/ipld-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,50 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => {
done()
})
})

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

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

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

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

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

0 comments on commit db7dc8b

Please sign in to comment.