0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
QueryCache.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #ifndef Hypertable_RangeServer_QueryCache_h
23 #define Hypertable_RangeServer_QueryCache_h
24 
25 #include <Common/Checksum.h>
26 #include <Common/Mutex.h>
27 
28 #include <boost/multi_index_container.hpp>
29 #include <boost/multi_index/hashed_index.hpp>
30 #include <boost/multi_index/mem_fun.hpp>
31 #include <boost/multi_index/sequenced_index.hpp>
32 #include <boost/multi_index/member.hpp>
33 #include <boost/shared_array.hpp>
34 
35 #include <algorithm>
36 #include <cstring>
37 #include <fstream>
38 #include <set>
39 
40 namespace Hypertable {
41  using namespace boost::multi_index;
42 
45 
47  class QueryCache {
48 
49  public:
50 
54  class Key {
55  public:
59  bool operator==(const Key &other) const {
60  return memcmp(digest, other.digest, 16) == 0;
61  }
63  uint64_t digest[2];
64  };
65 
67  class RowKey {
68  public:
77  RowKey(const char *t, const char *r) : tablename(t), row(r) {
78  hash = fletcher32(t, strlen(t)) ^ fletcher32(r, strlen(r));
79  }
83  bool operator==(const RowKey &other) const {
84  return !strcmp(tablename, other.tablename) && !strcmp(row, other.row);
85  }
87  const char *tablename;
89  const char *row;
91  uint32_t hash;
92  };
93 
98  QueryCache(uint64_t max_memory);
99 
121  bool insert(Key *key, const char *tablename, const char *row,
122  std::set<uint8_t> &columns, uint32_t cell_count,
123  boost::shared_array<uint8_t> &result, uint32_t result_length);
124 
135  bool lookup(Key *key, boost::shared_array<uint8_t> &result, uint32_t *lenp,
136  uint32_t *cell_count);
137 
147  void invalidate(const char * tablename, const char *row, std::set<uint8_t> &columns);
148 
152  uint64_t available_memory() {
153  std::lock_guard<MutexWithStatistics> lock(m_mutex);
154  return m_avail_memory;
155  }
156 
160  uint64_t memory_used() {
161  std::lock_guard<MutexWithStatistics> lock(m_mutex);
162  return m_max_memory-m_avail_memory;
163  }
164 
172  void get_stats(uint64_t *max_memoryp, uint64_t *available_memoryp,
173  uint64_t *total_lookupsp, uint64_t *total_hitsp,
174  int32_t *total_waiters);
175 
178  void dump_keys(std::ofstream &out);
179 
180  private:
181 
184  public:
185  QueryCacheEntry(Key &k, const char *tname, const char *rw,
186  std::set<uint8_t> &column_ids, uint32_t cells,
187  boost::shared_array<uint8_t> &res, uint32_t rlen) :
188  key(k), row_key(tname, rw), result(res), result_length(rlen), cell_count(cells) {
189  columns.swap(column_ids);
190  }
191  Key lookup_key() const { return key; }
192  RowKey invalidate_key() const { return row_key; }
193  void dump() { std::cout << row_key.tablename << ":" << row_key.row << "\n"; }
196  std::set<uint8_t> columns;
197  boost::shared_array<uint8_t> result;
198  uint32_t result_length;
199  uint32_t cell_count;
200  };
201 
202  struct KeyHash {
203  std::size_t operator()(const Key k) const {
204  return (std::size_t)k.digest[0];
205  }
206  };
207 
208  struct RowKeyHash {
209  std::size_t operator()(const RowKey k) const {
210  return k.hash;
211  }
212  };
213 
214  typedef boost::multi_index_container<
215  QueryCacheEntry,
216  indexed_by<
217  sequenced<>,
218  hashed_unique<const_mem_fun<QueryCacheEntry, Key,
219  &QueryCacheEntry::lookup_key>, KeyHash>,
220  hashed_non_unique<const_mem_fun<QueryCacheEntry, RowKey,
221  &QueryCacheEntry::invalidate_key>, RowKeyHash>
222  >
223  > Cache;
224 
225  typedef Cache::nth_index<0>::type Sequence;
226  typedef Cache::nth_index<1>::type LookupHashIndex;
227  typedef Cache::nth_index<2>::type InvalidateHashIndex;
228 
231 
234 
236  uint64_t m_max_memory {};
237 
239  uint64_t m_avail_memory {};
240 
242  uint64_t m_total_lookup_count {};
243 
245  uint64_t m_total_hit_count {};
246 
248  uint32_t m_recent_hit_count {};
249  };
250 
252  typedef std::shared_ptr<QueryCache> QueryCachePtr;
253 
255 
256 }
257 
258 
259 #endif // Hypertable_RangeServer_QueryCache_h
uint32_t cell_count
Definition: QueryCache.h:199
void dump()
Definition: QueryCache.h:193
RowKey row_key
Definition: QueryCache.h:195
QueryCacheEntry(Key &k, const char *tname, const char *rw, std::set< uint8_t > &column_ids, uint32_t cells, boost::shared_array< uint8_t > &res, uint32_t rlen)
Definition: QueryCache.h:185
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
Definition: QueryCache.h:223
MutexWithStatistics m_mutex
Mutex to serialize member access
Definition: QueryCache.h:230
bool operator==(const Key &other) const
Equality operator.
Definition: QueryCache.h:59
uint32_t result_length
Definition: QueryCache.h:198
Internal cache entry.
Definition: QueryCache.h:183
Cache::nth_index< 2 >::type InvalidateHashIndex
Definition: QueryCache.h:227
uint32_t fletcher32(const void *data8, size_t len8)
Compute fletcher32 checksum for arbitary data.
Definition: Checksum.cc:42
Hash key to query cache.
Definition: QueryCache.h:54
const char * tablename
Table name
Definition: QueryCache.h:87
std::size_t operator()(const RowKey k) const
Definition: QueryCache.h:209
uint32_t hash
Hash code computed from tablename and row.
Definition: QueryCache.h:91
Cache::nth_index< 0 >::type Sequence
Definition: QueryCache.h:225
Query cache.
Definition: QueryCache.h:47
std::set< uint8_t > columns
Definition: QueryCache.h:196
boost::shared_array< uint8_t > result
Definition: QueryCache.h:197
std::shared_ptr< QueryCache > QueryCachePtr
Smart pointer to QueryCache.
Definition: QueryCache.h:252
bool operator==(const RowKey &other) const
Equality operator.
Definition: QueryCache.h:83
Mutex that maintains wait threads count.
Definition: Mutex.h:42
RowKey(const char *t, const char *r)
Constructor.
Definition: QueryCache.h:77
Hypertable definitions
Implementation of checksum routines.
Scoped lockers for recursive and non-recursive mutexes.
Key lookup_key() const
Definition: QueryCache.h:191
Provides access to internal components of opaque key.
Definition: Key.h:40
Row key information.
Definition: QueryCache.h:67
Cache m_cache
Internal cache data structure.
Definition: QueryCache.h:233
Cache::nth_index< 1 >::type LookupHashIndex
Definition: QueryCache.h:226
Key key
Definition: QueryCache.h:194
RowKey invalidate_key() const
Definition: QueryCache.h:192
uint64_t memory_used()
Gets memory used.
Definition: QueryCache.h:160
uint64_t available_memory()
Gets available memory.
Definition: QueryCache.h:152
std::size_t operator()(const Key k) const
Definition: QueryCache.h:203
uint64_t digest[2]
Hash digest.
Definition: QueryCache.h:63