Skip to content

Commit 60af455

Browse files
committed
IRv2 to archive command line interface
1 parent 88d83bf commit 60af455

File tree

3 files changed

+219
-2
lines changed

3 files changed

+219
-2
lines changed

components/core/src/clp_s/CommandLineArguments.cpp

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ CommandLineArguments::parse_arguments(int argc, char const** argv) {
106106
std::cerr << " c - compress" << std::endl;
107107
std::cerr << " x - decompress" << std::endl;
108108
std::cerr << " s - search" << std::endl;
109+
std::cerr << " i - compress IR format" << std::endl;
109110
std::cerr << std::endl;
110111
std::cerr << "Try "
111112
<< " c --help OR"
112113
<< " x --help OR"
113-
<< " s --help for command-specific details." << std::endl;
114+
<< " s --help OR"
115+
<< " i --help for command-specific details." << std::endl;
114116

115117
po::options_description visible_options;
116118
visible_options.add(general_options);
@@ -125,6 +127,7 @@ CommandLineArguments::parse_arguments(int argc, char const** argv) {
125127
case (char)Command::Compress:
126128
case (char)Command::Extract:
127129
case (char)Command::Search:
130+
case (char)Command::IrCompress:
128131
m_command = (Command)command_input;
129132
break;
130133
default:
@@ -696,6 +699,147 @@ CommandLineArguments::parse_arguments(int argc, char const** argv) {
696699
"The --count-by-time and --count options are mutually exclusive."
697700
);
698701
}
702+
} else if (Command::IrCompress == m_command) {
703+
po::options_description compression_positional_options;
704+
// clang-format off
705+
compression_positional_options.add_options()(
706+
"archives-dir",
707+
po::value<std::string>(&m_archives_dir)->value_name("DIR"),
708+
"output directory"
709+
)(
710+
"input-paths",
711+
po::value<std::vector<std::string>>(&m_file_paths)->value_name("PATHS"),
712+
"input paths"
713+
);
714+
// clang-format on
715+
716+
po::options_description compression_options("Compression options");
717+
std::string metadata_db_config_file_path;
718+
std::string input_path_list_file_path;
719+
// clang-format off
720+
compression_options.add_options()(
721+
"compression-level",
722+
po::value<int>(&m_compression_level)->value_name("LEVEL")->
723+
default_value(m_compression_level),
724+
"1 (fast/low compression) to 9 (slow/high compression)."
725+
)(
726+
"target-encoded-size",
727+
po::value<size_t>(&m_target_encoded_size)->value_name("TARGET_ENCODED_SIZE")->
728+
default_value(m_target_encoded_size),
729+
"Target size (B) for the dictionaries and encoded messages before a new "
730+
"archive is created."
731+
)(
732+
"min-table-size",
733+
po::value<size_t>(&m_minimum_table_size)->value_name("MIN_TABLE_SIZE")->
734+
default_value(m_minimum_table_size),
735+
"Minimum size (B) for a packed table before it gets compressed."
736+
)(
737+
"max-document-size",
738+
po::value<size_t>(&m_max_document_size)->value_name("DOC_SIZE")->
739+
default_value(m_max_document_size),
740+
"Maximum allowed size (B) for a single document before compression fails."
741+
)(
742+
"timestamp-key",
743+
po::value<std::string>(&m_timestamp_key)->value_name("TIMESTAMP_COLUMN_KEY")->
744+
default_value(m_timestamp_key),
745+
"Path (e.g. x.y) for the field containing the log event's timestamp."
746+
)(
747+
"db-config-file",
748+
po::value<std::string>(&metadata_db_config_file_path)->value_name("FILE")->
749+
default_value(metadata_db_config_file_path),
750+
"Global metadata DB YAML config"
751+
)(
752+
"files-from,f",
753+
po::value<std::string>(&input_path_list_file_path)
754+
->value_name("FILE")
755+
->default_value(input_path_list_file_path),
756+
"Compress files specified in FILE"
757+
)(
758+
"print-archive-stats",
759+
po::bool_switch(&m_print_archive_stats),
760+
"Print statistics (json) about the archive after it's compressed."
761+
)(
762+
"single-file-archive",
763+
po::bool_switch(&m_single_file_archive),
764+
"Create a single archive file instead of multiple files."
765+
)(
766+
"disable-log-order",
767+
po::bool_switch(&m_disable_log_order),
768+
"Do not record log order at ingestion time."
769+
);
770+
// clang-format on
771+
772+
po::positional_options_description positional_options;
773+
positional_options.add("archives-dir", 1);
774+
positional_options.add("input-paths", -1);
775+
776+
po::options_description all_compression_options;
777+
all_compression_options.add(compression_options);
778+
all_compression_options.add(compression_positional_options);
779+
780+
std::vector<std::string> unrecognized_options
781+
= po::collect_unrecognized(parsed.options, po::include_positional);
782+
unrecognized_options.erase(unrecognized_options.begin());
783+
po::store(
784+
po::command_line_parser(unrecognized_options)
785+
.options(all_compression_options)
786+
.positional(positional_options)
787+
.run(),
788+
parsed_command_line_options
789+
);
790+
po::notify(parsed_command_line_options);
791+
792+
if (parsed_command_line_options.count("help")) {
793+
print_ir_compression_usage();
794+
795+
std::cerr << "Examples:\n";
796+
std::cerr << " # Compress file1.ir and dir1 into archives-dir\n";
797+
std::cerr << " " << m_program_name << " i archives-dir file1.ir dir1\n";
798+
799+
po::options_description visible_options;
800+
visible_options.add(general_options);
801+
visible_options.add(compression_options);
802+
std::cerr << visible_options << '\n';
803+
return ParsingResult::InfoCommand;
804+
}
805+
806+
if (m_archives_dir.empty()) {
807+
throw std::invalid_argument("No archives directory specified.");
808+
}
809+
810+
if (false == input_path_list_file_path.empty()) {
811+
if (false == read_paths_from_file(input_path_list_file_path, m_file_paths)) {
812+
SPDLOG_ERROR("Failed to read paths from {}", input_path_list_file_path);
813+
return ParsingResult::Failure;
814+
}
815+
}
816+
817+
if (m_file_paths.empty()) {
818+
throw std::invalid_argument("No input paths specified.");
819+
}
820+
821+
// Parse and validate global metadata DB config
822+
if (false == metadata_db_config_file_path.empty()) {
823+
clp::GlobalMetadataDBConfig metadata_db_config;
824+
try {
825+
metadata_db_config.parse_config_file(metadata_db_config_file_path);
826+
} catch (std::exception& e) {
827+
SPDLOG_ERROR("Failed to validate metadata database config - {}.", e.what());
828+
return ParsingResult::Failure;
829+
}
830+
831+
if (clp::GlobalMetadataDBConfig::MetadataDBType::MySQL
832+
!= metadata_db_config.get_metadata_db_type())
833+
{
834+
SPDLOG_ERROR(
835+
"Invalid metadata database type for {}; only supported type is MySQL.",
836+
m_program_name
837+
);
838+
return ParsingResult::Failure;
839+
}
840+
841+
m_metadata_db_config = std::move(metadata_db_config);
842+
}
699843
}
700844
} catch (std::exception& e) {
701845
SPDLOG_ERROR("{}", e.what());
@@ -809,4 +953,8 @@ void CommandLineArguments::print_search_usage() const {
809953
" [OUTPUT_HANDLER [OUTPUT_HANDLER_OPTIONS]]"
810954
<< std::endl;
811955
}
956+
957+
void CommandLineArguments::print_ir_compression_usage() const {
958+
std::cerr << "Usage: " << m_program_name << " i [OPTIONS] ARCHIVES_DIR [FILE/DIR ...]\n";
959+
}
812960
} // namespace clp_s

