OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LRUEvictionAlgorithm.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  TODO(Misiu): This algorithm can be replaced with the LruCache implemented in
19  the StringDictionary. However, that implementation is missing functionality
20  such as the ability to remove arbitrary chunks, (which we need to selectively
21  clear the cache). This functionality could be added to the StringDict version
22  but for now we're replicating algorithm here.
23 */
24 
25 #include "LRUEvictionAlgorithm.h"
26 
29  if (cache_items_list_.size() < 1) {
30  throw NoEntryFoundException();
31  }
32  auto last = cache_items_list_.back();
33  CHECK(cache_items_map_.erase(last) > 0) << "Chunk not evicted!";
34  cache_items_list_.pop_back();
35  return last;
36 }
37 
40  auto it = cache_items_map_.find(key);
41  if (it != cache_items_map_.end()) {
42  cache_items_list_.erase(it->second);
43  cache_items_map_.erase(it);
44  }
45  cache_items_list_.emplace_front(key);
46  cache_items_map_[key] = cache_items_list_.begin();
47 }
48 
51  auto it = cache_items_map_.find(key);
52  if (it == cache_items_map_.end()) {
53  return;
54  }
55  cache_items_list_.erase(it->second);
56  cache_items_map_.erase(key);
57 }
58 
61  std::string ret = "Eviction queue:\n{";
62  for (auto chunk : cache_items_list_) {
63  ret += show_chunk(chunk) + ", ";
64  }
65  ret += "}\n";
66  return ret;
67 }
heavyai::shared_mutex cache_mutex_
std::vector< int > ChunkKey
Definition: types.h:36
std::list< ChunkKey > cache_items_list_
const ChunkKey evictNextChunk() override
void touchChunk(const ChunkKey &) override
std::map< const ChunkKey, std::list< ChunkKey >::iterator > cache_items_map_
void removeChunk(const ChunkKey &) override
std::string show_chunk(const ChunkKey &key)
Definition: types.h:98
std::shared_lock< T > shared_lock
std::unique_lock< T > unique_lock
#define CHECK(condition)
Definition: Logger.h:289
This file includes the class specification for the Least Recently Used cache eviction algorithm used ...