Created
April 17, 2020 13:48
-
-
Save rah003/a73def501c85f85ff10b7f163bec8b8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = ctx.getJCRSession("foflinks"); | |
println ("at root") | |
saveCount = 0 | |
// batch of nodes to be processed in next run | |
nextBatch = [] | |
// batch of nodes to be processed in current run | |
currentBatch = [] | |
// a node to start with | |
currentBatch.add("/") | |
// batch of nodes with SO error ... to be deleted once some of their children had been deleted and their evaluation subpath had been made less deep | |
stackOverflowBatch = [] | |
println ("cleanup starting") | |
while (currentBatch.size() > 0) { | |
cleanLinks(currentBatch); | |
// prepare next batch for processing | |
currentBatch.clear() | |
currentBatch.addAll(nextBatch) | |
nextBatch.clear() | |
} | |
println ("cleaning stack overflow nodes") | |
cleanLinks(stactOverflowBatch.reverse()) | |
println ("cleanup finished") | |
def cleanLinks(list) { | |
println (" about to start new batch with ${list.size()} elements") | |
// parent loop | |
while (list.size() > 0) { | |
parent = s.getNode(list.remove(0)) | |
children = parent.getNodes().iterator() | |
println(" checking ${parent.path} child nodes") | |
// child nodes loop | |
while (children.hasNext()) { | |
n = children.next() | |
if (n.name.startsWith("jcr:")) { | |
// ignore system nodes | |
continue; | |
} | |
// delete all unpublished children | |
if (!n.hasProperty("mgnl:activationStatus") || !n.getProperty("mgnl:activationStatus").getBoolean()) { | |
println(" ${n.path} not published, will remove") | |
try { | |
n.remove(); | |
} catch (StackOverflowError e) { | |
println(" failed to remove ${n.path}, will try again later") | |
// try again once child nodes had been deleted | |
stackOverflowBatch.add(n.path) | |
// mark child nodes for next round of deletion | |
nextBatch.add(n.path) | |
} | |
saveCount++ | |
} else { | |
println(" will check child nodes of ${n.path} in next round") | |
// published, need to evaluate it's children, but due to potentially very big depth of the nodes we can't run it recursively | |
nextBatch.add(n.path) | |
} | |
if (saveCount % 500 == 0) { | |
println(" save()"); | |
// save only every x nodos to balance amount of memory used for modified nodes vs speed of running due to time necessary for each save | |
s.save() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment