Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::FifoMultimap< Key, Value > Class Template Reference

#include <lru-cache.h>

Collaboration diagram for impala::FifoMultimap< Key, Value >:

Public Types

typedef std::pair< Key, Value > ValueType
 
typedef void(* DeleterFn )(Value *)
 

Public Member Functions

 FifoMultimap (size_t capacity, const DeleterFn &deleter=&FifoMultimap::DummyDeleter)
 
 ~FifoMultimap ()
 Walk the list of elements and call the deleter function for each element. More...
 
void Put (const Key &k, const Value &v)
 
bool Pop (const Key &k, Value *out)
 
size_t size ()
 Returns the total number of entries in the collection. More...
 
size_t capacity () const
 Returns the capacity of the cache. More...
 

Private Types

typedef std::list< ValueTypeListType
 
typedef std::multimap< Key,
typename ListType::iterator > 
MapType
 

Private Member Functions

 DISALLOW_COPY_AND_ASSIGN (FifoMultimap)
 
void EvictValue ()
 

Static Private Member Functions

static void DummyDeleter (Value *v)
 

Private Attributes

const size_t capacity_
 Total capacity, cannot be changed at run-time. More...
 
const DeleterFn deleter_
 
SpinLock lock_
 Protects access to cache_ and lru_list_. More...
 
ListType lru_list_
 
MapType cache_
 

Detailed Description

template<typename Key, typename Value>
class impala::FifoMultimap< Key, Value >

Implementation of a FifoMultimap.

The FifoMultimap is a cache-like data structure that keeps capacity number of key-value pairs organized and accessible by key allowing multiple values per key. If capacity is reached, key-value pairs are evicted in the least recently added order.

When accessing the values that have identical keys (via Pop(k)), values are typically returned in LIFO order of this key. However, this property can only be guaranteed when compiled with C++11 enabled. The motivation for accessing values in this particular order is to avoid keeping all values of a particular key hot, even though only a subset of the values is actually used.

On destruction of this class all resources are freed using the optional deleter function.

This class is thread-safe and some methods may block under contention. This class cannot be copied or assigned.

Example of a cache with capacity 3:

  • First <K1, V1> is added to the cache, then <K2, V2>, <K2, V3>.

Eviction case: If a new key value pair is added <K1, V1> will be evicted as it is the oldest entry. (FIFO)

Element access: If K2 is accessed, V3 will be returned first. (LIFO)

This class is thread-safe and protects its members using a spin lock.

Definition at line 61 of file lru-cache.h.

Member Typedef Documentation

template<typename Key, typename Value>
typedef void(* impala::FifoMultimap< Key, Value >::DeleterFn)(Value *)

Deleter function type used to allow cleanup of resources held by Value when it is evicted. This method should only be used if it is not possible to embed the cleanup logic in the Value class itself.

Definition at line 68 of file lru-cache.h.

template<typename Key, typename Value>
typedef std::list<ValueType> impala::FifoMultimap< Key, Value >::ListType
private

Definition at line 111 of file lru-cache.h.

template<typename Key, typename Value>
typedef std::multimap<Key, typename ListType::iterator> impala::FifoMultimap< Key, Value >::MapType
private

Definition at line 118 of file lru-cache.h.

template<typename Key, typename Value>
typedef std::pair<Key, Value> impala::FifoMultimap< Key, Value >::ValueType

Definition at line 63 of file lru-cache.h.

Constructor & Destructor Documentation

template<typename Key, typename Value>
impala::FifoMultimap< Key, Value >::FifoMultimap ( size_t  capacity,
const DeleterFn deleter = &FifoMultimap< Key, Value >::DummyDeleter 
)
inline

Instantiates the collection with an upper bound of capacity key-value pairs stored in the FifoMultimap and a deleter function pointer that can be used to free resources when a value is evicted from the FifoMultimap.

Definition at line 73 of file lru-cache.h.

template<typename Key , typename Value >
impala::FifoMultimap< Key, Value >::~FifoMultimap ( )

Walk the list of elements and call the deleter function for each element.

Definition at line 21 of file lru-cache.inline.h.

Member Function Documentation

template<typename Key, typename Value>
size_t impala::FifoMultimap< Key, Value >::capacity ( ) const
inline

Returns the capacity of the cache.

Definition at line 98 of file lru-cache.h.

References impala::FifoMultimap< Key, Value >::capacity_.

Referenced by TEST().

template<typename Key, typename Value>
impala::FifoMultimap< Key, Value >::DISALLOW_COPY_AND_ASSIGN ( FifoMultimap< Key, Value >  )
private
template<typename Key, typename Value>
static void impala::FifoMultimap< Key, Value >::DummyDeleter ( Value *  v)
inlinestaticprivate

Definition at line 129 of file lru-cache.h.

template<typename Key , typename Value >
void impala::FifoMultimap< Key, Value >::EvictValue ( )
private

Evicts the least recently added element from the cache. First, the element is removed from both internal collections and as a last step the deleter_ function is called to free potentially acquired resources.

Definition at line 60 of file lru-cache.inline.h.

template<typename Key , typename Value >
bool impala::FifoMultimap< Key, Value >::Pop ( const Key &  k,
Value *  out 
)

Accesses an element of the collection by key and returns the value. In the process the key-value pair is removed from the collection. If the element is not found, returns boost::none.

Typically, this will return the last element that was added under key k in case of duplicate keys.

Definition at line 47 of file lru-cache.inline.h.

References impala::lock_.

Referenced by ParallelPut(), and TEST().

template<typename Key , typename Value >
void impala::FifoMultimap< Key, Value >::Put ( const Key &  k,
const Value &  v 
)

Add a key-value pair to the collection. Uniqueness of keys is not enforced. If the capacity is exceeded the oldest element will be evicted.

Definition at line 29 of file lru-cache.inline.h.

References impala::lock_.

Referenced by ParallelPut(), and TEST().

template<typename Key, typename Value>
size_t impala::FifoMultimap< Key, Value >::size ( )
inline

Returns the total number of entries in the collection.

Definition at line 92 of file lru-cache.h.

References impala::FifoMultimap< Key, Value >::cache_, and impala::FifoMultimap< Key, Value >::lock_.

Referenced by TEST().

Member Data Documentation

template<typename Key, typename Value>
MapType impala::FifoMultimap< Key, Value >::cache_
private

Underlying associative container, that maps a key to an entry in the recently used list. The iterator on the list is only invalidated if the key-value pair is removed.

Definition at line 122 of file lru-cache.h.

Referenced by impala::FifoMultimap< Key, Value >::size().

template<typename Key, typename Value>
const size_t impala::FifoMultimap< Key, Value >::capacity_
private

Total capacity, cannot be changed at run-time.

Definition at line 104 of file lru-cache.h.

Referenced by impala::FifoMultimap< Key, Value >::capacity().

template<typename Key, typename Value>
const DeleterFn impala::FifoMultimap< Key, Value >::deleter_
private

Definition at line 106 of file lru-cache.h.

template<typename Key, typename Value>
SpinLock impala::FifoMultimap< Key, Value >::lock_
private

Protects access to cache_ and lru_list_.

Definition at line 109 of file lru-cache.h.

Referenced by impala::FifoMultimap< Key, Value >::size().

template<typename Key, typename Value>
ListType impala::FifoMultimap< Key, Value >::lru_list_
private

The least recently added item is stored at the beginning of the list. The length of the list is limited to capacity number of elements. New elements are always appended to the end of the list.

Definition at line 116 of file lru-cache.h.


The documentation for this class was generated from the following files: