0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CellCache.cc
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 #include <Common/Compat.h>
23 
24 #include "Config.h"
25 #include "CellCache.h"
26 #include "CellCacheScanner.h"
27 #include "Global.h"
28 
29 #include <Hypertable/Lib/Key.h>
30 
31 #include <Common/Logger.h>
32 #include <Common/Serialization.h>
33 
34 #include <algorithm>
35 #include <cassert>
36 #include <iostream>
37 
38 using namespace Hypertable;
39 using namespace std;
40 
42  : m_cell_map(std::less<const SerializedKey>(), Alloc(m_arena)) {
43  assert(Config::properties); // requires Config::init* first
44  m_arena.set_page_size((size_t)
45  Config::get_i32("Hypertable.RangeServer.AccessGroup.CellCache.PageSize"));
46 }
47 
48 
51 void CellCache::add(const Key &key, const ByteString value) {
52  SerializedKey new_key;
53  uint8_t *ptr;
54  size_t total_len = key.length + value.length();
55 
56  m_key_bytes += key.length;
57  m_value_bytes += value.length();
58 
59  new_key.ptr = ptr = m_arena.alloc(total_len);
60 
61  memcpy(ptr, key.serial.ptr, key.length);
62  ptr += key.length;
63 
64  value.write(ptr);
65 
66  CellMap::value_type v(new_key, key.length);
67  std::pair<CellMap::iterator, bool> r = m_cell_map.insert(v);
68  if (!r.second) {
69  m_cell_map.erase(r.first);
70  m_cell_map.insert(v);
71  m_collisions++;
72  HT_WARNF("Collision detected key insert (row = %s)", new_key.row());
73  }
74  else {
75  if (key.flag <= FLAG_DELETE_CELL_VERSION)
76  m_deletes++;
77  }
78 }
79 
80 
83 void CellCache::add_counter(const Key &key, const ByteString value) {
84 
85  // Check for counter reset
86  if (*value.ptr == 9) {
87  HT_ASSERT(value.ptr[9] == '=');
88  add(key, value);
89  return;
90  }
91  else if (m_have_counter_deletes || key.flag != FLAG_INSERT) {
92  add(key, value);
94  return;
95  }
96 
97  HT_ASSERT(*value.ptr == 8);
98 
99  auto iter = m_cell_map.lower_bound(key.serial);
100 
101  if (iter == m_cell_map.end()) {
102  add(key, value);
103  return;
104  }
105 
106  const uint8_t *ptr;
107 
108  size_t len = (*iter).first.decode_length(&ptr);
109 
110  // If the lengths differ, assume they're different keys and do a normal add
111  if (len + (ptr-(*iter).first.ptr) != key.length) {
112  add(key, value);
113  return;
114  }
115 
116  if (memcmp(ptr+1, key.row, (key.flag_ptr+1)-(const uint8_t *)key.row)) {
117  add(key, value);
118  return;
119  }
120 
121  ByteString old_value;
122  old_value.ptr = (*iter).first.ptr + (*iter).second;
123 
124  HT_ASSERT(*old_value.ptr == 8 || *old_value.ptr == 9);
125 
126  /*
127  * If old value was a reset, just insert the new value
128  */
129  if (*old_value.ptr == 9) {
130  add(key, value);
131  return;
132  }
133 
134  /*
135  * copy timestamp/revision info from insert key to the one in the map
136  */
137  size_t offset = (key.flag_ptr-((const uint8_t *)key.serial.ptr)) + 1;
138  len = (*iter).second - offset;
139 
140 #if 0
141  // If key timestamp is not auto-assigned, assume that the timestamp uniquely
142  // identifies increments and that they come in timestamp order, so skip the
143  // increment if it's timestamp is <= the timestamp of the accumulated counter
144  if ((key.control & Key::REV_IS_TS) == 0) {
145  int64_t existing_ts = Key::decode_ts64(&ptr);
146  if (key.timestamp <= existing_ts)
147  return;
148  }
149 #endif
150 
151  // Copy timestamp/revision info from insert key to the one in the map
152  memcpy(((uint8_t *)(*iter).first.ptr) + offset, key.flag_ptr+1, len);
153 
154  // read old value
155  ptr = old_value.ptr+1;
156  size_t remaining = 8;
157  int64_t old_count = (int64_t)Serialization::decode_i64(&ptr, &remaining);
158 
159  // read new value
160  ptr = value.ptr+1;
161  remaining = 8;
162  int64_t new_count = (int64_t)Serialization::decode_i64(&ptr, &remaining);
163 
164  uint8_t *write_ptr = (uint8_t *)old_value.ptr+1;
165 
166  Serialization::encode_i64(&write_ptr, old_count+new_count);
167 }
168 
169 
171  lock_guard<mutex> lock(m_mutex);
172  const char *row, *last_row = 0;
173  int64_t last_count = 0;
174  for (CellMap::iterator iter = m_cell_map.begin();
175  iter != m_cell_map.end(); ++iter) {
176  row = iter->first.row();
177  if (last_row == 0)
178  last_row = row;
179  if (strcmp(row, last_row) != 0) {
180  CstrToInt64MapT::iterator iter = split_row_data.find(last_row);
181  if (iter == split_row_data.end())
182  split_row_data[last_row] = last_count;
183  else
184  iter->second += last_count;
185  last_row = row;
186  last_count = 0;
187  }
188  last_count++;
189  }
190  if (last_count > 0) {
191  CstrToInt64MapT::iterator iter = split_row_data.find(last_row);
192  if (iter == split_row_data.end())
193  split_row_data[last_row] = last_count;
194  else
195  iter->second += last_count;
196  }
197 }
198 
199 
200 
202  return make_shared<CellCacheScanner>(shared_from_this(), scan_ctx);
203 }
CellCacheArena m_arena
Definition: CellCache.h:152
static int64_t decode_ts64(const uint8_t **bufp, bool ascending=true)
Definition: Key.h:71
int64_t timestamp
Definition: Key.h:134
void set_page_size(size_t sz)
Sets the page size.
Definition: PageArena.h:210
const char * row
Definition: Key.h:129
#define HT_WARNF(msg,...)
Definition: Logger.h:290
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
uint8_t control
Definition: Key.h:126
uint32_t length
Definition: Key.h:124
const char * row() const
Definition: SerializedKey.h:53
static const uint32_t FLAG_INSERT
Definition: KeySpec.h:47
STL namespace.
Scan context information.
Definition: ScanContext.h:52
size_t write(uint8_t *dst) const
Writes the data of this ByteString into a pointer.
Definition: ByteString.h:93
CellListScannerPtr create_scanner(ScanContext *scan_ctx) override
Creates a CellCacheScanner object that contains an shared pointer to this CellCache.
Definition: CellCache.cc:201
CharT * alloc(size_t sz)
Allocate sz bytes.
Definition: PageArena.h:216
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
#define HT_ASSERT(_e_)
Definition: Logger.h:396
std::mutex m_mutex
Definition: CellCache.h:151
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
const uint8_t * flag_ptr
Definition: Key.h:133
std::map< const char *, int64_t, LtCstr, SplitRowDataAlloc > SplitRowDataMapT
Definition: CellList.h:66
Logging routines and macros.
virtual void add_counter(const Key &key, const ByteString value)
Definition: CellCache.cc:83
Compatibility Macros for C/C++.
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
size_t length() const
Retrieves the length of the serialized string.
Definition: ByteString.h:62
const uint8_t * ptr
The pointer to the serialized data.
Definition: ByteString.h:121
Hypertable definitions
SerializedKey serial
Definition: Key.h:123
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
std::shared_ptr< CellListScanner > CellListScannerPtr
Definition: CellList.h:35
uint8_t flag
Definition: Key.h:125
static const uint32_t FLAG_DELETE_CELL_VERSION
Definition: KeySpec.h:43
static const uint8_t REV_IS_TS
Definition: Key.h:46