rippled
Loading...
Searching...
No Matches
MemoryFactory.cpp
1#include <xrpl/basics/contract.h>
2#include <xrpl/nodestore/Factory.h>
3#include <xrpl/nodestore/Manager.h>
4
5#include <boost/beast/core/string.hpp>
6#include <boost/core/ignore_unused.hpp>
7
8#include <map>
9#include <memory>
10#include <mutex>
11
12namespace xrpl {
13namespace NodeStore {
14
23
24class MemoryFactory : public Factory
25{
26private:
30
31public:
32 explicit MemoryFactory(Manager& manager);
33
35 getName() const override;
36
39 size_t keyBytes,
40 Section const& keyValues,
42 Scheduler& scheduler,
43 beast::Journal journal) override;
44
46 open(std::string const& path)
47 {
48 std::lock_guard const _(mutex_);
49 auto const result =
51 MemoryDB& db = result.first->second;
52 if (db.open)
53 Throw<std::runtime_error>("already open");
54 return db;
55 }
56};
57
59
60void
62{
63 static MemoryFactory instance{manager};
64 memoryFactory = &instance;
65}
66
67//------------------------------------------------------------------------------
68
69class MemoryBackend : public Backend
70{
71private:
73
76 MemoryDB* db_{nullptr};
77
78public:
79 MemoryBackend(size_t keyBytes, Section const& keyValues, beast::Journal journal)
80 : name_(get(keyValues, "path")), journal_(journal)
81 {
82 boost::ignore_unused(journal_); // Keep unused journal_ just in case.
83 if (name_.empty())
84 Throw<std::runtime_error>("Missing path in Memory backend");
85 }
86
87 ~MemoryBackend() override
88 {
89 close();
90 }
91
93 getName() override
94 {
95 return name_;
96 }
97
98 void
99 open(bool) override
100 {
102 }
103
104 bool
105 isOpen() override
106 {
107 return static_cast<bool>(db_);
108 }
109
110 void
111 close() override
112 {
113 db_ = nullptr;
114 }
115
116 //--------------------------------------------------------------------------
117
118 Status
119 fetch(uint256 const& hash, std::shared_ptr<NodeObject>* pObject) override
120 {
121 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::fetch : non-null database");
122
123 std::lock_guard const _(db_->mutex);
124
125 Map::iterator const iter = db_->table.find(hash);
126 if (iter == db_->table.end())
127 {
128 pObject->reset();
129 return notFound;
130 }
131 *pObject = iter->second;
132 return ok;
133 }
134
136 fetchBatch(std::vector<uint256> const& hashes) override
137 {
139 results.reserve(hashes.size());
140 for (auto const& h : hashes)
141 {
143 Status const status = fetch(h, &nObj);
144 if (status != ok)
145 {
146 results.push_back({});
147 }
148 else
149 {
150 results.push_back(nObj);
151 }
152 }
153
154 return {results, ok};
155 }
156
157 void
158 store(std::shared_ptr<NodeObject> const& object) override
159 {
160 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::store : non-null database");
161 std::lock_guard const _(db_->mutex);
162 db_->table.emplace(object->getHash(), object);
163 }
164
165 void
166 storeBatch(Batch const& batch) override
167 {
168 for (auto const& e : batch)
169 store(e);
170 }
171
172 void
173 sync() override
174 {
175 }
176
177 void
179 {
180 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::for_each : non-null database");
181 for (auto const& e : db_->table)
182 f(e.second);
183 }
184
185 int
186 getWriteLoad() override
187 {
188 return 0;
189 }
190
191 void
192 setDeletePath() override
193 {
194 }
195
196 int
197 fdRequired() const override
198 {
199 return 0;
200 }
201};
202
203//------------------------------------------------------------------------------
204
205MemoryFactory::MemoryFactory(Manager& manager) : manager_(manager)
206{
207 manager_.insert(*this);
208}
209
212{
213 return "Memory";
214}
215
218 size_t keyBytes,
219 Section const& keyValues,
221 Scheduler& scheduler,
222 beast::Journal journal)
223{
224 return std::make_unique<MemoryBackend>(keyBytes, keyValues, journal);
225}
226
227} // namespace NodeStore
228} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
A backend used for the NodeStore.
Definition Backend.h:20
Base class for backend factories.
Definition Factory.h:16
Singleton for managing NodeStore factories and back ends.
Definition Manager.h:12
virtual void insert(Factory &factory)=0
Add a factory.
void setDeletePath() override
Remove contents on disk upon destruction.
void store(std::shared_ptr< NodeObject > const &object) override
Store a single object.
Status fetch(uint256 const &hash, std::shared_ptr< NodeObject > *pObject) override
Fetch a single object.
beast::Journal const journal_
void close() override
Close the backend.
void open(bool) override
Open the backend.
std::pair< std::vector< std::shared_ptr< NodeObject > >, Status > fetchBatch(std::vector< uint256 > const &hashes) override
Fetch a batch synchronously.
int fdRequired() const override
Returns the number of file descriptors the backend expects to need.
bool isOpen() override
Returns true is the database is open.
std::string getName() override
Get the human-readable name of this backend.
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
void storeBatch(Batch const &batch) override
Store a group of objects.
MemoryBackend(size_t keyBytes, Section const &keyValues, beast::Journal journal)
int getWriteLoad() override
Estimate the number of write operations pending.
std::string getName() const override
Retrieve the name of this factory.
MemoryDB & open(std::string const &path)
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.
Holds a collection of configuration values.
Definition BasicConfig.h:24
T empty(T... args)
T is_same_v
T make_tuple(T... args)
Status
Return codes from Backend operations.
void registerMemoryFactory(Manager &manager)
MemoryFactory * memoryFactory
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
T piecewise_construct
T push_back(T... args)
T reserve(T... args)
T reset(T... args)
T size(T... args)
std::map< uint256 const, std::shared_ptr< NodeObject > > table