diff --git a/src/jio.storage/revisionstorage.js b/src/jio.storage/revisionstorage.js index 77593cd1e7c89c40d908ce77ea7a13fe5157f6fc..dcbe17fad36c4836fcb4dafe408c04812aa94352 100644 --- a/src/jio.storage/revisionstorage.js +++ b/src/jio.storage/revisionstorage.js @@ -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); diff --git a/test/jiotests.js b/test/jiotests.js index 3186f54e27c51e60022382eb71e49ec84d22c4e2..79d178cb8a71b9e60a0b5e8e8f25a0900cbef439 100644 --- a/test/jiotests.js +++ b/test/jiotests.js @@ -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"},