0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StringCompressorPrefix.h
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 
27 #ifndef Common_StringCompressorPrefix_h
28 #define Common_StringCompressorPrefix_h
29 
30 #include "DynamicBuffer.h"
31 #include "String.h"
32 #include "Serialization.h"
33 
34 namespace Hypertable {
35 
44  public:
46  virtual void reset() {
47  m_last_string.clear();
49  }
50 
57  virtual void add(const char *str) {
58  m_uncompressed_len = strlen(str);
59  size_t prefix_len = get_prefix_length(str, m_uncompressed_len);
60  size_t suffix_len;
61  // null terminate suffix
62  if (prefix_len > m_uncompressed_len)
63  suffix_len = 1;
64  else
65  suffix_len = m_uncompressed_len - prefix_len + 1;
66 
68  + suffix_len;
69 
73  memcpy(m_compressed_string.ptr, (const void*) (str + prefix_len),
74  suffix_len);
76  }
77 
79  virtual size_t length() const {
80  return m_compressed_len;
81  }
82 
84  virtual size_t length_uncompressed() const {
85  return m_uncompressed_len;
86  }
87 
89  virtual void write(uint8_t *buf) const {
90  memcpy(buf, (const void *)m_compressed_string.base, m_compressed_len);
91  }
92 
94  virtual void write_uncompressed(uint8_t *buf) const {
95  // null terminated string
96  memcpy(buf, (const void *)m_last_string.c_str(),
97  m_last_string.length() + 1);
98  }
99 
100  private:
102  size_t get_prefix_length(const char *str, size_t len) const {
103  len = len > m_last_string.length() ? m_last_string.length() : len;
104  size_t ii;
105 
106  for (ii = 0; ii < len; ++ii)
107  if (m_last_string[ii] != str[ii])
108  break;
109  return ii;
110  }
111 
114 
117 
120 
123  };
124 
126 }
127 
128 #endif // Common_StringCompressorPrefix_h
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
size_t m_compressed_len
Length of the compressed string.
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
virtual size_t length_uncompressed() const
Retrieves the length of the uncompressed string.
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
virtual void reset()
Clears the internal state.
int encoded_length_vi32(uint32_t val)
Length of a variable length encoded 32-bit integer (up to 5 bytes)
A dynamic, resizable memory buffer.
Functions to serialize/deserialize primitives to/from a memory buffer.
size_t m_uncompressed_len
Length of the uncompressed string.
Hypertable definitions
void encode_vi32(uint8_t **bufp, uint32_t val)
Encode a integer (up to 32-bit) in variable length encoding.
String m_last_string
The previously added (uncompressed) string.
void clear()
Clears the buffer.
virtual void write(uint8_t *buf) const
Writes the compressed string to a buffer.
virtual size_t length() const
Retrieves the length of the compressed string.
uint8_t * base
Pointer to the allocated memory buffer.
virtual void add(const char *str)
Adds (and compresses) a string.
virtual void write_uncompressed(uint8_t *buf) const
Writes the uncompressed string to a buffer.
A String class based on std::string.
DynamicBuffer m_compressed_string
Dynamic array holding the compressed string.
size_t get_prefix_length(const char *str, size_t len) const
Helper function returning the prefix length of a new string.
A class to prefix-compress strings.
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