lucene源码-IndexCommit

来源:互联网 发布:淘宝网b2c 编辑:程序博客网 时间:2024/06/05 03:58
Expert: represents a single commit into an index as seen by the IndexDeletionPolicy or IndexReader.

Changes to the content of an index are made visible only after the writer who made that change commits by writing a new segments file (segments_N). This point in time, when the action of writing of a new segments file to the directory is completed, is an index commit.

Each index commit point has a unique segments file associated with it. The segments file associated with a later index commit point would have a larger N.


代表了一次索引提交的信息, 包括本次提交的_N和_N文件名, 本次提交后的所有索引文件, 本次提交后总的segment个数. 在获取directoryreader对象时, 如果该对象参数是null, 则查找索引文件夹下的最大_N来获取segmeninfos, 如果对象不为null则直接根据indexcommit对象中的_N信息来获取segmeninfos信息

public abstract class IndexCommit implements Comparable<IndexCommit> {  /**   * Get the segments file (<code>segments_N</code>) associated    * with this commit point.   */  public abstract String getSegmentsFileName();  /**   * Returns all index files referenced by this commit point.   */  public abstract Collection<String> getFileNames() throws IOException;  /**   * Returns the {@link Directory} for the index.   */  public abstract Directory getDirectory();    /**   * Delete this commit point.  This only applies when using   * the commit point in the context of IndexWriter's   * IndexDeletionPolicy.   * <p>   * Upon calling this, the writer is notified that this commit    * point should be deleted.    * <p>   * Decision that a commit-point should be deleted is taken by the {@link IndexDeletionPolicy} in effect   * and therefore this should only be called by its {@link IndexDeletionPolicy#onInit onInit()} or    * {@link IndexDeletionPolicy#onCommit onCommit()} methods.  */  public abstract void delete();  /** Returns true if this commit should be deleted; this is   *  only used by {@link IndexWriter} after invoking the   *  {@link IndexDeletionPolicy}. */  public abstract boolean isDeleted();  /** Returns number of segments referenced by this commit. */  public abstract int getSegmentCount();  /** Sole constructor. (For invocation by subclass    *  constructors, typically implicit.) */  protected IndexCommit() {  }  /** Two IndexCommits are equal if both their Directory and versions are equal. */  @Override  public boolean equals(Object other) {    if (other instanceof IndexCommit) {      IndexCommit otherCommit = (IndexCommit) other;      return otherCommit.getDirectory() == getDirectory() && otherCommit.getGeneration() == getGeneration();    } else {      return false;    }  }  @Override  public int hashCode() {    return getDirectory().hashCode() + Long.valueOf(getGeneration()).hashCode();  }  /** Returns the generation (the _N in segments_N) for this   *  IndexCommit */  public abstract long getGeneration();  /** Returns userData, previously passed to {@link   *  IndexWriter#setCommitData(Map)} for this commit.  Map is   *  {@code String -> String}. */  public abstract Map<String,String> getUserData() throws IOException;    @Override  public int compareTo(IndexCommit commit) {    if (getDirectory() != commit.getDirectory()) {      throw new UnsupportedOperationException("cannot compare IndexCommits from different Directory instances");    }    long gen = getGeneration();    long comgen = commit.getGeneration();    return Long.compare(gen, comgen);  }}