es|索引与段

2019-06-15 21:23:27 浏览数 (1)

/Users/heidsoft/research/lucene-solr/lucene/luke/src/java/org/apache/lucene/luke/models/commits/Segment.java

代码语言:javascript复制
public final class Segment {

  private String name;

  private int maxDoc;

  private long delGen;

  private int delCount;

  private String luceneVer;

  private String codecName;

  private String displaySize;

  private boolean useCompoundFile;

  static Segment of(SegmentCommitInfo segInfo) {
    Segment segment = new Segment();
    segment.name = segInfo.info.name;
    segment.maxDoc = segInfo.info.maxDoc();
    segment.delGen = segInfo.getDelGen();
    segment.delCount = segInfo.getDelCount();
    segment.luceneVer = segInfo.info.getVersion().toString();
    segment.codecName = segInfo.info.getCodec().getName();
    try {
      segment.displaySize = CommitsImpl.toDisplaySize(segInfo.sizeInBytes());
    } catch (IOException e) {
    }
    segment.useCompoundFile = segInfo.info.getUseCompoundFile();
    return segment;
  }

  public String getName() {
    return name;
  }

  public int getMaxDoc() {
    return maxDoc;
  }

  public long getDelGen() {
    return delGen;
  }

  public int getDelCount() {
    return delCount;
  }

  public String getLuceneVer() {
    return luceneVer;
  }

  public String getCodecName() {
    return codecName;
  }

  public String getDisplaySize() {
    return displaySize;
  }

  public boolean isUseCompoundFile() {
    return useCompoundFile;
  }

  private Segment() {
  }
}

/Users/heidsoft/research/lucene-solr/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java

代码语言:javascript复制
public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException {
  enableTestPoints = isEnableTestPoints();
  conf.setIndexWriter(this); // prevent reuse by other instances
  config = conf;
  infoStream = config.getInfoStream();
  softDeletesEnabled = config.getSoftDeletesField() != null;
  // obtain the write.lock. If the user configured a timeout,
  // we wrap with a sleeper and this might take some time.
  writeLock = d.obtainLock(WRITE_LOCK_NAME);
  
  boolean success = false;
  try {
    directoryOrig = d;
    directory = new LockValidatingDirectoryWrapper(d, writeLock);

    analyzer = config.getAnalyzer();
    mergeScheduler = config.getMergeScheduler();
    mergeScheduler.setInfoStream(infoStream);
    codec = config.getCodec();
    OpenMode mode = config.getOpenMode();
    final boolean indexExists;
    final boolean create;
    if (mode == OpenMode.CREATE) {
      indexExists = DirectoryReader.indexExists(directory);
      create = true;
    } else if (mode == OpenMode.APPEND) {
      indexExists = true;
      create = false;
    } else {
      // CREATE_OR_APPEND - create only if an index does not exist
      indexExists = DirectoryReader.indexExists(directory);
      create = !indexExists;
    }

    // If index is too old, reading the segments will throw
    // IndexFormatTooOldException.

    String[] files = directory.listAll();

    // Set up our initial SegmentInfos:
    IndexCommit commit = config.getIndexCommit();

    // Set up our initial SegmentInfos:
    StandardDirectoryReader reader;
    if (commit == null) {
      reader = null;
    } else {
      reader = commit.getReader();
    }

    if (create) {

      if (config.getIndexCommit() != null) {
        // We cannot both open from a commit point and create:
        if (mode == OpenMode.CREATE) {
          throw new IllegalArgumentException("cannot use IndexWriterConfig.setIndexCommit() with OpenMode.CREATE");
        } else {
          throw new IllegalArgumentException("cannot use IndexWriterConfig.setIndexCommit() when index has no commit");
        }
      }

      // Try to read first.  This is to allow create
      // against an index that's currently open for
      // searching.  In this case we write the next
      // segments_N file with no segments:
      final SegmentInfos sis = new SegmentInfos(config.getIndexCreatedVersionMajor());
      if (indexExists) {
        final SegmentInfos previous = SegmentInfos.readLatestCommit(directory);
        sis.updateGenerationVersionAndCounter(previous);
      }
      segmentInfos = sis;
      rollbackSegments = segmentInfos.createBackupSegmentInfos();

      // Record that we have a change (zero out all
      // segments) pending:
      changed();

0 人点赞