cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
nuke_db.c File Reference

In-memory SQLite mirroring layer that can fall back to disk under memory pressure. More...

#include <stdatomic.h>
#include <cwist/core/db/nuke_db.h>
#include <cwist/core/macros.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/sys/sys_info.h>
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
Include dependency graph for nuke_db.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define _DEFAULT_SOURCE
 
#define NSIG   32
 

Functions

int cwist_nuke_sync (void)
 Persist the current in-memory database back to disk or checkpoint WAL mode.
 
void cwist_nuke_close (void)
 Stop the sync thread, flush state, and tear down the global NUKE context.
 
int cwist_nuke_init (const char *disk_path, int sync_interval_ms)
 Initialize the global NUKE database mirror around one disk database.
 
sqlite3 * cwist_nuke_get_db (void)
 Return the SQLite handle that callers should use for the NUKE database.
 
void cwist_nuke_signal_handler (int signum)
 Placeholder signal handler (no-op).
 
unsigned char * cwist_nuke_serialize (sqlite3_int64 *out_size)
 Serialize the current NUKE database contents into a buffer.
 
int cwist_nuke_deserialize (unsigned char *data, sqlite3_int64 data_len)
 Replace the in-memory NUKE database with serialized contents.
 

Detailed Description

In-memory SQLite mirroring layer that can fall back to disk under memory pressure.

Definition in file nuke_db.c.

Macro Definition Documentation

◆ _DEFAULT_SOURCE

#define _DEFAULT_SOURCE

Definition at line 3 of file nuke_db.c.

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file nuke_db.c.

◆ NSIG

#define NSIG   32

Definition at line 43 of file nuke_db.c.

Function Documentation

◆ cwist_nuke_close()

void cwist_nuke_close ( void  )

Stop the sync thread, flush state, and tear down the global NUKE context.

Close databases safely. Performs a final synchronization before closing handles.

Definition at line 293 of file nuke_db.c.

References cwist_nuke_sync().

Referenced by cwist_app_destroy(), cwist_app_use_nuke_db(), and cwist_nuke_init().

◆ cwist_nuke_deserialize()

int cwist_nuke_deserialize ( unsigned char *  data,
sqlite3_int64  data_len 
)

Replace the in-memory NUKE database with serialized contents.

Load a raw SQLite byte buffer into the in-memory database.

Parameters
dataSerialized database buffer; ownership transfers to SQLite, which frees it on close (SQLITE_DESERIALIZE_FREEONCLOSE).
data_lenLength of data in bytes.
Returns
0 on success, or -1 on invalid arguments or when running in disk mode.

Definition at line 520 of file nuke_db.c.

References cwist_nuke_db_t::is_disk_mode, and cwist_nuke_db_t::mem_db.

◆ cwist_nuke_get_db()

sqlite3 * cwist_nuke_get_db ( void  )

Return the SQLite handle that callers should use for the NUKE database.

Get the raw SQLite3 handle for the In-Memory DB.

Returns
The in-memory handle in normal mode, or the disk handle after a low-memory fallback to disk mode. May be NULL before init or after close.

Definition at line 478 of file nuke_db.c.

References cwist_nuke_db_t::disk_db, cwist_nuke_db_t::is_disk_mode, and cwist_nuke_db_t::mem_db.

Referenced by cwist_app_get_db(), and cwist_app_use_nuke_db().

◆ cwist_nuke_init()

int cwist_nuke_init ( const char *  disk_path,
int  sync_interval_ms 
)

Initialize the global NUKE database mirror around one disk database.

Initialize Nuke DB.

Parameters
disk_pathPath to the on-disk SQLite database.
sync_interval_msBackground sync interval in milliseconds.
Returns
NUKE status code describing success, low-memory fallback, or failure.

Definition at line 377 of file nuke_db.c.

References cwist_nuke_db_t::auto_sync, cwist_get_available_ram(), cwist_nuke_close(), CWIST_NUKE_ERR_GENERIC, CWIST_NUKE_ERR_LOW_MEMORY, CWIST_NUKE_OK, cwist_strdup(), cwist_nuke_db_t::disk_db, cwist_nuke_db_t::disk_path, cwist_nuke_db_t::is_disk_mode, cwist_nuke_db_t::load_successful, cwist_nuke_db_t::mem_db, and cwist_nuke_db_t::sync_interval_ms.

Referenced by cwist_app_use_nuke_db().

◆ cwist_nuke_serialize()

unsigned char * cwist_nuke_serialize ( sqlite3_int64 *  out_size)

Serialize the current NUKE database contents into a buffer.

Serialize the in-memory database to a raw byte buffer.

Parameters
out_sizeReceives the serialized byte count.
Returns
Heap buffer produced by sqlite3_serialize (caller frees with sqlite3_free), or NULL when out_size is NULL or no database is open. Thread-safe via g_nuke_lock.

Definition at line 497 of file nuke_db.c.

References cwist_nuke_db_t::disk_db, cwist_nuke_db_t::is_disk_mode, and cwist_nuke_db_t::mem_db.

◆ cwist_nuke_signal_handler()

void cwist_nuke_signal_handler ( int  signum)

Placeholder signal handler (no-op).

Internal signal handler. Typically set up by cwist_nuke_init.

Parameters
signumSignal number; unused.

Definition at line 487 of file nuke_db.c.

References CWIST_UNUSED.

◆ cwist_nuke_sync()

int cwist_nuke_sync ( void  )

Persist the current in-memory database back to disk or checkpoint WAL mode.

Force synchronization from Memory to Disk.

Returns
0 on success, or -1 when syncing is impossible or fails.

Definition at line 228 of file nuke_db.c.

References cwist_nuke_db_t::disk_db, cwist_nuke_db_t::is_disk_mode, cwist_nuke_db_t::load_successful, and cwist_nuke_db_t::mem_db.

Referenced by cwist_nuke_close().