From cfcf3fa83747c03254ccc9c6fd416c6c15cefad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Fri, 6 Jun 2025 19:12:05 +0200 Subject: [PATCH] Split `db_val` implementation --- nano/store/CMakeLists.txt | 2 +- nano/store/account.hpp | 2 +- nano/store/db_val.hpp | 208 +++---------------- nano/store/db_val_impl.hpp | 135 ------------ nano/store/db_val_templ.hpp | 310 ++++++++++++++++++++++++++++ nano/store/lmdb/block.cpp | 2 +- nano/store/rocksdb/block.cpp | 2 +- nano/store/typed_iterator_templ.hpp | 2 +- 8 files changed, 346 insertions(+), 317 deletions(-) delete mode 100644 nano/store/db_val_impl.hpp create mode 100644 nano/store/db_val_templ.hpp diff --git a/nano/store/CMakeLists.txt b/nano/store/CMakeLists.txt index d5b4b248..53504cb6 100644 --- a/nano/store/CMakeLists.txt +++ b/nano/store/CMakeLists.txt @@ -6,7 +6,7 @@ add_library( component.hpp confirmation_height.hpp db_val.hpp - db_val_impl.hpp + db_val_templ.hpp iterator.hpp final_vote.hpp fwd.hpp diff --git a/nano/store/account.hpp b/nano/store/account.hpp index dab7556b..34ba21e4 100644 --- a/nano/store/account.hpp +++ b/nano/store/account.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include diff --git a/nano/store/db_val.hpp b/nano/store/db_val.hpp index 42fdb59b..7df6cd7d 100644 --- a/nano/store/db_val.hpp +++ b/nano/store/db_val.hpp @@ -48,161 +48,41 @@ public: convert_buffer_to_value (); } - db_val (nano::uint128_union const & val_a) : - span_view (val_a.bytes.data (), sizeof (val_a)) - { - } - - db_val (nano::uint256_union const & val_a) : - span_view (val_a.bytes.data (), sizeof (val_a)) - { - } - - db_val (nano::uint512_union const & val_a) : - span_view (val_a.bytes.data (), sizeof (val_a)) - { - } - - db_val (nano::qualified_root const & val_a) : - span_view (reinterpret_cast (&val_a), sizeof (val_a)) - { - } - - db_val (nano::account_info const & val_a); - - db_val (nano::account_info_v22 const & val_a); - - db_val (nano::pending_info const & val_a); - - db_val (nano::pending_key const & val_a); - - db_val (nano::confirmation_height_info const & val_a) : - buffer (std::make_shared> ()) - { - { - nano::vectorstream stream (*buffer); - val_a.serialize (stream); - } - convert_buffer_to_value (); - } - - db_val (nano::block_info const & val_a) : - span_view (reinterpret_cast (&val_a), sizeof (val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - db_val (nano::endpoint_key const & val_a) : - span_view (reinterpret_cast (&val_a), sizeof (val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - db_val (std::shared_ptr const & val_a); - - db_val (uint64_t val_a) : - buffer (std::make_shared> ()) - { - { - boost::endian::native_to_big_inplace (val_a); - nano::vectorstream stream (*buffer); - nano::write (stream, val_a); - } - convert_buffer_to_value (); - } + db_val (uint64_t); + db_val (nano::uint128_union const &); + db_val (nano::uint256_union const &); + db_val (nano::uint512_union const &); + db_val (nano::qualified_root const &); + db_val (nano::account_info const &); + db_val (nano::account_info_v22 const &); + db_val (nano::pending_info const &); + db_val (nano::pending_key const &); + db_val (nano::confirmation_height_info const &); + db_val (nano::block_info const &); + db_val (nano::endpoint_key const &); + db_val (std::shared_ptr const &); + explicit operator uint64_t () const; + explicit operator nano::uint128_union () const; + explicit operator nano::uint256_union () const; + explicit operator nano::uint512_union () const; + explicit operator nano::qualified_root () const; explicit operator nano::account_info () const; explicit operator nano::account_info_v22 () const; - - explicit operator block_info () const - { - nano::block_info result; - debug_assert (size () == sizeof (result)); - static_assert (sizeof (nano::block_info::account) + sizeof (nano::block_info::balance) == sizeof (result), "Packed class"); - std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); - return result; - } - explicit operator nano::pending_info () const; - explicit operator nano::pending_key () const; - - explicit operator nano::confirmation_height_info () const - { - nano::bufferstream stream (span_view.data (), span_view.size ()); - nano::confirmation_height_info result; - bool error (result.deserialize (stream)); - (void)error; - debug_assert (!error); - return result; - } - - explicit operator nano::uint128_union () const - { - return convert (); - } - - explicit operator nano::amount () const - { - return convert (); - } - - explicit operator nano::block_hash () const - { - return convert (); - } - - explicit operator nano::public_key () const - { - return convert (); - } - - explicit operator nano::qualified_root () const - { - return convert (); - } - - explicit operator nano::uint256_union () const - { - return convert (); - } - - explicit operator nano::uint512_union () const - { - return convert (); - } - - explicit operator std::array () const - { - nano::bufferstream stream (span_view.data (), span_view.size ()); - std::array result; - auto error = nano::try_read (stream, result); - (void)error; - debug_assert (!error); - return result; - } - - explicit operator nano::endpoint_key () const - { - nano::endpoint_key result; - debug_assert (span_view.size () == sizeof (result)); - std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); - return result; - } - - explicit operator block_w_sideband () const; - - explicit operator std::nullptr_t () const - { - return nullptr; - } - - explicit operator nano::no_value () const - { - return no_value::dummy; - } - + explicit operator nano::confirmation_height_info () const; + explicit operator nano::block_info () const; + explicit operator nano::endpoint_key () const; explicit operator std::shared_ptr () const; + explicit operator nano::amount () const; + explicit operator nano::block_hash () const; + explicit operator nano::public_key () const; + explicit operator std::array () const; + explicit operator block_w_sideband () const; + explicit operator std::shared_ptr () const; + explicit operator std::nullptr_t () const; + explicit operator nano::no_value () const; template std::shared_ptr convert_to_block () const; @@ -213,48 +93,22 @@ public: explicit operator std::shared_ptr () const; explicit operator std::shared_ptr () const; - explicit operator std::shared_ptr () const; - - explicit operator uint64_t () const - { - uint64_t result; - nano::bufferstream stream (span_view.data (), span_view.size ()); - auto error (nano::try_read (stream, result)); - (void)error; - debug_assert (!error); - boost::endian::big_to_native_inplace (result); - return result; - } - void * data () const { return const_cast (static_cast (span_view.data ())); } - size_t size () const { return span_view.size (); } - void convert_buffer_to_value () - { - if (buffer) - { - span_view = std::span (buffer->data (), buffer->size ()); - } - } + void convert_buffer_to_value (); std::span span_view; std::shared_ptr> buffer; private: template - T convert () const - { - T result; - debug_assert (span_view.size () == sizeof (result)); - std::copy (span_view.begin (), span_view.end (), result.bytes.data ()); - return result; - } + T convert () const; }; } diff --git a/nano/store/db_val_impl.hpp b/nano/store/db_val_impl.hpp deleted file mode 100644 index 54142860..00000000 --- a/nano/store/db_val_impl.hpp +++ /dev/null @@ -1,135 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -inline nano::store::db_val::db_val (nano::account_info const & val_a) : - span_view (reinterpret_cast (&val_a), val_a.db_size ()) -{ -} - -inline nano::store::db_val::db_val (nano::account_info_v22 const & val_a) : - span_view (reinterpret_cast (&val_a), val_a.db_size ()) -{ -} - -inline nano::store::db_val::db_val (std::shared_ptr const & val_a) : - buffer (std::make_shared> ()) -{ - { - nano::vectorstream stream (*buffer); - nano::serialize_block (stream, *val_a); - } - convert_buffer_to_value (); -} - -inline nano::store::db_val::db_val (nano::pending_info const & val_a) : - span_view (reinterpret_cast (&val_a), val_a.db_size ()) -{ - static_assert (std::is_standard_layout::value, "Standard layout is required"); -} - -inline nano::store::db_val::db_val (nano::pending_key const & val_a) : - span_view (reinterpret_cast (&val_a), sizeof (val_a)) -{ - static_assert (std::is_standard_layout::value, "Standard layout is required"); -} - -inline nano::store::db_val::operator nano::account_info () const -{ - nano::account_info result; - debug_assert (span_view.size () == result.db_size ()); - std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); - return result; -} - -inline nano::store::db_val::operator nano::account_info_v22 () const -{ - nano::account_info_v22 result; - debug_assert (span_view.size () == result.db_size ()); - std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); - return result; -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - nano::bufferstream stream (span_view.data (), span_view.size ()); - std::shared_ptr result (nano::deserialize_block (stream)); - return result; -} - -inline nano::store::db_val::operator nano::store::block_w_sideband () const -{ - nano::bufferstream stream (span_view.data (), span_view.size ()); - nano::store::block_w_sideband block_w_sideband; - block_w_sideband.block = (nano::deserialize_block (stream)); - auto error = block_w_sideband.sideband.deserialize (stream, block_w_sideband.block->type ()); - release_assert (!error); - block_w_sideband.block->sideband_set (block_w_sideband.sideband); - return block_w_sideband; -} - -inline nano::store::db_val::operator nano::pending_info () const -{ - nano::pending_info result; - debug_assert (span_view.size () == result.db_size ()); - std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); - return result; -} - -inline nano::store::db_val::operator nano::pending_key () const -{ - nano::pending_key result; - debug_assert (span_view.size () == sizeof (result)); - static_assert (sizeof (nano::pending_key::account) + sizeof (nano::pending_key::hash) == sizeof (result), "Packed class"); - std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); - return result; -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - nano::bufferstream stream (span_view.data (), span_view.size ()); - auto error (false); - auto result (nano::make_shared (error, stream)); - debug_assert (!error); - return result; -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - return convert_to_block (); -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - return convert_to_block (); -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - return convert_to_block (); -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - return convert_to_block (); -} - -inline nano::store::db_val::operator std::shared_ptr () const -{ - return convert_to_block (); -} - -template -inline std::shared_ptr nano::store::db_val::convert_to_block () const -{ - nano::bufferstream stream (span_view.data (), span_view.size ()); - auto error (false); - auto result (nano::make_shared (error, stream)); - debug_assert (!error); - return result; -} diff --git a/nano/store/db_val_templ.hpp b/nano/store/db_val_templ.hpp new file mode 100644 index 00000000..5d9f711c --- /dev/null +++ b/nano/store/db_val_templ.hpp @@ -0,0 +1,310 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace nano::store +{ +/* + * Constructors + */ + +inline db_val::db_val (uint64_t val_a) : + buffer (std::make_shared> ()) +{ + { + boost::endian::native_to_big_inplace (val_a); + nano::vectorstream stream (*buffer); + nano::write (stream, val_a); + } + convert_buffer_to_value (); +} + +inline db_val::db_val (nano::uint128_union const & val_a) : + span_view (val_a.bytes.data (), sizeof (val_a)) +{ +} + +inline db_val::db_val (nano::uint256_union const & val_a) : + span_view (val_a.bytes.data (), sizeof (val_a)) +{ +} + +inline db_val::db_val (nano::uint512_union const & val_a) : + span_view (val_a.bytes.data (), sizeof (val_a)) +{ +} + +inline db_val::db_val (nano::qualified_root const & val_a) : + span_view (reinterpret_cast (&val_a), sizeof (val_a)) +{ +} + +inline db_val::db_val (nano::account_info const & val_a) : + span_view (reinterpret_cast (&val_a), val_a.db_size ()) +{ +} + +inline db_val::db_val (nano::account_info_v22 const & val_a) : + span_view (reinterpret_cast (&val_a), val_a.db_size ()) +{ +} + +inline db_val::db_val (nano::pending_info const & val_a) : + span_view (reinterpret_cast (&val_a), val_a.db_size ()) +{ + static_assert (std::is_standard_layout::value, "Standard layout is required"); +} + +inline db_val::db_val (nano::pending_key const & val_a) : + span_view (reinterpret_cast (&val_a), sizeof (val_a)) +{ + static_assert (std::is_standard_layout::value, "Standard layout is required"); +} + +inline db_val::db_val (nano::confirmation_height_info const & val_a) : + buffer (std::make_shared> ()) +{ + { + nano::vectorstream stream (*buffer); + val_a.serialize (stream); + } + convert_buffer_to_value (); +} + +inline db_val::db_val (nano::block_info const & val_a) : + span_view (reinterpret_cast (&val_a), sizeof (val_a)) +{ + static_assert (std::is_standard_layout::value, "Standard layout is required"); +} + +inline db_val::db_val (nano::endpoint_key const & val_a) : + span_view (reinterpret_cast (&val_a), sizeof (val_a)) +{ + static_assert (std::is_standard_layout::value, "Standard layout is required"); +} + +inline db_val::db_val (std::shared_ptr const & val_a) : + buffer (std::make_shared> ()) +{ + { + nano::vectorstream stream (*buffer); + nano::serialize_block (stream, *val_a); + } + convert_buffer_to_value (); +} + +/* + * Conversion operators + */ + +inline db_val::operator uint64_t () const +{ + uint64_t result; + nano::bufferstream stream (span_view.data (), span_view.size ()); + auto error (nano::try_read (stream, result)); + (void)error; + debug_assert (!error); + boost::endian::big_to_native_inplace (result); + return result; +} + +inline db_val::operator nano::uint128_union () const +{ + return convert (); +} + +inline db_val::operator nano::uint256_union () const +{ + return convert (); +} + +inline db_val::operator nano::uint512_union () const +{ + return convert (); +} + +inline db_val::operator nano::qualified_root () const +{ + return convert (); +} + +inline db_val::operator nano::account_info () const +{ + nano::account_info result; + debug_assert (span_view.size () == result.db_size ()); + std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); + return result; +} + +inline db_val::operator nano::account_info_v22 () const +{ + nano::account_info_v22 result; + debug_assert (span_view.size () == result.db_size ()); + std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); + return result; +} + +inline db_val::operator nano::pending_info () const +{ + nano::pending_info result; + debug_assert (span_view.size () == result.db_size ()); + std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); + return result; +} + +inline db_val::operator nano::pending_key () const +{ + nano::pending_key result; + debug_assert (span_view.size () == sizeof (result)); + static_assert (sizeof (nano::pending_key::account) + sizeof (nano::pending_key::hash) == sizeof (result), "Packed class"); + std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); + return result; +} + +inline db_val::operator nano::confirmation_height_info () const +{ + nano::bufferstream stream (span_view.data (), span_view.size ()); + nano::confirmation_height_info result; + bool error (result.deserialize (stream)); + (void)error; + debug_assert (!error); + return result; +} + +inline db_val::operator block_info () const +{ + nano::block_info result; + debug_assert (size () == sizeof (result)); + static_assert (sizeof (nano::block_info::account) + sizeof (nano::block_info::balance) == sizeof (result), "Packed class"); + std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); + return result; +} + +inline db_val::operator nano::endpoint_key () const +{ + nano::endpoint_key result; + debug_assert (span_view.size () == sizeof (result)); + std::copy (span_view.begin (), span_view.end (), reinterpret_cast (&result)); + return result; +} + +inline db_val::operator std::shared_ptr () const +{ + nano::bufferstream stream (span_view.data (), span_view.size ()); + std::shared_ptr result (nano::deserialize_block (stream)); + return result; +} + +inline db_val::operator nano::amount () const +{ + return convert (); +} + +inline db_val::operator nano::block_hash () const +{ + return convert (); +} + +inline db_val::operator nano::public_key () const +{ + return convert (); +} + +inline db_val::operator std::array () const +{ + nano::bufferstream stream (span_view.data (), span_view.size ()); + std::array result; + auto error = nano::try_read (stream, result); + (void)error; + debug_assert (!error); + return result; +} + +inline db_val::operator nano::store::block_w_sideband () const +{ + nano::bufferstream stream (span_view.data (), span_view.size ()); + nano::store::block_w_sideband block_w_sideband; + block_w_sideband.block = (nano::deserialize_block (stream)); + auto error = block_w_sideband.sideband.deserialize (stream, block_w_sideband.block->type ()); + release_assert (!error); + block_w_sideband.block->sideband_set (block_w_sideband.sideband); + return block_w_sideband; +} + +inline db_val::operator std::shared_ptr () const +{ + nano::bufferstream stream (span_view.data (), span_view.size ()); + auto error (false); + auto result (nano::make_shared (error, stream)); + debug_assert (!error); + return result; +} + +inline db_val::operator std::nullptr_t () const +{ + return nullptr; +} + +inline db_val::operator nano::no_value () const +{ + return no_value::dummy; +} + +template +inline std::shared_ptr db_val::convert_to_block () const +{ + nano::bufferstream stream (span_view.data (), span_view.size ()); + auto error (false); + auto result (nano::make_shared (error, stream)); + debug_assert (!error); + return result; +} + +inline db_val::operator std::shared_ptr () const +{ + return convert_to_block (); +} + +inline db_val::operator std::shared_ptr () const +{ + return convert_to_block (); +} + +inline db_val::operator std::shared_ptr () const +{ + return convert_to_block (); +} + +inline db_val::operator std::shared_ptr () const +{ + return convert_to_block (); +} + +inline db_val::operator std::shared_ptr () const +{ + return convert_to_block (); +} + +inline void db_val::convert_buffer_to_value () +{ + if (buffer) + { + span_view = std::span (buffer->data (), buffer->size ()); + } +} + +template +inline T db_val::convert () const +{ + T result; + debug_assert (span_view.size () == sizeof (result)); + std::copy (span_view.begin (), span_view.end (), result.bytes.data ()); + return result; +} +} \ No newline at end of file diff --git a/nano/store/lmdb/block.cpp b/nano/store/lmdb/block.cpp index e2b823ca..f0f81a16 100644 --- a/nano/store/lmdb/block.cpp +++ b/nano/store/lmdb/block.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/nano/store/rocksdb/block.cpp b/nano/store/rocksdb/block.cpp index 8ac63885..c73f48f9 100644 --- a/nano/store/rocksdb/block.cpp +++ b/nano/store/rocksdb/block.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/nano/store/typed_iterator_templ.hpp b/nano/store/typed_iterator_templ.hpp index f3053359..bb55f159 100644 --- a/nano/store/typed_iterator_templ.hpp +++ b/nano/store/typed_iterator_templ.hpp @@ -1,5 +1,5 @@ #include -#include +#include #include namespace nano::store