]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/MemoryIndexing.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / MemoryIndexing.java
diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/MemoryIndexing.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/MemoryIndexing.java
new file mode 100644 (file)
index 0000000..adfdbd1
--- /dev/null
@@ -0,0 +1,147 @@
+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