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) { ...@@ -25,6 +25,20 @@ jIO.addStorageType('revision', function (spec, my) {
return o; 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 * Generate a new uuid
* @method generateUuid * @method generateUuid
...@@ -69,6 +83,20 @@ jIO.addStorageType('revision', function (spec, my) { ...@@ -69,6 +83,20 @@ jIO.addStorageType('revision', function (spec, my) {
return revision; 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 * Generates the next revision of [previous_revision]. [string] helps us
* to generate a hash code. * to generate a hash code.
...@@ -214,14 +242,20 @@ jIO.addStorageType('revision', function (spec, my) { ...@@ -214,14 +242,20 @@ jIO.addStorageType('revision', function (spec, my) {
* Add a document revision branch to the document tree * Add a document revision branch to the document tree
* @method updateDocumentTree * @method updateDocumentTree
* @param {object} doctree The document tree object * @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 {boolean} deleted The deleted flag
* @param {array} The document revs_info * @param {array} The document revs_info
*/ */
priv.updateDocumentTree = function (doctree, revs, deleted) { priv.updateDocumentTree = function (doctree, revs, deleted) {
var revs_info, doctree_iterator, flag; var revs_info, doctree_iterator, flag;
revs_info = []; 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; doctree_iterator = doctree;
while (revs.length > 0) { while (revs.length > 0) {
var i, rev = revs.pop(0); var i, rev = revs.pop(0);
......
...@@ -1361,7 +1361,8 @@ test ("Put", function(){ ...@@ -1361,7 +1361,8 @@ test ("Put", function(){
// put + revision history // put + revision history
o.doc = { o.doc = {
"_id": "put1", "_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" "title": "myPut3"
}; };
o.spy (o, "value", {"id": "put1", "ok": true, "rev": "3-rh3"}, 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