blob: b9135fd8593f98cd2d8ece1925a5aa50bf1d1d87 [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 header file defines the interface that the sqlite page cache
13** subsystem.
danielk19778c0a7912008-08-20 14:49:23 +000014*/
15
16#ifndef _PCACHE_H_
17
18typedef struct PgHdr PgHdr;
19typedef struct PCache PCache;
20
21/*
22** Every page in the cache is controlled by an instance of the following
23** structure.
24*/
25struct PgHdr {
dan22e21ff2011-11-08 20:08:44 +000026 sqlite3_pcache_page *pPage; /* Pcache object page handle */
27 void *pData; /* Page data */
danielk19778c0a7912008-08-20 14:49:23 +000028 void *pExtra; /* Extra content */
29 PgHdr *pDirty; /* Transient list of dirty pages */
drha85f7e32008-08-28 02:26:07 +000030 Pager *pPager; /* The pager this page is part of */
drha4510172012-02-02 15:50:17 +000031 Pgno pgno; /* Page number for this page */
danielk19778c0a7912008-08-20 14:49:23 +000032#ifdef SQLITE_CHECK_PAGES
drha85f7e32008-08-28 02:26:07 +000033 u32 pageHash; /* Hash of page content */
danielk19778c0a7912008-08-20 14:49:23 +000034#endif
drha85f7e32008-08-28 02:26:07 +000035 u16 flags; /* PGHDR flags defined below */
danielk1977bc2ca9e2008-11-13 14:28:28 +000036
drha85f7e32008-08-28 02:26:07 +000037 /**********************************************************************
38 ** Elements above are public. All that follows is private to pcache.c
39 ** and should not be accessed by other modules.
40 */
41 i16 nRef; /* Number of users of this page */
danielk19778c0a7912008-08-20 14:49:23 +000042 PCache *pCache; /* Cache that owns this page */
danielk1977b3175382008-10-17 18:51:52 +000043
danielk1977bc2ca9e2008-11-13 14:28:28 +000044 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
45 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
danielk19778c0a7912008-08-20 14:49:23 +000046};
47
48/* Bit values for PgHdr.flags */
drhb3df2e12008-09-17 20:06:26 +000049#define PGHDR_DIRTY 0x002 /* Page has changed */
50#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
51 ** writing this page to the database */
52#define PGHDR_NEED_READ 0x008 /* Content is unread */
53#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
54#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
danielk19778c0a7912008-08-20 14:49:23 +000055
56/* Initialize and shutdown the page cache subsystem */
57int sqlite3PcacheInitialize(void);
58void sqlite3PcacheShutdown(void);
59
60/* Page cache buffer management:
61** These routines implement SQLITE_CONFIG_PAGECACHE.
62*/
63void sqlite3PCacheBufferSetup(void *, int sz, int n);
danielk19778c0a7912008-08-20 14:49:23 +000064
65/* Create a new pager cache.
66** Under memory stress, invoke xStress to try to make pages clean.
67** Only clean and unpinned pages can be reclaimed.
68*/
69void sqlite3PcacheOpen(
danielk1977a858aa22008-08-22 16:22:17 +000070 int szPage, /* Size of every page */
71 int szExtra, /* Extra space associated with each page */
72 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +000073 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
74 void *pStress, /* Argument to xStress */
75 PCache *pToInit /* Preallocated space for the PCache */
danielk19778c0a7912008-08-20 14:49:23 +000076);
77
78/* Modify the page-size after the cache has been created. */
79void sqlite3PcacheSetPageSize(PCache *, int);
80
81/* Return the size in bytes of a PCache object. Used to preallocate
82** storage space.
83*/
84int sqlite3PcacheSize(void);
85
86/* One release per successful fetch. Page is pinned until released.
87** Reference counted.
88*/
89int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
90void sqlite3PcacheRelease(PgHdr*);
91
92void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
93void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
94void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
95void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
96
97/* Change a page number. Used by incr-vacuum. */
98void sqlite3PcacheMove(PgHdr*, Pgno);
99
danielk19778c0a7912008-08-20 14:49:23 +0000100/* Remove all pages with pgno>x. Reset the cache if x==0 */
101void sqlite3PcacheTruncate(PCache*, Pgno x);
102
danielk19778c0a7912008-08-20 14:49:23 +0000103/* Get a list of all dirty pages in the cache, sorted by page number */
104PgHdr *sqlite3PcacheDirtyList(PCache*);
105
danielk19778c0a7912008-08-20 14:49:23 +0000106/* Reset and close the cache object */
107void sqlite3PcacheClose(PCache*);
108
drhb3df2e12008-09-17 20:06:26 +0000109/* Clear flags from pages of the page cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000110void sqlite3PcacheClearSyncFlags(PCache *);
danielk19778c0a7912008-08-20 14:49:23 +0000111
danielk19778c0a7912008-08-20 14:49:23 +0000112/* Discard the contents of the cache */
danielk1977bea2a942009-01-20 17:06:27 +0000113void sqlite3PcacheClear(PCache*);
danielk19778c0a7912008-08-20 14:49:23 +0000114
115/* Return the total number of outstanding page references */
116int sqlite3PcacheRefCount(PCache*);
117
118/* Increment the reference count of an existing page */
119void sqlite3PcacheRef(PgHdr*);
120
danielk197771d5d2c2008-09-29 11:49:47 +0000121int sqlite3PcachePageRefcount(PgHdr*);
122
danielk19778c0a7912008-08-20 14:49:23 +0000123/* Return the total number of pages stored in the cache */
124int sqlite3PcachePagecount(PCache*);
125
danielk1977750e87d2009-07-25 11:46:48 +0000126#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000127/* Iterate through all dirty pages currently stored in the cache. This
128** interface is only available if SQLITE_CHECK_PAGES is defined when the
129** library is built.
danielk19778c0a7912008-08-20 14:49:23 +0000130*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000131void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
drh419fcf62008-10-11 17:42:28 +0000132#endif
danielk19778c0a7912008-08-20 14:49:23 +0000133
danielk1977d491e1b2008-08-26 18:05:48 +0000134/* Set and get the suggested cache-size for the specified pager-cache.
danielk19778c0a7912008-08-20 14:49:23 +0000135**
136** If no global maximum is configured, then the system attempts to limit
137** the total number of pages cached by purgeable pager-caches to the sum
138** of the suggested cache-sizes.
139*/
danielk19778c0a7912008-08-20 14:49:23 +0000140void sqlite3PcacheSetCachesize(PCache *, int);
danielk1977f3d3c272008-11-19 16:52:44 +0000141#ifdef SQLITE_TEST
142int sqlite3PcacheGetCachesize(PCache *);
143#endif
danielk19778c0a7912008-08-20 14:49:23 +0000144
drh09419b42011-11-16 19:29:17 +0000145/* Free up as much memory as possible from the page cache */
146void sqlite3PcacheShrink(PCache*);
147
drh419fcf62008-10-11 17:42:28 +0000148#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d491e1b2008-08-26 18:05:48 +0000149/* Try to return memory used by the pcache module to the main memory heap */
danielk197767e3da72008-08-21 12:19:44 +0000150int sqlite3PcacheReleaseMemory(int);
drh419fcf62008-10-11 17:42:28 +0000151#endif
danielk197767e3da72008-08-21 12:19:44 +0000152
drh419fcf62008-10-11 17:42:28 +0000153#ifdef SQLITE_TEST
danielk1977062d4cb2008-08-29 09:10:02 +0000154void sqlite3PcacheStats(int*,int*,int*,int*);
drh419fcf62008-10-11 17:42:28 +0000155#endif
danielk1977062d4cb2008-08-29 09:10:02 +0000156
danielk1977bc2ca9e2008-11-13 14:28:28 +0000157void sqlite3PCacheSetDefault(void);
158
danielk19778c0a7912008-08-20 14:49:23 +0000159#endif /* _PCACHE_H_ */