• 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. Navicat导出opencart2.3数据字典

    步骤请参考:http://blog.csdn.net/maquealone/article/details/60764420 运行SQL:   备注:opcml是数据库名称. select TABLE ...

  2. Microsoft Dynamics CRM 2011 面向Internet部署 (IFD) CRM 登录出现会话超时的解决办法

    一.IFD 登录的时候,过了一段时间,会马上出现“您的会话已过期”,怎么解决这个问题呢,可以通过改变这个时间.具体图如二 Link to Dynamics CRM Wiki Home Page 二.S ...

  3. OpenWrt的web服务器

    参考: http://www.szchehang.com/news/10602.html 我们登录的路由器主界面就是通过这个软件指定了80端口来访问的.我们要添加自己额外的网站服务,那只需要重新定义一 ...

  4. thinkphp 3.2.3在nginx+php下的url重写配置经验

    环境:centos7.2+lnmp1.3(nginx+php7.0+mysql5.5) 进入服务器配置路径:cd /usr/local/nginx/conf/nginx.conf 修改nginx.co ...

  5. Qt常用类及类方法简介之 QAction类

    1.QAction::QAction ( const QString & text, QObject * parent )    QAction类的构造函数之一,利用text,parent创建 ...

  6. ALGO-3_蓝桥杯_算法训练_K好数(DP)

    问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = ,L = 2的时候,所有K好数为11...... 共7个 ...

  7. selenium APi

    1.查看浏览器的名字方法:name实例:drvier.name 2.删除浏览器所以的cookies方法:delete_all_cookies()实例:driver.delete_all_cookies ...

  8. PHP简单查询界面

    <html> <style type='text/css'> table {border-collapse:collapse;} td {border:solid 1px #d ...

  9. 一次JVM内存调整

    单台服务器8G内存,2核 系统里装了redis, rocketmq, mysql, zookeeper, 还有20个左右的微服务,每个微服务的jvm 参数 -Xms128m -Xmx256m -Xmn ...

  10. [UE4]把工程升级到最新版本

    右键UE4工程文件,选择“Switch Unreal Engine version...” 确定后,再次双击打开工程升级到最新版本了.