• IndexReaderFactory.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package 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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    import 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的更多相关文章

  1. JedisCluster中应用的Apache Commons Pool对象池技术

    对象池技术在服务器开发上应用广泛.在各种对象池的实现中,尤其以数据库的连接池最为明显,可以说是每个服务器必须实现的部分.   apache common pool 官方文档可以参考:https://c ...

  2. 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 ...

  3. NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

    错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool/impl ...

  4. Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool

    错误如下: -- ::,-[TS] INFO http-- org.springframework.beans.factory.support.DefaultListableBeanFactory - ...

  5. Apache Commons Pool 故事一则

    Apache Commons Pool 故事一则 最近工作中遇到一个由于对commons-pool的使用不当而引发的问题,习得正确的使用姿势后,写下这个简单的故事,帮助理解Apache Commons ...

  6. 池化 - Apache Commons Pool

    对于那些创建耗时较长,或者资源占用较多的对象,比如网络连接,线程之类的资源,通常使用池化来管理这些对象,从而达到提高性能的目的.比如数据库连接池(c3p0, dbcp), java的线程池 Execu ...

  7. org/apache/commons/pool/impl/GenericObjectPool异常的解决办法

    org/apache/commons/pool/impl/GenericObjectPool异常的解决办法 webwork+spring+hibernate框架的集成, 一启动Tomcat服务器就出了 ...

  8. 对象池化技术 org.apache.commons.pool

    恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.Jakarta Commons Pool组件提供了一整套用于实现对象池化的框架,以及若干种各具特色的对象池实现,可以 ...

  9. Java_异常_03_ java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory

    异常信息: java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory 原因: 我用的是commons ...

随机推荐

  1. CDN、浏览器缓存

    CDN是什么? 谈到CDN的作用,可以用8年买火车票的经历来形象比喻: 8年前,还没有火车票代售点一说,12306.cn更是无从说起.那时候火车票还只能在火车站的售票大厅购买,而我所住的小县城并不通火 ...

  2. 二分法查找 (Binary Search)

    二分法查找适用于排列有序的数据.java实现方法如下: // Find the location of a value in array a // Array a must be sorted // ...

  3. 【shell】awk命令

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  4. protobuf 协议 windows 下 java 环境搭建

    使用maven编译protobuf所需要的jar包 1. 安装配置maven (1)下载maven        http://maven.apache.org/    版本:apache-maven ...

  5. BASIC-22_蓝桥杯_FJ的字符串

    示例代码: #include <stdio.h>#define N 1000000 int main(void){ int n = 0 , i = 0; char arr[N] , tmp ...

  6. C/C++基础----泛型算法

    算法不依赖与容器(使用迭代器),但大多数依赖于元素类型.如find需要==运算符,其他算法可能要求支持<运算符. 算法永远不会执行容器的操作,永远不会改变底层容器的大小(添加或删除元素). ac ...

  7. java反射例子

    一.field.setAccessible()方法: setAccessible方法是干什么用的呢让我们来看一段代码吧: public class User { private String name ...

  8. post 中文数据到elasticsearch restful接口报json_parse_exception 问题

    我们的客户端程序直接调用es 的restful接口, 通过post json数据去查询, 但post数据有中文的时候,有些中文会报异常,有些中文不会 {"error":{" ...

  9. jenkins将构建成功或失败的信息发送给指定URL(eg: pomelo采用jenkins持续集成)

     先提供一个思路供大家参考,想将构建成功或者失败的信息发送给指定URL的话,可以这样:1.A构建后触发另一个构建B,构建B执行某个插件2.插件的功能:   (1)利用jenkins API获取构建A最 ...

  10. ORM( ORM查询13种方法3. 单表的双下划线的使用 4. 外键的方法 5. 多对多的方法 ,聚合,分组,F查询,Q查询,事务 )

    必知必会13条 <1> all(): 查询所有结果 <2> get(**kwargs): 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或 ...