rippled
Loading...
Searching...
No Matches
DatabaseNodeImp.h
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#ifndef RIPPLE_NODESTORE_DATABASENODEIMP_H_INCLUDED
21#define RIPPLE_NODESTORE_DATABASENODEIMP_H_INCLUDED
22
23#include <xrpld/nodestore/Database.h>
24
25#include <xrpl/basics/TaggedCache.h>
26#include <xrpl/basics/chrono.h>
27
28namespace ripple {
29namespace NodeStore {
30
32{
33public:
34 DatabaseNodeImp() = delete;
37 operator=(DatabaseNodeImp const&) = delete;
38
40 Scheduler& scheduler,
41 int readThreads,
43 Section const& config,
45 : Database(scheduler, readThreads, config, j)
46 , backend_(std::move(backend))
47 {
48 std::optional<int> cacheSize, cacheAge;
49
50 if (config.exists("cache_size"))
51 {
52 cacheSize = get<int>(config, "cache_size");
53 if (cacheSize.value() < 0)
54 {
55 Throw<std::runtime_error>(
56 "Specified negative value for cache_size");
57 }
58 }
59
60 if (config.exists("cache_age"))
61 {
62 cacheAge = get<int>(config, "cache_age");
63 if (cacheAge.value() < 0)
64 {
65 Throw<std::runtime_error>(
66 "Specified negative value for cache_age");
67 }
68 }
69
70 if (cacheSize != 0 || cacheAge != 0)
71 {
73 "DatabaseNodeImp",
74 cacheSize.value_or(0),
75 std::chrono::minutes(cacheAge.value_or(0)),
76 stopwatch(),
77 j);
78 }
79
80 XRPL_ASSERT(
82 "ripple::NodeStore::DatabaseNodeImp::DatabaseNodeImp : non-null "
83 "backend");
84 }
85
87 {
88 stop();
89 }
90
92 getName() const override
93 {
94 return backend_->getName();
95 }
96
98 getWriteLoad() const override
99 {
100 return backend_->getWriteLoad();
101 }
102
103 void
104 importDatabase(Database& source) override
105 {
106 importInternal(*backend_.get(), source);
107 }
108
109 void
110 store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t)
111 override;
112
113 bool
115 {
116 // only one database
117 return true;
118 }
119
120 void
121 sync() override
122 {
123 backend_->sync();
124 }
125
127 fetchBatch(std::vector<uint256> const& hashes);
128
129 void
131 uint256 const& hash,
132 std::uint32_t ledgerSeq,
133 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback)
134 override;
135
136 void
137 sweep() override;
138
139private:
140 // Cache for database objects. This cache is not always initialized. Check
141 // for null before using.
143 // Persistent key/value storage
145
148 uint256 const& hash,
150 FetchReport& fetchReport,
151 bool duplicate) override;
152
153 void
155 {
156 backend_->for_each(f);
157 }
158};
159
160} // namespace NodeStore
161} // namespace ripple
162
163#endif
A generic endpoint for log messages.
Definition Journal.h:60
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
std::shared_ptr< Backend > backend_
std::string getName() const override
Retrieve the name associated with this backend.
void importDatabase(Database &source) override
Import objects from another database.
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
DatabaseNodeImp(DatabaseNodeImp const &)=delete
std::vector< std::shared_ptr< NodeObject > > fetchBatch(std::vector< uint256 > const &hashes)
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
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 asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback) override
Fetch an object without waiting.
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
DatabaseNodeImp & operator=(DatabaseNodeImp const &)=delete
void sweep() override
Remove expired entries from the positive and negative caches.
Persistency layer for NodeObject.
Definition Database.h:52
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:200
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:45
bool exists(std::string const &name) const
Returns true if a key with the given name exists.
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
NodeObjectType
The types of node objects.
Definition NodeObject.h:32
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:119
STL namespace.
Contains information about a fetch operation.
T value(T... args)
T value_or(T... args)