proxygen
folly::io::Codec Class Referenceabstract

#include <Compression.h>

Inheritance diagram for folly::io::Codec:
folly::io::StreamCodec

Public Member Functions

virtual ~Codec ()
 
uint64_t maxUncompressedLength () const
 
CodecType type () const
 
bool needsUncompressedLength () const
 
std::unique_ptr< IOBufcompress (const folly::IOBuf *data)
 
std::string compress (StringPiece data)
 
std::unique_ptr< IOBufuncompress (const IOBuf *data, folly::Optional< uint64_t > uncompressedLength=folly::none)
 
std::string uncompress (StringPiece data, folly::Optional< uint64_t > uncompressedLength=folly::none)
 
uint64_t maxCompressedLength (uint64_t uncompressedLength) const
 
folly::Optional< uint64_tgetUncompressedLength (const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength=folly::none) const
 
virtual std::vector< std::stringvalidPrefixes () const
 
virtual bool canUncompress (const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength=folly::none) const
 

Static Public Attributes

static constexpr uint64_t UNLIMITED_UNCOMPRESSED_LENGTH = uint64_t(-1)
 

Protected Member Functions

 Codec (CodecType type, folly::Optional< int > level=folly::none, folly::StringPiece name={}, bool counters=true)
 

Private Member Functions

virtual uint64_t doMaxUncompressedLength () const
 
virtual bool doNeedsUncompressedLength () const
 
virtual std::unique_ptr< IOBufdoCompress (const folly::IOBuf *data)=0
 
virtual std::unique_ptr< IOBufdoUncompress (const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength)=0
 
virtual std::string doCompressString (StringPiece data)
 
virtual std::string doUncompressString (StringPiece data, folly::Optional< uint64_t > uncompressedLength)
 
virtual uint64_t doMaxCompressedLength (uint64_t uncompressedLength) const =0
 
virtual folly::Optional< uint64_tdoGetUncompressedLength (const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength) const
 

Private Attributes

CodecType type_
 
folly::detail::CompressionCounter bytesBeforeCompression_
 
folly::detail::CompressionCounter bytesAfterCompression_
 
folly::detail::CompressionCounter bytesBeforeDecompression_
 
folly::detail::CompressionCounter bytesAfterDecompression_
 
folly::detail::CompressionCounter compressions_
 
folly::detail::CompressionCounter decompressions_
 
folly::detail::CompressionCounter compressionMilliseconds_
 
folly::detail::CompressionCounter decompressionMilliseconds_
 

Detailed Description

Definition at line 131 of file Compression.h.

Constructor & Destructor Documentation

virtual folly::io::Codec::~Codec ( )
inlinevirtual

Definition at line 133 of file Compression.h.

133 {}
folly::io::Codec::Codec ( CodecType  type,
folly::Optional< int >  level = folly::none,
folly::StringPiece  name = {},
bool  counters = true 
)
protected

Definition at line 71 of file Compression.cpp.

References folly::BYTES_AFTER_COMPRESSION, folly::BYTES_AFTER_DECOMPRESSION, folly::BYTES_BEFORE_COMPRESSION, folly::BYTES_BEFORE_DECOMPRESSION, bytesAfterCompression_, bytesAfterDecompression_, bytesBeforeCompression_, bytesBeforeDecompression_, folly::COMPRESSION_MILLISECONDS, compressionMilliseconds_, folly::COMPRESSIONS, compressions_, counter, counter_, folly::DECOMPRESSION_MILLISECONDS, decompressionMilliseconds_, folly::DECOMPRESSIONS, decompressions_, name, folly::SUM, timer_, type(), and uint32_t.

Referenced by folly::io::StreamCodec::doUncompress().

76  : type_(type) {
77  if (counters) {
79  name,
80  level,
84  name,
85  level,
87  CompressionCounterType::SUM};
89  type,
90  name,
91  level,
93  CompressionCounterType::SUM};
95  type,
96  name,
97  level,
99  CompressionCounterType::SUM};
100  compressions_ = {type,
101  name,
102  level,
104  CompressionCounterType::SUM};
106  name,
107  level,
109  CompressionCounterType::SUM};
111  name,
112  level,
114  CompressionCounterType::SUM};
116  type,
117  name,
118  level,
120  CompressionCounterType::SUM};
121  }
122 }
folly::detail::CompressionCounter compressionMilliseconds_
Definition: Compression.h:270
folly::detail::CompressionCounter bytesAfterCompression_
Definition: Compression.h:265
folly::detail::CompressionCounter bytesBeforeDecompression_
Definition: Compression.h:266
const char * name
Definition: http_parser.c:437
folly::detail::CompressionCounter decompressions_
Definition: Compression.h:269
folly::detail::CompressionCounter bytesBeforeCompression_
Definition: Compression.h:264
folly::detail::CompressionCounter bytesAfterDecompression_
Definition: Compression.h:267
folly::detail::CompressionCounter decompressionMilliseconds_
Definition: Compression.h:271
CodecType type() const
Definition: Compression.h:147
folly::detail::CompressionCounter compressions_
Definition: Compression.h:268
CodecType type_
Definition: Compression.h:263

