Skip to content

Commit 145bc4d

Browse files
committed
for testing purposes
1 parent 154bc60 commit 145bc4d

File tree

2 files changed

+50
-31
lines changed

2 files changed

+50
-31
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ipfs-repo",
33
"version": "0.9.1",
44
"description": "IPFS Repo implementation",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"test": "aegir-test",
@@ -67,4 +67,4 @@
6767
"nginnever <[email protected]>",
6868
"npmcdn-to-unpkg-bot <[email protected]>"
6969
]
70-
}
70+
}

src/stores/blockstore.js

+48-29
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict'
22

33
const Block = require('ipfs-block')
4-
const pull = require('pull-stream')
54
const Lock = require('lock')
65
const base32 = require('base32.js')
76
const path = require('path')
8-
const pullWrite = require('pull-write')
97
const parallel = require('run-parallel')
8+
const pull = require('pull-stream')
9+
const pullWrite = require('pull-write')
1010
const pullDefer = require('pull-defer/source')
1111

1212
const PREFIX_LENGTH = 5
13+
const EXTENSION = 'data'
1314

1415
exports = module.exports
1516

1617
function multihashToPath (multihash) {
17-
const extension = 'data'
1818
const encoder = new base32.Encoder()
1919
const hash = encoder.write(multihash).finalize()
20-
const filename = `${hash}.${extension}`
20+
const filename = `${hash}.${EXTENSION}`
2121
const folder = filename.slice(0, PREFIX_LENGTH)
2222

2323
return path.join(folder, filename)
@@ -87,40 +87,31 @@ exports.setUp = (basePath, BlobStore, locks) => {
8787
},
8888

8989
/*
90-
* returns a pull-stream to write blockBlob into
90+
* putStream - write multiple blocks
91+
*
92+
* returns a pull-stream that expects blockBlobs
93+
*
9194
* NOTE: blockBlob is a { data: <>, key: <> } and not a
9295
* ipfs-block instance. This is because Block instances support
9396
* several types of hashing and it is up to the BlockService
9497
* to understand the right one to use (given the CID)
9598
*/
96-
// TODO use a more explicit name, given that getStream is just for
97-
// one block, multiple blocks should have different naming
99+
// TODO
100+
// consider using a more explicit name, this can cause some confusion
101+
// since the natural association is
102+
// getStream - createReadStream - read one
103+
// putStream - createWriteStream - write one
104+
// where in fact it is:
105+
// getStream - createReadStream - read one (the same)
106+
// putStream - createFilesWriteStream = write several
107+
//
98108
putStream () {
99109
let ended = false
100110
let written = []
101111
let push = null
102112

103113
const sink = pullWrite((blockBlobs, cb) => {
104-
const tasks = blockBlobs.map((blockBlob) => {
105-
return (cb) => {
106-
writeBlock(blockBlob, (err, meta) => {
107-
if (err) {
108-
return cb(err)
109-
}
110-
111-
if (push) {
112-
const read = push
113-
push = null
114-
read(null, meta)
115-
return cb()
116-
}
117-
118-
written.push(meta)
119-
cb()
120-
})
121-
}
122-
})
123-
114+
const tasks = writeTasks(blockBlobs)
124115
parallel(tasks, cb)
125116
}, null, 100, (err) => {
126117
ended = err || true
@@ -129,7 +120,6 @@ exports.setUp = (basePath, BlobStore, locks) => {
129120
}
130121
})
131122

132-
// TODO ??Why does a putStream need to be a source as well??
133123
const source = (end, cb) => {
134124
if (end) {
135125
ended = end
@@ -145,7 +135,36 @@ exports.setUp = (basePath, BlobStore, locks) => {
145135
push = cb
146136
}
147137

148-
return { source: source, sink: sink }
138+
/*
139+
* Creates individual tasks to write each block blob that can be
140+
* exectured in parallel
141+
*/
142+
function writeTasks (blockBlobs) {
143+
return blockBlobs.map((blockBlob) => {
144+
return (cb) => {
145+
writeBlock(blockBlob, (err, meta) => {
146+
if (err) {
147+
return cb(err)
148+
}
149+
150+
if (push) {
151+
const read = push
152+
push = null
153+
read(null, meta)
154+
return cb()
155+
}
156+
157+
written.push(meta)
158+
cb()
159+
})
160+
}
161+
})
162+
}
163+
164+
return {
165+
source: source,
166+
sink: sink
167+
}
149168
},
150169

151170
has (key, callback) {

0 commit comments

Comments
 (0)