Skip to content

Instantly share code, notes, and snippets.

@andrewmkhoury
Last active April 29, 2021 08:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewmkhoury/c5588a6a4b57e7e0e593 to your computer and use it in GitHub Desktop.
Save andrewmkhoury/c5588a6a4b57e7e0e593 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
}
@andrewmkhoury
Copy link
Author

Usage

countNodes(session.workingNode)

Note: This is mainly for Tar SegmentNodeStore. For Mongo based systems, use oak-mongo.js via mongo shell and run oak.countChildren("/path")

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