aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/btree.c53
-rw-r--r--src/loadext.c2
-rw-r--r--src/main.c31
-rw-r--r--src/sqlite.h.in66
-rw-r--r--src/sqlite3ext.h9
-rw-r--r--src/sqliteInt.h3
-rw-r--r--src/test1.c93
7 files changed, 244 insertions, 13 deletions
diff --git a/src/btree.c b/src/btree.c
index ccb7eac1a..15073998c 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -3939,16 +3939,18 @@ int sqlite3BtreeIncrVacuum(Btree *p){
/*
** This routine is called prior to sqlite3PagerCommit when a transaction
** is committed for an auto-vacuum database.
-**
-** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
-** the database file should be truncated to during the commit process.
-** i.e. the database has been reorganized so that only the first *pnTrunc
-** pages are in use.
*/
-static int autoVacuumCommit(BtShared *pBt){
+static int autoVacuumCommit(Btree *p){
int rc = SQLITE_OK;
- Pager *pPager = pBt->pPager;
- VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
+ Pager *pPager;
+ BtShared *pBt;
+ sqlite3 *db;
+ VVA_ONLY( int nRef );
+
+ assert( p!=0 );
+ pBt = p->pBt;
+ pPager = pBt->pPager;
+ VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
assert( sqlite3_mutex_held(pBt->mutex) );
invalidateAllOverflowCache(pBt);
@@ -3956,6 +3958,7 @@ static int autoVacuumCommit(BtShared *pBt){
if( !pBt->incrVacuum ){
Pgno nFin; /* Number of pages in database after autovacuuming */
Pgno nFree; /* Number of pages on the freelist initially */
+ Pgno nVac; /* Number of pages to vacuum */
Pgno iFree; /* The next page to be freed */
Pgno nOrig; /* Database size before freeing */
@@ -3969,18 +3972,42 @@ static int autoVacuumCommit(BtShared *pBt){
}
nFree = get4byte(&pBt->pPage1->aData[36]);
- nFin = finalDbSize(pBt, nOrig, nFree);
+ db = p->db;
+ if( db->xAutovacPages ){
+ int iDb;
+ for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
+ if( db->aDb[iDb].pBt==p ) break;
+ }
+ nVac = db->xAutovacPages(
+ db->pAutovacPagesArg,
+ db->aDb[iDb].zDbSName,
+ nOrig,
+ nFree,
+ pBt->pageSize
+ );
+ if( nVac>nFree ){
+ nVac = nFree;
+ }
+ if( nVac==0 ){
+ return SQLITE_OK;
+ }
+ }else{
+ nVac = nFree;
+ }
+ nFin = finalDbSize(pBt, nOrig, nVac);
if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
if( nFin<nOrig ){
rc = saveAllCursors(pBt, 0, 0);
}
for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
- rc = incrVacuumStep(pBt, nFin, iFree, 1);
+ rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree);
}
if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
- put4byte(&pBt->pPage1->aData[32], 0);
- put4byte(&pBt->pPage1->aData[36], 0);
+ if( nVac==nFree ){
+ put4byte(&pBt->pPage1->aData[32], 0);
+ put4byte(&pBt->pPage1->aData[36], 0);
+ }
put4byte(&pBt->pPage1->aData[28], nFin);
pBt->bDoTruncate = 1;
pBt->nPage = nFin;
@@ -4031,7 +4058,7 @@ int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
sqlite3BtreeEnter(p);
#ifndef SQLITE_OMIT_AUTOVACUUM
if( pBt->autoVacuum ){
- rc = autoVacuumCommit(pBt);
+ rc = autoVacuumCommit(p);
if( rc!=SQLITE_OK ){
sqlite3BtreeLeave(p);
return rc;
diff --git a/src/loadext.c b/src/loadext.c
index 29371336c..4edefec0c 100644
--- a/src/loadext.c
+++ b/src/loadext.c
@@ -483,6 +483,8 @@ static const sqlite3_api_routines sqlite3Apis = {
/* Version 3.36.1 and later */
sqlite3_changes64,
sqlite3_total_changes64,
+ /* Version 3.37.0 and later */
+ sqlite3_autovacuum_pages,
};
/* True if x is the directory separator character
diff --git a/src/main.c b/src/main.c
index bf33b640b..aa34977bf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1403,6 +1403,9 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
** structure?
*/
sqlite3DbFree(db, db->aDb[1].pSchema);
+ if( db->xAutovacDestr ){
+ db->xAutovacDestr(db->pAutovacPagesArg);
+ }
sqlite3_mutex_leave(db->mutex);
db->eOpenState = SQLITE_STATE_CLOSED;
sqlite3_mutex_free(db->mutex);
@@ -2304,6 +2307,34 @@ void *sqlite3_preupdate_hook(
}
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
+/*
+** Register a function to be invoked prior to each autovacuum that
+** determines the number of pages to vacuum.
+*/
+int sqlite3_autovacuum_pages(
+ sqlite3 *db, /* Attach the hook to this database */
+ unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
+ void *pArg, /* Argument to the function */
+ void (*xDestructor)(void*) /* Destructor for pArg */
+){
+#ifdef SQLITE_ENABLE_API_ARMOR
+ if( !sqlite3SafetyCheckOk(db) ){
+ if( xDestructor ) xDestructor(pArg);
+ return SQLITE_MISUSE_BKPT;
+ }
+#endif
+ sqlite3_mutex_enter(db->mutex);
+ if( db->xAutovacDestr ){
+ db->xAutovacDestr(db->pAutovacPagesArg);
+ }
+ db->xAutovacPages = xCallback;
+ db->pAutovacPagesArg = pArg;
+ db->xAutovacDestr = xDestructor;
+ sqlite3_mutex_leave(db->mutex);
+ return SQLITE_OK;
+}
+
+
#ifndef SQLITE_OMIT_WAL
/*
** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index 4435925de..f20d457e3 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -6407,6 +6407,72 @@ void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
/*
+** CAPI3REF: Autovacuum Compaction Amount Callback
+** METHOD: sqlite3
+**
+** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
+** function C that is invoked prior to each autovacuum of the database
+** file. ^The callback is passed a copy of the generic data pointer (P),
+** the schema-name of the attached database that is being autovacuumed,
+** the the size of the database file in pages, the number of free pages,
+** and the number of bytes per page, respectively. The callback should
+** return the number of free pages that should be removed by the
+** autovacuum. ^If the callback returns zero, then no autovacuum happens.
+** ^If the value returned is greater than or equal to the number of
+** free pages, then a complete autovacuum happens.
+**
+** <p>^If there are multiple ATTACH-ed database files that are being
+** modified as part of a transaction commit, then the autovacuum pages
+** callback is invoked separately for each file.
+**
+** <p><b>The callback is not reentrant.</b> The callback function should
+** not attempt to invoke any other SQLite interface. If it does, bad
+** things may happen, including segmentation faults and corrupt database
+** files. The callback function should be a simple function that
+** does some arithmetic on its input parameters and returns a result.
+**
+** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional
+** destructor for the P parameter. ^If X is not NULL, then X(P) is
+** invoked whenever the database connection closes or when the callback
+** is overwritten by another invocation of sqlite3_autovacuum_pages().
+**
+** <p>^There is only one autovacuum pages callback per database connection.
+** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
+** previous invocations for that database connection. ^If the callback
+** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
+** then the autovacuum steps callback is cancelled. The return value
+** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
+** be some other error code if something goes wrong. The current
+** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
+** return codes might be added in future releases.
+**
+** <p>If no autovacuum pages callback is specified (the usual case) or
+** a NULL pointer is provided for the callback,
+** then the default behavior is to vacuum all free pages. So, in other
+** words, the default behavior is the same as if the callback function
+** were something like this:
+**
+** <blockquote><pre>
+** &nbsp; unsigned int demonstration_autovac_pages_callback(
+** &nbsp; void *pClientData,
+** &nbsp; const char *zSchema,
+** &nbsp; unsigned int nDbPage,
+** &nbsp; unsigned int nFreePage,
+** &nbsp; unsigned int nBytePerPage
+** &nbsp; ){
+** &nbsp; return nFreePage;
+** &nbsp; }
+** </pre></blockquote>
+*/
+int sqlite3_autovacuum_pages(
+ sqlite3 *db,
+ unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
+ void*,
+ void(*)(void*)
+);
+
+
+/*
** CAPI3REF: Data Change Notification Callbacks
** METHOD: sqlite3
**
diff --git a/src/sqlite3ext.h b/src/sqlite3ext.h
index 98d930524..4ec11324b 100644
--- a/src/sqlite3ext.h
+++ b/src/sqlite3ext.h
@@ -340,6 +340,10 @@ struct sqlite3_api_routines {
/* Version 3.36.1 and later */
sqlite3_int64 (*changes64)(sqlite3*);
sqlite3_int64 (*total_changes64)(sqlite3*);
+ /* Version 3.37.0 and later */
+ int (*autovacuum_pages)(sqlite3*,
+ unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
+ void*, void(*)(void*));
};
/*
@@ -646,6 +650,11 @@ typedef int (*sqlite3_loadext_entry)(
#define sqlite3_database_file_object sqlite3_api->database_file_object
/* Version 3.34.0 and later */
#define sqlite3_txn_state sqlite3_api->txn_state
+/* Version 3.36.1 and later */
+#define sqlite3_changes64 sqlite3_api->changes64
+#define sqlite3_total_changes64 sqlite3_api->total_changes64
+* Version 3.37.0 and later */
+#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index dbf994d53..b50800c66 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -1565,6 +1565,9 @@ struct sqlite3 {
void (*xRollbackCallback)(void*); /* Invoked at every commit. */
void *pUpdateArg;
void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
+ void *pAutovacPagesArg; /* Client argument to autovac_pages */
+ void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
+ unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
Parse *pParse; /* Current parse */
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
diff --git a/src/test1.c b/src/test1.c
index ceafbe503..6bf7b17d3 100644
--- a/src/test1.c
+++ b/src/test1.c
@@ -8233,6 +8233,98 @@ static int SQLITE_TCLAPI test_decode_hexdb(
return TCL_OK;
}
+/*
+** Client data for the autovacuum_pages callback.
+*/
+struct AutovacPageData {
+ Tcl_Interp *interp;
+ char *zScript;
+};
+typedef struct AutovacPageData AutovacPageData;
+
+/*
+** Callback functions for sqlite3_autovacuum_pages
+*/
+static unsigned int test_autovacuum_pages_callback(
+ void *pClientData,
+ const char *zSchema,
+ unsigned int nFilePages,
+ unsigned int nFreePages,
+ unsigned int nBytePerPage
+){
+ AutovacPageData *pData = (AutovacPageData*)pClientData;
+ Tcl_DString str;
+ unsigned int x;
+ char zBuf[100];
+ Tcl_DStringInit(&str);
+ Tcl_DStringAppend(&str, pData->zScript, -1);
+ Tcl_DStringAppendElement(&str, zSchema);
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFilePages);
+ Tcl_DStringAppendElement(&str, zBuf);
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFreePages);
+ Tcl_DStringAppendElement(&str, zBuf);
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nBytePerPage);
+ Tcl_DStringAppendElement(&str, zBuf);
+ Tcl_ResetResult(pData->interp);
+ Tcl_Eval(pData->interp, Tcl_DStringValue(&str));
+ Tcl_DStringFree(&str);
+ x = nFreePages;
+ (void)Tcl_GetIntFromObj(0, Tcl_GetObjResult(pData->interp), (int*)&x);
+ return x;
+}
+
+/*
+** Usage: sqlite3_autovacuum_pages DB SCRIPT
+**
+** Add an autovacuum-pages callback to database connection DB. The callback
+** will invoke SCRIPT, after appending parameters.
+**
+** If SCRIPT is an empty string or is omitted, then the callback is
+** cancelled.
+*/
+static int SQLITE_TCLAPI test_autovacuum_pages(
+ void * clientData,
+ Tcl_Interp *interp,
+ int objc,
+ Tcl_Obj *CONST objv[]
+){
+ AutovacPageData *pData;
+ sqlite3 *db;
+ int rc;
+ const char *zScript;
+ size_t nScript;
+ if( objc!=2 && objc!=3 ){
+ Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCRIPT?");
+ return TCL_ERROR;
+ }
+ if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
+ zScript = objc==3 ? Tcl_GetString(objv[2]) : 0;
+ nScript = zScript ? strlen(zScript) : 0;
+ pData = sqlite3_malloc64( sizeof(*pData) + nScript + 1 );
+ if( pData==0 ){
+ Tcl_AppendResult(interp, "out of memory", (void*)0);
+ return TCL_ERROR;
+ }
+ pData->interp = interp;
+ if( zScript ){
+ pData->zScript = (char*)&pData[1];
+ memcpy(pData->zScript, zScript, nScript+1);
+ rc = sqlite3_autovacuum_pages(db,test_autovacuum_pages_callback,
+ pData, sqlite3_free);
+ }else{
+ pData->zScript = 0;
+ rc = sqlite3_autovacuum_pages(db, 0, 0, 0);
+ }
+ if( rc ){
+ char zBuf[1000];
+ sqlite3_snprintf(sizeof(zBuf), zBuf,
+ "sqlite3_autovacuum_pages() returns %d", rc);
+ Tcl_AppendResult(interp, zBuf, (void*)0);
+ return TCL_ERROR;
+ }
+ return TCL_OK;
+}
+
/*
** Register commands with the TCL interpreter.
@@ -8524,6 +8616,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
{ "atomic_batch_write", test_atomic_batch_write, 0 },
{ "sqlite3_mmap_warm", test_mmap_warm, 0 },
{ "sqlite3_config_sorterref", test_config_sorterref, 0 },
+ { "sqlite3_autovacuum_pages", test_autovacuum_pages, 0 },
{ "decode_hexdb", test_decode_hexdb, 0 },
{ "test_write_db", test_write_db, 0 },
{ "sqlite3_register_cksumvfs", test_register_cksumvfs, 0 },