Joedb 9.5.0
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(joedb::Buffered_File &file): db(file, false)
40 {
41 }
42 };
43 }
44
45 /// Handle concurrent access to a @ref joedb::Buffered_File using a @ref joedb::Connection
46 class Client:
47 protected detail::Client_Data,
48 public joedb::Writable_Client
49 {
50 friend class Client_Lock;
51
52 private:
53 int64_t schema_checkpoint;
54
55 protected:
56 void read_journal() override
57 {
58 db.play_journal();
59 if (schema_checkpoint)
60 {
61 if (db.schema_journal.get_checkpoint() > schema_checkpoint)
62 Database::throw_exception("Can't upgrade schema during pull");
63 db.check_single_row();
64 }
65 }
66
67 public:
68 Client
69 (
70 joedb::Buffered_File &file,
71 joedb::Connection &connection,
72 joedb::Content_Check content_check = joedb::Content_Check::quick
73 ):
74 detail::Client_Data(file),
75 joedb::Writable_Client(db.journal, connection, content_check),
76 schema_checkpoint(0)
77 {
78 if (get_checkpoint_difference() > 0)
79 push_unlock();
80
81 db.play_journal(); // makes transaction shorter if db is big
82 joedb::Writable_Client::transaction([this](){
83 db.initialize();
84 });
85
86 schema_checkpoint = db.schema_journal.get_checkpoint();
87 }
88
89 const Database &get_database() const
90 {
91 return db;
92 }
93
94 /// Execute a write transaction
95 ///
96 /// This function can be called with a lambda like this:
97 /// @code
98 /// client.transaction([](Writable_Database &db)
99 /// {
100 /// db.write_comment("Hello");
101 /// });
102 /// @endcode
103 /// The transaction function locks and pulls the connection before
104 /// executing the lambda, pushes and unlocks it after.
105 template<typename F> auto transaction
106 (
107 F transaction
108 )
109 {
110 return joedb::Writable_Client::transaction([&]()
111 {
112 return transaction(db);
113 });
114 }
115 };
116
117 /// For more flexibility than the transaction lambda
118 ///
119 /// See joedb::Client_Lock for more information
120 ///
121 /// @include client_lock.cpp
122 class Client_Lock: public joedb::Client_Lock
123 {
124 public:
125 Client_Lock(Client &client): joedb::Client_Lock(client)
126 {
127 }
128
129 Writable_Database &get_database()
130 {
131 JOEDB_DEBUG_ASSERT(locked);
132 return static_cast<Client &>(client).db;
133 }
134 };
135)RRR";
136
138 out << "\n#endif\n";
139 }
140}
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