Seastar
High performance C++ framework for concurrent servers
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#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <cstring>
26#endif
27
28#include <seastar/core/align.hh>
29#include <seastar/core/internal/io_intent.hh>
30#include <seastar/core/temporary_buffer.hh>
31
32namespace seastar {
33namespace internal {
34
35template <typename CharType>
36struct file_read_state {
37 typedef temporary_buffer<CharType> tmp_buf_type;
38
39 file_read_state(uint64_t offset, uint64_t front, size_t to_read,
40 size_t memory_alignment, size_t disk_alignment, io_intent* intent)
41 : buf(tmp_buf_type::aligned(memory_alignment,
42 align_up(to_read, disk_alignment)))
43 , _offset(offset)
44 , _to_read(to_read)
45 , _front(front)
46 , _iref(intent) {}
47
48 bool done() const {
49 return eof || pos >= _to_read;
50 }
51
59 void trim_buf_before_ret() {
60 if (have_good_bytes()) {
61 buf.trim(pos);
62 buf.trim_front(_front);
63 } else {
64 buf.trim(0);
65 }
66 }
67
68 uint64_t cur_offset() const {
69 return _offset + pos;
70 }
71
72 size_t left_space() const {
73 return buf.size() - pos;
74 }
75
76 size_t left_to_read() const {
77 // positive as long as (done() == false)
78 return _to_read - pos;
79 }
80
81 void append_new_data(tmp_buf_type& new_data) {
82 auto to_copy = std::min(left_space(), new_data.size());
83
84 std::memcpy(buf.get_write() + pos, new_data.get(), to_copy);
85 pos += to_copy;
86 }
87
88 bool have_good_bytes() const {
89 return pos > _front;
90 }
91
92 io_intent* get_intent() {
93 return _iref.retrieve();
94 }
95
96public:
97 bool eof = false;
98 tmp_buf_type buf;
99 size_t pos = 0;
100private:
101 uint64_t _offset;
102 size_t _to_read;
103 uint64_t _front;
104 internal::intent_reference _iref;
105};
106
107} // namespace internal
108} // namespace seastar
Seastar API namespace.
Definition: abort_on_ebadf.hh:26