Joedb 10.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Database.h
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//
3// This code was automatically generated by the joedb compiler
4// https://www.joedb.org/
5//
6// Path to compiler: /home/rcoulom/repos/joedb/compcmake/gcc_debug/joedbc
7// Version: 10.5.0
8// joedbc compilation time: Oct 25 2026 16:06:59
9// Generation of this file: 2026-10-25 19:48:05 GMT
10//
11/////////////////////////////////////////////////////////////////////////////
12#ifndef joedb_db_encoded_file_Database_declared
13#define joedb_db_encoded_file_Database_declared
14
17#include "joedb/error/assert.h"
18#include "joedb/get_version.h"
19#include "joedb/hash_combine.h"
20#include "ids.h"
21
22#include <string>
23#include <cstring>
24#include <vector>
25#include <algorithm>
26#include <string_view>
27#include <utility>
28
29#include "joedb/Blob.h"
30
31static_assert(std::string_view(joedb::get_version()) == "10.5.0");
32
34{
35 /// @namespace joedb::db::encoded_file
36 ///
37 /// Automatically generated by joedbc
38
39 using joedb::Record_Id;
40 using joedb::Table_Id;
41 using joedb::Field_Id;
42
44
45
46 namespace detail
47 {
48 struct data_of_buffer
49 {
50 std::vector<joedb::Blob> field_value_of_data;
51 std::vector<int64_t> field_value_of_size;
52 std::vector<int64_t> field_value_of_offset;
53
54 joedb::Freedom_Keeper freedom_keeper;
55
56 size_t size() const {return freedom_keeper.size();}
57
58 void resize(size_t new_size)
59 {
60 field_value_of_data.resize(new_size);
61 field_value_of_size.resize(new_size);
62 field_value_of_offset.resize(new_size);
63 freedom_keeper.resize(new_size);
64 }
65 };
66 }
67
68
69 /// Store all the tables of the database
71 {
72 friend class Readable;
73 friend class container_of_buffer;
74
75 public:
76 template<typename E = joedb::Exception>
77 static void throw_exception(std::string_view message)
78 {
79 std::string s("joedb::db::encoded_file: ");
80 s += message;
81 throw E(s);
82 }
83
84 bool is_valid(id_of_buffer id) const {return is_valid_record_id_for_buffer(id.get_record_id());}
85
86 protected:
87 detail::data_of_buffer storage_of_buffer;
88 bool is_valid_record_id_for_buffer(Record_Id record_id) const {return storage_of_buffer.freedom_keeper.is_used(record_id);}
89
91 {
93 storage_of_buffer.field_value_of_data[to_underlying(record_id)] = joedb::Blob();
94 storage_of_buffer.field_value_of_size[to_underlying(record_id)] = 0;
95 storage_of_buffer.field_value_of_offset[to_underlying(record_id)] = 0;
96 storage_of_buffer.freedom_keeper.free(record_id);
97 }
98
100 {
101 storage_of_buffer.freedom_keeper.use(record_id);
102 }
103
104 void internal_vector_insert_buffer(Record_Id record_id, size_t size)
105 {
106 JOEDB_RELEASE_ASSERT(storage_of_buffer.freedom_keeper.is_free_vector(record_id, size));
107 storage_of_buffer.freedom_keeper.use_vector(record_id, size);
108 }
109
111 (
112 Record_Id record_id,
113 joedb::Blob field_value_of_data
114 )
115 {
117 storage_of_buffer.field_value_of_data[to_underlying(record_id)] = field_value_of_data;
118 }
119
121 (
122 Record_Id record_id,
123 size_t size,
124 const joedb::Blob *value
125 )
126 {
127 JOEDB_RELEASE_ASSERT(storage_of_buffer.freedom_keeper.is_used_vector(record_id, size));
128 joedb::Blob *target = &storage_of_buffer.field_value_of_data.data()[to_underlying(record_id)];
129 if (target != value)
130 std::copy_n(value, size, target);
131 }
132
134 (
135 Record_Id record_id,
136 int64_t field_value_of_size
137 )
138 {
140 storage_of_buffer.field_value_of_size[to_underlying(record_id)] = field_value_of_size;
141 }
142
144 (
145 Record_Id record_id,
146 size_t size,
147 const int64_t *value
148 )
149 {
150 JOEDB_RELEASE_ASSERT(storage_of_buffer.freedom_keeper.is_used_vector(record_id, size));
151 int64_t *target = &storage_of_buffer.field_value_of_size.data()[to_underlying(record_id)];
152 if (target != value)
153 std::copy_n(value, size, target);
154 }
155
157 (
158 Record_Id record_id,
159 int64_t field_value_of_offset
160 )
161 {
163 storage_of_buffer.field_value_of_offset[to_underlying(record_id)] = field_value_of_offset;
164 }
165
167 (
168 Record_Id record_id,
169 size_t size,
170 const int64_t *value
171 )
172 {
173 JOEDB_RELEASE_ASSERT(storage_of_buffer.freedom_keeper.is_used_vector(record_id, size));
174 int64_t *target = &storage_of_buffer.field_value_of_offset.data()[to_underlying(record_id)];
175 if (target != value)
176 std::copy_n(value, size, target);
177 }
178
179 public:
181
183 {
184 return id_of_buffer
185 (
186 Record_Id(storage_of_buffer.freedom_keeper.get_next(id.get_record_id()))
187 );
188 }
189
191 {
192 return id_of_buffer
193 (
194 Record_Id(storage_of_buffer.freedom_keeper.get_previous(id.get_record_id()))
195 );
196 }
197
198 template<class Comparator>
199 std::vector<id_of_buffer> sorted_buffer(Comparator comparator) const;
200
202 {
203 return id_of_buffer();
204 }
205
207 {
209 return (joedb::Blob)(storage_of_buffer.field_value_of_data[record.get_id()]);
210 }
211
212 int64_t get_size(id_of_buffer record) const
213 {
215 return (int64_t)(storage_of_buffer.field_value_of_size[record.get_id()]);
216 }
217
218 int64_t get_offset(id_of_buffer record) const
219 {
221 return (int64_t)(storage_of_buffer.field_value_of_offset[record.get_id()]);
222 }
223 };
224 /// returned by @ref Database::get_buffer_table
225 class container_of_buffer
226 {
227 friend class Database;
228
229 private:
230 const Database &db;
231 container_of_buffer(const Database &db): db(db) {}
232
233 public:
234 class iterator
235 {
237 private:
238 const joedb::Freedom_Keeper *fk;
239 Record_Id index;
240 iterator(const detail::data_of_buffer &data): fk(&data.freedom_keeper), index(joedb::Freedom_Keeper_Constants::used_list) {}
241 public:
242 using iterator_category = std::forward_iterator_tag;
244 using difference_type = std::ptrdiff_t;
247
248 bool operator==(const iterator &i) const {return index == i.index;}
249 bool operator!=(const iterator &i) const {return index != i.index;}
250 iterator &operator++() {index = fk->get_next(index); return *this;}
251 iterator operator++(int) {auto copy = *this; index = fk->get_next(index); return copy;}
252 iterator &operator--() {index = fk->get_previous(index); return *this;}
253 iterator operator--(int) {auto copy = *this; index = fk->get_previous(index); return copy;}
255 };
256
257 iterator begin() const {return ++iterator(db.storage_of_buffer);}
258 iterator end() const {return iterator(db.storage_of_buffer);}
259 bool is_empty() const {return db.storage_of_buffer.freedom_keeper.get_used_count() == Record_Id{0};}
260 joedb::index_t get_size() const {return to_underlying(db.storage_of_buffer.freedom_keeper.get_used_count());}
261 static id_of_buffer get_at(size_t i) {return id_of_buffer(Record_Id(i));}
262 bool is_valid_at(size_t i) {return db.storage_of_buffer.freedom_keeper.is_used(Record_Id(i));}
263 id_of_buffer first() const {return *begin();}
264 id_of_buffer last() const {return *--end();}
265 id_of_buffer get_end() const {return *end();}
266 };
267
269 {
270 return container_of_buffer(*this);
271 }
272
273 template<class Comparator>
274 std::vector<id_of_buffer> Database::sorted_buffer(Comparator comparator) const
275 {
276 std::vector<id_of_buffer> result;
277 for (auto x: get_buffer_table())
278 result.emplace_back(x);
279 std::sort(result.begin(), result.end(), comparator);
280 return result;
281 }
282}
283
284#endif
Store all the tables of the database.
Definition Database.h:71
static void throw_exception(std::string_view message)
Definition Database.h:77
void internal_delete_buffer(Record_Id record_id)
Definition Database.h:90
container_of_buffer get_buffer_table() const
Definition Database.h:268
void internal_update_buffer__size(Record_Id record_id, int64_t field_value_of_size)
Definition Database.h:134
int64_t get_size(id_of_buffer record) const
Definition Database.h:212
bool is_valid(id_of_buffer id) const
Definition Database.h:84
joedb::Blob get_data(id_of_buffer record) const
Definition Database.h:206
void internal_update_vector_buffer__data(Record_Id record_id, size_t size, const joedb::Blob *value)
Definition Database.h:121
detail::data_of_buffer storage_of_buffer
Definition Database.h:87
id_of_buffer next(id_of_buffer id) const
Definition Database.h:182
void internal_insert_buffer(Record_Id record_id)
Definition Database.h:99
void internal_update_buffer__offset(Record_Id record_id, int64_t field_value_of_offset)
Definition Database.h:157
void internal_update_buffer__data(Record_Id record_id, joedb::Blob field_value_of_data)
Definition Database.h:111
static id_of_buffer null_buffer()
Definition Database.h:201
bool is_valid_record_id_for_buffer(Record_Id record_id) const
Definition Database.h:88
id_of_buffer previous(id_of_buffer id) const
Definition Database.h:190
std::vector< id_of_buffer > sorted_buffer(Comparator comparator) const
Definition Database.h:274
void internal_update_vector_buffer__offset(Record_Id record_id, size_t size, const int64_t *value)
Definition Database.h:167
void internal_vector_insert_buffer(Record_Id record_id, size_t size)
Definition Database.h:104
int64_t get_offset(id_of_buffer record) const
Definition Database.h:218
void internal_update_vector_buffer__size(Record_Id record_id, size_t size, const int64_t *value)
Definition Database.h:144
returned by Database::get_buffer_table
Definition Database.h:226
static id_of_buffer get_at(size_t i)
Definition Database.h:261
Strongly-typed wrapper around an integer representing a row of the buffer table.
Definition ids.h:25
constexpr Record_Id get_record_id() const
Definition ids.h:36
constexpr auto get_id() const
Definition ids.h:35
#define JOEDB_RELEASE_ASSERT(x)
always-tested assertion (release and debug mode)
Definition assert.h:24
ptrdiff_t index_t
Definition index_types.h:18
constexpr const char * get_version()
Definition get_version.h:7
Automatically generated by joedbc.
Definition Client.h:19
constexpr index_t to_underlying(Record_Id id)
Definition index_types.h:59