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#include <xrpl/basics/TaggedCache.h>
25#include <xrpl/basics/chrono.h>
26
27namespace ripple {
28namespace NodeStore {
29
31{
32public:
33 DatabaseNodeImp() = delete;
36 operator=(DatabaseNodeImp const&) = delete;
37
39 Scheduler& scheduler,
40 int readThreads,
42 Section const& config,
44 : Database(scheduler, readThreads, config, j)
45 , backend_(std::move(backend))
46 {
47 std::optional<int> cacheSize, cacheAge;
48
49 if (config.exists("cache_size"))
50 {
51 cacheSize = get<int>(config, "cache_size");
52 if (cacheSize.value() < 0)
53 {
54 Throw<std::runtime_error>(
55 "Specified negative value for cache_size");
56 }
57 }
58
59 if (config.exists("cache_age"))
60 {
61 cacheAge = get<int>(config, "cache_age");
62 if (cacheAge.value() < 0)
63 {
64 Throw<std::runtime_error>(
65 "Specified negative value for cache_age");
66 }
67 }
68
69 if (cacheSize != 0 || cacheAge != 0)
70 {
71 cache_ = std::make_shared<TaggedCache<uint256, NodeObject>>(
72 "DatabaseNodeImp",
73 cacheSize.value_or(0),
74 std::chrono::minutes(cacheAge.value_or(0)),
75 stopwatch(),
76 j);
77 }
78
79 XRPL_ASSERT(
81 "ripple::NodeStore::DatabaseNodeImp::DatabaseNodeImp : non-null "
82 "backend");
83 }
84
86 {
87 stop();
88 }
89
91 getName() const override
92 {
93 return backend_->getName();
94 }
95
97 getWriteLoad() const override
98 {
99 return backend_->getWriteLoad();
100 }
101
102 void
103 importDatabase(Database& source) override
104 {
105 importInternal(*backend_.get(), source);
106 }
107
108 void
109 store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t)
110 override;
111
112 bool
114 {
115 // only one database
116 return true;
117 }
118
119 void
120 sync() override
121 {
122 backend_->sync();
123 }
124
126 fetchBatch(std::vector<uint256> const& hashes);
127
128 void
130 uint256 const& hash,
131 std::uint32_t ledgerSeq,
132 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback)
133 override;
134
135 void
136 sweep() override;
137
138private:
139 // Cache for database objects. This cache is not always initialized. Check
140 // for null before using.
142 // Persistent key/value storage
144
147 uint256 const& hash,
149 FetchReport& fetchReport,
150 bool duplicate) override;
151
152 void
154 {
155 backend_->for_each(f);
156 }
157};
158
159} // namespace NodeStore
160} // namespace ripple
161
162#endif
A generic endpoint for log messages.
Definition: Journal.h:59
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:50
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:198
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition: BasicConfig.h:46
bool exists(std::string const &name) const
Returns true if a key with the given name exists.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:120
STL namespace.
Contains information about a fetch operation.
T value(T... args)
T value_or(T... args)