Member Function Documentation

bool folly::io::Codec::canUncompress ( const folly::IOBuf data,
folly::Optional< uint64_t uncompressedLength = folly::none 
) const
virtual

Returns true if the codec thinks it can uncompress the data. If a codec doesn't have magic bytes at the beginning, like LZ4 and Snappy, it can always return false. default: Returns false.

Definition at line 263 of file Compression.cpp.

Referenced by folly::io::StreamCodec::doUncompress().

263  {
264  return false;
265 }
std::unique_ptr< IOBuf > folly::io::Codec::compress ( const folly::IOBuf data)

Compress data, returning an IOBuf (which may share storage with data). Throws std::invalid_argument if data is larger than maxUncompressedLength().

Definition at line 143 of file Compression.cpp.

References bytesAfterCompression_, bytesBeforeCompression_, compressionMilliseconds_, compressions_, folly::IOBuf::computeChainDataLength(), doCompress(), maxUncompressedLength(), folly::Random::oneIn(), and uint64_t.

143  {
144  if (data == nullptr) {
145  throw std::invalid_argument("Codec: data must not be nullptr");
146  }
147  const uint64_t len = data->computeChainDataLength();
148  if (len > maxUncompressedLength()) {
149  throw std::runtime_error("Codec: uncompressed length too large");
150  }
151  bool const logging = folly::Random::oneIn(kLoggingRate);
152  folly::Optional<Timer> const timer =
153  logging ? Timer(compressionMilliseconds_) : folly::Optional<Timer>();
154  auto result = doCompress(data);
155  if (logging) {
156  compressions_++;
158  bytesAfterCompression_ += result->computeChainDataLength();
159  }
160  return result;
161 }
folly::detail::CompressionCounter compressionMilliseconds_
Definition: Compression.h:270
static bool oneIn(uint32_t n)
Definition: Random.h:311
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
folly::detail::CompressionCounter bytesAfterCompression_
Definition: Compression.h:265
folly::detail::CompressionCounter bytesBeforeCompression_
Definition: Compression.h:264
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
virtual std::unique_ptr< IOBuf > doCompress(const folly::IOBuf *data)=0
folly::detail::CompressionCounter compressions_
Definition: Compression.h:268
uint64_t maxUncompressedLength() const
std::string folly::io::Codec::compress ( StringPiece  data)

Compresses data. May involve additional copies compared to the overload that takes and returns IOBufs. Has the same error semantics as the IOBuf version.

Definition at line 163 of file Compression.cpp.

References bytesAfterCompression_, bytesBeforeCompression_, compressionMilliseconds_, compressions_, doCompressString(), maxUncompressedLength(), folly::Random::oneIn(), folly::Range< Iter >::size(), and uint64_t.

163  {
164  const uint64_t len = data.size();
165  if (len > maxUncompressedLength()) {
166  throw std::runtime_error("Codec: uncompressed length too large");
167  }
168  bool const logging = folly::Random::oneIn(kLoggingRate);
169  folly::Optional<Timer> const timer =
170  logging ? Timer(compressionMilliseconds_) : folly::Optional<Timer>();
171  auto result = doCompressString(data);
172  if (logging) {
173  compressions_++;
175  bytesAfterCompression_ += result.size();
176  }
177  return result;
178 }
folly::detail::CompressionCounter compressionMilliseconds_
Definition: Compression.h:270
static bool oneIn(uint32_t n)
Definition: Random.h:311
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
folly::detail::CompressionCounter bytesAfterCompression_
Definition: Compression.h:265
virtual std::string doCompressString(StringPiece data)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
folly::detail::CompressionCounter bytesBeforeCompression_
Definition: Compression.h:264
folly::detail::CompressionCounter compressions_
Definition: Compression.h:268
uint64_t maxUncompressedLength() const
virtual std::unique_ptr<IOBuf> folly::io::Codec::doCompress ( const folly::IOBuf data)
privatepure virtual
std::string folly::io::Codec::doCompressString ( StringPiece  data)
privatevirtual

