Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
file_b_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_b_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
64int main()
65{
66 const fs::path file_path =
67 fs::temp_directory_path() / "aleph_file_b_tree_example.idx";
68
69 try
70 {
71 std::error_code ec;
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);
78
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";
87
88 {
89 File_B_Tree<int, Aleph::less<int>, 3> catalog(file_path.string(), false);
90 for (int value : {40, 10, 90, 20, 70, 60, 30, 50, 80})
91 catalog.insert(value);
92
93 catalog.remove(20);
94 catalog.insert(65);
95 catalog.sync();
96
98 << "File_B_Tree example produced an invalid in-memory tree";
99
100 std::cout << "first session\n";
101 std::cout << "-------------\n";
102 print_array("catalog keys :", catalog.keys());
103 std::cout << "height : " << catalog.height() << "\n";
104 std::cout << "page size : " << catalog.page_size_bytes() << "\n";
105 std::cout << "page count : " << catalog.page_count() << "\n";
106 std::cout << "auto sync : " << std::boolalpha
107 << catalog.auto_sync_enabled() << "\n\n";
108 }
109
110 {
111 File_B_Tree<int, Aleph::less<int>, 3> reopened(file_path.string());
112
113 std::cout << "after reopen\n";
114 std::cout << "------------\n";
115 print_array("catalog keys :", reopened.keys());
116 std::cout << "page count : " << reopened.page_count() << "\n";
117
118 if (const auto lb = reopened.lower_bound(64); lb.has_value())
119 std::cout << "lower_bound(64) -> " << *lb << "\n";
120
121 if (const auto ub = reopened.upper_bound(70); ub.has_value())
122 std::cout << "upper_bound(70) -> " << *ub << "\n";
123 }
124
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);
131 }
132 catch (const std::exception & ex)
133 {
134 std::cerr << "file_b_tree_example failed: " << ex.what() << '\n';
135 return 1;
136 }
137
138 return 0;
139}
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.
int main()
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.