Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lru-cache.inline.h
Go to the documentation of this file.
1 // Copyright 2015 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IMPALA_UTIL_LRU_CACHE_INLINE_H_
16 #define IMPALA_UTIL_LRU_CACHE_INLINE_H_
17 
18 namespace impala {
19 
20 template <typename Key, typename Value>
22  for (typename ListType::iterator b = lru_list_.begin(),
23  e = lru_list_.end(); b != e; ++b) {
24  deleter_(&(b->second));
25  }
26 }
27 
28 template <typename Key, typename Value>
29 void FifoMultimap<Key, Value>::Put(const Key& k, const Value& v) {
30  boost::lock_guard<SpinLock> g(lock_);
31  if (lru_list_.size() >= capacity_) EvictValue();
32  const ValueType& kv_pair = std::make_pair(k, v);
33  const typename MapType::value_type& val =
34  std::make_pair(k, lru_list_.insert(lru_list_.end(), kv_pair));
35  // Find an insert hint to use with the insert operation
36  typename MapType::iterator it = cache_.lower_bound(k);
37  if (it == cache_.end() || it->first != k) {
38  cache_.insert(val);
39  } else {
40  // If the insert hint is sufficiently good, the element will be inserted before in the
41  // sequence of elements having the same key
42  cache_.insert(it, val);
43  }
44 }
45 
46 template <typename Key, typename Value>
47 bool FifoMultimap<Key, Value>::Pop(const Key& k, Value* out) {
48  boost::lock_guard<SpinLock> g(lock_);
49  // Find the first value under key k.
50  typename MapType::iterator it = cache_.lower_bound(k);
51  if (it == cache_.end() || it->first != k) return false;
52  typename ListType::iterator lit = it->second;
53  cache_.erase(it);
54  *out = lit->second;
55  lru_list_.erase(lit);
56  return true;
57 }
58 
59 template <typename Key, typename Value>
61  typename ListType::iterator to_evict = lru_list_.begin();
62  // Find all elements under this key, until C++11 the order of the elements is not
63  // guaranteed.
64  std::pair<typename MapType::iterator, typename MapType::iterator> range =
65  cache_.equal_range(to_evict->first);
66  // Search until the value is found in the range of the same key.
67  while (range.first != range.second) {
68  if (range.first->second == to_evict) {
69  cache_.erase(range.first);
70  break;
71  }
72  ++range.first;
73  }
74  DCHECK(range.first != range.second); // LCOV_EXCL_LINE
75  deleter_(&(*to_evict).second);
76  lru_list_.erase(to_evict);
77 }
78 
79 }
80 
81 #endif // IMPALA_UTIL_LRU_CACHE_INLINE_H_
void Put(const Key &k, const Value &v)
bool Pop(const Key &k, Value *out)
boost::mutex lock_
protects all fields below
Definition: coordinator.h:233
~FifoMultimap()
Walk the list of elements and call the deleter function for each element.
std::pair< Key, Value > ValueType
Definition: lru-cache.h:63