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();
}
}
Cache Lucene IndexReader with Apache Commons Pool的更多相关文章
- JedisCluster中应用的Apache Commons Pool对象池技术
对象池技术在服务器开发上应用广泛.在各种对象池的实现中,尤其以数据库的连接池最为明显,可以说是每个服务器必须实现的部分. apache common pool 官方文档可以参考:https://c ...
- Tomcat 开发web项目报Illegal access: this web application instance has been stopped already. Could not load [org.apache.commons.pool.impl.CursorableLinkedList$Cursor]. 错误
开发Java web项目,在tomcat运行后报如下错误: Illegal access: this web application instance has been stopped already ...
- NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool/impl ...
- Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
错误如下: -- ::,-[TS] INFO http-- org.springframework.beans.factory.support.DefaultListableBeanFactory - ...
- Apache Commons Pool 故事一则
Apache Commons Pool 故事一则 最近工作中遇到一个由于对commons-pool的使用不当而引发的问题,习得正确的使用姿势后,写下这个简单的故事,帮助理解Apache Commons ...
- 池化 - Apache Commons Pool
对于那些创建耗时较长,或者资源占用较多的对象,比如网络连接,线程之类的资源,通常使用池化来管理这些对象,从而达到提高性能的目的.比如数据库连接池(c3p0, dbcp), java的线程池 Execu ...
- org/apache/commons/pool/impl/GenericObjectPool异常的解决办法
org/apache/commons/pool/impl/GenericObjectPool异常的解决办法 webwork+spring+hibernate框架的集成, 一启动Tomcat服务器就出了 ...
- 对象池化技术 org.apache.commons.pool
恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.Jakarta Commons Pool组件提供了一整套用于实现对象池化的框架,以及若干种各具特色的对象池实现,可以 ...
- Java_异常_03_ java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory
异常信息: java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory 原因: 我用的是commons ...
随机推荐
- Ubuntu 安装lrzsz工具
rz(上传) sz(下载)) 1. 安装 sudo apt-get install lrzsz 2. rz可覆盖原文件 rz -y
- Angular 4 表单
一. 模板表单 1. 如下图 2. code 3. 效果图 二.响应式表单 1. 增加ReactiveFormsModule 2.响应式表单用到的类和指令 3. 控制器代码 4. html <! ...
- innotop监控mysql
InnoTop 是一个系统活动报告,类似于Linux性能工具,它与Linux的top命令相仿,并参考mytop工具而设计. 它专门用后监控InnoDB性能和MySQL服务器.主要用于监控事务,死锁,外 ...
- MySQL数据库函数
一:字符串函数: 1.concat(); concat(S1,S2,S3,......Sn); 把传入参数链接 成一个字符串; 2.insert(); insert(str,x,y,insert); ...
- Winfrom 实现转圈等待
1.放弃进度条.动态进度图片等方式实现用户体验优化方式(主要是优化用户等待体验),建议使用方式? 答:对于From或者Control而言,其提供了Cursor属性设置即可. 例如: this.Curs ...
- Spring AOP的注解方式实现
spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...
- js中,object可以调用style对象,[]不可以调用style对象
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" Content=&q ...
- Jmeter接口压测
对于各个组件的使用,建议参考官方文档 1. Jmeter参数化,从txt文件读取. 1.txt
- 1058 A+B in Hogwarts (20 分)
1058 A+B in Hogwarts (20 分) If you are a fan of Harry Potter, you would know the world of magic has ...
- [datatable]关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
-- :09关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在实际编程工程中,常常遇到这样的情况:DataTable并不 ...