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

Commit 7eba946

Browse files
Merge pull request #21 from ipfs/get-blocks
fix: use bs.getBlocks and cleanup interface
2 parents 5ab53e7 + 3e5b314 commit 7eba946

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"run-series": "^1.1.4"
5353
},
5454
"dependencies": {
55+
"multihashes": "^0.2.2",
5556
"run-parallel-limit": "^1.0.3"
5657
},
5758
"contributors": [

src/index.js

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

33
const parallelLimit = require('run-parallel-limit')
4+
const mh = require('multihashes')
45

56
// BlockService is a hybrid block datastore. It stores data in a local
67
// datastore and may retrieve data from a remote Exchange.
@@ -69,14 +70,17 @@ module.exports = class BlockService {
6970
return callback(new Error('Invalid batch of multihashes'))
7071
}
7172

72-
var results = {}
73+
if (this.isOnline()) {
74+
this._bitswap.getBlocks(multihashes, (results) => {
75+
callback(null, results)
76+
})
77+
return
78+
}
7379

74-
parallelLimit(multihashes.map((multihash) => (next) => {
75-
this.getBlock(multihash, extension, (err, block) => {
76-
results[multihash] = {
77-
err: err,
78-
block: block
79-
}
80+
const results = {}
81+
parallelLimit(multihashes.map((key) => (next) => {
82+
this._repo.datastore.get(key, extension, (error, block) => {
83+
results[mh.toB58String(key)] = {error, block}
8084
next()
8185
})
8286
}), 100, (err) => {

test/block-service-test.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const expect = require('chai').expect
55
const Block = require('ipfs-block')
6+
const mh = require('multihashes')
67
const BlockService = require('../src')
78

89
module.exports = (repo) => {
@@ -93,15 +94,15 @@ module.exports = (repo) => {
9394
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
9495
expect(err).to.not.exist
9596
expect(Object.keys(blocks)).to.have.lengthOf(3)
96-
expect(blocks[b1.key]).to.exist
97-
expect(blocks[b1.key].err).to.not.exist
98-
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
99-
expect(blocks[b2.key]).to.exist
100-
expect(blocks[b2.key].err).to.not.exist
101-
expect(blocks[b2.key].block.data).to.deep.equal(b2.data)
102-
expect(blocks[b3.key]).to.exist
103-
expect(blocks[b3.key].err).to.not.exist
104-
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
97+
expect(blocks[mh.toB58String(b1.key)]).to.exist
98+
expect(blocks[mh.toB58String(b1.key)].error).to.not.exist
99+
expect(blocks[mh.toB58String(b1.key)].block.data).to.deep.equal(b1.data)
100+
expect(blocks[mh.toB58String(b2.key)]).to.exist
101+
expect(blocks[mh.toB58String(b2.key)].error).to.not.exist
102+
expect(blocks[mh.toB58String(b2.key)].block.data).to.deep.equal(b2.data)
103+
expect(blocks[mh.toB58String(b3.key)]).to.exist
104+
expect(blocks[mh.toB58String(b3.key)].error).to.not.exist
105+
expect(blocks[mh.toB58String(b3.key)].block.data).to.deep.equal(b3.data)
105106
done()
106107
})
107108
})
@@ -118,15 +119,15 @@ module.exports = (repo) => {
118119
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
119120
expect(err).to.not.exist
120121
expect(Object.keys(blocks)).to.have.lengthOf(3)
121-
expect(blocks[b1.key]).to.exist
122-
expect(blocks[b1.key].err).to.not.exist
123-
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
124-
expect(blocks[b2.key]).to.exist
125-
expect(blocks[b2.key].err).to.exist
126-
expect(blocks[b2.key].block).to.not.exist
127-
expect(blocks[b3.key]).to.exist
128-
expect(blocks[b3.key].err).to.not.exist
129-
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
122+
expect(blocks[mh.toB58String(b1.key)]).to.exist
123+
expect(blocks[mh.toB58String(b1.key)].error).to.not.exist
124+
expect(blocks[mh.toB58String(b1.key)].block.data).to.deep.equal(b1.data)
125+
expect(blocks[mh.toB58String(b2.key)]).to.exist
126+
expect(blocks[mh.toB58String(b2.key)].error).to.exist
127+
expect(blocks[mh.toB58String(b2.key)].block).to.not.exist
128+
expect(blocks[mh.toB58String(b3.key)]).to.exist
129+
expect(blocks[mh.toB58String(b3.key)].error).to.not.exist
130+
expect(blocks[mh.toB58String(b3.key)].block.data).to.deep.equal(b3.data)
130131
done()
131132
})
132133
})
@@ -257,6 +258,28 @@ module.exports = (repo) => {
257258
bs.goOnline(bitswap)
258259
bs.addBlock(new Block('secret sauce'), done)
259260
})
261+
262+
it('getBlocks through bitswap', (done) => {
263+
const b1 = new Block('secret sauce 1')
264+
const b2 = new Block('secret sauce 2')
265+
266+
const bitswap = {
267+
getBlocks (keys, cb) {
268+
cb({
269+
[mh.toB58String(b1.key)]: {block: b1},
270+
[mh.toB58String(b2.key)]: {block: b2}
271+
})
272+
}
273+
}
274+
275+
bs.goOnline(bitswap)
276+
bs.getBlocks([b1.key, b2.key], (err, results) => {
277+
expect(err).to.not.exist
278+
expect(results[mh.toB58String(b1.key)].block).to.be.eql(b1)
279+
expect(results[mh.toB58String(b2.key)].block).to.be.eql(b2)
280+
done()
281+
})
282+
})
260283
})
261284
})
262285
}

0 commit comments

Comments
 (0)