forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearizing_input_stream.hh
118 lines (98 loc) · 3.11 KB
/
linearizing_input_stream.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (C) 2019-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <list>
#include <fmt/format.h>
#include <seastar/net/byteorder.hh>
#include <seastar/util/backtrace.hh>
#include "utils/fragment_range.hh"
#include "utils/bit_cast.hh"
namespace utils {
// Facilitates transparently reading from a fragmented range.
template<typename T, typename Exception = std::runtime_error>
requires FragmentRange<T>
class linearizing_input_stream {
using iterator = typename T::iterator;
using fragment_type = typename T::fragment_type;
private:
iterator _it;
iterator _end;
fragment_type _current;
size_t _size;
// We need stable addresses for the `bytes`, which, due to the small
// value optimization, can invalidate any attached bytes_view on move.
std::list<bytes> _linearized_values;
private:
size_t remove_current_prefix(size_t size) {
if (size < _current.size()) {
_current.remove_prefix(size);
_size -= size;
return size;
}
const auto ret = _current.size();
_size -= ret;
++_it;
_current = (_it == _end) ? fragment_type{} : *_it;
return ret;
}
void check_size(size_t size) const {
if (size > _size) {
seastar::throw_with_backtrace<Exception>(
fmt::format("linearizing_input_stream::check_size() - not enough bytes (requested {:d}, got {:d})", size, _size));
}
}
std::pair<bytes_view, bool> do_read(size_t size) {
check_size(size);
if (size <= _current.size()) {
bytes_view ret(_current.begin(), size);
remove_current_prefix(size);
return {ret, false};
}
auto out = _linearized_values.emplace_back(bytes::initialized_later{}, size).begin();
while (size) {
out = std::copy_n(_current.begin(), std::min(size, _current.size()), out);
size -= remove_current_prefix(size);
}
return {_linearized_values.back(), true};
}
public:
explicit linearizing_input_stream(const T& fr)
: _it(fr.begin())
, _end(fr.end())
, _current(*_it)
, _size(fr.size_bytes()) {
}
// Not cheap to copy, would copy all linearized values.
linearizing_input_stream(const linearizing_input_stream&) = delete;
size_t size() const {
return _size;
}
bool empty() const {
return _size == 0;
}
// The returned view is only valid as long as the stream is alive.
bytes_view read(size_t size) {
return do_read(size).first;
}
template <typename Type>
requires std::is_trivial_v<Type>
Type read_trivial() {
auto [bv, linearized] = do_read(sizeof(Type));
auto ret = net::ntoh(::read_unaligned<net::packed<Type>>(bv.begin()));
if (linearized) {
_linearized_values.pop_back();
}
return ret;
}
void skip(size_t size) {
check_size(size);
while (size) {
size -= remove_current_prefix(size);
}
}
};
} // namespace utils