diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp')
-rw-r--r-- | library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp b/library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp new file mode 100644 index 0000000000..da2831fbd2 --- /dev/null +++ b/library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp @@ -0,0 +1,54 @@ +#include <library/cpp/blockcodecs/core/codecs.h> +#include <library/cpp/blockcodecs/core/common.h> +#include <library/cpp/blockcodecs/core/register.h> + +#include <contrib/libs/fastlz/fastlz.h> + +using namespace NBlockCodecs; + +namespace { + struct TFastLZCodec: public TAddLengthCodec<TFastLZCodec> { + inline TFastLZCodec(int level) + : MyName("fastlz-" + ToString(level)) + , Level(level) + { + } + + static inline size_t DoMaxCompressedLength(size_t in) noexcept { + return Max<size_t>(in + in / 20, 128); + } + + TStringBuf Name() const noexcept override { + return MyName; + } + + inline size_t DoCompress(const TData& in, void* buf) const { + if (Level) { + return fastlz_compress_level(Level, in.data(), in.size(), buf); + } + + return fastlz_compress(in.data(), in.size(), buf); + } + + inline void DoDecompress(const TData& in, void* out, size_t len) const { + const int ret = fastlz_decompress(in.data(), in.size(), out, len); + + if (ret < 0 || (size_t)ret != len) { + ythrow TDataError() << TStringBuf("can not decompress"); + } + } + + const TString MyName; + const int Level; + }; + + struct TFastLZRegistrar { + TFastLZRegistrar() { + for (int i = 0; i < 3; ++i) { + RegisterCodec(MakeHolder<TFastLZCodec>(i)); + } + RegisterAlias("fastlz", "fastlz-0"); + } + }; + const TFastLZRegistrar Registrar{}; +} |