Joedb 9.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: 9.5.0
8// joedbc compilation time: May 5 2025 19:51:43
9// Generation of this file: 2025-05-05 17:51:47 GMT
10//
11/////////////////////////////////////////////////////////////////////////////
12#ifndef joedb_db_encoded_file_Database_declared
13#define joedb_db_encoded_file_Database_declared
14
20#include "joedb/error/assert.h"
21#include "joedb/get_version.h"
22#include "ids.h"
23
24#include <string>
25#include <cstdint>
26#include <cstring>
27#include <vector>
28#include <algorithm>
29#include <string_view>
30
31static_assert(std::string_view(joedb::get_version()) == "9.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
43 namespace detail
44 {
45 extern const char * schema_string;
46 inline constexpr size_t schema_string_size = 75;
47 }
48 class container_of_buffer;
49
50 namespace detail
51 {
52 struct data_of_buffer
53 {
54 std::vector<joedb::Blob> field_value_of_data;
55 std::vector<int64_t> field_value_of_size;
56 std::vector<int64_t> field_value_of_offset;
57
58 joedb::Compact_Freedom_Keeper freedom_keeper;
59
60 size_t size() const {return freedom_keeper.size();}
61
62 void resize(size_t new_size)
63 {
64 field_value_of_data.resize(new_size);
65 field_value_of_size.resize(new_size);
66 field_value_of_offset.resize(new_size);
67 freedom_keeper.resize(new_size);
68 }
69 };
70 }
71
72
73 /// Store all the tables of the database
75 {
76 friend class Readable;
77 friend class id_of_buffer;
78 friend class container_of_buffer;
79
80 public:
81 template<typename E = joedb::Exception>
82 static void throw_exception(const std::string &message)
83 {
84 throw E("joedb::db::encoded_file: " + message);
85 }
86
89
90 void set_max_record_id(size_t record_id)
91 {
92 max_record_id = record_id;
93 }
94
95 bool is_valid(id_of_buffer id) const {return is_valid_record_id_for_buffer(id.get_record_id());}
96
97 protected:
98 detail::data_of_buffer storage_of_buffer;
99 bool is_valid_record_id_for_buffer(Record_Id record_id) const {return storage_of_buffer.freedom_keeper.is_used(size_t(record_id) + 1);}
100
101 void internal_delete_buffer(Record_Id record_id)
102 {
104 storage_of_buffer.field_value_of_data[size_t(record_id) - 1] = joedb::Blob();;
105 storage_of_buffer.field_value_of_size[size_t(record_id) - 1] = 0;
106 storage_of_buffer.field_value_of_offset[size_t(record_id) - 1] = 0;
107 storage_of_buffer.freedom_keeper.free(size_t(record_id) + 1);
108 }
109
110 void internal_insert_buffer(Record_Id record_id)
111 {
112 storage_of_buffer.freedom_keeper.use(size_t(record_id) + 1);
113 }
114
115 void internal_vector_insert_buffer(Record_Id record_id, size_t size)
116 {
117 storage_of_buffer.freedom_keeper.use_vector(size_t(record_id) + 1, size);
118 }
119
121 (
122 Record_Id record_id,
123 joedb::Blob field_value_of_data
124 )
125 {
127 storage_of_buffer.field_value_of_data[size_t(record_id) - 1] = field_value_of_data;
128 }
129
131 (
132 Record_Id record_id,
133 size_t size,
134 const joedb::Blob *value
135 )
136 {
137 for (size_t i = 0; i < size; i++)
139 joedb::Blob *target = &storage_of_buffer.field_value_of_data[size_t(record_id) - 1];
140 if (target != value)
141 std::copy_n(value, size, target);
142 }
143
145 (
146 Record_Id record_id,
147 int64_t field_value_of_size
148 )
149 {
151 storage_of_buffer.field_value_of_size[size_t(record_id) - 1] = field_value_of_size;
152 }
153
155 (
156 Record_Id record_id,
157 size_t size,
158 const int64_t *value
159 )
160 {
161 for (size_t i = 0; i < size; i++)
163 int64_t *target = &storage_of_buffer.field_value_of_size[size_t(record_id) - 1];
164 if (target != value)
165 std::copy_n(value, size, target);
166 }
167
169 (
170 Record_Id record_id,
171 int64_t field_value_of_offset
172 )
173 {
175 storage_of_buffer.field_value_of_offset[size_t(record_id) - 1] = field_value_of_offset;
176 }
177
179 (
180 Record_Id record_id,
181 size_t size,
182 const int64_t *value
183 )
184 {
185 for (size_t i = 0; i < size; i++)
187 int64_t *target = &storage_of_buffer.field_value_of_offset[size_t(record_id) - 1];
188 if (target != value)
189 std::copy_n(value, size, target);
190 }
191
192
193 void delete_from(Table_Id table_id, Record_Id record_id) final
194 {
195 if (table_id == Table_Id(1))
196 internal_delete_buffer(record_id);
197 }
198
199 void insert_into(Table_Id table_id, Record_Id record_id) final
200 {
201 if (size_t(record_id) <= 0 || (max_record_id && size_t(record_id) > max_record_id))
202 throw_exception("insert_into: too big");
203 if (table_id == Table_Id(1))
204 {
205 if (is_valid_record_id_for_buffer(record_id))
206 throw_exception("Duplicate insert into table buffer");
207 if (storage_of_buffer.size() < size_t(record_id))
208 storage_of_buffer.resize(size_t(record_id));
209 internal_insert_buffer(record_id);
210 }
211 }
212
213
215 (
216 Table_Id table_id,
217 Record_Id record_id,
218 size_t size
219 ) final
220 {
221 if
222 (
223 size_t(record_id) <= 0 ||
224 (max_record_id && (size_t(record_id) > max_record_id || size > max_record_id))
225 )
226 {
227 throw_exception("insert_vector: null record_id, or too big");
228 }
229 if (table_id == Table_Id(1))
230 {
231 if (storage_of_buffer.size() < size_t(record_id) + size - 1)
232 storage_of_buffer.resize(size_t(record_id) + size - 1);
233 internal_vector_insert_buffer(record_id, size);
234 }
235 }
236
238 (
239 Table_Id table_id,
240 Record_Id record_id,
241 Field_Id field_id,
242 int64_t value
243 )
244 final
245 {
246 if (table_id == Table_Id(1))
247 {
248 if (field_id == Field_Id(2))
249 {
250 internal_update_buffer__size(record_id, value);
251 return;
252 }
253 if (field_id == Field_Id(3))
254 {
255 internal_update_buffer__offset(record_id, value);
256 return;
257 }
258 return;
259 }
260 }
261
263 (
264 Table_Id table_id,
265 Record_Id record_id,
266 Field_Id field_id,
267 joedb::Blob value
268 )
269 final
270 {
271 if (table_id == Table_Id(1))
272 {
273 if (field_id == Field_Id(1))
274 {
275 internal_update_buffer__data(record_id, value);
276 return;
277 }
278 return;
279 }
280 }
281
283 (
284 Table_Id table_id,
285 Record_Id record_id,
286 Field_Id field_id,
287 size_t size,
288 const int64_t *value
289 )
290 final
291 {
292 if (table_id == Table_Id(1))
293 {
294 if (field_id == Field_Id(2))
295 {
296 internal_update_vector_buffer__size(record_id, size, value);
297 return;
298 }
299 if (field_id == Field_Id(3))
300 {
301 internal_update_vector_buffer__offset(record_id, size, value);
302 return;
303 }
304 return;
305 }
306 }
307
309 (
310 Table_Id table_id,
311 Record_Id record_id,
312 Field_Id field_id,
313 size_t size,
314 const joedb::Blob *value
315 )
316 final
317 {
318 if (table_id == Table_Id(1))
319 {
320 if (field_id == Field_Id(1))
321 {
322 internal_update_vector_buffer__data(record_id, size, value);
323 return;
324 }
325 return;
326 }
327 }
328
330 (
331 Table_Id table_id,
332 Record_Id record_id,
333 Field_Id field_id,
334 size_t &capacity
335 )
336 final
337 {
338 if (table_id == Table_Id(1))
339 {
340 capacity = size_t(storage_of_buffer.freedom_keeper.size());
341 if (field_id == Field_Id(2))
342 {
343 return (storage_of_buffer.field_value_of_size.data() + size_t(record_id) - 1);
344 }
345 if (field_id == Field_Id(3))
346 {
347 return (storage_of_buffer.field_value_of_offset.data() + size_t(record_id) - 1);
348 }
349 return nullptr;
350 }
351 return nullptr;
352 }
353
355 (
356 Table_Id table_id,
357 Record_Id record_id,
358 Field_Id field_id,
359 size_t &capacity
360 )
361 final
362 {
363 if (table_id == Table_Id(1))
364 {
365 capacity = size_t(storage_of_buffer.freedom_keeper.size());
366 if (field_id == Field_Id(1))
367 {
368 return (storage_of_buffer.field_value_of_data.data() + size_t(record_id) - 1);
369 }
370 return nullptr;
371 }
372 return nullptr;
373 }
374
375 void comment(const std::string &comment) override {}
376 void timestamp(int64_t timestamp) override {}
377 void valid_data() final {}
378
379 bool upgrading_schema = false;
382
384 {
385 return schema_file.get_data().size() < detail::schema_string_size;
386 }
387
389 {
390 constexpr size_t pos = joedb::Header::size;
391 const size_t schema_file_size = schema_file.get_data().size();
392
393 if
394 (
395 schema_file_size < pos ||
396 schema_file_size > detail::schema_string_size ||
397 std::memcmp
398 (
399 schema_file.get_data().data() + pos,
400 detail::schema_string + pos,
401 schema_file_size - pos
402 ) != 0
403 )
404 {
405 throw_exception("Trying to open a file with incompatible schema");
406 }
407 }
408
409 void create_table(const std::string &name) override
410 {
414 }
415
416 void drop_table(Table_Id table_id) final
417 {
418 schema_journal.drop_table(table_id);
420 }
421
423 (
424 Table_Id table_id,
425 const std::string &name
426 ) final
427 {
428 schema_journal.rename_table(table_id, name);
430 }
431
433 (
434 Table_Id table_id,
435 const std::string &name,
436 joedb::Type type
437 ) override
438 {
439 schema_journal.add_field(table_id, name, type);
441 }
442
443 void drop_field(Table_Id table_id, Field_Id field_id) final
444 {
445 schema_journal.drop_field(table_id, field_id);
447 }
448
450 (
451 Table_Id table_id,
452 Field_Id field_id,
453 const std::string &name
454 ) final
455 {
456 schema_journal.rename_field(table_id, field_id, name);
458 }
459
460 void custom(const std::string &name) override
461 {
464 }
465
466 public:
471
472 int64_t get_schema_checkpoint() const
473 {
475 }
476
478 {
479 max_record_id = size_t(journal.get_checkpoint());
480 journal.replay_log(*this);
481 max_record_id = 0;
482
483 check_schema();
484
486 throw_exception<joedb::Out_Of_Date>("Schema is out of date. Can't upgrade a read-only database.");
487 }
488
490
492 {
493 return id_of_buffer
494 (
495 Record_Id(storage_of_buffer.freedom_keeper.get_next(id.get_id() + 1) - 1)
496 );
497 }
498
500 {
501 return id_of_buffer
502 (
503 Record_Id(storage_of_buffer.freedom_keeper.get_previous(id.get_id() + 1) - 1)
504 );
505 }
506
507 template<class Comparator>
508 std::vector<id_of_buffer> sorted_buffer(Comparator comparator) const;
509
511 {
512 return id_of_buffer();
513 }
514
516 {
518 return (joedb::Blob)(storage_of_buffer.field_value_of_data[record.get_id() - 1]);
519 }
520
521 int64_t get_size(id_of_buffer record) const
522 {
524 return (int64_t)(storage_of_buffer.field_value_of_size[record.get_id() - 1]);
525 }
526
527 int64_t get_offset(id_of_buffer record) const
528 {
530 return (int64_t)(storage_of_buffer.field_value_of_offset[record.get_id() - 1]);
531 }
532 };
533 /// returned by @ref Database::get_buffer_table
535 {
536 friend class Database;
537
538 private:
539 const Database &db;
540 container_of_buffer(const Database &db): db(db) {}
541
542 public:
544 {
546 private:
548 size_t index;
549 iterator(const detail::data_of_buffer &data): fk(&data.freedom_keeper), index(0) {}
550 public:
551 typedef std::forward_iterator_tag iterator_category;
553 typedef std::ptrdiff_t difference_type;
556
557 bool operator==(const iterator &i) const {return index == i.index;}
558 bool operator!=(const iterator &i) const {return index != i.index;}
559 iterator &operator++() {index = fk->get_next(index); return *this;}
560 iterator operator++(int) {auto copy = *this; index = fk->get_next(index); return copy;}
561 iterator &operator--() {index = fk->get_previous(index); return *this;}
562 iterator operator--(int) {auto copy = *this; index = fk->get_previous(index); return copy;}
563 id_of_buffer operator*() const {return id_of_buffer(Record_Id(index - 1));}
564 };
565
566 iterator begin() const {return ++iterator(db.storage_of_buffer);}
568 bool is_empty() const {return db.storage_of_buffer.freedom_keeper.is_empty();}
569 size_t get_size() const {return db.storage_of_buffer.freedom_keeper.get_used_count();}
570 static id_of_buffer get_at(size_t i) {return id_of_buffer(Record_Id(i));}
571 bool is_valid_at(size_t i) {return db.storage_of_buffer.freedom_keeper.is_used(i + 1);}
572 id_of_buffer first() const {return *begin();}
573 id_of_buffer last() const {return *--end();}
574 id_of_buffer get_end() const {return *end();}
575 };
576
578 {
579 return container_of_buffer(*this);
580 }
581
582 template<class Comparator>
583 std::vector<id_of_buffer> Database::sorted_buffer(Comparator comparator) const
584 {
585 std::vector<id_of_buffer> result;
586 for (auto x: get_buffer_table())
587 result.emplace_back(x);
588 std::sort(result.begin(), result.end(), comparator);
589 return result;
590 }
591}
592
593#endif
ptrdiff_t get_previous(ptrdiff_t index) const override
void resize(ptrdiff_t new_size) override
ptrdiff_t size() const override
ptrdiff_t get_next(ptrdiff_t index) const override
std::string & get_data()
Definition Memory_File.h:20
void replay_log(Writable &writable)
int64_t get_checkpoint() const
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) final
void create_table(const std::string &name) final
void drop_field(Table_Id table_id, Field_Id field_id) final
void custom(const std::string &name) final
void rename_table(Table_Id table_id, const std::string &name) final
void add_field(Table_Id table_id, const std::string &name, Type type) final
void drop_table(Table_Id table_id) final
void soft_checkpoint()
Definition Writable.h:21
Store all the tables of the database.
Definition Database.h:75
joedb::Writable_Journal schema_journal
Definition Database.h:381
void internal_delete_buffer(Record_Id record_id)
Definition Database.h:101
void initialize_with_readonly_journal(joedb::Readonly_Journal &journal)
Definition Database.h:477
container_of_buffer get_buffer_table() const
Definition Database.h:577
joedb::Blob * get_own_blob_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) final
Definition Database.h:355
void internal_update_buffer__size(Record_Id record_id, int64_t field_value_of_size)
Definition Database.h:145
int64_t get_size(id_of_buffer record) const
Definition Database.h:521
bool is_valid(id_of_buffer id) const
Definition Database.h:95
static void throw_exception(const std::string &message)
Definition Database.h:82
void insert_into(Table_Id table_id, Record_Id record_id) final
Definition Database.h:199
void update_int64(Table_Id table_id, Record_Id record_id, Field_Id field_id, int64_t value) final
Definition Database.h:238
void update_blob(Table_Id table_id, Record_Id record_id, Field_Id field_id, joedb::Blob value) final
Definition Database.h:263
joedb::Blob get_data(id_of_buffer record) const
Definition Database.h:515
int64_t * get_own_int64_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) final
Definition Database.h:330
void internal_update_vector_buffer__data(Record_Id record_id, size_t size, const joedb::Blob *value)
Definition Database.h:131
void drop_field(Table_Id table_id, Field_Id field_id) final
Definition Database.h:443
void update_vector_int64(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const int64_t *value) final
Definition Database.h:283
int64_t get_schema_checkpoint() const
Definition Database.h:472
void add_field(Table_Id table_id, const std::string &name, joedb::Type type) override
Definition Database.h:433
void insert_vector(Table_Id table_id, Record_Id record_id, size_t size) final
Definition Database.h:215
detail::data_of_buffer storage_of_buffer
Definition Database.h:98
id_of_buffer next(id_of_buffer id) const
Definition Database.h:491
void internal_insert_buffer(Record_Id record_id)
Definition Database.h:110
void timestamp(int64_t timestamp) override
Definition Database.h:376
void internal_update_buffer__offset(Record_Id record_id, int64_t field_value_of_offset)
Definition Database.h:169
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) final
Definition Database.h:450
void delete_from(Table_Id table_id, Record_Id record_id) final
Definition Database.h:193
void internal_update_buffer__data(Record_Id record_id, joedb::Blob field_value_of_data)
Definition Database.h:121
joedb::Memory_File schema_file
Definition Database.h:380
static id_of_buffer null_buffer()
Definition Database.h:510
void set_max_record_id(size_t record_id)
Definition Database.h:90
bool is_valid_record_id_for_buffer(Record_Id record_id) const
Definition Database.h:99
void custom(const std::string &name) override
Definition Database.h:460
id_of_buffer previous(id_of_buffer id) const
Definition Database.h:499
void update_vector_blob(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const joedb::Blob *value) final
Definition Database.h:309
std::vector< id_of_buffer > sorted_buffer(Comparator comparator) const
Definition Database.h:583
void internal_update_vector_buffer__offset(Record_Id record_id, size_t size, const int64_t *value)
Definition Database.h:179
void create_table(const std::string &name) override
Definition Database.h:409
void rename_table(Table_Id table_id, const std::string &name) final
Definition Database.h:423
void comment(const std::string &comment) override
Definition Database.h:375
void internal_vector_insert_buffer(Record_Id record_id, size_t size)
Definition Database.h:115
void drop_table(Table_Id table_id) final
Definition Database.h:416
int64_t get_offset(id_of_buffer record) const
Definition Database.h:527
void internal_update_vector_buffer__size(Record_Id record_id, size_t size, const int64_t *value)
Definition Database.h:155
Implement the joedb::Readable interface for a compiled database.
Definition Readable.h:27
returned by Database::get_buffer_table
Definition Database.h:535
static id_of_buffer get_at(size_t i)
Definition Database.h:570
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_DEBUG_ASSERT(x)
Definition assert.h:20
constexpr const char * get_version()
Definition get_version.h:7
Automatically generated by joedbc.
Definition Client.h:19
static constexpr size_t size
Definition Header.h:19