Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi: Extract some serialization methods for use in other files. #444

Merged
merged 4 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ set(SOURCE_FILES_unitTest
src/clp/ffi/ir_stream/decoding_methods.inc
src/clp/ffi/ir_stream/encoding_methods.cpp
src/clp/ffi/ir_stream/encoding_methods.hpp
src/clp/ffi/ir_stream/utils.cpp
src/clp/ffi/ir_stream/utils.hpp
src/clp/ffi/ir_stream/utils.inc
src/clp/ffi/ir_stream/protocol_constants.hpp
src/clp/ffi/SchemaTree.cpp
src/clp/ffi/SchemaTree.hpp
Expand Down
3 changes: 3 additions & 0 deletions components/core/src/clp/clp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ set(
../ffi/ir_stream/decoding_methods.inc
../ffi/ir_stream/encoding_methods.cpp
../ffi/ir_stream/encoding_methods.hpp
../ffi/ir_stream/utils.cpp
../ffi/ir_stream/utils.hpp
../ffi/ir_stream/utils.inc
../FileReader.cpp
../FileReader.hpp
../FileWriter.cpp
Expand Down
55 changes: 1 addition & 54 deletions components/core/src/clp/ffi/ir_stream/encoding_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "../../ir/parsing.hpp"
#include "../../ir/types.hpp"
#include "../../time_types.hpp"
#include "byteswap.hpp"
#include "protocol_constants.hpp"
#include "utils.hpp"

using clp::ir::eight_byte_encoded_variable_t;
using clp::ir::epoch_time_ms_t;
Expand All @@ -17,15 +17,6 @@ using std::vector;

namespace clp::ffi::ir_stream {
// Local function prototypes
/**
* Serializes the given integer into the IR stream
* @tparam integer_t
* @param value
* @param ir_buf
*/
template <typename integer_t>
static void serialize_int(integer_t value, vector<int8_t>& ir_buf);

/**
* Serializes the given logtype into the IR stream
* @param logtype
Expand All @@ -34,14 +25,6 @@ static void serialize_int(integer_t value, vector<int8_t>& ir_buf);
*/
static bool serialize_logtype(string_view logtype, vector<int8_t>& ir_buf);

/**
* Serializes the given metadata into the IR stream
* @param metadata
* @param ir_buf
* @return true on success, false otherwise
*/
static bool serialize_metadata(nlohmann::json& metadata, vector<int8_t>& ir_buf);

/**
* Adds the basic metadata fields to the given JSON object
* @param timestamp_pattern
Expand Down Expand Up @@ -90,21 +73,6 @@ class DictionaryVariableHandler {
vector<int8_t>& m_ir_buf;
};

template <typename integer_t>
static void serialize_int(integer_t value, vector<int8_t>& 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) {
value_big_endian = bswap_16(value);
} else if constexpr (sizeof(value) == 4) {
value_big_endian = bswap_32(value);
} else if constexpr (sizeof(value) == 8) {
value_big_endian = bswap_64(value);
}
auto data = reinterpret_cast<int8_t*>(&value_big_endian);
ir_buf.insert(ir_buf.end(), data, data + sizeof(value));
}

static bool serialize_logtype(string_view logtype, vector<int8_t>& ir_buf) {
auto length = logtype.length();
if (length <= UINT8_MAX) {
Expand All @@ -124,27 +92,6 @@ static bool serialize_logtype(string_view logtype, vector<int8_t>& ir_buf) {
return true;
}

static bool serialize_metadata(nlohmann::json& metadata, vector<int8_t>& ir_buf) {
ir_buf.push_back(cProtocol::Metadata::EncodingJson);

auto metadata_serialized
= metadata.dump(-1, ' ', false, nlohmann::json::error_handler_t::ignore);
auto metadata_serialized_length = metadata_serialized.length();
if (metadata_serialized_length <= UINT8_MAX) {
ir_buf.push_back(cProtocol::Metadata::LengthUByte);
ir_buf.push_back(bit_cast<int8_t>(static_cast<uint8_t>(metadata_serialized_length)));
} else if (metadata_serialized_length <= UINT16_MAX) {
ir_buf.push_back(cProtocol::Metadata::LengthUShort);
serialize_int(static_cast<uint16_t>(metadata_serialized_length), ir_buf);
} else {
// Can't encode metadata longer than 64 KiB
return false;
}
ir_buf.insert(ir_buf.cend(), metadata_serialized.cbegin(), metadata_serialized.cend());

return true;
}

static void add_base_metadata_fields(
string_view timestamp_pattern,
string_view timestamp_pattern_syntax,
Expand Down
32 changes: 32 additions & 0 deletions components/core/src/clp/ffi/ir_stream/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "utils.hpp"

#include <cstdint>
#include <vector>

#include <json/single_include/nlohmann/json.hpp>

#include "../../type_utils.hpp"
#include "protocol_constants.hpp"

namespace clp::ffi::ir_stream {
auto serialize_metadata(nlohmann::json& metadata, std::vector<int8_t>& ir_buf) -> bool {
ir_buf.push_back(cProtocol::Metadata::EncodingJson);

auto const metadata_serialized
= metadata.dump(-1, ' ', false, nlohmann::json::error_handler_t::ignore);
auto const metadata_serialized_length = metadata_serialized.length();
if (metadata_serialized_length <= UINT8_MAX) {
ir_buf.push_back(cProtocol::Metadata::LengthUByte);
ir_buf.push_back(bit_cast<int8_t>(static_cast<uint8_t>(metadata_serialized_length)));
} else if (metadata_serialized_length <= UINT16_MAX) {
ir_buf.push_back(cProtocol::Metadata::LengthUShort);
serialize_int(static_cast<uint16_t>(metadata_serialized_length), ir_buf);
} else {
// Can't encode metadata longer than 64 KiB
return false;
}
ir_buf.insert(ir_buf.cend(), metadata_serialized.cbegin(), metadata_serialized.cend());

return true;
}
} // namespace clp::ffi::ir_stream
31 changes: 31 additions & 0 deletions components/core/src/clp/ffi/ir_stream/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef CLP_FFI_IR_STREAM_UTILS_HPP
#define CLP_FFI_IR_STREAM_UTILS_HPP

#include <cstdint>
#include <vector>

#include <json/single_include/nlohmann/json.hpp>

namespace clp::ffi::ir_stream {
/**
* Serializes the given metadata into the IR stream.
* @param metadata
* @param ir_buf
* @return Whether the serialization succeeded.
*/
[[nodiscard]] auto
serialize_metadata(nlohmann::json& metadata, std::vector<int8_t>& ir_buf) -> bool;

/**
* Serializes the given integer into the IR stream.
* @tparam integer_t
* @param value
* @param ir_buf
*/
template <typename integer_t>
auto serialize_int(integer_t value, std::vector<int8_t>& ir_buf) -> void;
} // namespace clp::ffi::ir_stream

#include "utils.inc"

#endif
28 changes: 28 additions & 0 deletions components/core/src/clp/ffi/ir_stream/utils.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CLP_FFI_IR_STREAM_UTILS_INC
#define CLP_FFI_IR_STREAM_UTILS_INC

#include <cstdint>
#include <span>
#include <vector>

#include "byteswap.hpp"

namespace clp::ffi::ir_stream {
template <typename integer_t>
auto serialize_int(integer_t value, std::vector<int8_t>& ir_buf) -> void {
integer_t value_big_endian{};
static_assert(sizeof(integer_t) == 2 || sizeof(integer_t) == 4 || sizeof(integer_t) == 8);
if constexpr (sizeof(value) == 2) {
value_big_endian = bswap_16(value);
} else if constexpr (sizeof(value) == 4) {
value_big_endian = bswap_32(value);
} else if constexpr (sizeof(value) == 8) {
value_big_endian = bswap_64(value);
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
std::span<int8_t> const data_view{reinterpret_cast<int8_t*>(&value_big_endian), sizeof(value)};
ir_buf.insert(ir_buf.end(), data_view.begin(), data_view.end());
}
} // namespace clp::ffi::ir_stream

#endif
Loading