Uploaded image for project: 'ModeShape'
  1. ModeShape
  2. MODE-2295

Indexing not working as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Blocker Blocker
    • 4.0.0.Beta2
    • 4.0.0.Beta1
    • Query
    • None

      Please run this test on an empty repo, it will fail. I'm not sure if and how much this is related to MODE-2290, but indexing is still not working as expected.

      I know that it doesn't make sense to register indexes like that, my real code differs a bit, but this test was the easiest way to reproduce the problem.

      public class WmModeshapeIndex2Test {
      
      	
      	@Inject
      	private Session session;
      
      	@Inject
      	protected transient Logger logger;
      
      	@Test
      	public void test001CheckSomethingWithIndexes() throws RepositoryException, InterruptedException {
      		
      		regNodeType(session,"nt:someType2");
      
      		ensureIndex(indexManager(), "ntsome2sysname", IndexDefinition.IndexKind.VALUE, "local", "nt:someType2", "", null, "sysName", PropertyType.STRING);
      		
      		Thread.sleep(500);
      		
      		Node newNode = session.getRootNode().addNode("SOMENODE","nt:someType2");
      		newNode.setProperty("sysName", "X");
      		
      		Thread.sleep(500);
      		
      		ensureIndex(indexManager(), "ntusysname", IndexDefinition.IndexKind.VALUE, "local", "nt:unstructured", "", null, "sysName", PropertyType.STRING);
      	
      		Node newNode2 = session.getRootNode().addNode("SOMENODE","nt:unstructured");
      		newNode2.setProperty("sysName", "X");
      		
      		Thread.sleep(500);
      		
      		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"));
      		QueryResult result = q.execute();
      		
      		String plan = ((org.modeshape.jcr.api.query.QueryResult)result).getPlan();
      		logger.info(plan);
      		
      		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, true);
      	}
      	
      	private void regNodeType(Session repoSession, String typeName) throws RepositoryException{
      			NodeTypeManager mgr = repoSession.getWorkspace().getNodeTypeManager();
      
      			// Create a template for the node type ...
      			NodeTypeTemplate type = mgr.createNodeTypeTemplate();
      			type.setName(typeName);
      			type.setDeclaredSuperTypeNames(new String[]{CrConstants.JCR_NT_UNSTRUCTURED});
      			type.setAbstract(false);
      			type.setOrderableChildNodes(true);
      			type.setMixin(false);
      			type.setQueryable(true);
      			mgr.registerNodeType(type, true);
      	}
      

              rhauch Randall Hauch (Inactive)
              bes82 Bjoern Schmidt (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: