]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
Add locking for IndexUtils.flushIndexCaches
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexUtils.java
index 8b1a928b353497bbc733c1b89e7831e986659dd7..0cb5d9c4d9f5a78fedb3d8038bc1785bcbc6bb88 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.db.indexing;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
@@ -21,6 +20,7 @@ import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.ObjectsWithType;
 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;
@@ -91,61 +91,48 @@ 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>();
-        Layer0 L0 = Layer0.getInstance(graph);
-        String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
-               String search = "Types:" + IndexQueries.quoteTerm(typeName);
+        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 typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
-               String search = "Types:" + IndexQueries.quoteTerm(typeName) + " AND Name:" + IndexQueries.quoteTerm(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)) results.add(resource);
+        for(Resource resource : findResources(graph, model, search)) {
+            if(graph.isInstanceOf(resource, type)) results.add(resource);
         }
         return results;
     }
-    
-    public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
 
-       MemoryIndexing mem = MemoryIndexing.getInstance(session);
-       mem.flush(progress);
-       
+    public static void flushIndexCaches(IProgressMonitor monitor, Session session) throws Exception {
+        session.getService(IndexedRelations.class).flush(monitor, session);
     }
-    
+
     public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
-       
+
        if(progress == null) progress = new NullProgressMonitor();
 
        MemoryIndexing mem = MemoryIndexing.getInstance(session);
        Layer0X L0X = Layer0X.getInstance(session);
 
-       mem.flush(progress);
-               
+       session.getService(IndexedRelations.class).flush(progress, session);
+
        IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
         List<Object> results;
         try {