40# ifndef TPL_FILE_B_TREE_H
41# define TPL_FILE_B_TREE_H
51# include <type_traits>
151 template <
typename Key,
153 size_t MinDegree = 16,
155 requires StrictWeakOrder<Compare, 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");
170 static constexpr size_t MaxKeys = 2 * MinDegree - 1;
172 static constexpr size_t MinKeys = MinDegree - 1;
180 static_cast<std::uint32_t
>(Codec::encoded_size);
183 std::endian::native == std::endian::little ? 1 : 2;
242 [[nodiscard]]
static constexpr const char *
tree_kind() noexcept
244 return "File_B_Tree";
262 <<
"File_B_Tree::" << operation
263 <<
"(): file was opened in read-only mode";
271 [[nodiscard]]
static constexpr auto wal_magic() noexcept
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>
376 std::array<char, 7> reserved = {};
379 const auto codec_id = Codec::storage_id;
380 reserved[0] =
static_cast<char>(codec_id & 0xFFu);
381 reserved[1] =
static_cast<char>((codec_id >> 8) & 0xFFu);
382 reserved[2] =
static_cast<char>((codec_id >> 16) & 0xFFu);
383 reserved[3] =
static_cast<char>((codec_id >> 24) & 0xFFu);
391 page.
kind =
static_cast<std::uint8_t
>(kind);
396 const std::string & source_path,
397 const std::uint32_t version)
399 if constexpr (not std::is_trivially_copyable_v<Key>)
401 <<
"File_B_Tree: file " << source_path
402 <<
" uses legacy native format version " << version
403 <<
", which requires trivially copyable keys; reopen or rewrite it "
409 const std::string & source_path,
410 const std::uint32_t wal_version)
412 if constexpr (not std::is_trivially_copyable_v<Key>)
414 <<
"File_B_Tree: WAL " << source_path
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());
595 [[nodiscard]]
static Key
read_key(std::istream & in,
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)
785 const std::string & target_path)
791 const std::array<char, 3> padding = {};
792 out.write(padding.data(), padding.size());
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");
844 file_.write(reserved.data(), reserved.size());
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
896 [[nodiscard]]
static constexpr size_t
904 const std::uint32_t wal_version,
905 const std::uint64_t
size,
910 const std::uint64_t dirty_count)
noexcept
919 const auto reserved =
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_);
958 write_pod(
out,
static_cast<std::uint64_t
>(MinDegree), target_path,
959 "WAL minimum degree");
962 out.write(reserved.data(), reserved.size());
964 <<
"File_B_Tree: cannot write WAL reserved bytes to " << target_path;
970 "WAL checkpoint sequence");
971 write_pod(
out, dirty_count, target_path,
"WAL dirty count");
976 target_path,
"WAL checksum");
988 out.write(trailer_magic.data(), trailer_magic.size());
990 <<
"File_B_Tree: cannot write WAL commit trailer to " << target_path;
992 "WAL trailer checkpoint sequence");
993 write_pod(
out, dirty_count, target_path,
"WAL trailer dirty count");
995 "WAL payload checksum");
999 <<
"File_B_Tree: cannot flush " << target_path;
1002 <<
"File_B_Tree: cannot close " << target_path;
1009 const std::string & path,
1010 const std::uint32_t version)
1013 page.
kind = read_pod<std::uint8_t>(in, path,
"page kind");
1014 page.
key_count = read_pod<std::uint16_t>(in, path,
"page key count");
1015 page.
child_count = read_pod<std::uint16_t>(in, path,
"page child count");
1017 std::array<char, 3> padding = {};
1018 in.read(padding.data(), padding.size());
1020 <<
"File_B_Tree: cannot read page padding from " << path;
1022 page.
link = read_pod<page_id_t>(in, path,
"page link");
1026 read_pod<std::uint64_t>(in, path,
"page checkpoint sequence");
1028 for (
size_t i = 0; i <
MaxKeys; ++i)
1032 page.
children[i] = read_pod<page_id_t>(in, path,
"page child");
1036 const auto stored = read_pod<std::uint32_t>(in, path,
"page checksum");
1043 <<
"File_B_Tree: page checksum mismatch in " << path;
1050 const std::string & target_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_);
1065 write_pod(
out,
static_cast<std::uint64_t
>(MinDegree), target_path,
1066 "header minimum degree");
1069 out.write(reserved.data(), reserved.size());
1071 <<
"File_B_Tree: cannot write reserved header bytes to " << target_path;
1077 "header checkpoint sequence");
1081 target_path,
"header checksum");
1090 namespace fs = std::filesystem;
1091 const fs::path path(target_path);
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";
1109 <<
"File_B_Tree: cannot flush " << target_path;
1112 <<
"File_B_Tree: cannot close " << target_path;
1122 if (
file_.is_open())
1125 if (not
file_.is_open())
1132 if (not
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 "
1164 std::ifstream in(
wal_path_, std::ios::binary);
1166 <<
"File_B_Tree: cannot open WAL " <<
wal_path_ <<
" for reading";
1168 const auto expected_magic =
wal_magic();
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_;
1176 const auto wal_version = read_pod<std::uint32_t>(in,
wal_path_,
"WAL version");
1181 <<
"File_B_Tree: unsupported WAL version " << wal_version
1186 const auto key_size = read_pod<std::uint32_t>(in,
wal_path_,
"WAL key size");
1190 <<
"File_B_Tree: WAL " <<
wal_path_ <<
" was created for key size "
1191 << key_size <<
", but this instantiation expects "
1194 const auto min_degree =
1195 read_pod<std::uint64_t>(in,
wal_path_,
"WAL minimum degree");
1198 <<
" was created with minimum degree " << min_degree
1199 <<
", but this instantiation expects " << MinDegree;
1201 const auto encoding = read_pod<std::uint8_t>(in,
wal_path_,
"WAL encoding");
1206 <<
" was created with an incompatible encoding";
1208 std::array<char, 7> reserved = {};
1209 in.read(reserved.data(), reserved.size());
1211 <<
"File_B_Tree: cannot read WAL reserved bytes from " <<
wal_path_;
1216 <<
" was created with an incompatible key codec";
1218 const auto wal_size = read_pod<std::uint64_t>(in,
wal_path_,
"WAL size");
1219 const auto wal_root_page =
1220 read_pod<page_id_t>(in,
wal_path_,
"WAL root page");
1221 const auto wal_page_count =
1222 read_pod<std::uint64_t>(in,
wal_path_,
"WAL page count");
1223 const auto wal_free_head =
1224 read_pod<page_id_t>(in,
wal_path_,
"WAL free page head");
1225 const auto wal_checkpoint_sequence =
1226 read_pod<std::uint64_t>(in,
wal_path_,
"WAL checkpoint sequence");
1227 const auto wal_dirty_count =
1228 read_pod<std::uint64_t>(in,
wal_path_,
"WAL dirty count");
1229 const auto wal_checksum =
1230 read_pod<std::uint32_t>(in,
wal_path_,
"WAL checksum");
1233 wal_version, wal_size, wal_root_page,
1235 wal_free_head, wal_checkpoint_sequence,
1237 <<
"File_B_Tree: WAL checksum mismatch in " <<
wal_path_;
1240 const auto actual_size = fs::file_size(
wal_path_, ec);
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())
1262 for (std::uint64_t i = 0; i < wal_dirty_count; ++i)
1264 const auto page_id = read_pod<page_id_t>(in,
wal_path_,
"WAL page id");
1266 <<
"File_B_Tree: invalid WAL page id " << page_id
1275 else if (has_commit_trailer)
1281 if (has_commit_trailer)
1284 std::array<char, detail::Ordered_Tree_Snapshot_Magic_Size>
1286 in.read(trailer_magic.data(), trailer_magic.size());
1288 <<
"File_B_Tree: cannot read WAL commit trailer from "
1291 <<
"File_B_Tree: missing WAL commit trailer in " <<
wal_path_;
1293 const auto trailer_checkpoint_sequence =
1295 "WAL trailer checkpoint sequence");
1296 const auto trailer_dirty_count =
1297 read_pod<std::uint64_t>(in,
wal_path_,
"WAL trailer dirty count");
1298 const auto trailer_payload_checksum =
1299 read_pod<std::uint32_t>(in,
wal_path_,
"WAL payload checksum");
1302 == wal_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_;
1313 bool stale_wal =
false;
1321 bool header_current =
false;
1333 for (
size_t i = 0; i < recovered_records.
size(); ++i)
1335 const auto persisted =
1337 if (not persisted.has_value()
1338 or persisted->checkpoint_sequence < wal_checkpoint_sequence)
1354 wal_free_head, wal_checkpoint_sequence);
1355 for (
size_t i = 0; i < recovered_records.
size(); ++i)
1357 const auto persisted =
1359 if (persisted.has_value()
1360 and persisted->checkpoint_sequence >= wal_checkpoint_sequence)
1363 recovered_records[i].
page);
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;
1405 std::ifstream in(source_path, std::ios::binary);
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;
1416 <<
"File_B_Tree: file " << source_path
1417 <<
" does not contain a compatible paged B-Tree";
1419 const auto version =
1420 read_pod<std::uint32_t>(in, source_path,
"header version");
1426 <<
"File_B_Tree: unsupported format version " << version
1427 <<
" in " << source_path;
1431 const auto key_size =
1432 read_pod<std::uint32_t>(in, source_path,
"header 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 =
1439 read_pod<std::uint64_t>(in, source_path,
"header minimum degree");
1441 <<
"File_B_Tree: file " << source_path
1442 <<
" was created with minimum degree " << min_degree
1443 <<
", but this instantiation expects " << MinDegree;
1445 const auto encoding =
1446 read_pod<std::uint8_t>(in, source_path,
"header encoding");
1448 <<
"File_B_Tree: file " << source_path
1449 <<
" was created with an incompatible encoding";
1451 std::array<char, 7> reserved = {};
1452 in.read(reserved.data(), reserved.size());
1454 <<
"File_B_Tree: cannot read reserved header bytes from " << source_path;
1456 <<
"File_B_Tree: file " << source_path
1457 <<
" was created with an incompatible key codec";
1461 image.
size = read_pod<std::uint64_t>(in, source_path,
"header size");
1462 image.
root_page = read_pod<page_id_t>(in, source_path,
"header root page");
1464 read_pod<std::uint64_t>(in, source_path,
"header page count");
1466 "header free page head");
1471 read_pod<std::uint64_t>(in, source_path,
1472 "header checkpoint sequence");
1473 const auto stored = read_pod<std::uint32_t>(in, source_path,
1477 static_cast<std::uint64_t
>(image.
size),
1481 <<
"File_B_Tree: header checksum mismatch in " << source_path;
1485 const auto stored = read_pod<std::uint32_t>(in, source_path,
1488 version,
static_cast<std::uint64_t
>(image.
size),
1491 <<
"File_B_Tree: header checksum mismatch in " << source_path;
1495 const auto actual_size = fs::file_size(source_path, ec);
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;
1561 std::ifstream in(
file_path_, std::ios::binary);
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;
1570 return read_pod<std::uint32_t>(in,
file_path_,
"header version");
1574 return std::nullopt;
1578 [[nodiscard]] std::optional<Page>
1582 return std::nullopt;
1584 if (not
file_.is_open())
1588 file_.seekg(0, std::ios::end);
1589 if (not
file_.good())
1590 return std::nullopt;
1592 const auto file_size =
file_.tellg();
1597 return std::nullopt;
1601 if (not
file_.good())
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;
1665 [[nodiscard]]
bool equals(
const Key & lhs,
const Key & rhs)
const
1682 const Key & key)
const
1691 const Key & key)
const
1751 return pages_.size() - 1;
1758 page(page_id) = free_page;
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;
1805 const bool child_is_leaf =
is_leaf(
page(child_id));
1815 for (
size_t i = MinDegree; i < child.
key_count; ++i)
1819 if (not child_is_leaf)
1821 for (
size_t i = MinDegree; i < child.
child_count; ++i)
1868 page(left_id).children[
page(left_id).child_count - 1]);
1906 for (
size_t i = 0; i < right.
key_count; ++i)
1923 if (
page(
page(parent_id).children[idx]).key_count >= MinDegree)
1927 and page(
page(parent_id).children[idx - 1]).key_count >= MinDegree)
1933 if (idx + 1 <
page(parent_id).child_count
1934 and page(
page(parent_id).children[idx + 1]).key_count >= MinDegree)
1940 if (idx + 1 <
page(parent_id).child_count)
1968 if (
page(left_child).key_count >= MinDegree)
1976 if (
page(right_child).key_count >= MinDegree)
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;
2076 std::optional<Key> child_min =
min_key;
2077 std::optional<Key> child_max =
max_key;
2079 child_min = p.
keys[i - 1];
2081 child_max = p.
keys[i];
2084 depth + 1, leaf_depth, counted, visited))
2116 const bool auto_sync =
true,
2117 const Compare &
cmp = Compare())
2135 const bool auto_sync =
true,
2136 const Compare &
cmp = Compare())
2144 <<
"File_B_Tree requires a non-empty file path";
2151 <<
"File_B_Tree: cannot open missing file " <<
file_path_
2152 <<
" in read-only mode";
2159 file_.seekg(0, std::ios::end);
2160 const std::streamoff file_size =
file_.tellg();
2162 <<
"File_B_Tree: cannot determine the size of " <<
file_path_;
2186 const bool auto_sync =
true,
2187 const Compare &
cmp = Compare())
2206 const bool auto_sync =
true,
2207 const Compare &
cmp = Compare())
2229 if (
file_.is_open())
2256 [[nodiscard]]
const std::string &
file_path() const noexcept
2321 return pages_.size() - 1;
2379 <<
"File_B_Tree: backing file " <<
file_path_ <<
" no longer exists";
2399 [[nodiscard]]
size_t size() const noexcept {
return size_; }
2453 [[nodiscard]]
bool search(
const Key & key)
const
2510 Key
copy = std::move(key);
2586 std::optional<Key> candidate;
2591 if (idx <
page(curr).key_count)
2609 std::optional<Key> candidate;
2614 if (idx <
page(curr).key_count)
2649 size_t leaf_depth =
static_cast<size_t>(-1);
2653 leaf_depth, counted, visited)
2659 template <
typename Key,
2661 size_t MinDegree = 16,
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
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.
size_t size_t int32_t value
size_t size_t int32_t * out
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.
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.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
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.