From e4aebe8608c558524db9742801598333476f410e Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 3 Feb 2019 13:24:10 +0800 Subject: [PATCH] common/random_cache: remove unused RandomCache the only consumer of it was KeyValueStore, which was removed in 19c39faead87655d174965d054de8bbbe4d79067 . Signed-off-by: Kefu Chai --- src/common/random_cache.hpp | 110 ------------------------------------ 1 file changed, 110 deletions(-) delete mode 100644 src/common/random_cache.hpp diff --git a/src/common/random_cache.hpp b/src/common/random_cache.hpp deleted file mode 100644 index ad4a5d06a92b..000000000000 --- a/src/common/random_cache.hpp +++ /dev/null @@ -1,110 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab -/* - * Ceph - scalable distributed file system - * - * Copyright (C) 2014 UnitedStack - * - * Author: Haomai Wang - * - * This is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software - * Foundation. See file COPYING. - * - */ - -#ifndef CEPH_RANDOMCACHE_H -#define CEPH_RANDOMCACHE_H - -#include "common/ceph_mutex.h" -#include "include/compat.h" -#include "include/unordered_map.h" - - -// Although This is a ramdom cache implementation, here still consider to make -// the trim progress more reasonable. Each item owns its lookup frequency, -// when the cache is full it will randomly pick up several items and compare the -// frequency associated with. The least frequency of items will be evicted. -template -class RandomCache { - // The first element of pair is the frequency of item, it's used to evict item - ceph::unordered_map > contents; - ceph::mutex lock = ceph::make_mutex("RandomCache::lock"); - uint64_t max_size; - K last_trim_key; - - // When cache reach full, consider to evict a certain number of items - static const uint64_t EVICT_COUNT = 5; - // Avoid too much overhead on comparing items's frequency, the number of - // compare items is expected to small. - static const uint64_t COMPARE_COUNT = 3; - - // In order to make evict cache progress more lightweight and effective, - // several items are expected to evicted in one call - void trim_cache(uint64_t evict_count) { - typename ceph::unordered_map >::iterator it = contents.find(last_trim_key); - uint64_t total_compare = evict_count * COMPARE_COUNT; - map candidates; - - while (total_compare--) { - if (it == contents.end()) { - it = contents.begin(); - } - - candidates[it->second.first] = it->first; - it++; - } - if (it != contents.end()) - last_trim_key = it->first; - else - last_trim_key = contents.begin()->first; - - for (typename map::iterator j = candidates.begin(); j != candidates.end(); j++) { - contents.erase(j->second); - evict_count--; - if (!evict_count) - break; - } - } - - public: - RandomCache(size_t max_size=20) : max_size(max_size) {} - ~RandomCache() { - contents.clear(); - } - - void clear(K key) { - std::lock_guard l(lock); - contents.erase(key); - } - - void set_size(size_t new_size) { - std::lock_guard l(lock); - max_size = new_size; - if (max_size <= contents.size()) { - trim_cache(contents.size() - max_size); - } - } - - bool lookup(K key, V *out) { - std::lock_guard l(lock); - typename ceph::unordered_map >::iterator it = contents.find(key); - if (it != contents.end()) { - it->second.first++; - *out = it->second.second; - return true; - } - return false; - } - - void add(K key, V value) { - std::lock_guard l(lock); - if (max_size <= contents.size()) { - trim_cache(EVICT_COUNT); - } - contents[key] = make_pair(1, value); - } -}; - -#endif -- 2.47.3