前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >es|索引与段

es|索引与段

作者头像
heidsoft
发布2019-06-15 21:23:27
1K0
发布2019-06-15 21:23:27
举报

/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();
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云数智圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档