interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / pcache.h

191 lines · 6932 bytes Raw Blame History
1/*
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.
14*/
15
16#ifndef _PCACHE_H_
17#define _PCACHE_H_
18
19typedef struct PgHdr PgHdr;
20typedef struct PCache PCache;
21
22/*
23** Every page in the cache is controlled by an instance of the following
24** structure.
25*/
26struct PgHdr {
27 sqlite3_pcache_page *pPage; /* Pcache object page handle */
28 void *pData; /* Page data */
29 void *pExtra; /* Extra content */
30 PCache *pCache; /* PRIVATE: Cache that owns this page */
31 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
32 Pager *pPager; /* The pager this page is part of */
33#ifdef SQLITE_CHECK_PAGES
34 u64 pageHash; /* Hash of page content */
35#endif
36 Pgno pgno; /* Page number for this page */
37 u16 flags; /* PGHDR flags defined below */
38
39 /**********************************************************************
40 ** Elements above, except pCache, are public. All that follow are
41 ** private to pcache.c and should not be accessed by other modules.
42 ** pCache is grouped with the public elements for efficiency.
43 */
44 i64 nRef; /* Number of users of this page */
45 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
46 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
47 /* NB: pDirtyNext and pDirtyPrev are undefined if the
48 ** PgHdr object is not dirty */
49};
50
51/* Bit values for PgHdr.flags */
52#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
53#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
54#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
55#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
56 ** writing this page to the database */
57#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
58#define PGHDR_MMAP 0x020 /* This is an mmap page object */
59
60#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
61
62/* Initialize and shutdown the page cache subsystem */
63int sqlite3PcacheInitialize(void);
64void sqlite3PcacheShutdown(void);
65
66/* Page cache buffer management:
67** These routines implement SQLITE_CONFIG_PAGECACHE.
68*/
69void sqlite3PCacheBufferSetup(void *, int sz, int n);
70
71/* Create a new pager cache.
72** Under memory stress, invoke xStress to try to make pages clean.
73** Only clean and unpinned pages can be reclaimed.
74*/
75int sqlite3PcacheOpen(
76 int szPage, /* Size of every page */
77 int szExtra, /* Extra space associated with each page */
78 int bPurgeable, /* True if pages are on backing store */
79 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
80 void *pStress, /* Argument to xStress */
81 PCache *pToInit /* Preallocated space for the PCache */
82);
83
84/* Modify the page-size after the cache has been created. */
85int sqlite3PcacheSetPageSize(PCache *, int);
86
87/* Return the size in bytes of a PCache object. Used to preallocate
88** storage space.
89*/
90int sqlite3PcacheSize(void);
91
92/* One release per successful fetch. Page is pinned until released.
93** Reference counted.
94*/
95sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
96int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
97PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
98void sqlite3PcacheRelease(PgHdr*);
99
100void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
101void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
102void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
103void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
104void sqlite3PcacheClearWritable(PCache*);
105
106/* Change a page number. Used by incr-vacuum. */
107void sqlite3PcacheMove(PgHdr*, Pgno);
108
109/* Remove all pages with pgno>x. Reset the cache if x==0 */
110void sqlite3PcacheTruncate(PCache*, Pgno x);
111
112/* Get a list of all dirty pages in the cache, sorted by page number */
113PgHdr *sqlite3PcacheDirtyList(PCache*);
114
115/* Reset and close the cache object */
116void sqlite3PcacheClose(PCache*);
117
118/* Clear flags from pages of the page cache */
119void sqlite3PcacheClearSyncFlags(PCache *);
120
121/* Discard the contents of the cache */
122void sqlite3PcacheClear(PCache*);
123
124/* Return the total number of outstanding page references */
125i64 sqlite3PcacheRefCount(PCache*);
126
127/* Increment the reference count of an existing page */
128void sqlite3PcacheRef(PgHdr*);
129
130i64 sqlite3PcachePageRefcount(PgHdr*);
131
132/* Return the total number of pages stored in the cache */
133int sqlite3PcachePagecount(PCache*);
134
135#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
136/* Iterate through all dirty pages currently stored in the cache. This
137** interface is only available if SQLITE_CHECK_PAGES is defined when the
138** library is built.
139*/
140void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
141#endif
142
143#if defined(SQLITE_DEBUG)
144/* Check invariants on a PgHdr object */
145int sqlite3PcachePageSanity(PgHdr*);
146#endif
147
148/* Set and get the suggested cache-size for the specified pager-cache.
149**
150** If no global maximum is configured, then the system attempts to limit
151** the total number of pages cached by purgeable pager-caches to the sum
152** of the suggested cache-sizes.
153*/
154void sqlite3PcacheSetCachesize(PCache *, int);
155#ifdef SQLITE_TEST
156int sqlite3PcacheGetCachesize(PCache *);
157#endif
158
159/* Set or get the suggested spill-size for the specified pager-cache.
160**
161** The spill-size is the minimum number of pages in cache before the cache
162** will attempt to spill dirty pages by calling xStress.
163*/
164int sqlite3PcacheSetSpillsize(PCache *, int);
165
166/* Free up as much memory as possible from the page cache */
167void sqlite3PcacheShrink(PCache*);
168
169#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
170/* Try to return memory used by the pcache module to the main memory heap */
171int sqlite3PcacheReleaseMemory(int);
172#endif
173
174#ifdef SQLITE_TEST
175void sqlite3PcacheStats(int*,int*,int*,int*);
176#endif
177
178void sqlite3PCacheSetDefault(void);
179
180/* Return the header size */
181int sqlite3HeaderSizePcache(void);
182int sqlite3HeaderSizePcache1(void);
183
184/* Number of dirty pages as a percentage of the configured cache size */
185int sqlite3PCachePercentDirty(PCache*);
186
187#ifdef SQLITE_DIRECT_OVERFLOW_READ
188int sqlite3PCacheIsDirty(PCache *pCache);
189#endif
190
191#endif /* _PCACHE_H_ */