Skip to content

Commit

Permalink
fix(index): change index filenames to sha1s of keys
Browse files Browse the repository at this point in the history
Fixes: #41

BREAKING CHANGE: The index format has changed and previous caches are no longer compatible. Existing caches will need to be regenerated.
  • Loading branch information
zkat committed Mar 2, 2017
1 parent ea00ba6 commit bbc5fca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const asyncMap = require('slide/lib/async-map')
const contentPath = require('./content/path')
const crypto = require('crypto')
const fixOwner = require('./util/fix-owner')
const fs = require('graceful-fs')
const lockfile = require('lockfile')
Expand Down Expand Up @@ -170,8 +171,17 @@ function indexPath (cache, key) {

module.exports._hashKey = hashKey
function hashKey (key) {
// relatively readable key. Conflicts handled by buckets.
return key.replace(/[^a-z0-9_-]+/ig, '_').toLowerCase().slice(0, 120)
// sha1 conflicts can be generated, but it doesn't matter in this case,
// since we intend for there to be regular conflicts anyway. You can have
// the entire cache in a single bucket and all that'll do is just make a big
// file with a lot of contention, if you can even pull it off in the `key`
// string. So whatever. `sha1` is faster and it doesn't trigger the warnings
// `md5` tends to (yet?...).
return crypto
.createHash('sha1')
.update(key.toLowerCase()) // lump case-variant keys into same bucket.
.digest('hex')
.slice(0, 7)
}

function formatEntry (cache, entry) {
Expand Down
6 changes: 3 additions & 3 deletions test/index.insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ test('key case-sensitivity', function (t) {
test('hash conflict in same bucket', function (t) {
// NOTE - this test will break if `index._hashKey` changes its algorithm.
// Adapt to it accordingly.
const NEWKEY = KEY + '!'
const CONFLICTING = KEY + '!!!'
const NEWKEY = KEY.toUpperCase()
const CONFLICTING = KEY.toLowerCase()
return index.insert(
CACHE, NEWKEY, DIGEST
).then(() => (
Expand All @@ -157,7 +157,7 @@ test('hash conflict in same bucket', function (t) {
key: NEWKEY,
digest: DIGEST
}, {
key: KEY + '!!!',
key: CONFLICTING,
digest: DIGEST
}], 'multiple entries for conflicting keys in the same bucket')
})
Expand Down

0 comments on commit bbc5fca

Please sign in to comment.