Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
concurrency_tutorial.cpp
Go to the documentation of this file.
1#include "tutorial/Client.h"
4
5/////////////////////////////////////////////////////////////////////////////
6int main()
7/////////////////////////////////////////////////////////////////////////////
8{
9 //
10 // This sets up a configuration with a server and 2 clients.
11 //
12 joedb::Memory_File server_file;
13 joedb::Memory_File client1_file;
14 joedb::Memory_File client2_file;
15
16 joedb::File_Connection connection(server_file);
17
18 tutorial::Client client1(client1_file, connection);
19 tutorial::Client client2(client2_file, connection);
20
21 //
22 // The databases are empty. client1 will add a few cities.
23 //
24 // All write operations are performed via the transaction function.
25 // The transaction function takes a lambda as parameter.
26 // The lock_pull operation is performed before the lambda, and the push_unlock
27 // operation is performed after the lambda, if no exception was thrown.
28 // If any exception was thrown during the lambda, then the changes
29 // are not pushed to the connection, and the connection is unlocked.
30 // Writes that occured in a transaction before an exception are not sent to
31 // the connection, but they are written to the file.
32 //
34 {
35 db.new_city("Paris");
36 db.new_city("New York");
37 db.new_city("Tokyo");
38 });
39
40 //
41 // client1.get_database() gives a read-only access to the client file
42 //
43 std::cout << "Number of cities for client1: ";
44 std::cout << client1.get_database().get_city_table().get_size() << '\n';
45
46 //
47 // Client1 added cities, and they were pushed to the central database.
48 // They have not yet reached client2.
49 //
50 std::cout << "Number of cities for client2 before pulling: ";
51 std::cout << client2.get_database().get_city_table().get_size() << '\n';
52
53 //
54 // Let's pull to update the database of client2
55 //
56 client2.pull();
57 std::cout << "Number of cities for client2 after pulling: ";
58 std::cout << client2.get_database().get_city_table().get_size() << '\n';
59
60 return 0;
61}
int64_t pull(std::chrono::milliseconds wait=std::chrono::milliseconds(0))
Definition Client.h:123
Handle concurrent access to a joedb::Buffered_File using a joedb::Connection.
Definition Client.h:45
void transaction(F transaction)
Execute a write transaction.
Definition Client.h:104
const Database & get_database() const
Definition Client.h:87
container_of_city get_city_table() const
Definition Database.h:850
A writable Database constructed from a writable joedb::Buffered_File.
size_t get_size() const
Definition Database.h:842
int main()