rippled
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 <ripple/basics/TaggedCache.h>
24 #include <ripple/basics/chrono.h>
25 #include <ripple/nodestore/Database.h>
26 
27 namespace ripple {
28 namespace NodeStore {
29 
30 class DatabaseNodeImp : public Database
31 {
32 public:
33  DatabaseNodeImp() = delete;
34  DatabaseNodeImp(DatabaseNodeImp const&) = 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  assert(backend_);
80  }
81 
83  {
84  stop();
85  }
86 
88  getName() const override
89  {
90  return backend_->getName();
91  }
92 
94  getWriteLoad() const override
95  {
96  return backend_->getWriteLoad();
97  }
98 
99  void
100  importDatabase(Database& source) override
101  {
102  importInternal(*backend_.get(), source);
103  }
104 
105  void
106  store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t)
107  override;
108 
110  {
111  // only one database
112  return true;
113  }
114 
115  void
116  sync() override
117  {
118  backend_->sync();
119  }
120 
122  fetchBatch(std::vector<uint256> const& hashes);
123 
124  void
125  asyncFetch(
126  uint256 const& hash,
127  std::uint32_t ledgerSeq,
128  std::function<void(std::shared_ptr<NodeObject> const&)>&& callback)
129  override;
130 
131  bool
132  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
133  {
134  return Database::storeLedger(*srcLedger, backend_);
135  }
136 
137  void
138  sweep() override;
139 
140 private:
141  // Cache for database objects. This cache is not always initialized. Check
142  // for null before using.
144  // Persistent key/value storage
146 
149  uint256 const& hash,
151  FetchReport& fetchReport,
152  bool duplicate) override;
153 
154  void
156  {
157  backend_->for_each(f);
158  }
159 
161  getCounters() const override
162  {
163  return backend_->counters();
164  }
165 };
166 
167 } // namespace NodeStore
168 } // namespace ripple
169 
170 #endif
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:42
ripple::NodeStore::DatabaseNodeImp::backend_
std::shared_ptr< Backend > backend_
Definition: DatabaseNodeImp.h:145
ripple::NodeStore::DatabaseNodeImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseNodeImp.h:94
ripple::NodeStore::DatabaseNodeImp::importDatabase
void importDatabase(Database &source) override
Import objects from another database.
Definition: DatabaseNodeImp.h:100
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:51
std::string
STL class.
std::shared_ptr
STL class.
ripple::NodeStore::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport, bool duplicate) override
Definition: DatabaseNodeImp.cpp:74
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::optional::value_or
T value_or(T... args)
std::chrono::minutes
ripple::NodeStore::DatabaseNodeImp
Definition: DatabaseNodeImp.h:30
ripple::NodeStore::Database::stop
virtual void stop()
Definition: Database.cpp:165
ripple::Section::exists
bool exists(std::string const &name) const
Returns true if a key with the given name exists.
Definition: BasicConfig.cpp:105
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:32
ripple::NodeStore::DatabaseNodeImp::getCounters
std::optional< Backend::Counters< std::uint64_t > > getCounters() const override
Retrieve backend read and write stats.
Definition: DatabaseNodeImp.h:161
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:88
std::function
ripple::NodeStore::DatabaseNodeImp::for_each
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
Definition: DatabaseNodeImp.h:155
ripple::base_uint< 256 >
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:213
ripple::NodeStore::DatabaseNodeImp::cache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
Definition: DatabaseNodeImp.h:143
ripple::NodeStore::DatabaseNodeImp::DatabaseNodeImp
DatabaseNodeImp()=delete
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::int32_t
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::NodeStore::DatabaseNodeImp::operator=
DatabaseNodeImp & operator=(DatabaseNodeImp const &)=delete
ripple::NodeStore::DatabaseNodeImp::isSameDB
bool isSameDB(std::uint32_t, std::uint32_t) override
Definition: DatabaseNodeImp.h:109
ripple::NodeStore::DatabaseNodeImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
Definition: DatabaseNodeImp.cpp:28
ripple::NodeStore::DatabaseNodeImp::~DatabaseNodeImp
~DatabaseNodeImp()
Definition: DatabaseNodeImp.h:82
std::optional::value
T value(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Database::storeLedger
virtual bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger)=0
Store a ledger from a different database.
ripple::NodeStore::DatabaseNodeImp::storeLedger
bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger) override
Store a ledger from a different database.
Definition: DatabaseNodeImp.h:132
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:67
std
STL namespace.
ripple::NodeStore::DatabaseNodeImp::fetchBatch
std::vector< std::shared_ptr< NodeObject > > fetchBatch(std::vector< uint256 > const &hashes)
Definition: DatabaseNodeImp.cpp:147
ripple::NodeStore::DatabaseNodeImp::DatabaseNodeImp
DatabaseNodeImp(Scheduler &scheduler, int readThreads, std::shared_ptr< Backend > backend, Section const &config, beast::Journal j)
Definition: DatabaseNodeImp.h:38
std::optional< int >
ripple::NodeStore::DatabaseNodeImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseNodeImp.h:88
ripple::NodeStore::DatabaseNodeImp::asyncFetch
void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback) override
Fetch an object without waiting.
Definition: DatabaseNodeImp.cpp:49
ripple::NodeStore::DatabaseNodeImp::sync
void sync() override
Definition: DatabaseNodeImp.h:116