0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CellCacheScanner.cc
Go to the documentation of this file.
1 /*
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 #include <Common/Compat.h>
23 
24 #include "CellCacheScanner.h"
25 #include "Global.h"
26 
27 #include <Hypertable/Lib/Key.h>
28 
29 #include <Common/Logger.h>
30 
31 #include <algorithm>
32 #include <cassert>
33 
34 using namespace Hypertable;
35 using namespace std;
36 
38  ScanContext *scan_ctx)
39  : CellListScanner(scan_ctx), m_cell_cache_ptr(cellcache),
40  m_cell_cache_mutex(cellcache->m_mutex) {
41  lock_guard<mutex> lock(m_cell_cache_mutex);
42  DynamicBuffer current_buf;
43  Key current;
44  String tmp_str;
45 
46  m_keys_only = (scan_ctx->spec) ? (scan_ctx->spec->keys_only && !scan_ctx->spec->value_regexp) : false;
47 
48  current_buf.grow(scan_ctx->start_key.row_len +
50  scan_ctx->end_key.row_len +
51  scan_ctx->end_key.column_qualifier_len + 32);
52 
53 
61  if (scan_ctx->has_cell_interval) {
62  CellCache::CellMap::iterator iter;
63 
69  scan_ctx->start_key.row, 0,
70  "", TIMESTAMP_MAX, 0);
71 
72  current.serial.ptr = current_buf.base;
73 
74  for (iter = m_cell_cache_ptr->m_cell_map.lower_bound(current.serial);
75  iter != m_cell_cache_ptr->m_cell_map.end(); ++iter) {
76  current.load(iter->first);
77  if (current.flag != FLAG_DELETE_ROW ||
78  strcmp(current.row, scan_ctx->start_key.row))
79  break;
80  m_deletes.insert(CellCache::CellMap::value_type(iter->first, iter->second));
81  }
82 
83  if (scan_ctx->has_start_cf_qualifier) {
84 
85  current_buf.clear();
87  scan_ctx->start_key.row,
88  scan_ctx->start_key.column_family_code,
89  "", TIMESTAMP_MAX, 0);
90 
91  current.serial.ptr = current_buf.base;
92 
93  for (iter = m_cell_cache_ptr->m_cell_map.lower_bound(current.serial);
94  iter != m_cell_cache_ptr->m_cell_map.end(); ++iter) {
95  current.load(iter->first);
96  if (current.flag != FLAG_DELETE_COLUMN_FAMILY ||
97  current.column_family_code != scan_ctx->start_key.column_family_code ||
98  strcmp(current.row, scan_ctx->start_key.row))
99  break;
100  m_deletes.insert(CellCache::CellMap::value_type(iter->first, iter->second));
101  }
102  }
103  }
104 
105  m_start_iter = m_cell_cache_ptr->m_cell_map.lower_bound(scan_ctx->start_serkey);
106  if (m_start_iter != m_cell_cache_ptr->m_cell_map.end())
107  m_end_iter = m_cell_cache_ptr->m_cell_map.lower_bound(scan_ctx->end_serkey);
108  else
109  m_end_iter = m_cell_cache_ptr->m_cell_map.end();
111 
112  if (!m_deletes.empty()) {
113  m_in_deletes = true;
114  m_delete_iter = m_deletes.begin();
115  }
116 
117  while (m_cur_iter != m_end_iter) {
118  m_cur_entry.key.load( (*m_cur_iter).first );
121  m_cur_entry.value.ptr = m_cur_entry.key.serial.ptr + (*m_cur_iter).second;
122  return;
123  }
124  ++m_cur_iter;
125  }
126  m_eos = true;
127  return;
128 }
129 
131 
132  try_again:
133 
134  if (m_entry_cache_next < m_entry_cache.size()) {
135  memcpy(&key, &m_entry_cache[m_entry_cache_next].key, sizeof(key));
136  memcpy(&value, &m_entry_cache[m_entry_cache_next].value, sizeof(value));
137  return true;
138  }
139 
140  if (m_eos)
141  return false;
142 
144  goto try_again;
145 
146 }
147 
150 }
151 
152 
154 
155  if (m_in_deletes) {
156  m_cur_entry.key.load( (*m_delete_iter).first );
157  m_cur_entry.value.ptr = m_cur_entry.key.serial.ptr + (*m_delete_iter).second;
158  return true;
159  }
160 
161  if (!m_eos) {
162  if (m_keys_only)
164  return true;
165  }
166 
167  return false;
168 }
169 
170 
171 
173 
174  if (m_in_deletes) {
175  ++m_delete_iter;
176  if (m_delete_iter == m_deletes.end()) {
177  m_in_deletes = false;
178  // reset current entry since its loaded with the last entry in m_deletes
179  m_cur_entry.key.load( (*m_cur_iter).first );
180  m_cur_entry.value.ptr = m_cur_entry.key.serial.ptr + (*m_cur_iter).second;
181  }
182  return;
183  }
184 
185  ++m_cur_iter;
186  while (m_cur_iter != m_end_iter) {
187 
188  m_cur_entry.key.load( (*m_cur_iter).first );
191  m_cur_entry.value.ptr = m_cur_entry.key.serial.ptr + (*m_cur_iter).second;
192  return;
193  }
194  ++m_cur_iter;
195  }
196  m_eos = true;
197 }
198 
199 
200 /*
201  * std::vector<CellCacheEntry> m_entry_cache;
202  * size_t m_entry_cache_next;
203  */
205  lock_guard<mutex> lock(m_cell_cache_mutex);
206 
207  m_entry_cache_next = 0;
208  m_entry_cache.clear();
209 
210  if (m_eos)
211  return;
212 
213  while (m_entry_cache.size() < (size_t)Global::cell_cache_scanner_cache_size) {
214 
215  if (!internal_get()) {
216  m_eos = true;
217  break;
218  }
219  m_entry_cache.push_back(m_cur_entry);
220 
222  }
223 
224 
225 }
SerializedKey start_serkey
Definition: ScanContext.h:60
const char * row
Definition: Key.h:129
static const uint32_t FLAG_DELETE_ROW
Definition: KeySpec.h:40
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
CellCache::CellMap::iterator m_cur_iter
const char * value_regexp
Definition: ScanSpec.h:280
STL namespace.
Scan context information.
Definition: ScanContext.h:52
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
Key key
static const uint32_t FLAG_DELETE_COLUMN_FAMILY
Definition: KeySpec.h:41
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
uint32_t row_len
Definition: Key.h:131
ByteString value
const ScanSpec * spec
Definition: ScanContext.h:55
void grow(size_t new_size, bool nocopy=false)
Grows the buffer and copies the data unless nocopy is true.
Logging routines and macros.
CellCacheScanner(CellCachePtr cellcache, ScanContext *scan_ctx)
std::vector< CellCacheEntry > m_entry_cache
Compatibility Macros for C/C++.
bool load(const SerializedKey &key)
Parses the opaque key and loads the components into the member variables.
Definition: Key.cc:158
CellCacheMap::iterator m_delete_iter
SerializedKey end_serkey
Definition: ScanContext.h:60
const uint8_t * ptr
The pointer to the serialized data.
Definition: ByteString.h:121
Hypertable definitions
SerializedKey serial
Definition: Key.h:123
void create_key_and_append(DynamicBuffer &dst_buf, const Key &key, bool time_order_asc)
Definition: Key.cc:105
static const int64_t TIMESTAMP_MAX
Definition: KeySpec.h:35
void clear()
Clears the buffer.
virtual bool get(Key &key, ByteString &value)
Provides access to internal components of opaque key.
Definition: Key.h:40
uint32_t column_qualifier_len
Definition: Key.h:132
uint8_t * base
Pointer to the allocated memory buffer.
static int32_t cell_cache_scanner_cache_size
Definition: Global.h:89
uint8_t column_family_code
Definition: Key.h:127
uint8_t flag
Definition: Key.h:125
CellCache::CellMap::iterator m_start_iter
std::shared_ptr< CellCache > CellCachePtr
Shared smart pointer to CellCache.
Definition: CellCache.h:163
CellCache::CellMap::iterator m_end_iter