Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
TaskQueue.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 "etlng/Models.hpp"
23#include "util/Mutex.hpp"
24
25#include <cstddef>
26#include <cstdint>
27#include <optional>
28#include <queue>
29#include <utility>
30#include <vector>
31
32namespace etlng::impl {
33
35 [[nodiscard]] bool
36 operator()(model::LedgerData const& lhs, model::LedgerData const& rhs) const noexcept
37 {
38 return lhs.seq > rhs.seq;
39 }
40};
41
46class TaskQueue {
47 std::size_t limit_;
48 std::uint32_t increment_;
49
50 struct Data {
51 std::uint32_t expectedSequence;
52 std::priority_queue<model::LedgerData, std::vector<model::LedgerData>, ReverseOrderComparator> forwardLoadQueue;
53
54 Data(std::uint32_t seq) : expectedSequence(seq)
55 {
56 }
57 };
58
60
61public:
62 struct Settings {
63 std::uint32_t startSeq = 0u; // sequence to start from (for dequeue)
64 std::uint32_t increment = 1u; // increment sequence by this value once dequeue was successful
65 std::optional<std::size_t> limit = std::nullopt;
66 };
67
72 explicit TaskQueue(Settings settings)
73 : limit_(settings.limit.value_or(0uz)), increment_(settings.increment), data_(settings.startSeq)
74 {
75 }
76
84 [[nodiscard]] bool
86 {
87 auto lock = data_.lock();
88
89 if (limit_ == 0uz or lock->forwardLoadQueue.size() < limit_) {
90 lock->forwardLoadQueue.push(std::move(item));
91 return true;
92 }
93
94 return false;
95 }
96
102 [[nodiscard]] std::optional<model::LedgerData>
104 {
105 auto lock = data_.lock();
106 std::optional<model::LedgerData> out;
107
108 if (not lock->forwardLoadQueue.empty() && lock->forwardLoadQueue.top().seq == lock->expectedSequence) {
109 out.emplace(lock->forwardLoadQueue.top());
110 lock->forwardLoadQueue.pop();
111 lock->expectedSequence += increment_;
112 }
113
114 return out;
115 }
116
123 [[nodiscard]] bool
125 {
126 return data_.lock()->forwardLoadQueue.empty();
127 }
128};
129
130} // namespace etlng::impl
A wrapper for std::priority_queue that serialises operations using a mutex.
Definition TaskQueue.hpp:46
bool enqueue(model::LedgerData item)
Enqueue a new item onto the queue if space is available.
Definition TaskQueue.hpp:85
bool empty()
Check if the queue is empty.
Definition TaskQueue.hpp:124
std::optional< model::LedgerData > dequeue()
Dequeue the next available item out of the queue.
Definition TaskQueue.hpp:103
TaskQueue(Settings settings)
Construct a new priority queue.
Definition TaskQueue.hpp:72
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:96
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:134
Definition TaskQueue.hpp:34
Definition TaskQueue.hpp:62
Represents an entire ledger diff worth of transactions and objects.
Definition Models.hpp:143