]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
Added new field TypeId to dependency index for exact type searching
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexUtils.java
index 5262d99a2d85690715f4264a9a01cfaf664bcf95..760acd68c465578b269cdf12e164dafbb65a2516 100644 (file)
@@ -11,21 +11,29 @@ import org.apache.lucene.util.BytesRef;
 import org.apache.lucene.util.NumericUtils;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.ObjectsWithType;
-import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.indexing.exception.IndexCorruptedException;
+import org.simantics.db.layer0.genericrelation.Dependencies;
+import org.simantics.db.layer0.genericrelation.IndexQueries;
+import org.simantics.db.layer0.genericrelation.IndexedRelations;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.service.CollectionSupport;
 import org.simantics.layer0.Layer0;
 import org.simantics.operation.Layer0X;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class IndexUtils {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(IndexUtils.class);
+
     public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
        
         Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
@@ -83,40 +91,31 @@ public class IndexUtils {
     public static Collection<Resource> findByName(ReadGraph graph, Resource model, String name) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(graph);
         HashSet<Resource> results = new HashSet<Resource>();
-        
-               String search = "Name:" + name;
 
-        for(Map<String, Object> entry : find(graph, model, search)) {
-               Resource resource = (Resource)entry.get("Resource");
+        String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name);
+
+        for(Resource resource : findResources(graph, model, search)) {
             if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
         }
         return results;
     }
 
     public static Collection<Resource> findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
-       
-        HashSet<Resource> results = new HashSet<Resource>();
-        
-               String search = "Types:*" + NameUtils.getSafeName(graph, type);
+        HashSet<Resource> results = new HashSet<>();
+        String search = IndexQueries.resourceIdTerm(Dependencies.FIELD_TYPE_RESOURCE, type);
 
-        for(Map<String, Object> entry : find(graph, model, search)) {
-               Resource resource = (Resource)entry.get("Resource");
-               if(graph.isInstanceOf(resource, type)) results.add(resource);
+        for(Resource resource : findResources(graph, model, search)) {
+            if(graph.isInstanceOf(resource, type)) results.add(resource);
         }
         return results;
     }
 
     public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
-       
-        Layer0 L0 = Layer0.getInstance(graph);
-        
         HashSet<Resource> results = new HashSet<Resource>();
-        
-               String search = "Types:*" + type + " AND Name:" + name;
+        String search = IndexQueries.and(IndexQueries.resourceIdTerm(Dependencies.FIELD_TYPE_RESOURCE, type), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name));
 
-        for(Map<String, Object> entry : find(graph, model, search)) {
-               Resource resource = (Resource)entry.get("Resource");
-               if(graph.isInstanceOf(resource, type) && name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
+        for(Resource resource : findResources(graph, model, search)) {
+            if(graph.isInstanceOf(resource, type)) results.add(resource);
         }
         return results;
     }
@@ -138,10 +137,25 @@ public class IndexUtils {
        mem.flush(progress);
                
        IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
-       return searcher.doList(progress, session);
-       
+        List<Object> results;
+        try {
+            results = searcher.doList(progress, session);
+        } catch (IndexCorruptedException e) {
+            LOGGER.error("Index is corrupted for indexRoot {}", indexRoot, e);
+            rebuild(session, progress);
+            // if this fails then no can do
+            searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
+            results = searcher.doList(progress, session);
+        }
+        return results;
     }
     
+    private static void rebuild(Session session, IProgressMonitor monitor) throws Exception {
+        LOGGER.error("Trying to rebuild index");
+        DatabaseIndexing.deleteAllIndexes();
+        session.getService(IndexedRelations.class).fullRebuild(SubMonitor.convert(monitor, 100), session);
+    }
+
     public static Term longTerm(String key, Long value) {
        BytesRef ref = new BytesRef();    
        NumericUtils.longToPrefixCoded( value, 0, ref );