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.manager;
018:
019: import java.io.IOException;
020:
021: import org.apache.lucene.index.IndexReader;
022: import org.apache.lucene.search.IndexSearcher;
023: import org.apache.lucene.store.Directory;
024: import org.compass.core.util.FieldInvoker;
025:
026: /**
027: * @author kimchy
028: */
029: public class LuceneIndexHolder {
030:
031: private static FieldInvoker fieldInvoker;
032:
033: static {
034: try {
035: fieldInvoker = new FieldInvoker(IndexReader.class,
036: "refCount").prepare();
037: } catch (NoSuchFieldException e) {
038: e.printStackTrace();
039: }
040: }
041:
042: private long lastCacheInvalidation = System.currentTimeMillis();
043:
044: private IndexSearcher indexSearcher;
045:
046: private IndexReader indexReader;
047:
048: private int count = 0;
049:
050: private boolean markForClose = false;
051:
052: private String subIndex;
053:
054: public LuceneIndexHolder(String subIndex, Directory dir)
055: throws IOException {
056: this .indexReader = IndexReader.open(dir);
057: this .indexSearcher = new IndexSearcher(indexReader);
058: this .subIndex = subIndex;
059: }
060:
061: public LuceneIndexHolder(String subIndex,
062: IndexSearcher indexSearcher) {
063: this .subIndex = subIndex;
064: this .indexSearcher = indexSearcher;
065: this .indexReader = indexSearcher.getIndexReader();
066: }
067:
068: public void refresh(IndexReader indexReader) {
069: try {
070: indexSearcher.close();
071: } catch (IOException e) {
072: // do nothing
073: }
074: this .indexReader = indexReader;
075: indexSearcher = new IndexSearcher(indexReader);
076: }
077:
078: public IndexSearcher getIndexSearcher() {
079: return indexSearcher;
080: }
081:
082: public IndexReader getIndexReader() {
083: return indexReader;
084: }
085:
086: public String getSubIndex() {
087: return this .subIndex;
088: }
089:
090: public synchronized void acquire() {
091: count++;
092: }
093:
094: public synchronized void release() {
095: count--;
096: checkIfCanClose();
097: }
098:
099: public synchronized void markForClose() {
100: markForClose = true;
101: checkIfCanClose();
102: }
103:
104: private void checkIfCanClose() {
105: if (markForClose && count <= 0) {
106: try {
107: indexSearcher.close();
108: } catch (Exception e) {
109: // do nothing
110: }
111: try {
112: indexReader.close();
113: } catch (Exception e) {
114: // do nothing
115: }
116: }
117: }
118:
119: public long getLastCacheInvalidation() {
120: return lastCacheInvalidation;
121: }
122:
123: public void setLastCacheInvalidation(long lastCacheInvalidation) {
124: this.lastCacheInvalidation = lastCacheInvalidation;
125: }
126: }
|