public int truncateTo(long offset) throws IOException {
        // Do offset translation before truncating the index to avoid needless scanning
        // in case we truncate the full index
        LogOffsetPosition mapping = translateOffset(offset);
        OffsetIndex offsetIndex = offsetIndex();
        TimeIndex timeIndex = timeIndex();

        offsetIndex.truncateTo(offset);
        timeIndex.truncateTo(offset);
        txnIndex.truncateTo(offset);

        // After truncation, reset and allocate more space for the (new currently active) index
        offsetIndex.resize(offsetIndex.maxIndexSize());
        timeIndex.resize(timeIndex.maxIndexSize());

        int bytesTruncated;
        if (mapping == null)
            bytesTruncated = 0;
        else
            bytesTruncated = log.truncateTo(mapping.position);

        if (log.sizeInBytes() == 0) {
            created = time.milliseconds();
            rollingBasedTimestamp = OptionalLong.empty();
        }

        bytesSinceLastIndexEntry = 0;
        if (maxTimestampSoFar() >= 0)
            maxTimestampAndOffsetSoFar = readLargestTimestamp();

        return bytesTruncated;
    }
Truncate off all index and log entries with offsets >= the given offset.
If the given offset is larger than the largest message in this segment, do nothing.

This method is not thread-safe.

@param offset The offset to truncate to
@return The number of log bytes truncated
storage/src/main/java/org/apache/kafka/storage/internals/log/LogSegment.java