aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2009-06-18 17:22:39 +0000
committerdrh <drh@noemail.net>2009-06-18 17:22:39 +0000
commitfa9601a9a6a8ddc35be690cfa2cb8fbd8c92fa31 (patch)
treecf176971cc5a5345f352468fd463a8d456bb8c17 /src
parentccf6d0934db46a2afef1e0ad507e237b4fc69ac5 (diff)
downloadsqlite-fa9601a9a6a8ddc35be690cfa2cb8fbd8c92fa31.tar.gz
sqlite-fa9601a9a6a8ddc35be690cfa2cb8fbd8c92fa31.zip
Move codec management from database connections into the pager so that it
will work together with shared cache. (CVS 6782) FossilOrigin-Name: ed08b53cd64c4ff2c94ef4e48441c5236041c9ca
Diffstat (limited to 'src')
-rw-r--r--src/btree.c9
-rw-r--r--src/build.c11
-rw-r--r--src/pager.c62
-rw-r--r--src/pager.h9
-rw-r--r--src/sqliteInt.h6
-rw-r--r--src/test2.c4
6 files changed, 60 insertions, 41 deletions
diff --git a/src/btree.c b/src/btree.c
index 8d6a1482d..a83c8115d 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** $Id: btree.c,v 1.635 2009/06/18 11:29:21 drh Exp $
+** $Id: btree.c,v 1.636 2009/06/18 17:22:39 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
@@ -1589,7 +1589,7 @@ int sqlite3BtreeOpen(
pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
#endif
}
- rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
+ rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
if( rc ) goto btree_open_out;
pBt->usableSize = pBt->pageSize - nReserve;
assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
@@ -1880,8 +1880,8 @@ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
assert( !pBt->pPage1 && !pBt->pCursor );
pBt->pageSize = (u16)pageSize;
freeTempSpace(pBt);
- rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
}
+ rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
pBt->usableSize = pBt->pageSize - (u16)nReserve;
if( iFix ) pBt->pageSizeFixed = 1;
sqlite3BtreeLeave(p);
@@ -2036,7 +2036,8 @@ static int lockBtree(BtShared *pBt){
pBt->usableSize = (u16)usableSize;
pBt->pageSize = (u16)pageSize;
freeTempSpace(pBt);
- rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
+ rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
+ pageSize-usableSize);
if( rc ) goto page1_init_failed;
return SQLITE_OK;
}
diff --git a/src/build.c b/src/build.c
index a159ee43a..53d03b743 100644
--- a/src/build.c
+++ b/src/build.c
@@ -22,7 +22,7 @@
** COMMIT
** ROLLBACK
**
-** $Id: build.c,v 1.551 2009/06/16 04:35:39 danielk1977 Exp $
+** $Id: build.c,v 1.552 2009/06/18 17:22:39 drh Exp $
*/
#include "sqliteInt.h"
@@ -429,15 +429,6 @@ void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
** schema hash tables and therefore do not have to make any changes
** to any of those tables.
*/
-#ifdef SQLITE_HAS_CODEC
- for(i=0; i<db->nDb; i++){
- struct Db *pDb = &db->aDb[i];
- if( pDb->pBt==0 ){
- if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
- pDb->pAux = 0;
- }
- }
-#endif
for(i=j=2; i<db->nDb; i++){
struct Db *pDb = &db->aDb[i];
if( pDb->pBt==0 ){
diff --git a/src/pager.c b/src/pager.c
index 522a926b5..60ef5678f 100644
--- a/src/pager.c
+++ b/src/pager.c
@@ -18,7 +18,7 @@
** file simultaneously, or one process from reading the database while
** another is writing.
**
-** @(#) $Id: pager.c,v 1.593 2009/06/11 17:32:45 drh Exp $
+** @(#) $Id: pager.c,v 1.594 2009/06/18 17:22:39 drh Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
@@ -104,10 +104,10 @@ int sqlite3PagerTrace=1; /* True to enable tracing */
*/
#ifdef SQLITE_HAS_CODEC
# define CODEC1(P,D,N,X,E) \
- if( P->xCodec && P->xCodec(P->pCodecArg,D,N,X)==0 ){ E; }
+ if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
# define CODEC2(P,D,N,X,E,O) \
if( P->xCodec==0 ){ O=(char*)D; }else \
- if( (O=(char*)(P->xCodec(P->pCodecArg,D,N,X)))==0 ){ E; }
+ if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
#else
# define CODEC1(P,D,N,X,E) /* NO-OP */
# define CODEC2(P,D,N,X,E,O) O=(char*)D
@@ -290,7 +290,8 @@ struct Pager {
char dbFileVers[16]; /* Changes whenever database file changes */
u32 sectorSize; /* Assumed sector size during rollback */
- int nExtra; /* Add this many bytes to each in-memory page */
+ u16 nExtra; /* Add this many bytes to each in-memory page */
+ i16 nReserve; /* Number of unused bytes at end of each page */
u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
int pageSize; /* Number of bytes in a page */
Pgno mxPgno; /* Maximum allowed size of the database */
@@ -305,7 +306,9 @@ struct Pager {
void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
#ifdef SQLITE_HAS_CODEC
void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
- void *pCodecArg; /* First argument to xCodec() */
+ void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
+ void (*xCodecFree)(void*); /* Destructor for the codec */
+ void *pCodec; /* First argument to xCodec... methods */
#endif
char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
i64 journalSizeLimit; /* Size limit for persistent journal files */
@@ -927,7 +930,7 @@ static int readJournalHdr(
** PagerSetPagesize() is tested.
*/
iPageSize16 = (u16)iPageSize;
- rc = sqlite3PagerSetPagesize(pPager, &iPageSize16);
+ rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
testcase( rc!=SQLITE_OK );
assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
@@ -2362,6 +2365,21 @@ void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
}
/*
+** Report the current page size and number of reserved bytes back
+** to the codec.
+*/
+#ifdef SQLITE_HAS_CODEC
+static void pagerReportSize(Pager *pPager){
+ if( pPager->xCodecSizeChng ){
+ pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
+ (int)pPager->nReserve);
+ }
+}
+#else
+# define pagerReportSize(X) /* No-op if we do not support a codec */
+#endif
+
+/*
** Change the page size used by the Pager object. The new page size
** is passed in *pPageSize.
**
@@ -2391,7 +2409,7 @@ void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
** function was called, or because the memory allocation attempt failed,
** then *pPageSize is set to the old, retained page size before returning.
*/
-int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
+int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
int rc = pPager->errCode;
if( rc==SQLITE_OK ){
u16 pageSize = *pPageSize;
@@ -2412,6 +2430,10 @@ int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
}
}
*pPageSize = (u16)pPager->pageSize;
+ if( nReserve<0 ) nReserve = pPager->nReserve;
+ assert( nReserve>=0 && nReserve<1000 );
+ pPager->nReserve = (i16)nReserve;
+ pagerReportSize(pPager);
}
return rc;
}
@@ -2660,6 +2682,10 @@ int sqlite3PagerClose(Pager *pPager){
sqlite3PageFree(pPager->pTmpSpace);
sqlite3PcacheClose(pPager->pPCache);
+#ifdef SQLITE_HAS_CODEC
+ if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
+#endif
+
assert( !pPager->aSavepoint && !pPager->pInJournal );
assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
@@ -3281,7 +3307,7 @@ int sqlite3PagerOpen(
*/
if( rc==SQLITE_OK ){
assert( pPager->memDb==0 );
- rc = sqlite3PagerSetPagesize(pPager, &szPageDflt);
+ rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
testcase( rc!=SQLITE_OK );
}
@@ -3296,6 +3322,7 @@ int sqlite3PagerOpen(
}
/* Initialize the PCache object. */
+ assert( nExtra<1000 );
nExtra = ROUND8(nExtra);
sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
!memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
@@ -3331,7 +3358,7 @@ int sqlite3PagerOpen(
/* pPager->pFirst = 0; */
/* pPager->pFirstSynced = 0; */
/* pPager->pLast = 0; */
- pPager->nExtra = nExtra;
+ pPager->nExtra = (u16)nExtra;
pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
assert( isOpen(pPager->fd) || tempFile );
setSectorSize(pPager);
@@ -5044,15 +5071,24 @@ int sqlite3PagerNosync(Pager *pPager){
#ifdef SQLITE_HAS_CODEC
/*
-** Set the codec for this pager
+** Set or retrieve the codec for this pager
*/
-void sqlite3PagerSetCodec(
+static void sqlite3PagerSetCodec(
Pager *pPager,
void *(*xCodec)(void*,void*,Pgno,int),
- void *pCodecArg
+ void (*xCodecSizeChng)(void*,int,int),
+ void (*xCodecFree)(void*),
+ void *pCodec
){
+ if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
pPager->xCodec = xCodec;
- pPager->pCodecArg = pCodecArg;
+ pPager->xCodecSizeChng = xCodecSizeChng;
+ pPager->xCodecFree = xCodecFree;
+ pPager->pCodec = pCodec;
+ pagerReportSize(pPager);
+}
+static void *sqlite3PagerGetCodec(Pager *pPager){
+ return pPager->pCodec;
}
#endif
diff --git a/src/pager.h b/src/pager.h
index a0110f859..aed165f06 100644
--- a/src/pager.h
+++ b/src/pager.h
@@ -13,7 +13,7 @@
** subsystem. The page cache subsystem reads and writes a file a page
** at a time and provides a journal for rollback.
**
-** @(#) $Id: pager.h,v 1.101 2009/04/30 09:10:38 danielk1977 Exp $
+** @(#) $Id: pager.h,v 1.102 2009/06/18 17:22:39 drh Exp $
*/
#ifndef _PAGER_H_
@@ -93,7 +93,7 @@ int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
/* Functions used to configure a Pager object. */
void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
-int sqlite3PagerSetPagesize(Pager*, u16*);
+int sqlite3PagerSetPagesize(Pager*, u16*, int);
int sqlite3PagerMaxPageCount(Pager*, int);
void sqlite3PagerSetCachesize(Pager*, int);
void sqlite3PagerSetSafetyLevel(Pager*,int,int);
@@ -141,11 +141,6 @@ int sqlite3PagerIsMemdb(Pager*);
/* Functions used to truncate the database file. */
void sqlite3PagerTruncateImage(Pager*,Pgno);
-/* Used by encryption extensions. */
-#ifdef SQLITE_HAS_CODEC
- void sqlite3PagerSetCodec(Pager*,void*(*)(void*,void*,Pgno,int),void*);
-#endif
-
/* Functions to support testing and debugging. */
#if !defined(NDEBUG) || defined(SQLITE_TEST)
Pgno sqlite3PagerPagenumber(DbPage*);
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 0601584b8..67fc1344f 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
-** @(#) $Id: sqliteInt.h,v 1.884 2009/06/15 20:45:35 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.885 2009/06/18 17:22:39 drh Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
@@ -645,10 +645,6 @@ struct Db {
u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */
u8 safety_level; /* How aggressive at syncing data to disk */
Schema *pSchema; /* Pointer to database schema (possibly shared) */
-#ifdef SQLITE_HAS_CODEC
- void *pAux; /* Auxiliary data. Usually NULL */
- void (*xFreeAux)(void*); /* Routine to free pAux */
-#endif
};
/*
diff --git a/src/test2.c b/src/test2.c
index 4db8eede6..0ea382830 100644
--- a/src/test2.c
+++ b/src/test2.c
@@ -13,7 +13,7 @@
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
**
-** $Id: test2.c,v 1.70 2009/02/05 16:31:46 drh Exp $
+** $Id: test2.c,v 1.71 2009/06/18 17:22:39 drh Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
@@ -86,7 +86,7 @@ static int pager_open(
}
sqlite3PagerSetCachesize(pPager, nPage);
pageSize = test_pagesize;
- sqlite3PagerSetPagesize(pPager, &pageSize);
+ sqlite3PagerSetPagesize(pPager, &pageSize, -1);
sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPager);
Tcl_AppendResult(interp, zBuf, 0);
return TCL_OK;