Skip to content

Commit

Permalink
Style, move assertive checks into debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 11, 2024
1 parent 6fb504d commit a731c9e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions include/bitcoin/database/impl/primitives/arraymap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
44 changes: 23 additions & 21 deletions include/bitcoin/database/impl/primitives/hashmap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<uint8_t, key_size>(std::next(
BC_ASSERT(!system::is_lesser(ptr->size(), meta_size));
return system::unsafe_array_cast<uint8_t, array_count<Key>>(std::next(
ptr->begin(), Link::size));
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -322,8 +324,8 @@ bool CLASS::commit(const Link& link, const Key& key) NOEXCEPT
return false;

// Set element search key.
system::unsafe_array_cast<uint8_t, key_size>(std::next(ptr->begin(),
Link::size)) = key;
system::unsafe_array_cast<uint8_t, array_count<Key>>(
std::next(ptr->begin(), Link::size)) = key;

// Commit element to search index.
auto& next = system::unsafe_array_cast<uint8_t, Link::size>(ptr->begin());
Expand Down
5 changes: 2 additions & 3 deletions include/bitcoin/database/primitives/hashmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ class hashmap
Link commit_link(const Link& link, const Key& key) NOEXCEPT;

private:
static constexpr auto key_size = array_count<Key>;
static constexpr auto is_slab = (Size == max_size_t);
static constexpr auto meta_size = Link::size + array_count<Key>;
static constexpr auto buffer_size = is_slab ? system::maximum<ptrdiff_t> :
system::possible_narrow_and_sign_cast<ptrdiff_t>(
Size + Link::size + key_size);
system::possible_narrow_and_sign_cast<ptrdiff_t>(Size + meta_size);

using head = database::head<Link, Key, Hash>;
using manager = database::manager<Link, Key, Size>;
Expand Down
2 changes: 1 addition & 1 deletion test/primitives/arraymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class arraymap_
////
//// iostream<memory> stream{ *ptr };
//// const auto sink = std::make_shared<database::writer>(stream);
//// if constexpr (!is_slab) { sink.set_limit(Size * size); }
//// if constexpr (!is_slab) { DEBUG_ONLY(sink.set_limit(Size * size)); }
//// return sink;
////}
};
Expand Down

0 comments on commit a731c9e

Please sign in to comment.