Skip to content

Commit

Permalink
Merge pull request #1876 from tursodatabase/wal-checkpoint-seq-api
Browse files Browse the repository at this point in the history
Add libsql_wal_checkpoint_seq_count() API
  • Loading branch information
penberg authored Dec 17, 2024
2 parents bda357e + bb7f011 commit b8f5e89
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 12 deletions.
57 changes: 57 additions & 0 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10953,6 +10953,15 @@ SQLITE_API void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlit
*/
SQLITE_API int libsql_wal_disable_checkpoint(sqlite3 *db);

/*
** CAPI3REF: Get the checkpoint sequence counter of the WAL file
** METHOD: sqlite3
**
** ^The [libsql_wal_checkpoint_seq_count(D,P)] interface returns the checkpoint sequence counter
** of the WAL file for [database connection] D into *P.
*/
SQLITE_API int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt);

/*
** CAPI3REF: Get the number of frames in the WAL file
** METHOD: sqlite3
Expand Down Expand Up @@ -14036,6 +14045,9 @@ typedef struct libsql_wal_methods {
** response to a ROLLBACK TO command. */
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);

/* Return the current checkpoint generation in the WAL file. */
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);

/* Return the number of frames in the WAL */
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);

Expand Down Expand Up @@ -57354,6 +57366,9 @@ typedef struct libsql_wal_methods {
** response to a ROLLBACK TO command. */
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);

/* Return the current checkpoint generation in the WAL file. */
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);

/* Return the number of frames in the WAL */
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);

Expand Down Expand Up @@ -65282,6 +65297,18 @@ SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
return rc;
}

/**
** Return the current checkpoint generation in the WAL file.
**/
SQLITE_PRIVATE int sqlite3PagerWalCheckpointSeqCount(Pager *pPager, unsigned int *pnCkpt){
if( pagerUseWal(pPager) ){
return pPager->wal->methods.xCheckpointSeqCount(pPager->wal->pData, pnCkpt);
} else {
*pnCkpt = 0;
return SQLITE_OK;
}
}

/**
** Return the number of frames in the WAL file.
**
Expand Down Expand Up @@ -69534,6 +69561,11 @@ static int walFrames(
return rc;
}

SQLITE_PRIVATE int sqlite3WalCheckpointSeqCount(Wal *pWal, unsigned int *pnCkpt){
*pnCkpt = pWal->nCkpt;
return SQLITE_OK;
}

SQLITE_PRIVATE int sqlite3WalFrameCount(Wal *pWal, int locked, unsigned int *pnFrames){
int rc = SQLITE_OK;
if( locked==0 ) {
Expand Down Expand Up @@ -70053,6 +70085,7 @@ static int sqlite3WalOpen(
out->methods.xUndo = (int (*)(wal_impl *, int (*)(void *, unsigned int), void *))sqlite3WalUndo;
out->methods.xSavepoint = (void (*)(wal_impl *, unsigned int *))sqlite3WalSavepoint;
out->methods.xSavepointUndo = (int (*)(wal_impl *, unsigned int *))sqlite3WalSavepointUndo;
out->methods.xCheckpointSeqCount = (int (*)(wal_impl *, unsigned int *))sqlite3WalCheckpointSeqCount;
out->methods.xFrameCount = (int (*)(wal_impl *, int, unsigned int *))sqlite3WalFrameCount;
out->methods.xFrames = (int (*)(wal_impl *, int, libsql_pghdr *, unsigned int, int, int, int *))sqlite3WalFrames;
out->methods.xCheckpoint = (int (*)(wal_impl *, sqlite3 *, int, int (*)(void *), void *, int, int, unsigned char *, int *, int *, int (*)(void*, int, const unsigned char*, int, int, int), void*))sqlite3WalCheckpoint;
Expand Down Expand Up @@ -183207,6 +183240,30 @@ int libsql_wal_disable_checkpoint(sqlite3 *db) {
return SQLITE_OK;
}

/*
** Return the checkpoint sequence counter of the given database.
*/
int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt) {
int rc = SQLITE_OK;
Pager *pPager;

#ifdef SQLITE_OMIT_WAL
*pnFrame = 0;
return SQLITE_OK;
#else
#ifdef SQLITE_ENABLE_API_ARMOR
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
#endif

sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalCheckpointSeqCount(pPager, pnCkpt);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
#endif
}

