Skip to content
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
3 changes: 3 additions & 0 deletions builds/gnu/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ include_bitcoin_database_impl_primitives_HEADERS = \
${srcdir}/../../include/bitcoin/database/impl/primitives/hashhead.ipp \
${srcdir}/../../include/bitcoin/database/impl/primitives/hashmap.ipp \
${srcdir}/../../include/bitcoin/database/impl/primitives/hashmaps.ipp \
${srcdir}/../../include/bitcoin/database/impl/primitives/headmap.ipp \
${srcdir}/../../include/bitcoin/database/impl/primitives/iterator.ipp \
${srcdir}/../../include/bitcoin/database/impl/primitives/keys.ipp \
${srcdir}/../../include/bitcoin/database/impl/primitives/linkage.ipp \
Expand Down Expand Up @@ -249,6 +250,7 @@ include_bitcoin_database_primitives_HEADERS = \
${srcdir}/../../include/bitcoin/database/primitives/hashhead.hpp \
${srcdir}/../../include/bitcoin/database/primitives/hashmap.hpp \
${srcdir}/../../include/bitcoin/database/primitives/hashmaps.hpp \
${srcdir}/../../include/bitcoin/database/primitives/headmap.hpp \
${srcdir}/../../include/bitcoin/database/primitives/iterator.hpp \
${srcdir}/../../include/bitcoin/database/primitives/keys.hpp \
${srcdir}/../../include/bitcoin/database/primitives/linkage.hpp \
Expand Down Expand Up @@ -371,6 +373,7 @@ test_libbitcoin_database_test_SOURCES = \
${srcdir}/../../test/primitives/hashhead.cpp \
${srcdir}/../../test/primitives/hashmap.cpp \
${srcdir}/../../test/primitives/hashmaps.cpp \
${srcdir}/../../test/primitives/headmap.cpp \
${srcdir}/../../test/primitives/iterator.cpp \
${srcdir}/../../test/primitives/keys.cpp \
${srcdir}/../../test/primitives/linkage.cpp \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<ClCompile Include="..\..\..\..\test\primitives\hashhead.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\hashmap.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\hashmaps.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\headmap.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\iterator.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\keys.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\linkage.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<ClCompile Include="..\..\..\..\test\primitives\hashmaps.cpp">
<Filter>src\primitives</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\primitives\headmap.cpp">
<Filter>src\primitives</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\primitives\iterator.cpp">
<Filter>src\primitives</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashhead.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashmap.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashmaps.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\headmap.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\iterator.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\keys.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\linkage.hpp" />
Expand Down Expand Up @@ -236,6 +237,7 @@
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashhead.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashmap.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashmaps.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\headmap.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\iterator.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\keys.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\linkage.ipp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashmaps.hpp">
<Filter>include\bitcoin\database\primitives</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\headmap.hpp">
<Filter>include\bitcoin\database\primitives</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\iterator.hpp">
<Filter>include\bitcoin\database\primitives</Filter>
</ClInclude>
Expand Down Expand Up @@ -421,6 +424,9 @@
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashmaps.ipp">
<Filter>include\bitcoin\database\impl\primitives</Filter>
</None>
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\headmap.ipp">
<Filter>include\bitcoin\database\impl\primitives</Filter>
</None>
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\iterator.ipp">
<Filter>include\bitcoin\database\impl\primitives</Filter>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<ClCompile Include="..\..\..\..\test\primitives\hashhead.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\hashmap.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\hashmaps.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\headmap.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\iterator.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\keys.cpp" />
<ClCompile Include="..\..\..\..\test\primitives\linkage.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<ClCompile Include="..\..\..\..\test\primitives\hashmaps.cpp">
<Filter>src\primitives</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\primitives\headmap.cpp">
<Filter>src\primitives</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\primitives\iterator.cpp">
<Filter>src\primitives</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashhead.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashmap.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashmaps.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\headmap.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\iterator.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\keys.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\linkage.hpp" />
Expand Down Expand Up @@ -236,6 +237,7 @@
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashhead.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashmap.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashmaps.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\headmap.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\iterator.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\keys.ipp" />
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\linkage.ipp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\hashmaps.hpp">
<Filter>include\bitcoin\database\primitives</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\headmap.hpp">
<Filter>include\bitcoin\database\primitives</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\database\primitives\iterator.hpp">
<Filter>include\bitcoin\database\primitives</Filter>
</ClInclude>
Expand Down Expand Up @@ -421,6 +424,9 @@
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\hashmaps.ipp">
<Filter>include\bitcoin\database\impl\primitives</Filter>
</None>
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\headmap.ipp">
<Filter>include\bitcoin\database\impl\primitives</Filter>
</None>
<None Include="..\..\..\..\include\bitcoin\database\impl\primitives\iterator.ipp">
<Filter>include\bitcoin\database\impl\primitives</Filter>
</None>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <bitcoin/database/primitives/hashhead.hpp>
#include <bitcoin/database/primitives/hashmap.hpp>
#include <bitcoin/database/primitives/hashmaps.hpp>
#include <bitcoin/database/primitives/headmap.hpp>
#include <bitcoin/database/primitives/iterator.hpp>
#include <bitcoin/database/primitives/keys.hpp>
#include <bitcoin/database/primitives/linkage.hpp>
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/database/impl/memory/mmap_staging.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ bool CLASS::lazy_install_() NOEXCEPT

