Skip to content

Commit

Permalink
feat(cache): add versioning to content and index
Browse files Browse the repository at this point in the history
Fixes: #50

This will allow us to do automated migrations in the future
as the cache format changes.

BREAKING CHANGE: index/content directories are now versioned. Previous caches are no longer compatible and cannot be migrated.
  • Loading branch information
zkat committed Mar 3, 2017
1 parent 03f81ba commit 31bc549
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/content/path.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
'use strict'

var contentVer = require('../../package.json')['cache-version'].content
var path = require('path')

// Current format of content file path:
//
// ~/.my-cache/content-v1/sha512/ba/bada55deadbeefc0ffee
//
module.exports = contentPath
function contentPath (cache, address, hashAlgorithm) {
address = address && address.toLowerCase()
hashAlgorithm = hashAlgorithm ? hashAlgorithm.toLowerCase() : 'sha512'
return path.join(
cache, 'content', hashAlgorithm, address.slice(0, 2), address)
cache,
`content-v${contentVer}`,
hashAlgorithm,
address.slice(0, 2),
address
)
}
10 changes: 8 additions & 2 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const Promise = require('bluebird')
const split = require('split')
const through = require('mississippi').through

const indexV = require('../package.json')['cache-version'].index

module.exports.insert = insert
function insert (cache, key, digest, opts) {
opts = opts || {}
Expand Down Expand Up @@ -105,7 +107,7 @@ function del (cache, key) {

module.exports.lsStream = lsStream
function lsStream (cache) {
const indexDir = path.join(cache, 'index')
const indexDir = bucketDir(cache)
const stream = through.obj()
fs.readdir(indexDir, function (err, buckets) {
if (err && err.code === 'ENOENT') {
Expand Down Expand Up @@ -177,10 +179,14 @@ function notFoundError (cache, key) {
return err
}

function bucketDir (cache) {
return path.join(cache, `index-v${indexV}`)
}

module.exports._bucketPath = bucketPath
function bucketPath (cache, key) {
const hashed = hashKey(key)
return path.join(cache, 'index', hashed.slice(0, 2), hashed)
return path.join(bucketDir(cache), hashed.slice(0, 2), hashed)
}

module.exports._hashKey = hashKey
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "cacache",
"version": "5.0.3",
"cache-version": {
"content": "1",
"index": "1"
},
"description": "General content-addressable cache system that maintains a filesystem registry of file data.",
"main": "index.js",
"files": [
Expand Down

0 comments on commit 31bc549

Please sign in to comment.