Skip to content

Commit

Permalink
Refactor utc deserialization; Add back proper constexpr encoding check.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlion committed Jul 15, 2024
1 parent 085f1e9 commit 746006f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 57 deletions.
4 changes: 2 additions & 2 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 27 additions & 55 deletions cpp/src/ffi_go/ir/deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,9 @@

namespace ffi_go::ir {
using clp::BufferReader;
using clp::ffi::ir_stream::cProtocol::Eof;
using clp::ffi::ir_stream::cProtocol::Payload::UtcOffsetChange;
using clp::ffi::ir_stream::deserialize_preamble;
using clp::ffi::ir_stream::deserialize_tag;
using clp::ffi::ir_stream::encoded_tag_t;
using clp::ffi::ir_stream::get_encoding_type;
using clp::ffi::ir_stream::IRErrorCode;
using clp::ir::eight_byte_encoded_variable_t;
using clp::ir::four_byte_encoded_variable_t;
using clp::UtcOffset;

namespace {
/**
Expand Down Expand Up @@ -64,22 +57,6 @@ template <class encoded_variable_t>
size_t* matching_query
) -> int;

/**
* Deserializes all the UTC offset change packets from the given IR stream.
* @param ir_buf
* @param tag Outputs the tag after deserializing UTC offset changes.
* @param utc_offset Outputs the last successfully deserialized UTC offset.
* updated by the last deserialized UTC offset on success.
* @return IRErrorCode::IRErrorCode_Success on success.
* @return IRErrorCode::IRErrorCode_Incomplete_IR if the reader doesn't contain enough data to
* deserialize.
*/
[[nodiscard]] auto deserialize_all_utc_offset_change_packets(
BufferReader& ir_buf,
encoded_tag_t& tag,
clp::UtcOffset& utc_offset
) -> IRErrorCode;

/**
* Deserializes packets from the given IR buffer until a new log event is fully deserialized.
* @tparam encoded_variable_t
Expand Down Expand Up @@ -215,23 +192,6 @@ auto deserialize_wildcard_match(
}
}

auto deserialize_all_utc_offset_change_packets(
BufferReader& ir_buf,
encoded_tag_t& tag,
clp::UtcOffset& utc_offset
) -> IRErrorCode {
while (UtcOffsetChange == tag) {
if (auto const err{clp::ffi::ir_stream::deserialize_utc_offset_change(ir_buf, utc_offset)})
{
return err;
}
if (auto const err{deserialize_tag(ir_buf, tag)}) {
return err;
}
}
return IRErrorCode::IRErrorCode_Success;
}

template <class encoded_variable_t>
auto deserialize_to_next_log_event(BufferReader& ir_buf, Deserializer* deserializer)
-> IRErrorCode {
Expand All @@ -240,37 +200,44 @@ auto deserialize_to_next_log_event(BufferReader& ir_buf, Deserializer* deseriali
|| std::is_same_v<encoded_variable_t, four_byte_encoded_variable_t>)
);

encoded_tag_t tag{};
epoch_time_ms_t timestamp{};
IRErrorCode err{};
clp::ffi::ir_stream::encoded_tag_t tag{};
std::optional<clp::UtcOffset> utc_offset;

if (err = deserialize_tag(ir_buf, tag); IRErrorCode::IRErrorCode_Success != err) {
return err;
}

if (UtcOffsetChange == tag) {
while (true) {
if (IRErrorCode const err{clp::ffi::ir_stream::deserialize_tag(ir_buf, tag)};
IRErrorCode::IRErrorCode_Success != err)
{
return err;
}
if (clp::ffi::ir_stream::cProtocol::Payload::UtcOffsetChange != tag) {
break;
}
clp::UtcOffset deserialized_utc_offset{0};
if (err = deserialize_all_utc_offset_change_packets(ir_buf, tag, deserialized_utc_offset);
if (IRErrorCode const err{clp::ffi::ir_stream::deserialize_utc_offset_change(
ir_buf,
deserialized_utc_offset
)};
IRErrorCode::IRErrorCode_Success != err)
{
return err;
}
utc_offset.emplace(deserialized_utc_offset);
}

if (Eof == tag) {
if (clp::ffi::ir_stream::cProtocol::Eof == tag) {
return IRErrorCode::IRErrorCode_Eof;
}

IRErrorCode err{};
epoch_time_ms_t timestamp{};
if constexpr (std::is_same_v<encoded_variable_t, eight_byte_encoded_variable_t>) {
err = clp::ffi::ir_stream::eight_byte_encoding::deserialize_log_event(
ir_buf,
tag,
deserializer->m_log_event.m_log_message,
timestamp
);
} else {
} else if constexpr (std::is_same_v<encoded_variable_t, four_byte_encoded_variable_t>) {
epoch_time_ms_t timestamp_delta{};
err = clp::ffi::ir_stream::four_byte_encoding::deserialize_log_event(
ir_buf,
Expand All @@ -279,6 +246,8 @@ auto deserialize_to_next_log_event(BufferReader& ir_buf, Deserializer* deseriali
timestamp_delta
);
timestamp = deserializer->m_timestamp + timestamp_delta;
} else {
static_assert(cAlwaysFalse<encoded_variable_t>, "Invalid/unhandled encoding type");
}

if (IRErrorCode::IRErrorCode_Success != err) {
Expand Down Expand Up @@ -317,16 +286,19 @@ CLP_FFI_GO_METHOD auto ir_deserializer_new_deserializer_with_preamble(
BufferReader ir_buf{static_cast<char const*>(ir_view.m_data), ir_view.m_size};

bool four_byte_encoding{};
if (IRErrorCode const err{get_encoding_type(ir_buf, four_byte_encoding)};
if (IRErrorCode const err{clp::ffi::ir_stream::get_encoding_type(ir_buf, four_byte_encoding)};
IRErrorCode::IRErrorCode_Success != err)
{
return static_cast<int>(err);
}
*ir_encoding = four_byte_encoding ? 1 : 0;

if (IRErrorCode const err{
deserialize_preamble(ir_buf, *metadata_type, *metadata_pos, *metadata_size)
};
if (IRErrorCode const err{clp::ffi::ir_stream::deserialize_preamble(
ir_buf,
*metadata_type,
*metadata_pos,
*metadata_size
)};
IRErrorCode::IRErrorCode_Success != err)
{
return static_cast<int>(err);
Expand Down
Binary file modified lib/libclp_ffi_linux_amd64.a
Binary file not shown.

0 comments on commit 746006f

Please sign in to comment.