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/nodestore/Database.h>
24 #include <ripple/basics/chrono.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;
34  DatabaseNodeImp& operator=(DatabaseNodeImp const&) = delete;
35 
37  std::string const& name,
38  Scheduler& scheduler,
39  int readThreads,
40  Stoppable& parent,
42  Section const& config,
44  : Database(name, parent, scheduler, readThreads, config, j)
45  , pCache_(std::make_shared<TaggedCache<uint256, NodeObject>>(
47  , nCache_(std::make_shared<KeyCache<uint256>>(
49  , backend_(std::move(backend))
50  {
51  assert(backend_);
52  setParent(parent);
53  }
54 
55  ~DatabaseNodeImp() override
56  {
57  // Stop threads before data members are destroyed.
58  stopThreads();
59  }
60 
62  getName() const override
63  {
64  return backend_->getName();
65  }
66 
68  getWriteLoad() const override
69  {
70  return backend_->getWriteLoad();
71  }
72 
73  void
74  import(Database& source) override
75  {
76  importInternal(*backend_.get(), source);
77  }
78 
79  void
80  store(NodeObjectType type, Blob&& data,
81  uint256 const& hash, std::uint32_t seq) override;
82 
84  fetch(uint256 const& hash, std::uint32_t seq) override
85  {
86  return doFetch(hash, seq, *pCache_, *nCache_, false);
87  }
88 
89  bool
90  asyncFetch(uint256 const& hash, std::uint32_t seq,
91  std::shared_ptr<NodeObject>& object) override;
92 
93  bool
94  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
95  {
96  return Database::storeLedger(
97  *srcLedger, backend_, pCache_, nCache_, nullptr);
98  }
99 
100  int
102  {
103  // We prefer a client not fill our cache
104  // We don't want to push data out of the cache
105  // before it's retrieved
106  return pCache_->getTargetSize() / asyncDivider;
107  }
108 
109  float
110  getCacheHitRate() override {return pCache_->getHitRate();}
111 
112  void
113  tune(int size, std::chrono::seconds age) override;
114 
115  void
116  sweep() override;
117 
118 private:
119  // Positive cache
121 
122  // Negative cache
124 
125  // Persistent key/value storage
127 
129  fetchFrom(uint256 const& hash, std::uint32_t seq) override
130  {
131  return fetchInternal(hash, backend_);
132  }
133 
134  void
136  {
137  backend_->for_each(f);
138  }
139 };
140 
141 }
142 }
143 
144 #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:126
ripple::NodeStore::DatabaseNodeImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseNodeImp.h:68
ripple::NodeStore::DatabaseNodeImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t seq) override
Store the object.
Definition: DatabaseNodeImp.cpp:28
ripple::NodeStore::cacheTargetSize
@ cacheTargetSize
Definition: nodestore/impl/Tuning.h:29
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:55
ripple::NodeStore::Database::doFetch
std::shared_ptr< NodeObject > doFetch(uint256 const &hash, std::uint32_t seq, TaggedCache< uint256, NodeObject > &pCache, KeyCache< uint256 > &nCache, bool isAsync)
Definition: Database.cpp:189
ripple::NodeStore::DatabaseNodeImp::fetchFrom
std::shared_ptr< NodeObject > fetchFrom(uint256 const &hash, std::uint32_t seq) override
Definition: DatabaseNodeImp.h:129
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::chrono::seconds
ripple::NodeStore::DatabaseNodeImp
Definition: DatabaseNodeImp.h:29
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:87
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:52
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:135
ripple::Stoppable::setParent
void setParent(Stoppable &parent)
Set the parent of this Stoppable.
Definition: Stoppable.cpp:46
ripple::NodeStore::DatabaseNodeImp::getCacheHitRate
float getCacheHitRate() override
Get the positive cache hits to total attempts ratio.
Definition: DatabaseNodeImp.h:110
ripple::base_uint< 256 >
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:161
ripple::NodeStore::DatabaseNodeImp::getDesiredAsyncReadCount
int getDesiredAsyncReadCount(std::uint32_t seq) override
Get the maximum number of async reads the node store prefers.
Definition: DatabaseNodeImp.h:101
ripple::NodeStore::DatabaseNodeImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseNodeImp.h:120
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:60
std::int32_t
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:57
ripple::NodeStore::DatabaseNodeImp::operator=
DatabaseNodeImp & operator=(DatabaseNodeImp const &)=delete
ripple::NodeStore::Database::fetchInternal
std::shared_ptr< NodeObject > fetchInternal(uint256 const &hash, std::shared_ptr< Backend > backend)
Definition: Database.cpp:123
ripple::NodeStore::DatabaseNodeImp::asyncFetch
bool asyncFetch(uint256 const &hash, std::uint32_t seq, std::shared_ptr< NodeObject > &object) override
Fetch an object without waiting.
Definition: DatabaseNodeImp.cpp:39
ripple::NodeStore::DatabaseNodeImp::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseNodeImp.h:123
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
Copies a ledger stored in a different database to this one.
ripple::NodeStore::DatabaseNodeImp::storeLedger
bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger) override
Copies a ledger stored in a different database to this one.
Definition: DatabaseNodeImp.h:94
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:61
ripple::NodeStore::Database::stopThreads
void stopThreads()
Definition: Database.cpp:95
std
STL namespace.
ripple::NodeStore::DatabaseNodeImp::fetch
std::shared_ptr< NodeObject > fetch(uint256 const &hash, std::uint32_t seq) override
Fetch an object.
Definition: DatabaseNodeImp.h:84
ripple::NodeObject
A simple object that the Ledger uses to store entries.
Definition: NodeObject.h:50
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:36
ripple::KeyCache
Maintains a cache of keys with no associated data.
Definition: KeyCache.h:44
ripple::NodeStore::DatabaseNodeImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseNodeImp.h:62
ripple::NodeStore::DatabaseNodeImp::~DatabaseNodeImp
~DatabaseNodeImp() override
Definition: DatabaseNodeImp.h:55
ripple::NodeStore::asyncDivider
@ asyncDivider
Definition: nodestore/impl/Tuning.h:32