Skip to content

Commit

Permalink
OAK-10801: Introduce functions to remove nodes that are older than a …
Browse files Browse the repository at this point in the history
…timestamp
  • Loading branch information
Joscorbe committed May 13, 2024
1 parent 6706898 commit a1999ab
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion oak-run/src/main/js/oak-mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,53 @@ var oak = (function(global){
return {nRemoved : count};
};

/**
* Finds all the descendants with a _modified timestamp older than the
* given timestamp.
*
* @memberof oak
* @method findDescendantsOlderThan
* @param {string} path of the parent node to search.
* @param {number} timestamp to compare with.
*/
api.findDescendantsOlderThan = function(path, timestamp) {
var count = 0;
var depth = pathDepth(path);
var prefix = path + "/";
depth++;
var query = { _id: pathFilter(depth, prefix), _modified: {$lt: timestamp}};
db.nodes.find(query).forEach(function(doc) {
print(doc._id);
count++;
});
print("nCandidates : " + count);
}

/**
* Removes all the descendants with a _modified timestamp older than the
* given timestamp.
* Use removeDescendantsAndSelf() first to get the list of nodes to remove.
*
* @memberof oak
* @method removeDescendantsOlderThan
* @param {string} path of the parent node to search.
* @param {number} timestamp to compare with.
*/
api.removeDescendantsOlderThan = function(path, timestamp) {
var count = 0;
var depth = pathDepth(path);
var prefix = path + "/";
depth++;
var query = { _id: pathFilter(depth, prefix), _modified: {$lt: timestamp}};
db.nodes.find(query).forEach(function(doc) {
print("Removing " + doc._id + " and its children");
var result = api.removeDescendantsAndSelf(api.pathFromId(doc._id));
count += result.nRemoved;
print(" removed : " + result.nRemoved);
});
print("nRemoved : " + count);
}

/**
* List all checkpoints.
*
Expand Down Expand Up @@ -1195,7 +1242,7 @@ var oak = (function(global){
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};

// http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
var escapeForRegExp = function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
Expand Down

0 comments on commit a1999ab

Please sign in to comment.