Skip to content

Instantly share code, notes, and snippets.

@chetanmeh
Created June 16, 2016 15:38
Show Gist options
  • Save chetanmeh/82d8d38a50ad28f2a1ad32e9b031d9af to your computer and use it in GitHub Desktop.
Save chetanmeh/82d8d38a50ad28f2a1ad32e9b031d9af to your computer and use it in GitHub Desktop.
Script to get node details and property details from an Oak based setup
package felixconsole
import com.google.common.base.Stopwatch
import com.google.common.collect.FluentIterable
import com.google.common.collect.TreeTraverser
import org.apache.jackrabbit.JcrConstants
import org.apache.jackrabbit.oak.api.PropertyState
import org.apache.jackrabbit.oak.api.Tree
import org.apache.jackrabbit.oak.plugins.tree.TreeFactory
import org.apache.jackrabbit.oak.spi.state.NodeState
import org.apache.jackrabbit.oak.spi.state.NodeStore
import org.slf4j.LoggerFactory
log = LoggerFactory.getLogger('script-console')
/**
* This script dump stats related to node and property count. The files
* are generated under crx-quickstart/logs folder with name node-stats.txt
* and property-stats.txt
*/
def nodeStatsTxt = new File("crx-quickstart/logs/node-stats.txt")
def propertyStatsTxt = new File("crx-quickstart/logs/property-stats.txt")
NodeStore ns = getStore()
Stopwatch w = Stopwatch.createStarted()
def treeItr = getTraversor(ns.root)
def count = 0
nodeStatsTxt.withPrintWriter {PrintWriter nodeStatsWriter ->
propertyStatsTxt.withPrintWriter {PrintWriter propsStatsWriter ->
treeItr.each {Tree tree ->
def noOfProps = tree.propertyCount
def path = tree.path
def type = "NA"
tree.properties.each {PropertyState ps ->
propsStatsWriter.println("${ps.name}|${ps.array}|${ps.count()}")
if (ps.name == JcrConstants.JCR_PRIMARYTYPE){
type = ps.name
}
}
count++
if (count % 10000 == 0){
print0("Traversed $count so far. Path [$path]")
}
nodeStatsWriter.println("$noOfProps|$path|$type")
}
}
}
print0("Node stats dumped to ${nodeStatsTxt.getAbsolutePath()}")
print0("Property stats dumped to ${propertyStatsTxt.getAbsolutePath()}")
print0("Total time taken : $w")
def print0(def msg){
log.info(msg)
println(msg)
}
FluentIterable<Tree> getTraversor(NodeState ns ){
def traversor = new TreeTraverser<Tree>(){
@Override
Iterable<Tree> children(Tree root) {
return root.children
}
}
return traversor.preOrderTraversal(TreeFactory.createReadOnlyTree(ns))
}
def getStore(){
osgi.getService(org.apache.sling.jcr.api.SlingRepository.class).manager.store
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment