This is an automated email from the ASF dual-hosted git repository. joscorbe pushed a commit to branch OAK-10798 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit ad7e429b02f724a05679ca0e96ab2c45d0e2a6e7 Author: Jose Cordero <corde...@adobe.com> AuthorDate: Mon May 13 15:04:26 2024 +0200 OAK-10798: Introduce functions to clean nodes based on regular expression --- oak-run/src/main/js/oak-mongo.js | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/oak-run/src/main/js/oak-mongo.js b/oak-run/src/main/js/oak-mongo.js index 8f8d264e7e..4bba3fb1dd 100644 --- a/oak-run/src/main/js/oak-mongo.js +++ b/oak-run/src/main/js/oak-mongo.js @@ -329,6 +329,59 @@ var oak = (function(global){ return {nRemoved : count}; }; + /** + * Helper method to find nodes based on Regular Expression. + * + * @memberof oak + * @method regexFind + * @param {string} pattern the pattern to match the nodes. + */ + api.regexFind = function(pattern) { + print(db.nodes.find({_id: {$regex: pattern}})); + db.nodes.find({_id: {$regex: pattern}}).forEach(function(doc) { + print(doc._id); + }); + } + + /** + * Remove the complete subtree of all the nodes matching a regex pattern. + * Use regexFind to find the nodes that match the pattern prior deletion. + * + * @memberof oak + * @method removeDescendantsMatching + * @param {string} pattern the pattern to match the nodes to be removed. + */ + api.removeDescendantsMatching = function(pattern) { + var count = 0; + db.nodes.find({_id: {$regex: pattern}}).forEach(function(doc) { + print("Removing " + doc._id + " and its children"); + var result = api.removeDescendantsAndSelf(api.pathFromId(doc._id)); + count += result.nRemoved; + print("nRemoved : " + result.nRemoved); + }); + print("Total removed : " + count); + } + + /** + * Wrapper function to clean all the /tmpXXXXXX nodes from the repository. + * + * @memberof oak + * @method removeRootTempNodes + */ + api.removeRootTempNodes = function() { + this.removeDescendantsMatching("^1:/tmp(?!$).*"); + } + + /** + * List all the nodes under /tmpXXXXXX. + * + * @memberof oak + * @method listRootTempNodes + */ + api.listRootTempNodes = function() { + this.regexFind("^1:/tmp(?!$).*"); + } + /** * List all checkpoints. *