LucenePool

package com.yulong.lucene.core;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class LucenePool extends Thread {

/**
* 是否执行线程方法
*/
private boolean isrun = true;

/**
* 索引锁定池
*
* key 索引路径
* value Map<线程名词,线程状态>
*
*/
private Map<String,Map> pool = new HashMap<String,Map>();

/**
* 索引对象缓存池
*/
private Map<String,IndexWriter> index = new HashMap<String, IndexWriter>();

/**
* 得到索引读写器
*
* @param key 索引路径
* @param value Map<线程名词,线程状态>
* @return
*/
@SuppressWarnings("unchecked")
public IndexWriter get(String key,String threadname){
// isrun = false;
IndexWriter iw = null;
try {
iw = getTrueIndexWriter(key);
// if(pool.containsKey(key)){
// Map map = pool.get(key);
// map.put(threadname, "true");
// iw = index.get(key);
// }else{//如果不存在索引读写器
// Map map = new HashMap();
// iw = getTrueIndexWriter(key);
// map.put(threadname, "true");
// pool.put(key, map);
// index.put(key, iw);
// }
} catch (Exception e) {
e.printStackTrace();
}
// isrun = true;
return iw;
}

/**
* 通知线程处理完毕
*
* @param key
* @param threadname
*/
@SuppressWarnings("unchecked")
public void over(String key,String threadname){
Map map = pool.get(key);
map.put(threadname, "false");
}

/**
* 得到可写的临时文件
*
* @return
* @throws IOException
* @throws CorruptIndexException
*/
private IndexWriter getTrueIndexWriter(String temppath) throws Exception{
IndexWriter iwriter = null;
File file = new File(temppath);
if(!file.exists()){// 如果文件不存在就创建
file.mkdirs();
}
//检查索引文件的文档熟路是否超过大小
Directory directory = FSDirectory.open(new File(temppath));
try{
iwriter = new IndexWriter(directory, LuceneContacts.analyzer, false, new IndexWriter.MaxFieldLength(2500000));
}catch (Exception e) {
iwriter = new IndexWriter(directory, LuceneContacts.analyzer, true, new IndexWriter.MaxFieldLength(2500000));
}
return iwriter;
}

/**
* 处理检测是否存在可释放的索引器
*/
@SuppressWarnings("static-access")
public void run(){
while(true){
if(isrun){//清理不可用的读写器
String str = "";
for(Iterator<String> it = pool.keySet().iterator();it.hasNext();){
String key = it.next();
Map map = pool.get(key);
if(!map.containsValue("true")){
IndexWriter iw = index.get(key);
index.remove(key);
if(iw!=null){
if(str!="")
str += ",";
str += key;
try {try {iw.close();} catch (Exception e) {}
}finally{if(iw!=null){try {iw.close();} catch (Exception e) {}}}
}
}
}
if(str!=""){
String temp[] = str.split(",");
for (int i = 0; i < temp.length; i++) {
System.out.println("*********************************************************");
System.out.println("清理:"+temp[i]);
System.out.println("*********************************************************");
pool.remove(temp[i]);
}
}
}
try {
this.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值