Skip to content

Commit b8f5e89

Browse files
authored
Merge pull request #1876 from tursodatabase/wal-checkpoint-seq-api
Add libsql_wal_checkpoint_seq_count() API
2 parents bda357e + bb7f011 commit b8f5e89

File tree

21 files changed

+308
-12
lines changed

21 files changed

+308
-12
lines changed

libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10953,6 +10953,15 @@ SQLITE_API void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlit
1095310953
*/
1095410954
SQLITE_API int libsql_wal_disable_checkpoint(sqlite3 *db);
1095510955

10956+
/*
10957+
** CAPI3REF: Get the checkpoint sequence counter of the WAL file
10958+
** METHOD: sqlite3
10959+
**
10960+
** ^The [libsql_wal_checkpoint_seq_count(D,P)] interface returns the checkpoint sequence counter
10961+
** of the WAL file for [database connection] D into *P.
10962+
*/
10963+
SQLITE_API int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt);
10964+
1095610965
/*
1095710966
** CAPI3REF: Get the number of frames in the WAL file
1095810967
** METHOD: sqlite3
@@ -14036,6 +14045,9 @@ typedef struct libsql_wal_methods {
1403614045
** response to a ROLLBACK TO command. */
1403714046
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);
1403814047

14048+
/* Return the current checkpoint generation in the WAL file. */
14049+
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);
14050+
1403914051
/* Return the number of frames in the WAL */
1404014052
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);
1404114053

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

57369+
/* Return the current checkpoint generation in the WAL file. */
57370+
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);
57371+
5735757372
/* Return the number of frames in the WAL */
5735857373
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);
5735957374

@@ -65282,6 +65297,18 @@ SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
6528265297
return rc;
6528365298
}
6528465299

65300+
/**
65301+
** Return the current checkpoint generation in the WAL file.
65302+
**/
65303+
SQLITE_PRIVATE int sqlite3PagerWalCheckpointSeqCount(Pager *pPager, unsigned int *pnCkpt){
65304+
if( pagerUseWal(pPager) ){
65305+
return pPager->wal->methods.xCheckpointSeqCount(pPager->wal->pData, pnCkpt);
65306+
} else {
65307+
*pnCkpt = 0;
65308+
return SQLITE_OK;
65309+
}
65310+
}
65311+
6528565312
/**
6528665313
** Return the number of frames in the WAL file.
6528765314
**
@@ -69534,6 +69561,11 @@ static int walFrames(
6953469561
return rc;
6953569562
}
6953669563

69564+
SQLITE_PRIVATE int sqlite3WalCheckpointSeqCount(Wal *pWal, unsigned int *pnCkpt){
69565+
*pnCkpt = pWal->nCkpt;
69566+
return SQLITE_OK;
69567+
}
69568+
6953769569
SQLITE_PRIVATE int sqlite3WalFrameCount(Wal *pWal, int locked, unsigned int *pnFrames){
6953869570
int rc = SQLITE_OK;
6953969571
if( locked==0 ) {
@@ -70053,6 +70085,7 @@ static int sqlite3WalOpen(
7005370085
out->methods.xUndo = (int (*)(wal_impl *, int (*)(void *, unsigned int), void *))sqlite3WalUndo;
7005470086
out->methods.xSavepoint = (void (*)(wal_impl *, unsigned int *))sqlite3WalSavepoint;
7005570087
out->methods.xSavepointUndo = (int (*)(wal_impl *, unsigned int *))sqlite3WalSavepointUndo;
70088+
out->methods.xCheckpointSeqCount = (int (*)(wal_impl *, unsigned int *))sqlite3WalCheckpointSeqCount;
7005670089
out->methods.xFrameCount = (int (*)(wal_impl *, int, unsigned int *))sqlite3WalFrameCount;
7005770090
out->methods.xFrames = (int (*)(wal_impl *, int, libsql_pghdr *, unsigned int, int, int, int *))sqlite3WalFrames;
7005870091
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;
@@ -183207,6 +183240,30 @@ int libsql_wal_disable_checkpoint(sqlite3 *db) {
183207183240
return SQLITE_OK;
183208183241
}
183209183242

183243+
/*
183244+
** Return the checkpoint sequence counter of the given database.
183245+
*/
183246+
int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt) {
183247+
int rc = SQLITE_OK;
183248+
Pager *pPager;
183249+
183250+
#ifdef SQLITE_OMIT_WAL
183251+
*pnFrame = 0;
183252+
return SQLITE_OK;
183253+
#else
183254+
#ifdef SQLITE_ENABLE_API_ARMOR
183255+
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
183256+
#endif
183257+
183258+
sqlite3_mutex_enter(db->mutex);
183259+
pPager = sqlite3BtreePager(db->aDb[0].pBt);
183260+
rc = sqlite3PagerWalCheckpointSeqCount(pPager, pnCkpt);
183261+
sqlite3Error(db, rc);
183262+
sqlite3_mutex_leave(db->mutex);
183263+
return rc;
183264+
#endif
183265+
}
183266+
183210183267
/*
183211183268
** Return the number of frames in the WAL of the given database.
183212183269
*/

