diff --git a/components/core/src/clp/DictionaryReader.hpp b/components/core/src/clp/DictionaryReader.hpp index 0499e50eb..694240ad5 100644 --- a/components/core/src/clp/DictionaryReader.hpp +++ b/components/core/src/clp/DictionaryReader.hpp @@ -107,8 +107,8 @@ class DictionaryReader { // Variables bool m_is_open; - FileReader m_dictionary_file_reader; - FileReader m_segment_index_file_reader; + std::unique_ptr m_dictionary_file_reader; + std::unique_ptr m_segment_index_file_reader; #if USE_PASSTHROUGH_COMPRESSION streaming_compression::passthrough::Decompressor m_dictionary_decompressor; streaming_compression::passthrough::Decompressor m_segment_index_decompressor; @@ -133,14 +133,19 @@ void DictionaryReader::open( constexpr size_t cDecompressorFileReadBufferCapacity = 64 * 1024; // 64 KB - open_dictionary_for_reading( - dictionary_path, - segment_index_path, - cDecompressorFileReadBufferCapacity, - m_dictionary_file_reader, - m_dictionary_decompressor, - m_segment_index_file_reader, - m_segment_index_decompressor + m_dictionary_file_reader = make_unique(dictionary_path); + + // Skip header and then open the decompressor + m_dictionary_file_reader->seek_from_begin(sizeof(uint64_t)); + m_dictionary_decompressor.open(*m_dictionary_file_reader, cDecompressorFileReadBufferCapacity); + + m_segment_index_file_reader = make_unique(segment_index_path); + + // Skip header and then open the decompressor + m_segment_index_file_reader->seek_from_begin(sizeof(uint64_t)); + m_segment_index_decompressor.open( + *m_segment_index_file_reader, + cDecompressorFileReadBufferCapacity ); m_is_open = true; @@ -153,9 +158,9 @@ void DictionaryReader::close() { } m_segment_index_decompressor.close(); - m_segment_index_file_reader.close(); + m_segment_index_file_reader.reset(); m_dictionary_decompressor.close(); - m_dictionary_file_reader.close(); + m_dictionary_file_reader.reset(); m_num_segments_read_from_index = 0; m_entries.clear(); @@ -170,7 +175,7 @@ void DictionaryReader::read_new_entries() { } // Read dictionary header - auto num_dictionary_entries = read_dictionary_header(m_dictionary_file_reader); + auto num_dictionary_entries = read_dictionary_header(*m_dictionary_file_reader); // Validate dictionary header if (num_dictionary_entries < m_entries.size()) { @@ -190,7 +195,7 @@ void DictionaryReader::read_new_entries() { } // Read segment index header - auto num_segments = read_segment_index_header(m_segment_index_file_reader); + auto num_segments = read_segment_index_header(*m_segment_index_file_reader); // Validate segment index header if (num_segments < m_num_segments_read_from_index) { diff --git a/components/core/src/clp/DictionaryWriter.hpp b/components/core/src/clp/DictionaryWriter.hpp index e9b6f623c..9e3541913 100644 --- a/components/core/src/clp/DictionaryWriter.hpp +++ b/components/core/src/clp/DictionaryWriter.hpp @@ -3,12 +3,9 @@ #include #include -#include -#include #include "ArrayBackedPosIntSet.hpp" #include "Defs.h" -#include "dictionary_utils.hpp" #include "FileWriter.hpp" #include "spdlog_with_specializations.hpp" #include "streaming_compression/passthrough/Compressor.hpp" @@ -63,18 +60,6 @@ class DictionaryWriter { */ void write_header_and_flush_to_disk(); - /** - * Opens dictionary, loads entries, and then sets it up for writing - * @param dictionary_path - * @param segment_index_path - * @param max_id - */ - void open_and_preload( - std::string const& dictionary_path, - std::string const& segment_index_path, - variable_dictionary_id_t max_id - ); - /** * Adds the given segment and IDs to the segment index * @param segment_id @@ -191,85 +176,6 @@ void DictionaryWriter::write_header_and_flush_to_di m_dictionary_file_writer.flush(); } -template -void DictionaryWriter::open_and_preload( - std::string const& dictionary_path, - std::string const& segment_index_path, - variable_dictionary_id_t const max_id -) { - if (m_is_open) { - throw OperationFailed(ErrorCode_NotReady, __FILENAME__, __LINE__); - } - - m_max_id = max_id; - - FileReader dictionary_file_reader; - FileReader segment_index_file_reader; -#if USE_PASSTHROUGH_COMPRESSION - streaming_compression::passthrough::Decompressor dictionary_decompressor; - streaming_compression::passthrough::Decompressor segment_index_decompressor; -#elif USE_ZSTD_COMPRESSION - streaming_compression::zstd::Decompressor dictionary_decompressor; - streaming_compression::zstd::Decompressor segment_index_decompressor; -#else - static_assert(false, "Unsupported compression mode."); -#endif - constexpr size_t cDecompressorFileReadBufferCapacity = 64 * 1024; // 64 KB - open_dictionary_for_reading( - dictionary_path, - segment_index_path, - cDecompressorFileReadBufferCapacity, - dictionary_file_reader, - dictionary_decompressor, - segment_index_file_reader, - segment_index_decompressor - ); - - auto num_dictionary_entries = read_dictionary_header(dictionary_file_reader); - if (num_dictionary_entries > m_max_id) { - SPDLOG_ERROR("DictionaryWriter ran out of IDs."); - throw OperationFailed(ErrorCode_OutOfBounds, __FILENAME__, __LINE__); - } - // Loads entries from the given dictionary file - EntryType entry; - for (size_t i = 0; i < num_dictionary_entries; ++i) { - entry.clear(); - entry.read_from_file(dictionary_decompressor); - auto const& str_value = entry.get_value(); - if (m_value_to_id.count(str_value)) { - SPDLOG_ERROR("Entry's value already exists in dictionary"); - throw OperationFailed(ErrorCode_Corrupt, __FILENAME__, __LINE__); - } - - m_value_to_id[str_value] = entry.get_id(); - ; - m_data_size += entry.get_data_size(); - } - - m_next_id = num_dictionary_entries; - - segment_index_decompressor.close(); - segment_index_file_reader.close(); - dictionary_decompressor.close(); - dictionary_file_reader.close(); - - m_dictionary_file_writer.open( - dictionary_path, - FileWriter::OpenMode::CREATE_IF_NONEXISTENT_FOR_SEEKABLE_WRITING - ); - // Open compressor - m_dictionary_compressor.open(m_dictionary_file_writer); - - m_segment_index_file_writer.open( - segment_index_path, - FileWriter::OpenMode::CREATE_IF_NONEXISTENT_FOR_SEEKABLE_WRITING - ); - // Open compressor - m_segment_index_compressor.open(m_segment_index_file_writer); - - m_is_open = true; -} - template void DictionaryWriter::index_segment( segment_id_t segment_id, diff --git a/components/core/src/clp/FileReader.cpp b/components/core/src/clp/FileReader.cpp index 06a986383..8a51b1827 100644 --- a/components/core/src/clp/FileReader.cpp +++ b/components/core/src/clp/FileReader.cpp @@ -12,15 +12,26 @@ using std::string; namespace clp { +FileReader::FileReader(string const& path) : m_file{fopen(path.c_str(), "rb")} { + if (nullptr == m_file) { + if (ENOENT == errno) { + throw OperationFailed(ErrorCode_FileNotFound, __FILE__, __LINE__); + } + throw OperationFailed(ErrorCode_errno, __FILE__, __LINE__); + } + m_path = path; +} + FileReader::~FileReader() { - close(); + if (nullptr != m_file) { + // NOTE: We don't check errors for fclose since it seems the only reason it could fail is + // if it was interrupted by a signal + fclose(m_file); + } free(m_getdelim_buf); } ErrorCode FileReader::try_read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) { - if (nullptr == m_file) { - return ErrorCode_NotInit; - } if (nullptr == buf) { return ErrorCode_BadParam; } @@ -40,10 +51,6 @@ ErrorCode FileReader::try_read(char* buf, size_t num_bytes_to_read, size_t& num_ } ErrorCode FileReader::try_seek_from_begin(size_t pos) { - if (nullptr == m_file) { - return ErrorCode_NotInit; - } - int retval = fseeko(m_file, pos, SEEK_SET); if (0 != retval) { return ErrorCode_errno; @@ -53,10 +60,6 @@ ErrorCode FileReader::try_seek_from_begin(size_t pos) { } ErrorCode FileReader::try_get_pos(size_t& pos) { - if (nullptr == m_file) { - return ErrorCode_NotInit; - } - pos = ftello(m_file); if ((off_t)-1 == pos) { return ErrorCode_errno; @@ -65,49 +68,14 @@ ErrorCode FileReader::try_get_pos(size_t& pos) { return ErrorCode_Success; } -ErrorCode FileReader::try_open(string const& path) { - // Cleanup in case caller forgot to call close before calling this function - close(); - - m_file = fopen(path.c_str(), "rb"); - if (nullptr == m_file) { - if (ENOENT == errno) { - return ErrorCode_FileNotFound; - } - return ErrorCode_errno; - } - m_path = path; - - return ErrorCode_Success; -} - -void FileReader::open(string const& path) { - ErrorCode error_code = try_open(path); - if (ErrorCode_Success != error_code) { - if (ErrorCode_FileNotFound == error_code) { - throw "File not found: " + boost::filesystem::weakly_canonical(path).string() + "\n"; - } else { - throw OperationFailed(error_code, __FILENAME__, __LINE__); - } - } -} - -void FileReader::close() { - if (m_file != nullptr) { - // NOTE: We don't check errors for fclose since it seems the only reason it could fail is if - // it was interrupted by a signal - fclose(m_file); - m_file = nullptr; - } -} - ErrorCode FileReader::try_read_to_delimiter(char delim, bool keep_delimiter, bool append, string& str) { - assert(nullptr != m_file); - if (false == append) { str.clear(); } + + // NOTE: If `m_getdelim_buf` is a null pointer or if `m_getdelim_buf_len` is insufficient in + // size, `getdelim` will malloc or realloc enough memory, respectively, to hold the characters. ssize_t num_bytes_read = getdelim(&m_getdelim_buf, &m_getdelim_buf_len, delim, m_file); if (num_bytes_read < 1) { if (ferror(m_file)) { @@ -125,10 +93,6 @@ FileReader::try_read_to_delimiter(char delim, bool keep_delimiter, bool append, } ErrorCode FileReader::try_fstat(struct stat& stat_buffer) { - if (nullptr == m_file) { - throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__); - } - auto return_value = fstat(fileno(m_file), &stat_buffer); if (0 != return_value) { return ErrorCode_errno; diff --git a/components/core/src/clp/FileReader.hpp b/components/core/src/clp/FileReader.hpp index 56e376af6..269297cf3 100644 --- a/components/core/src/clp/FileReader.hpp +++ b/components/core/src/clp/FileReader.hpp @@ -25,7 +25,7 @@ class FileReader : public ReaderInterface { char const* what() const noexcept override { return "FileReader operation failed"; } }; - FileReader() : m_file(nullptr), m_getdelim_buf_len(0), m_getdelim_buf(nullptr) {} + FileReader(std::string const& path); ~FileReader(); @@ -33,7 +33,6 @@ class FileReader : public ReaderInterface { /** * Tries to get the current position of the read head in the file * @param pos Position of the read head in the file - * @return ErrorCode_NotInit if the file is not open * @return ErrorCode_errno on error * @return ErrorCode_Success on success */ @@ -41,7 +40,6 @@ class FileReader : public ReaderInterface { /** * Tries to seek from the beginning of the file to the given position * @param pos - * @return ErrorCode_NotInit if the file is not open * @return ErrorCode_errno on error * @return ErrorCode_Success on success */ @@ -52,7 +50,6 @@ class FileReader : public ReaderInterface { * @param buf * @param num_bytes_to_read The number of bytes to try and read * @param num_bytes_read The actual number of bytes read - * @return ErrorCode_NotInit if the file is not open * @return ErrorCode_BadParam if buf is invalid * @return ErrorCode_errno on error * @return ErrorCode_EndOfFile on EOF @@ -73,28 +70,6 @@ class FileReader : public ReaderInterface { ErrorCode try_read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str) override; - // Methods - bool is_open() const { return m_file != nullptr; } - - /** - * Tries to open a file - * @param path - * @return ErrorCode_Success on success - * @return ErrorCode_FileNotFound if the file was not found - * @return ErrorCode_errno otherwise - */ - ErrorCode try_open(std::string const& path); - /** - * Opens a file - * @param path - * @throw FileReader::OperationFailed on failure - */ - void open(std::string const& path); - /** - * Closes the file if it's open - */ - void close(); - [[nodiscard]] std::string const& get_path() const { return m_path; } /** @@ -106,9 +81,9 @@ class FileReader : public ReaderInterface { ErrorCode try_fstat(struct stat& stat_buffer); private: - FILE* m_file; - size_t m_getdelim_buf_len; - char* m_getdelim_buf; + FILE* m_file{nullptr}; + size_t m_getdelim_buf_len{0}; + char* m_getdelim_buf{nullptr}; std::string m_path; }; } // namespace clp diff --git a/components/core/src/clp/Utils.cpp b/components/core/src/clp/Utils.cpp index 1a45c5bf9..f487a3880 100644 --- a/components/core/src/clp/Utils.cpp +++ b/components/core/src/clp/Utils.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -17,7 +18,9 @@ #include "spdlog_with_specializations.hpp" using std::list; +using std::make_unique; using std::string; +using std::unique_ptr; using std::vector; namespace clp { @@ -137,16 +140,18 @@ string get_unambiguous_path(string const& path) { } ErrorCode read_list_of_paths(string const& list_path, vector& paths) { - FileReader file_reader; - ErrorCode error_code = file_reader.try_open(list_path); - if (ErrorCode_Success != error_code) { - return error_code; + unique_ptr file_reader; + try { + file_reader = make_unique(list_path); + } catch (FileReader::OperationFailed const& err) { + return err.get_error_code(); } // Read file string line; + ErrorCode error_code{ErrorCode_Success}; while (true) { - error_code = file_reader.try_read_to_delimiter('\n', false, false, line); + error_code = file_reader->try_read_to_delimiter('\n', false, false, line); if (ErrorCode_Success != error_code) { break; } @@ -160,8 +165,6 @@ ErrorCode read_list_of_paths(string const& list_path, vector& paths) { return error_code; } - file_reader.close(); - return ErrorCode_Success; } @@ -262,38 +265,29 @@ void load_lexer_from_file( } if (contains_delimiter) { - FileReader schema_reader; - ErrorCode error_code = schema_reader.try_open(schema_ast->m_file_path); - if (ErrorCode_Success != error_code) { - throw std::runtime_error( - schema_file_path + ":" + std::to_string(rule->m_line_num + 1) + ": error: '" - + rule->m_name + "' has regex pattern which contains delimiter '" - + char(delimiter_name) + "'.\n" - ); - } else { - // more detailed debugging based on looking at the file - string line; - for (uint32_t i = 0; i <= rule->m_line_num; i++) { - schema_reader.read_to_delimiter('\n', false, false, line); - } - int colon_pos = 0; - for (char i : line) { - colon_pos++; - if (i == ':') { - break; - } + FileReader schema_reader{schema_ast->m_file_path}; + // more detailed debugging based on looking at the file + string line; + for (uint32_t i = 0; i <= rule->m_line_num; i++) { + schema_reader.read_to_delimiter('\n', false, false, line); + } + int colon_pos = 0; + for (char i : line) { + colon_pos++; + if (i == ':') { + break; } - string indent(10, ' '); - string spaces(colon_pos, ' '); - string arrows(line.size() - colon_pos, '^'); - - throw std::runtime_error( - schema_file_path + ":" + std::to_string(rule->m_line_num + 1) + ": error: '" - + rule->m_name + "' has regex pattern which contains delimiter '" - + char(delimiter_name) + "'.\n" + indent + line + "\n" + indent + spaces - + arrows + "\n" - ); } + string indent(10, ' '); + string spaces(colon_pos, ' '); + string arrows(line.size() - colon_pos, '^'); + + throw std::runtime_error( + schema_file_path + ":" + std::to_string(rule->m_line_num + 1) + ": error: '" + + rule->m_name + "' has regex pattern which contains delimiter '" + + char(delimiter_name) + "'.\n" + indent + line + "\n" + indent + spaces + + arrows + "\n" + ); } lexer.add_rule(lexer.m_symbol_id[rule->m_name], std::move(rule->m_regex_ptr)); } diff --git a/components/core/src/clp/clg/clg.cpp b/components/core/src/clp/clg/clg.cpp index 4580358b7..dd35a3283 100644 --- a/components/core/src/clp/clg/clg.cpp +++ b/components/core/src/clp/clg/clg.cpp @@ -494,15 +494,13 @@ int main(int argc, char const* argv[]) { if (command_line_args.get_search_strings_file_path().empty()) { search_strings.push_back(command_line_args.get_search_string()); } else { - FileReader file_reader; - file_reader.open(command_line_args.get_search_strings_file_path()); + FileReader file_reader{command_line_args.get_search_strings_file_path()}; string line; while (file_reader.read_to_delimiter('\n', false, false, line)) { if (!line.empty()) { search_strings.push_back(line); } } - file_reader.close(); } // Validate archives directory @@ -589,8 +587,7 @@ int main(int argc, char const* argv[]) { use_heuristic = false; char buf[max_map_schema_length]; - FileReader file_reader; - file_reader.try_open(schema_file_path); + FileReader file_reader{schema_file_path}; size_t num_bytes_read; file_reader.read(buf, max_map_schema_length, num_bytes_read); diff --git a/components/core/src/clp/clp/compression.cpp b/components/core/src/clp/clp/compression.cpp index 1741557bc..a0d5bf276 100644 --- a/components/core/src/clp/clp/compression.cpp +++ b/components/core/src/clp/clp/compression.cpp @@ -1,6 +1,7 @@ #include "compression.hpp" #include +#include #include #include @@ -19,8 +20,10 @@ using clp::streaming_archive::writer::split_archive; using std::cerr; using std::cout; using std::endl; +using std::make_unique; using std::out_of_range; using std::string; +using std::unique_ptr; using std::vector; namespace clp::clp { @@ -192,9 +195,11 @@ bool read_and_validate_grouped_file_list( string const& list_path, vector& grouped_files ) { - FileReader grouped_file_path_reader; - ErrorCode error_code = grouped_file_path_reader.try_open(list_path); - if (ErrorCode_Success != error_code) { + unique_ptr grouped_file_path_reader; + try { + grouped_file_path_reader = make_unique(list_path); + } catch (FileReader::OperationFailed const& exception) { + auto const error_code = exception.get_error_code(); if (ErrorCode_FileNotFound == error_code) { SPDLOG_ERROR("'{}' does not exist.", list_path.c_str()); } else if (ErrorCode_errno == error_code) { @@ -205,10 +210,12 @@ bool read_and_validate_grouped_file_list( return false; } - FileReader grouped_file_id_reader; + unique_ptr grouped_file_id_reader; string grouped_file_ids_path = list_path.substr(0, list_path.length() - 4) + ".gid"; - error_code = grouped_file_id_reader.try_open(grouped_file_ids_path); - if (ErrorCode_Success != error_code) { + try { + grouped_file_id_reader = make_unique(grouped_file_ids_path); + } catch (FileReader::OperationFailed const& exception) { + auto const error_code = exception.get_error_code(); if (ErrorCode_FileNotFound == error_code) { SPDLOG_ERROR("'{}' does not exist.", grouped_file_ids_path.c_str()); } else if (ErrorCode_errno == error_code) { @@ -228,9 +235,10 @@ bool read_and_validate_grouped_file_list( string path; string path_without_prefix; group_id_t group_id; + ErrorCode error_code{ErrorCode_Success}; while (true) { // Read path - error_code = grouped_file_path_reader.try_read_to_delimiter('\n', false, false, path); + error_code = grouped_file_path_reader->try_read_to_delimiter('\n', false, false, path); if (ErrorCode_Success != error_code) { break; } @@ -242,7 +250,7 @@ bool read_and_validate_grouped_file_list( } // Read group ID - error_code = grouped_file_id_reader.try_read_numeric_value(group_id); + error_code = grouped_file_id_reader->try_read_numeric_value(group_id); if (ErrorCode_Success != error_code) { if (ErrorCode_EndOfFile == error_code) { SPDLOG_ERROR("There are more grouped file paths than IDs."); @@ -294,9 +302,6 @@ bool read_and_validate_grouped_file_list( return false; } - grouped_file_path_reader.close(); - grouped_file_id_reader.close(); - // Validate the list contained at least one file if (grouped_files.empty()) { SPDLOG_ERROR("'{}' did not contain any paths.", list_path.c_str()); diff --git a/components/core/src/clp/dictionary_utils.cpp b/components/core/src/clp/dictionary_utils.cpp index 2fecd7e04..d8bd20a9a 100644 --- a/components/core/src/clp/dictionary_utils.cpp +++ b/components/core/src/clp/dictionary_utils.cpp @@ -1,35 +1,12 @@ #include "dictionary_utils.hpp" -namespace clp { -void open_dictionary_for_reading( - std::string const& dictionary_path, - std::string const& segment_index_path, - size_t decompressor_file_read_buffer_capacity, - FileReader& dictionary_file_reader, - streaming_compression::Decompressor& dictionary_decompressor, - FileReader& segment_index_file_reader, - streaming_compression::Decompressor& segment_index_decompressor -) { - dictionary_file_reader.open(dictionary_path); - // Skip header - dictionary_file_reader.seek_from_begin(sizeof(uint64_t)); - // Open decompressor - dictionary_decompressor.open(dictionary_file_reader, decompressor_file_read_buffer_capacity); - - segment_index_file_reader.open(segment_index_path); - // Skip header - segment_index_file_reader.seek_from_begin(sizeof(uint64_t)); - // Open decompressor - segment_index_decompressor.open( - segment_index_file_reader, - decompressor_file_read_buffer_capacity - ); -} +#include "FileReader.hpp" +namespace clp { uint64_t read_dictionary_header(FileReader& file_reader) { auto dictionary_file_reader_pos = file_reader.get_pos(); file_reader.seek_from_begin(0); - uint64_t num_dictionary_entries; + uint64_t num_dictionary_entries{0}; file_reader.read_numeric_value(num_dictionary_entries, false); file_reader.seek_from_begin(dictionary_file_reader_pos); return num_dictionary_entries; @@ -39,7 +16,7 @@ uint64_t read_segment_index_header(FileReader& file_reader) { // Read segment index header auto segment_index_file_reader_pos = file_reader.get_pos(); file_reader.seek_from_begin(0); - uint64_t num_segments; + uint64_t num_segments{0}; file_reader.read_numeric_value(num_segments, false); file_reader.seek_from_begin(segment_index_file_reader_pos); return num_segments; diff --git a/components/core/src/clp/dictionary_utils.hpp b/components/core/src/clp/dictionary_utils.hpp index 42012964f..14509a4e9 100644 --- a/components/core/src/clp/dictionary_utils.hpp +++ b/components/core/src/clp/dictionary_utils.hpp @@ -1,22 +1,10 @@ #ifndef CLP_DICTIONARY_UTILS_HPP #define CLP_DICTIONARY_UTILS_HPP -#include - #include "FileReader.hpp" #include "streaming_compression/Decompressor.hpp" namespace clp { -void open_dictionary_for_reading( - std::string const& dictionary_path, - std::string const& segment_index_path, - size_t decompressor_file_read_buffer_capacity, - FileReader& dictionary_file_reader, - streaming_compression::Decompressor& dictionary_decompressor, - FileReader& segment_index_file_reader, - streaming_compression::Decompressor& segment_index_decompressor -); - uint64_t read_dictionary_header(FileReader& file_reader); uint64_t read_segment_index_header(FileReader& file_reader); diff --git a/components/core/src/clp/streaming_archive/reader/Archive.cpp b/components/core/src/clp/streaming_archive/reader/Archive.cpp index 05e42cac3..141e5fce5 100644 --- a/components/core/src/clp/streaming_archive/reader/Archive.cpp +++ b/components/core/src/clp/streaming_archive/reader/Archive.cpp @@ -37,11 +37,9 @@ void Archive::open(string const& path) { string metadata_file_path = path + '/' + cMetadataFileName; archive_format_version_t format_version{}; try { - FileReader file_reader; - file_reader.open(metadata_file_path); + FileReader file_reader{metadata_file_path}; ArchiveMetadata const metadata{file_reader}; format_version = metadata.get_archive_format_version(); - file_reader.close(); } catch (TraceableException& traceable_exception) { auto error_code = traceable_exception.get_error_code(); if (ErrorCode_errno == error_code) { diff --git a/components/core/tests/test-BufferedFileReader.cpp b/components/core/tests/test-BufferedFileReader.cpp index 734eca87b..f182a777c 100644 --- a/components/core/tests/test-BufferedFileReader.cpp +++ b/components/core/tests/test-BufferedFileReader.cpp @@ -277,8 +277,7 @@ TEST_CASE("Test delimiter", "[BufferedFileReader]") { file_reader.open(test_file_path); std::string test_string; - clp::FileReader ref_file_reader; - ref_file_reader.open(test_file_path); + clp::FileReader ref_file_reader{test_file_path}; std::string ref_string; // Validate that a FileReader and a BufferedFileReader return the same strings (split by @@ -292,7 +291,6 @@ TEST_CASE("Test delimiter", "[BufferedFileReader]") { REQUIRE(test_string == ref_string); } - ref_file_reader.close(); file_reader.close(); boost::filesystem::remove(test_file_path); } diff --git a/components/core/tests/test-MemoryMappedFile.cpp b/components/core/tests/test-MemoryMappedFile.cpp index 20d433f32..9cb855e0b 100644 --- a/components/core/tests/test-MemoryMappedFile.cpp +++ b/components/core/tests/test-MemoryMappedFile.cpp @@ -42,10 +42,8 @@ TEST_CASE("memory_mapped_file_view_basic", "[ReadOnlyMemoryMappedFile]") { auto const test_input_path{ get_test_dir() / std::filesystem::path{"test_network_reader_src"} / "random.log" }; - clp::FileReader file_reader; - file_reader.open(test_input_path.string()); + clp::FileReader file_reader{test_input_path.string()}; auto const expected{read_content(file_reader)}; - file_reader.close(); clp::ReadOnlyMemoryMappedFile const mmap_file{test_input_path.string()}; auto const view{mmap_file.get_view()}; diff --git a/components/core/tests/test-NetworkReader.cpp b/components/core/tests/test-NetworkReader.cpp index d06876f5f..38172507d 100644 --- a/components/core/tests/test-NetworkReader.cpp +++ b/components/core/tests/test-NetworkReader.cpp @@ -67,10 +67,8 @@ auto get_content(clp::ReaderInterface& reader, size_t read_buf_size) -> std::vec } // namespace TEST_CASE("network_reader_basic", "[NetworkReader]") { - clp::FileReader ref_reader; - ref_reader.open(get_test_input_local_path()); + clp::FileReader ref_reader{get_test_input_local_path()}; auto const expected{get_content(ref_reader)}; - ref_reader.close(); clp::CurlGlobalInstance const curl_global_instance; clp::NetworkReader reader{get_test_input_remote_url()}; @@ -84,12 +82,10 @@ TEST_CASE("network_reader_basic", "[NetworkReader]") { TEST_CASE("network_reader_with_offset_and_seek", "[NetworkReader]") { constexpr size_t cOffset{319}; - clp::FileReader ref_reader; - ref_reader.open(get_test_input_local_path()); + clp::FileReader ref_reader{get_test_input_local_path()}; ref_reader.seek_from_begin(cOffset); auto const expected{get_content(ref_reader)}; auto const ref_end_pos{ref_reader.get_pos()}; - ref_reader.close(); // Read from an offset onwards by starting the download from that offset. { diff --git a/components/core/tests/test-ParserWithUserSchema.cpp b/components/core/tests/test-ParserWithUserSchema.cpp index d69a94958..3689c69e8 100644 --- a/components/core/tests/test-ParserWithUserSchema.cpp +++ b/components/core/tests/test-ParserWithUserSchema.cpp @@ -162,9 +162,8 @@ TEST_CASE("Test forward lexer", "[Search]") { std::string schema_file_name = "../tests/test_schema_files/search_schema.txt"; std::string schema_file_path = boost::filesystem::weakly_canonical(schema_file_name).string(); load_lexer_from_file(schema_file_path, false, forward_lexer); - FileReader file_reader; + FileReader file_reader{"../tests/test_search_queries/easy.txt"}; LogSurgeonReader reader_wrapper(file_reader); - file_reader.open("../tests/test_search_queries/easy.txt"); log_surgeon::ParserInputBuffer parser_input_buffer; parser_input_buffer.read_if_safe(reader_wrapper); forward_lexer.reset(); @@ -187,9 +186,8 @@ TEST_CASE("Test reverse lexer", "[Search]") { std::string schema_file_name = "../tests/test_schema_files/search_schema.txt"; std::string schema_file_path = boost::filesystem::weakly_canonical(schema_file_name).string(); load_lexer_from_file(schema_file_path, false, reverse_lexer); - FileReader file_reader; + FileReader file_reader{"../tests/test_search_queries/easy.txt"}; LogSurgeonReader reader_wrapper(file_reader); - file_reader.open("../tests/test_search_queries/easy.txt"); log_surgeon::ParserInputBuffer parser_input_buffer; parser_input_buffer.read_if_safe(reader_wrapper); reverse_lexer.reset();