WEB开发网
开发学院软件开发Java 深入浅出 jackrabbit 八 索引合并(上) 阅读

深入浅出 jackrabbit 八 索引合并(上)

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示: 7 关闭这些 readers 8 根据名字删除已经被合并的 PersistentIndex 的索引文件和目录等, 我们再来看看代码,深入浅出 jackrabbit 八 索引合并(上)(5),代码中已经加入了 ahuaxuan 的注释: Java代码publicvoidrun(){for(;;){

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"); 
  } 

上一页  1 2 3 4 5 6 7  下一页

Tags:深入浅出 jackrabbit 索引

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接