Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*-------------------------------------------------------------------------
 * cfs_codec.h
 *    Compressed File Store (CFS) — abstract codec API.
 *
 * This header defines the codec abstraction layer used by CFS.  Each
 * compression algorithm (LZ4, Zstd, or the identity "raw" pass-through)
 * is represented by a CfsCodec structure filled with function pointers.
 *
 * Design goals
 * ------------
 *  - Zero dynamic allocation during compress/decompress — the caller
 *    supplies input and output buffers.
 *  - Algorithm-specific state (e.g. Zstd CCTX/DCTX) is managed through
 *    an opaque CfsCodecState handle so the hot path stays lock-free and
 *    per-backend.
 *  - Compile-time feature guards (#ifdef USE_LZ4 / USE_ZSTD) so the
 *    server binary can be built without a particular library.
 *
 * Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
 *
 * IDENTIFICATION
 *    src/include/storage/cfs/cfs_codec.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef CFS_CODEC_H
#define CFS_CODEC_H
 
#include "storage/cfs/cfs.h"
 
/* ----------------------------------------------------------------
 * Opaque per-algorithm state handle.
 *
 * Created once per backend session (or per worker) via CfsCodec.state_create
 * and released via CfsCodec.state_destroy.  Holding separate Zstd CCTX/DCTX
 * contexts per backend avoids locking on the hot I/O path.
 * ---------------------------------------------------------------- */
typedef struct CfsCodecState CfsCodecState; /* opaque; defined in cfs_codec.c */
 
/* ----------------------------------------------------------------
 * Error codes returned by codec functions.
 * ---------------------------------------------------------------- */
typedef enum CfsCodecResult
{
    CFS_CODEC_OK = 0,           /* success                                     */
    CFS_CODEC_ERROR = -1,       /* generic/library-level error                 */
    CFS_CODEC_OVERFLOW = -2,    /* compressed output exceeds dst_size          */
    CFS_CODEC_CORRUPT = -3,     /* decompression detected corrupt input        */
    CFS_CODEC_NOOP = -4,        /* internal sentinel: "no compression yet"     */
    CFS_CODEC_UNAVAIL = -5,     /* codec not compiled in (missing library)     */
} CfsCodecResult;
 
/* ----------------------------------------------------------------
 * CfsCodec — vtable for one compression algorithm.
 * ---------------------------------------------------------------- */
typedef struct CfsCodec
{
    /*
     * Algorithm identifier — must match one of the CfsAlgorithm enum values
     * so that CfsCodecTable[algorithm] == codec for that algorithm.
     */
    CfsAlgorithm algorithm;
 
    /* ---- lifecycle ---- */
 
    /*
     * state_create() - Allocate and initialize per-backend
     * compression/decompression context.  Returns NULL if the algorithm is
     * not available. Must be called before compress() or decompress().
     */
    CfsCodecState *(*state_create) (void);
 
    /*
     * state_destroy() - Release resources allocated by state_create(). Safe
     * to call with state == NULL.
     */
    void        (*state_destroy) (CfsCodecState *state);
 
    /* ---- compression ---- */
 
    /*
     * compress() - Compress src[0..src_size) into dst[0..dst_size).
     *
     * On success sets *out_size to the number of bytes written to dst and
     * returns CFS_CODEC_OK.
     *
     * Returns CFS_CODEC_OVERFLOW when the compressed output would exceed
     * dst_size — the caller should treat the page as RAW.
     *
     * (CFS_CODEC_NOOP is not returned by codecs; it is only an internal
     * sentinel used before compression runs.)
     */
    CfsCodecResult (*compress) (CfsCodecState *state,
                                const void *src,
                                size_t src_size,
                                void *dst,
                                size_t dst_size,
                                int level,
                                size_t *out_size);
 
    /* ---- decompression ---- */
 
    /*
     * decompress() - Decompress src[0..src_size) into dst[0..dst_size).
     *
     * On success sets *out_size to the number of bytes written and returns
     * CFS_CODEC_OK.  The caller must supply the correct expected_size (==
     * original_size from CfsCompressedBlockHeader) for algorithms that need
     * it (LZ4).
     *
     * Returns CFS_CODEC_CORRUPT on detected data corruption.
     */
    CfsCodecResult (*decompress) (CfsCodecState *state,
                                  const void *src,
                                  size_t src_size,
                                  void *dst,
                                  size_t dst_size,
                                  size_t expected_size,
                                  size_t *out_size);
 
    /* ---- capacity ---- */
 
    /*
     * compress_bound() - Return the maximum possible compressed size for an
     * input of src_size bytes.  Used by callers to allocate the compression
     * scratch buffer.
     */
    size_t      (*compress_bound) (size_t src_size);
 
    /*
     * available() - Return true if the codec library was compiled in and is
     * operational.  Returns false when the server was built without the
     * corresponding library (e.g. no -llz4).
     */
    bool        (*available) (void);
 
} CfsCodec;
 
/* ----------------------------------------------------------------
 * Global codec dispatch table
 *
 * Indexed by CfsAlgorithm value.
 * CfsCodecTable[CFS_ALGORITHM_RAW]  → raw pass-through
 * CfsCodecTable[CFS_ALGORITHM_LZ4]  → LZ4 codec
 * CfsCodecTable[CFS_ALGORITHM_ZSTD] → Zstd codec
 * ---------------------------------------------------------------- */
extern const CfsCodec *CfsCodecTable[CFS_ALGORITHM_COUNT];
 
/* ----------------------------------------------------------------
 * Convenience accessors
 * ---------------------------------------------------------------- */
 
/*
 * CfsCodecLookup(algorithm)
 *      Return the CfsCodec for the given algorithm, or NULL if algorithm
 *      is out of range.
 */
static inline const CfsCodec *
CfsCodecLookup(CfsAlgorithm algorithm)
{
    if (!CFS_ALGORITHM_VALID(algorithm))
        return NULL;
    return CfsCodecTable[(int) algorithm];
}
 
/*
 * CfsCodecAvailable(algorithm)
 *      Return true if the algorithm is supported and the backing library
 *      was compiled in.
 */
static inline bool
CfsCodecAvailable(CfsAlgorithm algorithm)
{
    const CfsCodec *codec = CfsCodecLookup(algorithm);
 
    return codec != NULL && codec->available != NULL && codec->available();
}
 
/* ----------------------------------------------------------------
 * Per-algorithm native level ceiling
 * ---------------------------------------------------------------- */
 
/*
 * CfsCodecMaxLevel(algorithm)
 *      Return the native maximum compression level of the given algorithm's
 *      codec.  The reloption "level" and the cfs_default_lz4_level /
 *      cfs_default_zstd_level GUCs share the global range
 *      (1..CFS_COMPRESS_LEVEL_MAX), but each codec caps at its own maximum.
 *      Any code that combines a GUC-sourced level with the final codec
 *      must narrow the level through this function (see
 *      cfs_smgr_write_opts).
 */
extern int  CfsCodecMaxLevel(CfsAlgorithm algorithm);
 
/* ----------------------------------------------------------------
 * Initialisation — must be called once at subsystem startup
 * ---------------------------------------------------------------- */
extern void CfsCodecInit(void);
 
/* ----------------------------------------------------------------
 * Per-backend state management helpers
 * ---------------------------------------------------------------- */
 
/*
 * CfsCodecStateCreate(algorithm)
 *      Create (or return cached) per-backend state for the given algorithm.
 *      Returns NULL if the algorithm is unavailable.
 */
extern CfsCodecState *CfsCodecStateCreate(CfsAlgorithm algorithm);
 
/*
 * CfsCodecStateDestroy(algorithm)
 *      Destroy cached per-backend state for the given algorithm.
 *      Called at session/worker exit.
 */
extern void CfsCodecStateDestroy(CfsAlgorithm algorithm);
 
/*
 * CfsCodecStateDestroyAll()
 *      Destroy all per-backend codec states.
 */
extern void CfsCodecStateDestroyAll(void);
 
#endif                          /* CFS_CODEC_H */