0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BlockCompressionCodecSnappy.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/DynamicBuffer.h>
32 #include <Common/Logger.h>
33 #include <Common/Checksum.h>
34 
35 using namespace Hypertable;
36 
37 void
39  DynamicBuffer &output, BlockHeader &header, size_t reserve) {
40  output.reserve(header.encoded_length()+snappy::MaxCompressedLength(input.fill())+reserve);
41  size_t outlen;
42  snappy::RawCompress((const char *)input.base, input.fill(),
43  (char *)output.base+header.encoded_length(), &outlen);
44 
45  HT_ASSERT(outlen+reserve <= output.size);
46 
47  /* check for an incompressible block */
48  if (outlen >= input.fill()) {
49  header.set_compression_type(NONE);
50  memcpy(output.base+header.encoded_length(), input.base, input.fill());
51  header.set_data_length(input.fill());
52  header.set_data_zlength(input.fill());
53  }
54  else {
56  header.set_data_length(input.fill());
57  header.set_data_zlength(outlen);
58  }
59 
60  header.set_data_checksum(fletcher32(output.base + header.encoded_length(),
61  header.get_data_zlength()));
62 
63  output.ptr = output.base;
64  header.encode(&output.ptr);
65  output.ptr += header.get_data_zlength();
66 }
67 
68 
69 void
71  DynamicBuffer &output, BlockHeader &header) {
72  const uint8_t *msg_ptr = input.base;
73  size_t remaining = input.fill();
74 
75  header.decode(&msg_ptr, &remaining);
76 
77  if (header.get_data_zlength() > remaining)
78  HT_THROWF(Error::BLOCK_COMPRESSOR_BAD_HEADER, "Block decompression error, "
79  "header zlength = %lu, actual = %lu",
80  (Lu)header.get_data_zlength(), (Lu)remaining);
81 
82  uint32_t checksum = fletcher32(msg_ptr, header.get_data_zlength());
83 
84  if (checksum != header.get_data_checksum())
86  "checksum mismatch header=%lx, computed=%lx",
87  (Lu)header.get_data_checksum(), (Lu)checksum);
88 
89  try {
90  output.reserve(header.get_data_length());
91 
92  // check compress bit
93  if (header.get_compression_type() == NONE)
94  memcpy(output.base, msg_ptr, header.get_data_length());
95  else {
96  if (!snappy::RawUncompress((const char *)msg_ptr,
97  header.get_data_zlength(), (char *)output.base))
99  "Compressed block inflate error");
100  }
101 
102  output.ptr = output.base + header.get_data_length();
103  }
104  catch (Exception &e) {
105  output.free();
106  throw;
107  }
108 }
void free()
Frees resources.
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
uint16_t get_compression_type()
Gets the compression type field.
Definition: BlockHeader.h:128
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.
Declarations for BlockCompressionCodecSnappy.
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
A dynamic, resizable memory buffer.
uint32_t size
The size of the allocated memory buffer (base)
Logging routines and macros.
Compatibility Macros for C/C++.
virtual void inflate(const DynamicBuffer &input, DynamicBuffer &output, BlockHeader &header)
Decompresses a buffer compressed with the SNAPPY algorithm.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Decodes serialized block header.
Definition: BlockHeader.cc:104
Hypertable definitions
Implementation of checksum routines.
virtual void deflate(const DynamicBuffer &input, DynamicBuffer &output, BlockHeader &header, size_t reserve=0)
Compresses a buffer using the SNAPPY algorithm.
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
This is a generic exception class for Hypertable.
Definition: Error.h:314
virtual size_t encoded_length()
Returns length of serizlized block header.
Definition: BlockHeader.cc:76
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
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