Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Readonly_Journal.h
Go to the documentation of this file.
1#ifndef joedb_Readonly_Journal_declared
2#define joedb_Readonly_Journal_declared
3
4#include "joedb/Writable.h"
7
8#include <array>
9
10namespace joedb
11{
12 class Writable_Journal;
13
14 /// @ingroup journal
16 {
17 friend class Writable_Journal;
18 friend class Journal_Hasher;
19
20 public:
21 enum class Check
22 {
23 joedb = 1,
24 version = 2,
25 big_size = 4,
26 small_size = 8,
27 set_checkpoint = 16,
31 overwrite = all & ~big_size & ~checkpoint_mismatch,
33 };
34
35 static bool check_flag(Check check, Check flag)
36 {
37 return static_cast<int>(check) & static_cast<int>(flag);
38 }
39
40 private:
41 void read_checkpoint(const std::array<int64_t, 4> &pos);
42 void pull_without_locking();
43
44 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
45 void perform_update_##type_id(Writable &writable);
46 #include "joedb/TYPE_MACRO.h"
47
48 protected:
50 uint32_t file_version;
53
57
59 std::string safe_read_string();
60
61 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
62 void read_vector_of_##type_id(cpp_type *data, size_t size);
63 #include "joedb/TYPE_MACRO.h"
64
65 enum class operation_t: uint8_t
66 {
67 create_table = 0x01,
68 drop_table = 0x02,
69 add_field = 0x03,
70 drop_field = 0x04,
71 insert_into = 0x05,
72 delete_from = 0x06,
73 update = 0x07, // deprecated
74 append = 0x08,
75 update_last = 0x09, // deprecated
76 comment = 0x0a,
77 timestamp = 0x0b,
78 rename_table = 0x0c,
79 rename_field = 0x0d,
80 valid_data = 0x0e,
81 insert_vector = 0x0f,
82 custom = 0x10,
83 update_vector = 0x11, // deprecated
84 update_next = 0x12, // deprecated
85 blob = 0x13,
86 updates = 0x80,
87 #define TYPE_MACRO(t, rt, type_id, r, w)\
88 update_##type_id,\
89 update_last_##type_id,\
90 update_next_##type_id,\
91 update_vector_##type_id,
92 #include "joedb/TYPE_MACRO.h"
93 };
94
95 public:
97
99 Readonly_Journal(lock, check)
100 {
101 }
102
104 (
106 Check check = Check::readonly
107 ):
109 {
110 }
111
112 uint32_t get_file_version() const {return file_version;}
113 bool at_end_of_file() const;
114 int64_t get_position() const {return file.get_position();}
116 bool is_empty() const {return file.get_size() == header_size;}
117 bool is_shared() const {return file.is_shared();}
118 void pull();
119 const Buffered_File &get_file() const {return file;}
120 void replay_log(Writable &writable);
122 void rewind();
123 void set_position(int64_t position);
124 void one_step(Writable &writable);
125 void play_until(Writable &writable, int64_t end);
127 {
129 writable.default_checkpoint();
130 }
132
133 Async_Reader get_async_tail_reader(int64_t start_position) const
134 {
135 return Async_Reader(file, start_position, get_checkpoint_position());
136 }
137
138 Async_Reader get_async_reader(int64_t start_position, int64_t until_position) const
139 {
140 return Async_Reader
141 (
142 file,
143 start_position,
144 std::min(until_position, get_checkpoint_position())
145 );
146 }
147
152
153 static constexpr uint32_t version_number = 0x00000004;
154 static constexpr uint32_t compatible_version = 0x00000004;
155 static constexpr int64_t header_size = 41;
156 static constexpr int64_t checkpoint_offset = 5 + 4;
157 static constexpr bool is_second_checkpoint_copy(int64_t offset)
158 {
159 return offset == 17 || offset == 33;
160 }
161
162 virtual Writable_Journal *get_writable_journal() {return nullptr;}
163 virtual ~Readonly_Journal() = default;
164 };
165}
166
167#endif
virtual int64_t get_size() const
bool is_shared() const noexcept
int64_t get_position() const noexcept
virtual Writable_Journal * get_writable_journal()
static constexpr uint32_t version_number
void play_until(Writable &writable, int64_t end)
void one_step(Writable &writable)
Readonly_Journal(Buffered_File &file, Check check=Check::readonly)
static constexpr bool is_second_checkpoint_copy(int64_t offset)
Readonly_Journal(Journal_Construction_Lock &&lock, Check check)
uint32_t get_file_version() const
void replay_with_checkpoint_comments(Writable &writable)
static constexpr uint32_t compatible_version
void set_position(int64_t position)
virtual ~Readonly_Journal()=default
void replay_log(Writable &writable)
static bool check_flag(Check check, Check flag)
Async_Reader get_async_reader(int64_t start_position, int64_t until_position) const
static constexpr int64_t checkpoint_offset
int64_t get_checkpoint_position() const
const Buffered_File & get_file() const
void play_until_checkpoint(Writable &writable)
Async_Reader get_async_blob_reader(Blob blob) const
static constexpr int64_t header_size
Async_Reader get_async_tail_reader(int64_t start_position) const
void default_checkpoint()
Definition Writable.cpp:32
Definition Blob.h:7