2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright 2021 ScyllaDB
22 #include <seastar/core/internal/io_intent.hh>
27 template <typename CharType>
28 struct file_read_state {
29 typedef temporary_buffer<CharType> tmp_buf_type;
31 file_read_state(uint64_t offset, uint64_t front, size_t to_read,
32 size_t memory_alignment, size_t disk_alignment, io_intent* intent)
33 : buf(tmp_buf_type::aligned(memory_alignment,
34 align_up(to_read, disk_alignment)))
41 return eof || pos >= _to_read;
45 * Trim the buffer to the actual number of read bytes and cut the
46 * bytes from offset 0 till "_front".
48 * @note this function has to be called only if we read bytes beyond
51 void trim_buf_before_ret() {
52 if (have_good_bytes()) {
54 buf.trim_front(_front);
60 uint64_t cur_offset() const {
64 size_t left_space() const {
65 return buf.size() - pos;
68 size_t left_to_read() const {
69 // positive as long as (done() == false)
70 return _to_read - pos;
73 void append_new_data(tmp_buf_type& new_data) {
74 auto to_copy = std::min(left_space(), new_data.size());
76 std::memcpy(buf.get_write() + pos, new_data.get(), to_copy);
80 bool have_good_bytes() const {
84 io_intent* get_intent() {
85 return _iref.retrieve();
96 internal::intent_reference _iref;
99 } // namespace internal
100 } // namespace seastar