1 /*******************************************************************************
2 * Copyright (c) 2007, 2015 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 * Semantum Oy - Fix for simantics issue #6053
12 *******************************************************************************/
13 package org.simantics.db.indexing;
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
24 import java.util.concurrent.CompletableFuture;
25 import java.util.concurrent.Semaphore;
26 import java.util.concurrent.atomic.AtomicReference;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.DocumentStoredFieldVisitor;
30 import org.apache.lucene.document.Field;
31 import org.apache.lucene.document.FieldType;
32 import org.apache.lucene.document.LongField;
33 import org.apache.lucene.document.TextField;
34 import org.apache.lucene.index.CorruptIndexException;
35 import org.apache.lucene.index.DirectoryReader;
36 import org.apache.lucene.index.FieldInfo;
37 import org.apache.lucene.index.IndexNotFoundException;
38 import org.apache.lucene.index.IndexReader;
39 import org.apache.lucene.index.IndexWriter;
40 import org.apache.lucene.index.IndexWriterConfig;
41 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
42 import org.apache.lucene.index.IndexableField;
43 import org.apache.lucene.index.StoredFieldVisitor;
44 import org.apache.lucene.index.Term;
45 import org.apache.lucene.queryparser.classic.ParseException;
46 import org.apache.lucene.search.IndexSearcher;
47 import org.apache.lucene.search.MatchAllDocsQuery;
48 import org.apache.lucene.search.Query;
49 import org.apache.lucene.search.ScoreDoc;
50 import org.apache.lucene.search.TermQuery;
51 import org.apache.lucene.search.TopDocs;
52 import org.apache.lucene.store.Directory;
53 import org.apache.lucene.store.FSDirectory;
54 import org.apache.lucene.util.Version;
55 import org.eclipse.core.runtime.IProgressMonitor;
56 import org.eclipse.core.runtime.SubMonitor;
57 import org.simantics.databoard.util.ObjectUtils;
58 import org.simantics.db.ReadGraph;
59 import org.simantics.db.RequestProcessor;
60 import org.simantics.db.Resource;
61 import org.simantics.db.Session;
62 import org.simantics.db.common.request.SafeName;
63 import org.simantics.db.common.utils.NameUtils;
64 import org.simantics.db.exception.DatabaseException;
65 import org.simantics.db.indexing.internal.IndexingJob;
66 import org.simantics.db.layer0.adapter.GenericRelation;
67 import org.simantics.db.request.Read;
68 import org.simantics.db.service.CollectionSupport;
69 import org.simantics.db.service.SerialisationSupport;
70 import org.simantics.utils.FileUtils;
71 import org.simantics.utils.datastructures.Pair;
72 import org.simantics.utils.threads.ThreadUtils;
73 import org.slf4j.Logger;
75 import gnu.trove.map.hash.THashMap;
78 * @author Tuukka Lehtonen
79 * @author Antti Villberg
81 abstract public class IndexedRelationsSearcherBase {
83 protected enum State {
84 // No index is available
86 // An index is available, but there is a problem with it
88 // An index is available but no reader or writer is ready
92 // A writer (and a reader) is ready
96 private State state = State.READY;
97 private Throwable exception;
99 public Throwable getException() {
103 public void setProblem(Throwable t) {
105 getLogger().error("Setting problem for {} and previous state {}", this, this.state, t);
106 this.state = State.PROBLEM;
110 public void setNone() {
111 this.state = State.NONE;
114 public void setReady() {
115 this.state = State.READY;
118 protected boolean checkState(State state) {
119 return this.state == state;
122 protected void assertState(State state) throws AssertionError {
124 if(this.state != state) throw new AssertionError("Illegal state, expected " + state.name() + " but was in " + this.state.name());
128 public void changeState(IProgressMonitor monitor, Session session, State state) {
129 changeState(monitor, session, state, 0);
132 protected void changeState(IProgressMonitor monitor, Session session, State state, int depth) {
134 if (this.state == state) {
135 if (getLogger().isDebugEnabled())
136 getLogger().debug("Trying to change state {} to the same as previous state {} in depth {} with {}", state, this.state, depth, this);
140 if (IndexPolicy.TRACE_INDEX_MANAGEMENT)
141 System.err.println("Index state " + this.state.name() + " => " + state.name() + " " + this);
145 // Try to exit problem state
146 if (State.PROBLEM == this.state && depth > 0) {
147 getLogger().info("Try to exit problem state for {} and state {}", this, state);
148 Throwable t = bestEffortClear(monitor, session);
150 getLogger().error("Best effort clear has failed for state {} and this {}", state, this, t);
154 // Managed to get into initial state
155 this.state = State.NONE;
156 getLogger().info("Managed to get into initial state {}", this.state);
160 // Cannot move into read from no index
161 if (State.NONE == this.state && State.READ == state) {
162 if (getLogger().isDebugEnabled())
163 getLogger().debug("Cannot move into read from no index in {} with state {}", this, state);
166 // Cannot move into write from no index
167 if (State.NONE == this.state && State.WRITE == state) {
168 if (getLogger().isDebugEnabled())
169 getLogger().debug("Cannot move into write from no index in {} with state {}", this, state);
173 boolean success = false;
177 if (searcher != null) {
180 if (reader != null) {
190 if (State.READ == state || State.WRITE == state) {
194 boolean forWriting = State.WRITE == state;
196 if (directory != null)
197 throw new IllegalStateException(getDescriptor() + "Index already loaded");
199 SubMonitor mon = SubMonitor.convert(monitor, 100);
201 mon.beginTask("Loading index", 100);
203 if (IndexPolicy.TRACE_INDEX_LOAD)
204 System.out.println(getDescriptor() + "Loading Lucene index from " + indexPath + " for " + (forWriting ? "writing" : "reading"));
206 long start = System.nanoTime();
208 directory = getDirectory(session);
211 // Never overwrite an index that is about to be loaded.
212 // TODO: could use OpenMode.CREATE_OR_APPEND but must test first
213 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, Queries.getAnalyzer()).setOpenMode(OpenMode.APPEND);
215 // FIXME: platform #4676
216 writer = new IndexWriter(directory, config);
217 } catch (IndexNotFoundException e) {
218 // There was no pre-existing index on disk. Create it now.
219 writer = new IndexWriter(directory, config.setOpenMode(OpenMode.CREATE));
222 reader = DirectoryReader.open(directory);
223 searcher = new IndexSearcher(reader);
225 reader = DirectoryReader.open(directory);
226 searcher = new IndexSearcher(reader);
229 long end = System.nanoTime();
233 if (IndexPolicy.PERF_INDEX_LOAD) {
234 double time = (end - start) * 1e-6;
235 System.out.println(getDescriptor() + "Loaded Lucene index from " + indexPath + " for " + (forWriting ? "writing" : "reading") + " in " + time + " ms");
242 } catch (Throwable t) {
247 this.state = State.PROBLEM;
248 changeState(monitor, session, State.NONE, depth+1);
258 public static final FieldType STRING_TYPE = new FieldType();
261 STRING_TYPE.setIndexed(true);
262 STRING_TYPE.setStored(true);
263 STRING_TYPE.setTokenized(true);
264 STRING_TYPE.freeze();
267 protected static Field makeField(String fieldName, String fieldClass) throws DatabaseException {
268 switch (fieldClass) {
269 case "Long": return new LongField(fieldName, 0L, Field.Store.YES);
270 case "String": return new Field (fieldName, "", STRING_TYPE);
271 case "Text": return new TextField(fieldName, "", Field.Store.YES);
273 throw new DatabaseException("Can only index Long, String and Text fields, encountered field type " + fieldClass);
277 protected static Field[] makeFieldsForRelation(GenericRelation r, int boundLength, Document document) throws DatabaseException {
278 Pair<String, String>[] fields = r.getFields();
279 Field[] fs = new Field[Math.max(0, fields.length - boundLength)];
280 for (int i = boundLength; i < fields.length; i++) {
281 Field f = makeField(fields[i].first, fields[i].second);
282 fs[i - boundLength] = f;
283 if (document != null)
289 void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData)
290 throws CorruptIndexException, IOException, DatabaseException {
291 assertAccessOpen(true);
293 if (IndexPolicy.TRACE_INDEX_UPDATE)
294 System.out.println(getDescriptor() + "Inserting " + documentsData.size() + " documents into index at " + indexPath);
296 long start = 0, end = 0;
297 if (IndexPolicy.PERF_INDEX_UPDATE)
298 start = System.nanoTime();
301 Document document = new Document();
302 Field[] fs = makeFieldsForRelation(r, boundLength, document);
304 for (Object[] documentData : documentsData) {
305 if (setFields(fs, documentData) == null)
308 if (IndexPolicy.TRACE_INDEX_UPDATE)
309 System.out.println(getDescriptor() + "Inserting document " + document);
311 writer.addDocument(document);
314 if (IndexPolicy.PERF_INDEX_UPDATE) {
315 end = System.nanoTime();
316 double ms = (end - start) * 1e-6;
317 System.out.println(getDescriptor() + "Inserted " + documentsData.size() + " documents into index at " + indexPath + " in " + ms + " ms");
324 void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key, Collection<Object> keyValues) throws DatabaseException, CorruptIndexException, IOException {
325 assertAccessOpen(true);
327 if (IndexPolicy.TRACE_INDEX_UPDATE)
328 System.out.println(getDescriptor() + "Removing " + keyValues.size() + " documents from index at " + indexPath);
330 long start = 0, end = 0;
331 if (IndexPolicy.PERF_INDEX_UPDATE)
332 start = System.nanoTime();
335 for (Object keyValue : keyValues) {
336 Term removedTerm = null;
337 if (keyValue instanceof Long) {
338 removedTerm = IndexUtils.longTerm(key, (Long) keyValue);
339 } else if (keyValue instanceof String) {
340 removedTerm = new Term(key, (String) keyValue);
342 // FIXME: should throw an exception for illegal input data but this would leave the index in an incoherent state
343 getLogger().error("Attempting to remove document from index of {} with key {} and unrecognized key value type {} : {}", input, key, keyValue, keyValue != null ? keyValue.getClass() : "null");
347 if (IndexPolicy.TRACE_INDEX_UPDATE)
348 System.out.println(getDescriptor() + "Removing document with key " + removedTerm);
349 writer.deleteDocuments(removedTerm);
352 if (IndexPolicy.PERF_INDEX_UPDATE) {
353 end = System.nanoTime();
354 double ms = (end - start) * 1e-6;
355 System.out.println(getDescriptor() + "Removed " + keyValues.size() + " documents from index at " + indexPath + " in " + ms + " ms");
362 void removeIndex(IProgressMonitor monitor) throws DatabaseException, CorruptIndexException, IOException {
363 assertAccessOpen(true);
365 long start = 0, end = 0;
366 if (IndexPolicy.PERF_INDEX_UPDATE)
367 start = System.nanoTime();
373 if (IndexPolicy.PERF_INDEX_UPDATE) {
374 end = System.nanoTime();
375 double ms = (end - start) * 1e-6;
376 System.out.println(getDescriptor() + "Removed all documents from index at " + indexPath + " in " + ms + " ms");
383 boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r, int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException, DatabaseException {
385 boolean didReplace = false;
387 assertAccessOpen(true);
388 if (keyValues.size() != documentsData.size())
389 throw new IllegalArgumentException("keyValues size does not match documents data size, " + keyValues.size() + " <> " + documentsData.size());
391 if (IndexPolicy.TRACE_INDEX_UPDATE)
392 System.out.println(getDescriptor() + "Replacing " + keyValues.size() + " documents from index at " + indexPath);
394 long start = 0, end = 0;
395 if (IndexPolicy.PERF_INDEX_UPDATE)
396 start = System.nanoTime();
399 Iterator<Object> keyIt = keyValues.iterator();
400 Iterator<Object[]> documentDataIt = documentsData.iterator();
402 Document document = new Document();
403 Field[] fs = makeFieldsForRelation(r, boundLength, document);
406 while (keyIt.hasNext()) {
407 Object keyValue = keyIt.next();
408 Object[] documentData = documentDataIt.next();
410 Term removedTerm = null;
411 if (keyValue instanceof Long) {
412 removedTerm = IndexUtils.longTerm(key, (Long) keyValue);
413 } else if (keyValue instanceof String) {
414 removedTerm = new Term(key, (String) keyValue);
416 // FIXME: should throw an exception for illegal input data but this would leave the index in an incoherent state
417 System.err.println("[" + getClass().getSimpleName() + "] Unrecognized document key to remove '" + keyValue + "', only " + String.class + " and " + Resource.class + " are supported.");
418 continue nextDocument;
421 if (setFields(fs, documentData) == null)
422 continue nextDocument;
424 if (IndexPolicy.TRACE_INDEX_UPDATE)
425 System.out.println(getDescriptor() + "Replacing document with key " + removedTerm + " with " + document);
427 boolean done = false;
428 if(requireChangeInfoOnReplace()) {
429 TopDocs exist = searcher.search(new TermQuery(removedTerm), null, 2);
430 if(exist.scoreDocs.length == 1) {
431 Document doc = reader.document(exist.scoreDocs[0].doc);
432 if(!areSame(doc, document)) {
433 writer.deleteDocuments(removedTerm);
434 writer.addDocument(document);
436 if (IndexPolicy.TRACE_INDEX_UPDATE)
437 System.out.println("-replaced single existing");
439 if (IndexPolicy.TRACE_INDEX_UPDATE)
440 System.out.println("-was actually same than single existing");
446 writer.deleteDocuments(removedTerm);
447 writer.addDocument(document);
449 if (IndexPolicy.TRACE_INDEX_UPDATE)
450 System.out.println("-had many or none - removed all existing");
455 if (IndexPolicy.PERF_INDEX_UPDATE) {
456 end = System.nanoTime();
457 double ms = (end - start) * 1e-6;
458 System.out.println(getDescriptor() + "Replaced " + keyValues.size() + " documents from index at " + indexPath + " in " + ms + " ms");
468 protected boolean requireChangeInfoOnReplace() {
472 private boolean areSame(Document d1, Document d2) {
473 List<IndexableField> fs1 = d1.getFields();
474 List<IndexableField> fs2 = d2.getFields();
475 if(fs1.size() != fs2.size()) return false;
476 for(int i=0;i<fs1.size();i++) {
477 IndexableField f1 = fs1.get(i);
478 IndexableField f2 = fs2.get(i);
479 String s1 = f1.stringValue();
480 String s2 = f2.stringValue();
481 if (IndexPolicy.TRACE_INDEX_UPDATE)
482 System.err.println("areSame " + s1 + " vs. " + s2 );
483 if(!ObjectUtils.objectEquals(s1,s2)) return false;
488 final RequestProcessor session;
490 final Resource relation;
493 * The schema of the index, i.e. the fields that will be indexed per
494 * document for the specified relation. Since the relation stays the same
495 * throughout the lifetime of this class, the index schema is also assumed
496 * to the same. This means that {@link GenericRelation#getFields()} is
497 * assumed to stay the same.
499 final IndexSchema schema;
511 IndexSearcher searcher;
513 IndexedRelationsSearcherBase(RequestProcessor session, Resource relation, Resource input) {
514 this.session = session;
515 this.relation = relation;
517 this.indexPath = getIndexDirectory(session.getSession(), relation, input);
518 if(isIndexAvailable()) {
523 this.schema = IndexSchema.readFromRelation(session, relation);
526 Directory getDirectory(Session session) throws IOException {
527 return FSDirectory.open(indexPath.toFile());
530 abstract String getDescriptor();
533 * Ensures that searcher is in read or write state.
535 * @param forWriting <code>true</code> to open index for writing,
536 * <code>false</code> for reading
537 * @return true is required state was reached
540 boolean startAccess(IProgressMonitor monitor, Session session, boolean forWriting) {
542 changeState(monitor, session, State.WRITE);
543 return checkState(State.WRITE);
545 changeState(monitor, session, State.READ);
546 return checkState(State.READ);
550 boolean hasAccess(boolean forWriting) {
553 return checkState(State.WRITE);
555 return checkState(State.WRITE) || checkState(State.READ);
559 void assertAccessOpen(boolean forWriting) {
561 if(!checkState(State.WRITE))
562 throw new IllegalStateException("index not opened for writing (directory=" + directory + ", reader=" + reader + ")");
564 if(!(checkState(State.WRITE) || checkState(State.READ)))
565 throw new IllegalStateException("index not opened for reading (directory=" + directory + ", writer=" + writer + ")");
568 void closeWriter(IndexWriter writer) throws CorruptIndexException, IOException {
573 // May throw OOME, see IndexWriter javadoc for the correct actions.
575 } catch (OutOfMemoryError e) {
581 public static String getPattern(GenericRelation relation, int boundCount) {
583 for (int i = 0; i < boundCount; i++)
585 for (int i = 0; i < relation.getFields().length - boundCount; i++)
590 void initializeIndex(IProgressMonitor monitor, ReadGraph graph, Object[] bound, boolean overwrite)
591 throws IOException, DatabaseException
593 IndexingJob.jobifyIfPossible(
595 "Reindexing " + NameUtils.getSafeLabel(graph, input),
598 GenericRelation r = graph.adapt(relation, GenericRelation.class);
600 throw new DatabaseException("Given resource " + relation + "could not be adapted to GenericRelation.");
602 GenericRelation selection = r.select(getPattern(r, bound.length), bound);
604 List<Object[]> results = selection.realize(graph);
605 initializeIndexImpl(new CompletableFuture<>(), mon, r, results, bound, overwrite);
606 } catch (IOException e) {
607 getLogger().error("Index is in problematic state! {}", this, e);
608 throw new DatabaseException(e);
613 private static final int INDEXING_THREAD_COUNT = 2; // this is quite good parallelism level for lucene
615 void initializeIndexImpl(CompletableFuture<?> result, IProgressMonitor monitor, GenericRelation r, List<Object[]> results, final Object[] bound, boolean overwrite) throws IOException {
617 final SubMonitor mon = SubMonitor.convert(monitor, 100);
619 if (IndexPolicy.TRACE_INDEX_INIT)
620 System.out.println(getDescriptor() + "Initializing index at " + indexPath + " (overwrite = " + overwrite + ")");
621 mon.beginTask("Initializing Index", 100);
624 if (Files.exists(indexPath)) {
625 mon.subTask("Erasing previous index");
626 if (getLogger().isDebugEnabled())
627 getLogger().debug("Erasing previous index {}", indexPath.toAbsolutePath());
628 FileUtils.emptyDirectory(indexPath);
632 final AtomicReference<FSDirectory> directory = new AtomicReference<FSDirectory>();
633 final AtomicReference<IndexWriter> writer = new AtomicReference<IndexWriter>();
636 mon.subTask("Start index write");
637 Files.createDirectories(indexPath);
639 directory.set(FSDirectory.open(indexPath.toFile()));
640 IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_4_9, Queries.getAnalyzer()).setOpenMode(OpenMode.CREATE);
641 writer.set(new IndexWriter(directory.get(), conf));
645 long realizeStart = 0;
646 if (IndexPolicy.PERF_INDEX_INIT)
647 realizeStart = System.nanoTime();
649 mon.subTask("Calculating indexed content");
654 if (IndexPolicy.PERF_INDEX_INIT)
655 System.out.println(getDescriptor() + "Realized index with " + results.size() + " entries at " + indexPath + " in " + (1e-9 * (System.nanoTime()-realizeStart)) + " seconds.");
657 if (IndexPolicy.TRACE_INDEX_INIT)
658 System.out.println(getDescriptor() + "Indexed relation " + r + " produced " + results.size() + " results");
660 long start = IndexPolicy.PERF_INDEX_INIT ? System.nanoTime() : 0;
662 mon.subTask("Indexing content");
663 final Semaphore s = new Semaphore(0);
664 mon.setWorkRemaining(results.size());
665 for (int i = 0; i < INDEXING_THREAD_COUNT; i++) {
666 final int startIndex = i;
667 ThreadUtils.getBlockingWorkExecutor().submit(() -> {
669 Document document = new Document();
670 Field[] fs = makeFieldsForRelation(r, bound.length, document);
672 for (int index = startIndex; index < results.size(); index += INDEXING_THREAD_COUNT) {
673 if (setFields(fs, results.get(index)) == null)
676 writer.get().addDocument(document);
677 } catch (CorruptIndexException e) {
678 getLogger().error("Index is corrupted! {}", this, e);
679 throw new IllegalStateException(e);
680 } catch (IOException e) {
681 getLogger().error("Index is in problematic state! {}", this, e);
682 throw new IllegalStateException(e);
689 } catch (DatabaseException e) {
690 throw new IllegalStateException(e);
698 s.acquire(INDEXING_THREAD_COUNT);
699 } catch (InterruptedException e) {
700 getLogger().error("Could not initialize index {}", this, e);
703 // http://www.gossamer-threads.com/lists/lucene/java-dev/47895
704 // and http://lucene.apache.org/java/docs/index.html#27+November+2011+-+Lucene+Core+3.5.0
705 // advise against calling optimize at all. So let's not do it anymore.
706 //writer.get().optimize();
707 //writer.get().commit();
709 mon.subTask("Flushing");
711 if (IndexPolicy.PERF_INDEX_INIT)
712 System.out.println(getDescriptor() + "Wrote index at " + indexPath + " in " + (1e-9 * (System.nanoTime()-start)) + " seconds.");
714 result.complete(null);
715 // } catch (DatabaseException e) {
716 // getLogger().error("Could not initialize index due to db {}", this, e);
719 closeWriter(writer.getAndSet(null));
721 FileUtils.uncheckedClose(directory.getAndSet(null));
724 } catch (Throwable t) {
725 getLogger().error("Could not initialize index", t);
726 result.completeExceptionally(t);
731 public List<Object[]> debugDocs(IProgressMonitor monitor) throws ParseException, IOException, DatabaseException {
733 Query query = new MatchAllDocsQuery();
735 TopDocs td = searcher.search(query, Integer.MAX_VALUE);
737 ScoreDoc[ ] scoreDocs = td.scoreDocs;
738 List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
740 for(ScoreDoc scoreDoc:scoreDocs) {
744 Document doc = reader.document(scoreDoc.doc);
745 List<IndexableField> fs = doc.getFields();
746 Object[] o = new Object[fs.size()];
748 for (IndexableField f : fs) {
749 o[index++] = f.stringValue();
753 } catch (CorruptIndexException e) {
754 getLogger().error("Index is corrupted! {}", this, e);
755 throw new DatabaseException(e);
756 } catch (IOException e) {
757 getLogger().error("Index is in problematic state! {}", this, e);
758 throw new DatabaseException(e);
768 List<Map<String, Object>> doSearch(IProgressMonitor monitor, RequestProcessor processor, String search, int maxResultCount) throws ParseException, IOException,
771 // An empty search string will crash QueryParser
772 // Just return no results for empty queries.
773 //System.out.println("search: '" + search + "'");
774 if (search.isEmpty())
775 return Collections.emptyList();
777 assertAccessOpen(false);
779 Query query = Queries.parse(search, schema);
781 long start = System.nanoTime();
783 maxResultCount = Math.min(maxResultCount, searcher.getIndexReader().numDocs());
784 if (maxResultCount == 0)
785 return Collections.emptyList();
787 final TopDocs docs = searcher.search(query, null, maxResultCount);
789 // for(Object[] o : debugDocs(monitor)) {
790 // System.err.println("-" + Arrays.toString(o));
793 if (IndexPolicy.PERF_INDEX_QUERY) {
794 long end = System.nanoTime();
795 System.out.println(getDescriptor() + "search(" + search + ", " + maxResultCount + ") into index at " + indexPath + " took " + (1e-9 * (end-start)) + " seconds.");
798 if (docs.totalHits == 0) {
799 return Collections.emptyList();
802 return processor.syncRequest(new Read<List<Map<String, Object>>>() {
805 public List<Map<String, Object>> perform(ReadGraph graph) throws DatabaseException {
807 GenericRelation r = graph.adapt(relation, GenericRelation.class);
809 throw new DatabaseException("Given resource " + graph.syncRequest(new SafeName(relation))
810 + "could not be adapted to GenericRelation.");
812 SerialisationSupport support = graph.getService(SerialisationSupport.class);
814 List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(docs.scoreDocs.length);
816 final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
818 for (ScoreDoc scoreDoc : docs.scoreDocs) {
822 reader.document(scoreDoc.doc, visitor);
824 Document doc = visitor.getDocument();
826 List<IndexableField> fs = doc.getFields();
827 Map<String, Object> entry = new THashMap<String, Object>(fs.size());
828 for (IndexableField f : fs) {
829 IndexSchema.Type type = schema.typeMap.get(f.name());
830 if (type == IndexSchema.Type.LONG) {
831 entry.put(f.name(), support.getResource(f.numericValue().longValue()));
833 entry.put(f.name(), f.stringValue());
839 } catch (CorruptIndexException e) {
840 getLogger().error("Index is corrupted! {}", this, e);
841 throw new DatabaseException(e);
842 } catch (IOException e) {
843 getLogger().error("Index is in problematic state! {}", this, e);
844 throw new DatabaseException(e);
852 static class ResourceVisitor extends StoredFieldVisitor {
857 public Status needsField(FieldInfo fieldInfo) throws IOException {
858 if("Resource".equals(fieldInfo.name)) return Status.YES;
863 public void longField(FieldInfo fieldInfo, long value) throws IOException {
869 static class DumpVisitor extends StoredFieldVisitor {
871 public List<Object> values;
873 DumpVisitor(List<Object> values) {
874 this.values = values;
878 public Status needsField(FieldInfo fieldInfo) throws IOException {
883 public void longField(FieldInfo fieldInfo, long value) throws IOException {
888 public void stringField(FieldInfo fieldInfo, String value) throws IOException {
894 List<Resource> doSearchResources(IProgressMonitor monitor, RequestProcessor processor, String search, int maxResultCount) throws ParseException, IOException,
897 // An empty search string will crash QueryParser
898 // Just return no results for empty queries.
899 //System.out.println("search: '" + search + "'");
900 if (search.isEmpty())
901 return Collections.emptyList();
903 assertAccessOpen(false);
905 Query query = Queries.parse(search, schema);
907 long start = System.nanoTime();
909 maxResultCount = Math.min(maxResultCount, searcher.getIndexReader().numDocs());
910 if (maxResultCount == 0)
911 return Collections.emptyList();
913 final TopDocs docs = searcher.search(query, null, maxResultCount);
915 // for(Object[] o : debugDocs(monitor)) {
916 // System.err.println("-" + Arrays.toString(o));
919 if (IndexPolicy.PERF_INDEX_QUERY) {
920 long end = System.nanoTime();
921 System.out.println(getDescriptor() + "search(" + search + ", " + maxResultCount + ") into index at " + indexPath + " took " + (1e-9 * (end-start)) + " seconds.");
924 if (docs.totalHits == 0) {
925 return Collections.emptyList();
928 return processor.syncRequest(new Read<List<Resource>>() {
931 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
933 CollectionSupport cs = graph.getService(CollectionSupport.class);
934 SerialisationSupport support = graph.getService(SerialisationSupport.class);
936 List<Resource> result = cs.createList();
938 ResourceVisitor visitor = new ResourceVisitor();
940 for (ScoreDoc scoreDoc : docs.scoreDocs) {
942 reader.document(scoreDoc.doc, visitor);
943 result.add(support.getResource(visitor.id));
944 } catch (CorruptIndexException e) {
945 getLogger().error("Index is corrupted! {}", this, e);
946 throw new DatabaseException(e);
947 } catch (IOException e) {
948 getLogger().error("Index is in problematic state! {}", this, e);
949 throw new DatabaseException(e);
957 List<Object> doList(IProgressMonitor monitor, RequestProcessor processor) throws ParseException, IOException,
960 assertAccessOpen(false);
962 Query query = new MatchAllDocsQuery();
964 final TopDocs docs = searcher.search(query, Integer.MAX_VALUE);
966 ArrayList<Object> result = new ArrayList<Object>();
968 DumpVisitor visitor = new DumpVisitor(result);
970 for (ScoreDoc scoreDoc : docs.scoreDocs) {
974 reader.document(scoreDoc.doc, visitor);
976 } catch (CorruptIndexException e) {
977 getLogger().error("Index is corrupted! {}", this, e);
978 throw new DatabaseException(e);
979 } catch (IOException e) {
980 getLogger().error("Index is in problematic state! {}", this, e);
981 throw new DatabaseException(e);
990 protected static Path getIndexDirectory(Session session, Resource relation, Resource input) {
991 Path path = DatabaseIndexing.getIndexLocation(session, relation, input);
992 // System.out.println("getIndexDirectory = " + path);
996 Path getIndexPath() {
1000 boolean isIndexAvailable() {
1001 return Files.isDirectory(indexPath);
1004 abstract Throwable bestEffortClear(IProgressMonitor monitor, Session session);
1007 * Start from scratch. Clear all caches and rebuild the index.
1009 Throwable clearDirectory(IProgressMonitor monitor, Session session) {
1011 Path file = getIndexPath();
1014 FileUtils.delete(file);
1015 } catch (Throwable t) {
1016 getLogger().error("Could not delete directory {}", file.toAbsolutePath(), t);
1019 if (Files.exists(file))
1020 return new IllegalStateException("Failed to delete directory " + file.toAbsolutePath());
1024 private Field[] setFields(Field[] fs, Object[] result) {
1025 for (int i = 0; i < result.length; i++) {
1026 Object value = result[i];
1027 if (value instanceof String) {
1028 if (IndexPolicy.DEBUG_INDEX_INIT)
1029 System.out.println(getDescriptor() + "index " + fs[i].name() + " = " + value + " : String");
1030 fs[i].setStringValue((String) value);
1031 } else if (value instanceof Long) {
1032 if (IndexPolicy.DEBUG_INDEX_INIT)
1033 System.out.println(getDescriptor() + "index " + fs[i].name() + " = " + value + " : Long");
1034 fs[i].setLongValue((Long) value);
1036 getLogger().error("Can only index Long and String fields, encountered " + value);
1043 protected abstract Logger getLogger();
1046 public String toString() {
1047 return getClass().getSimpleName() + " [" + String.valueOf(schema) + ", " + String.valueOf(relation) + ", " + String.valueOf(input) + ", " + String.valueOf(indexPath) + ", " + String.valueOf(directory) + ", " + String.valueOf(state) + "]";