snappy.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/snappy/snappy.h>
  5. using namespace NBlockCodecs;
  6. namespace {
  7. struct TSnappyCodec: public ICodec {
  8. size_t DecompressedLength(const TData& in) const override {
  9. size_t ret;
  10. if (snappy::GetUncompressedLength(in.data(), in.size(), &ret)) {
  11. return ret;
  12. }
  13. ythrow TDecompressError(0);
  14. }
  15. size_t MaxCompressedLength(const TData& in) const override {
  16. return snappy::MaxCompressedLength(in.size());
  17. }
  18. size_t Compress(const TData& in, void* out) const override {
  19. size_t ret;
  20. snappy::RawCompress(in.data(), in.size(), (char*)out, &ret);
  21. return ret;
  22. }
  23. size_t Decompress(const TData& in, void* out) const override {
  24. if (snappy::RawUncompress(in.data(), in.size(), (char*)out)) {
  25. return DecompressedLength(in);
  26. }
  27. ythrow TDecompressError(0);
  28. }
  29. TStringBuf Name() const noexcept override {
  30. return "snappy";
  31. }
  32. };
  33. struct TSnappyRegistrar {
  34. TSnappyRegistrar() {
  35. RegisterCodec(MakeHolder<TSnappyCodec>());
  36. }
  37. };
  38. const TSnappyRegistrar Registrar{};
  39. }