24 #include "../../algorithm.h"
25 #include "../../error_code.h"
26 #include "../../protocol/converter.h"
36 template <
typename T >
37 using opt = std::optional< T >;
43 static constexpr uint32_t
key_mask = 0x7FFFFFFF;
56 return r * ( ( x + r - 1 ) / r );
59 inline opt< cell_kind >
60 ser_cell( uint32_t key, std::span< std::byte const > value, std::span< std::byte, cell_size > dest )
65 static_assert(
sizeof( uint32_t ) ==
hcell_size );
68 std::memcpy( &val, value.data(), value.size() );
70 tmp = ( tmp << 32 ) + val;
72 std::memcpy( dest.data(), &tmp,
cell_size );
79 tmp = ( tmp << 32 ) + size;
80 std::memcpy( dest.data(), &tmp,
cell_size );
95 std::memcpy( &tmp, c.data(), c.size() );
96 auto front =
static_cast< uint32_t
>( tmp >> 32 );
99 .key =
static_cast< uint32_t
>( front &
key_mask ),
100 .val =
static_cast< uint32_t
>( tmp & 0xFFFF'FFFF ),
103 if ( r.is_seq && r.val == 0 )
108 inline opt< std::span< std::byte > >
111 if ( end - val_end <
static_cast< int >(
cell_size ) )
116 key, { beg, val_end }, std::span< std::byte, cell_size >{ val_end,
cell_size } );
121 return std::span{ beg, new_end };
128 template <
typename T >
135 std::span< std::byte, conv::max_size > front{ buffer.data(),
conv::max_size };
139 return std::span{ buffer.data(), buffer.data() + *used };
142 inline opt< std::span< std::byte > >
143 store_val( std::span< std::byte const > val, std::span< std::byte > buffer )
145 if ( buffer.size() < val.size() )
147 std::span< std::byte > front{ buffer.data(), val.size() };
148 std::memcpy( front.data(), val.data(), val.size() );
152 inline opt< std::span< std::byte > >
153 store_val( std::span< std::byte > val, std::span< std::byte > buffer )
155 return store_val( std::span< std::byte const >{ val.data(), val.size() }, buffer );
158 template <
typename T >
205 auto s =
static_cast< status >( code );
210 return "not enough space to store the value";
212 return "no page header found in memory";
214 return "failed to serialize the value";
216 return "failed to write to memory";
218 return "failed to read from memory";
220 return "failed to deserialize the value";
222 return "failed to reset keys";
224 return "failed to clear page";
226 return "failed to locate page";
228 return "error in on_kval callback";
230 return "memory size is not a multiple of page size";
232 return "unknown error";
241 inline constexpr cfg::status_category error_category_v< cfg::status > = {};
267 if ( mem_size % page_size != 0 )
271 for ( uint32_t i = 0; i < mem_size / page_size; i++ ) {
273 auto addr = i * page_size;
281 }
else if ( !hdr_st )
283 else if ( *hdr_st != *st )
301 if ( mem_size % page_size != 0 )
305 for ( uint32_t i = 0; i < mem_size / page_size; i++ ) {
307 auto addr = i * page_size;
315 else if ( *hdr_st != *st )
340 inline bool decr_addr( std::size_t& addr, std::size_t n, std::size_t start_addr )
345 if ( addr < start_addr )
353 std::size_t start_addr,
359 for ( std::size_t i = 0; i < cell_val; ++i ) {
362 auto buffer_offset = ( cell_val - i - 1 ) *
cell_size;
363 if ( buffer_offset +
cell_size > buffer.size() )
367 std::span< std::byte, cell_size >{
368 buffer.data() + buffer_offset, cell_size } ) )
371 return buffer.subspan( 0, cell_val *
cell_size );
373 std::memcpy( buffer.data(), &cell_val,
sizeof( cell_val ) );
374 return buffer.subspan( 0,
sizeof( cell_val ) );
381 auto const capacity = end_addr - addr;
382 std::span< std::byte > buffer = iface.
get_buffer();
386 std::span< std::byte const >
data = *used;
389 key, buffer.data(), buffer.data() +
data.size(), buffer.data() + buffer.size() );
392 if ( capacity <
data.size() )
415 std::size_t addr = end_addr;
417 std::size_t last_free = end_addr;
421 if ( !iface.
read( addr, std::span< std::byte, cell_size >{ tmp } ) )
431 std::span< std::byte > buffer = iface.
get_buffer();
436 if ( !iface.
read( addr, std::span< std::byte, cell_size >{ tmp } ) )
441 auto [is_seq, key, val] = *c;
449 std::span< std::byte > val_sp =
455 if (
auto res =
store_key( last_free, end_addr, key, iface );
484 if ( !iface.
write( addr, std::span< std::byte >{ hdr } ) )
501 std::size_t addr = end_addr;
508 if ( !iface.
read( addr, std::span< std::byte, cell_size >{ tmp } ) )
514 auto [is_seq, key, val] = *c;
521 std::span< std::byte > val_sp =
524 if ( !iface.
on_kval( key, val_sp ) )
547 if ( a.size() > b.size() )
549 auto c = b.subspan( 0, a.size() );
553 return b == std::byte{ 0x00 };
561 auto k = cont.back();
568 auto iter =
find( cont, key );
569 if ( iter == cont.end() )
570 return cache_res::SEEN;
573 return cache_res::NOT_SEEN;
The bounded class represents a wrapper over type T constrained between MinVal and MaxVal as compile-t...
Definition: bounded.h:44
MIT License.
Definition: base.h:25
static constexpr uint16_t cell_size
Definition: base.h:28
opt< std::size_t > pop_from_container(auto &cont)
Definition: handler.h:557
status update(std::size_t mem_size, std::size_t page_size, update_iface &iface)
Definition: handler.h:463
bool is_prefix_of_with_zeros(std::span< std::byte const > a, std::span< std::byte const > b)
Definition: handler.h:545
static constexpr uint32_t sin_bit_mask
Definition: handler.h:44
cell_kind
Definition: handler.h:49
opt< deser_res > deser_cell(std::span< std::byte, cell_size > c)
Definition: handler.h:92
locate_current_info locate_current_page(std::size_t mem_size, std::size_t page_size, read_iface &iface)
Definition: handler.h:265
locate_next_info locate_next_page(std::size_t mem_size, std::size_t page_size, read_iface &iface)
Definition: handler.h:299
opt< std::span< std::byte > > store_val(T const &val, std::span< std::byte > buffer)
Definition: handler.h:129
bool is_free_cell(std::span< std::byte, cell_size > cell)
Definition: handler.h:176
std::optional< T > opt
Definition: handler.h:37
static constexpr uint32_t key_mask
Definition: handler.h:43
std::array< std::byte, 2 > get_hdr(hdr_state hst) noexcept
Definition: page.h:77
status dump_unseen_keys(std::size_t addr, std::size_t end_addr, update_iface &iface)
Definition: handler.h:401
hdr_state state
Definition: handler.h:295
uint64_t cell
Definition: base.h:27
status load(std::size_t mem_size, std::size_t page_size, load_iface &iface)
Definition: handler.h:531
uint32_t val
Definition: handler.h:89
std::span< std::byte > manifest_value(bool is_seq, auto &cell_val, std::size_t start_addr, auto &addr, auto &iface, auto &buffer)
Definition: handler.h:350
opt< T > get_val(std::span< std::byte const > data)
Definition: handler.h:159
std::size_t addr
Definition: handler.h:261
opt< std::span< std::byte > > store_kval_impl(uint32_t key, std::byte *beg, std::byte *val_end, std::byte *end)
Definition: handler.h:109
status update_stored_config(std::size_t start_addr, std::size_t end_addr, update_iface &iface)
Definition: handler.h:412
bool decr_addr(std::size_t &addr, std::size_t n, std::size_t start_addr)
Definition: handler.h:340
hdr_state
Definition: page.h:36
std::size_t addr
Definition: handler.h:294
hdr_state next(hdr_state cs) noexcept
Definition: page.h:42
bool is_seq
Definition: handler.h:87
status load_stored_config(std::size_t start_addr, std::size_t end_addr, load_iface &iface)
Definition: handler.h:499
cfg::status status
Definition: handler.h:293
opt< hdr_state > hdr_to_hdr_state(std::span< std::byte, cell_size > b) noexcept
Definition: page.h:70
cfg::status status
Definition: handler.h:260
constexpr uint32_t closest_multiple_of(uint32_t x, uint32_t r) noexcept
Definition: handler.h:54
cache_res key_check_unseen_container(auto &cont, uint32_t key)
Definition: handler.h:566
uint32_t key
Definition: handler.h:88
static constexpr uint16_t hcell_size
Definition: base.h:29
status
Definition: handler.h:184
@ MEM_NOT_PAGE_MULTIPLY_ERROR
opt< cell_kind > ser_cell(uint32_t key, std::span< std::byte const > value, std::span< std::byte, cell_size > dest)
Definition: handler.h:60
cache_res
Definition: handler.h:171
status store_key(std::size_t &addr, std::size_t end_addr, uint32_t key, update_iface &iface)
Definition: handler.h:379
Definition: handler.h:259
Definition: handler.h:292
static constexpr T deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:81
static constexpr void serialize_at(std::span< std::byte, max_size > buffer, T item)
Definition: serializer.h:76
static constexpr std::size_t max_size
Definition: serializer.h:73
decltype(converter_for_impl< D, E >()) converter_for
Definition: converter.h:54
MIT License.
Definition: impl.h:31
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:168
constexpr bool all_of(Container &&cont, PredicateCallable &&f=std::identity())
Returns true if call to predicate 'f(x)' returns true for all items in 'cont'.
Definition: algorithm.h:342
constexpr bool equal(LhContainer &&lh, RhContainer &&rh, BinaryPredicateCallable &&f=std::equal_to< void >{})
Returns true if containers 'lh' and 'rh' has same size and calls to predicate f - f(lh[i],...
Definition: algorithm.h:356
void swap(static_vector< T, N > const &lh, static_vector< T, N > const &rh) noexcept
Definition: static_vector.h:294
T res
Definition: algorithm.h:505
constexpr auto find(Container &&cont, T const &item)
Finds first item in container 'cont' that is equal to 'item', returns iterator for container,...
Definition: algorithm.h:140
uint32_t error_value_type
Definition: error_code.h:31
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118
Definition: handler.h:254
virtual std::span< std::byte > get_buffer()=0
Definition: handler.h:492
virtual cache_res check_key_cache(uint32_t key)=0
virtual error_code on_kval(uint32_t key, std::span< std::byte >)=0
Definition: handler.h:247
virtual ~read_iface()=default
virtual error_code read(std::size_t addr, std::span< std::byte, cell_size > data)=0
Definition: handler.h:202
char const * message(error_value_type code) const noexcept override
Definition: handler.h:203
Definition: handler.h:325
virtual bool value_changed(uint32_t key, std::span< std::byte const > data)=0
virtual opt< uint32_t > take_unseen_key()=0
virtual opt< std::span< std::byte const > > serialize_value(uint32_t key, std::span< std::byte > buffer)=0
virtual cache_res check_key_cache(uint32_t key)=0
virtual error_code clear_page(std::size_t addr)=0
virtual error_code write(std::size_t start_addr, std::span< std::byte const > data)=0
virtual error_code reset_keys()=0
Definition: error_code.h:42
Definition: error_code.h:80