• 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. python类的继承的两种方式

    class Animal(object): """docstring for Animal""" def __init__(self, na ...

  2. tyvj1659中中救援队

    题目:http://www.joyoi.cn/problem/tyvj-1659 发现每条边要走两次,每个点要走它连接的边数次. 所以把边的权值赋成 本身的值+两个端点的点权,求最小生成树即可. !边 ...

  3. 最新hadoop虚拟机安装教程(附带图文)

    前两天看到有人留言问在什么情况下需要部署hadoop,我给的回答也很简单,就是在需要处理海量数据的时候才需要考虑部署hadoop.关于这个问题在很早之前的一篇分享文档也有说到这个问题,数据量少的完全发 ...

  4. MySQL集群Percona XtraDB Cluster安装搭建步骤详解

    http://www.linuxidc.com/Linux/2017-05/143501.htm http://blog.csdn.net/thundermeng/article/details/52 ...

  5. 2013-8-6 ubuntu基本操作

    1,apt-get下载文件默认安装路径 apt-get 下载后,软件所在路径是什么?? /var/cache/apt/archives ubuntu 默认的PATH为 PATH=/home/brigh ...

  6. python3 钉钉群机器人 webhook

    import requests import json url='https://oapi.dingtalk.com/robot/send?access_token=替换成你自己的toten' pro ...

  7. [Java基础] Java float保留两位小数或多位小数

    方法1:用Math.round计算,这里返回的数字格式的. float price=89.89; int itemNum=3; float totalPrice=price*itemNum; floa ...

  8. [UE4]引擎自身提供的无锁队列等无锁容器(TLockFreePointerList)

    常用的接口: TLockFreePointerListFIFO<T>:先进先出: TLockFreePointerListLIFO<T>:后进先出: TLockFreePoin ...

  9. javascript的防篡改对象之preventExtensions()方法

    js在默认情况下,所有的对象都是可扩展的.这也是让很多开发人员头特疼的问题.因为在同一环境中,一不小心就会发生修改了不必要的对象,而自己却不知道. 在ECMAScript5可以解决这种问题了. pre ...

  10. JS 传各种文件到后端

    由于要写一个前端上传文件按钮功能,本人前端是小白,所以在网上搜索了许多,发现FileReader非常好用. 不多BB,直接来. 1,前端只需要一个input标签, <input type=&qu ...