18 static int merge_main(
int argc,
char **argv)
23 std::cerr <<
"usage: " << argv[0];
24 std::cerr <<
" <db_1.joedb> ... <db_N.joedb> <output.joedb>\n";
25 std::cerr <<
"or read file names from input stream: " << argv[0];
26 std::cerr <<
" <output.joedb> <file_list.txt\n";
27 std::cerr <<
"Note: output file must not already exist\n";
35 Writable_Journal output_journal(output_file);
40 std::vector<std::string> file_names;
42 for (
int i = 1; i < argc - 1; i++)
43 file_names.emplace_back(argv[i]);
45 if (file_names.empty())
47 std::cerr <<
"No input file on the command line: reading file names from standard input.\n";
49 std::string file_name;
50 while (std::cin >> file_name)
51 file_names.emplace_back(std::move(file_name));
54 if (file_names.empty())
56 std::cerr <<
"Error: no input file\n";
63 std::string reference_schema;
64 std::unique_ptr<Database> merged_db;
65 const int width = int(std::to_string(file_names.size()).size());
68 for (
size_t i = 0; i < file_names.size(); i++)
70 std::cerr << std::setw(width) << i + 1 <<
" / ";
71 std::cerr << file_names.size() <<
": " << file_names[i] <<
"...";
75 std::unique_ptr<Database> db(
new Database());
78 Readonly_Journal input_journal(input_file);
80 Memory_File schema_file;
81 Writable_Journal schema_journal(schema_file);
84 Selective_Writable schema_filter
90 std::unique_ptr<Selective_Writable> output_schema;
91 std::unique_ptr<Multiplexer> multiplexer;
95 multiplexer.reset(
new Multiplexer{*db, schema_filter});
101 new Selective_Writable
108 multiplexer.reset(
new Multiplexer{*db, schema_filter, *output_schema});
111 input_journal.replay_log(*multiplexer);
117 schema_journal.default_checkpoint();
119 reference_schema = schema_file.get_data();
120 else if (schema_file.get_data() != reference_schema)
124 std::string(
" does not have the same schema as ") +
132 merged_db = std::move(db);
134 merge(*merged_db, *db);
136 catch (
const Exception &e)
138 std::cerr <<
' ' << e.what();
150 std::cerr <<
"Number of errors: " << errors <<
'\n';
@ create_new
fails if already exists, locks the file for writing
@ read_existing
fails if does not exist
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.
void merge(Database &merged, const Database &db)
void dump_data(const Readable &db, Writable &writable)