Commit 4f5abd39 authored by Vincent Bechu's avatar Vincent Bechu

replicatestorage: limit calculation hash for attachment

parent 603f4dda
...@@ -39,8 +39,11 @@ ...@@ -39,8 +39,11 @@
return rusha.digestFromString(content); return rusha.digestFromString(content);
} }
function generateHashFromArrayBuffer(content) { function generateHashFromArrayBuffer(content, context) {
// XXX Improve performance by moving calculation to WebWorker // XXX Improve performance by moving calculation to WebWorker
if (context._skip_attachment_hash) {
return "1";
}
return rusha.digestFromArrayBuffer(content); return rusha.digestFromArrayBuffer(content);
} }
...@@ -145,6 +148,14 @@ ...@@ -145,6 +148,14 @@
if (this._check_remote_attachment_deletion === undefined) { if (this._check_remote_attachment_deletion === undefined) {
this._check_remote_attachment_deletion = false; this._check_remote_attachment_deletion = false;
} }
this._skip_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));
} }
ReplicateStorage.prototype.remove = function (id) { ReplicateStorage.prototype.remove = function (id) {
...@@ -282,7 +293,8 @@ ...@@ -282,7 +293,8 @@
}) })
.push(function (evt) { .push(function (evt) {
return generateHashFromArrayBuffer( return generateHashFromArrayBuffer(
evt.target.result evt.target.result,
context
); );
}, function (error) { }, function (error) {
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
...@@ -400,7 +412,7 @@ ...@@ -400,7 +412,7 @@
}) })
.push(function (evt) { .push(function (evt) {
var array_buffer = evt.target.result, var array_buffer = evt.target.result,
local_hash = generateHashFromArrayBuffer(array_buffer); local_hash = generateHashFromArrayBuffer(array_buffer, context);
if (local_hash !== status_hash) { if (local_hash !== status_hash) {
return checkAndPropagateAttachment(skip_attachment_dict, return checkAndPropagateAttachment(skip_attachment_dict,
......
...@@ -4947,7 +4947,7 @@ ...@@ -4947,7 +4947,7 @@
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4" hash: "1"
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -5967,7 +5967,7 @@ ...@@ -5967,7 +5967,7 @@
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4" hash: "1"
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -6103,7 +6103,7 @@ ...@@ -6103,7 +6103,7 @@
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
hash: "cd762363c1c11ecb48611583520bba111f0034d4" hash: "1"
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -8276,4 +8276,140 @@ ...@@ -8276,4 +8276,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)); }(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