Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lru-cache.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_H_
16 #define IMPALA_UTIL_LRU_CACHE_H_
17 
18 #include <boost/optional.hpp>
19 #include <boost/thread.hpp>
20 #include <boost/thread/mutex.hpp>
21 #include <boost/unordered_map.hpp>
22 #include <glog/logging.h>
23 #include <list>
24 #include <map>
25 #include <stack>
26 
27 #include "gutil/macros.h"
28 #include "util/spinlock.h"
29 
30 namespace impala {
31 
60 template<typename Key, typename Value>
61 class FifoMultimap {
62  public:
63  typedef std::pair<Key, Value> ValueType;
64 
68  typedef void (*DeleterFn)(Value*);
69 
74  : capacity_(capacity), deleter_(deleter) {}
75 
77  ~FifoMultimap();
78 
81  void Put(const Key& k, const Value& v);
82 
89  bool Pop(const Key& k, Value* out);
90 
92  size_t size(){
93  boost::lock_guard<SpinLock> g(lock_);
94  return cache_.size();
95  }
96 
98  size_t capacity() const { return capacity_; }
99 
100  private:
102 
104  const size_t capacity_;
105 
107 
110 
111  typedef std::list<ValueType> ListType;
112 
117 
118  typedef std::multimap<Key, typename ListType::iterator> MapType;
119 
123 
127  void EvictValue();
128 
129  static void DummyDeleter(Value* v) {}
130 };
131 
132 }
133 
134 #include "lru-cache.inline.h"
135 
136 #endif // IMPALA_UTIL_LRU_CACHE_H_
void Put(const Key &k, const Value &v)
size_t capacity() const
Returns the capacity of the cache.
Definition: lru-cache.h:98
bool Pop(const Key &k, Value *out)
Lightweight spinlock.
Definition: spinlock.h:24
void(* DeleterFn)(Value *)
Definition: lru-cache.h:68
DISALLOW_COPY_AND_ASSIGN(FifoMultimap)
static void DummyDeleter(Value *v)
Definition: lru-cache.h:129
const DeleterFn deleter_
Definition: lru-cache.h:106
size_t size()
Returns the total number of entries in the collection.
Definition: lru-cache.h:92
const size_t capacity_
Total capacity, cannot be changed at run-time.
Definition: lru-cache.h:104
~FifoMultimap()
Walk the list of elements and call the deleter function for each element.
SpinLock lock_
Protects access to cache_ and lru_list_.
Definition: lru-cache.h:109
FifoMultimap(size_t capacity, const DeleterFn &deleter=&FifoMultimap::DummyDeleter)
Definition: lru-cache.h:73
std::multimap< Key, typename ListType::iterator > MapType
Definition: lru-cache.h:118
std::pair< Key, Value > ValueType
Definition: lru-cache.h:63
std::list< ValueType > ListType
Definition: lru-cache.h:111