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.List;
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;
38 import gnu.trove.map.hash.THashMap;
39 import gnu.trove.set.hash.TLongHashSet;
42 * @author Tuukka Lehtonen
43 * @author Antti Villberg
45 public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
47 private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsMemorySearcher.class);
49 final IndexedRelationsSearcher backend;
50 final GenericRelation r;
52 TLongHashSet changed = new TLongHashSet();
54 IndexedRelationsMemorySearcher(RequestProcessor session, IndexedRelationsSearcher backend, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
55 super(session, relation, input);
56 this.backend = backend;
62 String getDescriptor() {
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);
72 super.insertIndex(monitor, r, boundLength, documentsData);
76 boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
77 int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
79 for(Object[] o : documentsData) {
80 Long resource = (Long)o[1];
81 changed.add(resource);
83 return super.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
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());
93 super.removeIndex(monitor, r, processor, key, keyValues);
96 public List<Object[]> allDocs(IProgressMonitor monitor, Session session) throws IOException {
98 Query query = new MatchAllDocsQuery();
100 startAccess(null, session, false);
102 TopDocs td = searcher.search(query, Integer.MAX_VALUE);
104 ScoreDoc[ ] scoreDocs = td.scoreDocs;
105 List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
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);
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()];
117 for (IndexableField f : fs) {
118 String clazz = classMap.get(f.name());
119 if ("Long".equals(clazz)) {
120 o[index++] = Long.parseLong(f.stringValue());
122 o[index++] = f.stringValue();
128 changeState(monitor, session, State.READY);
136 public void commit() {
140 } catch (CorruptIndexException e) {
142 } catch (IOException e) {
147 // public static String cacheReport() {
148 // StringBuilder sb = new StringBuilder();
149 // sb.append("Directories: ").append(directories.size()).append("\n");
150 // for (String key : directories.keySet()) {
151 // RAMDirectory dir = directories.get(key);
152 // if (dir != null) {
153 // sb.append("\t").append(dir).append("\n");
156 // sb.append("Searchers: ").append(searchers.size()).append("\n");
157 // for (String key : searchers.keySet()) {
158 // IndexedRelationsMemorySearcher s = searchers.get(key);
160 // sb.append("\t").append(s.getClass()).append(": ").append(s.getIndexPath()).append("\n");
163 // return sb.toString();
167 Directory getDirectory(Session session) throws IOException {
168 MemoryIndexing mem = MemoryIndexing.getInstance(session);
169 String path = indexPath.toAbsolutePath().toString();
170 return mem.getDirectory(path, Queries.getAnalyzer());
174 Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
175 MemoryIndexing mem = MemoryIndexing.getInstance(session);
178 String path = indexPath.toAbsolutePath().toString();
186 protected boolean requireChangeInfoOnReplace() {
191 protected Logger getLogger() {