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