Rename to FileSystemBlobStore

This commit is contained in:
Antonio Scandurra
2015-10-30 18:42:38 +01:00
parent f8c2c912e3
commit abcfc8579b
7 changed files with 187 additions and 187 deletions

View File

@@ -0,0 +1,94 @@
'use strict'
const fs = require('fs-plus')
const path = require('path')
module.exports =
class FileSystemBlobStore {
static load (directory) {
let instance = new FileSystemBlobStore(directory)
instance.load()
return instance
}
constructor (directory) {
this.inMemoryBlobs = new Map()
this.blobFilename = path.join(directory, 'BLOB')
this.mapFilename = path.join(directory, 'MAP')
this.storedBlob = new Buffer(0)
this.storedMap = {}
}
load () {
if (!fs.existsSync(this.mapFilename)) {
return
}
if (!fs.existsSync(this.blobFilename)) {
return
}
this.storedBlob = fs.readFileSync(this.blobFilename)
this.storedMap = JSON.parse(fs.readFileSync(this.mapFilename))
}
save () {
let dump = this.getDump()
let cacheBlob = Buffer.concat(dump[0])
let cacheMap = JSON.stringify(dump[1])
fs.writeFileSync(this.blobFilename, cacheBlob)
fs.writeFileSync(this.mapFilename, cacheMap)
}
has (key) {
return this.inMemoryBlobs.hasOwnProperty(key) || this.storedMap.hasOwnProperty(key)
}
get (key) {
return this.getFromMemory(key) || this.getFromStorage(key)
}
set (key, buffer) {
return this.inMemoryBlobs.set(key, buffer)
}
delete (key) {
this.inMemoryBlobs.delete(key)
delete this.storedMap[key]
}
getFromMemory (key) {
return this.inMemoryBlobs.get(key)
}
getFromStorage (key) {
if (this.storedMap[key] == null) {
return
}
return this.storedBlob.slice.apply(this.storedBlob, this.storedMap[key])
}
getDump () {
let buffers = []
let cacheMap = {}
let currentBufferStart = 0
function dump (key, getBufferByKey) {
let buffer = getBufferByKey(key)
buffers.push(buffer)
cacheMap[key] = [currentBufferStart, currentBufferStart + buffer.length]
currentBufferStart += buffer.length
}
for (let key of this.inMemoryBlobs.keys()) {
dump(key, this.getFromMemory.bind(this))
}
for (let key of Object.keys(this.storedMap)) {
if (!cacheMap[key]) {
dump(key, this.getFromStorage.bind(this))
}
}
return [buffers, cacheMap]
}
}

View File

@@ -1,94 +0,0 @@
'use strict'
const fs = require('fs-plus')
const path = require('path')
module.exports =
class FileSystemCacheBlobStorage {
static load (directory) {
let instance = new FileSystemCacheBlobStorage(directory)
instance.load()
return instance
}
constructor (directory) {
this.inMemoryCache = new Map()
this.cacheBlobFilename = path.join(directory, 'v8-compile-cache.blob')
this.cacheMapFilename = path.join(directory, 'v8-compile-cache.map')
this.storedCacheBlob = new Buffer(0)
this.storedCacheMap = {}
}
load () {
if (!fs.existsSync(this.cacheMapFilename)) {
return
}
if (!fs.existsSync(this.cacheBlobFilename)) {
return
}
this.storedCacheBlob = fs.readFileSync(this.cacheBlobFilename)
this.storedCacheMap = JSON.parse(fs.readFileSync(this.cacheMapFilename))
}
save () {
let dump = this.getDump()
let cacheBlob = Buffer.concat(dump[0])
let cacheMap = JSON.stringify(dump[1])
fs.writeFileSync(this.cacheBlobFilename, cacheBlob)
fs.writeFileSync(this.cacheMapFilename, cacheMap)
}
has (key) {
return this.inMemoryCache.hasOwnProperty(key) || this.storedCacheMap.hasOwnProperty(key)
}
get (key) {
return this.getFromMemory(key) || this.getFromStorage(key)
}
set (key, buffer) {
return this.inMemoryCache.set(key, buffer)
}
delete (key) {
this.inMemoryCache.delete(key)
delete this.storedCacheMap[key]
}
getFromMemory (key) {
return this.inMemoryCache.get(key)
}
getFromStorage (key) {
if (this.storedCacheMap[key] == null) {
return
}
return this.storedCacheBlob.slice.apply(this.storedCacheBlob, this.storedCacheMap[key])
}
getDump () {
let buffers = []
let cacheMap = {}
let currentBufferStart = 0
function dump (key, getBufferByKey) {
let buffer = getBufferByKey(key)
buffers.push(buffer)
cacheMap[key] = [currentBufferStart, currentBufferStart + buffer.length]
currentBufferStart += buffer.length
}
for (let key of this.inMemoryCache.keys()) {
dump(key, this.getFromMemory.bind(this))
}
for (let key of Object.keys(this.storedCacheMap)) {
if (!cacheMap[key]) {
dump(key, this.getFromStorage.bind(this))
}
}
return [buffers, cacheMap]
}
}

View File

@@ -6,12 +6,12 @@ const cachedVm = require('cached-run-in-this-context')
class NativeCompileCache {
constructor () {
this.cacheStorage = null
this.cacheStore = null
this.previousModuleCompile = null
}
setCacheStorage (storage) {
this.cacheStorage = storage
setCacheStore (store) {
this.cacheStore = store
}
install () {
@@ -28,7 +28,7 @@ class NativeCompileCache {
}
overrideModuleCompile () {
let cacheStorage = this.cacheStorage
let cacheStore = this.cacheStore
let resolvedArgv = null
Module.prototype._compile = function (content, filename) {
let self = this
@@ -52,17 +52,17 @@ class NativeCompileCache {
let wrapper = Module.wrap(content)
let compiledWrapper = null
if (cacheStorage.has(filename)) {
let buffer = cacheStorage.get(filename)
if (cacheStore.has(filename)) {
let buffer = cacheStore.get(filename)
let compilationResult = cachedVm.runInThisContextCached(wrapper, filename, buffer)
compiledWrapper = compilationResult.result
if (compilationResult.wasRejected) {
cacheStorage.delete(filename)
cacheStore.delete(filename)
}
} else {
let compilationResult = cachedVm.runInThisContext(wrapper, filename)
if (compilationResult.cacheBuffer) {
cacheStorage.set(filename, compilationResult.cacheBuffer)
cacheStore.set(filename, compilationResult.cacheBuffer)
}
compiledWrapper = compilationResult.result
}