// Commit the remainder to the commitment target and populate the tail.
const auto target = std::max(page_ceiling(to_width<zero>(
capacity_.load())), page_ceiling(logical));
to_commitment())), page_ceiling(logical));
if ((target > floor) && (mmap_commit(std::next(memory_map_[zero], floor),
target - floor, headroom_) == fail))
{
Expand All @@ -538,7 +538,7 @@ bool CLASS::lazy_install_() NOEXCEPT
bit_all<uint64_t> : unmask_right<uint64_t>(flags - first));
}

engaged_.store(true);
lazy_.store(true);

// Attribute the anonymous span for diagnostics (smaps decomposition).
mmap_name(std::next(memory_map_[zero], floor), reserved - floor,
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/database/impl/memory/mmap_storage.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void CLASS::prepare(size_t STAGING_ONLY(offset),
// in flight across passes; the count persists until mark.
writers_.fetch_add(one);

if (!engaged_.load(relaxed))
if (!engaged_.load(relaxed) && !lazy_.load(relaxed))
return;

// Declare intent before the write (sequentially consistent, pairing with
Expand Down
213 changes: 213 additions & 0 deletions include/bitcoin/database/impl/primitives/headmap.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
* Copyright (c) 2011-2026 libbitcoin developers
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_DATABASE_PRIMITIVES_HEADMAP_IPP
#define LIBBITCOIN_DATABASE_PRIMITIVES_HEADMAP_IPP

#include <bitcoin/database/define.hpp>

namespace libbitcoin {
namespace database {

TEMPLATE
CLASS::headmap(storage& head) NOEXCEPT
: file_(head)
{
}

// not thread safe
// ----------------------------------------------------------------------------

TEMPLATE
bool CLASS::create() NOEXCEPT
{
// An empty head is a zero count.
return is_zero(head_size());
}

TEMPLATE
bool CLASS::close() NOEXCEPT
{
// Count is the head logical size.
return true;
}

TEMPLATE
bool CLASS::backup(bool) NOEXCEPT
{
// Count is the head logical size.
return true;
}

TEMPLATE
bool CLASS::restore() NOEXCEPT
{
// Content is fully restored with the head, so there is nothing to prune.
return verify();
}

TEMPLATE
bool CLASS::verify() const NOEXCEPT
{
return is_zero(head_size() % bucket_size);
}

// sizing
// ----------------------------------------------------------------------------

TEMPLATE
size_t CLASS::head_size() const NOEXCEPT
{
return file_.size();
}

TEMPLATE
size_t CLASS::body_size() const NOEXCEPT
{
// There is no body (all content is in the head).
return zero;
}

TEMPLATE
Link CLASS::count() const NOEXCEPT
{
return position_to_link(head_size());
}

TEMPLATE
bool CLASS::truncate(const Link& count) NOEXCEPT
{
if (count.is_terminal())
return false;

return file_.truncate(link_to_position(count));
}

TEMPLATE
bool CLASS::reserve(const Link& count) NOEXCEPT
{
if (count.is_terminal())
return false;

return file_.reserve(link_to_position(count));
}

// error condition
// ----------------------------------------------------------------------------

TEMPLATE
code CLASS::get_fault() const NOEXCEPT
{
return file_.get_fault();
}

TEMPLATE
size_t CLASS::get_space() const NOEXCEPT
{
return file_.get_space();
}

TEMPLATE
code CLASS::reload() NOEXCEPT
{
return error::success;
}

// query interface
// ----------------------------------------------------------------------------

TEMPLATE
Link CLASS::at(size_t index) const NOEXCEPT
{
using namespace system;

// Buckets at or above the logical size are unallocated (count publishes).
if (index >= count())
return {};

const auto ptr = file_.get(link_to_position(index));
if (!ptr)
return {};

const auto raw = ptr.data();
if constexpr (aligned)
{
// Reads full padded word (masked by to_link).
const auto& head = *pointer_cast<std::atomic<cell>>(raw);
return to_link(head.load(std::memory_order_relaxed));
}
else
{
const auto& head = cell_array(raw);
cell value{};

mutex_.lock_shared();
cell_array(value) = head;
mutex_.unlock_shared();

return to_link(value);
}
}

// NOT WRITER-WRITER THREAD SAFE (the logical top is read-write).
TEMPLATE
bool CLASS::push(const Link& link) NOEXCEPT
{
using namespace system;
if (link.is_terminal())
return false;

const auto position = head_size();

// Dispose accessor (lock) before allocate.
{
const auto ptr = file_.get_capacity(position);
if (!ptr || (ptr.size() < possible_narrow_sign_cast<ptrdiff_t>(
bucket_size)))
return false;

// Writes into unpublished capacity.
file_.prepare(position, bucket_size);
const auto raw = ptr.data();
cell value = link.value;
if constexpr (aligned)
{
// Writes full padded word.
auto& head = *pointer_cast<std::atomic<cell>>(raw);
head.store(value, std::memory_order_relaxed);
}
else
{
auto& head = cell_array(raw);

mutex_.lock();
head = cell_array(value);
mutex_.unlock();
}

file_.mark(position, bucket_size);
}

// Publication of the pre-written bucket is its allocation.
return file_.allocate(bucket_size) != storage::eof;
}

} // namespace database
} // namespace libbitcoin

#endif
Loading