diff --git a/internal/core/src/CMakeLists.txt b/internal/core/src/CMakeLists.txt index f135f3b306..070d8ecd12 100644 --- a/internal/core/src/CMakeLists.txt +++ b/internal/core/src/CMakeLists.txt @@ -29,7 +29,6 @@ add_subdirectory( log ) add_subdirectory( pb ) add_subdirectory( storage ) add_subdirectory( segcore ) -add_subdirectory( cache ) add_subdirectory( query ) add_subdirectory( common ) add_subdirectory( indexbuilder ) diff --git a/internal/core/src/cache/CMakeLists.txt b/internal/core/src/cache/CMakeLists.txt deleted file mode 100644 index 9c3a632800..0000000000 --- a/internal/core/src/cache/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -#------------------------------------------------------------------------------- -# Copyright (C) 2019-2020 Zilliz. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software distributed under the License -# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -# or implied. See the License for the specific language governing permissions and limitations under the License. -#------------------------------------------------------------------------------- -aux_source_directory( ${MILVUS_ENGINE_SRC}/cache CACHE_FILES ) -add_library( cache STATIC ) -target_sources( cache PRIVATE ${CACHE_FILES} - CacheMgr.inl - Cache.inl - ) -target_include_directories( cache PUBLIC ${MILVUS_ENGINE_SRC}/cache ) -target_link_libraries( cache PRIVATE fiu) diff --git a/internal/core/src/cache/Cache.h b/internal/core/src/cache/Cache.h deleted file mode 100644 index 34594c4573..0000000000 --- a/internal/core/src/cache/Cache.h +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#pragma once - -#include "LRU.h" -#include "utils/Log.h" - -#include -#include -#include -#include - -namespace milvus { -namespace cache { - -template -class Cache { - public: - // mem_capacity, units:GB - Cache(int64_t capacity_gb, int64_t cache_max_count, const std::string& header = ""); - ~Cache() = default; - - int64_t - usage() const { - return usage_; - } - - // unit: BYTE - int64_t - capacity() const { - return capacity_; - } - - // unit: BYTE - void - set_capacity(int64_t capacity); - - double - freemem_percent() const { - return freemem_percent_; - } - - void - set_freemem_percent(double percent) { - freemem_percent_ = percent; - } - - size_t - size() const; - - bool - exists(const std::string& key); - - ItemObj - get(const std::string& key); - - void - insert(const std::string& key, const ItemObj& item); - - void - erase(const std::string& key); - - bool - reserve(const int64_t size); - - void - print(); - - void - clear(); - - private: - void - insert_internal(const std::string& key, const ItemObj& item); - - void - erase_internal(const std::string& key); - - void - free_memory_internal(const int64_t target_size); - - private: - std::string header_; - int64_t usage_; - int64_t capacity_; - double freemem_percent_; - - LRU lru_; - mutable std::mutex mutex_; -}; - -} // namespace cache -} // namespace milvus - -#include "cache/Cache.inl" diff --git a/internal/core/src/cache/Cache.inl b/internal/core/src/cache/Cache.inl deleted file mode 100644 index c371efe515..0000000000 --- a/internal/core/src/cache/Cache.inl +++ /dev/null @@ -1,191 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -namespace milvus { -namespace cache { - -constexpr double DEFAULT_THRESHOLD_PERCENT = 0.7; - -template -Cache::Cache(int64_t capacity, int64_t cache_max_count, const std::string& header) - : header_(header), - usage_(0), - capacity_(capacity), - freemem_percent_(DEFAULT_THRESHOLD_PERCENT), - lru_(cache_max_count) { -} - -template -void -Cache::set_capacity(int64_t capacity) { - std::lock_guard lock(mutex_); - if (capacity > 0) { - capacity_ = capacity; - free_memory_internal(capacity); - } -} - -template -size_t -Cache::size() const { - std::lock_guard lock(mutex_); - return lru_.size(); -} - -template -bool -Cache::exists(const std::string& key) { - std::lock_guard lock(mutex_); - return lru_.exists(key); -} - -template -ItemObj -Cache::get(const std::string& key) { - std::lock_guard lock(mutex_); - if (!lru_.exists(key)) { - return nullptr; - } - return lru_.get(key); -} - -template -void -Cache::insert(const std::string& key, const ItemObj& item) { - std::lock_guard lock(mutex_); - insert_internal(key, item); -} - -template -void -Cache::erase(const std::string& key) { - std::lock_guard lock(mutex_); - erase_internal(key); -} - -template -bool -Cache::reserve(const int64_t item_size) { - std::lock_guard lock(mutex_); - if (item_size > capacity_) { - LOG_SERVER_ERROR_ << header_ << " item size " << (item_size >> 20) << "MB too big to insert into cache capacity" - << (capacity_ >> 20) << "MB"; - return false; - } - if (item_size > capacity_ - usage_) { - free_memory_internal(capacity_ - item_size); - } - return true; -} - -template -void -Cache::clear() { - std::lock_guard lock(mutex_); - lru_.clear(); - usage_ = 0; - LOG_SERVER_DEBUG_ << header_ << " Clear cache !"; -} - - -template -void -Cache::print() { - std::lock_guard lock(mutex_); - size_t cache_count = lru_.size(); - // for (auto it = lru_.begin(); it != lru_.end(); ++it) { - // LOG_SERVER_DEBUG_ << it->first; - // } - LOG_SERVER_DEBUG_ << header_ << " [item count]: " << cache_count << ", [usage] " << (usage_ >> 20) - << "MB, [capacity] " << (capacity_ >> 20) << "MB"; -} - -template -void -Cache::insert_internal(const std::string& key, const ItemObj& item) { - if (item == nullptr) { - return; - } - - size_t item_size = item->Size(); - - // if key already exist, subtract old item size - if (lru_.exists(key)) { - const ItemObj& old_item = lru_.get(key); - usage_ -= old_item->Size(); - } - - // plus new item size - usage_ += item_size; - - // if usage exceed capacity, free some items - if (usage_ > capacity_) { - LOG_SERVER_DEBUG_ << header_ << " Current usage " << (usage_ >> 20) << "MB is too high for capacity " - << (capacity_ >> 20) << "MB, start free memory"; - free_memory_internal(capacity_); - } - - // insert new item - lru_.put(key, item); - LOG_SERVER_DEBUG_ << header_ << " Insert " << key << " size: " << (item_size >> 20) << "MB into cache"; - LOG_SERVER_DEBUG_ << header_ << " Count: " << lru_.size() << ", Usage: " << (usage_ >> 20) << "MB, Capacity: " - << (capacity_ >> 20) << "MB"; -} - -template -void -Cache::erase_internal(const std::string& key) { - if (!lru_.exists(key)) { - return; - } - - const ItemObj& item = lru_.get(key); - size_t item_size = item->Size(); - - lru_.erase(key); - - usage_ -= item_size; - LOG_SERVER_DEBUG_ << header_ << " Erase " << key << " size: " << (item_size >> 20) << "MB from cache"; - LOG_SERVER_DEBUG_ << header_ << " Count: " << lru_.size() << ", Usage: " << (usage_ >> 20) << "MB, Capacity: " - << (capacity_ >> 20) << "MB"; -} - -template -void -Cache::free_memory_internal(const int64_t target_size) { - int64_t threshold = std::min((int64_t)(capacity_ * freemem_percent_), target_size); - int64_t delta_size = usage_ - threshold; - if (delta_size <= 0) { - delta_size = 1; // ensure at least one item erased - } - - std::set key_array; - int64_t released_size = 0; - - auto it = lru_.rbegin(); - while (it != lru_.rend() && released_size < delta_size) { - auto& key = it->first; - auto& obj_ptr = it->second; - - key_array.emplace(key); - released_size += obj_ptr->Size(); - ++it; - } - - LOG_SERVER_DEBUG_ << header_ << " To be released memory size: " << (released_size >> 20) << "MB"; - - for (auto& key : key_array) { - erase_internal(key); - } -} - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/CacheMgr.h b/internal/core/src/cache/CacheMgr.h deleted file mode 100644 index 1d1cfb5ccf..0000000000 --- a/internal/core/src/cache/CacheMgr.h +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#pragma once - -#include "Cache.h" -#include "utils/Log.h" - -#include -#include - -namespace milvus { -namespace cache { - -template -class CacheMgr { - public: - virtual uint64_t - ItemCount() const; - - virtual bool - ItemExists(const std::string& key); - - virtual ItemObj - GetItem(const std::string& key); - - virtual void - InsertItem(const std::string& key, const ItemObj& data); - - virtual void - EraseItem(const std::string& key); - - virtual bool - Reserve(const int64_t size); - - virtual void - PrintInfo(); - - virtual void - ClearCache(); - - int64_t - CacheUsage() const; - - int64_t - CacheCapacity() const; - - void - SetCapacity(int64_t capacity); - - protected: - CacheMgr(); - - virtual ~CacheMgr(); - - protected: - std::shared_ptr> cache_; -}; - -} // namespace cache -} // namespace milvus - -#include "cache/CacheMgr.inl" diff --git a/internal/core/src/cache/CacheMgr.inl b/internal/core/src/cache/CacheMgr.inl deleted file mode 100644 index ebb44d8f48..0000000000 --- a/internal/core/src/cache/CacheMgr.inl +++ /dev/null @@ -1,134 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -namespace milvus { -namespace cache { - -template -CacheMgr::CacheMgr() { -} - -template -CacheMgr::~CacheMgr() { -} - -template -uint64_t -CacheMgr::ItemCount() const { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return 0; - } - return (uint64_t)(cache_->size()); -} - -template -bool -CacheMgr::ItemExists(const std::string& key) { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return false; - } - return cache_->exists(key); -} - -template -ItemObj -CacheMgr::GetItem(const std::string& key) { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return nullptr; - } - return cache_->get(key); -} - -template -void -CacheMgr::InsertItem(const std::string& key, const ItemObj& data) { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return; - } - cache_->insert(key, data); -} - -template -void -CacheMgr::EraseItem(const std::string& key) { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return; - } - cache_->erase(key); -} - -template -bool -CacheMgr::Reserve(const int64_t size) { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return false; - } - return cache_->reserve(size); -} - -template -void -CacheMgr::PrintInfo() { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return; - } - cache_->print(); -} - -template -void -CacheMgr::ClearCache() { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return; - } - cache_->clear(); -} - -template -int64_t -CacheMgr::CacheUsage() const { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return 0; - } - return cache_->usage(); -} - -template -int64_t -CacheMgr::CacheCapacity() const { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return 0; - } - return cache_->capacity(); -} - -template -void -CacheMgr::SetCapacity(int64_t capacity) { - if (cache_ == nullptr) { - LOG_SERVER_ERROR_ << "Cache doesn't exist"; - return; - } - cache_->set_capacity(capacity); -} - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/CpuCacheMgr.cpp b/internal/core/src/cache/CpuCacheMgr.cpp deleted file mode 100644 index fc819f5bb7..0000000000 --- a/internal/core/src/cache/CpuCacheMgr.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#include "cache/CpuCacheMgr.h" - -#include - -#include - -#include "utils/Log.h" -#include "value/config/ServerConfig.h" - -namespace milvus { -namespace cache { - -CpuCacheMgr& -CpuCacheMgr::GetInstance() { - static CpuCacheMgr s_mgr; - return s_mgr; -} - -CpuCacheMgr::CpuCacheMgr() { - cache_ = std::make_shared>(config.cache.cache_size(), 1UL << 32, "[CACHE CPU]"); - - if (config.cache.cpu_cache_threshold() > 0.0) { - cache_->set_freemem_percent(config.cache.cpu_cache_threshold()); - } - ConfigMgr::GetInstance().Attach("cache.cache_size", this); -} - -CpuCacheMgr::~CpuCacheMgr() { - ConfigMgr::GetInstance().Detach("cache.cache_size", this); -} - -DataObjPtr -CpuCacheMgr::GetItem(const std::string& key) { - auto ret = CacheMgr::GetItem(key); - return ret; -} - -void -CpuCacheMgr::ConfigUpdate(const std::string& name) { - SetCapacity(config.cache.cache_size()); -} - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/CpuCacheMgr.h b/internal/core/src/cache/CpuCacheMgr.h deleted file mode 100644 index bd53354a28..0000000000 --- a/internal/core/src/cache/CpuCacheMgr.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#pragma once - -#include -#include - -#include "cache/CacheMgr.h" -#include "cache/DataObj.h" -#include "value/config/ConfigMgr.h" - -namespace milvus { -namespace cache { - -class CpuCacheMgr : public CacheMgr, public ConfigObserver { - public: - static CpuCacheMgr& - GetInstance(); - - private: - CpuCacheMgr(); - - ~CpuCacheMgr(); - - public: - DataObjPtr - GetItem(const std::string& key) override; - - public: - void - ConfigUpdate(const std::string& name) override; -}; - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/DataObj.h b/internal/core/src/cache/DataObj.h deleted file mode 100644 index a4d65c44ba..0000000000 --- a/internal/core/src/cache/DataObj.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#pragma once - -#include - -namespace milvus { -namespace cache { - -class DataObj { - public: - virtual int64_t - Size() = 0; - virtual ~DataObj() = default; -}; - -using DataObjPtr = std::shared_ptr; - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/GpuCacheMgr.cpp b/internal/core/src/cache/GpuCacheMgr.cpp deleted file mode 100644 index 9e5be8babe..0000000000 --- a/internal/core/src/cache/GpuCacheMgr.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#include "cache/GpuCacheMgr.h" -#include "utils/Log.h" -#include "value/config/ServerConfig.h" - -#include -#include -#include - -namespace milvus { -namespace cache { - -#ifdef MILVUS_GPU_VERSION -std::mutex GpuCacheMgr::global_mutex_; -std::unordered_map GpuCacheMgr::instance_; - -GpuCacheMgr::GpuCacheMgr(int64_t gpu_id) : gpu_id_(gpu_id) { - std::string header = "[CACHE GPU" + std::to_string(gpu_id) + "]"; - cache_ = std::make_shared>(config.gpu.cache_size(), 1UL << 32, header); - - if (config.gpu.cache_threshold() > 0.0) { - cache_->set_freemem_percent(config.gpu.cache_threshold()); - } - ConfigMgr::GetInstance().Attach("gpu.cache_threshold", this); -} - -GpuCacheMgr::~GpuCacheMgr() { - ConfigMgr::GetInstance().Detach("gpu.cache_threshold", this); -} - -GpuCacheMgrPtr -GpuCacheMgr::GetInstance(int64_t gpu_id) { - if (instance_.find(gpu_id) == instance_.end()) { - std::lock_guard lock(global_mutex_); - if (instance_.find(gpu_id) == instance_.end()) { - instance_[gpu_id] = std::make_shared(gpu_id); - } - } - return instance_[gpu_id]; -} - -void -GpuCacheMgr::ConfigUpdate(const std::string& name) { - std::lock_guard lock(global_mutex_); - for (auto& it : instance_) { - it.second->SetCapacity(config.gpu.cache_size()); - } -} - -#endif - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/GpuCacheMgr.h b/internal/core/src/cache/GpuCacheMgr.h deleted file mode 100644 index a4d2ae9ed0..0000000000 --- a/internal/core/src/cache/GpuCacheMgr.h +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#include -#include -#include -#include -#include - -#include "cache/CacheMgr.h" -#include "cache/DataObj.h" -#include "value/config/ConfigMgr.h" - -namespace milvus { -namespace cache { - -#ifdef MILVUS_GPU_VERSION -class GpuCacheMgr; -using GpuCacheMgrPtr = std::shared_ptr; -using MutexPtr = std::shared_ptr; - -class GpuCacheMgr : public CacheMgr, public ConfigObserver { - public: - explicit GpuCacheMgr(int64_t gpu_id); - - ~GpuCacheMgr(); - - static GpuCacheMgrPtr - GetInstance(int64_t gpu_id); - - public: - void - ConfigUpdate(const std::string& name) override; - - private: - int64_t gpu_id_; - static std::mutex global_mutex_; - static std::unordered_map instance_; -}; -#endif - -} // namespace cache -} // namespace milvus diff --git a/internal/core/src/cache/LRU.h b/internal/core/src/cache/LRU.h deleted file mode 100644 index e8a853cea0..0000000000 --- a/internal/core/src/cache/LRU.h +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include - -namespace milvus { -namespace cache { - -template -class LRU { - public: - typedef typename std::pair key_value_pair_t; - typedef typename std::list::iterator list_iterator_t; - typedef typename std::list::reverse_iterator reverse_list_iterator_t; - - explicit LRU(size_t max_size) : max_size_(max_size) { - } - - void - put(const key_t& key, const value_t& value) { - auto it = cache_items_map_.find(key); - cache_items_list_.push_front(key_value_pair_t(key, value)); - if (it != cache_items_map_.end()) { - cache_items_list_.erase(it->second); - cache_items_map_.erase(it); - } - cache_items_map_[key] = cache_items_list_.begin(); - - if (cache_items_map_.size() > max_size_) { - auto last = cache_items_list_.end(); - last--; - cache_items_map_.erase(last->first); - cache_items_list_.pop_back(); - } - } - - const value_t& - get(const key_t& key) { - auto it = cache_items_map_.find(key); - if (it == cache_items_map_.end()) { - throw std::range_error("There is no such key in cache"); - } else { - cache_items_list_.splice(cache_items_list_.begin(), cache_items_list_, it->second); - return it->second->second; - } - } - - void - erase(const key_t& key) { - auto it = cache_items_map_.find(key); - if (it != cache_items_map_.end()) { - cache_items_list_.erase(it->second); - cache_items_map_.erase(it); - } - } - - bool - exists(const key_t& key) const { - return cache_items_map_.find(key) != cache_items_map_.end(); - } - - size_t - size() const { - return cache_items_map_.size(); - } - - list_iterator_t - begin() { - iter_ = cache_items_list_.begin(); - return iter_; - } - - list_iterator_t - end() { - return cache_items_list_.end(); - } - - reverse_list_iterator_t - rbegin() { - return cache_items_list_.rbegin(); - } - - reverse_list_iterator_t - rend() { - return cache_items_list_.rend(); - } - - void - clear() { - cache_items_list_.clear(); - cache_items_map_.clear(); - } - - private: - std::list cache_items_list_; - std::unordered_map cache_items_map_; - size_t max_size_; - list_iterator_t iter_; -}; - -} // namespace cache -} // namespace milvus