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

Commit fc9eed1

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 fc9eed1

15 files changed

+169
-155
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ 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+
async () => context.ipld.get([parent.cid]).first(),
84+
(node, next) => addLink(context, {
85+
parent: node,
8686
parentCid: parent.cid,
8787
size: child.size,
8888
cid: child.cid,
@@ -165,8 +165,8 @@ const copyToDirectory = (context, sources, destination, destinationTrail, option
165165
const parent = destinationTrail[destinationTrail.length - 1]
166166

167167
waterfall([
168-
(next) => context.ipld.get(parent.cid, next),
169-
(result, next) => next(null, { cid: parent.cid, node: result.value })
168+
async () => context.ipld.get([parent.cid]).first(),
169+
(node, next) => next(null, { cid: parent.cid, node })
170170
].concat(
171171
sourceTrails.map((sourceTrail, index) => {
172172
return (parent, done) => {

src/core/utils/add-link.js

Lines changed: 18 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,8 @@ 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+
async () => context.ipld.get([options.parentCid]).first(),
47+
(node, cb) => cb(null, node),
4748
(node, cb) => addLink(context, {
4849
...options,
4950
parent: node
@@ -108,17 +109,21 @@ const addToDirectory = (context, options, callback) => {
108109
waterfall([
109110
(done) => DAGNode.rmLink(options.parent, options.name, done),
110111
(parent, done) => DAGNode.addLink(parent, new DAGLink(options.name, options.size, options.cid), done),
111-
(parent, done) => {
112+
async (parent) => {
112113
// 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, {
114+
const cid = await context.ipld.put(
115+
[parent],
116+
toMulticodecCode(options.codec),
117+
{
118+
cidVersion: options.cidVersion,
119+
hashAlg: toMulticodecCode(options.hashAlg),
120+
hashOnly: !options.flush
121+
}
122+
).first()
123+
return {
119124
node: parent,
120125
cid
121-
}))
126+
}
122127
}
123128
], callback)
124129
}
@@ -162,10 +167,9 @@ const updateShard = (context, positions, child, options, callback) => {
162167
size: child.size,
163168
multihash: child.cid.buffer
164169
}], {}, done),
165-
({ node: { links: [ shard ] } }, done) => {
166-
return context.ipld.get(shard.cid, (err, result) => {
167-
done(err, { cid: shard.cid, node: result && result.value })
168-
})
170+
async ({ node: { links: [ shard ] } }) => {
171+
const node = await context.ipld.get([shard.cid]).first()
172+
return { cid: shard.cid, node }
169173
},
170174
(result, cb) => updateShardParent(context, bucket, node, link.name, result.node, result.cid, prefix, options, cb)
171175
], cb)

src/core/utils/create-node.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@ 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+
async (node) => {
14+
const cid = await context.ipld.put(
15+
[node],
16+
toMulticodecCode(options.format),
17+
{
18+
cidVersion: options.cidVersion,
19+
hashAlg: toMulticodecCode(options.hashAlg)
20+
}
21+
).first()
22+
return {
23+
cid,
24+
node
25+
}
26+
}
2027
], callback)
2128
}
2229

src/core/utils/hamt-utils.js

Lines changed: 44 additions & 41 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
@@ -21,17 +22,21 @@ const updateHamtDirectory = (context, links, bucket, options, callback) => {
2122

2223
DAGNode.create(dir.marshal(), links, cb)
2324
},
24-
(parent, done) => {
25+
async (parent) => {
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, {
27+
const cid = await 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()
36+
return {
3237
node: parent,
3338
cid
34-
}))
39+
}
3540
}
3641
], callback)
3742
}
@@ -137,43 +142,41 @@ const generatePath = (context, fileName, rootNode, callback) => {
137142

138143
// found subshard
139144
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
145+
context.ipld.get([link.cid]).first().then(
146+
(node) => {
147+
// subshard hasn't been loaded, descend to the next level of the HAMT
148+
if (!path[index - 1]) {
149+
log(`Loaded new subshard ${segment.prefix}`)
150+
151+
return recreateHamtLevel(node.links, rootBucket, segment.bucket, parseInt(segment.prefix, 16), async (err, bucket) => {
152+
if (err) {
153+
return next(err)
154+
}
155+
156+
const position = await rootBucket._findNewBucketAndPos(fileName)
157+
158+
index++
159+
path.unshift({
160+
bucket: position.bucket,
161+
prefix: toPrefix(position.pos),
162+
node: node
163+
})
164+
165+
next()
162166
})
167+
}
163168

164-
next()
165-
})
166-
}
169+
const nextSegment = path[index - 1]
167170

