001: package org.compass.core.lucene.engine;
002:
003: import java.io.IOException;
004: import java.util.List;
005:
006: import org.apache.lucene.index.IndexReader;
007: import org.apache.lucene.index.MultiReader;
008: import org.apache.lucene.search.IndexSearcher;
009: import org.apache.lucene.search.MultiSearcher;
010: import org.apache.lucene.search.Searchable;
011: import org.apache.lucene.search.Searcher;
012: import org.compass.core.engine.SearchEngineException;
013: import org.compass.core.engine.SearchEngineInternalSearch;
014: import org.compass.core.lucene.engine.manager.LuceneIndexHolder;
015:
016: /**
017: * A Lucene specific search "internals", allowing for Lucene {@link IndexReader} and {@link Searcher}
018: * access.
019: *
020: * @author kimchy
021: */
022: public class LuceneSearchEngineInternalSearch implements
023: SearchEngineInternalSearch, LuceneDelegatedClose {
024:
025: private MultiSearcher searcher;
026: protected MultiReader reader;
027:
028: private boolean closed;
029: private List<LuceneIndexHolder> indexHoldersToClose;
030:
031: /**
032: * Creates a new instance, with a searcher and index holders which will be used
033: * to release when calling close.
034: *
035: * @param searcher The searcher, which is also used to construct the reader
036: * @param indexHolders Holders to be released when calling close.
037: */
038: public LuceneSearchEngineInternalSearch(MultiSearcher searcher,
039: List<LuceneIndexHolder> indexHolders) {
040: this .searcher = searcher;
041: this .indexHoldersToClose = indexHolders;
042: }
043:
044: /**
045: * Returns <code>true</code> if it represents an empty index scope.
046: */
047: public boolean isEmpty() {
048: return searcher == null
049: || searcher.getSearchables().length == 0;
050: }
051:
052: /**
053: * Returns a Lucene {@link Searcher}.
054: */
055: public Searcher getSearcher() {
056: return this .searcher;
057: }
058:
059: /**
060: * Returns a Lucene {@link IndexReader}.
061: */
062: public IndexReader getReader() throws SearchEngineException {
063: if (reader != null) {
064: return this .reader;
065: }
066:
067: Searchable[] searchables = searcher.getSearchables();
068: IndexReader[] readers = new IndexReader[searchables.length];
069: for (int i = 0; i < searchables.length; i++) {
070: readers[i] = ((IndexSearcher) searchables[i])
071: .getIndexReader();
072: }
073: reader = new MultiReader(readers, false);
074: return this .reader;
075: }
076:
077: /**
078: * Closes this instance of Lucene search "internals". This is an optional operation
079: * since Compass will take care of closing it when commit/rollback is called on the
080: * transaction.
081: */
082: public void close() throws SearchEngineException {
083: if (closed) {
084: return;
085: }
086: closed = true;
087:
088: if (reader != null) {
089: // close the multi reader so we dec ref its count
090: try {
091: reader.close();
092: } catch (IOException e) {
093: // ignore
094: }
095: }
096:
097: if (indexHoldersToClose != null) {
098: for (LuceneIndexHolder indexHolder : indexHoldersToClose) {
099: indexHolder.release();
100: }
101: }
102: }
103:
104: }
|