Commit f87b8f3c authored by Tristan Cavelier's avatar Tristan Cavelier

mergeRemoteTree replaced by mergeDocumentTree

parent fbe16407
...@@ -345,88 +345,69 @@ var utilities = { ...@@ -345,88 +345,69 @@ var utilities = {
}, },
/** /**
* @method mergeRemoteTree - merge revs_info into current tree * Merges document object trees
* @param {docTreeNode} object - document tree * @method mergeDocumentTree
* @param {old_rev} string - revision of the tree node to set to "branch" * @param {object} document_tree_list The document tree array
* @param {new_rev } string - revison of the tree node to add as leaf
* @param {revs_info} object - these revisions need to be checked and added
* @param {addNodes} object - array for nodes to add to the tree
* @info: - old_rev = null here and it's not needed
* because new_rev will be used as the revision
* for the new tree node (we are copy&pasting
* from another storage)
*/ */
mergeRemoteTree : function (initialTree, docTreeNode, old_rev, new_rev, mergeDocumentTree: function (document_tree_list) {
newDocumentRevisions, addNodes, onTree, deletedLeaf){ var arrayConcat, each, synchronizeFrom, tree_list;
var sync_rev = newDocumentRevisions[0].rev, // arrayConcat([1,2], [3,4]) = [1,2,3,4]
current_tree_rev = docTreeNode['rev'], arrayConcat = function () {
kids = docTreeNode['kids'], var i,newlist=[];
nodeStatus = 'available', for (j=0; j<arguments.length; ++j) {
addNodesLen, for (i=0; i<arguments[j].length; ++i) {
numberOfKids, newlist.push(arguments[j][i]);
key,
i,
j;
for (key in docTreeNode) {
if (key === "rev") {
// common ancestor? = does the revision on the current
// tree node match the currently checked remote tree
// revision
// match = common ancestor
if (sync_rev === current_tree_rev){
onTree = true;
// in order to loop we also add the revision of
// the common ancestor node to the array
// using push!
addNodes.unshift( current_tree_rev );
// get length, now that we need it
addNodesLen = utilities.isObjectSize( addNodes )-1;
// the addNodes array will now look like this
// [current_node, all_missing_nodes]
for (j = 0; j < addNodesLen; j+=1){
// last node being added is deleted
if (deletedLeaf === true && j === addNodesLen-1){
nodeStatus = 'deleted';
}
utilities.setTreeNode(initialTree, addNodes[j],
addNodes[j+1], nodeStatus);
}
// no match = continue down the tree
} else if ( utilities.isObjectEmpty( kids ) === false ){
numberOfKids = utilities.isObjectSize( kids );
for ( i = 0; i < numberOfKids; i+=1 ){
utilities.mergeRemoteTree( initialTree, kids[i], old_rev, new_rev,
newDocumentRevisions, addNodes, onTree, deletedLeaf );
} }
}
return newlist;
};
// end of tree = start over checking the next remote revision // "each" executes "fun" on each values of "array"
} else if ( onTree === false ){ // if return false, then stop browsing
each = function (array, fun, start) {
// revision from _revs_info was not found in tree. var i;
// add it to addNodes, remove it from newDocumentRevisions for (i = start || 0; i < array.length; i += 1) {
// call mergeRemoteTree again with new modified arrays if (fun(array[i], i) === false) {
// until a common ancestor is found return false;
// remember to add this revision once an ancestor is found
// we use push here, because we later loop this from 0 to x
addNodes.unshift(sync_rev);
// pop it off the revs_info
newDocumentRevisions.shift();
// this should start over with the full document tree
// otherwise it will only continue on the current (last) node
utilities.mergeRemoteTree( initialTree, initialTree, old_rev, new_rev,
newDocumentRevisions, addNodes, onTree, deletedLeaf );
} }
} }
return true;
};
// merges all trees
synchronize = function (tree_list) {
var new_children;
new_children = [];
each(tree_list, function (tree, tree_index) {
var res;
if (new_children.length === 0) {
new_children.push(tree);
return;
}
res = each(new_children, function (child, child_index) {
if (tree.rev === child.rev) {
new_children[child_index].children = synchronize(
arrayConcat(
tree.children,
child.children
)
)
return false;
} }
return docTreeNode; });
if (res === true) {
new_children.push(tree);
}
});
return new_children;
};
tree_list = [];
each(document_tree_list, function (tree) {
tree_list = arrayConcat(tree_list, tree.children);
});
return {"children":synchronize(tree_list)};
}, },
getActiveLeaves : function (docTreeNode) { getActiveLeaves : function (docTreeNode) {
......
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