码迷,mamicode.com
首页 > 其他好文 > 详细

Cache Lucene IndexReader with Apache Commons Pool

时间:2014-06-11 23:34:37      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:des   Lucene   class   code   java   tar   

  • 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,布布扣,bubuko.com

Cache Lucene IndexReader with Apache Commons Pool

标签:des   Lucene   class   code   java   tar   

原文地址:http://www.cnblogs.com/xsi640/p/3772301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!