rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/TaggedCache.ipp>
21#include <xrpl/ledger/CachedView.h>
22
23namespace ripple {
24namespace detail {
25
26bool
28{
29 return read(k) != nullptr;
30}
31
34{
35 static CountedObjects::Counter hits{"CachedView::hit"};
36 static CountedObjects::Counter hitsexpired{"CachedView::hitExpired"};
37 static CountedObjects::Counter misses{"CachedView::miss"};
38 bool cacheHit = false;
39 bool baseRead = false;
40
41 auto const digest = [&]() -> std::optional<uint256> {
42 {
44 auto const iter = map_.find(k.key);
45 if (iter != map_.end())
46 {
47 cacheHit = true;
48 return iter->second;
49 }
50 }
51 return base_.digest(k.key);
52 }();
53 if (!digest)
54 return nullptr;
55 auto sle = cache_.fetch(*digest, [&]() {
56 baseRead = true;
57 return base_.read(k);
58 });
59 // If the sle is null, then a failure must have occurred in base_.read()
60 XRPL_ASSERT(
61 sle || baseRead,
62 "ripple::CachedView::read : null SLE result from base");
63 if (cacheHit && baseRead)
64 hitsexpired.increment();
65 else if (cacheHit)
66 hits.increment();
67 else
68 misses.increment();
69
70 if (!cacheHit)
71 {
72 // Avoid acquiring this lock unless necessary. It is only necessary if
73 // the key was not found in the map_. The lock is needed to add the key
74 // and digest.
76 map_.emplace(k.key, *digest);
77 }
78 if (!sle || !k.check(*sle))
79 return nullptr;
80 return sle;
81}
82
83} // namespace detail
84} // namespace ripple
Implementation for CountedObject.
virtual std::optional< digest_type > digest(key_type const &key) const =0
Return the digest associated with the key.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
SharedPointerType fetch(key_type const &key)
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition CachedView.h:142
DigestAwareReadView const & base_
Definition CachedView.h:37
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
bool exists(Keylet const &k) const override
Determine if a state item exists.
std::unordered_map< key_type, uint256, hardened_hash<> > map_
Definition CachedView.h:40
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:39
bool check(STLedgerEntry const &) const
Returns true if the SLE matches the type.
Definition Keylet.cpp:28
uint256 key
Definition Keylet.h:40