1#include <xrpl/basics/contract.h>
2#include <xrpl/beast/core/LexicalCast.h>
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/nodestore/Factory.h>
5#include <xrpl/nodestore/Manager.h>
6#include <xrpl/nodestore/detail/DecodedBlob.h>
7#include <xrpl/nodestore/detail/EncodedBlob.h>
8#include <xrpl/nodestore/detail/codec.h>
10#include <boost/filesystem.hpp>
12#include <nudb/nudb.hpp>
56 Throw<std::runtime_error>(
"nodestore: Missing path in NuDB backend");
64 nudb::context& context,
76 Throw<std::runtime_error>(
"nodestore: Missing path in NuDB backend");
86 catch (nudb::system_error
const&)
106 open(
bool createIfMissing, uint64_t appType, uint64_t uid, uint64_t salt)
override
108 using namespace boost::filesystem;
113 "xrpl::NodeStore::NuDBBackend::open : database is already "
115 JLOG(
j_.
error()) <<
"database is already open";
119 auto const folder = path(
name_);
120 auto const dp = (folder /
"nudb.dat").
string();
121 auto const kp = (folder /
"nudb.key").
string();
122 auto const lp = (folder /
"nudb.log").
string();
126 create_directories(folder);
127 nudb::create<nudb::xxhasher>(
129 if (ec == nudb::errc::file_exists)
132 Throw<nudb::system_error>(ec);
134 db_.open(dp, kp, lp, ec);
136 Throw<nudb::system_error>(ec);
139 Throw<std::runtime_error>(
"nodestore: unknown appnum");
146 return db_.is_open();
150 open(
bool createIfMissing)
override
152 open(createIfMissing,
appnum, nudb::make_uid(), nudb::make_salt());
165 JLOG(
j_.
fatal()) <<
"NuBD close() failed: " << ec.message();
166 Throw<nudb::system_error>(ec);
171 boost::filesystem::remove_all(
name_, ec);
175 <<
"Filesystem remove_all of " <<
name_ <<
" failed with: " << ec.message();
189 [&hash, pno, &status](
void const* data,
std::size_t size) {
190 nudb::detail::buffer bf;
191 auto const result = nodeobject_decompress(data, size, bf);
192 DecodedBlob decoded(hash.data(), result.first, result.second);
193 if (!decoded.wasOk())
195 status = dataCorrupt;
198 *pno = decoded.createObject();
202 if (ec == nudb::error::key_not_found)
205 Throw<nudb::system_error>(ec);
214 for (
auto const& h : hashes)
217 Status const status = fetch(h, &nObj);
228 return {results,
ok};
236 nudb::detail::buffer bf;
238 db_.insert(e.
getKey(), result.first, result.second, ec);
239 if (ec && ec != nudb::error::key_exists)
240 Throw<nudb::system_error>(ec);
250 report.elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
261 for (
auto const& e :
batch)
263 report.elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
276 auto const dp = db_.dat_path();
277 auto const kp = db_.key_path();
278 auto const lp = db_.log_path();
283 Throw<nudb::system_error>(ec);
291 nudb::detail::buffer bf;
293 DecodedBlob decoded(key, result.first, result.second);
294 if (!decoded.
wasOk())
304 Throw<nudb::system_error>(ec);
305 db_.open(dp, kp, lp, ec);
307 Throw<nudb::system_error>(ec);
325 auto const dp = db_.dat_path();
326 auto const kp = db_.key_path();
327 auto const lp = db_.log_path();
331 Throw<nudb::system_error>(ec);
332 nudb::verify_info vi;
333 nudb::verify<nudb::xxhasher>(vi, dp, kp, 0, nudb::no_progress{}, ec);
335 Throw<nudb::system_error>(ec);
336 db_.open(dp, kp, lp, ec);
338 Throw<nudb::system_error>(ec);
351 using namespace boost::filesystem;
352 auto const folder = path(name);
353 auto const kp = (folder /
"nudb.key").
string();
355 std::size_t const defaultSize = nudb::block_size(kp);
359 if (!
get_if_exists(keyValues,
"nudb_block_size", blockSizeStr))
366 std::size_t const parsedBlockSize = beast::lexicalCastThrow<std::size_t>(blockSizeStr);
369 if (parsedBlockSize < 4096 || parsedBlockSize > 32768 ||
370 (parsedBlockSize & (parsedBlockSize - 1)) != 0)
373 s <<
"Invalid nudb_block_size: " << parsedBlockSize
374 <<
". Must be power of 2 between 4096 and 32768.";
375 Throw<std::runtime_error>(s.
str());
378 JLOG(journal.
info()) <<
"Using custom NuDB block size: " << parsedBlockSize <<
" bytes";
379 return parsedBlockSize;
384 s <<
"Invalid nudb_block_size value: " << blockSizeStr <<
". Error: " << e.
what();
385 Throw<std::runtime_error>(s.
str());
426 nudb::context& context,
430 keyBytes, keyValues,
burstSize, scheduler, context, journal);
A generic endpoint for log messages.
A backend used for the NodeStore.
Parsed key/value blob into NodeObject components.
std::shared_ptr< NodeObject > createObject()
Create a NodeObject from this data.
bool wasOk() const noexcept
Determine if the decoding was successful.
Convert a NodeObject from in-memory to database format.
void const * getKey() const noexcept
std::size_t getSize() const noexcept
void const * getData() const noexcept
Base class for backend factories.
Singleton for managing NodeStore factories and back ends.
virtual void insert(Factory &factory)=0
Add a factory.
NuDBBackend(size_t keyBytes, Section const &keyValues, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)
std::size_t const blockSize_
void verify() override
Perform consistency checks on database.
Status fetch(uint256 const &hash, std::shared_ptr< NodeObject > *pno) override
Fetch a single object.
void storeBatch(Batch const &batch) override
Store a group of objects.
int getWriteLoad() override
Estimate the number of write operations pending.
std::optional< std::size_t > getBlockSize() const override
Get the block size for backends that support it.
static std::size_t parseBlockSize(std::string const &name, Section const &keyValues, beast::Journal journal)
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.
void store(std::shared_ptr< NodeObject > const &no) override
Store a single object.
static constexpr std::uint64_t appnum
NuDBBackend(size_t keyBytes, Section const &keyValues, std::size_t burstSize, Scheduler &scheduler, nudb::context &context, beast::Journal journal)
std::string getName() override
Get the human-readable name of this backend.
void do_insert(std::shared_ptr< NodeObject > const &no)
void open(bool createIfMissing) override
Open the backend.
void open(bool createIfMissing, uint64_t appType, uint64_t uid, uint64_t salt) override
Open the backend.
std::size_t const burstSize_
void close() override
Close the backend.
void setDeletePath() override
Remove contents on disk upon destruction.
bool isOpen() override
Returns true is the database is open.
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
std::atomic< bool > deletePath_
std::unique_ptr< Backend > createInstance(size_t keyBytes, Section const &keyValues, std::size_t burstSize, Scheduler &scheduler, nudb::context &context, beast::Journal journal) override
Create an instance of this factory's backend.
NuDBFactory(Manager &manager)
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::string getName() const override
Retrieve the name of this factory.
Scheduling for asynchronous backend activity.
virtual void onBatchWrite(BatchWriteReport const &report)=0
Reports the completion of a batch write Allows the scheduler to monitor the node store's performance.
Holds a collection of configuration values.
void registerNuDBFactory(Manager &manager)
std::pair< void const *, std::size_t > nodeobject_compress(void const *in, std::size_t in_size, BufferFactory &&bf)
Status
Return codes from Backend operations.
std::pair< void const *, std::size_t > nodeobject_decompress(void const *in, std::size_t in_size, BufferFactory &&bf)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::error_code make_error_code(xrpl::TokenCodecErrc e)
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
@ open
We haven't closed our ledger yet, but others might have.
bool get_if_exists(Section const §ion, std::string const &name, T &v)
Contains information about a batch write operation.