0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BlockCompressionCodecQuicklz.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 
26 
27 #include <Common/Compat.h>
28 
30 
31 #include <Common/Checksum.h>
32 #include <Common/DynamicBuffer.h>
33 #include <Common/Error.h>
34 #include <Common/Logger.h>
35 #include <Common/String.h>
36 #include <Common/Thread.h>
37 
38 using namespace Hypertable;
39 
40 
41 void
43  DynamicBuffer &output, BlockHeader &header, size_t reserve) {
44  uint32_t avail_out = input.fill() + 400;
45  size_t len;
46 
47  output.clear();
48  output.reserve(header.encoded_length() + avail_out + reserve);
49 
50  // compress
51  len = qlz_compress((char *)input.base, (char *)output.base+header.encoded_length(),
52  input.fill(), &m_compress);
53 
54  if (len == 0)
55  HT_FATALF("Problem deflating block of size %lld", (Lld)input.fill());
56 
57  /* check for an incompressible block */
58  if (len >= input.fill()) {
59  header.set_compression_type(NONE);
60  memcpy(output.base+header.encoded_length(), input.base, input.fill());
61  header.set_data_length(input.fill());
62  header.set_data_zlength(input.fill());
63  }
64  else {
66  header.set_data_length(input.fill());
67  header.set_data_zlength(len);
68  }
69  header.set_data_checksum(fletcher32(output.base + header.encoded_length(),
70  header.get_data_zlength()));
71 
72  output.ptr = output.base;
73  header.encode(&output.ptr);
74  output.ptr += header.get_data_zlength();
75 }
76 
77 
79  DynamicBuffer &output, BlockHeader &header) {
80  const uint8_t *msg_ptr = input.base;
81  size_t remaining = input.fill();
82 
83  header.decode(&msg_ptr, &remaining);
84 
85  if (header.get_data_zlength() > remaining)
86  HT_THROWF(Error::BLOCK_COMPRESSOR_BAD_HEADER, "Block decompression error, "
87  "header zlength = %lu, actual = %lu",
88  (Lu)header.get_data_zlength(), (Lu)remaining);
89 
90  uint32_t checksum = fletcher32(msg_ptr, header.get_data_zlength());
91 
92  if (checksum != header.get_data_checksum())
94  "checksum mismatch header=%lx, computed=%lx",
95  (Lu)header.get_data_checksum(), (Lu)checksum);
96 
97  output.reserve(header.get_data_length());
98 
99  // check compress type
100  if (header.get_compression_type() == NONE)
101  memcpy(output.base, msg_ptr, header.get_data_length());
102  else {
103  size_t len;
104  // decompress
105  len = qlz_decompress((char *)msg_ptr, (char *)output.base, &m_decompress);
106 
107  HT_ASSERT(len == header.get_data_length());
108  }
109  output.ptr = output.base + header.get_data_length();
110 }
void set_data_length(uint32_t length)
Sets the uncompressed data length field.
Definition: BlockHeader.h:91
void set_data_checksum(uint32_t checksum)
Sets the checksum field.
Definition: BlockHeader.h:113
qlz_state_compress m_compress
Compression state.
uint16_t get_compression_type()
Gets the compression type field.
Definition: BlockHeader.h:128
virtual void deflate(const DynamicBuffer &input, DynamicBuffer &output, BlockHeader &header, size_t reserve=0)
Compresses a buffer using the QUICKLZ algorithm.
void set_data_zlength(uint32_t zlength)
Sets the compressed data length field.
Definition: BlockHeader.h:101
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
uint32_t get_data_checksum()
Gets the checksum field.
Definition: BlockHeader.h:118
#define HT_ASSERT(_e_)
Definition: Logger.h:396
uint32_t fletcher32(const void *data8, size_t len8)
Compute fletcher32 checksum for arbitary data.
Definition: Checksum.cc:42
uint32_t get_data_length()
Gets the uncompressed data length field.
Definition: BlockHeader.h:96
qlz_state_decompress m_decompress
Decompression state.
A dynamic, resizable memory buffer.
Logging routines and macros.
Compatibility Macros for C/C++.
Declarations for BlockCompressionCodecQuicklz.
Importing boost::thread and boost::thread_group into the Hypertable namespace.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Decodes serialized block header.
Definition: BlockHeader.cc:104
Hypertable definitions
virtual void inflate(const DynamicBuffer &input, DynamicBuffer &output, BlockHeader &header)
Decompresses a buffer compressed with the QUICKLZ algorithm.
#define HT_FATALF(msg,...)
Definition: Logger.h:343
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
Implementation of checksum routines.
void clear()
Clears the buffer.
void set_compression_type(uint16_t type)
Sets the compression type field.
Definition: BlockHeader.h:123
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
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 size_t encoded_length()
Returns length of serizlized block header.
Definition: BlockHeader.cc:76
A String class based on std::string.
long unsigned int Lu
Shortcut for printf formats.
Definition: String.h:47
uint32_t get_data_zlength()
Gets the compressed data length field.
Definition: BlockHeader.h:106
Base class for block headers.
Definition: BlockHeader.h:48
Error codes, Exception handling, error logging.
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
virtual void encode(uint8_t **bufp)
Encodes serialized representation of block header.
Definition: BlockHeader.cc:82