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