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 {
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(void const* key, std::shared_ptr<NodeObject>* pObject) override
120 {
121 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::fetch : non-null database");
122 uint256 const hash(uint256::fromVoid(key));
123
125
126 Map::iterator iter = db_->table.find(hash);
127 if (iter == db_->table.end())
128 {
129 pObject->reset();
130 return notFound;
131 }
132 *pObject = iter->second;
133 return ok;
134 }
135
138 {
140 results.reserve(hashes.size());
141 for (auto const& h : hashes)
142 {
144 Status status = fetch(h->begin(), &nObj);
145 if (status != ok)
146 results.push_back({});
147 else
148 results.push_back(nObj);
149 }
150
151 return {results, ok};
152 }
153
154 void
155 store(std::shared_ptr<NodeObject> const& object) override
156 {
157 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::store : non-null database");
159 db_->table.emplace(object->getHash(), object);
160 }
161
162 void
163 storeBatch(Batch const& batch) override
164 {
165 for (auto const& e : batch)
166 store(e);
167 }
168
169 void
170 sync() override
171 {
172 }
173
174 void
176 {
177 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::for_each : non-null database");
178 for (auto const& e : db_->table)
179 f(e.second);
180 }
181
182 int
183 getWriteLoad() override
184 {
185 return 0;
186 }
187
188 void
189 setDeletePath() override
190 {
191 }
192
193 int
194 fdRequired() const override
195 {
196 return 0;
197 }
198};
199
200//------------------------------------------------------------------------------
201
202MemoryFactory::MemoryFactory(Manager& manager) : manager_(manager)
203{
204 manager_.insert(*this);
205}
206
209{
210 return "Memory";
211}
212
215 size_t keyBytes,
216 Section const& keyValues,
218 Scheduler& scheduler,
219 beast::Journal journal)
220{
221 return std::make_unique<MemoryBackend>(keyBytes, keyValues, journal);
222}
223
224} // namespace NodeStore
225} // 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.
beast::Journal const journal_
void close() override
Close the backend.
Status fetch(void const *key, std::shared_ptr< NodeObject > *pObject) override
Fetch a single object.
void open(bool) override
Open the backend.
std::pair< std::vector< std::shared_ptr< NodeObject > >, Status > fetchBatch(std::vector< uint256 const * > 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
static base_uint fromVoid(void const *data)
Definition base_uint.h:293
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