Skip to content

Instantly share code, notes, and snippets.

@chetanmeh
Last active March 12, 2022 15:40
Show Gist options
  • Save chetanmeh/cc480a584448b412ea5321908e1b0254 to your computer and use it in GitHub Desktop.
Save chetanmeh/cc480a584448b412ea5321908e1b0254 to your computer and use it in GitHub Desktop.
package hpe
import org.apache.jackrabbit.oak.api.Blob
import org.apache.jackrabbit.oak.api.PropertyState
import org.apache.jackrabbit.oak.api.Type
import org.apache.jackrabbit.oak.spi.commit.CommitInfo
import org.apache.jackrabbit.oak.spi.commit.EmptyHook
import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry
import org.apache.jackrabbit.oak.spi.state.NodeBuilder
import org.apache.jackrabbit.oak.spi.state.NodeState
import org.apache.jackrabbit.oak.spi.state.NodeStateUtils
import org.apache.jackrabbit.oak.spi.state.NodeStore
ns = getStore()
RevisionVector = loadClass('org.apache.jackrabbit.oak.plugins.document.RevisionVector')
Revision = loadClass('org.apache.jackrabbit.oak.plugins.document.Revision')
NodeState oldRoot = getOldRoot(ns)
def badIndexes = ['damAssetLucene', 'hpeAuthorDamAsset', 'hpeSlingResource']
NodeBuilder builder = ns.root.builder()
badIndexes.each {String idxName ->
println "Updating index node $idxName"
NodeState old = oldRoot.getChildNode('oak:index').getChildNode(idxName)
assert old.exists()
NodeBuilder idxb = builder.getChildNode('oak:index').getChildNode(idxName)
copy(old, idxb)
}
ns.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY)
println "Index updated"
def copy(NodeState oldState, NodeBuilder nb) {
oldState.childNodeEntries.each {ChildNodeEntry cne ->
if (NodeStateUtils.isHidden(cne.name)){
nb.child(cne.name).remove()
copyState(cne.name, cne.nodeState, nb)
}
}
}
def copyState(String name, NodeState state, NodeBuilder parent){
NodeBuilder nb = parent.child(name)
state.properties.each {PropertyState ps ->
nb.setProperty(ps)
//Check that binary exists
if (ps.type == Type.BINARIES){
ps.getValue(Type.BINARIES).each { Blob b ->
InputStream is = b.newStream
is.close()
}
}
}
state.childNodeEntries.each{cne -> copyState(cne.name, cne.nodeState, nb)}
}
def getStore(){
osgi.getService(org.apache.sling.jcr.api.SlingRepository.class).manager.store
}
NodeState getOldRoot(NodeStore nodeStore) {
String rev = 'r159b54ef411-0-1,r159b54ef411-0-2,r159b54ef411-0-3,r159b54ef411-0-4'
NodeState state = nodeStore.getRoot(RevisionVector.fromString(rev))
assert state : "No state found at checkpoint $rev"
return state
}
Class loadClass(String className){
return ns.class.classLoader.loadClass(className)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment