0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FillScanBlock.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 #include "FillScanBlock.h"
24 
25 namespace Hypertable {
26 
27  bool
29  uint32_t *cell_count, int64_t buffer_size) {
30  Key key;
31  ByteString value;
32  size_t value_len;
33  bool more = true;
34  size_t limit = buffer_size;
35  size_t remaining = buffer_size;
36  uint8_t *ptr;
37  ScanContext *scan_context = scanner->scan_context();
38  bool keys_only = scan_context->spec->keys_only;
39  char numbuf[24];
40  DynamicBuffer counter_value;
41  bool counter;
42  String empty_value("");
43 
44  assert(dbuf.base == 0);
45 
46  while ((more = scanner->get(key, value))) {
47  counter = false;
48 
49  if (cell_count)
50  (*cell_count)++;
51 
52  if (keys_only) {
53  value.ptr = 0;
54  counter_value.clear();
55  value_len = 0;
56  }
57  else {
58  counter = scan_context->cell_predicates[key.column_family_code].counter &&
59  (key.flag == FLAG_INSERT);
60 
61  if (counter) {
62  const uint8_t *decode;
63  int64_t count;
64  size_t remain = value.decode_length(&decode);
65  // value must be encoded 64 bit int followed by '=' character
66  if (remain != 9)
67  HT_FATAL_OUT << "Expected counter to be encoded 64 bit int but remain=" << remain
68  << " ,key=" << key << " ,value="<< value.str() << HT_END;
69 
70  count = Serialization::decode_i64(&decode, &remain);
71  HT_ASSERT(*decode == '=');
72  //convert counter to ascii
73  sprintf(numbuf, "%lld", (Lld) count);
74  value_len = strlen(numbuf);
75  counter_value.clear();
76  append_as_byte_string(counter_value, numbuf, value_len);
77  value_len = counter_value.fill();
78  }
79  else
80  value_len = value.length();
81  }
82 
83  if (value.ptr == 0) {
84  value.ptr = (const uint8_t *)empty_value.c_str();
85  value_len = 1;
86  }
87 
88  if (dbuf.base == 0) {
89  if (key.length + value_len > limit) {
90  limit = key.length + value_len;
91  remaining = limit;
92  }
93  dbuf.reserve(4 + limit);
94  // skip encoded length
95  dbuf.ptr = dbuf.base + 4;
96  }
97  if (key.length + value_len <= remaining) {
98 
99  dbuf.add_unchecked(key.serial.ptr, key.length);
100 
101  if (counter)
102  dbuf.add_unchecked(counter_value.base, value_len);
103  else
104  dbuf.add_unchecked(value.ptr, value_len);
105 
106  remaining -= (key.length + value_len);
107  scanner->forward();
108  }
109  else
110  break;
111  }
112 
113  if (dbuf.base == 0) {
114  dbuf.reserve(4);
115  dbuf.ptr = dbuf.base + 4;
116  }
117 
118  ptr = dbuf.base;
119  Serialization::encode_i32(&ptr, dbuf.fill() - 4);
120 
121  return more;
122  }
123 
124 }
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
uint32_t length
Definition: Key.h:124
static const uint32_t FLAG_INSERT
Definition: KeySpec.h:47
Scan context information.
Definition: ScanContext.h:52
uint8_t * ptr
Pointer to the end of the used part of the buffer.
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
void append_as_byte_string(DynamicBuffer &dst_buf, const void *value, uint32_t value_len)
Serializes and appends a byte array to a DynamicBuffer object.
Definition: ByteString.h:130
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
#define HT_ASSERT(_e_)
Definition: Logger.h:396
const char * str() const
Returns a pointer to the String's deserialized data.
Definition: ByteString.h:106
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
const ScanSpec * spec
Definition: ScanContext.h:55
std::shared_ptr< MergeScannerRange > MergeScannerRangePtr
Smart pointer to MergeScannerRange.
vector< CellPredicate > cell_predicates
Definition: ScanContext.h:72
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
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
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
void clear()
Clears the buffer.
size_t decode_length(const uint8_t **dptr) const
Retrieves the decoded length and returns a pointer to the string.
Definition: ByteString.h:83
Provides access to internal components of opaque key.
Definition: Key.h:40
uint8_t * base
Pointer to the allocated memory buffer.
size_t fill() const
Returns the size of the used portion.
Definition: DynamicBuffer.h:70
bool FillScanBlock(MergeScannerRangePtr &scanner, DynamicBuffer &dbuf, uint32_t *cell_count, int64_t buffer_size)
Fills a block of scan results to be sent back to client.
uint8_t column_family_code
Definition: Key.h:127
uint8_t flag
Definition: Key.h:125
#define HT_FATAL_OUT
Definition: Logger.h:347
uint8_t * add_unchecked(const void *data, size_t len)
Adds additional data without boundary checks.
Declaration for FillScanBlock.
void reserve(size_t len, bool nocopy=false)
Reserve space for additional data Will grow the space to exactly what's needed.
Definition: DynamicBuffer.h:95