}
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 {
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.simantics.db.ReadGraph;
import org.simantics.db.RequestProcessor;
import org.simantics.db.Resource;
+import org.simantics.db.Session;
import org.simantics.db.common.request.ReadRequest;
import org.simantics.db.common.request.UniqueRead;
import org.simantics.db.common.utils.NameUtils;
}
}
+ @Override
+ public void flush(IProgressMonitor progress, Session session) throws IndexException {
+ long startTime = System.currentTimeMillis();
+ SubMonitor monitor = SubMonitor.convert(progress);
+ MemoryIndexing mem = MemoryIndexing.getInstance(session);
+
+ try {
+ List<IndexedRelationsSearcherBase> searchers = mem.getAllSearchers();
+ int count = searchers.size();
+ Semaphore sema = new Semaphore(0);
+ searchers.stream().parallel().forEach(s -> {
+ LockHandle handle = lock(session, Pair.make(s.getRelation(), s.getInput()), true);
+ try {
+ if (s.isIndexAvailable() && s instanceof IndexedRelationsSearcher) {
+ IndexedRelationsSearcher searcher = (IndexedRelationsSearcher) s;
+ try {
+ List<Object[]> os = searcher.cache.allDocs(monitor, session);
+ searcher.applyChanges(monitor, session, searcher.cache.r, os);
+ } catch (Exception e) {
+ LOGGER.error("Could not flush in-memory changes to on-disk index", e);
+ }
+ }
+ monitor.worked(1);
+ s.changeState(monitor, session, State.READY);
+ } finally {
+ handle.unlock();
+ sema.release();
+ }
+ });
+ sema.acquire(count);
+ long totalTime = System.currentTimeMillis() - startTime;
+ LOGGER.info("Database index cache flush done in {} ms", totalTime);
+ } catch (InterruptedException e) {
+ LOGGER.error("Index searcher flush interrupted", e);
+ }
+ }
+
}
*/
final IndexSchema schema;
- Resource input;
+ final Resource input;
Path indexPath;
this.schema = IndexSchema.readFromRelation(session, relation);
}
+ public Resource getRelation() {
+ return relation;
+ }
+
+ public Resource getInput() {
+ return input;
+ }
+
Directory getDirectory(Session session) throws IOException {
return FSDirectory.open(indexPath.toFile());
}
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;
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;
/**
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();
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);
}
-
+
}
import org.eclipse.core.runtime.IProgressMonitor;
import org.simantics.db.RequestProcessor;
import org.simantics.db.Resource;
+import org.simantics.db.Session;
import org.simantics.db.layer0.adapter.GenericRelation;
/**
public void fullRebuild(IProgressMonitor monitor, RequestProcessor processor) throws IndexException;
+ /**
+ * Flush any memory-based index caches to disk and close down any resources
+ * related to each separate index.
+ *
+ * Must be invoked outside of any database transactions.
+ *
+ * @param monitor
+ * @param processor
+ * @throws IndexException
+ */
+ public void flush(IProgressMonitor monitor, Session session) throws IndexException;
+
}