1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.indexing;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
21 import org.apache.lucene.index.CorruptIndexException;
22 import org.apache.lucene.queryparser.classic.ParseException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.simantics.db.RequestProcessor;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Session;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.adapter.GenericRelation;
29 import org.simantics.utils.datastructures.Pair;
32 * @author Tuukka Lehtonen
33 * @author Antti Villberg
35 public class IndexedRelationsSearcher extends IndexedRelationsSearcherBase {
37 IndexedRelationsMemorySearcher cache;
39 IndexedRelationsSearcher(RequestProcessor session, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
40 super(session, relation, input);
41 this.cache = new IndexedRelationsMemorySearcher(session, this, relation, input, r);
45 String getDescriptor() {
50 public void setProblem(Throwable t) {
56 boolean startAccess(IProgressMonitor monitor, Session session, boolean forWriting) {
57 boolean success = super.startAccess(monitor, session, false);
58 if(!success) return false;
59 success = cache.startAccess(monitor, session, forWriting);
61 setProblem(cache.getException());
67 void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData)
68 throws CorruptIndexException, IOException, DatabaseException {
70 Collection<Object> keyValues = new ArrayList<Object>();
71 for(Object[] data : documentsData) {
72 keyValues.add(data[1]);
75 cache.replaceIndex(monitor, "Resource", keyValues, r, boundLength, documentsData);
81 void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key, Collection<Object> keyValues) throws DatabaseException,CorruptIndexException, IOException {
83 Collection<Object[]> documentsData = new ArrayList<Object[]>();
85 Pair<String,String>[] fields = r.getFields();
87 for(Object keyValue : keyValues) {
88 Object[] data = new Object[fields.length-1];
90 for(int i=1;i<fields.length;i++) {
91 String fieldName = fields[i].first;
92 if(key.equals(fieldName)) {
93 data[index++] = keyValue;
95 String fieldClass = fields[i].second;
96 if ("Long".equals(fieldClass)) {
98 } else if ("String".equals(fieldClass) || "Text".equals(fieldClass)) {
101 throw new DatabaseException("Can only index Long and String fields, encountered class " + fieldClass);
105 documentsData.add(data);
108 cache.replaceIndex(monitor, key, keyValues, r, 1, documentsData);
114 boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
115 int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
118 boolean result = cache.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
124 List<Map<String, Object>> persistentCachedSearch(IProgressMonitor monitor, RequestProcessor processor, String search,
125 int maxResultCount) throws ParseException, IOException, DatabaseException {
127 MemoryIndexing mem = MemoryIndexing.getInstance(session.getSession());
129 String key = indexPath.getAbsolutePath();
131 Map<String,List<Map<String, Object>>> cache = mem.persistentCache.get(key);
133 List<Map<String,Object>> result = cache.get(search);
134 if(result != null) return result;
137 startAccess(monitor, processor.getSession(), false);
139 List<Map<String, Object>> results = super.doSearch(monitor, processor, search, maxResultCount);
141 cache = new HashMap<String,List<Map<String,Object>>>();
142 mem.persistentCache.put(key, cache);
145 if(results.size() < 500)
146 cache.put(search, results);
152 List<Resource> persistentCachedSearchResources(IProgressMonitor monitor, RequestProcessor processor, String search,
153 int maxResultCount) throws ParseException, IOException, DatabaseException {
155 MemoryIndexing mem = MemoryIndexing.getInstance(session.getSession());
157 String key = indexPath.getAbsolutePath();
159 Map<String,List<Resource>> cache = mem.persistentCacheResources.get(key);
161 List<Resource> result = cache.get(search);
162 if(result != null) return result;
165 startAccess(monitor, processor.getSession(), false);
167 List<Resource> results = super.doSearchResources(monitor, processor, search, maxResultCount);
169 cache = new HashMap<String,List<Resource>>();
170 mem.persistentCacheResources.put(key, cache);
173 if(results.size() < 500)
174 cache.put(search, results);
180 List<Object> persistentCachedList(IProgressMonitor monitor, RequestProcessor processor) throws ParseException, IOException, DatabaseException {
182 startAccess(monitor, processor.getSession(), false);
184 List<Object> results = super.doList(monitor, processor);
191 List<Map<String, Object>> doSearch(IProgressMonitor monitor, RequestProcessor processor, String search,
192 int maxResultCount) throws ParseException, IOException, DatabaseException {
194 List<Map<String,Object>> persistent = persistentCachedSearch(monitor, processor, search, maxResultCount);
195 List<Map<String,Object>> cached = cache.doSearch(monitor, processor, search, maxResultCount);
197 ArrayList<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
199 for(Map<String,Object> m : persistent) {
200 Resource r = (Resource)m.get("Resource");
201 if(!cache.changed.contains(r.getResourceId())) {
205 result.addAll(cached);
211 List<Resource> doSearchResources(IProgressMonitor monitor, RequestProcessor processor, String search,
212 int maxResultCount) throws ParseException, IOException, DatabaseException {
214 List<Resource> persistent = persistentCachedSearchResources(monitor, processor, search, maxResultCount);
215 List<Resource> cached = cache.doSearchResources(monitor, processor, search, maxResultCount);
217 ArrayList<Resource> result = new ArrayList<Resource>();
218 for(Resource r : persistent) {
219 if(!cache.changed.contains(r.getResourceId())) {
223 result.addAll(cached);
228 List<Object> doList(IProgressMonitor monitor, RequestProcessor processor) throws ParseException, IOException, DatabaseException {
230 List<Object> persistent = persistentCachedList(monitor, processor);
232 // TODO: check that caches have been properly flushed
233 //List<Object> cached = cache.doList(monitor, processor);
234 //if(!cached.isEmpty()) throw new DatabaseException("doList does not support caching");
240 void applyChanges(IProgressMonitor monitor, Session session, GenericRelation r, Collection<Object[]> os) throws Exception {
244 ArrayList<Object> replaceKeys = new ArrayList<Object>();
245 ArrayList<Object[]> replaceValues = new ArrayList<Object[]>();
246 ArrayList<Object> removeKeys = new ArrayList<Object>();
247 for(Object[] o : os) {
248 Long parent = (Long)o[0];
249 Long key = (Long)o[1];
251 replaceKeys.add(key);
252 replaceValues.add(o);
258 changeState(monitor, session, State.READY);
260 super.startAccess(null, session, true);
262 super.replaceIndex(null, "Resource", replaceKeys, r, 1, replaceValues);
263 super.removeIndex(null, r, null, "Resource", removeKeys);
267 changeState(monitor, session, State.READY);
272 Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
275 changeState(monitor, session, State.NONE);
277 Throwable t = clearDirectory(monitor, session);
278 if(t != null) return t;
280 t = cache.bestEffortClear(monitor, session);
281 if(t != null) return t;
283 String key = indexPath.getAbsolutePath();
284 MemoryIndexing mem = MemoryIndexing.getInstance(session);
285 mem.persistentCache.remove(key);
286 mem.persistentCacheResources.remove(key);