168-
const nextSegment = path[index - 1]
171+
// add intermediate links to bucket
172+
addLinksToHamtBucket(node.links, nextSegment.bucket, rootBucket, (error) => {
173+
nextSegment.node = node
169174

170-
// add intermediate links to bucket
171-
addLinksToHamtBucket(result.value.links, nextSegment.bucket, rootBucket, (error) => {
172-
nextSegment.node = result.value
173-
174-
next(error)
175-
})
176-
})
175+
next(error)
176+
})
177+
},
178+
(error) => next(error)
179+
)
177180
},
178181
async (err, path) => {
179182
await rootBucket.put(fileName, true)

src/core/utils/load-node.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const loadNode = (context, dagLink, callback) => {
1010
log(`Loading DAGNode for child ${cid.toBaseEncodedString()}`)
1111

1212
waterfall([
13-
(cb) => context.ipld.get(cid, cb),
14-
(result, cb) => cb(null, {
15-
node: result.value,
13+
async () => context.ipld.get([cid]).first(),
14+
(node, cb) => cb(null, {
15+
node,
1616
cid
1717
})
1818
], callback)

src/core/utils/remove-link.js

Lines changed: 26 additions & 17 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,8 @@ 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+
async () => context.ipld.get([options.parentCid]).first(),
44+
(node, cb) => cb(null, node),
4445
(node, cb) => removeLink(context, {
4546
...options,
4647
parent: node
@@ -68,15 +69,19 @@ const removeLink = (context, options, callback) => {
6869
const removeFromDirectory = (context, options, callback) => {
6970
waterfall([
7071
(cb) => DAGNode.rmLink(options.parent, options.name, cb),
71-
(newParentNode, cb) => {
72-
context.ipld.put(newParentNode, {
73-
version: options.cidVersion,
74-
format: options.codec,
75-
hashAlg: options.hashAlg
76-
}, (error, cid) => cb(error, {
72+
async (newParentNode) => {
73+
const cid = await context.ipld.put(
74+
[newParentNode],
75+
toMulticodecCode(options.codec),
76+
{
77+
cidVersion: options.cidVersion,
78+
hashAlg: toMulticodecCode(options.hashAlg)
79+
}
80+
).first()
81+
return {
7782
node: newParentNode,
7883
cid
79-
}))
84+
}
8085
},
8186
(result, cb) => {
8287
log('Updated regular directory', result.cid.toBaseEncodedString())
@@ -129,16 +134,20 @@ const updateShard = (context, positions, child, options, callback) => {
129134

130135
return waterfall([
131136
(done) => DAGNode.rmLink(node, link.name, done),
132-
(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, {
137+
async (node) => {
138+
const cid = await context.ipld.put(
139+
[node],
140+
toMulticodecCode(options.codec),
141+
{
142+
cidVersion: options.cidVersion,
143+
hashAlg: toMulticodecCode(options.hashAlg),
144+
hashOnly: !options.flush
145+
}
146+
).first()
147+
return {
139148
node,
140149
cid
141-
}))
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ 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+
async () => {
16+
return context.ipld.get(trail.map(node => node.cid)).all()
17+
},
1618
(nodes, cb) => {
1719
let index = trail.length - 1
1820

0 commit comments

Comments
 (0)