Commit 6823e03e authored by Romain Courteaud's avatar Romain Courteaud

ERP5Storage: directly create local document when posting.

This allow to not check the document in the second step of synchro.
parent 64cc2e3b
......@@ -97,26 +97,45 @@
// Do not sync the signature document
skip_document_dict[context._signature_hash] = null;
function propagateModification(destination, doc, hash, id, options) {
function propagateModification(source, destination, doc, hash, id,
options) {
var result,
post_id,
to_skip = true;
if (options === undefined) {
options = {};
}
if (options.use_post) {
result = destination.post(doc)
.push(function () {
.push(function (new_id) {
to_skip = false;
post_id = new_id;
return source.put(post_id, doc);
})
.push(function () {
return source.remove(id);
})
.push(function () {
return context._signature_sub_storage.remove(id);
})
.push(function () {
to_skip = true;
return context._signature_sub_storage.put(post_id, {
"hash": hash
});
})
.push(function () {
skip_document_dict[post_id] = null;
});
} else {
result = destination.put(id, doc);
result = destination.put(id, doc)
.push(function () {
return context._signature_sub_storage.put(id, {
"hash": hash
});
});
}
return result
.push(function () {
return context._signature_sub_storage.put(id, {
"hash": hash
});
})
.push(function () {
if (to_skip) {
skip_document_dict[id] = null;
......@@ -150,8 +169,8 @@
var local_hash = generateHash(JSON.stringify(doc)),
remote_hash;
if (remote_doc === undefined) {
return propagateModification(destination, doc, local_hash, id,
options);
return propagateModification(source, destination, doc, local_hash,
id, options);
}
remote_hash = generateHash(JSON.stringify(remote_doc));
......@@ -192,7 +211,8 @@
}
// Modifications on remote side
// Push them locally
return propagateModification(source, doc, remote_hash, id);
return propagateModification(destination, source, doc,
remote_hash, id);
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
......@@ -238,13 +258,14 @@
throw new jIO.util.jIOError("Conflict on '" + id + "'",
409);
}
return propagateModification(destination, doc, local_hash, id);
return propagateModification(source, destination, doc,
local_hash, id);
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
// Document has been deleted remotely
return propagateModification(destination, doc, local_hash,
id);
return propagateModification(source, destination, doc,
local_hash, id);
}
throw error;
});
......
......@@ -436,7 +436,7 @@
test("local document creation and use remote post", function () {
stop();
expect(10);
expect(12);
var id,
post_id,
......@@ -481,6 +481,16 @@
equal(error.message, "Cannot find document: " + id);
equal(error.status_code, 404);
})
.then(function () {
return context.jio.__storage._signature_sub_storage.get(id);
})
.then(function () {
ok(false, "Signature should have been deleted");
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 404);
})
// But another document should have been created
.then(function () {
......@@ -520,6 +530,117 @@
});
});
test("local document creation, remote post and delayed allDocs", function () {
stop();
expect(11);
var id,
post_id = "_foobar",
context = this;
function Storage200DelayedAllDocs(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
Storage200DelayedAllDocs.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
Storage200DelayedAllDocs.prototype.post = function (param) {
return this.put(post_id, param);
};
Storage200DelayedAllDocs.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments);
};
Storage200DelayedAllDocs.prototype.hasCapacity = function () {
return true;
};
Storage200DelayedAllDocs.prototype.buildQuery = function () {
return [];
};
jIO.addStorage(
'replicatestorage200delayedalldocs',
Storage200DelayedAllDocs
);
this.jio = jIO.createJIO({
type: "replicate",
use_remote_post: true,
local_sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
},
remote_sub_storage: {
type: "replicatestorage200delayedalldocs",
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);
})
.then(function () {
return context.jio.__storage._signature_sub_storage.get(id);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 404);
})
// But another document should have been created
.then(function () {
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