24 #include "../../algorithm.h"
25 #include "../../protocol/converter.h"
26 #include "../../result.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 >
210 virtual result read( std::size_t addr, std::span< std::byte, cell_size >
data ) = 0;
229 if ( mem_size % page_size != 0 )
230 return { .
status = status::MEM_NOT_PAGE_MULTIPLY_ERROR };
233 for ( uint32_t i = 0; i < mem_size / page_size; i++ ) {
235 auto addr = i * page_size;
236 if ( iface.
read( addr,
data ) != result::SUCCESS )
237 return { .status = status::READ_ERROR };
241 return { .status = status::SUCCESS, .addr = addr - page_size };
243 }
else if ( !hdr_st )
245 else if ( *hdr_st != *st )
246 return { .status = status::SUCCESS, .addr = addr - page_size };
249 return { .status = status::MISSING_PAGE };
250 return { .status = status::SUCCESS, .addr = mem_size - page_size };
263 if ( mem_size % page_size != 0 )
264 return { .
status = status::MEM_NOT_PAGE_MULTIPLY_ERROR };
267 for ( uint32_t i = 0; i < mem_size / page_size; i++ ) {
269 auto addr = i * page_size;
270 if ( iface.
read( addr,
data ) != result::SUCCESS )
271 return { .status = status::READ_ERROR };
274 return { .status = status::SUCCESS, .addr = addr, .state =
hdr_state::A };
277 else if ( *hdr_st != *st )
278 return { .status = status::SUCCESS, .addr = addr, .state = *hdr_st };
282 return { .status = status::LOCATE_FAILED_ERROR };
283 return { .status = status::SUCCESS, .addr = 0x00, .state =
next( *hdr_st ) };
288 virtual result write( std::size_t start_addr, std::span< std::byte const >
data ) = 0;
302 inline bool decr_addr( std::size_t& addr, std::size_t n, std::size_t start_addr )
307 if ( addr < start_addr )
315 std::size_t start_addr,
321 for ( std::size_t i = 0; i < cell_val; ++i ) {
324 auto buffer_offset = ( cell_val - i - 1 ) *
cell_size;
325 if ( buffer_offset +
cell_size > buffer.size() )
329 std::span< std::byte, cell_size >{
330 buffer.data() + buffer_offset, cell_size } ) !=
334 return buffer.subspan( 0, cell_val *
cell_size );
336 std::memcpy( buffer.data(), &cell_val,
sizeof( cell_val ) );
337 return buffer.subspan( 0,
sizeof( cell_val ) );
344 auto const capacity = end_addr - addr;
345 std::span< std::byte > buffer = iface.
get_buffer();
348 return status::SERIALIZE_VALUE_ERROR;
349 std::span< std::byte const >
data = *used;
352 key, buffer.data(), buffer.data() +
data.size(), buffer.data() + buffer.size() );
355 if ( capacity <
data.size() )
358 if ( iface.
write( addr,
data ) != result::SUCCESS )
359 return status::WRITE_ERROR;
361 return status::SUCCESS;
368 if (
res != status::SUCCESS )
371 return status::SUCCESS;
378 std::size_t addr = end_addr;
380 std::size_t last_free = end_addr;
384 if ( iface.
read( addr, std::span< std::byte, cell_size >{ tmp } ) !=
386 return status::READ_ERROR;
395 std::span< std::byte > buffer = iface.
get_buffer();
400 if ( iface.
read( addr, std::span< std::byte, cell_size >{ tmp } ) !=
402 return status::READ_ERROR;
405 return status::DESER_ERROR;
406 auto [is_seq, key, val] = *c;
414 std::span< std::byte > val_sp =
420 if (
auto res =
store_key( last_free, end_addr, key, iface );
421 res != status::SUCCESS )
431 status != status::MISSING_PAGE ) {
432 if (
status != status::SUCCESS )
435 if ( r != status::FULL )
439 if (
status != status::SUCCESS )
443 return status::RESET_KEYS_ERROR;
445 if ( iface.
clear_page( addr ) != result::SUCCESS )
446 return status::CLEAR_ERROR;
449 if ( iface.
write( addr, std::span< std::byte >{ hdr } ) != result::SUCCESS )
450 return status::WRITE_ERROR;
466 std::size_t addr = end_addr;
473 if ( iface.
read( addr, std::span< std::byte, cell_size >{ tmp } ) !=
475 return status::READ_ERROR;
480 auto [is_seq, key, val] = *c;
487 std::span< std::byte > val_sp =
490 if ( iface.
on_kval( key, val_sp ) != result::SUCCESS )
491 return status::ON_KVAL_ERROR;
494 return status::SUCCESS;
500 if (
status == status::MISSING_PAGE )
501 return status::SUCCESS;
502 if (
status != status::SUCCESS )
513 if ( a.size() > b.size() )
515 auto c = b.subspan( 0, a.size() );
519 return b == std::byte{ 0x00 };
527 auto k = cont.back();
534 auto iter =
find( cont, key );
535 if ( iter == cont.end() )
536 return cache_res::SEEN;
539 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:523
update_status_e
Definition: handler.h:184
@ MEM_NOT_PAGE_MULTIPLY_ERROR
status update(std::size_t mem_size, std::size_t page_size, update_iface &iface)
Definition: handler.h:428
bool is_prefix_of_with_zeros(std::span< std::byte const > a, std::span< std::byte const > b)
Definition: handler.h:511
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:227
locate_next_info locate_next_page(std::size_t mem_size, std::size_t page_size, read_iface &iface)
Definition: handler.h:261
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:78
status dump_unseen_keys(std::size_t addr, std::size_t end_addr, update_iface &iface)
Definition: handler.h:364
hdr_state state
Definition: handler.h:257
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:497
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:312
opt< T > get_val(std::span< std::byte const > data)
Definition: handler.h:159
std::size_t addr
Definition: handler.h:223
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:375
bool decr_addr(std::size_t &addr, std::size_t n, std::size_t start_addr)
Definition: handler.h:302
hdr_state
Definition: page.h:37
std::size_t addr
Definition: handler.h:256
hdr_state next(hdr_state cs) noexcept
Definition: page.h:43
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:464
cfg::status status
Definition: handler.h:255
opt< hdr_state > hdr_to_hdr_state(std::span< std::byte, cell_size > b) noexcept
Definition: page.h:71
cfg::status status
Definition: handler.h:222
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:532
uint32_t key
Definition: handler.h:88
static constexpr uint16_t hcell_size
Definition: base.h:29
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:342
Definition: handler.h:221
Definition: handler.h:254
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
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
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
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118
Definition: handler.h:216
virtual std::span< std::byte > get_buffer()=0
Definition: handler.h:457
virtual cache_res check_key_cache(uint32_t key)=0
virtual result on_kval(uint32_t key, std::span< std::byte >)=0
Definition: handler.h:209
virtual result read(std::size_t addr, std::span< std::byte, cell_size > data)=0
virtual ~read_iface()=default
Definition: handler.h:202
Definition: handler.h:287
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 result write(std::size_t start_addr, std::span< std::byte const > data)=0
virtual result clear_page(std::size_t addr)=0
virtual result reset_keys()=0
virtual cache_res check_key_cache(uint32_t key)=0
result represents an result of some operation, as an alternative to returning just bool with true/fal...
Definition: result.h:42