1 package org.simantics.db.indexing;
\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
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
28 * @author Tuukka Lehtonen
\r
29 * @author Antti Villberg
\r
31 public class MemoryIndexing {
\r
33 final private Session session;
\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
38 final private Map<String,RAMDirectory> directories = new HashMap<String,RAMDirectory>();
\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
43 public MemoryIndexing(Session session) {
\r
44 this.session = session;
\r
47 protected File getIndexDirectory(Resource relation, Resource input) {
\r
48 return DatabaseIndexing.getIndexLocation(session, relation, input);
\r
51 public IndexedRelationsSearcher get(RequestProcessor processor, Resource relation, Resource input) {
\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
62 } catch (Exception e) {
\r
63 Logger.defaultLogError(e);
\r
68 public IndexedRelationsSearcherBase getImmutable(RequestProcessor processor, Resource relation, Resource input) {
\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
78 } catch (Exception e) {
\r
79 Logger.defaultLogError(e);
\r
84 public static MemoryIndexing getInstance(Session session) {
\r
85 MemoryIndexing ret = session.peekService(MemoryIndexing.class);
\r
87 ret = new MemoryIndexing(session);
\r
88 session.registerService(MemoryIndexing.class, ret);
\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
110 public void remove(String path) {
\r
111 directories.remove(path);
\r
114 public void flush(IProgressMonitor progress) throws Exception {
\r
116 SubMonitor monitor = SubMonitor.convert(progress);
\r
118 Set<Map.Entry<String, IndexedRelationsSearcher>> set = searchers.entrySet();
\r
119 Set<Map.Entry<String, IndexedRelationsSearcherBase>> iset = immutableSearchers.entrySet();
\r
121 monitor.setWorkRemaining(set.size()+iset.size());
\r
123 for(Map.Entry<String, IndexedRelationsSearcher> entry : set) {
\r
125 IndexedRelationsSearcher persistent = entry.getValue();
\r
126 IndexedRelationsMemorySearcher searcher = persistent.cache;
\r
128 if(persistent.isIndexAvailable()) {
\r
129 List<Object[]> os = searcher.allDocs(monitor, session);
\r
130 persistent.applyChanges(monitor, session, searcher.r, os);
\r
134 entry.getValue().changeState(monitor, session, State.READY);
\r
138 for(Map.Entry<String, IndexedRelationsSearcherBase> entry : iset) {
\r
140 entry.getValue().changeState(monitor, session, State.READY);
\r