rippled
CachedView.cpp
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 #include <ripple/basics/contract.h>
21 #include <ripple/ledger/CachedView.h>
22 #include <ripple/protocol/Serializer.h>
23 
24 namespace ripple {
25 namespace detail {
26 
27 bool
29 {
30  return read(k) != nullptr;
31 }
32 
35 {
36  {
37  std::lock_guard lock(mutex_);
38  auto const iter = map_.find(k.key);
39  if (iter != map_.end())
40  {
41  if (!iter->second || !k.check(*iter->second))
42  return nullptr;
43  return iter->second;
44  }
45  }
46  auto const digest = base_.digest(k.key);
47  if (!digest)
48  return nullptr;
49  auto sle = cache_.fetch(*digest, [&]() { return base_.read(k); });
50  std::lock_guard lock(mutex_);
51  auto const er = map_.emplace(k.key, sle);
52  auto const& iter = er.first;
53  bool const inserted = er.second;
54  if (iter->second && !k.check(*iter->second))
55  {
56  if (!inserted)
57  {
58  // On entry, this function did not find this key in map_. Now
59  // something (another thread?) has inserted the sle into the map and
60  // it has the wrong type.
61  LogicError("CachedView::read: wrong type");
62  }
63  return nullptr;
64  }
65  return iter->second;
66 }
67 
68 } // namespace detail
69 } // namespace ripple
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
std::shared_ptr
STL class.
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
std::lock_guard
STL class.
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::Keylet::key
uint256 key
Definition: Keylet.h:40
ripple::detail::CachedViewImpl::map_
std::unordered_map< key_type, std::shared_ptr< SLE const >, hardened_hash<> > map_
Definition: CachedView.h:44
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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::TaggedCache::fetch
std::shared_ptr< T > fetch(const key_type &key)
Definition: TaggedCache.h:396
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:48
ripple::detail::CachedViewImpl::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: CachedView.cpp:28
ripple::Keylet::check
bool check(STLedgerEntry const &) const
Returns true if the SLE matches the type.
Definition: Keylet.cpp:26
ripple::DigestAwareReadView::digest
virtual std::optional< digest_type > digest(key_type const &key) const =0
Return the digest associated with the key.
ripple::detail::CachedViewImpl::mutex_
std::mutex mutex_
Definition: CachedView.h:40