Skip to content

Instantly share code, notes, and snippets.

@stillalex
Last active July 7, 2018 23:46
Show Gist options
  • Save stillalex/e7067bcb86c89bef66c8 to your computer and use it in GitHub Desktop.
Save stillalex/e7067bcb86c89bef66c8 to your computer and use it in GitHub Desktop.
import java.io.InputStream;
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, deep = false, String path = "/", Integer flush = 50000, 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(deep) {
InputStream s = b.getNewStream();
try {
byte[] buffer = new byte[1024];
int l = s.read(buffer, 0, buffer.length);
} finally {
s.close();
}
} else if(b instanceof SegmentBlob) {
if(!((SegmentBlob)b).isExternal()) {
b.length()
}
} else {
b.length()
}
}
}
}
for(child in n.getChildNodeEntries()) {
countNodes(child.getNodeState(), deep, path + child.getName() + "/", flush, 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
}
@stillalex
Copy link
Author

stillalex commented Nov 12, 2014

:load https://gist.githubusercontent.com/stillalex/e7067bcb86c89bef66c8/raw/d7a5a9b839c3bb0ae5840252022f871fd38374d3/childCount.groovy
countNodes(session.workingNode)

for deep binary check:

:load https://gist.githubusercontent.com/stillalex/e7067bcb86c89bef66c8/raw/d7a5a9b839c3bb0ae5840252022f871fd38374d3/childCount.groovy
countNodes(session.workingNode, true)

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