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