/*
** Return the number of frames in the WAL of the given database.
*/
Expand Down
28 changes: 16 additions & 12 deletions libsql-ffi/bundled/bindings/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ extern "C" {
extern "C" {
pub fn sqlite3_vmprintf(
arg1: *const ::std::os::raw::c_char,
arg2: *mut __va_list_tag,
arg2: va_list,
) -> *mut ::std::os::raw::c_char;
}
extern "C" {
Expand All @@ -956,7 +956,7 @@ extern "C" {
arg1: ::std::os::raw::c_int,
arg2: *mut ::std::os::raw::c_char,
arg3: *const ::std::os::raw::c_char,
arg4: *mut __va_list_tag,
arg4: va_list,
) -> *mut ::std::os::raw::c_char;
}
extern "C" {
Expand Down Expand Up @@ -2503,7 +2503,7 @@ extern "C" {
pub fn sqlite3_str_vappendf(
arg1: *mut sqlite3_str,
zFormat: *const ::std::os::raw::c_char,
arg2: *mut __va_list_tag,
arg2: va_list,
);
}
extern "C" {
Expand Down Expand Up @@ -2866,6 +2866,12 @@ extern "C" {
extern "C" {
pub fn libsql_wal_disable_checkpoint(db: *mut sqlite3) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libsql_wal_checkpoint_seq_count(
db: *mut sqlite3,
pnCkpt: *mut ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libsql_wal_frame_count(
arg1: *mut sqlite3,
Expand Down Expand Up @@ -3355,6 +3361,12 @@ pub struct libsql_wal_methods {
aWalData: *mut ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int,
>,
pub xCheckpointSeqCount: ::std::option::Option<
unsafe extern "C" fn(
pWal: *mut wal_impl,
pnCkpt: *mut ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int,
>,
pub xFrameCount: ::std::option::Option<
unsafe extern "C" fn(
pWal: *mut wal_impl,
Expand Down Expand Up @@ -3570,12 +3582,4 @@ extern "C" {
extern "C" {
pub static sqlite3_wal_manager: libsql_wal_manager;
}
pub type __builtin_va_list = [__va_list_tag; 1usize];
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct __va_list_tag {
pub gp_offset: ::std::os::raw::c_uint,
pub fp_offset: ::std::os::raw::c_uint,
pub overflow_arg_area: *mut ::std::os::raw::c_void,
pub reg_save_area: *mut ::std::os::raw::c_void,
}
pub type __builtin_va_list = *mut ::std::os::raw::c_char;
12 changes: 12 additions & 0 deletions libsql-ffi/bundled/bindings/session_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,12 @@ extern "C" {
extern "C" {
pub fn libsql_wal_disable_checkpoint(db: *mut sqlite3) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libsql_wal_checkpoint_seq_count(
db: *mut sqlite3,
pnCkpt: *mut ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libsql_wal_frame_count(
arg1: *mut sqlite3,
Expand Down Expand Up @@ -3867,6 +3873,12 @@ pub struct libsql_wal_methods {
aWalData: *mut ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int,
>,
pub xCheckpointSeqCount: ::std::option::Option<
unsafe extern "C" fn(
pWal: *mut wal_impl,
pnCkpt: *mut ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int,
>,
pub xFrameCount: ::std::option::Option<
unsafe extern "C" fn(
pWal: *mut wal_impl,
Expand Down
57 changes: 57 additions & 0 deletions libsql-ffi/bundled/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10953,6 +10953,15 @@ SQLITE_API void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlit
*/
SQLITE_API int libsql_wal_disable_checkpoint(sqlite3 *db);

/*
** CAPI3REF: Get the checkpoint sequence counter of the WAL file
** METHOD: sqlite3
**
** ^The [libsql_wal_checkpoint_seq_count(D,P)] interface returns the checkpoint sequence counter
** of the WAL file for [database connection] D into *P.
*/
SQLITE_API int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt);

/*
** CAPI3REF: Get the number of frames in the WAL file
** METHOD: sqlite3
Expand Down Expand Up @@ -14036,6 +14045,9 @@ typedef struct libsql_wal_methods {
** response to a ROLLBACK TO command. */
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);

/* Return the current checkpoint generation in the WAL file. */
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);

/* Return the number of frames in the WAL */
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);

Expand Down Expand Up @@ -57354,6 +57366,9 @@ typedef struct libsql_wal_methods {
** response to a ROLLBACK TO command. */
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);

/* Return the current checkpoint generation in the WAL file. */
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);

/* Return the number of frames in the WAL */
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);

Expand Down Expand Up @@ -65282,6 +65297,18 @@ SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
return rc;
}

/**
** Return the current checkpoint generation in the WAL file.
**/
SQLITE_PRIVATE int sqlite3PagerWalCheckpointSeqCount(Pager *pPager, unsigned int *pnCkpt){
if( pagerUseWal(pPager) ){
return pPager->wal->methods.xCheckpointSeqCount(pPager->wal->pData, pnCkpt);
} else {
*pnCkpt = 0;
return SQLITE_OK;
}
}

/**
** Return the number of frames in the WAL file.
**
Expand Down Expand Up @@ -69534,6 +69561,11 @@ static int walFrames(
return rc;
}

SQLITE_PRIVATE int sqlite3WalCheckpointSeqCount(Wal *pWal, unsigned int *pnCkpt){
*pnCkpt = pWal->nCkpt;
return SQLITE_OK;
}

SQLITE_PRIVATE int sqlite3WalFrameCount(Wal *pWal, int locked, unsigned int *pnFrames){
int rc = SQLITE_OK;
if( locked==0 ) {
Expand Down Expand Up @@ -70053,6 +70085,7 @@ static int sqlite3WalOpen(
out->methods.xUndo = (int (*)(wal_impl *, int (*)(void *, unsigned int), void *))sqlite3WalUndo;
out->methods.xSavepoint = (void (*)(wal_impl *, unsigned int *))sqlite3WalSavepoint;
out->methods.xSavepointUndo = (int (*)(wal_impl *, unsigned int *))sqlite3WalSavepointUndo;
out->methods.xCheckpointSeqCount = (int (*)(wal_impl *, unsigned int *))sqlite3WalCheckpointSeqCount;
out->methods.xFrameCount = (int (*)(wal_impl *, int, unsigned int *))sqlite3WalFrameCount;
out->methods.xFrames = (int (*)(wal_impl *, int, libsql_pghdr *, unsigned int, int, int, int *))sqlite3WalFrames;
out->methods.xCheckpoint = (int (*)(wal_impl *, sqlite3 *, int, int (*)(void *), void *, int, int, unsigned char *, int *, int *, int (*)(void*, int, const unsigned char*, int, int, int), void*))sqlite3WalCheckpoint;
Expand Down Expand Up @@ -183207,6 +183240,30 @@ int libsql_wal_disable_checkpoint(sqlite3 *db) {
return SQLITE_OK;
}

/*
** Return the checkpoint sequence counter of the given database.
*/
int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt) {
int rc = SQLITE_OK;
Pager *pPager;

#ifdef SQLITE_OMIT_WAL
*pnFrame = 0;
return SQLITE_OK;
#else
#ifdef SQLITE_ENABLE_API_ARMOR
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
#endif

sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalCheckpointSeqCount(pPager, pnCkpt);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
#endif
}

/*
** Return the number of frames in the WAL of the given database.
*/
Expand Down
12 changes: 12 additions & 0 deletions libsql-ffi/bundled/src/sqlite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -10564,6 +10564,15 @@ SQLITE_API void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlit
*/
SQLITE_API int libsql_wal_disable_checkpoint(sqlite3 *db);

/*
** CAPI3REF: Get the checkpoint sequence counter of the WAL file
** METHOD: sqlite3
**
** ^The [libsql_wal_checkpoint_seq_count(D,P)] interface returns the checkpoint sequence counter
** of the WAL file for [database connection] D into *P.
*/
SQLITE_API int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt);

/*
** CAPI3REF: Get the number of frames in the WAL file
** METHOD: sqlite3
Expand Down Expand Up @@ -13647,6 +13656,9 @@ typedef struct libsql_wal_methods {
** response to a ROLLBACK TO command. */
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);

/* Return the current checkpoint generation in the WAL file. */
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);

/* Return the number of frames in the WAL */
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ impl Wal for InjectorWal {
self.inner.savepoint_undo(rollback_data)
}

fn checkpoint_seq_count(&self) -> Result<u32> {
self.inner.checkpoint_seq_count()
}

fn frame_count(&self, locked: i32) -> Result<u32> {
self.inner.frame_count(locked)
}
Expand Down
1 change: 1 addition & 0 deletions libsql-sqlite3/doc/libsql_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ The libSQL has the following WAL API functions, which are useful for
syncing the WAL between databases:
* `libsql_wal_disable_checkpoint` -- Disable checkpointing, including on database close.
* `libsql_wal_checkpoint_seq_count` -- Return the checkpoint sequence number of the WAL.
* `libsql_wal_frame_count` -- Get the number of frames in the WAL.
* `libsql_wal_get_frame` -- Get a frame from the WAL.
* `libsql_wal_insert_begin` -- Begin WAL insertion.
Expand Down
24 changes: 24 additions & 0 deletions libsql-sqlite3/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,30 @@ int libsql_wal_disable_checkpoint(sqlite3 *db) {
return SQLITE_OK;
}

/*
** Return the checkpoint sequence counter of the given database.
*/
int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt) {
int rc = SQLITE_OK;
Pager *pPager;

#ifdef SQLITE_OMIT_WAL
*pnFrame = 0;
return SQLITE_OK;
#else
#ifdef SQLITE_ENABLE_API_ARMOR
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
#endif

sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalCheckpointSeqCount(pPager, pnCkpt);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
#endif
}

/*
** Return the number of frames in the WAL of the given database.
*/
Expand Down
12 changes: 12 additions & 0 deletions libsql-sqlite3/src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -7771,6 +7771,18 @@ int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
return rc;
}

/**
** Return the current checkpoint generation in the WAL file.
**/
int sqlite3PagerWalCheckpointSeqCount(Pager *pPager, unsigned int *pnCkpt){
if( pagerUseWal(pPager) ){
return pPager->wal->methods.xCheckpointSeqCount(pPager->wal->pData, pnCkpt);
} else {
*pnCkpt = 0;
return SQLITE_OK;
}
}

/**
** Return the number of frames in the WAL file.
**
Expand Down
Loading

0 comments on commit b8f5e89

Please sign in to comment.