Definition at line 267 of file Compression.cpp.

References doCompress(), gmock_output_test::output, folly::range(), string, and folly::IOBuf::WRAP_BUFFER.

Referenced by compress().

267  {
268  const IOBuf inputBuffer{IOBuf::WRAP_BUFFER, data};
269  auto outputBuffer = doCompress(&inputBuffer);
271  output.reserve(outputBuffer->computeChainDataLength());
272  for (auto range : *outputBuffer) {
273  output.append(reinterpret_cast<const char*>(range.data()), range.size());
274  }
275  return output;
276 }
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
virtual std::unique_ptr< IOBuf > doCompress(const folly::IOBuf *data)=0
const char * string
Definition: Conv.cpp:212
Optional< uint64_t > folly::io::Codec::doGetUncompressedLength ( const folly::IOBuf data,
folly::Optional< uint64_t uncompressedLength 
) const
privatevirtual

Definition at line 308 of file Compression.cpp.

Referenced by getUncompressedLength().

310  {
311  return uncompressedLength;
312 }
virtual uint64_t folly::io::Codec::doMaxCompressedLength ( uint64_t  uncompressedLength) const
privatepure virtual
uint64_t folly::io::Codec::doMaxUncompressedLength ( ) const
privatevirtual

Definition at line 255 of file Compression.cpp.

References UNLIMITED_UNCOMPRESSED_LENGTH.

Referenced by folly::io::StreamCodec::doUncompress(), and maxUncompressedLength().

255  {
257 }
static constexpr uint64_t UNLIMITED_UNCOMPRESSED_LENGTH
Definition: Compression.h:135
bool folly::io::Codec::doNeedsUncompressedLength ( ) const
privatevirtual

Definition at line 251 of file Compression.cpp.

Referenced by folly::io::StreamCodec::doUncompress(), and needsUncompressedLength().

251  {
252  return false;
253 }
virtual std::unique_ptr<IOBuf> folly::io::Codec::doUncompress ( const folly::IOBuf data,
folly::Optional< uint64_t uncompressedLength 
)
privatepure virtual
std::string folly::io::Codec::doUncompressString ( StringPiece  data,
folly::Optional< uint64_t uncompressedLength 
)
privatevirtual

Definition at line 278 of file Compression.cpp.

References doUncompress(), gmock_output_test::output, folly::range(), string, and folly::IOBuf::WRAP_BUFFER.

Referenced by uncompress().

280  {
281  const IOBuf inputBuffer{IOBuf::WRAP_BUFFER, data};
282  auto outputBuffer = doUncompress(&inputBuffer, uncompressedLength);
284  output.reserve(outputBuffer->computeChainDataLength());
285  for (auto range : *outputBuffer) {
286  output.append(reinterpret_cast<const char*>(range.data()), range.size());
287  }
288  return output;
289 }
virtual std::unique_ptr< IOBuf > doUncompress(const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength)=0
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
const char * string
Definition: Conv.cpp:212
Optional< uint64_t > folly::io::Codec::getUncompressedLength ( const folly::IOBuf data,
folly::Optional< uint64_t uncompressedLength = folly::none 
) const

Extracts the uncompressed length from the compressed data if possible. If the codec doesn't store the uncompressed length, or the data is corrupted it returns the given uncompressedLength. If the uncompressed length is stored in the compressed data and uncompressedLength is not none and they do not match a std::runtime_error is thrown.

Definition at line 295 of file Compression.cpp.

References folly::IOBuf::computeChainDataLength(), doGetUncompressedLength(), and folly::Optional< Value >::value_or().

Referenced by folly::io::StreamCodec::doUncompress().

297  {
298  auto const compressedLength = data->computeChainDataLength();
299  if (compressedLength == 0) {
300  if (uncompressedLength.value_or(0) != 0) {
301  throw std::runtime_error("Invalid uncompressed length");
302  }
303  return 0;
304  }
305  return doGetUncompressedLength(data, uncompressedLength);
306 }
virtual folly::Optional< uint64_t > doGetUncompressedLength(const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength) const
FOLLY_CPP14_CONSTEXPR Value value_or(U &&dflt) const &
Definition: Optional.h:330
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
uint64_t folly::io::Codec::maxCompressedLength ( uint64_t  uncompressedLength) const

Returns a bound on the maximum compressed length when compressing data with the given uncompressed length.

