Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
StrandedPriorityQueue.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/async/AnyStrand.hpp"
23
24#include <cstddef>
25#include <functional>
26#include <optional>
27#include <queue>
28#include <type_traits>
29#include <utility>
30#include <vector>
31
32namespace util {
33
38template <typename T, typename Compare = std::less<T>>
41 std::size_t limit_;
42 std::priority_queue<T, std::vector<T>, Compare> queue_;
43
44public:
50 StrandedPriorityQueue(util::async::AnyStrand&& strand, std::optional<std::size_t> limit = std::nullopt)
51 : strand_(std::move(strand)), limit_(limit.value_or(0uz))
52 {
53 }
54
63 template <typename I>
64 [[nodiscard]] bool
65 enqueue(I&& item)
66 requires std::is_same_v<std::decay_t<I>, T>
67 {
68 return strand_
69 .execute([&item, this] {
70 if (limit_ == 0uz or queue_.size() < limit_) {
71 queue_.push(std::forward<I>(item));
72 return true;
73 }
74 return false;
75 })
76 .get()
77 .value_or(false); // if some exception happens - failed to add
78 }
79
85 [[nodiscard]] std::optional<T>
87 {
88 return strand_
89 .execute([this] -> std::optional<T> {
90 std::optional<T> out;
91
92 if (not queue_.empty()) {
93 out.emplace(queue_.top());
94 queue_.pop();
95 }
96
97 return out;
98 })
99 .get()
100 .value_or(std::nullopt);
101 }
102
109 [[nodiscard]] bool
111 {
112 return strand_.execute([this] { return queue_.empty(); }).get().value();
113 }
114};
115
116} // namespace util
A wrapper for std::priority_queue that serialises operations using a strand.
Definition StrandedPriorityQueue.hpp:39
StrandedPriorityQueue(util::async::AnyStrand &&strand, std::optional< std::size_t > limit=std::nullopt)
Construct a new priority queue on a strand.
Definition StrandedPriorityQueue.hpp:50
bool empty()
Check if the queue is empty.
Definition StrandedPriorityQueue.hpp:110
bool enqueue(I &&item)
Enqueue a new item onto the queue if space is available.
Definition StrandedPriorityQueue.hpp:65
std::optional< T > dequeue()
Dequeue the next available item out of the queue.
Definition StrandedPriorityQueue.hpp:86
A type-erased execution context.
Definition AnyStrand.hpp:40
auto execute(SomeHandlerWithoutStopToken auto &&fn)
Execute a function without a stop token on the strand.
Definition AnyStrand.hpp:65
This namespace contains various utilities.
Definition AccountUtils.hpp:30