Skip to content

Commit 154bc60

Browse files
committed
feat: blockstore gets blockBlobs instead of blocks (the difference is that now it receives the key in which it should store it
1 parent 311551a commit 154bc60

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/stores/blockstore.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ exports.setUp = (basePath, BlobStore, locks) => {
2727
const store = new BlobStore(basePath + '/blocks')
2828
const lock = new Lock()
2929

30-
function writeBlock (block, callback) {
31-
if (!block || !block.data) {
30+
// blockBlob is an object with:
31+
// { data: <>, key: <> }
32+
function writeBlock (blockBlob, callback) {
33+
if (!blockBlob || !blockBlob.data) {
3234
return callback(new Error('Invalid block'))
3335
}
3436

35-
const key = multihashToPath(block.key())
37+
const key = multihashToPath(blockBlob.key)
3638

3739
lock(key, (release) => {
3840
pull(
3941
pull.values([
40-
block.data
42+
blockBlob.data
4143
]),
4244
store.write(key, release(released))
4345
)
@@ -84,18 +86,24 @@ exports.setUp = (basePath, BlobStore, locks) => {
8486
return deferred
8587
},
8688

87-
// returns a pull-stream to write blocks into
89+
/*
90+
* returns a pull-stream to write blockBlob into
91+
* NOTE: blockBlob is a { data: <>, key: <> } and not a
92+
* ipfs-block instance. This is because Block instances support
93+
* several types of hashing and it is up to the BlockService
94+
* to understand the right one to use (given the CID)
95+
*/
8896
// TODO use a more explicit name, given that getStream is just for
8997
// one block, multiple blocks should have different naming
9098
putStream () {
9199
let ended = false
92100
let written = []
93101
let push = null
94102

95-
const sink = pullWrite((blocks, cb) => {
96-
const tasks = blocks.map((block) => {
103+
const sink = pullWrite((blockBlobs, cb) => {
104+
const tasks = blockBlobs.map((blockBlob) => {
97105
return (cb) => {
98-
writeBlock(block, (err, meta) => {
106+
writeBlock(blockBlob, (err, meta) => {
99107
if (err) {
100108
return cb(err)
101109
}

test/blockstore-test.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ module.exports = (repo) => {
1212
describe('blockstore', () => {
1313
const helloKey = 'CIQLS/CIQLSTJHXGJU2PQIUUXFFV62PWV7VREE57RXUU4A52IIR55M4LX432I.data'
1414

15-
const helloIpldKey = 'CIQO2/CIQO2EUTF47PSTAHSL54KUTDS2AAN2DH4URM7H5KRATUGQFCM4OUIQI.data'
16-
1715
const blockCollection = _.range(100).map((i) => new Block(new Buffer(`hello-${i}-${Math.random()}`)))
1816

1917
describe('.putStream', () => {
2018
it('simple', (done) => {
2119
const b = new Block('hello world')
2220
pull(
23-
pull.values([b]),
21+
pull.values([
22+
{ data: b.data, key: b.key() }
23+
]),
2424
repo.blockstore.putStream(),
2525
pull.collect((err, meta) => {
2626
expect(err).to.not.exist
@@ -43,13 +43,17 @@ module.exports = (repo) => {
4343
}
4444

4545
pull(
46-
pull.values([b]),
46+
pull.values([
47+
{ data: b.data, key: b.key() }
48+
]),
4749
repo.blockstore.putStream(),
4850
pull.collect(finish)
4951
)
5052

5153
pull(
52-
pull.values([b]),
54+
pull.values([
55+
{ data: b.data, key: b.key() }
56+
]),
5357
repo.blockstore.putStream(),
5458
pull.collect(finish)
5559
)
@@ -59,6 +63,9 @@ module.exports = (repo) => {
5963
parallel(_.range(50).map(() => (cb) => {
6064
pull(
6165
pull.values(blockCollection),
66+
pull.map((b) => {
67+
return { data: b.data, key: b.key() }
68+
}),
6269
repo.blockstore.putStream(),
6370
pull.collect((err, meta) => {
6471
expect(err).to.not.exist
@@ -69,19 +76,6 @@ module.exports = (repo) => {
6976
}), done)
7077
})
7178

72-
it('custom extension', function (done) {
73-
const b = new Block('hello world 2')
74-
pull(
75-
pull.values([b]),
76-
repo.blockstore.putStream(),
77-
pull.collect((err, meta) => {
78-
expect(err).to.not.exist
79-
expect(meta[0].key).to.be.eql(helloIpldKey)
80-
done()
81-
})
82-
)
83-
})
84-
8579
it('returns an error on invalid block', (done) => {
8680
pull(
8781
pull.values(['hello']),

0 commit comments

Comments
 (0)