• 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. Java初学几个程序背诵

    1.Hello world class first { public static void main(String[] args){ System.out.println("Hello w ...

  2. bzoj1876 SuperGCD

    Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比赛计算GCD.有一天Sheng bill很嚣张地找到了你,并要 ...

  3. 原创)CentOS6.4下安装xampp(一定要在linux上下载)

    一.xampp下载地址: http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/1.8.3/ 二.我下载的是xampp-linux-x64 ...

  4. Redis 密码设置和查看密码

    Redis 密码设置和查看密码 redis没有实现访问控制这个功能,但是它提供了一个轻量级的认证方式,可以编辑redis.conf配置来启用认证. 1.初始化Redis密码: 在配置文件中有个参数: ...

  5. [UE4]Visual Studio的相关插件安装:UE4.natvis和UnrealVS Extension

    转自:http://aigo.iteye.com/blog/2281182 UE4.natvis 官方文档: https://docs.unrealengine.com/latest/INT/Prog ...

  6. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  7. RNN总结

    RNN既可以表述为循环神 经网络(recurrent neural network),也可以表述为递归神经网络(recursive neural network),前者一般用于处理以时间序列为输入的问 ...

  8. js 下关于json的销毁和添加

    var json={a:1,b:2} 现在给json添加个c,可以这样写 json.c=3或json["c"]=3 我删除一个属性 delete json.a alert(json ...

  9. 【BZOJ】1101 [POI2007]Zap(莫比乌斯反演)

    题目 传送门:QWQ 分析 莫比乌斯反演. 还不是很熟练qwq 代码 //bzoj1101 //给出a,b,d,询问有多少对二元组(x,y)满足gcd(x,y)=d.x<=a,y<=b # ...

  10. pycharm下getpass.getpass()卡住

    pycharm下getpass.getpass()卡住不运行是什么问题 python pycharm 首先声明 下面这几行代码在命令行和eclipse下都能正常运行 import getpass pr ...