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];
63 template <
typename Tree>
64 void print_iter_band(
const char * label,
69 std::cout << label <<
" [";
70 bool first_item =
true;
71 for (
auto it = tree.get_range_it(first, last); it.has_curr(); it.next_ne())
75 std::cout << it.get_curr();
84 const fs::path file_path =
85 fs::temp_directory_path() /
"aleph_file_bplus_tree_example.idx";
90 fs::remove(file_path, ec);
91 fs::remove(file_path.string() +
".wal", ec);
92 fs::remove(file_path.string() +
".wal.tmp", ec);
93 fs::remove(file_path.string() +
".journal", ec);
94 fs::remove(file_path.string() +
".journal.tmp", ec);
95 fs::remove(file_path.string() +
".lock", ec);
97 std::cout <<
"Persistent B+ Tree example\n";
98 std::cout <<
"==========================\n";
99 std::cout <<
"file path : " << file_path.string() <<
"\n";
100 std::cout <<
"sidecars : " << file_path.string() <<
".lock, "
101 << file_path.string() <<
".wal, "
102 << file_path.string() <<
".wal.tmp, "
103 << file_path.string() <<
".journal, "
104 << file_path.string() <<
".journal.tmp\n\n";
109 for (
int value : {105, 110, 115, 120, 125, 130, 135, 140, 145, 150})
117 <<
"File_BPlus_Tree example produced an invalid in-memory tree";
119 std::cout <<
"first session\n";
120 std::cout <<
"-------------\n";
123 print_iter_band(
"iter 118..146 :", ledger, 118, 146);
125 std::cout <<
"page count : " << ledger.
page_count() <<
"\n";
126 std::cout <<
"auto sync : " << std::boolalpha
133 std::cout <<
"after reopen\n";
134 std::cout <<
"------------\n";
137 print_iter_band(
"iter 118..146 :", reopened, 118, 146);
138 std::cout <<
"page count : " << reopened.
page_count() <<
"\n";
140 if (
const auto ub = reopened.
upper_bound(145); ub.has_value())
141 std::cout <<
"upper_bound(145) -> " << *ub <<
"\n";
144 fs::remove(file_path, ec);
145 fs::remove(file_path.string() +
".wal", ec);
146 fs::remove(file_path.string() +
".wal.tmp", ec);
147 fs::remove(file_path.string() +
".journal", ec);
148 fs::remove(file_path.string() +
".journal.tmp", ec);
149 fs::remove(file_path.string() +
".lock", ec);
151 catch (
const std::exception & ex)
153 std::cerr <<
"file_bplus_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.
bool insert(const Key &key)
Insert a key if it is not already present.
void sync() const
Flush the current tree image using redo WAL or full-image fallback.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
bool verify() const
Verify structural B+ Tree invariants across cached pages.
bool remove(const Key &key)
Remove a key if present.
Array< Key > range(const Key &first, const Key &last) const
Collect all keys in the inclusive range [first, last].
size_t page_count() const noexcept
Return the number of allocated pages currently tracked.
Array< Key > keys() const
Materialize the tree contents in sorted order.
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
size_t page_size_bytes() const noexcept
Return the fixed serialized page size.
QuadTree - Hierarchical spatial index for 2D points.
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.