-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
allow deletions of indexes, handle null integrity values in compact better #55
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ const fixOwner = require('./util/fix-owner') | |
const hashToSegments = require('./util/hash-to-segments') | ||
const indexV = require('../package.json')['cache-version'].index | ||
const moveFile = require('@npmcli/move-file') | ||
const rimraf = util.promisify(require('rimraf')) | ||
const _rimraf = require('rimraf') | ||
const rimraf = util.promisify(_rimraf) | ||
rimraf.sync = _rimraf.sync | ||
|
||
const appendFile = util.promisify(fs.appendFile) | ||
const readFile = util.promisify(fs.readFile) | ||
|
@@ -35,15 +37,31 @@ module.exports.compact = compact | |
async function compact (cache, key, matchFn, opts = {}) { | ||
const bucket = bucketPath(cache, key) | ||
const entries = await bucketEntries(bucket) | ||
// reduceRight because the bottom-most result is the newest | ||
const newEntries = [] | ||
// we loop backwards because the bottom-most result is the newest | ||
// since we add new entries with appendFile | ||
const newEntries = entries.reduceRight((acc, newEntry) => { | ||
if (!acc.find((oldEntry) => matchFn(oldEntry, newEntry))) { | ||
acc.push(newEntry) | ||
for (let i = entries.length - 1; i >= 0; --i) { | ||
const entry = entries[i] | ||
// a null integrity could mean either a delete was appended | ||
// or the user has simply stored an index that does not map | ||
// to any content. we determine if the user wants to keep the | ||
// null integrity based on the validateEntry function passed in options. | ||
// if the integrity is null and no validateEntry is provided, we break | ||
// as we consider the null integrity to be a deletion of everything | ||
// that came before it. | ||
if (entry.integrity === null && !opts.validateEntry) { | ||
break | ||
} | ||
|
||
return acc | ||
}, []) | ||
// if this entry is valid, and it is either the first entry or | ||
// the newEntries array doesn't already include an entry that | ||
// matches this one based on the provided matchFn, then we add | ||
// it to the beginning of our list | ||
if ((!opts.validateEntry || opts.validateEntry(entry) === true) && | ||
(newEntries.length === 0 || !newEntries.find((oldEntry) => matchFn(oldEntry, entry)))) { | ||
newEntries.unshift(entry) | ||
} | ||
} | ||
|
||
const newIndex = '\n' + newEntries.map((entry) => { | ||
const stringified = JSON.stringify(entry) | ||
|
@@ -85,7 +103,13 @@ async function compact (cache, key, matchFn, opts = {}) { | |
// write the file atomically | ||
await disposer(setup(), teardown, write) | ||
|
||
return newEntries.map((entry) => formatEntry(cache, entry, true)) | ||
// we reverse the list we generated such that the newest | ||
// entries come first in order to make looping through them easier | ||
// the true passed to formatEntry tells it to keep null | ||
// integrity values, if they made it this far it's because | ||
// the user specified a metadata field in keepMissingIntegrityWhenSet | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
// and as such we should return it | ||
return newEntries.reverse().map((entry) => formatEntry(cache, entry, true)) | ||
} | ||
|
||
module.exports.insert = insert | ||
|
@@ -201,14 +225,24 @@ function findSync (cache, key) { | |
|
||
module.exports.delete = del | ||
|
||
function del (cache, key, opts) { | ||
return insert(cache, key, null, opts) | ||
function del (cache, key, opts = {}) { | ||
if (!opts.removeFully) { | ||
return insert(cache, key, null, opts) | ||
} | ||
|
||
const bucket = bucketPath(cache, key) | ||
return rimraf(bucket) | ||
} | ||
|
||
module.exports.delete.sync = delSync | ||
|
||
function delSync (cache, key, opts) { | ||
return insertSync(cache, key, null, opts) | ||
function delSync (cache, key, opts = {}) { | ||
if (!opts.removeFully) { | ||
return insertSync(cache, key, null, opts) | ||
} | ||
|
||
const bucket = bucketPath(cache, key) | ||
return rimraf.sync(bucket) | ||
} | ||
|
||
module.exports.lsStream = lsStream | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, if I'm reading this correctly, adding a
null
index entry and then a new index entry, then compacting is somewhat the same as doingremoveAll
and then adding the new one? Like, thenull
means "ignore all that came before"?