/*-------------------------------------------------------------------------
|
* 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 */
|