Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
joedb_push.cpp
Go to the documentation of this file.
7#include "joedb/Signal.h"
8
9#include <iostream>
10#include <limits>
11#include <thread>
12#include <chrono>
13#include <cstring>
14#include <cstdlib>
15
16namespace joedb
17{
18 ////////////////////////////////////////////////////////////////////////////
19 static int joedb_push(int argc, char **argv)
20 ////////////////////////////////////////////////////////////////////////////
21 {
22 const bool local = false;
23 File_Parser file_parser(Open_Mode::read_existing, true);
24 Connection_Parser connection_parser(local);
25
26 int arg_index = 1;
27 bool follow = false;
28
29 if (arg_index < argc && std::strcmp(argv[arg_index], "--follow") == 0)
30 {
31 follow = true;
32 arg_index++;
33 }
34
35 int64_t until_checkpoint = std::numeric_limits<int64_t>::max();
36
37 if (arg_index + 1 < argc && std::strcmp(argv[arg_index], "--until") == 0)
38 {
39 until_checkpoint = std::atoll(argv[arg_index + 1]);
40 arg_index += 2;
41 }
42
43 if (arg_index >= argc)
44 {
45 std::cerr << "usage: " << argv[0];
46 std::cerr << " [--follow] [--until <checkpoint>] <file> <connection>\n\n";
47 file_parser.print_help(std::cerr);
48 connection_parser.print_help(std::cerr);
49 return 1;
50 }
51
52 Buffered_File &file = *file_parser.parse(std::cerr, argc, argv, arg_index);
53 Readonly_Journal journal(file);
54
55 Connection &connection = connection_parser.build
56 (
57 argc - arg_index,
58 argv + arg_index,
59 &file
60 );
61
62 int64_t from_checkpoint = connection.handshake(journal, true);
64
65 while
66 (
67 from_checkpoint < until_checkpoint &&
68 Signal::get_signal() != SIGINT
69 )
70 {
71 if (journal.get_checkpoint_position() > from_checkpoint)
72 {
73 from_checkpoint = connection.push_until
74 (
75 journal,
76 from_checkpoint,
77 until_checkpoint,
78 false
79 );
80 }
81
82 if (follow)
83 {
84 std::this_thread::sleep_for(std::chrono::seconds(1));
85 journal.pull();
86 }
87 else
88 break;
89 }
90
91 return 0;
92 }
93}
94
95/////////////////////////////////////////////////////////////////////////////
96int main(int argc, char **argv)
97/////////////////////////////////////////////////////////////////////////////
98{
99 return joedb::main_exception_catcher(joedb::joedb_push, argc, argv);
100}
static int get_signal()
Definition Signal.cpp:27
static void start()
Definition Signal.cpp:34
int main()
@ read_existing
fails if does not exist
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.
Definition Blob.h:7