Commit b0e84879 authored by Tristan Cavelier's avatar Tristan Cavelier

jio.js updated

parent fd0b1d71
...@@ -360,8 +360,8 @@ var command = function (spec, my) { ...@@ -360,8 +360,8 @@ var command = function (spec, my) {
priv.docid = spec.docid || priv.doc._id; priv.docid = spec.docid || priv.doc._id;
priv.option = spec.options || {}; priv.option = spec.options || {};
priv.callbacks = spec.callbacks || {}; priv.callbacks = spec.callbacks || {};
priv.success = priv.callbacks.success || function () {}; priv.success = [priv.callbacks.success || function () {}];
priv.error = priv.callbacks.error || function () {}; priv.error = [priv.callbacks.error || function () {}];
priv.retry = function () { priv.retry = function () {
that.error({ that.error({
status: 13, status: 13,
...@@ -557,9 +557,14 @@ var command = function (spec, my) { ...@@ -557,9 +557,14 @@ var command = function (spec, my) {
*/ */
that.executeOn = function (storage) {}; that.executeOn = function (storage) {};
that.success = function (return_value) { that.success = function (return_value) {
var i;
priv.on_going = false; priv.on_going = false;
priv.success(return_value); for (i = 0; i < priv.success.length; i += 1) {
priv.success[i](return_value);
}
priv.end(doneStatus()); priv.end(doneStatus());
priv.success = [];
priv.error = [];
}; };
that.retry = function (return_error) { that.retry = function (return_error) {
priv.on_going = false; priv.on_going = false;
...@@ -570,13 +575,31 @@ var command = function (spec, my) { ...@@ -570,13 +575,31 @@ var command = function (spec, my) {
} }
}; };
that.error = function (return_error) { that.error = function (return_error) {
var i;
priv.on_going = false; priv.on_going = false;
priv.error(return_error); for (i = 0; i < priv.error.length; i += 1) {
priv.error[i](return_error);
}
priv.end(failStatus()); priv.end(failStatus());
priv.success = [];
priv.error = [];
}; };
that.end = function () { that.end = function () {
priv.end(doneStatus()); priv.end(doneStatus());
}; };
that.addCallbacks = function (success, error) {
if (arguments.length > 1) {
priv.success.push(success || function () {});
priv.error.push(error || function () {});
} else {
priv.success.push(function (response) {
(success || function () {})(undefined, response);
});
priv.error.push(function (err) {
(success || function () {})(err, undefined);
});
}
};
that.onSuccessDo = function (fun) { that.onSuccessDo = function (fun) {
if (fun) { if (fun) {
priv.success = fun; priv.success = fun;
...@@ -1230,16 +1253,9 @@ var job = function (spec) { ...@@ -1230,16 +1253,9 @@ var job = function (spec) {
* @param {object} job The other job. * @param {object} job The other job.
*/ */
that.update = function (job) { that.update = function (job) {
priv.command.error({ priv.command.addCallbacks(job.getCommand().onSuccessDo()[0],
status: 12, job.getCommand().onErrorDo()[0]);
statusText: 'Replaced',
error: 'replaced',
message: 'Job has been replaced by another one.',
reason: 'job has been replaced by another one'
});
priv.date = new Date(job.getDate().getTime()); priv.date = new Date(job.getDate().getTime());
priv.command = job.getCommand();
priv.status = job.getStatus();
}; };
/** /**
...@@ -1783,7 +1799,7 @@ var jobRules = (function () { ...@@ -1783,7 +1799,7 @@ var jobRules = (function () {
return 'wait'; return 'wait';
} }
}); });
Object.defineProperty(that, "none", { Object.defineProperty(that, "ok", {
configurable: false, configurable: false,
enumerable: false, enumerable: false,
writable: false, writable: false,
...@@ -1791,19 +1807,66 @@ var jobRules = (function () { ...@@ -1791,19 +1807,66 @@ var jobRules = (function () {
return 'none'; return 'none';
} }
}); });
that.default_action = that.none; that.default_action = that.ok;
that.default_compare = function (job1, job2) { that.default_compare = function (job1, job2) {
return (job1.getCommand().getDocId() === job2.getCommand().getDocId() && return job1.getId() !== job2.getId() &&
job1.getCommand().getAttachmentId() === job1.getStatus().getLabel() !== "done" &&
job2.getCommand().getAttachmentId() && job1.getStatus().getLabel() !== "fail" &&
job1.getCommand().getDocInfo('_rev') === JSON.stringify(job1.getStorage().serialized()) ===
job2.getCommand().getDocInfo('_rev') && JSON.stringify(job2.getStorage().serialized());
job1.getCommand().getOption('rev') ===
job2.getCommand().getOption('rev') &&
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized()));
}; };
// Compare Functions //
Object.defineProperty(that, "sameDocumentId", {
configurable: false,
enumerable: false,
writable: false,
value: function (job1, job2) {
return job1.getCommand().getDocId() === job2.getCommand().getDocId();
}
});
Object.defineProperty(that, "sameRevision", {
configurable: false,
enumerable: false,
writable: false,
value: function (job1, job2) {
return job1.getCommand().getDocInfo("_rev") ===
job2.getCommand().getDocInfo("_rev");
}
});
Object.defineProperty(that, "sameAttachmentId", {
configurable: false,
enumerable: false,
writable: false,
value: function (job1, job2) {
return job1.getCommand().getAttachmentId() ===
job2.getCommand().getAttachmentId();
}
});
Object.defineProperty(that, "sameDocument", {
configurable: false,
enumerable: false,
writable: false,
value: function (job1, job2) {
return JSON.stringify(job1.getCommand().cloneDoc()) ===
JSON.stringify(job2.getCommand().cloneDoc());
}
});
Object.defineProperty(that, "sameOption", {
configurable: false,
enumerable: false,
writable: false,
value: function (job1, job2) {
return JSON.stringify(job1.getCommand().cloneOption()) ===
JSON.stringify(job2.getCommand().cloneOption());
}
});
// Methods // // Methods //
/** /**
* Returns an action according the jobs given in parameters. * Returns an action according the jobs given in parameters.
...@@ -1813,16 +1876,28 @@ var jobRules = (function () { ...@@ -1813,16 +1876,28 @@ var jobRules = (function () {
* @return {string} An action string. * @return {string} An action string.
*/ */
priv.getAction = function (job1, job2) { priv.getAction = function (job1, job2) {
var j1label, j2label, j1status; var method1, method2, tmp = priv.action, i, j, condition_list = [], res;
j1label = job1.getCommand().getLabel(); method1 = job1.getCommand().getLabel();
j2label = job2.getCommand().getLabel(); method2 = job2.getCommand().getLabel();
j1status = (job1.getStatus().getLabel() === 'on going' ? tmp = tmp[method1] = tmp[method1] || {};
'on going' : 'not on going'); tmp = tmp[method2] = tmp[method2] || [];
if (priv.action[j1label] && priv.action[j1label][j1status] && for (i = 0; i < tmp.length; i += 1) {
priv.action[j1label][j1status][j2label]) { // browsing all method1 method2 rules
return priv.action[j1label][j1status][j2label](job1, job2); condition_list = tmp[i].condition_list;
res = true;
for (j = 0; j < condition_list.length; j += 1) {
// test all the rule's conditions
if (!condition_list[j](job1, job2)) {
res = false;
break;
}
}
if (res) {
// if all respects condition list, then action
return tmp[i].rule();
}
} }
return that.default_action(job1, job2); return that.default_action();
}; };
/** /**
...@@ -1833,10 +1908,11 @@ var jobRules = (function () { ...@@ -1833,10 +1908,11 @@ var jobRules = (function () {
* @return {boolean} true if comparable, else false. * @return {boolean} true if comparable, else false.
*/ */
priv.canCompare = function (job1, job2) { priv.canCompare = function (job1, job2) {
var job1label = job1.getCommand().getLabel(), var method1, method2;
job2label = job2.getCommand().getLabel(); method1 = job1.getCommand().getLabel();
if (priv.compare[job1label] && priv.compare[job2label]) { method2 = job2.getCommand().getLabel();
return priv.compare[job1label][job2label](job1, job2); if (priv.compare[method1] && priv.compare[method1][method2]) {
return priv.compare[method1][method2](job1, job2);
} }
return that.default_compare(job1, job2); return that.default_compare(job1, job2);
}; };
...@@ -1878,11 +1954,14 @@ var jobRules = (function () { ...@@ -1878,11 +1954,14 @@ var jobRules = (function () {
configurable: false, configurable: false,
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (method1, ongoing, method2, rule) { value: function (method1, method2, condition_list, rule) {
var ongoing_s = (ongoing ? 'on going' : 'not on going'); var tmp = priv.action;
priv.action[method1] = priv.action[method1] || {}; tmp = tmp[method1] = tmp[method1] || {};
priv.action[method1][ongoing_s] = priv.action[method1][ongoing_s] || {}; tmp = tmp[method2] = tmp[method2] || [];
priv.action[method1][ongoing_s][method2] = rule; tmp.push({
"condition_list": condition_list,
"rule": rule
});
} }
}); });
...@@ -1906,112 +1985,86 @@ var jobRules = (function () { ...@@ -1906,112 +1985,86 @@ var jobRules = (function () {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Adding some rules // Adding some rules
/* /*
LEGEND: Rules
- s: storage original job |job to add |condition |action
- m: method
- n: name post post same doc update
- c: content " " same docid, same rev wait
- o: options " put same doc update
- =: are equal " " same docid, same rev wait
- !: are not equal " putA " wait
" remove " "
select ALL s= n= put post same doc update
removefailordone fail|done " " same docid, same rev wait
/ elim repl nacc wait " put same doc update
Remove !ongoing Save 1 x x x " " same docid, same rev wait
Save !ongoing Remove 1 x x x " putA " "
GetList !ongoing GetList 0 1 x x " remove " "
Remove !ongoing Remove 0 1 x x putA post same docid, same rev wait
Load !ongoing Load 0 1 x x " put " "
Save c= !ongoing Save 0 1 x x " putA same doc update
Save c! !ongoing Save 0 1 x x " " same docid, same rev, same attmt wait
GetList ongoing GetList 0 0 1 x " remove same docid, same rev "
Remove ongoing Remove 0 0 1 x remove post same docid, same rev wait
Remove ongoing Load 0 0 1 x " put " "
Remove !ongoing Load 0 0 1 x " putA " "
Load ongoing Load 0 0 1 x " remove " update
Save c= ongoing Save 0 0 1 x get get same doc, same options update
Remove ongoing Save 0 0 0 1 allDocs allDocs same doc, same options update
Load ongoing Remove 0 0 0 1 */
Load ongoing Save 0 0 0 1
Load !ongoing Remove 0 0 0 1 that.addActionRule("post", "post", [that.sameDocument], that.update);
Load !ongoing Save 0 0 0 1 that.addActionRule("post", "post",
Save ongoing Remove 0 0 0 1 [that.sameDocumentId, that.sameRevision], that.wait);
Save ongoing Load 0 0 0 1 that.addActionRule("post", "put", [that.sameDocument], that.update);
Save c! ongoing Save 0 0 0 1 that.addActionRule("post", "put",
Save !ongoing Load 0 0 0 1 [that.sameDocumentId, that.sameRevision], that.wait);
GetList ongoing Remove 0 0 0 0 that.addActionRule("post", "putAttachment",
GetList ongoing Load 0 0 0 0 [that.sameDocumentId, that.sameRevision], that.wait);
GetList ongoing Save 0 0 0 0 that.addActionRule("post", "remove",
GetList !ongoing Remove 0 0 0 0 [that.sameDocumentId, that.sameRevision], that.wait);
GetList !ongoing Load 0 0 0 0
GetList !ongoing Save 0 0 0 0 that.addActionRule("put", "post", [that.sameDocument], that.update);
Remove ongoing GetList 0 0 0 0 that.addActionRule("put", "post",
Remove !ongoing GetList 0 0 0 0 [that.sameDocumentId, that.sameRevision], that.wait);
Load ongoing GetList 0 0 0 0 that.addActionRule("put", "put", [that.sameDocument], that.update);
Load !ongoing GetList 0 0 0 0 that.addActionRule("put", "put",
Save ongoing GetList 0 0 0 0 [that.sameDocumentId, that.sameRevision], that.wait);
Save !ongoing GetList 0 0 0 0 that.addActionRule("put", "putAttachment",
[that.sameDocumentId, that.sameRevision], that.wait);
For more information, see documentation that.addActionRule("put", "remove",
*/ [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', true, 'post', that.dontAccept);
that.addActionRule('post', true, 'put', that.wait); that.addActionRule("putAttachment", "post",
that.addActionRule('post', true, 'get', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', true, 'remove', that.wait); that.addActionRule("putAttachment", "put",
that.addActionRule('post', true, 'putAttachment', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', false, 'post', that.update); that.addActionRule("putAttachment", "putAttachment", [that.sameDocument],
that.addActionRule('post', false, 'put', that.wait); that.update);
that.addActionRule('post', false, 'get', that.wait); that.addActionRule("putAttachment", "putAttachment", [
that.addActionRule('post', false, 'remove', that.eliminate); that.sameDocumentId,
that.addActionRule('post', false, 'putAttachment', that.wait); that.sameRevision,
that.sameAttachmentId
that.addActionRule('put', true, 'post', that.dontAccept); ], that.wait);
that.addActionRule('put', true, 'put', that.wait); that.addActionRule("putAttachment", "remove",
that.addActionRule('put', true, 'get', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('put', true, 'remove', that.wait);
that.addActionRule('put', true, 'putAttachment', that.wait); that.addActionRule("remove", "post",
that.addActionRule('put', false, 'post', that.dontAccept); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('put', false, 'put', that.update); that.addActionRule("remove", "put",
that.addActionRule('put', false, 'get', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('put', false, 'remove', that.eliminate); that.addActionRule("remove", "putAttachment",
that.addActionRule('put', false, 'putAttachment', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule("remove", "remove",
that.addActionRule('get', true, 'post', that.wait); [that.sameDocumentId, that.sameRevision], that.update);
that.addActionRule('get', true, 'put', that.wait);
that.addActionRule('get', true, 'get', that.dontAccept); that.addActionRule("get", "get",
that.addActionRule('get', true, 'remove', that.wait); [that.sameDocument, that.sameOption], that.update);
that.addActionRule('get', true, 'putAttachment', that.wait); that.addActionRule("allDocs", "allDocs",
that.addActionRule('get', false, 'post', that.wait); [that.sameDocument, that.sameOption], that.update);
that.addActionRule('get', false, 'put', that.wait);
that.addActionRule('get', false, 'get', that.update);
that.addActionRule('get', false, 'remove', that.wait);
that.addActionRule('get', false, 'putAttachment', that.wait);
that.addActionRule('remove', true, 'post', that.wait);
that.addActionRule('remove', true, 'get', that.dontAccept);
that.addActionRule('remove', true, 'remove', that.dontAccept);
that.addActionRule('remove', true, 'putAttachment', that.dontAccept);
that.addActionRule('remove', false, 'post', that.eliminate);
that.addActionRule('remove', false, 'put', that.dontAccept);
that.addActionRule('remove', false, 'get', that.dontAccept);
that.addActionRule('remove', false, 'remove', that.update);
that.addActionRule('remove', false, 'putAttachment', that.dontAccept);
that.addActionRule('allDocs', true, 'allDocs', that.dontAccept);
that.addActionRule('allDocs', false, 'allDocs', that.update);
that.addActionRule('putAttachment', true, 'post', that.dontAccept);
that.addActionRule('putAttachment', true, 'put', that.wait);
that.addActionRule('putAttachment', true, 'get', that.wait);
that.addActionRule('putAttachment', true, 'remove', that.wait);
that.addActionRule('putAttachment', true, 'putAttachment', that.wait);
that.addActionRule('putAttachment', false, 'post', that.dontAccept);
that.addActionRule('putAttachment', false, 'put', that.wait);
that.addActionRule('putAttachment', false, 'get', that.wait);
that.addActionRule('putAttachment', false, 'remove', that.eliminate);
that.addActionRule('putAttachment', false, 'putAttachment', that.update);
// end adding rules // end adding rules
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
return that; return that;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment