0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CellCache.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_CellCache_h
23 #define Hypertable_RangeServer_CellCache_h
24 
28 
30 
31 #include <map>
32 #include <memory>
33 #include <mutex>
34 #include <set>
35 
36 namespace Hypertable {
37 
38  struct key_revision_lt {
39  bool operator()(const Key &k1, const Key &k2) const {
40  return k1.revision < k2.revision;
41  }
42  };
43 
44  typedef std::set<Key, key_revision_lt> KeySet;
45 
46 
52  class CellCache : public CellList, public std::enable_shared_from_this<CellCache> {
53 
54  public:
55 
57  struct Statistics {
58  size_t size {};
59  size_t deletes {};
60  int64_t memory_used {};
61  int64_t memory_allocated {};
62  int64_t key_bytes {};
63  int64_t value_bytes {};
64  };
65 
66  CellCache();
68  virtual ~CellCache() { m_cell_map.clear(); }
77  void add(const Key &key, const ByteString value) override;
78 
79  virtual void add_counter(const Key &key, const ByteString value);
80 
81  void split_row_estimate_data(SplitRowDataMapT &split_row_data) override;
82 
86  CellListScannerPtr create_scanner(ScanContext *scan_ctx) override;
87 
88  void lock() { m_mutex.lock(); }
89  void unlock() { m_mutex.unlock(); }
90 
91  size_t size() { std::lock_guard<std::mutex> lock(m_mutex); return m_cell_map.size(); }
92 
93  bool empty() { std::lock_guard<std::mutex> lock(m_mutex); return m_cell_map.empty(); }
94 
98  int64_t memory_used() {
99  std::lock_guard<std::mutex> lock(m_mutex);
100  return m_arena.used();
101  }
102 
106  uint64_t memory_allocated() {
107  std::lock_guard<std::mutex> lock(m_mutex);
108  return m_arena.total();
109  }
110 
111  int64_t logical_size() {
112  std::lock_guard<std::mutex> lock(m_mutex);
113  return m_key_bytes + m_value_bytes;
114  }
115 
116  void add_statistics(Statistics &stats) {
117  std::lock_guard<std::mutex> lock(m_mutex);
118  stats.size += m_cell_map.size();
119  stats.deletes += m_deletes;
120  stats.memory_used += m_arena.used();
121  stats.memory_allocated += m_arena.total();
122  stats.key_bytes += m_key_bytes;
123  stats.value_bytes += m_value_bytes;
124  }
125 
126  int32_t delete_count() {
127  std::lock_guard<std::mutex> lock(m_mutex);
128  return m_deletes;
129  }
130 
131  void populate_key_set(KeySet &keys) {
132  Key key;
133  for (CellMap::const_iterator iter = m_cell_map.begin();
134  iter != m_cell_map.end(); ++iter) {
135  key.load((*iter).first);
136  keys.insert(key);
137  }
138  }
139 
140  CellCacheArena &arena() { return m_arena; }
141 
142  friend class CellCacheScanner;
143 
144  typedef std::pair<const SerializedKey, uint32_t> Value;
146  typedef std::map<const SerializedKey, uint32_t,
147  std::less<const SerializedKey>, Alloc> CellMap;
148 
149  protected:
150 
153  CellMap m_cell_map;
154  int32_t m_deletes {};
155  int32_t m_collisions {};
156  int64_t m_key_bytes {};
157  int64_t m_value_bytes {};
159 
160  };
161 
163  typedef std::shared_ptr<CellCache> CellCachePtr;
164 
165 }
166 
167 #endif // Hypertable_RangeServer_CellCache_h
CellCacheArena m_arena
Definition: CellCache.h:152
size_t total() const
Statistic accessors - returns total allocated size.
Definition: PageArena.h:357
static std::mutex mutex
Definition: Logger.cc:43
uint64_t memory_allocated()
Returns the amount of memory allocated by the CellCache.
Definition: CellCache.h:106
Represents a sorted list of key/value pairs in memory.
Definition: CellCache.h:52
Holds cache statistics.
Definition: CellCache.h:57
bool operator()(const Key &k1, const Key &k2) const
Definition: CellCache.h:39
Scan context information.
Definition: ScanContext.h:52
int64_t memory_used()
Returns the amount of memory used by the CellCache.
Definition: CellCache.h:98
CellListScannerPtr create_scanner(ScanContext *scan_ctx) override
Creates a CellCacheScanner object that contains an shared pointer to this CellCache.
Definition: CellCache.cc:201
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
std::mutex m_mutex
Definition: CellCache.h:151
std::set< Key, key_revision_lt > KeySet
Definition: CellCache.h:44
void populate_key_set(KeySet &keys)
Definition: CellCache.h:131
CellCacheAllocator< Value > Alloc
Definition: CellCache.h:145
std::map< const char *, int64_t, LtCstr, SplitRowDataAlloc > SplitRowDataMapT
Definition: CellList.h:66
Provides a scanning interface to a CellCache.
virtual void add_counter(const Key &key, const ByteString value)
Definition: CellCache.cc:83
CellCacheArena & arena()
Definition: CellCache.h:140
bool load(const SerializedKey &key)
Parses the opaque key and loads the components into the member variables.
Definition: Key.cc:158
int64_t logical_size()
Definition: CellCache.h:111
virtual ~CellCache()
Definition: CellCache.h:68
void add_statistics(Statistics &stats)
Definition: CellCache.h:116
std::pair< const SerializedKey, uint32_t > Value
Definition: CellCache.h:144
Hypertable definitions
size_t used() const
Statistic accessors - returns used bytes.
Definition: PageArena.h:351
std::map< const SerializedKey, uint32_t, std::less< const SerializedKey >, Alloc > CellMap
Definition: CellCache.h:147
Provides access to internal components of opaque key.
Definition: Key.h:40
void split_row_estimate_data(SplitRowDataMapT &split_row_data) override
Populates split_row_data with unique row and count estimates for this list.
Definition: CellCache.cc:170
void add(const Key &key, const ByteString value) override
Adds a key/value pair to the CellCache.
Definition: CellCache.cc:51
int64_t revision
Definition: Key.h:135
std::shared_ptr< CellListScanner > CellListScannerPtr
Definition: CellList.h:35
int32_t delete_count()
Definition: CellCache.h:126
std::shared_ptr< CellCache > CellCachePtr
Shared smart pointer to CellCache.
Definition: CellCache.h:163
Abstract base class for cell lists (sorted lists of key/value pairs).
Definition: CellList.h:42