rippled
Loading...
Searching...
No Matches
DatabaseNodeImp.cpp
1#include <xrpl/nodestore/detail/DatabaseNodeImp.h>
2
3namespace xrpl {
4namespace NodeStore {
5
6void
8{
9 storeStats(1, data.size());
10
11 auto obj = NodeObject::createObject(type, std::move(data), hash);
12 backend_->store(obj);
13}
14
15void
17 uint256 const& hash,
18 std::uint32_t ledgerSeq,
19 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback)
20{
21 Database::asyncFetch(hash, ledgerSeq, std::move(callback));
22}
23
25DatabaseNodeImp::fetchNodeObject(uint256 const& hash, std::uint32_t, FetchReport& fetchReport, bool duplicate)
26{
27 std::shared_ptr<NodeObject> nodeObject = nullptr;
28 Status status;
29
30 try
31 {
32 status = backend_->fetch(hash.data(), &nodeObject);
33 }
34 catch (std::exception const& e)
35 {
36 JLOG(j_.fatal()) << "fetchNodeObject " << hash << ": Exception fetching from backend: " << e.what();
37 Rethrow();
38 }
39
40 switch (status)
41 {
42 case ok:
43 case notFound:
44 break;
45 case dataCorrupt:
46 JLOG(j_.fatal()) << "fetchNodeObject " << hash << ": nodestore data is corrupted";
47 break;
48 default:
49 JLOG(j_.warn()) << "fetchNodeObject " << hash << ": backend returns unknown result " << status;
50 break;
51 }
52
53 if (nodeObject)
54 fetchReport.wasFound = true;
55
56 return nodeObject;
57}
58
61{
62 using namespace std::chrono;
63 auto const before = steady_clock::now();
64
66 batch.reserve(hashes.size());
67 for (size_t i = 0; i < hashes.size(); ++i)
68 {
69 auto const& hash = hashes[i];
71 }
72
73 // Get the node objects that match the hashes from the backend. To protect
74 // against the backends returning fewer or more results than expected, the
75 // container is resized to the number of hashes.
76 auto results = backend_->fetchBatch(batch).first;
77 XRPL_ASSERT(
78 results.size() == hashes.size() || results.empty(),
79 "number of output objects either matches number of input hashes or is empty");
80 results.resize(hashes.size());
81 for (size_t i = 0; i < results.size(); ++i)
82 {
83 if (!results[i])
84 {
85 JLOG(j_.error()) << "fetchBatch - "
86 << "record not found in db. hash = " << strHex(hashes[i]);
87 }
88 }
89
90 auto fetchDurationUs = std::chrono::duration_cast<std::chrono::microseconds>(steady_clock::now() - before).count();
91 updateFetchMetrics(hashes.size(), 0, fetchDurationUs);
92 return results;
93}
94
95} // namespace NodeStore
96} // namespace xrpl
Stream fatal() const
Definition Journal.h:324
Stream error() const
Definition Journal.h:318
Stream warn() const
Definition Journal.h:312
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
std::shared_ptr< Backend > backend_
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport, bool duplicate) override
void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback) override
Fetch an object without waiting.
std::vector< std::shared_ptr< NodeObject > > fetchBatch(std::vector< uint256 > const &hashes)
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:216
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition Database.h:228
beast::Journal const j_
Definition Database.h:195
virtual void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback)
Fetch an object without waiting.
Definition Database.cpp:149
pointer data()
Definition base_uint.h:101
Status
Return codes from Backend operations.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:28
T push_back(T... args)
T reserve(T... args)
T size(T... args)
Contains information about a fetch operation.
T what(T... args)