xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
nodestore
ManagerImp.cpp
1
#include <xrpl/nodestore/detail/ManagerImp.h>
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/beast/utility/Journal.h>
5
#include <xrpl/beast/utility/instrumentation.h>
6
#include <xrpl/config/BasicConfig.h>
7
#include <xrpl/config/Constants.h>
8
#include <xrpl/nodestore/Backend.h>
9
#include <xrpl/nodestore/Database.h>
10
#include <xrpl/nodestore/Manager.h>
11
#include <xrpl/nodestore/NodeObject.h>
12
#include <xrpl/nodestore/Scheduler.h>
13
#include <xrpl/nodestore/detail/DatabaseNodeImp.h>
14
15
#include <boost/algorithm/string/predicate.hpp>
16
17
#include <
algorithm
>
18
#include <
cstddef
>
19
#include <
memory
>
20
#include <
mutex
>
21
#include <
stdexcept
>
22
#include <
string
>
23
#include <
utility
>
24
25
namespace
xrpl::node_store
{
26
27
ManagerImp
&
28
ManagerImp::instance
()
29
{
30
static
ManagerImp
kInst;
31
return
kInst;
32
}
33
34
void
35
ManagerImp::missingBackend
()
36
{
37
Throw<std::runtime_error>
(
38
"Your xrpld.cfg is missing a [node_db] entry, "
39
"please see the xrpld-example.cfg file!"
);
40
}
41
42
// We shouldn't rely on global variables for lifetime management because their
43
// lifetime is not well-defined. ManagerImp may get destroyed before the Factory
44
// classes, and then, calling Manager::instance().erase() in the destructors of
45
// the Factory classes is an undefined behaviour.
46
void
47
registerNuDBFactory
(
Manager
& manager);
48
#if XRPL_ROCKSDB_AVAILABLE
49
void
50
registerRocksDBFactory(
Manager
& manager);
51
#endif
52
void
53
registerNullFactory
(
Manager
& manager);
54
void
55
registerMemoryFactory
(
Manager
& manager);
56
57
ManagerImp::ManagerImp
()
58
{
59
registerNuDBFactory
(*
this
);
60
#if XRPL_ROCKSDB_AVAILABLE
61
registerRocksDBFactory(*
this
);
62
#endif
63
registerNullFactory
(*
this
);
64
registerMemoryFactory
(*
this
);
65
}
66
67
std::unique_ptr<Backend>
68
ManagerImp::makeBackend
(
69
Section
const
& parameters,
70
std::size_t
burstSize,
71
Scheduler
& scheduler,
72
beast::Journal
journal)
73
{
74
std::string
const
type{
get
(parameters,
Keys::kType
)};
75
if
(type.
empty
())
76
missingBackend
();
77
78
auto
factory{
find
(type)};
79
if
(factory ==
nullptr
)
80
{
81
missingBackend
();
82
}
83
84
return
factory->createInstance(
85
NodeObject::kKeyBytes
, parameters, burstSize, scheduler, journal);
86
}
87
88
std::unique_ptr<Database>
89
ManagerImp::makeDatabase
(
90
std::size_t
burstSize,
91
Scheduler
& scheduler,
92
int
readThreads,
93
Section
const
& config,
94
beast::Journal
journal)
95
{
96
auto
backend{
makeBackend
(config, burstSize, scheduler, journal)};
97
backend->open();
98
return
std::make_unique<DatabaseNodeImp>
(
99
scheduler, readThreads, std::move(backend), config, journal);
100
}
101
102
void
103
ManagerImp::insert
(
Factory
& factory)
104
{
105
std::scoped_lock
const
_(
mutex_
);
106
list_
.push_back(&factory);
107
}
108
109
void
110
ManagerImp::erase
(
Factory
& factory)
111
{
112
std::scoped_lock
const
_(
mutex_
);
113
auto
const
iter =
114
std::ranges::find_if
(
list_
, [&factory](
Factory
* other) {
return
other == &factory; });
115
XRPL_ASSERT(iter !=
list_
.end(),
"xrpl::node_store::ManagerImp::erase : valid input"
);
116
list_
.erase(iter);
117
}
118
119
Factory
*
120
ManagerImp::find
(
std::string
const
& name)
121
{
122
std::scoped_lock
const
_(
mutex_
);
123
auto
const
iter =
std::ranges::find_if
(
124
list_
, [&name](
Factory
* other) {
return
boost::iequals(name, other->
getName
()); });
125
if
(iter ==
list_
.end())
126
return
nullptr
;
127
return
*iter;
128
}
129
130
//------------------------------------------------------------------------------
131
132
Manager
&
133
Manager::instance
()
134
{
135
return
ManagerImp::instance
();
136
}
137
138
}
// namespace xrpl::node_store
algorithm
std::string
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::NodeObject::kKeyBytes
static constexpr std::size_t kKeyBytes
Definition
NodeObject.h:39
xrpl::Section
Holds a collection of configuration values.
Definition
BasicConfig.h:28
xrpl::node_store::Factory
Base class for backend factories.
Definition
Factory.h:23
xrpl::node_store::Factory::getName
virtual std::string getName() const =0
Retrieve the name of this factory.
xrpl::node_store::ManagerImp
Definition
ManagerImp.h:19
xrpl::node_store::ManagerImp::find
Factory * find(std::string const &name) override
Return a pointer to the matching factory if it exists.
Definition
ManagerImp.cpp:120
xrpl::node_store::ManagerImp::insert
void insert(Factory &factory) override
Add a factory.
Definition
ManagerImp.cpp:103
xrpl::node_store::ManagerImp::missingBackend
static void missingBackend()
Definition
ManagerImp.cpp:35
xrpl::node_store::ManagerImp::list_
std::vector< Factory * > list_
Definition
ManagerImp.h:22
xrpl::node_store::ManagerImp::ManagerImp
ManagerImp()
Definition
ManagerImp.cpp:57
xrpl::node_store::ManagerImp::instance
static ManagerImp & instance()
Definition
ManagerImp.cpp:28
xrpl::node_store::ManagerImp::erase
void erase(Factory &factory) override
Remove a factory.
Definition
ManagerImp.cpp:110
xrpl::node_store::ManagerImp::makeDatabase
std::unique_ptr< Database > makeDatabase(std::size_t burstSize, Scheduler &scheduler, int readThreads, Section const &config, beast::Journal journal) override
Construct a NodeStore database.
Definition
ManagerImp.cpp:89
xrpl::node_store::ManagerImp::mutex_
std::mutex mutex_
Definition
ManagerImp.h:21
xrpl::node_store::ManagerImp::makeBackend
std::unique_ptr< Backend > makeBackend(Section const ¶meters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal) override
Create a backend.
Definition
ManagerImp.cpp:68
xrpl::node_store::Manager
Singleton for managing NodeStore factories and back ends.
Definition
Manager.h:19
xrpl::node_store::Manager::instance
static Manager & instance()
Returns the instance of the manager singleton.
Definition
ManagerImp.cpp:133
xrpl::node_store::Scheduler
Scheduling for asynchronous backend activity.
Definition
include/xrpl/nodestore/Scheduler.h:46
cstddef
std::string::empty
T empty(T... args)
std::ranges::find_if
T find_if(T... args)
std::make_unique
T make_unique(T... args)
memory
mutex
xrpl::node_store
Definition
ServiceRegistry.h:18
xrpl::node_store::registerMemoryFactory
void registerMemoryFactory(Manager &manager)
Definition
MemoryFactory.cpp:75
xrpl::node_store::registerNuDBFactory
void registerNuDBFactory(Manager &manager)
Definition
libxrpl/nodestore/backend/NuDBFactory.cpp:440
xrpl::node_store::registerNullFactory
void registerNullFactory(Manager &manager)
Definition
NullFactory.cpp:123
xrpl::get
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
Definition
BasicConfig.h:360
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::scoped_lock
std::size_t
stdexcept
string
xrpl::Keys::kType
static constexpr auto kType
Definition
Constants.h:174
std::unique_ptr
utility
Generated by
1.17.0