static void inflateGens(SegmentInfos infos,Collection<String> files,InfoStream infoStream){
  long maxSegmentGen=Long.MIN_VALUE;
  int maxSegmentName=Integer.MIN_VALUE;
  Map<String,Long> maxPerSegmentGen=new HashMap<>();
  for (  String fileName : files) {
    if (fileName.equals(IndexFileNames.OLD_SEGMENTS_GEN) || fileName.equals(IndexWriter.WRITE_LOCK_NAME)) {
    }
 else     if (fileName.startsWith(IndexFileNames.SEGMENTS)) {
      try {
        maxSegmentGen=Math.max(SegmentInfos.generationFromSegmentsFileName(fileName),maxSegmentGen);
      }
 catch (      NumberFormatException ignore) {
      }
    }
 else     if (fileName.startsWith(IndexFileNames.PENDING_SEGMENTS)) {
      try {
        maxSegmentGen=Math.max(SegmentInfos.generationFromSegmentsFileName(fileName.substring(8)),maxSegmentGen);
      }
 catch (      NumberFormatException ignore) {
      }
    }
 else {
      String segmentName=IndexFileNames.parseSegmentName(fileName);
      assert segmentName.startsWith("_") : "wtf? file=" + fileName;
      if (fileName.toLowerCase(Locale.ROOT).endsWith(".tmp")) {
        continue;
      }
      maxSegmentName=Math.max(maxSegmentName,Integer.parseInt(segmentName.substring(1),Character.MAX_RADIX));
      Long curGen=maxPerSegmentGen.get(segmentName);
      if (curGen == null) {
        curGen=0L;
      }
      try {
        curGen=Math.max(curGen,IndexFileNames.parseGeneration(fileName));
      }
 catch (      NumberFormatException ignore) {
      }
      maxPerSegmentGen.put(segmentName,curGen);
    }
  }
  infos.setNextWriteGeneration(Math.max(infos.getGeneration(),maxSegmentGen));
  if (infos.counter < 1 + maxSegmentName) {
    if (infoStream.isEnabled("IFD")) {
      infoStream.message("IFD","init: inflate infos.counter to " + (1 + maxSegmentName) + " vs current="+ infos.counter);
    }
    infos.counter=1 + maxSegmentName;
  }
  for (  SegmentCommitInfo info : infos) {
    Long gen=maxPerSegmentGen.get(info.info.name);
    assert gen != null;
    long genLong=gen;
    if (info.getNextWriteDelGen() < genLong + 1) {
      if (infoStream.isEnabled("IFD")) {
        infoStream.message("IFD","init: seg=" + info.info.name + " set nextWriteDelGen="+ (genLong + 1)+ " vs current="+ info.getNextWriteDelGen());
      }
      info.setNextWriteDelGen(genLong + 1);
    }
    if (info.getNextWriteFieldInfosGen() < genLong + 1) {
      if (infoStream.isEnabled("IFD")) {
        infoStream.message("IFD","init: seg=" + info.info.name + " set nextWriteFieldInfosGen="+ (genLong + 1)+ " vs current="+ info.getNextWriteFieldInfosGen());
      }
      info.setNextWriteFieldInfosGen(genLong + 1);
    }
    if (info.getNextWriteDocValuesGen() < genLong + 1) {
      if (infoStream.isEnabled("IFD")) {
        infoStream.message("IFD","init: seg=" + info.info.name + " set nextWriteDocValuesGen="+ (genLong + 1)+ " vs current="+ info.getNextWriteDocValuesGen());
      }
      info.setNextWriteDocValuesGen(genLong + 1);
    }
  }
}

static void inflateGens ( SegmentInfos infos , Collection < String > files , InfoStream infoStream ) { long maxSegmentGen = Long . MIN_VALUE ; int maxSegmentName = Integer . MIN_VALUE ; Map < String , Long > maxPerSegmentGen = new HashMap < > ( ) ; for ( String fileName : files ) { if ( fileName . equals ( IndexFileNames . OLD_SEGMENTS_GEN ) || fileName . equals ( IndexWriter . WRITE_LOCK_NAME ) ) { } else if ( fileName . startsWith ( IndexFileNames . SEGMENTS ) ) { try { maxSegmentGen = Math . max ( SegmentInfos . generationFromSegmentsFileName ( fileName ) , maxSegmentGen ) ; } catch ( NumberFormatException ignore ) { } } else if ( fileName . startsWith ( IndexFileNames . PENDING_SEGMENTS ) ) { try { maxSegmentGen = Math . max ( SegmentInfos . generationFromSegmentsFileName ( fileName . substring ( 8 ) ) , maxSegmentGen ) ; } catch ( NumberFormatException ignore ) { } } else { String segmentName = IndexFileNames . parseSegmentName ( fileName ) ; assert segmentName . startsWith ( "_" ) : "wtf? file=" + fileName ; if ( fileName . toLowerCase ( Locale . ROOT ) . endsWith ( ".tmp" ) ) { continue ; } maxSegmentName = Math . max ( maxSegmentName , Integer . parseInt ( segmentName . substring ( 1 ) , Character . MAX_RADIX ) ) ; Long curGen = maxPerSegmentGen . get ( segmentName ) ; if ( curGen == null ) { curGen = 0L ; } try { curGen = Math . max ( curGen , IndexFileNames . parseGeneration ( fileName ) ) ; } catch ( NumberFormatException ignore ) { } maxPerSegmentGen . put ( segmentName , curGen ) ; } } infos . setNextWriteGeneration ( Math . max ( infos . getGeneration ( ) , maxSegmentGen ) ) ; if ( infos . counter < 1 + maxSegmentName ) { if ( infoStream . isEnabled ( "IFD" ) ) { infoStream . message ( "IFD" , "init: inflate infos.counter to " + ( 1 + maxSegmentName ) + " vs current=" + infos . counter ) ; } infos . counter = 1 + maxSegmentName ; } for ( SegmentCommitInfo info : infos ) { Long gen = maxPerSegmentGen . get ( info . info . name ) ; assert gen != null ; long genLong = gen ; if ( info . getNextWriteDelGen ( ) < genLong + 1 ) { if ( infoStream . isEnabled ( "IFD" ) ) { infoStream . message ( "IFD" , "init: seg=" + info . info . name + " set nextWriteDelGen=" + ( genLong + 1 ) + " vs current=" + info . getNextWriteDelGen ( ) ) ; } info . setNextWriteDelGen ( genLong + 1 ) ; } if ( info . getNextWriteFieldInfosGen ( ) < genLong + 1 ) { if ( infoStream . isEnabled ( "IFD" ) ) { infoStream . message ( "IFD" , "init: seg=" + info . info . name + " set nextWriteFieldInfosGen=" + ( genLong + 1 ) + " vs current=" + info . getNextWriteFieldInfosGen ( ) ) ; } info . setNextWriteFieldInfosGen ( genLong + 1 ) ; } if ( info . getNextWriteDocValuesGen ( ) < genLong + 1 ) { if ( infoStream . isEnabled ( "IFD" ) ) { infoStream . message ( "IFD" , "init: seg=" + info . info . name + " set nextWriteDocValuesGen=" + ( genLong + 1 ) + " vs current=" + info . getNextWriteDocValuesGen ( ) ) ; } info . setNextWriteDocValuesGen ( genLong + 1 ) ; } } }
set all gens beyond what we currently see in the directory , to avoid double - write in cases where the previous indexwriter did not gracefully close / rollback ( e . g . os / machine crashed or lost power ) .
