Commit c1a40dc3 authored by Tristan Cavelier's avatar Tristan Cavelier

revisionstorage.js improved to update branches with revision history objects

parent c3aae0e5
......@@ -25,6 +25,20 @@ jIO.addStorageType('revision', function (spec, my) {
return o;
};
/**
* Clones an object in deep (without functions)
* @method clone
* @param {any} object The object to clone
* @return {any} The cloned object
*/
priv.clone = function (object) {
var tmp = JSON.stringify(object);
if (tmp === undefined) {
return undefined;
}
return JSON.parse(tmp);
};
/**
* Generate a new uuid
* @method generateUuid
......@@ -69,6 +83,20 @@ jIO.addStorageType('revision', function (spec, my) {
return revision;
};
/**
* Convert the revision history object to an array of revisions.
* @method revisionHistoryToArray
* @param {object} revs The revision history
* @return {array} The revision array
*/
priv.revisionHistoryToArray = function (revs) {
var i, start = revs.start, newlist = [];
for (i = 0; i < revs.ids.length; i += 1, start -= 1) {
newlist.push(start + "-" + revs.ids[i]);
}
return newlist;
};
/**
* Generates the next revision of [previous_revision]. [string] helps us
* to generate a hash code.
......@@ -214,14 +242,20 @@ jIO.addStorageType('revision', function (spec, my) {
* Add a document revision branch to the document tree
* @method updateDocumentTree
* @param {object} doctree The document tree object
* @param {array} revs The revisions array
* @param {object|array} revs The revision history object or a revision array
* @param {boolean} deleted The deleted flag
* @param {array} The document revs_info
*/
priv.updateDocumentTree = function (doctree, revs, deleted) {
var revs_info, doctree_iterator, flag;
revs_info = [];
revs = JSON.parse(JSON.stringify(revs));
if (revs.ids) {
// revs is a revision history object
revs = priv.revisionHistoryToArray(revs);
} else {
// revs is an array of revisions
revs = priv.clone(revs);
}
doctree_iterator = doctree;
while (revs.length > 0) {
var i, rev = revs.pop(0);
......
......@@ -1361,7 +1361,8 @@ test ("Put", function(){
// put + revision history
o.doc = {
"_id": "put1",
"_revs": ["3-rh3", "2-rh2", "1-rh1"],
//"_revs": ["3-rh3", "2-rh2", "1-rh1"], // same as below
"_revs": {"start": 3, "ids": ["rh3", "rh2", "rh1"]},
"title": "myPut3"
};
o.spy (o, "value", {"id": "put1", "ok": true, "rev": "3-rh3"},
......
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