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  if (config.exists("cache_size"))
49  {
50  cacheSize = get<int>(config, "cache_size");
51  if (cacheSize.value() < 0)
52  {
53  Throw<std::runtime_error>(
54  "Specified negative value for cache_size");
55  }
56  }
57  if (config.exists("cache_age"))
58  {
59  cacheAge = get<int>(config, "cache_age");
60  if (cacheAge.value() < 0)
61  {
62  Throw<std::runtime_error>(
63  "Specified negative value for cache_age");
64  }
65  }
66  if (cacheSize || cacheAge)
67  {
68  if (!cacheSize || *cacheSize == 0)
69  cacheSize = 16384;
70  if (!cacheAge || *cacheAge == 0)
71  cacheAge = 5;
72  cache_ = std::make_shared<TaggedCache<uint256, NodeObject>>(
73  "DatabaseNodeImp",
74  cacheSize.value(),
75  std::chrono::minutes{cacheAge.value()},
76  stopwatch(),
77  j);
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  void
115  sync() override
116  {
117  backend_->sync();
118  }
119 
121  fetchBatch(std::vector<uint256> const& hashes);
122 
123  bool
124  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
125  {
126  return Database::storeLedger(*srcLedger, backend_);
127  }
128 
129  void
130  sweep() override;
131 
132 private:
133  // Cache for database objects. This cache is not always initialized. Check
134  // for null before using.
136  // Persistent key/value storage
138 
141  uint256 const& hash,
143  FetchReport& fetchReport) override;
144 
145  void
147  {
148  backend_->for_each(f);
149  }
150 
152  getCounters() const override
153  {
154  return backend_->counters();
155  }
156 };
157 
158 } // namespace NodeStore
159 } // namespace ripple
160 
161 #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:137
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:52
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::NodeStore::Database::stop
virtual void stop()
Definition: Database.cpp:89
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:152
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:146
ripple::base_uint< 256 >
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:119
ripple::NodeStore::DatabaseNodeImp::cache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
Definition: DatabaseNodeImp.h:135
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:124
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
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::sync
void sync() override
Definition: DatabaseNodeImp.h:115