Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
write_server_blob.cpp
Go to the documentation of this file.
5
6#include <iostream>
7
8/// Demonstration of joedb::Server_File
9///
10/// This demonstrates how to connect to a joedb server to read and write blobs,
11/// without downloading a full replica of the database.
12static int write_server_blob(int argc, char **argv)
13{
14 if (argc < 2)
15 {
16 std::cerr << "usage: " << argv[0] << " <blob_string>\n";
17 std::cerr << "This program will try to connect to a local server.\n";
18 std::cerr << "Before running this program, start a joedb server with:\n";
19 std::cerr << "joedb_server --socket blobs.sock blobs.joedb\n";
20 std::cerr << "You can interactively read and write blobs this way:\n";
21 std::cerr << "joedb_client --db none server local blobs.sock\n";
22 return 1;
23 }
24
25 // Connect to the server
26 joedb::Local_Connector connector("blobs.sock");
27 joedb::Server_File server_file(connector, &std::cerr);
28
29 // Creating the client: server file serves both as file and connection
30 joedb::Writable_Journal_Client client(server_file, server_file);
31
32 // Write blobs with a Client_Lock: keeps the server locked between writes
33 {
35
36 for (int i = 3; --i >= 0;)
37 {
38 const joedb::Blob blob = lock.get_journal().write_blob(argv[1]);
39 lock.checkpoint_and_push();
40 std::cout << "wrote blob with lock: " << blob.get_position() << '\n';
41 std::cout << "blob: " << server_file.read_blob(blob) << '\n';
42 }
43
44 lock.unlock();
45 }
46
47 // Write blobs with a transaction: lock and unlock for each write
48 for (int i = 3; --i >= 0;)
49 {
50 const auto blob = client.transaction([argv](joedb::Writable_Journal &journal)
51 {
52 return journal.write_blob(argv[1]);
53 });
54 std::cout << "wrote blob with transaction: " << blob.get_position() << '\n';
55 std::cout << "blob: " << server_file.read_blob(blob) << '\n';
56 }
57
58 return 0;
59}
60
61int main(int argc, char **argv)
62{
63 return joedb::main_exception_catcher(write_server_blob, argc, argv);
64}
int64_t get_position() const noexcept
Definition Blob.h:29
Directly read file served from joedb_server.
Definition Server_File.h:22
Blob write_blob(const std::string &data) final
int main()
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.