-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathFullBelowCache.h
146 lines (122 loc) · 3.96 KB
/
FullBelowCache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_SHAMAP_FULLBELOWCACHE_H_INCLUDED
#define RIPPLE_SHAMAP_FULLBELOWCACHE_H_INCLUDED
#include <ripple/basics/base_uint.h>
#include <ripple/basics/KeyCache.h>
#include <ripple/beast/insight/Collector.h>
#include <atomic>
#include <string>
namespace ripple {
namespace detail {
/** Remembers which tree keys have all descendants resident.
This optimizes the process of acquiring a complete tree.
*/
template <class Key>
class BasicFullBelowCache
{
private:
using CacheType = KeyCache <Key>;
public:
enum
{
defaultCacheTargetSize = 0
};
using key_type = Key;
using size_type = typename CacheType::size_type;
using clock_type = typename CacheType::clock_type;
/** Construct the cache.
@param name A label for diagnostics and stats reporting.
@param collector The collector to use for reporting stats.
@param targetSize The cache target size.
@param targetExpirationSeconds The expiration time for items.
*/
BasicFullBelowCache (std::string const& name, clock_type& clock,
beast::insight::Collector::ptr const& collector =
beast::insight::NullCollector::New (),
std::size_t target_size = defaultCacheTargetSize,
std::chrono::seconds expiration = std::chrono::minutes{2})
: m_cache (name, clock, collector, target_size,
expiration)
, m_gen (1)
{
}
/** Return the clock associated with the cache. */
clock_type& clock()
{
return m_cache.clock ();
}
/** Return the number of elements in the cache.
Thread safety:
Safe to call from any thread.
*/
size_type size () const
{
return m_cache.size ();
}
/** Remove expired cache items.
Thread safety:
Safe to call from any thread.
*/
void sweep ()
{
m_cache.sweep ();
}
/** Refresh the last access time of an item, if it exists.
Thread safety:
Safe to call from any thread.
@param key The key to refresh.
@return `true` If the key exists.
*/
bool touch_if_exists (key_type const& key)
{
return m_cache.touch_if_exists (key);
}
/** Insert a key into the cache.
If the key already exists, the last access time will still
be refreshed.
Thread safety:
Safe to call from any thread.
@param key The key to insert.
*/
void insert (key_type const& key)
{
m_cache.insert (key);
}
/** generation determines whether cached entry is valid */
std::uint32_t getGeneration (void) const
{
return m_gen;
}
void clear ()
{
m_cache.clear ();
++m_gen;
}
void reset ()
{
m_cache.clear();
m_gen = 1;
}
private:
KeyCache <Key> m_cache;
std::atomic <std::uint32_t> m_gen;
};
} // detail
using FullBelowCache = detail::BasicFullBelowCache <uint256>;
}
#endif