]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsMemorySearcher.java
Added new field TypeId to dependency index for exact type searching
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexedRelationsMemorySearcher.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.indexing;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.index.CorruptIndexException;
22 import org.apache.lucene.index.IndexableField;
23 import org.apache.lucene.search.MatchAllDocsQuery;
24 import org.apache.lucene.search.Query;
25 import org.apache.lucene.search.ScoreDoc;
26 import org.apache.lucene.search.TopDocs;
27 import org.apache.lucene.store.Directory;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.simantics.db.RequestProcessor;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.adapter.GenericRelation;
34 import org.simantics.utils.datastructures.Pair;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import gnu.trove.map.hash.THashMap;
39 import gnu.trove.set.hash.TLongHashSet;
40
41 /**
42  * @author Tuukka Lehtonen
43  * @author Antti Villberg
44  */
45 public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsMemorySearcher.class);
48     
49     final IndexedRelationsSearcher backend;
50     final GenericRelation r;
51     
52     TLongHashSet changed = new TLongHashSet();
53     
54     IndexedRelationsMemorySearcher(RequestProcessor session, IndexedRelationsSearcher backend, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
55         super(session, relation, input);
56         this.backend = backend;
57         this.r = r;
58         setReady();
59     }
60
61     @Override
62     String getDescriptor() {
63         return "MEM: ";
64     }
65     
66     @Override
67     void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException, DatabaseException {
68         for(Object[] o : documentsData) {
69             Long resource = (Long)o[1];
70             changed.add(resource);
71         }
72         super.insertIndex(monitor, r, boundLength, documentsData);
73     }
74     
75     @Override
76     boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
77             int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
78             DatabaseException {
79         for(Object[] o : documentsData) {
80             Long resource = (Long)o[1];
81             changed.add(resource);
82         }
83         return super.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
84     }
85     
86     @Override
87     void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key,
88             Collection<Object> keyValues) throws DatabaseException, CorruptIndexException, IOException {
89         for(Object o : keyValues) {
90             Resource resource= (Resource)o;
91             changed.add(resource.getResourceId());
92         }
93         super.removeIndex(monitor, r, processor, key, keyValues);
94     }
95     
96     public List<Object[]> allDocs(IProgressMonitor monitor, Session session) throws IOException {
97         
98         Query query = new MatchAllDocsQuery(); 
99
100         startAccess(null, session, false);
101         
102         TopDocs td = searcher.search(query, Integer.MAX_VALUE);
103         
104         ScoreDoc[ ] scoreDocs = td.scoreDocs; 
105         List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
106
107         final Map<String, String> classMap = new THashMap<String, String>();
108         for (Pair<String, String> field : r.getFields()) {
109             classMap.put(field.first, field.second);
110         }
111
112         for(ScoreDoc scoreDoc:scoreDocs) {
113             Document doc = reader.document(scoreDoc.doc);
114             List<IndexableField> fs = doc.getFields();
115             Object[] o = new Object[fs.size()];
116             int index = 0; 
117             for (IndexableField f : fs) {
118                 String clazz = classMap.get(f.name());
119                 if ("Long".equals(clazz)) {
120                     o[index++] = Long.parseLong(f.stringValue());
121                 } else {
122                     o[index++] = f.stringValue();
123                 }
124             }
125             result.add(o);
126         }
127
128         changeState(monitor, session, State.READY);
129
130         return result;
131
132     }
133     
134     public void commit() {
135         try {
136             if(writer != null)
137                 writer.commit();
138         } catch (IOException e) {
139             getLogger().error("Index commit failed", e);
140         }
141     }
142
143 //    public static String cacheReport() {
144 //        StringBuilder sb = new StringBuilder();
145 //        sb.append("Directories: ").append(directories.size()).append("\n");
146 //        for (String key : directories.keySet()) {
147 //            RAMDirectory dir = directories.get(key);
148 //            if (dir != null) {
149 //                sb.append("\t").append(dir).append("\n");
150 //            }
151 //        }
152 //        sb.append("Searchers: ").append(searchers.size()).append("\n");
153 //        for (String key : searchers.keySet()) {
154 //            IndexedRelationsMemorySearcher s = searchers.get(key);
155 //            if (s != null) {
156 //                sb.append("\t").append(s.getClass()).append(": ").append(s.getIndexPath()).append("\n");
157 //            }
158 //        }
159 //        return sb.toString();
160 //    }
161
162     @Override
163     Directory getDirectory(Session session) throws IOException {
164         MemoryIndexing mem = MemoryIndexing.getInstance(session);
165         String path = indexPath.toAbsolutePath().toString();
166         return mem.getDirectory(path, Queries.getAnalyzer());
167     }
168     
169     @Override
170     Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
171         MemoryIndexing mem = MemoryIndexing.getInstance(session);
172         changed.clear();
173         
174         String path = indexPath.toAbsolutePath().toString();
175         mem.remove(path);
176         
177         return null;
178         
179     }
180     
181     @Override
182     protected boolean requireChangeInfoOnReplace() {
183         return false;
184     }
185
186     @Override
187     protected Logger getLogger() {
188         return LOGGER;
189     }
190     
191 }