proxygen
|
#include <Compression.h>
Public Types | |
enum | FlushOp { FlushOp::NONE, FlushOp::FLUSH, FlushOp::END } |
Protected Member Functions | |
StreamCodec (CodecType type, folly::Optional< int > level=folly::none, folly::StringPiece name={}, bool counters=true) | |
folly::Optional< uint64_t > | uncompressedLength () const |
Protected Member Functions inherited from folly::io::Codec | |
Codec (CodecType type, folly::Optional< int > level=folly::none, folly::StringPiece name={}, bool counters=true) | |
Private Types | |
enum | State { State::RESET, State::COMPRESS, State::COMPRESS_FLUSH, State::COMPRESS_END, State::UNCOMPRESS, State::END } |
Private Member Functions | |
std::unique_ptr< IOBuf > | doCompress (const folly::IOBuf *data) override |
std::unique_ptr< IOBuf > | doUncompress (const folly::IOBuf *data, folly::Optional< uint64_t > uncompressedLength) override |
virtual bool | doNeedsDataLength () const |
virtual void | doResetStream ()=0 |
virtual bool | doCompressStream (folly::ByteRange &input, folly::MutableByteRange &output, FlushOp flushOp)=0 |
virtual bool | doUncompressStream (folly::ByteRange &input, folly::MutableByteRange &output, FlushOp flushOp)=0 |
void | assertStateIs (State expected) const |
Private Attributes | |
State | state_ {State::RESET} |
ByteRange | previousInput_ {} |
folly::Optional< uint64_t > | uncompressedLength_ {} |
bool | progressMade_ {true} |
Additional Inherited Members | |
Static Public Attributes inherited from folly::io::Codec | |
static constexpr uint64_t | UNLIMITED_UNCOMPRESSED_LENGTH = uint64_t(-1) |
Definition at line 274 of file Compression.h.
|
strong |
|
strongprivate |
Enumerator | |
---|---|
RESET | |
COMPRESS | |
COMPRESS_FLUSH | |
COMPRESS_END | |
UNCOMPRESS | |
END |
Definition at line 424 of file Compression.h.
|
inlineoverride |
|
inlineprotected |
Definition at line 392 of file Compression.h.
References folly::gen::move, and name.
|
private |
Definition at line 322 of file Compression.cpp.
bool folly::io::StreamCodec::compressStream | ( | folly::ByteRange & | input, |
folly::MutableByteRange & | output, | ||
StreamCodec::FlushOp | flushOp = StreamCodec::FlushOp::NONE |
||
) |
Compresses some data from the input buffer and writes the compressed data into the output buffer. It may read input without producing any output, except when forced to flush.
The input buffer is advanced to point to the range of data that hasn't yet been read. Compression will resume at this point for the next call to compressStream(). The output buffer is advanced one byte past the last byte written.
The default flushOp is NONE, which allows compressStream() complete discretion in how much data to gather before writing any output.
If flushOp is END, all pending and input data is flushed to the output buffer, and the frame is ended. compressStream() must be called with the same input and flushOp END until it returns true. At this point the caller must call resetStream() to use the codec again.
If flushOp is FLUSH, all pending and input data is flushed to the output buffer, but the frame is not ended. compressStream() must be called with the same input and flushOp END until it returns true. At this point the caller can continue to compressStream() with any input data and flushOp. The uncompressor, if passed all the produced output data, will be able to uncompress all the input data passed to compressStream() so far. Excessive use of flushOp FLUSH will deteriorate compression ratio. This is useful for stateful streaming across a network. Most users don't need to use this flushOp.
A std::logic_error is thrown on incorrect usage of the API. A std::runtime_error is thrown upon error conditions or if no forward progress could be made twice in a row.
Definition at line 336 of file Compression.cpp.
References folly::Range< Iter >::empty(), END, FLUSH, NONE, folly::Range< Iter >::size(), and uint64_t.
Referenced by folly::io::test::compressSome().
|
overrideprivatevirtual |
Implements folly::io::Codec.
Definition at line 444 of file Compression.cpp.
References folly::io::addOutputBuffer(), buffer(), folly::IOBuf::computeChainDataLength(), current, folly::data(), folly::IOBuf::data(), folly::Range< Iter >::empty(), END, folly::IOBuf::length(), folly::io::Codec::maxCompressedLength(), folly::IOBuf::next(), NONE, gmock_output_test::output, folly::Range< Iter >::size(), and uint64_t.
|
privatepure virtual |
|
privatevirtual |
Definition at line 318 of file Compression.cpp.
|
privatepure virtual |
|
overrideprivatevirtual |
Implements folly::io::Codec.
Definition at line 494 of file Compression.cpp.
References folly::io::addOutputBuffer(), folly::Range< Iter >::advance(), folly::IOBuf::append(), b, folly::IOBuf::begin(), buffer(), folly::io::BZIP2, folly::IOBufQueue::cacheChainLength(), folly::io::Codec::canUncompress(), folly::IOBufQueue::chainLength(), folly::IOBuf::clone(), folly::IOBuf::cloneCoalescedAsValue(), folly::IOBuf::coalesce(), folly::io::Codec::Codec(), folly::io::COMPRESSION_LEVEL_BEST, folly::io::COMPRESSION_LEVEL_DEFAULT, folly::io::COMPRESSION_LEVEL_FASTEST, folly::io::computeBufferLength(), folly::IOBuf::computeChainDataLength(), folly::IOBuf::create(), current, folly::data(), folly::Range< Iter >::data(), folly::IOBuf::data(), folly::io::compression::detail::dataStartsWithLE(), folly::io::Codec::doCompress(), folly::io::Codec::doMaxCompressedLength(), folly::io::Codec::doMaxUncompressedLength(), folly::io::Codec::doNeedsUncompressedLength(), folly::io::Codec::doUncompress(), folly::Range< Iter >::empty(), folly::encodeVarint(), END, FLUSH, folly::io::getCodec(), folly::io::getStreamCodec(), folly::io::Codec::getUncompressedLength(), folly::io::GZIP, folly::Optional< Value >::hasValue(), int8_t, folly::IOBuf::isChained(), folly::kMaxVarintLength64, folly::IOBuf::length(), folly::io::LZ4, folly::io::LZ4_FRAME, folly::io::LZ4_VARINT_SIZE, folly::io::LZMA2, folly::io::LZMA2_VARINT_SIZE, max, folly::io::Codec::maxCompressedLength(), folly::io::Codec::maxUncompressedLength(), min, folly::IOBufQueue::move(), folly::IOBuf::next(), folly::io::NO_COMPRESSION, NONE, gmock_output_test::output, folly::io::detail::CursorBase< Derived, BufType >::peekBytes(), folly::IOBufQueue::postallocate(), folly::IOBufQueue::preallocate(), folly::io::compression::detail::prefixToStringLE(), folly::range(), folly::io::detail::CursorBase< Derived, BufType >::read(), SCOPE_EXIT, folly::Range< Iter >::size(), folly::Skip, folly::io::SNAPPY, stream, folly::IOBuf::tailroom(), folly::TooManyBytes, folly::tryDecodeVarint(), folly::io::Codec::type(), uint32_t, uint64_t, uint8_t, folly::Range< Iter >::uncheckedAdvance(), val, folly::io::Codec::validPrefixes(), folly::IOBuf::writableTail(), folly::io::ZLIB, folly::io::ZSTD, and folly::io::ZSTD_FAST.
|
privatepure virtual |
bool folly::io::StreamCodec::needsDataLength | ( | ) | const |
Does the codec need the data length before compression streaming?
Definition at line 314 of file Compression.cpp.
void folly::io::StreamCodec::resetStream | ( | folly::Optional< uint64_t > | uncompressedLength = folly::none | ) |
Reset the state of the codec, and set the uncompressed length for the next streaming operation. If uncompressedLength is not none it must be exactly the uncompressed length. compressStream() must be passed exactly uncompressedLength input bytes before the stream is ended. uncompressStream() must be passed a compressed frame that uncompresses to uncompressedLength.
Definition at line 329 of file Compression.cpp.
|
inlineprotected |
Definition at line 401 of file Compression.h.
References folly::data().
bool folly::io::StreamCodec::uncompressStream | ( | folly::ByteRange & | input, |
folly::MutableByteRange & | output, | ||
StreamCodec::FlushOp | flushOp = StreamCodec::FlushOp::NONE |
||
) |
Uncompresses some data from the input buffer and writes the uncompressed data into the output buffer. It may read input without producing any output.
The input buffer is advanced to point to the range of data that hasn't yet been read. Uncompression will resume at this point for the next call to uncompressStream(). The output buffer is advanced one byte past the last byte written.
The default flushOp is NONE, which allows uncompressStream() complete discretion in how much output data to flush. The uncompressor may not make maximum forward progress, but will make some forward progress when possible.
If flushOp is END, the caller guarantees that no more input will be presented to uncompressStream(). uncompressStream() must be called with the same input and flushOp END until it returns true. This is not mandatory, but if the input is all available in one buffer, and there is enough output space to write the entire frame, codecs can uncompress faster.
If flushOp is FLUSH, uncompressStream() is guaranteed to make the maximum amount of forward progress possible. When using this flushOp and uncompressStream() returns with !output.empty()
the caller knows that all pending output has been flushed. This is useful for stateful streaming across a network, and it should be used in conjunction with compressStream() with flushOp FLUSH. Most users don't need to use this flushOp.
A std::runtime_error is thrown upon error conditions or if no forward progress could be made upon two consecutive calls to the function (only the second call will throw an exception).
Returns true at the end of a frame. At this point resetStream() must be called to reuse the codec.
Definition at line 400 of file Compression.cpp.
References folly::Range< Iter >::empty(), and folly::Range< Iter >::size().
Referenced by folly::io::test::uncompressSome().
|
private |
Definition at line 435 of file Compression.h.
|
private |
Definition at line 437 of file Compression.h.
|
private |
Definition at line 434 of file Compression.h.
|
private |
Definition at line 436 of file Compression.h.