rippled
RawStateTable.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_RAWSTATETABLE_H_INCLUDED
21 #define RIPPLE_LEDGER_RAWSTATETABLE_H_INCLUDED
22 
23 #include <ripple/ledger/RawView.h>
24 #include <ripple/ledger/ReadView.h>
25 
26 #include <boost/container/pmr/monotonic_buffer_resource.hpp>
27 #include <boost/container/pmr/polymorphic_allocator.hpp>
28 
29 #include <map>
30 #include <utility>
31 
32 namespace ripple {
33 namespace detail {
34 
35 // Helper class that buffers raw modifications
37 {
38 public:
40  // Initial size for the monotonic_buffer_resource used for allocations
41  // The size was chosen from the old `qalloc` code (which this replaces).
42  // It is unclear how the size initially chosen in qalloc.
43  static constexpr size_t initialBufferSize = kilobytes(256);
44 
47  boost::container::pmr::monotonic_buffer_resource>(
50 
53  boost::container::pmr::monotonic_buffer_resource>(
55  , items_{rhs.items_, monotonic_resource_.get()}
56  , dropsDestroyed_{rhs.dropsDestroyed_} {};
57 
58  RawStateTable(RawStateTable&&) = default;
59 
61  operator=(RawStateTable&&) = delete;
63  operator=(RawStateTable const&) = delete;
64 
65  void
66  apply(RawView& to) const;
67 
68  bool
69  exists(ReadView const& base, Keylet const& k) const;
70 
72  succ(
73  ReadView const& base,
74  key_type const& key,
75  std::optional<key_type> const& last) const;
76 
77  void
78  erase(std::shared_ptr<SLE> const& sle);
79 
80  void
81  insert(std::shared_ptr<SLE> const& sle);
82 
83  void
84  replace(std::shared_ptr<SLE> const& sle);
85 
87  read(ReadView const& base, Keylet const& k) const;
88 
89  void
90  destroyXRP(XRPAmount const& fee);
91 
93  slesBegin(ReadView const& base) const;
94 
96  slesEnd(ReadView const& base) const;
97 
99  slesUpperBound(ReadView const& base, uint256 const& key) const;
100 
101 private:
102  enum class Action {
103  erase,
104  insert,
105  replace,
106  };
107 
108  class sles_iter_impl;
109 
110  struct sleAction
111  {
114 
115  // Constructor needed for emplacement in std::map
116  sleAction(Action action_, std::shared_ptr<SLE> const& sle_)
117  : action(action_), sle(sle_)
118  {
119  }
120  };
121 
122  // Use boost::pmr functionality instead of the std::pmr
123  // functions b/c clang does not support pmr yet (as-of 9/2020)
124  using items_t = std::map<
125  key_type,
126  sleAction,
128  boost::container::pmr::polymorphic_allocator<
130  // monotonic_resource_ must outlive `items_`. Make a pointer so it may be
131  // easily moved.
135 
137 };
138 
139 } // namespace detail
140 } // namespace ripple
141 
142 #endif
ripple::detail::RawStateTable::dropsDestroyed_
XRPAmount dropsDestroyed_
Definition: RawStateTable.h:136
ripple::detail::RawStateTable::operator=
RawStateTable & operator=(RawStateTable &&)=delete
std::shared_ptr
STL class.
utility
std::make_unique
T make_unique(T... args)
std::pair
ripple::detail::RawStateTable::succ
std::optional< key_type > succ(ReadView const &base, key_type const &key, std::optional< key_type > const &last) const
Definition: RawStateTable.cpp:199
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:131
ripple::detail::RawStateTable::RawStateTable
RawStateTable()
Definition: RawStateTable.h:45
ripple::detail::RawStateTable::sleAction::sleAction
sleAction(Action action_, std::shared_ptr< SLE > const &sle_)
Definition: RawStateTable.h:116
ripple::detail::RawStateTable::Action::erase
@ erase
std::unique_ptr::get
T get(T... args)
ripple::kilobytes
constexpr auto kilobytes(T value) noexcept
Definition: ByteUtilities.h:27
ripple::detail::RawStateTable::read
std::shared_ptr< SLE const > read(ReadView const &base, Keylet const &k) const
Definition: RawStateTable.cpp:307
ripple::detail::RawStateTable::items_t
std::map< key_type, sleAction, std::less< key_type >, boost::container::pmr::polymorphic_allocator< std::pair< const key_type, sleAction > >> items_t
Definition: RawStateTable.h:129
ripple::detail::RawStateTable::Action::replace
@ replace
std::less
ripple::detail::RawStateTable::sleAction::action
Action action
Definition: RawStateTable.h:112
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::base_uint< 256 >
ripple::detail::RawStateTable::RawStateTable
RawStateTable(RawStateTable const &rhs)
Definition: RawStateTable.h:51
ripple::detail::RawStateTable::slesUpperBound
std::unique_ptr< ReadView::sles_type::iter_base > slesUpperBound(ReadView const &base, uint256 const &key) const
Definition: RawStateTable.cpp:343
ripple::detail::RawStateTable::sleAction
Definition: RawStateTable.h:110
ripple::detail::RawStateTable::Action::insert
@ insert
ripple::detail::RawStateTable::slesBegin
std::unique_ptr< ReadView::sles_type::iter_base > slesBegin(ReadView const &base) const
Definition: RawStateTable.cpp:329
ripple::detail::RawStateTable::Action
Action
Definition: RawStateTable.h:102
ripple::detail::RawStateTable::erase
void erase(std::shared_ptr< SLE > const &sle)
Definition: RawStateTable.cpp:234
map
ripple::detail::RawStateTable::exists
bool exists(ReadView const &base, Keylet const &k) const
Definition: RawStateTable.cpp:180
ripple::detail::RawStateTable::initialBufferSize
static constexpr size_t initialBufferSize
Definition: RawStateTable.h:43
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::RawStateTable::apply
void apply(RawView &to) const
Definition: RawStateTable.cpp:158
ripple::detail::RawStateTable::items_
items_t items_
Definition: RawStateTable.h:134
ripple::detail::RawStateTable::destroyXRP
void destroyXRP(XRPAmount const &fee)
Definition: RawStateTable.cpp:323
ripple::detail::RawStateTable::monotonic_resource_
std::unique_ptr< boost::container::pmr::monotonic_buffer_resource > monotonic_resource_
Definition: RawStateTable.h:133
ripple::detail::RawStateTable::insert
void insert(std::shared_ptr< SLE > const &sle)
Definition: RawStateTable.cpp:260
std::optional
ripple::detail::RawStateTable
Definition: RawStateTable.h:36
ripple::detail::RawStateTable::replace
void replace(std::shared_ptr< SLE > const &sle)
Definition: RawStateTable.cpp:285
ripple::detail::RawStateTable::key_type
ReadView::key_type key_type
Definition: RawStateTable.h:39
ripple::detail::RawStateTable::slesEnd
std::unique_ptr< ReadView::sles_type::iter_base > slesEnd(ReadView const &base) const
Definition: RawStateTable.cpp:336
std::unique_ptr
STL class.
ripple::detail::RawStateTable::sleAction::sle
std::shared_ptr< SLE > sle
Definition: RawStateTable.h:113
ripple::XRPAmount
Definition: XRPAmount.h:46