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

Commit 25aade7

Browse files
committed
feat: .getMany
1 parent bc7ec4a commit 25aade7

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const asyncMap = require('async/map')
4+
35
/**
46
* BlockService is a hybrid block datastore. It stores data in a local
57
* datastore and may retrieve data from a remote Exchange.
@@ -94,6 +96,24 @@ class BlockService {
9496
return this._repo.blocks.get(cid, callback)
9597
}
9698

99+
/**
100+
* Get multiple blocks a block by cid.
101+
*
102+
* @param {CID} cid
103+
* @param {function(Error, Block)} callback
104+
* @returns {void}
105+
*/
106+
getMany (cids, callback) {
107+
if (!Array.isArray(cids)) {
108+
return callback(new Error('first arg must be an array of cids'))
109+
}
110+
if (this.hasExchange()) {
111+
return this._bitswap.getMany(cids, callback)
112+
}
113+
114+
asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback)
115+
}
116+
97117
/**
98118
* Delete a block from the blockstore.
99119
*

test/block-service-test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = (repo) => {
5555

5656
it('store many blocks', (done) => {
5757
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
58+
5859
map(data, (d, cb) => {
5960
multihashing(d, 'sha2-256', (err, hash) => {
6061
expect(err).to.not.exist()
@@ -66,8 +67,9 @@ module.exports = (repo) => {
6667
})
6768
})
6869

69-
it('get many blocks', (done) => {
70+
it('get many blocks through get', (done) => {
7071
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
72+
7173
waterfall([
7274
(cb) => map(data, (d, cb) => {
7375
multihashing(d, 'sha2-256', (err, hash) => {
@@ -87,6 +89,27 @@ module.exports = (repo) => {
8789
], done)
8890
})
8991

92+
it('get many blocks through getMany', (done) => {
93+
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
94+
95+
waterfall([
96+
(cb) => map(data, (d, cb) => {
97+
multihashing(d, 'sha2-256', (err, hash) => {
98+
expect(err).to.not.exist()
99+
cb(null, new Block(d, new CID(hash)))
100+
})
101+
}, cb),
102+
(blocks, cb) => map(blocks, (b, cb) => cb(null, b.cid), (err, cids) => {
103+
expect(err).to.not.exist()
104+
bs.getMany(cids, (err, _blocks) => {
105+
expect(err).to.not.exist()
106+
expect(blocks).to.eql(blocks)
107+
cb()
108+
})
109+
})
110+
], done)
111+
})
112+
90113
it('delete a block', (done) => {
91114
const data = Buffer.from('Will not live that much')
92115
multihashing(data, 'sha2-256', (err, hash) => {

0 commit comments

Comments
 (0)