Joedb 10.3.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
write_server_blob.cpp
Go to the documentation of this file.
2#include "joedb/ui/type_io.h"
7
8#include <iostream>
9
10/// Demonstration of joedb::Server_File
11///
12/// This demonstrates how to connect to a joedb server to read and write blobs,
13/// without downloading a full replica of the database.
14static int write_server_blob(joedb::Arguments &arguments)
15{
16 joedb::Parsed_Logger logger(arguments);
17 const std::string blob_string{arguments.get_next("<blob_string>")};
18
19 if (arguments.missing())
20 {
21 arguments.print_help(std::cerr);
22 std::cerr << "This program will try to connect to a local server.\n";
23 std::cerr << "Before running this program, start a joedb server with:\n";
24 std::cerr << "joedb_server blobs.joedb\n";
25 std::cerr << "You can interactively read and write blobs this way:\n";
26 std::cerr << "joedb_client --db none server local blobs.joedb.sock\n";
27 return 1;
28 }
29
30 // Connect to the server
31 joedb::Local_Connector connector("blobs.joedb.sock");
32 joedb::Server_File server_file(connector, logger.get());
33
34 // Creating the client: server file serves both as file and connection
35 joedb::Writable_Journal_Client client(server_file, server_file);
36
37 // Write blobs with a Client_Lock: keeps the server locked between writes
38 {
40
41 for (int i = 3; --i >= 0;)
42 {
43 const joedb::Blob blob = lock.get_journal().write_blob(blob_string);
44 lock.checkpoint_and_push();
45 std::cout << "wrote blob with lock: ";
46 joedb::write_blob(std::cout, blob);
47 std::cout << "\nblob: " << server_file.read_blob(blob) << '\n';
48 }
49
50 lock.unlock();
51 }
52
53 // Write blobs with a transaction: lock and unlock for each write
54 for (int i = 3; --i >= 0;)
55 {
56 const auto blob = client.transaction([&](joedb::Writable_Journal &journal)
57 {
58 return journal.write_blob(blob_string);
59 });
60 std::cout << "wrote blob with transaction: ";
61 joedb::write_blob(std::cout, blob);
62 std::cout << "\nblob: " << server_file.read_blob(blob) << '\n';
63 }
64
65 return 0;
66}
67
68int main(int argc, char **argv)
69{
70 return joedb::main_wrapper(write_server_blob, argc, argv);
71}
Class for conveniently parsing command-line arguments.
Definition Arguments.h:19
bool missing() const
Definition Arguments.h:147
std::string_view get_next()
std::ostream & print_help(std::ostream &out) const
Directly read file served from joedb_server.
Definition Server_File.h:22
Blob write_blob(const std::string &data) override
int main()
int main_wrapper(int(*main)(Arguments &), int argc, char **argv)
Process command-line arguments and catch exceptions from main.
void write_blob(std::ostream &out, Blob blob)
Definition type_io.cpp:243