rippled
Loading...
Searching...
No Matches
CachedView.cpp
1#include <xrpl/basics/TaggedCache.ipp>
2#include <xrpl/ledger/CachedView.h>
3
4namespace ripple {
5namespace detail {
6
7bool
9{
10 return read(k) != nullptr;
11}
12
15{
16 static CountedObjects::Counter hits{"CachedView::hit"};
17 static CountedObjects::Counter hitsexpired{"CachedView::hitExpired"};
18 static CountedObjects::Counter misses{"CachedView::miss"};
19 bool cacheHit = false;
20 bool baseRead = false;
21
22 auto const digest = [&]() -> std::optional<uint256> {
23 {
25 auto const iter = map_.find(k.key);
26 if (iter != map_.end())
27 {
28 cacheHit = true;
29 return iter->second;
30 }
31 }
32 return base_.digest(k.key);
33 }();
34 if (!digest)
35 return nullptr;
36 auto sle = cache_.fetch(*digest, [&]() {
37 baseRead = true;
38 return base_.read(k);
39 });
40 // If the sle is null, then a failure must have occurred in base_.read()
41 XRPL_ASSERT(
42 sle || baseRead,
43 "ripple::CachedView::read : null SLE result from base");
44 if (cacheHit && baseRead)
45 hitsexpired.increment();
46 else if (cacheHit)
47 hits.increment();
48 else
49 misses.increment();
50
51 if (!cacheHit)
52 {
53 // Avoid acquiring this lock unless necessary. It is only necessary if
54 // the key was not found in the map_. The lock is needed to add the key
55 // and digest.
57 map_.emplace(k.key, *digest);
58 }
59 if (!sle || !k.check(*sle))
60 return nullptr;
61 return sle;
62}
63
64} // namespace detail
65} // 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:123
DigestAwareReadView const & base_
Definition CachedView.h:18
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.
Definition CachedView.cpp:8
std::unordered_map< key_type, uint256, hardened_hash<> > map_
Definition CachedView.h:21
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
bool check(STLedgerEntry const &) const
Returns true if the SLE matches the type.
Definition Keylet.cpp:9
uint256 key
Definition Keylet.h:21