xrpld
Loading...
Searching...
No Matches
DatabaseNodeImp.cpp
1#include <xrpl/nodestore/detail/DatabaseNodeImp.h>
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/nodestore/Database.h>
8#include <xrpl/nodestore/NodeObject.h>
9#include <xrpl/nodestore/Scheduler.h>
10#include <xrpl/nodestore/Types.h>
11
12#include <cstdint>
13#include <exception>
14#include <functional>
15#include <memory>
16#include <utility>
17
18namespace xrpl::NodeStore {
19
20void
22{
23 storeStats(1, data.size());
24
25 auto obj = NodeObject::createObject(type, std::move(data), hash);
26 backend_->store(obj);
27 if (cache_)
28 {
29 // After the store, replace a negative cache entry if there is one
30 cache_->canonicalize(hash, obj, [](std::shared_ptr<NodeObject> const& n) {
31 return n->getType() == NodeObjectType::Dummy;
32 });
33 }
34}
35
36void
38 uint256 const& hash,
39 std::uint32_t ledgerSeq,
40 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback)
41{
42 if (cache_)
43 {
44 std::shared_ptr<NodeObject> const obj = cache_->fetch(hash);
45 if (obj)
46 {
47 callback(obj->getType() == NodeObjectType::Dummy ? nullptr : obj);
48 return;
49 }
50 }
51 Database::asyncFetch(hash, ledgerSeq, std::move(callback));
52}
53
54void
56{
57 if (cache_)
58 cache_->sweep();
59}
60
63 uint256 const& hash,
65 FetchReport& fetchReport,
66 bool duplicate)
67{
68 std::shared_ptr<NodeObject> nodeObject = cache_ ? cache_->fetch(hash) : nullptr;
69 if (!nodeObject)
70 {
71 JLOG(j_.trace()) << "fetchNodeObject " << hash << ": record not "
72 << (cache_ ? "cached" : "found");
73
74 Status status = Status::Ok;
75 try
76 {
77 status = backend_->fetch(hash, &nodeObject);
78 }
79 catch (std::exception const& e)
80 {
81 JLOG(j_.fatal()) << "fetchNodeObject " << hash
82 << ": Exception fetching from backend: " << e.what();
83 rethrow();
84 }
85
86 switch (status)
87 {
88 case Status::Ok:
89 if (cache_)
90 {
91 if (nodeObject)
92 {
93 cache_->canonicalizeReplaceClient(hash, nodeObject);
94 }
95 else
96 {
97 auto notFound = NodeObject::createObject(NodeObjectType::Dummy, {}, hash);
98 cache_->canonicalizeReplaceClient(hash, notFound);
99 if (notFound->getType() != NodeObjectType::Dummy)
100 nodeObject = notFound;
101 }
102 }
103 break;
104 case Status::NotFound:
105 break;
107 JLOG(j_.fatal()) << "fetchNodeObject " << hash << ": nodestore data is corrupted";
108 break;
109 default:
110 JLOG(j_.warn()) << "fetchNodeObject " << hash << ": backend returns unknown result "
111 << static_cast<int>(status);
112 break;
113 }
114 }
115 else
116 {
117 JLOG(j_.trace()) << "fetchNodeObject " << hash << ": record found in cache";
118 if (nodeObject->getType() == NodeObjectType::Dummy)
119 nodeObject.reset();
120 }
121
122 if (nodeObject)
123 fetchReport.wasFound = true;
124
125 return nodeObject;
126}
127
128} // namespace xrpl::NodeStore
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
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 sweep() override
Remove expired entries from the positive and negative caches.
void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback) override
Fetch an object without waiting.
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:221
beast::Journal const j_
Definition Database.h:200
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:178
Status
Return codes from Backend operations.
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
XRPL_NO_SANITIZE_ADDRESS void rethrow()
Rethrow the exception currently being handled.
Definition contract.h:33
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
BaseUInt< 256 > uint256
Definition base_uint.h:562
T reset(T... args)
Contains information about a fetch operation.
T what(T... args)