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.compass.core.lucene.engine.optimizer;
018:
019: import java.io.IOException;
020:
021: import org.apache.lucene.index.IndexReader;
022: import org.apache.lucene.index.IndexWriter;
023: import org.apache.lucene.store.Directory;
024: import org.apache.lucene.store.LockObtainFailedException;
025: import org.compass.core.CompassException;
026: import org.compass.core.config.CompassConfigurable;
027: import org.compass.core.config.CompassSettings;
028: import org.compass.core.engine.SearchEngineException;
029: import org.compass.core.lucene.LuceneEnvironment;
030: import org.compass.core.lucene.engine.manager.LuceneSearchEngineIndexManager;
031:
032: /**
033: * @author kimchy
034: */
035: public class AdaptiveOptimizer extends AbstractOptimizer implements
036: CompassConfigurable {
037:
038: private int mergeFactor;
039: private LuceneSearchEngineIndexManager indexManager;
040:
041: public void configure(CompassSettings settings)
042: throws CompassException {
043: mergeFactor = settings.getSettingAsInt(
044: LuceneEnvironment.Optimizer.Adaptive.MERGE_FACTOR, 10);
045: }
046:
047: public boolean canBeScheduled() {
048: return true;
049: }
050:
051: protected boolean isOptimizeOnlyIfIndexChanged() {
052: return false;
053: }
054:
055: protected void doOptimize(String subIndex)
056: throws SearchEngineException {
057: if (log.isDebugEnabled()) {
058: log.debug("Optimizing sub-index [" + subIndex + "]");
059: }
060: long time = System.currentTimeMillis();
061: indexManager = (LuceneSearchEngineIndexManager) getSearchEngineFactory()
062: .getIndexManager();
063: IndexWriter indexWriter;
064: try {
065: indexWriter = indexManager.openIndexWriter(indexManager
066: .getSettings().getSettings(), subIndex);
067: } catch (LockObtainFailedException e) {
068: log
069: .debug("Failed to obtain lock in order to optimizer, will try next time...");
070: return;
071: } catch (IOException e) {
072: throw new SearchEngineException(
073: "Failed to open index writer for optimization for sub index ["
074: + subIndex + "]", e);
075: }
076:
077: try {
078: indexWriter.optimize(mergeFactor);
079: } catch (Exception e) {
080: try {
081: Directory dir = indexManager.getStore().openDirectory(
082: subIndex);
083: if (IndexReader.isLocked(dir)) {
084: IndexReader.unlock(dir);
085: }
086: } catch (Exception e1) {
087: // do nothing
088: }
089: } finally {
090: try {
091: indexWriter.close();
092: } catch (Exception e) {
093: // ignore
094: }
095: }
096: long optimizeTime = System.currentTimeMillis() - time;
097:
098: if (log.isDebugEnabled()) {
099: log.debug("Optimization of sub-index [" + subIndex
100: + "] took [" + (optimizeTime) + "ms]");
101: }
102: }
103: }
|