]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/MemoryIndexing.java
Add locking for IndexUtils.flushIndexCaches
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / MemoryIndexing.java
index cee5eb26bfb616799c522baca31cf186b2a46a17..7f86d2bc1635fe762d40c4321632448ab916ff87 100644 (file)
@@ -2,12 +2,10 @@ package org.simantics.db.indexing;
 
 import java.io.IOException;
 import java.nio.file.Path;
-import java.util.Collection;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Semaphore;
-import java.util.stream.Stream;
 
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.index.IndexWriter;
@@ -17,13 +15,13 @@ import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.RAMDirectory;
 import org.apache.lucene.util.Version;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubMonitor;
 import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
+import org.simantics.db.common.SimanticsInternal;
 import org.simantics.db.common.request.Adapt;
-import org.simantics.db.indexing.IndexedRelationsSearcherBase.State;
 import org.simantics.db.layer0.adapter.GenericRelation;
+import org.simantics.db.layer0.genericrelation.IndexedRelations;
 import org.slf4j.LoggerFactory;
 
 /**
@@ -52,6 +50,13 @@ public class MemoryIndexing {
         return DatabaseIndexing.getIndexLocation(session, relation, input);
     }
 
+    List<IndexedRelationsSearcherBase> getAllSearchers() {
+        List<IndexedRelationsSearcherBase> r = new ArrayList<>();
+        r.addAll(searchers.values());
+        r.addAll(immutableSearchers.values());
+        return r;
+    }
+
     public IndexedRelationsSearcher get(RequestProcessor processor, Resource relation, Resource input) {
         Path location = getIndexDirectory(relation, input);
         String key = location.toAbsolutePath().toString();
@@ -109,35 +114,15 @@ public class MemoryIndexing {
         directories.remove(path);
     }
 
+    /**
+     * @param progress
+     * @throws Exception
+     * @deprecated Use {@link IndexUtils#flushIndexCaches(IProgressMonitor, Session)} instead
+     */
+    @Deprecated
     public void flush(IProgressMonitor progress) throws Exception {
-        long startTime = System.currentTimeMillis();
-        SubMonitor monitor = SubMonitor.convert(progress);
-        Collection<IndexedRelationsSearcher> searcherEntries = searchers.values();
-        Collection<IndexedRelationsSearcherBase> immutableSearcherEntries = immutableSearchers.values();
-        int count = searcherEntries.size() + immutableSearcherEntries.size();
-        Semaphore sema = new Semaphore(0);
-        Stream.concat(searcherEntries.stream(), immutableSearcherEntries.stream()).parallel().forEach(base -> {
-            try {
-                if (base.isIndexAvailable()) {
-                    if (base instanceof IndexedRelationsSearcher) {
-                        IndexedRelationsMemorySearcher searcher = ((IndexedRelationsSearcher) base).cache;
-                        try {
-                            List<Object[]> os = searcher.allDocs(monitor, session);
-                            ((IndexedRelationsSearcher) base).applyChanges(monitor, session, searcher.r, os);
-                        } catch (Exception e) {
-                            LOGGER.error("Could not flush", e);
-                        }
-                    }
-                }
-                monitor.worked(1);
-                base.changeState(monitor, session, State.READY);
-            } finally {
-                sema.release();
-            }
-        });
-        sema.acquire(count);
-        long totalTime = System.currentTimeMillis() - startTime;
-        LOGGER.info("index flush " + totalTime);
+        Session s = SimanticsInternal.getSession();
+        s.getService(IndexedRelations.class).flush(progress, s);
     }
-    
+
 }