Skip to content

Instantly share code, notes, and snippets.

@helms-charity
Forked from stillalex/childCountWarning.groovy
Created February 12, 2022 21:10
Show Gist options
  • Save helms-charity/b43a38b9273792dc7bada8f1b5cdb64a to your computer and use it in GitHub Desktop.
Save helms-charity/b43a38b9273792dc7bada8f1b5cdb64a to your computer and use it in GitHub Desktop.
counts number of nodes in tree, logging a warn is the number of child nodes exceeds a certain threshold
import java.util.concurrent.atomic.AtomicInteger
import org.apache.jackrabbit.oak.api.Type
import org.apache.jackrabbit.oak.plugins.segment.SegmentBlob
import org.apache.jackrabbit.oak.spi.state.NodeState
def countNodes(NodeState n, String path = "/", Integer flush = 1000000, Long warnAt = 1000, AtomicInteger count = new AtomicInteger(0), AtomicInteger binaries = new AtomicInteger(0), root = true) {
if(root) {
println "Counting nodes in tree ${path}"
}
cnt = count.incrementAndGet()
if (cnt % flush == 0) println(" " + cnt)
try {
for(prop in n.getProperties()) {
if(prop.getType() == Type.BINARY || prop.getType() == Type.BINARIES) {
for(b in prop.getValue(Type.BINARIES)) {
binaries.incrementAndGet()
if(b instanceof SegmentBlob) {
if(!((SegmentBlob)b).isExternal()) {
b.length()
}
} else {
b.length()
}
}
}
}
kids = n.getChildNodeCount(warnAt)
if(kids >= warnAt) {
println "${path} has ${kids} child nodes"
}
for(child in n.getChildNodeEntries()) {
countNodes(child.getNodeState(), path + child.getName() + "/", flush, warnAt, count, binaries, false)
}
} catch(e) {
println "warning unable to read node ${path}"
}
if(root) {
println "Total nodes in tree ${path}: ${cnt}"
println "Total binaries in tree ${path}: ${binaries.get()}"
}
return cnt
}
@helms-charity
Copy link
Author

helms-charity commented Mar 14, 2023

My notes: I tried editing this to only look in a specific path, instead of root, but the script was giving me errors (seemed to be thwarted by s3-stored blob paths) so I think I gave up and quit it before finishing.

##my test was to run it only on /content/data
java -Xmx8G -jar oak-run-1.22.11.jar console repository/segmentstore/ :load childCountWarning-content-data.groovy >> countNodes-contentdata.log 2>> countNodes-contentdata-errors.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment