Skip to content

Commit

Permalink
build: Add headers specification in CMakeLists.txt and remove spdlog …
Browse files Browse the repository at this point in the history
…in public headers (#46)
  • Loading branch information
sitaowang1998 authored Jan 7, 2025
1 parent dcef9ae commit 74116cb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 48 deletions.
18 changes: 16 additions & 2 deletions src/spider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ set(SPIDER_CORE_HEADERS

add_library(spider_core)
target_sources(spider_core PRIVATE ${SPIDER_CORE_SOURCES})
target_sources(spider_core PUBLIC ${SPIDER_CORE_HEADERS})
target_sources(
spider_core
PUBLIC
FILE_SET spider_core_headers
TYPE HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/..
FILES ${SPIDER_CORE_HEADERS}
)
target_link_libraries(
spider_core
PUBLIC
Expand Down Expand Up @@ -150,7 +157,14 @@ set(SPIDER_CLIENT_SHARED_HEADERS

add_library(spider_client)
target_sources(spider_client PRIVATE ${SPIDER_CLIENT_SHARED_SOURCES})
target_sources(spider_client PUBLIC ${SPIDER_CLIENT_SHARED_HEADERS})
target_sources(
spider_client
PUBLIC
FILE_SET spider_client_shared_headers
TYPE HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/..
FILES ${SPIDER_CLIENT_SHARED_HEADERS}
)
target_link_libraries(
spider_client
PUBLIC
Expand Down
42 changes: 42 additions & 0 deletions src/spider/worker/FunctionManager.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "FunctionManager.hpp"

#include <cstddef>
#include <optional>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

#include <boost/dll/alias.hpp>
#include <spdlog/spdlog.h>

#include "../io/MsgPack.hpp" // IWYU pragma: keep
#include "TaskExecutorMessage.hpp"
Expand Down Expand Up @@ -60,6 +64,44 @@ void create_error_buffer(
packer.pack(message);
}

auto response_get_result_buffers(msgpack::sbuffer const& buffer
) -> std::optional<std::vector<msgpack::sbuffer>> {
// NOLINTBEGIN(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic)
try {
std::vector<msgpack::sbuffer> result_buffers;
msgpack::object_handle const handle = msgpack::unpack(buffer.data(), buffer.size());
msgpack::object const object = handle.get();

if (msgpack::type::ARRAY != object.type || object.via.array.size < 2) {
spdlog::error("Cannot split result into buffers: Wrong type");
return std::nullopt;
}

if (worker::TaskExecutorResponseType::Result
!= object.via.array.ptr[0].as<worker::TaskExecutorResponseType>())
{
spdlog::error(
"Cannot split result into buffers: Wrong response type {}",
static_cast<std::underlying_type_t<worker::TaskExecutorResponseType>>(
object.via.array.ptr[0].as<worker::TaskExecutorResponseType>()
)
);
return std::nullopt;
}

for (size_t i = 1; i < object.via.array.size; ++i) {
msgpack::object const& obj = object.via.array.ptr[i];
result_buffers.emplace_back();
msgpack::pack(result_buffers.back(), obj);
}
return result_buffers;
} catch (msgpack::type_error& e) {
spdlog::error("Cannot split result into buffers: {}", e.what());
return std::nullopt;
}
// NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic)
}

auto FunctionManager::get_function(std::string const& name) const -> Function const* {
if (auto const func_iter = m_function_map.find(name); func_iter != m_function_map.end()) {
return &(func_iter->second);
Expand Down
41 changes: 2 additions & 39 deletions src/spider/worker/FunctionManager.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef SPIDER_WORKER_FUNCTIONMANAGER_HPP
#define SPIDER_WORKER_FUNCTIONMANAGER_HPP

#include <cstddef>
#include <cstdint>
#include <exception>
#include <functional>
Expand All @@ -16,7 +15,6 @@
#include <absl/container/flat_hash_map.h>
#include <boost/uuid/uuid.hpp>
#include <fmt/format.h>
#include <spdlog/spdlog.h>

#include "../client/Data.hpp"
#include "../client/task.hpp"
Expand Down Expand Up @@ -162,43 +160,8 @@ auto response_get_result(msgpack::sbuffer const& buffer) -> std::optional<std::t
// NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic)
}

inline auto response_get_result_buffers(msgpack::sbuffer const& buffer
) -> std::optional<std::vector<msgpack::sbuffer>> {
// NOLINTBEGIN(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic)
try {
std::vector<msgpack::sbuffer> result_buffers;
msgpack::object_handle const handle = msgpack::unpack(buffer.data(), buffer.size());
msgpack::object const object = handle.get();

if (msgpack::type::ARRAY != object.type || object.via.array.size < 2) {
spdlog::error("Cannot split result into buffers: Wrong type");
return std::nullopt;
}

if (worker::TaskExecutorResponseType::Result
!= object.via.array.ptr[0].as<worker::TaskExecutorResponseType>())
{
spdlog::error(
"Cannot split result into buffers: Wrong response type {}",
static_cast<std::underlying_type_t<worker::TaskExecutorResponseType>>(
object.via.array.ptr[0].as<worker::TaskExecutorResponseType>()
)
);
return std::nullopt;
}

for (size_t i = 1; i < object.via.array.size; ++i) {
msgpack::object const& obj = object.via.array.ptr[i];
result_buffers.emplace_back();
msgpack::pack(result_buffers.back(), obj);
}
return result_buffers;
} catch (msgpack::type_error& e) {
spdlog::error("Cannot split result into buffers: {}", e.what());
return std::nullopt;
}
// NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic)
}
auto response_get_result_buffers(msgpack::sbuffer const& buffer
) -> std::optional<std::vector<msgpack::sbuffer>>;

template <TaskIo T>
auto create_result_response(T const& t) -> msgpack::sbuffer {
Expand Down
10 changes: 5 additions & 5 deletions tests/worker/worker-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <random>
#include <stdexcept>

#include "../../src/spider/client/Data.hpp"
#include "../../src/spider/client/Driver.hpp"
#include "../../src/spider/client/Job.hpp"
#include "../../src/spider/client/TaskContext.hpp"
#include "../../src/spider/client/TaskGraph.hpp"
#include <spider/client/Data.hpp>
#include <spider/client/Driver.hpp>
#include <spider/client/Job.hpp>
#include <spider/client/TaskContext.hpp>
#include <spider/client/TaskGraph.hpp>

auto sum_test(spider::TaskContext& /*context*/, int const x, int const y) -> int {
std::cerr << x << " + " << y << " = " << x + y << "\n";
Expand Down
4 changes: 2 additions & 2 deletions tests/worker/worker-test.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef SPIDER_TEST_WORKER_TEST_HPP
#define SPIDER_TEST_WORKER_TEST_HPP

#include "../../src/spider/client/Data.hpp"
#include "../../src/spider/client/TaskContext.hpp"
#include <spider/client/Data.hpp>
#include <spider/client/TaskContext.hpp>

auto sum_test(spider::TaskContext& /*context*/, int x, int y) -> int;

Expand Down

0 comments on commit 74116cb

Please sign in to comment.