aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/blockcodecs/core/codecs.h
blob: 878678d5c7a4a9419308c496b62ad8e4865477f4 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once

#include <util/generic/buffer.h>
#include <util/generic/strbuf.h>
#include <util/generic/string.h> 
#include <util/generic/typetraits.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>

namespace NBlockCodecs {
    struct TData: public TStringBuf {
        inline TData() = default;

        Y_HAS_MEMBER(Data);
        Y_HAS_MEMBER(Size);

        template <class T, std::enable_if_t<!THasSize<T>::value || !THasData<T>::value, int> = 0>
        inline TData(const T& t)
            : TStringBuf((const char*)t.data(), t.size())
        {
        }

        template <class T, std::enable_if_t<THasSize<T>::value && THasData<T>::value, int> = 0>
        inline TData(const T& t)
            : TStringBuf((const char*)t.Data(), t.Size())
        {
        }
    };

    struct TCodecError: public yexception {
    };

    struct TNotFound: public TCodecError {
    };

    struct TDataError: public TCodecError {
    };

    struct ICodec {
        virtual ~ICodec();

        // main interface
        virtual size_t DecompressedLength(const TData& in) const = 0;
        virtual size_t MaxCompressedLength(const TData& in) const = 0;
        virtual size_t Compress(const TData& in, void* out) const = 0;
        virtual size_t Decompress(const TData& in, void* out) const = 0;

        virtual TStringBuf Name() const noexcept = 0; 

        // some useful helpers
        void Encode(const TData& in, TBuffer& out) const;
        void Decode(const TData& in, TBuffer& out) const;

        void Encode(const TData& in, TString& out) const; 
        void Decode(const TData& in, TString& out) const; 

        inline TString Encode(const TData& in) const { 
            TString out; 

            Encode(in, out);

            return out;
        }

        inline TString Decode(const TData& in) const { 
            TString out; 

            Decode(in, out);

            return out;
        }
    private:
        size_t GetDecompressedLength(const TData& in) const;
    };

    using TCodecPtr = THolder<ICodec>;

    const ICodec* Codec(const TStringBuf& name);

    // some aux methods
    typedef TVector<TStringBuf> TCodecList; 
    TCodecList ListAllCodecs();
    TString ListAllCodecsAsString(); 

    // SEARCH-8344: Get the size of max possible decompressed block
    size_t GetMaxPossibleDecompressedLength();
    // SEARCH-8344: Globally set the size of max possible decompressed block
    void SetMaxPossibleDecompressedLength(size_t maxPossibleDecompressedLength);

}