Skip to content

Commit

Permalink
Refactor Transactions
Browse files Browse the repository at this point in the history
The old implementation used redundant locking mechanisms and
unnecessarily complicated circular buffers.

Instead this now uses just a single recursive lock and a normal
collection, which simplifies this a lot and hopefully makes emscripten
more happy as well.

This also integrates the multiplexed settings into the Node as a drop-in
replacement for the basic settings.
  • Loading branch information
vimpostor committed Sep 28, 2023
1 parent 9aa650e commit 978a011
Show file tree
Hide file tree
Showing 5 changed files with 470 additions and 581 deletions.
6 changes: 6 additions & 0 deletions include/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ struct node : protected std::tuple<Arguments...> {
return *_settings;
}

template<typename T>
void
setSettings(std::unique_ptr<T> &settings) {
_settings = std::move(settings);
}

template<std::size_t Index, typename Self>
friend constexpr auto &
input_port(Self *self) noexcept;
Expand Down
50 changes: 50 additions & 0 deletions include/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,50 @@

namespace fair::graph {

namespace detail {
template<class T>
inline constexpr void
hash_combine(std::size_t &seed, const T &v) noexcept {
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
} // namespace detail

struct SettingsCtx {
// using TimePoint = std::chrono::time_point<std::chrono::utc_clock>; // TODO: change once the C++20 support is ubiquitous
using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
std::optional<TimePoint> time = std::nullopt; /// UTC time-stamp from which the setting is valid
property_map context; /// user-defined multiplexing context for which the setting is valid

SettingsCtx() {}

explicit SettingsCtx(const TimePoint &t, const property_map &ctx = {}) {
time = t;
context = ctx;
}

bool
operator==(const SettingsCtx &) const
= default;

bool
operator<(const SettingsCtx &other) {
// order by time
return !time || (other.time && *time < *other.time);
}

[[nodiscard]] std::size_t
hash() const noexcept {
std::size_t seed = 0;
if (time) {
detail::hash_combine(seed, time.value().time_since_epoch().count());
}
for (const auto &[key, val] : context) {
detail::hash_combine(seed, key);
detail::hash_combine(seed, pmtv::to_base64(val));
}
return seed;
}
};

/**
Expand Down Expand Up @@ -603,4 +642,15 @@ class basic_settings : public settings_base {
static_assert(Settings<basic_settings<int>>);

} // namespace fair::graph

namespace std {
template<>
struct hash<fair::graph::SettingsCtx> {
[[nodiscard]] size_t
operator()(const fair::graph::SettingsCtx &ctx) const noexcept {
return ctx.hash();
}
};
} // namespace std

#endif // GRAPH_PROTOTYPE_SETTINGS_HPP
Loading

0 comments on commit 978a011

Please sign in to comment.