Repository: apache/kafka
File: storage/src/main/java/org/apache/kafka/storage/internals/log/AbstractIndex.java (Java)

Code before:
        }
    }

    /**
     * Forcefully free the buffer's mmap.
     */
    // Visible for testing
    protected void forceUnmap() throws IOException {
        try {
            ByteBufferUnmapper.unmap(file.getAbsolutePath(), mmap);
        } finally {
            mmap = null;
        }
    }

    // The caller is expected to hold `lock` when calling this method
    protected void incrementEntries() {
        ++entries;
    }

    protected void truncateToEntries0(int entries) {
        this.entries = entries;
        mmap.position(entries * entrySize());
    }

    protected final <T, E extends Exception> T inResizeLock(Lock lock, StorageAction<T, E> action) throws E {
        lock.lock();
        try {
            return action.execute();
        } finally {
            lock.unlock();
        }
    }

    /**
     * Find the slot in which the largest entry less than or equal to the given target key or value is stored.
     * The comparison is made using the `IndexEntry.compareTo()` method.
     *
     * @param idx The index buffer
     * @param target The index key to look for
     * @return The slot found or -1 if the least entry in the index is larger than the target key or the index is empty
     */
    protected int largestLowerBoundSlotFor(ByteBuffer idx, long target, IndexSearchType searchEntity) {
        return indexSlotRangeFor(idx, target, searchEntity, SearchResultType.LARGEST_LOWER_BOUND);
    }

    /**
     * Find the smallest entry greater than or equal the target key or value. If none can be found, -1 is returned.
     */
    protected int smallestUpperBoundSlotFor(ByteBuffer idx, long target, IndexSearchType searchEntity) {
        return indexSlotRangeFor(idx, target, searchEntity, SearchResultType.SMALLEST_UPPER_BOUND);

Review comment (suggestion):
Perhaps it's better to make this `inResizeReadLock` and avoid passing in lock from the input. For consistency, we could also introduce a method like `inLock` for the other lock. This way, we could make both locks private.

Code after:
    /**
     * Forcefully free the buffer's mmap.
     */
    // Visible for testing
    protected void forceUnmap() throws IOException {
        try {
            ByteBufferUnmapper.unmap(file.getAbsolutePath(), mmap);
        } finally {
            mmap = null;
        }
    }

    // The caller is expected to hold `lock` when calling this method
    protected void incrementEntries() {
        ++entries;
    }

    protected void truncateToEntries0(int entries) {
        this.entries = entries;
        mmap.position(entries * entrySize());
    }

    protected final <T> T inLock(Supplier<T> action) {
        return LockUtils.inLock(lock, action);
    }

    protected final void inLock(Runnable action) {
        LockUtils.inLock(lock, action);
    }

    protected final <T, E extends Exception> T inLockThrows(LockUtils.ThrowingSupplier<T, E> action) throws E {
        return LockUtils.inLockThrows(lock, action);
    }

    protected final <E extends Exception> void inLockThrows(LockUtils.ThrowingRunnable<E> action) throws E {
        LockUtils.inLockThrows(lock, action);
    }

    protected final <T> T inRemapReadLock(Supplier<T> action) {
        return LockUtils.inLock(remapLock.readLock(), action);
    }

    protected final void inRemapReadLock(Runnable action) {
        LockUtils.inLock(remapLock.readLock(), action);
    }

    protected final <T, E extends Exception> T inRemapWriteLockThrows(LockUtils.ThrowingSupplier<T, E> action) throws E {
        return LockUtils.inLockThrows(remapLock.writeLock(), action);
    }

    protected final <E extends Exception> void inRemapWriteLockThrows(LockUtils.ThrowingRunnable<E> action) throws E {