lz4.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/lz4/lz4.h>
  5. #include <contrib/libs/lz4/lz4hc.h>
  6. using namespace NBlockCodecs;
  7. namespace {
  8. struct TLz4Base {
  9. static inline size_t DoMaxCompressedLength(size_t in) {
  10. return LZ4_compressBound(SafeIntegerCast<int>(in));
  11. }
  12. };
  13. struct TLz4FastCompress {
  14. inline TLz4FastCompress(int memory)
  15. : Memory(memory)
  16. {
  17. }
  18. inline size_t DoCompress(const TData& in, void* buf) const {
  19. return LZ4_compress_default(in.data(), (char*)buf, in.size(), LZ4_compressBound(in.size()));
  20. }
  21. inline TString CPrefix() {
  22. return "fast" + ToString(Memory);
  23. }
  24. const int Memory;
  25. };
  26. struct TLz4BestCompress {
  27. inline size_t DoCompress(const TData& in, void* buf) const {
  28. return LZ4_compress_HC(in.data(), (char*)buf, in.size(), LZ4_compressBound(in.size()), 0);
  29. }
  30. static inline TString CPrefix() {
  31. return "hc";
  32. }
  33. };
  34. struct TLz4FastDecompress {
  35. inline void DoDecompress(const TData& in, void* out, size_t len) const {
  36. ssize_t res = LZ4_decompress_fast(in.data(), (char*)out, len);
  37. if (res < 0) {
  38. ythrow TDecompressError(res);
  39. }
  40. }
  41. static inline TStringBuf DPrefix() {
  42. return TStringBuf("fast");
  43. }
  44. };
  45. struct TLz4SafeDecompress {
  46. inline void DoDecompress(const TData& in, void* out, size_t len) const {
  47. ssize_t res = LZ4_decompress_safe(in.data(), (char*)out, in.size(), len);
  48. if (res < 0) {
  49. ythrow TDecompressError(res);
  50. }
  51. }
  52. static inline TStringBuf DPrefix() {
  53. return TStringBuf("safe");
  54. }
  55. };
  56. template <class TC, class TD>
  57. struct TLz4Codec: public TAddLengthCodec<TLz4Codec<TC, TD>>, public TLz4Base, public TC, public TD {
  58. inline TLz4Codec()
  59. : MyName("lz4-" + TC::CPrefix() + "-" + TD::DPrefix())
  60. {
  61. }
  62. template <class T>
  63. inline TLz4Codec(const T& t)
  64. : TC(t)
  65. , MyName("lz4-" + TC::CPrefix() + "-" + TD::DPrefix())
  66. {
  67. }
  68. TStringBuf Name() const noexcept override {
  69. return MyName;
  70. }
  71. const TString MyName;
  72. };
  73. struct TLz4Registrar {
  74. TLz4Registrar() {
  75. for (int i = 10; i <= 20; ++i) {
  76. typedef TLz4Codec<TLz4FastCompress, TLz4FastDecompress> T1;
  77. typedef TLz4Codec<TLz4FastCompress, TLz4SafeDecompress> T2;
  78. THolder<T1> t1(new T1(i));
  79. THolder<T2> t2(new T2(i));
  80. RegisterCodec(std::move(t1));
  81. RegisterCodec(std::move(t2));
  82. }
  83. RegisterCodec(MakeHolder<TLz4Codec<TLz4BestCompress, TLz4FastDecompress>>());
  84. RegisterCodec(MakeHolder<TLz4Codec<TLz4BestCompress, TLz4SafeDecompress>>());
  85. RegisterAlias("lz4-fast-safe", "lz4-fast14-safe");
  86. RegisterAlias("lz4-fast-fast", "lz4-fast14-fast");
  87. RegisterAlias("lz4", "lz4-fast-safe");
  88. RegisterAlias("lz4fast", "lz4-fast-fast");
  89. RegisterAlias("lz4hc", "lz4-hc-safe");
  90. }
  91. };
  92. const TLz4Registrar Registrar{};
  93. }