Commit 94e21721 authored by Tristan Cavelier's avatar Tristan Cavelier

improve synchronization algorithm

parent 6d8edb39
...@@ -122,7 +122,6 @@ ...@@ -122,7 +122,6 @@
ReplicateStorage.prototype.syncGetAnswerList = function (command, ReplicateStorage.prototype.syncGetAnswerList = function (command,
answer_list) { answer_list) {
console.log("Starting synchro");
var i, l, answer, answer_modified_date, winner, winner_modified_date, var i, l, answer, answer_modified_date, winner, winner_modified_date,
winner_str, promise_list = [], winner_index, winner_id; winner_str, promise_list = [], winner_index, winner_id;
/*jslint continue: true */ /*jslint continue: true */
...@@ -149,7 +148,6 @@ ...@@ -149,7 +148,6 @@
delete winner._attachments; delete winner._attachments;
winner_id = winner._id; winner_id = winner._id;
winner_str = uniqueJSONStringify(winner); winner_str = uniqueJSONStringify(winner);
console.log('winner', winner_str);
// document synchronisation // document synchronisation
for (i = 0, l = answer_list.length; i < l; i += 1) { for (i = 0, l = answer_list.length; i < l; i += 1) {
...@@ -158,17 +156,19 @@ ...@@ -158,17 +156,19 @@
if (i === winner_index) { continue; } if (i === winner_index) { continue; }
if (answer === 404) { if (answer === 404) {
delete winner._id; delete winner._id;
console.log("Synchronizing (post) " + winner_index + " to " + i); promise_list.push(success(
promise_list.push(command.storage(this._storage_list[i]).post(winner)); command.storage(this._storage_list[i]).post(winner)
));
winner._id = winner_id; winner._id = winner_id;
// delete _id AND reassign _id -> avoid modifying document before // delete _id AND reassign _id -> avoid modifying document before
// resolving the get method. // resolving the get method.
continue; continue;
} }
delete answer._attachments; delete answer._attachments;
if (uniqueJSONStringify(answer) !== winner_str) { if (uniqueJSONStringify(answer.data) !== winner_str) {
console.log("Synchronizing (put) " + winner_index + " to " + i); promise_list.push(success(
promise_list.push(command.storage(this._storage_list[i]).put(winner)); command.storage(this._storage_list[i]).put(winner)
));
} }
} }
return all(promise_list); return all(promise_list);
...@@ -262,13 +262,11 @@ ...@@ -262,13 +262,11 @@
var count = 0, error_count = 0; var count = 0, error_count = 0;
function resolver(index) { function resolver(index) {
return function (answer) { return function (answer) {
console.log(index, answer);
count += 1; count += 1;
if (count === 1) { if (count === 1) {
resolve(answer); resolve(answer);
} }
answer_list[index] = answer; answer_list[index] = answer;
console.log(count, error_count, length);
if (count + error_count === length && count > 0) { if (count + error_count === length && count > 0) {
this_.syncGetAnswerList(command, answer_list); this_.syncGetAnswerList(command, answer_list);
} }
...@@ -284,7 +282,6 @@ ...@@ -284,7 +282,6 @@
if (error_count === length) { if (error_count === length) {
reject(reason); reject(reason);
} }
console.log(count, error_count, length);
if (count + error_count === length && count > 0) { if (count + error_count === length && count > 0) {
this_.syncGetAnswerList(command, answer_list); this_.syncGetAnswerList(command, answer_list);
} }
...@@ -362,7 +359,8 @@ ...@@ -362,7 +359,8 @@
}; };
ReplicateStorage.prototype.repair = function (command, param, option) { ReplicateStorage.prototype.repair = function (command, param, option) {
var storage_list = this._storage_list, length = storage_list.length; var storage_list = this._storage_list, length = storage_list.length,
this_ = this;
if (typeof param._id !== 'string' || !param._id) { if (typeof param._id !== 'string' || !param._id) {
command.error("bad_request"); command.error("bad_request");
...@@ -390,45 +388,14 @@ ...@@ -390,45 +388,14 @@
} }
function synchronizeDocument(answers) { function synchronizeDocument(answers) {
var i, tmp, winner, winner_str, promise_list = [], return this_.syncGetAnswerList(answers.map(function (answer) {
metadata_dict = {}, not_found_dict = {}, modified_list = []; if (answer.result === "success") {
for (i = 0; i < answers.length; i += 1) { return answer;
if (answers[i].result !== "success") {
not_found_dict[i] = true;
} else {
metadata_dict[i] = answers[i].data;
tmp = metadata_dict[i].modified;
tmp = new Date(tmp === undefined ? NaN : tmp);
tmp.index = i;
modified_list.push(tmp);
} }
} if (answer.status === 404) {
modified_list.sort(); return 404;
if (modified_list.length === 0) {
// do nothing because no document was found
return [];
}
tmp = modified_list.pop();
winner = metadata_dict[tmp.index];
winner_str = uniqueJSONStringify(winner);
tmp = tmp.index;
// if no document has valid modified metadata
// just take the first one and replicate to the other one
for (i = 0; i < length; i += 1) {
if (i !== tmp && winner_str !== uniqueJSONStringify(metadata_dict[i])) {
// console.log("Synchronizing document `" + winner_str +
// "` into storage number " + i + " by doing a `" +
// (not_found_dict[i] ? "post" : "put") + "`. ");
promise_list.push(
storage_list[i][not_found_dict[i] ? "post" : "put"](winner)
);
} }
} }));
return all(promise_list);
} }
function checkAnswers(answers) { function checkAnswers(answers) {
......
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