Commit 1ef272aa authored by Romain Courteaud's avatar Romain Courteaud

ReplicateStorage: allow to new document with a remote.post

parent 3c3d9a39
......@@ -51,6 +51,8 @@
document_id: this._signature_hash,
sub_storage: spec.local_sub_storage
});
this._use_remote_post = spec.use_remote_post || false;
}
ReplicateStorage.prototype.remove = function (id) {
......@@ -95,19 +97,34 @@
// Do not sync the signature document
skip_document_dict[context._signature_hash] = null;
function propagateModification(destination, doc, hash, id) {
return destination.put(id, doc)
function propagateModification(destination, doc, hash, id, options) {
var result,
to_skip = true;
if (options === undefined) {
options = {};
}
if (options.use_post) {
result = destination.post(doc)
.push(function () {
to_skip = false;
});
} else {
result = destination.put(id, doc);
}
return result
.push(function () {
return context._signature_sub_storage.put(id, {
"hash": hash
});
})
.push(function () {
if (to_skip) {
skip_document_dict[id] = null;
}
});
}
function checkLocalCreation(queue, source, destination, id) {
function checkLocalCreation(queue, source, destination, id, options) {
var remote_doc;
queue
.push(function () {
......@@ -133,7 +150,8 @@
var local_hash = generateHash(JSON.stringify(doc)),
remote_hash;
if (remote_doc === undefined) {
return propagateModification(destination, doc, local_hash, id);
return propagateModification(destination, doc, local_hash, id,
options);
}
remote_hash = generateHash(JSON.stringify(remote_doc));
......@@ -234,8 +252,11 @@
});
}
function pushStorage(source, destination) {
function pushStorage(source, destination, options) {
var queue = new RSVP.Queue();
if (!options.hasOwnProperty("use_post")) {
options.use_post = false;
}
return queue
.push(function () {
return RSVP.all([
......@@ -265,7 +286,7 @@
for (key in local_dict) {
if (local_dict.hasOwnProperty(key)) {
if (!signature_dict.hasOwnProperty(key)) {
checkLocalCreation(queue, source, destination, key);
checkLocalCreation(queue, source, destination, key, options);
}
}
}
......@@ -319,11 +340,12 @@
.push(function () {
return pushStorage(context._local_sub_storage,
context._remote_sub_storage);
context._remote_sub_storage,
{use_post: context._use_remote_post});
})
.push(function () {
return pushStorage(context._remote_sub_storage,
context._local_sub_storage);
context._local_sub_storage, {});
});
};
......
......@@ -45,6 +45,7 @@
equal(jio.__storage._remote_sub_storage.__type, "replicatestorage500");
deepEqual(jio.__storage._query_options, {});
equal(jio.__storage._use_remote_post, false);
equal(jio.__storage._signature_hash,
"_replicate_7209dfbcaff00f6637f939fdd71fa896793ed385");
......@@ -62,7 +63,7 @@
});
test("accept query", function () {
test("accept parameters", function () {
var jio = jIO.createJIO({
type: "replicate",
local_sub_storage: {
......@@ -71,13 +72,15 @@
remote_sub_storage: {
type: "replicatestorage500"
},
query: {query: 'portal_type: "Foo"', limit: [0, 1234567890]}
query: {query: 'portal_type: "Foo"', limit: [0, 1234567890]},
use_remote_post: true
});
deepEqual(
jio.__storage._query_options,
{query: 'portal_type: "Foo"', limit: [0, 1234567890]}
);
equal(jio.__storage._use_remote_post, true);
equal(jio.__storage._signature_hash,
"_replicate_623653d45a4e770a2c9f6b71e3144d18ee1b5bec");
......@@ -431,6 +434,92 @@
});
});
test("local document creation and use remote post", function () {
stop();
expect(10);
var id,
post_id,
context = this;
this.jio = jIO.createJIO({
type: "replicate",
use_remote_post: 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();
})
// Document 'id' has been deleted in both storages
.then(function () {
return context.jio.__storage._remote_sub_storage.get(id);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + id);
equal(error.status_code, 404);
})
.then(function () {
return context.jio.__storage._local_sub_storage.get(id);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + id);
equal(error.status_code, 404);
})
// But another document should have been created
.then(function () {
return context.jio.__storage._remote_sub_storage.allDocs();
})
.then(function (result) {
equal(result.data.total_rows, 1);
post_id = result.data.rows[0].id;
return context.jio.__storage._remote_sub_storage.get(post_id);
})
.then(function (result) {
deepEqual(result, {
title: "foo"
});
})
.then(function () {
return context.jio.__storage._local_sub_storage.get(post_id);
})
.then(function (result) {
deepEqual(result, {
title: "foo"
});
})
.then(function () {
return context.jio.__storage._signature_sub_storage.get(post_id);
})
.then(function (result) {
deepEqual(result, {
hash: "5ea9013447539ad65de308cbd75b5826a2ae30e5"
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("remote document creation", function () {
stop();
expect(2);
......
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