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
26 uint256 const& hash,
28 FetchReport& fetchReport,
29 bool duplicate)
30{
31 std::shared_ptr<NodeObject> nodeObject = nullptr;
32 Status status = ok;
33
34 try
35 {
36 status = backend_->fetch(hash, &nodeObject);
37 }
38 catch (std::exception const& e)
39 {
40 JLOG(j_.fatal()) << "fetchNodeObject " << hash
41 << ": Exception fetching from backend: " << e.what();
42 Rethrow();
43 }
44
45 switch (status)
46 {
47 case ok:
48 case notFound:
49 break;
50 case dataCorrupt:
51 JLOG(j_.fatal()) << "fetchNodeObject " << hash << ": nodestore data is corrupted";
52 break;
53 default:
54 JLOG(j_.warn()) << "fetchNodeObject " << hash << ": backend returns unknown result "
55 << status;
56 break;
57 }
58
59 if (nodeObject)
60 fetchReport.wasFound = true;
61
62 return nodeObject;
63}
64
67{
68 using namespace std::chrono;
69 auto const before = steady_clock::now();
70
71 // Get the node objects that match the hashes from the backend. To protect
72 // against the backends returning fewer or more results than expected, the
73 // container is resized to the number of hashes.
74 auto results = backend_->fetchBatch(hashes).first;
75 XRPL_ASSERT(
76 results.size() == hashes.size() || results.empty(),
77 "number of output objects either matches number of input hashes or is empty");
78 results.resize(hashes.size());
79 for (size_t i = 0; i < results.size(); ++i)
80 {
81 if (!results[i])
82 {
83 JLOG(j_.error()) << "fetchBatch - "
84 << "record not found in db. hash = " << strHex(hashes[i]);
85 }
86 }
87
88 auto fetchDurationUs =
89 std::chrono::duration_cast<std::chrono::microseconds>(steady_clock::now() - before).count();
90 updateFetchMetrics(hashes.size(), 0, fetchDurationUs);
91 return results;
92}
93
94} // namespace NodeStore
95} // namespace xrpl
Stream fatal() const
Definition Journal.h:325
Stream error() const
Definition Journal.h:319
Stream warn() const
Definition Journal.h:313
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:156
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
XRPL_NO_SANITIZE_ADDRESS void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:33
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
T size(T... args)
Contains information about a fetch operation.
T what(T... args)