Query cache. More...
#include <QueryCache.h>
Classes | |
class | Key |
Hash key to query cache. More... | |
struct | KeyHash |
class | QueryCacheEntry |
Internal cache entry. More... | |
class | RowKey |
Row key information. More... | |
struct | RowKeyHash |
Public Member Functions | |
QueryCache (uint64_t max_memory) | |
Constructor. More... | |
bool | insert (Key *key, const char *tablename, const char *row, std::set< uint8_t > &columns, uint32_t cell_count, boost::shared_array< uint8_t > &result, uint32_t result_length) |
Inserts a query result. More... | |
bool | lookup (Key *key, boost::shared_array< uint8_t > &result, uint32_t *lenp, uint32_t *cell_count) |
Lookup. More... | |
void | invalidate (const char *tablename, const char *row, std::set< uint8_t > &columns) |
Invalidates cache entries. More... | |
uint64_t | available_memory () |
Gets available memory. More... | |
uint64_t | memory_used () |
Gets memory used. More... | |
void | get_stats (uint64_t *max_memoryp, uint64_t *available_memoryp, uint64_t *total_lookupsp, uint64_t *total_hitsp, int32_t *total_waiters) |
Gets cache statistics. More... | |
void | dump_keys (std::ofstream &out) |
Dumps keys to output file. More... | |
Private Types | |
typedef boost::multi_index_container < QueryCacheEntry, indexed_by < sequenced<>, hashed_unique < const_mem_fun < QueryCacheEntry, Key,&QueryCacheEntry::lookup_key > , KeyHash >, hashed_non_unique < const_mem_fun < QueryCacheEntry, RowKey,&QueryCacheEntry::invalidate_key > , RowKeyHash > > > | Cache |
typedef Cache::nth_index< 0 >::type | Sequence |
typedef Cache::nth_index< 1 >::type | LookupHashIndex |
typedef Cache::nth_index< 2 >::type | InvalidateHashIndex |
Private Attributes | |
MutexWithStatistics | m_mutex |
Mutex to serialize member access More... | |
Cache | m_cache |
Internal cache data structure. More... | |
uint64_t | m_max_memory {} |
Maximum memory to be used by cache. More... | |
uint64_t | m_avail_memory {} |
Available memory. More... | |
uint64_t | m_total_lookup_count {} |
Total lookup count. More... | |
uint64_t | m_total_hit_count {} |
Total hit count. More... | |
uint32_t | m_recent_hit_count {} |
Recent hit count (for logging) More... | |
Query cache.
Definition at line 47 of file QueryCache.h.
|
private |
Definition at line 223 of file QueryCache.h.
|
private |
Definition at line 227 of file QueryCache.h.
|
private |
Definition at line 226 of file QueryCache.h.
|
private |
Definition at line 225 of file QueryCache.h.
QueryCache::QueryCache | ( | uint64_t | max_memory | ) |
Constructor.
Initializes m_max_memory and m_avail_memory to max_memory
.
max_memory | Maximum amount of memory to be used by the cache |
Definition at line 40 of file QueryCache.cc.
|
inline |
Gets available memory.
Returns m_avail_memory
Definition at line 152 of file QueryCache.h.
void QueryCache::dump_keys | ( | std::ofstream & | out | ) |
Dumps keys to output file.
out | Output file to dump keys to |
Definition at line 137 of file QueryCache.cc.
void QueryCache::get_stats | ( | uint64_t * | max_memoryp, |
uint64_t * | available_memoryp, | ||
uint64_t * | total_lookupsp, | ||
uint64_t * | total_hitsp, | ||
int32_t * | total_waiters | ||
) |
Gets cache statistics.
max_memoryp | Address of variable to hold max memory. |
available_memoryp | Address of variable to hold available memory. |
total_lookupsp | Address of variable to hold total lookups. |
total_hitsp | Address of variable to hold total hits. |
total_waiters | Address of variable to hold number of threads waiting on the mutex |
Definition at line 125 of file QueryCache.cc.
bool QueryCache::insert | ( | Key * | key, |
const char * | tablename, | ||
const char * | row, | ||
std::set< uint8_t > & | columns, | ||
uint32_t | cell_count, | ||
boost::shared_array< uint8_t > & | result, | ||
uint32_t | result_length | ||
) |
Inserts a query result.
If the size of the entry is greater than m_max_memory, then the function returns without modifying the cache. Then the old entry is removed, if there was one. Then room is created in the cache for the new entry by removing the oldest entries until enough space is available. Finally, a new cache entry is created and inserted into the cache. This function also maintains the m_avail_memory value which represents how much room is available in the cache. It is computed as m_max_memory minus an approximation of how much space is taken up by the existing cache entries.
key | Hash key for entry to be inserted |
tablename | Table name for entry to be inserted (must remain valid for lifetime of cache entry) |
row | Row of entry to be inserted (must remain valid for lifetime of cache entry) |
columns | Set of column IDs from scan specification used to create entry to be inserted |
cell_count | Count of cells in entry to be inserted |
result | Query result |
result_length | Length of query result |
Definition at line 47 of file QueryCache.cc.
void QueryCache::invalidate | ( | const char * | tablename, |
const char * | row, | ||
std::set< uint8_t > & | columns | ||
) |
Invalidates cache entries.
Creates a RowKey object from tablename
and row
and finds all matching entires in the cache. For each matching cache entry whose columns intersect with columns
, the entry is invalidated. The entry is also invalidated if either columns
is empty or the cache entries columns are empty.
tablename | Table of entries to invalidate |
row | Row entries to invalidate |
columns | Columns of entries to invalidate |
HT_ASSERT(strcmp((*p.first).row_key.tablename, tablename) == 0 && strcmp((*p.first).row_key.row.c_str(), row) == 0);
Definition at line 162 of file QueryCache.cc.
bool QueryCache::lookup | ( | Key * | key, |
boost::shared_array< uint8_t > & | result, | ||
uint32_t * | lenp, | ||
uint32_t * | cell_count | ||
) |
Lookup.
Looks up the entry with key key
, and if found, returns the query result and associated information in result
, lenp
, and cell_count
. Also, if a cache entry is found, it is removed and re-inserted for LRU ordering.
key | Hash key |
result | Reference to shared array to hold result |
lenp | Pointer to variable to hold result length |
cell_count | Pointer to variable to hold count of cells in result |
Definition at line 91 of file QueryCache.cc.
|
inline |
Gets memory used.
Memory used is calculated as m_max_memory minus m_avail_memory.
Definition at line 160 of file QueryCache.h.
|
private |
Available memory.
Definition at line 239 of file QueryCache.h.
|
private |
Internal cache data structure.
Definition at line 233 of file QueryCache.h.
|
private |
Maximum memory to be used by cache.
Definition at line 236 of file QueryCache.h.
|
private |
Mutex to serialize member access
Definition at line 230 of file QueryCache.h.
|
private |
Recent hit count (for logging)
Definition at line 248 of file QueryCache.h.
|
private |
Total hit count.
Definition at line 245 of file QueryCache.h.
|
private |
Total lookup count.
Definition at line 242 of file QueryCache.h.