Joedb 10.0.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Client_h.cpp
Go to the documentation of this file.
3
5{
6 ////////////////////////////////////////////////////////////////////////////
8 ////////////////////////////////////////////////////////////////////////////
9 (
10 const Compiler_Options &options
11 ):
12 Generator(".", "Client.h", options)
13 {
14 }
15
16 ////////////////////////////////////////////////////////////////////////////
18 ////////////////////////////////////////////////////////////////////////////
19 {
21
22 out << R"RRR(
23#include "Writable_Database.h"
24#include "joedb/concurrency/Writable_Client.h"
25
26)RRR";
27
29 out << R"RRR(
30 namespace detail
31 {
32 ///////////////////////////////////////////////////////////////////////////
33 class Client_Data
34 ///////////////////////////////////////////////////////////////////////////
35 {
36 protected:
37 Writable_Database db;
38
39 Client_Data
40 (
41 joedb::Buffered_File &file,
42 joedb::Recovery recovery
43 ):
44 db(file, recovery, false)
45 {
46 }
47 };
48 }
49
50 /// Handle concurrent access to a @ref joedb::Buffered_File using a @ref joedb::Connection
51 class Client:
52 protected detail::Client_Data,
53 public joedb::Writable_Client
54 {
55 friend class Client_Lock;
56
57 private:
58 int64_t schema_checkpoint;
59
60 protected:
61 void read_journal() override
62 {
63 db.play_journal();
64 if (schema_checkpoint)
65 {
66 if (db.schema_journal.get_checkpoint() > schema_checkpoint)
67 Database::throw_exception("Can't upgrade schema during pull");
68 db.check_single_row();
69 }
70 }
71
72 public:
73 Client
74 (
75 joedb::Buffered_File &file,
76 joedb::Connection &connection,
77 joedb::Content_Check content_check = joedb::Content_Check::fast,
78 joedb::Recovery recovery = joedb::Recovery::none
79 ):
80 detail::Client_Data(file, recovery),
81 joedb::Writable_Client(db.journal, connection, content_check),
82 schema_checkpoint(0)
83 {
84 if (get_checkpoint_difference() > 0)
85 push_unlock();
86
87 db.play_journal(); // makes transaction shorter if db is big
88 joedb::Writable_Client::transaction([this](){
89 db.initialize();
90 });
91
92 schema_checkpoint = db.schema_journal.get_checkpoint();
93 }
94
95 const Database &get_database() const
96 {
97 return db;
98 }
99
100 /// Execute a write transaction
101 ///
102 /// This function can be called with a lambda like this:
103 /// @code
104 /// client.transaction([](Writable_Database &db)
105 /// {
106 /// db.write_comment("Hello");
107 /// });
108 /// @endcode
109 /// The transaction function locks and pulls the connection before
110 /// executing the lambda, pushes and unlocks it after.
111 template<typename F> auto transaction
112 (
113 F transaction
114 )
115 {
116 return joedb::Writable_Client::transaction([&]()
117 {
118 return transaction(db);
119 });
120 }
121 };
122
123 /// For more flexibility than the transaction lambda
124 ///
125 /// See joedb::Client_Lock for more information
126 ///
127 /// @include client_lock.cpp
128 class Client_Lock: public joedb::Client_Lock
129 {
130 public:
131 Client_Lock(Client &client): joedb::Client_Lock(client)
132 {
133 }
134
135 Writable_Database &get_database()
136 {
137 JOEDB_DEBUG_ASSERT(locked);
138 return static_cast<Client &>(client).db;
139 }
140 };
141)RRR";
142
144 out << "\n#endif\n";
145 }
146}
const std::vector< std::string > & get_name_space() const
void generate() override
Definition Client_h.cpp:17
Client_h(const Compiler_Options &options)
Definition Client_h.cpp:9
const Compiler_Options & options
Definition Generator.h:14
void namespace_open(std::ostream &out, const std::vector< std::string > &n)
void namespace_close(std::ostream &out, const std::vector< std::string > &n)
void namespace_include_guard(std::ostream &out, const char *name, const std::vector< std::string > &n)
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5