esdb_archive_nif (reckon_db v1.6.0)

View Source

Optimized archive compression for reckon-db.

This module provides high-performance compression implementations:

  • LZ4: Fastest compression/decompression (real-time use)
  • Zstd: Best compression ratio (cold storage)
  • Zlib: Standard compatibility (Erlang term_to_binary)

The mode is automatically detected at startup based on whether the NIF library is available. Community edition users (hex.pm) will always use the Erlang fallbacks, which provide identical functionality.

Algorithm Selection

| Algorithm | Speed | Ratio | Use Case | |-----------|-------|-------|----------| | LZ4 | Fastest | ~2.5:1 | Hot archives, real-time | | Zstd | Fast | ~4:1 | Cold archives, storage | | Zlib | Moderate | ~3:1 | Compatibility |

Usage

  %% Compress with LZ4 (fastest)
  {ok, Compressed} = esdb_archive_nif:compress(Data, lz4).
 
  %% Compress with Zstd level 3 (good balance)
  {ok, Compressed} = esdb_archive_nif:compress(Data, zstd, 3).
 
  %% Decompress
  {ok, Original} = esdb_archive_nif:decompress(Compressed, lz4).
 
  %% Check which mode is active
  nif = esdb_archive_nif:implementation().  %% Enterprise
  erlang = esdb_archive_nif:implementation(). %% Community

Summary

Functions

Compress data with specified algorithm using default level.

Compress data with specified algorithm and compression level.

Compress data using LZ4 (fastest).

Compress data using Zlib with default level.

Compress data using Zlib with specified level (0-9).

Compress data using Zstd with default level.

Compress data using Zstd with specified level (1-22).

Get compression statistics for data with algorithm.

Get compression statistics with specified level.

Decompress data with specified algorithm.

Decompress LZ4-compressed data.

Decompress Zlib-compressed data.

Decompress Zstd-compressed data.

Get the current implementation mode.

Check if the NIF is loaded (Enterprise mode).

Functions

compress(Data, Algorithm)

-spec compress(Data :: binary(), Algorithm :: lz4 | zstd | zlib) -> {ok, binary()} | {error, term()}.

Compress data with specified algorithm using default level.

compress(Data, Algorithm, Level)

-spec compress(Data :: binary(), Algorithm :: lz4 | zstd | zlib, Level :: integer()) ->
                  {ok, binary()} | {error, term()}.

Compress data with specified algorithm and compression level.

compress_lz4(Data)

-spec compress_lz4(Data :: binary()) -> {ok, binary()} | {error, term()}.

Compress data using LZ4 (fastest).

compress_zlib(Data)

-spec compress_zlib(Data :: binary()) -> {ok, binary()} | {error, term()}.

Compress data using Zlib with default level.

compress_zlib(Data, Level)

-spec compress_zlib(Data :: binary(), Level :: integer()) -> {ok, binary()} | {error, term()}.

Compress data using Zlib with specified level (0-9).

compress_zstd(Data)

-spec compress_zstd(Data :: binary()) -> {ok, binary()} | {error, term()}.

Compress data using Zstd with default level.

compress_zstd(Data, Level)

-spec compress_zstd(Data :: binary(), Level :: integer()) -> {ok, binary()} | {error, term()}.

Compress data using Zstd with specified level (1-22).

compression_stats(Data, Algorithm)

-spec compression_stats(Data :: binary(), Algorithm :: lz4 | zstd | zlib) ->
                           {ok,
                            #{original_size := integer(),
                              compressed_size := integer(),
                              ratio := float()}} |
                           {error, term()}.

Get compression statistics for data with algorithm.

compression_stats(Data, Algorithm, Level)

-spec compression_stats(Data :: binary(), Algorithm :: lz4 | zstd | zlib, Level :: integer()) ->
                           {ok,
                            #{original_size := integer(),
                              compressed_size := integer(),
                              ratio := float()}} |
                           {error, term()}.

Get compression statistics with specified level.

decompress(Data, Algorithm)

-spec decompress(Data :: binary(), Algorithm :: lz4 | zstd | zlib) -> {ok, binary()} | {error, term()}.

Decompress data with specified algorithm.

decompress_lz4(Data)

-spec decompress_lz4(Data :: binary()) -> {ok, binary()} | {error, term()}.

Decompress LZ4-compressed data.

decompress_zlib(Data)

-spec decompress_zlib(Data :: binary()) -> {ok, binary()} | {error, term()}.

Decompress Zlib-compressed data.

decompress_zstd(Data)

-spec decompress_zstd(Data :: binary()) -> {ok, binary()} | {error, term()}.

Decompress Zstd-compressed data.

implementation()

-spec implementation() -> nif | erlang.

Get the current implementation mode.

is_nif_loaded()

-spec is_nif_loaded() -> boolean().

Check if the NIF is loaded (Enterprise mode).