Cache Lucene IndexReader with Apache Commons Pool
-
IndexReaderFactory.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556package
org.ostree.module.lucene;
import
org.apache.commons.pool.KeyedPoolableObjectFactory;
import
org.apache.lucene.index.IndexReader;
import
org.apache.lucene.store.FSDirectory;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
java.io.File;
import
java.util.NoSuchElementException;
public
class
IndexReaderFactory
implements
KeyedPoolableObjectFactory<String, IndexReader> {
private
String baseIndexDir=
"/var/data/ostree/index"
;
private
static
final
Logger logger = LoggerFactory
.getLogger(IndexReaderFactory.
class
);
public
IndexReaderFactory(String baseIndexDir) {
this
.baseIndexDir = baseIndexDir;
}
@Override
public
IndexReader makeObject(String key)
throws
Exception {
logger.info(
"open index: "
+ key);
File file=
new
File(baseIndexDir,key);
if
(!file.exists()){
throw
new
NoSuchElementException(key +
" index doesn't exist!"
);
}
FSDirectory dir =FSDirectory.open(file);
return
IndexReader.open(dir,
true
);
}
@Override
public
void
destroyObject(String key, IndexReader reader)
throws
Exception {
logger.info(
"destroy index: "
+ key);
reader.close();
}
@Override
public
boolean
validateObject(String key, IndexReader reader) {
logger.info(
"validate index: "
+ key);
if
(reader!=
null
){
return
true
;
}
return
false
;
}
@Override
public
void
activateObject(String key, IndexReader reader)
throws
Exception {
logger.debug(
"activate index: "
+ key);
}
@Override
public
void
passivateObject(String key, IndexReader reader)
throws
Exception {
logger.debug(
"passivate index: "
+ key);
}
}
-
Usage
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import
org.apache.commons.pool.KeyedObjectPool;
import
org.apache.commons.pool.impl.GenericKeyedObjectPool;
import
org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import
org.apache.lucene.document.Document;
import
org.apache.lucene.index.IndexReader;
import
org.apache.lucene.index.Term;
import
org.apache.lucene.search.IndexSearcher;
import
org.apache.lucene.search.NGramPhraseQuery;
import
org.apache.lucene.search.ScoreDoc;
import
org.apache.lucene.search.TopDocs;
public
class
LuceneSearcherPool {
public
static
void
main(String[] args) {
GenericKeyedObjectPool.Config config =
new
GenericKeyedObjectPool.Config();
GenericKeyedObjectPoolFactory<String, IndexReader> poolFactory =
new
GenericKeyedObjectPoolFactory<String, IndexReader>(
new
IndexReaderFactory(
"/var/data/ostree/index"
), config);
KeyedObjectPool<String, IndexReader> pool = poolFactory.createPool();
try
{
String[] dates = {
"2012-01-01"
,
"2011-01-04"
,
"2012-01-05"
};
for
(String date : dates) {
for
(
int
i =
0
; i <
10
; i++) {
long
start = System.currentTimeMillis();
IndexReader reader = pool.borrowObject(date);
test(reader);
pool.returnObject(date, reader);
System.out.println(date +
":"
+ i +
";"
+ (System.currentTimeMillis() - start));
}
}
pool.close();
}
catch
(Exception ex) {
}
}
public
static
void
test(IndexReader reader)
throws
Exception {
String input =
"java"
;
int
num =
5
;
IndexSearcher searcher =
new
IndexSearcher(reader);
//build your query here
//Query query =
TopDocs hits = searcher.search(query, num);
for
(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
// handle the Document
}
searcher.close();
}
}