Skip to content

Commit

Permalink
core: Extract TransactionManager from `clp::ffi::ir_stream::Deseria…
Browse files Browse the repository at this point in the history
…lizer`; Improve class' docs and require nothrow for its handlers. (y-scope#548)

Co-authored-by: kirkrodrigues <[email protected]>
  • Loading branch information
LinZhihao-723 and kirkrodrigues authored Oct 4, 2024
1 parent 9202761 commit 035449a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ set(SOURCE_FILES_unitTest
src/clp/TimestampPattern.cpp
src/clp/TimestampPattern.hpp
src/clp/TraceableException.hpp
src/clp/TransactionManager.hpp
src/clp/type_utils.hpp
src/clp/utf8_utils.cpp
src/clp/utf8_utils.hpp
Expand Down
51 changes: 51 additions & 0 deletions components/core/src/clp/TransactionManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef CLP_TRANSACTIONMANAGER_HPP
#define CLP_TRANSACTIONMANAGER_HPP

#include <type_traits>

namespace clp {
/**
* A class that on destruction, performs different actions depending on whether a transaction
* succeeds or fails. The default state assumes the transaction fails.
* @tparam SuccessHandler A cleanup lambda to call on success.
* @tparam FailureHandler A cleanup lambda to call on failure.
*/
template <typename SuccessHandler, typename FailureHandler>
requires(std::is_nothrow_invocable_v<SuccessHandler> && std::is_nothrow_invocable_v<FailureHandler>)
class TransactionManager {
public:
// Constructor
TransactionManager(SuccessHandler success_handler, FailureHandler failure_handler)
: m_success_handler{success_handler},
m_failure_handler{failure_handler} {}

// Delete copy/move constructor and assignment
TransactionManager(TransactionManager const&) = delete;
TransactionManager(TransactionManager&&) = delete;
auto operator=(TransactionManager const&) -> TransactionManager& = delete;
auto operator=(TransactionManager&&) -> TransactionManager& = delete;

// Destructor
~TransactionManager() {
if (m_success) {
m_success_handler();
} else {
m_failure_handler();
}
}

// Methods
/**
* Marks the transaction as successful.
*/
auto mark_success() -> void { m_success = true; }

private:
// Variables
SuccessHandler m_success_handler;
FailureHandler m_failure_handler;
bool m_success{false};
};
} // namespace clp

#endif // CLP_TRANSACTIONMANAGER_HPP
49 changes: 3 additions & 46 deletions components/core/src/clp/ffi/ir_stream/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string_view>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

Expand All @@ -14,6 +13,7 @@

#include "../../ReaderInterface.hpp"
#include "../../time_types.hpp"
#include "../../TransactionManager.hpp"
#include "../KeyValuePairLogEvent.hpp"
#include "decoding_methods.hpp"
#include "ir_unit_deserialization_methods.hpp"
Expand All @@ -22,49 +22,6 @@

namespace clp::ffi::ir_stream {
namespace {
/**
* Class to perform different actions depending on whether a transaction succeeds or fails. The
* default state assumes the transaction fails.
* @tparam SuccessHandler A cleanup lambda to call on success.
* @tparam FailureHandler A cleanup lambda to call on failure.
*/
template <typename SuccessHandler, typename FailureHandler>
requires(std::is_invocable_v<SuccessHandler> && std::is_invocable_v<FailureHandler>)
class TransactionManager {
public:
// Constructor
TransactionManager(SuccessHandler success_handler, FailureHandler failure_handler)
: m_success_handler{success_handler},
m_failure_handler{failure_handler} {}

// Delete copy/move constructor and assignment
TransactionManager(TransactionManager const&) = delete;
TransactionManager(TransactionManager&&) = delete;
auto operator=(TransactionManager const&) -> TransactionManager& = delete;
auto operator=(TransactionManager&&) -> TransactionManager& = delete;

// Destructor
~TransactionManager() {
if (m_success) {
m_success_handler();
} else {
m_failure_handler();
}
}

// Methods
/**
* Marks the transaction as successful.
*/
auto mark_success() -> void { m_success = true; }

private:
// Variables
SuccessHandler m_success_handler;
FailureHandler m_failure_handler;
bool m_success{false};
};

/**
* @param tag
* @return Whether the tag represents a schema tree node.
Expand Down Expand Up @@ -122,8 +79,8 @@ auto Deserializer::deserialize_to_next_log_event(clp::ReaderInterface& reader
auto const utc_offset_snapshot{m_utc_offset};
m_schema_tree->take_snapshot();
TransactionManager revert_manager{
[]() -> void {},
[&]() -> void {
[]() noexcept -> void {},
[&]() noexcept -> void {
m_utc_offset = utc_offset_snapshot;
m_schema_tree->revert();
}
Expand Down

0 comments on commit 035449a

Please sign in to comment.