Commit 90b51d10 authored by Tristan Cavelier's avatar Tristan Cavelier

wip: smart post on storage.js

parent c8386265
...@@ -290,16 +290,78 @@ var storage = function(spec, my) { ...@@ -290,16 +290,78 @@ var storage = function(spec, my) {
}; };
that.post = function (command) { that.post = function (command) {
var docid, option; setTimeout(function () {
docid = command.getDocId(); var f, options, document_tree, doc, prev_rev;
option = command.cloneOption(); f = {};
// check if the tree already exists options = command.cloneOption();
that._get ( options["max_retry"] = options["max_retry"] || 3;
docid + '.tree.json', f.begin = function () {
option, prev_rev = command.getDocInfo("_rev");
if (typeof prev_rev === "string" &&
// if the tree does not exists yet !that.checkRevisionFormat(prev_rev)) {
// if the tree exists // if the previous revision given is bad
that.error(that.createErrorObject(
400, "Bad Request", "Invalid rev format"
));
return;
}
doc = that.createDocument(
command.getDoc() || {},
command.getDocId() || that.generateUuid(),
prev_rev
);
// the previous revision is correct
prev_rev = that.revisionToArray(prev_rev);
f.getDocumentTree();
};
// check if the tree already exists
f.getDocumentTree = function () {
that.addJob(
'_get',
that.serialized(),
doc._id+'.tree.json',
options,
function (response) {
// if the tree exists
document_tree = response;
f.postDocument();
},function (error) {
if (error.status === 404) {
// if the tree does not exists yet
document_tree = that.createDocumentTree();
f.postDocument();
} else {
that.error(that.createErrorObject(
error.status, error.statusText,
"Unable to get the revision tree"
));
}
}
);
};
f.postDocument = function () {
that.addJob(
'_post',
that.serialized(),
doc._id+'.'+doc._rev,
options,
function (response) {
f.putDocumentTree()
},function (error) {
that.error(that.createErrorObject(
error.status, error.statusText, error
));
}
);
};
f.putDocumentTree = function () {
if (!that.addDocumentToDocumentTree(doc)) {
// conflict!
}
// xxx
};
f.begin();
});
}; };
that._post = function () { that._post = function () {
......
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