Joedb 10.0.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
joedb_logdump.cpp
Go to the documentation of this file.
2#include "joedb/Multiplexer.h"
11
12#include <iostream>
13#include <memory>
14#include <cstring>
15#include <optional>
16
17namespace joedb
18{
19 /////////////////////////////////////////////////////////////////////////////
20 static void dump
21 /////////////////////////////////////////////////////////////////////////////
22 (
23 Readonly_Journal &journal,
24 Writable &writable,
25 bool print_checkpoint
26 )
27 {
28 if (print_checkpoint)
29 journal.replay_with_checkpoint_comments(writable);
30 else
31 journal.replay_log(writable);
32 }
33
34 /////////////////////////////////////////////////////////////////////////////
35 static int logdump(Arguments &arguments)
36 /////////////////////////////////////////////////////////////////////////////
37 {
38 const bool sql = arguments.has_flag("sql");
39 const bool raw = arguments.has_flag("raw");
40 const bool header = arguments.has_flag("header");
41 const bool schema_only = arguments.has_flag("schema_only");
42 const bool ignore_header = arguments.has_flag("ignore_header");
43 const bool load = arguments.has_flag("load");
44 const bool print_checkpoint = arguments.has_flag("print_checkpoint");
45 const bool blob = arguments.has_flag("blob");
46 const std::string_view file_name = arguments.get_next("file.joedb");
47
48 if (arguments.missing())
49 {
50 arguments.print_help(std::cerr);
51 return 1;
52 }
53
54 File file(file_name.data(), Open_Mode::read_existing);
55
56 if (header)
57 dump_header(std::cout, file);
58 else
59 {
60 std::optional<Readonly_Journal> journal;
61
62 journal.emplace
63 (
64 Journal_Construction_Lock
65 (
66 file,
68 )
69 );
70
71 std::unique_ptr<Writable> writable;
72
73 const Buffered_File *blob_reader = blob ? &file : nullptr;
74
75 if (sql)
76 writable.reset(new SQL_Dump_Writable(std::cout, blob_reader));
77 else if (raw)
78 writable.reset(new Raw_Dump_Writable(std::cout));
79 else
80 writable.reset(new Interpreter_Dump_Writable(std::cout, blob));
81
82 if (schema_only)
83 {
84 Selective_Writable selective_writable
85 (
86 *writable,
88 );
89 journal->replay_log(selective_writable);
90 }
91 else if (load)
92 {
93 Database db;
94 Multiplexer multiplexer{*writable, db};
95 dump(*journal, multiplexer, print_checkpoint);
96 }
97 else
98 dump(*journal, *writable, print_checkpoint);
99 }
100
101 return 0;
102 }
103}
104
105/////////////////////////////////////////////////////////////////////////////
106int main(int argc, char **argv)
107/////////////////////////////////////////////////////////////////////////////
108{
109 return joedb::main_wrapper(joedb::logdump, argc, argv);
110}
int main()
void dump_header(std::ostream &out, Buffered_File &file)
@ read_existing
fails if does not exist
@ ignore_header
use file size as checkpoint
int main_wrapper(int(*main)(Arguments &), int argc, char **argv)
Process command-line arguments and catch exceptions from main.
void dump(const Readable &db, Writable &writable, bool schema_only)
Definition dump.cpp:12
Definition Blob.h:7
JOEDB_FILE File
Definition File.h:25