Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_tree_snapshot.H
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
40# ifndef TPL_TREE_SNAPSHOT_H
41# define TPL_TREE_SNAPSHOT_H
42
43# include <array>
44# include <bit>
45# include <cstdint>
46# include <cstring>
47# include <filesystem>
48# include <fstream>
49# include <string>
50# include <type_traits>
51
52# include <ah-errors.H>
53# include <tpl_array.H>
54
55namespace Aleph
56{
57 namespace detail
58 {
59 inline constexpr size_t Ordered_Tree_Snapshot_Magic_Size = 16;
60 inline constexpr std::uint32_t Ordered_Tree_Snapshot_Version = 1;
61
63 {
65 std::uint32_t version = 0;
66 std::uint32_t key_size = 0;
67 std::uint64_t min_degree = 0;
68 std::uint64_t count = 0;
69 };
70
71 template <size_t N>
72 [[nodiscard]] constexpr std::array<char, Ordered_Tree_Snapshot_Magic_Size>
73 ordered_tree_snapshot_magic(const char (& text)[N]) noexcept
74 {
75 std::array<char, Ordered_Tree_Snapshot_Magic_Size> ret = {};
76 constexpr size_t limit =
78 ? N - 1
80
81 for (size_t i = 0; i < limit; ++i)
82 ret[i] = text[i];
83
84 return ret;
85 }
86
87 [[nodiscard]] inline bool ordered_tree_snapshot_exists(const std::string & file_path)
88 {
89 return std::ifstream(file_path, std::ios::binary).good();
90 }
91
92 inline void ensure_ordered_tree_snapshot_parent_dir(const std::string & file_path,
93 const char * kind)
94 {
95 namespace fs = std::filesystem;
96
97 const fs::path path(file_path);
98 if (not path.has_parent_path())
99 return;
100
101 std::error_code ec;
102 (void) fs::create_directories(path.parent_path(), ec);
104 << kind << ": cannot create directory " << path.parent_path().string()
105 << " for snapshot " << file_path << " (" << ec.message() << ")";
106 }
107
108 template <typename Key>
109 [[nodiscard]] Key ordered_tree_read_key(std::istream & in,
110 const std::string & file_path,
111 const char * kind,
112 const std::uint64_t index)
113 {
114 static_assert(std::is_trivially_copyable_v<Key>,
115 "Persistent tree snapshots require trivially copyable keys");
116
117 std::array<char, sizeof(Key)> bytes = {};
118 in.read(bytes.data(), bytes.size());
120 << kind << ": cannot read key #" << index << " from snapshot "
121 << file_path;
122
123 return std::bit_cast<Key>(bytes);
124 }
125
126 template <typename Key>
127 void ordered_tree_write_key(std::ostream & out,
128 const Key & key,
129 const std::string & file_path,
130 const char * kind,
131 const std::uint64_t index)
132 {
133 static_assert(std::is_trivially_copyable_v<Key>,
134 "Persistent tree snapshots require trivially copyable keys");
135
136 const auto bytes = std::bit_cast<std::array<char, sizeof(Key)>>(key);
137 out.write(bytes.data(), bytes.size());
139 << kind << ": cannot write key #" << index << " to snapshot "
140 << file_path;
141 }
142
144 const Ordered_Tree_Snapshot_Header & header,
145 const std::array<char, Ordered_Tree_Snapshot_Magic_Size> & expected_magic,
146 const std::uint32_t expected_key_size,
147 const std::uint64_t expected_min_degree,
148 const std::string & file_path,
149 const char * kind)
150 {
151 ah_runtime_error_unless(std::memcmp(header.magic, expected_magic.data(),
153 << kind << ": snapshot " << file_path << " has an unexpected format";
154
156 << kind << ": snapshot " << file_path << " uses unsupported version "
157 << header.version;
158
160 << kind << ": snapshot " << file_path << " was created for key size "
161 << header.key_size << ", but this instantiation expects "
163
165 << kind << ": snapshot " << file_path << " was created with minimum "
166 << "degree " << header.min_degree << ", but this instantiation expects "
168 }
169
170 template <typename Key>
172 const std::string & file_path,
173 const std::array<char, Ordered_Tree_Snapshot_Magic_Size> & expected_magic,
174 const std::uint64_t expected_min_degree,
175 const char * kind)
176 {
177 static_assert(std::is_trivially_copyable_v<Key>,
178 "Persistent tree snapshots require trivially copyable keys");
179
180 std::ifstream in(file_path, std::ios::binary);
182 << kind << ": cannot open snapshot " << file_path << " for reading";
183
185 in.read(reinterpret_cast<char *>(&header), sizeof(header));
187 << kind << ": cannot read snapshot header from " << file_path;
188
189 ordered_tree_validate_header(header, expected_magic, sizeof(Key),
190 expected_min_degree, file_path, kind);
191
193 ret.reserve(header.count);
194 for (std::uint64_t i = 0; i < header.count; ++i)
195 ret.append(ordered_tree_read_key<Key>(in, file_path, kind, i));
196
197 return ret;
198 }
199
200 template <typename Key>
202 const std::string & file_path,
203 const std::array<char, Ordered_Tree_Snapshot_Magic_Size> & expected_magic,
204 const std::uint64_t expected_min_degree,
205 const Array<Key> & keys,
206 const char * kind)
207 {
208 static_assert(std::is_trivially_copyable_v<Key>,
209 "Persistent tree snapshots require trivially copyable keys");
210
212
213 std::ofstream out(file_path, std::ios::binary | std::ios::trunc);
215 << kind << ": cannot open snapshot " << file_path << " for writing";
216
218 std::memcpy(header.magic, expected_magic.data(),
221 header.key_size = sizeof(Key);
223 header.count = keys.size();
224
225 out.write(reinterpret_cast<const char *>(&header), sizeof(header));
227 << kind << ": cannot write snapshot header to " << file_path;
228
229 for (size_t i = 0; i < keys.size(); ++i)
230 ordered_tree_write_key(out, keys[i], file_path, kind, i);
231
232 out.flush();
234 << kind << ": cannot flush snapshot " << file_path;
235 }
236 } // namespace detail
237} // namespace Aleph
238
239# endif // TPL_TREE_SNAPSHOT_H
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
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:315
#define N
Definition fib.C:294
void ordered_tree_validate_header(const Ordered_Tree_Snapshot_Header &header, const std::array< char, Ordered_Tree_Snapshot_Magic_Size > &expected_magic, const std::uint32_t expected_key_size, const std::uint64_t expected_min_degree, const std::string &file_path, const char *kind)
constexpr std::uint32_t Ordered_Tree_Snapshot_Version
Array< Key > load_ordered_tree_snapshot(const std::string &file_path, const std::array< char, Ordered_Tree_Snapshot_Magic_Size > &expected_magic, const std::uint64_t expected_min_degree, const char *kind)
void ensure_ordered_tree_snapshot_parent_dir(const std::string &file_path, const char *kind)
constexpr std::array< char, Ordered_Tree_Snapshot_Magic_Size > ordered_tree_snapshot_magic(const char(&text)[N]) noexcept
void ordered_tree_write_key(std::ostream &out, const Key &key, const std::string &file_path, const char *kind, const std::uint64_t index)
bool ordered_tree_snapshot_exists(const std::string &file_path)
void save_ordered_tree_snapshot(const std::string &file_path, const std::array< char, Ordered_Tree_Snapshot_Magic_Size > &expected_magic, const std::uint64_t expected_min_degree, const Array< Key > &keys, const char *kind)
Key ordered_tree_read_key(std::istream &in, const std::string &file_path, const char *kind, const std::uint64_t index)
constexpr size_t Ordered_Tree_Snapshot_Magic_Size
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.
char magic[Ordered_Tree_Snapshot_Magic_Size]
int keys[]
Dynamic array container with automatic resizing.