Skip to content

Commit

Permalink
Buffer logtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Jul 8, 2024
1 parent 8d74f00 commit dec9f56
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
24 changes: 17 additions & 7 deletions components/core/src/clp/ffi/ir_stream/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,25 @@ auto serialize_value_null(vector<int8_t>& buf) -> void;
* Serializes a value of string type.
* @tparam encoded_variable_t
* @param val
* @param logtype_buf
* @param buf Outputs the serialized byte sequence.
* @return Whether serialization succeeded.
*/
template <typename encoded_variable_t>
[[nodiscard]] auto serialize_value_string(string_view val, vector<int8_t>& buf) -> bool;
[[nodiscard]] auto
serialize_value_string(string_view val, string& logtype_buf, vector<int8_t>& buf) -> bool;

/**
* Serializes a msgpack array as a JSON string, using clp encoding.
* @tparam encoded_variable_t
* @param val
* @param logtype_buf
* @param buf Outputs the serialized byte sequence.
* @return Whether serialization succeeded.
*/
template <typename encoded_variable_t>
[[nodiscard]] auto serialize_value_array(msgpack::object const& val, vector<int8_t>& buf) -> bool;
[[nodiscard]] auto
serialize_value_array(msgpack::object const& val, string& logtype_buf, vector<int8_t>& buf) -> bool;

auto get_schema_tree_node_type_from_msgpack_val(msgpack::object const& val
) -> optional<SchemaTreeNode::Type> {
Expand Down Expand Up @@ -201,18 +205,21 @@ auto serialize_value_null(vector<int8_t>& buf) -> void {
}

template <typename encoded_variable_t>
auto serialize_value_string(string_view val, vector<int8_t>& buf) -> bool {
auto serialize_value_string(string_view val, string& logtype_buf, vector<int8_t>& buf) -> bool {
if (string_view::npos == val.find(' ')) {
return serialize_string(val, buf);
}
return serialize_clp_string<encoded_variable_t>(val, buf);
logtype_buf.clear();
return serialize_clp_string<encoded_variable_t>(val, logtype_buf, buf);
}

template <typename encoded_variable_t>
auto serialize_value_array(msgpack::object const& val, vector<int8_t>& buf) -> bool {
auto serialize_value_array(msgpack::object const& val, string& logtype_buf, vector<int8_t>& buf)
-> bool {
std::ostringstream oss;
oss << val;
return serialize_clp_string<encoded_variable_t>(oss.str(), buf);
logtype_buf.clear();
return serialize_clp_string<encoded_variable_t>(oss.str(), logtype_buf, buf);
}
} // namespace

Expand Down Expand Up @@ -439,6 +446,7 @@ auto Serializer<encoded_variable_t>::serialize_val(
if (false
== serialize_value_string<encoded_variable_t>(
val.as<string_view>(),
m_logtype_buf,
m_value_group_buf
))
{
Expand All @@ -454,7 +462,9 @@ auto Serializer<encoded_variable_t>::serialize_val(
break;

case SchemaTreeNode::Type::UnstructuredArray:
if (false == serialize_value_array<encoded_variable_t>(val, m_value_group_buf)) {
if (false
== serialize_value_array<encoded_variable_t>(val, m_logtype_buf, m_value_group_buf))
{
return false;
}
break;
Expand Down
2 changes: 2 additions & 0 deletions components/core/src/clp/ffi/ir_stream/Serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

#include <boost-outcome/include/boost/outcome/std_result.hpp>
Expand Down Expand Up @@ -120,6 +121,7 @@ class Serializer {
Buffer m_ir_buf;
SchemaTree m_schema_tree;

std::string m_logtype_buf;
Buffer m_schema_tree_node_buf;
Buffer m_key_group_buf;
Buffer m_value_group_buf;
Expand Down
8 changes: 5 additions & 3 deletions components/core/src/clp/ffi/ir_stream/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ auto serialize_int(integer_t value, std::vector<int8_t>& ir_buf) -> void;
* Serializes a string using clp encoding.
* @tparam encoded_variable_t
* @param str
* @param logtype Outputs the logtype of the serialized clp string.
* @param buf Outputs the serialized byte sequence.
* @return Whether serialization succeeded.
*/
template <typename encoded_variable_t>
[[nodiscard]] auto serialize_clp_string(std::string_view str, std::vector<int8_t>& buf) -> bool;
[[nodiscard]] auto
serialize_clp_string(std::string_view str, std::string& logtype, std::vector<int8_t>& buf) -> bool;

/**
* Serializes a string packet.
Expand All @@ -68,12 +70,12 @@ auto serialize_int(integer_t value, std::vector<int8_t>& ir_buf) -> void {
}

template <typename encoded_variable_t>
[[nodiscard]] auto serialize_clp_string(std::string_view str, std::vector<int8_t>& buf) -> bool {
[[nodiscard]] auto
serialize_clp_string(std::string_view str, std::string& logtype, std::vector<int8_t>& buf) -> bool {
static_assert(
(std::is_same_v<encoded_variable_t, clp::ir::eight_byte_encoded_variable_t>
|| std::is_same_v<encoded_variable_t, clp::ir::four_byte_encoded_variable_t>)
);
std::string logtype;
bool error{};
if constexpr (std::is_same_v<encoded_variable_t, clp::ir::four_byte_encoded_variable_t>) {
buf.push_back(cProtocol::Payload::ValueFourByteEncodingClpStr);
Expand Down

0 comments on commit dec9f56

Please sign in to comment.