Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CursorFromDiffProvider.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#include "util/Assert.hpp"
26
27#include <xrpl/basics/base_uint.h>
28
29#include <algorithm>
30#include <cstddef>
31#include <cstdint>
32#include <iterator>
33#include <memory>
34#include <ranges>
35#include <set>
36#include <vector>
37
38namespace etl::impl {
39
41 std::shared_ptr<BackendInterface> backend_;
42 size_t numCursors_;
43
44public:
45 CursorFromDiffProvider(std::shared_ptr<BackendInterface> const& backend, size_t numCursors)
46 : backend_{backend}, numCursors_{numCursors}
47 {
48 }
49
50 [[nodiscard]] std::vector<CursorPair>
51 getCursors(uint32_t const seq) const override
52 {
53 namespace rg = std::ranges;
54 namespace vs = std::views;
55
56 auto const fetchDiff = [this, seq](uint32_t offset) {
57 return data::synchronousAndRetryOnTimeout([this, seq, offset](auto yield) {
58 return backend_->fetchLedgerDiff(seq - offset, yield);
59 });
60 };
61
62 auto const range = backend_->fetchLedgerRange();
63 ASSERT(range.has_value(), "Ledger range is not available when cache is loading");
64
65 std::set<ripple::uint256> liveCursors;
66 std::set<ripple::uint256> deletedCursors;
67 auto i = 0;
68 while (liveCursors.size() < numCursors_ and seq - i >= range->minSequence) {
69 auto diffs = fetchDiff(i++);
70 rg::copy(
71 diffs //
72 | vs::filter([&deletedCursors](auto const& obj) {
73 return not obj.blob.empty() and !deletedCursors.contains(obj.key);
74 }) //
75 | vs::transform([](auto const& obj) { return obj.key; }),
76 std::inserter(liveCursors, std::begin(liveCursors))
77 );
78
79 // track the deleted objects
80 rg::copy(
81 diffs //
82 | vs::filter([](auto const& obj) { return obj.blob.empty(); }) //
83 | vs::transform([](auto const& obj) { return obj.key; }),
84 std::inserter(deletedCursors, std::begin(deletedCursors))
85 );
86 }
87
88 std::vector<ripple::uint256> cursors{data::kFIRST_KEY};
89 rg::copy(liveCursors | vs::take(std::min(liveCursors.size(), numCursors_)), std::back_inserter(cursors));
90 rg::sort(cursors);
91 cursors.push_back(data::kLAST_KEY);
92
93 std::vector<CursorPair> pairs;
94 pairs.reserve(cursors.size());
95
96 // FIXME: this should be `cursors | vs::pairwise` (C++23)
97 std::transform(
98 std::begin(cursors),
99 std::prev(std::end(cursors)),
100 std::next(std::begin(cursors)),
101 std::back_inserter(pairs),
102 [](auto&& a, auto&& b) -> CursorPair { return {a, b}; }
103 );
104
105 return pairs;
106 }
107};
108
109} // namespace etl::impl
Definition CursorFromDiffProvider.hpp:40
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