Joedb 10.0.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Database_Writable.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.0.0
8// joedbc compilation time: May 27 2025 17:38:37
9// Generation of this file: 2025-05-27 15:38:58 GMT
10//
11/////////////////////////////////////////////////////////////////////////////
12#ifndef tutorial_Database_Writable_declared
13#define tutorial_Database_Writable_declared
14
18#include "Database.h"
19
20#include <string>
21#include <stdint.h>
22#include <cstring>
23#include <vector>
24
25namespace tutorial
26{
27 namespace detail
28 {
29 extern const char * schema_string;
30 inline constexpr size_t schema_string_size = 99;
31 }
32
33 class container_of_city;
34 class container_of_person;
35
36 /// implement @ref joedb::Writable in a @ref Database
38 {
39 protected:
41 Table_Id current_table_id = Table_Id{0};
42
43 void delete_from(Table_Id table_id, Record_Id record_id) override
44 {
45 if (table_id == Table_Id(1))
46 internal_delete_city(record_id);
47 else if (table_id == Table_Id(2))
48 internal_delete_person(record_id);
49 }
50
51 void insert_into(Table_Id table_id, Record_Id record_id) override
52 {
53 if (to_underlying(record_id) < 0 || (max_record_id && to_underlying(record_id) >= max_record_id))
54 throw_exception("insert_into: too big");
55 if (table_id == Table_Id(1))
56 {
57 if (is_valid_record_id_for_city(record_id))
58 throw_exception("Duplicate insert into table city");
59 if (storage_of_city.size() <= size_t(record_id))
60 storage_of_city.resize(to_underlying(record_id) + 1);
61 internal_insert_city(record_id);
62 }
63 else if (table_id == Table_Id(2))
64 {
65 if (is_valid_record_id_for_person(record_id))
66 throw_exception("Duplicate insert into table person");
67 if (storage_of_person.size() <= size_t(record_id))
68 storage_of_person.resize(to_underlying(record_id) + 1);
69 internal_insert_person(record_id);
70 }
71 }
72
74 (
75 Table_Id table_id,
76 Record_Id record_id,
77 size_t size
78 ) override
79 {
80 joedb::Freedom_Keeper *fk = nullptr;
81
82 if (table_id == Table_Id(1))
83 fk = &storage_of_city.freedom_keeper;
84 else if (table_id == Table_Id(2))
85 fk = &storage_of_person.freedom_keeper;
86
87 if (fk)
88 {
89 JOEDB_RELEASE_ASSERT(fk->is_used_vector(record_id, size));
90 joedb::Writable::delete_vector(table_id, record_id, size);
91 }
92 }
93
95 (
96 Table_Id table_id,
97 Record_Id record_id,
98 size_t size
99 ) override
100 {
101 if
102 (
103 to_underlying(record_id) < 0 ||
104 (max_record_id && (to_underlying(record_id) >= max_record_id || joedb::index_t(size) >= max_record_id))
105 )
106 {
107 throw_exception("insert_vector: null record_id, or too big");
108 }
109 if (table_id == Table_Id(1))
110 {
111 if (storage_of_city.size() < size_t(record_id) + size)
112 storage_of_city.resize(to_underlying(record_id) + size);
113 internal_vector_insert_city(record_id, size);
114 }
115 else if (table_id == Table_Id(2))
116 {
117 if (storage_of_person.size() < size_t(record_id) + size)
118 storage_of_person.resize(to_underlying(record_id) + size);
119 internal_vector_insert_person(record_id, size);
120 }
121 }
122
124 (
125 Table_Id table_id,
126 Record_Id record_id,
127 Field_Id field_id,
128 const std::string& value
129 )
130 override
131 {
132 if (table_id == Table_Id(1))
133 {
134 if (field_id == Field_Id(1))
135 {
136 internal_update_city__name(record_id, value);
137 return;
138 }
139 return;
140 }
141 if (table_id == Table_Id(2))
142 {
143 if (field_id == Field_Id(1))
144 {
145 internal_update_person__first_name(record_id, value);
146 return;
147 }
148 if (field_id == Field_Id(2))
149 {
150 internal_update_person__last_name(record_id, value);
151 return;
152 }
153 return;
154 }
155 }
156
158 (
159 Table_Id table_id,
160 Record_Id record_id,
161 Field_Id field_id,
162 joedb::Record_Id value
163 )
164 override
165 {
166 if (table_id == Table_Id(2))
167 {
168 if (field_id == Field_Id(3))
169 {
170 internal_update_person__home(record_id, id_of_city(value));
171 return;
172 }
173 return;
174 }
175 }
176
178 (
179 Table_Id table_id,
180 Record_Id record_id,
181 Field_Id field_id,
182 size_t size,
183 const std::string *value
184 )
185 override
186 {
187 if (table_id == Table_Id(1))
188 {
189 if (field_id == Field_Id(1))
190 {
191 internal_update_vector_city__name(record_id, size, value);
192 return;
193 }
194 return;
195 }
196 if (table_id == Table_Id(2))
197 {
198 if (field_id == Field_Id(1))
199 {
200 internal_update_vector_person__first_name(record_id, size, value);
201 return;
202 }
203 if (field_id == Field_Id(2))
204 {
205 internal_update_vector_person__last_name(record_id, size, value);
206 return;
207 }
208 return;
209 }
210 }
211
213 (
214 Table_Id table_id,
215 Record_Id record_id,
216 Field_Id field_id,
217 size_t size,
218 const joedb::Record_Id *value
219 )
220 override
221 {
222 if (table_id == Table_Id(2))
223 {
224 if (field_id == Field_Id(3))
225 {
226 internal_update_vector_person__home(record_id, size, reinterpret_cast<const id_of_city*>(value));
227 return;
228 }
229 return;
230 }
231 }
232
234 (
235 Table_Id table_id,
236 Record_Id record_id,
237 Field_Id field_id,
238 size_t &capacity
239 )
240 override
241 {
242 if (table_id == Table_Id(1))
243 {
244 capacity = size_t(storage_of_city.freedom_keeper.size());
245 if (field_id == Field_Id(1))
246 {
247 return (storage_of_city.field_value_of_name.data() + to_underlying(record_id));
248 }
249 return nullptr;
250 }
251 if (table_id == Table_Id(2))
252 {
253 capacity = size_t(storage_of_person.freedom_keeper.size());
254 if (field_id == Field_Id(1))
255 {
256 return (storage_of_person.field_value_of_first_name.data() + to_underlying(record_id));
257 }
258 if (field_id == Field_Id(2))
259 {
260 return (storage_of_person.field_value_of_last_name.data() + to_underlying(record_id));
261 }
262 return nullptr;
263 }
264 return nullptr;
265 }
266
268 (
269 Table_Id table_id,
270 Record_Id record_id,
271 Field_Id field_id,
272 size_t &capacity
273 )
274 override
275 {
276 if (table_id == Table_Id(2))
277 {
278 capacity = size_t(storage_of_person.freedom_keeper.size());
279 if (field_id == Field_Id(3))
280 {
281 return reinterpret_cast<Record_Id *>(storage_of_person.field_value_of_home.data() + to_underlying(record_id));
282 }
283 return nullptr;
284 }
285 return nullptr;
286 }
287
288 void comment(const std::string &comment) override {}
289 void timestamp(int64_t timestamp) override {}
290 void valid_data() override {}
291
292 bool upgrading_schema = false;
295
297 {
298 return schema_file.get_data().size() < detail::schema_string_size;
299 }
300
302 {
303 constexpr size_t pos = joedb::Header::size;
304 const size_t schema_file_size = schema_file.get_data().size();
305
306 if
307 (
308 schema_file_size < pos ||
309 schema_file_size > detail::schema_string_size ||
310 std::memcmp
311 (
312 schema_file.get_data().data() + pos,
313 detail::schema_string + pos,
314 schema_file_size - pos
315 ) != 0
316 )
317 {
318 throw_exception("Trying to open a file with incompatible schema");
319 }
320 }
321
322 void create_table(const std::string &name) override
323 {
327 }
328
329 void drop_table(Table_Id table_id) override
330 {
331 schema_journal.drop_table(table_id);
333 }
334
336 (
337 Table_Id table_id,
338 const std::string &name
339 ) override
340 {
341 schema_journal.rename_table(table_id, name);
343 }
344
346 (
347 Table_Id table_id,
348 const std::string &name,
349 joedb::Type type
350 ) override
351 {
352 schema_journal.add_field(table_id, name, type);
354 }
355
356 void drop_field(Table_Id table_id, Field_Id field_id) override
357 {
358 schema_journal.drop_field(table_id, field_id);
360 }
361
363 (
364 Table_Id table_id,
365 Field_Id field_id,
366 const std::string &name
367 ) override
368 {
369 schema_journal.rename_field(table_id, field_id, name);
371 }
372
373 void custom(const std::string &name) override
374 {
377 }
378
379 public:
384
386 {
387 max_record_id = record_id;
388 }
389
390 int64_t get_schema_checkpoint() const
391 {
393 }
394
396 {
397 max_record_id = size_t(journal.get_checkpoint());
398 journal.replay_log(*this);
399 max_record_id = 0;
400
401 check_schema();
402
404 throw_exception<joedb::Out_Of_Date>("Schema is out of date. Can't upgrade a read-only database.");
405 }
406 };
407}
408
409#endif
bool is_used_vector(Record_Id index, size_t size) const
std::string & get_data()
Definition Memory_File.h:20
void replay_log(Writable &writable)
int64_t get_checkpoint() const
void add_field(Table_Id table_id, const std::string &name, Type type) override
void custom(const std::string &name) override
void create_table(const std::string &name) override
void rename_table(Table_Id table_id, const std::string &name) override
void drop_table(Table_Id table_id) override
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) override
void drop_field(Table_Id table_id, Field_Id field_id) override
Superclass with all joedb journal event listeners as virtual functions.
Definition Writable.h:17
virtual void delete_vector(Table_Id table_id, Record_Id record_id, size_t size)=0
Definition Writable.cpp:44
implement joedb::Writable in a Database
joedb::Record_Id * get_own_reference_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) override
std::string * get_own_string_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) override
void update_vector_reference(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const joedb::Record_Id *value) override
void set_max_record_id(joedb::index_t record_id)
void initialize_with_readonly_journal(joedb::Readonly_Journal &journal)
void rename_table(Table_Id table_id, const std::string &name) override
void custom(const std::string &name) override
void delete_vector(Table_Id table_id, Record_Id record_id, size_t size) override
void drop_field(Table_Id table_id, Field_Id field_id) override
void timestamp(int64_t timestamp) override
void drop_table(Table_Id table_id) override
void insert_into(Table_Id table_id, Record_Id record_id) override
void comment(const std::string &comment) override
void update_string(Table_Id table_id, Record_Id record_id, Field_Id field_id, const std::string &value) override
void update_reference(Table_Id table_id, Record_Id record_id, Field_Id field_id, joedb::Record_Id value) override
void insert_vector(Table_Id table_id, Record_Id record_id, size_t size) override
void add_field(Table_Id table_id, const std::string &name, joedb::Type type) override
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) override
void update_vector_string(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const std::string *value) override
joedb::Writable_Journal schema_journal
void create_table(const std::string &name) override
void delete_from(Table_Id table_id, Record_Id record_id) override
Store all the tables of the database.
Definition Database.h:91
void internal_update_vector_person__first_name(Record_Id record_id, size_t size, const std::string *value)
Definition Database.h:263
void internal_update_vector_city__name(Record_Id record_id, size_t size, const std::string *value)
Definition Database.h:234
void internal_insert_person(Record_Id record_id)
Definition Database.h:203
friend class id_of_city
Definition Database.h:93
void internal_update_vector_person__home(Record_Id record_id, size_t size, const id_of_city *value)
Definition Database.h:319
detail::data_of_city storage_of_city
Definition Database.h:110
detail::data_of_person storage_of_person
Definition Database.h:112
void internal_update_vector_person__last_name(Record_Id record_id, size_t size, const std::string *value)
Definition Database.h:292
void internal_update_person__home(Record_Id record_id, id_of_city field_value_of_home)
Definition Database.h:309
bool is_valid_record_id_for_city(Record_Id record_id) const
Definition Database.h:111
static void throw_exception(const std::string &message)
Definition Database.h:101
bool is_valid_record_id_for_person(Record_Id record_id) const
Definition Database.h:113
void internal_update_person__first_name(Record_Id record_id, const std::string &field_value_of_first_name)
Definition Database.h:251
void internal_update_person__last_name(Record_Id record_id, const std::string &field_value_of_last_name)
Definition Database.h:280
void internal_insert_city(Record_Id record_id)
Definition Database.h:186
void internal_update_city__name(Record_Id record_id, const std::string &field_value_of_name)
Definition Database.h:222
void internal_delete_person(Record_Id record_id)
Definition Database.h:176
void internal_delete_city(Record_Id record_id)
Definition Database.h:169
void internal_vector_insert_city(Record_Id record_id, size_t size)
Definition Database.h:192
void internal_vector_insert_person(Record_Id record_id, size_t size)
Definition Database.h:209
Strongly-typed wrapper around an integer representing a row of the city table.
Definition ids.h:25
#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
Automatically generated by joedbc.
Definition Client.h:19
static constexpr size_t size
Definition Header.h:19