@Override
    public void truncateTo(long offset) {
        lock.lock();
        try {
            ByteBuffer idx = mmap().duplicate();
            int slot = largestLowerBoundSlotFor(idx, offset, IndexSearchType.KEY);

            /* There are 3 cases for choosing the new size
             * 1) if there is no entry in the index <= the offset, delete everything
             * 2) if there is an entry for this exact offset, delete it and everything larger than it
             * 3) if there is no entry for this offset, delete everything larger than the next smallest
             */
            int newEntries;
            if (slot < 0)
                newEntries = 0;
            else if (relativeOffset(idx, slot) == offset - baseOffset())
                newEntries = slot;
            else
                newEntries = slot + 1;
            truncateToEntries(newEntries);
        } finally {
            lock.unlock();
        }
    }
Append an entry for the given offset/location pair to the index. This entry must have a larger offset than all subsequent entries.
@throws IndexOffsetOverflowException if the offset causes index offset to overflow
@throws InvalidOffsetException if provided offset is not larger than the last offset
storage/src/main/java/org/apache/kafka/storage/internals/log/OffsetIndex.java