0000: 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 /*.** 2008 Augus
0010: 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 05.**.** The a
0020: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
0030: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
0040: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
0050: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
0060: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
0070: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
0080: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
0090: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
00a0: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
00b0: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
00c0: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
00d0: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
00e0: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
00f0: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
0100: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
0110: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
0120: 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
0180: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 implements that
0190: 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a 2f 0a page cache..*/.
01a0: 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 #include "sqlite
01b0: 49 6e 74 2e 68 22 0a 0a 2f 2a 0a 2a 2a 20 41 20 Int.h"../*.** A
01c0: 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20 63 61 complete page ca
01d0: 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e che is an instan
01e0: 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
01f0: 74 75 72 65 2e 20 20 45 76 65 72 79 0a 2a 2a 20 ture. Every.**
0200: 65 6e 74 72 79 20 69 6e 20 74 68 65 20 63 61 63 entry in the cac
0210: 68 65 20 68 6f 6c 64 73 20 61 20 73 69 6e 67 6c he holds a singl
0220: 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 e page of the da
0230: 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
0240: 65 0a 2a 2a 20 62 74 72 65 65 20 6c 61 79 65 72 e.** btree layer
0250: 20 6f 6e 6c 79 20 6f 70 65 72 61 74 65 73 20 6f only operates o
0260: 6e 20 74 68 65 20 63 61 63 68 65 64 20 63 6f 70 n the cached cop
0270: 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 y of the databas
0280: 65 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 e pages..**.** A
0290: 20 70 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 page cache entr
02a0: 79 20 69 73 20 22 63 6c 65 61 6e 22 20 69 66 20 y is "clean" if
02b0: 69 74 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 it exactly match
02c0: 65 73 20 77 68 61 74 20 69 73 20 63 75 72 72 65 es what is curre
02d0: 6e 74 6c 79 0a 2a 2a 20 6f 6e 20 64 69 73 6b 2e ntly.** on disk.
02e0: 20 20 41 20 70 61 67 65 20 69 73 20 22 64 69 72 A page is "dir
02f0: 74 79 22 20 69 66 20 69 74 20 68 61 73 20 62 65 ty" if it has be
0300: 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 en modified and
0310: 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 70 needs to be.** p
0320: 65 72 73 69 73 74 65 64 20 74 6f 20 64 69 73 6b ersisted to disk
0330: 2e 0a 2a 2a 0a 2a 2a 20 70 44 69 72 74 79 2c 20 ..**.** pDirty,
0340: 70 44 69 72 74 79 54 61 69 6c 2c 20 70 53 79 6e pDirtyTail, pSyn
0350: 63 65 64 3a 0a 2a 2a 20 20 20 41 6c 6c 20 64 69 ced:.** All di
0360: 72 74 79 20 70 61 67 65 73 20 61 72 65 20 6c 69 rty pages are li
0370: 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 64 6f nked into the do
0380: 75 62 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 ubly linked list
0390: 20 75 73 69 6e 67 0a 2a 2a 20 20 20 50 67 48 64 using.** PgHd
03a0: 72 2e 70 44 69 72 74 79 4e 65 78 74 20 61 6e 64 r.pDirtyNext and
03b0: 20 70 44 69 72 74 79 50 72 65 76 2e 20 54 68 65 pDirtyPrev. The
03c0: 20 6c 69 73 74 20 69 73 20 6d 61 69 6e 74 61 69 list is maintai
03d0: 6e 65 64 20 69 6e 20 4c 52 55 20 6f 72 64 65 72 ned in LRU order
03e0: 0a 2a 2a 20 20 20 73 75 63 68 20 74 68 61 74 20 .** such that
03f0: 70 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 74 p was added to t
0400: 68 65 20 6c 69 73 74 20 6d 6f 72 65 20 72 65 63 he list more rec
0410: 65 6e 74 6c 79 20 74 68 61 6e 20 70 2d 3e 70 44 ently than p->pD
0420: 69 72 74 79 4e 65 78 74 2e 0a 2a 2a 20 20 20 50 irtyNext..** P
0430: 43 61 63 68 65 2e 70 44 69 72 74 79 20 70 6f 69 Cache.pDirty poi
0440: 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 nts to the first
0450: 20 28 6e 65 77 65 73 74 29 20 65 6c 65 6d 65 6e (newest) elemen
0460: 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20 61 6e t in the list an
0470: 64 0a 2a 2a 20 20 20 70 44 69 72 74 79 54 61 69 d.** pDirtyTai
0480: 6c 20 74 6f 20 74 68 65 20 6c 61 73 74 20 28 6f l to the last (o
0490: 6c 64 65 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 ldest)..**.**
04a0: 54 68 65 20 50 43 61 63 68 65 2e 70 53 79 6e 63 The PCache.pSync
04b0: 65 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 ed variable is u
04c0: 73 65 64 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 sed to optimize
04d0: 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 searching for a
04e0: 64 69 72 74 79 0a 2a 2a 20 20 20 70 61 67 65 20 dirty.** page
04f0: 74 6f 20 65 6a 65 63 74 20 66 72 6f 6d 20 74 68 to eject from th
0500: 65 20 63 61 63 68 65 20 6d 69 64 2d 74 72 61 6e e cache mid-tran
0510: 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 62 saction. It is b
0520: 65 74 74 65 72 20 74 6f 20 65 6a 65 63 74 0a 2a etter to eject.*
0530: 2a 20 20 20 61 20 70 61 67 65 20 74 68 61 74 20 * a page that
0540: 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 does not require
0550: 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 a journal sync
0560: 74 68 61 6e 20 6f 6e 65 20 74 68 61 74 20 64 6f than one that do
0570: 65 73 2e 20 0a 2a 2a 20 20 20 54 68 65 72 65 66 es. .** Theref
0580: 6f 72 65 2c 20 70 53 79 6e 63 65 64 20 69 73 20 ore, pSynced is
0590: 6d 61 69 6e 74 61 69 6e 65 64 20 74 6f 20 74 68 maintained to th
05a0: 61 74 20 69 74 20 2a 61 6c 6d 6f 73 74 2a 20 61 at it *almost* a
05b0: 6c 77 61 79 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 lways points.**
05c0: 20 20 74 6f 20 65 69 74 68 65 72 20 74 68 65 20 to either the
05d0: 6f 6c 64 65 73 74 20 70 61 67 65 20 69 6e 20 74 oldest page in t
05e0: 68 65 20 70 44 69 72 74 79 2f 70 44 69 72 74 79 he pDirty/pDirty
05f0: 54 61 69 6c 20 6c 69 73 74 20 74 68 61 74 20 68 Tail list that h
0600: 61 73 20 61 0a 2a 2a 20 20 20 63 6c 65 61 72 20 as a.** clear
0610: 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
0620: 66 6c 61 67 20 6f 72 20 74 6f 20 61 20 70 61 67 flag or to a pag
0630: 65 20 74 68 61 74 20 69 73 20 6f 6c 64 65 72 20 e that is older
0640: 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 0a 2a 2a than this one.**
0650: 20 20 20 28 73 6f 20 74 68 61 74 20 74 68 65 20 (so that the
0660: 72 69 67 68 74 20 70 61 67 65 20 74 6f 20 65 6a right page to ej
0670: 65 63 74 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 ect can be found
0680: 20 62 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 44 by following pD
0690: 69 72 74 79 50 72 65 76 0a 2a 2a 20 20 20 70 6f irtyPrev.** po
06a0: 69 6e 74 65 72 73 29 2e 0a 2a 2f 0a 73 74 72 75 inters)..*/.stru
06b0: 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 ct PCache {. Pg
06c0: 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 Hdr *pDirty, *pD
06d0: 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 irtyTail;
06e0: 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 /* List of dir
06f0: 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 ty pages in LRU
0700: 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 order */. PgHdr
0710: 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 *pSynced;
0720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
0730: 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 * Last synced pa
0740: 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 ge in dirty page
0750: 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e list */. int n
0760: 52 65 66 53 75 6d 3b 20 20 20 20 20 20 20 20 20 RefSum;
0770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
0780: 2a 20 53 75 6d 20 6f 66 20 72 65 66 20 63 6f 75 * Sum of ref cou
0790: 6e 74 73 20 6f 76 65 72 20 61 6c 6c 20 70 61 67 nts over all pag
07a0: 65 73 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 43 61 es */. int szCa
07b0: 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 che;
07c0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
07d0: 6f 6e 66 69 67 75 72 65 64 20 63 61 63 68 65 20 onfigured cache
07e0: 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a size */. int sz
07f0: 53 70 69 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 Spill;
0800: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
0810: 20 53 69 7a 65 20 62 65 66 6f 72 65 20 73 70 69 Size before spi
0820: 6c 6c 69 6e 67 20 6f 63 63 75 72 73 20 2a 2f 0a lling occurs */.
0830: 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 int szPage;
0840: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0850: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
0860: 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 every page in t
0870: 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 20 69 his cache */. i
0880: 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 20 20 nt szExtra;
0890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
08a0: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 78 /* Size of ex
08b0: 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 65 61 tra space for ea
08c0: 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 ch page */. u8
08d0: 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 20 bPurgeable;
08e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
08f0: 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 /* True if page
0900: 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 s are on backing
0910: 20 73 74 6f 72 65 20 2a 2f 0a 20 20 75 38 20 65 store */. u8 e
0920: 43 72 65 61 74 65 3b 20 20 20 20 20 20 20 20 20 Create;
0930: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0940: 2f 2a 20 65 43 72 65 61 74 65 20 76 61 6c 75 65 /* eCreate value
0950: 20 66 6f 72 20 66 6f 72 20 78 46 65 74 63 68 28 for for xFetch(
0960: 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 ) */. int (*xSt
0970: 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 ress)(void*,PgHd
0980: 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 r*); /* Ca
0990: 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 61 ll to try make a
09a0: 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 page clean */.
09b0: 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b 20 void *pStress;
09c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
09d0: 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
09e0: 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 to xStress */.
09f0: 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
0a00: 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 *pCache;
0a10: 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62 6c /* Pluggabl
0a20: 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 2a e cache module *
0a30: 2f 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a /.};../*********
0a40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 54 65 73 74 20 61 ********* Test a
0a60: 6e 64 20 44 65 62 75 67 20 4c 6f 67 69 63 20 2a nd Debug Logic *
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a80: 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 44 65 62 *****/./*.** Deb
0a90: 75 67 20 74 72 61 63 69 6e 67 20 6d 61 63 72 6f ug tracing macro
0aa0: 73 2e 20 20 45 6e 61 62 6c 65 20 62 79 20 62 79 s. Enable by by
0ab0: 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 22 30 changing the "0
0ac0: 22 20 74 6f 20 22 31 22 20 61 6e 64 0a 2a 2a 20 " to "1" and.**
0ad0: 72 65 63 6f 6d 70 69 6c 69 6e 67 2e 0a 2a 2a 0a recompiling..**.
0ae0: 2a 2a 20 57 68 65 6e 20 73 71 6c 69 74 65 33 50 ** When sqlite3P
0af0: 63 61 63 68 65 54 72 61 63 65 20 69 73 20 31 2c cacheTrace is 1,
0b00: 20 73 69 6e 67 6c 65 20 6c 69 6e 65 20 74 72 61 single line tra
0b10: 63 65 20 6d 65 73 73 61 67 65 73 20 61 72 65 20 ce messages are
0b20: 69 73 73 75 65 64 2e 0a 2a 2a 20 57 68 65 6e 20 issued..** When
0b30: 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 61 sqlite3PcacheTra
0b40: 63 65 20 69 73 20 32 2c 20 61 20 64 75 6d 70 20 ce is 2, a dump
0b50: 6f 66 20 74 68 65 20 70 63 61 63 68 65 20 73 68 of the pcache sh
0b60: 6f 77 69 6e 67 20 61 6c 6c 20 63 61 63 68 65 20 owing all cache
0b70: 65 6e 74 72 69 65 73 0a 2a 2a 20 69 73 20 64 69 entries.** is di
0b80: 73 70 6c 61 79 65 64 20 66 6f 72 20 6d 61 6e 79 splayed for many
0b90: 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 72 65 73 operations, res
0ba0: 75 6c 74 69 6e 67 20 69 6e 20 61 20 6c 6f 74 20 ulting in a lot
0bb0: 6f 66 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 23 69 of output..*/.#i
0bc0: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
0bd0: 5f 44 45 42 55 47 29 20 26 26 20 30 0a 20 20 69 _DEBUG) && 0. i
0be0: 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
0bf0: 54 72 61 63 65 20 3d 20 32 3b 20 20 20 20 20 20 Trace = 2;
0c00: 20 2f 2a 20 30 3a 20 6f 66 66 20 20 31 3a 20 73 /* 0: off 1: s
0c10: 69 6d 70 6c 65 20 20 32 3a 20 63 61 63 68 65 20 imple 2: cache
0c20: 64 75 6d 70 73 20 2a 2f 0a 20 20 69 6e 74 20 73 dumps */. int s
0c30: 71 6c 69 74 65 33 50 63 61 63 68 65 4d 78 44 75 qlite3PcacheMxDu
0c40: 6d 70 20 3d 20 39 39 39 39 3b 20 20 20 2f 2a 20 mp = 9999; /*
0c50: 4d 61 78 20 63 61 63 68 65 20 65 6e 74 72 69 65 Max cache entrie
0c60: 73 20 66 6f 72 20 70 63 61 63 68 65 44 75 6d 70 s for pcacheDump
0c70: 28 29 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 70 () */.# define p
0c80: 63 61 63 68 65 54 72 61 63 65 28 58 29 20 69 66 cacheTrace(X) if
0c90: 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 (sqlite3PcacheTr
0ca0: 61 63 65 29 7b 73 71 6c 69 74 65 33 44 65 62 75 ace){sqlite3Debu
0cb0: 67 50 72 69 6e 74 66 20 58 3b 7d 0a 20 20 76 6f gPrintf X;}. vo
0cc0: 69 64 20 70 63 61 63 68 65 44 75 6d 70 28 50 43 id pcacheDump(PC
0cd0: 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
0ce0: 20 20 20 69 6e 74 20 4e 3b 0a 20 20 20 20 69 6e int N;. in
0cf0: 74 20 69 2c 20 6a 3b 0a 20 20 20 20 73 71 6c 69 t i, j;. sqli
0d00: 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 20 te3_pcache_page
0d10: 2a 70 4c 6f 77 65 72 3b 0a 20 20 20 20 50 67 48 *pLower;. PgH
0d20: 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 75 6e 73 dr *pPg;. uns
0d30: 69 67 6e 65 64 20 63 68 61 72 20 2a 61 3b 0a 20 igned char *a;.
0d40: 20 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 . if( sqlite
0d50: 33 50 63 61 63 68 65 54 72 61 63 65 3c 32 20 29 3PcacheTrace<2 )
0d60: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 return;. if(
0d70: 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 3d pCache->pCache=
0d80: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
0d90: 20 4e 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 N = sqlite3Pcac
0da0: 68 65 50 61 67 65 63 6f 75 6e 74 28 70 43 61 63 hePagecount(pCac
0db0: 68 65 29 3b 0a 20 20 20 20 69 66 28 20 4e 3e 73 he);. if( N>s
0dc0: 71 6c 69 74 65 33 50 63 61 63 68 65 4d 78 44 75 qlite3PcacheMxDu
0dd0: 6d 70 20 29 20 4e 20 3d 20 73 71 6c 69 74 65 33 mp ) N = sqlite3
0de0: 50 63 61 63 68 65 4d 78 44 75 6d 70 3b 0a 20 20 PcacheMxDump;.
0df0: 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 4e 3b for(i=1; i<=N;
0e00: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 70 4c i++){. pL
0e10: 6f 77 65 72 20 3d 20 73 71 6c 69 74 65 33 47 6c ower = sqlite3Gl
0e20: 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
0e30: 65 32 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 e2.xFetch(pCache
0e40: 2d 3e 70 43 61 63 68 65 2c 20 69 2c 20 30 29 3b ->pCache, i, 0);
0e50: 0a 20 20 20 20 20 20 20 69 66 28 20 70 4c 6f 77 . if( pLow
0e60: 65 72 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 er==0 ) continue
0e70: 3b 0a 20 20 20 20 20 20 20 70 50 67 20 3d 20 28 ;. pPg = (
0e80: 50 67 48 64 72 2a 29 70 4c 6f 77 65 72 2d 3e 70 PgHdr*)pLower->p
0e90: 45 78 74 72 61 3b 0a 20 20 20 20 20 20 20 70 72 Extra;. pr
0ea0: 69 6e 74 66 28 22 25 33 64 3a 20 6e 52 65 66 20 intf("%3d: nRef
0eb0: 25 32 64 20 66 6c 67 73 20 25 30 32 78 20 64 61 %2d flgs %02x da
0ec0: 74 61 20 22 2c 20 69 2c 20 70 50 67 2d 3e 6e 52 ta ", i, pPg->nR
0ed0: 65 66 2c 20 70 50 67 2d 3e 66 6c 61 67 73 29 3b ef, pPg->flags);
0ee0: 0a 20 20 20 20 20 20 20 61 20 3d 20 28 75 6e 73 . a = (uns
0ef0: 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4c 6f igned char *)pLo
0f00: 77 65 72 2d 3e 70 42 75 66 3b 0a 20 20 20 20 20 wer->pBuf;.
0f10: 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 32 3b for(j=0; j<12;
0f20: 20 6a 2b 2b 29 20 70 72 69 6e 74 66 28 22 25 30 j++) printf("%0
0f30: 32 78 22 2c 20 61 5b 6a 5d 29 3b 0a 20 20 20 20 2x", a[j]);.
0f40: 20 20 20 70 72 69 6e 74 66 28 22 5c 6e 22 29 3b printf("\n");
0f50: 0a 20 20 20 20 20 20 20 69 66 28 20 70 50 67 2d . if( pPg-
0f60: 3e 70 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 >pPage==0 ){.
0f70: 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f sqlite3Glo
0f80: 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
0f90: 32 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 2.xUnpin(pCache-
0fa0: 3e 70 43 61 63 68 65 2c 20 70 4c 6f 77 65 72 2c >pCache, pLower,
0fb0: 20 30 29 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 0);. }.
0fc0: 20 20 7d 0a 20 20 7d 0a 20 20 23 65 6c 73 65 0a }. }. #else.
0fd0: 23 20 64 65 66 69 6e 65 20 70 63 61 63 68 65 54 # define pcacheT
0fe0: 72 61 63 65 28 58 29 0a 23 20 64 65 66 69 6e 65 race(X).# define
0ff0: 20 70 63 61 63 68 65 44 75 6d 70 28 58 29 0a 23 pcacheDump(X).#
1000: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 endif../*.** Che
1010: 63 6b 20 69 6e 76 61 72 69 61 6e 74 73 20 6f 6e ck invariants on
1020: 20 61 20 50 67 48 64 72 20 65 6e 74 72 79 2e 20 a PgHdr entry.
1030: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
1040: 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 4f 4b everything is OK
1050: 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 66 61 6c 73 ..** Return fals
1060: 65 20 69 66 20 61 6e 79 20 69 6e 76 61 72 69 61 e if any invaria
1070: 6e 74 20 69 73 20 76 69 6f 6c 61 74 65 64 2e 0a nt is violated..
1080: 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1090: 6e 65 20 69 73 20 66 6f 72 20 75 73 65 20 69 6e ne is for use in
10a0: 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 side of assert()
10b0: 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 statements only
10c0: 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c . For.** exampl
10d0: 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 e:.**.**
10e0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
10f0: 33 50 63 61 63 68 65 50 61 67 65 53 61 6e 69 74 3PcachePageSanit
1100: 79 28 70 50 67 29 20 29 3b 0a 2a 2f 0a 23 69 66 y(pPg) );.*/.#if
1110: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1120: 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 .int sqlite3Pcac
1130: 68 65 50 61 67 65 53 61 6e 69 74 79 28 50 67 48 hePageSanity(PgH
1140: 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 43 61 63 dr *pPg){. PCac
1150: 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20 61 73 he *pCache;. as
1160: 73 65 72 74 28 20 70 50 67 21 3d 30 20 29 3b 0a sert( pPg!=0 );.
1170: 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 assert( pPg->p
1180: 67 6e 6f 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 gno>0 || pPg->pP
1190: 61 67 65 72 3d 3d 30 20 29 3b 20 20 20 20 2f 2a ager==0 ); /*
11a0: 20 50 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 Page number is
11b0: 31 20 6f 72 20 6d 6f 72 65 20 2a 2f 0a 20 20 70 1 or more */. p
11c0: 43 61 63 68 65 20 3d 20 70 50 67 2d 3e 70 43 61 Cache = pPg->pCa
11d0: 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 che;. assert( p
11e0: 43 61 63 68 65 21 3d 30 20 29 3b 20 20 20 20 20 Cache!=0 );
11f0: 20 2f 2a 20 45 76 65 72 79 20 70 61 67 65 20 68 /* Every page h
1200: 61 73 20 61 6e 20 61 73 73 6f 63 69 61 74 65 64 as an associated
1210: 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 69 66 28 PCache */. if(
1220: 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 20 50 47 pPg->flags & PG
1230: 48 44 52 5f 43 4c 45 41 4e 20 29 7b 0a 20 20 20 HDR_CLEAN ){.
1240: 20 61 73 73 65 72 74 28 20 28 70 50 67 2d 3e 66 assert( (pPg->f
1250: 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 lags & PGHDR_DIR
1260: 54 59 29 3d 3d 30 20 29 3b 2f 2a 20 43 61 6e 6e TY)==0 );/* Cann
1270: 6f 74 20 62 65 20 62 6f 74 68 20 43 4c 45 41 4e ot be both CLEAN
1280: 20 61 6e 64 20 44 49 52 54 59 20 2a 2f 0a 20 20 and DIRTY */.
1290: 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
12a0: 2d 3e 70 44 69 72 74 79 21 3d 70 50 67 20 29 3b ->pDirty!=pPg );
12b0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 4c 45 /* CLE
12c0: 41 4e 20 70 61 67 65 73 20 6e 6f 74 20 6f 6e 20 AN pages not on
12d0: 64 69 72 74 79 20 6c 69 73 74 20 2a 2f 0a 20 20 dirty list */.
12e0: 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
12f0: 2d 3e 70 44 69 72 74 79 54 61 69 6c 21 3d 70 50 ->pDirtyTail!=pP
1300: 67 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 57 52 g );. }. /* WR
1310: 49 54 45 41 42 4c 45 20 70 61 67 65 73 20 6d 75 ITEABLE pages mu
1320: 73 74 20 61 6c 73 6f 20 62 65 20 44 49 52 54 59 st also be DIRTY
1330: 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 */. if( pPg->f
1340: 6c 61 67 73 20 26 20 50 47 48 44 52 5f 57 52 49 lags & PGHDR_WRI
1350: 54 45 41 42 4c 45 20 29 7b 0a 20 20 20 20 61 73 TEABLE ){. as
1360: 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 sert( pPg->flags
1370: 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 20 29 & PGHDR_DIRTY )
1380: 3b 20 20 20 20 20 2f 2a 20 57 52 49 54 45 41 42 ; /* WRITEAB
1390: 4c 45 20 69 6d 70 6c 69 65 73 20 44 49 52 54 59 LE implies DIRTY
13a0: 20 2a 2f 0a 20 20 7d 0a 20 20 2f 2a 20 4e 45 45 */. }. /* NEE
13b0: 44 5f 53 59 4e 43 20 63 61 6e 20 62 65 20 73 65 D_SYNC can be se
13c0: 74 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 t independently
13d0: 6f 66 20 57 52 49 54 45 41 42 4c 45 2e 20 20 54 of WRITEABLE. T
13e0: 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 2c 0a his can happen,.
13f0: 20 20 2a 2a 20 66 6f 72 20 65 78 61 6d 70 6c 65 ** for example
1400: 2c 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68 65 , when using the
1410: 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
1420: 74 57 72 69 74 65 28 29 20 6f 70 74 69 6d 69 7a tWrite() optimiz
1430: 61 74 69 6f 6e 3a 0a 20 20 2a 2a 20 20 20 20 28 ation:. ** (
1440: 31 29 20 20 50 61 67 65 20 58 20 69 73 20 6a 6f 1) Page X is jo
1450: 75 72 6e 61 6c 6c 65 64 2c 20 61 6e 64 20 67 65 urnalled, and ge
1460: 74 73 20 57 52 49 54 45 41 42 4c 45 20 61 6e 64 ts WRITEABLE and
1470: 20 4e 45 45 44 5f 53 45 45 4b 2e 0a 20 20 2a 2a NEED_SEEK.. **
1480: 20 20 20 20 28 32 29 20 20 50 61 67 65 20 58 20 (2) Page X
1490: 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 6c 69 73 moved to freelis
14a0: 74 2c 20 57 52 49 54 45 41 42 4c 45 20 69 73 20 t, WRITEABLE is
14b0: 63 6c 65 61 72 65 64 0a 20 20 2a 2a 20 20 20 20 cleared. **
14c0: 28 33 29 20 20 50 61 67 65 20 58 20 72 65 75 73 (3) Page X reus
14d0: 65 64 2c 20 57 52 49 54 45 41 42 4c 45 20 69 73 ed, WRITEABLE is
14e0: 20 73 65 74 20 61 67 61 69 6e 0a 20 20 2a 2a 20 set again. **
14f0: 49 66 20 4e 45 45 44 5f 53 59 4e 43 20 68 61 64 If NEED_SYNC had
1500: 20 62 65 65 6e 20 63 6c 65 61 72 65 64 20 69 6e been cleared in
1510: 20 73 74 65 70 20 32 2c 20 74 68 65 6e 20 69 74 step 2, then it
1520: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 would not be re
1530: 73 65 74 0a 20 20 2a 2a 20 69 6e 20 73 74 65 70 set. ** in step
1540: 20 33 2c 20 61 6e 64 20 70 61 67 65 20 6d 69 67 3, and page mig
1550: 68 74 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e ht be written in
1560: 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1570: 77 69 74 68 6f 75 74 20 66 69 72 73 74 0a 20 20 without first.
1580: 2a 2a 20 73 79 6e 63 69 6e 67 20 74 68 65 20 72 ** syncing the r
1590: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c ollback journal,
15a0: 20 77 68 69 63 68 20 6d 69 67 68 74 20 63 61 75 which might cau
15b0: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f 6e se corruption on
15c0: 20 61 20 70 6f 77 65 72 0a 20 20 2a 2a 20 6c 6f a power. ** lo
15d0: 73 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e ss.. **. ** An
15e0: 6f 74 68 65 72 20 65 78 61 6d 70 6c 65 20 69 73 other example is
15f0: 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 when the databa
1600: 73 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 se page size is
1610: 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 smaller than the
1620: 0a 20 20 2a 2a 20 64 69 73 6b 20 73 65 63 74 6f . ** disk secto
1630: 72 20 73 69 7a 65 2e 20 20 57 68 65 6e 20 61 6e r size. When an
1640: 79 20 70 61 67 65 20 6f 66 20 61 20 73 65 63 74 y page of a sect
1650: 6f 72 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 65 64 or is journalled
1660: 2c 20 61 6c 6c 20 70 61 67 65 73 0a 20 20 2a 2a , all pages. **
1670: 20 69 6e 20 74 68 61 74 20 73 65 63 74 6f 72 20 in that sector
1680: 61 72 65 20 6d 61 72 6b 65 64 20 4e 45 45 44 5f are marked NEED_
1690: 53 59 4e 43 20 65 76 65 6e 20 69 66 20 74 68 65 SYNC even if the
16a0: 79 20 61 72 65 20 73 74 69 6c 6c 20 43 4c 45 41 y are still CLEA
16b0: 4e 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 N, just. ** in
16c0: 63 61 73 65 20 74 68 65 79 20 61 72 65 20 6c 61 case they are la
16d0: 74 65 72 20 6d 6f 64 69 66 69 65 64 2c 20 73 69 ter modified, si
16e0: 6e 63 65 20 61 6c 6c 20 70 61 67 65 73 20 69 6e nce all pages in
16f0: 20 74 68 65 20 73 61 6d 65 20 73 65 63 74 6f 72 the same sector
1700: 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 6a 6f . ** must be jo
1710: 75 72 6e 61 6c 6c 65 64 20 61 6e 64 20 73 79 6e urnalled and syn
1720: 63 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 6f ced before any o
1730: 66 20 74 68 6f 73 65 20 70 61 67 65 73 20 63 61 f those pages ca
1740: 6e 20 62 65 20 73 61 66 65 6c 79 0a 20 20 2a 2a n be safely. **
1750: 20 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 written.. */.
1760: 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
1770: 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 dif /* SQLITE_DE
1780: 42 55 47 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a BUG */.../******
1790: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e ************ Lin
17b0: 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d ked List Managem
17c0: 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ent ************
17d0: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 41 6c ********/../* Al
17e0: 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 lowed values for
17f0: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1800: 20 74 6f 20 70 63 61 63 68 65 4d 61 6e 61 67 65 to pcacheManage
1810: 44 69 72 74 79 4c 69 73 74 28 29 20 2a 2f 0a 23 DirtyList() */.#
1820: 64 65 66 69 6e 65 20 50 43 41 43 48 45 5f 44 49 define PCACHE_DI
1830: 52 54 59 4c 49 53 54 5f 52 45 4d 4f 56 45 20 20 RTYLIST_REMOVE
1840: 20 31 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 1 /* Remove
1850: 70 50 61 67 65 20 66 72 6f 6d 20 64 69 72 74 79 pPage from dirty
1860: 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 list */.#define
1870: 20 50 43 41 43 48 45 5f 44 49 52 54 59 4c 49 53 PCACHE_DIRTYLIS
1880: 54 5f 41 44 44 20 20 20 20 20 20 32 20 20 20 20 T_ADD 2
1890: 2f 2a 20 41 64 64 20 70 50 61 67 65 20 74 6f 20 /* Add pPage to
18a0: 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 2a the dirty list *
18b0: 2f 0a 23 64 65 66 69 6e 65 20 50 43 41 43 48 45 /.#define PCACHE
18c0: 5f 44 49 52 54 59 4c 49 53 54 5f 46 52 4f 4e 54 _DIRTYLIST_FRONT
18d0: 20 20 20 20 33 20 20 20 20 2f 2a 20 4d 6f 76 65 3 /* Move
18e0: 20 70 50 61 67 65 20 74 6f 20 74 68 65 20 66 72 pPage to the fr
18f0: 6f 6e 74 20 6f 66 20 74 68 65 20 6c 69 73 74 20 ont of the list
1900: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 61 67 65 */../*.** Manage
1910: 20 70 50 61 67 65 27 73 20 70 61 72 74 69 63 69 pPage's partici
1920: 70 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 64 69 pation on the di
1930: 72 74 79 20 6c 69 73 74 2e 20 20 42 69 74 73 20 rty list. Bits
1940: 6f 66 20 74 68 65 20 61 64 64 52 65 6d 6f 76 65 of the addRemove
1950: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 64 65 74 .** argument det
1960: 65 72 6d 69 6e 65 73 20 77 68 61 74 20 6f 70 65 ermines what ope
1970: 72 61 74 69 6f 6e 20 74 6f 20 64 6f 2e 20 20 54 ration to do. T
1980: 68 65 20 30 78 30 31 20 62 69 74 20 6d 65 61 6e he 0x01 bit mean
1990: 73 20 66 69 72 73 74 0a 2a 2a 20 72 65 6d 6f 76 s first.** remov
19a0: 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 e pPage from the
19b0: 20 64 69 72 74 79 20 6c 69 73 74 2e 20 20 54 68 dirty list. Th
19c0: 65 20 30 78 30 32 20 6d 65 61 6e 73 20 61 64 64 e 0x02 means add
19d0: 20 70 50 61 67 65 20 62 61 63 6b 20 74 6f 0a 2a pPage back to.*
19e0: 2a 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 * the dirty list
19f0: 2e 20 20 44 6f 69 6e 67 20 62 6f 74 68 20 6d 6f . Doing both mo
1a00: 76 65 73 20 70 50 61 67 65 20 74 6f 20 74 68 65 ves pPage to the
1a10: 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20 64 69 front of the di
1a20: 72 74 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 rty list..*/.sta
1a30: 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 4d tic void pcacheM
1a40: 61 6e 61 67 65 44 69 72 74 79 4c 69 73 74 28 50 anageDirtyList(P
1a50: 67 48 64 72 20 2a 70 50 61 67 65 2c 20 75 38 20 gHdr *pPage, u8
1a60: 61 64 64 52 65 6d 6f 76 65 29 7b 0a 20 20 50 43 addRemove){. PC
1a70: 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d ache *p = pPage-
1a80: 3e 70 43 61 63 68 65 3b 0a 0a 20 20 70 63 61 63 >pCache;.. pcac
1a90: 68 65 54 72 61 63 65 28 28 22 25 70 2e 44 49 52 heTrace(("%p.DIR
1aa0: 54 59 4c 49 53 54 2e 25 73 20 25 64 5c 6e 22 2c TYLIST.%s %d\n",
1ab0: 20 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 p,.
1ac0: 20 20 20 20 61 64 64 52 65 6d 6f 76 65 3d 3d 31 addRemove==1
1ad0: 20 3f 20 22 52 45 4d 4f 56 45 22 20 3a 20 61 64 ? "REMOVE" : ad
1ae0: 64 52 65 6d 6f 76 65 3d 3d 32 20 3f 20 22 41 44 dRemove==2 ? "AD
1af0: 44 22 20 3a 20 22 46 52 4f 4e 54 22 2c 0a 20 20 D" : "FRONT",.
1b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
1b10: 61 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 69 age->pgno));. i
1b20: 66 28 20 61 64 64 52 65 6d 6f 76 65 20 26 20 50 f( addRemove & P
1b30: 43 41 43 48 45 5f 44 49 52 54 59 4c 49 53 54 5f CACHE_DIRTYLIST_
1b40: 52 45 4d 4f 56 45 20 29 7b 0a 20 20 20 20 61 73 REMOVE ){. as
1b50: 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
1b60: 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 rtyNext || pPage
1b70: 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 ==p->pDirtyTail
1b80: 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1b90: 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 Page->pDirtyPrev
1ba0: 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 || pPage==p->pD
1bb0: 69 72 74 79 20 29 3b 0a 20 20 0a 20 20 20 20 2f irty );. . /
1bc0: 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 * Update the PCa
1bd0: 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 che1.pSynced var
1be0: 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 iable if necessa
1bf0: 72 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ry. */. if( p
1c00: 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 ->pSynced==pPage
1c10: 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 53 79 ){. p->pSy
1c20: 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 nced = pPage->pD
1c30: 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a irtyPrev;. }.
1c40: 20 20 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
1c50: 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a ->pDirtyNext ){.
1c60: 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 pPage->pDi
1c70: 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 rtyNext->pDirtyP
1c80: 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 rev = pPage->pDi
1c90: 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 65 6c rtyPrev;. }el
1ca0: 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
1cb0: 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 ( pPage==p->pDir
1cc0: 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 20 20 20 tyTail );.
1cd0: 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 p->pDirtyTail =
1ce0: 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
1cf0: 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 v;. }. if(
1d00: 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
1d10: 65 76 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 ev ){. pPag
1d20: 65 2d 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70 e->pDirtyPrev->p
1d30: 44 69 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67 DirtyNext = pPag
1d40: 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 e->pDirtyNext;.
1d50: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d60: 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 /* If there are
1d70: 6e 6f 77 20 6e 6f 20 64 69 72 74 79 20 70 61 67 now no dirty pag
1d80: 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c es in the cache,
1d90: 20 73 65 74 20 65 43 72 65 61 74 65 20 74 6f 20 set eCreate to
1da0: 32 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 2. . ** Thi
1db0: 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 s is an optimiza
1dc0: 74 69 6f 6e 20 74 68 61 74 20 61 6c 6c 6f 77 73 tion that allows
1dd0: 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 sqlite3PcacheFe
1de0: 74 63 68 28 29 20 74 6f 20 73 6b 69 70 0a 20 20 tch() to skip.
1df0: 20 20 20 20 2a 2a 20 73 65 61 72 63 68 69 6e 67 ** searching
1e00: 20 66 6f 72 20 61 20 64 69 72 74 79 20 70 61 67 for a dirty pag
1e10: 65 20 74 6f 20 65 6a 65 63 74 20 66 72 6f 6d 20 e to eject from
1e20: 74 68 65 20 63 61 63 68 65 20 77 68 65 6e 20 69 the cache when i
1e30: 74 20 6d 69 67 68 74 0a 20 20 20 20 20 20 2a 2a t might. **
1e40: 20 6f 74 68 65 72 77 69 73 65 20 68 61 76 65 20 otherwise have
1e50: 74 6f 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 to. */. as
1e60: 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e sert( pPage==p->
1e70: 70 44 69 72 74 79 20 29 3b 0a 20 20 20 20 20 20 pDirty );.
1e80: 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67 p->pDirty = pPag
1e90: 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 e->pDirtyNext;.
1ea0: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
1eb0: 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 70 2d bPurgeable || p-
1ec0: 3e 65 43 72 65 61 74 65 3d 3d 32 20 29 3b 0a 20 >eCreate==2 );.
1ed0: 20 20 20 20 20 69 66 28 20 70 2d 3e 70 44 69 72 if( p->pDir
1ee0: 74 79 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 ty==0 ){
1ef0: 20 2f 2a 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 2d /*OPTIMIZATION-
1f00: 49 46 2d 54 52 55 45 2a 2f 0a 20 20 20 20 20 20 IF-TRUE*/.
1f10: 20 20 61 73 73 65 72 74 28 20 70 2d 3e 62 50 75 assert( p->bPu
1f20: 72 67 65 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d rgeable==0 || p-
1f30: 3e 65 43 72 65 61 74 65 3d 3d 31 20 29 3b 0a 20 >eCreate==1 );.
1f40: 20 20 20 20 20 20 20 70 2d 3e 65 43 72 65 61 74 p->eCreat
1f50: 65 20 3d 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 e = 2;. }.
1f60: 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 61 }. }. if( a
1f70: 64 64 52 65 6d 6f 76 65 20 26 20 50 43 41 43 48 ddRemove & PCACH
1f80: 45 5f 44 49 52 54 59 4c 49 53 54 5f 41 44 44 20 E_DIRTYLIST_ADD
1f90: 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 ){. pPage->pD
1fa0: 69 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 20 20 irtyPrev = 0;.
1fb0: 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e pPage->pDirtyN
1fc0: 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b ext = p->pDirty;
1fd0: 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
1fe0: 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 pDirtyNext ){.
1ff0: 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
2000: 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 e->pDirtyNext->p
2010: 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a DirtyPrev==0 );.
2020: 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 pPage->pDi
2030: 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 rtyNext->pDirtyP
2040: 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 rev = pPage;.
2050: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d }else{. p-
2060: 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 70 50 >pDirtyTail = pP
2070: 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 age;. if( p
2080: 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a ->bPurgeable ){.
2090: 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
20a0: 70 2d 3e 65 43 72 65 61 74 65 3d 3d 32 20 29 3b p->eCreate==2 );
20b0: 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 43 72 65 . p->eCre
20c0: 61 74 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d ate = 1;. }
20d0: 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 44 . }. p->pD
20e0: 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 0a 20 irty = pPage;..
20f0: 20 20 20 2f 2a 20 49 66 20 70 53 79 6e 63 65 64 /* If pSynced
2100: 20 69 73 20 4e 55 4c 4c 20 61 6e 64 20 74 68 69 is NULL and thi
2110: 73 20 70 61 67 65 20 68 61 73 20 61 20 63 6c 65 s page has a cle
2120: 61 72 20 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 ar NEED_SYNC fla
2130: 67 2c 20 73 65 74 0a 20 20 20 20 2a 2a 20 70 53 g, set. ** pS
2140: 79 6e 63 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 ynced to point t
2150: 6f 20 69 74 2e 20 43 68 65 63 6b 69 6e 67 20 74 o it. Checking t
2160: 68 65 20 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 he NEED_SYNC fla
2170: 67 20 69 73 20 61 6e 20 0a 20 20 20 20 2a 2a 20 g is an . **
2180: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 61 73 optimization, as
2190: 20 69 66 20 70 53 79 6e 63 65 64 20 70 6f 69 6e if pSynced poin
21a0: 74 73 20 74 6f 20 61 20 70 61 67 65 20 77 69 74 ts to a page wit
21b0: 68 20 74 68 65 20 4e 45 45 44 5f 53 59 4e 43 0a h the NEED_SYNC.
21c0: 20 20 20 20 2a 2a 20 66 6c 61 67 20 73 65 74 20 ** flag set
21d0: 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
21e0: 63 68 53 74 72 65 73 73 28 29 20 73 65 61 72 63 chStress() searc
21f0: 68 65 73 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 hes through all
2200: 6e 65 77 65 72 20 0a 20 20 20 20 2a 2a 20 65 6e newer . ** en
2210: 74 72 69 65 73 20 6f 66 20 74 68 65 20 64 69 72 tries of the dir
2220: 74 79 2d 6c 69 73 74 20 66 6f 72 20 61 20 70 61 ty-list for a pa
2230: 67 65 20 77 69 74 68 20 4e 45 45 44 5f 53 59 4e ge with NEED_SYN
2240: 43 20 63 6c 65 61 72 20 61 6e 79 77 61 79 2e 20 C clear anyway.
2250: 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 2d 3e */. if( !p->
2260: 70 53 79 6e 63 65 64 20 0a 20 20 20 20 20 26 26 pSynced . &&
2270: 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 0==(pPage->flag
2280: 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
2290: 43 29 20 20 20 2f 2a 4f 50 54 49 4d 49 5a 41 54 C) /*OPTIMIZAT
22a0: 49 4f 4e 2d 49 46 2d 46 41 4c 53 45 2a 2f 0a 20 ION-IF-FALSE*/.
22b0: 20 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 ){. p->p
22c0: 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a Synced = pPage;.
22d0: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 63 61 63 }. }. pcac
22e0: 68 65 44 75 6d 70 28 70 29 3b 0a 7d 0a 0a 2f 2a heDump(p);.}../*
22f0: 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 6f 75 .** Wrapper arou
2300: 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 6c 65 nd the pluggable
2310: 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e 20 6d caches xUnpin m
2320: 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 63 61 ethod. If the ca
2330: 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 che is.** being
2340: 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d used for an in-m
2350: 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 emory database,
2360: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
2370: 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
2380: 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 55 tic void pcacheU
2390: 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a npin(PgHdr *p){.
23a0: 20 20 69 66 28 20 70 2d 3e 70 43 61 63 68 65 2d if( p->pCache-
23b0: 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 >bPurgeable ){.
23c0: 20 20 20 70 63 61 63 68 65 54 72 61 63 65 28 28 pcacheTrace((
23d0: 22 25 70 2e 55 4e 50 49 4e 20 25 64 5c 6e 22 2c "%p.UNPIN %d\n",
23e0: 20 70 2d 3e 70 43 61 63 68 65 2c 20 70 2d 3e 70 p->pCache, p->p
23f0: 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 gno));. sqlit
2400: 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
2410: 63 61 63 68 65 32 2e 78 55 6e 70 69 6e 28 70 2d cache2.xUnpin(p-
2420: 3e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c >pCache->pCache,
2430: 20 70 2d 3e 70 50 61 67 65 2c 20 30 29 3b 0a 20 p->pPage, 0);.
2440: 20 20 20 70 63 61 63 68 65 44 75 6d 70 28 70 2d pcacheDump(p-
2450: 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a 7d 0a >pCache);. }.}.
2460: 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
2470: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
2480: 65 73 20 6f 66 20 63 61 63 68 65 20 72 65 71 75 es of cache requ
2490: 65 73 74 65 64 2e 20 20 20 70 2d 3e 73 7a 43 61 ested. p->szCa
24a0: 63 68 65 20 69 73 20 74 68 65 0a 2a 2a 20 63 61 che is the.** ca
24b0: 63 68 65 20 73 69 7a 65 20 72 65 71 75 65 73 74 che size request
24c0: 65 64 20 62 79 20 74 68 65 20 22 50 52 41 47 4d ed by the "PRAGM
24d0: 41 20 63 61 63 68 65 5f 73 69 7a 65 22 20 73 74 A cache_size" st
24e0: 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 atement..*/.stat
24f0: 69 63 20 69 6e 74 20 6e 75 6d 62 65 72 4f 66 43 ic int numberOfC
2500: 61 63 68 65 50 61 67 65 73 28 50 43 61 63 68 65 achePages(PCache
2510: 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 *p){. if( p->s
2520: 7a 43 61 63 68 65 3e 3d 30 20 29 7b 0a 20 20 20 zCache>=0 ){.
2530: 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 /* IMPLEMENTATI
2540: 4f 4e 2d 4f 46 3a 20 52 2d 34 32 30 35 39 2d 34 ON-OF: R-42059-4
2550: 37 32 31 31 20 49 66 20 74 68 65 20 61 72 67 75 7211 If the argu
2560: 6d 65 6e 74 20 4e 20 69 73 20 70 6f 73 69 74 69 ment N is positi
2570: 76 65 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 ve then the.
2580: 2a 2a 20 73 75 67 67 65 73 74 65 64 20 63 61 63 ** suggested cac
2590: 68 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 74 he size is set t
25a0: 6f 20 4e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 o N. */. retu
25b0: 72 6e 20 70 2d 3e 73 7a 43 61 63 68 65 3b 0a 20 rn p->szCache;.
25c0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
25d0: 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 MPLEMENTATION-OF
25e0: 3a 20 52 2d 36 31 34 33 36 2d 31 33 36 33 39 20 : R-61436-13639
25f0: 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 If the argument
2600: 4e 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 N is negative, t
2610: 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e hen. ** the n
2620: 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65 20 70 umber of cache p
2630: 61 67 65 73 20 69 73 20 61 64 6a 75 73 74 65 64 ages is adjusted
2640: 20 74 6f 20 75 73 65 20 61 70 70 72 6f 78 69 6d to use approxim
2650: 61 74 65 6c 79 20 61 62 73 28 4e 2a 31 30 32 34 ately abs(N*1024
2660: 29 0a 20 20 20 20 2a 2a 20 62 79 74 65 73 20 6f ). ** bytes o
2670: 66 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 20 f memory. */.
2680: 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 28 2d return (int)((-
2690: 31 30 32 34 2a 28 69 36 34 29 70 2d 3e 73 7a 43 1024*(i64)p->szC
26a0: 61 63 68 65 29 2f 28 70 2d 3e 73 7a 50 61 67 65 ache)/(p->szPage
26b0: 2b 70 2d 3e 73 7a 45 78 74 72 61 29 29 3b 0a 20 +p->szExtra));.
26c0: 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
26d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
26e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
26f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 ********** Gener
2700: 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a al Interfaces **
2710: 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 ****.**.** Initi
2720: 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f alize and shutdo
2730: 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 wn the page cach
2740: 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 e subsystem. Nei
2750: 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a ther of these .*
2760: 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 * functions are
2770: 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 69 threadsafe..*/.i
2780: 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
2790: 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 Initialize(void)
27a0: 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 {. if( sqlite3G
27b0: 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
27c0: 68 65 32 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a he2.xInit==0 ){.
27d0: 20 20 20 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 /* IMPLEMENT
27e0: 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 32 36 38 30 ATION-OF: R-2680
27f0: 31 2d 36 34 31 33 37 20 49 66 20 74 68 65 20 78 1-64137 If the x
2800: 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 Init() method is
2810: 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a NULL, then the.
2820: 20 20 20 20 2a 2a 20 62 75 69 6c 74 2d 69 6e 20 ** built-in
2830: 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 default page cac
2840: 68 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 he is used inste
2850: 61 64 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 ad of the applic
2860: 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 0a 20 20 ation defined.
2870: 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 2e ** page cache.
2880: 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 */. sqlite3P
2890: 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 CacheSetDefault(
28a0: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
28b0: 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
28c0: 66 69 67 2e 70 63 61 63 68 65 32 2e 78 49 6e 69 fig.pcache2.xIni
28d0: 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 t(sqlite3GlobalC
28e0: 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 70 41 onfig.pcache2.pA
28f0: 72 67 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c 69 rg);.}.void sqli
2900: 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 te3PcacheShutdow
2910: 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 n(void){. if( s
2920: 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
2930: 69 67 2e 70 63 61 63 68 65 32 2e 78 53 68 75 74 ig.pcache2.xShut
2940: 64 6f 77 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 49 down ){. /* I
2950: 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 MPLEMENTATION-OF
2960: 3a 20 52 2d 32 36 30 30 30 2d 35 36 35 38 39 20 : R-26000-56589
2970: 54 68 65 20 78 53 68 75 74 64 6f 77 6e 28 29 20 The xShutdown()
2980: 6d 65 74 68 6f 64 20 6d 61 79 20 62 65 20 4e 55 method may be NU
2990: 4c 4c 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 LL. */. sqlit
29a0: 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
29b0: 63 61 63 68 65 32 2e 78 53 68 75 74 64 6f 77 6e cache2.xShutdown
29c0: 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
29d0: 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 70 41 72 nfig.pcache2.pAr
29e0: 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a g);. }.}../*.**
29f0: 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
2a00: 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 in bytes of a P
2a10: 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f Cache object..*/
2a20: 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 .int sqlite3Pcac
2a30: 68 65 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 heSize(void){ re
2a40: 74 75 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 turn sizeof(PCac
2a50: 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 he); }../*.** Cr
2a60: 65 61 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 eate a new PCach
2a70: 65 20 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 e object. Storag
2a80: 65 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 e space to hold
2a90: 74 68 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 the object.** ha
2aa0: 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 s already been a
2ab0: 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 llocated and is
2ac0: 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 passed in as the
2ad0: 20 70 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 p pointer. .**
2ae0: 54 68 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f The caller disco
2af0: 76 65 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 vers how much sp
2b00: 61 63 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ace needs to be
2b10: 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a allocated by .**
2b20: 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 calling sqlite3
2b30: 50 63 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2a PcacheSize()..**
2b40: 0a 2a 2a 20 73 7a 45 78 74 72 61 20 69 73 20 73 .** szExtra is s
2b50: 6f 6d 65 20 65 78 74 72 61 20 73 70 61 63 65 20 ome extra space
2b60: 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 allocated for ea
2b70: 63 68 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 ch page. The fi
2b80: 72 73 74 0a 2a 2a 20 38 20 62 79 74 65 73 20 6f rst.** 8 bytes o
2b90: 66 20 74 68 65 20 65 78 74 72 61 20 73 70 61 63 f the extra spac
2ba0: 65 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 e will be zeroed
2bb0: 20 61 73 20 74 68 65 20 70 61 67 65 20 69 73 20 as the page is
2bc0: 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 62 75 allocated,.** bu
2bd0: 74 20 72 65 6d 61 69 6e 69 6e 67 20 63 6f 6e 74 t remaining cont
2be0: 65 6e 74 20 77 69 6c 6c 20 62 65 20 75 6e 69 6e ent will be unin
2bf0: 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 6f 75 itialized. Thou
2c00: 67 68 20 69 74 20 69 73 20 6f 70 61 71 75 65 0a gh it is opaque.
2c10: 2a 2a 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c ** to this modul
2c20: 65 2c 20 74 68 65 20 65 78 74 72 61 20 73 70 61 e, the extra spa
2c30: 63 65 20 72 65 61 6c 6c 79 20 65 6e 64 73 20 75 ce really ends u
2c40: 70 20 62 65 69 6e 67 20 74 68 65 20 4d 65 6d 50 p being the MemP
2c50: 61 67 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 age.** structure
2c60: 20 69 6e 20 74 68 65 20 70 61 67 65 72 2e 0a 2a in the pager..*
2c70: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 /.int sqlite3Pca
2c80: 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 cheOpen(. int s
2c90: 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 zPage,
2ca0: 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
2cb0: 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f of every page */
2cc0: 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 . int szExtra,
2cd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ce0: 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 /* Extra space a
2cf0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 ssociated with e
2d00: 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ach page */. in
2d10: 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 t bPurgeable,
2d20: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
2d30: 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 ue if pages are
2d40: 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 on backing store
2d50: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 */. int (*xStr
2d60: 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 ess)(void*,PgHdr
2d70: 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 *),/* Call to tr
2d80: 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 y to make pages
2d90: 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 clean */. void
2da0: 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 20 20 *pStress,
2db0: 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
2dc0: 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a ent to xStress *
2dd0: 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 20 20 20 /. PCache *p
2de0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2df0: 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 /* Preallocated
2e00: 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 50 space for the P
2e10: 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 Cache */.){. me
2e20: 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f mset(p, 0, sizeo
2e30: 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20 70 2d f(PCache));. p-
2e40: 3e 73 7a 50 61 67 65 20 3d 20 31 3b 0a 20 20 70 >szPage = 1;. p
2e50: 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a 45 78 ->szExtra = szEx
2e60: 74 72 61 3b 0a 20 20 61 73 73 65 72 74 28 20 73 tra;. assert( s
2e70: 7a 45 78 74 72 61 3e 3d 38 20 29 3b 20 20 2f 2a zExtra>=8 ); /*
2e80: 20 46 69 72 73 74 20 38 20 62 79 74 65 73 20 77 First 8 bytes w
2e90: 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 20 2a 2f ill be zeroed */
2ea0: 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 . p->bPurgeable
2eb0: 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 = bPurgeable;.
2ec0: 20 70 2d 3e 65 43 72 65 61 74 65 20 3d 20 32 3b p->eCreate = 2;
2ed0: 0a 20 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 . p->xStress =
2ee0: 78 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 xStress;. p->pS
2ef0: 74 72 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b tress = pStress;
2f00: 0a 20 20 70 2d 3e 73 7a 43 61 63 68 65 20 3d 20 . p->szCache =
2f10: 31 30 30 3b 0a 20 20 70 2d 3e 73 7a 53 70 69 6c 100;. p->szSpil
2f20: 6c 20 3d 20 31 3b 0a 20 20 70 63 61 63 68 65 54 l = 1;. pcacheT
2f30: 72 61 63 65 28 28 22 25 70 2e 4f 50 45 4e 20 73 race(("%p.OPEN s
2f40: 7a 50 61 67 65 20 25 64 20 62 50 75 72 67 65 61 zPage %d bPurgea
2f50: 62 6c 65 20 25 64 5c 6e 22 2c 70 2c 73 7a 50 61 ble %d\n",p,szPa
2f60: 67 65 2c 62 50 75 72 67 65 61 62 6c 65 29 29 3b ge,bPurgeable));
2f70: 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
2f80: 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
2f90: 7a 65 28 70 2c 20 73 7a 50 61 67 65 29 3b 0a 7d ze(p, szPage);.}
2fa0: 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
2fb0: 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 he page size for
2fc0: 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 PCache object.
2fd0: 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
2fe0: 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 72 ensure that ther
2ff0: 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 e.** are no outs
3000: 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 tanding page ref
3010: 65 72 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 erences when thi
3020: 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
3030: 6c 6c 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c lled..*/.int sql
3040: 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 ite3PcacheSetPag
3050: 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 eSize(PCache *pC
3060: 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 ache, int szPage
3070: 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 ){. assert( pCa
3080: 63 68 65 2d 3e 6e 52 65 66 53 75 6d 3d 3d 30 20 che->nRefSum==0
3090: 26 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 && pCache->pDirt
30a0: 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 y==0 );. if( pC
30b0: 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a ache->szPage ){.
30c0: 20 20 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 sqlite3_pcac
30d0: 68 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e he *pNew;. pN
30e0: 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ew = sqlite3Glob
30f0: 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 alConfig.pcache2
3100: 2e 78 43 72 65 61 74 65 28 0a 20 20 20 20 20 20 .xCreate(.
3110: 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 szPage
3120: 2c 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 , pCache->szExtr
3130: 61 20 2b 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f a + ROUND8(sizeo
3140: 66 28 50 67 48 64 72 29 29 2c 0a 20 20 20 20 20 f(PgHdr)),.
3150: 20 20 20 20 20 20 20 20 20 20 20 70 43 61 63 68 pCach
3160: 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 0a 20 20 e->bPurgeable.
3170: 20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 );. if( pNe
3180: 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 w==0 ) return SQ
3190: 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b LITE_NOMEM_BKPT;
31a0: 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
31b0: 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 alConfig.pcache2
31c0: 2e 78 43 61 63 68 65 73 69 7a 65 28 70 4e 65 77 .xCachesize(pNew
31d0: 2c 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50 , numberOfCacheP
31e0: 61 67 65 73 28 70 43 61 63 68 65 29 29 3b 0a 20 ages(pCache));.
31f0: 20 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 if( pCache->p
3200: 43 61 63 68 65 20 29 7b 0a 20 20 20 20 20 20 73 Cache ){. s
3210: 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
3220: 69 67 2e 70 63 61 63 68 65 32 2e 78 44 65 73 74 ig.pcache2.xDest
3230: 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 roy(pCache->pCac
3240: 68 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 he);. }. p
3250: 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 Cache->pCache =
3260: 70 4e 65 77 3b 0a 20 20 20 20 70 43 61 63 68 65 pNew;. pCache
3270: 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 ->szPage = szPag
3280: 65 3b 0a 20 20 20 20 70 63 61 63 68 65 54 72 61 e;. pcacheTra
3290: 63 65 28 28 22 25 70 2e 50 41 47 45 53 49 5a 45 ce(("%p.PAGESIZE
32a0: 20 25 64 5c 6e 22 2c 70 43 61 63 68 65 2c 73 7a %d\n",pCache,sz
32b0: 50 61 67 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 Page));. }. re
32c0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
32d0: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 }../*.** Try to
32e0: 6f 62 74 61 69 6e 20 61 20 70 61 67 65 20 66 72 obtain a page fr
32f0: 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a om the cache..**
3300: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
3310: 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
3320: 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 er to an sqlite3
3330: 5f 70 63 61 63 68 65 5f 70 61 67 65 20 6f 62 6a _pcache_page obj
3340: 65 63 74 20 69 66 0a 2a 2a 20 73 75 63 68 20 61 ect if.** such a
3350: 6e 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 72 65 n object is alre
3360: 61 64 79 20 69 6e 20 63 61 63 68 65 2c 20 6f 72 ady in cache, or
3370: 20 69 66 20 61 20 6e 65 77 20 6f 6e 65 20 69 73 if a new one is
3380: 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20 54 68 69 created..** Thi
3390: 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
33a0: 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
33b0: 20 69 66 20 74 68 65 20 6f 62 6a 65 63 74 20 77 if the object w
33c0: 61 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 0a as not in cache.
33d0: 2a 2a 20 61 6e 64 20 63 6f 75 6c 64 20 6e 6f 74 ** and could not
33e0: 20 62 65 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a be created..**.
33f0: 2a 2a 20 54 68 65 20 63 72 65 61 74 65 46 6c 61 ** The createFla
3400: 67 73 20 73 68 6f 75 6c 64 20 62 65 20 30 20 74 gs should be 0 t
3410: 6f 20 63 68 65 63 6b 20 66 6f 72 20 65 78 69 73 o check for exis
3420: 74 69 6e 67 20 70 61 67 65 73 20 61 6e 64 20 73 ting pages and s
3430: 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 33 20 28 6e hould.** be 3 (n
3440: 6f 74 20 31 2c 20 62 75 74 20 33 29 20 74 6f 20 ot 1, but 3) to
3450: 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 61 20 try to create a
3460: 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 new page..**.**
3470: 49 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 If the createFla
3480: 67 20 69 73 20 30 2c 20 74 68 65 6e 20 4e 55 4c g is 0, then NUL
3490: 4c 20 69 73 20 61 6c 77 61 79 73 20 72 65 74 75 L is always retu
34a0: 72 6e 65 64 20 69 66 20 74 68 65 20 70 61 67 65 rned if the page
34b0: 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 .** is not alrea
34c0: 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e dy in the cache.
34d0: 20 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 If createFlag
34e0: 69 73 20 31 2c 20 74 68 65 6e 20 61 20 6e 65 77 is 1, then a new
34f0: 20 70 61 67 65 0a 2a 2a 20 69 73 20 63 72 65 61 page.** is crea
3500: 74 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 61 74 ted only if that
3510: 20 63 61 6e 20 62 65 20 64 6f 6e 65 20 77 69 74 can be done wit
3520: 68 6f 75 74 20 73 70 69 6c 6c 69 6e 67 20 64 69 hout spilling di
3530: 72 74 79 20 70 61 67 65 73 0a 2a 2a 20 61 6e 64 rty pages.** and
3540: 20 77 69 74 68 6f 75 74 20 65 78 63 65 65 64 69 without exceedi
3550: 6e 67 20 74 68 65 20 63 61 63 68 65 20 73 69 7a ng the cache siz
3560: 65 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 e limit..**.** T
3570: 68 65 20 63 61 6c 6c 65 72 20 6e 65 65 64 73 20 he caller needs
3580: 74 6f 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 to invoke sqlite
3590: 33 50 63 61 63 68 65 46 65 74 63 68 46 69 6e 69 3PcacheFetchFini
35a0: 73 68 28 29 20 74 6f 20 70 72 6f 70 65 72 6c 79 sh() to properly
35b0: 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 .** initialize t
35c0: 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
35d0: 65 5f 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6e e_page object an
35e0: 64 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 d convert it int
35f0: 6f 20 61 0a 2a 2a 20 50 67 48 64 72 20 6f 62 6a o a.** PgHdr obj
3600: 65 63 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ect. The sqlite
3610: 33 50 63 61 63 68 65 46 65 74 63 68 28 29 20 61 3PcacheFetch() a
3620: 6e 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nd sqlite3Pcache
3630: 46 65 74 63 68 46 69 6e 69 73 68 28 29 0a 2a 2a FetchFinish().**
3640: 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 73 70 routines are sp
3650: 6c 69 74 20 74 68 69 73 20 77 61 79 20 66 6f 72 lit this way for
3660: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 61 performance rea
3670: 73 6f 6e 73 2e 20 57 68 65 6e 20 73 65 70 61 72 sons. When separ
3680: 61 74 65 64 0a 2a 2a 20 74 68 65 79 20 63 61 6e ated.** they can
3690: 20 62 6f 74 68 20 28 75 73 75 61 6c 6c 79 29 20 both (usually)
36a0: 6f 70 65 72 61 74 65 20 77 69 74 68 6f 75 74 20 operate without
36b0: 68 61 76 69 6e 67 20 74 6f 20 70 75 73 68 20 76 having to push v
36c0: 61 6c 75 65 73 20 74 6f 0a 2a 2a 20 74 68 65 20 alues to.** the
36d0: 73 74 61 63 6b 20 6f 6e 20 65 6e 74 72 79 20 61 stack on entry a
36e0: 6e 64 20 70 6f 70 20 74 68 65 6d 20 62 61 63 6b nd pop them back
36f0: 20 6f 66 66 20 6f 6e 20 65 78 69 74 2c 20 77 68 off on exit, wh
3700: 69 63 68 20 73 61 76 65 73 20 61 0a 2a 2a 20 6c ich saves a.** l
3710: 6f 74 20 6f 66 20 70 75 73 68 69 6e 67 20 61 6e ot of pushing an
3720: 64 20 70 6f 70 70 69 6e 67 2e 0a 2a 2f 0a 73 71 d popping..*/.sq
3730: 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 lite3_pcache_pag
3740: 65 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 e *sqlite3Pcache
3750: 46 65 74 63 68 28 0a 20 20 50 43 61 63 68 65 20 Fetch(. PCache
3760: 2a 70 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f *pCache, /
3770: 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 * Obtain the pag
3780: 65 20 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68 e from this cach
3790: 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
37a0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
37b0: 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f Page number to o
37c0: 62 74 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 btain */. int c
37d0: 72 65 61 74 65 46 6c 61 67 20 20 20 20 20 20 20 reateFlag
37e0: 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65 /* If true, cre
37f0: 61 74 65 20 70 61 67 65 20 69 66 20 69 74 20 64 ate page if it d
3800: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c oes not exist al
3810: 72 65 61 64 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e ready */.){. in
3820: 74 20 65 43 72 65 61 74 65 3b 0a 20 20 73 71 6c t eCreate;. sql
3830: 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 ite3_pcache_page
3840: 20 2a 70 52 65 73 3b 0a 0a 20 20 61 73 73 65 72 *pRes;.. asser
3850: 74 28 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a t( pCache!=0 );.
3860: 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
3870: 2d 3e 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 ->pCache!=0 );.
3880: 20 61 73 73 65 72 74 28 20 63 72 65 61 74 65 46 assert( createF
3890: 6c 61 67 3d 3d 33 20 7c 7c 20 63 72 65 61 74 65 lag==3 || create
38a0: 46 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Flag==0 );. ass
38b0: 65 72 74 28 20 70 43 61 63 68 65 2d 3e 65 43 72 ert( pCache->eCr
38c0: 65 61 74 65 3d 3d 28 28 70 43 61 63 68 65 2d 3e eate==((pCache->
38d0: 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 70 43 bPurgeable && pC
38e0: 61 63 68 65 2d 3e 70 44 69 72 74 79 29 20 3f 20 ache->pDirty) ?
38f0: 31 20 3a 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 1 : 2) );.. /*
3900: 65 43 72 65 61 74 65 20 64 65 66 69 6e 65 73 20 eCreate defines
3910: 77 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74 68 what to do if th
3920: 65 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 20 e page does not
3930: 65 78 69 73 74 2e 0a 20 20 2a 2a 20 20 20 20 30 exist.. ** 0
3940: 20 20 20 20 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f Do not allo
3950: 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e cate a new page.
3960: 20 20 28 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 (createFlag==0
3970: 29 0a 20 20 2a 2a 20 20 20 20 31 20 20 20 20 20 ). ** 1
3980: 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
3990: 61 67 65 20 69 66 20 64 6f 69 6e 67 20 73 6f 20 age if doing so
39a0: 69 73 20 69 6e 65 78 70 65 6e 73 69 76 65 2e 0a is inexpensive..
39b0: 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 28 63 ** (c
39c0: 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 41 4e 44 reateFlag==1 AND
39d0: 20 62 50 75 72 67 65 61 62 6c 65 20 41 4e 44 20 bPurgeable AND
39e0: 70 44 69 72 74 79 29 0a 20 20 2a 2a 20 20 20 20 pDirty). **
39f0: 32 20 20 20 20 20 41 6c 6c 6f 63 61 74 65 20 61 2 Allocate a
3a00: 20 6e 65 77 20 70 61 67 65 20 65 76 65 6e 20 69 new page even i
3a10: 74 20 64 6f 69 6e 67 20 73 6f 20 69 73 20 64 69 t doing so is di
3a20: 66 66 69 63 75 6c 74 2e 0a 20 20 2a 2a 20 20 20 fficult.. **
3a30: 20 20 20 20 20 20 20 28 63 72 65 61 74 65 46 6c (createFl
3a40: 61 67 3d 3d 31 20 41 4e 44 20 21 28 62 50 75 72 ag==1 AND !(bPur
3a50: 67 65 61 62 6c 65 20 41 4e 44 20 70 44 69 72 74 geable AND pDirt
3a60: 79 29 0a 20 20 2a 2f 0a 20 20 65 43 72 65 61 74 y). */. eCreat
3a70: 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67 20 26 e = createFlag &
3a80: 20 70 43 61 63 68 65 2d 3e 65 43 72 65 61 74 65 pCache->eCreate
3a90: 3b 0a 20 20 61 73 73 65 72 74 28 20 65 43 72 65 ;. assert( eCre
3aa0: 61 74 65 3d 3d 30 20 7c 7c 20 65 43 72 65 61 74 ate==0 || eCreat
3ab0: 65 3d 3d 31 20 7c 7c 20 65 43 72 65 61 74 65 3d e==1 || eCreate=
3ac0: 3d 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =2 );. assert(
3ad0: 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 7c 7c createFlag==0 ||
3ae0: 20 70 43 61 63 68 65 2d 3e 65 43 72 65 61 74 65 pCache->eCreate
3af0: 3d 3d 65 43 72 65 61 74 65 20 29 3b 0a 20 20 61 ==eCreate );. a
3b00: 73 73 65 72 74 28 20 63 72 65 61 74 65 46 6c 61 ssert( createFla
3b10: 67 3d 3d 30 20 7c 7c 20 65 43 72 65 61 74 65 3d g==0 || eCreate=
3b20: 3d 31 2b 28 21 70 43 61 63 68 65 2d 3e 62 50 75 =1+(!pCache->bPu
3b30: 72 67 65 61 62 6c 65 7c 7c 21 70 43 61 63 68 65 rgeable||!pCache
3b40: 2d 3e 70 44 69 72 74 79 29 20 29 3b 0a 20 20 70 ->pDirty) );. p
3b50: 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f Res = sqlite3Glo
3b60: 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
3b70: 32 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 2.xFetch(pCache-
3b80: 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65 >pCache, pgno, e
3b90: 43 72 65 61 74 65 29 3b 0a 20 20 70 63 61 63 68 Create);. pcach
3ba0: 65 54 72 61 63 65 28 28 22 25 70 2e 46 45 54 43 eTrace(("%p.FETC
3bb0: 48 20 25 64 25 73 20 28 72 65 73 75 6c 74 3a 20 H %d%s (result:
3bc0: 25 70 29 5c 6e 22 2c 70 43 61 63 68 65 2c 70 67 %p)\n",pCache,pg
3bd0: 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 no,.
3be0: 20 20 20 63 72 65 61 74 65 46 6c 61 67 3f 22 20 createFlag?"
3bf0: 63 72 65 61 74 65 22 3a 22 22 2c 70 52 65 73 29 create":"",pRes)
3c00: 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 73 );. return pRes
3c10: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
3c20: 65 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 e sqlite3PcacheF
3c30: 65 74 63 68 28 29 20 72 6f 75 74 69 6e 65 20 69 etch() routine i
3c40: 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f s unable to allo
3c50: 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 70 61 cate a new.** pa
3c60: 67 65 20 62 65 63 61 75 73 65 20 6e 6f 20 63 6c ge because no cl
3c70: 65 61 6e 20 70 61 67 65 73 20 61 72 65 20 61 76 ean pages are av
3c80: 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 75 73 ailable for reus
3c90: 65 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 0a e and the cache.
3ca0: 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 20 68 61 ** size limit ha
3cb0: 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c 20 s been reached,
3cc0: 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
3cd0: 65 20 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 e can be invoked
3ce0: 20 74 6f 20 0a 2a 2a 20 74 72 79 20 68 61 72 64 to .** try hard
3cf0: 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 er to allocate a
3d00: 20 70 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75 page. This rou
3d10: 74 69 6e 65 20 6d 69 67 68 74 20 69 6e 76 6f 6b tine might invok
3d20: 65 20 74 68 65 20 73 74 72 65 73 73 0a 2a 2a 20 e the stress.**
3d30: 63 61 6c 6c 62 61 63 6b 20 74 6f 20 73 70 69 6c callback to spil
3d40: 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f l dirty pages to
3d50: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 the journal. I
3d60: 74 20 77 69 6c 6c 20 74 68 65 6e 20 74 72 79 20 t will then try
3d70: 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 to.** allocate t
3d80: 68 65 20 6e 65 77 20 70 61 67 65 20 61 6e 64 20 he new page and
3d90: 77 69 6c 6c 20 6f 6e 6c 79 20 66 61 69 6c 20 74 will only fail t
3da0: 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 o allocate a new
3db0: 20 70 61 67 65 20 6f 6e 0a 2a 2a 20 61 6e 20 4f page on.** an O
3dc0: 4f 4d 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 OM error..**.**
3dd0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f This routine sho
3de0: 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 6f uld be invoked o
3df0: 6e 6c 79 20 61 66 74 65 72 20 73 71 6c 69 74 65 nly after sqlite
3e00: 33 50 63 61 63 68 65 46 65 74 63 68 28 29 20 66 3PcacheFetch() f
3e10: 61 69 6c 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ails..*/.int sql
3e20: 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 53 ite3PcacheFetchS
3e30: 74 72 65 73 73 28 0a 20 20 50 43 61 63 68 65 20 tress(. PCache
3e40: 2a 70 43 61 63 68 65 2c 20 20 20 20 20 20 20 20 *pCache,
3e50: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 /* Obta
3e60: 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d in the page from
3e70: 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 this cache */.
3e80: 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 Pgno pgno,
3e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3ea0: 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
3eb0: 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 73 to obtain */. s
3ec0: 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 qlite3_pcache_pa
3ed0: 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 2f ge **ppPage /
3ee0: 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 20 68 * Write result h
3ef0: 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 ere */.){. PgHd
3f00: 72 20 2a 70 50 67 3b 0a 20 20 69 66 28 20 70 43 r *pPg;. if( pC
3f10: 61 63 68 65 2d 3e 65 43 72 65 61 74 65 3d 3d 32 ache->eCreate==2
3f20: 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 ) return 0;..
3f30: 69 66 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 if( sqlite3Pcach
3f40: 65 50 61 67 65 63 6f 75 6e 74 28 70 43 61 63 68 ePagecount(pCach
3f50: 65 29 3e 70 43 61 63 68 65 2d 3e 73 7a 53 70 69 e)>pCache->szSpi
3f60: 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 6e ll ){. /* Fin
3f70: 64 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 d a dirty page t
3f80: 6f 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 o write-out and
3f90: 72 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 recycle. First t
3fa0: 72 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 ry to find a .
3fb0: 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 ** page that d
3fc0: 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 oes not require
3fd0: 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 a journal-sync (
3fe0: 6f 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e one with PGHDR_N
3ff0: 45 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 EED_SYNC. **
4000: 63 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 cleared), but if
4010: 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 that is not pos
4020: 73 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 sible settle for
4030: 20 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 any other .
4040: 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 ** unreferenced
4050: 64 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 dirty page..
4060: 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 **. ** If the
4070: 20 4c 52 55 20 70 61 67 65 20 69 6e 20 74 68 65 LRU page in the
4080: 20 64 69 72 74 79 20 6c 69 73 74 20 74 68 61 74 dirty list that
4090: 20 68 61 73 20 61 20 63 6c 65 61 72 20 50 47 48 has a clear PGH
40a0: 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a 20 20 20 DR_NEED_SYNC.
40b0: 20 2a 2a 20 66 6c 61 67 20 69 73 20 63 75 72 72 ** flag is curr
40c0: 65 6e 74 6c 79 20 72 65 66 65 72 65 6e 63 65 64 ently referenced
40d0: 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f , then the follo
40e0: 77 69 6e 67 20 6d 61 79 20 6c 65 61 76 65 20 70 wing may leave p
40f0: 53 79 6e 63 65 64 0a 20 20 20 20 2a 2a 20 73 65 Synced. ** se
4100: 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 28 70 t incorrectly (p
4110: 6f 69 6e 74 69 6e 67 20 74 6f 20 6f 74 68 65 72 ointing to other
4120: 20 74 68 61 6e 20 74 68 65 20 4c 52 55 20 70 61 than the LRU pa
4130: 67 65 20 77 69 74 68 20 4e 45 45 44 5f 53 59 4e ge with NEED_SYN
4140: 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61 72 65 64 C. ** cleared
4150: 29 2e 20 54 68 69 73 20 69 73 20 4f 6b 2c 20 61 ). This is Ok, a
4160: 73 20 70 53 79 6e 63 65 64 20 69 73 20 6a 75 73 s pSynced is jus
4170: 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f t an optimizatio
4180: 6e 2e 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 n. */. for(p
4190: 50 67 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 Pg=pCache->pSync
41a0: 65 64 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 ed; . pPg
41b0: 20 26 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c && (pPg->nRef |
41c0: 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 | (pPg->flags&PG
41d0: 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b HDR_NEED_SYNC));
41e0: 20 0a 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 . pPg=pP
41f0: 67 2d 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 g->pDirtyPrev.
4200: 20 20 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d );. pCache-
4210: 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 67 3b 0a >pSynced = pPg;.
4220: 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a if( !pPg ){.
4230: 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 for(pPg=pC
4240: 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c ache->pDirtyTail
4250: 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 ; pPg && pPg->nR
4260: 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 ef; pPg=pPg->pDi
4270: 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a rtyPrev);. }.
4280: 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 if( pPg ){.
4290: 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 23 69 66 int rc;.#if
42a0: 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f 47 5f 43 def SQLITE_LOG_C
42b0: 41 43 48 45 5f 53 50 49 4c 4c 0a 20 20 20 20 20 ACHE_SPILL.
42c0: 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c sqlite3_log(SQL
42d0: 49 54 45 5f 46 55 4c 4c 2c 20 0a 20 20 20 20 20 ITE_FULL, .
42e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 73 70 "sp
42f0: 69 6c 6c 20 70 61 67 65 20 25 64 20 6d 61 6b 69 ill page %d maki
4300: 6e 67 20 72 6f 6f 6d 20 66 6f 72 20 25 64 20 2d ng room for %d -
4310: 20 63 61 63 68 65 20 75 73 65 64 3a 20 25 64 2f cache used: %d/
4320: 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 %d",.
4330: 20 20 20 20 20 20 20 70 50 67 2d 3e 70 67 6e 6f pPg->pgno
4340: 2c 20 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 , pgno,.
4350: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
4360: 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
4370: 61 63 68 65 32 2e 78 50 61 67 65 63 6f 75 6e 74 ache2.xPagecount
4380: 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
4390: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
43a0: 20 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50 numberOfCacheP
43b0: 61 67 65 73 28 70 43 61 63 68 65 29 29 3b 0a 23 ages(pCache));.#
43c0: 65 6e 64 69 66 0a 20 20 20 20 20 20 70 63 61 63 endif. pcac
43d0: 68 65 54 72 61 63 65 28 28 22 25 70 2e 53 50 49 heTrace(("%p.SPI
43e0: 4c 4c 20 25 64 5c 6e 22 2c 70 43 61 63 68 65 2c LL %d\n",pCache,
43f0: 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 pPg->pgno));.
4400: 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e rc = pCache->
4410: 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e xStress(pCache->
4420: 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20 pStress, pPg);.
4430: 20 20 20 20 20 70 63 61 63 68 65 44 75 6d 70 28 pcacheDump(
4440: 70 43 61 63 68 65 29 3b 0a 20 20 20 20 20 20 69 pCache);. i
4450: 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
4460: 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 && rc!=SQLITE_B
4470: 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72 USY ){. r
4480: 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
4490: 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 }. }. }. *p
44a0: 70 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 pPage = sqlite3G
44b0: 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
44c0: 68 65 32 2e 78 46 65 74 63 68 28 70 43 61 63 68 he2.xFetch(pCach
44d0: 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c e->pCache, pgno,
44e0: 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 2a 70 2);. return *p
44f0: 70 50 61 67 65 3d 3d 30 20 3f 20 53 51 4c 49 54 pPage==0 ? SQLIT
4500: 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 20 3a 20 53 E_NOMEM_BKPT : S
4510: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
4520: 2a 2a 20 54 68 69 73 20 69 73 20 61 20 68 65 6c ** This is a hel
4530: 70 65 72 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 per routine for
4540: 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
4550: 63 68 46 69 6e 69 73 68 28 29 0a 2a 2a 0a 2a 2a chFinish().**.**
4560: 20 49 6e 20 74 68 65 20 75 6e 63 6f 6d 6d 6f 6e In the uncommon
4570: 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 case where the
4580: 70 61 67 65 20 62 65 69 6e 67 20 66 65 74 63 68 page being fetch
4590: 65 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a ed has not been.
45a0: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 ** initialized,
45b0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
45c0: 69 6e 76 6f 6b 65 64 20 74 6f 20 64 6f 20 74 68 invoked to do th
45d0: 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
45e0: 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
45f0: 65 20 69 73 20 62 72 6f 6b 65 6e 20 6f 75 74 20 e is broken out
4600: 69 6e 74 6f 20 61 20 73 65 70 61 72 61 74 65 20 into a separate
4610: 66 75 6e 63 74 69 6f 6e 20 73 69 6e 63 65 20 69 function since i
4620: 74 0a 2a 2a 20 72 65 71 75 69 72 65 73 20 65 78 t.** requires ex
4630: 74 72 61 20 73 74 61 63 6b 20 6d 61 6e 69 70 75 tra stack manipu
4640: 6c 61 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 lation that can
4650: 62 65 20 61 76 6f 69 64 65 64 20 69 6e 20 74 68 be avoided in th
4660: 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 e common.** case
4670: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 ..*/.static SQLI
4680: 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 50 67 48 64 TE_NOINLINE PgHd
4690: 72 20 2a 70 63 61 63 68 65 46 65 74 63 68 46 69 r *pcacheFetchFi
46a0: 6e 69 73 68 57 69 74 68 49 6e 69 74 28 0a 20 20 nishWithInit(.
46b0: 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 PCache *pCache,
46c0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
46d0: 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 btain the page f
46e0: 72 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a rom this cache *
46f0: 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 /. Pgno pgno,
4700: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4710: 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f /* Page number o
4720: 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 73 71 6c btained */. sql
4730: 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 ite3_pcache_page
4740: 20 2a 70 50 61 67 65 20 20 2f 2a 20 50 61 67 65 *pPage /* Page
4750: 20 6f 62 74 61 69 6e 65 64 20 62 79 20 70 72 69 obtained by pri
4760: 6f 72 20 50 63 61 63 68 65 46 65 74 63 68 28 29 or PcacheFetch()
4770: 20 63 61 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 50 67 call */.){. Pg
4780: 48 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 61 Hdr *pPgHdr;. a
4790: 73 73 65 72 74 28 20 70 50 61 67 65 21 3d 30 20 ssert( pPage!=0
47a0: 29 3b 0a 20 20 70 50 67 48 64 72 20 3d 20 28 50 );. pPgHdr = (P
47b0: 67 48 64 72 2a 29 70 50 61 67 65 2d 3e 70 45 78 gHdr*)pPage->pEx
47c0: 74 72 61 3b 0a 20 20 61 73 73 65 72 74 28 20 70 tra;. assert( p
47d0: 50 67 48 64 72 2d 3e 70 50 61 67 65 3d 3d 30 20 PgHdr->pPage==0
47e0: 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 67 );. memset(&pPg
47f0: 48 64 72 2d 3e 70 44 69 72 74 79 2c 20 30 2c 20 Hdr->pDirty, 0,
4800: 73 69 7a 65 6f 66 28 50 67 48 64 72 29 20 2d 20 sizeof(PgHdr) -
4810: 6f 66 66 73 65 74 6f 66 28 50 67 48 64 72 2c 70 offsetof(PgHdr,p
4820: 44 69 72 74 79 29 29 3b 0a 20 20 70 50 67 48 64 Dirty));. pPgHd
4830: 72 2d 3e 70 50 61 67 65 20 3d 20 70 50 61 67 65 r->pPage = pPage
4840: 3b 0a 20 20 70 50 67 48 64 72 2d 3e 70 44 61 74 ;. pPgHdr->pDat
4850: 61 20 3d 20 70 50 61 67 65 2d 3e 70 42 75 66 3b a = pPage->pBuf;
4860: 0a 20 20 70 50 67 48 64 72 2d 3e 70 45 78 74 72 . pPgHdr->pExtr
4870: 61 20 3d 20 28 76 6f 69 64 20 2a 29 26 70 50 67 a = (void *)&pPg
4880: 48 64 72 5b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 Hdr[1];. memset
4890: 28 70 50 67 48 64 72 2d 3e 70 45 78 74 72 61 2c (pPgHdr->pExtra,
48a0: 20 30 2c 20 38 29 3b 0a 20 20 70 50 67 48 64 72 0, 8);. pPgHdr
48b0: 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
48c0: 65 3b 0a 20 20 70 50 67 48 64 72 2d 3e 70 67 6e e;. pPgHdr->pgn
48d0: 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 70 50 67 48 o = pgno;. pPgH
48e0: 64 72 2d 3e 66 6c 61 67 73 20 3d 20 50 47 48 44 dr->flags = PGHD
48f0: 52 5f 43 4c 45 41 4e 3b 0a 20 20 72 65 74 75 72 R_CLEAN;. retur
4900: 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 n sqlite3PcacheF
4910: 65 74 63 68 46 69 6e 69 73 68 28 70 43 61 63 68 etchFinish(pCach
4920: 65 2c 70 67 6e 6f 2c 70 50 61 67 65 29 3b 0a 7d e,pgno,pPage);.}
4930: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
4940: 74 69 6e 65 20 63 6f 6e 76 65 72 74 73 20 74 68 tine converts th
4950: 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
4960: 5f 70 61 67 65 20 6f 62 6a 65 63 74 20 72 65 74 _page object ret
4970: 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 urned by.** sqli
4980: 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 29 te3PcacheFetch()
4990: 20 69 6e 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c into an initial
49a0: 69 7a 65 64 20 50 67 48 64 72 20 6f 62 6a 65 63 ized PgHdr objec
49b0: 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 t. This routine
49c0: 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c 6c .** must be call
49d0: 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 ed after sqlite3
49e0: 50 63 61 63 68 65 46 65 74 63 68 28 29 20 69 6e PcacheFetch() in
49f0: 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 61 20 order to get a
4a00: 75 73 61 62 6c 65 0a 2a 2a 20 72 65 73 75 6c 74 usable.** result
4a10: 2e 0a 2a 2f 0a 50 67 48 64 72 20 2a 73 71 6c 69 ..*/.PgHdr *sqli
4a20: 74 65 33 50 63 61 63 68 65 46 65 74 63 68 46 69 te3PcacheFetchFi
4a30: 6e 69 73 68 28 0a 20 20 50 43 61 63 68 65 20 2a nish(. PCache *
4a40: 70 43 61 63 68 65 2c 20 20 20 20 20 20 20 20 20 pCache,
4a50: 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 /* Obtain th
4a60: 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73 e page from this
4a70: 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
4a80: 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
4a90: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
4aa0: 6e 75 6d 62 65 72 20 6f 62 74 61 69 6e 65 64 20 number obtained
4ab0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 */. sqlite3_pca
4ac0: 63 68 65 5f 70 61 67 65 20 2a 70 50 61 67 65 20 che_page *pPage
4ad0: 20 2f 2a 20 50 61 67 65 20 6f 62 74 61 69 6e 65 /* Page obtaine
4ae0: 64 20 62 79 20 70 72 69 6f 72 20 50 63 61 63 68 d by prior Pcach
4af0: 65 46 65 74 63 68 28 29 20 63 61 6c 6c 20 2a 2f eFetch() call */
4b00: 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 .){. PgHdr *pPg
4b10: 48 64 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 Hdr;.. assert(
4b20: 70 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 70 50 pPage!=0 );. pP
4b30: 67 48 64 72 20 3d 20 28 50 67 48 64 72 20 2a 29 gHdr = (PgHdr *)
4b40: 70 50 61 67 65 2d 3e 70 45 78 74 72 61 3b 0a 0a pPage->pExtra;..
4b50: 20 20 69 66 28 20 21 70 50 67 48 64 72 2d 3e 70 if( !pPgHdr->p
4b60: 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 Page ){. retu
4b70: 72 6e 20 70 63 61 63 68 65 46 65 74 63 68 46 69 rn pcacheFetchFi
4b80: 6e 69 73 68 57 69 74 68 49 6e 69 74 28 70 43 61 nishWithInit(pCa
4b90: 63 68 65 2c 20 70 67 6e 6f 2c 20 70 50 61 67 65 che, pgno, pPage
4ba0: 29 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d );. }. pCache-
4bb0: 3e 6e 52 65 66 53 75 6d 2b 2b 3b 0a 20 20 70 50 >nRefSum++;. pP
4bc0: 67 48 64 72 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 gHdr->nRef++;.
4bd0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
4be0: 63 61 63 68 65 50 61 67 65 53 61 6e 69 74 79 28 cachePageSanity(
4bf0: 70 50 67 48 64 72 29 20 29 3b 0a 20 20 72 65 74 pPgHdr) );. ret
4c00: 75 72 6e 20 70 50 67 48 64 72 3b 0a 7d 0a 0a 2f urn pPgHdr;.}../
4c10: 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 *.** Decrement t
4c20: 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
4c30: 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66 nt on a page. If
4c40: 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65 the page is cle
4c50: 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 an and the.** re
4c60: 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72 ference count dr
4c70: 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69 ops to 0, then i
4c80: 74 20 69 73 20 6d 61 64 65 20 65 6c 69 67 69 62 t is made eligib
4c90: 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 le for recycling
4ca0: 2e 0a 2a 2f 0a 76 6f 69 64 20 53 51 4c 49 54 45 ..*/.void SQLITE
4cb0: 5f 4e 4f 49 4e 4c 49 4e 45 20 73 71 6c 69 74 65 _NOINLINE sqlite
4cc0: 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 3PcacheRelease(P
4cd0: 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 gHdr *p){. asse
4ce0: 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
4cf0: 0a 20 20 70 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 . p->pCache->nR
4d00: 65 66 53 75 6d 2d 2d 3b 0a 20 20 69 66 28 20 28 efSum--;. if( (
4d10: 2d 2d 70 2d 3e 6e 52 65 66 29 3d 3d 30 20 29 7b --p->nRef)==0 ){
4d20: 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 . if( p->flag
4d30: 73 26 50 47 48 44 52 5f 43 4c 45 41 4e 20 29 7b s&PGHDR_CLEAN ){
4d40: 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 . pcacheUnp
4d50: 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 in(p);. }else
4d60: 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 4d 61 {. pcacheMa
4d70: 6e 61 67 65 44 69 72 74 79 4c 69 73 74 28 70 2c nageDirtyList(p,
4d80: 20 50 43 41 43 48 45 5f 44 49 52 54 59 4c 49 53 PCACHE_DIRTYLIS
4d90: 54 5f 46 52 4f 4e 54 29 3b 0a 20 20 20 20 7d 0a T_FRONT);. }.
4da0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 }.}../*.** Inc
4db0: 72 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 rease the refere
4dc0: 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 nce count of a s
4dd0: 75 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 upplied page by
4de0: 31 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 1..*/.void sqlit
4df0: 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 64 e3PcacheRef(PgHd
4e00: 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 r *p){. assert(
4e10: 70 2d 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 61 73 p->nRef>0);. as
4e20: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
4e30: 63 68 65 50 61 67 65 53 61 6e 69 74 79 28 70 29 chePageSanity(p)
4e40: 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b );. p->nRef++;
4e50: 0a 20 20 70 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 . p->pCache->nR
4e60: 65 66 53 75 6d 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a efSum++;.}../*.*
4e70: 2a 20 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 * Drop a page fr
4e80: 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 om the cache. Th
4e90: 65 72 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 ere must be exac
4ea0: 74 6c 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 tly one referenc
4eb0: 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 e to the.** page
4ec0: 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
4ed0: 64 65 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 deletes that ref
4ee0: 65 72 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 erence, so after
4ef0: 20 69 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a it returns the.
4f00: 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 ** page pointed
4f10: 74 6f 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c to by p is inval
4f20: 69 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 id..*/.void sqli
4f30: 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 te3PcacheDrop(Pg
4f40: 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 Hdr *p){. asser
4f50: 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b t( p->nRef==1 );
4f60: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
4f70: 65 33 50 63 61 63 68 65 50 61 67 65 53 61 6e 69 e3PcachePageSani
4f80: 74 79 28 70 29 20 29 3b 0a 20 20 69 66 28 20 70 ty(p) );. if( p
4f90: 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
4fa0: 52 54 59 20 29 7b 0a 20 20 20 20 70 63 61 63 68 RTY ){. pcach
4fb0: 65 4d 61 6e 61 67 65 44 69 72 74 79 4c 69 73 74 eManageDirtyList
4fc0: 28 70 2c 20 50 43 41 43 48 45 5f 44 49 52 54 59 (p, PCACHE_DIRTY
4fd0: 4c 49 53 54 5f 52 45 4d 4f 56 45 29 3b 0a 20 20 LIST_REMOVE);.
4fe0: 7d 0a 20 20 70 2d 3e 70 43 61 63 68 65 2d 3e 6e }. p->pCache->n
4ff0: 52 65 66 53 75 6d 2d 2d 3b 0a 20 20 73 71 6c 69 RefSum--;. sqli
5000: 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
5010: 70 63 61 63 68 65 32 2e 78 55 6e 70 69 6e 28 70 pcache2.xUnpin(p
5020: 2d 3e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ->pCache->pCache
5030: 2c 20 70 2d 3e 70 50 61 67 65 2c 20 31 29 3b 0a , p->pPage, 1);.
5040: 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
5050: 72 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6d re the page is m
5060: 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 arked as dirty.
5070: 49 66 20 69 74 20 69 73 6e 27 74 20 64 69 72 74 If it isn't dirt
5080: 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 y already,.** ma
5090: 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 76 6f 69 ke it so..*/.voi
50a0: 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d d sqlite3PcacheM
50b0: 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 20 2a akeDirty(PgHdr *
50c0: 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
50d0: 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 >nRef>0 );. ass
50e0: 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 ert( sqlite3Pcac
50f0: 68 65 50 61 67 65 53 61 6e 69 74 79 28 70 29 20 hePageSanity(p)
5100: 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 );. if( p->flag
5110: 73 20 26 20 28 50 47 48 44 52 5f 43 4c 45 41 4e s & (PGHDR_CLEAN
5120: 7c 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 |PGHDR_DONT_WRIT
5130: 45 29 20 29 7b 20 20 20 20 2f 2a 4f 50 54 49 4d E) ){ /*OPTIM
5140: 49 5a 41 54 49 4f 4e 2d 49 46 2d 46 41 4c 53 45 IZATION-IF-FALSE
5150: 2a 2f 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 */. p->flags
5160: 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f 57 &= ~PGHDR_DONT_W
5170: 52 49 54 45 3b 0a 20 20 20 20 69 66 28 20 70 2d RITE;. if( p-
5180: 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 43 >flags & PGHDR_C
5190: 4c 45 41 4e 20 29 7b 0a 20 20 20 20 20 20 70 2d LEAN ){. p-
51a0: 3e 66 6c 61 67 73 20 5e 3d 20 28 50 47 48 44 52 >flags ^= (PGHDR
51b0: 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 43 4c 45 _DIRTY|PGHDR_CLE
51c0: 41 4e 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 AN);. pcach
51d0: 65 54 72 61 63 65 28 28 22 25 70 2e 44 49 52 54 eTrace(("%p.DIRT
51e0: 59 20 25 64 5c 6e 22 2c 70 2d 3e 70 43 61 63 68 Y %d\n",p->pCach
51f0: 65 2c 70 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 e,p->pgno));.
5200: 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 assert( (p->f
5210: 6c 61 67 73 20 26 20 28 50 47 48 44 52 5f 44 49 lags & (PGHDR_DI
5220: 52 54 59 7c 50 47 48 44 52 5f 43 4c 45 41 4e 29 RTY|PGHDR_CLEAN)
5230: 29 3d 3d 50 47 48 44 52 5f 44 49 52 54 59 20 29 )==PGHDR_DIRTY )
5240: 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 4d 61 ;. pcacheMa
5250: 6e 61 67 65 44 69 72 74 79 4c 69 73 74 28 70 2c nageDirtyList(p,
5260: 20 50 43 41 43 48 45 5f 44 49 52 54 59 4c 49 53 PCACHE_DIRTYLIS
5270: 54 5f 41 44 44 29 3b 0a 20 20 20 20 7d 0a 20 20 T_ADD);. }.
5280: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
5290: 33 50 63 61 63 68 65 50 61 67 65 53 61 6e 69 74 3PcachePageSanit
52a0: 79 28 70 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f y(p) );. }.}../
52b0: 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
52c0: 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 he page is marke
52d0: 64 20 61 73 20 63 6c 65 61 6e 2e 20 49 66 20 69 d as clean. If i
52e0: 74 20 69 73 6e 27 74 20 63 6c 65 61 6e 20 61 6c t isn't clean al
52f0: 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 69 ready,.** make i
5300: 74 20 73 6f 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 t so..*/.void sq
5310: 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
5320: 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a lean(PgHdr *p){.
5330: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
5340: 33 50 63 61 63 68 65 50 61 67 65 53 61 6e 69 74 3PcachePageSanit
5350: 79 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 y(p) );. assert
5360: 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 ( (p->flags & PG
5370: 48 44 52 5f 44 49 52 54 59 29 21 3d 30 20 29 3b HDR_DIRTY)!=0 );
5380: 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 . assert( (p->f
5390: 6c 61 67 73 20 26 20 50 47 48 44 52 5f 43 4c 45 lags & PGHDR_CLE
53a0: 41 4e 29 3d 3d 30 20 29 3b 0a 20 20 70 63 61 63 AN)==0 );. pcac
53b0: 68 65 4d 61 6e 61 67 65 44 69 72 74 79 4c 69 73 heManageDirtyLis
53c0: 74 28 70 2c 20 50 43 41 43 48 45 5f 44 49 52 54 t(p, PCACHE_DIRT
53d0: 59 4c 49 53 54 5f 52 45 4d 4f 56 45 29 3b 0a 20 YLIST_REMOVE);.
53e0: 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 p->flags &= ~(P
53f0: 47 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 GHDR_DIRTY|PGHDR
5400: 5f 4e 45 45 44 5f 53 59 4e 43 7c 50 47 48 44 52 _NEED_SYNC|PGHDR
5410: 5f 57 52 49 54 45 41 42 4c 45 29 3b 0a 20 20 70 _WRITEABLE);. p
5420: 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
5430: 5f 43 4c 45 41 4e 3b 0a 20 20 70 63 61 63 68 65 _CLEAN;. pcache
5440: 54 72 61 63 65 28 28 22 25 70 2e 43 4c 45 41 4e Trace(("%p.CLEAN
5450: 20 25 64 5c 6e 22 2c 70 2d 3e 70 43 61 63 68 65 %d\n",p->pCache
5460: 2c 70 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 61 73 ,p->pgno));. as
5470: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
5480: 63 68 65 50 61 67 65 53 61 6e 69 74 79 28 70 29 chePageSanity(p)
5490: 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 );. if( p->nRe
54a0: 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 f==0 ){. pcac
54b0: 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20 7d 0a heUnpin(p);. }.
54c0: 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 }../*.** Make ev
54d0: 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
54e0: 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a cache clean..*/.
54f0: 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
5500: 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 heCleanAll(PCach
5510: 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 e *pCache){. Pg
5520: 48 64 72 20 2a 70 3b 0a 20 20 70 63 61 63 68 65 Hdr *p;. pcache
5530: 54 72 61 63 65 28 28 22 25 70 2e 43 4c 45 41 4e Trace(("%p.CLEAN
5540: 2d 41 4c 4c 5c 6e 22 2c 70 43 61 63 68 65 29 29 -ALL\n",pCache))
5550: 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 20 3d 20 ;. while( (p =
5560: 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 21 pCache->pDirty)!
5570: 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
5580: 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
5590: 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
55a0: 2a 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44 * Clear the PGHD
55b0: 52 5f 4e 45 45 44 5f 53 59 4e 43 20 61 6e 64 20 R_NEED_SYNC and
55c0: 50 47 48 44 52 5f 57 52 49 54 45 41 42 4c 45 20 PGHDR_WRITEABLE
55d0: 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 flag from all di
55e0: 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 76 6f rty pages..*/.vo
55f0: 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
5600: 43 6c 65 61 72 57 72 69 74 61 62 6c 65 28 50 43 ClearWritable(PC
5610: 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
5620: 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 70 63 61 PgHdr *p;. pca
5630: 63 68 65 54 72 61 63 65 28 28 22 25 70 2e 43 4c cheTrace(("%p.CL
5640: 45 41 52 2d 57 52 49 54 45 41 42 4c 45 5c 6e 22 EAR-WRITEABLE\n"
5650: 2c 70 43 61 63 68 65 29 29 3b 0a 20 20 66 6f 72 ,pCache));. for
5660: 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
5670: 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 y; p; p=p->pDirt
5680: 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 yNext){. p->f
5690: 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 5f lags &= ~(PGHDR_
56a0: 4e 45 45 44 5f 53 59 4e 43 7c 50 47 48 44 52 5f NEED_SYNC|PGHDR_
56b0: 57 52 49 54 45 41 42 4c 45 29 3b 0a 20 20 7d 0a WRITEABLE);. }.
56c0: 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 pCache->pSynce
56d0: 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 d = pCache->pDir
56e0: 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a tyTail;.}../*.**
56f0: 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 Clear the PGHDR
5700: 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
5710: 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 79 20 70 from all dirty p
5720: 61 67 65 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ages..*/.void sq
5730: 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 lite3PcacheClear
5740: 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 SyncFlags(PCache
5750: 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
5760: 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 dr *p;. for(p=p
5770: 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 Cache->pDirty; p
5780: 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 ; p=p->pDirtyNex
5790: 74 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 t){. p->flags
57a0: 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f &= ~PGHDR_NEED_
57b0: 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43 61 63 SYNC;. }. pCac
57c0: 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 43 he->pSynced = pC
57d0: 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c ache->pDirtyTail
57e0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
57f0: 65 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 e the page numbe
5800: 72 20 6f 66 20 70 61 67 65 20 70 20 74 6f 20 6e r of page p to n
5810: 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 76 6f 69 64 ewPgno. .*/.void
5820: 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f sqlite3PcacheMo
5830: 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50 67 6e ve(PgHdr *p, Pgn
5840: 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50 43 o newPgno){. PC
5850: 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 ache *pCache = p
5860: 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 ->pCache;. asse
5870: 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
5880: 0a 20 20 61 73 73 65 72 74 28 20 6e 65 77 50 67 . assert( newPg
5890: 6e 6f 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 no>0 );. assert
58a0: 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 ( sqlite3PcacheP
58b0: 61 67 65 53 61 6e 69 74 79 28 70 29 20 29 3b 0a ageSanity(p) );.
58c0: 20 20 70 63 61 63 68 65 54 72 61 63 65 28 28 22 pcacheTrace(("
58d0: 25 70 2e 4d 4f 56 45 20 25 64 20 2d 3e 20 25 64 %p.MOVE %d -> %d
58e0: 5c 6e 22 2c 70 43 61 63 68 65 2c 70 2d 3e 70 67 \n",pCache,p->pg
58f0: 6e 6f 2c 6e 65 77 50 67 6e 6f 29 29 3b 0a 20 20 no,newPgno));.
5900: 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
5910: 66 69 67 2e 70 63 61 63 68 65 32 2e 78 52 65 6b fig.pcache2.xRek
5920: 65 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 ey(pCache->pCach
5930: 65 2c 20 70 2d 3e 70 50 61 67 65 2c 20 70 2d 3e e, p->pPage, p->
5940: 70 67 6e 6f 2c 6e 65 77 50 67 6e 6f 29 3b 0a 20 pgno,newPgno);.
5950: 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50 67 p->pgno = newPg
5960: 6e 6f 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c no;. if( (p->fl
5970: 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 ags&PGHDR_DIRTY)
5980: 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 && (p->flags&PG
5990: 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 HDR_NEED_SYNC) )
59a0: 7b 0a 20 20 20 20 70 63 61 63 68 65 4d 61 6e 61 {. pcacheMana
59b0: 67 65 44 69 72 74 79 4c 69 73 74 28 70 2c 20 50 geDirtyList(p, P
59c0: 43 41 43 48 45 5f 44 49 52 54 59 4c 49 53 54 5f CACHE_DIRTYLIST_
59d0: 46 52 4f 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f FRONT);. }.}../
59e0: 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 *.** Drop every
59f0: 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 cache entry whos
5a00: 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 e page number is
5a10: 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 greater than "p
5a20: 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c gno". The.** cal
5a30: 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 ler must ensure
5a40: 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e that there are n
5a50: 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 o outstanding re
5a60: 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 ferences to any
5a70: 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 pages.** other t
5a80: 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 han page 1 with
5a90: 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 a page number gr
5aa0: 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e eater than pgno.
5ab0: 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
5ac0: 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 is a reference t
5ad0: 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 o page 1 and the
5ae0: 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 pgno parameter
5af0: 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a passed to this.*
5b00: 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c * function is 0,
5b10: 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 then the data a
5b20: 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 rea associated w
5b30: 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 ith page 1 is ze
5b40: 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 roed, but.** the
5b50: 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
5b60: 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a not dropped..*/.
5b70: 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
5b80: 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 heTruncate(PCach
5b90: 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 e *pCache, Pgno
5ba0: 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 pgno){. if( pCa
5bb0: 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 che->pCache ){.
5bc0: 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 PgHdr *p;.
5bd0: 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 PgHdr *pNext;.
5be0: 20 20 20 70 63 61 63 68 65 54 72 61 63 65 28 28 pcacheTrace((
5bf0: 22 25 70 2e 54 52 55 4e 43 41 54 45 20 25 64 5c "%p.TRUNCATE %d\
5c00: 6e 22 2c 70 43 61 63 68 65 2c 70 67 6e 6f 29 29 n",pCache,pgno))
5c10: 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 ;. for(p=pCac
5c20: 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 he->pDirty; p; p
5c30: 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 =pNext){. p
5c40: 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 Next = p->pDirty
5c50: 4e 65 78 74 3b 0a 20 20 20 20 20 20 2f 2a 20 54 Next;. /* T
5c60: 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 his routine neve
5c70: 72 20 67 65 74 73 20 63 61 6c 6c 20 77 69 74 68 r gets call with
5c80: 20 61 20 70 6f 73 69 74 69 76 65 20 70 67 6e 6f a positive pgno
5c90: 20 65 78 63 65 70 74 20 72 69 67 68 74 0a 20 20 except right.
5ca0: 20 20 20 20 2a 2a 20 61 66 74 65 72 20 73 71 6c ** after sql
5cb0: 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 ite3PcacheCleanA
5cc0: 6c 6c 28 29 2e 20 20 53 6f 20 69 66 20 74 68 65 ll(). So if the
5cd0: 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67 re are dirty pag
5ce0: 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 es,. ** it
5cf0: 6d 75 73 74 20 62 65 20 74 68 61 74 20 70 67 6e must be that pgn
5d00: 6f 3d 3d 30 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 o==0.. */.
5d10: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
5d20: 70 67 6e 6f 3e 30 20 29 3b 0a 20 20 20 20 20 20 pgno>0 );.
5d30: 69 66 28 20 70 2d 3e 70 67 6e 6f 3e 70 67 6e 6f if( p->pgno>pgno
5d40: 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
5d50: 72 74 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 rt( p->flags&PGH
5d60: 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 DR_DIRTY );.
5d70: 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
5d80: 65 4d 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 eMakeClean(p);.
5d90: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
5da0: 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 26 26 20 if( pgno==0 &&
5db0: 70 43 61 63 68 65 2d 3e 6e 52 65 66 53 75 6d 20 pCache->nRefSum
5dc0: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
5dd0: 5f 70 63 61 63 68 65 5f 70 61 67 65 20 2a 70 50 _pcache_page *pP
5de0: 61 67 65 31 3b 0a 20 20 20 20 20 20 70 50 61 67 age1;. pPag
5df0: 65 31 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 e1 = sqlite3Glob
5e00: 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 alConfig.pcache2
5e10: 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e .xFetch(pCache->
5e20: 70 43 61 63 68 65 2c 31 2c 30 29 3b 0a 20 20 20 pCache,1,0);.
5e30: 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 if( ALWAYS(pP
5e40: 61 67 65 31 29 20 29 7b 20 20 2f 2a 20 50 61 67 age1) ){ /* Pag
5e50: 65 20 31 20 69 73 20 61 6c 77 61 79 73 20 61 76 e 1 is always av
5e60: 61 69 6c 61 62 6c 65 20 69 6e 20 63 61 63 68 65 ailable in cache
5e70: 2c 20 62 65 63 61 75 73 65 0a 20 20 20 20 20 20 , because.
5e80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e90: 20 20 20 20 20 20 20 2a 2a 20 70 43 61 63 68 65 ** pCache
5ea0: 2d 3e 6e 52 65 66 53 75 6d 3e 30 20 2a 2f 0a 20 ->nRefSum>0 */.
5eb0: 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 memset(pP
5ec0: 61 67 65 31 2d 3e 70 42 75 66 2c 20 30 2c 20 70 age1->pBuf, 0, p
5ed0: 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b 0a Cache->szPage);.
5ee0: 20 20 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 pgno = 1
5ef0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
5f00: 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
5f10: 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e lConfig.pcache2.
5f20: 78 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 xTruncate(pCache
5f30: 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2b 31 ->pCache, pgno+1
5f40: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
5f50: 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e 0a 2a Close a cache..*
5f60: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 /.void sqlite3Pc
5f70: 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65 acheClose(PCache
5f80: 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 61 73 73 *pCache){. ass
5f90: 65 72 74 28 20 70 43 61 63 68 65 2d 3e 70 43 61 ert( pCache->pCa
5fa0: 63 68 65 21 3d 30 20 29 3b 0a 20 20 70 63 61 63 che!=0 );. pcac
5fb0: 68 65 54 72 61 63 65 28 28 22 25 70 2e 43 4c 4f heTrace(("%p.CLO
5fc0: 53 45 5c 6e 22 2c 70 43 61 63 68 65 29 29 3b 0a SE\n",pCache));.
5fd0: 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
5fe0: 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 44 onfig.pcache2.xD
5ff0: 65 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 estroy(pCache->p
6000: 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a Cache);.}../* .*
6010: 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f * Discard the co
6020: 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 ntents of the ca
6030: 63 68 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c che..*/.void sql
6040: 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 ite3PcacheClear(
6050: 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
6060: 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
6070: 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c Truncate(pCache,
6080: 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 0);.}../*.** Me
6090: 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 rge two lists of
60a0: 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 pages connected
60b0: 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 by pDirty and i
60c0: 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a n pgno order..**
60d0: 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 66 Do not bother f
60e0: 69 78 69 6e 67 20 74 68 65 20 70 44 69 72 74 79 ixing the pDirty
60f0: 50 72 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a Prev pointers..*
6100: 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a /.static PgHdr *
6110: 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 pcacheMergeDirty
6120: 4c 69 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20 List(PgHdr *pA,
6130: 50 67 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 PgHdr *pB){. Pg
6140: 48 64 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 Hdr result, *pTa
6150: 69 6c 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 il;. pTail = &r
6160: 65 73 75 6c 74 3b 0a 20 20 61 73 73 65 72 74 28 esult;. assert(
6170: 20 70 41 21 3d 30 20 26 26 20 70 42 21 3d 30 20 pA!=0 && pB!=0
6180: 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 );. for(;;){.
6190: 20 20 69 66 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 if( pA->pgno<p
61a0: 42 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 B->pgno ){.
61b0: 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d pTail->pDirty =
61c0: 20 70 41 3b 0a 20 20 20 20 20 20 70 54 61 69 6c pA;. pTail
61d0: 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 41 20 = pA;. pA
61e0: 3d 20 70 41 2d 3e 70 44 69 72 74 79 3b 0a 20 20 = pA->pDirty;.
61f0: 20 20 20 20 69 66 28 20 70 41 3d 3d 30 20 29 7b if( pA==0 ){
6200: 0a 20 20 20 20 20 20 20 20 70 54 61 69 6c 2d 3e . pTail->
6210: 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 pDirty = pB;.
6220: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
6230: 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
6240: 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 pTail->pDir
6250: 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 ty = pB;. p
6260: 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20 20 20 Tail = pB;.
6270: 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72 74 79 pB = pB->pDirty
6280: 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 3d 3d ;. if( pB==
6290: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 0 ){. pTa
62a0: 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b il->pDirty = pA;
62b0: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
62c0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
62d0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c }. return resul
62e0: 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a t.pDirty;.}../*.
62f0: 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 ** Sort the list
6300: 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 of pages in acc
6310: 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 ending order by
6320: 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 pgno. Pages are
6330: 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 .** connected by
6340: 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 pDirty pointers
6350: 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 . The pDirtyPre
6360: 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a v pointers are.*
6370: 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 * corrupted by t
6380: 68 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 his sort..**.**
6390: 53 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e Since there cann
63a0: 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 ot be more than
63b0: 32 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 2^31 distinct pa
63c0: 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 ges in a databas
63d0: 65 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e e,.** there cann
63e0: 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 ot be more than
63f0: 33 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 31 buckets requi
6400: 72 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 red by the merge
6410: 20 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 sorter..** One
6420: 65 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 extra bucket is
6430: 61 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f added to catch o
6440: 76 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 verflow in case
6450: 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 something.** eve
6460: 72 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b r changes to mak
6470: 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 e the previous s
6480: 65 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 entence incorrec
6490: 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f t..*/.#define N_
64a0: 53 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a SORT_BUCKET 32.
64b0: 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 static PgHdr *pc
64c0: 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 acheSortDirtyLis
64d0: 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 t(PgHdr *pIn){.
64e0: 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 PgHdr *a[N_SORT
64f0: 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 _BUCKET], *p;.
6500: 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 int i;. memset(
6510: 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 a, 0, sizeof(a))
6520: 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 ;. while( pIn )
6530: 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 {. p = pIn;.
6540: 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 pIn = p->pDir
6550: 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 ty;. p->pDirt
6560: 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 y = 0;. for(i
6570: 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 =0; ALWAYS(i<N_S
6580: 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 ORT_BUCKET-1); i
6590: 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 ++){. if( a
65a0: 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 [i]==0 ){.
65b0: 20 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 a[i] = p;.
65c0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
65d0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
65e0: 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 p = pcacheMergeD
65f0: 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 irtyList(a[i], p
6600: 29 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 );. a[i]
6610: 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
6620: 20 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 }. if( NEVER
6630: 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 (i==N_SORT_BUCKE
6640: 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a T-1) ){. /*
6650: 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 To get here, th
6660: 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 ere need to be 2
6670: 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 ^(N_SORT_BUCKET)
6680: 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 elements in.
6690: 20 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 ** the input
66a0: 6c 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 list. But that
66b0: 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 is impossible..
66c0: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b */. a[
66d0: 69 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 i] = pcacheMerge
66e0: 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 DirtyList(a[i],
66f0: 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 p);. }. }.
6700: 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 p = a[0];. for(
6710: 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 i=1; i<N_SORT_BU
6720: 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 CKET; i++){.
6730: 69 66 28 20 61 5b 69 5d 3d 3d 30 20 29 20 63 6f if( a[i]==0 ) co
6740: 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d 20 ntinue;. p =
6750: 70 20 3f 20 70 63 61 63 68 65 4d 65 72 67 65 44 p ? pcacheMergeD
6760: 69 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d irtyList(p, a[i]
6770: 29 20 3a 20 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 ) : a[i];. }.
6780: 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
6790: 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74 ** Return a list
67a0: 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 of all dirty pa
67b0: 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 ges in the cache
67c0: 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 , sorted by page
67d0: 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 50 67 48 64 number..*/.PgHd
67e0: 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 r *sqlite3Pcache
67f0: 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 DirtyList(PCache
6800: 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
6810: 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 dr *p;. for(p=p
6820: 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 Cache->pDirty; p
6830: 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 ; p=p->pDirtyNex
6840: 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 t){. p->pDirt
6850: 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 y = p->pDirtyNex
6860: 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
6870: 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c pcacheSortDirtyL
6880: 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69 72 ist(pCache->pDir
6890: 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 ty);.}../* .** R
68a0: 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 eturn the total
68b0: 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 number of refere
68c0: 6e 63 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65 nces to all page
68d0: 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 s held by the ca
68e0: 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 che..**.** This
68f0: 69 73 20 6e 6f 74 20 74 68 65 20 74 6f 74 61 6c is not the total
6900: 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
6910: 20 72 65 66 65 72 65 6e 63 65 64 2c 20 62 75 74 referenced, but
6920: 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 0a the sum of the.
6930: 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 ** reference cou
6940: 6e 74 20 66 6f 72 20 61 6c 6c 20 70 61 67 65 73 nt for all pages
6950: 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
6960: 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 PcacheRefCount(P
6970: 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
6980: 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d return pCache-
6990: 3e 6e 52 65 66 53 75 6d 3b 0a 7d 0a 0a 2f 2a 0a >nRefSum;.}../*.
69a0: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
69b0: 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
69c0: 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 es to the page s
69d0: 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
69e0: 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 73 gument..*/.int s
69f0: 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
6a00: 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a Refcount(PgHdr *
6a10: 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
6a20: 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 nRef;.}../* .**
6a30: 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
6a40: 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
6a50: 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a in the cache..*
6a60: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 /.int sqlite3Pca
6a70: 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61 chePagecount(PCa
6a80: 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
6a90: 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e assert( pCache->
6aa0: 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 72 pCache!=0 );. r
6ab0: 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
6ac0: 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
6ad0: 32 2e 78 50 61 67 65 63 6f 75 6e 74 28 70 43 61 2.xPagecount(pCa
6ae0: 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 7d 0a che->pCache);.}.
6af0: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
6b00: 45 53 54 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 EST./*.** Get th
6b10: 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 e suggested cach
6b20: 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f e-size value..*/
6b30: 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 .int sqlite3Pcac
6b40: 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 heGetCachesize(P
6b50: 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
6b60: 20 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 4f return numberO
6b70: 66 43 61 63 68 65 50 61 67 65 73 28 70 43 61 63 fCachePages(pCac
6b80: 68 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f he);.}.#endif../
6b90: 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 *.** Set the sug
6ba0: 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a gested cache-siz
6bb0: 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 76 6f 69 64 e value..*/.void
6bc0: 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 sqlite3PcacheSe
6bd0: 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 tCachesize(PCach
6be0: 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 6d e *pCache, int m
6bf0: 78 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 xPage){. assert
6c00: 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
6c10: 21 3d 30 20 29 3b 0a 20 20 70 43 61 63 68 65 2d !=0 );. pCache-
6c20: 3e 73 7a 43 61 63 68 65 20 3d 20 6d 78 50 61 67 >szCache = mxPag
6c30: 65 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 e;. sqlite3Glob
6c40: 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 alConfig.pcache2
6c50: 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 .xCachesize(pCac
6c60: 68 65 2d 3e 70 43 61 63 68 65 2c 0a 20 20 20 20 he->pCache,.
6c70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c90: 20 20 20 20 20 6e 75 6d 62 65 72 4f 66 43 61 63 numberOfCac
6ca0: 68 65 50 61 67 65 73 28 70 43 61 63 68 65 29 29 hePages(pCache))
6cb0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
6cc0: 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 he suggested cac
6cd0: 68 65 2d 73 70 69 6c 6c 20 76 61 6c 75 65 2e 20 he-spill value.
6ce0: 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 Make no changes
6cf0: 20 69 66 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 if if the.** ar
6d00: 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2e 20 gument is zero.
6d10: 20 52 65 74 75 72 6e 20 74 68 65 20 65 66 66 65 Return the effe
6d20: 63 74 69 76 65 20 63 61 63 68 65 2d 73 70 69 6c ctive cache-spil
6d30: 6c 20 73 69 7a 65 2c 20 77 68 69 63 68 20 77 69 l size, which wi
6d40: 6c 6c 0a 2a 2a 20 62 65 20 74 68 65 20 6c 61 72 ll.** be the lar
6d50: 67 65 72 20 6f 66 20 74 68 65 20 73 7a 53 70 69 ger of the szSpi
6d60: 6c 6c 20 61 6e 64 20 73 7a 43 61 63 68 65 2e 0a ll and szCache..
6d70: 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 63 */.int sqlite3Pc
6d80: 61 63 68 65 53 65 74 53 70 69 6c 6c 73 69 7a 65 acheSetSpillsize
6d90: 28 50 43 61 63 68 65 20 2a 70 2c 20 69 6e 74 20 (PCache *p, int
6da0: 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 mxPage){. int r
6db0: 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d es;. assert( p-
6dc0: 3e 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 >pCache!=0 );.
6dd0: 69 66 28 20 6d 78 50 61 67 65 20 29 7b 0a 20 20 if( mxPage ){.
6de0: 20 20 69 66 28 20 6d 78 50 61 67 65 3c 30 20 29 if( mxPage<0 )
6df0: 7b 0a 20 20 20 20 20 20 6d 78 50 61 67 65 20 3d {. mxPage =
6e00: 20 28 69 6e 74 29 28 28 2d 31 30 32 34 2a 28 69 (int)((-1024*(i
6e10: 36 34 29 6d 78 50 61 67 65 29 2f 28 70 2d 3e 73 64)mxPage)/(p->s
6e20: 7a 50 61 67 65 2b 70 2d 3e 73 7a 45 78 74 72 61 zPage+p->szExtra
6e30: 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d ));. }. p-
6e40: 3e 73 7a 53 70 69 6c 6c 20 3d 20 6d 78 50 61 67 >szSpill = mxPag
6e50: 65 3b 0a 20 20 7d 0a 20 20 72 65 73 20 3d 20 6e e;. }. res = n
6e60: 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61 67 65 umberOfCachePage
6e70: 73 28 70 29 3b 0a 20 20 69 66 28 20 72 65 73 3c s(p);. if( res<
6e80: 70 2d 3e 73 7a 53 70 69 6c 6c 20 29 20 72 65 73 p->szSpill ) res
6e90: 20 3d 20 70 2d 3e 73 7a 53 70 69 6c 6c 3b 20 0a = p->szSpill; .
6ea0: 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a return res;.}.
6eb0: 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 75 70 20 61 ./*.** Free up a
6ec0: 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61 73 s much memory as
6ed0: 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 20 74 possible from t
6ee0: 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a he page cache..*
6ef0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 /.void sqlite3Pc
6f00: 61 63 68 65 53 68 72 69 6e 6b 28 50 43 61 63 68 acheShrink(PCach
6f10: 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 61 73 e *pCache){. as
6f20: 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e 70 43 sert( pCache->pC
6f30: 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 73 71 6c ache!=0 );. sql
6f40: 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
6f50: 2e 70 63 61 63 68 65 32 2e 78 53 68 72 69 6e 6b .pcache2.xShrink
6f60: 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
6f70: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
6f80: 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 n the size of th
6f90: 65 20 68 65 61 64 65 72 20 61 64 64 65 64 20 62 e header added b
6fa0: 79 20 74 68 69 73 20 6d 69 64 64 6c 65 77 61 72 y this middlewar
6fb0: 65 20 6c 61 79 65 72 0a 2a 2a 20 69 6e 20 74 68 e layer.** in th
6fc0: 65 20 70 61 67 65 2d 63 61 63 68 65 20 68 69 65 e page-cache hie
6fd0: 72 61 72 63 68 79 2e 0a 2a 2f 0a 69 6e 74 20 73 rarchy..*/.int s
6fe0: 71 6c 69 74 65 33 48 65 61 64 65 72 53 69 7a 65 qlite3HeaderSize
6ff0: 50 63 61 63 68 65 28 76 6f 69 64 29 7b 20 72 65 Pcache(void){ re
7000: 74 75 72 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 turn ROUND8(size
7010: 6f 66 28 50 67 48 64 72 29 29 3b 20 7d 0a 0a 2f of(PgHdr)); }../
7020: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
7030: 6e 75 6d 62 65 72 20 6f 66 20 64 69 72 74 79 20 number of dirty
7040: 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 pages currently
7050: 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 61 73 in the cache, as
7060: 20 61 20 70 65 72 63 65 6e 74 61 67 65 0a 2a 2a a percentage.**
7070: 20 6f 66 20 74 68 65 20 63 6f 6e 66 69 67 75 72 of the configur
7080: 65 64 20 63 61 63 68 65 20 73 69 7a 65 2e 0a 2a ed cache size..*
7090: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 43 61 /.int sqlite3PCa
70a0: 63 68 65 50 65 72 63 65 6e 74 44 69 72 74 79 28 chePercentDirty(
70b0: 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
70c0: 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
70d0: 3b 0a 20 20 69 6e 74 20 6e 44 69 72 74 79 20 3d ;. int nDirty =
70e0: 20 30 3b 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 0;. int nCache
70f0: 20 3d 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 = numberOfCache
7100: 50 61 67 65 73 28 70 43 61 63 68 65 29 3b 0a 20 Pages(pCache);.
7110: 20 66 6f 72 28 70 44 69 72 74 79 3d 70 43 61 63 for(pDirty=pCac
7120: 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 44 69 72 he->pDirty; pDir
7130: 74 79 3b 20 70 44 69 72 74 79 3d 70 44 69 72 74 ty; pDirty=pDirt
7140: 79 2d 3e 70 44 69 72 74 79 4e 65 78 74 29 20 6e y->pDirtyNext) n
7150: 44 69 72 74 79 2b 2b 3b 0a 20 20 72 65 74 75 72 Dirty++;. retur
7160: 6e 20 6e 43 61 63 68 65 20 3f 20 28 69 6e 74 29 n nCache ? (int)
7170: 28 28 28 69 36 34 29 6e 44 69 72 74 79 20 2a 20 (((i64)nDirty *
7180: 31 30 30 29 20 2f 20 6e 43 61 63 68 65 29 20 3a 100) / nCache) :
7190: 20 30 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 0;.}..#if defin
71a0: 65 64 28 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f ed(SQLITE_CHECK_
71b0: 50 41 47 45 53 29 20 7c 7c 20 64 65 66 69 6e 65 PAGES) || define
71c0: 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
71d0: 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c 20 64 69 /*.** For all di
71e0: 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e rty pages curren
71f0: 74 6c 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 tly in the cache
7200: 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 70 65 , invoke the spe
7210: 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c 6c 62 61 cified.** callba
7220: 63 6b 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 ck. This is only
7230: 20 75 73 65 64 20 69 66 20 74 68 65 20 53 51 4c used if the SQL
7240: 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 ITE_CHECK_PAGES
7250: 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64 65 66 69 macro is.** defi
7260: 6e 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ned..*/.void sql
7270: 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 ite3PcacheIterat
7280: 65 44 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 eDirty(PCache *p
7290: 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 Cache, void (*xI
72a0: 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 7b 0a ter)(PgHdr *)){.
72b0: 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 3b PgHdr *pDirty;
72c0: 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d 70 43 . for(pDirty=pC
72d0: 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 44 ache->pDirty; pD
72e0: 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70 44 69 irty; pDirty=pDi
72f0: 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78 74 29 rty->pDirtyNext)
7300: 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44 69 72 {. xIter(pDir
7310: 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 ty);. }.}.#endi
7320: 66 0a f.