40# ifndef TPL_FILE_B_TREE_H
41# define TPL_FILE_B_TREE_H
51# include <type_traits>
151 template <
typename Key,
158 static_assert(
MinDegree >= 2,
"File_B_Tree requires MinDegree >= 2");
159 static_assert(std::default_initializable<Key>,
160 "File_B_Tree requires default-initializable keys");
161 static_assert(std::copy_constructible<Key>
and std::is_copy_assignable_v<Key>,
162 "File_B_Tree requires copyable keys");
164 "File_B_Tree requires a fixed-size key codec");
165 static_assert(std::same_as<
decltype(Codec::decode(
166 static_cast<const unsigned char *
>(
nullptr))),
168 "File_B_Tree codec must decode back to Key");
180 static_cast<std::uint32_t
>(Codec::encoded_size);
183 std::endian::native == std::endian::little ? 1 : 2;
244 return "File_B_Tree";
263 <<
"(): file was opened in read-only mode";
290 +
sizeof(std::uint64_t) * 5 +
sizeof(std::uint8_t) + 7;
305 return sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
323 return sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
324 +
sizeof(
page_id_t) +
sizeof(std::uint64_t)
325 + Codec::encoded_size *
MaxKeys
335 const std::uint32_t version)
noexcept
343 const std::uint32_t version)
noexcept
352 const std::uint32_t version)
noexcept
358 const std::uint32_t version)
noexcept
366 const std::uint32_t version)
noexcept
370 :
static_cast<std::uint32_t
>(
sizeof(Key));
373 [[
nodiscard]]
static constexpr std::array<char, 7>
379 const auto codec_id = Codec::storage_id;
391 page.
kind =
static_cast<std::uint8_t
>(kind);
397 const std::uint32_t version)
399 if constexpr (
not std::is_trivially_copyable_v<Key>)
402 <<
" uses legacy native format version " << version
403 <<
", which requires trivially copyable keys; reopen or rewrite it "
412 if constexpr (
not std::is_trivially_copyable_v<Key>)
415 <<
" uses legacy native format version " <<
wal_version
416 <<
", which requires trivially copyable keys; recover it with a "
417 <<
"trivially copyable instantiation or discard the stale WAL";
451 namespace fs = std::filesystem;
454 if (
not path.has_parent_path())
458 (
void) fs::create_directories(path.parent_path(),
ec);
460 <<
"File_B_Tree: cannot create directory "
461 << path.parent_path().string() <<
" for " <<
file_path_
462 <<
" (" <<
ec.message() <<
")";
478 std::ofstream create(
file_path_, std::ios::binary | std::ios::trunc);
480 <<
"File_B_Tree: cannot create " <<
file_path_;
484 ? (std::ios::binary | std::ios::in)
485 : (std::ios::binary | std::ios::in | std::ios::out);
500 template <
typename T>
506 out.write(
reinterpret_cast<const char *
>(&value),
sizeof(value));
508 <<
"File_B_Tree: cannot write " <<
what <<
" to " <<
file_path;
511 template <
typename T>
517 in.read(
reinterpret_cast<char *
>(&value),
sizeof(value));
519 <<
"File_B_Tree: cannot read " <<
what <<
" from " <<
file_path;
528 if constexpr (std::is_trivially_copyable_v<Key>)
530 const auto bytes = std::bit_cast<std::array<
char,
sizeof(Key)>>(key);
531 out.write(bytes.data(), bytes.size());
533 <<
"File_B_Tree: cannot write " <<
what <<
" to " <<
file_path;
537 <<
"File_B_Tree: cannot write legacy native " <<
what <<
" to "
538 <<
file_path <<
" because legacy native formats require trivially "
547 std::array<unsigned char, Codec::encoded_size> bytes = {};
548 Codec::encode(key, bytes.data());
549 out.write(
reinterpret_cast<const char *
>(bytes.data()), bytes.size());
551 <<
"File_B_Tree: cannot write " <<
what <<
" to " <<
file_path;
566 if constexpr (std::is_trivially_copyable_v<Key>)
568 std::array<
char,
sizeof(Key)> bytes = {};
569 in.read(bytes.data(), bytes.size());
571 <<
"File_B_Tree: cannot read " <<
what <<
" from " <<
file_path;
572 return std::bit_cast<Key>(bytes);
577 <<
"File_B_Tree: cannot read legacy native " <<
what <<
" from "
578 <<
file_path <<
" because legacy native formats require trivially "
588 std::array<unsigned char, Codec::encoded_size> bytes = {};
589 in.read(
reinterpret_cast<char *
>(bytes.data()), bytes.size());
591 <<
"File_B_Tree: cannot read " <<
what <<
" from " <<
file_path;
592 return Codec::decode(bytes.data());
598 const std::uint32_t version)
606 const std::uint32_t
crc,
609 return Codec::add_to_crc(
crc, key);
613 const std::uint32_t
crc,
616 if constexpr (std::is_trivially_copyable_v<Key>)
621 <<
"File_B_Tree: cannot checksum legacy native keys because "
622 <<
"legacy native formats require trivially copyable keys";
628 const std::uint32_t version,
629 const std::uint64_t
size,
649 const std::uint32_t version,
650 const std::uint64_t
size,
674 const std::array<char, 3>
padding = {};
681 for (
size_t i = 0; i <
MaxKeys; ++i)
690 const std::array<char, 3>
padding = {};
698 for (
size_t i = 0; i <
MaxKeys; ++i)
707 const std::array<char, 3>
padding = {};
715 for (
size_t i = 0; i <
MaxKeys; ++i)
726 const std::array<char, 3>
padding = {};
735 for (
size_t i = 0; i <
MaxKeys; ++i)
747 const std::array<char, 3>
padding = {};
756 for (
size_t i = 0; i <
MaxKeys; ++i)
768 const std::array<char, 3>
padding = {};
776 for (
size_t i = 0; i <
MaxKeys; ++i)
791 const std::array<char, 3>
padding = {};
794 <<
"File_B_Tree: cannot write page padding to " <<
target_path;
798 "page checkpoint sequence");
800 for (
size_t i = 0; i <
MaxKeys; ++i)
814 <<
"File_B_Tree: cannot seek for writing in " <<
file_path_;
831 file_.write(magic.data(), magic.size());
833 <<
"File_B_Tree: cannot write magic to " <<
file_path_;
841 "header minimum degree");
846 <<
"File_B_Tree: cannot write reserved header bytes to " <<
file_path_;
852 "header checkpoint sequence");
862 static_cast<std::uint64_t
>(
pages_.size() - 1),
885 +
sizeof(std::uint64_t) * 7 +
sizeof(std::uint8_t) + 7;
888 [[
nodiscard]]
static constexpr std::uint32_t
905 const std::uint64_t
size,
934 +
sizeof(std::uint64_t) * 2 +
sizeof(std::uint32_t);
940 std::ofstream
out(
target_path, std::ios::binary | std::ios::trunc);
942 <<
"File_B_Tree: cannot open " <<
target_path <<
" for writing";
945 out.write(magic.data(), magic.size());
947 <<
"File_B_Tree: cannot write WAL magic to " <<
target_path;
950 const auto size =
static_cast<std::uint64_t
>(
size_);
959 "WAL minimum degree");
964 <<
"File_B_Tree: cannot write WAL reserved bytes to " <<
target_path;
970 "WAL checkpoint sequence");
990 <<
"File_B_Tree: cannot write WAL commit trailer to " <<
target_path;
992 "WAL trailer checkpoint sequence");
995 "WAL payload checksum");
1009 const std::string & path,
1010 const std::uint32_t version)
1017 std::array<char, 3>
padding = {};
1020 <<
"File_B_Tree: cannot read page padding from " << path;
1028 for (
size_t i = 0; i <
MaxKeys; ++i)
1043 <<
"File_B_Tree: page checksum mismatch in " << path;
1054 out.write(magic.data(), magic.size());
1056 <<
"File_B_Tree: cannot write magic to " <<
target_path;
1059 const auto size =
static_cast<std::uint64_t
>(
size_);
1066 "header minimum degree");
1071 <<
"File_B_Tree: cannot write reserved header bytes to " <<
target_path;
1077 "header checkpoint sequence");
1090 namespace fs = std::filesystem;
1092 if (path.has_parent_path())
1095 (
void) fs::create_directories(path.parent_path(),
ec);
1097 <<
"File_B_Tree: cannot create directory "
1098 << path.parent_path().string() <<
" for " <<
target_path
1099 <<
" (" <<
ec.message() <<
")";
1102 std::ofstream
out(
target_path, std::ios::binary | std::ios::trunc);
1104 <<
"File_B_Tree: cannot open " <<
target_path <<
" for writing";
1122 if (
file_.is_open())
1142 <<
"File_B_Tree: cannot flush " <<
file_path_;
1150 namespace fs = std::filesystem;
1155 <<
"File_B_Tree: cannot open read-only while WAL recovery sidecars "
1166 <<
"File_B_Tree: cannot open WAL " <<
wal_path_ <<
" for reading";
1169 std::array<char, detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
1170 in.read(magic.data(), magic.size());
1172 <<
"File_B_Tree: cannot read WAL magic from " <<
wal_path_;
1174 <<
"File_B_Tree: incompatible WAL file " <<
wal_path_;
1181 <<
"File_B_Tree: unsupported WAL version " <<
wal_version
1190 <<
"File_B_Tree: WAL " <<
wal_path_ <<
" was created for key size "
1191 << key_size <<
", but this instantiation expects "
1194 const auto min_degree =
1198 <<
" was created with minimum degree " << min_degree
1199 <<
", but this instantiation expects " <<
MinDegree;
1206 <<
" was created with an incompatible encoding";
1211 <<
"File_B_Tree: cannot read WAL reserved bytes from " <<
wal_path_;
1216 <<
" was created with an incompatible key codec";
1237 <<
"File_B_Tree: WAL checksum mismatch in " <<
wal_path_;
1242 <<
"File_B_Tree: cannot determine the size of WAL " <<
wal_path_
1243 <<
" (" <<
ec.message() <<
")";
1249 <<
"File_B_Tree: WAL size mismatch in " <<
wal_path_
1250 <<
" (expected " << expected_size <<
", got " <<
actual_size <<
")";
1252 if (
file_.is_open())
1266 <<
"File_B_Tree: invalid WAL page id " << page_id
1284 std::array<char, detail::Ordered_Tree_Snapshot_Magic_Size>
1288 <<
"File_B_Tree: cannot read WAL commit trailer from "
1291 <<
"File_B_Tree: missing WAL commit trailer in " <<
wal_path_;
1295 "WAL trailer checkpoint sequence");
1303 <<
"File_B_Tree: WAL trailer checkpoint mismatch in "
1306 <<
"File_B_Tree: WAL trailer dirty-count mismatch in "
1310 <<
"File_B_Tree: WAL payload checksum mismatch in " <<
wal_path_;
1368 <<
"File_B_Tree: cannot flush recovered data to " <<
file_path_;
1377 namespace fs = std::filesystem;
1383 <<
"File_B_Tree: cannot open read-only while journal recovery "
1384 <<
"sidecars are pending for " <<
file_path_;
1394 if (
file_.is_open())
1403 namespace fs = std::filesystem;
1407 <<
"File_B_Tree: cannot open " <<
source_path <<
" for reading";
1410 std::array<char, detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
1411 in.read(magic.data(), magic.size());
1413 <<
"File_B_Tree: cannot read header magic from " <<
source_path;
1417 <<
" does not contain a compatible paged B-Tree";
1419 const auto version =
1426 <<
"File_B_Tree: unsupported format version " << version
1431 const auto key_size =
1434 <<
"File_B_Tree: file " <<
source_path <<
" was created for key size "
1435 << key_size <<
", but this instantiation expects "
1438 const auto min_degree =
1442 <<
" was created with minimum degree " << min_degree
1443 <<
", but this instantiation expects " <<
MinDegree;
1449 <<
" was created with an incompatible encoding";
1454 <<
"File_B_Tree: cannot read reserved header bytes from " <<
source_path;
1457 <<
" was created with an incompatible key codec";
1466 "header free page head");
1470 image.checkpoint_sequence =
1472 "header checkpoint sequence");
1477 static_cast<std::uint64_t
>(
image.size),
1479 image.free_page_head,
1480 image.checkpoint_sequence))
1481 <<
"File_B_Tree: header checksum mismatch in " <<
source_path;
1488 version,
static_cast<std::uint64_t
>(
image.size),
1490 image.free_page_head))
1491 <<
"File_B_Tree: header checksum mismatch in " <<
source_path;
1497 <<
"File_B_Tree: cannot determine the size of " <<
source_path
1498 <<
" (" <<
ec.message() <<
")";
1499 const auto expected_size =
1502 <<
"File_B_Tree: file size mismatch in " <<
source_path
1503 <<
" (expected " << expected_size <<
", got " <<
actual_size <<
")";
1505 image.pages.empty();
1512 <<
"File_B_Tree: invalid root page id in " <<
source_path;
1514 <<
"File_B_Tree: invalid free-list head in " <<
source_path;
1532 for (
size_t i = 0; i <
pages_.size(); ++i)
1537 <<
"File_B_Tree: structural verification failed for " <<
file_path_;
1551 return std::ifstream(
file_path_, std::ios::binary).good();
1559 return std::nullopt;
1563 return std::nullopt;
1565 std::array<char, detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
1566 in.read(magic.data(), magic.size());
1568 return std::nullopt;
1574 return std::nullopt;
1582 return std::nullopt;
1588 file_.seekg(0, std::ios::end);
1590 return std::nullopt;
1597 return std::nullopt;
1602 return std::nullopt;
1611 return std::nullopt;
1654 <<
"File_B_Tree: invalid page id " << page_id;
1661 <<
"File_B_Tree: invalid page id " << page_id;
1682 const Key & key)
const
1691 const Key & key)
const
1751 return pages_.size() - 1;
1767 return std::nullopt;
1773 return std::nullopt;
1779 return std::nullopt;
1780 return leaf.
keys[0];
1786 return std::nullopt;
1792 return std::nullopt;
1798 return std::nullopt;
1906 for (
size_t i = 0; i < right.
key_count; ++i)
1933 if (idx + 1 <
page(parent_id).child_count
1940 if (idx + 1 <
page(parent_id).child_count)
1978 const Key succ = *
min_in(right_child);
2003 for (
size_t i = 0; i < p.
key_count; ++i)
2008 for (
size_t i = 0; i < p.
key_count; ++i)
2017 const std::optional<Key> &
min_key,
2018 const std::optional<Key> &
max_key,
2020 size_t & leaf_depth,
2024 if (page_id == 0
or page_id >=
pages_.size())
2028 if (visited[page_id] != 0)
2030 visited[page_id] = 1;
2063 if (leaf_depth ==
static_cast<size_t>(-1))
2065 return leaf_depth == depth;
2084 depth + 1, leaf_depth,
counted, visited))
2117 const Compare &
cmp = Compare())
2136 const Compare &
cmp = Compare())
2144 <<
"File_B_Tree requires a non-empty file path";
2150 <<
"File_B_Tree: cannot open missing file " <<
file_path_
2151 <<
" in read-only mode";
2157 file_.seekg(0, std::ios::end);
2160 <<
"File_B_Tree: cannot determine the size of " <<
file_path_;
2185 const Compare &
cmp = Compare())
2205 const Compare &
cmp = Compare())
2227 if (
file_.is_open())
2319 return pages_.size() - 1;
2377 <<
"File_B_Tree: backing file " <<
file_path_ <<
" no longer exists";
2508 Key
copy = std::move(key);
2589 if (idx <
page(curr).key_count)
2612 if (idx <
page(curr).key_count)
2647 size_t leaf_depth =
static_cast<size_t>(-1);
2657 template <
typename Key,
C++20 concepts for constraining comparison functors.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
Standard functor implementations and comparison objects.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void empty() noexcept
Empties the container.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Generic B-Tree with configurable minimum degree.
Persistent page-managed B-Tree stored in a single binary file.
static constexpr std::uint32_t NativeWalVersion
static Key erase_key_at(Page &page, const size_t idx)
Gen_File_B_Tree & operator=(const Gen_File_B_Tree &)=delete
Assignment is disabled.
void mark_page_dirty(const page_id_t page_id)
std::optional< Key > min_in(page_id_t page_id) const
static constexpr size_t wal_trailer_bytes() noexcept
static std::uint32_t add_native_key_to_crc(const std::uint32_t crc, const Key &key)
const std::string & get_file_path() const noexcept
Synonym for file_path().
std::optional< Key > max_in(page_id_t page_id) const
detail::Pid_File_Lock lock_
static constexpr std::uint32_t PortableFormatVersion
size_t page_count() const noexcept
Return the number of allocated pages currently tracked.
static Key read_portable_key(std::istream &in, const std::string &file_path, const char *what)
bool verify() const
Verify structural B-Tree invariants across cached pages.
static constexpr size_t header_bytes() noexcept
size_t lower_bound_index(const Page &page, const Key &key) const
void ensure_writable(const char *operation) const
void release_page(const page_id_t page_id)
Gen_File_B_Tree(std::string file_path, const bool auto_sync=true, const Compare &cmp=Compare())
Open or create a paged persistent B-Tree file for read-write access.
bool search(const Key &key) const
Synonym for contains().
void set_auto_sync(const bool enabled) noexcept
Enable or disable automatic flushing of dirty pages.
std::optional< Page > read_storage_page_if_current(const page_id_t page_id) const
void write_current_header_to_storage(const std::uint64_t checkpoint_sequence) const
static constexpr bool version_uses_portable_codec(const std::uint32_t version) noexcept
size_t dirty_page_count() const noexcept
static constexpr std::uint32_t NativeCheckpointFormatVersion
static constexpr std::uint32_t key_size_for_version(const std::uint32_t version) noexcept
void write_wal_to_path(const std::string &target_path, const std::uint64_t checkpoint_sequence) const
bool contains(const Key &key) const
Return whether a key is present.
~Gen_File_B_Tree() noexcept
Close the backing file.
static constexpr size_t serialized_page_bytes(const std::uint32_t version) noexcept
std::string journal_path_
std::optional< Key > max_key() const
Return the maximum key, if any.
static constexpr const char * tree_kind() noexcept
static constexpr bool is_free(const Page &page) noexcept
Loaded_Image read_image(const std::string &source_path) const
void split_child(const page_id_t parent_id, const size_t idx)
static constexpr size_t checksummed_page_bytes() noexcept
static constexpr std::uint32_t EncodedKeySize
Array< unsigned char > dirty_pages_
void init_sidecar_paths()
static void write_portable_key(std::ostream &out, const Key &key, const std::string &file_path, const char *what)
static void ensure_legacy_native_format_supported(const std::string &source_path, const std::uint32_t version)
Gen_File_B_Tree(const char *file_path, const bool auto_sync=true, const Compare &cmp=Compare())
Open or create a paged persistent B-Tree file for read-write access.
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
bool is_empty() const noexcept
Synonym for empty().
void clear()
Remove every key from the tree.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
static constexpr size_t page_bytes() noexcept
static Key read_native_key(std::istream &in, const std::string &file_path, const char *what)
void mark_header_dirty() const noexcept
std::optional< std::uint32_t > storage_format_version_on_disk() const
size_t height() const noexcept
Return the current height of the paged tree.
Gen_File_B_Tree(Gen_File_B_Tree &&)=delete
Moving is disabled.
static constexpr size_t checksummed_header_bytes() noexcept
static void insert_key_at(Page &page, const size_t idx, const Key &key)
static constexpr bool is_leaf(const Page &page) noexcept
static std::uint32_t wal_record_checksum_add_v2(std::uint32_t crc, const page_id_t page_id, const Page &page)
void merge_children(const page_id_t parent_id, const size_t idx)
static constexpr auto wal_trailer_magic() noexcept
const Page & page(const page_id_t page_id) const
std::string journal_tmp_path_
void open_storage(const bool truncate=false) const
void write_full_image_to_path(const std::string &target_path, const std::uint64_t checkpoint_sequence) const
static constexpr std::uint32_t CommitTrailerWalVersion
static constexpr std::uint32_t WalVersion
Array< Key > keys() const
Materialize the tree contents in sorted order.
static std::uint32_t page_checksum_v3(const Page &page)
size_t ensure_child_has_extra(const page_id_t parent_id, const size_t idx)
static constexpr size_t legacy_page_bytes() noexcept
static constexpr auto Read_Only
static std::uint32_t page_checksum_v4(const Page &page)
static void write_key(std::ostream &out, const Key &key, const std::string &file_path, const char *what)
void write_header_to_storage(const std::uint64_t size, const page_id_t root_page, const std::uint64_t page_count, const page_id_t free_page_head, const std::uint64_t checkpoint_sequence) const
Gen_File_B_Tree(const char *file_path, const Paged_File_Open_Mode open_mode, const bool auto_sync=true, const Compare &cmp=Compare())
Open a paged persistent B-Tree file with an explicit access mode.
std::optional< Key > min_key() const
Return the minimum key, if any.
static constexpr std::uint32_t FormatVersion
bool verify_node(const page_id_t page_id, const std::optional< Key > &min_key, const std::optional< Key > &max_key, const size_t depth, size_t &leaf_depth, size_t &counted, Array< unsigned char > &visited) const
void seekp_to(const std::streamoff offset) const
static std::uint32_t header_checksum(const std::uint32_t version, const std::uint64_t size, const page_id_t root_page, const std::uint64_t page_count, const page_id_t free_page_head, const std::uint64_t checkpoint_sequence) noexcept
static Key read_key(std::istream &in, const std::string &file_path, const char *what, const std::uint32_t version)
static constexpr std::uint32_t PortableWalVersion
void borrow_from_prev(const page_id_t parent_id, const size_t idx)
static void write_native_key(std::ostream &out, const Key &key, const std::string &file_path, const char *what)
static constexpr auto file_magic() noexcept
static void write_page_record(std::ostream &out, const Page &page, const std::string &target_path)
size_t page_size_bytes() const noexcept
Return the fixed serialized page size.
static Page read_page_from_stream(std::istream &in, const std::string &path, const std::uint32_t version)
void borrow_from_next(const page_id_t parent_id, const size_t idx)
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
static constexpr size_t MinKeys
void write_full_image(std::ostream &out, const std::string &target_path, const std::uint64_t checkpoint_sequence) const
void checkpoint() const
Synonym for sync().
size_t size() const noexcept
Return the number of stored keys.
static constexpr bool is_internal(const Page &page) noexcept
static page_id_t erase_child_at(Page &page, const size_t idx)
void collect_keys(const page_id_t page_id, Array< Key > &out) const
static constexpr std::array< char, 7 > reserved_bytes_for_version(const std::uint32_t version) noexcept
Paged_File_Open_Mode open_mode_
void stamp_all_pages_for_checkpoint(const std::uint64_t checkpoint_sequence) noexcept
std::streamoff current_page_offset(const page_id_t page_id) const noexcept
bool empty() const noexcept
Return whether the tree has no keys.
const Compare & key_comp() const noexcept
Return the comparison object.
static constexpr std::uint32_t HeaderCheckpointFormatVersion
static constexpr size_t serialized_header_bytes(const std::uint32_t version) noexcept
static constexpr std::uint32_t wal_record_format_version(const std::uint32_t wal_version) noexcept
static void insert_child_at(Page &page, const size_t idx, const page_id_t child_id)
Paged_File_Open_Mode open_mode() const noexcept
Return the access mode used to open the file.
static constexpr std::uint8_t encoding_marker_for_version(const std::uint32_t version) noexcept
void sync() const
Flush the current tree image using redo WAL or full-image fallback.
Gen_File_B_Tree(const Gen_File_B_Tree &)=delete
Copying is disabled.
static constexpr size_t wal_header_bytes() noexcept
bool remove(const Key &key)
Remove a key if present.
void reload()
Discard unsynchronized changes and reload pages from disk.
page_id_t free_page_head_
const std::string & file_path() const noexcept
Return the backing file path.
static constexpr size_t wal_record_bytes(const std::uint32_t wal_version) noexcept
std::uint64_t checkpoint_sequence() const noexcept
Return the durable checkpoint sequence stored in the backing file.
Key key_type
Stored key type.
const Compare & get_compare() const noexcept
Return the comparison object.
static constexpr auto Read_Write
void ensure_parent_dir() const
void insert_nonfull(const page_id_t page_id, const Key &key)
static void write_pod(std::ostream &out, const T &value, const std::string &file_path, const char *what)
static std::uint32_t wal_record_checksum_add(std::uint32_t crc, const page_id_t page_id, const Page &page)
static T read_pod(std::istream &in, const std::string &file_path, const char *what)
static constexpr std::uint32_t LegacyFormatVersion
static constexpr size_t MaxChildren
static std::uint32_t wal_header_checksum(const std::uint32_t wal_version, const std::uint64_t size, const page_id_t root_page, const std::uint64_t page_count, const page_id_t free_page_head, const std::uint64_t checkpoint_sequence, const std::uint64_t dirty_count) noexcept
static constexpr std::uint8_t PortableEncodingMarker
bool insert(Key &&key)
Insert a key by move if it is not already present.
page_id_t allocate_page(const PageKind kind)
static std::uint32_t page_checksum(const Page &page)
std::uint64_t checkpoint_sequence_
std::uint32_t storage_format_version_
static constexpr size_t MaxKeys
static std::uint32_t wal_record_checksum_add_v3(std::uint32_t crc, const page_id_t page_id, const Page &page)
detail::Pid_File_Lock_Mode lock_mode() const noexcept
bool strictly_sorted(const Page &page) const
static constexpr auto wal_magic() noexcept
static constexpr std::uint32_t ChecksummedFormatVersion
void clear_dirty_state() const noexcept
static constexpr size_t portable_page_bytes() noexcept
static constexpr std::uint32_t LegacyWalVersion
void write_current_page_to_storage(const page_id_t page_id, const Page &page) const
static constexpr std::uint8_t EndianMarker
static void ensure_legacy_native_wal_supported(const std::string &source_path, const std::uint32_t wal_version)
bool equals(const Key &lhs, const Key &rhs) const
bool has_pending_changes() const noexcept
std::string wal_tmp_path_
bool remove_from(const page_id_t page_id, const Key &key)
static std::uint32_t add_key_to_crc(const std::uint32_t crc, const Key &key)
page_id_t page_id_type
Type of on-disk page identifiers.
static constexpr size_t legacy_header_bytes() noexcept
bool read_only() const noexcept
Gen_File_B_Tree(std::string file_path, const Paged_File_Open_Mode open_mode, const bool auto_sync=true, const Compare &cmp=Compare())
Open a paged persistent B-Tree file with an explicit access mode.
bool is_read_only() const noexcept
Return whether this handle is read-only.
size_t upper_bound_index(const Page &page, const Key &key) const
bool insert(const Key &key)
Insert a key if it is not already present.
static constexpr size_t native_page_bytes() noexcept
static std::uint32_t header_checksum_v2(const std::uint32_t version, const std::uint64_t size, const page_id_t root_page, const std::uint64_t page_count, const page_id_t free_page_head) noexcept
static constexpr Page make_page(const PageKind kind) noexcept
Page & page(const page_id_t page_id)
void apply_current_cache_to_storage(const std::uint64_t checkpoint_sequence) const
void acquire(const std::string &path, const char *kind, const Pid_File_Lock_Mode mode=Pid_File_Lock_Mode::Exclusive)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Freq_Node * pred
Predecessor node in level-order traversal.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
std::uint32_t crc32_add_bytes(std::uint32_t crc, const void *data, const size_t size) noexcept
void sync_parent_directory(const std::string &path, const char *kind)
constexpr std::array< char, Ordered_Tree_Snapshot_Magic_Size > ordered_tree_snapshot_magic(const char(&text)[N]) noexcept
void copy_file_contents(const std::string &src, const std::string &dst, const char *kind)
void remove_file_if_exists(const std::string &path, const char *kind)
std::uint32_t crc32_begin() noexcept
std::uint32_t crc32_add(std::uint32_t crc, const T &value) noexcept
constexpr size_t Ordered_Tree_Snapshot_Magic_Size
void rename_file_atomic(const std::string &src, const std::string &dst, const char *kind)
std::uint32_t crc32_finish(const std::uint32_t crc) noexcept
void sync_file_by_path(const std::string &path, const char *kind)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Paged_File_Open_Mode
Open mode for paged persistent tree files.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
auto mode(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the mode (most frequent value).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
std::uint64_t checkpoint_sequence
std::uint64_t checkpoint_sequence
std::uint16_t child_count
page_id_t children[MaxChildren]
Dynamic array container with automatic resizing.
B-Tree implementation with configurable minimum degree.
Helpers for durable paged-tree files.
Fixed-size portable codecs for paged persistent tree payloads.
Internal helpers for snapshot-backed persistent tree wrappers.