Commit fd0b1d71 authored by Tristan Cavelier's avatar Tristan Cavelier

JobRules improved, 'eliminate' is not used anymore.

parent a4482dab
...@@ -37,7 +37,7 @@ var jobRules = (function () { ...@@ -37,7 +37,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,
...@@ -45,19 +45,66 @@ var jobRules = (function () { ...@@ -45,19 +45,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.
...@@ -67,16 +114,28 @@ var jobRules = (function () { ...@@ -67,16 +114,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();
}; };
/** /**
...@@ -87,10 +146,11 @@ var jobRules = (function () { ...@@ -87,10 +146,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);
}; };
...@@ -132,11 +192,14 @@ var jobRules = (function () { ...@@ -132,11 +192,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
});
} }
}); });
...@@ -160,112 +223,86 @@ var jobRules = (function () { ...@@ -160,112 +223,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
- c: content
- o: options
- =: are equal
- !: are not equal
select ALL s= n= post 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 " wait
GetList !ongoing GetList 0 1 x x " remove " "
Remove !ongoing Remove 0 1 x x put post same doc update
Load !ongoing Load 0 1 x x " " same docid, same rev wait
Save c= !ongoing Save 0 1 x x " put same doc update
Save c! !ongoing Save 0 1 x x " " same docid, same rev wait
GetList ongoing GetList 0 0 1 x " putA " "
Remove ongoing Remove 0 0 1 x " remove " "
Remove ongoing Load 0 0 1 x putA post same docid, same rev wait
Remove !ongoing Load 0 0 1 x " put " "
Load ongoing Load 0 0 1 x " putA same doc update
Save c= ongoing Save 0 0 1 x " " same docid, same rev, same attmt wait
Remove ongoing Save 0 0 0 1 " remove same docid, same rev "
Load ongoing Remove 0 0 0 1 remove post same docid, same rev wait
Load ongoing Save 0 0 0 1 " put " "
Load !ongoing Remove 0 0 0 1 " putA " "
Load !ongoing Save 0 0 0 1 " remove " update
Save ongoing Remove 0 0 0 1 get get same doc, same options update
Save ongoing Load 0 0 0 1 allDocs allDocs same doc, same options update
Save c! ongoing Save 0 0 0 1 */
Save !ongoing Load 0 0 0 1
GetList ongoing Remove 0 0 0 0
GetList ongoing Load 0 0 0 0
GetList ongoing Save 0 0 0 0
GetList !ongoing Remove 0 0 0 0
GetList !ongoing Load 0 0 0 0
GetList !ongoing Save 0 0 0 0
Remove ongoing GetList 0 0 0 0
Remove !ongoing GetList 0 0 0 0
Load ongoing GetList 0 0 0 0
Load !ongoing GetList 0 0 0 0
Save ongoing GetList 0 0 0 0
Save !ongoing GetList 0 0 0 0
For more information, see documentation that.addActionRule("post", "post", [that.sameDocument], that.update);
*/ that.addActionRule("post", "post",
that.addActionRule('post', true, 'post', that.update); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', true, 'put', that.wait); that.addActionRule("post", "put", [that.sameDocument], that.update);
that.addActionRule('post', true, 'get', that.wait); that.addActionRule("post", "put",
that.addActionRule('post', true, 'remove', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', true, 'putAttachment', that.wait); that.addActionRule("post", "putAttachment",
that.addActionRule('post', false, 'post', that.update); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', false, 'put', that.wait); that.addActionRule("post", "remove",
that.addActionRule('post', false, 'get', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('post', false, 'remove', that.eliminate);
that.addActionRule('post', false, 'putAttachment', that.wait);
that.addActionRule('put', true, 'post', that.update); that.addActionRule("put", "post", [that.sameDocument], that.update);
that.addActionRule('put', true, 'put', that.wait); that.addActionRule("put", "post",
that.addActionRule('put', true, 'get', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('put', true, 'remove', that.wait); that.addActionRule("put", "put", [that.sameDocument], that.update);
that.addActionRule('put', true, 'putAttachment', that.wait); that.addActionRule("put", "put",
that.addActionRule('put', false, 'post', that.update); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('put', false, 'put', that.update); that.addActionRule("put", "putAttachment",
that.addActionRule('put', false, 'get', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('put', false, 'remove', that.eliminate); that.addActionRule("put", "remove",
that.addActionRule('put', false, 'putAttachment', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('get', true, 'post', that.wait); that.addActionRule("putAttachment", "post",
that.addActionRule('get', true, 'put', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('get', true, 'get', that.update); that.addActionRule("putAttachment", "put",
that.addActionRule('get', true, 'remove', that.wait); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('get', true, 'putAttachment', that.wait); that.addActionRule("putAttachment", "putAttachment", [that.sameDocument],
that.addActionRule('get', false, 'post', that.wait); that.update);
that.addActionRule('get', false, 'put', that.wait); that.addActionRule("putAttachment", "putAttachment", [
that.addActionRule('get', false, 'get', that.update); that.sameDocumentId,
that.addActionRule('get', false, 'remove', that.wait); that.sameRevision,
that.addActionRule('get', false, 'putAttachment', that.wait); that.sameAttachmentId
], that.wait);
that.addActionRule("putAttachment", "remove",
[that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('remove', true, 'post', that.wait); that.addActionRule("remove", "post",
that.addActionRule('remove', true, 'get', that.update); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('remove', true, 'remove', that.update); that.addActionRule("remove", "put",
that.addActionRule('remove', true, 'putAttachment', that.update); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('remove', false, 'post', that.eliminate); that.addActionRule("remove", "putAttachment",
that.addActionRule('remove', false, 'put', that.update); [that.sameDocumentId, that.sameRevision], that.wait);
that.addActionRule('remove', false, 'get', that.update); that.addActionRule("remove", "remove",
that.addActionRule('remove', false, 'remove', that.update); [that.sameDocumentId, that.sameRevision], that.update);
that.addActionRule('remove', false, 'putAttachment', that.update);
that.addActionRule('allDocs', true, 'allDocs', that.update); that.addActionRule("get", "get",
that.addActionRule('allDocs', false, 'allDocs', that.update); [that.sameDocument, that.sameOption], that.update);
that.addActionRule("allDocs", "allDocs",
[that.sameDocument, that.sameOption], that.update);
that.addActionRule('putAttachment', true, 'post', that.update);
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.update);
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;
......
...@@ -642,12 +642,26 @@ test ("Similar Jobs at the same time (Update)", function () { ...@@ -642,12 +642,26 @@ test ("Similar Jobs at the same time (Update)", function () {
o.spy(o, "value", {"ok": true, "id": "file"}, "job1 ok", "f"); o.spy(o, "value", {"ok": true, "id": "file"}, "job1 ok", "f");
o.spy(o, "value", {"ok": true, "id": "file"}, "job2 ok", "f2"); o.spy(o, "value", {"ok": true, "id": "file"}, "job2 ok", "f2");
o.spy(o, "value", {"ok": true, "id": "file"}, "job3 ok", "f3"); o.spy(o, "value", {"ok": true, "id": "file"}, "job3 ok", "f3");
o.jio.put({"_id": "file", "content": "content"}, o.f); o.jio.put({"_id": "file", "content": "content"}, o.f); // 1
o.jio.put({"_id": "file", "content": "content"}, o.f2); o.jio.put({"_id": "file", "content": "content"}, o.f2); // 2
o.jio.put({"_id": "file", "content": "content"}, o.f3); o.jio.put({"_id": "file", "content": "content"}, o.f3); // 3
deepEqual(getLastJob(o.jio.getId()).id, 1, "Check job queue");
console.log(JSON.parse(JSON.stringify(localStorage)));
o.tick(o, 1000, "f");
o.tick(o, "f2");
o.tick(o, "f3");
o.spy(o, "value", {"ok": true, "id": "file"}, "job4 ok", "f");
o.spy(o, "value", {"ok": true, "id": "file"}, "job5 ok", "f2");
o.spy(o, "value", {"ok": true, "id": "file"}, "job6 ok", "f3");
o.jio.put({"_id": "file", "content": "content"}, o.f); // 4
o.jio.remove({"_id": "file", "content": "content"}, o.f2); // 5
o.jio.put({"_id": "file", "content": "content"}, o.f3); // 6
deepEqual(getLastJob(o.jio.getId()).id, 5, "Check job queue");
o.tick(o, 1000, "f"); o.tick(o, 1000, "f");
o.tick(o, "f2"); o.tick(o, "f2");
o.tick(o, "f3"); o.tick(o, "f3");
o.jio.stop(); o.jio.stop();
}); });
...@@ -659,15 +673,15 @@ test ("One document aim jobs at the same time (Wait for job(s))" , function () { ...@@ -659,15 +673,15 @@ test ("One document aim jobs at the same time (Wait for job(s))" , function () {
o.jio = JIO.newJio({"type":"dummyallok"}); o.jio = JIO.newJio({"type":"dummyallok"});
o.spy(o, "value", {"ok": true, "id": "file"}, "job1", "f"); o.spy(o, "value", {"ok": true, "id": "file"}, "job1", "f");
o.spy(o, "value", {"ok": true, "id": "file"}, "job2", "f2"); o.spy(o, "value", {"ok": true, "id": "file"}, "job2", "f2");
o.spy(o, "value", {"_id": "file", "title": "get_title"}, "job3", "f3"); o.spy(o, "value", {"ok": true, "id": "file"}, "job3", "f3");
o.jio.post({"_id": "file", "content": "content"}, o.f); o.jio.put({"_id": "file", "content": "content"}, o.f);
o.testLastJobWaitForJob(undefined, "job1 is not waiting for someone"); o.testLastJobWaitForJob(undefined, "job1 is not waiting for someone");
o.jio.put({"_id": "file", "content": "content"}, o.f2); o.jio.remove({"_id": "file", "content": "content"}, o.f2);
o.testLastJobWaitForJob([1], "job2 is waiting"); o.testLastJobWaitForJob([1], "job2 is waiting");
o.jio.get({"_id": "file"}, o.f3); o.jio.put({"_id": "file"}, o.f3);
o.testLastJobWaitForJob([1, 2], "job3 is waiting"); o.testLastJobWaitForJob([1, 2], "job3 is waiting");
o.tick(o, 1000, "f"); o.tick(o, 1000, "f");
...@@ -677,26 +691,6 @@ test ("One document aim jobs at the same time (Wait for job(s))" , function () { ...@@ -677,26 +691,6 @@ test ("One document aim jobs at the same time (Wait for job(s))" , function () {
}); });
test ("One document aim jobs at the same time (Elimination)" , function () {
var o = generateTools(this);
o.jio = JIO.newJio({"type":"dummyallok"});
o.spy(o, "status", 10, "job1 stopped", "f");
o.spy(o, "value", {"ok": true, "id": "file"}, "job2", "f2");
o.jio.post({"_id": "file", "content": "content"}, o.f);
o.testLastJobLabel("post", "job1 exists");
o.jio.remove({"_id": "file"}, o.f2);
o.testLastJobLabel("remove", "job1 does not exist anymore");
o.tick(o, 1000, "f");
o.tick(o, "f2");
o.jio.stop();
});
test ("Server will be available soon (Wait for time)" , function () { test ("Server will be available soon (Wait for time)" , function () {
var o = generateTools(this); var o = generateTools(this);
......
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