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;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * @author Tuukka Lehtonen
44 * @author Antti Villberg
46 public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
48 private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsMemorySearcher.class);
50 final IndexedRelationsSearcher backend;
51 final GenericRelation r;
53 TLongHashSet changed = new TLongHashSet();
55 IndexedRelationsMemorySearcher(RequestProcessor session, IndexedRelationsSearcher backend, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
56 super(session, relation, input);
57 this.backend = backend;
63 String getDescriptor() {
68 void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException, DatabaseException {
69 for(Object[] o : documentsData) {
70 Long resource = (Long)o[1];
71 changed.add(resource);
73 super.insertIndex(monitor, r, boundLength, documentsData);
77 boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
78 int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
80 for(Object[] o : documentsData) {
81 Long resource = (Long)o[1];
82 changed.add(resource);
84 return super.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
88 void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key,
89 Collection<Object> keyValues) throws DatabaseException, CorruptIndexException, IOException {
90 for(Object o : keyValues) {
91 Resource resource= (Resource)o;
92 changed.add(resource.getResourceId());
94 super.removeIndex(monitor, r, processor, key, keyValues);
97 public List<Object[]> allDocs(IProgressMonitor monitor, Session session) throws ParseException, IOException,
100 Query query = new MatchAllDocsQuery();
102 startAccess(null, session, false);
104 TopDocs td = searcher.search(query, Integer.MAX_VALUE);
106 ScoreDoc[ ] scoreDocs = td.scoreDocs;
107 List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
109 final Map<String, String> classMap = new THashMap<String, String>();
110 for (Pair<String, String> field : r.getFields()) {
111 classMap.put(field.first, field.second);
114 for(ScoreDoc scoreDoc:scoreDocs) {
118 Document doc = reader.document(scoreDoc.doc);
119 List<IndexableField> fs = doc.getFields();
120 Object[] o = new Object[fs.size()];
122 for (IndexableField f : fs) {
123 String clazz = classMap.get(f.name());
124 if ("Long".equals(clazz)) {
125 o[index++] = Long.parseLong(f.stringValue());
127 o[index++] = f.stringValue();
131 } catch (CorruptIndexException e) {
132 throw new DatabaseException(e);
133 } catch (IOException e) {
134 throw new DatabaseException(e);
139 changeState(monitor, session, State.READY);
147 public void commit() {
151 } catch (CorruptIndexException e) {
153 } catch (IOException e) {
158 // public static String cacheReport() {
159 // StringBuilder sb = new StringBuilder();
160 // sb.append("Directories: ").append(directories.size()).append("\n");
161 // for (String key : directories.keySet()) {
162 // RAMDirectory dir = directories.get(key);
163 // if (dir != null) {
164 // sb.append("\t").append(dir).append("\n");
167 // sb.append("Searchers: ").append(searchers.size()).append("\n");
168 // for (String key : searchers.keySet()) {
169 // IndexedRelationsMemorySearcher s = searchers.get(key);
171 // sb.append("\t").append(s.getClass()).append(": ").append(s.getIndexPath()).append("\n");
174 // return sb.toString();
178 Directory getDirectory(Session session) throws IOException {
179 MemoryIndexing mem = MemoryIndexing.getInstance(session);
180 String path = indexPath.toAbsolutePath().toString();
181 return mem.getDirectory(path, Queries.getAnalyzer());
185 Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
186 MemoryIndexing mem = MemoryIndexing.getInstance(session);
189 String path = indexPath.toAbsolutePath().toString();
197 protected boolean requireChangeInfoOnReplace() {
202 protected Logger getLogger() {