Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*-------------------------------------------------------------------------
 * cfs_compress.h
 *    Compressed File Store (CFS) — page-level compress/decompress API.
 *
 * This header exposes the primary operations used by the storage
 * manager integration :
 *
 *   CfsCompressPage()   — compress an 8 kB PostgreSQL page into a
 *                         CfsCompressedBlockHeader + payload blob ready to
 *                         be appended to a .cfp file.
 *
 *   CfsDecompressPage() — read a CfsCompressedBlockHeader + payload and
 *                         reconstruct the original 8 kB page, verifying
 *                         all CRC-32C checksums along the way.
 *
 * Both functions are designed to work with caller-supplied scratch buffers
 * so they are suitable for use on the I/O hot path without palloc().
 *
 * Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
 *
 * IDENTIFICATION
 *    src/include/storage/cfs/cfs_compress.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef CFS_COMPRESS_H
#define CFS_COMPRESS_H
 
#include "storage/cfs/cfs.h"
#include "storage/cfs/cfs_codec.h"
 
/* ----------------------------------------------------------------
 * CfsCompressOptions — parameters passed to CfsCompressPage().
 * ---------------------------------------------------------------- */
typedef struct CfsCompressOptions
{
    CfsAlgorithm algorithm;     /* compression algorithm to use             */
    int         level;          /* algorithm-specific level (1 = fastest)   */
    bool        explicit_algorithm; /* ERROR instead of fallback if
                                     * unavailable */
    bool        allow_raw_fallback; /* allow RAW fallback when compression
                                     * fails */
 
    /*
     * Minimum gain in bytes required to accept the compressed form. If
     * (original_size - compressed_size) < min_gain_bytes, the page is stored
     * RAW (CFS_ALGORITHM_RAW, full 8 kB payload) even when the requested
     * algorithm is LZ4 or Zstd.
     */
    uint32      min_gain_bytes;
} CfsCompressOptions;
 
/* ----------------------------------------------------------------
 * Default option initialiser
 * ---------------------------------------------------------------- */
#define CFS_COMPRESS_OPTIONS_INIT \
    { \
        .algorithm      = CFS_ALGORITHM_LZ4, \
        .level          = CFS_COMPRESS_LEVEL_DEFAULT, \
        .explicit_algorithm = false, \
        .allow_raw_fallback = true, \
        .min_gain_bytes = CFS_MIN_GAIN_BYTES_DEFAULT, \
    }
 
/* ----------------------------------------------------------------
 * CfsCompressResult — output descriptor from CfsCompressPage().
 * ---------------------------------------------------------------- */
typedef struct CfsCompressResult
{
    /*
     * Points into the caller-supplied scratch buffer.  For a contiguous
     * result (!payload_external) the region result_data[0 .. total_size)
     * contains CfsCompressedBlockHeader followed by the payload and is
     * ready to be written to the .cfp file in one pwrite() call.  For an
     * external payload (RAW, payload_external) only the
     * CfsCompressedBlockHeader is stored here; the payload lives at
     * payload_data and the record must be written with the vector form
     * (see cfs_pwritev_all).
     */
    void       *result_data;
 
    /*
     * Total byte count: sizeof(CfsCompressedBlockHeader) + aligned payload.
     * This is the number of bytes to append to the .cfp file.
     */
    size_t      total_size;
 
    /*
     * Actual compressed (or raw) payload size, without alignment padding.
     * Stored in CfsMapEntry.compressed_size.
     */
    uint32      compressed_size;
 
    /*
     * Algorithm actually used (may differ from opts.algorithm when the page
     * was stored RAW due to insufficient gain).
     */
    CfsAlgorithm algorithm_used;
 
    /*
     * True when the page was stored without compression (RAW).
     */
    bool        is_raw;
 
    /*
     * True when the payload is NOT laid out behind the header in the
     * scratch buffer: payload_data then points at the caller's original
     * page (the D4 zero-copy RAW path) and padding_bytes of zero padding
     * must follow the payload on disk to reach total_size.  The on-disk
     * record is byte-identical to the copied form; only the write is
     * vectorised (header, page, zero padding) through cfs_pwritev_all().
     */
    bool        payload_external;
    void       *payload_data;   /* payload start when payload_external  */
    size_t      padding_bytes;  /* zero bytes after the payload         */
} CfsCompressResult;
 
/*
 * CfsDecompressResult — output descriptor from CfsDecompressPage().
 */
typedef struct CfsDecompressResult
{
    size_t      decompressed_size;
    CfsAlgorithm algorithm_used;
    bool        is_raw;
} CfsDecompressResult;
 
/* ----------------------------------------------------------------
 * Primary API
 * ---------------------------------------------------------------- */
 
/*
 * CfsCompressPage
 *      Compress a single PostgreSQL page.
 *
 * Parameters:
 *   page       — pointer to the uncompressed 8 kB page (read-only).
 *   page_size  — size of the page; must equal BLCKSZ (= CFS_BLCKSZ).
 *   opts       — compression options (algorithm, level, min_gain_bytes, lsn).
 *   scratch    — caller-supplied buffer of at least CfsCompressScratchSize()
 *                bytes.  On success, result->result_data points into this
 *                buffer.
 *   scratch_sz — size of the scratch buffer in bytes.
 *   result     — output descriptor; filled on success.
 *
 * Errors are reported via ereport(ERROR) only; there is no status return
 * value.
 *
 * Thread safety:
 *   The function uses the per-backend CfsCodecState cached inside
 *   CfsCodecStateCreate(); callers must not share scratch buffers or
 *   CfsCompressResult across threads/backends.
 */