components/core/src/clp_s/CommandLineArguments.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class CommandLineArguments {
2626
enum class Command : char {
2727
Compress = 'c',
2828
Extract = 'x',
29-
Search = 's'
29+
Search = 's',
30+
IrCompress = 'i'
3031
};
3132

3233
enum class OutputHandlerType : uint8_t {
@@ -163,6 +164,8 @@ class CommandLineArguments {
163164

164165
void print_decompression_usage() const;
165166

167+
void print_ir_compression_usage() const;
168+
166169
void print_search_usage() const;
167170

168171
// Variables

components/core/src/clp_s/clp-s.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ namespace {
5050
*/
5151
bool compress(CommandLineArguments const& command_line_arguments);
5252

53+
/**
54+
* Compresses the input IR files specified by the command line arguments into an archive.
55+
* @param command_line_arguments
56+
* @return Whether compression was successful
57+
*/
58+
auto ir_compress(CommandLineArguments const& command_line_arguments) -> bool;
59+
5360
/**
5461
* Decompresses the archive specified by the given JsonConstructorOption.
5562
* @param json_constructor_option
@@ -121,6 +128,61 @@ bool compress(CommandLineArguments const& command_line_arguments) {
121128
return true;
122129
}
123130

131+
auto setup_compression_options(
132+
CommandLineArguments const& command_line_arguments,
133+
clp_s::JsonParserOption& option
134+
) -> bool {
135+
auto archives_dir = std::filesystem::path(command_line_arguments.get_archives_dir());
136+
// Create output directory in case it doesn't exist
137+
try {
138+
std::filesystem::create_directory(archives_dir.string());
139+
} catch (std::exception& e) {
140+
SPDLOG_ERROR(
141+
"Failed to create archives directory {} - {}",
142+
archives_dir.string(),
143+
e.what()
144+
);
145+
return false;
146+
}
147+
option.file_paths = command_line_arguments.get_file_paths();
148+
option.archives_dir = archives_dir.string();
149+
option.target_encoded_size = command_line_arguments.get_target_encoded_size();
150+
option.max_document_size = command_line_arguments.get_max_document_size();
151+
option.min_table_size = command_line_arguments.get_minimum_table_size();
152+
option.compression_level = command_line_arguments.get_compression_level();
153+
option.timestamp_key = command_line_arguments.get_timestamp_key();
154+
option.print_archive_stats = command_line_arguments.print_archive_stats();
155+
option.single_file_archive = command_line_arguments.get_single_file_archive();
156+
option.record_log_order = command_line_arguments.get_record_log_order();
157+
158+
auto const& db_config_container = command_line_arguments.get_metadata_db_config();
159+
if (db_config_container.has_value()) {
160+
auto const& db_config = db_config_container.value();
161+
option.metadata_db = std::make_shared<clp::GlobalMySQLMetadataDB>(
162+
db_config.get_metadata_db_host(),
163+
db_config.get_metadata_db_port(),
164+
db_config.get_metadata_db_username(),
165+
db_config.get_metadata_db_password(),
166+
db_config.get_metadata_db_name(),
167+
db_config.get_metadata_table_prefix()
168+
);
169+
}
170+
return true;
171+
}
172+
173+
auto ir_compress(CommandLineArguments const& command_line_arguments) -> bool {
174+
clp_s::JsonParserOption option{};
175+
if (false == setup_compression_options(command_line_arguments, option)) {
176+
return false;
177+
}
178+
179+
// Functionality Coming in later PR
180+
// -->Instantiate Json Parser
181+
// -->Call new parsing function in Json Parser to parse IRv2 to archive
182+
// -->Store Archive
183+
return true;
184+
}
185+
124186
void decompress_archive(clp_s::JsonConstructorOption const& json_constructor_option) {
125187
clp_s::JsonConstructor constructor(json_constructor_option);
126188
constructor.store();
@@ -290,6 +352,10 @@ int main(int argc, char const* argv[]) {
290352
if (false == compress(command_line_arguments)) {
291353
return 1;
292354
}
355+
} else if (CommandLineArguments::Command::IrCompress == command_line_arguments.get_command()) {
356+
if (false == ir_compress(command_line_arguments)) {
357+
return 1;
358+
}
293359
} else if (CommandLineArguments::Command::Extract == command_line_arguments.get_command()) {
294360
auto const& archives_dir = command_line_arguments.get_archives_dir();
295361
if (false == std::filesystem::is_directory(archives_dir)) {

0 commit comments

Comments
 (0)