rippled
RippleLineCache.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_APP_PATHS_RIPPLELINECACHE_H_INCLUDED
21 #define RIPPLE_APP_PATHS_RIPPLELINECACHE_H_INCLUDED
22 
23 #include <ripple/app/ledger/Ledger.h>
24 #include <ripple/app/paths/TrustLine.h>
25 #include <ripple/basics/CountedObject.h>
26 #include <ripple/basics/hardened_hash.h>
27 
28 #include <cstddef>
29 #include <memory>
30 #include <mutex>
31 #include <vector>
32 
33 namespace ripple {
34 
35 // Used by Pathfinder
36 class RippleLineCache final : public CountedObject<RippleLineCache>
37 {
38 public:
39  explicit RippleLineCache(
41  beast::Journal j);
43 
45  getLedger() const
46  {
47  return ledger_;
48  }
49 
63  getRippleLines(AccountID const& accountID, LineDirection direction);
64 
65 private:
67 
70 
72 
73  struct AccountKey final : public CountedObject<AccountKey>
74  {
78 
80  AccountID const& account,
81  LineDirection direction,
82  std::size_t hash)
83  : account_(account), direction_(direction), hash_value_(hash)
84  {
85  }
86 
87  AccountKey(AccountKey const& other) = default;
88 
89  AccountKey&
90  operator=(AccountKey const& other) = default;
91 
92  bool
93  operator==(AccountKey const& lhs) const
94  {
95  return hash_value_ == lhs.hash_value_ && account_ == lhs.account_ &&
96  direction_ == lhs.direction_;
97  }
98 
100  get_hash() const
101  {
102  return hash_value_;
103  }
104 
105  struct Hash
106  {
107  explicit Hash() = default;
108 
110  operator()(AccountKey const& key) const noexcept
111  {
112  return key.get_hash();
113  }
114  };
115  };
116 
117  // Use a shared_ptr so entries can be removed from the map safely.
118  // Even though a shared_ptr to a vector will take more memory just a vector,
119  // most accounts are not going to have any entries (estimated over 90%), so
120  // vectors will not need to be created for them. This should lead to far
121  // less memory usage overall.
122  hash_map<
123  AccountKey,
125  AccountKey::Hash>
128 };
129 
130 } // namespace ripple
131 
132 #endif
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
std::shared_ptr
STL class.
ripple::LineDirection
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition: TrustLine.h:41
ripple::RippleLineCache::mLock
std::mutex mLock
Definition: RippleLineCache.h:66
ripple::RippleLineCache::AccountKey::hash_value_
std::size_t hash_value_
Definition: RippleLineCache.h:77
vector
ripple::RippleLineCache::AccountKey::Hash::operator()
std::size_t operator()(AccountKey const &key) const noexcept
Definition: RippleLineCache.h:110
ripple::RippleLineCache::ledger_
std::shared_ptr< ReadView const > ledger_
Definition: RippleLineCache.h:69
ripple::RippleLineCache
Definition: RippleLineCache.h:36
ripple::RippleLineCache::AccountKey
Definition: RippleLineCache.h:73
ripple::RippleLineCache::AccountKey::account_
AccountID account_
Definition: RippleLineCache.h:75
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::RippleLineCache::AccountKey::operator==
bool operator==(AccountKey const &lhs) const
Definition: RippleLineCache.h:93
cstddef
ripple::RippleLineCache::totalLineCount_
std::size_t totalLineCount_
Definition: RippleLineCache.h:127
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:96
ripple::RippleLineCache::AccountKey::get_hash
std::size_t get_hash() const
Definition: RippleLineCache.h:100
ripple::RippleLineCache::AccountKey::Hash::Hash
Hash()=default
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::RippleLineCache::AccountKey::AccountKey
AccountKey(AccountID const &account, LineDirection direction, std::size_t hash)
Definition: RippleLineCache.h:79
ripple::RippleLineCache::AccountKey::operator=
AccountKey & operator=(AccountKey const &other)=default
memory
ripple::RippleLineCache::AccountKey::Hash
Definition: RippleLineCache.h:105
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RippleLineCache::~RippleLineCache
~RippleLineCache()
Definition: RippleLineCache.cpp:34
ripple::RippleLineCache::RippleLineCache
RippleLineCache(std::shared_ptr< ReadView const > const &l, beast::Journal j)
Definition: RippleLineCache.cpp:26
ripple::RippleLineCache::hasher_
ripple::hardened_hash hasher_
Definition: RippleLineCache.h:68
ripple::RippleLineCache::lines_
hash_map< AccountKey, std::shared_ptr< std::vector< PathFindTrustLine > >, AccountKey::Hash > lines_
Definition: RippleLineCache.h:126
ripple::RippleLineCache::getRippleLines
std::shared_ptr< std::vector< PathFindTrustLine > > getRippleLines(AccountID const &accountID, LineDirection direction)
Find the trust lines associated with an account.
Definition: RippleLineCache.cpp:42
mutex
std::size_t
ripple::RippleLineCache::AccountKey::direction_
LineDirection direction_
Definition: RippleLineCache.h:76
std::unordered_map
STL class.
ripple::RippleLineCache::journal_
beast::Journal journal_
Definition: RippleLineCache.h:71
ripple::RippleLineCache::getLedger
std::shared_ptr< ReadView const > const & getLedger() const
Definition: RippleLineCache.h:45