extern void CfsCompressPage(const void *page,
                            size_t page_size,
                            const CfsCompressOptions *opts,
                            void *scratch,
                            size_t scratch_sz,
                            CfsCompressResult *result);
 
/*
 * CfsCompressPageNoCopy — the D4 write-path form of CfsCompressPage():
 * identical decisions and identical on-disk bytes, but a RAW result is not
 * copied into the scratch buffer.  result_data then holds only the
 * CfsCompressedBlockHeader, payload_external is true, payload_data points
 * at the caller's original page and padding_bytes zero bytes must follow
 * the payload on disk (write the record with cfs_pwritev_all()).  Only the
 * storage append path may use this form; every other consumer must stick
 * to the contiguous CfsCompressPage() contract.
 */
extern void CfsCompressPageNoCopy(const void *page,
                                  size_t page_size,
                                  const CfsCompressOptions *opts,
                                  void *scratch,
                                  size_t scratch_sz,
                                  CfsCompressResult *result);
 
/*
 * CfsDecompressPage
 *      Decompress a block read from a .cfp file back into a full 8 kB page.
 *
 * Parameters:
 *   compressed_data — pointer to CfsCompressedBlockHeader followed immediately
 *                     by the compressed payload (as stored in .cfp).
 *   data_size       — byte count of compressed_data (header + payload).
 *   out_page        — caller-supplied buffer of at least CFS_BLCKSZ bytes;
 *                     receives the reconstructed uncompressed page.
 *   out_page_size   — size of out_page; must be >= CFS_BLCKSZ.
 *   result          — optional output descriptor; may be NULL.  When non-NULL
 *                     receives decompressed_size, algorithm_used, and is_raw.
 *
 * Returns:
 *   CFS_DECOMPRESS_OK         — success; out_page contains the 8 kB page.
 *   CFS_DECOMPRESS_BAD_HDR    — header CRC32C mismatch or bad magic.
 *   CFS_DECOMPRESS_BAD_DATA   — compressed payload CRC32C mismatch.
 *   CFS_DECOMPRESS_CORRUPT    — decompression library reported corruption.
 *   CFS_DECOMPRESS_BAD_ORIG   — original-page CRC32C mismatch after decomp.
 *   CFS_DECOMPRESS_ERROR      — other internal error.
 */
typedef enum CfsDecompressStatus
{
    CFS_DECOMPRESS_OK = 0,
    CFS_DECOMPRESS_BAD_HDR = 1, /* magic / version / header CRC bad    */
    CFS_DECOMPRESS_BAD_DATA = 2,    /* payload CRC mismatch                */
    CFS_DECOMPRESS_CORRUPT = 3, /* codec reported corrupt data         */
    CFS_DECOMPRESS_BAD_ORIG = 4,    /* original-page CRC mismatch          */
    CFS_DECOMPRESS_ERROR = 5,   /* unexpected internal error           */
} CfsDecompressStatus;
 
extern CfsDecompressStatus CfsDecompressPage(const void *compressed_data,
                                             size_t data_size,
                                             void *out_page,
                                             size_t out_page_size,
                                             CfsDecompressResult *result);
 
/* ----------------------------------------------------------------
 * Utility helpers
 * ---------------------------------------------------------------- */
 
/*
 * CfsCompressScratchSize(page_size)
 *      Return the minimum scratch buffer size needed by CfsCompressPage()
 *      for the given page size.  In practice page_size == BLCKSZ.
 *
 * The scratch buffer must hold:
 *   CfsCompressedBlockHeader  +  max_compressed_payload (all algorithms)
 */
extern size_t CfsCompressScratchSize(size_t page_size);
extern CfsAlgorithm CfsAlgorithmFromName(const char *name);
extern const char *CfsAlgorithmGetName(CfsAlgorithm algorithm);
 
/*
 * CfsDecompressStatusString
 *      Human-readable status string for error reporting.
 */
extern const char *CfsDecompressStatusString(CfsDecompressStatus status);
 
/*
 * CfsVerifyBlockHeader
 *      Validate magic, version, and header CRC of a CfsCompressedBlockHeader.
 *      Returns true on success, false on any mismatch.
 *      Used by pg_cfs_check and recovery code.
 */
extern bool CfsVerifyBlockHeader(const CfsCompressedBlockHeader *hdr);
 
extern uint32 CfsCrc32cHeader(const CfsCompressedBlockHeader *hdr);
extern uint32 CfsCrc32cPayload(const void *payload, size_t payload_size);
extern void CfsFillBlockHeader(CfsCompressedBlockHeader *hdr,
                               CfsAlgorithm algorithm,
                               const void *compressed_data,
                               uint32 compressed_size,
                               const void *original_data,
                               uint32 original_size);
 
#endif                          /* CFS_COMPRESS_H */