From 76cbf5a85bb2937a40040252364039ce7acec4c1 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Tue, 19 Sep 2023 00:27:30 +0100 Subject: [PATCH] Extracting classes from component.hpp in to their own files --- nano/nano_node/entry.cpp | 1 + nano/node/bootstrap/bootstrap_server.cpp | 2 + nano/node/bootstrap_ascending/iterators.cpp | 2 + nano/node/bootstrap_ascending/service.cpp | 1 + nano/node/confirmation_height_bounded.cpp | 3 + nano/node/confirmation_height_unbounded.cpp | 3 + nano/node/ledger_walker.cpp | 1 + nano/node/online_reps.cpp | 1 + nano/secure/ledger.cpp | 10 + nano/store/CMakeLists.txt | 33 + nano/store/account.cpp | 15 + nano/store/account.hpp | 36 + nano/store/block.cpp | 1 + nano/store/block.hpp | 45 ++ nano/store/component.cpp | 94 +-- nano/store/component.hpp | 740 +------------------- nano/store/confirmation_height.cpp | 15 + nano/store/confirmation_height.hpp | 43 ++ nano/store/db_val.cpp | 1 + nano/store/db_val.hpp | 381 ++++++++++ nano/store/final.cpp | 1 + nano/store/final.hpp | 35 + nano/store/frontier.cpp | 1 + nano/store/frontier.hpp | 32 + nano/store/iterator.cpp | 1 + nano/store/iterator.hpp | 65 ++ nano/store/iterator_impl.cpp | 1 + nano/store/iterator_impl.hpp | 27 + nano/store/lmdb/account.hpp | 2 +- nano/store/lmdb/block.hpp | 4 +- nano/store/lmdb/confirmation_height.hpp | 2 +- nano/store/lmdb/db_val.cpp | 28 + nano/store/lmdb/db_val.hpp | 10 + nano/store/lmdb/final_vote.hpp | 2 +- nano/store/lmdb/frontier.hpp | 2 +- nano/store/lmdb/iterator.hpp | 2 + nano/store/lmdb/lmdb.cpp | 30 +- nano/store/lmdb/lmdb.hpp | 12 +- nano/store/lmdb/online_weight.hpp | 2 +- nano/store/lmdb/peer.hpp | 2 +- nano/store/lmdb/pending.hpp | 2 +- nano/store/lmdb/pruned.hpp | 2 +- nano/store/lmdb/version.hpp | 2 +- nano/store/lmdb/wallet_value.hpp | 2 +- nano/store/online_weight.cpp | 1 + nano/store/online_weight.hpp | 32 + nano/store/peer.cpp | 1 + nano/store/peer.hpp | 32 + nano/store/pending.cpp | 1 + nano/store/pending.hpp | 34 + nano/store/pruned.cpp | 1 + nano/store/pruned.hpp | 35 + nano/store/rocksdb/account.hpp | 2 +- nano/store/rocksdb/block.hpp | 6 +- nano/store/rocksdb/confirmation_height.hpp | 2 +- nano/store/rocksdb/db_val.cpp | 28 + nano/store/rocksdb/db_val.hpp | 10 + nano/store/rocksdb/final_vote.hpp | 2 +- nano/store/rocksdb/frontier.hpp | 3 +- nano/store/rocksdb/iterator.hpp | 1 + nano/store/rocksdb/online_weight.hpp | 2 +- nano/store/rocksdb/peer.hpp | 2 +- nano/store/rocksdb/pending.hpp | 2 +- nano/store/rocksdb/pruned.hpp | 2 +- nano/store/rocksdb/rocksdb.cpp | 30 +- nano/store/rocksdb/version.hpp | 2 +- nano/store/tables.hpp | 35 + nano/store/transaction.cpp | 64 ++ nano/store/transaction.hpp | 72 ++ nano/store/version.cpp | 1 + nano/store/version.hpp | 27 + nano/store/versioning.hpp | 8 + 72 files changed, 1225 insertions(+), 910 deletions(-) create mode 100644 nano/store/account.cpp create mode 100644 nano/store/account.hpp create mode 100644 nano/store/block.cpp create mode 100644 nano/store/block.hpp create mode 100644 nano/store/confirmation_height.cpp create mode 100644 nano/store/confirmation_height.hpp create mode 100644 nano/store/db_val.cpp create mode 100644 nano/store/db_val.hpp create mode 100644 nano/store/final.cpp create mode 100644 nano/store/final.hpp create mode 100644 nano/store/frontier.cpp create mode 100644 nano/store/frontier.hpp create mode 100644 nano/store/iterator.cpp create mode 100644 nano/store/iterator.hpp create mode 100644 nano/store/iterator_impl.cpp create mode 100644 nano/store/iterator_impl.hpp create mode 100644 nano/store/lmdb/db_val.cpp create mode 100644 nano/store/lmdb/db_val.hpp create mode 100644 nano/store/online_weight.cpp create mode 100644 nano/store/online_weight.hpp create mode 100644 nano/store/peer.cpp create mode 100644 nano/store/peer.hpp create mode 100644 nano/store/pending.cpp create mode 100644 nano/store/pending.hpp create mode 100644 nano/store/pruned.cpp create mode 100644 nano/store/pruned.hpp create mode 100644 nano/store/rocksdb/db_val.cpp create mode 100644 nano/store/rocksdb/db_val.hpp create mode 100644 nano/store/tables.hpp create mode 100644 nano/store/transaction.cpp create mode 100644 nano/store/transaction.hpp create mode 100644 nano/store/version.cpp create mode 100644 nano/store/version.hpp diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index f16fd1c922..ae1dcb795f 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include diff --git a/nano/node/bootstrap/bootstrap_server.cpp b/nano/node/bootstrap/bootstrap_server.cpp index 9a70a93fb0..bb6dcf3000 100644 --- a/nano/node/bootstrap/bootstrap_server.cpp +++ b/nano/node/bootstrap/bootstrap_server.cpp @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include // TODO: Make threads configurable nano::bootstrap_server::bootstrap_server (nano::store::component & store_a, nano::ledger & ledger_a, nano::network_constants const & network_constants_a, nano::stats & stats_a) : diff --git a/nano/node/bootstrap_ascending/iterators.cpp b/nano/node/bootstrap_ascending/iterators.cpp index 3a8391bd30..9f29f98bd8 100644 --- a/nano/node/bootstrap_ascending/iterators.cpp +++ b/nano/node/bootstrap_ascending/iterators.cpp @@ -1,7 +1,9 @@ #include #include #include +#include #include +#include /* * database_iterator diff --git a/nano/node/bootstrap_ascending/service.cpp b/nano/node/bootstrap_ascending/service.cpp index 87682a04f0..e7d2117931 100644 --- a/nano/node/bootstrap_ascending/service.cpp +++ b/nano/node/bootstrap_ascending/service.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include using namespace std::chrono_literals; diff --git a/nano/node/confirmation_height_bounded.cpp b/nano/node/confirmation_height_bounded.cpp index e1ce791347..cec0958410 100644 --- a/nano/node/confirmation_height_bounded.cpp +++ b/nano/node/confirmation_height_bounded.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include diff --git a/nano/node/confirmation_height_unbounded.cpp b/nano/node/confirmation_height_unbounded.cpp index 62f1036bab..e443a96a8f 100644 --- a/nano/node/confirmation_height_unbounded.cpp +++ b/nano/node/confirmation_height_unbounded.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include diff --git a/nano/node/ledger_walker.cpp b/nano/node/ledger_walker.cpp index a2484d5ad0..7c58592af4 100644 --- a/nano/node/ledger_walker.cpp +++ b/nano/node/ledger_walker.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include diff --git a/nano/node/online_reps.cpp b/nano/node/online_reps.cpp index bbf1299bb4..370aaa6790 100644 --- a/nano/node/online_reps.cpp +++ b/nano/node/online_reps.cpp @@ -2,6 +2,7 @@ #include #include #include +#include nano::online_reps::online_reps (nano::ledger & ledger_a, nano::node_config const & config_a) : ledger{ ledger_a }, diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index d46a7574f6..de98c67e88 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -5,7 +5,17 @@ #include #include #include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include #include diff --git a/nano/store/CMakeLists.txt b/nano/store/CMakeLists.txt index 75c9027cf8..aa48a3926b 100644 --- a/nano/store/CMakeLists.txt +++ b/nano/store/CMakeLists.txt @@ -1,9 +1,18 @@ add_library( nano_store + account.hpp + block.hpp component.hpp + confirmation_height.hpp + db_val.hpp + iterator.hpp + iterator_impl.hpp + final.hpp + frontier.hpp lmdb/account.hpp lmdb/block.hpp lmdb/confirmation_height.hpp + lmdb/db_val.hpp lmdb/final_vote.hpp lmdb/frontier.hpp lmdb/lmdb.hpp @@ -17,9 +26,14 @@ add_library( lmdb/transaction_impl.hpp lmdb/version.hpp lmdb/wallet_value.hpp + online_weight.hpp + peer.hpp + pending.hpp + pruned.hpp rocksdb/account.hpp rocksdb/block.hpp rocksdb/confirmation_height.hpp + rocksdb/db_val.hpp rocksdb/final_vote.hpp rocksdb/frontier.hpp rocksdb/iterator.hpp @@ -30,11 +44,23 @@ add_library( rocksdb/rocksdb.hpp rocksdb/transaction_impl.hpp rocksdb/version.hpp + tables.hpp + transaction.hpp + version.hpp versioning.hpp + account.cpp + block.cpp component.cpp + confirmation_height.cpp + db_val.cpp + iterator.cpp + iterator_impl.cpp + final.cpp + frontier.cpp lmdb/account.cpp lmdb/block.cpp lmdb/confirmation_height.cpp + lmdb/db_val.cpp lmdb/final_vote.cpp lmdb/frontier.cpp lmdb/lmdb.cpp @@ -46,9 +72,14 @@ add_library( lmdb/pruned.cpp lmdb/version.cpp lmdb/wallet_value.cpp + online_weight.cpp + peer.cpp + pending.cpp + pruned.cpp rocksdb/account.cpp rocksdb/block.cpp rocksdb/confirmation_height.cpp + rocksdb/db_val.cpp rocksdb/final_vote.cpp rocksdb/frontier.cpp rocksdb/online_weight.cpp @@ -58,6 +89,8 @@ add_library( rocksdb/rocksdb.cpp rocksdb/transaction.cpp rocksdb/version.cpp + transaction.cpp + version.cpp versioning.cpp) target_link_libraries( diff --git a/nano/store/account.cpp b/nano/store/account.cpp new file mode 100644 index 0000000000..d08002c148 --- /dev/null +++ b/nano/store/account.cpp @@ -0,0 +1,15 @@ +#include + +std::optional nano::account_store::get (nano::transaction const & transaction, nano::account const & account) +{ + nano::account_info info; + bool error = get (transaction, account, info); + if (!error) + { + return info; + } + else + { + return std::nullopt; + } +} diff --git a/nano/store/account.hpp b/nano/store/account.hpp new file mode 100644 index 0000000000..ad5da11e0b --- /dev/null +++ b/nano/store/account.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages account storage and iteration + */ +class account_store +{ +public: + virtual void put (nano::write_transaction const &, nano::account const &, nano::account_info const &) = 0; + virtual bool get (nano::transaction const &, nano::account const &, nano::account_info &) = 0; + std::optional get (nano::transaction const &, nano::account const &); + virtual void del (nano::write_transaction const &, nano::account const &) = 0; + virtual bool exists (nano::transaction const &, nano::account const &) = 0; + virtual size_t count (nano::transaction const &) = 0; + virtual nano::store_iterator begin (nano::transaction const &, nano::account const &) const = 0; + virtual nano::store_iterator begin (nano::transaction const &) const = 0; + virtual nano::store_iterator rbegin (nano::transaction const &) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const &) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/block.cpp b/nano/store/block.cpp new file mode 100644 index 0000000000..40d128b96e --- /dev/null +++ b/nano/store/block.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/block.hpp b/nano/store/block.hpp new file mode 100644 index 0000000000..b0059c96c3 --- /dev/null +++ b/nano/store/block.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +class block_w_sideband +{ +public: + std::shared_ptr block; + nano::block_sideband sideband; +}; +/** + * Manages block storage and iteration + */ +class block_store +{ +public: + virtual void put (nano::write_transaction const &, nano::block_hash const &, nano::block const &) = 0; + virtual void raw_put (nano::write_transaction const &, std::vector const &, nano::block_hash const &) = 0; + virtual nano::block_hash successor (nano::transaction const &, nano::block_hash const &) const = 0; + virtual void successor_clear (nano::write_transaction const &, nano::block_hash const &) = 0; + virtual std::shared_ptr get (nano::transaction const &, nano::block_hash const &) const = 0; + virtual std::shared_ptr random (nano::transaction const &) = 0; + virtual void del (nano::write_transaction const &, nano::block_hash const &) = 0; + virtual bool exists (nano::transaction const &, nano::block_hash const &) = 0; + virtual uint64_t count (nano::transaction const &) = 0; + virtual nano::store_iterator begin (nano::transaction const &, nano::block_hash const &) const = 0; + virtual nano::store_iterator begin (nano::transaction const &) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/component.cpp b/nano/store/component.cpp index d5f94d1cb7..03cd6716c2 100644 --- a/nano/store/component.cpp +++ b/nano/store/component.cpp @@ -1,67 +1,9 @@ -#include #include +#include +#include #include - -nano::read_transaction::read_transaction (std::unique_ptr read_transaction_impl) : - impl (std::move (read_transaction_impl)) -{ -} - -void * nano::read_transaction::get_handle () const -{ - return impl->get_handle (); -} - -void nano::read_transaction::reset () const -{ - impl->reset (); -} - -void nano::read_transaction::renew () const -{ - impl->renew (); -} - -void nano::read_transaction::refresh () const -{ - reset (); - renew (); -} - -nano::write_transaction::write_transaction (std::unique_ptr write_transaction_impl) : - impl (std::move (write_transaction_impl)) -{ - /* - * For IO threads, we do not want them to block on creating write transactions. - */ - debug_assert (nano::thread_role::get () != nano::thread_role::name::io); -} - -void * nano::write_transaction::get_handle () const -{ - return impl->get_handle (); -} - -void nano::write_transaction::commit () -{ - impl->commit (); -} - -void nano::write_transaction::renew () -{ - impl->renew (); -} - -void nano::write_transaction::refresh () -{ - impl->commit (); - impl->renew (); -} - -bool nano::write_transaction::contains (nano::tables table_a) const -{ - return impl->contains (table_a); -} +#include +#include // clang-format off nano::store::component::component ( @@ -109,31 +51,3 @@ void nano::store::component::initialize (nano::write_transaction const & transac ledger_cache_a.rep_weights.representation_put (constants.genesis->account (), std::numeric_limits::max ()); frontier.put (transaction_a, hash_l, constants.genesis->account ()); } - -std::optional nano::account_store::get (const nano::transaction & transaction, const nano::account & account) -{ - nano::account_info info; - bool error = get (transaction, account, info); - if (!error) - { - return info; - } - else - { - return std::nullopt; - } -} - -std::optional nano::confirmation_height_store::get (const nano::transaction & transaction, const nano::account & account) -{ - nano::confirmation_height_info info; - bool error = get (transaction, account, info); - if (!error) - { - return info; - } - else - { - return std::nullopt; - } -} diff --git a/nano/store/component.hpp b/nano/store/component.hpp index e6fa1a4a1f..e5ee585c0d 100644 --- a/nano/store/component.hpp +++ b/nano/store/component.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -13,723 +15,17 @@ namespace nano { -// Move to versioning with a specific version if required for a future upgrade -template -class block_w_sideband_v18 -{ -public: - std::shared_ptr block; - nano::block_sideband_v18 sideband; -}; - -class block_w_sideband -{ -public: - std::shared_ptr block; - nano::block_sideband sideband; -}; - -/** - * Encapsulates database specific container - */ -template -class db_val -{ -public: - db_val (Val const & value_a) : - value (value_a) - { - } - - db_val () : - db_val (0, nullptr) - { - } - - db_val (std::nullptr_t) : - db_val (0, this) - { - } - - db_val (nano::uint128_union const & val_a) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - } - - db_val (nano::uint256_union const & val_a) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - } - - db_val (nano::uint512_union const & val_a) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - } - - db_val (nano::qualified_root const & val_a) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - } - - db_val (nano::account_info const & val_a) : - db_val (val_a.db_size (), const_cast (&val_a)) - { - } - - db_val (nano::account_info_v14 const & val_a) : - db_val (val_a.db_size (), const_cast (&val_a)) - { - } - - db_val (nano::pending_info const & val_a) : - db_val (val_a.db_size (), const_cast (&val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - db_val (nano::pending_info_v14 const & val_a) : - db_val (val_a.db_size (), const_cast (&val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - db_val (nano::pending_key const & val_a) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - 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) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - db_val (nano::endpoint_key const & val_a) : - db_val (sizeof (val_a), const_cast (&val_a)) - { - static_assert (std::is_standard_layout::value, "Standard layout is required"); - } - - 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 (); - } - - 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 (); - } - - explicit operator nano::account_info () const - { - nano::account_info result; - debug_assert (size () == result.db_size ()); - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); - return result; - } - - explicit operator nano::account_info_v14 () const - { - nano::account_info_v14 result; - debug_assert (size () == result.db_size ()); - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); - return result; - } - - explicit operator nano::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 (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), reinterpret_cast (&result)); - return result; - } - - explicit operator nano::pending_info_v14 () const - { - nano::pending_info_v14 result; - debug_assert (size () == result.db_size ()); - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); - return result; - } - - explicit operator nano::pending_info () const - { - nano::pending_info result; - debug_assert (size () == result.db_size ()); - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); - return result; - } - - explicit operator nano::pending_key () const - { - nano::pending_key result; - debug_assert (size () == sizeof (result)); - static_assert (sizeof (nano::pending_key::account) + sizeof (nano::pending_key::hash) == sizeof (result), "Packed class"); - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), reinterpret_cast (&result)); - return result; - } - - explicit operator nano::confirmation_height_info () const - { - nano::bufferstream stream (reinterpret_cast (data ()), 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 (reinterpret_cast (data ()), 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; - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), reinterpret_cast (&result)); - return result; - } - - template - explicit operator block_w_sideband_v18 () const - { - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - auto error (false); - block_w_sideband_v18 block_w_sideband; - block_w_sideband.block = std::make_shared (error, stream); - release_assert (!error); - - error = block_w_sideband.sideband.deserialize (stream, block_w_sideband.block->type ()); - release_assert (!error); - - return block_w_sideband; - } - - explicit operator block_w_sideband () const - { - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - nano::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; - } - - explicit operator state_block_w_sideband_v14 () const - { - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - auto error (false); - nano::state_block_w_sideband_v14 block_w_sideband; - block_w_sideband.state_block = std::make_shared (error, stream); - debug_assert (!error); - - block_w_sideband.sideband.type = nano::block_type::state; - error = block_w_sideband.sideband.deserialize (stream); - debug_assert (!error); - - return block_w_sideband; - } - - explicit operator std::nullptr_t () const - { - return nullptr; - } - - explicit operator nano::no_value () const - { - return no_value::dummy; - } - - explicit operator std::shared_ptr () const - { - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - std::shared_ptr result (nano::deserialize_block (stream)); - return result; - } - - template - std::shared_ptr convert_to_block () const - { - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - auto error (false); - auto result (std::make_shared (error, stream)); - debug_assert (!error); - return result; - } - - explicit operator std::shared_ptr () const - { - return convert_to_block (); - } - - explicit operator std::shared_ptr () const - { - return convert_to_block (); - } - - explicit operator std::shared_ptr () const - { - return convert_to_block (); - } - - explicit operator std::shared_ptr () const - { - return convert_to_block (); - } - - explicit operator std::shared_ptr () const - { - return convert_to_block (); - } - - explicit operator std::shared_ptr () const - { - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - auto error (false); - auto result (nano::make_shared (error, stream)); - debug_assert (!error); - return result; - } - - explicit operator uint64_t () const - { - uint64_t result; - nano::bufferstream stream (reinterpret_cast (data ()), size ()); - auto error (nano::try_read (stream, result)); - (void)error; - debug_assert (!error); - boost::endian::big_to_native_inplace (result); - return result; - } - - operator Val * () const - { - // Allow passing a temporary to a non-c++ function which doesn't have constness - return const_cast (&value); - } - - operator Val const & () const - { - return value; - } - - // Must be specialized - void * data () const; - size_t size () const; - db_val (size_t size_a, void * data_a); - void convert_buffer_to_value (); - - Val value; - std::shared_ptr> buffer; - -private: - template - T convert () const - { - T result; - debug_assert (size () == sizeof (result)); - std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), result.bytes.data ()); - return result; - } -}; -template -class store_iterator_impl -{ -public: - virtual ~store_iterator_impl () = default; - virtual nano::store_iterator_impl & operator++ () = 0; - virtual nano::store_iterator_impl & operator-- () = 0; - virtual bool operator== (nano::store_iterator_impl const & other_a) const = 0; - virtual bool is_end_sentinal () const = 0; - virtual void fill (std::pair &) const = 0; - nano::store_iterator_impl & operator= (nano::store_iterator_impl const &) = delete; - bool operator== (nano::store_iterator_impl const * other_a) const - { - return (other_a != nullptr && *this == *other_a) || (other_a == nullptr && is_end_sentinal ()); - } - bool operator!= (nano::store_iterator_impl const & other_a) const - { - return !(*this == other_a); - } -}; -/** - * Iterates the key/value pairs of a transaction - */ -template -class store_iterator final -{ -public: - store_iterator (std::nullptr_t) - { - } - store_iterator (std::unique_ptr> impl_a) : - impl (std::move (impl_a)) - { - impl->fill (current); - } - store_iterator (nano::store_iterator && other_a) : - current (std::move (other_a.current)), - impl (std::move (other_a.impl)) - { - } - nano::store_iterator & operator++ () - { - ++*impl; - impl->fill (current); - return *this; - } - nano::store_iterator & operator-- () - { - --*impl; - impl->fill (current); - return *this; - } - nano::store_iterator & operator= (nano::store_iterator && other_a) noexcept - { - impl = std::move (other_a.impl); - current = std::move (other_a.current); - return *this; - } - nano::store_iterator & operator= (nano::store_iterator const &) = delete; - std::pair * operator-> () - { - return ¤t; - } - bool operator== (nano::store_iterator const & other_a) const - { - return (impl == nullptr && other_a.impl == nullptr) || (impl != nullptr && *impl == other_a.impl.get ()) || (other_a.impl != nullptr && *other_a.impl == impl.get ()); - } - bool operator!= (nano::store_iterator const & other_a) const - { - return !(*this == other_a); - } - -private: - std::pair current; - std::unique_ptr> impl; -}; - -// Keep this in alphabetical order -enum class tables -{ - accounts, - blocks, - confirmation_height, - default_unused, // RocksDB only - final_votes, - frontiers, - meta, - online_weight, - peers, - pending, - pruned, - vote -}; - -class transaction_impl -{ -public: - virtual ~transaction_impl () = default; - virtual void * get_handle () const = 0; -}; - -class read_transaction_impl : public transaction_impl -{ -public: - virtual void reset () = 0; - virtual void renew () = 0; -}; - -class write_transaction_impl : public transaction_impl -{ -public: - virtual void commit () = 0; - virtual void renew () = 0; - virtual bool contains (nano::tables table_a) const = 0; -}; - -class transaction -{ -public: - virtual ~transaction () = default; - virtual void * get_handle () const = 0; -}; - -/** - * RAII wrapper of a read MDB_txn where the constructor starts the transaction - * and the destructor aborts it. - */ -class read_transaction final : public transaction -{ -public: - explicit read_transaction (std::unique_ptr read_transaction_impl); - void * get_handle () const override; - void reset () const; - void renew () const; - void refresh () const; - -private: - std::unique_ptr impl; -}; - -/** - * RAII wrapper of a read-write MDB_txn where the constructor starts the transaction - * and the destructor commits it. - */ -class write_transaction final : public transaction -{ -public: - explicit write_transaction (std::unique_ptr write_transaction_impl); - void * get_handle () const override; - void commit (); - void renew (); - void refresh (); - bool contains (nano::tables table_a) const; - -private: - std::unique_ptr impl; -}; - +class account_store; +class block_store; +class confirmation_height_store; +class final_vote_store; +class frontier_store; class ledger_cache; - -/** - * Manages frontier storage and iteration - */ -class frontier_store -{ -public: - virtual void put (nano::write_transaction const &, nano::block_hash const &, nano::account const &) = 0; - virtual nano::account get (nano::transaction const &, nano::block_hash const &) const = 0; - virtual void del (nano::write_transaction const &, nano::block_hash const &) = 0; - virtual nano::store_iterator begin (nano::transaction const &) const = 0; - virtual nano::store_iterator begin (nano::transaction const &, nano::block_hash const &) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; -}; - -/** - * Manages account storage and iteration - */ -class account_store -{ -public: - virtual void put (nano::write_transaction const &, nano::account const &, nano::account_info const &) = 0; - virtual bool get (nano::transaction const &, nano::account const &, nano::account_info &) = 0; - std::optional get (nano::transaction const &, nano::account const &); - virtual void del (nano::write_transaction const &, nano::account const &) = 0; - virtual bool exists (nano::transaction const &, nano::account const &) = 0; - virtual size_t count (nano::transaction const &) = 0; - virtual nano::store_iterator begin (nano::transaction const &, nano::account const &) const = 0; - virtual nano::store_iterator begin (nano::transaction const &) const = 0; - virtual nano::store_iterator rbegin (nano::transaction const &) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const &) const = 0; -}; - -/** - * Manages pending storage and iteration - */ -class pending_store -{ -public: - virtual void put (nano::write_transaction const &, nano::pending_key const &, nano::pending_info const &) = 0; - virtual void del (nano::write_transaction const &, nano::pending_key const &) = 0; - virtual bool get (nano::transaction const &, nano::pending_key const &, nano::pending_info &) = 0; - virtual bool exists (nano::transaction const &, nano::pending_key const &) = 0; - virtual bool any (nano::transaction const &, nano::account const &) = 0; - virtual nano::store_iterator begin (nano::transaction const &, nano::pending_key const &) const = 0; - virtual nano::store_iterator begin (nano::transaction const &) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; -}; - -/** - * Manages peer storage and iteration - */ -class peer_store -{ -public: - virtual void put (nano::write_transaction const & transaction_a, nano::endpoint_key const & endpoint_a) = 0; - virtual void del (nano::write_transaction const & transaction_a, nano::endpoint_key const & endpoint_a) = 0; - virtual bool exists (nano::transaction const & transaction_a, nano::endpoint_key const & endpoint_a) const = 0; - virtual size_t count (nano::transaction const & transaction_a) const = 0; - virtual void clear (nano::write_transaction const & transaction_a) = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; - virtual nano::store_iterator end () const = 0; -}; - -/** - * Manages online weight storage and iteration - */ -class online_weight_store -{ -public: - virtual void put (nano::write_transaction const &, uint64_t, nano::amount const &) = 0; - virtual void del (nano::write_transaction const &, uint64_t) = 0; - virtual nano::store_iterator begin (nano::transaction const &) const = 0; - virtual nano::store_iterator rbegin (nano::transaction const &) const = 0; - virtual nano::store_iterator end () const = 0; - virtual size_t count (nano::transaction const &) const = 0; - virtual void clear (nano::write_transaction const &) = 0; -}; - -/** - * Manages pruned storage and iteration - */ -class pruned_store -{ -public: - virtual void put (nano::write_transaction const & transaction_a, nano::block_hash const & hash_a) = 0; - virtual void del (nano::write_transaction const & transaction_a, nano::block_hash const & hash_a) = 0; - virtual bool exists (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const = 0; - virtual nano::block_hash random (nano::transaction const & transaction_a) = 0; - virtual size_t count (nano::transaction const & transaction_a) const = 0; - virtual void clear (nano::write_transaction const &) = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; -}; - -/** - * Manages confirmation height storage and iteration - */ -class confirmation_height_store -{ -public: - virtual void put (nano::write_transaction const & transaction_a, nano::account const & account_a, nano::confirmation_height_info const & confirmation_height_info_a) = 0; - - /** Retrieves confirmation height info relating to an account. - * The parameter confirmation_height_info_a is always written. - * On error, the confirmation height and frontier hash are set to 0. - * Ruturns true on error, false on success. - */ - virtual bool get (nano::transaction const & transaction_a, nano::account const & account_a, nano::confirmation_height_info & confirmation_height_info_a) = 0; - std::optional get (nano::transaction const & transaction_a, nano::account const & account_a); - virtual bool exists (nano::transaction const & transaction_a, nano::account const & account_a) const = 0; - virtual void del (nano::write_transaction const & transaction_a, nano::account const & account_a) = 0; - virtual uint64_t count (nano::transaction const & transaction_a) = 0; - virtual void clear (nano::write_transaction const &, nano::account const &) = 0; - virtual void clear (nano::write_transaction const &) = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a, nano::account const & account_a) const = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const &) const = 0; -}; - -/** - * Manages final vote storage and iteration - */ -class final_vote_store -{ -public: - virtual bool put (nano::write_transaction const & transaction_a, nano::qualified_root const & root_a, nano::block_hash const & hash_a) = 0; - virtual std::vector get (nano::transaction const & transaction_a, nano::root const & root_a) = 0; - virtual void del (nano::write_transaction const & transaction_a, nano::root const & root_a) = 0; - virtual size_t count (nano::transaction const & transaction_a) const = 0; - virtual void clear (nano::write_transaction const &, nano::root const &) = 0; - virtual void clear (nano::write_transaction const &) = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a, nano::qualified_root const & root_a) const = 0; - virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; -}; - -/** - * Manages version storage - */ -class version_store -{ -public: - virtual void put (nano::write_transaction const &, int) = 0; - virtual int get (nano::transaction const &) const = 0; -}; - -/** - * Manages block storage and iteration - */ -class block_store -{ -public: - virtual void put (nano::write_transaction const &, nano::block_hash const &, nano::block const &) = 0; - virtual void raw_put (nano::write_transaction const &, std::vector const &, nano::block_hash const &) = 0; - virtual nano::block_hash successor (nano::transaction const &, nano::block_hash const &) const = 0; - virtual void successor_clear (nano::write_transaction const &, nano::block_hash const &) = 0; - virtual std::shared_ptr get (nano::transaction const &, nano::block_hash const &) const = 0; - virtual std::shared_ptr random (nano::transaction const &) = 0; - virtual void del (nano::write_transaction const &, nano::block_hash const &) = 0; - virtual bool exists (nano::transaction const &, nano::block_hash const &) = 0; - virtual uint64_t count (nano::transaction const &) = 0; - virtual nano::store_iterator begin (nano::transaction const &, nano::block_hash const &) const = 0; - virtual nano::store_iterator begin (nano::transaction const &) const = 0; - virtual nano::store_iterator end () const = 0; - virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; -}; +class online_weight_store; +class peer_store; +class pending_store; +class pruned_store; +class version_store; namespace store { @@ -801,15 +97,3 @@ namespace store }; } // namespace store } // namespace nano - -namespace std -{ -template <> -struct hash<::nano::tables> -{ - size_t operator() (::nano::tables const & table_a) const - { - return static_cast (table_a); - } -}; -} diff --git a/nano/store/confirmation_height.cpp b/nano/store/confirmation_height.cpp new file mode 100644 index 0000000000..1398f73108 --- /dev/null +++ b/nano/store/confirmation_height.cpp @@ -0,0 +1,15 @@ +#include + +std::optional nano::confirmation_height_store::get (nano::transaction const & transaction, nano::account const & account) +{ + nano::confirmation_height_info info; + bool error = get (transaction, account, info); + if (!error) + { + return info; + } + else + { + return std::nullopt; + } +} diff --git a/nano/store/confirmation_height.hpp b/nano/store/confirmation_height.hpp new file mode 100644 index 0000000000..2a35fb0785 --- /dev/null +++ b/nano/store/confirmation_height.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages confirmation height storage and iteration + */ +class confirmation_height_store +{ +public: + virtual void put (nano::write_transaction const & transaction_a, nano::account const & account_a, nano::confirmation_height_info const & confirmation_height_info_a) = 0; + + /** Retrieves confirmation height info relating to an account. + * The parameter confirmation_height_info_a is always written. + * On error, the confirmation height and frontier hash are set to 0. + * Ruturns true on error, false on success. + */ + virtual bool get (nano::transaction const & transaction_a, nano::account const & account_a, nano::confirmation_height_info & confirmation_height_info_a) = 0; + std::optional get (nano::transaction const & transaction_a, nano::account const & account_a); + virtual bool exists (nano::transaction const & transaction_a, nano::account const & account_a) const = 0; + virtual void del (nano::write_transaction const & transaction_a, nano::account const & account_a) = 0; + virtual uint64_t count (nano::transaction const & transaction_a) = 0; + virtual void clear (nano::write_transaction const &, nano::account const &) = 0; + virtual void clear (nano::write_transaction const &) = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a, nano::account const & account_a) const = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const &) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/db_val.cpp b/nano/store/db_val.cpp new file mode 100644 index 0000000000..cab8d8ef04 --- /dev/null +++ b/nano/store/db_val.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/db_val.hpp b/nano/store/db_val.hpp new file mode 100644 index 0000000000..08da3834e3 --- /dev/null +++ b/nano/store/db_val.hpp @@ -0,0 +1,381 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#include + +namespace nano +{ +/** + * Encapsulates database specific container + */ +template +class db_val +{ +public: + db_val (Val const & value_a) : + value (value_a) + { + } + + db_val () : + db_val (0, nullptr) + { + } + + db_val (std::nullptr_t) : + db_val (0, this) + { + } + + db_val (nano::uint128_union const & val_a) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + } + + db_val (nano::uint256_union const & val_a) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + } + + db_val (nano::uint512_union const & val_a) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + } + + db_val (nano::qualified_root const & val_a) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + } + + db_val (nano::account_info const & val_a) : + db_val (val_a.db_size (), const_cast (&val_a)) + { + } + + db_val (nano::account_info_v14 const & val_a) : + db_val (val_a.db_size (), const_cast (&val_a)) + { + } + + db_val (nano::pending_info const & val_a) : + db_val (val_a.db_size (), const_cast (&val_a)) + { + static_assert (std::is_standard_layout::value, "Standard layout is required"); + } + + db_val (nano::pending_info_v14 const & val_a) : + db_val (val_a.db_size (), const_cast (&val_a)) + { + static_assert (std::is_standard_layout::value, "Standard layout is required"); + } + + db_val (nano::pending_key const & val_a) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + static_assert (std::is_standard_layout::value, "Standard layout is required"); + } + + 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) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + static_assert (std::is_standard_layout::value, "Standard layout is required"); + } + + db_val (nano::endpoint_key const & val_a) : + db_val (sizeof (val_a), const_cast (&val_a)) + { + static_assert (std::is_standard_layout::value, "Standard layout is required"); + } + + 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 (); + } + + 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 (); + } + + explicit operator nano::account_info () const + { + nano::account_info result; + debug_assert (size () == result.db_size ()); + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); + return result; + } + + explicit operator nano::account_info_v14 () const + { + nano::account_info_v14 result; + debug_assert (size () == result.db_size ()); + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); + return result; + } + + explicit operator nano::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 (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), reinterpret_cast (&result)); + return result; + } + + explicit operator nano::pending_info_v14 () const + { + nano::pending_info_v14 result; + debug_assert (size () == result.db_size ()); + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); + return result; + } + + explicit operator nano::pending_info () const + { + nano::pending_info result; + debug_assert (size () == result.db_size ()); + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + result.db_size (), reinterpret_cast (&result)); + return result; + } + + explicit operator nano::pending_key () const + { + nano::pending_key result; + debug_assert (size () == sizeof (result)); + static_assert (sizeof (nano::pending_key::account) + sizeof (nano::pending_key::hash) == sizeof (result), "Packed class"); + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), reinterpret_cast (&result)); + return result; + } + + explicit operator nano::confirmation_height_info () const + { + nano::bufferstream stream (reinterpret_cast (data ()), 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 (reinterpret_cast (data ()), 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; + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), reinterpret_cast (&result)); + return result; + } + + template + explicit operator block_w_sideband_v18 () const + { + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + auto error (false); + block_w_sideband_v18 block_w_sideband; + block_w_sideband.block = std::make_shared (error, stream); + release_assert (!error); + + error = block_w_sideband.sideband.deserialize (stream, block_w_sideband.block->type ()); + release_assert (!error); + + return block_w_sideband; + } + + explicit operator block_w_sideband () const + { + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + nano::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; + } + + explicit operator state_block_w_sideband_v14 () const + { + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + auto error (false); + nano::state_block_w_sideband_v14 block_w_sideband; + block_w_sideband.state_block = std::make_shared (error, stream); + debug_assert (!error); + + block_w_sideband.sideband.type = nano::block_type::state; + error = block_w_sideband.sideband.deserialize (stream); + debug_assert (!error); + + return block_w_sideband; + } + + explicit operator std::nullptr_t () const + { + return nullptr; + } + + explicit operator nano::no_value () const + { + return no_value::dummy; + } + + explicit operator std::shared_ptr () const + { + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + std::shared_ptr result (nano::deserialize_block (stream)); + return result; + } + + template + std::shared_ptr convert_to_block () const + { + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + auto error (false); + auto result (std::make_shared (error, stream)); + debug_assert (!error); + return result; + } + + explicit operator std::shared_ptr () const + { + return convert_to_block (); + } + + explicit operator std::shared_ptr () const + { + return convert_to_block (); + } + + explicit operator std::shared_ptr () const + { + return convert_to_block (); + } + + explicit operator std::shared_ptr () const + { + return convert_to_block (); + } + + explicit operator std::shared_ptr () const + { + return convert_to_block (); + } + + explicit operator std::shared_ptr () const + { + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + auto error (false); + auto result (nano::make_shared (error, stream)); + debug_assert (!error); + return result; + } + + explicit operator uint64_t () const + { + uint64_t result; + nano::bufferstream stream (reinterpret_cast (data ()), size ()); + auto error (nano::try_read (stream, result)); + (void)error; + debug_assert (!error); + boost::endian::big_to_native_inplace (result); + return result; + } + + operator Val * () const + { + // Allow passing a temporary to a non-c++ function which doesn't have constness + return const_cast (&value); + } + + operator Val const & () const + { + return value; + } + + // Must be specialized + void * data () const; + size_t size () const; + db_val (size_t size_a, void * data_a); + void convert_buffer_to_value (); + + Val value; + std::shared_ptr> buffer; + +private: + template + T convert () const + { + T result; + debug_assert (size () == sizeof (result)); + std::copy (reinterpret_cast (data ()), reinterpret_cast (data ()) + sizeof (result), result.bytes.data ()); + return result; + } +}; +} // namespace nano diff --git a/nano/store/final.cpp b/nano/store/final.cpp new file mode 100644 index 0000000000..69d469dc76 --- /dev/null +++ b/nano/store/final.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/final.hpp b/nano/store/final.hpp new file mode 100644 index 0000000000..b51cbe4cf9 --- /dev/null +++ b/nano/store/final.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages final vote storage and iteration + */ +class final_vote_store +{ +public: + virtual bool put (nano::write_transaction const & transaction_a, nano::qualified_root const & root_a, nano::block_hash const & hash_a) = 0; + virtual std::vector get (nano::transaction const & transaction_a, nano::root const & root_a) = 0; + virtual void del (nano::write_transaction const & transaction_a, nano::root const & root_a) = 0; + virtual size_t count (nano::transaction const & transaction_a) const = 0; + virtual void clear (nano::write_transaction const &, nano::root const &) = 0; + virtual void clear (nano::write_transaction const &) = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a, nano::qualified_root const & root_a) const = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/frontier.cpp b/nano/store/frontier.cpp new file mode 100644 index 0000000000..2d12551d59 --- /dev/null +++ b/nano/store/frontier.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/frontier.hpp b/nano/store/frontier.hpp new file mode 100644 index 0000000000..56eb8d2563 --- /dev/null +++ b/nano/store/frontier.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages frontier storage and iteration + */ +class frontier_store +{ +public: + virtual void put (nano::write_transaction const &, nano::block_hash const &, nano::account const &) = 0; + virtual nano::account get (nano::transaction const &, nano::block_hash const &) const = 0; + virtual void del (nano::write_transaction const &, nano::block_hash const &) = 0; + virtual nano::store_iterator begin (nano::transaction const &) const = 0; + virtual nano::store_iterator begin (nano::transaction const &, nano::block_hash const &) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/iterator.cpp b/nano/store/iterator.cpp new file mode 100644 index 0000000000..15bf18facf --- /dev/null +++ b/nano/store/iterator.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/iterator.hpp b/nano/store/iterator.hpp new file mode 100644 index 0000000000..bf88b5861c --- /dev/null +++ b/nano/store/iterator.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include + +#include + +namespace nano +{ +/** + * Iterates the key/value pairs of a transaction + */ +template +class store_iterator final +{ +public: + store_iterator (std::nullptr_t) + { + } + store_iterator (std::unique_ptr> impl_a) : + impl (std::move (impl_a)) + { + impl->fill (current); + } + store_iterator (nano::store_iterator && other_a) : + current (std::move (other_a.current)), + impl (std::move (other_a.impl)) + { + } + nano::store_iterator & operator++ () + { + ++*impl; + impl->fill (current); + return *this; + } + nano::store_iterator & operator-- () + { + --*impl; + impl->fill (current); + return *this; + } + nano::store_iterator & operator= (nano::store_iterator && other_a) noexcept + { + impl = std::move (other_a.impl); + current = std::move (other_a.current); + return *this; + } + nano::store_iterator & operator= (nano::store_iterator const &) = delete; + std::pair * operator-> () + { + return ¤t; + } + bool operator== (nano::store_iterator const & other_a) const + { + return (impl == nullptr && other_a.impl == nullptr) || (impl != nullptr && *impl == other_a.impl.get ()) || (other_a.impl != nullptr && *other_a.impl == impl.get ()); + } + bool operator!= (nano::store_iterator const & other_a) const + { + return !(*this == other_a); + } + +private: + std::pair current; + std::unique_ptr> impl; +}; +} // namespace nano::store diff --git a/nano/store/iterator_impl.cpp b/nano/store/iterator_impl.cpp new file mode 100644 index 0000000000..4abbeb3f2a --- /dev/null +++ b/nano/store/iterator_impl.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/iterator_impl.hpp b/nano/store/iterator_impl.hpp new file mode 100644 index 0000000000..16e958ef14 --- /dev/null +++ b/nano/store/iterator_impl.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +namespace nano +{ +template +class store_iterator_impl +{ +public: + virtual ~store_iterator_impl () = default; + virtual nano::store_iterator_impl & operator++ () = 0; + virtual nano::store_iterator_impl & operator-- () = 0; + virtual bool operator== (nano::store_iterator_impl const & other_a) const = 0; + virtual bool is_end_sentinal () const = 0; + virtual void fill (std::pair &) const = 0; + nano::store_iterator_impl & operator= (nano::store_iterator_impl const &) = delete; + bool operator== (nano::store_iterator_impl const * other_a) const + { + return (other_a != nullptr && *this == *other_a) || (other_a == nullptr && is_end_sentinal ()); + } + bool operator!= (nano::store_iterator_impl const & other_a) const + { + return !(*this == other_a); + } +}; +} // namespace nano::store diff --git a/nano/store/lmdb/account.hpp b/nano/store/lmdb/account.hpp index e78947c558..d15c2ce887 100644 --- a/nano/store/lmdb/account.hpp +++ b/nano/store/lmdb/account.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/block.hpp b/nano/store/lmdb/block.hpp index c690d853ee..cb1df35109 100644 --- a/nano/store/lmdb/block.hpp +++ b/nano/store/lmdb/block.hpp @@ -1,12 +1,12 @@ #pragma once -#include +#include +#include #include namespace nano { -using mdb_val = db_val; class block_predecessor_mdb_set; namespace lmdb { diff --git a/nano/store/lmdb/confirmation_height.hpp b/nano/store/lmdb/confirmation_height.hpp index 68ca1a1560..ff29a4eae8 100644 --- a/nano/store/lmdb/confirmation_height.hpp +++ b/nano/store/lmdb/confirmation_height.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/db_val.cpp b/nano/store/lmdb/db_val.cpp new file mode 100644 index 0000000000..c0a100abb6 --- /dev/null +++ b/nano/store/lmdb/db_val.cpp @@ -0,0 +1,28 @@ +#include + +namespace nano +{ +template <> +void * mdb_val::data () const +{ + return value.mv_data; +} + +template <> +std::size_t mdb_val::size () const +{ + return value.mv_size; +} + +template <> +mdb_val::db_val (std::size_t size_a, void * data_a) : + value ({ size_a, data_a }) +{ +} + +template <> +void mdb_val::convert_buffer_to_value () +{ + value = { buffer->size (), const_cast (buffer->data ()) }; +} +} diff --git a/nano/store/lmdb/db_val.hpp b/nano/store/lmdb/db_val.hpp new file mode 100644 index 0000000000..5638103c18 --- /dev/null +++ b/nano/store/lmdb/db_val.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include + +namespace nano +{ +using mdb_val = db_val; +} diff --git a/nano/store/lmdb/final_vote.hpp b/nano/store/lmdb/final_vote.hpp index 5612fab943..40bdfdb406 100644 --- a/nano/store/lmdb/final_vote.hpp +++ b/nano/store/lmdb/final_vote.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/frontier.hpp b/nano/store/lmdb/frontier.hpp index b493f339cd..b8af5b2f23 100644 --- a/nano/store/lmdb/frontier.hpp +++ b/nano/store/lmdb/frontier.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/iterator.hpp b/nano/store/lmdb/iterator.hpp index 5d74d176c7..bf1fe9b32a 100644 --- a/nano/store/lmdb/iterator.hpp +++ b/nano/store/lmdb/iterator.hpp @@ -1,6 +1,8 @@ #pragma once #include +#include +#include #include diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 94593dbce6..ae84e0bb51 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -1,9 +1,10 @@ #include #include #include -#include #include +#include #include +#include #include #include @@ -12,33 +13,6 @@ #include -namespace nano -{ -template <> -void * mdb_val::data () const -{ - return value.mv_data; -} - -template <> -std::size_t mdb_val::size () const -{ - return value.mv_size; -} - -template <> -mdb_val::db_val (std::size_t size_a, void * data_a) : - value ({ size_a, data_a }) -{ -} - -template <> -void mdb_val::convert_buffer_to_value () -{ - value = { buffer->size (), const_cast (buffer->data ()) }; -} -} - nano::lmdb::store::store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, nano::ledger_constants & constants, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, nano::lmdb_config const & lmdb_config_a, bool backup_before_upgrade_a) : // clang-format off nano::store::component{ diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index 0fb962f7a2..77c72dc199 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -34,8 +35,6 @@ namespace filesystem namespace nano { -using mdb_val = db_val; - class logging_mt; class transaction; @@ -193,13 +192,4 @@ namespace lmdb friend void modify_confirmation_height_to_v15 (nano::lmdb::store &, nano::transaction const &, nano::account const &, uint64_t); }; } - -template <> -void * mdb_val::data () const; -template <> -std::size_t mdb_val::size () const; -template <> -mdb_val::db_val (std::size_t size_a, void * data_a); -template <> -void mdb_val::convert_buffer_to_value (); } diff --git a/nano/store/lmdb/online_weight.hpp b/nano/store/lmdb/online_weight.hpp index 7827b36f43..9a1c527f53 100644 --- a/nano/store/lmdb/online_weight.hpp +++ b/nano/store/lmdb/online_weight.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/peer.hpp b/nano/store/lmdb/peer.hpp index 2cbc274ae9..83fd1ee785 100644 --- a/nano/store/lmdb/peer.hpp +++ b/nano/store/lmdb/peer.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/pending.hpp b/nano/store/lmdb/pending.hpp index bc3d7115f8..86fd1c1e5b 100644 --- a/nano/store/lmdb/pending.hpp +++ b/nano/store/lmdb/pending.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/pruned.hpp b/nano/store/lmdb/pruned.hpp index b166f11d7e..0ad8f07e5b 100644 --- a/nano/store/lmdb/pruned.hpp +++ b/nano/store/lmdb/pruned.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/nano/store/lmdb/version.hpp b/nano/store/lmdb/version.hpp index 47300a9d46..ff9b723564 100644 --- a/nano/store/lmdb/version.hpp +++ b/nano/store/lmdb/version.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/lmdb/wallet_value.hpp b/nano/store/lmdb/wallet_value.hpp index 2ef9b1390a..f5e70e54f7 100644 --- a/nano/store/lmdb/wallet_value.hpp +++ b/nano/store/lmdb/wallet_value.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include diff --git a/nano/store/online_weight.cpp b/nano/store/online_weight.cpp new file mode 100644 index 0000000000..230a4f266d --- /dev/null +++ b/nano/store/online_weight.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/online_weight.hpp b/nano/store/online_weight.hpp new file mode 100644 index 0000000000..ee65a9b02e --- /dev/null +++ b/nano/store/online_weight.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages online weight storage and iteration + */ +class online_weight_store +{ +public: + virtual void put (nano::write_transaction const &, uint64_t, nano::amount const &) = 0; + virtual void del (nano::write_transaction const &, uint64_t) = 0; + virtual nano::store_iterator begin (nano::transaction const &) const = 0; + virtual nano::store_iterator rbegin (nano::transaction const &) const = 0; + virtual nano::store_iterator end () const = 0; + virtual size_t count (nano::transaction const &) const = 0; + virtual void clear (nano::write_transaction const &) = 0; +}; +} // namespace nano::store diff --git a/nano/store/peer.cpp b/nano/store/peer.cpp new file mode 100644 index 0000000000..e442b57549 --- /dev/null +++ b/nano/store/peer.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/peer.hpp b/nano/store/peer.hpp new file mode 100644 index 0000000000..64d36796af --- /dev/null +++ b/nano/store/peer.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages peer storage and iteration + */ +class peer_store +{ +public: + virtual void put (nano::write_transaction const & transaction_a, nano::endpoint_key const & endpoint_a) = 0; + virtual void del (nano::write_transaction const & transaction_a, nano::endpoint_key const & endpoint_a) = 0; + virtual bool exists (nano::transaction const & transaction_a, nano::endpoint_key const & endpoint_a) const = 0; + virtual size_t count (nano::transaction const & transaction_a) const = 0; + virtual void clear (nano::write_transaction const & transaction_a) = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; + virtual nano::store_iterator end () const = 0; +}; +} // namespace nano::store diff --git a/nano/store/pending.cpp b/nano/store/pending.cpp new file mode 100644 index 0000000000..dd80f3579b --- /dev/null +++ b/nano/store/pending.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/pending.hpp b/nano/store/pending.hpp new file mode 100644 index 0000000000..8944d2cb76 --- /dev/null +++ b/nano/store/pending.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages pending storage and iteration + */ +class pending_store +{ +public: + virtual void put (nano::write_transaction const &, nano::pending_key const &, nano::pending_info const &) = 0; + virtual void del (nano::write_transaction const &, nano::pending_key const &) = 0; + virtual bool get (nano::transaction const &, nano::pending_key const &, nano::pending_info &) = 0; + virtual bool exists (nano::transaction const &, nano::pending_key const &) = 0; + virtual bool any (nano::transaction const &, nano::account const &) = 0; + virtual nano::store_iterator begin (nano::transaction const &, nano::pending_key const &) const = 0; + virtual nano::store_iterator begin (nano::transaction const &) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/pruned.cpp b/nano/store/pruned.cpp new file mode 100644 index 0000000000..e4342c7b4d --- /dev/null +++ b/nano/store/pruned.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/pruned.hpp b/nano/store/pruned.hpp new file mode 100644 index 0000000000..f9b48a22c9 --- /dev/null +++ b/nano/store/pruned.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages pruned storage and iteration + */ +class pruned_store +{ +public: + virtual void put (nano::write_transaction const & transaction_a, nano::block_hash const & hash_a) = 0; + virtual void del (nano::write_transaction const & transaction_a, nano::block_hash const & hash_a) = 0; + virtual bool exists (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const = 0; + virtual nano::block_hash random (nano::transaction const & transaction_a) = 0; + virtual size_t count (nano::transaction const & transaction_a) const = 0; + virtual void clear (nano::write_transaction const &) = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const = 0; + virtual nano::store_iterator begin (nano::transaction const & transaction_a) const = 0; + virtual nano::store_iterator end () const = 0; + virtual void for_each_par (std::function, nano::store_iterator)> const & action_a) const = 0; +}; +} // namespace nano::store diff --git a/nano/store/rocksdb/account.hpp b/nano/store/rocksdb/account.hpp index 445ee5abed..dfac80e1d0 100644 --- a/nano/store/rocksdb/account.hpp +++ b/nano/store/rocksdb/account.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/block.hpp b/nano/store/rocksdb/block.hpp index cc9ad528dd..21264bd679 100644 --- a/nano/store/rocksdb/block.hpp +++ b/nano/store/rocksdb/block.hpp @@ -1,12 +1,10 @@ #pragma once -#include - -#include +#include +#include namespace nano { -using rocksdb_val = db_val<::rocksdb::Slice>; class block_predecessor_rocksdb_set; namespace rocksdb { diff --git a/nano/store/rocksdb/confirmation_height.hpp b/nano/store/rocksdb/confirmation_height.hpp index a4e7f7260b..abb4668ba4 100644 --- a/nano/store/rocksdb/confirmation_height.hpp +++ b/nano/store/rocksdb/confirmation_height.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/db_val.cpp b/nano/store/rocksdb/db_val.cpp new file mode 100644 index 0000000000..2c56704321 --- /dev/null +++ b/nano/store/rocksdb/db_val.cpp @@ -0,0 +1,28 @@ +#include + +namespace nano +{ +template <> +void * rocksdb_val::data () const +{ + return (void *)value.data (); +} + +template <> +std::size_t rocksdb_val::size () const +{ + return value.size (); +} + +template <> +rocksdb_val::db_val (std::size_t size_a, void * data_a) : + value (static_cast (data_a), size_a) +{ +} + +template <> +void rocksdb_val::convert_buffer_to_value () +{ + value = ::rocksdb::Slice (reinterpret_cast (buffer->data ()), buffer->size ()); +} +} diff --git a/nano/store/rocksdb/db_val.hpp b/nano/store/rocksdb/db_val.hpp new file mode 100644 index 0000000000..c1dd18d295 --- /dev/null +++ b/nano/store/rocksdb/db_val.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include + +namespace nano +{ +using rocksdb_val = db_val<::rocksdb::Slice>; +} diff --git a/nano/store/rocksdb/final_vote.hpp b/nano/store/rocksdb/final_vote.hpp index f8af2c2376..10b8a4ede0 100644 --- a/nano/store/rocksdb/final_vote.hpp +++ b/nano/store/rocksdb/final_vote.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/frontier.hpp b/nano/store/rocksdb/frontier.hpp index d0d346a2bc..e6b68ae95c 100644 --- a/nano/store/rocksdb/frontier.hpp +++ b/nano/store/rocksdb/frontier.hpp @@ -1,6 +1,7 @@ #pragma once -#include +#include +#include namespace nano { diff --git a/nano/store/rocksdb/iterator.hpp b/nano/store/rocksdb/iterator.hpp index a11a5132a6..8732a36678 100644 --- a/nano/store/rocksdb/iterator.hpp +++ b/nano/store/rocksdb/iterator.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include diff --git a/nano/store/rocksdb/online_weight.hpp b/nano/store/rocksdb/online_weight.hpp index fbc9f05690..ab4325328b 100644 --- a/nano/store/rocksdb/online_weight.hpp +++ b/nano/store/rocksdb/online_weight.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/peer.hpp b/nano/store/rocksdb/peer.hpp index 66b6da1918..16dbabf543 100644 --- a/nano/store/rocksdb/peer.hpp +++ b/nano/store/rocksdb/peer.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/pending.hpp b/nano/store/rocksdb/pending.hpp index 256197de51..afd773b02f 100644 --- a/nano/store/rocksdb/pending.hpp +++ b/nano/store/rocksdb/pending.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/pruned.hpp b/nano/store/rocksdb/pruned.hpp index ad8b6e1eba..29c03d7766 100644 --- a/nano/store/rocksdb/pruned.hpp +++ b/nano/store/rocksdb/pruned.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/rocksdb/rocksdb.cpp b/nano/store/rocksdb/rocksdb.cpp index ba37fef785..204157b650 100644 --- a/nano/store/rocksdb/rocksdb.cpp +++ b/nano/store/rocksdb/rocksdb.cpp @@ -1,7 +1,8 @@ #include -#include #include +#include #include +#include #include #include @@ -34,33 +35,6 @@ class event_listener : public rocksdb::EventListener }; } -namespace nano -{ -template <> -void * rocksdb_val::data () const -{ - return (void *)value.data (); -} - -template <> -std::size_t rocksdb_val::size () const -{ - return value.size (); -} - -template <> -rocksdb_val::db_val (std::size_t size_a, void * data_a) : - value (static_cast (data_a), size_a) -{ -} - -template <> -void rocksdb_val::convert_buffer_to_value () -{ - value = ::rocksdb::Slice (reinterpret_cast (buffer->data ()), buffer->size ()); -} -} - nano::rocksdb::store::store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, nano::ledger_constants & constants, nano::rocksdb_config const & rocksdb_config_a, bool open_read_only_a) : // clang-format off nano::store::component{ diff --git a/nano/store/rocksdb/version.hpp b/nano/store/rocksdb/version.hpp index 73d69bc765..4aaf0e0d5f 100644 --- a/nano/store/rocksdb/version.hpp +++ b/nano/store/rocksdb/version.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace nano { diff --git a/nano/store/tables.hpp b/nano/store/tables.hpp new file mode 100644 index 0000000000..e0a03bd020 --- /dev/null +++ b/nano/store/tables.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include + +namespace nano +{ +// Keep this in alphabetical order +enum class tables +{ + accounts, + blocks, + confirmation_height, + default_unused, // RocksDB only + final_votes, + frontiers, + meta, + online_weight, + peers, + pending, + pruned, + vote +}; +} // namespace nano + +namespace std +{ +template <> +struct hash<::nano::tables> +{ + size_t operator() (::nano::tables const & table_a) const + { + return static_cast (table_a); + } +}; // struct hash +} // namespace std diff --git a/nano/store/transaction.cpp b/nano/store/transaction.cpp new file mode 100644 index 0000000000..ed822381d1 --- /dev/null +++ b/nano/store/transaction.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +nano::read_transaction::read_transaction (std::unique_ptr read_transaction_impl) : + impl (std::move (read_transaction_impl)) +{ +} + +void * nano::read_transaction::get_handle () const +{ + return impl->get_handle (); +} + +void nano::read_transaction::reset () const +{ + impl->reset (); +} + +void nano::read_transaction::renew () const +{ + impl->renew (); +} + +void nano::read_transaction::refresh () const +{ + reset (); + renew (); +} + +nano::write_transaction::write_transaction (std::unique_ptr write_transaction_impl) : + impl (std::move (write_transaction_impl)) +{ + /* + * For IO threads, we do not want them to block on creating write transactions. + */ + debug_assert (nano::thread_role::get () != nano::thread_role::name::io); +} + +void * nano::write_transaction::get_handle () const +{ + return impl->get_handle (); +} + +void nano::write_transaction::commit () +{ + impl->commit (); +} + +void nano::write_transaction::renew () +{ + impl->renew (); +} + +void nano::write_transaction::refresh () +{ + impl->commit (); + impl->renew (); +} + +bool nano::write_transaction::contains (nano::tables table_a) const +{ + return impl->contains (table_a); +} diff --git a/nano/store/transaction.hpp b/nano/store/transaction.hpp new file mode 100644 index 0000000000..e8c46415b3 --- /dev/null +++ b/nano/store/transaction.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include + +#include + +namespace nano +{ +class transaction_impl +{ +public: + virtual ~transaction_impl () = default; + virtual void * get_handle () const = 0; +}; + +class read_transaction_impl : public transaction_impl +{ +public: + virtual void reset () = 0; + virtual void renew () = 0; +}; + +class write_transaction_impl : public transaction_impl +{ +public: + virtual void commit () = 0; + virtual void renew () = 0; + virtual bool contains (nano::tables table_a) const = 0; +}; + +class transaction +{ +public: + virtual ~transaction () = default; + virtual void * get_handle () const = 0; +}; + +/** + * RAII wrapper of a read MDB_txn where the constructor starts the transaction + * and the destructor aborts it. + */ +class read_transaction final : public transaction +{ +public: + explicit read_transaction (std::unique_ptr read_transaction_impl); + void * get_handle () const override; + void reset () const; + void renew () const; + void refresh () const; + +private: + std::unique_ptr impl; +}; + +/** + * RAII wrapper of a read-write MDB_txn where the constructor starts the transaction + * and the destructor commits it. + */ +class write_transaction final : public transaction +{ +public: + explicit write_transaction (std::unique_ptr write_transaction_impl); + void * get_handle () const override; + void commit (); + void renew (); + void refresh (); + bool contains (nano::tables table_a) const; + +private: + std::unique_ptr impl; +}; +} // namespace nano diff --git a/nano/store/version.cpp b/nano/store/version.cpp new file mode 100644 index 0000000000..d8917be835 --- /dev/null +++ b/nano/store/version.cpp @@ -0,0 +1 @@ +#include diff --git a/nano/store/version.hpp b/nano/store/version.hpp new file mode 100644 index 0000000000..192cb032a8 --- /dev/null +++ b/nano/store/version.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#include + +namespace nano +{ +class block_hash; +class read_transaction; +class transaction; +class write_transaction; +} +namespace nano +{ +/** + * Manages version storage + */ +class version_store +{ +public: + virtual void put (nano::write_transaction const &, int) = 0; + virtual int get (nano::transaction const &) const = 0; +}; + +} // namespace nano::store diff --git a/nano/store/versioning.hpp b/nano/store/versioning.hpp index 429de22d97..45a3ad885e 100644 --- a/nano/store/versioning.hpp +++ b/nano/store/versioning.hpp @@ -71,4 +71,12 @@ class block_sideband_v18 final uint64_t timestamp{ 0 }; nano::block_details details; }; +// Move to versioning with a specific version if required for a future upgrade +template +class block_w_sideband_v18 +{ +public: + std::shared_ptr block; + nano::block_sideband_v18 sideband; +}; }