8#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
9void joedb::Readonly_Journal::perform_update_##type_id(Writable &writable)\
11 const cpp_type value = read_method();\
12 writable.update_##type_id\
14 table_of_last_operation,\
15 record_of_last_operation,\
16 field_of_last_update,\
37 checkpoint_position(
Header::size),
38 hard_checkpoint_position(
Header::size)
56 throw Exception(
"missing joedb signature");
59 throw Exception(
"unsupported file format version");
64 throw Exception(
"Checkpoint is bigger than file size");
74void joedb::Readonly_Journal::read_checkpoint
77 const std::array<int64_t, 4> &pos,
81 for (
int i = 0; i < 2; i++)
83 if (pos[2 * i] == pos[2 * i + 1] && pos[2 * i] >= hard_checkpoint_position)
85 hard_checkpoint_position = pos[2 * i];
89 for (
int j = 0; j < 2; j++)
93 -pos[2 * i + j] >= checkpoint_position &&
94 (file_size < 0 || -pos[2 * i + j] <= file_size)
97 checkpoint_position = -pos[2 * i + j];
104 if (hard_checkpoint_position > checkpoint_position)
105 checkpoint_position = hard_checkpoint_position;
109void joedb::Readonly_Journal::pull_without_locking()
112 std::array<int64_t, 4> pos;
113 file.pread((
char *)&pos,
sizeof(pos), 0);
114 read_checkpoint(pos, -1);
121 const int64_t old_checkpoint = checkpoint_position;
124 pull_without_locking();
126 return checkpoint_position - old_checkpoint;
134 play_until_checkpoint(writable);
145 writable.start_writing(get_position());
146 while(get_position() < checkpoint_position)
149 writable.comment(std::to_string(get_position()));
151 writable.end_writing(get_position());
152 file.flush_for_writing();
167 while (get_position() < end)
175 if (get_position() < end)
177 const int64_t writable_position = writable.
get_position();
178 if (get_position() < writable_position)
181 raw_play_until(dummy_writable, writable_position);
184 raw_play_until(writable, end);
189 file.flush_for_writing();
198 case operation_t::create_table:
200 const std::string name = safe_read_string();
205 case operation_t::drop_table:
212 case operation_t::rename_table:
215 const std::string name = safe_read_string();
220 case operation_t::add_field:
223 const std::string name = safe_read_string();
224 const Type type = read_type();
225 writable.
add_field(table_id, name, type);
229 case operation_t::drop_field:
237 case operation_t::rename_field:
241 const std::string name = safe_read_string();
246 case operation_t::insert_into:
249 const Record_Id record_id = file.read_reference();
251 table_of_last_operation = table_id;
252 record_of_last_operation = record_id;
256 case operation_t::delete_from:
259 const Record_Id record_id = file.read_reference();
264 case operation_t::insert_vector:
267 const Record_Id record_id = file.read_reference();
268 const size_t size = file.compact_read<
size_t>();
270 table_of_last_operation = table_id;
271 record_of_last_operation = record_id;
275 case operation_t::delete_vector:
278 const Record_Id record_id = file.read_reference();
279 const size_t size = file.compact_read<
size_t>();
284 case operation_t::append:
285 writable.
insert_into(table_of_last_operation, ++record_of_last_operation);
288 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
289 case operation_t::update_##type_id:\
290 table_of_last_operation = file.read_strong_type<Table_Id>();\
291 record_of_last_operation = file.read_reference();\
292 field_of_last_update = file.read_strong_type<Field_Id>();\
293 perform_update_##type_id(writable);\
296 case operation_t::update_last_##type_id:\
297 field_of_last_update = file.read_strong_type<Field_Id>();\
298 perform_update_##type_id(writable);\
301 case operation_t::update_next_##type_id:\
302 ++record_of_last_operation;\
303 perform_update_##type_id(writable);\
307 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
308 case operation_t::update_vector_##type_id:\
310 table_of_last_operation = file.read_strong_type<Table_Id>();\
311 record_of_last_operation = file.read_reference();\
312 field_of_last_update = file.read_strong_type<Field_Id>();\
313 const size_t size = file.compact_read<size_t>();\
316 if (int64_t(size) > checkpoint_position)\
317 throw Exception("update_vector too big");\
319 cpp_type *data = writable.get_own_##type_id##_storage\
321 table_of_last_operation,\
322 record_of_last_operation,\
323 field_of_last_update,\
326 std::vector<cpp_type> buffer;\
329 buffer.resize(size);\
332 else if (to_underlying(record_of_last_operation) < 0 || to_underlying(record_of_last_operation) + size > capacity)\
333 throw Exception("update_vector out of range");\
334 read_vector_of_##type_id(data, size);\
335 writable.update_vector_##type_id\
337 table_of_last_operation,\
338 record_of_last_operation,\
339 field_of_last_update,\
347 case operation_t::custom:
349 const std::string name = safe_read_string();
354 case operation_t::comment:
356 const std::string comment = safe_read_string();
361 case operation_t::timestamp:
363 const int64_t timestamp = file.read<int64_t>();
368 case operation_t::valid_data:
372 case operation_t::blob:
374 const int64_t size = file.compact_read<int64_t>();
379 std::string s(
size_t(size), 0);
381 file.read_data(s.data(), s.size());
391 throw Exception(
"Unexpected operation: get_position() = " + std::to_string(get_position()));
401 if (type_id == Type::Type_Id::reference)
404 return Type(type_id);
411 return file.safe_read_string(checkpoint_position);
414#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
415void joedb::Readonly_Journal::read_vector_of_##type_id(cpp_type *data, size_t size)\
417 for (size_t i = 0; i < size; i++)\
418 data[i] = read_method();\
420#define TYPE_MACRO_NO_INT
421#define TYPE_MACRO_NO_FLOAT
424#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
425void joedb::Readonly_Journal::read_vector_of_##type_id(cpp_type *data, size_t size)\
427 file.read_data((char *)data, size * sizeof(cpp_type));\
429#define TYPE_MACRO_NO_STRING
430#define TYPE_MACRO_NO_REFERENCE
431#define TYPE_MACRO_NO_BLOB
virtual size_t pread(char *data, size_t size, int64_t offset) const
Read a range of bytes.
void set_position(int64_t position)
static void reading_past_end_of_file()
Writable with empty insert_vector and delete_vector.
bool is_for_writable_journal() const
void play_until(Writable &writable, int64_t end)
void one_step(Writable &writable)
static constexpr uint32_t format_version
std::string safe_read_string()
Record_Id record_of_last_operation
void replay_with_checkpoint_comments(Writable &writable)
void replay_log(Writable &writable)
void raw_play_until(Writable &writable, int64_t end)
Field_Id field_of_last_update
int64_t checkpoint_position
Readonly_Journal(Journal_Construction_Lock &lock)
Table_Id table_of_last_operation
static Type reference(Table_Id table_id)
Superclass with all joedb journal event listeners as virtual functions.
virtual void insert_vector(Table_Id table_id, Record_Id record_id, size_t size)=0
virtual Blob write_blob(const std::string &data)
virtual void insert_into(Table_Id table_id, Record_Id record_id)
virtual void drop_table(Table_Id table_id)
virtual void create_table(const std::string &name)
virtual void drop_field(Table_Id table_id, Field_Id field_id)
virtual void rename_table(Table_Id table_id, const std::string &name)
virtual void delete_vector(Table_Id table_id, Record_Id record_id, size_t size)=0
virtual void soft_checkpoint()
virtual bool wants_blob_data() const
virtual void custom(const std::string &name)
virtual void start_writing(int64_t position)
virtual void add_field(Table_Id table_id, const std::string &name, Type type)
virtual void delete_from(Table_Id table_id, Record_Id record_id)
virtual void end_writing(int64_t position)
virtual int64_t get_position() const
virtual void comment(const std::string &comment)
virtual void on_blob(Blob blob)
virtual void valid_data()
virtual void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name)
virtual void timestamp(int64_t timestamp)
@ ignore_header
use file size as checkpoint