rippled
Loading...
Searching...
No Matches
MemoryFactory.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/nodestore/Factory.h>
21#include <xrpld/nodestore/Manager.h>
22
23#include <xrpl/basics/contract.h>
24
25#include <boost/beast/core/string.hpp>
26#include <boost/core/ignore_unused.hpp>
27
28#include <map>
29#include <memory>
30#include <mutex>
31
32namespace ripple {
33namespace NodeStore {
34
43
44class MemoryFactory : public Factory
45{
46private:
49
50public:
52 ~MemoryFactory() override;
53
55 getName() const override;
56
59 size_t keyBytes,
60 Section const& keyValues,
62 Scheduler& scheduler,
63 beast::Journal journal) override;
64
66 open(std::string const& path)
67 {
69 auto const result = map_.emplace(
71 MemoryDB& db = result.first->second;
72 if (db.open)
73 Throw<std::runtime_error>("already open");
74 return db;
75 }
76};
77
79
80//------------------------------------------------------------------------------
81
82class MemoryBackend : public Backend
83{
84private:
86
89 MemoryDB* db_{nullptr};
90
91public:
93 size_t keyBytes,
94 Section const& keyValues,
95 beast::Journal journal)
96 : name_(get(keyValues, "path")), journal_(journal)
97 {
98 boost::ignore_unused(journal_); // Keep unused journal_ just in case.
99 if (name_.empty())
100 Throw<std::runtime_error>("Missing path in Memory backend");
101 }
102
103 ~MemoryBackend() override
104 {
105 close();
106 }
107
109 getName() override
110 {
111 return name_;
112 }
113
114 void
115 open(bool createIfMissing) override
116 {
118 }
119
120 bool
121 isOpen() override
122 {
123 return static_cast<bool>(db_);
124 }
125
126 void
127 close() override
128 {
129 db_ = nullptr;
130 }
131
132 //--------------------------------------------------------------------------
133
134 Status
135 fetch(void const* key, std::shared_ptr<NodeObject>* pObject) override
136 {
137 XRPL_ASSERT(
138 db_, "ripple::NodeStore::MemoryBackend::fetch : non-null database");
139 uint256 const hash(uint256::fromVoid(key));
140
142
143 Map::iterator iter = db_->table.find(hash);
144 if (iter == db_->table.end())
145 {
146 pObject->reset();
147 return notFound;
148 }
149 *pObject = iter->second;
150 return ok;
151 }
152
155 {
157 results.reserve(hashes.size());
158 for (auto const& h : hashes)
159 {
161 Status status = fetch(h->begin(), &nObj);
162 if (status != ok)
163 results.push_back({});
164 else
165 results.push_back(nObj);
166 }
167
168 return {results, ok};
169 }
170
171 void
172 store(std::shared_ptr<NodeObject> const& object) override
173 {
174 XRPL_ASSERT(
175 db_, "ripple::NodeStore::MemoryBackend::store : non-null database");
177 db_->table.emplace(object->getHash(), object);
178 }
179
180 void
181 storeBatch(Batch const& batch) override
182 {
183 for (auto const& e : batch)
184 store(e);
185 }
186
187 void
188 sync() override
189 {
190 }
191
192 void
194 {
195 XRPL_ASSERT(
196 db_,
197 "ripple::NodeStore::MemoryBackend::for_each : non-null database");
198 for (auto const& e : db_->table)
199 f(e.second);
200 }
201
202 int
203 getWriteLoad() override
204 {
205 return 0;
206 }
207
208 void
209 setDeletePath() override
210 {
211 }
212
213 int
214 fdRequired() const override
215 {
216 return 0;
217 }
218};
219
220//------------------------------------------------------------------------------
221
226
231
234{
235 return "Memory";
236}
237
240 size_t keyBytes,
241 Section const& keyValues,
243 Scheduler& scheduler,
244 beast::Journal journal)
245{
246 return std::make_unique<MemoryBackend>(keyBytes, keyValues, journal);
247}
248
249} // namespace NodeStore
250} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
A backend used for the NodeStore.
Definition Backend.h:40
Base class for backend factories.
Definition Factory.h:37
static Manager & instance()
Returns the instance of the manager singleton.
virtual void erase(Factory &factory)=0
Remove a factory.
virtual void insert(Factory &factory)=0
Add a factory.
void close() override
Close the backend.
void store(std::shared_ptr< NodeObject > const &object) override
Store a single object.
void open(bool createIfMissing) override
Open the backend.
std::string getName() override
Get the human-readable name of this backend.
std::pair< std::vector< std::shared_ptr< NodeObject > >, Status > fetchBatch(std::vector< uint256 const * > const &hashes) override
Fetch a batch synchronously.
MemoryBackend(size_t keyBytes, Section const &keyValues, beast::Journal journal)
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
int getWriteLoad() override
Estimate the number of write operations pending.
void storeBatch(Batch const &batch) override
Store a group of objects.
void setDeletePath() override
Remove contents on disk upon destruction.
int fdRequired() const override
Returns the number of file descriptors the backend expects to need.
Status fetch(void const *key, std::shared_ptr< NodeObject > *pObject) override
Fetch a single object.
bool isOpen() override
Returns true is the database is open.
std::string getName() const override
Retrieve the name of this factory.
std::map< std::string, MemoryDB, boost::beast::iless > map_
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.
MemoryDB & open(std::string const &path)
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:45
static base_uint fromVoid(void const *data)
Definition base_uint.h:319
T empty(T... args)
T is_same_v
T make_tuple(T... args)
static MemoryFactory memoryFactory
Status
Return codes from Backend operations.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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