深入浅出 jackrabbit 八 索引合并(上)
2009-09-17 00:00:00 来源:WEB开发网7 关闭这些 readers
8 根据名字删除已经被合并的 PersistentIndex 的索引文件和目录等。
我们再来看看代码,代码中已经加入了 ahuaxuan 的注释:
Java代码
public void run() {
for (;;) {
boolean isIdle = false;
//队列长度为0,表示消费者处于空闲状态,那么会进入wait状态
if (mergeTasks.size() == 0) {
mergerIdle.release();
isIdle = true;
}
/*2判断队列中是否有退出命令
*/
Merge task = (Merge) mergeTasks.remove();
if (task == QUIT) {
mergerIdle.release();
break;
}
if (isIdle) {
try {
mergerIdle.acquire();
} catch (InterruptedException e) {
Thread.interrupted();
log.warn("Unable to acquire mergerIdle sync");
}
}
log.debug("accepted merge request");
// reset deleted documents
deletedDocuments.clear();
// get readers
/*4 根据persistentindex的名字取到所有的persistentindex
的IndexReader对象
*/
String[] names = new String[task.indexes.length];
for (int i = 0; i < task.indexes.length; i++) {
names[i] = task.indexes[i].name;
}
try {
log.debug("create new index");
/*再创建一个新的PersistentIndex,原来的index文件合并到这个新的目录中
*/
PersistentIndex index = multiIndex.getOrCreateIndex(null);
boolean success = false;
try {
log.debug("get index readers from MultiIndex");
IndexReader[] readers = multiIndex.getIndexReaders(names, this);
try {
// do the merge
long time = System.currentTimeMillis();
/*6 将前面的IndexReader对象添加到PersistentIndex的indexwriter方法中,并执行optimize。
*/
index.addIndexes(readers);
time = System.currentTimeMillis() - time;
int docCount = 0;
for (int i = 0; i < readers.length; i++) {
docCount += readers[i].numDocs();
}
log.info("merged " + docCount + " documents in " + time + " ms into " + index.getName() + ".");
} finally {
for (int i = 0; i < readers.length; i++) {
/*7 关闭这些readers
*/
try {
readers[i].close();
} catch (IOException e) {
log.warn("Unable to close IndexReader: " + e);
}
}
}
// inform multi index
// if we cannot get the sync immediately we have to quit
if (!indexReplacement.attempt(0)) {
log.debug("index merging canceled");
break;
}
try {
log.debug("replace indexes");
multiIndex.replaceIndexes(names, index, deletedDocuments);
} finally {
indexReplacement.release();
}
success = true;
} finally {
if (!success) {
// delete index
log.debug("deleting index " + index.getName());
/*8 根据名字删除已经被合并的PersistentIndex的索引文件和目录等。
*/
multiIndex.deleteIndex(index);
}
}
} catch (Throwable e) {
log.error("Error while merging indexes: " + e);
}
}
log.info("IndexMerger terminated");
}
Tags:深入浅出 jackrabbit 索引
编辑录入:爽爽 [复制链接] [打 印]- ››深入浅出实战攻防恶意PDF文档
- ››深入浅出ShellExecute
- ››深入浅出URL编码
- ››深入浅出Java多线程程序设计
- ››深入浅出 jackrabbit 八 索引合并(上)
- ››深入浅出 jackrabbit 九 索引合并(下)
- ››深入浅出 jackrabbit 十 redolog 和 recovery.doc...
- ››深入浅出 jackrabbit 十一 jackrabbit改进要点
- ››深入浅出 jackrabbit 十二 key-value存储系统
- ››深入浅出 jackrabbit 十三 查询之AST和QT
- ››深入浅出 jackrabbit 一
- ››深入浅出 jackrabbit 二 索引概览
更多精彩
赞助商链接