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

fix: over eager preload #1693

Merged
merged 2 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/core/components/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ module.exports = function dag (self) {

if (typeof options === 'function') {
callback = options
options = {}

// Allow options in path position
if (typeof path !== 'string') {
options = path
path = null
} else {
options = {}
}
}

options = options || {}
Expand Down Expand Up @@ -156,7 +163,7 @@ module.exports = function dag (self) {
if (err) { return callback(err) }

mapAsync(res.value.links, (link, cb) => {
self.dag._getRecursive(link.multihash, cb)
self.dag._getRecursive(link.multihash, options, cb)
}, (err, nodes) => {
// console.log('nodes:', nodes)
if (err) return callback(err)
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function prepareFile (self, opts, file, callback) {
waterfall([
(cb) => opts.onlyHash
? cb(null, file)
: self.object.get(file.multihash, opts, cb),
: self.object.get(file.multihash, Object.assign({}, opts, { preload: false }), cb),
(node, cb) => {
const b58Hash = cid.toBaseEncodedString()

Expand Down Expand Up @@ -118,7 +118,7 @@ function pinFile (self, opts, file, cb) {
const isRootDir = !file.path.includes('/')
const shouldPin = pin && isRootDir && !opts.onlyHash && !opts.hashAlg
if (shouldPin) {
return self.pin.add(file.hash, err => cb(err, file))
return self.pin.add(file.hash, { preload: false }, err => cb(err, file))
} else {
cb(null, file)
}
Expand Down
6 changes: 1 addition & 5 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,7 @@ module.exports = function object (self) {
return callback(err)
}

if (options.preload !== false) {
self._preload(cid)
}

self.object.get(node.multihash, callback)
self.object.get(node.multihash, { preload: options.preload }, callback)
})
}
}),
Expand Down
10 changes: 6 additions & 4 deletions src/core/components/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ module.exports = (self) => {
add: promisify((paths, options, callback) => {
if (typeof options === 'function') {
callback = options
options = null
options = {}
}
const recursive = options ? options.recursive : true
options = options || {}

const recursive = options.recursive == null ? true : options.recursive

resolvePath(self.object, paths, (err, mhs) => {
if (err) { return callback(err) }
Expand All @@ -137,7 +139,7 @@ module.exports = (self) => {

// entire graph of nested links should be pinned,
// so make sure we have all the objects
dag._getRecursive(key, (err) => {
dag._getRecursive(key, { preload: options.preload }, (err) => {
if (err) { return cb(err) }
// found all objects, we can add the pin
return cb(null, key)
Expand All @@ -153,7 +155,7 @@ module.exports = (self) => {
}

// make sure we have the object
dag.get(new CID(multihash), (err) => {
dag.get(new CID(multihash), { preload: options.preload }, (err) => {
if (err) { return cb(err) }
// found the object, we can add the pin
return cb(null, key)
Expand Down
9 changes: 7 additions & 2 deletions test/core/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ describe('preload', () => {
const wrappingDir = res.find(file => file.path === '')
expect(wrappingDir).to.exist()

ipfs.ls(wrappingDir.hash, (err) => {
// Adding these files with have preloaded wrappingDir.hash, clear it out
MockPreloadNode.clearPreloadCids((err) => {
expect(err).to.not.exist()
MockPreloadNode.waitForCids(wrappingDir.hash, done)

ipfs.ls(wrappingDir.hash, (err) => {
expect(err).to.not.exist()
MockPreloadNode.waitForCids(wrappingDir.hash, done)
})
})
})
})
Expand Down
17 changes: 16 additions & 1 deletion test/utils/mock-preload-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,22 @@ module.exports.waitForCids = (cids, opts, cb) => {
getPreloadCids(opts.addr, (err, preloadCids) => {
if (err) return cb(err)

if (cids.every(cid => preloadCids.includes(cid))) {
// See if our cached preloadCids includes all the cids we're looking for.
const { missing, duplicates } = cids.reduce((results, cid) => {
const count = preloadCids.filter(preloadedCid => preloadedCid === cid).length
if (count === 0) {
results.missing.push(cid)
} else if (count > 1) {
results.duplicates.push(cid)
}
return results
}, { missing: [], duplicates: [] })

if (duplicates.length) {
return cb(errCode(new Error(`Multiple occurances of ${duplicates} found`), 'ERR_DUPLICATE'))
}

if (missing.length === 0) {
return cb()
}

Expand Down