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(std::piecewise_construct, std::make_tuple(path), std::make_tuple());
50 MemoryDB& db = result.first->second;
51 if (db.open)
52 Throw<std::runtime_error>("already open");
53 return db;
54 }
55};
56
58
59void
61{
62 static MemoryFactory instance{manager};
63 memoryFactory = &instance;
64}
65
66//------------------------------------------------------------------------------
67
68class MemoryBackend : public Backend
69{
70private:
72
75 MemoryDB* db_{nullptr};
76
77public:
78 MemoryBackend(size_t keyBytes, Section const& keyValues, beast::Journal journal)
79 : name_(get(keyValues, "path")), journal_(journal)
80 {
81 boost::ignore_unused(journal_); // Keep unused journal_ just in case.
82 if (name_.empty())
83 Throw<std::runtime_error>("Missing path in Memory backend");
84 }
85
86 ~MemoryBackend() override
87 {
88 close();
89 }
90
92 getName() override
93 {
94 return name_;
95 }
96
97 void
98 open(bool) override
99 {
101 }
102
103 bool
104 isOpen() override
105 {
106 return static_cast<bool>(db_);
107 }
108
109 void
110 close() override
111 {
112 db_ = nullptr;
113 }
114
115 //--------------------------------------------------------------------------
116
117 Status
118 fetch(void const* key, std::shared_ptr<NodeObject>* pObject) override
119 {
120 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::fetch : non-null database");
121 uint256 const hash(uint256::fromVoid(key));
122
124
125 Map::iterator 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
137 {
139 results.reserve(hashes.size());
140 for (auto const& h : hashes)
141 {
143 Status status = fetch(h->begin(), &nObj);
144 if (status != ok)
145 results.push_back({});
146 else
147 results.push_back(nObj);
148 }
149
150 return {results, ok};
151 }
152
153 void
154 store(std::shared_ptr<NodeObject> const& object) override
155 {
156 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::store : non-null database");
158 db_->table.emplace(object->getHash(), object);
159 }
160
161 void
162 storeBatch(Batch const& batch) override
163 {
164 for (auto const& e : batch)
165 store(e);
166 }
167
168 void
169 sync() override
170 {
171 }
172
173 void
175 {
176 XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::for_each : non-null database");
177 for (auto const& e : db_->table)
178 f(e.second);
179 }
180
181 int
182 getWriteLoad() override
183 {
184 return 0;
185 }
186
187 void
188 setDeletePath() override
189 {
190 }
191
192 int
193 fdRequired() const override
194 {
195 return 0;
196 }
197};
198
199//------------------------------------------------------------------------------
200
201MemoryFactory::MemoryFactory(Manager& manager) : manager_(manager)
202{
203 manager_.insert(*this);
204}
205
208{
209 return "Memory";
210}
211
214 size_t keyBytes,
215 Section const& keyValues,
217 Scheduler& scheduler,
218 beast::Journal journal)
219{
220 return std::make_unique<MemoryBackend>(keyBytes, keyValues, journal);
221}
222
223} // namespace NodeStore
224} // 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:25
static base_uint fromVoid(void const *data)
Definition base_uint.h:292
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