0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SerializedCellsWriter.cc
Go to the documentation of this file.
1 
19 #include "Common/Compat.h"
20 #include "Common/Error.h"
21 #include "Common/Logger.h"
22 #include "Common/Serialization.h"
23 
24 #include "Hypertable/Lib/KeySpec.h"
25 
26 #include "SerializedCellsWriter.h"
27 #include "SerializedCellsFlag.h"
28 
29 using namespace Hypertable;
30 
31 
32 bool SerializedCellsWriter::add(const char *row, const char *column_family,
33  const char *column_qualifier, int64_t timestamp,
34  const void *value, int32_t value_length,
35  uint8_t cell_flag) {
36  int32_t row_length = strlen(row);
37  int32_t column_family_length = column_family ? strlen(column_family) : 0;
38  int32_t column_qualifier_length = column_qualifier ? strlen(column_qualifier) : 0;
39  uint8_t flag = 0;
40 
41  if (row_length == 0)
43  "Attempt to add empty row key to serialized cells buffer");
44 
45  bool need_row = false;
46  if (row_length != m_previous_row_length
47  || (m_previous_row_offset >= 0 && memcmp(row, m_buf.base + m_previous_row_offset, row_length)))
48  need_row = true;
49 
50  if (!value && value_length)
51  value_length = 0;
52 
53  int32_t length = 9 + column_family_length + 3
54  + column_qualifier_length + value_length + 1;
55  if (m_buf.empty()) // Add version to beginning of buffer
56  length += 4;
57  if (need_row)
58  length += row_length;
59 
60  if (timestamp == AUTO_ASSIGN)
62  else if (timestamp != TIMESTAMP_NULL) {
64  length += 8;
65  }
66 
67  // need to leave room for the termination byte
68  if (length > (int32_t)m_buf.remaining()) {
69  if (m_grow)
70  m_buf.ensure(length);
71  else {
72  if (!m_buf.empty())
73  return false;
74  m_buf.grow(length);
75  }
76  }
77 
78  // version
79  if (m_buf.empty())
81 
82  // flag byte
83  *m_buf.ptr++ = flag;
84 
85  // timestamp
86  if ((flag & SerializedCellsFlag::HAVE_TIMESTAMP) != 0)
87  Serialization::encode_i64(&m_buf.ptr, timestamp);
88 
89  // revision
91  (flag & SerializedCellsFlag::REV_IS_TS) == 0)
93 
94  // row; only write it if it's not identical to the previous row
95  if (need_row) {
96  memcpy(m_buf.ptr, row, row_length);
98  m_buf.ptr += row_length;
99  m_previous_row_length = row_length;
100  }
101  *m_buf.ptr++ = 0;
102 
103  // column_family
104  if (column_family)
105  memcpy(m_buf.ptr, column_family, column_family_length);
106  m_buf.ptr += column_family_length;
107  *m_buf.ptr++ = 0;
108 
109  // column_qualifier
110  if (column_qualifier)
111  memcpy(m_buf.ptr, column_qualifier, column_qualifier_length);
112  m_buf.ptr += column_qualifier_length;
113  *m_buf.ptr++ = 0;
114 
115  Serialization::encode_i32(&m_buf.ptr, value_length);
116  if (value)
117  memcpy(m_buf.ptr, value, value_length);
118  m_buf.ptr += value_length;
119  Serialization::encode_i8(&m_buf.ptr, cell_flag);
120 
121  return true;
122 }
123 
124 
126  m_buf.clear();
129  m_finalized = false;
130 }
bool empty() const
Returns true if the buffer is empty.
Definition: DynamicBuffer.h:73
uint8_t * ptr
Pointer to the end of the used part of the buffer.
void grow(size_t new_size, bool nocopy=false)
Grows the buffer and copies the data unless nocopy is true.
Logging routines and macros.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
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.
static const int64_t TIMESTAMP_NULL
Definition: KeySpec.h:36
Hypertable definitions
void clear()
Clears the buffer.
uint8_t * base
Pointer to the allocated memory buffer.
static const int64_t AUTO_ASSIGN
Definition: KeySpec.h:38
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
void ensure(size_t len)
Ensure space for additional data Will grow the space to 1.5 of the needed space with existing data un...
Definition: DynamicBuffer.h:82
size_t remaining() const
Returns the size of the unused portion.
Definition: DynamicBuffer.h:67
void encode_i8(uint8_t **bufp, uint8_t val)
Encodes a byte into the given buffer.
Definition: Serialization.h:49