blob: 242f3071d90ae2fec8c16b9f97ea93c8bbd4aae3 [file] [log] [blame]
danielk19778c0a7912008-08-20 14:49:23 +00001/*
2** 2008 August 05
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file implements that page cache.
danielk19778c0a7912008-08-20 14:49:23 +000013*/
14#include "sqliteInt.h"
15
16/*
17** A complete page cache is an instance of this structure.
18*/
19struct PCache {
danielk1977d491e1b2008-08-26 18:05:48 +000020 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
21 PgHdr *pSynced; /* Last synced page in dirty page list */
danielk1977bc2ca9e2008-11-13 14:28:28 +000022 int nRef; /* Number of referenced pages */
drha85f7e32008-08-28 02:26:07 +000023 int nMax; /* Configured cache size */
drha85f7e32008-08-28 02:26:07 +000024 int szPage; /* Size of every page in this cache */
25 int szExtra; /* Size of extra space for each page */
26 int bPurgeable; /* True if pages are on backing store */
drha85f7e32008-08-28 02:26:07 +000027 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
28 void *pStress; /* Argument to xStress */
danielk1977bc2ca9e2008-11-13 14:28:28 +000029 sqlite3_pcache *pCache; /* Pluggable cache module */
danielk1977bea2a942009-01-20 17:06:27 +000030 PgHdr *pPage1; /* Reference to page 1 */
danielk19778c0a7912008-08-20 14:49:23 +000031};
32
33/*
drha85f7e32008-08-28 02:26:07 +000034** Some of the assert() macros in this code are too expensive to run
35** even during normal debugging. Use them only rarely on long-running
36** tests. Enable the expensive asserts using the
37** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
38*/
39#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
40# define expensive_assert(X) assert(X)
41#else
42# define expensive_assert(X)
43#endif
danielk19778c0a7912008-08-20 14:49:23 +000044
danielk19778c0a7912008-08-20 14:49:23 +000045/********************************** Linked List Management ********************/
46
drha85f7e32008-08-28 02:26:07 +000047#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
drh41d30272008-08-20 21:47:45 +000048/*
danielk1977d491e1b2008-08-26 18:05:48 +000049** Check that the pCache->pSynced variable is set correctly. If it
50** is not, either fail an assert or return zero. Otherwise, return
51** non-zero. This is only used in debugging builds, as follows:
52**
drha85f7e32008-08-28 02:26:07 +000053** expensive_assert( pcacheCheckSynced(pCache) );
danielk1977d491e1b2008-08-26 18:05:48 +000054*/
55static int pcacheCheckSynced(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +000056 PgHdr *p;
57 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
danielk1977d491e1b2008-08-26 18:05:48 +000058 assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
59 }
60 return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
61}
drha85f7e32008-08-28 02:26:07 +000062#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
danielk1977d491e1b2008-08-26 18:05:48 +000063
danielk19778c0a7912008-08-20 14:49:23 +000064/*
danielk1977bc2ca9e2008-11-13 14:28:28 +000065** Remove page pPage from the list of dirty pages.
danielk19778c0a7912008-08-20 14:49:23 +000066*/
danielk1977bc2ca9e2008-11-13 14:28:28 +000067static void pcacheRemoveFromDirtyList(PgHdr *pPage){
68 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +000069
danielk1977bc2ca9e2008-11-13 14:28:28 +000070 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
71 assert( pPage->pDirtyPrev || pPage==p->pDirty );
danielk19778c0a7912008-08-20 14:49:23 +000072
danielk1977bc2ca9e2008-11-13 14:28:28 +000073 /* Update the PCache1.pSynced variable if necessary. */
74 if( p->pSynced==pPage ){
75 PgHdr *pSynced = pPage->pDirtyPrev;
76 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
77 pSynced = pSynced->pDirtyPrev;
danielk1977d491e1b2008-08-26 18:05:48 +000078 }
danielk1977bc2ca9e2008-11-13 14:28:28 +000079 p->pSynced = pSynced;
danielk1977d491e1b2008-08-26 18:05:48 +000080 }
danielk19778c0a7912008-08-20 14:49:23 +000081
danielk1977bc2ca9e2008-11-13 14:28:28 +000082 if( pPage->pDirtyNext ){
83 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
danielk19778c0a7912008-08-20 14:49:23 +000084 }else{
danielk1977bc2ca9e2008-11-13 14:28:28 +000085 assert( pPage==p->pDirtyTail );
86 p->pDirtyTail = pPage->pDirtyPrev;
danielk19778c0a7912008-08-20 14:49:23 +000087 }
danielk1977bc2ca9e2008-11-13 14:28:28 +000088 if( pPage->pDirtyPrev ){
89 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +000090 }else{
danielk1977bc2ca9e2008-11-13 14:28:28 +000091 assert( pPage==p->pDirty );
92 p->pDirty = pPage->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +000093 }
danielk1977bc2ca9e2008-11-13 14:28:28 +000094 pPage->pDirtyNext = 0;
95 pPage->pDirtyPrev = 0;
96
97 expensive_assert( pcacheCheckSynced(p) );
danielk19778c0a7912008-08-20 14:49:23 +000098}
99
100/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000101** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
102** pPage).
danielk19778c0a7912008-08-20 14:49:23 +0000103*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000104static void pcacheAddToDirtyList(PgHdr *pPage){
105 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000106
danielk1977bc2ca9e2008-11-13 14:28:28 +0000107 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
108
109 pPage->pDirtyNext = p->pDirty;
110 if( pPage->pDirtyNext ){
111 assert( pPage->pDirtyNext->pDirtyPrev==0 );
112 pPage->pDirtyNext->pDirtyPrev = pPage;
danielk19778c0a7912008-08-20 14:49:23 +0000113 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000114 p->pDirty = pPage;
115 if( !p->pDirtyTail ){
116 p->pDirtyTail = pPage;
117 }
118 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
119 p->pSynced = pPage;
120 }
121 expensive_assert( pcacheCheckSynced(p) );
danielk19778c0a7912008-08-20 14:49:23 +0000122}
123
124/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000125** Wrapper around the pluggable caches xUnpin method. If the cache is
126** being used for an in-memory database, this function is a no-op.
danielk19778c0a7912008-08-20 14:49:23 +0000127*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000128static void pcacheUnpin(PgHdr *p){
129 PCache *pCache = p->pCache;
danielk1977801880f2008-08-21 15:54:01 +0000130 if( pCache->bPurgeable ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000131 if( p->pgno==1 ){
132 pCache->pPage1 = 0;
danielk1977d491e1b2008-08-26 18:05:48 +0000133 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000134 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000135 }
danielk19778c0a7912008-08-20 14:49:23 +0000136}
137
138/*************************************************** General Interfaces ******
139**
140** Initialize and shutdown the page cache subsystem. Neither of these
141** functions are threadsafe.
142*/
143int sqlite3PcacheInitialize(void){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000144 if( sqlite3GlobalConfig.pcache.xInit==0 ){
drhf759bb82010-09-09 18:25:34 +0000145 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
146 ** built-in default page cache is used instead of the application defined
147 ** page cache. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000148 sqlite3PCacheSetDefault();
danielk19778c0a7912008-08-20 14:49:23 +0000149 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000150 return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
danielk19778c0a7912008-08-20 14:49:23 +0000151}
152void sqlite3PcacheShutdown(void){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000153 if( sqlite3GlobalConfig.pcache.xShutdown ){
drhf759bb82010-09-09 18:25:34 +0000154 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000155 sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
156 }
danielk19778c0a7912008-08-20 14:49:23 +0000157}
158
159/*
160** Return the size in bytes of a PCache object.
161*/
162int sqlite3PcacheSize(void){ return sizeof(PCache); }
163
164/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000165** Create a new PCache object. Storage space to hold the object
166** has already been allocated and is passed in as the p pointer.
167** The caller discovers how much space needs to be allocated by
168** calling sqlite3PcacheSize().
danielk19778c0a7912008-08-20 14:49:23 +0000169*/
170void sqlite3PcacheOpen(
171 int szPage, /* Size of every page */
172 int szExtra, /* Extra space associated with each page */
173 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +0000174 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
danielk19778c0a7912008-08-20 14:49:23 +0000175 void *pStress, /* Argument to xStress */
176 PCache *p /* Preallocated space for the PCache */
177){
danielk19778c0a7912008-08-20 14:49:23 +0000178 memset(p, 0, sizeof(PCache));
179 p->szPage = szPage;
180 p->szExtra = szExtra;
181 p->bPurgeable = bPurgeable;
danielk19778c0a7912008-08-20 14:49:23 +0000182 p->xStress = xStress;
183 p->pStress = pStress;
184 p->nMax = 100;
danielk19778c0a7912008-08-20 14:49:23 +0000185}
186
drh41d30272008-08-20 21:47:45 +0000187/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000188** Change the page size for PCache object. The caller must ensure that there
189** are no outstanding page references when this function is called.
drh41d30272008-08-20 21:47:45 +0000190*/
danielk19778c0a7912008-08-20 14:49:23 +0000191void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000192 assert( pCache->nRef==0 && pCache->pDirty==0 );
193 if( pCache->pCache ){
194 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
195 pCache->pCache = 0;
drh2ec050c2010-03-02 23:34:54 +0000196 pCache->pPage1 = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000197 }
danielk19778c0a7912008-08-20 14:49:23 +0000198 pCache->szPage = szPage;
199}
200
201/*
202** Try to obtain a page from the cache.
203*/
204int sqlite3PcacheFetch(
205 PCache *pCache, /* Obtain the page from this cache */
206 Pgno pgno, /* Page number to obtain */
207 int createFlag, /* If true, create page if it does not exist already */
208 PgHdr **ppPage /* Write the page here */
209){
danielk1977f599a192008-08-28 10:21:16 +0000210 PgHdr *pPage = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000211 int eCreate;
danielk1977f599a192008-08-28 10:21:16 +0000212
danielk19778c0a7912008-08-20 14:49:23 +0000213 assert( pCache!=0 );
danielk197789bc4bc2009-07-21 19:25:24 +0000214 assert( createFlag==1 || createFlag==0 );
danielk19778c0a7912008-08-20 14:49:23 +0000215 assert( pgno>0 );
danielk19778c0a7912008-08-20 14:49:23 +0000216
danielk1977bc2ca9e2008-11-13 14:28:28 +0000217 /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
218 ** allocate it now.
219 */
220 if( !pCache->pCache && createFlag ){
221 sqlite3_pcache *p;
222 int nByte;
223 nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
224 p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
225 if( !p ){
226 return SQLITE_NOMEM;
227 }
228 sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
229 pCache->pCache = p;
230 }
danielk1977f599a192008-08-28 10:21:16 +0000231
danielk197789bc4bc2009-07-21 19:25:24 +0000232 eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000233 if( pCache->pCache ){
234 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
235 }
236
237 if( !pPage && eCreate==1 ){
238 PgHdr *pPg;
239
240 /* Find a dirty page to write-out and recycle. First try to find a
241 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
242 ** cleared), but if that is not possible settle for any other
243 ** unreferenced dirty page.
244 */
245 expensive_assert( pcacheCheckSynced(pCache) );
246 for(pPg=pCache->pSynced;
247 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
248 pPg=pPg->pDirtyPrev
249 );
drha9638962010-02-04 17:38:31 +0000250 pCache->pSynced = pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000251 if( !pPg ){
252 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
253 }
254 if( pPg ){
255 int rc;
256 rc = pCache->xStress(pCache->pStress, pPg);
257 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
258 return rc;
danielk19778c0a7912008-08-20 14:49:23 +0000259 }
260 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000261
262 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
danielk19778c0a7912008-08-20 14:49:23 +0000263 }
264
danielk1977bc2ca9e2008-11-13 14:28:28 +0000265 if( pPage ){
danielk1977e1fd5082009-01-23 16:45:00 +0000266 if( !pPage->pData ){
dan026e5982010-06-10 06:53:26 +0000267 memset(pPage, 0, sizeof(PgHdr));
268 pPage->pData = (void *)&pPage[1];
269 pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
270 memset(pPage->pExtra, 0, pCache->szExtra);
danielk1977e1fd5082009-01-23 16:45:00 +0000271 pPage->pCache = pCache;
272 pPage->pgno = pgno;
273 }
274 assert( pPage->pCache==pCache );
275 assert( pPage->pgno==pgno );
dan026e5982010-06-10 06:53:26 +0000276 assert( pPage->pData==(void *)&pPage[1] );
277 assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
danielk1977e1fd5082009-01-23 16:45:00 +0000278
danielk1977bc2ca9e2008-11-13 14:28:28 +0000279 if( 0==pPage->nRef ){
danielk1977f599a192008-08-28 10:21:16 +0000280 pCache->nRef++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000281 }
282 pPage->nRef++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000283 if( pgno==1 ){
284 pCache->pPage1 = pPage;
danielk1977f599a192008-08-28 10:21:16 +0000285 }
danielk19778c0a7912008-08-20 14:49:23 +0000286 }
danielk1977f599a192008-08-28 10:21:16 +0000287 *ppPage = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000288 return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
danielk19778c0a7912008-08-20 14:49:23 +0000289}
290
291/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000292** Decrement the reference count on a page. If the page is clean and the
293** reference count drops to 0, then it is made elible for recycling.
danielk19778c0a7912008-08-20 14:49:23 +0000294*/
295void sqlite3PcacheRelease(PgHdr *p){
296 assert( p->nRef>0 );
danielk1977502b7432008-08-25 14:49:42 +0000297 p->nRef--;
298 if( p->nRef==0 ){
299 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000300 pCache->nRef--;
301 if( (p->flags&PGHDR_DIRTY)==0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000302 pcacheUnpin(p);
danielk1977d491e1b2008-08-26 18:05:48 +0000303 }else{
danielk1977bc2ca9e2008-11-13 14:28:28 +0000304 /* Move the page to the head of the dirty list. */
305 pcacheRemoveFromDirtyList(p);
306 pcacheAddToDirtyList(p);
danielk1977d491e1b2008-08-26 18:05:48 +0000307 }
danielk1977502b7432008-08-25 14:49:42 +0000308 }
danielk19778c0a7912008-08-20 14:49:23 +0000309}
310
danielk1977bc2ca9e2008-11-13 14:28:28 +0000311/*
312** Increase the reference count of a supplied page by 1.
313*/
danielk19778c0a7912008-08-20 14:49:23 +0000314void sqlite3PcacheRef(PgHdr *p){
danielk1977502b7432008-08-25 14:49:42 +0000315 assert(p->nRef>0);
316 p->nRef++;
danielk19778c0a7912008-08-20 14:49:23 +0000317}
318
319/*
danielk19774abdfa42008-08-27 09:44:39 +0000320** Drop a page from the cache. There must be exactly one reference to the
321** page. This function deletes that reference, so after it returns the
322** page pointed to by p is invalid.
danielk19778c0a7912008-08-20 14:49:23 +0000323*/
324void sqlite3PcacheDrop(PgHdr *p){
325 PCache *pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000326 assert( p->nRef==1 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000327 if( p->flags&PGHDR_DIRTY ){
328 pcacheRemoveFromDirtyList(p);
329 }
danielk19778c0a7912008-08-20 14:49:23 +0000330 pCache = p->pCache;
331 pCache->nRef--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000332 if( p->pgno==1 ){
333 pCache->pPage1 = 0;
334 }
335 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
danielk19778c0a7912008-08-20 14:49:23 +0000336}
337
338/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000339** Make sure the page is marked as dirty. If it isn't dirty already,
danielk19778c0a7912008-08-20 14:49:23 +0000340** make it so.
341*/
342void sqlite3PcacheMakeDirty(PgHdr *p){
danielk197733e32162008-08-23 18:53:08 +0000343 p->flags &= ~PGHDR_DONT_WRITE;
danielk19778c0a7912008-08-20 14:49:23 +0000344 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000345 if( 0==(p->flags & PGHDR_DIRTY) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000346 p->flags |= PGHDR_DIRTY;
347 pcacheAddToDirtyList( p);
danielk1977d491e1b2008-08-26 18:05:48 +0000348 }
danielk1977f599a192008-08-28 10:21:16 +0000349}
350
351/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000352** Make sure the page is marked as clean. If it isn't clean already,
danielk1977f599a192008-08-28 10:21:16 +0000353** make it so.
354*/
355void sqlite3PcacheMakeClean(PgHdr *p){
356 if( (p->flags & PGHDR_DIRTY) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000357 pcacheRemoveFromDirtyList(p);
358 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
359 if( p->nRef==0 ){
360 pcacheUnpin(p);
361 }
danielk1977f599a192008-08-28 10:21:16 +0000362 }
danielk19778c0a7912008-08-20 14:49:23 +0000363}
364
365/*
366** Make every page in the cache clean.
367*/
368void sqlite3PcacheCleanAll(PCache *pCache){
369 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000370 while( (p = pCache->pDirty)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000371 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000372 }
373}
374
375/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000376** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
377*/
378void sqlite3PcacheClearSyncFlags(PCache *pCache){
379 PgHdr *p;
380 for(p=pCache->pDirty; p; p=p->pDirtyNext){
381 p->flags &= ~PGHDR_NEED_SYNC;
382 }
383 pCache->pSynced = pCache->pDirtyTail;
384}
385
386/*
387** Change the page number of page p to newPgno.
danielk19778c0a7912008-08-20 14:49:23 +0000388*/
389void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000390 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000391 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000392 assert( newPgno>0 );
393 sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
danielk19778c0a7912008-08-20 14:49:23 +0000394 p->pgno = newPgno;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000395 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
396 pcacheRemoveFromDirtyList(p);
397 pcacheAddToDirtyList(p);
danielk19778c0a7912008-08-20 14:49:23 +0000398 }
danielk19778c0a7912008-08-20 14:49:23 +0000399}
400
401/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000402** Drop every cache entry whose page number is greater than "pgno". The
403** caller must ensure that there are no outstanding references to any pages
404** other than page 1 with a page number greater than pgno.
405**
406** If there is a reference to page 1 and the pgno parameter passed to this
407** function is 0, then the data area associated with page 1 is zeroed, but
408** the page object is not dropped.
danielk19778c0a7912008-08-20 14:49:23 +0000409*/
410void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000411 if( pCache->pCache ){
412 PgHdr *p;
413 PgHdr *pNext;
414 for(p=pCache->pDirty; p; p=pNext){
415 pNext = p->pDirtyNext;
drhf3609ee2010-03-19 19:23:51 +0000416 /* This routine never gets call with a positive pgno except right
417 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
418 ** it must be that pgno==0.
419 */
420 assert( p->pgno>0 );
421 if( ALWAYS(p->pgno>pgno) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000422 assert( p->flags&PGHDR_DIRTY );
423 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000424 }
425 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000426 if( pgno==0 && pCache->pPage1 ){
427 memset(pCache->pPage1->pData, 0, pCache->szPage);
428 pgno = 1;
429 }
430 sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
danielk1977062d4cb2008-08-29 09:10:02 +0000431 }
432}
danielk19778c0a7912008-08-20 14:49:23 +0000433
434/*
435** Close a cache.
436*/
437void sqlite3PcacheClose(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000438 if( pCache->pCache ){
439 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000440 }
441}
442
443/*
444** Discard the contents of the cache.
445*/
danielk1977bea2a942009-01-20 17:06:27 +0000446void sqlite3PcacheClear(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000447 sqlite3PcacheTruncate(pCache, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000448}
449
450/*
451** Merge two lists of pages connected by pDirty and in pgno order.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000452** Do not both fixing the pDirtyPrev pointers.
danielk19778c0a7912008-08-20 14:49:23 +0000453*/
454static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
455 PgHdr result, *pTail;
456 pTail = &result;
457 while( pA && pB ){
458 if( pA->pgno<pB->pgno ){
459 pTail->pDirty = pA;
460 pTail = pA;
461 pA = pA->pDirty;
462 }else{
463 pTail->pDirty = pB;
464 pTail = pB;
465 pB = pB->pDirty;
466 }
467 }
468 if( pA ){
469 pTail->pDirty = pA;
470 }else if( pB ){
471 pTail->pDirty = pB;
472 }else{
473 pTail->pDirty = 0;
474 }
475 return result.pDirty;
476}
477
478/*
479** Sort the list of pages in accending order by pgno. Pages are
danielk1977bc2ca9e2008-11-13 14:28:28 +0000480** connected by pDirty pointers. The pDirtyPrev pointers are
danielk19778c0a7912008-08-20 14:49:23 +0000481** corrupted by this sort.
drhe64ca7b2009-07-16 18:21:17 +0000482**
483** Since there cannot be more than 2^31 distinct pages in a database,
484** there cannot be more than 31 buckets required by the merge sorter.
485** One extra bucket is added to catch overflow in case something
486** ever changes to make the previous sentence incorrect.
danielk19778c0a7912008-08-20 14:49:23 +0000487*/
drhe64ca7b2009-07-16 18:21:17 +0000488#define N_SORT_BUCKET 32
danielk19778c0a7912008-08-20 14:49:23 +0000489static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
drhe64ca7b2009-07-16 18:21:17 +0000490 PgHdr *a[N_SORT_BUCKET], *p;
danielk19778c0a7912008-08-20 14:49:23 +0000491 int i;
492 memset(a, 0, sizeof(a));
493 while( pIn ){
494 p = pIn;
495 pIn = p->pDirty;
496 p->pDirty = 0;
drhe64ca7b2009-07-16 18:21:17 +0000497 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
danielk19778c0a7912008-08-20 14:49:23 +0000498 if( a[i]==0 ){
499 a[i] = p;
500 break;
501 }else{
502 p = pcacheMergeDirtyList(a[i], p);
503 a[i] = 0;
504 }
505 }
drhe64ca7b2009-07-16 18:21:17 +0000506 if( NEVER(i==N_SORT_BUCKET-1) ){
507 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
508 ** the input list. But that is impossible.
danielk19778c0a7912008-08-20 14:49:23 +0000509 */
510 a[i] = pcacheMergeDirtyList(a[i], p);
511 }
512 }
513 p = a[0];
514 for(i=1; i<N_SORT_BUCKET; i++){
515 p = pcacheMergeDirtyList(p, a[i]);
516 }
517 return p;
518}
519
520/*
521** Return a list of all dirty pages in the cache, sorted by page number.
522*/
523PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
524 PgHdr *p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000525 for(p=pCache->pDirty; p; p=p->pDirtyNext){
526 p->pDirty = p->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +0000527 }
528 return pcacheSortDirtyList(pCache->pDirty);
529}
530
danielk19778c0a7912008-08-20 14:49:23 +0000531/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000532** Return the total number of referenced pages held by the cache.
danielk19778c0a7912008-08-20 14:49:23 +0000533*/
534int sqlite3PcacheRefCount(PCache *pCache){
535 return pCache->nRef;
536}
537
danielk1977bc2ca9e2008-11-13 14:28:28 +0000538/*
539** Return the number of references to the page supplied as an argument.
540*/
danielk197771d5d2c2008-09-29 11:49:47 +0000541int sqlite3PcachePageRefcount(PgHdr *p){
542 return p->nRef;
543}
544
danielk19778c0a7912008-08-20 14:49:23 +0000545/*
546** Return the total number of pages in the cache.
547*/
548int sqlite3PcachePagecount(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000549 int nPage = 0;
550 if( pCache->pCache ){
551 nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000552 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000553 return nPage;
danielk19778c0a7912008-08-20 14:49:23 +0000554}
555
danielk1977f3d3c272008-11-19 16:52:44 +0000556#ifdef SQLITE_TEST
danielk19778c0a7912008-08-20 14:49:23 +0000557/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000558** Get the suggested cache-size value.
danielk19778c0a7912008-08-20 14:49:23 +0000559*/
560int sqlite3PcacheGetCachesize(PCache *pCache){
561 return pCache->nMax;
562}
danielk1977f3d3c272008-11-19 16:52:44 +0000563#endif
danielk19778c0a7912008-08-20 14:49:23 +0000564
565/*
566** Set the suggested cache-size value.
567*/
568void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
danielk19778c0a7912008-08-20 14:49:23 +0000569 pCache->nMax = mxPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000570 if( pCache->pCache ){
571 sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
572 }
danielk19778c0a7912008-08-20 14:49:23 +0000573}
574
danielk1977750e87d2009-07-25 11:46:48 +0000575#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk197767e3da72008-08-21 12:19:44 +0000576/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000577** For all dirty pages currently in the cache, invoke the specified
578** callback. This is only used if the SQLITE_CHECK_PAGES macro is
579** defined.
danielk197767e3da72008-08-21 12:19:44 +0000580*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000581void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
582 PgHdr *pDirty;
583 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
584 xIter(pDirty);
danielk197767e3da72008-08-21 12:19:44 +0000585 }
danielk1977062d4cb2008-08-29 09:10:02 +0000586}
587#endif