Add utility method to remove unmerged branches
@@ -401,7 +401,7 @@ var oak = (function(global){
* @method removeCollisions
* @param {string} path the path of a document
* @param {number} clusterId collision markers for this clusterId will be removed.
- * @param {number} (optional) limit maximum number of collision markers to remove, default is 1000000.
+ * @param {number} [limit=1000000] maximum number of collision markers to remove.
* @returns {object} the result of the MongoDB update.
*/
api.removeCollisions = function(path, clusterId, limit) {
@@ -452,6 +452,70 @@ var oak = (function(global){
}
};
+ /**
+ * Removes all unmerged branches on the document with the given path and
+ * clusterId. This method will only remove unmerged branches when the
+ * clusterId is inactive.
+ *
+ * @memberof oak
+ * @method removeUnmergedBranches
+ * @param {string} path the path of a document
+ * @param {number} clusterId collision markers for this clusterId will be removed.
+ * @param {number} [limit=1000000] maximum number of unmerged branches to remove.
+ * @returns {object} the result of the MongoDB update.
+ */
+ api.removeUnmergedBranches = function(path, clusterId, limit) {
+ if (path === undefined) {
+ print("No path specified");
+ return;
+ }
+ if (clusterId === undefined) {
+ print("No clusterId specified");
+ return;
+ }
+ if (limit === undefined) {
+ limit = 1000000;
+ }
+ // refuse to remove when clusterId is marked active
+ var clusterNode = db.clusterNodes.findOne({_id: clusterId.toString()});
+ if (clusterNode && clusterNode.state == "ACTIVE") {
+ print("Cluster node with id " + clusterId + " is active!");
+ print("Can only remove unmerged branches for inactive cluster node.");
+ return;
+ }
+
+ var doc = this.findOne(path);
+ if (!doc) {
+ print("No document for path: " + path);
+ return;
+ }
+ var unset = {};
+ var r;
+ var num = 0;
+ for (r in doc._revisions) {
+ if (new Revision(r).getClusterId() != clusterId) {
+ continue;
+ }
+ if (doc._revisions[r].startsWith("br")) {
+ unset["_revisions." + r] = "";
+ unset["_bc." + r] = "";
+ num++;
+ }
+ if (num >= limit) {
+ break;
+ }
+ }
+ if (num > 0) {
+ var update = {};
+ update["$inc"] = {_modCount: NumberLong(1)};
+ update["$unset"] = unset;
+ print("Removing " + num + " unmerged branches for clusterId " + clusterId);
+ return db.nodes.update({_id: pathDepth(path) + ":" + path}, update);
+ } else {
+ print("No unmerged branches found for clusterId " + clusterId);
+ }
+ };
+
/**
* Finds the document with the given path.
*
