0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
KeyCompressorPrefix.cc
Go to the documentation of this file.
1 
22 #include "Common/Compat.h"
23 #include "Common/Serialization.h"
24 
25 #include "KeyCompressorPrefix.h"
26 
27 using namespace Hypertable;
28 
29 
31  m_buffer.clear();
34 }
35 
36 void KeyCompressorPrefix::add(const Key &key) {
37  uint32_t payload_length = key.length - (((const uint8_t *)key.row)-key.serial.ptr) + 1;
38  HT_ASSERT(key.serial.ptr);
39  m_last_control = key.control;
40  if (m_buffer.fill() == 0) {
42  m_compressed_key.ensure(payload_length+8);
44  *(m_compressed_key.ptr)++ = key.control;
45  *(m_compressed_key.ptr)++ = 0;
47  m_suffix_length = payload_length-1;
49  }
50  else {
51  size_t n = std::min((size_t)(payload_length-1), m_buffer.fill());
52 
53  uint8_t *ptr = m_buffer.base;
54  const uint8_t *end = m_buffer.base + n;
55  const uint8_t *incoming = (const uint8_t *)key.row;
56  const uint8_t *incoming_end = incoming + (payload_length-1);
57 
58  // match what's in the buffer
59  while (ptr < end) {
60  if (*ptr != *incoming)
61  break;
62  ptr++;
63  incoming++;
64  }
65 
66  // If the entire buffer matches,
67  // match the suffix in the compressed key buffer
68  if (ptr == m_buffer.ptr) {
69  n = std::min(m_suffix_length, (size_t)(incoming_end-incoming));
70  const uint8_t *base = m_suffix;
71  end = base + n;
72  while (m_suffix < end) {
73  if (*m_suffix != *incoming)
74  break;
75  m_suffix++;
76  incoming++;
77  }
78  if (m_suffix > base) {
80  m_buffer.add(base, m_suffix-base);
81  }
82  }
83  else
84  m_buffer.ptr = ptr;
85 
86  m_suffix_length = incoming_end - incoming;
87 
88  uint32_t matching = m_buffer.fill();
89  size_t encoding_bytes = Serialization::encoded_length_vi32(matching);
90  uint32_t total_bytes = 1 + encoding_bytes + m_suffix_length;
91 
94  *m_compressed_key.ptr++ = key.control;
96  memcpy(m_compressed_key.ptr, m_suffix, m_suffix_length);
99  }
100 
101 }
102 
104  return m_compressed_key.fill();
105 }
106 
110  return m_uncompressed_key.fill();
111 }
112 
113 void KeyCompressorPrefix::write(uint8_t *buf) {
114  memcpy(buf, m_compressed_key.base, m_compressed_key.fill());
115 }
116 
121 }
122 
124  uint32_t length = 1 + m_buffer.fill() + m_suffix_length;
125  size_t encoding_bytes = Serialization::encoded_length_vi32(length);
126 
128  m_uncompressed_key.ensure(encoding_bytes + length);
133 }
bool empty() const
Returns true if the buffer is empty.
Definition: DynamicBuffer.h:73
const char * row
Definition: Key.h:129
uint8_t control
Definition: Key.h:126
uint32_t length
Definition: Key.h:124
uint8_t * ptr
Pointer to the end of the used part of the buffer.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
int encoded_length_vi32(uint32_t val)
Length of a variable length encoded 32-bit integer (up to 5 bytes)
uint8_t * add(const void *data, size_t len)
Adds more data WITH boundary checks; if required the buffer is resized and existing data is preserved...
Compatibility Macros for C/C++.
Functions to serialize/deserialize primitives to/from a memory buffer.
const uint8_t * ptr
The pointer to the serialized data.
Definition: ByteString.h:121
Hypertable definitions
SerializedKey serial
Definition: Key.h:123
void encode_vi32(uint8_t **bufp, uint32_t val)
Encode a integer (up to 32-bit) in variable length encoding.
void clear()
Clears the buffer.
virtual void write(uint8_t *buf)
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
virtual void write_uncompressed(uint8_t *buf)
virtual void add(const Key &key)
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
uint8_t * add_unchecked(const void *data, size_t len)
Adds additional data without boundary checks.
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