36#include <gtest/gtest.h>
51#if defined(__unix__) || defined(__APPLE__)
62 namespace fs = std::filesystem;
65 long long process_id() noexcept
68 return static_cast<long long>(_getpid());
70 return static_cast<long long>(getpid());
74 fs::path make_temp_path()
84 static std::atomic<unsigned long long>
counter{0};
85 const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
86 const auto id = std::to_string(now) +
"_" + std::to_string(process_id()) +
87 "_" + std::to_string(
counter++);
88 const fs::path dir = fs::temp_directory_path() /
"aleph_file_bplustree_tests";
89 fs::create_directories(dir);
90 return dir / (
id +
".idx");
97 explicit TempFile(fs::path p) : path(
std::move(p)) {}
102 fs::remove(path, ec);
103 fs::remove(path.string() +
".wal", ec);
104 fs::remove(path.string() +
".wal.tmp", ec);
105 fs::remove(path.string() +
".journal", ec);
106 fs::remove(path.string() +
".journal.tmp", ec);
107 fs::remove(path.string() +
".lock", ec);
108 fs::remove(path.string() +
".old", ec);
109 fs::remove(path.string() +
".new", ec);
113 template <
typename T>
117 ret.reserve(arr.
size());
118 for (
size_t i = 0; i < arr.
size(); ++i)
119 ret.push_back(arr[i]);
123#if defined(__unix__) || defined(__APPLE__)
124 constexpr bool running_under_tsan() noexcept
126# if defined(__SANITIZE_THREAD__)
128# elif defined(__has_feature)
129# if __has_feature(thread_sanitizer)
139 template <
typename F>
140 int run_in_child(
F && fn)
142 const pid_t pid = ::fork();
161 if (::waitpid(pid, &status, 0) < 0)
167 constexpr std::uint32_t file_bplus_tree_test_file_version = 5;
168 constexpr std::uint32_t file_bplus_tree_test_wal_version = 4;
169 constexpr std::uint32_t file_bplus_tree_test_key_size =
171 constexpr std::uint64_t file_bplus_tree_test_min_degree = 3;
172 constexpr std::uint8_t file_bplus_tree_test_encoding = 3;
174 std::array<char, 7> file_bplus_tree_reserved_bytes()
176 std::array<char, 7> reserved = {};
178 reserved[0] =
static_cast<char>(codec_id & 0xFFu);
179 reserved[1] =
static_cast<char>((codec_id >> 8) & 0xFFu);
180 reserved[2] =
static_cast<char>((codec_id >> 16) & 0xFFu);
181 reserved[3] =
static_cast<char>((codec_id >> 24) & 0xFFu);
185 std::uint32_t bplus_wal_checksum(
const std::uint32_t wal_version,
186 const std::uint64_t
size,
187 const std::uint64_t root_page,
188 const std::uint64_t first_leaf_page,
189 const std::uint64_t page_count,
190 const std::uint64_t free_page_head,
191 const std::uint64_t checkpoint_sequence,
192 const std::uint64_t dirty_count)
195 const auto reserved = file_bplus_tree_reserved_bytes();
212 std::uint32_t bplus_wal_payload_checksum(
const std::uint64_t page_count,
213 const size_t page_bytes,
214 const std::vector<char> & page_blob)
217 for (std::uint64_t page_id = 1; page_id <= page_count; ++page_id)
221 crc, page_blob.data() + (page_id - 1) * page_bytes, page_bytes);
226 void write_bplus_wal_from_file(
const fs::path & data_path,
227 const fs::path & wal_path)
229 constexpr std::uint32_t file_version = file_bplus_tree_test_file_version;
230 constexpr std::uint32_t wal_version = file_bplus_tree_test_wal_version;
231 constexpr std::uint32_t key_size = file_bplus_tree_test_key_size;
232 constexpr std::uint64_t min_degree = file_bplus_tree_test_min_degree;
233 constexpr std::uint8_t encoding = file_bplus_tree_test_encoding;
234 constexpr size_t max_keys = 5;
235 constexpr size_t max_children = 6;
236 constexpr size_t page_bytes =
237 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
238 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * max_keys
239 +
sizeof(std::uint64_t) * max_children +
sizeof(std::uint32_t);
241 std::ifstream in(data_path, std::ios::binary);
242 ASSERT_TRUE(in.good());
244 std::array<char, Aleph::detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
245 in.read(magic.data(), magic.size());
246 ASSERT_TRUE(in.good());
248 auto read_u32 = [&](std::uint32_t &
value)
250 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
251 ASSERT_TRUE(in.good());
253 auto read_u64 = [&](std::uint64_t &
value)
255 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
256 ASSERT_TRUE(in.good());
258 auto read_u8 = [&](std::uint8_t &
value)
260 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
261 ASSERT_TRUE(in.good());
264 std::uint32_t version = 0;
265 std::uint32_t file_key_size = 0;
266 std::uint64_t file_min_degree = 0;
267 std::uint8_t file_encoding = 0;
269 read_u32(file_key_size);
270 read_u64(file_min_degree);
271 read_u8(file_encoding);
272 ASSERT_EQ(version, file_version);
273 ASSERT_EQ(file_key_size, key_size);
274 ASSERT_EQ(file_min_degree, min_degree);
275 ASSERT_EQ(file_encoding, encoding);
277 const auto reserved = file_bplus_tree_reserved_bytes();
278 std::array<char, 7> stored_reserved = {};
279 in.read(stored_reserved.data(), stored_reserved.size());
280 ASSERT_TRUE(in.good());
281 ASSERT_EQ(stored_reserved, reserved);
283 std::uint64_t
size = 0;
284 std::uint64_t root_page = 0;
285 std::uint64_t first_leaf_page = 0;
286 std::uint64_t page_count = 0;
287 std::uint64_t free_page_head = 0;
288 std::uint64_t checkpoint_sequence = 0;
289 std::uint32_t checksum = 0;
292 read_u64(first_leaf_page);
293 read_u64(page_count);
294 read_u64(free_page_head);
295 read_u64(checkpoint_sequence);
298 std::vector<char> page_blob(page_bytes * page_count);
299 in.read(page_blob.data(), page_blob.size());
300 ASSERT_TRUE(in.good());
302 std::ofstream
out(wal_path, std::ios::binary | std::ios::trunc);
303 ASSERT_TRUE(
out.good());
305 const auto wal_magic =
307 const auto trailer_magic =
309 out.write(wal_magic.data(), wal_magic.size());
310 out.write(
reinterpret_cast<const char *
>(&wal_version),
sizeof(wal_version));
311 out.write(
reinterpret_cast<const char *
>(&key_size),
sizeof(key_size));
312 out.write(
reinterpret_cast<const char *
>(&min_degree),
sizeof(min_degree));
313 out.write(
reinterpret_cast<const char *
>(&encoding),
sizeof(encoding));
314 out.write(reserved.data(), reserved.size());
315 out.write(
reinterpret_cast<const char *
>(&
size),
sizeof(
size));
316 out.write(
reinterpret_cast<const char *
>(&root_page),
sizeof(root_page));
317 out.write(
reinterpret_cast<const char *
>(&first_leaf_page),
sizeof(first_leaf_page));
318 out.write(
reinterpret_cast<const char *
>(&page_count),
sizeof(page_count));
319 out.write(
reinterpret_cast<const char *
>(&free_page_head),
sizeof(free_page_head));
320 out.write(
reinterpret_cast<const char *
>(&checkpoint_sequence),
sizeof(checkpoint_sequence));
321 out.write(
reinterpret_cast<const char *
>(&page_count),
sizeof(page_count));
322 const auto wal_checksum = bplus_wal_checksum(
324 size, root_page, first_leaf_page, page_count, free_page_head,
325 checkpoint_sequence, page_count);
326 out.write(
reinterpret_cast<const char *
>(&wal_checksum),
sizeof(wal_checksum));
328 for (std::uint64_t page_id = 1; page_id <= page_count; ++page_id)
330 out.write(
reinterpret_cast<const char *
>(&page_id),
sizeof(page_id));
331 out.write(page_blob.data() + (page_id - 1) * page_bytes, page_bytes);
334 const auto payload_checksum =
335 bplus_wal_payload_checksum(page_count, page_bytes, page_blob);
336 out.write(trailer_magic.data(), trailer_magic.size());
337 out.write(
reinterpret_cast<const char *
>(&checkpoint_sequence),
338 sizeof(checkpoint_sequence));
339 out.write(
reinterpret_cast<const char *
>(&page_count),
sizeof(page_count));
340 out.write(
reinterpret_cast<const char *
>(&payload_checksum),
341 sizeof(payload_checksum));
343 ASSERT_TRUE(
out.good());
346 std::uint64_t read_bplus_page_count(
const fs::path & data_path)
348 std::ifstream in(data_path, std::ios::binary);
349 EXPECT_TRUE(in.good());
354 +
sizeof(std::uint32_t) * 2 +
sizeof(std::uint64_t)
355 +
sizeof(std::uint8_t) + 7 +
sizeof(std::uint64_t) * 3);
356 EXPECT_TRUE(in.good());
357 std::uint64_t page_count = 0;
358 in.read(
reinterpret_cast<char *
>(&page_count),
sizeof(page_count));
359 EXPECT_TRUE(in.good());
363 std::vector<std::uint64_t> read_bplus_wal_page_ids(
const fs::path & wal_path)
365 constexpr size_t page_bytes =
366 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
367 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * 5
368 +
sizeof(std::uint64_t) * 6 +
sizeof(std::uint32_t);
370 std::ifstream in(wal_path, std::ios::binary);
371 EXPECT_TRUE(in.good());
372 std::vector<std::uint64_t> page_ids;
376 auto read_u32 = [&](std::uint32_t &
value)
378 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
379 EXPECT_TRUE(in.good());
381 auto read_u64 = [&](std::uint64_t &
value)
383 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
384 EXPECT_TRUE(in.good());
386 auto read_u8 = [&](std::uint8_t &
value)
388 in.read(
reinterpret_cast<char *
>(&
value),
sizeof(
value));
389 EXPECT_TRUE(in.good());
392 std::array<char, Aleph::detail::Ordered_Tree_Snapshot_Magic_Size> magic = {};
393 std::array<char, 7> reserved = {};
394 std::array<char, Aleph::detail::Ordered_Tree_Snapshot_Magic_Size>
396 std::uint32_t wal_version = 0;
397 std::uint32_t key_size = 0;
398 std::uint64_t min_degree = 0;
399 std::uint8_t encoding = 0;
400 std::uint64_t ignored64 = 0;
401 std::uint32_t ignored32 = 0;
402 std::uint64_t dirty_count = 0;
404 in.read(magic.data(), magic.size());
405 EXPECT_TRUE(in.good());
406 read_u32(wal_version);
408 read_u64(min_degree);
410 in.read(reserved.data(), reserved.size());
411 EXPECT_TRUE(in.good());
418 read_u64(dirty_count);
421 page_ids.reserve(dirty_count);
422 for (std::uint64_t i = 0; i < dirty_count; ++i)
424 std::uint64_t page_id = 0;
426 page_ids.push_back(page_id);
427 in.seekg(
static_cast<std::streamoff
>(page_bytes), std::ios::cur);
428 EXPECT_TRUE(in.good());
431 in.read(trailer_magic.data(), trailer_magic.size());
432 EXPECT_TRUE(in.good());
436 void copy_region(
const fs::path & src,
const fs::path & dst,
437 const std::streamoff
offset,
const size_t size)
439 std::ifstream in(src, std::ios::binary);
440 ASSERT_TRUE(in.good());
441 std::fstream
out(dst, std::ios::binary | std::ios::in | std::ios::out);
442 ASSERT_TRUE(
out.good());
444 std::vector<char> buffer(
size);
446 ASSERT_TRUE(in.good());
447 in.read(buffer.data(), buffer.size());
448 ASSERT_TRUE(in.good());
451 ASSERT_TRUE(
out.good());
452 out.write(buffer.data(), buffer.size());
453 ASSERT_TRUE(
out.good());
457TEST(FileBPlusTree, ReopensAndPreservesRangeQueries)
459 TempFile tmp(make_temp_path());
463 for (
int value : {105, 110, 115, 120, 125, 130, 135, 140, 145, 150})
466 EXPECT_TRUE(tree.
verify());
468 (std::vector<int>{120, 125, 130, 135, 140}));
472 EXPECT_TRUE(reopened.
verify());
474 (std::vector<int>{105, 110, 115, 120, 125, 130, 135, 140, 145, 150}));
476 (std::vector<int>{120, 125, 130, 135, 140}));
477 EXPECT_EQ(reopened.
lower_bound(126), std::optional<int>(130));
478 EXPECT_EQ(reopened.
upper_bound(140), std::optional<int>(145));
481TEST(FileBPlusTree, ReloadRestoresPersistedLeafSequence)
483 TempFile tmp(make_temp_path());
486 for (
int value : {10, 20, 30, 40, 50})
490 EXPECT_TRUE(tree.
insert(60));
491 EXPECT_TRUE(tree.
remove(20));
493 (std::vector<int>{10, 30, 40, 50, 60}));
497 (std::vector<int>{10, 20, 30, 40, 50}));
498 EXPECT_TRUE(tree.
verify());
501TEST(FileBPlusTree, LazyIteratorsTraverseLeafChain)
503 TempFile tmp(make_temp_path());
506 for (
int value : {105, 110, 115, 120, 125, 130, 135, 140, 145, 150})
509 std::vector<int> all_keys;
511 all_keys.push_back(it.get_curr());
513 (std::vector<int>{105, 110, 115, 120, 125, 130, 135, 140, 145, 150}));
515 std::vector<int> band;
517 ASSERT_TRUE(band_it.has_curr());
518 EXPECT_EQ(*band_it, 120);
519 EXPECT_EQ(band_it.get_curr(), 120);
520 band.push_back(band_it.get_curr());
522 for (; band_it.has_curr(); band_it.next_ne())
523 band.push_back(band_it.get_curr());
525 EXPECT_EQ(band, (std::vector<int>{120, 125, 130, 135, 140}));
528TEST(FileBPlusTree, SupportsBoundedStringCodec)
530 TempFile tmp(make_temp_path());
532 const std::vector<std::string>
expected = {
533 "alfa",
"beta",
"delta",
"epsilon",
"gama"
538 tree(tmp.path.string(),
false);
539 EXPECT_TRUE(tree.
insert(
"delta"));
540 EXPECT_TRUE(tree.
insert(
"alfa"));
541 EXPECT_TRUE(tree.
insert(
"gama"));
542 EXPECT_TRUE(tree.
insert(
"beta"));
543 EXPECT_TRUE(tree.
insert(
"epsilon"));
544 EXPECT_TRUE(tree.
verify());
545 EXPECT_EQ(
to_vector(tree.
range(std::string(
"beta"), std::string(
"epsilon"))),
546 (std::vector<std::string>{
"beta",
"delta",
"epsilon"}));
551 reopened(tmp.path.string(),
554 EXPECT_TRUE(reopened.
verify());
557 std::vector<std::string> band;
558 for (
auto it = reopened.
get_range_it(std::string(
"beta"),
559 std::string(
"epsilon"));
560 it.has_curr(); it.next())
561 band.push_back(it.get_curr());
562 EXPECT_EQ(band, (std::vector<std::string>{
"beta",
"delta",
"epsilon"}));
565TEST(FileBPlusTree, BoundedStringCodecRejectsOversizedKeys)
567 TempFile tmp(make_temp_path());
571 tree(tmp.path.string(),
false);
572 EXPECT_TRUE(tree.
insert(
"demasiado-largo"));
573 EXPECT_THROW(tree.
sync(), std::runtime_error);
574 EXPECT_TRUE(tree.
verify());
577TEST(FileBPlusTree, CheckpointSequenceAdvancesAndPersists)
579 TempFile tmp(make_temp_path());
581 std::uint64_t first = 0;
582 std::uint64_t second = 0;
587 EXPECT_TRUE(tree.
insert(10));
590 EXPECT_GT(first, 1u);
592 EXPECT_TRUE(tree.
insert(20));
595 EXPECT_GT(second, first);
602TEST(FileBPlusTree, SecondOpenSameProcessIsRejected)
604 TempFile tmp(make_temp_path());
611TEST(FileBPlusTree, ReadOnlySharedOpensAreAllowedAndBlockWriter)
613 TempFile tmp(make_temp_path());
617 for (
int value : {10, 20, 30, 40})
626 const auto read_only_mode =
630 EXPECT_EQ(reader1.
open_mode(), read_only_mode);
631 EXPECT_EQ(
to_vector(reader2.
range(0, 50)), (std::vector<int>{10, 20, 30, 40}));
638TEST(FileBPlusTree, ReadOnlyRejectsMutations)
640 TempFile tmp(make_temp_path());
644 EXPECT_TRUE(tree.
insert(10));
651 EXPECT_THROW(reader.
insert(20), std::runtime_error);
652 EXPECT_THROW(reader.
remove(10), std::runtime_error);
653 EXPECT_THROW(reader.
clear(), std::runtime_error);
654 EXPECT_THROW(reader.
sync(), std::runtime_error);
655 EXPECT_EQ(
to_vector(reader.
range(0, 20)), (std::vector<int>{10}));
658TEST(FileBPlusTree, ReadOnlyRejectsPendingRecoverySidecars)
660 TempFile tmp(make_temp_path());
664 EXPECT_TRUE(tree.
insert(10));
669 std::ofstream
out(tmp.path.string() +
".wal",
670 std::ios::binary | std::ios::trunc);
671 ASSERT_TRUE(
out.good());
672 out <<
"pending recovery";
681TEST(FileBPlusTree, ReadOnlySharedLocksWorkAcrossProcesses)
683#if defined(__unix__) || defined(__APPLE__)
684 if (running_under_tsan())
685 GTEST_SKIP() <<
"TSAN does not support these fork-based lock checks";
687 TempFile tmp(make_temp_path());
691 EXPECT_TRUE(tree.
insert(10));
692 EXPECT_TRUE(tree.
insert(20));
699 const int shared_status = run_in_child([&]() ->
int
705 return child.
range(0, 30).
size() == 2 ? 0 : 2;
712 ASSERT_NE(shared_status, -1);
713 ASSERT_TRUE(WIFEXITED(shared_status));
714 EXPECT_EQ(WEXITSTATUS(shared_status), 0);
716 const int writer_status = run_in_child([&]() ->
int
724 catch (
const std::runtime_error &)
733 ASSERT_NE(writer_status, -1);
734 ASSERT_TRUE(WIFEXITED(writer_status));
735 EXPECT_EQ(WEXITSTATUS(writer_status), 0);
737 GTEST_SKIP() <<
"fork-based lock validation is only available on Unix-like systems";
741TEST(FileBPlusTree, ReusesStaleLockSidecar)
743 TempFile tmp(make_temp_path());
746 std::ofstream
out(tmp.path.string() +
".lock",
747 std::ios::binary | std::ios::trunc);
748 ASSERT_TRUE(
out.good());
754 EXPECT_TRUE(tree.
insert(5));
759 EXPECT_EQ(
to_vector(reopened.
range(0, 10)), (std::vector<int>{5}));
762TEST(FileBPlusTree, RejectsBTreeSnapshotFormat)
764 TempFile tmp(make_temp_path());
768 EXPECT_TRUE(tree.
insert(1));
769 EXPECT_TRUE(tree.
insert(2));
776TEST(FileBPlusTree, RejectsChecksumCorruption)
778 TempFile tmp(make_temp_path());
782 for (
int value : {5, 10, 15, 20, 25, 30})
786 std::fstream io(tmp.path, std::ios::binary | std::ios::in | std::ios::out);
787 ASSERT_TRUE(io.good());
788 io.seekg(0, std::ios::end);
789 const auto end = io.tellg();
791 io.seekg(end - std::streamoff(1));
795 ASSERT_TRUE(io.good());
796 byte ^=
static_cast<char>(0x33);
797 io.seekp(end - std::streamoff(1));
799 ASSERT_TRUE(io.good());
806TEST(FileBPlusTree, RecoversFromJournalImage)
808 TempFile tmp(make_temp_path());
809 const auto journal = tmp.path.string() +
".journal";
813 for (
int value : {25, 5, 35, 15, 45, 30, 10, 20, 40})
819 fs::copy_file(tmp.path, journal, fs::copy_options::overwrite_existing, ec);
820 ASSERT_FALSE(ec) << ec.message();
823 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
824 ASSERT_TRUE(
out.good());
829 EXPECT_TRUE(reopened.
verify());
831 (std::vector<int>{5, 10, 15, 20, 25, 30, 35, 40, 45}));
832 EXPECT_FALSE(fs::exists(journal));
835TEST(FileBPlusTree, RecoversFromWalImage)
837 TempFile tmp(make_temp_path());
838 const auto wal = tmp.path.string() +
".wal";
842 for (
int value : {25, 5, 35, 15, 45, 30, 10, 20, 40})
846 write_bplus_wal_from_file(tmp.path, wal);
849 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
850 ASSERT_TRUE(
out.good());
855 EXPECT_TRUE(reopened.
verify());
857 (std::vector<int>{5, 10, 15, 20, 25, 30, 35, 40, 45}));
858 EXPECT_FALSE(fs::exists(wal));
861TEST(FileBPlusTree, RejectsCorruptWalCommitTrailer)
863 TempFile tmp(make_temp_path());
864 const auto wal = tmp.path.string() +
".wal";
868 for (
int value : {25, 5, 35, 15, 45, 30, 10, 20, 40})
872 write_bplus_wal_from_file(tmp.path, wal);
874 std::fstream wal_io(wal, std::ios::binary | std::ios::in | std::ios::out);
875 ASSERT_TRUE(wal_io.good());
876 wal_io.seekg(0, std::ios::end);
877 const auto wal_end = wal_io.tellg();
878 ASSERT_GT(wal_end, 0);
879 wal_io.seekg(wal_end - std::streamoff(1));
882 wal_io.read(&
byte, 1);
883 ASSERT_TRUE(wal_io.good());
884 byte ^=
static_cast<char>(0x11);
885 wal_io.seekp(wal_end - std::streamoff(1));
886 wal_io.write(&
byte, 1);
887 ASSERT_TRUE(wal_io.good());
891 std::ofstream
out(tmp.path, std::ios::binary | std::ios::trunc);
892 ASSERT_TRUE(
out.good());
900TEST(FileBPlusTree, RecoversFromPartiallyCheckpointedWal)
902 constexpr size_t header_bytes =
904 +
sizeof(std::uint64_t) * 6 +
sizeof(std::uint8_t) + 7;
905 constexpr size_t page_bytes =
906 sizeof(std::uint8_t) +
sizeof(std::uint16_t) * 2 + 3
907 +
sizeof(std::uint64_t) +
sizeof(std::uint64_t) +
sizeof(int) * 5
908 +
sizeof(std::uint64_t) * 6 +
sizeof(std::uint32_t);
910 TempFile tmp(make_temp_path());
911 const auto wal = tmp.path.string() +
".wal";
912 const auto old_snapshot = tmp.path.string() +
".old";
913 const auto new_snapshot = tmp.path.string() +
".new";
917 for (
int value : {105, 110, 115, 120, 125, 130, 135, 140, 145, 150})
923 fs::copy_file(tmp.path, old_snapshot, fs::copy_options::overwrite_existing, ec);
924 ASSERT_FALSE(ec) << ec.message();
928 EXPECT_TRUE(tree.
remove(120));
932 fs::copy_file(tmp.path, new_snapshot, fs::copy_options::overwrite_existing, ec);
933 ASSERT_FALSE(ec) << ec.message();
934 ASSERT_EQ(read_bplus_page_count(old_snapshot), read_bplus_page_count(new_snapshot));
936 write_bplus_wal_from_file(new_snapshot, wal);
937 const auto dirty_pages = read_bplus_wal_page_ids(wal);
938 ASSERT_GE(dirty_pages.size(), 2u);
940 fs::copy_file(old_snapshot, tmp.path, fs::copy_options::overwrite_existing, ec);
941 ASSERT_FALSE(ec) << ec.message();
942 copy_region(new_snapshot, tmp.path, 0, header_bytes);
943 copy_region(new_snapshot, tmp.path,
944 static_cast<std::streamoff
>(header_bytes
945 + (dirty_pages.front() - 1) * page_bytes),
950 EXPECT_TRUE(reopened.
verify());
952 EXPECT_FALSE(fs::exists(wal));
955TEST(FileBPlusTree, IgnoresStaleWalWhenMainFileIsNewer)
957 TempFile tmp(make_temp_path());
958 const auto wal = tmp.path.string() +
".wal";
959 const auto old_snapshot = tmp.path.string() +
".old";
963 for (
int value : {25, 5, 35, 15, 45, 30, 10, 20, 40})
969 fs::copy_file(tmp.path, old_snapshot, fs::copy_options::overwrite_existing, ec);
970 ASSERT_FALSE(ec) << ec.message();
974 EXPECT_TRUE(tree.
insert(50));
975 EXPECT_TRUE(tree.
insert(55));
979 write_bplus_wal_from_file(old_snapshot, wal);
980 fs::remove(old_snapshot, ec);
983 EXPECT_TRUE(reopened.
verify());
985 (std::vector<int>{5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55}));
986 EXPECT_FALSE(fs::exists(wal));
989TEST(FileBPlusTree, ReopenAfterSplitsAndMergesPreservesStructure)
991 TempFile tmp(make_temp_path());
998 for (
int value : {1, 2, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 80})
1001 EXPECT_TRUE(tree.
verify());
1006 EXPECT_TRUE(reopened.
verify());
1007 EXPECT_EQ(reopened.
min_key(), std::optional<int>(4));
1008 EXPECT_EQ(reopened.
max_key(), std::optional<int>(79));
1010 (std::vector<int>{10, 11, 12, 13, 14, 18, 19, 20}));
1011 EXPECT_FALSE(reopened.
contains(1));
1012 EXPECT_FALSE(reopened.
contains(64));
1013 EXPECT_TRUE(reopened.
contains(66));
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.
bool has_curr() const noexcept
Return whether the iterator still points to a key.
Persistent page-managed B+ Tree stored in a single binary 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.
Iterator get_it() const noexcept
Return a lazy iterator over the full key order.
Paged_File_Open_Mode open_mode() const noexcept
Return the access mode used to open the file.
std::optional< Key > max_key() const
Return the maximum key, if any.
std::uint64_t checkpoint_sequence() const noexcept
Return the durable checkpoint sequence stored in the backing file.
void sync() const
Flush the current tree image using redo WAL or full-image fallback.
bool verify() const
Verify structural B+ Tree invariants across cached pages.
bool remove(const Key &key)
Remove a key if present.
void reload()
Discard unsynchronized changes and reload pages from disk.
void clear()
Remove every key from the tree.
std::optional< Key > min_key() const
Return the minimum key, if any.
Array< Key > range(const Key &first, const Key &last) const
Collect all keys in the inclusive range [first, last].
void checkpoint() const
Synonym for sync().
Array< Key > keys() const
Materialize the tree contents in sorted order.
Iterator get_range_it(const Key &first, const Key &last) const
Return a lazy iterator over an inclusive key range.
bool contains(const Key &key) const
Return whether a key is present.
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
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.
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.