Apollo  6.0
Open source self driving car software
base_map_cache.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 #include <functional>
19 #include <utility>
20 #include "cyber/common/log.h"
21 
23 
24 namespace apollo {
25 namespace localization {
26 namespace msf {
27 
28 template <class Key, class Element>
30 
32 template <class Key, class Element, class MapLRUCache = LRUCache<Key, Element>>
33 class MapNodeCache {
34  public:
35  using DestroyFunc = std::function<bool(Element*)>;
36  static bool CacheL1Destroy(Element* value) {
37  value->SetIsReserved(false);
38  return true;
39  }
40  static bool CacheL2Destroy(Element* value) {
41  return !(value->GetIsReserved());
42  }
43 
44  public:
46  MapNodeCache(unsigned int capacity, const DestroyFunc& destroy_func)
47  : destroy_func_(destroy_func), lru_map_nodes_(capacity) {}
52  bool Get(const Key& key, Element** value);
56  bool GetSilent(const Key& key, Element** value);
59  Element* Put(const Key& key, Element* value);
62  Element* Remove(const Key& key);
65  Element* ClearOne();
66  // bool clear();
69  bool IsExist(const Key& key);
70 
73  bool ChangeCapacity(int capacity) {
74  return lru_map_nodes_.ChangeCapacity(capacity);
75  }
77  unsigned int Size() { return lru_map_nodes_.size(); }
79  unsigned Capacity() { return lru_map_nodes_.capacity(); }
80 
81  private:
86  const DestroyFunc destroy_func_;
87  MapLRUCache lru_map_nodes_;
88 };
89 
90 template <class Key, class Element, class MapLRUCache>
92  Element** value) {
93  auto value_ptr = lru_map_nodes_.Get(key);
94  if (!value_ptr) {
95  return false;
96  }
97  *value = *value_ptr;
98  return true;
99 }
100 
101 template <class Key, class Element, class MapLRUCache>
103  Element** value) {
104  auto value_ptr = lru_map_nodes_.GetSilently(key);
105  if (!value_ptr) {
106  return false;
107  }
108  *value = *value_ptr;
109  return true;
110 }
111 
112 template <class Key, class Element, class MapLRUCache>
114  Element* value) {
115  if (value == nullptr) {
116  AINFO << "LRUCache Warning: put a NULL";
117  return nullptr;
118  }
119 
120  auto* value_ptr = lru_map_nodes_.Get(key);
121  Element* node_remove = nullptr;
122  if (value_ptr) {
123  node_remove = *value_ptr;
124  if (destroy_func_(node_remove)) {
125  *value_ptr = value;
126  } else {
127  node_remove = value;
128  }
129  return node_remove;
130  }
131 
132  if (lru_map_nodes_.size() >= lru_map_nodes_.capacity()) {
133  auto* node = lru_map_nodes_.Last();
134  node_remove = node->val;
135  Key key_tmp;
136  lru_map_nodes_.PutAndGetObsolete(key, &value, &key_tmp);
137  return node_remove;
138  }
139 
140  lru_map_nodes_.Put(key, std::move(value));
141  return node_remove;
142 }
143 
144 template <class Key, class Element, class MapLRUCache>
146  auto* node_remove = lru_map_nodes_.GetSilently(key);
147  if (node_remove && lru_map_nodes_.Remove(key)) {
148  return *node_remove;
149  }
150 
151  return nullptr;
152 }
153 
154 template <class Key, class Element, class MapLRUCache>
156  auto* node_remove = lru_map_nodes_.Last();
157  if (!node_remove) {
158  return nullptr;
159  }
160  while (node_remove != lru_map_nodes_.First()) {
161  if (destroy_func_(node_remove->val)) {
162  lru_map_nodes_.Remove(node_remove->key);
163  return node_remove->val;
164  }
165  node_remove = node_remove->prev;
166  }
167  if (node_remove == lru_map_nodes_.First() &&
168  destroy_func_(node_remove->val)) {
169  lru_map_nodes_.Remove(node_remove->key);
170  return node_remove->val;
171  }
172  return nullptr;
173 }
174 
175 template <class Key, class Element, class MapLRUCache>
177  return lru_map_nodes_.Prioritize(key);
178 }
179 
180 } // namespace msf
181 } // namespace localization
182 } // namespace apollo
bool IsExist(const Key &key)
Find element for key in the cache. If it exists, move it to the head of queue.
Definition: base_map_cache.h:176
The data structure of the LRUCache.
Definition: base_map_cache.h:33
Definition: lru_cache.h:43
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
unsigned int Size()
return cache&#39;s in use.
Definition: base_map_cache.h:77
bool GetSilent(const Key &key, Element **value)
Find element for key if it exists in the cache. If not exist, return false. This function is thread s...
Definition: base_map_cache.h:102
~MapNodeCache()
The destructor.
Definition: base_map_cache.h:49
static bool CacheL1Destroy(Element *value)
Definition: base_map_cache.h:36
MapNodeCache(unsigned int capacity, const DestroyFunc &destroy_func)
The constructor.
Definition: base_map_cache.h:46
bool ChangeCapacity(int capacity)
Change cache&#39;s max capacity. New capacity must be larger than size in use.
Definition: base_map_cache.h:73
std::function< bool(apollo::localization::msf::pyramid_map::BaseMapNode *)> DestroyFunc
Definition: base_map_cache.h:35
Element * Put(const Key &key, Element *value)
Caches element for key. If cache is full, return the removed element, otherwise return null...
Definition: base_map_cache.h:113
unsigned Capacity()
return cache&#39;s max capacity.
Definition: base_map_cache.h:79
static bool CacheL2Destroy(Element *value)
Definition: base_map_cache.h:40
bool Get(const Key &key, Element **value)
Find element for key if it exists in the cache. If not exist, return false.
Definition: base_map_cache.h:91
apollo::cyber::base::std value
Element * Remove(const Key &key)
Remove element for key. if it exist in the cache, return the element, otherwise return null...
Definition: base_map_cache.h:145
#define AINFO
Definition: log.h:42
Element * ClearOne()
Remove the Least Recently Used element in the cache. return the removed element or null...
Definition: base_map_cache.h:155