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