Merge branch 'stable' into beta

This commit is contained in:
Max Brunsfeld 2016-01-25 10:29:29 -08:00
commit c4adc38273
2 changed files with 40 additions and 5 deletions

View File

@ -1,4 +1,6 @@
temp = require 'temp'
path = require 'path'
fs = require 'fs-plus'
FileSystemBlobStore = require '../src/file-system-blob-store'
describe "FileSystemBlobStore", ->
@ -77,3 +79,27 @@ describe "FileSystemBlobStore", ->
expect(blobStore.get("b", "invalidation-key-2")).toBeUndefined()
expect(blobStore.get("b", "invalidation-key-3")).toBeUndefined()
expect(blobStore.get("c", "invalidation-key-4")).toBeUndefined()
it "ignores errors when loading an invalid blob store", ->
blobStore.set("a", "invalidation-key-1", new Buffer("a"))
blobStore.set("b", "invalidation-key-2", new Buffer("b"))
blobStore.save()
# Simulate corruption
fs.writeFileSync(path.join(storageDirectory, "MAP"), new Buffer([0]))
fs.writeFileSync(path.join(storageDirectory, "INVKEYS"), new Buffer([0]))
fs.writeFileSync(path.join(storageDirectory, "BLOB"), new Buffer([0]))
blobStore = FileSystemBlobStore.load(storageDirectory)
expect(blobStore.get("a", "invalidation-key-1")).toBeUndefined()
expect(blobStore.get("b", "invalidation-key-2")).toBeUndefined()
blobStore.set("a", "invalidation-key-1", new Buffer("x"))
blobStore.set("b", "invalidation-key-2", new Buffer("y"))
blobStore.save()
blobStore = FileSystemBlobStore.load(storageDirectory)
expect(blobStore.get("a", "invalidation-key-1")).toEqual(new Buffer("x"))
expect(blobStore.get("b", "invalidation-key-2")).toEqual(new Buffer("y"))

View File

@ -12,12 +12,16 @@ class FileSystemBlobStore {
}
constructor (directory) {
this.inMemoryBlobs = new Map()
this.invalidationKeys = {}
this.blobFilename = path.join(directory, 'BLOB')
this.blobMapFilename = path.join(directory, 'MAP')
this.invalidationKeysFilename = path.join(directory, 'INVKEYS')
this.lockFilename = path.join(directory, 'LOCK')
this.reset()
}
reset () {
this.inMemoryBlobs = new Map()
this.invalidationKeys = {}
this.storedBlob = new Buffer(0)
this.storedBlobMap = {}
}
@ -32,9 +36,14 @@ class FileSystemBlobStore {
if (!fs.existsSync(this.invalidationKeysFilename)) {
return
}
this.storedBlob = fs.readFileSync(this.blobFilename)
this.storedBlobMap = JSON.parse(fs.readFileSync(this.blobMapFilename))
this.invalidationKeys = JSON.parse(fs.readFileSync(this.invalidationKeysFilename))
try {
this.storedBlob = fs.readFileSync(this.blobFilename)
this.storedBlobMap = JSON.parse(fs.readFileSync(this.blobMapFilename))
this.invalidationKeys = JSON.parse(fs.readFileSync(this.invalidationKeysFilename))
} catch (e) {
this.reset()
}
}
save () {