39#include <system_error>
48 namespace fs = std::filesystem;
53 std::cout << label <<
" [";
54 for (
size_t i = 0; i < values.
size(); ++i)
58 std::cout << values[i];
66 const fs::path file_path =
67 fs::temp_directory_path() /
"aleph_file_b_tree_example.idx";
72 fs::remove(file_path, ec);
73 fs::remove(file_path.string() +
".wal", ec);
74 fs::remove(file_path.string() +
".wal.tmp", ec);
75 fs::remove(file_path.string() +
".journal", ec);
76 fs::remove(file_path.string() +
".journal.tmp", ec);
77 fs::remove(file_path.string() +
".lock", ec);
79 std::cout <<
"Persistent B-Tree example\n";
80 std::cout <<
"=========================\n";
81 std::cout <<
"file path : " << file_path.string() <<
"\n";
82 std::cout <<
"sidecars : " << file_path.string() <<
".lock, "
83 << file_path.string() <<
".wal, "
84 << file_path.string() <<
".wal.tmp, "
85 << file_path.string() <<
".journal, "
86 << file_path.string() <<
".journal.tmp\n\n";
90 for (
int value : {40, 10, 90, 20, 70, 60, 30, 50, 80})
98 <<
"File_B_Tree example produced an invalid in-memory tree";
100 std::cout <<
"first session\n";
101 std::cout <<
"-------------\n";
103 std::cout <<
"height : " << catalog.
height() <<
"\n";
105 std::cout <<
"page count : " << catalog.
page_count() <<
"\n";
106 std::cout <<
"auto sync : " << std::boolalpha
113 std::cout <<
"after reopen\n";
114 std::cout <<
"------------\n";
116 std::cout <<
"page count : " << reopened.
page_count() <<
"\n";
118 if (
const auto lb = reopened.
lower_bound(64); lb.has_value())
119 std::cout <<
"lower_bound(64) -> " << *lb <<
"\n";
121 if (
const auto ub = reopened.
upper_bound(70); ub.has_value())
122 std::cout <<
"upper_bound(70) -> " << *ub <<
"\n";
125 fs::remove(file_path, ec);
126 fs::remove(file_path.string() +
".wal", ec);
127 fs::remove(file_path.string() +
".wal.tmp", ec);
128 fs::remove(file_path.string() +
".journal", ec);
129 fs::remove(file_path.string() +
".journal.tmp", ec);
130 fs::remove(file_path.string() +
".lock", ec);
132 catch (
const std::exception & ex)
134 std::cerr <<
"file_b_tree_example failed: " << ex.what() <<
'\n';
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
size_t size_t int32_t value
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.
size_t page_count() const noexcept
Return the number of allocated pages currently tracked.
bool verify() const
Verify structural B-Tree invariants across cached pages.
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
size_t height() const noexcept
Return the current height of the paged tree.
Array< Key > keys() const
Materialize the tree contents in sorted order.
size_t page_size_bytes() const noexcept
Return the fixed serialized page size.
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
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.
bool insert(const Key &key)
Insert a key if it is not already present.
Main namespace for Aleph-w library functions.
void print_array(const char *label, const Container &a)
Page-managed persistent B-Tree with file-backed storage.