Skip to content

Instantly share code, notes, and snippets.

@chetanmeh
Last active December 26, 2015 12:49
Show Gist options
  • Save chetanmeh/7153951 to your computer and use it in GitHub Desktop.
Save chetanmeh/7153951 to your computer and use it in GitHub Desktop.
Oak related scripts
import com.gmongo.GMongo
import com.mongodb.util.JSON
import groovy.json.*
//https://github.com/poiati/gmongo
def mk = osgi.getService(org.apache.jackrabbit.mk.api.MicroKernel.class)
def db = GMongo.patchAndReturn(mk.store.nodes.DB)
def id = '0:/'
def obj = db.nodes.findOne(_id:id)
def jsonStr = JSON.serialize(obj)
print JsonOutput.prettyPrint(jsonStr)
import groovy.text.SimpleTemplateEngine
import java.util.concurrent.TimeUnit;
nodeStore = osgi.getService(org.apache.jackrabbit.oak.spi.state.NodeStore.class)
store = nodeStore.store
def caches = [
doc:[cache:store.nodesCache,stats:store.cacheStats],
nodes: [cache:nodeStore.nodeCache,stats:nodeStore.nodeCacheStats],
nodeChildren:[cache:nodeStore.nodeChildrenCache,stats:nodeStore.nodeChildrenCacheStats],
docChildren:[cache:nodeStore.docChildrenCache,stats:nodeStore.docChildrenCacheStats],
]
def ttf = new TemplateFactory()
ttf.columns = [
[name:"name",displayName:"name",size:14],
[name:"elementCount",displayName:"Size",size:10],
[name:"requestCount",displayName:"Requests",size:10],
[name:"hitCount",displayName:"Hits",size:10],
[name:"hitRate",displayName:"Hit Rate",size:10],
[name:"missCount",displayName:"Misses",size:10],
[name:"missRate",displayName:"Miss Rate",size:10],
[name:"loadCount",displayName:"Loads",size:10],
[name:"totalLoadTime",displayName:"Load Time",size:10],
[name:"loadPenalty",displayName:"Load Penalty",size:10],
[name:"evictionCount",displayName:"Evictions",size:10],
[name:"maxTotalWeight",displayName:"Max Weight",size:10],
[name:"currentWeight",displayName:"CurrentWeight",size:10],
]
def cacheDetails = caches.collect {name,data ->
def c = data.stats
[
name : name,
requestCount : c.requestCount,
hitCount : c.hitCount,
hitRate : String.format("%1.2f", c.hitRate),
missCount : c.missCount,
missRate : String.format("%1.2f", c.missRate),
loadCount : c.loadCount,
totalLoadTime : timeInWords(c.totalLoadTime),
loadPenalty : String.format("%1.2f", c.averageLoadPenalty) ,
evictionCount : c.evictionCount,
elementCount : c.elementCount,
maxTotalWeight: humanReadableByteCount(c.getMaxTotalWeight(), true),
currentWeight : humanReadableByteCount(c.estimateCurrentWeight(), true),
]
}
println new SimpleTemplateEngine().createTemplate(ttf.template).make([rows:cacheDetails]).toString()
class TemplateFactory {
def columns = []
def getTemplate() { """
${columns.collect{ " <%print \"$it.displayName\".center($it.size)%> " }.join()}
${columns.collect{ " <%print \"_\"*$it.size %> " }.join()}
<% rows.each {%>${columns.collect{ " \${it.${it.name}.toString().padRight($it.size).substring(0,$it.size)} " }.join()}
<% } %>"""
}
}
def timeInWords(long nanos) {
long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
return String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);
}
def humanReadableByteCount(long bytes, boolean si) {
if (bytes < 0) {
return "0";
}
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) ;
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
import com.mongodb.Mongo
import org.osgi.framework.FrameworkUtil
def msgClass = FrameworkUtil.getBundle(Mongo.class).loadClass('com.mongodb.OutMessage')
println msgClass.@REQUEST_ID
import org.osgi.framework.FrameworkUtil
mk = osgi.getService(org.apache.jackrabbit.mk.api.MicroKernel.class)
revClass = loadClass(mk, 'org.apache.jackrabbit.oak.plugins.mongomk.Revision')
printTS('r142997d90d9-0-1')
def printTS(rev){
def r = createRevision(rev)
println rev + "=>" + r.timestamp + "=>" + new Date( r.timestamp)
}
def createRevision(revString){
return revClass.metaClass.invokeStaticMethod(revClass, 'fromString', revString)
}
def loadClass(obj, className){
FrameworkUtil.getBundle(obj.class).loadClass(className)
}
import com.gmongo.GMongo
import com.mongodb.util.JSON
import groovy.json.*
import org.osgi.framework.FrameworkUtil
//This scripts simplifies the Node object found from Mongo DB by replacing the revision
//string with timestamp numeric value. The timestamps are further baselined against minimum rev
//present in the obj
//List of properties where revision is not to be replaced
valueAsRev = ['_lastRev']
mk = osgi.getService(org.apache.jackrabbit.mk.api.MicroKernel.class)
revClass = loadClass(mk, 'org.apache.jackrabbit.oak.plugins.mongomk.Revision')
revCompartor = mk.store.comparator
def db = GMongo.patchAndReturn(mk.store.nodes.DB)
def path = "/var/discovery/impl/ongoingVotings/f8287fa6-8a4d-4f9c-a09e-04c889e29bec"
def id = path.count('/')+":"+path
def obj = db.nodes.findOne(_id:id)
baseTime = determineBaseTime(obj)
simplifyRevString(obj)
def jsonStr = JSON.serialize(obj)
print JsonOutput.prettyPrint(jsonStr)
def simplifyRevString(obj){
obj.each {k,v ->
if(valueAsRev.contains(k)){
return
}
if(v instanceof Map){
def sortedMap = new TreeMap(revCompartor)
v.each {k1,v1 -> sortedMap[createRevision(k1)] = v1}
def clonedMap = new LinkedHashMap()
def revToReplace = []
sortedMap.entrySet().each { e ->
def r = e.key
def revStr = "${r.timestamp-baseTime}-${r.counter}-${r.clusterId}"
revToReplace << e.key
clonedMap[revStr] = e.value
}
v.clear()
v.putAll(clonedMap)
}
}
}
def determineBaseTime(obj){
def revTimes = []
obj.each {k,v ->
if(v instanceof Map){
def revList
if(valueAsRev.contains(k)){
revList = v.values()
}else{
revList = v.keySet()
}
revList.each { r ->
if(isRevision(r)){
def rev = createRevision(r)
revTimes << rev.timestamp
}
}
}
}
return revTimes.sort().first()
}
def createRevision(revString){
return revClass.metaClass.invokeStaticMethod(revClass, 'fromString', revString)
}
def isRevision(r){
return r[0] == 'b' || r[0] == 'r'
}
def loadClass(obj, className){
FrameworkUtil.getBundle(obj.class).loadClass(className)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment