2014-05-23 18:36:23 +00:00
|
|
|
var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
|
2014-07-21 19:08:39 +00:00
|
|
|
// NOTE: prefer setImmediate to nextTick for proper recursion yielding.
|
|
|
|
// see https://github.com/js-platform/filer/pull/24
|
|
|
|
var asyncCallback = require('../../lib/async.js').setImmediate;
|
2014-01-21 21:25:09 +00:00
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
/**
|
|
|
|
* Make shared in-memory DBs possible when using the same name.
|
|
|
|
*/
|
|
|
|
var createDB = (function() {
|
|
|
|
var pool = {};
|
|
|
|
return function getOrCreate(name) {
|
2014-08-21 00:53:01 +00:00
|
|
|
if(!pool.hasOwnProperty(name)) {
|
2014-05-23 18:36:23 +00:00
|
|
|
pool[name] = {};
|
|
|
|
}
|
2014-08-21 00:53:01 +00:00
|
|
|
return pool[name];
|
2014-05-23 18:36:23 +00:00
|
|
|
};
|
|
|
|
}());
|
2014-05-22 17:37:15 +00:00
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
function MemoryContext(db, readOnly) {
|
|
|
|
this.readOnly = readOnly;
|
|
|
|
this.objectStore = db;
|
|
|
|
}
|
2014-08-16 20:22:41 +00:00
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
MemoryContext.prototype.clear = function(callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
asyncCallback(function() {
|
|
|
|
callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
});
|
|
|
|
return;
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2014-05-23 18:36:23 +00:00
|
|
|
var objectStore = this.objectStore;
|
|
|
|
Object.keys(objectStore).forEach(function(key){
|
|
|
|
delete objectStore[key];
|
|
|
|
});
|
|
|
|
asyncCallback(callback);
|
|
|
|
};
|
2014-08-16 20:22:41 +00:00
|
|
|
|
|
|
|
// Memory context doesn't care about differences between Object and Buffer
|
|
|
|
MemoryContext.prototype.getObject =
|
|
|
|
MemoryContext.prototype.getBuffer =
|
|
|
|
function(key, callback) {
|
2014-05-23 18:36:23 +00:00
|
|
|
var that = this;
|
|
|
|
asyncCallback(function() {
|
|
|
|
callback(null, that.objectStore[key]);
|
|
|
|
});
|
|
|
|
};
|
2014-08-16 20:22:41 +00:00
|
|
|
MemoryContext.prototype.putObject =
|
|
|
|
MemoryContext.prototype.putBuffer =
|
|
|
|
function(key, value, callback) {
|
2014-05-23 18:36:23 +00:00
|
|
|
if(this.readOnly) {
|
|
|
|
asyncCallback(function() {
|
|
|
|
callback("[MemoryContext] Error: write operation on read only context");
|
2013-11-22 21:12:31 +00:00
|
|
|
});
|
2014-05-23 18:36:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.objectStore[key] = value;
|
|
|
|
asyncCallback(callback);
|
|
|
|
};
|
2014-08-16 20:22:41 +00:00
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
MemoryContext.prototype.delete = function(key, callback) {
|
|
|
|
if(this.readOnly) {
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(function() {
|
2014-05-23 18:36:23 +00:00
|
|
|
callback("[MemoryContext] Error: write operation on read only context");
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|
2014-05-23 18:36:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete this.objectStore[key];
|
|
|
|
asyncCallback(callback);
|
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
|
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
function Memory(name) {
|
|
|
|
this.name = name || FILE_SYSTEM_NAME;
|
|
|
|
}
|
|
|
|
Memory.isSupported = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
Memory.prototype.open = function(callback) {
|
2014-08-21 00:53:01 +00:00
|
|
|
this.db = createDB(this.name);
|
|
|
|
asyncCallback(callback);
|
2014-05-23 18:36:23 +00:00
|
|
|
};
|
|
|
|
Memory.prototype.getReadOnlyContext = function() {
|
|
|
|
return new MemoryContext(this.db, true);
|
|
|
|
};
|
|
|
|
Memory.prototype.getReadWriteContext = function() {
|
|
|
|
return new MemoryContext(this.db, false);
|
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
|
2014-05-23 18:36:23 +00:00
|
|
|
module.exports = Memory;
|