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"
25#include "util/log/Logger.hpp"
26
27#include <grpcpp/grpcpp.h>
28#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
29
30#include <cstdint>
31#include <memory>
32#include <utility>
33
34namespace etl::impl {
35
40private:
41 util::Logger log_{"ETL"};
42
43 std::shared_ptr<BackendInterface> backend_;
44 std::shared_ptr<etlng::LoadBalancerInterface> loadBalancer_;
45
46public:
50 LedgerFetcher(std::shared_ptr<BackendInterface> backend, std::shared_ptr<etlng::LoadBalancerInterface> balancer)
51 : backend_(std::move(backend)), loadBalancer_(std::move(balancer))
52 {
53 }
54
64 [[nodiscard]] OptionalGetLedgerResponseType
65 fetchData(uint32_t sequence) override
66 {
67 LOG(log_.debug()) << "Attempting to fetch ledger with sequence = " << sequence;
68
69 auto response = loadBalancer_->fetchLedger(sequence, false, false);
70 if (response)
71 LOG(log_.trace()) << "GetLedger reply = " << response->DebugString();
72 return response;
73 }
74
84 [[nodiscard]] OptionalGetLedgerResponseType
85 fetchDataAndDiff(uint32_t sequence) override
86 {
87 LOG(log_.debug()) << "Attempting to fetch ledger with sequence = " << sequence;
88
89 auto const isCacheFull = backend_->cache().isFull();
90 auto const isLedgerCached = backend_->cache().latestLedgerSequence() >= sequence;
91 if (isLedgerCached) {
92 LOG(log_.info()) << sequence << " is already cached, the current latest seq in cache is "
93 << backend_->cache().latestLedgerSequence() << " and the cache is "
94 << (isCacheFull ? "full" : "not full");
95 }
96
97 auto response = loadBalancer_->fetchLedger(sequence, true, !isCacheFull || isLedgerCached);
98 if (response)
99 LOG(log_.trace()) << "GetLedger reply = " << response->DebugString();
100
101 return response;
102 }
103};
104
105} // namespace etl::impl
GRPC Ledger data fetcher.
Definition LedgerFetcher.hpp:39
OptionalGetLedgerResponseType fetchData(uint32_t sequence) override
Extract data for a particular ledger from an ETL source.
Definition LedgerFetcher.hpp:65
OptionalGetLedgerResponseType fetchDataAndDiff(uint32_t sequence) override
Extract diff data for a particular ledger from an ETL source.
Definition LedgerFetcher.hpp:85
LedgerFetcher(std::shared_ptr< BackendInterface > backend, std::shared_ptr< etlng::LoadBalancerInterface > balancer)
Create an instance of the fetcher.
Definition LedgerFetcher.hpp:50
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:214
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:209
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:219
An interface for LedgerFetcher.
Definition LedgerFetcherInterface.hpp:34