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

Commit 452d545

Browse files
fix importer
1 parent a7f4825 commit 452d545

File tree

2 files changed

+34
-50
lines changed

2 files changed

+34
-50
lines changed

Diff for: src/importer/flush-tree.js

+33-48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const mh = require('multihashes')
44
const UnixFS = require('ipfs-unixfs')
55
const CID = require('cids')
66
const dagPB = require('ipld-dag-pb')
7-
const asyncEach = require('async/each')
7+
const mapValues = require('async/mapValues')
8+
const parallel = require('async/parallel')
89

910
const DAGLink = dagPB.DAGLink
1011
const DAGNode = dagPB.DAGNode
@@ -102,30 +103,17 @@ function createSizeIndex (files) {
102103
* add as a link to the dirNode
103104
*/
104105
function traverse (tree, sizeIndex, path, ipldResolver, source, done) {
105-
const keys = Object.keys(tree)
106-
107-
let tmp = tree
108-
109-
asyncEach(keys, (key, cb) => {
110-
if (isLeaf(tmp[key])) {
111-
cb()
112-
} else {
113-
path = path ? path + '/' + key : key
114-
console.log('->', path)
106+
mapValues(tree, (node, key, cb) => {
107+
if (isLeaf(node)) {
108+
return cb(null, node)
109+
}
115110

116-
traverse(tmp[key], sizeIndex, path, ipldResolver, source, (err, multihash) => {
117-
if (err) {
118-
return done(err)
119-
}
120-
tmp[key] = multihash
121-
cb()
122-
})
111+
traverse(node, sizeIndex, path ? `${path}/${key}` : key, ipldResolver, source, cb)
112+
}, (err, tree) => {
113+
if (err) {
114+
return done(err)
123115
}
124-
}, () => {
125-
next(tmp, done)
126-
})
127116

128-
function next (tree, cb) {
129117
// at this stage, all keys are multihashes
130118
// create a dir node
131119
// add all the multihashes as links
@@ -142,39 +130,36 @@ function traverse (tree, sizeIndex, path, ipldResolver, source, done) {
142130
node.addRawLink(link)
143131
})
144132

145-
console.log('0---->', path)
146-
node.multihash((err, multihash) => {
133+
parallel([
134+
(cb) => node.multihash(cb),
135+
(cb) => node.size(cb)
136+
], (err, res) => {
147137
if (err) {
148-
return cb(err)
138+
return done(err)
149139
}
150-
node.size((err, size) => {
140+
141+
const multihash = res[0]
142+
const size = res[1]
143+
144+
sizeIndex[mh.toB58String(multihash)] = size
145+
ipldResolver.put({
146+
node: node,
147+
cid: new CID(multihash)
148+
}, (err) => {
151149
if (err) {
152-
return cb(err)
150+
source.push(new Error('failed to store dirNode'))
151+
} else if (path) {
152+
source.push({
153+
path: path,
154+
multihash: multihash,
155+
size: size
156+
})
153157
}
154158

155-
sizeIndex[mh.toB58String(multihash)] = size
156-
console.log('1---->', path)
157-
158-
ipldResolver.put({
159-
node: node,
160-
cid: new CID(multihash)
161-
}, (err) => {
162-
if (err) {
163-
source.push(new Error('failed to store dirNode'))
164-
} else if (path) {
165-
console.log('2---->', path)
166-
source.push({
167-
path: path,
168-
multihash: multihash,
169-
size: size
170-
})
171-
}
172-
173-
cb(null, multihash)
174-
})
159+
done(null, multihash)
175160
})
176161
})
177-
}
162+
})
178163
}
179164

180165
function isLeaf (value) {

Diff for: test/test-importer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function stringifyMh (files) {
1818
}
1919

2020
module.exports = function (repo) {
21-
describe.only('importer', function () {
21+
describe('importer', function () {
2222
let ipldResolver
2323

2424
const bigFile = fs.readFileSync(path.join(__dirname, '/test-data/1.2MiB.txt'))
@@ -94,7 +94,6 @@ module.exports = function (repo) {
9494
importer(ipldResolver),
9595
pull.collect((err, files) => {
9696
expect(err).to.not.exist
97-
console.log(files)
9897
expect(files.length).to.equal(3)
9998
stringifyMh(files).forEach((file) => {
10099
if (file.path === 'foo/bar/200Bytes.txt') {

0 commit comments

Comments
 (0)