]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/internal/read_state.hh
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / include / seastar / core / internal / read_state.hh
1 /*
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.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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
16 * under the License.
17 */
18 /*
19 * Copyright 2021 ScyllaDB
20 */
21
22 #include <seastar/core/internal/io_intent.hh>
23
24 namespace seastar {
25 namespace internal {
26
27 template <typename CharType>
28 struct file_read_state {
29 typedef temporary_buffer<CharType> tmp_buf_type;
30
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)))
35 , _offset(offset)
36 , _to_read(to_read)
37 , _front(front)
38 , _iref(intent) {}
39
40 bool done() const {
41 return eof || pos >= _to_read;
42 }
43
44 /**
45 * Trim the buffer to the actual number of read bytes and cut the
46 * bytes from offset 0 till "_front".
47 *
48 * @note this function has to be called only if we read bytes beyond
49 * "_front".
50 */
51 void trim_buf_before_ret() {
52 if (have_good_bytes()) {
53 buf.trim(pos);
54 buf.trim_front(_front);
55 } else {
56 buf.trim(0);
57 }
58 }
59
60 uint64_t cur_offset() const {
61 return _offset + pos;
62 }
63
64 size_t left_space() const {
65 return buf.size() - pos;
66 }
67
68 size_t left_to_read() const {
69 // positive as long as (done() == false)
70 return _to_read - pos;
71 }
72
73 void append_new_data(tmp_buf_type& new_data) {
74 auto to_copy = std::min(left_space(), new_data.size());
75
76 std::memcpy(buf.get_write() + pos, new_data.get(), to_copy);
77 pos += to_copy;
78 }
79
80 bool have_good_bytes() const {
81 return pos > _front;
82 }
83
84 io_intent* get_intent() {
85 return _iref.retrieve();
86 }
87
88 public:
89 bool eof = false;
90 tmp_buf_type buf;
91 size_t pos = 0;
92 private:
93 uint64_t _offset;
94 size_t _to_read;
95 uint64_t _front;
96 internal::intent_reference _iref;
97 };
98
99 } // namespace internal
100 } // namespace seastar