Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LedgerFetcher.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, 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 "util/log/Logger.hpp"
24
25#include <grpcpp/grpcpp.h>
26#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
27
28#include <cstdint>
29#include <memory>
30#include <utility>
31
32namespace etl::impl {
33
37template <typename LoadBalancerType>
39public:
40 using OptionalGetLedgerResponseType = typename LoadBalancerType::OptionalGetLedgerResponseType;
41
42private:
43 util::Logger log_{"ETL"};
44
45 std::shared_ptr<BackendInterface> backend_;
46 std::shared_ptr<LoadBalancerType> loadBalancer_;
47
48public:
52 LedgerFetcher(std::shared_ptr<BackendInterface> backend, std::shared_ptr<LoadBalancerType> balancer)
53 : backend_(std::move(backend)), loadBalancer_(std::move(balancer))
54 {
55 }
56
66 [[nodiscard]] OptionalGetLedgerResponseType
67 fetchData(uint32_t sequence)
68 {
69 LOG(log_.debug()) << "Attempting to fetch ledger with sequence = " << sequence;
70
71 auto response = loadBalancer_->fetchLedger(sequence, false, false);
72 if (response)
73 LOG(log_.trace()) << "GetLedger reply = " << response->DebugString();
74 return response;
75 }
76
86 [[nodiscard]] OptionalGetLedgerResponseType
87 fetchDataAndDiff(uint32_t sequence)
88 {
89 LOG(log_.debug()) << "Attempting to fetch ledger with sequence = " << sequence;
90
91 auto const isCacheFull = backend_->cache().isFull();
92 auto const isLedgerCached = backend_->cache().latestLedgerSequence() >= sequence;
93 if (isLedgerCached) {
94 LOG(log_.info()) << sequence << " is already cached, the current latest seq in cache is "
95 << backend_->cache().latestLedgerSequence() << " and the cache is "
96 << (isCacheFull ? "full" : "not full");
97 }
98
99 auto response = loadBalancer_->fetchLedger(sequence, true, !isCacheFull || isLedgerCached);
100 if (response)
101 LOG(log_.trace()) << "GetLedger reply = " << response->DebugString();
102
103 return response;
104 }
105};
106
107} // namespace etl::impl
GRPC Ledger data fetcher.
Definition LedgerFetcher.hpp:38
LedgerFetcher(std::shared_ptr< BackendInterface > backend, std::shared_ptr< LoadBalancerType > balancer)
Create an instance of the fetcher.
Definition LedgerFetcher.hpp:52
OptionalGetLedgerResponseType fetchDataAndDiff(uint32_t sequence)
Extract diff data for a particular ledger from an ETL source.
Definition LedgerFetcher.hpp:87
OptionalGetLedgerResponseType fetchData(uint32_t sequence)
Extract data for a particular ledger from an ETL source.
Definition LedgerFetcher.hpp:67
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:200
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:195
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:205