Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CursorFromFixDiffNumProvider.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 <boost/algorithm/string/predicate.hpp>
27#include <boost/asio/io_context.hpp>
28#include <xrpl/basics/base_uint.h>
29#include <xrpl/basics/strHex.h>
30
31#include <algorithm>
32#include <cstddef>
33#include <cstdint>
34#include <iterator>
35#include <memory>
36#include <ranges>
37#include <string>
38#include <vector>
39
40namespace etl::impl {
41
43 std::shared_ptr<BackendInterface> backend_;
44
45 size_t numDiffs_;
46
47public:
48 CursorFromFixDiffNumProvider(std::shared_ptr<BackendInterface> const& backend, size_t numDiffs)
49 : backend_{backend}, numDiffs_{numDiffs}
50 {
51 }
52
53 [[nodiscard]] std::vector<CursorPair>
54 getCursors(uint32_t const seq) const override
55 {
56 namespace rg = std::ranges;
57 namespace vs = std::views;
58
59 auto diffs = std::vector<data::LedgerObject>{};
60
61 auto const append = [](auto&& a, auto&& b) { a.insert(std::end(a), std::begin(b), std::end(b)); };
62 auto const fetchDiff = [this, seq](uint32_t offset) {
63 return data::synchronousAndRetryOnTimeout([this, seq, offset](auto yield) {
64 return backend_->fetchLedgerDiff(seq - offset, yield);
65 });
66 };
67
68 rg::for_each(vs::iota(0u, numDiffs_), [&](auto i) { append(diffs, fetchDiff(i)); });
69 rg::sort(diffs, [](auto const& a, auto const& b) {
70 return a.key < b.key or (a.key == b.key and std::size(a.blob) < std::size(b.blob));
71 });
72
73 auto const [removalCursor, last] =
74 rg::unique(diffs, [](auto const& a, auto const& b) { return a.key == b.key; });
75 diffs.erase(removalCursor, last);
76
77 std::vector<ripple::uint256> cursors{data::kFIRST_KEY};
78 rg::copy(
79 diffs //
80 | vs::filter([](auto const& obj) { return not obj.blob.empty(); }) //
81 | vs::transform([](auto const& obj) { return obj.key; }),
82 std::back_inserter(cursors)
83 );
84 cursors.push_back(data::kLAST_KEY); // last pair should cover the remaining range
85
86 std::vector<CursorPair> pairs;
87 pairs.reserve(cursors.size());
88
89 // FIXME: this should be `cursors | vs::pairwise` (C++23)
90 std::transform(
91 std::begin(cursors),
92 std::prev(std::end(cursors)),
93 std::next(std::begin(cursors)),
94 std::back_inserter(pairs),
95 [](auto&& a, auto&& b) -> CursorPair { return {a, b}; }
96 );
97
98 return pairs;
99 }
100};
101
102} // namespace etl::impl
Definition CursorFromFixDiffNumProvider.hpp:42
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