Definition at line 291 of file Compression.cpp.

References doMaxCompressedLength().

Referenced by folly::io::StreamCodec::doCompress(), and folly::io::StreamCodec::doUncompress().

291  {
292  return doMaxCompressedLength(uncompressedLength);
293 }
virtual uint64_t doMaxCompressedLength(uint64_t uncompressedLength) const =0
uint64_t folly::io::Codec::maxUncompressedLength ( ) const

Return the maximum length of data that may be compressed with this codec. NO_COMPRESSION and ZLIB support arbitrary lengths; LZ4 supports up to 1.9GiB; SNAPPY supports up to 4GiB. May return UNLIMITED_UNCOMPRESSED_LENGTH if unlimited.

Definition at line 247 of file Compression.cpp.

References doMaxUncompressedLength().

Referenced by compress(), folly::io::StreamCodec::doUncompress(), and uncompress().

247  {
248  return doMaxUncompressedLength();
249 }
virtual uint64_t doMaxUncompressedLength() const
bool folly::io::Codec::needsUncompressedLength ( ) const

Does this codec need the exact uncompressed length on decompression?

Definition at line 243 of file Compression.cpp.

References doNeedsUncompressedLength().

Referenced by uncompress().

243  {
244  return doNeedsUncompressedLength();
245 }
virtual bool doNeedsUncompressedLength() const
CodecType folly::io::Codec::type ( ) const
inline

Return the codec's type.

Definition at line 147 of file Compression.h.

References folly::data(), name, folly::none, string, type, type_, and uint64_t.

Referenced by Codec(), folly::io::StreamCodec::doUncompress(), folly::io::getCodec(), and folly::io::getStreamCodec().

147  {
148  return type_;
149  }
CodecType type_
Definition: Compression.h:263
std::unique_ptr< IOBuf > folly::io::Codec::uncompress ( const IOBuf data,
folly::Optional< uint64_t uncompressedLength = folly::none 
)

Uncompress data. Throws std::runtime_error on decompression error.

Some codecs (LZ4) require the exact uncompressed length; this is indicated by needsUncompressedLength().

For other codes (zlib), knowing the exact uncompressed length ahead of time might be faster.

Regardless of the behavior of the underlying compressor, uncompressing an empty IOBuf chain will return an empty IOBuf chain.

Definition at line 180 of file Compression.cpp.

References bytesAfterDecompression_, bytesBeforeDecompression_, folly::IOBuf::computeChainDataLength(), folly::IOBuf::create(), decompressionMilliseconds_, decompressions_, doUncompress(), folly::IOBuf::empty(), maxUncompressedLength(), needsUncompressedLength(), folly::Random::oneIn(), and folly::Optional< Value >::value_or().

182  {
183  if (data == nullptr) {
184  throw std::invalid_argument("Codec: data must not be nullptr");
185  }
186  if (!uncompressedLength) {
187  if (needsUncompressedLength()) {
188  throw std::invalid_argument("Codec: uncompressed length required");
189  }
190  } else if (*uncompressedLength > maxUncompressedLength()) {
191  throw std::runtime_error("Codec: uncompressed length too large");
192  }
193 
194  if (data->empty()) {
195  if (uncompressedLength.value_or(0) != 0) {
196  throw std::runtime_error("Codec: invalid uncompressed length");
197  }
198  return IOBuf::create(0);
199  }
200 
201  bool const logging = folly::Random::oneIn(kLoggingRate);
202  folly::Optional<Timer> const timer =
203  logging ? Timer(decompressionMilliseconds_) : folly::Optional<Timer>();
204  auto result = doUncompress(data, uncompressedLength);
205  if (logging) {
206  decompressions_++;
207  bytesBeforeDecompression_ += data->computeChainDataLength();
208  bytesAfterDecompression_ += result->computeChainDataLength();
209  }
210  return result;
211 }
virtual std::unique_ptr< IOBuf > doUncompress(const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength)=0
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
static bool oneIn(uint32_t n)
Definition: Random.h:311
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
folly::detail::CompressionCounter bytesBeforeDecompression_
Definition: Compression.h:266
folly::detail::CompressionCounter decompressions_
Definition: Compression.h:269
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
folly::detail::CompressionCounter bytesAfterDecompression_
Definition: Compression.h:267
FOLLY_CPP14_CONSTEXPR Value value_or(U &&dflt) const &
Definition: Optional.h:330
folly::detail::CompressionCounter decompressionMilliseconds_
Definition: Compression.h:271
uint64_t maxUncompressedLength() const
bool needsUncompressedLength() const
std::string folly::io::Codec::uncompress ( StringPiece  data,
folly::Optional< uint64_t uncompressedLength = folly::none 
)

