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 gnu.trove.map.hash.THashMap;
15 import gnu.trove.set.hash.TLongHashSet;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.index.CorruptIndexException;
25 import org.apache.lucene.index.IndexableField;
26 import org.apache.lucene.queryparser.classic.ParseException;
27 import org.apache.lucene.search.MatchAllDocsQuery;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.search.ScoreDoc;
30 import org.apache.lucene.search.TopDocs;
31 import org.apache.lucene.store.Directory;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.simantics.db.RequestProcessor;
34 import org.simantics.db.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.layer0.adapter.GenericRelation;
38 import org.simantics.utils.datastructures.Pair;
41 * @author Tuukka Lehtonen
42 * @author Antti Villberg
44 public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
46 final IndexedRelationsSearcher backend;
47 final GenericRelation r;
49 TLongHashSet changed = new TLongHashSet();
51 IndexedRelationsMemorySearcher(RequestProcessor session, IndexedRelationsSearcher backend, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
52 super(session, relation, input);
53 this.backend = backend;
59 String getDescriptor() {
64 void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException, DatabaseException {
65 for(Object[] o : documentsData) {
66 Long resource = (Long)o[1];
67 changed.add(resource);
69 super.insertIndex(monitor, r, boundLength, documentsData);
73 boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
74 int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
76 for(Object[] o : documentsData) {
77 Long resource = (Long)o[1];
78 changed.add(resource);
80 return super.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
84 void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key,
85 Collection<Object> keyValues) throws DatabaseException, CorruptIndexException, IOException {
86 for(Object o : keyValues) {
87 Resource resource= (Resource)o;
88 changed.add(resource.getResourceId());
90 super.removeIndex(monitor, r, processor, key, keyValues);
93 public List<Object[]> allDocs(IProgressMonitor monitor, Session session) throws ParseException, IOException,
96 Query query = new MatchAllDocsQuery();
98 startAccess(null, session, false);
100 TopDocs td = searcher.search(query, Integer.MAX_VALUE);
102 ScoreDoc[ ] scoreDocs = td.scoreDocs;
103 List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
105 final Map<String, String> classMap = new THashMap<String, String>();
106 for (Pair<String, String> field : r.getFields()) {
107 classMap.put(field.first, field.second);
110 for(ScoreDoc scoreDoc:scoreDocs) {
114 Document doc = reader.document(scoreDoc.doc);
115 List<IndexableField> fs = doc.getFields();
116 Object[] o = new Object[fs.size()];
118 for (IndexableField f : fs) {
119 String clazz = classMap.get(f.name());
120 if ("Long".equals(clazz)) {
121 o[index++] = Long.parseLong(f.stringValue());
123 o[index++] = f.stringValue();
127 } catch (CorruptIndexException e) {
128 throw new DatabaseException(e);
129 } catch (IOException e) {
130 throw new DatabaseException(e);
135 changeState(monitor, session, State.READY);
143 public void commit() {
147 } catch (CorruptIndexException e) {
149 } catch (IOException e) {
154 // public static String cacheReport() {
155 // StringBuilder sb = new StringBuilder();
156 // sb.append("Directories: ").append(directories.size()).append("\n");
157 // for (String key : directories.keySet()) {
158 // RAMDirectory dir = directories.get(key);
159 // if (dir != null) {
160 // sb.append("\t").append(dir).append("\n");
163 // sb.append("Searchers: ").append(searchers.size()).append("\n");
164 // for (String key : searchers.keySet()) {
165 // IndexedRelationsMemorySearcher s = searchers.get(key);
167 // sb.append("\t").append(s.getClass()).append(": ").append(s.getIndexPath()).append("\n");
170 // return sb.toString();
174 Directory getDirectory(Session session) throws IOException {
175 MemoryIndexing mem = MemoryIndexing.getInstance(session);
176 String path = indexPath.getAbsolutePath();
177 return mem.getDirectory(path, Queries.getAnalyzer());
181 Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
185 MemoryIndexing mem = MemoryIndexing.getInstance(session);
188 String path = indexPath.getAbsolutePath();
196 protected boolean requireChangeInfoOnReplace() {