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 
43 public:
44  CachedViewImpl() = delete;
45  CachedViewImpl(CachedViewImpl const&) = delete;
47  operator=(CachedViewImpl const&) = delete;
48 
50  : base_(*base), cache_(cache)
51  {
52  }
53 
54  //
55  // ReadView
56  //
57 
58  bool
59  exists(Keylet const& k) const override;
60 
62  read(Keylet const& k) const override;
63 
64  bool
65  open() const override
66  {
67  return base_.open();
68  }
69 
70  LedgerInfo const&
71  info() const override
72  {
73  return base_.info();
74  }
75 
76  Fees const&
77  fees() const override
78  {
79  return base_.fees();
80  }
81 
82  Rules const&
83  rules() const override
84  {
85  return base_.rules();
86  }
87 
90  key_type const& key,
91  std::optional<key_type> const& last = std::nullopt) const override
92  {
93  return base_.succ(key, last);
94  }
95 
97  slesBegin() const override
98  {
99  return base_.slesBegin();
100  }
101 
103  slesEnd() const override
104  {
105  return base_.slesEnd();
106  }
107 
109  slesUpperBound(uint256 const& key) const override
110  {
111  return base_.slesUpperBound(key);
112  }
113 
115  txsBegin() const override
116  {
117  return base_.txsBegin();
118  }
119 
121  txsEnd() const override
122  {
123  return base_.txsEnd();
124  }
125 
126  bool
127  txExists(key_type const& key) const override
128  {
129  return base_.txExists(key);
130  }
131 
132  tx_type
133  txRead(key_type const& key) const override
134  {
135  return base_.txRead(key);
136  }
137 
138  //
139  // DigestAwareReadView
140  //
141 
143  digest(key_type const& key) const override
144  {
145  return base_.digest(key);
146  }
147 };
148 
149 } // namespace detail
150 
155 template <class Base>
157 {
158 private:
160 
162 
163 public:
164  using base_type = Base;
165 
166  CachedView() = delete;
167  CachedView(CachedView const&) = delete;
168  CachedView&
169  operator=(CachedView const&) = delete;
170 
172  : CachedViewImpl(base.get(), cache), sp_(base)
173  {
174  }
175 
181  base() const
182  {
183  return sp_;
184  }
185 };
186 
187 } // namespace ripple
188 
189 #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:65
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:164
ripple::detail::CachedViewImpl::digest
std::optional< digest_type > digest(key_type const &key) const override
Definition: CachedView.h:143
ripple::detail::CachedViewImpl::cache_
CachedSLEs & cache_
Definition: CachedView.h:39
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:60
ripple::detail::CachedViewImpl::txRead
tx_type txRead(key_type const &key) const override
Definition: CachedView.h:133
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:121
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:77
ripple::detail::CachedViewImpl::CachedViewImpl
CachedViewImpl(DigestAwareReadView const *base, CachedSLEs &cache)
Definition: CachedView.h:49
ripple::detail::CachedViewImpl::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: CachedView.h:115
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:89
ripple::detail::CachedViewImpl::txExists
bool txExists(key_type const &key) const override
Definition: CachedView.h:127
ripple::detail::CachedViewImpl::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: CachedView.h:103
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
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::detail::CachedViewImpl::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: CachedView.h:109
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: protocol/Fees.h:32
ripple::CachedView
Wraps a DigestAwareReadView to provide caching.
Definition: CachedView.h:156
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:258
ripple::ReadView::txRead
virtual tx_type txRead(key_type const &key) const =0
Read a transaction from the tx map.
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:83
memory
ripple::LedgerHeader
Information about the notional ledger backing the view.
Definition: LedgerHeader.h:33
ripple::detail::CachedViewImpl::map_
std::unordered_map< key_type, uint256, hardened_hash<> > map_
Definition: CachedView.h:41
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:58
ripple::detail::CachedViewImpl::info
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: CachedView.h:71
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:181
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:159
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:171
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:97