private boolean needToRollOverIndex(IndexEntry entry) {
    Offset offset = entry.getValue().getOffset();
    int thisValueSize = entry.getValue().getBytes().capacity();
    if (validIndexSegments.size() == 0) {
      logger.info("Creating first segment with start offset {}", offset);
      return true;
    }
    IndexSegment lastSegment = validIndexSegments.lastEntry().getValue();
    if (lastSegment.getVersion() != PersistentIndex.CURRENT_VERSION) {
      logger.info("Index: {} Rolling over because the Index version has changed from {} to {}", dataDir,
          lastSegment.getVersion(), PersistentIndex.CURRENT_VERSION);
      return true;
    }
    //Must check the log segment name before checking the size written for the edge case when last index segment is
    //sealed after log segment gets auto closed.
    if (!offset.getName().equals(lastSegment.getLogSegmentName())) {
      logger.info("Index: {} Rolling over from {} to {} because the log has rolled over from {} to {}", dataDir,
          lastSegment.getStartOffset(), offset, lastSegment.getLogSegmentName(), offset.getName());
      return true;
    }
    if (lastSegment.getSizeWritten() >= maxInMemoryIndexSizeInBytes) {
      logger.info("Index: {} Rolling over from {} to {} because the size written {} >= maxInMemoryIndexSizeInBytes {}",
          dataDir, lastSegment.getStartOffset(), offset, lastSegment.getSizeWritten(), maxInMemoryIndexSizeInBytes);
      return true;
    }
    if (lastSegment.getNumberOfItems() >= maxInMemoryNumElements) {
      logger.info(
          "Index: {} Rolling over from {} to {} because the number of items in the last segment: {} >= maxInMemoryNumElements {}",
          dataDir, lastSegment.getStartOffset(), offset, lastSegment.getNumberOfItems(), maxInMemoryNumElements);
      return true;
    }
    if (lastSegment.getValueSize() != thisValueSize) {
      logger.info(
          "Index: {} Rolling over from {} to {} because the segment value size: {} != current entry value size: {}",
          dataDir, lastSegment.getStartOffset(), offset, lastSegment.getValueSize(), thisValueSize);
      return true;
    }
    return false;
  }
Checks if the index segment needs to roll over to a new segment
@param entry The new entry that needs to be added to the existing active segment
@return True, if segment needs to roll over. False, otherwise
ambry-store/src/main/java/com/github/ambry/store/PersistentIndex.java