From bb326b8f0d925162ba773f235064c93cf61c98b7 Mon Sep 17 00:00:00 2001 From: kirkrodrigues <2454684+kirkrodrigues@users.noreply.github.com> Date: Wed, 20 Dec 2023 08:02:39 -0500 Subject: [PATCH] Rename IR-stream related methods: (#189) - Change encode/decode to serialize/deserialize. - Change "message" to "log event" where appropriate. --- .../src/ffi/ir_stream/decoding_methods.cpp | 138 ++++++++++-------- .../src/ffi/ir_stream/decoding_methods.hpp | 53 +++---- .../src/ffi/ir_stream/encoding_methods.cpp | 64 ++++---- .../src/ffi/ir_stream/encoding_methods.hpp | 24 +-- .../core/src/ir/LogEventDeserializer.cpp | 4 +- .../core/tests/test-ir_encoding_methods.cpp | 94 +++++++----- 6 files changed, 204 insertions(+), 173 deletions(-) diff --git a/components/core/src/ffi/ir_stream/decoding_methods.cpp b/components/core/src/ffi/ir_stream/decoding_methods.cpp index 9fb6a963e..51e433914 100644 --- a/components/core/src/ffi/ir_stream/decoding_methods.cpp +++ b/components/core/src/ffi/ir_stream/decoding_methods.cpp @@ -21,41 +21,41 @@ template static bool is_variable_tag(encoded_tag_t tag, bool& is_encoded_var); /** - * Decodes an integer from the given reader - * @tparam integer_t Type of the integer to decode + * Deserializes an integer from the given reader + * @tparam integer_t Type of the integer to deserialize * @param reader - * @param value Returns the decoded integer - * @return true on success, false if the reader doesn't contain enough data to decode + * @param value Returns the deserialized integer + * @return true on success, false if the reader doesn't contain enough data to deserialize */ template -static bool decode_int(ReaderInterface& reader, integer_t& value); +static bool deserialize_int(ReaderInterface& reader, integer_t& value); /** - * Decodes the next logtype string from the given reader + * Deserializes a logtype from the given reader * @param reader * @param encoded_tag - * @param logtype Returns the logtype string + * @param logtype Returns the logtype * @return IRErrorCode_Success on success * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize */ static IRErrorCode -parse_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype); +deserialize_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype); /** - * Decodes the next dictionary-type variable string from the given reader + * Deserializes a dictionary-type variable from the given reader * @param reader * @param encoded_tag * @param dict_var Returns the dictionary variable * @return IRErrorCode_Success on success * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if input buffer doesn't contain enough data to decode + * @return IRErrorCode_Incomplete_IR if input buffer doesn't contain enough data to deserialize */ static IRErrorCode -parse_dictionary_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& dict_var); +deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& dict_var); /** - * Parses the next timestamp from the given reader + * Deserializes a timestamp from the given reader * @tparam encoded_variable_t Type of the encoded variable * @param reader * @param encoded_tag @@ -63,40 +63,43 @@ parse_dictionary_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& * the actual timestamp if encoded_variable_t == eight_byte_encoded_variable_t * @return IRErrorCode_Success on success * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize */ template static IRErrorCode -parse_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts); +deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts); /** - * Decodes the next encoded message from the given reader + * Deserializes the next log event from the given reader * @tparam encoded_variable_t Type of the encoded variable * @param reader - * @param message Returns the decoded message + * @param message Returns the deserialized message * @param timestamp Returns the timestamp delta if * encoded_variable_t == four_byte_encoded_variable_t or the actual timestamp if * encoded_variable_t == eight_byte_encoded_variable_t * @return IRErrorCode_Success on success - * @return IRErrorCode_Decode_Error if the encoded message cannot be properly decoded - * @return Same as ffi::ir_stream::deserialize_ir_message + * @return IRErrorCode_Decode_Error if the log event cannot be properly deserialized + * @return Same as ffi::ir_stream::deserialize_log_event */ template static IRErrorCode -generic_decode_next_message(ReaderInterface& reader, string& message, epoch_time_ms_t& timestamp); +generic_deserialize_log_event(ReaderInterface& reader, string& message, epoch_time_ms_t& timestamp); /** - * Reads metadata information from the given reader + * Deserializes metadata from the given reader * @param reader * @param metadata_type Returns the type of the metadata found in the IR * @param metadata_pos Returns the starting position of the metadata in reader * @param metadata_size Returns the size of the metadata written in the IR * @return IRErrorCode_Success on success * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize */ -static IRErrorCode -read_metadata_info(ReaderInterface& reader, encoded_tag_t& metadata_type, uint16_t& metadata_size); +static IRErrorCode deserialize_metadata( + ReaderInterface& reader, + encoded_tag_t& metadata_type, + uint16_t& metadata_size +); template static bool is_variable_tag(encoded_tag_t tag, bool& is_encoded_var) { @@ -127,7 +130,7 @@ static bool is_variable_tag(encoded_tag_t tag, bool& is_encoded_var) { } template -static bool decode_int(ReaderInterface& reader, integer_t& value) { +static bool deserialize_int(ReaderInterface& reader, integer_t& value) { integer_t value_little_endian; if (reader.try_read_numeric_value(value_little_endian) != ErrorCode_Success) { return false; @@ -148,23 +151,23 @@ static bool decode_int(ReaderInterface& reader, integer_t& value) { } static IRErrorCode -parse_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype) { +deserialize_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype) { size_t logtype_length; if (encoded_tag == cProtocol::Payload::LogtypeStrLenUByte) { uint8_t length; - if (false == decode_int(reader, length)) { + if (false == deserialize_int(reader, length)) { return IRErrorCode_Incomplete_IR; } logtype_length = length; } else if (encoded_tag == cProtocol::Payload::LogtypeStrLenUShort) { uint16_t length; - if (false == decode_int(reader, length)) { + if (false == deserialize_int(reader, length)) { return IRErrorCode_Incomplete_IR; } logtype_length = length; } else if (encoded_tag == cProtocol::Payload::LogtypeStrLenInt) { int32_t length; - if (false == decode_int(reader, length)) { + if (false == deserialize_int(reader, length)) { return IRErrorCode_Incomplete_IR; } logtype_length = length; @@ -179,24 +182,24 @@ parse_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtyp } static IRErrorCode -parse_dictionary_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& dict_var) { - // Decode variable's length +deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& dict_var) { + // Deserialize variable's length size_t var_length; if (cProtocol::Payload::VarStrLenUByte == encoded_tag) { uint8_t length; - if (false == decode_int(reader, length)) { + if (false == deserialize_int(reader, length)) { return IRErrorCode_Incomplete_IR; } var_length = length; } else if (cProtocol::Payload::VarStrLenUShort == encoded_tag) { uint16_t length; - if (false == decode_int(reader, length)) { + if (false == deserialize_int(reader, length)) { return IRErrorCode_Incomplete_IR; } var_length = length; } else if (cProtocol::Payload::VarStrLenInt == encoded_tag) { int32_t length; - if (false == decode_int(reader, length)) { + if (false == deserialize_int(reader, length)) { return IRErrorCode_Incomplete_IR; } var_length = length; @@ -214,7 +217,7 @@ parse_dictionary_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& template static IRErrorCode -parse_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts) { +deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts) { static_assert( (is_same_v || is_same_v) @@ -224,31 +227,31 @@ parse_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_m if (cProtocol::Payload::TimestampVal != encoded_tag) { return IRErrorCode_Corrupted_IR; } - if (false == decode_int(reader, ts)) { + if (false == deserialize_int(reader, ts)) { return IRErrorCode_Incomplete_IR; } } else { if (cProtocol::Payload::TimestampDeltaByte == encoded_tag) { int8_t ts_delta; - if (false == decode_int(reader, ts_delta)) { + if (false == deserialize_int(reader, ts_delta)) { return IRErrorCode_Incomplete_IR; } ts = ts_delta; } else if (cProtocol::Payload::TimestampDeltaShort == encoded_tag) { int16_t ts_delta; - if (false == decode_int(reader, ts_delta)) { + if (false == deserialize_int(reader, ts_delta)) { return IRErrorCode_Incomplete_IR; } ts = ts_delta; } else if (cProtocol::Payload::TimestampDeltaInt == encoded_tag) { int32_t ts_delta; - if (false == decode_int(reader, ts_delta)) { + if (false == deserialize_int(reader, ts_delta)) { return IRErrorCode_Incomplete_IR; } ts = ts_delta; } else if (cProtocol::Payload::TimestampDeltaLong == encoded_tag) { int64_t ts_delta; - if (false == decode_int(reader, ts_delta)) { + if (false == deserialize_int(reader, ts_delta)) { return IRErrorCode_Incomplete_IR; } ts = ts_delta; @@ -260,15 +263,18 @@ parse_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_m } template -static IRErrorCode -generic_decode_next_message(ReaderInterface& reader, string& message, epoch_time_ms_t& timestamp) { +static IRErrorCode generic_deserialize_log_event( + ReaderInterface& reader, + string& message, + epoch_time_ms_t& timestamp +) { message.clear(); vector encoded_vars; vector dict_vars; string logtype; if (auto error_code - = deserialize_ir_message(reader, logtype, encoded_vars, dict_vars, timestamp); + = deserialize_log_event(reader, logtype, encoded_vars, dict_vars, timestamp); IRErrorCode_Success != error_code) { return error_code; @@ -303,8 +309,11 @@ generic_decode_next_message(ReaderInterface& reader, string& message, epoch_time return IRErrorCode_Success; } -static IRErrorCode -read_metadata_info(ReaderInterface& reader, encoded_tag_t& metadata_type, uint16_t& metadata_size) { +static IRErrorCode deserialize_metadata( + ReaderInterface& reader, + encoded_tag_t& metadata_type, + uint16_t& metadata_size +) { if (ErrorCode_Success != reader.try_read_numeric_value(metadata_type)) { return IRErrorCode_Incomplete_IR; } @@ -317,14 +326,14 @@ read_metadata_info(ReaderInterface& reader, encoded_tag_t& metadata_type, uint16 switch (encoded_tag) { case cProtocol::Metadata::LengthUByte: uint8_t ubyte_res; - if (false == decode_int(reader, ubyte_res)) { + if (false == deserialize_int(reader, ubyte_res)) { return IRErrorCode_Incomplete_IR; } metadata_size = ubyte_res; break; case cProtocol::Metadata::LengthUShort: uint16_t ushort_res; - if (false == decode_int(reader, ushort_res)) { + if (false == deserialize_int(reader, ushort_res)) { return IRErrorCode_Incomplete_IR; } metadata_size = ushort_res; @@ -336,7 +345,7 @@ read_metadata_info(ReaderInterface& reader, encoded_tag_t& metadata_type, uint16 } template -auto deserialize_ir_message( +auto deserialize_log_event( ReaderInterface& reader, string& logtype, vector& encoded_vars, @@ -357,12 +366,12 @@ auto deserialize_ir_message( while (is_variable_tag(encoded_tag, is_encoded_var)) { if (is_encoded_var) { encoded_variable_t encoded_variable; - if (false == decode_int(reader, encoded_variable)) { + if (false == deserialize_int(reader, encoded_variable)) { return IRErrorCode_Incomplete_IR; } encoded_vars.push_back(encoded_variable); } else { - if (auto error_code = parse_dictionary_var(reader, encoded_tag, var_str); + if (auto error_code = deserialize_dict_var(reader, encoded_tag, var_str); IRErrorCode_Success != error_code) { return error_code; @@ -375,7 +384,7 @@ auto deserialize_ir_message( } // Handle logtype - if (auto error_code = parse_logtype(reader, encoded_tag, logtype); + if (auto error_code = deserialize_logtype(reader, encoded_tag, logtype); IRErrorCode_Success != error_code) { return error_code; @@ -386,8 +395,11 @@ auto deserialize_ir_message( if (ErrorCode_Success != reader.try_read_numeric_value(encoded_tag)) { return IRErrorCode_Incomplete_IR; } - if (auto error_code - = parse_timestamp(reader, encoded_tag, timestamp_or_timestamp_delta); + if (auto error_code = deserialize_timestamp( + reader, + encoded_tag, + timestamp_or_timestamp_delta + ); IRErrorCode_Success != error_code) { return error_code; @@ -417,13 +429,13 @@ IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encod return IRErrorCode_Success; } -IRErrorCode decode_preamble( +IRErrorCode deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, size_t& metadata_pos, uint16_t& metadata_size ) { - if (auto error_code = read_metadata_info(reader, metadata_type, metadata_size); + if (auto error_code = deserialize_metadata(reader, metadata_type, metadata_size); error_code != IRErrorCode_Success) { return error_code; @@ -435,13 +447,13 @@ IRErrorCode decode_preamble( return IRErrorCode_Success; } -IRErrorCode decode_preamble( +IRErrorCode deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, std::vector& metadata ) { uint16_t metadata_size{0}; - if (auto error_code = read_metadata_info(reader, metadata_type, metadata_size); + if (auto error_code = deserialize_metadata(reader, metadata_type, metadata_size); error_code != IRErrorCode_Success) { return error_code; @@ -488,12 +500,12 @@ IRProtocolErrorCode validate_protocol_version(std::string_view protocol_version) } namespace four_byte_encoding { - IRErrorCode decode_next_message( + IRErrorCode deserialize_log_event( ReaderInterface& reader, string& message, epoch_time_ms_t& timestamp_delta ) { - return generic_decode_next_message( + return generic_deserialize_log_event( reader, message, timestamp_delta @@ -503,8 +515,8 @@ namespace four_byte_encoding { namespace eight_byte_encoding { IRErrorCode - decode_next_message(ReaderInterface& reader, string& message, epoch_time_ms_t& timestamp) { - return generic_decode_next_message( + deserialize_log_event(ReaderInterface& reader, string& message, epoch_time_ms_t& timestamp) { + return generic_deserialize_log_event( reader, message, timestamp @@ -513,7 +525,7 @@ namespace eight_byte_encoding { } // namespace eight_byte_encoding // Explicitly declare specializations -template auto deserialize_ir_message( +template auto deserialize_log_event( ReaderInterface& reader, string& logtype, vector& encoded_vars, @@ -521,7 +533,7 @@ template auto deserialize_ir_message( epoch_time_ms_t& timestamp_or_timestamp_delta ) -> IRErrorCode; -template auto deserialize_ir_message( +template auto deserialize_log_event( ReaderInterface& reader, string& logtype, vector& encoded_vars, diff --git a/components/core/src/ffi/ir_stream/decoding_methods.hpp b/components/core/src/ffi/ir_stream/decoding_methods.hpp index 0932ada5c..1a2d8c738 100644 --- a/components/core/src/ffi/ir_stream/decoding_methods.hpp +++ b/components/core/src/ffi/ir_stream/decoding_methods.hpp @@ -45,7 +45,7 @@ class DecodingException : public TraceableException { }; /** - * Decodes the encoding type for the encoded IR stream + * Deserializes the IR stream's encoding type * @param reader * @param is_four_bytes_encoding Returns the encoding type * @return ErrorCode_Success on success @@ -55,7 +55,7 @@ class DecodingException : public TraceableException { IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encoding); /** - * Deserializes an IR message from the given stream + * Deserializes a log event from the given stream * @tparam encoded_variable_t * @param reader * @param logtype Returns the logtype @@ -69,7 +69,7 @@ IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encod * @return IRErrorCode_Eof on reaching the end of the stream */ template -auto deserialize_ir_message( +auto deserialize_log_event( ReaderInterface& reader, std::string& logtype, std::vector& encoded_vars, @@ -117,16 +117,16 @@ void generic_decode_message( ); /** - * Decodes the preamble for an IR stream. + * Deserializes the preamble for an IR stream. * @param reader - * @param metadata_type Returns the type of the metadata found in the IR + * @param metadata_type Returns the type of the metadata deserialized from the IR * @param metadata_pos Returns the starting position of the metadata in reader - * @param metadata_size Returns the size of the metadata written in the IR + * @param metadata_size Returns the size of the metadata deserialized from the IR * @return IRErrorCode_Success on success * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize */ -IRErrorCode decode_preamble( +IRErrorCode deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, size_t& metadata_pos, @@ -134,15 +134,15 @@ IRErrorCode decode_preamble( ); /** - * Decodes the preamble for an IR stream. + * Deserializes the preamble for an IR stream. * @param reader - * @param metadata_type Returns the type of the metadata found in the IR + * @param metadata_type Returns the type of the metadata deserialized from the IR * @param metadata Returns the metadata in the given vector * @return IRErrorCode_Success on success * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize */ -IRErrorCode decode_preamble( +IRErrorCode deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, std::vector& metadata @@ -163,33 +163,36 @@ IRProtocolErrorCode validate_protocol_version(std::string_view protocol_version) namespace eight_byte_encoding { /** - * Decodes the next message for the eight-byte encoding IR stream. + * Deserializes the next log event from an eight-byte encoding IR stream. * @param reader - * @param message Returns the decoded message - * @param timestamp Returns the decoded timestamp + * @param message Returns the deserialized message + * @param timestamp Returns the deserialized timestamp * @return ErrorCode_Success on success * @return ErrorCode_Corrupted_IR if reader contains invalid IR - * @return ErrorCode_Decode_Error if the encoded message cannot be properly decoded - * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return ErrorCode_Decode_Error if the log event cannot be properly deserialized + * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize * @return ErrorCode_End_of_IR if the IR ends */ - IRErrorCode - decode_next_message(ReaderInterface& reader, std::string& message, epoch_time_ms_t& timestamp); + IRErrorCode deserialize_log_event( + ReaderInterface& reader, + std::string& message, + epoch_time_ms_t& timestamp + ); } // namespace eight_byte_encoding namespace four_byte_encoding { /** - * Decodes the next message for the four-byte encoding IR stream. + * Deserializes the next log event from a four-byte encoding IR stream. * @param reader - * @param message Returns the decoded message - * @param timestamp_delta Returns the decoded timestamp delta + * @param message Returns the deserialized message + * @param timestamp_delta Returns the deserialized timestamp delta * @return ErrorCode_Success on success * @return ErrorCode_Corrupted_IR if reader contains invalid IR - * @return ErrorCode_Decode_Error if the encoded message cannot be properly decoded - * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to decode + * @return ErrorCode_Decode_Error if the log event cannot be properly deserialized + * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize * @return ErrorCode_End_of_IR if the IR ends */ - IRErrorCode decode_next_message( + IRErrorCode deserialize_log_event( ReaderInterface& reader, std::string& message, epoch_time_ms_t& timestamp_delta diff --git a/components/core/src/ffi/ir_stream/encoding_methods.cpp b/components/core/src/ffi/ir_stream/encoding_methods.cpp index 4ef749405..17c0730ac 100644 --- a/components/core/src/ffi/ir_stream/encoding_methods.cpp +++ b/components/core/src/ffi/ir_stream/encoding_methods.cpp @@ -13,29 +13,29 @@ using std::vector; namespace ffi::ir_stream { // Local function prototypes /** - * Encodes an integer into the IR stream + * Serializes the given integer into the IR stream * @tparam integer_t * @param value * @param ir_buf */ template -static void encode_int(integer_t value, vector& ir_buf); +static void serialize_int(integer_t value, vector& ir_buf); /** - * Encodes the given logtype into the IR stream + * Serializes the given logtype into the IR stream * @param logtype * @param ir_buf * @return true on success, false otherwise */ -static bool encode_logtype(string_view logtype, vector& ir_buf); +static bool serialize_logtype(string_view logtype, vector& ir_buf); /** - * Encodes the given metadata into the IR stream + * Serializes the given metadata into the IR stream * @param metadata * @param ir_buf * @return true on success, false otherwise */ -static bool encode_metadata(nlohmann::json& metadata, vector& ir_buf); +static bool serialize_metadata(nlohmann::json& metadata, vector& ir_buf); /** * Adds the basic metadata fields to the given JSON object @@ -69,10 +69,10 @@ class DictionaryVariableHandler { m_ir_buf.push_back(bit_cast(static_cast(length))); } else if (length <= UINT16_MAX) { m_ir_buf.push_back(cProtocol::Payload::VarStrLenUShort); - encode_int(static_cast(length), m_ir_buf); + serialize_int(static_cast(length), m_ir_buf); } else if (length <= INT32_MAX) { m_ir_buf.push_back(cProtocol::Payload::VarStrLenInt); - encode_int(static_cast(length), m_ir_buf); + serialize_int(static_cast(length), m_ir_buf); } else { return false; } @@ -86,7 +86,7 @@ class DictionaryVariableHandler { }; template -static void encode_int(integer_t value, vector& ir_buf) { +static void serialize_int(integer_t value, vector& ir_buf) { integer_t value_big_endian; static_assert(sizeof(integer_t) == 2 || sizeof(integer_t) == 4 || sizeof(integer_t) == 8); if constexpr (sizeof(value) == 2) { @@ -100,17 +100,17 @@ static void encode_int(integer_t value, vector& ir_buf) { ir_buf.insert(ir_buf.end(), data, data + sizeof(value)); } -static bool encode_logtype(string_view logtype, vector& ir_buf) { +static bool serialize_logtype(string_view logtype, vector& ir_buf) { auto length = logtype.length(); if (length <= UINT8_MAX) { ir_buf.push_back(cProtocol::Payload::LogtypeStrLenUByte); ir_buf.push_back(bit_cast(static_cast(length))); } else if (length <= UINT16_MAX) { ir_buf.push_back(cProtocol::Payload::LogtypeStrLenUShort); - encode_int(static_cast(length), ir_buf); + serialize_int(static_cast(length), ir_buf); } else if (length <= INT32_MAX) { ir_buf.push_back(cProtocol::Payload::LogtypeStrLenInt); - encode_int(static_cast(length), ir_buf); + serialize_int(static_cast(length), ir_buf); } else { // Logtype is too long for encoding return false; @@ -119,7 +119,7 @@ static bool encode_logtype(string_view logtype, vector& ir_buf) { return true; } -static bool encode_metadata(nlohmann::json& metadata, vector& ir_buf) { +static bool serialize_metadata(nlohmann::json& metadata, vector& ir_buf) { ir_buf.push_back(cProtocol::Metadata::EncodingJson); auto metadata_serialized @@ -130,7 +130,7 @@ static bool encode_metadata(nlohmann::json& metadata, vector& ir_buf) { ir_buf.push_back(bit_cast(static_cast(metadata_serialized_length))); } else if (metadata_serialized_length <= UINT16_MAX) { ir_buf.push_back(cProtocol::Metadata::LengthUShort); - encode_int(static_cast(metadata_serialized_length), ir_buf); + serialize_int(static_cast(metadata_serialized_length), ir_buf); } else { // Can't encode metadata longer than 64 KiB return false; @@ -155,7 +155,7 @@ static void add_base_metadata_fields( } namespace eight_byte_encoding { - bool encode_preamble( + bool serialize_preamble( string_view timestamp_pattern, string_view timestamp_pattern_syntax, string_view time_zone_id, @@ -175,10 +175,10 @@ namespace eight_byte_encoding { metadata_json ); - return encode_metadata(metadata_json, ir_buf); + return serialize_metadata(metadata_json, ir_buf); } - bool encode_message( + bool serialize_log_event( epoch_time_ms_t timestamp, string_view message, string& logtype, @@ -186,7 +186,7 @@ namespace eight_byte_encoding { ) { auto encoded_var_handler = [&ir_buf](eight_byte_encoded_variable_t encoded_var) { ir_buf.push_back(cProtocol::Payload::VarEightByteEncoding); - encode_int(encoded_var, ir_buf); + serialize_int(encoded_var, ir_buf); }; if (false @@ -201,20 +201,20 @@ namespace eight_byte_encoding { return false; } - if (false == encode_logtype(logtype, ir_buf)) { + if (false == serialize_logtype(logtype, ir_buf)) { return false; } // Encode timestamp ir_buf.push_back(cProtocol::Payload::TimestampVal); - encode_int(timestamp, ir_buf); + serialize_int(timestamp, ir_buf); return true; } } // namespace eight_byte_encoding namespace four_byte_encoding { - bool encode_preamble( + bool serialize_preamble( string_view timestamp_pattern, string_view timestamp_pattern_syntax, string_view time_zone_id, @@ -237,30 +237,30 @@ namespace four_byte_encoding { metadata_json[cProtocol::Metadata::ReferenceTimestampKey] = std::to_string(reference_timestamp); - return encode_metadata(metadata_json, ir_buf); + return serialize_metadata(metadata_json, ir_buf); } - bool encode_message( + bool serialize_log_event( epoch_time_ms_t timestamp_delta, string_view message, string& logtype, vector& ir_buf ) { - if (false == encode_message(message, logtype, ir_buf)) { + if (false == serialize_message(message, logtype, ir_buf)) { return false; } - if (false == encode_timestamp(timestamp_delta, ir_buf)) { + if (false == serialize_timestamp(timestamp_delta, ir_buf)) { return false; } return true; } - bool encode_message(string_view message, string& logtype, vector& ir_buf) { + bool serialize_message(string_view message, string& logtype, vector& ir_buf) { auto encoded_var_handler = [&ir_buf](four_byte_encoded_variable_t encoded_var) { ir_buf.push_back(cProtocol::Payload::VarFourByteEncoding); - encode_int(encoded_var, ir_buf); + serialize_int(encoded_var, ir_buf); }; if (false @@ -275,26 +275,26 @@ namespace four_byte_encoding { return false; } - if (false == encode_logtype(logtype, ir_buf)) { + if (false == serialize_logtype(logtype, ir_buf)) { return false; } return true; } - bool encode_timestamp(epoch_time_ms_t timestamp_delta, std::vector& ir_buf) { + bool serialize_timestamp(epoch_time_ms_t timestamp_delta, std::vector& ir_buf) { if (INT8_MIN <= timestamp_delta && timestamp_delta <= INT8_MAX) { ir_buf.push_back(cProtocol::Payload::TimestampDeltaByte); ir_buf.push_back(static_cast(timestamp_delta)); } else if (INT16_MIN <= timestamp_delta && timestamp_delta <= INT16_MAX) { ir_buf.push_back(cProtocol::Payload::TimestampDeltaShort); - encode_int(static_cast(timestamp_delta), ir_buf); + serialize_int(static_cast(timestamp_delta), ir_buf); } else if (INT32_MIN <= timestamp_delta && timestamp_delta <= INT32_MAX) { ir_buf.push_back(cProtocol::Payload::TimestampDeltaInt); - encode_int(static_cast(timestamp_delta), ir_buf); + serialize_int(static_cast(timestamp_delta), ir_buf); } else if (INT64_MIN <= timestamp_delta && timestamp_delta <= INT64_MAX) { ir_buf.push_back(cProtocol::Payload::TimestampDeltaLong); - encode_int(static_cast(timestamp_delta), ir_buf); + serialize_int(static_cast(timestamp_delta), ir_buf); } else { // Delta exceeds maximum representable by a 64-bit int return false; diff --git a/components/core/src/ffi/ir_stream/encoding_methods.hpp b/components/core/src/ffi/ir_stream/encoding_methods.hpp index 8a677eec0..c1437adf6 100644 --- a/components/core/src/ffi/ir_stream/encoding_methods.hpp +++ b/components/core/src/ffi/ir_stream/encoding_methods.hpp @@ -9,14 +9,14 @@ namespace ffi::ir_stream { namespace eight_byte_encoding { /** - * Encodes the preamble for the eight-byte encoding IR stream + * Serializes the preamble for the eight-byte encoding IR stream * @param timestamp_pattern * @param timestamp_pattern_syntax * @param time_zone_id * @param ir_buf * @return true on success, false otherwise */ - bool encode_preamble( + bool serialize_preamble( std::string_view timestamp_pattern, std::string_view timestamp_pattern_syntax, std::string_view time_zone_id, @@ -24,14 +24,14 @@ namespace eight_byte_encoding { ); /** - * Encodes the given message into the eight-byte encoding IR stream + * Serializes the given log event into the eight-byte encoding IR stream * @param timestamp * @param message * @param logtype * @param ir_buf * @return true on success, false otherwise */ - bool encode_message( + bool serialize_log_event( epoch_time_ms_t timestamp, std::string_view message, std::string& logtype, @@ -41,7 +41,7 @@ namespace eight_byte_encoding { namespace four_byte_encoding { /** - * Encodes the preamble for the four-byte encoding IR stream + * Serializes the preamble for the four-byte encoding IR stream * @param timestamp_pattern * @param timestamp_pattern_syntax * @param time_zone_id @@ -49,7 +49,7 @@ namespace four_byte_encoding { * @param ir_buf * @return true on success, false otherwise */ - bool encode_preamble( + bool serialize_preamble( std::string_view timestamp_pattern, std::string_view timestamp_pattern_syntax, std::string_view time_zone_id, @@ -58,14 +58,14 @@ namespace four_byte_encoding { ); /** - * Encodes the given message into the four-byte encoding IR stream + * Serializes the given log event into the four-byte encoding IR stream * @param timestamp_delta * @param message * @param logtype * @param ir_buf * @return true on success, false otherwise */ - bool encode_message( + bool serialize_log_event( epoch_time_ms_t timestamp_delta, std::string_view message, std::string& logtype, @@ -73,7 +73,7 @@ namespace four_byte_encoding { ); /** - * Encodes the given message into the four-byte encoding IR stream without encoding timestamp + * Serializes the given message into the four-byte encoding IR stream * delta * @param message * @param logtype @@ -81,15 +81,15 @@ namespace four_byte_encoding { * @return true on success, false otherwise */ bool - encode_message(std::string_view message, std::string& logtype, std::vector& ir_buf); + serialize_message(std::string_view message, std::string& logtype, std::vector& ir_buf); /** - * Encodes the given timestamp delta into the four-byte encoding IR stream + * Serializes the given timestamp delta into the four-byte encoding IR stream * @param timestamp_delta * @param ir_buf * @return true on success, false otherwise */ - bool encode_timestamp(epoch_time_ms_t timestamp_delta, std::vector& ir_buf); + bool serialize_timestamp(epoch_time_ms_t timestamp_delta, std::vector& ir_buf); } // namespace four_byte_encoding } // namespace ffi::ir_stream diff --git a/components/core/src/ir/LogEventDeserializer.cpp b/components/core/src/ir/LogEventDeserializer.cpp index d0ae9aa68..262d64b7c 100644 --- a/components/core/src/ir/LogEventDeserializer.cpp +++ b/components/core/src/ir/LogEventDeserializer.cpp @@ -12,7 +12,7 @@ auto LogEventDeserializer::create(ReaderInterface& reader) -> BOOST_OUTCOME_V2_NAMESPACE::std_result> { ffi::ir_stream::encoded_tag_t metadata_type{0}; std::vector metadata; - auto ir_error_code = ffi::ir_stream::decode_preamble(reader, metadata_type, metadata); + auto ir_error_code = ffi::ir_stream::deserialize_preamble(reader, metadata_type, metadata); if (ffi::ir_stream::IRErrorCode_Success != ir_error_code) { switch (ir_error_code) { case ffi::ir_stream::IRErrorCode_Incomplete_IR: @@ -71,7 +71,7 @@ auto LogEventDeserializer::deserialize_log_event() std::vector dict_vars; std::vector encoded_vars; - auto ir_error_code = ffi::ir_stream::deserialize_ir_message( + auto ir_error_code = ffi::ir_stream::deserialize_log_event( m_reader, logtype, encoded_vars, diff --git a/components/core/tests/test-ir_encoding_methods.cpp b/components/core/tests/test-ir_encoding_methods.cpp index d878ddda4..4bef71af7 100644 --- a/components/core/tests/test-ir_encoding_methods.cpp +++ b/components/core/tests/test-ir_encoding_methods.cpp @@ -20,7 +20,7 @@ using ffi::four_byte_encoded_variable_t; using ffi::ir_stream::cProtocol::EightByteEncodingMagicNumber; using ffi::ir_stream::cProtocol::FourByteEncodingMagicNumber; using ffi::ir_stream::cProtocol::MagicNumberLength; -using ffi::ir_stream::decode_preamble; +using ffi::ir_stream::deserialize_preamble; using ffi::ir_stream::encoded_tag_t; using ffi::ir_stream::get_encoding_type; using ffi::ir_stream::IRErrorCode; @@ -49,7 +49,7 @@ template epoch_time_ms_t get_next_timestamp_for_test(); /** - * Helper function that encodes a preamble of encoding type = encoded_variable_t and writes into + * Helper function that serializes a preamble of encoding type = encoded_variable_t and writes into * ir_buf * @tparam encoded_variable_t Type of the encoded variable * @param timestamp_pattern @@ -57,10 +57,10 @@ epoch_time_ms_t get_next_timestamp_for_test(); * @param time_zone_id * @param reference_timestamp Only used when encoded_variable_t == four_byte_encoded_variable_t * @param ir_buf - * @return True if preamble is encoded without error, otherwise false + * @return True if the preamble is serialized without error, otherwise false */ template -bool encode_preamble( +bool serialize_preamble( string_view timestamp_pattern, string_view timestamp_pattern_syntax, string_view time_zone_id, @@ -69,17 +69,17 @@ bool encode_preamble( ); /** - * Helper function that encodes a message of encoding type = encoded_variable_t and writes into + * Helper function that serializes a log event of encoding type = encoded_variable_t and writes into * ir_buf * @tparam encoded_variable_t Type of the encoded variable * @param timestamp * @param message * @param logtype * @param ir_buf - * @return True if message is encoded without error, otherwise false + * @return True if the log event is serialized without error, otherwise false */ template -bool encode_message( +bool serialize_message( epoch_time_ms_t timestamp, string_view message, string& logtype, @@ -87,19 +87,21 @@ bool encode_message( ); /** - * Helper function that decodes a message of encoding type = encoded_variable_t from the ir_buf + * Helper function that deserializes a log event of encoding type = encoded_variable_t from the + * ir_buf * @tparam encoded_variable_t Type of the encoded variable * @param reader * @param message * @param decoded_ts Returns the decoded timestamp * @return IRErrorCode_Success on success - * @return Same as the ffi::ir_stream::eight_byte_encoding::decode_next_message when + * @return Same as the ffi::ir_stream::eight_byte_encoding::deserialize_log_event when * encoded_variable_t == eight_byte_encoded_variable_t - * @return Same as the ffi::ir_stream::four_byte_encoding::decode_next_message when + * @return Same as the ffi::ir_stream::four_byte_encoding::deserialize_log_event when * encoded_variable_t == four_byte_encoded_variable_t */ template -IRErrorCode decode_next_message(BufferReader& reader, string& message, epoch_time_ms_t& decoded_ts); +IRErrorCode +deserialize_log_event(BufferReader& reader, string& message, epoch_time_ms_t& decoded_ts); /** * Struct to hold the timestamp info from the IR stream's metadata @@ -156,7 +158,7 @@ epoch_time_ms_t get_next_timestamp_for_test() { // A helper function to generalize the testing caller interface. // The reference_timestamp is only used by four bytes encoding template -bool encode_preamble( +bool serialize_preamble( string_view timestamp_pattern, string_view timestamp_pattern_syntax, string_view time_zone_id, @@ -169,14 +171,14 @@ bool encode_preamble( ); if constexpr (is_same_v) { - return ffi::ir_stream::eight_byte_encoding::encode_preamble( + return ffi::ir_stream::eight_byte_encoding::serialize_preamble( timestamp_pattern, timestamp_pattern_syntax, time_zone_id, ir_buf ); } else { - return ffi::ir_stream::four_byte_encoding::encode_preamble( + return ffi::ir_stream::four_byte_encoding::serialize_preamble( timestamp_pattern, timestamp_pattern_syntax, time_zone_id, @@ -199,14 +201,14 @@ bool encode_message( ); if constexpr (is_same_v) { - return ffi::ir_stream::eight_byte_encoding::encode_message( + return ffi::ir_stream::eight_byte_encoding::serialize_log_event( timestamp, message, logtype, ir_buf ); } else { - return ffi::ir_stream::four_byte_encoding::encode_message( + return ffi::ir_stream::four_byte_encoding::serialize_log_event( timestamp, message, logtype, @@ -217,20 +219,24 @@ bool encode_message( template IRErrorCode -decode_next_message(BufferReader& reader, string& message, epoch_time_ms_t& decoded_ts) { +deserialize_log_event(BufferReader& reader, string& message, epoch_time_ms_t& decoded_ts) { static_assert( (is_same_v) || (is_same_v) ); if constexpr (is_same_v) { - return ffi::ir_stream::eight_byte_encoding::decode_next_message( + return ffi::ir_stream::eight_byte_encoding::deserialize_log_event( reader, message, decoded_ts ); } else { - return ffi::ir_stream::four_byte_encoding::decode_next_message(reader, message, decoded_ts); + return ffi::ir_stream::four_byte_encoding::deserialize_log_event( + reader, + message, + decoded_ts + ); } } @@ -299,8 +305,8 @@ TEST_CASE("get_encoding_type", "[ffi][get_encoding_type]") { } TEMPLATE_TEST_CASE( - "decode_preamble", - "[ffi][decode_preamble]", + "deserialize_preamble", + "[ffi][deserialize_preamble]", four_byte_encoded_variable_t, eight_byte_encoded_variable_t ) { @@ -309,7 +315,7 @@ TEMPLATE_TEST_CASE( constexpr char timestamp_pattern_syntax[] = "yyyy-MM-dd HH:mm:ss"; constexpr char time_zone_id[] = "Asia/Tokyo"; epoch_time_ms_t const reference_ts = get_current_ts(); - REQUIRE(encode_preamble( + REQUIRE(serialize_preamble( timestamp_pattern, timestamp_pattern_syntax, time_zone_id, @@ -331,7 +337,7 @@ TEMPLATE_TEST_CASE( encoded_tag_t metadata_type{0}; size_t metadata_pos{0}; uint16_t metadata_size{0}; - REQUIRE(decode_preamble(ir_buffer, metadata_type, metadata_pos, metadata_size) + REQUIRE(deserialize_preamble(ir_buffer, metadata_type, metadata_pos, metadata_size) == IRErrorCode::IRErrorCode_Success); REQUIRE(encoded_preamble_end_pos == ir_buffer.get_pos()); @@ -359,7 +365,7 @@ TEMPLATE_TEST_CASE( // Test if preamble can be decoded by the string copy method std::vector json_metadata_vec; ir_buffer.seek_from_begin(MagicNumberLength); - REQUIRE(decode_preamble(ir_buffer, metadata_type, json_metadata_vec) + REQUIRE(deserialize_preamble(ir_buffer, metadata_type, json_metadata_vec) == IRErrorCode::IRErrorCode_Success); string_view json_metadata_copied{ size_checked_pointer_cast(json_metadata_vec.data()), @@ -375,7 +381,12 @@ TEMPLATE_TEST_CASE( ir_buf.size() }; incomplete_preamble_buffer.seek_from_begin(MagicNumberLength); - REQUIRE(decode_preamble(incomplete_preamble_buffer, metadata_type, metadata_pos, metadata_size) + REQUIRE(deserialize_preamble( + incomplete_preamble_buffer, + metadata_type, + metadata_pos, + metadata_size + ) == IRErrorCode::IRErrorCode_Incomplete_IR); // Test if corrupted IR can be detected @@ -384,13 +395,18 @@ TEMPLATE_TEST_CASE( size_checked_pointer_cast(ir_buf.data()), ir_buf.size() }; - REQUIRE(decode_preamble(corrupted_preamble_buffer, metadata_type, metadata_pos, metadata_size) + REQUIRE(deserialize_preamble( + corrupted_preamble_buffer, + metadata_type, + metadata_pos, + metadata_size + ) == IRErrorCode::IRErrorCode_Corrupted_IR); } TEMPLATE_TEST_CASE( "decode_next_message_general", - "[ffi][decode_next_message]", + "[ffi][deserialize_log_event]", four_byte_encoded_variable_t, eight_byte_encoded_variable_t ) { @@ -411,7 +427,7 @@ TEMPLATE_TEST_CASE( // Test if message can be decoded properly REQUIRE(IRErrorCode::IRErrorCode_Success - == decode_next_message(ir_buffer, decoded_message, timestamp)); + == deserialize_log_event(ir_buffer, decoded_message, timestamp)); REQUIRE(message == decoded_message); REQUIRE(timestamp == reference_timestamp); REQUIRE(ir_buffer.get_pos() == encoded_message_end_pos); @@ -419,7 +435,7 @@ TEMPLATE_TEST_CASE( // Test corrupted IR ir_buffer.seek_from_begin(encoded_message_start_pos + 1); REQUIRE(IRErrorCode::IRErrorCode_Corrupted_IR - == decode_next_message(ir_buffer, message, timestamp)); + == deserialize_log_event(ir_buffer, message, timestamp)); // Test incomplete IR ir_buf.resize(encoded_message_end_pos - 4); @@ -428,13 +444,13 @@ TEMPLATE_TEST_CASE( ir_buf.size() }; REQUIRE(IRErrorCode::IRErrorCode_Incomplete_IR - == decode_next_message(incomplete_preamble_buffer, message, timestamp)); + == deserialize_log_event(incomplete_preamble_buffer, message, timestamp)); } // NOTE: This test only tests eight_byte_encoded_variable_t because we trigger // IRErrorCode_Decode_Error by manually modifying the logtype within the IR, and this is easier for // the eight_byte_encoded_variable_t case. -TEST_CASE("message_decode_error", "[ffi][decode_next_message]") { +TEST_CASE("message_decode_error", "[ffi][deserialize_log_event]") { vector ir_buf; string logtype; @@ -464,7 +480,7 @@ TEST_CASE("message_decode_error", "[ffi][decode_next_message]") { ir_with_extra_escape.size() }; REQUIRE(IRErrorCode::IRErrorCode_Decode_Error - == decode_next_message( + == deserialize_log_event( ir_with_extra_escape_buffer, decoded_message, timestamp @@ -479,14 +495,14 @@ TEST_CASE("message_decode_error", "[ffi][decode_next_message]") { ir_with_extra_placeholder.size() }; REQUIRE(IRErrorCode::IRErrorCode_Decode_Error - == decode_next_message( + == deserialize_log_event( ir_with_extra_placeholder_buffer, decoded_message, timestamp )); } -TEST_CASE("decode_next_message_four_byte_timestamp_delta", "[ffi][decode_next_message]") { +TEST_CASE("decode_next_message_four_byte_timestamp_delta", "[ffi][deserialize_log_event]") { string const message = "Static <\text>, dictVar1, 123, 456345232.7234223, " "dictVar2, 987, 654.3, end of static text"; auto ts_delta = GENERATE( @@ -514,7 +530,7 @@ TEST_CASE("decode_next_message_four_byte_timestamp_delta", "[ffi][decode_next_me string decoded_message; epoch_time_ms_t decoded_delta_ts{}; REQUIRE(IRErrorCode::IRErrorCode_Success - == decode_next_message( + == deserialize_log_event( ir_buffer, decoded_message, decoded_delta_ts @@ -536,7 +552,7 @@ TEST_CASE("validate_protocol_version", "[ffi][validate_version_protocol]") { TEMPLATE_TEST_CASE( "decode_ir_complete", - "[ffi][decode_next_message]", + "[ffi][deserialize_log_event]", four_byte_encoded_variable_t, eight_byte_encoded_variable_t ) { @@ -547,7 +563,7 @@ TEMPLATE_TEST_CASE( constexpr char timestamp_pattern[] = "%Y-%m-%d %H:%M:%S,%3"; constexpr char timestamp_pattern_syntax[] = "yyyy-MM-dd HH:mm:ss"; constexpr char time_zone_id[] = "Asia/Tokyo"; - REQUIRE(encode_preamble( + REQUIRE(serialize_preamble( timestamp_pattern, timestamp_pattern_syntax, time_zone_id, @@ -591,7 +607,7 @@ TEMPLATE_TEST_CASE( encoded_tag_t metadata_type; size_t metadata_pos; uint16_t metadata_size; - REQUIRE(decode_preamble(complete_ir_buffer, metadata_type, metadata_pos, metadata_size) + REQUIRE(deserialize_preamble(complete_ir_buffer, metadata_type, metadata_pos, metadata_size) == IRErrorCode::IRErrorCode_Success); REQUIRE(encoded_preamble_end_pos == complete_ir_buffer.get_pos()); @@ -610,7 +626,7 @@ TEMPLATE_TEST_CASE( epoch_time_ms_t timestamp; for (size_t ix = 0; ix < reference_messages.size(); ix++) { REQUIRE(IRErrorCode::IRErrorCode_Success - == decode_next_message(complete_ir_buffer, decoded_message, timestamp)); + == deserialize_log_event(complete_ir_buffer, decoded_message, timestamp)); REQUIRE(decoded_message == reference_messages[ix]); REQUIRE(timestamp == reference_timestamps[ix]); }