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  std::string const& name,
40  Scheduler& scheduler,
41  int readThreads,
42  Stoppable& parent,
44  Section const& config,
46  : Database(name, parent, scheduler, readThreads, config, j)
47  , cache_(nullptr)
48  , backend_(std::move(backend))
49  {
50  std::optional<int> cacheSize, cacheAge;
51  if (config.exists("cache_size"))
52  {
53  cacheSize = get<int>(config, "cache_size");
54  if (cacheSize.value() < 0)
55  {
56  Throw<std::runtime_error>(
57  "Specified negative value for cache_size");
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  if (cacheSize || cacheAge)
70  {
71  if (!cacheSize || *cacheSize == 0)
72  cacheSize = 16384;
73  if (!cacheAge || *cacheAge == 0)
74  cacheAge = 5;
75  cache_ = std::make_shared<TaggedCache<uint256, NodeObject>>(
76  name,
77  cacheSize.value(),
78  std::chrono::minutes{cacheAge.value()},
79  stopwatch(),
80  j);
81  }
82  assert(backend_);
83  setParent(parent);
84  }
85 
86  ~DatabaseNodeImp() override
87  {
88  // Stop read threads in base before data members are destroyed
90  }
91 
93  getName() const override
94  {
95  return backend_->getName();
96  }
97 
99  getWriteLoad() const override
100  {
101  return backend_->getWriteLoad();
102  }
103 
104  void
105  import(Database& source) override
106  {
107  importInternal(*backend_.get(), source);
108  }
109 
110  void
111  store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t)
112  override;
113 
115  {
116  // only one database
117  return true;
118  }
119  void
120  sync() override
121  {
122  backend_->sync();
123  }
124 
126  fetchBatch(std::vector<uint256> const& hashes);
127 
128  bool
129  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
130  {
131  return Database::storeLedger(*srcLedger, backend_);
132  }
133 
134  void
135  sweep() override;
136 
137 private:
138  // Cache for database objects. This cache is not always initialized. Check
139  // for null before using.
141  // Persistent key/value storage
143 
146  uint256 const& hash,
148  FetchReport& fetchReport) override;
149 
150  void
152  {
153  backend_->for_each(f);
154  }
155 
157  getCounters() const override
158  {
159  return backend_->counters();
160  }
161 };
162 
163 } // namespace NodeStore
164 } // namespace ripple
165 
166 #endif
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::DatabaseNodeImp::backend_
std::shared_ptr< Backend > backend_
Definition: DatabaseNodeImp.h:142
ripple::NodeStore::DatabaseNodeImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseNodeImp.h:99
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:50
std::string
STL class.
std::shared_ptr
STL class.
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::chrono::minutes
ripple::NodeStore::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport) override
Definition: DatabaseNodeImp.cpp:47
ripple::NodeStore::DatabaseNodeImp
Definition: DatabaseNodeImp.h:30
ripple::Section::exists
bool exists(std::string const &name) const
Returns true if a key with the given name exists.
Definition: BasicConfig.cpp:107
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:157
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:151
ripple::Stoppable::setParent
void setParent(Stoppable &parent)
Set the parent of this Stoppable.
Definition: Stoppable.cpp:43
ripple::base_uint< 256 >
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:106
ripple::NodeStore::DatabaseNodeImp::cache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
Definition: DatabaseNodeImp.h:140
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:114
ripple::NodeStore::DatabaseNodeImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Definition: DatabaseNodeImp.cpp:28
ripple::NodeStore::Database::stopReadThreads
void stopReadThreads()
Definition: Database.cpp:78
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:129
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:40
std
STL namespace.
ripple::NodeStore::DatabaseNodeImp::fetchBatch
std::vector< std::shared_ptr< NodeObject > > fetchBatch(std::vector< uint256 > const &hashes)
Definition: DatabaseNodeImp.cpp:104
std::optional< int >
ripple::NodeStore::DatabaseNodeImp::DatabaseNodeImp
DatabaseNodeImp(std::string const &name, Scheduler &scheduler, int readThreads, Stoppable &parent, std::shared_ptr< Backend > backend, Section const &config, beast::Journal j)
Definition: DatabaseNodeImp.h:38
ripple::NodeStore::DatabaseNodeImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseNodeImp.h:93
ripple::NodeStore::DatabaseNodeImp::~DatabaseNodeImp
~DatabaseNodeImp() override
Definition: DatabaseNodeImp.h:86
ripple::NodeStore::DatabaseNodeImp::sync
void sync() override
Definition: DatabaseNodeImp.h:120