]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.indexing;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.util.HashMap;\r
6 import java.util.List;\r
7 import java.util.Map;\r
8 import java.util.Set;\r
9 \r
10 import org.apache.lucene.analysis.Analyzer;\r
11 import org.apache.lucene.index.IndexWriter;\r
12 import org.apache.lucene.index.IndexWriterConfig;\r
13 import org.apache.lucene.index.IndexWriterConfig.OpenMode;\r
14 import org.apache.lucene.store.Directory;\r
15 import org.apache.lucene.store.RAMDirectory;\r
16 import org.apache.lucene.util.Version;\r
17 import org.eclipse.core.runtime.IProgressMonitor;\r
18 import org.eclipse.core.runtime.SubMonitor;\r
19 import org.simantics.db.RequestProcessor;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.Session;\r
22 import org.simantics.db.common.request.Adapt;\r
23 import org.simantics.db.common.utils.Logger;\r
24 import org.simantics.db.indexing.IndexedRelationsSearcherBase.State;\r
25 import org.simantics.db.layer0.adapter.GenericRelation;\r
26 \r
27 /**\r
28  * @author Tuukka Lehtonen\r
29  * @author Antti Villberg\r
30  */\r
31 public class MemoryIndexing {\r
32 \r
33         final private Session session;\r
34         \r
35     final Map<String,Map<String,List<Map<String, Object>>>> persistentCache = new HashMap<String,Map<String,List<Map<String, Object>>>>();\r
36     final Map<String,Map<String,List<Resource>>> persistentCacheResources = new HashMap<String,Map<String,List<Resource>>>();\r
37         \r
38     final private Map<String,RAMDirectory> directories = new HashMap<String,RAMDirectory>();\r
39     \r
40     final private Map<String,IndexedRelationsSearcherBase> immutableSearchers = new HashMap<String,IndexedRelationsSearcherBase>();\r
41     final private Map<String,IndexedRelationsSearcher> searchers = new HashMap<String,IndexedRelationsSearcher>();\r
42 \r
43     public MemoryIndexing(Session session) {\r
44         this.session = session;\r
45     }\r
46     \r
47     protected File getIndexDirectory(Resource relation, Resource input) {\r
48         return DatabaseIndexing.getIndexLocation(session, relation, input);\r
49     }\r
50     \r
51     public IndexedRelationsSearcher get(RequestProcessor processor, Resource relation, Resource input) {\r
52         try {\r
53             File location = getIndexDirectory(relation, input);\r
54             String key = location.getAbsolutePath();\r
55             IndexedRelationsSearcher searcher = searchers.get(key);\r
56             if (searcher == null) {\r
57                 GenericRelation r = processor.sync(new Adapt<GenericRelation>(relation, GenericRelation.class));\r
58                 searcher = new IndexedRelationsSearcher(processor, relation, input, r);\r
59                 searchers.put(key, searcher);\r
60             }\r
61             return searcher;\r
62         } catch (Exception e) {\r
63             Logger.defaultLogError(e);\r
64             return null;\r
65         }\r
66     }\r
67 \r
68     public IndexedRelationsSearcherBase getImmutable(RequestProcessor processor, Resource relation, Resource input) {\r
69         try {\r
70             File location = getIndexDirectory(relation, input);\r
71             String key = location.getAbsolutePath();\r
72             IndexedRelationsSearcherBase searcher = immutableSearchers.get(key);\r
73             if (searcher == null) {\r
74                 searcher = new ImmutableIndexedRelationsSearcher(processor, relation, input);\r
75                 immutableSearchers.put(key, searcher);\r
76             }\r
77             return searcher;\r
78         } catch (Exception e) {\r
79             Logger.defaultLogError(e);\r
80             return null;\r
81         }\r
82     }\r
83     \r
84     public static MemoryIndexing getInstance(Session session) {\r
85         MemoryIndexing ret = session.peekService(MemoryIndexing.class);\r
86         if(ret == null) {\r
87                 ret = new MemoryIndexing(session);\r
88             session.registerService(MemoryIndexing.class, ret);\r
89         }\r
90         return ret;\r
91     }\r
92     \r
93     public synchronized Directory getDirectory(String path, Analyzer analyzer) throws IOException {\r
94         RAMDirectory directory = directories.get(path);\r
95         if (directory == null) {\r
96             synchronized (directories) {\r
97                 directory = directories.get(path);\r
98                 if (directory == null) {\r
99                     directory = new RAMDirectory();\r
100                     IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);\r
101                     new IndexWriter(directory, config.setOpenMode(OpenMode.CREATE)).close();\r
102                     directories.put(path, directory);\r
103                 }\r
104             }\r
105         }\r
106         return directory;\r
107 \r
108     }\r
109     \r
110     public void remove(String path) {\r
111         directories.remove(path);\r
112     }\r
113     \r
114     public void flush(IProgressMonitor progress) throws Exception {\r
115         \r
116         SubMonitor monitor = SubMonitor.convert(progress);\r
117         \r
118         Set<Map.Entry<String, IndexedRelationsSearcher>> set = searchers.entrySet();\r
119         Set<Map.Entry<String, IndexedRelationsSearcherBase>> iset = immutableSearchers.entrySet();\r
120         \r
121         monitor.setWorkRemaining(set.size()+iset.size());\r
122 \r
123         for(Map.Entry<String, IndexedRelationsSearcher> entry : set) {\r
124 \r
125             IndexedRelationsSearcher persistent = entry.getValue();\r
126             IndexedRelationsMemorySearcher searcher = persistent.cache;\r
127 \r
128                 if(persistent.isIndexAvailable()) {\r
129                         List<Object[]> os = searcher.allDocs(monitor, session);\r
130                         persistent.applyChanges(monitor, session, searcher.r, os);\r
131                 }\r
132             \r
133             monitor.worked(1);\r
134                 entry.getValue().changeState(monitor, session, State.READY);\r
135 \r
136         }\r
137 \r
138         for(Map.Entry<String, IndexedRelationsSearcherBase> entry : iset) {\r
139                 \r
140                 entry.getValue().changeState(monitor, session, State.READY);\r
141             monitor.worked(1);\r
142             \r
143         }\r
144         \r
145     }\r
146     \r
147 }\r