libsql-ffi/bundled/bindings/bindgen.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ extern "C" {
940940
extern "C" {
941941
pub fn sqlite3_vmprintf(
942942
arg1: *const ::std::os::raw::c_char,
943-
arg2: *mut __va_list_tag,
943+
arg2: va_list,
944944
) -> *mut ::std::os::raw::c_char;
945945
}
946946
extern "C" {
@@ -956,7 +956,7 @@ extern "C" {
956956
arg1: ::std::os::raw::c_int,
957957
arg2: *mut ::std::os::raw::c_char,
958958
arg3: *const ::std::os::raw::c_char,
959-
arg4: *mut __va_list_tag,
959+
arg4: va_list,
960960
) -> *mut ::std::os::raw::c_char;
961961
}
962962
extern "C" {
@@ -2503,7 +2503,7 @@ extern "C" {
25032503
pub fn sqlite3_str_vappendf(
25042504
arg1: *mut sqlite3_str,
25052505
zFormat: *const ::std::os::raw::c_char,
2506-
arg2: *mut __va_list_tag,
2506+
arg2: va_list,
25072507
);
25082508
}
25092509
extern "C" {
@@ -2866,6 +2866,12 @@ extern "C" {
28662866
extern "C" {
28672867
pub fn libsql_wal_disable_checkpoint(db: *mut sqlite3) -> ::std::os::raw::c_int;
28682868
}
2869+
extern "C" {
2870+
pub fn libsql_wal_checkpoint_seq_count(
2871+
db: *mut sqlite3,
2872+
pnCkpt: *mut ::std::os::raw::c_uint,
2873+
) -> ::std::os::raw::c_int;
2874+
}
28692875
extern "C" {
28702876
pub fn libsql_wal_frame_count(
28712877
arg1: *mut sqlite3,
@@ -3355,6 +3361,12 @@ pub struct libsql_wal_methods {
33553361
aWalData: *mut ::std::os::raw::c_uint,
33563362
) -> ::std::os::raw::c_int,
33573363
>,
3364+
pub xCheckpointSeqCount: ::std::option::Option<
3365+
unsafe extern "C" fn(
3366+
pWal: *mut wal_impl,
3367+
pnCkpt: *mut ::std::os::raw::c_uint,
3368+
) -> ::std::os::raw::c_int,
3369+
>,
33583370
pub xFrameCount: ::std::option::Option<
33593371
unsafe extern "C" fn(
33603372
pWal: *mut wal_impl,
@@ -3570,12 +3582,4 @@ extern "C" {
35703582
extern "C" {
35713583
pub static sqlite3_wal_manager: libsql_wal_manager;
35723584
}
3573-
pub type __builtin_va_list = [__va_list_tag; 1usize];
3574-
#[repr(C)]
3575-
#[derive(Debug, Copy, Clone)]
3576-
pub struct __va_list_tag {
3577-
pub gp_offset: ::std::os::raw::c_uint,
3578-
pub fp_offset: ::std::os::raw::c_uint,
3579-
pub overflow_arg_area: *mut ::std::os::raw::c_void,
3580-
pub reg_save_area: *mut ::std::os::raw::c_void,
3581-
}
3585+
pub type __builtin_va_list = *mut ::std::os::raw::c_char;

libsql-ffi/bundled/bindings/session_bindgen.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,12 @@ extern "C" {
28832883
extern "C" {
28842884
pub fn libsql_wal_disable_checkpoint(db: *mut sqlite3) -> ::std::os::raw::c_int;
28852885
}
2886+
extern "C" {
2887+
pub fn libsql_wal_checkpoint_seq_count(
2888+
db: *mut sqlite3,
2889+
pnCkpt: *mut ::std::os::raw::c_uint,
2890+
) -> ::std::os::raw::c_int;
2891+
}
28862892
extern "C" {
28872893
pub fn libsql_wal_frame_count(
28882894
arg1: *mut sqlite3,
@@ -3867,6 +3873,12 @@ pub struct libsql_wal_methods {
38673873
aWalData: *mut ::std::os::raw::c_uint,
38683874
) -> ::std::os::raw::c_int,
38693875
>,
3876+
pub xCheckpointSeqCount: ::std::option::Option<
3877+
unsafe extern "C" fn(
3878+
pWal: *mut wal_impl,
3879+
pnCkpt: *mut ::std::os::raw::c_uint,
3880+
) -> ::std::os::raw::c_int,
3881+
>,
38703882
pub xFrameCount: ::std::option::Option<
38713883
unsafe extern "C" fn(
38723884
pWal: *mut wal_impl,

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10953,6 +10953,15 @@ SQLITE_API void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlit
1095310953
*/
1095410954
SQLITE_API int libsql_wal_disable_checkpoint(sqlite3 *db);
1095510955

10956+
/*
10957+
** CAPI3REF: Get the checkpoint sequence counter of the WAL file
10958+
** METHOD: sqlite3
10959+
**
10960+
** ^The [libsql_wal_checkpoint_seq_count(D,P)] interface returns the checkpoint sequence counter
10961+
** of the WAL file for [database connection] D into *P.
10962+
*/
10963+
SQLITE_API int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt);
10964+
1095610965
/*
1095710966
** CAPI3REF: Get the number of frames in the WAL file
1095810967
** METHOD: sqlite3
@@ -14036,6 +14045,9 @@ typedef struct libsql_wal_methods {
1403614045
** response to a ROLLBACK TO command. */
1403714046
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);
1403814047

14048+
/* Return the current checkpoint generation in the WAL file. */
14049+
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);
14050+
1403914051
/* Return the number of frames in the WAL */
1404014052
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);
1404114053

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

57369+
/* Return the current checkpoint generation in the WAL file. */
57370+
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);
57371+
5735757372
/* Return the number of frames in the WAL */
5735857373
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);
5735957374

@@ -65282,6 +65297,18 @@ SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
6528265297
return rc;
6528365298
}
6528465299

65300+
/**
65301+
** Return the current checkpoint generation in the WAL file.
65302+
**/
65303+
SQLITE_PRIVATE int sqlite3PagerWalCheckpointSeqCount(Pager *pPager, unsigned int *pnCkpt){
65304+
if( pagerUseWal(pPager) ){
65305+
return pPager->wal->methods.xCheckpointSeqCount(pPager->wal->pData, pnCkpt);
65306+
} else {
65307+
*pnCkpt = 0;
65308+
return SQLITE_OK;
65309+
}
65310+
}
65311+
6528565312
/**
6528665313
** Return the number of frames in the WAL file.
6528765314
**
@@ -69534,6 +69561,11 @@ static int walFrames(
6953469561
return rc;
6953569562
}
6953669563

69564+
SQLITE_PRIVATE int sqlite3WalCheckpointSeqCount(Wal *pWal, unsigned int *pnCkpt){
69565+
*pnCkpt = pWal->nCkpt;
69566+
return SQLITE_OK;
69567+
}
69568+
6953769569
SQLITE_PRIVATE int sqlite3WalFrameCount(Wal *pWal, int locked, unsigned int *pnFrames){
6953869570
int rc = SQLITE_OK;
6953969571
if( locked==0 ) {
@@ -70053,6 +70085,7 @@ static int sqlite3WalOpen(
7005370085
out->methods.xUndo = (int (*)(wal_impl *, int (*)(void *, unsigned int), void *))sqlite3WalUndo;
7005470086
out->methods.xSavepoint = (void (*)(wal_impl *, unsigned int *))sqlite3WalSavepoint;
7005570087
out->methods.xSavepointUndo = (int (*)(wal_impl *, unsigned int *))sqlite3WalSavepointUndo;
70088+
out->methods.xCheckpointSeqCount = (int (*)(wal_impl *, unsigned int *))sqlite3WalCheckpointSeqCount;
7005670089
out->methods.xFrameCount = (int (*)(wal_impl *, int, unsigned int *))sqlite3WalFrameCount;
7005770090
out->methods.xFrames = (int (*)(wal_impl *, int, libsql_pghdr *, unsigned int, int, int, int *))sqlite3WalFrames;
7005870091
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;
@@ -183207,6 +183240,30 @@ int libsql_wal_disable_checkpoint(sqlite3 *db) {
183207183240
return SQLITE_OK;
183208183241
}
183209183242

183243+
/*
183244+
** Return the checkpoint sequence counter of the given database.
183245+
*/
183246+
int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt) {
183247+
int rc = SQLITE_OK;
183248+
Pager *pPager;
183249+
183250+
#ifdef SQLITE_OMIT_WAL
183251+
*pnFrame = 0;
183252+
return SQLITE_OK;
183253+
#else
183254+
#ifdef SQLITE_ENABLE_API_ARMOR
183255+
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
183256+
#endif
183257+
183258+
sqlite3_mutex_enter(db->mutex);
183259+
pPager = sqlite3BtreePager(db->aDb[0].pBt);
183260+
rc = sqlite3PagerWalCheckpointSeqCount(pPager, pnCkpt);
183261+
sqlite3Error(db, rc);
183262+
sqlite3_mutex_leave(db->mutex);
183263+
return rc;
183264+
#endif
183265+
}
183266+
183210183267
/*
183211183268
** Return the number of frames in the WAL of the given database.
183212183269
*/

libsql-ffi/bundled/src/sqlite3.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10564,6 +10564,15 @@ SQLITE_API void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlit
1056410564
*/
1056510565
SQLITE_API int libsql_wal_disable_checkpoint(sqlite3 *db);
1056610566

10567+
/*
10568+
** CAPI3REF: Get the checkpoint sequence counter of the WAL file
10569+
** METHOD: sqlite3
10570+
**
10571+
** ^The [libsql_wal_checkpoint_seq_count(D,P)] interface returns the checkpoint sequence counter
10572+
** of the WAL file for [database connection] D into *P.
10573+
*/
10574+
SQLITE_API int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt);
10575+
1056710576
/*
1056810577
** CAPI3REF: Get the number of frames in the WAL file
1056910578
** METHOD: sqlite3
@@ -13647,6 +13656,9 @@ typedef struct libsql_wal_methods {
1364713656
** response to a ROLLBACK TO command. */
1364813657
int (*xSavepointUndo)(wal_impl* pWal, unsigned int *aWalData);
1364913658

13659+
/* Return the current checkpoint generation in the WAL file. */
13660+
int (*xCheckpointSeqCount)(wal_impl* pWal, unsigned int *pnCkpt);
13661+
1365013662
/* Return the number of frames in the WAL */
1365113663
int (*xFrameCount)(wal_impl* pWal, int, unsigned int *);
1365213664

libsql-replication/src/injector/sqlite_injector/injector_wal.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ impl Wal for InjectorWal {
138138
self.inner.savepoint_undo(rollback_data)
139139
}
140140

141+
fn checkpoint_seq_count(&self) -> Result<u32> {
142+
self.inner.checkpoint_seq_count()
143+
}
144+
141145
fn frame_count(&self, locked: i32) -> Result<u32> {
142146
self.inner.frame_count(locked)
143147
}

libsql-sqlite3/doc/libsql_extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ The libSQL has the following WAL API functions, which are useful for
362362
syncing the WAL between databases:
363363
364364
* `libsql_wal_disable_checkpoint` -- Disable checkpointing, including on database close.
365+
* `libsql_wal_checkpoint_seq_count` -- Return the checkpoint sequence number of the WAL.
365366
* `libsql_wal_frame_count` -- Get the number of frames in the WAL.
366367
* `libsql_wal_get_frame` -- Get a frame from the WAL.
367368
* `libsql_wal_insert_begin` -- Begin WAL insertion.

libsql-sqlite3/src/main.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,30 @@ int libsql_wal_disable_checkpoint(sqlite3 *db) {
24492449
return SQLITE_OK;
24502450
}
24512451

2452+
/*
2453+
** Return the checkpoint sequence counter of the given database.
2454+
*/
2455+
int libsql_wal_checkpoint_seq_count(sqlite3 *db, unsigned int *pnCkpt) {
2456+
int rc = SQLITE_OK;
2457+
Pager *pPager;
2458+
2459+
#ifdef SQLITE_OMIT_WAL
2460+
*pnFrame = 0;
2461+
return SQLITE_OK;
2462+
#else
2463+
#ifdef SQLITE_ENABLE_API_ARMOR
2464+
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
2465+
#endif
2466+
2467+
sqlite3_mutex_enter(db->mutex);
2468+
pPager = sqlite3BtreePager(db->aDb[0].pBt);
2469+
rc = sqlite3PagerWalCheckpointSeqCount(pPager, pnCkpt);
2470+
sqlite3Error(db, rc);
2471+
sqlite3_mutex_leave(db->mutex);
2472+
return rc;
2473+
#endif
2474+
}
2475+
24522476
/*
24532477
** Return the number of frames in the WAL of the given database.
24542478
*/

libsql-sqlite3/src/pager.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7771,6 +7771,18 @@ int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
77717771
return rc;
77727772
}
77737773

7774+
/**
7775+
** Return the current checkpoint generation in the WAL file.
7776+
**/
7777+
int sqlite3PagerWalCheckpointSeqCount(Pager *pPager, unsigned int *pnCkpt){
7778+
if( pagerUseWal(pPager) ){
7779+
return pPager->wal->methods.xCheckpointSeqCount(pPager->wal->pData, pnCkpt);
7780+
} else {
7781+
*pnCkpt = 0;
7782+
return SQLITE_OK;
7783+
}
7784+
}
7785+
77747786
/**
77757787
** Return the number of frames in the WAL file.
77767788
**

0 commit comments

Comments
 (0)