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