Joedb 9.1.4
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 < 3)
15 {
16 std::cerr << "usage: " << argv[0] << " <port> <blob_string>\n";
17 std::cerr << "This program will try to connect to a server on localhost.\n";
18 std::cerr << "Before running this program, start a joedb server with:\n";
19 std::cerr << "joedb_server -port <port> blobs.joedb\n";
20 std::cerr << "You can interactively read and write blobs this way:\n";
21 std::cerr << "joedb_client --nodb server network localhost <port>\n";
22 return 1;
23 }
24
25 // Connect to the server
26 joedb::Network_Channel channel("localhost", argv[1]);
27 joedb::Server_File server_file(channel);
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_data(argv[2]);
39 lock.push();
40 std::cout << "wrote blob with lock: " << blob.get_position() << '\n';
41 std::cout << "blob: " << server_file.read_blob_data(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 joedb::Blob blob;
51 client.transaction([&blob, argv](joedb::Writable_Journal &journal)
52 {
53 blob = journal.write_blob_data(argv[2]);
54 });
55 std::cout << "wrote blob with transaction: " << blob.get_position() << '\n';
56 std::cout << "blob: " << server_file.read_blob_data(blob) << '\n';
57 }
58
59 return 0;
60}
61
62int main(int argc, char **argv)
63{
64 return joedb::main_exception_catcher(write_server_blob, argc, argv);
65}
int64_t get_position() const noexcept
Definition Blob.h:28
Directly read file served from joedb_server.
Definition Server_File.h:23
Blob write_blob_data(const std::string &data) final
int main()
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.