aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/blockcodecs/codecs/snappy/snappy.cpp
blob: 44d8c140a42495e2fe3c8fb967e32ae3d65c69a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <library/cpp/blockcodecs/core/codecs.h>
#include <library/cpp/blockcodecs/core/common.h>
#include <library/cpp/blockcodecs/core/register.h>

#include <contrib/libs/snappy/snappy.h>

using namespace NBlockCodecs;

namespace {
    struct TSnappyCodec: public ICodec {
        size_t DecompressedLength(const TData& in) const override {
            size_t ret;

            if (snappy::GetUncompressedLength(in.data(), in.size(), &ret)) {
                return ret;
            }

            ythrow TDecompressError(0);
        }

        size_t MaxCompressedLength(const TData& in) const override {
            return snappy::MaxCompressedLength(in.size());
        }

        size_t Compress(const TData& in, void* out) const override {
            size_t ret;

            snappy::RawCompress(in.data(), in.size(), (char*)out, &ret);

            return ret;
        }

        size_t Decompress(const TData& in, void* out) const override {
            if (snappy::RawUncompress(in.data(), in.size(), (char*)out)) {
                return DecompressedLength(in);
            }

            ythrow TDecompressError(0);
        }

        TStringBuf Name() const noexcept override {
            return "snappy";
        }
    };

    struct TSnappyRegistrar {
        TSnappyRegistrar() {
            RegisterCodec(MakeHolder<TSnappyCodec>()); 
        }
    };
    const TSnappyRegistrar Registrar{};
}