Latest Code

This commit is contained in:
Dobie Wollert
2015-04-06 03:28:20 -04:00
parent 966152a631
commit d3089dcd17
105 changed files with 8731 additions and 96 deletions

View File

@ -0,0 +1,68 @@
module.exports = Collection;
var inherits = require('util').inherits,
EventEmitter = require('events').EventEmitter,
KeyLock = require('./KeyLock.js'),
request = require('./request.js');
function Collection(name) {
this._name = name;
}
inherits(Collection, EventEmitter);
Collection.prototype.get = function(key, cb) {
this._request('get', key, null, function(err, msg) {
if (err)
return cb(err);
else if (!msg.json)
return cb(null, undefined);
else
return cb(null, JSON.parse(msg.json));
});
};
Collection.prototype.set = function(key, value, cb) {
var data = { json: JSON.stringify(value) };
this._request('set', key, data, cb && function(err, msg) {
return cb(err);
});
};
Collection.prototype.del = function(key, cb) {
return this._request('set', key, null, cb && function(err, msg) {
return cb(err);
});
};
Collection.prototype.acquire = function(key, cb) {
var self = this;
this._request('acquire', key, null, function(err, msg) {
if (err)
return cb(err);
var json = msg.json;
var lock = new KeyLock(self, key, json);
cb(null, lock, lock.get());
});
};
Collection.prototype.configure = function(config) {
var self = this;
this._request('configure', null, { config: config }, function(err, msg) {
if (err)
self.emit('error', err);
});
return this;
};
// This function clobbers `data` if specified
Collection.prototype._request = function(method, key, data, cb) {
request(method, this._name, key, data, cb);
};

View File

@ -0,0 +1,56 @@
module.exports = KeyLock;
var request = require('./request.js');
function KeyLock(collection, key, json) {
this._collection = collection;
this._key = key;
this._json = json;
this._updated = false;
this._released = false;
}
KeyLock.prototype.get = function() {
if (!this._json)
return undefined;
else
return JSON.parse(this._json);
};
KeyLock.prototype.set = function(newValue) {
if (this._released)
throw new Error("Can't set after releasing a lock.");
this._json = JSON.stringify(newValue);
this._updated = true;
};
KeyLock.prototype.del = function() {
if (this._released)
throw new Error("Can't delete after releasing a lock.");
this._json = undefined;
this._updated = true;
};
KeyLock.prototype.release = function(cb) {
if (this._released)
throw new Error('KeyLock has already been released.');
this._released = true;
if (!this._updated)
this._collection._request('release', this._key, null, cb && afterRelease);
else
this._collection._request('setRelease',
this._key,
{ json: this._json },
cb && afterRelease);
function afterRelease(err, msg) {
return cb(err);
}
};

View File

@ -0,0 +1,51 @@
module.exports = request;
process.on('message', onMessage);
var requestIdCounter = 0;
var requestCallbacks = {};
// This function clobbers `data` if specified
function request(method, collection, key, data, cb) {
data = data || {};
data.type = 'DSM_REQUEST';
data.method = method;
data.collection = collection;
data.key = key;
if (cb) {
var requestId = getRequestId();
requestCallbacks[requestId] = cb;
data.requestId = requestId;
}
process.send(data);
}
function onMessage(msg) {
if (msg.type !== 'DSM_REPLY')
return;
var requestId = msg.requestId;
var cb = requestCallbacks[requestId];
delete requestCallbacks[requestId];
if (msg.err) {
var err = new Error('Master error: ' + msg.err);
return cb(err);
}
cb(null, msg);
}
function getRequestId() {
return ++requestIdCounter;
}