-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
212 lines (178 loc) · 7.85 KB
/
main.cpp
File metadata and controls
212 lines (178 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// mgconsole - console client for Memgraph database
//
// Copyright (C) 2016-2023 Memgraph Ltd. [https://memgraph.com]
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <signal.h>
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <optional>
#include <thread>
#include <unordered_map>
#ifdef _WIN32
#include <cstdlib>
#else /* _WIN32 */
#include <pwd.h>
#include <termios.h>
#include <unistd.h>
#endif /* _WIN32 */
#include <gflags/gflags.h>
#include <mgclient.h>
#include <replxx.h>
#include "batch_import.hpp"
#include "interactive.hpp"
#include "parsing.hpp"
#include "serial_import.hpp"
#include "utils/assert.hpp"
#include "utils/constants.hpp"
#include "utils/utils.hpp"
#include "version.hpp"
using namespace std::string_literals;
volatile sig_atomic_t is_shutting_down = 0;
// connection
DEFINE_string(host, "127.0.0.1", "Server address. It can be a DNS resolvable hostname.");
DEFINE_int32(port, 7687, "Server port.");
DEFINE_string(username, "", "Database username.");
DEFINE_string(password, "", "Database password.");
DEFINE_bool(use_ssl, false, "Use SSL when connecting to the server.");
// output
DEFINE_bool(fit_to_screen, false, "Fit output width to screen width.");
DEFINE_bool(term_colors, false, "Use terminal colors syntax highlighting.");
DEFINE_string(output_format, "tabular",
"Query output format can be csv, tabular or cypherl. If output format is "
"not tabular `fit-to-screen` flag is ignored.");
DEFINE_bool(verbose_execution_info, false,
"Output the additional information about query such as query cost, parsing, planning and execution times.");
DEFINE_validator(output_format, [](const char *, const std::string &value) {
if (value == constants::kCsvFormat || value == constants::kTabularFormat || value == constants::kCypherlFormat) {
return true;
}
return false;
});
// CSV
DEFINE_string(csv_delimiter, ",", "Character used to separate fields.");
DEFINE_validator(csv_delimiter, [](const char *, const std::string &value) {
if (value.size() != 1) {
return false;
}
return true;
});
DEFINE_string(csv_escapechar, "", "Character used to escape the quotechar(\") if the `csv-doublequote` flag is false.");
DEFINE_bool(csv_doublequote, true,
"Controls how instances of quotechar(\") appearing inside a field should "
"themselves be quoted. When true, the character is doubled. When false, "
"the escapechar is used as a prefix to the quotechar. "
"If `csv-doublequote` is false, 'csv-escapechar' must be set.");
// history
DEFINE_string(history, "~/.memgraph", "Use the specified directory to save history.");
DEFINE_bool(no_history, false, "Do not save history.");
DEFINE_string(
import_mode, "serial",
"Import mode defines the way how the queries will be executed. `serial` mode will try to execute queries in the "
"specified input order. `batched-parallel` will batched and parallelize query execution. NOTE: batched-parallel is "
"an experimental feature, the behavior might be unexpected because it depends on how the underlying database "
"system is configured (e.g., in the transactional setup there might be many serialization errors, while in the "
"analytical setup, ordering of nodes/edges is very important. `parser` mode will just print info about the "
"provided queries. NOTE: `parser` mode won't execute any query against the underlying database system.");
DEFINE_validator(import_mode, [](const char *, const std::string &value) {
if (value == constants::kSerialMode || value == constants::kBatchedParallel || value == constants::kParserMode) {
return true;
}
return false;
});
DEFINE_int32(batch_size, 1000, "A single batch size only when --import-mode=batched-parallel.");
DEFINE_int32(workers_number, 32,
"The number of threads to execute batches in parallel, only when --import-mode=batched-parallel");
DEFINE_bool(collect_parser_stats, true, "Collect parsing statistics only when --import-mode=parser");
DEFINE_bool(print_parser_stats, true, "Print parser statistics for each query only when --import-mode=parser");
DECLARE_int32(min_log_level);
int main(int argc, char **argv) {
gflags::SetVersionString(version_string);
gflags::SetUsageMessage(constants::kUsage);
gflags::ParseCommandLineFlags(&argc, &argv, true);
format::CsvOptions csv_opts{FLAGS_csv_delimiter, FLAGS_csv_escapechar, FLAGS_csv_doublequote};
format::OutputOptions output_opts{FLAGS_output_format, FLAGS_fit_to_screen};
if (output_opts.output_format == constants::kCsvFormat && !csv_opts.ValidateDoubleQuote()) {
console::EchoFailure(
"Unsupported combination of 'csv-doublequote' and 'csv-escapechar'\n"
"flags",
"Run '" + std::string(argv[0]) + " --help' for usage.");
return 1;
}
if (mg_init() != 0) {
console::EchoFailure("Internal error", "Couldn't initialize all the resources");
return 1;
}
#ifdef _WIN32
// ToDo(the-joksim):
// - How to handle shutdown inside a shutdown on Windows? (Windows uses
// messages instead of signals.)
// - the double shutdown (a second signal being sent to the process while the
// first
// signal is being handled, both signals causing process termination) should
// be a rare event (SIGTERM might be sent from other processes, such as
// daemons/services, e.g. when the system is shutting down). What behavior
// does the double shutdown cause, and what's the benefit in handling it?
#else /* _WIN32 */
auto shutdown = [](int exit_code = 0) {
if (is_shutting_down) return;
is_shutting_down = 1;
#ifdef __APPLE__
std::exit(exit_code);
#else /* __APPLE__ */
std::quick_exit(exit_code);
#endif /*__APPLE__*/
};
struct sigaction action;
action.sa_sigaction = nullptr;
action.sa_handler = shutdown;
// Prevent handling shutdown inside a shutdown. For example, SIGINT handler
// being interrupted by SIGTERM before is_shutting_down is set, thus causing
// double shutdown.
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, SIGTERM);
sigaddset(&action.sa_mask, SIGINT);
action.sa_flags = SA_RESTART;
sigaction(SIGTERM, &action, nullptr);
sigaction(SIGINT, &action, nullptr);
#endif /* _WIN32 */
utils::bolt::Config bolt_config{
.host = FLAGS_host,
.port = FLAGS_port,
.username = FLAGS_username,
.password = FLAGS_password,
.use_ssl = FLAGS_use_ssl,
};
if (console::is_a_tty(STDIN_FILENO)) { // INTERACTIVE
auto const history_file = std::invoke([&]() -> std::string {
if (const char *env_file = std::getenv("MGCONSOLE_HISTORY_PATH")) {
return env_file;
}
return FLAGS_history;
});
return mode::interactive::Run(bolt_config, history_file, FLAGS_no_history, FLAGS_verbose_execution_info, csv_opts,
output_opts);
} else if (FLAGS_import_mode == constants::kParserMode) {
return mode::parsing::Run(FLAGS_collect_parser_stats, FLAGS_print_parser_stats);
} else if (FLAGS_import_mode == constants::kBatchedParallel) {
return mode::batch_import::Run(bolt_config, FLAGS_batch_size, FLAGS_workers_number);
} else if (FLAGS_import_mode == constants::kSerialMode) {
return mode::serial_import::Run(bolt_config, csv_opts, output_opts);
} else {
MG_FAIL("Unknown import mode!");
}
return 0;
}