用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

Lucene框架IndexWriter、IndexReader单例

2013-05-09 作者: 神马举报

[java]代码库

package com.geostar.poi.support.lucene;
 
 import java.io.File;
 import java.io.IOException;
 
 import org.apache.log4j.Logger;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.index.CorruptIndexException;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.IndexWriter.MaxFieldLength;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.store.LockObtainFailedException;
 import org.apache.lucene.util.Version;
 
 import com.geostar.poi.support.POIConfig;
 
 public class POILuceneManager implements LuceneManager{
     private IndexWriter  indexWriter=null;
     private IndexReader  indexReader=null;
     //------lock 1
     private Object lock_w=new Object();
     //------lock 2
     private Object lock_r=new Object();
     
     private Logger logger=Logger.getLogger(POILuceneManager.class);
     @Override
     public IndexWriter getIndexWriter() throws CorruptIndexException, LockObtainFailedException, IOException {        
         synchronized(lock_w){
            if(indexWriter==null){
              System.out.println("创建对象");
              if(IndexWriter.isLocked(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)))){
                  IndexWriter.unlock(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)));
              };
              Analyzer any=new StandardAnalyzer(Version.LUCENE_CURRENT);
              indexWriter=new IndexWriter(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)),any,MaxFieldLength.UNLIMITED );
            };
            
        }
         return indexWriter;
     }
 
     @Override
     public IndexReader getIndexReader() throws CorruptIndexException, IOException {
       synchronized (lock_r) {
             if(indexReader==null){
                 indexReader=IndexReader.open(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)));
             };
         }
         return  indexReader;
     }
 
     @Override
     public void closeIndexWriter() throws IOException {
         // TODO Auto-generated method stub
 //synchronized (lock_w) {
             if(this.indexWriter!=null){
                 this.indexWriter.close();
             };
         //}
         
     }
 
     @Override
     public void closeIndexReader() throws IOException {
         // TODO Auto-generated method stub
 //synchronized (lock_r) {
             if(this.indexReader!=null){
                 this.indexReader.close();
             };
         //}
         
         
     }
 
     @Override
     public void closeAll() throws IOException {
         // TODO Auto-generated method stub
         this.closeIndexReader();
         this.closeIndexWriter();
         
     }
 
 }


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...