xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
nodestore
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
18
namespace
xrpl::node_store
{
19
20
void
21
DatabaseNodeImp::store
(
NodeObjectType
type,
Blob
&& data,
UInt256
const
& hash,
std::uint32_t
)
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
36
void
37
DatabaseNodeImp::asyncFetch
(
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
54
void
55
DatabaseNodeImp::sweep
()
56
{
57
if
(
cache_
)
58
cache_
->sweep();
59
}
60
61
std::shared_ptr<NodeObject>
62
DatabaseNodeImp::fetchNodeObject
(
63
UInt256
const
& hash,
64
std::uint32_t
,
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
;
106
case
Status::DataCorrupt
:
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::node_store
xrpl::NodeObject::createObject
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, UInt256 const &hash)
Create an object from fields.
Definition
NodeObject.cpp:19
xrpl::node_store::DatabaseNodeImp::store
void store(NodeObjectType type, Blob &&data, UInt256 const &hash, std::uint32_t) override
Store the object.
Definition
DatabaseNodeImp.cpp:21
xrpl::node_store::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition
DatabaseNodeImp.cpp:55
xrpl::node_store::DatabaseNodeImp::asyncFetch
void asyncFetch(UInt256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback) override
Fetch an object without waiting.
Definition
DatabaseNodeImp.cpp:37
xrpl::node_store::DatabaseNodeImp::backend_
std::shared_ptr< Backend > backend_
Definition
DatabaseNodeImp.h:128
xrpl::node_store::DatabaseNodeImp::cache_
std::shared_ptr< TaggedCache< UInt256, NodeObject > > cache_
Definition
DatabaseNodeImp.h:126
xrpl::node_store::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(UInt256 const &hash, std::uint32_t, FetchReport &fetchReport, bool duplicate) override
Definition
DatabaseNodeImp.cpp:62
xrpl::node_store::Database::asyncFetch
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
libxrpl/nodestore/Database.cpp:178
xrpl::node_store::Database::storeStats
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition
Database.h:249
xrpl::node_store::Database::j_
beast::Journal const j_
Definition
Database.h:228
cstdint
exception
std::function
functional
std::uint32_t
memory
xrpl::node_store
Definition
ServiceRegistry.h:18
xrpl::node_store::Status
Status
Return codes from Backend operations.
Definition
nodestore/Types.h:24
xrpl::node_store::Status::NotFound
@ NotFound
Definition
nodestore/Types.h:26
xrpl::node_store::Status::Ok
@ Ok
Definition
nodestore/Types.h:25
xrpl::node_store::Status::DataCorrupt
@ DataCorrupt
Definition
nodestore/Types.h:27
xrpl::NodeObjectType
NodeObjectType
The types of node objects.
Definition
NodeObject.h:18
xrpl::NodeObjectType::Dummy
@ Dummy
Definition
NodeObject.h:23
xrpl::rethrow
XRPL_NO_SANITIZE_ADDRESS void rethrow()
Rethrow the exception currently being handled.
Definition
contract.h:36
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::Blob
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition
Blob.h:11
std::shared_ptr::reset
T reset(T... args)
std::shared_ptr
xrpl::node_store::FetchReport
Contains information about a fetch operation.
Definition
include/xrpl/nodestore/Scheduler.h:15
xrpl::node_store::FetchReport::wasFound
bool wasFound
Definition
include/xrpl/nodestore/Scheduler.h:22
utility
std::exception::what
T what(T... args)
Generated by
1.17.0