OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LRUEvictionAlgorithm.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 
28  if (cache_items_list_.size() < 1)
29  throw NoEntryFoundException();
30  auto last = cache_items_list_.end();
31  last--;
32  cache_items_map_.erase(*last);
33  const ChunkKey ret = cache_items_list_.back();
34  cache_items_list_.pop_back();
35  return ret;
36 }
37 
39  auto it = cache_items_map_.find(key);
40  cache_items_list_.emplace_front(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_map_[key] = cache_items_list_.begin();
46 }
47 
49  auto it = cache_items_map_.find(key);
50  if (it == cache_items_map_.end())
51  return;
52  cache_items_list_.erase(it->second);
53  cache_items_map_.erase(key);
54 }
55 
57  std::string ret = "Eviction queue:\n{";
58  for (auto chunk : cache_items_list_)
59  ret += show_chunk(chunk) + ", ";
60  ret += "}\n";
61  return ret;
62 }
std::vector< int > ChunkKey
Definition: types.h:37
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:73