WEB开发网
开发学院软件开发Java 深入浅出 jackrabbit 四 索引提交(上) 阅读

深入浅出 jackrabbit 四 索引提交(上)

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示: Java代码voidaddDocuments(Document[]docs)throwsIOException{finalIndexWriterwriter=getIndexWriter();//一般情况下,我们的docs数组的长度为10,深入浅出 jackrabbit 四 索引提交(上)(6)

Java代码   

void addDocuments(Document[] docs) throws IOException { 
    final IndexWriter writer = getIndexWriter(); 
//一般情况下,我们的docs数组的长度为10,下面就创建10个线程 
    DynamicPooledExecutor.Command commands[] = 
        new DynamicPooledExecutor.Command[docs.length]; 
 
    for (int i = 0; i < docs.length; i++) { 
      // check if text extractor completed its work 
      final Document doc = getFinishedDocument(docs[i]); 
      // create a command for inverting the document 
      commands[i] = new DynamicPooledExecutor.Command() { 
        public Object call() throws Exception { 
          long time = System.currentTimeMillis(); 
//每个线程都使用往writer里加入document对象,这个时候,lucene开始解析document,并生产index数据 
          writer.addDocument(doc); 
          return new Long(System.currentTimeMillis() - time); 
        } 
      }; 
    } 
//并发执行 
    DynamicPooledExecutor.Result results[] = EXECUTOR.executeAndWait(commands); 
//置空readOnlyReader和sharedReader,为啥置空啊,index数据改了呗 
    invalidateSharedReader(); 
    IOException ex = null; 
 
//检查每个线程的执行情况,有一个出错就抛出异常,其他的异常保存到log中 
    for (int i = 0; i < results.length; i++) { 
      if (results[i].getException() != null) { 
        Throwable cause = results[i].getException().getCause(); 
        if (ex == null) { 
          // only throw the first exception 
          if (cause instanceof IOException) { 
            ex = (IOException) cause; 
          } else { 
            IOException e = new IOException(); 
            e.initCause(cause); 
            ex = e; 
          } 
        } else { 
          // all others are logged 
          log.warn("Exception while inverting document", cause); 
        } 
      } else { 
        log.debug("Inverted document in {} ms", results[i].get()); 
      } 
    } 
    if (ex != null) { 
      throw ex; 
    } 
  }

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

Tags:深入浅出 jackrabbit 索引

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