Commit b067eb2b authored by Romain Courteaud's avatar Romain Courteaud

Release version 3.12.0

Fix ReplicateStorage to reduce conflicts during synchronisation.
parent f2919d25
...@@ -7458,11 +7458,17 @@ return new Parser; ...@@ -7458,11 +7458,17 @@ return new Parser;
}; };
}(window, moment)); }(window, moment));
;/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob, ;/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array */ FileReader, ArrayBuffer, Uint8Array, navigator */
(function (window, RSVP, Blob, QueryFactory, Query, atob, (function (window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array) { FileReader, ArrayBuffer, Uint8Array, navigator) {
"use strict"; "use strict";
if (window.openDatabase === undefined) {
window.openDatabase = function () {
throw new Error('WebSQL is not supported by ' + navigator.userAgent);
};
}
var util = {}, var util = {},
jIO; jIO;
...@@ -7569,6 +7575,38 @@ return new Parser; ...@@ -7569,6 +7575,38 @@ return new Parser;
} }
util.readBlobAsDataURL = readBlobAsDataURL; util.readBlobAsDataURL = readBlobAsDataURL;
function stringify(obj) {
// Implement a stable JSON.stringify
// Object's keys are alphabetically ordered
var key,
key_list,
i,
value,
result_list;
if (obj.constructor === Object) {
key_list = Object.keys(obj).sort();
result_list = [];
for (i = 0; i < key_list.length; i += 1) {
key = key_list[i];
value = stringify(obj[key]);
if (value !== undefined) {
result_list.push(stringify(key) + ':' + value);
}
}
return '{' + result_list.join(',') + '}';
}
if (obj.constructor === Array) {
result_list = [];
for (i = 0; i < obj.length; i += 1) {
result_list.push(stringify(obj[i]));
}
return '[' + result_list.join(',') + ']';
}
return JSON.stringify(obj);
}
util.stringify = stringify;
// https://gist.github.com/davoclavo/4424731 // https://gist.github.com/davoclavo/4424731
function dataURItoBlob(dataURI) { function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string // convert base64 to raw binary data held in a string
...@@ -7931,7 +7969,7 @@ return new Parser; ...@@ -7931,7 +7969,7 @@ return new Parser;
window.jIO = jIO; window.jIO = jIO;
}(window, RSVP, Blob, QueryFactory, Query, atob, }(window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array)); FileReader, ArrayBuffer, Uint8Array, navigator));
;/* ;/*
* Rusha, a JavaScript implementation of the Secure Hash Algorithm, SHA-1, * Rusha, a JavaScript implementation of the Secure Hash Algorithm, SHA-1,
* as defined in FIPS PUB 180-1, tuned for high performance with large inputs. * as defined in FIPS PUB 180-1, tuned for high performance with large inputs.
...@@ -8370,7 +8408,7 @@ return new Parser; ...@@ -8370,7 +8408,7 @@ return new Parser;
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global jIO, RSVP, Rusha*/ /*global jIO, RSVP, Rusha*/
(function (jIO, RSVP, Rusha) { (function (jIO, RSVP, Rusha, stringify) {
"use strict"; "use strict";
var rusha = new Rusha(), var rusha = new Rusha(),
...@@ -8397,9 +8435,9 @@ return new Parser; ...@@ -8397,9 +8435,9 @@ return new Parser;
this._remote_sub_storage = jIO.createJIO(spec.remote_sub_storage); this._remote_sub_storage = jIO.createJIO(spec.remote_sub_storage);
this._signature_hash = "_replicate_" + generateHash( this._signature_hash = "_replicate_" + generateHash(
JSON.stringify(spec.local_sub_storage) + stringify(spec.local_sub_storage) +
JSON.stringify(spec.remote_sub_storage) + stringify(spec.remote_sub_storage) +
JSON.stringify(this._query_options) stringify(this._query_options)
); );
this._signature_sub_storage = jIO.createJIO({ this._signature_sub_storage = jIO.createJIO({
type: "document", type: "document",
...@@ -8540,90 +8578,6 @@ return new Parser; ...@@ -8540,90 +8578,6 @@ return new Parser;
}); });
} }
function checkLocalCreation(queue, source, destination, id, options,
getMethod) {
var remote_doc;
queue
.push(function () {
return destination.get(id);
})
.push(function (doc) {
remote_doc = doc;
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
// This document was never synced.
// Push it to the remote storage and store sync information
return;
}
throw error;
})
.push(function () {
// This document was never synced.
// Push it to the remote storage and store sync information
return getMethod(id);
})
.push(function (doc) {
var local_hash = generateHash(JSON.stringify(doc)),
remote_hash;
if (remote_doc === undefined) {
return propagateModification(source, destination, doc, local_hash,
id, options);
}
remote_hash = generateHash(JSON.stringify(remote_doc));
if (local_hash === remote_hash) {
// Same document
return context._signature_sub_storage.put(id, {
"hash": local_hash
})
.push(function () {
skip_document_dict[id] = null;
});
}
if (options.conflict_ignore === true) {
return;
}
if (options.conflict_force === true) {
return propagateModification(source, destination, doc, local_hash,
id, options);
}
// Already exists on destination
throw new jIO.util.jIOError("Conflict on '" + id + "': " +
JSON.stringify(doc) + " !== " +
JSON.stringify(remote_doc),
409);
});
}
function checkBulkLocalCreation(queue, source, destination, id_list,
options) {
queue
.push(function () {
return source.bulk(id_list);
})
.push(function (result_list) {
var i,
sub_queue = new RSVP.Queue();
function getResult(j) {
return function (id) {
if (id !== id_list[j].parameter_list[0]) {
throw new Error("Does not access expected ID " + id);
}
return result_list[j];
};
}
for (i = 0; i < result_list.length; i += 1) {
checkLocalCreation(sub_queue, source, destination,
id_list[i].parameter_list[0],
options, getResult(i));
}
return sub_queue;
});
}
function checkLocalDeletion(queue, destination, id, source) { function checkLocalDeletion(queue, destination, id, source) {
var status_hash; var status_hash;
queue queue
...@@ -8634,7 +8588,7 @@ return new Parser; ...@@ -8634,7 +8588,7 @@ return new Parser;
status_hash = result.hash; status_hash = result.hash;
return destination.get(id) return destination.get(id)
.push(function (doc) { .push(function (doc) {
var remote_hash = generateHash(JSON.stringify(doc)); var remote_hash = generateHash(stringify(doc));
if (remote_hash === status_hash) { if (remote_hash === status_hash) {
return destination.remove(id) return destination.remove(id)
.push(function () { .push(function () {
...@@ -8663,24 +8617,37 @@ return new Parser; ...@@ -8663,24 +8617,37 @@ return new Parser;
function checkSignatureDifference(queue, source, destination, id, function checkSignatureDifference(queue, source, destination, id,
conflict_force, conflict_ignore, conflict_force, conflict_ignore,
getMethod) { is_creation, is_modification,
getMethod, options) {
queue queue
.push(function () { .push(function () {
// Optimisation to save a get call to signature storage
if (is_creation === true) {
return RSVP.all([
getMethod(id),
{hash: undefined}
]);
}
if (is_modification === true) {
return RSVP.all([ return RSVP.all([
getMethod(id), getMethod(id),
context._signature_sub_storage.get(id) context._signature_sub_storage.get(id)
]); ]);
}
throw new jIO.util.jIOError("Unexpected call of"
+ " checkSignatureDifference",
409);
}) })
.push(function (result_list) { .push(function (result_list) {
var doc = result_list[0], var doc = result_list[0],
local_hash = generateHash(JSON.stringify(doc)), local_hash = generateHash(stringify(doc)),
status_hash = result_list[1].hash; status_hash = result_list[1].hash;
if (local_hash !== status_hash) { if (local_hash !== status_hash) {
// Local modifications // Local modifications
return destination.get(id) return destination.get(id)
.push(function (remote_doc) { .push(function (remote_doc) {
var remote_hash = generateHash(JSON.stringify(remote_doc)); var remote_hash = generateHash(stringify(remote_doc));
if (remote_hash !== status_hash) { if (remote_hash !== status_hash) {
// Modifications on both sides // Modifications on both sides
if (local_hash === remote_hash) { if (local_hash === remote_hash) {
...@@ -8697,19 +8664,29 @@ return new Parser; ...@@ -8697,19 +8664,29 @@ return new Parser;
} }
if (conflict_force !== true) { if (conflict_force !== true) {
throw new jIO.util.jIOError("Conflict on '" + id + "': " + throw new jIO.util.jIOError("Conflict on '" + id + "': " +
JSON.stringify(doc) + " !== " + stringify(doc) + " !== " +
JSON.stringify(remote_doc), stringify(remote_doc),
409); 409);
} }
} }
return propagateModification(source, destination, doc, return propagateModification(source, destination, doc,
local_hash, id); local_hash, id);
}, function (error) { }, function (error) {
var use_post;
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) { (error.status_code === 404)) {
// Document has been deleted remotely if (is_creation) {
// Remote document does not exists, create it following
// provided options
use_post = options.use_post;
} else {
// Remote document has been erased, put it to save
// modification
use_post = false;
}
return propagateModification(source, destination, doc, return propagateModification(source, destination, doc,
local_hash, id); local_hash, id,
{use_post: use_post});
} }
throw error; throw error;
}); });
...@@ -8718,6 +8695,7 @@ return new Parser; ...@@ -8718,6 +8695,7 @@ return new Parser;
} }
function checkBulkSignatureDifference(queue, source, destination, id_list, function checkBulkSignatureDifference(queue, source, destination, id_list,
document_status_list, options,
conflict_force, conflict_ignore) { conflict_force, conflict_ignore) {
queue queue
.push(function () { .push(function () {
...@@ -8740,7 +8718,9 @@ return new Parser; ...@@ -8740,7 +8718,9 @@ return new Parser;
checkSignatureDifference(sub_queue, source, destination, checkSignatureDifference(sub_queue, source, destination,
id_list[i].parameter_list[0], id_list[i].parameter_list[0],
conflict_force, conflict_ignore, conflict_force, conflict_ignore,
getResult(i)); document_status_list[i].is_creation,
document_status_list[i].is_modification,
getResult(i), options);
} }
return sub_queue; return sub_queue;
}); });
...@@ -8761,9 +8741,11 @@ return new Parser; ...@@ -8761,9 +8741,11 @@ return new Parser;
.push(function (result_list) { .push(function (result_list) {
var i, var i,
local_dict = {}, local_dict = {},
new_list = [], document_list = [],
change_list = [], document_status_list = [],
signature_dict = {}, signature_dict = {},
is_modification,
is_creation,
key; key;
for (i = 0; i < result_list[0].data.total_rows; i += 1) { for (i = 0; i < result_list[0].data.total_rows; i += 1) {
if (!skip_document_dict.hasOwnProperty( if (!skip_document_dict.hasOwnProperty(
...@@ -8779,54 +8761,46 @@ return new Parser; ...@@ -8779,54 +8761,46 @@ return new Parser;
signature_dict[result_list[1].data.rows[i].id] = i; signature_dict[result_list[1].data.rows[i].id] = i;
} }
} }
if (options.check_creation === true) {
for (key in local_dict) { for (key in local_dict) {
if (local_dict.hasOwnProperty(key)) { if (local_dict.hasOwnProperty(key)) {
if (!signature_dict.hasOwnProperty(key)) { is_modification = signature_dict.hasOwnProperty(key)
&& options.check_modification;
is_creation = !signature_dict.hasOwnProperty(key)
&& options.check_creation;
if (is_modification === true || is_creation === true) {
if (options.use_bulk_get === true) { if (options.use_bulk_get === true) {
new_list.push({ document_list.push({
method: "get", method: "get",
parameter_list: [key] parameter_list: [key]
}); });
} else { document_status_list.push({
checkLocalCreation(queue, source, destination, key, is_creation: is_creation,
options, source.get.bind(source)); is_modification: is_modification
}
}
}
}
if ((options.use_bulk_get === true) && (new_list.length !== 0)) {
checkBulkLocalCreation(queue, source, destination, new_list,
options);
}
}
for (key in signature_dict) {
if (signature_dict.hasOwnProperty(key)) {
if (local_dict.hasOwnProperty(key)) {
if (options.check_modification === true) {
if (options.use_bulk_get === true) {
change_list.push({
method: "get",
parameter_list: [key]
}); });
} else { } else {
checkSignatureDifference(queue, source, destination, key, checkSignatureDifference(queue, source, destination, key,
options.conflict_force, options.conflict_force,
options.conflict_ignore, options.conflict_ignore,
source.get.bind(source)); is_creation, is_modification,
source.get.bind(source),
options);
}
}
} }
} }
} else {
if (options.check_deletion === true) { if (options.check_deletion === true) {
for (key in signature_dict) {
if (signature_dict.hasOwnProperty(key)) {
if (!local_dict.hasOwnProperty(key)) {
checkLocalDeletion(queue, destination, key, source); checkLocalDeletion(queue, destination, key, source);
} }
} }
} }
} }
if ((options.use_bulk_get === true) && (change_list.length !== 0)) { if ((options.use_bulk_get === true) && (document_list.length !== 0)) {
checkBulkSignatureDifference(queue, source, destination, checkBulkSignatureDifference(queue, source, destination,
change_list, document_list, document_status_list,
options,
options.conflict_force, options.conflict_force,
options.conflict_ignore); options.conflict_ignore);
} }
...@@ -8921,7 +8895,7 @@ return new Parser; ...@@ -8921,7 +8895,7 @@ return new Parser;
jIO.addStorage('replicate', ReplicateStorage); jIO.addStorage('replicate', ReplicateStorage);
}(jIO, RSVP, Rusha)); }(jIO, RSVP, Rusha, jIO.util.stringify));
;/* ;/*
* Copyright 2015, Nexedi SA * Copyright 2015, Nexedi SA
* Released under the LGPL license. * Released under the LGPL license.
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
{ {
"name": "jio", "name": "jio",
"version": "v3.11.0", "version": "v3.12.0",
"license": "LGPLv3", "license": "LGPLv3",
"author": "Nexedi SA", "author": "Nexedi SA",
"contributors": [ "contributors": [
......
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