Repository: ClickHouse/ClickHouse
File: src/Disks/IO/AsynchronousBoundedReadBuffer.cpp (C++)

Code before:
        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedReads);
        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedBytes, result.size);
    }

    bytes_to_ignore = 0;
    resetWorkingBuffer();

    size_t bytes_read = result.size - result.offset;
    if (bytes_read)
    {
        page_cache_cell = result.page_cache_cell;
        working_buffer = Buffer(result.buf + result.offset, result.buf + result.size);
        pos = working_buffer.begin();
    }

    file_offset_of_buffer_end = impl->getFileOffsetOfBufferEnd();

    chassert(file_offset_of_buffer_end <= getFileSize());

    if (read_until_position && (file_offset_of_buffer_end > *read_until_position))
    {
        size_t excessive_bytes_read = file_offset_of_buffer_end - *read_until_position;

        if (excessive_bytes_read > working_buffer.size())
            throw Exception(ErrorCodes::LOGICAL_ERROR,
                            "File offset moved too far: old_file_offset = {}, new_file_offset = {}, read_until_position = {}, bytes_read = {}",
                            old_file_offset_of_buffer_end, file_offset_of_buffer_end, *read_until_position, bytes_read);

        working_buffer.resize(working_buffer.size() - excessive_bytes_read);
        file_offset_of_buffer_end = *read_until_position;
    }

    return !working_buffer.empty();
}


off_t AsynchronousBoundedReadBuffer::seek(off_t offset, int whence)
{
    ProfileEvents::increment(ProfileEvents::RemoteFSSeeks);

    size_t new_pos;
    if (whence == SEEK_SET)
    {
        chassert(offset >= 0);
        new_pos = offset;
    }
    else if (whence == SEEK_CUR)
    {
        new_pos = static_cast<size_t>(getPosition()) + offset;
    }
    else

Review comment (performance):
As far as I see, `page_cache_cell` is not used anyhow in `AsynchronousBoundedReadBuffer`, it is only assigned and reset

Code after:
        ProfileEvents::increment(ProfileEvents::RemoteFSPrefetchedBytes, result.size);
    }
    else
    {
        /// Don't allocate any buffers if page cache is in use, the cache has its own buffers (PageCacheCell).
        if (!use_page_cache)
            memory.resize(buffer_size);

        {
            ProfileEventTimeIncrement<Microseconds> watch(ProfileEvents::SynchronousRemoteReadWaitMicroseconds);
            result = readSync(memory.data(), buffer_size);
        }

        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedReads);
        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedBytes, result.size);
    }

    bytes_to_ignore = 0;
    resetWorkingBuffer();

    chassert(use_page_cache || !result.page_cache_cell);

    size_t bytes_read = result.size - result.offset;
    if (bytes_read)
    {
        page_cache_cell = result.page_cache_cell;
        working_buffer = Buffer(result.buf + result.offset, result.buf + result.size);
        pos = working_buffer.begin();
    }

    file_offset_of_buffer_end = result.file_offset_of_buffer_end;

    chassert(file_offset_of_buffer_end <= getFileSize());

    if (read_until_position && (file_offset_of_buffer_end > *read_until_position))
    {
        size_t excessive_bytes_read = file_offset_of_buffer_end - *read_until_position;

        if (excessive_bytes_read > working_buffer.size())
            throw Exception(ErrorCodes::LOGICAL_ERROR,
                            "File offset moved too far: old_file_offset = {}, new_file_offset = {}, read_until_position = {}, bytes_read = {}",
                            old_file_offset_of_buffer_end, file_offset_of_buffer_end, *read_until_position, bytes_read);

        working_buffer.resize(working_buffer.size() - excessive_bytes_read);
        file_offset_of_buffer_end = *read_until_position;
    }

    return !working_buffer.empty();
}