Skip to content

Commit

Permalink
ffi: Extract some serialization methods for use in other files. (y-sc…
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 authored Jun 16, 2024
1 parent 22bcd4b commit c9c9548
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 54 deletions.
2 changes: 2 additions & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ 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/protocol_constants.hpp
src/clp/ffi/SchemaTree.cpp
src/clp/ffi/SchemaTree.hpp
Expand Down
2 changes: 2 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,8 @@ 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
../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
47 changes: 47 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,47 @@
#ifndef CLP_FFI_IR_STREAM_UTILS_HPP
#define CLP_FFI_IR_STREAM_UTILS_HPP

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

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

#include "byteswap.hpp"

namespace clp::ffi::ir_stream {
/**
* Serializes the given metadata into the IR stream.
* @param metadata
* @param ir_buf
* @return Whether 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;

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

0 comments on commit c9c9548

Please sign in to comment.