Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove spend links from puts table and allocate contiguously. #565

Merged
merged 20 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/bitcoin/database/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ enum error_t : uint8_t
integrity7,
integrity8,
integrity9,
integrity10,
integrity11,

/// memory map
open_open,
Expand Down Expand Up @@ -116,12 +118,13 @@ enum error_t : uint8_t
tx_tx_allocate,
tx_spend_allocate,
tx_input_put,
tx_point_allocate,
tx_point_put,
tx_spend_set,
tx_output_put,
tx_puts_put,
tx_tx_set,
tx_spend_commit,
tx_spend_put,
tx_address_allocate,
tx_address_put,
tx_tx_commit,

Expand Down
5 changes: 2 additions & 3 deletions include/bitcoin/database/impl/primitives/arraymap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ template <typename Element, if_equal<Element::size, Size>>
bool CLASS::put(size_t key, const Element& element) NOEXCEPT
{
using namespace system;
const auto count = element.count();
const auto link = body_.allocate(count);
const auto link = body_.allocate(element.count());
const auto ptr = body_.get(link);
if (!ptr)
return false;
Expand All @@ -169,7 +168,7 @@ bool CLASS::put(size_t key, const Element& element) NOEXCEPT
iostream stream{ *ptr };
finalizer sink{ stream };

if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * count);) }
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
return element.to_data(sink) && head_.push(link, head_.putter_index(key));
}

Expand Down
25 changes: 16 additions & 9 deletions include/bitcoin/database/impl/primitives/hashmap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,22 @@ Key CLASS::get_key(const Link& link) NOEXCEPT

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
bool CLASS::find(const Key& key, Element& element) const NOEXCEPT
inline bool CLASS::find(const Key& key, Element& element) const NOEXCEPT
{
return !find_link(key, element).is_terminal();
}

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
inline Link CLASS::find_link(const Key& key, Element& element) const NOEXCEPT
{
// This override avoids duplicated memory_ptr construct in get(first()).
const auto ptr = get_memory();
return read(ptr, first(ptr, head_.top(key), key), element);
const auto link = first(ptr, head_.top(key), key);
if (link.is_terminal())
return {};

return read(ptr, link, element);
}

TEMPLATE
Expand Down Expand Up @@ -244,7 +255,7 @@ bool CLASS::set(const Link& link, const Element& element) NOEXCEPT
finalizer sink{ stream };
sink.skip_bytes(index_size);

if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size);) }
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
return element.to_data(sink);
}

Expand Down Expand Up @@ -395,7 +406,7 @@ bool CLASS::read(const memory_ptr& ptr, const Link& link,
reader source{ stream };
source.skip_bytes(index_size);

if constexpr (!is_slab) { BC_DEBUG_ONLY(source.set_limit(Size);) }
if constexpr (!is_slab) { BC_DEBUG_ONLY(source.set_limit(Size * element.count());) }
return element.from_data(source);
}

Expand Down Expand Up @@ -427,11 +438,7 @@ bool CLASS::write(const memory_ptr& ptr, const Link& link, const Key& key,
sink.skip_bytes(Link::size);
sink.write_bytes(key);

if constexpr (!is_slab)
{
BC_DEBUG_ONLY(sink.set_limit(Size * element.count());)
}

if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
auto& next = unsafe_array_cast<uint8_t, Link::size>(offset);
return element.to_data(sink) && head_.push(link, next, head_.index(key));
}
Expand Down
60 changes: 51 additions & 9 deletions include/bitcoin/database/impl/primitives/nomap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,55 @@ code CLASS::reload() NOEXCEPT
// query interface
// ----------------------------------------------------------------------------

TEMPLATE
inline Link CLASS::allocate(const Link& size) NOEXCEPT
{
return manager_.allocate(size);
}

TEMPLATE
inline memory_ptr CLASS::get_memory() const NOEXCEPT
{
return manager_.get();
}

// static
TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
bool CLASS::get(const Link& link, Element& element) const NOEXCEPT
bool CLASS::get(const memory_ptr& ptr, const Link& link,
Element& element) NOEXCEPT
{
using namespace system;
const auto ptr = manager_.get(link);
if (!ptr)
if (!ptr || link.is_terminal())
return false;

iostream stream{ *ptr };
const auto start = manager::link_to_position(link);
if (is_limited<ptrdiff_t>(start))
return false;

const auto size = ptr->size();
const auto position = possible_narrow_and_sign_cast<ptrdiff_t>(start);
if (position > size)
return false;

const auto offset = ptr->offset(start);
if (is_null(offset))
return false;

iostream stream{ offset, size - position };
reader source{ stream };
if constexpr (!is_slab) { source.set_limit(Size); }

if constexpr (!is_slab) { BC_DEBUG_ONLY(source.set_limit(Size * element.count());) }
return element.from_data(source);
}

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
inline bool CLASS::get(const Link& link, Element& element) const NOEXCEPT
{
return get(get_memory(), link, element);
}

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
inline bool CLASS::put(const Element& element) NOEXCEPT
Expand All @@ -152,21 +186,29 @@ inline bool CLASS::put(const Element& element) NOEXCEPT

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
bool CLASS::put_link(Link& link, const Element& element) NOEXCEPT
bool CLASS::put(const Link& link, const Element& element) NOEXCEPT
{
using namespace system;
const auto count = element.count();
link = manager_.allocate(count);
const auto ptr = manager_.get(link);
if (!ptr)
return false;

iostream stream{ *ptr };
flipper sink{ stream };
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * count);) }

if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
return element.to_data(sink);
}

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
inline bool CLASS::put_link(Link& link, const Element& element) NOEXCEPT
{
const auto count = element.count();
link = manager_.allocate(count);
return put(link, element);
}

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
inline Link CLASS::put_link(const Element& element) NOEXCEPT
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/database/impl/query/archive_read.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ inline hash_digest CLASS::get_tx_key(const tx_link& link) const NOEXCEPT
TEMPLATE
inline hash_digest CLASS::get_point_key(const point_link& link) const NOEXCEPT
{
table::point::record point{};
table::point::get_key point{};
if (!store_.point.get(link, point))
return {};

Expand Down
Loading
Loading