Commit 1e8e9c94 authored by Nick Terrell's avatar Nick Terrell Committed by Facebook Github Bot

Switch uncompressedLength to an Optional<uint64_t>

Summary: Instead of using `UNKNWON_UNCOMPRESSED_LENGTH` use an `Optional`.

Reviewed By: yfeldblum

Differential Revision: D5038919

fbshipit-source-id: 7fb60542277019996be3ff50509df5a5060cb1a0
parent 42c42914
This diff is collapsed.
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <folly/Optional.h>
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/io/IOBuf.h> #include <folly/io/IOBuf.h>
...@@ -110,7 +111,7 @@ class Codec { ...@@ -110,7 +111,7 @@ class Codec {
* Return the maximum length of data that may be compressed with this codec. * Return the maximum length of data that may be compressed with this codec.
* NO_COMPRESSION and ZLIB support arbitrary lengths; * NO_COMPRESSION and ZLIB support arbitrary lengths;
* LZ4 supports up to 1.9GiB; SNAPPY supports up to 4GiB. * LZ4 supports up to 1.9GiB; SNAPPY supports up to 4GiB.
* May return UNLIMITED_UNCOMPRESSED_LENGTH if unlimited. * May return UNLIMITED_UNCOMPRESSED_LENGTH (uint64_t(-1)) if unlimited.
*/ */
uint64_t maxUncompressedLength() const; uint64_t maxUncompressedLength() const;
...@@ -153,12 +154,11 @@ class Codec { ...@@ -153,12 +154,11 @@ class Codec {
* Regardless of the behavior of the underlying compressor, uncompressing * Regardless of the behavior of the underlying compressor, uncompressing
* an empty IOBuf chain will return an empty IOBuf chain. * an empty IOBuf chain will return an empty IOBuf chain.
*/ */
static constexpr uint64_t UNKNOWN_UNCOMPRESSED_LENGTH = uint64_t(-1); static constexpr uint64_t UNLIMITED_UNCOMPRESSED_LENGTH = uint64_t(-1);
static constexpr uint64_t UNLIMITED_UNCOMPRESSED_LENGTH = uint64_t(-2);
std::unique_ptr<IOBuf> uncompress( std::unique_ptr<IOBuf> uncompress(
const IOBuf* data, const IOBuf* data,
uint64_t uncompressedLength = UNKNOWN_UNCOMPRESSED_LENGTH); folly::Optional<uint64_t> uncompressedLength = folly::none);
/** /**
* Uncompresses data. May involve additional copies compared to the overload * Uncompresses data. May involve additional copies compared to the overload
...@@ -167,7 +167,7 @@ class Codec { ...@@ -167,7 +167,7 @@ class Codec {
*/ */
std::string uncompress( std::string uncompress(
StringPiece data, StringPiece data,
uint64_t uncompressedLength = UNKNOWN_UNCOMPRESSED_LENGTH); folly::Optional<uint64_t> uncompressedLength = folly::none);
protected: protected:
explicit Codec(CodecType type); explicit Codec(CodecType type);
...@@ -189,7 +189,7 @@ class Codec { ...@@ -189,7 +189,7 @@ class Codec {
*/ */
virtual bool canUncompress( virtual bool canUncompress(
const folly::IOBuf* data, const folly::IOBuf* data,
uint64_t uncompressedLength = UNKNOWN_UNCOMPRESSED_LENGTH) const; folly::Optional<uint64_t> uncompressedLength = folly::none) const;
private: private:
// default: no limits (save for special value UNKNOWN_UNCOMPRESSED_LENGTH) // default: no limits (save for special value UNKNOWN_UNCOMPRESSED_LENGTH)
...@@ -197,8 +197,9 @@ class Codec { ...@@ -197,8 +197,9 @@ class Codec {
// default: doesn't need uncompressed length // default: doesn't need uncompressed length
virtual bool doNeedsUncompressedLength() const; virtual bool doNeedsUncompressedLength() const;
virtual std::unique_ptr<IOBuf> doCompress(const folly::IOBuf* data) = 0; virtual std::unique_ptr<IOBuf> doCompress(const folly::IOBuf* data) = 0;
virtual std::unique_ptr<IOBuf> doUncompress(const folly::IOBuf* data, virtual std::unique_ptr<IOBuf> doUncompress(
uint64_t uncompressedLength) = 0; const folly::IOBuf* data,
folly::Optional<uint64_t> uncompressedLength) = 0;
// default: an implementation is provided by default to wrap the strings into // default: an implementation is provided by default to wrap the strings into
// IOBufs and delegate to the IOBuf methods. This incurs a copy of the output // IOBufs and delegate to the IOBuf methods. This incurs a copy of the output
// from IOBuf to string. Implementers, at their discretion, can override // from IOBuf to string. Implementers, at their discretion, can override
...@@ -206,7 +207,7 @@ class Codec { ...@@ -206,7 +207,7 @@ class Codec {
virtual std::string doCompressString(StringPiece data); virtual std::string doCompressString(StringPiece data);
virtual std::string doUncompressString( virtual std::string doUncompressString(
StringPiece data, StringPiece data,
uint64_t uncompressedLength); folly::Optional<uint64_t> uncompressedLength);
CodecType type_; CodecType type_;
}; };
......
...@@ -500,7 +500,7 @@ class CustomCodec : public Codec { ...@@ -500,7 +500,7 @@ class CustomCodec : public Codec {
return {prefix_}; return {prefix_};
} }
bool canUncompress(const IOBuf* data, uint64_t) const override { bool canUncompress(const IOBuf* data, Optional<uint64_t>) const override {
auto clone = data->cloneCoalescedAsValue(); auto clone = data->cloneCoalescedAsValue();
if (clone.length() < prefix_.size()) { if (clone.length() < prefix_.size()) {
return false; return false;
...@@ -517,7 +517,7 @@ class CustomCodec : public Codec { ...@@ -517,7 +517,7 @@ class CustomCodec : public Codec {
std::unique_ptr<IOBuf> doUncompress( std::unique_ptr<IOBuf> doUncompress(
const IOBuf* data, const IOBuf* data,
uint64_t uncompressedLength) override { Optional<uint64_t> uncompressedLength) override {
EXPECT_TRUE(canUncompress(data, uncompressedLength)); EXPECT_TRUE(canUncompress(data, uncompressedLength));
auto clone = data->cloneCoalescedAsValue(); auto clone = data->cloneCoalescedAsValue();
clone.trimStart(prefix_.size()); clone.trimStart(prefix_.size());
...@@ -573,9 +573,9 @@ TEST_P(AutomaticCodecTest, canUncompressOneBytes) { ...@@ -573,9 +573,9 @@ TEST_P(AutomaticCodecTest, canUncompressOneBytes) {
IOBuf buf{IOBuf::CREATE, 1}; IOBuf buf{IOBuf::CREATE, 1};
buf.append(1); buf.append(1);
EXPECT_FALSE(codec_->canUncompress(&buf, 1)); EXPECT_FALSE(codec_->canUncompress(&buf, 1));
EXPECT_FALSE(codec_->canUncompress(&buf, Codec::UNKNOWN_UNCOMPRESSED_LENGTH)); EXPECT_FALSE(codec_->canUncompress(&buf, folly::none));
EXPECT_FALSE(auto_->canUncompress(&buf, 1)); EXPECT_FALSE(auto_->canUncompress(&buf, 1));
EXPECT_FALSE(auto_->canUncompress(&buf, Codec::UNKNOWN_UNCOMPRESSED_LENGTH)); EXPECT_FALSE(auto_->canUncompress(&buf, folly::none));
} }
INSTANTIATE_TEST_CASE_P( INSTANTIATE_TEST_CASE_P(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment