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

Commit 94ce67e

Browse files
committed
refactor: use new IPLD API
This is part of the Awesome Endeavour: Async Iterators: ipfs/js-ipfs#1670
1 parent daee4b7 commit 94ce67e

15 files changed

+181
-163
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"detect-node": "^2.0.4",
4343
"detect-webworker": "^1.0.0",
4444
"dirty-chai": "^2.0.1",
45-
"ipld": "~0.20.0",
45+
"ipld": "git+https://github.com/ipld/js-ipld.git#new-api-impl",
4646
"ipld-in-memory": "^2.0.0",
4747
"multihashes": "~0.4.14",
4848
"pull-buffer-stream": "^1.0.0",
@@ -57,14 +57,15 @@
5757
"interface-datastore": "~0.6.0",
5858
"ipfs-multipart": "~0.1.0",
5959
"ipfs-unixfs": "~0.1.16",
60-
"ipfs-unixfs-importer": "~0.38.0",
61-
"ipfs-unixfs-exporter": "~0.35.5",
60+
"ipfs-unixfs-exporter": "git+https://github.com/ipfs/js-ipfs-unixfs-exporter.git#new-ipld-api",
61+
"ipfs-unixfs-importer": "git+https://github.com/ipfs/js-ipfs-unixfs-importer.git#new-ipld-api",
6262
"ipld-dag-pb": "~0.15.0",
6363
"is-pull-stream": "~0.0.0",
6464
"is-stream": "^1.1.0",
6565
"joi": "^14.0.4",
6666
"joi-browser": "^13.4.0",
6767
"mortice": "^1.2.1",
68+
"multicodec": "~0.5.0",
6869
"once": "^1.4.0",
6970
"promisify-es6": "^1.0.3",
7071
"pull-cat": "^1.1.11",

src/core/cp.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ const copyToFile = (context, source, destination, destinationTrail, options, cal
8080
const child = sourceTrail[sourceTrail.length - 1]
8181

8282
waterfall([
83-
(next) => context.ipld.get(parent.cid, next),
84-
(result, next) => addLink(context, {
85-
parent: result.value,
83+
(next) => context.ipld.get([parent.cid]).first().then(
84+
(node) => next(null, node),
85+
(error) => next(error)
86+
),
87+
(node, next) => addLink(context, {
88+
parent: node,
8689
parentCid: parent.cid,
8790
size: child.size,
8891
cid: child.cid,
@@ -165,8 +168,10 @@ const copyToDirectory = (context, sources, destination, destinationTrail, option
165168
const parent = destinationTrail[destinationTrail.length - 1]
166169

167170
waterfall([
168-
(next) => context.ipld.get(parent.cid, next),
169-
(result, next) => next(null, { cid: parent.cid, node: result.value })
171+
(next) => context.ipld.get([parent.cid]).first().then(
172+
(node) => next(null, { cid: parent.cid, node }),
173+
(error) => next(error)
174+
)
170175
].concat(
171176
sourceTrails.map((sourceTrail, index) => {
172177
return (parent, done) => {

src/core/utils/add-link.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
generatePath,
1515
updateHamtDirectory
1616
} = require('./hamt-utils')
17+
const toMulticodecCode = require('./to-multicodec-code')
1718

1819
const defaultOptions = {
1920
parent: undefined,
@@ -42,8 +43,10 @@ const addLink = (context, options, callback) => {
4243
log('Loading parent node', options.parentCid.toBaseEncodedString())
4344

4445
return waterfall([
45-
(cb) => context.ipld.get(options.parentCid, cb),
46-
(result, cb) => cb(null, result.value),
46+
(cb) => context.ipld.get([options.parentCid]).first().then(
47+
(node) => cb(null, node),
48+
(error) => cb(error)
49+
),
4750
(node, cb) => addLink(context, {
4851
...options,
4952
parent: node
@@ -110,15 +113,18 @@ const addToDirectory = (context, options, callback) => {
110113
(parent, done) => DAGNode.addLink(parent, new DAGLink(options.name, options.size, options.cid), done),
111114
(parent, done) => {
112115
// Persist the new parent DAGNode
113-
context.ipld.put(parent, {
114-
version: options.cidVersion,
115-
format: options.codec,
116-
hashAlg: options.hashAlg,
117-
hashOnly: !options.flush
118-
}, (error, cid) => done(error, {
119-
node: parent,
120-
cid
121-
}))
116+
context.ipld.put(
117+
[parent],
118+
toMulticodecCode(options.codec),
119+
{
120+
cidVersion: options.cidVersion,
121+
hashAlg: toMulticodecCode(options.hashAlg),
122+
hashOnly: !options.flush
123+
}
124+
).first().then(
125+
(cid) => done(null, { node: parent, cid }),
126+
(error) => done(error)
127+
)
122128
}
123129
], callback)
124130
}
@@ -163,9 +169,10 @@ const updateShard = (context, positions, child, options, callback) => {
163169
multihash: child.cid.buffer
164170
}], {}, done),
165171
({ node: { links: [ shard ] } }, done) => {
166-
return context.ipld.get(shard.cid, (err, result) => {
167-
done(err, { cid: shard.cid, node: result && result.value })
168-
})
172+
context.ipld.get([shard.cid]).first().then(
173+
(node) => done(null, { cid: shard.cid, node }),
174+
(error) => done(error)
175+
)
169176
},
170177
(result, cb) => updateShardParent(context, bucket, node, link.name, result.node, result.cid, prefix, options, cb)
171178
], cb)

src/core/utils/create-node.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ const UnixFS = require('ipfs-unixfs')
55
const {
66
DAGNode
77
} = require('ipld-dag-pb')
8+
const toMulticodecCode = require('./to-multicodec-code')
89

910
const createNode = (context, type, options, callback) => {
1011
waterfall([
1112
(done) => DAGNode.create(new UnixFS(type).marshal(), [], done),
12-
(node, done) => context.ipld.put(node, {
13-
version: options.cidVersion,
14-
format: options.format,
15-
hashAlg: options.hashAlg
16-
}, (err, cid) => done(err, {
17-
cid,
18-
node
19-
}))
13+
(node, done) => context.ipld.put(
14+
[node],
15+
toMulticodecCode(options.format),
16+
{
17+
cidVersion: options.cidVersion,
18+
hashAlg: toMulticodecCode(options.hashAlg)
19+
}
20+
).first().then(
21+
(cid) => done(null, { cid, node }),
22+
(error) => done(error)
23+
)
2024
], callback)
2125
}
2226

src/core/utils/hamt-utils.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Bucket = require('hamt-sharding/src/bucket')
99
const DirSharded = require('ipfs-unixfs-importer/src/importer/dir-sharded')
1010
const log = require('debug')('ipfs:mfs:core:utils:hamt-utils')
1111
const UnixFS = require('ipfs-unixfs')
12+
const toMulticodecCode = require('./to-multicodec-code')
1213

1314
const updateHamtDirectory = (context, links, bucket, options, callback) => {
1415
// update parent with new bit field
@@ -23,15 +24,18 @@ const updateHamtDirectory = (context, links, bucket, options, callback) => {
2324
},
2425
(parent, done) => {
2526
// Persist the new parent DAGNode
26-
context.ipld.put(parent, {
27-
version: options.cidVersion,
28-
format: options.codec,
29-
hashAlg: options.hashAlg,
30-
hashOnly: !options.flush
31-
}, (error, cid) => done(error, {
32-
node: parent,
33-
cid
34-
}))
27+
context.ipld.put(
28+
[parent],
29+
toMulticodecCode(options.codec),
30+
{
31+
cidVersion: options.cidVersion,
32+
hashAlg: toMulticodecCode(options.hashAlg),
33+
hashOnly: !options.flush
34+
}
35+
).first().then(
36+
(cid) => done(null, { cid, node: parent }),
37+
(error) => done(error)
38+
)
3539
}
3640
], callback)
3741
}
@@ -137,43 +141,41 @@ const generatePath = (context, fileName, rootNode, callback) => {
137141

138142
// found subshard
139143
log(`Found subshard ${segment.prefix}`)
140-
context.ipld.get(link.cid, (err, result) => {
141-
if (err) {
142-
return next(err)
143-
}
144-
145-
// subshard hasn't been loaded, descend to the next level of the HAMT
146-
if (!path[index - 1]) {
147-
log(`Loaded new subshard ${segment.prefix}`)
148-
const node = result.value
149-
150-
return recreateHamtLevel(node.links, rootBucket, segment.bucket, parseInt(segment.prefix, 16), async (err, bucket) => {
151-
if (err) {
152-
return next(err)
153-
}
154-
155-
const position = await rootBucket._findNewBucketAndPos(fileName)
156-
157-
index++
158-
path.unshift({
159-
bucket: position.bucket,
160-
prefix: toPrefix(position.pos),
161-
node: node
144+
context.ipld.get([link.cid]).first().then(
145+
(node) => {
146+
// subshard hasn't been loaded, descend to the next level of the HAMT
147+
if (!path[index - 1]) {
148+
log(`Loaded new subshard ${segment.prefix}`)
149+
150+
return recreateHamtLevel(node.links, rootBucket, segment.bucket, parseInt(segment.prefix, 16), async (err, bucket) => {
151+
if (err) {
152+
return next(err)
153+
}
154+
155+
const position = await rootBucket._findNewBucketAndPos(fileName)
156+
157+
index++
158+
path.unshift({
159+
bucket: position.bucket,
160+
prefix: toPrefix(position.pos),
161+
node: node
162+
})
163+
164+
next()
162165
})
166+
}
163167

164-
next()
165-
})
166-
}
167-
168-
const nextSegment = path[index - 1]
168+
const nextSegment = path[index - 1]
169169

170-
// add intermediate links to bucket
171-
addLinksToHamtBucket(result.value.links, nextSegment.bucket, rootBucket, (error) => {
172-
nextSegment.node = result.value
170+
// add intermediate links to bucket
171+
addLinksToHamtBucket(node.links, nextSegment.bucket, rootBucket, (error) => {
172+
nextSegment.node = node
173173

174-
next(error)
175-
})
176-
})
174+
next(error)
175+
})
176+
},
177+
(error) => next(error)
178+
)
177179
},
178180
async (err, path) => {
179181
await rootBucket.put(fileName, true)

src/core/utils/load-node.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const waterfall = require('async/waterfall')
43
const CID = require('cids')
54
const log = require('debug')('ipfs:mfs:utils:load-node')
65

@@ -9,13 +8,10 @@ const loadNode = (context, dagLink, callback) => {
98

109
log(`Loading DAGNode for child ${cid.toBaseEncodedString()}`)
1110

12-
waterfall([
13-
(cb) => context.ipld.get(cid, cb),
14-
(result, cb) => cb(null, {
15-
node: result.value,
16-
cid
17-
})
18-
], callback)
11+
context.ipld.get([cid]).first().then(
12+
(node) => callback(null, { cid, node }),
13+
(error) => callback(error)
14+
)
1915
}
2016

2117
module.exports = loadNode

src/core/utils/remove-link.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
generatePath,
1313
updateHamtDirectory
1414
} = require('./hamt-utils')
15+
const toMulticodecCode = require('./to-multicodec-code')
1516

1617
const defaultOptions = {
1718
parent: undefined,
@@ -39,8 +40,10 @@ const removeLink = (context, options, callback) => {
3940
log('Loading parent node', options.parentCid.toBaseEncodedString())
4041

4142
return waterfall([
42-
(cb) => context.ipld.get(options.parentCid, cb),
43-
(result, cb) => cb(null, result.value),
43+
(cb) => context.ipld.get([options.parentCid]).first().then(
44+
(node) => cb(null, node),
45+
(error) => cb(error)
46+
),
4447
(node, cb) => removeLink(context, {
4548
...options,
4649
parent: node
@@ -69,14 +72,17 @@ const removeFromDirectory = (context, options, callback) => {
6972
waterfall([
7073
(cb) => DAGNode.rmLink(options.parent, options.name, cb),
7174
(newParentNode, cb) => {
72-
context.ipld.put(newParentNode, {
73-
version: options.cidVersion,
74-
format: options.codec,
75-
hashAlg: options.hashAlg
76-
}, (error, cid) => cb(error, {
77-
node: newParentNode,
78-
cid
79-
}))
75+
context.ipld.put(
76+
[newParentNode],
77+
toMulticodecCode(options.codec),
78+
{
79+
cidVersion: options.cidVersion,
80+
hashAlg: toMulticodecCode(options.hashAlg)
81+
}
82+
).first().then(
83+
(cid) => cb(null, { cid, node: newParentNode }),
84+
(error) => cb(error)
85+
)
8086
},
8187
(result, cb) => {
8288
log('Updated regular directory', result.cid.toBaseEncodedString())
@@ -130,15 +136,18 @@ const updateShard = (context, positions, child, options, callback) => {
130136
return waterfall([
131137
(done) => DAGNode.rmLink(node, link.name, done),
132138
(node, done) => {
133-
context.ipld.put(node, {
134-
version: options.cidVersion,
135-
format: options.codec,
136-
hashAlg: options.hashAlg,
137-
hashOnly: !options.flush
138-
}, (error, cid) => done(error, {
139-
node,
140-
cid
141-
}))
139+
context.ipld.put(
140+
[node],
141+
toMulticodecCode(options.codec),
142+
{
143+
cidVersion: options.cidVersion,
144+
hashAlg: toMulticodecCode(options.hashAlg),
145+
hashOnly: !options.flush
146+
}
147+
).first().then(
148+
(cid) => done(null, { cid, node }),
149+
(error) => done(error)
150+
)
142151
},
143152
(result, done) => {
144153
bucket.del(child.name)

src/core/utils/to-multicodec-code.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const multicodec = require('multicodec')
4+
5+
// Converts a multicodec name to the corresponding code if it isn't a code
6+
// already
7+
const toMulticodecCode = (name) => {
8+
if (typeof name === 'string') {
9+
const constantName = name.toUpperCase().replace(/-/g, '_')
10+
return multicodec[constantName]
11+
} else {
12+
return name
13+
}
14+
}
15+
16+
module.exports = toMulticodecCode

src/core/utils/update-tree.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const updateTree = (context, trail, options, callback) => {
1212
options = Object.assign({}, defaultOptions, options)
1313

1414
waterfall([
15-
(cb) => context.ipld.getMany(trail.map(node => node.cid), cb),
15+
(cb) => context.ipld.get(trail.map(node => node.cid)).all().then(
16+
(nodes) => cb(null, nodes),
17+
(error) => cb(error)
18+
),
1619
(nodes, cb) => {
1720
let index = trail.length - 1
1821

0 commit comments

Comments
 (0)