Commit 72f73d40 authored by Felix Handte's avatar Felix Handte Committed by Facebook Github Bot

Compression: Add String Helpers for getUncompressedLength and canUncompress

Summary: As title.

Reviewed By: yfeldblum

Differential Revision: D18351375

fbshipit-source-id: 594270e7b6b7056f7223fa7575ee33fed8f7d266
parent ab0f3727
...@@ -264,6 +264,13 @@ bool Codec::canUncompress(const IOBuf*, Optional<uint64_t>) const { ...@@ -264,6 +264,13 @@ bool Codec::canUncompress(const IOBuf*, Optional<uint64_t>) const {
return false; return false;
} }
bool Codec::canUncompress(
StringPiece data,
Optional<uint64_t> uncompressedLength) const {
auto buf = IOBuf::wrapBufferAsValue(data.data(), data.size());
return canUncompress(&buf, uncompressedLength);
}
std::string Codec::doCompressString(const StringPiece data) { std::string Codec::doCompressString(const StringPiece data) {
const IOBuf inputBuffer{IOBuf::WRAP_BUFFER, data}; const IOBuf inputBuffer{IOBuf::WRAP_BUFFER, data};
auto outputBuffer = doCompress(&inputBuffer); auto outputBuffer = doCompress(&inputBuffer);
...@@ -305,6 +312,13 @@ Optional<uint64_t> Codec::getUncompressedLength( ...@@ -305,6 +312,13 @@ Optional<uint64_t> Codec::getUncompressedLength(
return doGetUncompressedLength(data, uncompressedLength); return doGetUncompressedLength(data, uncompressedLength);
} }
Optional<uint64_t> Codec::getUncompressedLength(
StringPiece data,
Optional<uint64_t> uncompressedLength) const {
auto buf = IOBuf::wrapBufferAsValue(data.data(), data.size());
return getUncompressedLength(&buf, uncompressedLength);
}
Optional<uint64_t> Codec::doGetUncompressedLength( Optional<uint64_t> Codec::doGetUncompressedLength(
const folly::IOBuf*, const folly::IOBuf*,
Optional<uint64_t> uncompressedLength) const { Optional<uint64_t> uncompressedLength) const {
......
...@@ -210,6 +210,13 @@ class Codec { ...@@ -210,6 +210,13 @@ class Codec {
const folly::IOBuf* data, const folly::IOBuf* data,
folly::Optional<uint64_t> uncompressedLength = folly::none) const; folly::Optional<uint64_t> uncompressedLength = folly::none) const;
/**
* Helper wrapper around getUncompressedLength(IOBuf)
*/
folly::Optional<uint64_t> getUncompressedLength(
folly::StringPiece data,
folly::Optional<uint64_t> uncompressedLength = folly::none) const;
protected: protected:
Codec( Codec(
CodecType type, CodecType type,
...@@ -236,6 +243,13 @@ class Codec { ...@@ -236,6 +243,13 @@ class Codec {
const folly::IOBuf* data, const folly::IOBuf* data,
folly::Optional<uint64_t> uncompressedLength = folly::none) const; folly::Optional<uint64_t> uncompressedLength = folly::none) const;
/**
* Helper wrapper around canUncompress(IOBuf)
*/
bool canUncompress(
folly::StringPiece data,
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)
virtual uint64_t doMaxUncompressedLength() const; virtual uint64_t doMaxUncompressedLength() const;
......
...@@ -534,22 +534,34 @@ TEST_P(StreamingUnitTest, maxCompressedLength) { ...@@ -534,22 +534,34 @@ TEST_P(StreamingUnitTest, maxCompressedLength) {
TEST_P(StreamingUnitTest, getUncompressedLength) { TEST_P(StreamingUnitTest, getUncompressedLength) {
auto const empty = IOBuf::create(0); auto const empty = IOBuf::create(0);
EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(""));
EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(empty.get())); EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(empty.get()));
EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(""));
EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(empty.get(), 0)); EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(empty.get(), 0));
EXPECT_ANY_THROW(codec_->getUncompressedLength(empty.get(), 1)); EXPECT_ANY_THROW(codec_->getUncompressedLength(empty.get(), 1));
auto const data = IOBuf::wrapBuffer(randomDataHolder.data(100)); auto const data = IOBuf::wrapBuffer(randomDataHolder.data(100));
auto const compressed = codec_->compress(data.get()); auto const compressed = codec_->compress(data.get());
auto const compressedString =
StringPiece(ByteRange(data->data(), data->length()));
if (auto const length = codec_->getUncompressedLength(data.get())) { if (auto const length = codec_->getUncompressedLength(data.get())) {
EXPECT_EQ(100, *length); EXPECT_EQ(100, *length);
} }
if (auto const length = codec_->getUncompressedLength(compressedString)) {
EXPECT_EQ(100, *length);
}
EXPECT_EQ(uint64_t(100), codec_->getUncompressedLength(data.get(), 100)); EXPECT_EQ(uint64_t(100), codec_->getUncompressedLength(data.get(), 100));
EXPECT_EQ(
uint64_t(100), codec_->getUncompressedLength(compressedString, 100));
// If the uncompressed length is stored in the frame, then make sure it throws // If the uncompressed length is stored in the frame, then make sure it throws
// when it is given the wrong length. // when it is given the wrong length.
if (codec_->getUncompressedLength(data.get()) == uint64_t(100)) { if (codec_->getUncompressedLength(data.get()) == uint64_t(100)) {
EXPECT_ANY_THROW(codec_->getUncompressedLength(data.get(), 200)); EXPECT_ANY_THROW(codec_->getUncompressedLength(data.get(), 200));
} }
if (codec_->getUncompressedLength(compressedString) == uint64_t(100)) {
EXPECT_ANY_THROW(codec_->getUncompressedLength(compressedString, 200));
}
} }
TEST_P(StreamingUnitTest, emptyData) { TEST_P(StreamingUnitTest, emptyData) {
......
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