001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.lucene.index;
018:
019: import java.io.FileNotFoundException;
020: import java.io.IOException;
021: import java.util.ArrayList;
022:
023: import org.apache.lucene.store.Directory;
024: import org.compass.core.CompassException;
025: import org.compass.core.CompassSession;
026: import org.compass.core.engine.SearchEngineException;
027: import org.compass.core.lucene.engine.LuceneSearchEngine;
028: import org.compass.core.lucene.engine.manager.LuceneSearchEngineIndexManager;
029: import org.compass.core.lucene.engine.store.LuceneSearchEngineStore;
030: import org.compass.core.spi.InternalCompassSession;
031: import org.compass.core.transaction.InternalCompassTransaction;
032: import org.compass.core.transaction.context.TransactionContextCallback;
033:
034: /**
035: * Provides information about the segments within a Lucene index and about the
036: * lucene index itself.
037: * <p/>
038: * Can not be instantiated directly, but has a factory method (<code>getSegmentsInfos</code>)
039: * which returns the index info for the given index path.
040: * </p>
041: *
042: * @author kimchy
043: */
044: public class LuceneSubIndexInfo {
045:
046: /**
047: * A Lucene single segment information
048: */
049: public static class LuceneSegmentInfo {
050: private String name;
051:
052: private int docCount;
053:
054: public LuceneSegmentInfo(String name, int docCount) {
055: this .name = name;
056: this .docCount = docCount;
057: }
058:
059: /**
060: * Returns the name of the segment
061: */
062: public String name() {
063: return this .name;
064: }
065:
066: /**
067: * Returns the number of documents within the segment
068: */
069: public int docCount() {
070: return docCount;
071: }
072: }
073:
074: private ArrayList segmentInfos;
075:
076: private long version;
077:
078: private String subIndex;
079:
080: protected LuceneSubIndexInfo(String subIndex, long version,
081: ArrayList segmentInfos) {
082: this .subIndex = subIndex;
083: this .version = version;
084: this .segmentInfos = segmentInfos;
085: }
086:
087: /**
088: * Returns the version of the index.
089: *
090: * @return The version of the index
091: */
092: public long version() {
093: return this .version;
094: }
095:
096: /**
097: * Retruns the number of segments.
098: *
099: * @return The number of segments
100: */
101: public int size() {
102: return segmentInfos.size();
103: }
104:
105: /**
106: * The segment info that maps to the given index.
107: *
108: * @param segmentIndex The segment index
109: * @return The segment info structure
110: */
111: public LuceneSegmentInfo info(int segmentIndex) {
112: return (LuceneSegmentInfo) segmentInfos.get(segmentIndex);
113: }
114:
115: /**
116: * The index parh of the given index.
117: *
118: * @return The index path
119: */
120: public String getSubIndex() {
121: return this .subIndex;
122: }
123:
124: /**
125: * Returns low level Lucene index sub index information. Note, this method
126: * can be called outside of a transactional context.
127: *
128: * @param subIndex The sub index to get the info for
129: * @param session The compass session that will be used for transactional support
130: * @return The sub index info
131: * @throws IOException Failed to read the segments from the directory
132: */
133: public static LuceneSubIndexInfo getIndexInfo(
134: final String subIndex, final CompassSession session)
135: throws IOException {
136: LuceneSearchEngine searchEngine = (LuceneSearchEngine) ((InternalCompassSession) session)
137: .getSearchEngine();
138: final LuceneSearchEngineIndexManager indexManager = (LuceneSearchEngineIndexManager) searchEngine
139: .getSearchEngineFactory().getIndexManager();
140:
141: return searchEngine
142: .getSearchEngineFactory()
143: .getTransactionContext()
144: .execute(
145: new TransactionContextCallback<LuceneSubIndexInfo>() {
146: public LuceneSubIndexInfo doInTransaction(
147: InternalCompassTransaction tr)
148: throws CompassException {
149: try {
150: return getIndexInfo(subIndex,
151: indexManager);
152: } catch (IOException e) {
153: throw new SearchEngineException(
154: "Failed to open index info for sub index ["
155: + subIndex + "]", e);
156: }
157: }
158: });
159: }
160:
161: public static LuceneSubIndexInfo getIndexInfo(String subIndex,
162: LuceneSearchEngineIndexManager indexManager)
163: throws IOException {
164: return getIndexInfo(subIndex, indexManager.getStore());
165: }
166:
167: /**
168: * Returns low level Lucene index sub index information. Note, this method must
169: * be called within a transactional context.
170: */
171: public static LuceneSubIndexInfo getIndexInfo(String subIndex,
172: LuceneSearchEngineStore store) throws IOException {
173: final Directory directory = store.openDirectory(subIndex);
174: try {
175: final SegmentInfos segmentInfos = new SegmentInfos();
176: segmentInfos.read(directory);
177:
178: ArrayList segmentInfosList = new ArrayList();
179: for (int i = 0; i < segmentInfos.size(); i++) {
180: SegmentInfo segmentInfo = segmentInfos.info(i);
181: LuceneSegmentInfo luceneSegmentInfo = new LuceneSegmentInfo(
182: segmentInfo.name, segmentInfo.docCount);
183: segmentInfosList.add(luceneSegmentInfo);
184: }
185: return new LuceneSubIndexInfo(subIndex, segmentInfos
186: .getVersion(), segmentInfosList);
187: } catch (FileNotFoundException e) {
188: // the segments file was not found, return null.
189: return null;
190: }
191: }
192: }
|