Can't get the Indexmanager to work :(
bes82 Aug 30, 2014 11:28 AMHi,
please take a look at the attached test and tell me what I'm doing wrong / why my index is not used?
{code}
package org.erratic.wm.test;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.query.Query;
import static org.apache.deltaspike.core.api.projectstage.ProjectStage.UnitTest;
import org.apache.deltaspike.testcontrol.api.TestControl;
import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.modeshape.jcr.api.index.IndexColumnDefinition;
import org.modeshape.jcr.api.index.IndexDefinition;
import org.modeshape.jcr.api.index.IndexDefinitionTemplate;
import org.modeshape.jcr.api.index.IndexManager;
import org.slf4j.Logger;
/**
*
* @author bschmidt
*/
@RunWith(CdiTestRunner.class)
@TestControl(startScopes = RequestScoped.class, projectStage = UnitTest.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
//@Ignore
public class WmModeshapeIndexTest {
@Inject
private Session session;
@Inject
protected transient Logger logger;
@Test
public void test001CheckSomethingWithIndexes() throws RepositoryException {
ensureIndex(indexManager(), "ntusysname", IndexDefinition.IndexKind.VALUE, "local", "nt:unstructured", "", null, "sysName", PropertyType.STRING);
Query q = session.getWorkspace().getQueryManager().createQuery("select BASE.* FROM [nt:unstructured] as BASE WHERE BASE.sysName=$sysName",Query.JCR_SQL2);
q.bindValue("sysName", session.getValueFactory().createValue("X"));
String plan = ((org.modeshape.jcr.api.query.Query)q).explain().getPlan();
logger.info(plan);
Assert.assertEquals(1,indexManager().getIndexDefinitions().size());
Assert.assertEquals(true,plan.contains("INDEX_USED=true"));
}
protected IndexManager indexManager(){
if (!(session instanceof org.modeshape.jcr.api.Session)){
return null;
}
try {
return ((org.modeshape.jcr.api.Session) session).getWorkspace().getIndexManager();
} catch (RepositoryException ex) {
logger.error(ex.toString(), ex);
}
return null;
}
protected void ensureIndex(IndexManager manager, String indexName, IndexDefinition.IndexKind kind, String providerName, String indexedNodeType, String desc, String workspaceNamePattern, String propertyName, int propertyType) throws RepositoryException {
if (manager.getIndexDefinitions().containsKey(indexName)){
return;
}
logger.info("registering index on property "+propertyName+", type "+indexedNodeType);
// Create the index template ...
IndexDefinitionTemplate template = manager.createIndexDefinitionTemplate();
template.setName(indexName);
template.setKind(kind);
template.setNodeTypeName(indexedNodeType);
template.setProviderName(providerName);
if (workspaceNamePattern != null) {
template.setWorkspaceNamePattern(workspaceNamePattern);
} else {
template.setAllWorkspaces();
}
if (desc != null) {
template.setDescription(desc);
}
// Set up the columns ...
IndexColumnDefinition colDefn = manager.createIndexColumnDefinitionTemplate().setPropertyName(propertyName).setColumnType(propertyType);
template.setColumnDefinitions(colDefn);
// Register the index ...
manager.registerIndex(template, false);
}
}
{code}