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