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

Commit

Permalink
fix importer
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Oct 27, 2016
1 parent ad50179 commit 410510d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
81 changes: 33 additions & 48 deletions src/importer/flush-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const mh = require('multihashes')
const UnixFS = require('ipfs-unixfs')
const CID = require('cids')
const dagPB = require('ipld-dag-pb')
const asyncEach = require('async/each')
const mapValues = require('async/mapValues')
const parallel = require('async/parallel')

const DAGLink = dagPB.DAGLink
const DAGNode = dagPB.DAGNode
Expand Down Expand Up @@ -102,30 +103,17 @@ function createSizeIndex (files) {
* add as a link to the dirNode
*/
function traverse (tree, sizeIndex, path, ipldResolver, source, done) {
const keys = Object.keys(tree)

let tmp = tree

asyncEach(keys, (key, cb) => {
if (isLeaf(tmp[key])) {
cb()
} else {
path = path ? path + '/' + key : key
console.log('->', path)
mapValues(tree, (node, key, cb) => {
if (isLeaf(node)) {
return cb(null, node)
}

traverse(tmp[key], sizeIndex, path, ipldResolver, source, (err, multihash) => {
if (err) {
return done(err)
}
tmp[key] = multihash
cb()
})
traverse(node, sizeIndex, path ? `${path}/${key}` : key, ipldResolver, source, cb)
}, (err, tree) => {
if (err) {
return done(err)
}
}, () => {
next(tmp, done)
})

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

console.log('0---->', path)
node.multihash((err, multihash) => {
parallel([
(cb) => node.multihash(cb),
(cb) => node.size(cb)
], (err, res) => {
if (err) {
return cb(err)
return done(err)
}
node.size((err, size) => {

const multihash = res[0]
const size = res[1]

sizeIndex[mh.toB58String(multihash)] = size
ipldResolver.put({
node: node,
cid: new CID(multihash)
}, (err) => {
if (err) {
return cb(err)
source.push(new Error('failed to store dirNode'))
} else if (path) {
source.push({
path: path,
multihash: multihash,
size: size
})
}

sizeIndex[mh.toB58String(multihash)] = size
console.log('1---->', path)

ipldResolver.put({
node: node,
cid: new CID(multihash)
}, (err) => {
if (err) {
source.push(new Error('failed to store dirNode'))
} else if (path) {
console.log('2---->', path)
source.push({
path: path,
multihash: multihash,
size: size
})
}

cb(null, multihash)
})
done(null, multihash)
})
})
}
})
}

function isLeaf (value) {
Expand Down
3 changes: 1 addition & 2 deletions test/test-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function stringifyMh (files) {
}

module.exports = function (repo) {
describe.only('importer', function () {
describe('importer', function () {
let ipldResolver

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

0 comments on commit 410510d

Please sign in to comment.