From 71360cba2a763f76a7e12e16bfe8a9379bd0fbe6 Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Wed, 6 Feb 2019 14:06:18 +0100 Subject: [PATCH] Extract io serialization to a library Summary: Serialization is only needed in distributed Memgraph Reviewers: mferencevic, msantl Reviewed By: msantl Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D1846 --- src/CMakeLists.txt | 3 +- src/distributed/coordination_rpc_messages.lcp | 1 + src/io/CMakeLists.txt | 18 +++++---- src/io/network/endpoint.cpp | 12 ------ src/io/network/endpoint.hpp | 25 ------------ src/io/network/serialization.hpp | 38 +++++++++++++++++++ 6 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 src/io/network/serialization.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c35ef2da4..241b36b7c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -233,7 +233,8 @@ add_custom_target(generate_capnp DEPENDS generate_lcp_distributed ${generated_ca set(MG_DISTRIBUTED_LIBS stdc++fs Threads::Threads fmt cppitertools antlr_opencypher_parser_lib dl glog gflags capnp kj - mg-utils mg-io mg-integrations-kafka mg-requests mg-communication mg-auth mg-stats) + mg-utils mg-io mg-io-serialization mg-integrations-kafka mg-requests + mg-communication mg-auth mg-stats) # STATIC library used by memgraph executables add_library(mg-distributed STATIC ${mg_distributed_sources}) diff --git a/src/distributed/coordination_rpc_messages.lcp b/src/distributed/coordination_rpc_messages.lcp index 46a024c5d..ee2deb7ac 100644 --- a/src/distributed/coordination_rpc_messages.lcp +++ b/src/distributed/coordination_rpc_messages.lcp @@ -9,6 +9,7 @@ #include "durability/distributed/recovery.hpp" #include "durability/distributed/serialization.hpp" #include "io/network/endpoint.hpp" +#include "io/network/serialization.hpp" cpp<# (lcp:namespace distributed) diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt index 5c3d683f1..060692737 100644 --- a/src/io/CMakeLists.txt +++ b/src/io/CMakeLists.txt @@ -4,13 +4,15 @@ set(io_src_files network/socket.cpp network/utils.cpp) -define_add_capnp(add_capnp io_src_files io_capnp_files) - -add_capnp(network/endpoint.capnp) - -add_custom_target(generate_io_capnp DEPENDS ${io_capnp_files}) - add_library(mg-io STATIC ${io_src_files}) target_link_libraries(mg-io stdc++fs Threads::Threads fmt glog mg-utils) -target_link_libraries(mg-io capnp kj) -add_dependencies(mg-io generate_io_capnp) + +set(io_serialization_src_files) + +define_add_capnp(add_capnp io_serialization_src_files io_capnp_files) +add_capnp(network/endpoint.capnp) +add_custom_target(generate_io_capnp DEPENDS ${io_capnp_files}) + +add_library(mg-io-serialization STATIC ${io_serialization_src_files}) +target_link_libraries(mg-io-serialization mg-io capnp kj) +add_dependencies(mg-io-serialization generate_io_capnp) diff --git a/src/io/network/endpoint.cpp b/src/io/network/endpoint.cpp index 5514c13da..42f3d9647 100644 --- a/src/io/network/endpoint.cpp +++ b/src/io/network/endpoint.cpp @@ -24,18 +24,6 @@ Endpoint::Endpoint(const std::string &address, uint16_t port) CHECK(family_ != 0) << "Not a valid IPv4 or IPv6 address: " << address; } -void Save(const Endpoint &endpoint, capnp::Endpoint::Builder *builder) { - builder->setAddress(endpoint.address()); - builder->setPort(endpoint.port()); - builder->setFamily(endpoint.family()); -} - -void Load(Endpoint *endpoint, const capnp::Endpoint::Reader &reader) { - endpoint->address_ = reader.getAddress(); - endpoint->port_ = reader.getPort(); - endpoint->family_ = reader.getFamily(); -} - bool Endpoint::operator==(const Endpoint &other) const { return address_ == other.address_ && port_ == other.port_ && family_ == other.family_; diff --git a/src/io/network/endpoint.hpp b/src/io/network/endpoint.hpp index decfb9054..9a032e6e7 100644 --- a/src/io/network/endpoint.hpp +++ b/src/io/network/endpoint.hpp @@ -5,11 +5,6 @@ #include #include -// TODO: SLK serialization should be its own thing -#include "communication/rpc/serialization.hpp" -#include "io/network/endpoint.capnp.h" -#include "utils/exceptions.hpp" - namespace io::network { /** @@ -35,24 +30,4 @@ class Endpoint { unsigned char family_{0}; }; -void Save(const Endpoint &endpoint, capnp::Endpoint::Builder *builder); - -void Load(Endpoint *endpoint, const capnp::Endpoint::Reader &reader); - } // namespace io::network - -namespace slk { - -inline void Save(const io::network::Endpoint &endpoint, slk::Builder *builder) { - slk::Save(endpoint.address_, builder); - slk::Save(endpoint.port_, builder); - slk::Save(endpoint.family_, builder); -} - -inline void Load(io::network::Endpoint *endpoint, slk::Reader *reader) { - slk::Load(&endpoint->address_, reader); - slk::Load(&endpoint->port_, reader); - slk::Load(&endpoint->family_, reader); -} - -} // namespace slk diff --git a/src/io/network/serialization.hpp b/src/io/network/serialization.hpp new file mode 100644 index 000000000..43b492175 --- /dev/null +++ b/src/io/network/serialization.hpp @@ -0,0 +1,38 @@ +#pragma once + +// TODO: SLK serialization should be its own thing +#include "communication/rpc/serialization.hpp" +#include "io/network/endpoint.capnp.h" +#include "io/network/endpoint.hpp" + +namespace io::network { + +inline void Save(const Endpoint &endpoint, capnp::Endpoint::Builder *builder) { + builder->setAddress(endpoint.address()); + builder->setPort(endpoint.port()); + builder->setFamily(endpoint.family()); +} + +inline void Load(Endpoint *endpoint, const capnp::Endpoint::Reader &reader) { + endpoint->address_ = reader.getAddress(); + endpoint->port_ = reader.getPort(); + endpoint->family_ = reader.getFamily(); +} + +} // namespace io::network + +namespace slk { + +inline void Save(const io::network::Endpoint &endpoint, slk::Builder *builder) { + slk::Save(endpoint.address_, builder); + slk::Save(endpoint.port_, builder); + slk::Save(endpoint.family_, builder); +} + +inline void Load(io::network::Endpoint *endpoint, slk::Reader *reader) { + slk::Load(&endpoint->address_, reader); + slk::Load(&endpoint->port_, reader); + slk::Load(&endpoint->family_, reader); +} + +} // namespace slk