Uncompresses data. May involve additional copies compared to the overload that takes and returns IOBufs. Has the same error semantics as the IOBuf version.

Definition at line 213 of file Compression.cpp.

References bytesAfterDecompression_, bytesBeforeDecompression_, decompressionMilliseconds_, decompressions_, doUncompressString(), folly::Range< Iter >::empty(), maxUncompressedLength(), needsUncompressedLength(), folly::Random::oneIn(), folly::Range< Iter >::size(), and folly::Optional< Value >::value_or().

215  {
216  if (!uncompressedLength) {
217  if (needsUncompressedLength()) {
218  throw std::invalid_argument("Codec: uncompressed length required");
219  }
220  } else if (*uncompressedLength > maxUncompressedLength()) {
221  throw std::runtime_error("Codec: uncompressed length too large");
222  }
223 
224  if (data.empty()) {
225  if (uncompressedLength.value_or(0) != 0) {
226  throw std::runtime_error("Codec: invalid uncompressed length");
227  }
228  return "";
229  }
230 
231  bool const logging = folly::Random::oneIn(kLoggingRate);
232  folly::Optional<Timer> const timer =
233  logging ? Timer(decompressionMilliseconds_) : folly::Optional<Timer>();
234  auto result = doUncompressString(data, uncompressedLength);
235  if (logging) {
236  decompressions_++;
238  bytesAfterDecompression_ += result.size();
239  }
240  return result;
241 }
virtual std::string doUncompressString(StringPiece data, folly::Optional< uint64_t > uncompressedLength)
static bool oneIn(uint32_t n)
Definition: Random.h:311
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
folly::detail::CompressionCounter bytesBeforeDecompression_
Definition: Compression.h:266
folly::detail::CompressionCounter decompressions_
Definition: Compression.h:269
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
folly::detail::CompressionCounter bytesAfterDecompression_
Definition: Compression.h:267
FOLLY_CPP14_CONSTEXPR Value value_or(U &&dflt) const &
Definition: Optional.h:330
folly::detail::CompressionCounter decompressionMilliseconds_
Definition: Compression.h:271
uint64_t maxUncompressedLength() const
bool needsUncompressedLength() const
std::vector< std::string > folly::io::Codec::validPrefixes ( ) const
virtual

Returns a superset of the set of prefixes for which canUncompress() will return true. A superset is allowed for optimizations in canUncompress() based on other knowledge such as length. None of the prefixes may be empty. default: No prefixes.

Definition at line 259 of file Compression.cpp.

Referenced by folly::io::StreamCodec::doUncompress().

259  {
260  return {};
261 }

Member Data Documentation

folly::detail::CompressionCounter folly::io::Codec::bytesAfterCompression_
private

Definition at line 265 of file Compression.h.

Referenced by Codec(), and compress().

folly::detail::CompressionCounter folly::io::Codec::bytesAfterDecompression_
private

Definition at line 267 of file Compression.h.

Referenced by Codec(), and uncompress().

folly::detail::CompressionCounter folly::io::Codec::bytesBeforeCompression_
private

Definition at line 264 of file Compression.h.

Referenced by Codec(), and compress().

folly::detail::CompressionCounter folly::io::Codec::bytesBeforeDecompression_
private

Definition at line 266 of file Compression.h.

Referenced by Codec(), and uncompress().

folly::detail::CompressionCounter folly::io::Codec::compressionMilliseconds_
private

Definition at line 270 of file Compression.h.

Referenced by Codec(), and compress().

folly::detail::CompressionCounter folly::io::Codec::compressions_
private

Definition at line 268 of file Compression.h.

Referenced by Codec(), and compress().

folly::detail::CompressionCounter folly::io::Codec::decompressionMilliseconds_
private

Definition at line 271 of file Compression.h.

Referenced by Codec(), and uncompress().

folly::detail::CompressionCounter folly::io::Codec::decompressions_
private

Definition at line 269 of file Compression.h.

Referenced by Codec(), and uncompress().

CodecType folly::io::Codec::type_
private

Definition at line 263 of file Compression.h.

constexpr uint64_t folly::io::Codec::UNLIMITED_UNCOMPRESSED_LENGTH = uint64_t(-1)
static

Definition at line 135 of file Compression.h.

Referenced by doMaxUncompressedLength().


The documentation for this class was generated from the following files: