xrpld
Loading...
Searching...
No Matches
MemoryFactory.cpp
1#include <xrpl/basics/base_uint.h>
2#include <xrpl/basics/contract.h>
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/config/BasicConfig.h>
6#include <xrpl/config/Constants.h>
7#include <xrpl/nodestore/Backend.h>
8#include <xrpl/nodestore/Factory.h>
9#include <xrpl/nodestore/Manager.h>
10#include <xrpl/nodestore/NodeObject.h>
11#include <xrpl/nodestore/Scheduler.h>
12#include <xrpl/nodestore/Types.h>
13
14#include <boost/beast/core/string.hpp>
15#include <boost/core/ignore_unused.hpp>
16
17#include <cstddef>
18#include <functional>
19#include <map>
20#include <memory>
21#include <mutex>
22#include <stdexcept>
23#include <string>
24#include <tuple>
25#include <utility>
26
27namespace xrpl::node_store {
28
37
38class MemoryFactory : public Factory
39{
40private:
44
45public:
46 explicit MemoryFactory(Manager& manager);
47
48 [[nodiscard]] std::string
49 getName() const override;
50
53 size_t keyBytes,
54 Section const& keyValues,
55 std::size_t burstSize,
56 Scheduler& scheduler,
57 beast::Journal journal) override;
58
61 {
62 std::scoped_lock const _(mutex_);
63 auto const result =
65 MemoryDB& db = result.first->second;
66 if (db.open)
67 Throw<std::runtime_error>("already open");
68 return db;
69 }
70};
71
73
74void
76{
77 static MemoryFactory kInstance{manager};
78 gMemoryFactory = &kInstance;
79}
80
81//------------------------------------------------------------------------------
82
83class MemoryBackend : public Backend
84{
85private:
87
90 MemoryDB* db_{nullptr};
91
92public:
93 MemoryBackend(size_t keyBytes, Section const& keyValues, beast::Journal journal)
94 : name_(get(keyValues, Keys::kPath)), journal_(journal)
95 {
96 boost::ignore_unused(journal_); // Keep unused journal_ just in case.
97 if (name_.empty())
98 Throw<std::runtime_error>("Missing path in Memory backend");
99 }
100
101 ~MemoryBackend() override
102 {
103 close();
104 }
105
107 getName() override
108 {
109 return name_;
110 }
111
112 void
113 open(bool) override
114 {
115 db_ = &gMemoryFactory->open(name_);
116 }
117
118 bool
119 isOpen() override
120 {
121 return static_cast<bool>(db_);
122 }
123
124 void
125 close() override
126 {
127 db_ = nullptr;
128 }
129
130 //--------------------------------------------------------------------------
131
132 Status
133 fetch(uint256 const& hash, std::shared_ptr<NodeObject>* pObject) override
134 {
135 XRPL_ASSERT(db_, "xrpl::node_store::MemoryBackend::fetch : non-null database");
136
137 std::scoped_lock const _(db_->mutex);
138
139 auto const iter = db_->table.find(hash);
140 if (iter == db_->table.end())
141 {
142 pObject->reset();
143 return Status::NotFound;
144 }
145 *pObject = iter->second;
146 return Status::Ok;
147 }
148
149 void
150 store(std::shared_ptr<NodeObject> const& object) override
151 {
152 XRPL_ASSERT(db_, "xrpl::node_store::MemoryBackend::store : non-null database");
153 std::scoped_lock const _(db_->mutex);
154 db_->table.emplace(object->getHash(), object);
155 }
156
157 void
158 storeBatch(Batch const& batch) override
159 {
160 for (auto const& e : batch)
161 store(e);
162 }
163
164 void
165 sync() override
166 {
167 }
168
169 void
171 {
172 XRPL_ASSERT(db_, "xrpl::node_store::MemoryBackend::forEach : non-null database");
173 for (auto const& e : db_->table)
174 f(e.second);
175 }
176
177 int
178 getWriteLoad() override
179 {
180 return 0;
181 }
182
183 void
184 setDeletePath() override
185 {
186 }
187
188 [[nodiscard]] int
189 fdRequired() const override
190 {
191 return 0;
192 }
193};
194
195//------------------------------------------------------------------------------
196
198{
199 manager_.insert(*this);
200}
201
204{
205 return "Memory";
206}
207
210 size_t keyBytes,
211 Section const& keyValues,
213 Scheduler& scheduler,
214 beast::Journal journal)
215{
216 return std::make_unique<MemoryBackend>(keyBytes, keyValues, journal);
217}
218
219} // namespace xrpl::node_store
A generic endpoint for log messages.
Definition Journal.h:44
Holds a collection of configuration values.
Definition BasicConfig.h:29
A backend used for the NodeStore.
Definition Backend.h:29
Base class for backend factories.
Definition Factory.h:23
Singleton for managing NodeStore factories and back ends.
Definition Manager.h:19
void open(bool) override
Open the backend.
bool isOpen() override
Returns true is the database is open.
int fdRequired() const override
Returns the number of file descriptors the backend expects to need.
std::map< uint256 const, std::shared_ptr< NodeObject > > Map
void storeBatch(Batch const &batch) override
Store a group of objects.
void close() override
Close the backend.
int getWriteLoad() override
Estimate the number of write operations pending.
std::string getName() override
Get the human-readable name of this backend.
void store(std::shared_ptr< NodeObject > const &object) override
Store a single object.
void forEach(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
Status fetch(uint256 const &hash, std::shared_ptr< NodeObject > *pObject) override
Fetch a single object.
MemoryBackend(size_t keyBytes, Section const &keyValues, beast::Journal journal)
void setDeletePath() override
Remove contents on disk upon destruction.
MemoryDB & open(std::string const &path)
std::string getName() const override
Retrieve the name of this factory.
std::unique_ptr< Backend > createInstance(size_t keyBytes, Section const &keyValues, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal) override
Create an instance of this factory's backend.
std::map< std::string, MemoryDB, boost::beast::iless > map_
Scheduling for asynchronous backend activity.
T make_tuple(T... args)
T make_unique(T... args)
MemoryFactory * gMemoryFactory
void registerMemoryFactory(Manager &manager)
std::vector< std::shared_ptr< NodeObject > > Batch
A batch of NodeObjects to write at once.
Status
Return codes from Backend operations.
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
BaseUInt< 256 > uint256
Definition base_uint.h:580
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T piecewise_construct
std::map< uint256 const, std::shared_ptr< NodeObject > > table