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 = map_.emplace(
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:
80 size_t keyBytes,
81 Section const& keyValues,
82 beast::Journal journal)
83 : name_(get(keyValues, "path")), journal_(journal)
84 {
85 boost::ignore_unused(journal_); // Keep unused journal_ just in case.
86 if (name_.empty())
87 Throw<std::runtime_error>("Missing path in Memory backend");
88 }
89
90 ~MemoryBackend() override
91 {
92 close();
93 }
94
96 getName() override
97 {
98 return name_;
99 }
100
101 void
102 open(bool) override
103 {
105 }
106
107 bool
108 isOpen() override
109 {
110 return static_cast<bool>(db_);
111 }
112
113 void
114 close() override
115 {
116 db_ = nullptr;
117 }
118
119 //--------------------------------------------------------------------------
120
121 Status
122 fetch(void const* key, std::shared_ptr<NodeObject>* pObject) override
123 {
124 XRPL_ASSERT(
125 db_, "xrpl::NodeStore::MemoryBackend::fetch : non-null database");
126 uint256 const hash(uint256::fromVoid(key));
127
129
130 Map::iterator iter = db_->table.find(hash);
131 if (iter == db_->table.end())
132 {
133 pObject->reset();
134 return notFound;
135 }
136 *pObject = iter->second;
137 return ok;
138 }
139
142 {
144 results.reserve(hashes.size());
145 for (auto const& h : hashes)
146 {
148 Status status = fetch(h->begin(), &nObj);
149 if (status != ok)
150 results.push_back({});
151 else
152 results.push_back(nObj);
153 }
154
155 return {results, ok};
156 }
157
158 void
159 store(std::shared_ptr<NodeObject> const& object) override
160 {
161 XRPL_ASSERT(
162 db_, "xrpl::NodeStore::MemoryBackend::store : non-null database");
164 db_->table.emplace(object->getHash(), object);
165 }
166
167 void
168 storeBatch(Batch const& batch) override
169 {
170 for (auto const& e : batch)
171 store(e);
172 }
173
174 void
175 sync() override
176 {
177 }
178
179 void
181 {
182 XRPL_ASSERT(
183 db_,
184 "xrpl::NodeStore::MemoryBackend::for_each : non-null database");
185 for (auto const& e : db_->table)
186 f(e.second);
187 }
188
189 int
190 getWriteLoad() override
191 {
192 return 0;
193 }
194
195 void
196 setDeletePath() override
197 {
198 }
199
200 int
201 fdRequired() const override
202 {
203 return 0;
204 }
205};
206
207//------------------------------------------------------------------------------
208
209MemoryFactory::MemoryFactory(Manager& manager) : manager_(manager)
210{
211 manager_.insert(*this);
212}
213
216{
217 return "Memory";
218}
219
222 size_t keyBytes,
223 Section const& keyValues,
225 Scheduler& scheduler,
226 beast::Journal journal)
227{
228 return std::make_unique<MemoryBackend>(keyBytes, keyValues, journal);
229}
230
231} // namespace NodeStore
232} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
A backend used for the NodeStore.
Definition Backend.h:21
Base class for backend factories.
Definition Factory.h:17
Singleton for managing NodeStore factories and back ends.
Definition Manager.h:13
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:26
static base_uint fromVoid(void const *data)
Definition base_uint.h:300
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:6
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