Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CursorFromAccountProvider.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, 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 "data/BackendInterface.hpp"
23#include "data/Types.hpp"
24#include "etl/impl/BaseCursorProvider.hpp"
25
26#include <xrpl/basics/base_uint.h>
27
28#include <algorithm>
29#include <cstddef>
30#include <cstdint>
31#include <iterator>
32#include <memory>
33#include <set>
34#include <vector>
35
36namespace etl::impl {
37
39 std::shared_ptr<BackendInterface> backend_;
40 size_t numCursors_;
41 size_t pageSize_;
42
43public:
44 CursorFromAccountProvider(std::shared_ptr<BackendInterface> const& backend, size_t numCursors, size_t pageSize)
45 : backend_{backend}, numCursors_{numCursors}, pageSize_{pageSize}
46 {
47 }
48
49 [[nodiscard]] std::vector<CursorPair>
50 getCursors(uint32_t const seq) const override
51 {
52 namespace rg = std::ranges;
53
54 auto accountRoots = [this, seq]() {
55 return data::synchronousAndRetryOnTimeout([this, seq](auto yield) {
56 return backend_->fetchAccountRoots(numCursors_, pageSize_, seq, yield);
57 });
58 }();
59
60 rg::sort(accountRoots);
61 std::vector<ripple::uint256> cursors{data::kFIRST_KEY};
62 rg::copy(accountRoots.begin(), accountRoots.end(), std::back_inserter(cursors));
63 rg::sort(cursors);
64 cursors.push_back(data::kLAST_KEY);
65
66 std::vector<CursorPair> pairs;
67 pairs.reserve(cursors.size());
68
69 // FIXME: this should be `cursors | vs::pairwise` (C++23)
70 std::transform(
71 std::begin(cursors),
72 std::prev(std::end(cursors)),
73 std::next(std::begin(cursors)),
74 std::back_inserter(pairs),
75 [](auto&& a, auto&& b) -> CursorPair { return {a, b}; }
76 );
77
78 return pairs;
79 }
80};
81
82} // namespace etl::impl
Definition CursorFromAccountProvider.hpp:38
auto synchronousAndRetryOnTimeout(FnType &&func)
Synchronously execute the given function object and retry until no DatabaseTimeout is thrown.
Definition BackendInterface.hpp:130
Definition BaseCursorProvider.hpp:33
Definition BaseCursorProvider.hpp:28