Skip to content

Instantly share code, notes, and snippets.

@tml
Forked from andrewmkhoury/oakTreeNodeCount.groovy
Created July 15, 2019 12:33
Show Gist options
  • Save tml/41909aef3e3dd25a453e1bf19a19ed46 to your computer and use it in GitHub Desktop.
Save tml/41909aef3e3dd25a453e1bf19a19ed46 to your computer and use it in GitHub Desktop.
Counting nodes in a tree of Oak repository using the oak-run console tool
import org.apache.jackrabbit.oak.spi.state.NodeState
import java.util.concurrent.atomic.AtomicInteger
def countNodes(NodeState n, String path = "/", flush = 5000, AtomicInteger count = new AtomicInteger(0), root = true) {
if(root) {
println "Counting nodes in tree ${path}"
}
cnt = count.incrementAndGet()
if (cnt % flush == 0) println(" " + cnt)
try {
for(child in n.getChildNodeEntries()) {
countNodes(child.getNodeState(), path + child.getName() + "/", flush, count, false)
}
} catch(e) {
println "warning unable to read node ${path}"
}
if(root) {
println "Total nodes in tree ${path}: ${cnt}"
}
return cnt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment