36#include <gtest/gtest.h>
51#if defined(__unix__) || defined(__APPLE__)
60 namespace fs = std::filesystem;
62 fs::path make_temp_path()
72 static std::atomic<unsigned long long>
counter{0};
73 const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
74#if defined(__unix__) || defined(__APPLE__)
75 const auto pid =
static_cast<long long>(::getpid());
79 const auto id = std::to_string(pid) +
"_" +
80 std::to_string(now) +
"_" +
82 const fs::path dir = fs::temp_directory_path() /
"aleph_file_btree_tests";
83 fs::create_directories(dir);
84 return dir / (
id +
".idx");
91 explicit TempFile(fs::path p) : path(
std::move(p)) {}
97 fs::remove(path.string() +
".wal", ec);
98 fs::remove(path.string() +
".wal.tmp", ec);
99 fs::remove(path.string() +
".journal", ec);
100 fs::remove(path.string() +
".journal.tmp", ec);
101 fs::remove(path.string() +
".lock", ec);
102 fs::remove(path.string() +
".old", ec);
103 fs::remove(path.string() +
".new", ec);
107 template <
typename T>
111 ret.reserve(arr.
size());
112 for (
size_t i = 0; i < arr.
size(); ++i)
113 ret.push_back(arr[i]);
117#if defined(__unix__) || defined(__APPLE__)
118 constexpr bool running_under_tsan() noexcept
120# if defined(__SANITIZE_THREAD__)
122# elif defined(__has_feature)
123# if __has_feature(thread_sanitizer)
133 template <
typename F>
134 int run_in_child(
F && fn)
136 const pid_t pid = ::fork();
155 if (::waitpid(pid, &status, 0) < 0)
161 constexpr std::uint32_t file_b_tree_test_file_version = 5;
162 constexpr std::uint32_t file_b_tree_test_wal_version = 4;
163 constexpr std::uint32_t file_b_tree_test_key_size =
165 constexpr std::uint64_t file_b_tree_test_min_degree = 3;
166 constexpr std::uint8_t file_b_tree_test_encoding = 3;
168 std::array<char, 7> file_b_tree_reserved_bytes()
170 std::array<char, 7> reserved = {};
172 reserved[0] =
static_cast<char>(codec_id & 0xFFu);
173 reserved[1] =
static_cast<char>((codec_id >> 8) & 0xFFu);
174 reserved[2] =
static_cast<char>((codec_id >> 16) & 0xFFu);
175 reserved[3] =
static_cast<char>((codec_id >> 24) & 0xFFu);
179 std::uint32_t btree_wal_checksum(
const std::uint32_t wal_version,
180 const std::uint64_t
size,
181 const std::uint64_t root_page,
182 const std::uint64_t page_count,
183 const std::uint64_t free_page_head,
184 const std::uint64_t checkpoint_sequence,
185 const std::uint64_t dirty_count)
188 const auto reserved = file_b_tree_reserved_bytes();
204 std::uint32_t btree_wal_payload_checksum(
const std::uint64_t page_count,
205 const size_t page_bytes,
206 const std::vector<char> & page_blob)
209 for (std::uint64_t page_id = 1; page_id <= page_count; ++page_id)
213 crc, page_blob.data() + (page_id - 1) * page_bytes, page_bytes);
218 void write_btree_wal_from_file(
const fs::path & data_path,
219 const fs::path & wal_path)
221 constexpr std::uint32_t file_version = file_b_tree_test_file_version;
222 constexpr std::uint32_t wal_version = file_b_tree_test_wal_version;
223 constexpr std::uint32_t key_size = file_b_tree_test_key_size;
224 constexpr std::uint64_t min_degree = file_b_tree_test_min_degree;
225 constexpr std::uint8_t encoding = file_b_tree_test_encoding;
226 constexpr size_t max_keys = 5;
227 constexpr size_t max_children = 6;
228 constexpr size_t page_bytes =
229 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
230 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * max_keys
231 +
sizeof(std::uint64_t) * max_children +
sizeof(std::uint32_t);
233 std::ifstream in(data_path, std::ios::binary);
234 ASSERT_TRUE(in.good());
236 std::array<char, Aleph::detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
237 in.read(magic.data(), magic.size());
238 ASSERT_TRUE(in.good());
240 auto read_u32 = [&](std::uint32_t &
value)
242 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
243 ASSERT_TRUE(in.good());
245 auto read_u64 = [&](std::uint64_t &
value)
247 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
248 ASSERT_TRUE(in.good());
250 auto read_u8 = [&](std::uint8_t &
value)
252 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
253 ASSERT_TRUE(in.good());
256 std::uint32_t version = 0;
257 std::uint32_t file_key_size = 0;
258 std::uint64_t file_min_degree = 0;
259 std::uint8_t file_encoding = 0;
261 read_u32(file_key_size);
262 read_u64(file_min_degree);
263 read_u8(file_encoding);
264 ASSERT_EQ(version, file_version);
265 ASSERT_EQ(file_key_size, key_size);
266 ASSERT_EQ(file_min_degree, min_degree);
267 ASSERT_EQ(file_encoding, encoding);
269 const auto reserved = file_b_tree_reserved_bytes();
270 std::array<char, 7> stored_reserved = {};
271 in.read(stored_reserved.data(), stored_reserved.size());
272 ASSERT_TRUE(in.good());
273 ASSERT_EQ(stored_reserved, reserved);
275 std::uint64_t
size = 0;
276 std::uint64_t root_page = 0;
277 std::uint64_t page_count = 0;
278 std::uint64_t free_page_head = 0;
279 std::uint64_t checkpoint_sequence = 0;
280 std::uint32_t checksum = 0;
283 read_u64(page_count);
284 read_u64(free_page_head);
285 read_u64(checkpoint_sequence);
288 std::vector<char> page_blob(page_bytes * page_count);
289 in.read(page_blob.data(), page_blob.size());
290 ASSERT_TRUE(in.good());
292 std::ofstream
out(wal_path, std::ios::binary | std::ios::trunc);
293 ASSERT_TRUE(
out.good());
295 const auto wal_magic =
297 const auto trailer_magic =
299 out.write(wal_magic.data(), wal_magic.size());
300 out.write(
reinterpret_cast<const char *
>(&wal_version),
sizeof(wal_version));
301 out.write(
reinterpret_cast<const char *
>(&key_size),
sizeof(key_size));
302 out.write(
reinterpret_cast<const char *
>(&min_degree),
sizeof(min_degree));
303 out.write(
reinterpret_cast<const char *
>(&encoding),
sizeof(encoding));
304 out.write(reserved.data(), reserved.size());
305 out.write(
reinterpret_cast<const char *
>(&
size),
sizeof(
size));
306 out.write(
reinterpret_cast<const char *
>(&root_page),
sizeof(root_page));
307 out.write(
reinterpret_cast<const char *
>(&page_count),
sizeof(page_count));
308 out.write(
reinterpret_cast<const char *
>(&free_page_head),
sizeof(free_page_head));
309 out.write(
reinterpret_cast<const char *
>(&checkpoint_sequence),
sizeof(checkpoint_sequence));
310 out.write(
reinterpret_cast<const char *
>(&page_count),
sizeof(page_count));
311 const auto wal_checksum =
312 btree_wal_checksum(wal_version,
size, root_page, page_count, free_page_head,
313 checkpoint_sequence, page_count);
314 out.write(
reinterpret_cast<const char *
>(&wal_checksum),
sizeof(wal_checksum));
316 for (std::uint64_t page_id = 1; page_id <= page_count; ++page_id)
318 out.write(
reinterpret_cast<const char *
>(&page_id),
sizeof(page_id));
319 out.write(page_blob.data() + (page_id - 1) * page_bytes, page_bytes);
322 const auto payload_checksum =
323 btree_wal_payload_checksum(page_count, page_bytes, page_blob);
324 out.write(trailer_magic.data(), trailer_magic.size());
325 out.write(
reinterpret_cast<const char *
>(&checkpoint_sequence),
326 sizeof(checkpoint_sequence));
327 out.write(
reinterpret_cast<const char *
>(&page_count),
sizeof(page_count));
328 out.write(
reinterpret_cast<const char *
>(&payload_checksum),
329 sizeof(payload_checksum));
331 ASSERT_TRUE(
out.good());
334 std::uint64_t read_btree_page_count(
const fs::path & data_path)
336 std::ifstream in(data_path, std::ios::binary);
337 EXPECT_TRUE(in.good());
342 +
sizeof(std::uint32_t) * 2 +
sizeof(std::uint64_t)
343 +
sizeof(std::uint8_t) + 7 +
sizeof(std::uint64_t) * 2);
344 EXPECT_TRUE(in.good());
345 std::uint64_t page_count = 0;
346 in.read(
reinterpret_cast<char *
>(&page_count),
sizeof(page_count));
347 EXPECT_TRUE(in.good());
351 int read_btree_root_first_key(
const fs::path & data_path)
353 constexpr size_t header_bytes =
355 +
sizeof(std::uint64_t) * 5 +
sizeof(std::uint8_t) + 7;
356 constexpr size_t page_bytes =
357 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
358 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * 5
359 +
sizeof(std::uint64_t) * 6 +
sizeof(std::uint32_t);
361 std::ifstream in(data_path, std::ios::binary);
362 EXPECT_TRUE(in.good());
367 +
sizeof(std::uint32_t) * 2 +
sizeof(std::uint64_t)
368 +
sizeof(std::uint8_t) + 7 +
sizeof(std::uint64_t));
369 EXPECT_TRUE(in.good());
371 std::uint64_t root_page = 0;
372 in.read(
reinterpret_cast<char *
>(&root_page),
sizeof(root_page));
373 EXPECT_TRUE(in.good());
375 const auto offset =
static_cast<std::streamoff
>(
376 header_bytes + (root_page - 1) * page_bytes +
sizeof(std::uint8_t)
377 +
sizeof(std::uint16_t) * 2 + 3 +
sizeof(std::uint64_t) * 2);
379 EXPECT_TRUE(in.good());
381 std::array<unsigned char, Aleph::detail::Paged_Value_Codec<int>::encoded_size>
383 in.read(
reinterpret_cast<char *
>(key_bytes.data()), key_bytes.size());
384 EXPECT_TRUE(in.good());
388 std::vector<std::uint64_t> read_btree_wal_page_ids(
const fs::path & wal_path)
390 constexpr size_t page_bytes =
391 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
392 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * 5
393 +
sizeof(std::uint64_t) * 6 +
sizeof(std::uint32_t);
395 std::ifstream in(wal_path, std::ios::binary);
396 EXPECT_TRUE(in.good());
397 std::vector<std::uint64_t> page_ids;
401 auto read_u32 = [&](std::uint32_t &
value)
403 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
404 EXPECT_TRUE(in.good());
406 auto read_u64 = [&](std::uint64_t &
value)
408 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
409 EXPECT_TRUE(in.good());
411 auto read_u8 = [&](std::uint8_t &
value)
413 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
414 EXPECT_TRUE(in.good());
417 std::array<char, Aleph::detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
418 std::array<char, 7> reserved = {};
419 std::array<char, Aleph::detail::Ordered_Tree_Snapshot_Magic_Size>
421 std::uint32_t wal_version = 0;
422 std::uint32_t key_size = 0;
423 std::uint64_t min_degree = 0;
424 std::uint8_t encoding = 0;
425 std::uint64_t ignored64 = 0;
426 std::uint32_t ignored32 = 0;
427 std::uint64_t dirty_count = 0;
429 in.read(magic.data(), magic.size());
430 EXPECT_TRUE(in.good());
431 read_u32(wal_version);
433 read_u64(min_degree);
435 in.read(reserved.data(), reserved.size());
436 EXPECT_TRUE(in.good());
442 read_u64(dirty_count);
445 page_ids.reserve(dirty_count);
446 for (std::uint64_t i = 0; i < dirty_count; ++i)
448 std::uint64_t page_id = 0;
450 page_ids.push_back(page_id);
451 in.seekg(
static_cast<std::streamoff
>(page_bytes), std::ios::cur);
452 EXPECT_TRUE(in.good());
455 in.read(trailer_magic.data(), trailer_magic.size());
456 EXPECT_TRUE(in.good());
460 void copy_region(
const fs::path & src,
const fs::path & dst,
461 const std::streamoff
offset,
const size_t size)
463 std::ifstream in(src, std::ios::binary);
464 ASSERT_TRUE(in.good());
465 std::fstream
out(dst, std::ios::binary | std::ios::in | std::ios::out);
466 ASSERT_TRUE(
out.good());
468 std::vector<char> buffer(
size);
470 ASSERT_TRUE(in.good());
471 in.read(buffer.data(), buffer.size());
472 ASSERT_TRUE(in.good());
475 ASSERT_TRUE(
out.good());
476 out.write(buffer.data(), buffer.size());
477 ASSERT_TRUE(
out.good());
481TEST(FileBTree, ReopensPersistedKeys)
483 TempFile tmp(make_temp_path());
487 for (
int value : {40, 10, 90, 20, 70, 60, 30})
490 EXPECT_TRUE(tree.
verify());
492 (std::vector<int>{10, 20, 30, 40, 60, 70, 90}));
496 EXPECT_TRUE(reopened.
verify());
498 (std::vector<int>{10, 20, 30, 40, 60, 70, 90}));
499 EXPECT_EQ(reopened.
lower_bound(55), std::optional<int>(60));
500 EXPECT_EQ(reopened.
upper_bound(60), std::optional<int>(70));
503TEST(FileBTree, AutoSyncCanBeDisabledAndForced)
505 TempFile tmp(make_temp_path());
510 EXPECT_TRUE(tree.
insert(10));
511 EXPECT_TRUE(tree.
insert(20));
522 EXPECT_TRUE(tree.
insert(10));
523 EXPECT_TRUE(tree.
insert(20));
528 EXPECT_EQ(
to_vector(reopened.
keys()), (std::vector<int>{10, 20}));
531TEST(FileBTree, CheckpointSequenceAdvancesAndPersists)
533 TempFile tmp(make_temp_path());
535 std::uint64_t first = 0;
536 std::uint64_t second = 0;
541 EXPECT_TRUE(tree.
insert(10));
544 EXPECT_GT(first, 1u);
546 EXPECT_TRUE(tree.
insert(20));
549 EXPECT_GT(second, first);
556TEST(FileBTree, SupportsBoundedStringCodec)
558 TempFile tmp(make_temp_path());
560 const std::vector<std::string>
expected = {
561 "alfa",
"beta",
"delta",
"epsilon",
"gama"
566 tree(tmp.path.string(),
false);
567 EXPECT_TRUE(tree.
insert(
"delta"));
568 EXPECT_TRUE(tree.
insert(
"alfa"));
569 EXPECT_TRUE(tree.
insert(
"gama"));
570 EXPECT_TRUE(tree.
insert(
"beta"));
571 EXPECT_TRUE(tree.
insert(
"epsilon"));
572 EXPECT_TRUE(tree.
verify());
577 reopened(tmp.path.string(),
580 EXPECT_TRUE(reopened.
verify());
582 EXPECT_EQ(reopened.
lower_bound(std::string(
"carrot")),
583 std::optional<std::string>(
"delta"));
586TEST(FileBTree, SecondOpenSameProcessIsRejected)
588 TempFile tmp(make_temp_path());
595TEST(FileBTree, ReadOnlySharedOpensAreAllowedAndBlockWriter)
597 TempFile tmp(make_temp_path());
601 EXPECT_TRUE(tree.
insert(10));
602 EXPECT_TRUE(tree.
insert(20));
613 EXPECT_EQ(reader1.
open_mode(), read_only_mode);
614 EXPECT_EQ(
to_vector(reader2.
keys()), (std::vector<int>{10, 20}));
621TEST(FileBTree, ReadOnlyRejectsMutations)
623 TempFile tmp(make_temp_path());
627 EXPECT_TRUE(tree.
insert(10));
634 EXPECT_THROW(reader.
insert(20), std::runtime_error);
635 EXPECT_THROW(reader.
remove(10), std::runtime_error);
636 EXPECT_THROW(reader.
clear(), std::runtime_error);
637 EXPECT_THROW(reader.
sync(), std::runtime_error);
641TEST(FileBTree, ReadOnlyRejectsPendingRecoverySidecars)
643 TempFile tmp(make_temp_path());
647 EXPECT_TRUE(tree.
insert(10));
652 std::ofstream
out(tmp.path.string() +
".wal",
653 std::ios::binary | std::ios::trunc);
654 ASSERT_TRUE(
out.good());
655 out <<
"pending recovery";
664TEST(FileBTree, ReadOnlySharedLocksWorkAcrossProcesses)
666#if defined(__unix__) || defined(__APPLE__)
667 if (running_under_tsan())
668 GTEST_SKIP() <<
"TSAN does not support these fork-based lock checks";
670 TempFile tmp(make_temp_path());
674 EXPECT_TRUE(tree.
insert(10));
681 const int shared_status = run_in_child([&]() ->
int
694 ASSERT_NE(shared_status, -1);
695 ASSERT_TRUE(WIFEXITED(shared_status));
696 EXPECT_EQ(WEXITSTATUS(shared_status), 0);
698 const int writer_status = run_in_child([&]() ->
int
706 catch (
const std::runtime_error &)
715 ASSERT_NE(writer_status, -1);
716 ASSERT_TRUE(WIFEXITED(writer_status));
717 EXPECT_EQ(WEXITSTATUS(writer_status), 0);
719 GTEST_SKIP() <<
"fork-based lock validation is only available on Unix-like systems";
723TEST(FileBTree, ReusesStaleLockSidecar)
725 TempFile tmp(make_temp_path());
728 std::ofstream
out(tmp.path.string() +
".lock",
729 std::ios::binary | std::ios::trunc);
730 ASSERT_TRUE(
out.good());
736 EXPECT_TRUE(tree.
insert(5));
741 EXPECT_EQ(
to_vector(reopened.
keys()), (std::vector<int>{5}));
744TEST(FileBTree, ReloadDiscardsUnsyncedChanges)
746 TempFile tmp(make_temp_path());
749 EXPECT_TRUE(tree.
insert(5));
750 EXPECT_TRUE(tree.
insert(15));
753 EXPECT_TRUE(tree.
insert(25));
754 EXPECT_TRUE(tree.
remove(5));
755 EXPECT_EQ(
to_vector(tree.
keys()), (std::vector<int>{15, 25}));
758 EXPECT_EQ(
to_vector(tree.
keys()), (std::vector<int>{5, 15}));
759 EXPECT_TRUE(tree.
verify());
762TEST(FileBTree, RejectsCorruptSnapshot)
764 TempFile tmp(make_temp_path());
767 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
768 out <<
"not a valid Aleph snapshot";
775TEST(FileBTree, RejectsChecksumCorruption)
777 TempFile tmp(make_temp_path());
781 for (
int value : {10, 20, 30, 40, 50})
785 std::fstream io(tmp.path, std::ios::binary | std::ios::in | std::ios::out);
786 ASSERT_TRUE(io.good());
787 io.seekg(0, std::ios::end);
788 const auto end = io.tellg();
790 io.seekg(end - std::streamoff(1));
794 ASSERT_TRUE(io.good());
795 byte ^=
static_cast<char>(0x5A);
796 io.seekp(end - std::streamoff(1));
798 ASSERT_TRUE(io.good());
805TEST(FileBTree, RecoversFromJournalImage)
807 TempFile tmp(make_temp_path());
808 const auto journal = tmp.path.string() +
".journal";
812 for (
int value : {12, 6, 18, 3, 9, 15, 21})
818 fs::copy_file(tmp.path, journal, fs::copy_options::overwrite_existing, ec);
819 ASSERT_FALSE(ec) << ec.message();
822 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
823 ASSERT_TRUE(
out.good());
828 EXPECT_TRUE(reopened.
verify());
830 (std::vector<int>{3, 6, 9, 12, 15, 18, 21}));
831 EXPECT_FALSE(fs::exists(journal));
834TEST(FileBTree, RecoversFromWalImage)
836 TempFile tmp(make_temp_path());
837 const auto wal = tmp.path.string() +
".wal";
841 for (
int value : {12, 6, 18, 3, 9, 15, 21})
845 write_btree_wal_from_file(tmp.path, wal);
848 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
849 ASSERT_TRUE(
out.good());
854 EXPECT_TRUE(reopened.
verify());
856 (std::vector<int>{3, 6, 9, 12, 15, 18, 21}));
857 EXPECT_FALSE(fs::exists(wal));
860TEST(FileBTree, RejectsCorruptWalCommitTrailer)
862 TempFile tmp(make_temp_path());
863 const auto wal = tmp.path.string() +
".wal";
867 for (
int value : {12, 6, 18, 3, 9, 15, 21})
871 write_btree_wal_from_file(tmp.path, wal);
873 std::fstream wal_io(wal, std::ios::binary | std::ios::in | std::ios::out);
874 ASSERT_TRUE(wal_io.good());
875 wal_io.seekg(0, std::ios::end);
876 const auto wal_end = wal_io.tellg();
877 ASSERT_GT(wal_end, 0);
878 wal_io.seekg(wal_end - std::streamoff(1));
881 wal_io.read(&
byte, 1);
882 ASSERT_TRUE(wal_io.good());
883 byte ^=
static_cast<char>(0x11);
884 wal_io.seekp(wal_end - std::streamoff(1));
885 wal_io.write(&
byte, 1);
886 ASSERT_TRUE(wal_io.good());
890 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
891 ASSERT_TRUE(
out.good());
899TEST(FileBTree, RecoversFromPartiallyCheckpointedWal)
901 constexpr size_t header_bytes =
903 +
sizeof(std::uint64_t) * 5 +
sizeof(std::uint8_t) + 7;
904 constexpr size_t page_bytes =
905 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
906 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * 5
907 +
sizeof(std::uint64_t) * 6 +
sizeof(std::uint32_t);
909 TempFile tmp(make_temp_path());
910 const auto wal = tmp.path.string() +
".wal";
911 const auto old_snapshot = tmp.path.string() +
".old";
912 const auto new_snapshot = tmp.path.string() +
".new";
922 fs::copy_file(tmp.path, old_snapshot, fs::copy_options::overwrite_existing, ec);
923 ASSERT_FALSE(ec) << ec.message();
925 const int root_key = read_btree_root_first_key(tmp.path);
929 EXPECT_TRUE(tree.
remove(root_key));
933 fs::copy_file(tmp.path, new_snapshot, fs::copy_options::overwrite_existing, ec);
934 ASSERT_FALSE(ec) << ec.message();
935 ASSERT_EQ(read_btree_page_count(old_snapshot), read_btree_page_count(new_snapshot));
937 write_btree_wal_from_file(new_snapshot, wal);
938 const auto dirty_pages = read_btree_wal_page_ids(wal);
939 ASSERT_GE(dirty_pages.size(), 2u);
941 fs::copy_file(old_snapshot, tmp.path, fs::copy_options::overwrite_existing, ec);
942 ASSERT_FALSE(ec) << ec.message();
943 copy_region(new_snapshot, tmp.path, 0, header_bytes);
944 copy_region(new_snapshot, tmp.path,
945 static_cast<std::streamoff
>(header_bytes
946 + (dirty_pages.front() - 1) * page_bytes),
951 EXPECT_TRUE(reopened.
verify());
953 EXPECT_FALSE(fs::exists(wal));
956TEST(FileBTree, IgnoresStaleWalWhenMainFileIsNewer)
958 TempFile tmp(make_temp_path());
959 const auto wal = tmp.path.string() +
".wal";
960 const auto old_snapshot = tmp.path.string() +
".old";
964 for (
int value : {12, 6, 18, 3, 9, 15, 21})
970 fs::copy_file(tmp.path, old_snapshot, fs::copy_options::overwrite_existing, ec);
971 ASSERT_FALSE(ec) << ec.message();
975 EXPECT_TRUE(tree.
insert(24));
976 EXPECT_TRUE(tree.
insert(27));
980 write_btree_wal_from_file(old_snapshot, wal);
981 fs::remove(old_snapshot, ec);
984 EXPECT_TRUE(reopened.
verify());
986 (std::vector<int>{3, 6, 9, 12, 15, 18, 21, 24, 27}));
987 EXPECT_FALSE(fs::exists(wal));
990TEST(FileBTree, RejectsBPlusTreeFormat)
992 TempFile tmp(make_temp_path());
996 EXPECT_TRUE(tree.
insert(1));
997 EXPECT_TRUE(tree.
insert(2));
1001 std::runtime_error);
1004TEST(FileBTree, ReopenAfterSplitsAndMergesPreservesStructure)
1006 TempFile tmp(make_temp_path());
1013 for (
int value : {1, 2, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 80})
1016 EXPECT_TRUE(tree.
verify());
1021 EXPECT_TRUE(reopened.
verify());
1022 EXPECT_EQ(reopened.
min_key(), std::optional<int>(4));
1023 EXPECT_EQ(reopened.
max_key(), std::optional<int>(79));
1026 4, 5, 6, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 23, 24, 25,
1027 26, 27, 28, 29, 30, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
1028 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
1029 61, 62, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79
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.
Persistent page-managed B+ Tree stored in a single binary file.
bool insert(const Key &key)
Insert a key if it is not already present.
Persistent page-managed B-Tree stored in a single binary file.
bool verify() const
Verify structural B-Tree invariants across cached pages.
void set_auto_sync(const bool enabled) noexcept
Enable or disable automatic flushing of dirty pages.
bool contains(const Key &key) const
Return whether a key is present.
std::optional< Key > max_key() const
Return the maximum key, if any.
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.
Array< Key > keys() const
Materialize the tree contents in sorted order.
std::optional< Key > min_key() const
Return the minimum key, if any.
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
void checkpoint() const
Synonym for sync().
Paged_File_Open_Mode open_mode() const noexcept
Return the access mode used to open the file.
void sync() const
Flush the current tree image using redo WAL or full-image fallback.
bool remove(const Key &key)
Remove a key if present.
void reload()
Discard unsynchronized changes and reload pages from disk.
std::uint64_t checkpoint_sequence() const noexcept
Return the durable checkpoint sequence stored in the backing file.
bool is_read_only() const noexcept
Return whether this handle is read-only.
bool insert(const Key &key)
Insert a key if it is not already present.
Minimal std::expected-style result type for C++20.
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
constexpr std::array< char, Ordered_Tree_Snapshot_Magic_Size > ordered_tree_snapshot_magic(const char(&text)[N]) noexcept
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
std::uint32_t crc32_finish(const std::uint32_t crc) noexcept
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
Page-managed persistent B-Tree with file-backed storage.
Page-managed persistent B+ Tree with file-backed storage.