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  , pCache_(std::make_shared<TaggedCache<uint256, NodeObject>>(
47  name,
50  stopwatch(),
51  j))
52  , nCache_(std::make_shared<KeyCache<uint256>>(
53  name,
54  stopwatch(),
57  , backend_(std::move(backend))
58  {
59  assert(backend_);
60  setParent(parent);
61  }
62 
63  ~DatabaseNodeImp() override
64  {
65  // Stop read threads in base before data members are destroyed
67  }
68 
70  getName() const override
71  {
72  return backend_->getName();
73  }
74 
76  getWriteLoad() const override
77  {
78  return backend_->getWriteLoad();
79  }
80 
81  void
82  import(Database& source) override
83  {
84  importInternal(*backend_.get(), source);
85  }
86 
87  void
88  store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t)
89  override;
90 
91  bool
92  asyncFetch(
93  uint256 const& hash,
94  std::uint32_t ledgerSeq,
95  std::shared_ptr<NodeObject>& nodeObject) override;
96 
97  bool
98  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
99  {
100  return Database::storeLedger(*srcLedger, backend_, pCache_, nCache_);
101  }
102 
104  {
105  // We prefer a client not fill our cache
106  // We don't want to push data out of the cache
107  // before it's retrieved
108  return pCache_->getTargetSize() / asyncDivider;
109  }
110 
111  float
112  getCacheHitRate() override
113  {
114  return pCache_->getHitRate();
115  }
116 
117  void
118  tune(int size, std::chrono::seconds age) override;
119 
120  void
121  sweep() override;
122 
123 private:
124  // Positive cache
126 
127  // Negative cache
129 
130  // Persistent key/value storage
132 
135  uint256 const& hash,
137  FetchReport& fetchReport) override;
138 
139  void
141  {
142  backend_->for_each(f);
143  }
144 };
145 
146 } // namespace NodeStore
147 } // namespace ripple
148 
149 #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:131
ripple::NodeStore::DatabaseNodeImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseNodeImp.h:76
ripple::NodeStore::cacheTargetSize
@ cacheTargetSize
Definition: nodestore/impl/Tuning.h:28
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:53
std::string
STL class.
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
ripple::NodeStore::DatabaseNodeImp::getDesiredAsyncReadCount
int getDesiredAsyncReadCount(std::uint32_t) override
Get the maximum number of async reads the node store prefers.
Definition: DatabaseNodeImp.h:103
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::chrono::seconds
ripple::NodeStore::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport) override
Definition: DatabaseNodeImp.cpp:74
ripple::NodeStore::DatabaseNodeImp
Definition: DatabaseNodeImp.h:29
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::tune
void tune(int size, std::chrono::seconds age) override
Set the maximum number of entries and maximum cache age for both caches.
Definition: DatabaseNodeImp.cpp:58
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:140
ripple::Stoppable::setParent
void setParent(Stoppable &parent)
Set the parent of this Stoppable.
Definition: Stoppable.cpp:43
ripple::NodeStore::DatabaseNodeImp::getCacheHitRate
float getCacheHitRate() override
Get the positive cache hits to total attempts ratio.
Definition: DatabaseNodeImp.h:112
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:119
ripple::NodeStore::DatabaseNodeImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseNodeImp.h:125
ripple::NodeStore::cacheTargetAge
constexpr std::chrono::seconds cacheTargetAge
Definition: nodestore/impl/Tuning.h:36
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:61
ripple::NodeStore::DatabaseNodeImp::operator=
DatabaseNodeImp & operator=(DatabaseNodeImp const &)=delete
ripple::NodeStore::DatabaseNodeImp::asyncFetch
bool asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::shared_ptr< NodeObject > &nodeObject) override
Fetch an object without waiting.
Definition: DatabaseNodeImp.cpp:42
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::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseNodeImp.h:128
ripple::NodeStore::Database::stopReadThreads
void stopReadThreads()
Definition: Database.cpp:93
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:98
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:67
std
STL namespace.
ripple::NodeObject
A simple object that the Ledger uses to store entries.
Definition: NodeObject.h:48
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::KeyCache
Maintains a cache of keys with no associated data.
Definition: KeyCache.h:43
ripple::NodeStore::DatabaseNodeImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseNodeImp.h:70
ripple::NodeStore::DatabaseNodeImp::~DatabaseNodeImp
~DatabaseNodeImp() override
Definition: DatabaseNodeImp.h:63
ripple::NodeStore::asyncDivider
@ asyncDivider
Definition: nodestore/impl/Tuning.h:32