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

Commit 8aa9e0a

Browse files
authored
feat: .getMany (#81)
* chore: update aegir * feat: .getMany * test: refactor offline tests * chore: fix jsdocs * chore: apply CR
1 parent b405a91 commit 8aa9e0a

File tree

3 files changed

+81
-65
lines changed

3 files changed

+81
-65
lines changed

Diff for: package.json

-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
"coverage-publish": "aegir coverage --provider coveralls",
1818
"docs": "aegir docs"
1919
},
20-
"pre-commit": [
21-
"lint",
22-
"test"
23-
],
2420
"repository": {
2521
"type": "git",
2622
"url": "git+https://github.com/ipfs/js-ipfs-block-service.git"
@@ -44,7 +40,6 @@
4440
"lodash": "^4.17.11",
4541
"multihashing-async": "~0.5.1",
4642
"ncp": "^2.0.0",
47-
"pre-commit": "^1.2.2",
4843
"rimraf": "^2.6.2"
4944
},
5045
"engines": {

Diff for: src/index.js

+27-8
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.
@@ -57,10 +59,10 @@ class BlockService {
5759
*/
5860
put (block, callback) {
5961
if (this.hasExchange()) {
60-
return this._bitswap.put(block, callback)
62+
this._bitswap.put(block, callback)
63+
} else {
64+
this._repo.blocks.put(block, callback)
6165
}
62-
63-
this._repo.blocks.put(block, callback)
6466
}
6567

6668
/**
@@ -72,10 +74,10 @@ class BlockService {
7274
*/
7375
putMany (blocks, callback) {
7476
if (this.hasExchange()) {
75-
return this._bitswap.putMany(blocks, callback)
77+
this._bitswap.putMany(blocks, callback)
78+
} else {
79+
this._repo.blocks.putMany(blocks, callback)
7680
}
77-
78-
this._repo.blocks.putMany(blocks, callback)
7981
}
8082

8183
/**
@@ -87,10 +89,27 @@ class BlockService {
8789
*/
8890
get (cid, callback) {
8991
if (this.hasExchange()) {
90-
return this._bitswap.get(cid, callback)
92+
this._bitswap.get(cid, callback)
93+
} else {
94+
this._repo.blocks.get(cid, callback)
9195
}
96+
}
9297

93-
return this._repo.blocks.get(cid, callback)
98+
/**
99+
* Get multiple blocks back from an array of cids.
100+
*
101+
* @param {Array<CID>} cids
102+
* @param {function(Error, Block)} callback
103+
* @returns {void}
104+
*/
105+
getMany (cids, callback) {
106+
if (!Array.isArray(cids)) {
107+
callback(new Error('first arg must be an array of cids'))
108+
} else if (this.hasExchange()) {
109+
this._bitswap.getMany(cids, callback)
110+
} else {
111+
asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback)
112+
}
94113
}
95114

96115
/**

Diff for: test/block-service-test.js

+54-52
Original file line numberDiff line numberDiff line change
@@ -17,78 +17,80 @@ const BlockService = require('../src')
1717
module.exports = (repo) => {
1818
describe('block-service', () => {
1919
let bs
20+
let testBlocks
2021

21-
before(() => {
22+
before((done) => {
2223
bs = new BlockService(repo)
24+
25+
const data = [
26+
Buffer.from('1'),
27+
Buffer.from('2'),
28+
Buffer.from('3'),
29+
Buffer.from('A random data block')
30+
]
31+
32+
map(data, (d, cb) => {
33+
multihashing(d, 'sha2-256', (err, hash) => {
34+
expect(err).to.not.exist()
35+
cb(null, new Block(d, new CID(hash)))
36+
})
37+
}, (err, blocks) => {
38+
expect(err).to.not.exist()
39+
testBlocks = blocks
40+
done()
41+
})
2342
})
2443

25-
describe('offline', () => {
44+
describe('fetch only from local Repo', () => {
2645
it('store and get a block', (done) => {
27-
const data = Buffer.from('A random data block')
28-
multihashing(data, 'sha2-256', (err, hash) => {
29-
expect(err).to.not.exist()
30-
const b = new Block(data, new CID(hash))
46+
const b = testBlocks[3]
3147

32-
waterfall([
33-
(cb) => bs.put(b, cb),
34-
(cb) => bs.get(b.cid, cb),
35-
(res, cb) => {
36-
expect(res).to.be.eql(b)
37-
cb()
38-
}
39-
], done)
40-
})
48+
waterfall([
49+
(cb) => bs.put(b, cb),
50+
(cb) => bs.get(b.cid, cb),
51+
(res, cb) => {
52+
expect(res).to.eql(b)
53+
cb()
54+
}
55+
], done)
4156
})
4257

43-
it('get a non existent block', (done) => {
44-
const data = Buffer.from('Not stored')
58+
it('get a non stored yet block', (done) => {
59+
const b = testBlocks[2]
4560

46-
multihashing(data, 'sha2-256', (err, hash) => {
47-
expect(err).to.not.exist()
48-
bs.get(new CID(hash), (err, block) => {
49-
expect(err).to.exist()
50-
expect(block).to.not.exist()
51-
done()
52-
})
61+
bs.get(b.cid, (err, block) => {
62+
expect(err).to.exist()
63+
expect(block).to.not.exist()
64+
done()
5365
})
5466
})
5567

5668
it('store many blocks', (done) => {
57-
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
58-
map(data, (d, cb) => {
59-
multihashing(d, 'sha2-256', (err, hash) => {
60-
expect(err).to.not.exist()
61-
cb(null, new Block(d, new CID(hash)))
62-
})
63-
}, (err, blocks) => {
69+
bs.putMany(testBlocks, done)
70+
})
71+
72+
it('get many blocks through .get', (done) => {
73+
map(testBlocks, (b, cb) => bs.get(b.cid, cb), (err, blocks) => {
6474
expect(err).to.not.exist()
65-
bs.putMany(blocks, done)
75+
expect(blocks).to.eql(testBlocks)
76+
done()
6677
})
6778
})
6879

69-
it('get many blocks', (done) => {
70-
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
71-
waterfall([
72-
(cb) => map(data, (d, cb) => {
73-
multihashing(d, 'sha2-256', (err, hash) => {
74-
expect(err).to.not.exist()
75-
cb(null, new Block(d, new CID(hash)))
76-
})
77-
}, cb),
78-
(blocks, cb) => map(
79-
blocks,
80-
(b, cb) => bs.get(b.cid, cb),
81-
(err, res) => {
82-
expect(err).to.not.exist()
83-
expect(res).to.be.eql(blocks)
84-
cb()
85-
}
86-
)
87-
], done)
80+
it('get many blocks through .getMany', (done) => {
81+
map(testBlocks, (b, cb) => cb(null, b.cid), (err, cids) => {
82+
expect(err).to.not.exist()
83+
bs.getMany(cids, (err, _blocks) => {
84+
expect(err).to.not.exist()
85+
expect(testBlocks).to.eql(_blocks)
86+
done()
87+
})
88+
})
8889
})
8990

9091
it('delete a block', (done) => {
9192
const data = Buffer.from('Will not live that much')
93+
9294
multihashing(data, 'sha2-256', (err, hash) => {
9395
expect(err).to.not.exist()
9496
const b = new Block(data, new CID(hash))
@@ -140,7 +142,7 @@ module.exports = (repo) => {
140142
})
141143
})
142144

143-
describe('has exchange', () => {
145+
describe('fetch through Bitswap (has exchange)', () => {
144146
beforeEach(() => {
145147
bs = new BlockService(repo)
146148
})

0 commit comments

Comments
 (0)