xrpld
Loading...
Searching...
No Matches
DatabaseNodeImp.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/TaggedCache.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/config/BasicConfig.h>
11#include <xrpl/config/Constants.h>
12#include <xrpl/nodestore/Backend.h>
13#include <xrpl/nodestore/Database.h>
14#include <xrpl/nodestore/NodeObject.h>
15#include <xrpl/nodestore/Scheduler.h>
16
17#include <chrono>
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <optional>
22#include <stdexcept>
23#include <utility>
24
25namespace xrpl::node_store {
26
28{
29public:
30 DatabaseNodeImp() = delete;
33 operator=(DatabaseNodeImp const&) = delete;
34
36 Scheduler& scheduler,
37 int readThreads,
39 Section const& config,
41 : Database(scheduler, readThreads, config, j), backend_(std::move(backend))
42 {
43 std::optional<int> cacheSize, cacheAge;
44
45 if (config.exists(Keys::kCacheSize))
46 {
47 cacheSize = get<int>(config, Keys::kCacheSize);
48 if (cacheSize.value() < 0)
49 Throw<std::runtime_error>("Specified negative value for cache_size");
50 }
51
52 if (config.exists(Keys::kCacheAge))
53 {
54 cacheAge = get<int>(config, Keys::kCacheAge);
55 if (cacheAge.value() < 0)
56 Throw<std::runtime_error>("Specified negative value for cache_age");
57 }
58
59 if (cacheSize.has_value() || cacheAge.has_value())
60 {
61 cache_ = std::make_shared<TaggedCache<uint256, NodeObject>>(
62 "DatabaseNodeImp",
63 cacheSize.value_or(0),
64 std::chrono::minutes(cacheAge.value_or(0)),
65 stopwatch(),
66 j);
67 }
68
69 XRPL_ASSERT(
71 "xrpl::node_store::DatabaseNodeImp::DatabaseNodeImp : non-null "
72 "backend");
73 }
74
76 {
77 stop();
78 }
79
81 getName() const override
82 {
83 return backend_->getName();
84 }
85
87 getWriteLoad() const override
88 {
89 return backend_->getWriteLoad();
90 }
91
92 void
93 importDatabase(Database& source) override
94 {
95 importInternal(*backend_.get(), source);
96 }
97
98 void
99 store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t) override;
100
101 bool
103 {
104 // only one database
105 return true;
106 }
107
108 void
109 sync() override
110 {
111 backend_->sync();
112 }
113
114 void
116 uint256 const& hash,
117 std::uint32_t ledgerSeq,
118 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback) override;
119
120 void
121 sweep() override;
122
123private:
124 // Cache for database objects. This cache is not always initialized. Check
125 // for null before using.
127 // Persistent key/value storage
129
131 fetchNodeObject(uint256 const& hash, std::uint32_t, FetchReport& fetchReport, bool duplicate)
132 override;
133
134 void
136 {
137 backend_->forEach(f);
138 }
139};
140
141} // namespace xrpl::node_store
A generic endpoint for log messages.
Definition Journal.h:44
Holds a collection of configuration values.
Definition BasicConfig.h:29
bool exists(std::string const &name) const
Returns true if a key with the given name exists.
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
void sweep() override
Remove expired entries from the positive and negative caches.
void importDatabase(Database &source) override
Import objects from another database.
DatabaseNodeImp(Scheduler &scheduler, int readThreads, std::shared_ptr< Backend > backend, Section const &config, beast::Journal j)
bool isSameDB(std::uint32_t, std::uint32_t) override
Check if two ledgers are in the same database.
std::shared_ptr< Backend > backend_
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
DatabaseNodeImp & operator=(DatabaseNodeImp const &)=delete
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback) override
Fetch an object without waiting.
std::string getName() const override
Retrieve the name associated with this backend.
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport, bool duplicate) override
void forEach(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
DatabaseNodeImp(DatabaseNodeImp const &)=delete
void importInternal(Backend &dstBackend, Database &srcDB)
Scheduling for asynchronous backend activity.
STL namespace.
NodeObjectType
The types of node objects.
Definition NodeObject.h:18
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
BaseUInt< 256 > uint256
Definition base_uint.h:580
T has_value(T... args)
static constexpr auto kCacheSize
Definition Constants.h:99
static constexpr auto kCacheAge
Definition Constants.h:97
Contains information about a fetch operation.