@@ -193,6 +193,10 @@ void LRUCache::set_capacity(size_t capacity) {
193
193
}
194
194
}
195
195
196
+ void LRUCache::set_charge_mode (ChargeMode charge_mode) {
197
+ _charge_mode = charge_mode;
198
+ }
199
+
196
200
uint64_t LRUCache::get_lookup_count () const {
197
201
std::lock_guard l (_mutex);
198
202
return _lookup_count;
@@ -295,7 +299,8 @@ void LRUCache::_evict_one_entry(LRUHandle* e) {
295
299
}
296
300
297
301
Cache::Handle* LRUCache::insert (const CacheKey& key, uint32_t hash, void * value, size_t charge,
298
- void (*deleter)(const CacheKey& key, void * value), CachePriority priority) {
302
+ void (*deleter)(const CacheKey& key, void * value), CachePriority priority,
303
+ size_t value_size) {
299
304
auto * e = reinterpret_cast <LRUHandle*>(malloc (sizeof (LRUHandle) - 1 + key.size ()));
300
305
e->value = value;
301
306
e->deleter = deleter;
@@ -306,6 +311,7 @@ Cache::Handle* LRUCache::insert(const CacheKey& key, uint32_t hash, void* value,
306
311
e->next = e->prev = nullptr ;
307
312
e->in_cache = true ;
308
313
e->priority = priority;
314
+ e->value_size = value_size;
309
315
memcpy (e->key_data , key.data (), key.size ());
310
316
std::vector<LRUHandle*> last_ref_list;
311
317
{
@@ -395,10 +401,12 @@ uint32_t ShardedLRUCache::_shard(uint32_t hash) {
395
401
return hash >> (32 - kNumShardBits );
396
402
}
397
403
398
- ShardedLRUCache::ShardedLRUCache (size_t capacity) : _last_id(0 ), _capacity(capacity) {
404
+ ShardedLRUCache::ShardedLRUCache (size_t capacity, ChargeMode charge_mode)
405
+ : _last_id(0 ), _capacity(capacity), _charge_mode(charge_mode) {
399
406
const size_t per_shard = (_capacity + (kNumShards - 1 )) / kNumShards ;
400
407
for (auto & _shard : _shards) {
401
408
_shard.set_capacity (per_shard);
409
+ _shard.set_charge_mode (_charge_mode);
402
410
}
403
411
}
404
412
@@ -427,9 +435,10 @@ bool ShardedLRUCache::adjust_capacity(int64_t delta, size_t min_capacity) {
427
435
}
428
436
429
437
Cache::Handle* ShardedLRUCache::insert (const CacheKey& key, void * value, size_t charge,
430
- void (*deleter)(const CacheKey& key, void * value), CachePriority priority) {
438
+ void (*deleter)(const CacheKey& key, void * value), CachePriority priority,
439
+ size_t value_size) {
431
440
const uint32_t hash = _hash_slice (key);
432
- return _shards[_shard (hash)].insert (key, hash, value, charge, deleter, priority);
441
+ return _shards[_shard (hash)].insert (key, hash, value, charge, deleter, priority, value_size );
433
442
}
434
443
435
444
Cache::Handle* ShardedLRUCache::lookup (const CacheKey& key) {
@@ -453,7 +462,8 @@ void* ShardedLRUCache::value(Handle* handle) {
453
462
454
463
Slice ShardedLRUCache::value_slice (Handle* handle) {
455
464
auto lru_handle = reinterpret_cast <LRUHandle*>(handle);
456
- return {(char *)lru_handle->value , lru_handle->charge };
465
+ size_t record_size = _charge_mode == ChargeMode::VALUESIZE ? lru_handle->charge : lru_handle->value_size ;
466
+ return {(char *)lru_handle->value , record_size};
457
467
}
458
468
459
469
uint64_t ShardedLRUCache::new_id () {
@@ -526,8 +536,8 @@ void ShardedLRUCache::get_cache_status(rapidjson::Document* document) {
526
536
}
527
537
}
528
538
529
- Cache* new_lru_cache (size_t capacity) {
530
- return new ShardedLRUCache (capacity);
539
+ Cache* new_lru_cache (size_t capacity, ChargeMode charge_mode ) {
540
+ return new ShardedLRUCache (capacity, charge_mode );
531
541
}
532
542
533
543
} // namespace starrocks
0 commit comments