code: public Log truncate(long index) {
      assertIsOpen();
      if (index > 0)
        assertValidIndex(index);
      Assert.index(index >= segments.commitIndex(), "cannot truncate committed entries");
  
      if (lastIndex() == index)
        return this;
  
      for (Segment segment : segments.reverseSegments()) {
        if (segment.validIndex(index)) {
          segment.truncate(index);
          break;
        } else if (segment.index() > index) {
          segments.removeSegment(segment);
        }
      }
      entryBuffer.clear();
      return this;
    }
docs: Truncates the log up to the given index.
  
  @param index The index at which to truncate the log.
  @return The updated log.
  @throws IllegalStateException If the log is not open.
  @throws IndexOutOfBoundsException If the given index is not within the bounds of the log.