2013-11-22 21:12:31 +00:00
|
|
|
define(function(require) {
|
2013-11-27 00:12:25 +00:00
|
|
|
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
|
|
|
|
function MemoryContext(db, readOnly) {
|
|
|
|
this.readOnly = readOnly;
|
|
|
|
this.objectStore = db;
|
|
|
|
}
|
|
|
|
MemoryContext.prototype.clear = function(callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
return callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
}
|
|
|
|
var objectStore = this.objectStore;
|
|
|
|
Object.keys(objectStore).forEach(function(key){
|
|
|
|
delete objectStore[key];
|
|
|
|
});
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
MemoryContext.prototype.get = function(key, callback) {
|
|
|
|
callback(null, this.objectStore[key]);
|
|
|
|
};
|
|
|
|
MemoryContext.prototype.put = function(key, value, callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
return callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
}
|
|
|
|
this.objectStore[key] = value;
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
MemoryContext.prototype.delete = function(key, callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
return callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
}
|
|
|
|
delete this.objectStore[key];
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function Memory(name) {
|
2013-11-27 00:12:25 +00:00
|
|
|
this.name = name || FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
this.db = {};
|
|
|
|
}
|
|
|
|
Memory.isSupported = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Memory.prototype.open = function(callback) {
|
|
|
|
callback(null, true);
|
|
|
|
};
|
|
|
|
Memory.prototype.getReadOnlyContext = function() {
|
|
|
|
return new MemoryContext(this.db, true);
|
|
|
|
};
|
|
|
|
Memory.prototype.getReadWriteContext = function() {
|
|
|
|
return new MemoryContext(this.db, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return Memory;
|
|
|
|
});
|