update File to write wallets to different files

the walletId is the filename
This commit is contained in:
Ryan X. Charles 2014-04-16 17:37:32 -03:00
parent b442e110e4
commit 5f8deb7d0b
2 changed files with 51 additions and 43 deletions

View File

@ -6,47 +6,54 @@ function Storage(opts) {
opts = opts || {};
this.data = {};
this.filename = opts.filename;
}
Storage.prototype.load = function(callback) {
if (!this.filename)
throw new Error('No filename');
fs.readFile(this.filename, function(err, data) {
Storage.prototype.load = function(walletId, callback) {
fs.readFile(walletId, function(err, data) {
if (err) return callback(err);
try {
this.data = JSON.parse(data);
this.data[walletId] = JSON.parse(data);
} catch (err) {
return callback(err);
if (callback)
return callback(err);
}
return callback(null);
if (callback)
return callback(null);
});
};
Storage.prototype.save = function(callback) {
var data = JSON.stringify(this.data);
Storage.prototype.save = function(walletId, callback) {
var data = JSON.stringify(this.data[walletId]);
//TODO: update to use a queue to ensure that saves are made sequentially
fs.writeFile(this.filename, data, function(err) {
return callback(err);
fs.writeFile(walletId, data, function(err) {
if (callback)
return callback(err);
});
};
Storage.prototype._read = function(k) {
return this.data[k];
var split = k.split('::');
var walletId = split[0];
var key = split[1];
return this.data[walletId][key];
};
Storage.prototype._write = function(k, v, callback) {
this.data[k] = v;
this.save(callback);
var split = k.split('::');
var walletId = split[0];
var key = split[1];
if (!this.data[walletId])
this.data[walletId] = {};
this.data[walletId][key] = v;
this.save(walletId, callback);
};
// get value by key
Storage.prototype.getGlobal = function(k) {
return this.data[k];
return this._read(k);
};
// set value for key
@ -56,13 +63,17 @@ Storage.prototype.setGlobal = function(k, v, callback) {
// remove value for key
Storage.prototype.removeGlobal = function(k, callback) {
delete this.data[k];
this.save(callback);
var split = k.split('::');
var walletId = split[0];
var key = split[1];
delete this.data[walletId][key];
this.save(walletId, callback);
};
Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k;
};
// get value by key
Storage.prototype.get = function(walletId, k) {
return this.getGlobal(this._key(walletId, k));

View File

@ -18,8 +18,8 @@ describe('Storage/File', function() {
callback();
};
var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs});
var storage = new Storage({filename: 'myfilename', password: 'password'});
storage.load(function(err) {
var storage = new Storage({password: 'password'});
storage.load('myfilename', function(err) {
done();
});
});
@ -33,8 +33,8 @@ describe('Storage/File', function() {
callback();
};
var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs});
var storage = new Storage({filename: 'myfilename', password: 'password'});
storage.save(function(err) {
var storage = new Storage({password: 'password'});
storage.save('myfilename', function(err) {
done();
});
});
@ -43,45 +43,42 @@ describe('Storage/File', function() {
describe('#_read', function() {
it('should return the value of a key', function() {
var storage = new Storage();
storage.data = {'test':'data'};
storage._read('test').should.equal('data');
storage.data = {'walletId':{'test':'data'}};
storage._read('walletId::test').should.equal('data');
});
});
describe('#_write', function() {
it('should save the value of a key and then run save', function(done) {
var storage = new Storage();
storage.save = function(callback) {
storage.data['key'].should.equal('value');
storage.save = function(walletId, callback) {
storage.data[walletId]['key'].should.equal('value');
callback();
};
storage._write('key', 'value', function() {
storage._write('walletId::key', 'value', function() {
done();
});
});
});
describe('#getGlobal', function() {
it('should store a global key', function(done) {
it('should call storage._read', function() {
var storage = new Storage();
storage.save = function(callback) {
storage.data['key'].should.equal('value');
callback();
};
storage.setGlobal('key', 'value', function() {
done();
});
storage.data = {'walletId':{'test':'test'}};
storage._read = sinon.spy();
storage.getGlobal('walletId::test');
storage._read.calledOnce.should.equal(true);
});
});
describe('#setGlobal', function() {
it('should store a global key', function(done) {
var storage = new Storage();
storage.save = function(callback) {
storage.data['key'].should.equal('value');
storage.save = function(walletId, callback) {
storage.data[walletId]['key'].should.equal('value');
callback();
};
storage.setGlobal('key', 'value', function() {
storage.setGlobal('walletId::key', 'value', function() {
done();
});
});
@ -90,12 +87,12 @@ describe('Storage/File', function() {
describe('#removeGlobal', function() {
it('should remove a global key', function(done) {
var storage = new Storage();
storage.data.key = 'value';
storage.save = function(callback) {
should.not.exist(storage.data['key']);
storage.data = {'walletId':{'key':'value'}};
storage.save = function(walletId, callback) {
should.not.exist(storage.data[walletId]['key']);
callback();
};
storage.removeGlobal('key', function() {
storage.removeGlobal('walletId::key', function() {
done();
});
});