codecs.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <util/generic/buffer.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/typetraits.h>
  6. #include <util/generic/vector.h>
  7. #include <util/generic/yexception.h>
  8. namespace NBlockCodecs {
  9. struct TData: public TStringBuf {
  10. inline TData() = default;
  11. Y_HAS_MEMBER(Data);
  12. Y_HAS_MEMBER(Size);
  13. template <class T, std::enable_if_t<!THasSize<T>::value || !THasData<T>::value, int> = 0>
  14. inline TData(const T& t)
  15. : TStringBuf((const char*)t.data(), t.size())
  16. {
  17. }
  18. template <class T, std::enable_if_t<THasSize<T>::value && THasData<T>::value, int> = 0>
  19. inline TData(const T& t)
  20. : TStringBuf((const char*)t.Data(), t.Size())
  21. {
  22. }
  23. };
  24. struct TCodecError: public yexception {
  25. };
  26. struct TNotFound: public TCodecError {
  27. };
  28. struct TDataError: public TCodecError {
  29. };
  30. struct ICodec {
  31. virtual ~ICodec();
  32. // main interface
  33. virtual size_t DecompressedLength(const TData& in) const = 0;
  34. virtual size_t MaxCompressedLength(const TData& in) const = 0;
  35. virtual size_t Compress(const TData& in, void* out) const = 0;
  36. virtual size_t Decompress(const TData& in, void* out) const = 0;
  37. virtual TStringBuf Name() const noexcept = 0;
  38. // some useful helpers
  39. void Encode(const TData& in, TBuffer& out) const;
  40. void Decode(const TData& in, TBuffer& out) const;
  41. void Encode(const TData& in, TString& out) const;
  42. void Decode(const TData& in, TString& out) const;
  43. inline TString Encode(const TData& in) const {
  44. TString out;
  45. Encode(in, out);
  46. return out;
  47. }
  48. inline TString Decode(const TData& in) const {
  49. TString out;
  50. Decode(in, out);
  51. return out;
  52. }
  53. private:
  54. size_t GetDecompressedLength(const TData& in) const;
  55. };
  56. using TCodecPtr = THolder<ICodec>;
  57. const ICodec* Codec(const TStringBuf& name);
  58. // some aux methods
  59. typedef TVector<TStringBuf> TCodecList;
  60. TCodecList ListAllCodecs();
  61. TString ListAllCodecsAsString();
  62. // SEARCH-8344: Get the size of max possible decompressed block
  63. size_t GetMaxPossibleDecompressedLength();
  64. // SEARCH-8344: Globally set the size of max possible decompressed block
  65. void SetMaxPossibleDecompressedLength(size_t maxPossibleDecompressedLength);
  66. }