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

Commit

Permalink
fix: use bs.getBlocks and cleanup interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 31, 2016
1 parent 5ab53e7 commit 3e5b314
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"run-series": "^1.1.4"
},
"dependencies": {
"multihashes": "^0.2.2",
"run-parallel-limit": "^1.0.3"
},
"contributors": [
Expand Down
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const parallelLimit = require('run-parallel-limit')
const mh = require('multihashes')

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

var results = {}
if (this.isOnline()) {
this._bitswap.getBlocks(multihashes, (results) => {
callback(null, results)
})
return
}

parallelLimit(multihashes.map((multihash) => (next) => {
this.getBlock(multihash, extension, (err, block) => {
results[multihash] = {
err: err,
block: block
}
const results = {}
parallelLimit(multihashes.map((key) => (next) => {
this._repo.datastore.get(key, extension, (error, block) => {
results[mh.toB58String(key)] = {error, block}
next()
})
}), 100, (err) => {
Expand Down
59 changes: 41 additions & 18 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect
const Block = require('ipfs-block')
const mh = require('multihashes')
const BlockService = require('../src')

module.exports = (repo) => {
Expand Down Expand Up @@ -93,15 +94,15 @@ module.exports = (repo) => {
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
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.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)
expect(blocks[mh.toB58String(b1.key)]).to.exist
expect(blocks[mh.toB58String(b1.key)].error).to.not.exist
expect(blocks[mh.toB58String(b1.key)].block.data).to.deep.equal(b1.data)
expect(blocks[mh.toB58String(b2.key)]).to.exist
expect(blocks[mh.toB58String(b2.key)].error).to.not.exist
expect(blocks[mh.toB58String(b2.key)].block.data).to.deep.equal(b2.data)
expect(blocks[mh.toB58String(b3.key)]).to.exist
expect(blocks[mh.toB58String(b3.key)].error).to.not.exist
expect(blocks[mh.toB58String(b3.key)].block.data).to.deep.equal(b3.data)
done()
})
})
Expand All @@ -118,15 +119,15 @@ module.exports = (repo) => {
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
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)
expect(blocks[mh.toB58String(b1.key)]).to.exist
expect(blocks[mh.toB58String(b1.key)].error).to.not.exist
expect(blocks[mh.toB58String(b1.key)].block.data).to.deep.equal(b1.data)
expect(blocks[mh.toB58String(b2.key)]).to.exist
expect(blocks[mh.toB58String(b2.key)].error).to.exist
expect(blocks[mh.toB58String(b2.key)].block).to.not.exist
expect(blocks[mh.toB58String(b3.key)]).to.exist
expect(blocks[mh.toB58String(b3.key)].error).to.not.exist
expect(blocks[mh.toB58String(b3.key)].block.data).to.deep.equal(b3.data)
done()
})
})
Expand Down Expand Up @@ -257,6 +258,28 @@ module.exports = (repo) => {
bs.goOnline(bitswap)
bs.addBlock(new Block('secret sauce'), done)
})

it('getBlocks through bitswap', (done) => {
const b1 = new Block('secret sauce 1')
const b2 = new Block('secret sauce 2')

const bitswap = {
getBlocks (keys, cb) {
cb({
[mh.toB58String(b1.key)]: {block: b1},
[mh.toB58String(b2.key)]: {block: b2}
})
}
}

bs.goOnline(bitswap)
bs.getBlocks([b1.key, b2.key], (err, results) => {
expect(err).to.not.exist
expect(results[mh.toB58String(b1.key)].block).to.be.eql(b1)
expect(results[mh.toB58String(b2.key)].block).to.be.eql(b2)
done()
})
})
})
})
}

0 comments on commit 3e5b314

Please sign in to comment.