Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
file_bplus_tree_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <exception>
37#include <filesystem>
38#include <iostream>
39#include <system_error>
40
41#include <ah-errors.H>
42#include <tpl_file_bplus_tree.H>
43
44using namespace Aleph;
45
46namespace
47{
48 namespace fs = std::filesystem;
49
50 template <typename T>
51 void print_array(const char * label, const Array<T> & values)
52 {
53 std::cout << label << " [";
54 for (size_t i = 0; i < values.size(); ++i)
55 {
56 if (i != 0)
57 std::cout << ", ";
58 std::cout << values[i];
59 }
60 std::cout << "]\n";
61 }
62
63 template <typename Tree>
64 void print_iter_band(const char * label,
65 const Tree & tree,
66 const int first,
67 const int last)
68 {
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())
72 {
73 if (not first_item)
74 std::cout << ", ";
75 std::cout << it.get_curr();
76 first_item = false;
77 }
78 std::cout << "]\n";
79 }
80}
81
82int main()
83{
84 const fs::path file_path =
85 fs::temp_directory_path() / "aleph_file_bplus_tree_example.idx";
86
87 try
88 {
89 std::error_code ec;
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);
96
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";
105
106 {
107 File_BPlus_Tree<int, Aleph::less<int>, 3> ledger(file_path.string(),
108 false);
109 for (int value : {105, 110, 115, 120, 125, 130, 135, 140, 145, 150})
110 ledger.insert(value);
111
112 ledger.insert(155);
113 ledger.remove(120);
114 ledger.sync();
115
117 << "File_BPlus_Tree example produced an invalid in-memory tree";
118
119 std::cout << "first session\n";
120 std::cout << "-------------\n";
121 print_array("all keys :", ledger.keys());
122 print_array("range 118..146:", ledger.range(118, 146));
123 print_iter_band("iter 118..146 :", ledger, 118, 146);
124 std::cout << "page size : " << ledger.page_size_bytes() << "\n";
125 std::cout << "page count : " << ledger.page_count() << "\n";
126 std::cout << "auto sync : " << std::boolalpha
127 << ledger.auto_sync_enabled() << "\n\n";
128 }
129
130 {
131 File_BPlus_Tree<int, Aleph::less<int>, 3> reopened(file_path.string());
132
133 std::cout << "after reopen\n";
134 std::cout << "------------\n";
135 print_array("all keys :", reopened.keys());
136 print_array("range 118..146:", reopened.range(118, 146));
137 print_iter_band("iter 118..146 :", reopened, 118, 146);
138 std::cout << "page count : " << reopened.page_count() << "\n";
139
140 if (const auto ub = reopened.upper_bound(145); ub.has_value())
141 std::cout << "upper_bound(145) -> " << *ub << "\n";
142 }
143
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);
150 }
151 catch (const std::exception & ex)
152 {
153 std::cerr << "file_bplus_tree_example failed: " << ex.what() << '\n';
154 return 1;
155 }
156
157 return 0;
158}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:250
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
Persistent page-managed B+ Tree stored in a single binary file.
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void print_array(const char *label, const Container &a)
Page-managed persistent B+ Tree with file-backed storage.