5
5
#include " bucket/LiveBucketIndex.h"
6
6
#include " bucket/BucketIndexUtils.h"
7
7
#include " bucket/BucketManager.h"
8
+ #include " bucket/BucketUtils.h"
8
9
#include " bucket/DiskIndex.h"
9
10
#include " util/Fs.h"
10
11
#include " util/GlobalChecks.h"
@@ -65,21 +66,6 @@ LiveBucketIndex::LiveBucketIndex(BucketManager& bm,
65
66
pageSize, filename);
66
67
mDiskIndex = std::make_unique<DiskIndex<LiveBucket>>(
67
68
bm, filename, pageSize, hash, ctx, hasher);
68
-
69
- auto percentCached = bm.getConfig ().BUCKETLIST_DB_CACHED_PERCENT ;
70
- if (percentCached > 0 )
71
- {
72
- auto const & counters = mDiskIndex ->getBucketEntryCounters ();
73
- auto cacheSize = (counters.numEntries () * percentCached) / 100 ;
74
-
75
- // Minimum cache size of 100 if we are going to cache a non-zero
76
- // number of entries
77
- // We don't want to reserve here, since caches only live as long as
78
- // the lifetime of the Bucket and fill relatively slowly
79
- mCache = std::make_unique<CacheT>(std::max<size_t >(cacheSize, 100 ),
80
- /* separatePRNG=*/ false ,
81
- /* reserve=*/ false );
82
- }
83
69
}
84
70
}
85
71
@@ -95,6 +81,44 @@ LiveBucketIndex::LiveBucketIndex(BucketManager const& bm, Archive& ar,
95
81
releaseAssertOrThrow (pageSize != 0 );
96
82
}
97
83
84
+ void
85
+ LiveBucketIndex::maybeInitializeCache (size_t bucketListTotalAccounts,
86
+ size_t maxBucketListAccountsToCache) const
87
+ {
88
+ // Everything is already in memory, no need for a redundant cache, or we've
89
+ // already initialized this cache
90
+ if (mInMemoryIndex || mCache )
91
+ {
92
+ return ;
93
+ }
94
+
95
+ releaseAssert (mDiskIndex );
96
+
97
+ auto accountsInThisBucket =
98
+ mDiskIndex ->getBucketEntryCounters ().entryTypeCounts .at (
99
+ LedgerEntryTypeAndDurability::ACCOUNT);
100
+
101
+ // Nothing to cache
102
+ if (accountsInThisBucket == 0 )
103
+ {
104
+ return ;
105
+ }
106
+
107
+ if (bucketListTotalAccounts < maxBucketListAccountsToCache)
108
+ {
109
+ // We can cache the entire bucket
110
+ mCache = std::make_unique<CacheT>(bucketListTotalAccounts);
111
+ }
112
+ else
113
+ {
114
+ double percentAccountsInBucket =
115
+ static_cast <double >(accountsInThisBucket) / bucketListTotalAccounts;
116
+ auto cacheSize = static_cast <size_t >(bucketListTotalAccounts *
117
+ percentAccountsInBucket);
118
+ mCache = std::make_unique<CacheT>(cacheSize);
119
+ }
120
+ }
121
+
98
122
LiveBucketIndex::IterT
99
123
LiveBucketIndex::begin () const
100
124
{
@@ -133,7 +157,7 @@ LiveBucketIndex::markBloomMiss() const
133
157
std::shared_ptr<BucketEntry const >
134
158
LiveBucketIndex::getCachedEntry (LedgerKey const & k) const
135
159
{
136
- if (shouldUseCache ())
160
+ if (shouldUseCache () && isCachedType (k) )
137
161
{
138
162
std::shared_lock<std::shared_mutex> lock (mCacheMutex );
139
163
auto cachePtr = mCache ->maybeGet (k);
@@ -262,6 +286,12 @@ LiveBucketIndex::getBucketEntryCounters() const
262
286
return mInMemoryIndex ->getBucketEntryCounters ();
263
287
}
264
288
289
+ bool
290
+ LiveBucketIndex::isCachedType (LedgerKey const & lk)
291
+ {
292
+ return lk.type () == ACCOUNT;
293
+ }
294
+
265
295
void
266
296
LiveBucketIndex::maybeAddToCache (
267
297
std::shared_ptr<BucketEntry const > const & entry) const
@@ -270,6 +300,10 @@ LiveBucketIndex::maybeAddToCache(
270
300
{
271
301
releaseAssertOrThrow (entry);
272
302
auto k = getBucketLedgerKey (*entry);
303
+ if (!isCachedType (k))
304
+ {
305
+ return ;
306
+ }
273
307
274
308
// If we are adding an entry to the cache, we must have missed it
275
309
// earlier.
0 commit comments