]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/MemoryIndexing.java
Add logging to indexing & replace File-API with NIO Path-API
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / MemoryIndexing.java
index adfdbd1961a2f3ab4ebff53e5b93b723763ee734..75c6b79bb51c6a1dc4da3b9c9e1064f3fbb93b25 100644 (file)
-package org.simantics.db.indexing;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.apache.lucene.analysis.Analyzer;\r
-import org.apache.lucene.index.IndexWriter;\r
-import org.apache.lucene.index.IndexWriterConfig;\r
-import org.apache.lucene.index.IndexWriterConfig.OpenMode;\r
-import org.apache.lucene.store.Directory;\r
-import org.apache.lucene.store.RAMDirectory;\r
-import org.apache.lucene.util.Version;\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.SubMonitor;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.request.Adapt;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.indexing.IndexedRelationsSearcherBase.State;\r
-import org.simantics.db.layer0.adapter.GenericRelation;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- * @author Antti Villberg\r
- */\r
-public class MemoryIndexing {\r
-\r
-       final private Session session;\r
-       \r
-    final Map<String,Map<String,List<Map<String, Object>>>> persistentCache = new HashMap<String,Map<String,List<Map<String, Object>>>>();\r
-    final Map<String,Map<String,List<Resource>>> persistentCacheResources = new HashMap<String,Map<String,List<Resource>>>();\r
-       \r
-    final private Map<String,RAMDirectory> directories = new HashMap<String,RAMDirectory>();\r
-    \r
-    final private Map<String,IndexedRelationsSearcherBase> immutableSearchers = new HashMap<String,IndexedRelationsSearcherBase>();\r
-    final private Map<String,IndexedRelationsSearcher> searchers = new HashMap<String,IndexedRelationsSearcher>();\r
-\r
-    public MemoryIndexing(Session session) {\r
-       this.session = session;\r
-    }\r
-    \r
-    protected File getIndexDirectory(Resource relation, Resource input) {\r
-        return DatabaseIndexing.getIndexLocation(session, relation, input);\r
-    }\r
-    \r
-    public IndexedRelationsSearcher get(RequestProcessor processor, Resource relation, Resource input) {\r
-        try {\r
-            File location = getIndexDirectory(relation, input);\r
-            String key = location.getAbsolutePath();\r
-            IndexedRelationsSearcher searcher = searchers.get(key);\r
-            if (searcher == null) {\r
-                GenericRelation r = processor.sync(new Adapt<GenericRelation>(relation, GenericRelation.class));\r
-                searcher = new IndexedRelationsSearcher(processor, relation, input, r);\r
-                searchers.put(key, searcher);\r
-            }\r
-            return searcher;\r
-        } catch (Exception e) {\r
-            Logger.defaultLogError(e);\r
-            return null;\r
-        }\r
-    }\r
-\r
-    public IndexedRelationsSearcherBase getImmutable(RequestProcessor processor, Resource relation, Resource input) {\r
-        try {\r
-            File location = getIndexDirectory(relation, input);\r
-            String key = location.getAbsolutePath();\r
-            IndexedRelationsSearcherBase searcher = immutableSearchers.get(key);\r
-            if (searcher == null) {\r
-                searcher = new ImmutableIndexedRelationsSearcher(processor, relation, input);\r
-                immutableSearchers.put(key, searcher);\r
-            }\r
-            return searcher;\r
-        } catch (Exception e) {\r
-            Logger.defaultLogError(e);\r
-            return null;\r
-        }\r
-    }\r
-    \r
-    public static MemoryIndexing getInstance(Session session) {\r
-       MemoryIndexing ret = session.peekService(MemoryIndexing.class);\r
-        if(ret == null) {\r
-               ret = new MemoryIndexing(session);\r
-            session.registerService(MemoryIndexing.class, ret);\r
-        }\r
-        return ret;\r
-    }\r
-    \r
-    public synchronized Directory getDirectory(String path, Analyzer analyzer) throws IOException {\r
-        RAMDirectory directory = directories.get(path);\r
-        if (directory == null) {\r
-            synchronized (directories) {\r
-                directory = directories.get(path);\r
-                if (directory == null) {\r
-                    directory = new RAMDirectory();\r
-                    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);\r
-                    new IndexWriter(directory, config.setOpenMode(OpenMode.CREATE)).close();\r
-                    directories.put(path, directory);\r
-                }\r
-            }\r
-        }\r
-        return directory;\r
-\r
-    }\r
-    \r
-    public void remove(String path) {\r
-        directories.remove(path);\r
-    }\r
-    \r
-    public void flush(IProgressMonitor progress) throws Exception {\r
-       \r
-        SubMonitor monitor = SubMonitor.convert(progress);\r
-        \r
-        Set<Map.Entry<String, IndexedRelationsSearcher>> set = searchers.entrySet();\r
-        Set<Map.Entry<String, IndexedRelationsSearcherBase>> iset = immutableSearchers.entrySet();\r
-        \r
-        monitor.setWorkRemaining(set.size()+iset.size());\r
-\r
-        for(Map.Entry<String, IndexedRelationsSearcher> entry : set) {\r
-\r
-            IndexedRelationsSearcher persistent = entry.getValue();\r
-            IndexedRelationsMemorySearcher searcher = persistent.cache;\r
-\r
-               if(persistent.isIndexAvailable()) {\r
-                       List<Object[]> os = searcher.allDocs(monitor, session);\r
-                       persistent.applyChanges(monitor, session, searcher.r, os);\r
-               }\r
-            \r
-            monitor.worked(1);\r
-               entry.getValue().changeState(monitor, session, State.READY);\r
-\r
-        }\r
-\r
-        for(Map.Entry<String, IndexedRelationsSearcherBase> entry : iset) {\r
-               \r
-               entry.getValue().changeState(monitor, session, State.READY);\r
-            monitor.worked(1);\r
-            \r
-        }\r
-        \r
-    }\r
-    \r
-}\r
+package org.simantics.db.indexing;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.IndexWriterConfig.OpenMode;
+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.request.Adapt;
+import org.simantics.db.indexing.IndexedRelationsSearcherBase.State;
+import org.simantics.db.layer0.adapter.GenericRelation;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Tuukka Lehtonen
+ * @author Antti Villberg
+ */
+public class MemoryIndexing {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MemoryIndexing.class);
+
+       final private Session session;
+       
+    final Map<String,Map<String,List<Map<String, Object>>>> persistentCache = new HashMap<String,Map<String,List<Map<String, Object>>>>();
+    final Map<String,Map<String,List<Resource>>> persistentCacheResources = new HashMap<String,Map<String,List<Resource>>>();
+       
+    final private Map<String,RAMDirectory> directories = new HashMap<String,RAMDirectory>();
+    
+    final private Map<String,IndexedRelationsSearcherBase> immutableSearchers = new HashMap<String,IndexedRelationsSearcherBase>();
+    final private Map<String,IndexedRelationsSearcher> searchers = new HashMap<String,IndexedRelationsSearcher>();
+
+    public MemoryIndexing(Session session) {
+       this.session = session;
+    }
+    
+    protected Path getIndexDirectory(Resource relation, Resource input) {
+        return DatabaseIndexing.getIndexLocation(session, relation, input);
+    }
+    
+    public IndexedRelationsSearcher get(RequestProcessor processor, Resource relation, Resource input) {
+        Path location = getIndexDirectory(relation, input);
+        try {
+            String key = location.toAbsolutePath().toString();
+            IndexedRelationsSearcher searcher = searchers.get(key);
+            if (searcher == null) {
+                GenericRelation r = processor.sync(new Adapt<GenericRelation>(relation, GenericRelation.class));
+                searcher = new IndexedRelationsSearcher(processor, relation, input, r);
+                searchers.put(key, searcher);
+            }
+            return searcher;
+        } catch (Exception e) {
+            LOGGER.error("Could not get searcher for relation {} and input {} in location {}", relation, input, location, e);
+            return null;
+        }
+    }
+
+    public IndexedRelationsSearcherBase getImmutable(RequestProcessor processor, Resource relation, Resource input) {
+        Path location = getIndexDirectory(relation, input);
+        try {
+            String key = location.toAbsolutePath().toString();
+            IndexedRelationsSearcherBase searcher = immutableSearchers.get(key);
+            if (searcher == null) {
+                searcher = new ImmutableIndexedRelationsSearcher(processor, relation, input);
+                immutableSearchers.put(key, searcher);
+            }
+            return searcher;
+        } catch (Exception e) {
+            LOGGER.error("Could not get searcher base for relation {} and input {} in location {}", relation, input, location, e);
+            return null;
+        }
+    }
+    
+    public static MemoryIndexing getInstance(Session session) {
+       MemoryIndexing ret = session.peekService(MemoryIndexing.class);
+        if(ret == null) {
+               ret = new MemoryIndexing(session);
+            session.registerService(MemoryIndexing.class, ret);
+        }
+        return ret;
+    }
+    
+    public synchronized Directory getDirectory(String path, Analyzer analyzer) throws IOException {
+        RAMDirectory directory = directories.get(path);
+        if (directory == null) {
+            synchronized (directories) {
+                directory = directories.get(path);
+                if (directory == null) {
+                    directory = new RAMDirectory();
+                    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
+                    new IndexWriter(directory, config.setOpenMode(OpenMode.CREATE)).close();
+                    directories.put(path, directory);
+                }
+            }
+        }
+        return directory;
+
+    }
+    
+    public void remove(String path) {
+        directories.remove(path);
+    }
+    
+    public void flush(IProgressMonitor progress) throws Exception {
+       
+        SubMonitor monitor = SubMonitor.convert(progress);
+        
+        Set<Map.Entry<String, IndexedRelationsSearcher>> set = searchers.entrySet();
+        Set<Map.Entry<String, IndexedRelationsSearcherBase>> iset = immutableSearchers.entrySet();
+        
+        monitor.setWorkRemaining(set.size()+iset.size());
+
+        for(Map.Entry<String, IndexedRelationsSearcher> entry : set) {
+
+            IndexedRelationsSearcher persistent = entry.getValue();
+            IndexedRelationsMemorySearcher searcher = persistent.cache;
+
+               if(persistent.isIndexAvailable()) {
+                       List<Object[]> os = searcher.allDocs(monitor, session);
+                       persistent.applyChanges(monitor, session, searcher.r, os);
+               }
+            
+            monitor.worked(1);
+               entry.getValue().changeState(monitor, session, State.READY);
+
+        }
+
+        for(Map.Entry<String, IndexedRelationsSearcherBase> entry : iset) {
+               
+               entry.getValue().changeState(monitor, session, State.READY);
+            monitor.worked(1);
+            
+        }
+        
+    }
+    
+}