Commit e38e542a authored by Romain Courteaud's avatar Romain Courteaud

ReplicateStorage: stop relying on closure

Simplify the code, to allow more changes later.
parent 3e6379c8
...@@ -202,15 +202,8 @@ ...@@ -202,15 +202,8 @@
arguments); arguments);
}; };
ReplicateStorage.prototype.repair = function () { function dispatchQueue(context, function_used, argument_list,
var context = this, number_queue) {
argument_list = arguments,
skip_document_dict = {};
// Do not sync the signature document
skip_document_dict[context._signature_hash] = null;
function dispatchQueue(function_used, argument_list, number_queue) {
var result_promise_list = [], var result_promise_list = [],
i; i;
...@@ -237,7 +230,7 @@ ...@@ -237,7 +230,7 @@
return result_promise_list[0]; return result_promise_list[0];
} }
function propagateAttachmentDeletion(skip_attachment_dict, function propagateAttachmentDeletion(context, skip_attachment_dict,
destination, destination,
id, name) { id, name) {
return destination.removeAttachment(id, name) return destination.removeAttachment(id, name)
...@@ -249,7 +242,7 @@ ...@@ -249,7 +242,7 @@
}); });
} }
function propagateAttachmentModification(skip_attachment_dict, function propagateAttachmentModification(context, skip_attachment_dict,
destination, destination,
blob, hash, id, name) { blob, hash, id, name) {
return destination.putAttachment(id, name, blob) return destination.putAttachment(id, name, blob)
...@@ -264,7 +257,8 @@ ...@@ -264,7 +257,8 @@
}); });
} }
function checkAndPropagateAttachment(skip_attachment_dict, function checkAndPropagateAttachment(context, skip_document_dict,
skip_attachment_dict,
status_hash, local_hash, blob, status_hash, local_hash, blob,
source, destination, id, name, source, destination, id, name,
conflict_force, conflict_revert, conflict_force, conflict_revert,
...@@ -311,11 +305,12 @@ ...@@ -311,11 +305,12 @@
// Modified only locally. No conflict or force // Modified only locally. No conflict or force
if (local_hash === null) { if (local_hash === null) {
// Deleted locally // Deleted locally
return propagateAttachmentDeletion(skip_attachment_dict, return propagateAttachmentDeletion(context, skip_attachment_dict,
destination, destination,
id, name); id, name);
} }
return propagateAttachmentModification(skip_attachment_dict, return propagateAttachmentModification(context,
skip_attachment_dict,
destination, blob, destination, blob,
local_hash, id, name); local_hash, id, name);
} }
...@@ -329,10 +324,11 @@ ...@@ -329,10 +324,11 @@
// Automatically resolve conflict or force revert // Automatically resolve conflict or force revert
if (remote_hash === null) { if (remote_hash === null) {
// Deleted remotely // Deleted remotely
return propagateAttachmentDeletion(skip_attachment_dict, return propagateAttachmentDeletion(context, skip_attachment_dict,
source, id, name); source, id, name);
} }
return propagateAttachmentModification( return propagateAttachmentModification(
context,
skip_attachment_dict, skip_attachment_dict,
source, source,
remote_blob, remote_blob,
...@@ -345,7 +341,8 @@ ...@@ -345,7 +341,8 @@
// Minimize conflict if it can be resolved // Minimize conflict if it can be resolved
if (remote_hash === null) { if (remote_hash === null) {
// Copy remote modification remotely // Copy remote modification remotely
return propagateAttachmentModification(skip_attachment_dict, return propagateAttachmentModification(context,
skip_attachment_dict,
destination, blob, destination, blob,
local_hash, id, name); local_hash, id, name);
} }
...@@ -356,7 +353,9 @@ ...@@ -356,7 +353,9 @@
}); });
} }
function checkAttachmentSignatureDifference(queue, skip_attachment_dict, function checkAttachmentSignatureDifference(queue, context,
skip_document_dict,
skip_attachment_dict,
source, source,
destination, id, name, destination, id, name,
conflict_force, conflict_force,
...@@ -398,7 +397,8 @@ ...@@ -398,7 +397,8 @@
local_hash = generateHashFromArrayBuffer(array_buffer); local_hash = generateHashFromArrayBuffer(array_buffer);
if (local_hash !== status_hash) { if (local_hash !== status_hash) {
return checkAndPropagateAttachment(skip_attachment_dict, return checkAndPropagateAttachment(context, skip_document_dict,
skip_attachment_dict,
status_hash, local_hash, blob, status_hash, local_hash, blob,
source, destination, id, name, source, destination, id, name,
conflict_force, conflict_revert, conflict_force, conflict_revert,
...@@ -407,7 +407,8 @@ ...@@ -407,7 +407,8 @@
}); });
} }
function checkAttachmentLocalDeletion(queue, skip_attachment_dict, function checkAttachmentLocalDeletion(queue, context, skip_document_dict,
skip_attachment_dict,
destination, id, name, source, destination, id, name, source,
conflict_force, conflict_revert, conflict_force, conflict_revert,
conflict_ignore) { conflict_ignore) {
...@@ -419,7 +420,8 @@ ...@@ -419,7 +420,8 @@
}) })
.push(function (result) { .push(function (result) {
status_hash = result.hash; status_hash = result.hash;
return checkAndPropagateAttachment(skip_attachment_dict, return checkAndPropagateAttachment(context, skip_document_dict,
skip_attachment_dict,
status_hash, null, null, status_hash, null, null,
source, destination, id, name, source, destination, id, name,
conflict_force, conflict_revert, conflict_force, conflict_revert,
...@@ -427,12 +429,12 @@ ...@@ -427,12 +429,12 @@
}); });
} }
function pushDocumentAttachment(skip_attachment_dict, id, source, function pushDocumentAttachment(context, skip_document_dict,
skip_attachment_dict, id, source,
destination, options) { destination, options) {
var queue = new RSVP.Queue(), var queue = new RSVP.Queue(),
local_dict = {}, local_dict = {},
signature_dict = {}; signature_dict = {};
return queue return queue
.push(function () { .push(function () {
return RSVP.all([ return RSVP.all([
...@@ -482,6 +484,8 @@ ...@@ -482,6 +484,8 @@
&& options.check_creation; && options.check_creation;
if (is_modification === true || is_creation === true) { if (is_modification === true || is_creation === true) {
argument_list.push([undefined, argument_list.push([undefined,
context,
skip_document_dict,
skip_attachment_dict, skip_attachment_dict,
source, source,
destination, id, key, destination, id, key,
...@@ -494,6 +498,7 @@ ...@@ -494,6 +498,7 @@
} }
} }
return dispatchQueue( return dispatchQueue(
context,
checkAttachmentSignatureDifference, checkAttachmentSignatureDifference,
argument_list, argument_list,
context._parallel_operation_attachment_amount context._parallel_operation_attachment_amount
...@@ -506,6 +511,8 @@ ...@@ -506,6 +511,8 @@
if (signature_dict.hasOwnProperty(key)) { if (signature_dict.hasOwnProperty(key)) {
if (!local_dict.hasOwnProperty(key)) { if (!local_dict.hasOwnProperty(key)) {
argument_list.push([undefined, argument_list.push([undefined,
context,
skip_document_dict,
skip_attachment_dict, skip_attachment_dict,
destination, id, key, destination, id, key,
source, source,
...@@ -516,6 +523,7 @@ ...@@ -516,6 +523,7 @@
} }
} }
return dispatchQueue( return dispatchQueue(
context,
checkAttachmentLocalDeletion, checkAttachmentLocalDeletion,
argument_list, argument_list,
context._parallel_operation_attachment_amount context._parallel_operation_attachment_amount
...@@ -524,8 +532,7 @@ ...@@ -524,8 +532,7 @@
}); });
} }
function repairDocumentAttachment(context, id, skip_document_dict) {
function repairDocumentAttachment(id) {
var skip_attachment_dict = {}; var skip_attachment_dict = {};
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -533,6 +540,8 @@ ...@@ -533,6 +540,8 @@
context._check_local_attachment_creation || context._check_local_attachment_creation ||
context._check_local_attachment_deletion) { context._check_local_attachment_deletion) {
return pushDocumentAttachment( return pushDocumentAttachment(
context,
skip_document_dict,
skip_attachment_dict, skip_attachment_dict,
id, id,
context._local_sub_storage, context._local_sub_storage,
...@@ -557,6 +566,8 @@ ...@@ -557,6 +566,8 @@
context._check_remote_attachment_creation || context._check_remote_attachment_creation ||
context._check_remote_attachment_deletion) { context._check_remote_attachment_deletion) {
return pushDocumentAttachment( return pushDocumentAttachment(
context,
skip_document_dict,
skip_attachment_dict, skip_attachment_dict,
id, id,
context._remote_sub_storage, context._remote_sub_storage,
...@@ -579,7 +590,8 @@ ...@@ -579,7 +590,8 @@
}); });
} }
function propagateModification(source, destination, doc, hash, id, function propagateModification(context, source, destination, doc, hash, id,
skip_document_dict,
options) { options) {
var result, var result,
post_id, post_id,
...@@ -652,12 +664,12 @@ ...@@ -652,12 +664,12 @@
}); });
} }
function propagateDeletion(destination, id) { function propagateDeletion(context, destination, id, skip_document_dict) {
// Do not delete a document if it has an attachment // Do not delete a document if it has an attachment
// ie, replication should prevent losing user data // ie, replication should prevent losing user data
// Synchronize attachments before, to ensure // Synchronize attachments before, to ensure
// all of them will be deleted too // all of them will be deleted too
return repairDocumentAttachment(id) return repairDocumentAttachment(context, id, skip_document_dict)
.push(function () { .push(function () {
return destination.allAttachments(id); return destination.allAttachments(id);
}) })
...@@ -680,7 +692,8 @@ ...@@ -680,7 +692,8 @@
}); });
} }
function checkAndPropagate(status_hash, local_hash, doc, function checkAndPropagate(context, skip_document_dict,
status_hash, local_hash, doc,
source, destination, id, source, destination, id,
conflict_force, conflict_revert, conflict_force, conflict_revert,
conflict_ignore, conflict_ignore,
...@@ -721,10 +734,11 @@ ...@@ -721,10 +734,11 @@
// Modified only locally. No conflict or force // Modified only locally. No conflict or force
if (local_hash === null) { if (local_hash === null) {
// Deleted locally // Deleted locally
return propagateDeletion(destination, id); return propagateDeletion(context, destination, id,
skip_document_dict);
} }
return propagateModification(source, destination, doc, return propagateModification(context, source, destination, doc,
local_hash, id, local_hash, id, skip_document_dict,
{use_post: ((options.use_post) && {use_post: ((options.use_post) &&
(remote_hash === null))}); (remote_hash === null))});
} }
...@@ -738,14 +752,16 @@ ...@@ -738,14 +752,16 @@
// Automatically resolve conflict or force revert // Automatically resolve conflict or force revert
if (remote_hash === null) { if (remote_hash === null) {
// Deleted remotely // Deleted remotely
return propagateDeletion(source, id); return propagateDeletion(context, source, id, skip_document_dict);
} }
return propagateModification( return propagateModification(
context,
destination, destination,
source, source,
remote_doc, remote_doc,
remote_hash, remote_hash,
id, id,
skip_document_dict,
{use_post: ((options.use_revert_post) && {use_post: ((options.use_revert_post) &&
(local_hash === null))} (local_hash === null))}
); );
...@@ -754,8 +770,8 @@ ...@@ -754,8 +770,8 @@
// Minimize conflict if it can be resolved // Minimize conflict if it can be resolved
if (remote_hash === null) { if (remote_hash === null) {
// Copy remote modification remotely // Copy remote modification remotely
return propagateModification(source, destination, doc, return propagateModification(context, source, destination, doc,
local_hash, id, local_hash, id, skip_document_dict,
{use_post: options.use_post}); {use_post: options.use_post});
} }
throw new jIO.util.jIOError("Conflict on '" + id + "': " + throw new jIO.util.jIOError("Conflict on '" + id + "': " +
...@@ -765,7 +781,8 @@ ...@@ -765,7 +781,8 @@
}); });
} }
function checkLocalDeletion(queue, destination, id, source, function checkLocalDeletion(queue, context, skip_document_dict,
destination, id, source,
conflict_force, conflict_revert, conflict_force, conflict_revert,
conflict_ignore, options) { conflict_ignore, options) {
var status_hash; var status_hash;
...@@ -775,7 +792,8 @@ ...@@ -775,7 +792,8 @@
}) })
.push(function (result) { .push(function (result) {
status_hash = result.hash; status_hash = result.hash;
return checkAndPropagate(status_hash, null, null, return checkAndPropagate(context, skip_document_dict,
status_hash, null, null,
source, destination, id, source, destination, id,
conflict_force, conflict_revert, conflict_force, conflict_revert,
conflict_ignore, conflict_ignore,
...@@ -783,7 +801,8 @@ ...@@ -783,7 +801,8 @@
}); });
} }
function checkSignatureDifference(queue, source, destination, id, function checkSignatureDifference(queue, context, skip_document_dict,
source, destination, id,
conflict_force, conflict_revert, conflict_force, conflict_revert,
conflict_ignore, conflict_ignore,
is_creation, is_modification, is_creation, is_modification,
...@@ -813,7 +832,8 @@ ...@@ -813,7 +832,8 @@
status_hash = result_list[1].hash; status_hash = result_list[1].hash;
if (local_hash !== status_hash) { if (local_hash !== status_hash) {
return checkAndPropagate(status_hash, local_hash, doc, return checkAndPropagate(context, skip_document_dict,
status_hash, local_hash, doc,
source, destination, id, source, destination, id,
conflict_force, conflict_revert, conflict_force, conflict_revert,
conflict_ignore, conflict_ignore,
...@@ -822,7 +842,8 @@ ...@@ -822,7 +842,8 @@
}); });
} }
function pushStorage(source, destination, signature_allDocs, options) { function pushStorage(context, skip_document_dict,
source, destination, signature_allDocs, options) {
var argument_list = [], var argument_list = [],
argument_list_deletion = []; argument_list_deletion = [];
if (!options.hasOwnProperty("use_post")) { if (!options.hasOwnProperty("use_post")) {
...@@ -862,7 +883,8 @@ ...@@ -862,7 +883,8 @@
is_creation = !signature_dict.hasOwnProperty(key) is_creation = !signature_dict.hasOwnProperty(key)
&& options.check_creation; && options.check_creation;
if (is_modification === true || is_creation === true) { if (is_modification === true || is_creation === true) {
argument_list[i] = [undefined, source, destination, argument_list[i] = [undefined, context, skip_document_dict,
source, destination,
key, key,
options.conflict_force, options.conflict_force,
options.conflict_revert, options.conflict_revert,
...@@ -877,6 +899,7 @@ ...@@ -877,6 +899,7 @@
queue queue
.push(function () { .push(function () {
return dispatchQueue( return dispatchQueue(
context,
checkSignatureDifference, checkSignatureDifference,
argument_list, argument_list,
options.operation_amount options.operation_amount
...@@ -888,6 +911,8 @@ ...@@ -888,6 +911,8 @@
if (signature_dict.hasOwnProperty(key)) { if (signature_dict.hasOwnProperty(key)) {
if (!local_dict.hasOwnProperty(key)) { if (!local_dict.hasOwnProperty(key)) {
argument_list_deletion[i] = [undefined, argument_list_deletion[i] = [undefined,
context,
skip_document_dict,
destination, key, destination, key,
source, source,
options.conflict_force, options.conflict_force,
...@@ -900,6 +925,7 @@ ...@@ -900,6 +925,7 @@
} }
queue.push(function () { queue.push(function () {
return dispatchQueue( return dispatchQueue(
context,
checkLocalDeletion, checkLocalDeletion,
argument_list_deletion, argument_list_deletion,
options.operation_amount options.operation_amount
...@@ -910,12 +936,20 @@ ...@@ -910,12 +936,20 @@
}); });
} }
function repairDocument(queue, id) { function repairDocument(queue, context, id, skip_document_dict) {
queue.push(function () { queue.push(function () {
return repairDocumentAttachment(id); return repairDocumentAttachment(context, id, skip_document_dict);
}); });
} }
ReplicateStorage.prototype.repair = function () {
var context = this,
argument_list = arguments,
skip_document_dict = {};
// Do not sync the signature document
skip_document_dict[context._signature_hash] = null;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
// Ensure that the document storage is usable // Ensure that the document storage is usable
...@@ -967,7 +1001,8 @@ ...@@ -967,7 +1001,8 @@
if (context._check_local_modification || if (context._check_local_modification ||
context._check_local_creation || context._check_local_creation ||
context._check_local_deletion) { context._check_local_deletion) {
return pushStorage(context._local_sub_storage, return pushStorage(context, skip_document_dict,
context._local_sub_storage,
context._remote_sub_storage, context._remote_sub_storage,
signature_allDocs, signature_allDocs,
{ {
...@@ -993,7 +1028,8 @@ ...@@ -993,7 +1028,8 @@
if (context._check_remote_modification || if (context._check_remote_modification ||
context._check_remote_creation || context._check_remote_creation ||
context._check_remote_deletion) { context._check_remote_deletion) {
return pushStorage(context._remote_sub_storage, return pushStorage(context, skip_document_dict,
context._remote_sub_storage,
context._local_sub_storage, context._local_sub_storage,
signature_allDocs, { signature_allDocs, {
use_revert_post: context._use_remote_post, use_revert_post: context._use_remote_post,
...@@ -1022,17 +1058,19 @@ ...@@ -1022,17 +1058,19 @@
return context._signature_sub_storage.allDocs() return context._signature_sub_storage.allDocs()
.push(function (result) { .push(function (result) {
var i, var i,
argument_list = [], local_argument_list = [],
len = result.data.total_rows; len = result.data.total_rows;
for (i = 0; i < len; i += 1) { for (i = 0; i < len; i += 1) {
argument_list.push( local_argument_list.push(
[undefined, result.data.rows[i].id] [undefined, context, result.data.rows[i].id,
skip_document_dict]
); );
} }
return dispatchQueue( return dispatchQueue(
context,
repairDocument, repairDocument,
argument_list, local_argument_list,
context._parallel_operation_amount context._parallel_operation_amount
); );
}); });
......
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