Commit a76c4cef authored by Vincent Bechu's avatar Vincent Bechu

replicatestorage: not calculate hash if not needed

parent 754e5df9
......@@ -34,14 +34,20 @@
Synchronization status is stored for each document as an local attachment.
****************************************************/
function generateHash(content) {
function generateHash(content, context) {
// XXX Improve performance by moving calculation to WebWorker
return rusha.digestFromString(content);
if (context === undefined || context._calculate_document_hash) {
return rusha.digestFromString(content);
}
return "1";
}
function generateHashFromArrayBuffer(content) {
function generateHashFromArrayBuffer(content, context) {
// XXX Improve performance by moving calculation to WebWorker
return rusha.digestFromArrayBuffer(content);
if (context._calculate_attachment_hash) {
return rusha.digestFromArrayBuffer(content);
}
return "1";
}
function ReplicateStorage(spec) {
......@@ -140,6 +146,22 @@
if (this._check_remote_attachment_deletion === undefined) {
this._check_remote_attachment_deletion = false;
}
this._calculate_attachment_hash =
this._check_remote_attachment_modification ||
this._check_local_attachment_modification ||
((this._check_local_attachment_creation ||
this._conflict_handling === CONFLICT_KEEP_LOCAL) &&
(this._check_remote_attachment_creation ||
this._conflict_handling === CONFLICT_KEEP_REMOTE));
this._calculate_document_hash =
this._check_remote_modification ||
this._check_local_modification ||
((this._check_local_creation ||
this._conflict_handling === CONFLICT_KEEP_LOCAL) &&
(this._check_remote_creation ||
this._conflict_handling === CONFLICT_KEEP_REMOTE));
}
ReplicateStorage.prototype.remove = function (id) {
......@@ -277,7 +299,8 @@
})
.push(function (evt) {
return generateHashFromArrayBuffer(
evt.target.result
evt.target.result,
context
);
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
......@@ -395,7 +418,7 @@
})
.push(function (evt) {
var array_buffer = evt.target.result,
local_hash = generateHashFromArrayBuffer(array_buffer);
local_hash = generateHashFromArrayBuffer(array_buffer, context);
if (local_hash !== status_hash) {
return checkAndPropagateAttachment(skip_attachment_dict,
......@@ -687,7 +710,7 @@
options) {
return destination.get(id)
.push(function (remote_doc) {
return [remote_doc, generateHash(stringify(remote_doc))];
return [remote_doc, generateHash(stringify(remote_doc), context)];
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
......@@ -809,7 +832,7 @@
})
.push(function (result_list) {
var doc = result_list[0],
local_hash = generateHash(stringify(doc)),
local_hash = generateHash(stringify(doc), context),
status_hash = result_list[1].hash;
if (local_hash !== status_hash) {
......
......@@ -4649,6 +4649,130 @@
});
});
test("local creation, same document id: keep remote", function () {
stop();
expect(2);
var id,
context = this;
this.jio = jIO.createJIO({
type: "replicate",
conflict_handling: 2,
check_local_modification: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: false,
check_remote_deletion: false,
local_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
},
remote_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
}
});
context.jio.post({"title": "foobar"})
.then(function (result) {
id = result;
return context.jio.__storage._remote_sub_storage.put(
id,
{"title": "foo"}
);
})
.then(function () {
return context.jio.repair();
})
.then(function () {
return context.jio.__storage._signature_sub_storage.get(id);
})
.then(function (result) {
deepEqual(result, {
hash: "5ea9013447539ad65de308cbd75b5826a2ae30e5"
});
})
.then(function () {
return context.jio.get(id);
})
.then(function (result) {
deepEqual(result, {"title": "foo"});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("remote creation, same document id: keep local", function () {
stop();
expect(2);
var id,
context = this;
this.jio = jIO.createJIO({
type: "replicate",
conflict_handling: 1,
check_local_creation: false,
check_local_modification: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_deletion: false,
local_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
},
remote_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
}
});
context.jio.post({"title": "foo"})
.then(function (result) {
id = result;
return context.jio.__storage._remote_sub_storage.put(
id,
{"title": "foobar"}
);
})
.then(function () {
return context.jio.repair();
})
.then(function () {
return context.jio.__storage._signature_sub_storage.get(id);
})
.then(function (result) {
deepEqual(result, {
hash: "5ea9013447539ad65de308cbd75b5826a2ae30e5"
});
})
.then(function () {
return context.jio.get(id);
})
.then(function (result) {
deepEqual(result, {"title": "foo"});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// attachment replication
/////////////////////////////////////////////////////////////////
......@@ -4919,7 +5043,7 @@
})
.then(function (result) {
deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4"
hash: "1"
});
})
.fail(function (error) {
......@@ -5939,7 +6063,7 @@
})
.then(function (result) {
deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4"
hash: "1"
});
})
.fail(function (error) {
......@@ -6075,7 +6199,7 @@
})
.then(function (result) {
deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4"
hash: "1"
});
})
.fail(function (error) {
......@@ -8248,4 +8372,140 @@
});
});
test("local creation same attachment, keep remote", function () {
stop();
expect(2);
var id,
context = this,
blob = new Blob([big_string]),
blob2 = new Blob([big_string + "a"]);
this.jio = jIO.createJIO({
type: "replicate",
conflict_handling: 2,
check_local_attachment_creation: true,
local_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
},
remote_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
}
});
context.jio.post({"title": "foo"})
.then(function (result) {
id = result;
return context.jio.repair();
})
.then(function () {
return context.jio.__storage._remote_sub_storage
.putAttachment(id, "foo", blob);
})
.then(function () {
return context.jio.putAttachment(id, "foo", blob2);
})
.then(function () {
return context.jio.repair();
})
.then(function () {
return context.jio.getAttachment(
id,
"foo",
{format: "text"}
);
})
.then(function (result) {
equal(result, big_string);
return context.jio.__storage._signature_sub_storage
.getAttachment(id, "foo", {format: "json"});
})
.then(function (result) {
deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4"
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("remote creation same attachment, keep local", function () {
stop();
expect(2);
var id,
context = this,
blob = new Blob([big_string]),
blob2 = new Blob([big_string + "a"]);
this.jio = jIO.createJIO({
type: "replicate",
conflict_handling: 1,
check_remote_attachment_creation: true,
local_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
},
remote_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
}
});
context.jio.post({"title": "foo"})
.then(function (result) {
id = result;
return context.jio.repair();
})
.then(function () {
return context.jio.__storage._remote_sub_storage
.putAttachment(id, "foo", blob2);
})
.then(function () {
return context.jio.putAttachment(id, "foo", blob);
})
.then(function () {
return context.jio.repair();
})
.then(function () {
return context.jio.__storage._remote_sub_storage.getAttachment(
id,
"foo",
{format: "text"}
);
})
.then(function (result) {
equal(result, big_string);
return context.jio.__storage._signature_sub_storage
.getAttachment(id, "foo", {format: "json"});
})
.then(function (result) {
deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4"
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob, RSVP));
\ No newline at end of file
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