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