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/ledger/CachedView.h>
21 #include <ripple/basics/contract.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 something
59  // (another thread?) has inserted the sle into the map and it has
60  // the wrong type.
61  LogicError("CachedView::read: wrong type");
62  }
63  return nullptr;
64  }
65  return iter->second;
66 }
67 
68 } // detail
69 } // ripple
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
std::shared_ptr
STL class.
ripple::detail::CachedViewImpl::cache_
CachedSLEs & cache_
Definition: CachedView.h:40
ripple::CachedSLEs::fetch
value_type fetch(digest_type const &digest, Handler const &h)
Fetch an item from the cache.
Definition: CachedSLEs.h:68
ripple::detail::CachedViewImpl::digest
boost::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition: CachedView.h:146
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:41
ripple::DigestAwareReadView::digest
virtual boost::optional< digest_type > digest(key_type const &key) const =0
Return the digest associated with the key.
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::detail::CachedViewImpl::map_
std::unordered_map< key_type, std::shared_ptr< SLE const >, hardened_hash<> > map_
Definition: CachedView.h:44
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:39
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:50
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::detail::CachedViewImpl::mutex_
std::mutex mutex_
Definition: CachedView.h:41