rippled
CachedView.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_LEDGER_CACHEDVIEW_H_INCLUDED
21 #define RIPPLE_LEDGER_CACHEDVIEW_H_INCLUDED
22 
23 #include <ripple/basics/hardened_hash.h>
24 #include <ripple/ledger/CachedSLEs.h>
25 #include <ripple/ledger/ReadView.h>
26 #include <map>
27 #include <memory>
28 #include <mutex>
29 #include <type_traits>
30 
31 namespace ripple {
32 
33 namespace detail {
34 
36 {
37 private:
40  std::mutex mutable mutex_;
42  key_type,
45 
46 public:
47  CachedViewImpl() = delete;
48  CachedViewImpl(CachedViewImpl const&) = delete;
50  operator=(CachedViewImpl const&) = delete;
51 
53  : base_(*base), cache_(cache)
54  {
55  }
56 
57  //
58  // ReadView
59  //
60 
61  bool
62  exists(Keylet const& k) const override;
63 
65  read(Keylet const& k) const override;
66 
67  bool
68  open() const override
69  {
70  return base_.open();
71  }
72 
73  LedgerInfo const&
74  info() const override
75  {
76  return base_.info();
77  }
78 
79  Fees const&
80  fees() const override
81  {
82  return base_.fees();
83  }
84 
85  Rules const&
86  rules() const override
87  {
88  return base_.rules();
89  }
90 
93  key_type const& key,
94  std::optional<key_type> const& last = std::nullopt) const override
95  {
96  return base_.succ(key, last);
97  }
98 
100  slesBegin() const override
101  {
102  return base_.slesBegin();
103  }
104 
106  slesEnd() const override
107  {
108  return base_.slesEnd();
109  }
110 
112  slesUpperBound(uint256 const& key) const override
113  {
114  return base_.slesUpperBound(key);
115  }
116 
118  txsBegin() const override
119  {
120  return base_.txsBegin();
121  }
122 
124  txsEnd() const override
125  {
126  return base_.txsEnd();
127  }
128 
129  bool
130  txExists(key_type const& key) const override
131  {
132  return base_.txExists(key);
133  }
134 
135  tx_type
136  txRead(key_type const& key) const override
137  {
138  return base_.txRead(key);
139  }
140 
141  //
142  // DigestAwareReadView
143  //
144 
146  digest(key_type const& key) const override
147  {
148  return base_.digest(key);
149  }
150 };
151 
152 } // namespace detail
153 
158 template <class Base>
160 {
161 private:
163 
165 
166 public:
167  using base_type = Base;
168 
169  CachedView() = delete;
170  CachedView(CachedView const&) = delete;
171  CachedView&
172  operator=(CachedView const&) = delete;
173 
175  : CachedViewImpl(base.get(), cache), sp_(base)
176  {
177  }
178 
184  base() const
185  {
186  return sp_;
187  }
188 };
189 
190 } // namespace ripple
191 
192 #endif
ripple::detail::CachedViewImpl
Definition: CachedView.h:35
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::detail::CachedViewImpl::open
bool open() const override
Returns true if this reflects an open ledger.
Definition: CachedView.h:68
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
std::shared_ptr
STL class.
ripple::TaggedCache< uint256, SLE const >
ripple::CachedView::base_type
Base base_type
Definition: CachedView.h:167
ripple::detail::CachedViewImpl::digest
std::optional< digest_type > digest(key_type const &key) const override
Definition: CachedView.h:146
ripple::detail::CachedViewImpl::cache_
CachedSLEs & cache_
Definition: CachedView.h:39
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:131
ripple::detail::CachedViewImpl::txRead
tx_type txRead(key_type const &key) const override
Definition: CachedView.h:136
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::ReadView::txsEnd
virtual std::unique_ptr< txs_type::iter_base > txsEnd() const =0
ripple::detail::CachedViewImpl::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: CachedView.h:124
ripple::ReadView::txExists
virtual bool txExists(key_type const &key) const =0
Returns true if a tx exists in the tx map.
ripple::ReadView::slesBegin
virtual std::unique_ptr< sles_type::iter_base > slesBegin() const =0
ripple::detail::CachedViewImpl::CachedViewImpl
CachedViewImpl()=delete
ripple::detail::CachedViewImpl::fees
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: CachedView.h:80
ripple::detail::CachedViewImpl::CachedViewImpl
CachedViewImpl(DigestAwareReadView const *base, CachedSLEs &cache)
Definition: CachedView.h:52
ripple::detail::CachedViewImpl::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: CachedView.h:118
ripple::detail::CachedViewImpl::succ
std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const override
Definition: CachedView.h:92
ripple::detail::CachedViewImpl::txExists
bool txExists(key_type const &key) const override
Definition: CachedView.h:130
ripple::detail::CachedViewImpl::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: CachedView.h:106
ripple::ReadView::txsBegin
virtual std::unique_ptr< txs_type::iter_base > txsBegin() const =0
ripple::detail::CachedViewImpl::read
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition: CachedView.cpp:34
ripple::CachedView::CachedView
CachedView()=delete
ripple::base_uint< 256 >
ripple::detail::CachedViewImpl::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: CachedView.h:112
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:49
ripple::CachedView
Wraps a DigestAwareReadView to provide caching.
Definition: CachedView.h:159
ripple::ReadView::slesUpperBound
virtual std::unique_ptr< sles_type::iter_base > slesUpperBound(key_type const &key) const =0
ripple::ReadView::slesEnd
virtual std::unique_ptr< sles_type::iter_base > slesEnd() const =0
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:329
ripple::ReadView::txRead
virtual tx_type txRead(key_type const &key) const =0
Read a transaction from the tx map.
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:96
ripple::detail::CachedViewImpl::map_
std::unordered_map< key_type, std::shared_ptr< SLE const >, hardened_hash<> > map_
Definition: CachedView.h:44
ripple::ReadView::succ
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
map
ripple::detail::CachedViewImpl::rules
Rules const & rules() const override
Returns the tx processing rules.
Definition: CachedView.h:86
memory
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:129
ripple::detail::CachedViewImpl::info
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: CachedView.h:74
ripple::detail::CachedViewImpl::operator=
CachedViewImpl & operator=(CachedViewImpl const &)=delete
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::CachedViewImpl::base_
DigestAwareReadView const & base_
Definition: CachedView.h:38
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::CachedView::operator=
CachedView & operator=(CachedView const &)=delete
ripple::Rules
Rules controlling protocol behavior.
Definition: Rules.h:33
ripple::CachedView::base
std::shared_ptr< Base const > const & base() const
Returns the base type.
Definition: CachedView.h:184
std::optional
mutex
ripple::detail::CachedViewImpl::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: CachedView.cpp:28
ripple::CachedView::sp_
std::shared_ptr< Base const > sp_
Definition: CachedView.h:162
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:75
ripple::DigestAwareReadView::digest
virtual std::optional< digest_type > digest(key_type const &key) const =0
Return the digest associated with the key.
std::unique_ptr
STL class.
ripple::detail::CachedViewImpl::mutex_
std::mutex mutex_
Definition: CachedView.h:40
std::unordered_map
STL class.
ripple::ReadView::open
virtual bool open() const =0
Returns true if this reflects an open ledger.
type_traits
ripple::CachedView::CachedView
CachedView(std::shared_ptr< Base const > const &base, CachedSLEs &cache)
Definition: CachedView.h:174
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:118
std::is_base_of
ripple::detail::CachedViewImpl::slesBegin
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: CachedView.h:100