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

Commit 6b45fea

Browse files
committed
refactor: pin set walking
1 parent 232fc39 commit 6b45fea

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

src/core/components/pin/pin-set.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,15 @@ exports = module.exports = function (dag) {
231231
dag.get(link.Hash, '', { preload: false }, (err, res) => {
232232
if (err) { return callback(err) }
233233
const keys = []
234-
const step = link => keys.push(link.Hash.buffer)
235-
pinSet.walkItems(res.value, step, err => {
234+
const stepPin = link => keys.push(link.Hash.buffer)
235+
pinSet.walkItems(res.value, { stepPin }, err => {
236236
if (err) { return callback(err) }
237237
return callback(null, keys)
238238
})
239239
})
240240
},
241241

242-
walkItems: (node, step, callback) => {
243-
pinSet.walkAll(node, step, () => {}, callback)
244-
},
245-
246-
walkAll: (node, stepPin, stepBin, callback) => {
242+
walkItems: (node, { stepPin = () => {}, stepBin = () => {} }, callback) => {
247243
let pbh
248244
try {
249245
pbh = readHeader(node)
@@ -263,7 +259,7 @@ exports = module.exports = function (dag) {
263259
// walk the links of this fanout bin
264260
return dag.get(linkHash, '', { preload: false }, (err, res) => {
265261
if (err) { return eachCb(err) }
266-
pinSet.walkAll(res.value, stepPin, stepBin, eachCb)
262+
pinSet.walkItems(res.value, { stepPin, stepBin }, eachCb)
267263
})
268264
}
269265
} else {
@@ -279,14 +275,14 @@ exports = module.exports = function (dag) {
279275
// "Empty block" used by the pinner
280276
const cids = [new CID(emptyKey)]
281277

282-
const step = link => cids.push(link.Hash)
278+
const stepBin = link => cids.push(link.Hash)
283279
eachSeries(rootNode.Links, (topLevelLink, cb) => {
284280
cids.push(topLevelLink.Hash)
285281

286282
dag.get(topLevelLink.Hash, '', { preload: false }, (err, res) => {
287283
if (err) { return cb(err) }
288284

289-
pinSet.walkAll(res.value, () => {}, step, cb)
285+
pinSet.walkItems(res.value, { stepBin }, cb)
290286
})
291287
}, (err) => callback(err, cids))
292288
}

test/core/pin-set.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ describe('pinSet', function () {
181181
})
182182
})
183183

184-
describe('walkAll', function () {
184+
describe('walkItems', function () {
185185
it(`fails if node doesn't have a pin-set protobuf header`, function (done) {
186186
createNode('datum', (err, node) => {
187187
expect(err).to.not.exist()
188188

189-
pinSet.walkAll(node, () => {}, () => {}, (err, res) => {
189+
pinSet.walkItems(node, {}, (err, res) => {
190190
expect(err).to.exist()
191191
expect(res).to.not.exist()
192192
done()
@@ -198,17 +198,17 @@ describe('pinSet', function () {
198198
this.timeout(90 * 1000)
199199

200200
const seenPins = []
201-
const walkerPin = (link, idx, data) => seenPins.push({ link, idx, data })
201+
const stepPin = (link, idx, data) => seenPins.push({ link, idx, data })
202202
const seenBins = []
203-
const walkerBin = (link, idx, data) => seenBins.push({ link, idx, data })
203+
const stepBin = (link, idx, data) => seenBins.push({ link, idx, data })
204204

205205
createNodes(maxItems + 1, (err, nodes) => {
206206
expect(err).to.not.exist()
207207

208208
pinSet.storeSet(nodes, (err, result) => {
209209
expect(err).to.not.exist()
210210

211-
pinSet.walkAll(result.node, walkerPin, walkerBin, err => {
211+
pinSet.walkItems(result.node, { stepPin, stepBin }, err => {
212212
expect(err).to.not.exist()
213213
expect(seenPins).to.have.length(maxItems + 1)
214214
expect(seenBins).to.have.length(defaultFanout)
@@ -217,20 +217,18 @@ describe('pinSet', function () {
217217
})
218218
})
219219
})
220-
})
221220

222-
describe('walkItems', function () {
223221
it('visits all non-fanout links of a root node', function (done) {
224222
const seen = []
225-
const walker = (link, idx, data) => seen.push({ link, idx, data })
223+
const stepPin = (link, idx, data) => seen.push({ link, idx, data })
226224

227225
createNodes(defaultFanout, (err, nodes) => {
228226
expect(err).to.not.exist()
229227

230228
pinSet.storeSet(nodes, (err, result) => {
231229
expect(err).to.not.exist()
232230

233-
pinSet.walkItems(result.node, walker, err => {
231+
pinSet.walkItems(result.node, { stepPin }, err => {
234232
expect(err).to.not.exist()
235233
expect(seen).to.have.length(defaultFanout)
236234
expect(seen[0].idx).to.eql(defaultFanout)

0 commit comments

Comments
 (0)