Commit 49a54a71 authored by Nick Terrell's avatar Nick Terrell Committed by Facebook Github Bot

Allow counters to be disabled

Summary:
Some codecs may not want counters enabled. Right now that will
`std::terminate`. Allow disabled counters, and add a test.

Reviewed By: felixhandte

Differential Revision: D14327170

fbshipit-source-id: 37858518c80e192e351e8578e088b59732d2a0cb
parent 8ef7d7f1
...@@ -69,13 +69,14 @@ namespace detail { ...@@ -69,13 +69,14 @@ namespace detail {
/// Wrapper around the makeCompressionCounterHandler() extension point. /// Wrapper around the makeCompressionCounterHandler() extension point.
class CompressionCounter { class CompressionCounter {
public: public:
CompressionCounter() {} CompressionCounter() : initialized_(true) {}
CompressionCounter( CompressionCounter(
folly::io::CodecType codecType, folly::io::CodecType codecType,
folly::StringPiece codecName, folly::StringPiece codecName,
folly::Optional<int> level, folly::Optional<int> level,
CompressionCounterKey key, CompressionCounterKey key,
CompressionCounterType counterType) { CompressionCounterType counterType)
: initialized_(false) {
initialize_ = [=]() { initialize_ = [=]() {
return makeCompressionCounterHandler( return makeCompressionCounterHandler(
codecType, codecName, level, key, counterType); codecType, codecName, level, key, counterType);
...@@ -112,7 +113,7 @@ class CompressionCounter { ...@@ -112,7 +113,7 @@ class CompressionCounter {
} }
} }
bool initialized_{false}; bool initialized_;
folly::Function<folly::Function<void(double)>()> initialize_; folly::Function<folly::Function<void(double)>()> initialize_;
folly::Function<void(double)> increment_; folly::Function<void(double)> increment_;
}; };
......
...@@ -464,6 +464,35 @@ static bool codecHasFlush(CodecType type) { ...@@ -464,6 +464,35 @@ static bool codecHasFlush(CodecType type) {
return type != CodecType::BZIP2; return type != CodecType::BZIP2;
} }
namespace {
class NoCountersCodec : public Codec {
public:
NoCountersCodec()
: Codec(CodecType::NO_COMPRESSION, {}, {}, /* counters */ false) {}
private:
uint64_t doMaxCompressedLength(uint64_t uncompressedLength) const override {
return uncompressedLength;
}
std::unique_ptr<IOBuf> doCompress(const IOBuf* buf) override {
return buf->clone();
}
std::unique_ptr<IOBuf> doUncompress(const IOBuf* buf, Optional<uint64_t>)
override {
return buf->clone();
}
};
} // namespace
TEST(CodecTest, NoCounters) {
NoCountersCodec codec;
for (size_t i = 0; i < 1000; ++i) {
EXPECT_EQ("hello", codec.uncompress(codec.compress("hello")));
}
}
class StreamingUnitTest : public testing::TestWithParam<CodecType> { class StreamingUnitTest : public testing::TestWithParam<CodecType> {
protected: protected:
void SetUp() override { void SetUp() override {
......
...@@ -49,3 +49,8 @@ TEST(FollyCountersTest, AvgWorks) { ...@@ -49,3 +49,8 @@ TEST(FollyCountersTest, AvgWorks) {
counter += 5; counter += 5;
} }
} }
TEST(FollyCountersTest, DefaultConstruction) {
CompressionCounter counter;
++counter;
}
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