fastlz.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <library/cpp/blockcodecs/core/codecs.h>
  2. #include <library/cpp/blockcodecs/core/common.h>
  3. #include <library/cpp/blockcodecs/core/register.h>
  4. #include <contrib/libs/fastlz/fastlz.h>
  5. using namespace NBlockCodecs;
  6. namespace {
  7. struct TFastLZCodec: public TAddLengthCodec<TFastLZCodec> {
  8. inline TFastLZCodec(int level)
  9. : MyName("fastlz-" + ToString(level))
  10. , Level(level)
  11. {
  12. }
  13. static inline size_t DoMaxCompressedLength(size_t in) noexcept {
  14. return Max<size_t>(in + in / 20, 128);
  15. }
  16. TStringBuf Name() const noexcept override {
  17. return MyName;
  18. }
  19. inline size_t DoCompress(const TData& in, void* buf) const {
  20. if (Level) {
  21. return fastlz_compress_level(Level, in.data(), in.size(), buf);
  22. }
  23. return fastlz_compress(in.data(), in.size(), buf);
  24. }
  25. inline void DoDecompress(const TData& in, void* out, size_t len) const {
  26. const int ret = fastlz_decompress(in.data(), in.size(), out, len);
  27. if (ret < 0 || (size_t)ret != len) {
  28. ythrow TDataError() << TStringBuf("can not decompress");
  29. }
  30. }
  31. const TString MyName;
  32. const int Level;
  33. };
  34. struct TFastLZRegistrar {
  35. TFastLZRegistrar() {
  36. for (int i = 0; i < 3; ++i) {
  37. RegisterCodec(MakeHolder<TFastLZCodec>(i));
  38. }
  39. RegisterAlias("fastlz", "fastlz-0");
  40. }
  41. };
  42. const TFastLZRegistrar Registrar{};
  43. }