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/chrono.h>
24 #include <ripple/nodestore/Database.h>
25 
26 namespace ripple {
27 namespace NodeStore {
28 
29 class DatabaseNodeImp : public Database
30 {
31 public:
32  DatabaseNodeImp() = delete;
33  DatabaseNodeImp(DatabaseNodeImp const&) = delete;
35  operator=(DatabaseNodeImp const&) = delete;
36 
38  std::string const& name,
39  Scheduler& scheduler,
40  int readThreads,
41  Stoppable& parent,
43  Section const& config,
45  : Database(name, parent, scheduler, readThreads, config, j)
46  , cache_(nullptr)
47  , backend_(std::move(backend))
48  {
49  std::optional<int> cacheSize, cacheAge;
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  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  if (cacheSize || cacheAge)
69  {
70  if (!cacheSize || *cacheSize == 0)
71  cacheSize = 16384;
72  if (!cacheAge || *cacheAge == 0)
73  cacheAge = 5;
74  cache_ = std::make_shared<TaggedCache<uint256, NodeObject>>(
75  name,
76  cacheSize.value(),
77  std::chrono::minutes{cacheAge.value()},
78  stopwatch(),
79  j);
80  }
81  assert(backend_);
82  setParent(parent);
83  }
84 
85  ~DatabaseNodeImp() override
86  {
87  // Stop read threads in base before data members are destroyed
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  import(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 
114  {
115  // only one database
116  return true;
117  }
118  void
119  sync() override
120  {
121  backend_->sync();
122  }
123 
125  fetchBatch(std::vector<uint256> const& hashes);
126 
127  bool
128  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
129  {
130  return Database::storeLedger(*srcLedger, backend_);
131  }
132 
133  void
134  sweep() override;
135 
136  Backend&
137  getBackend() override
138  {
139  return *backend_;
140  };
141 
142 private:
143  // Cache for database objects. This cache is not always initialized. Check
144  // for null before using.
146  // Persistent key/value storage
148 
151  uint256 const& hash,
153  FetchReport& fetchReport) override;
154 
155  void
157  {
158  backend_->for_each(f);
159  }
160 };
161 
162 } // namespace NodeStore
163 } // namespace ripple
164 
165 #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:147
ripple::NodeStore::DatabaseNodeImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseNodeImp.h:98
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:29
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::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:86
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:156
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:113
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:128
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::getBackend
Backend & getBackend() override
Definition: DatabaseNodeImp.h:137
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:37
ripple::NodeStore::DatabaseNodeImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseNodeImp.h:92
ripple::NodeStore::DatabaseNodeImp::~DatabaseNodeImp
~DatabaseNodeImp() override
Definition: DatabaseNodeImp.h:85
ripple::NodeStore::DatabaseNodeImp::sync
void sync() override
Definition: DatabaseNodeImp.h:119
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:39