From a731c9e92014c4bdaf15fd4cff9f952ae2e734b4 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Tue, 11 Jun 2024 09:30:10 -0400 Subject: [PATCH] Style, move assertive checks into debug. --- .../database/impl/primitives/arraymap.ipp | 4 +- .../database/impl/primitives/hashmap.ipp | 44 ++++++++++--------- .../bitcoin/database/primitives/hashmap.hpp | 5 +-- test/primitives/arraymap.cpp | 2 +- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/include/bitcoin/database/impl/primitives/arraymap.ipp b/include/bitcoin/database/impl/primitives/arraymap.ipp index 26628c66..d2ee2235 100644 --- a/include/bitcoin/database/impl/primitives/arraymap.ipp +++ b/include/bitcoin/database/impl/primitives/arraymap.ipp @@ -138,7 +138,7 @@ bool CLASS::get(const Link& link, Element& element) const NOEXCEPT iostream stream{ *ptr }; reader source{ stream }; - if constexpr (!is_slab) { source.set_limit(Size); } + if constexpr (!is_slab) { DEBUG_ONLY(source.set_limit(Size)); } return element.from_data(source); } @@ -163,7 +163,7 @@ bool CLASS::put_link(Link& link, const Element& element) NOEXCEPT iostream stream{ *ptr }; flipper sink{ stream }; - if constexpr (!is_slab) { sink.set_limit(Size * count); } + if constexpr (!is_slab) { DEBUG_ONLY(sink.set_limit(Size * count)); } return element.to_data(sink); } diff --git a/include/bitcoin/database/impl/primitives/hashmap.ipp b/include/bitcoin/database/impl/primitives/hashmap.ipp index 06c926aa..c8c54f8f 100644 --- a/include/bitcoin/database/impl/primitives/hashmap.ipp +++ b/include/bitcoin/database/impl/primitives/hashmap.ipp @@ -38,8 +38,8 @@ TEMPLATE bool CLASS::create() NOEXCEPT { Link count{}; - return head_.create() && - head_.get_body_count(count) && manager_.truncate(count); + return head_.create() && head_.get_body_count(count) && + manager_.truncate(count); } TEMPLATE @@ -58,16 +58,16 @@ TEMPLATE bool CLASS::restore() NOEXCEPT { Link count{}; - return head_.verify() && - head_.get_body_count(count) && manager_.truncate(count); + return head_.verify() && head_.get_body_count(count) && + manager_.truncate(count); } TEMPLATE bool CLASS::verify() const NOEXCEPT { Link count{}; - return head_.verify() && - head_.get_body_count(count) && count == manager_.count(); + return head_.verify() && head_.get_body_count(count) && + (count == manager_.count()); } // sizing @@ -130,7 +130,10 @@ code CLASS::reload() NOEXCEPT TEMPLATE Link CLASS::top(const Link& link) const NOEXCEPT { - return link < head_.buckets() ? head_.top(link) : Link{}; + if (link >= head_.buckets()) + return {}; + + return head_.top(link); } TEMPLATE @@ -165,12 +168,11 @@ TEMPLATE Key CLASS::get_key(const Link& link) NOEXCEPT { const auto ptr = manager_.get(link); - - // As with link, search key is presumed valid (otherwise null array). - if (!ptr || system::is_lesser(ptr->size(), Link::size + key_size)) + if (!ptr) return {}; - return system::unsafe_array_cast(std::next( + BC_ASSERT(!system::is_lesser(ptr->size(), meta_size)); + return system::unsafe_array_cast>(std::next( ptr->begin(), Link::size)); } @@ -185,9 +187,9 @@ bool CLASS::get(const Link& link, Element& element) const NOEXCEPT iostream stream{ *ptr }; reader source{ stream }; - source.skip_bytes(Link::size + key_size); + source.skip_bytes(meta_size); - if constexpr (!is_slab) { source.set_limit(Size); } + if constexpr (!is_slab) { DEBUG_ONLY(source.set_limit(Size);) } return element.from_data(source); } @@ -203,9 +205,9 @@ bool CLASS::get(const iterator& it, Element& element) const NOEXCEPT const auto buffer = ptr->offset(iterator::link_to_position(it.self())); iostream stream{ buffer, buffer_size }; reader source{ stream }; - source.skip_bytes(Link::size + key_size); + source.skip_bytes(meta_size); - if constexpr (!is_slab) { source.set_limit(Size); } + if constexpr (!is_slab) { DEBUG_ONLY(source.set_limit(Size);) } return element.from_data(source); } @@ -220,9 +222,9 @@ bool CLASS::set(const Link& link, const Element& element) NOEXCEPT iostream stream{ *ptr }; finalizer sink{ stream }; - sink.skip_bytes(Link::size + key_size); + sink.skip_bytes(meta_size); - if constexpr (!is_slab) { sink.set_limit(Size); } + if constexpr (!is_slab) { DEBUG_ONLY(sink.set_limit(Size)); } return element.to_data(sink); } @@ -278,7 +280,7 @@ bool CLASS::put_link(Link& link, const Key& key, return head_.push(link, next, index); }); - if constexpr (!is_slab) { sink.set_limit(Size * count); } + if constexpr (!is_slab) { DEBUG_ONLY(sink.set_limit(Size * count)); } return element.to_data(sink) && sink.finalize(); } @@ -310,7 +312,7 @@ bool CLASS::put(const Link& link, const Key& key, return head_.push(link, next, index); }); - if constexpr (!is_slab) { sink.set_limit(Size * count); } + if constexpr (!is_slab) { DEBUG_ONLY(sink.set_limit(Size * count)); } return element.to_data(sink) && sink.finalize(); } @@ -322,8 +324,8 @@ bool CLASS::commit(const Link& link, const Key& key) NOEXCEPT return false; // Set element search key. - system::unsafe_array_cast(std::next(ptr->begin(), - Link::size)) = key; + system::unsafe_array_cast>( + std::next(ptr->begin(), Link::size)) = key; // Commit element to search index. auto& next = system::unsafe_array_cast(ptr->begin()); diff --git a/include/bitcoin/database/primitives/hashmap.hpp b/include/bitcoin/database/primitives/hashmap.hpp index 5f687d5f..05c9e1b8 100644 --- a/include/bitcoin/database/primitives/hashmap.hpp +++ b/include/bitcoin/database/primitives/hashmap.hpp @@ -144,11 +144,10 @@ class hashmap Link commit_link(const Link& link, const Key& key) NOEXCEPT; private: - static constexpr auto key_size = array_count; static constexpr auto is_slab = (Size == max_size_t); + static constexpr auto meta_size = Link::size + array_count; static constexpr auto buffer_size = is_slab ? system::maximum : - system::possible_narrow_and_sign_cast( - Size + Link::size + key_size); + system::possible_narrow_and_sign_cast(Size + meta_size); using head = database::head; using manager = database::manager; diff --git a/test/primitives/arraymap.cpp b/test/primitives/arraymap.cpp index 2832ccec..3b571389 100644 --- a/test/primitives/arraymap.cpp +++ b/test/primitives/arraymap.cpp @@ -54,7 +54,7 @@ class arraymap_ //// //// iostream stream{ *ptr }; //// const auto sink = std::make_shared(stream); - //// if constexpr (!is_slab) { sink.set_limit(Size * size); } + //// if constexpr (!is_slab) { DEBUG_ONLY(sink.set_limit(Size * size)); } //// return sink; ////} };