0000: 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c /*.** 2004 April
0010: 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6.**.** The aut
0020: 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
0030: 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
0040: 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
0050: 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
0060: 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
0070: 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
0080: 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
0090: 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
00a0: 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
00b0: 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
00c0: 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
00d0: 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
00e0: 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
00f0: 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
0100: 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
0110: 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
0120: 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
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 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 *.** This file i
0180: 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 mplements a exte
0190: 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 rnal (disk-based
01a0: 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 ) database using
01b0: 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20 BTrees..** See
01c0: 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 the header comme
01d0: 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e nt on "btreeInt.
01e0: 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 h" for additiona
01f0: 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
0200: 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65 * Including a de
0210: 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c scription of fil
0220: 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20 e format and an
0230: 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72 overview of oper
0240: 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 ation..*/.#inclu
0250: 64 65 20 22 62 74 72 65 65 49 6e 74 2e 68 22 0a de "btreeInt.h".
0260: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 ./*.** The heade
0270: 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 r string that ap
0280: 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 pears at the beg
0290: 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a inning of every.
02a0: 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 ** SQLite databa
02b0: 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f se..*/.static co
02c0: 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48 nst char zMagicH
02d0: 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45 eader[] = SQLITE
02e0: 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f _FILE_HEADER;../
02f0: 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c *.** Set this gl
0300: 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f obal variable to
0310: 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 1 to enable tra
0320: 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54 cing using the T
0330: 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a RACE.** macro..*
0340: 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 /.#if 0.int sqli
0350: 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 31 3b te3BtreeTrace=1;
0360: 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
0370: 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 ble tracing */.#
0380: 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 define TRACE(X)
0390: 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65 if(sqlite3Btre
03a0: 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58 eTrace){printf X
03b0: 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b ;fflush(stdout);
03c0: 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
03d0: 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 TRACE(X).#endif
03e0: 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 ../*.** Extract
03f0: 61 20 32 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 a 2-byte big-end
0400: 69 61 6e 20 69 6e 74 65 67 65 72 20 66 72 6f 6d ian integer from
0410: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 an array of uns
0420: 69 67 6e 65 64 20 62 79 74 65 73 2e 0a 2a 2a 20 igned bytes..**
0430: 42 75 74 20 69 66 20 74 68 65 20 76 61 6c 75 65 But if the value
0440: 20 69 73 20 7a 65 72 6f 2c 20 6d 61 6b 65 20 69 is zero, make i
0450: 74 20 36 35 35 33 36 2e 0a 2a 2a 0a 2a 2a 20 54 t 65536..**.** T
0460: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
0470: 73 65 64 20 74 6f 20 65 78 74 72 61 63 74 20 74 sed to extract t
0480: 68 65 20 22 6f 66 66 73 65 74 20 74 6f 20 63 65 he "offset to ce
0490: 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 22 ll content area"
04a0: 20 76 61 6c 75 65 0a 2a 2a 20 66 72 6f 6d 20 74 value.** from t
04b0: 68 65 20 68 65 61 64 65 72 20 6f 66 20 61 20 62 he header of a b
04c0: 74 72 65 65 20 70 61 67 65 2e 20 20 49 66 20 74 tree page. If t
04d0: 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 he page size is
04e0: 36 35 35 33 36 20 61 6e 64 20 74 68 65 20 70 61 65536 and the pa
04f0: 67 65 0a 2a 2a 20 69 73 20 65 6d 70 74 79 2c 20 ge.** is empty,
0500: 74 68 65 20 6f 66 66 73 65 74 20 73 68 6f 75 6c the offset shoul
0510: 64 20 62 65 20 36 35 35 33 36 2c 20 62 75 74 20 d be 65536, but
0520: 74 68 65 20 32 2d 62 79 74 65 20 76 61 6c 75 65 the 2-byte value
0530: 20 73 74 6f 72 65 73 20 7a 65 72 6f 2e 0a 2a 2a stores zero..**
0540: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 This routine ma
0550: 6b 65 73 20 74 68 65 20 6e 65 63 65 73 73 61 72 kes the necessar
0560: 79 20 61 64 6a 75 73 74 6d 65 6e 74 20 74 6f 20 y adjustment to
0570: 36 35 35 33 36 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65536..*/.#defin
0580: 65 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 e get2byteNotZer
0590: 6f 28 58 29 20 20 28 28 28 28 28 69 6e 74 29 67 o(X) (((((int)g
05a0: 65 74 32 62 79 74 65 28 58 29 29 2d 31 29 26 30 et2byte(X))-1)&0
05b0: 78 66 66 66 66 29 2b 31 29 0a 0a 2f 2a 0a 2a 2a xffff)+1)../*.**
05c0: 20 56 61 6c 75 65 73 20 70 61 73 73 65 64 20 61 Values passed a
05d0: 73 20 74 68 65 20 35 74 68 20 61 72 67 75 6d 65 s the 5th argume
05e0: 6e 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 42 74 nt to allocateBt
05f0: 72 65 65 50 61 67 65 28 29 0a 2a 2f 0a 23 64 65 reePage().*/.#de
0600: 66 69 6e 65 20 42 54 41 4c 4c 4f 43 5f 41 4e 59 fine BTALLOC_ANY
0610: 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 2f 0 /
0620: 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 79 20 70 * Allocate any p
0630: 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 age */.#define B
0640: 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 31 20 20 TALLOC_EXACT 1
0650: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f /* Allo
0660: 63 61 74 65 20 65 78 61 63 74 20 70 61 67 65 20 cate exact page
0670: 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 23 if possible */.#
0680: 64 65 66 69 6e 65 20 42 54 41 4c 4c 4f 43 5f 4c define BTALLOC_L
0690: 45 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 E 2
06a0: 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 79 /* Allocate any
06b0: 20 70 61 67 65 20 3c 3d 20 74 68 65 20 70 61 72 page <= the par
06c0: 61 6d 65 74 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ameter */../*.**
06d0: 20 4d 61 63 72 6f 20 49 66 4e 6f 74 4f 6d 69 74 Macro IfNotOmit
06e0: 41 56 28 78 29 20 72 65 74 75 72 6e 73 20 28 78 AV(x) returns (x
06f0: 29 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ) if SQLITE_OMIT
0700: 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 6e _AUTOVACUUM is n
0710: 6f 74 20 0a 2a 2a 20 64 65 66 69 6e 65 64 2c 20 ot .** defined,
0720: 6f 72 20 30 20 69 66 20 69 74 20 69 73 2e 20 46 or 0 if it is. F
0730: 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a or example:.**.*
0740: 2a 20 20 20 62 49 6e 63 72 56 61 63 75 75 6d 20 * bIncrVacuum
0750: 3d 20 49 66 4e 6f 74 4f 6d 69 74 41 56 28 70 42 = IfNotOmitAV(pB
0760: 74 53 68 61 72 65 64 2d 3e 69 6e 63 72 56 61 63 tShared->incrVac
0770: 75 75 6d 29 3b 0a 2a 2f 0a 23 69 66 6e 64 65 66 uum);.*/.#ifndef
0780: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
0790: 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 20 OVACUUM.#define
07a0: 49 66 4e 6f 74 4f 6d 69 74 41 56 28 65 78 70 72 IfNotOmitAV(expr
07b0: 29 20 28 65 78 70 72 29 0a 23 65 6c 73 65 0a 23 ) (expr).#else.#
07c0: 64 65 66 69 6e 65 20 49 66 4e 6f 74 4f 6d 69 74 define IfNotOmit
07d0: 41 56 28 65 78 70 72 29 20 30 0a 23 65 6e 64 69 AV(expr) 0.#endi
07e0: 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
07f0: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
0800: 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 CHE./*.** A list
0810: 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 6a of BtShared obj
0820: 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 6c ects that are el
0830: 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 69 igible for parti
0840: 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 cipation.** in s
0850: 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 68 hared cache. Th
0860: 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 is variable has
0870: 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 6e file scope durin
0880: 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c g normal builds,
0890: 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 74 .** but the test
08a0: 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 74 harness needs t
08b0: 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 77 o access it so w
08c0: 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c e make it global
08d0: 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 75 for .** test bu
08e0: 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 ilds..**.** Acce
08f0: 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 ss to this varia
0900: 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65 64 ble is protected
0910: 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 by SQLITE_MUTEX
0920: 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e 0a _STATIC_MASTER..
0930: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
0940: 5f 54 45 53 54 0a 42 74 53 68 61 72 65 64 20 2a _TEST.BtShared *
0950: 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 SQLITE_WSD sqlit
0960: 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
0970: 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 t = 0;.#else.sta
0980: 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51 tic BtShared *SQ
0990: 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 LITE_WSD sqlite3
09a0: 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 SharedCacheList
09b0: 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 = 0;.#endif.#end
09c0: 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
09d0: 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a T_SHARED_CACHE *
09e0: 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
09f0: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
0a00: 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 CHE./*.** Enable
0a10: 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 or disable the
0a20: 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64 shared pager and
0a30: 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73 schema features
0a40: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
0a50: 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65 tine has no effe
0a60: 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64 ct on existing d
0a70: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
0a80: 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 ons..** The shar
0a90: 65 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 ed cache setting
0aa0: 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75 effects only fu
0ab0: 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a ture calls to.**
0ac0: 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c sqlite3_open(),
0ad0: 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
0ae0: 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 ), or sqlite3_op
0af0: 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 69 6e 74 20 en_v2()..*/.int
0b00: 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 sqlite3_enable_s
0b10: 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 20 hared_cache(int
0b20: 65 6e 61 62 6c 65 29 7b 0a 23 69 66 20 64 65 66 enable){.#if def
0b30: 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
0b40: 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
0b50: 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 66 TE_TEST) && !def
0b60: 69 6e 65 64 28 54 48 33 5f 43 4f 4d 50 41 54 49 ined(TH3_COMPATI
0b70: 42 49 4c 49 54 59 29 0a 20 20 2f 2a 20 45 6e 61 BILITY). /* Ena
0b80: 62 6c 65 20 67 6c 6f 62 61 6c 20 73 68 61 72 65 ble global share
0b90: 64 20 63 61 63 68 65 20 66 75 6e 63 74 69 6f 6e d cache function
0ba0: 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61 for debugging a
0bb0: 6e 64 20 75 6e 69 74 20 74 65 73 74 73 2c 20 0a nd unit tests, .
0bc0: 20 20 2a 2a 20 62 75 74 20 6e 6f 74 20 66 6f 72 ** but not for
0bd0: 20 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 72 65 release */. re
0be0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
0bf0: 53 45 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 SE;.#else. sqli
0c00: 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
0c10: 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c sharedCacheEnabl
0c20: 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 ed = enable;. r
0c30: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
0c40: 0a 23 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 .#endif.}.#endif
0c50: 0a 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ....#ifdef SQLIT
0c60: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
0c70: 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 CHE. /*. ** Th
0c80: 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 e functions quer
0c90: 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
0ca0: 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 eLock(), setShar
0cb0: 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
0cc0: 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 (),. ** and cle
0cd0: 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 arAllSharedCache
0ce0: 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a TableLocks(). *
0cf0: 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 * manipulate ent
0d00: 72 69 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 ries in the BtSh
0d10: 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 ared.pLock linke
0d20: 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 d list used to s
0d30: 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 tore. ** shared
0d40: 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 -cache table lev
0d50: 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 el locks. If the
0d60: 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 library is comp
0d70: 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 iled with the.
0d80: 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 ** shared-cache
0d90: 66 65 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 feature disabled
0da0: 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 , then there is
0db0: 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 only ever one us
0dc0: 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 er. ** of each
0dd0: 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
0de0: 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c re and so this l
0df0: 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 ocking is not ne
0e00: 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 cessary. . ** S
0e10: 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 o define the loc
0e20: 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 k related functi
0e30: 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 ons as no-ops..
0e40: 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 */. #define qu
0e50: 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
0e60: 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 bleLock(a,b,c) S
0e70: 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 QLITE_OK. #defi
0e80: 6e 65 20 73 65 74 53 68 61 72 65 64 43 61 63 68 ne setSharedCach
0e90: 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 eTableLock(a,b,c
0ea0: 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 ) SQLITE_OK. #d
0eb0: 65 66 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 efine clearAllSh
0ec0: 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
0ed0: 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 cks(a). #define
0ee0: 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 downgradeAllSha
0ef0: 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
0f00: 6b 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 ks(a). #define
0f10: 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 hasSharedCacheTa
0f20: 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 bleLock(a,b,c,d)
0f30: 20 31 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 1. #define has
0f40: 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c ReadConflicts(a,
0f50: 20 62 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 b) 0.#endif..#i
0f60: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
0f70: 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a T_SHARED_CACHE..
0f80: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
0f90: 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 BUG./*.**** This
0fa0: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c function is onl
0fb0: 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f y used as part o
0fc0: 66 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 f an assert() st
0fd0: 61 74 65 6d 65 6e 74 2e 20 2a 2a 2a 0a 2a 2a 0a atement. ***.**.
0fe0: 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 ** Check to see
0ff0: 69 66 20 70 42 74 72 65 65 20 68 6f 6c 64 73 20 if pBtree holds
1000: 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 the required loc
1010: 6b 73 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 ks to read or wr
1020: 69 74 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 74 ite to the .** t
1030: 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 able with root p
1040: 61 67 65 20 69 52 6f 6f 74 2e 20 20 20 52 65 74 age iRoot. Ret
1050: 75 72 6e 20 31 20 69 66 20 69 74 20 64 6f 65 73 urn 1 if it does
1060: 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a and 0 if not..*
1070: 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 *.** For example
1080: 2c 20 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74 , when writing t
1090: 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 72 o a table with r
10a0: 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 20 76 oot-page iRoot v
10b0: 69 61 20 0a 2a 2a 20 42 74 72 65 65 20 63 6f 6e ia .** Btree con
10c0: 6e 65 63 74 69 6f 6e 20 70 42 74 72 65 65 3a 0a nection pBtree:.
10d0: 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 **.** assert(
10e0: 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 hasSharedCacheT
10f0: 61 62 6c 65 4c 6f 63 6b 28 70 42 74 72 65 65 2c ableLock(pBtree,
1100: 20 69 52 6f 6f 74 2c 20 30 2c 20 57 52 49 54 45 iRoot, 0, WRITE
1110: 5f 4c 4f 43 4b 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 _LOCK) );.**.**
1120: 57 68 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 When writing to
1130: 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20 72 65 an index that re
1140: 73 69 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 sides in a shara
1150: 62 6c 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 ble database, th
1160: 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f e .** caller sho
1170: 75 6c 64 20 68 61 76 65 20 66 69 72 73 74 20 6f uld have first o
1180: 62 74 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 btained a lock s
1190: 70 65 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f pecifying the ro
11a0: 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 ot page of.** th
11b0: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
11c0: 74 61 62 6c 65 2e 20 54 68 69 73 20 6d 61 6b 65 table. This make
11d0: 73 20 74 68 69 6e 67 73 20 61 20 62 69 74 20 6d s things a bit m
11e0: 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2c ore complicated,
11f0: 0a 2a 2a 20 61 73 20 74 68 69 73 20 6d 6f 64 75 .** as this modu
1200: 6c 65 20 74 72 65 61 74 73 20 65 61 63 68 20 74 le treats each t
1210: 61 62 6c 65 20 61 73 20 61 20 73 65 70 61 72 61 able as a separa
1220: 74 65 20 73 74 72 75 63 74 75 72 65 2e 20 54 6f te structure. To
1230: 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 74 68 determine.** th
1240: 65 20 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f e table correspo
1250: 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 nding to the ind
1260: 65 78 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e ex being written
1270: 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 , this.** functi
1280: 6f 6e 20 68 61 73 20 74 6f 20 73 65 61 72 63 68 on has to search
1290: 20 74 68 72 6f 75 67 68 20 74 68 65 20 64 61 74 through the dat
12a0: 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a abase schema..**
12b0: 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 61 .** Instead of a
12c0: 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 lock on the tab
12d0: 6c 65 2f 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 le/index rooted
12e0: 61 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 at page iRoot, t
12f0: 68 65 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a he caller may.**
1300: 20 68 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f hold a write-lo
1310: 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 ck on the schema
1320: 20 74 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 table (root pag
1330: 65 20 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c e 1). This is al
1340: 73 6f 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 so.** acceptable
1350: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1360: 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 hasSharedCacheTa
1370: 62 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 bleLock(. Btree
1380: 20 2a 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 *pBtree,
1390: 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 /* Handle that
13a0: 20 6d 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 must hold lock
13b0: 2a 2f 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c */. Pgno iRoot,
13c0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
13d0: 6f 6f 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 oot page of b-tr
13e0: 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e ee */. int isIn
13f0: 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f dex, /
1400: 2a 20 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 * True if iRoot
1410: 69 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 is the root of a
1420: 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a n index b-tree *
1430: 2f 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 /. int eLockTyp
1440: 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 e /* Re
1450: 71 75 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 quired lock type
1460: 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 (READ_LOCK or W
1470: 52 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b RITE_LOCK) */.){
1480: 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 . Schema *pSche
1490: 6d 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 ma = (Schema *)p
14a0: 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 Btree->pBt->pSch
14b0: 65 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 ema;. Pgno iTab
14c0: 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a = 0;. BtLock *
14d0: 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 pLock;.. /* If
14e0: 74 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73 this database is
14f0: 20 6e 6f 74 20 73 68 61 72 65 61 62 6c 65 2c 20 not shareable,
1500: 6f 72 20 69 66 20 74 68 65 20 63 6c 69 65 6e 74 or if the client
1510: 20 69 73 20 72 65 61 64 69 6e 67 0a 20 20 2a 2a is reading. **
1520: 20 61 6e 64 20 68 61 73 20 74 68 65 20 72 65 61 and has the rea
1530: 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c d-uncommitted fl
1540: 61 67 20 73 65 74 2c 20 74 68 65 6e 20 6e 6f 20 ag set, then no
1550: 6c 6f 63 6b 20 69 73 20 72 65 71 75 69 72 65 64 lock is required
1560: 2e 20 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 . . ** Return t
1570: 72 75 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e rue immediately.
1580: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 . */. if( (pBt
1590: 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ree->sharable==0
15a0: 29 0a 20 20 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 ). || (eLockTy
15b0: 70 65 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 26 26 pe==READ_LOCK &&
15c0: 20 28 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c (pBtree->db->fl
15d0: 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 ags & SQLITE_Rea
15e0: 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 29 0a 20 dUncommitted)).
15f0: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
1600: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1610: 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 he client is rea
1620: 64 69 6e 67 20 20 6f 72 20 77 72 69 74 69 6e 67 ding or writing
1630: 20 61 6e 20 69 6e 64 65 78 20 61 6e 64 20 74 68 an index and th
1640: 65 20 73 63 68 65 6d 61 20 69 73 0a 20 20 2a 2a e schema is. **
1650: 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20 74 68 65 not loaded, the
1660: 6e 20 69 74 20 69 73 20 74 6f 6f 20 64 69 66 66 n it is too diff
1670: 69 63 75 6c 74 20 74 6f 20 61 63 74 75 61 6c 6c icult to actuall
1680: 79 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 y check to see i
1690: 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 72 72 65 f. ** the corre
16a0: 63 74 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c ct locks are hel
16b0: 64 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 20 62 6f d. So do not bo
16c0: 74 68 65 72 20 2d 20 6a 75 73 74 20 72 65 74 75 ther - just retu
16d0: 72 6e 20 74 72 75 65 2e 0a 20 20 2a 2a 20 54 68 rn true.. ** Th
16e0: 69 73 20 63 61 73 65 20 64 6f 65 73 20 6e 6f 74 is case does not
16f0: 20 63 6f 6d 65 20 75 70 20 76 65 72 79 20 6f 66 come up very of
1700: 74 65 6e 20 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f ten anyhow.. */
1710: 0a 20 20 69 66 28 20 69 73 49 6e 64 65 78 20 26 . if( isIndex &
1720: 26 20 28 21 70 53 63 68 65 6d 61 20 7c 7c 20 28 & (!pSchema || (
1730: 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 44 pSchema->flags&D
1740: 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3d B_SchemaLoaded)=
1750: 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 =0) ){. retur
1760: 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 n 1;. }.. /* F
1770: 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f igure out the ro
1780: 6f 74 2d 70 61 67 65 20 74 68 61 74 20 74 68 65 ot-page that the
1790: 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 lock should be
17a0: 68 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62 held on. For tab
17b0: 6c 65 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c le. ** b-trees,
17c0: 20 74 68 69 73 20 69 73 20 6a 75 73 74 20 74 68 this is just th
17d0: 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
17e0: 68 65 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20 he b-tree being
17f0: 72 65 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 69 read or. ** wri
1800: 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 20 tten. For index
1810: 62 2d 74 72 65 65 73 2c 20 69 74 20 69 73 20 74 b-trees, it is t
1820: 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
1830: 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 the associated.
1840: 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 ** table. */.
1850: 20 69 66 28 20 69 73 49 6e 64 65 78 20 29 7b 0a if( isIndex ){.
1860: 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b HashElem *p;
1870: 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 . for(p=sqlit
1880: 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 68 eHashFirst(&pSch
1890: 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 70 ema->idxHash); p
18a0: 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 ; p=sqliteHashNe
18b0: 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 49 6e xt(p)){. In
18c0: 64 65 78 20 2a 70 49 64 78 20 3d 20 28 49 6e 64 dex *pIdx = (Ind
18d0: 65 78 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 ex *)sqliteHashD
18e0: 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 ata(p);. if
18f0: 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 ( pIdx->tnum==(i
1900: 6e 74 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 20 nt)iRoot ){.
1910: 20 20 20 20 69 54 61 62 20 3d 20 70 49 64 78 2d iTab = pIdx-
1920: 3e 70 54 61 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 >pTable->tnum;.
1930: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1940: 65 6c 73 65 7b 0a 20 20 20 20 69 54 61 62 20 3d else{. iTab =
1950: 20 69 52 6f 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f iRoot;. }.. /
1960: 2a 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 * Search for the
1970: 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 required lock.
1980: 45 69 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c Either a write-l
1990: 6f 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 ock on root-page
19a0: 20 69 54 61 62 2c 20 61 20 0a 20 20 2a 2a 20 77 iTab, a . ** w
19b0: 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite-lock on the
19c0: 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f schema table, o
19d0: 72 20 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74 r (if the client
19e0: 20 69 73 20 72 65 61 64 69 6e 67 29 20 61 0a 20 is reading) a.
19f0: 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e ** read-lock on
1a00: 20 69 54 61 62 20 77 69 6c 6c 20 73 75 66 66 69 iTab will suffi
1a10: 63 65 2e 20 52 65 74 75 72 6e 20 31 20 69 66 20 ce. Return 1 if
1a20: 61 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72 65 any of these are
1a30: 20 66 6f 75 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f found. */. fo
1a40: 72 28 70 4c 6f 63 6b 3d 70 42 74 72 65 65 2d 3e r(pLock=pBtree->
1a50: 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 pBt->pLock; pLoc
1a60: 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e k; pLock=pLock->
1a70: 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
1a80: 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 pLock->pBtree==p
1a90: 42 74 72 65 65 20 0a 20 20 20 20 20 26 26 20 28 Btree . && (
1aa0: 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 pLock->iTable==i
1ab0: 54 61 62 20 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 Tab || (pLock->e
1ac0: 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1ad0: 20 26 26 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c && pLock->iTabl
1ae0: 65 3d 3d 31 29 29 0a 20 20 20 20 20 26 26 20 70 e==1)). && p
1af0: 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f Lock->eLock>=eLo
1b00: 63 6b 54 79 70 65 20 0a 20 20 20 20 29 7b 0a 20 ckType . ){.
1b10: 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1b20: 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
1b30: 61 69 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 ailed to find th
1b40: 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e e required lock.
1b50: 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a */. return 0;.
1b60: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1b70: 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 TE_DEBUG */..#if
1b80: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1b90: 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75 ./*.**** This fu
1ba0: 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 nction may be us
1bb0: 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 ed as part of as
1bc0: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
1bd0: 73 20 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0a 2a 2a 0a s only. ****.**.
1be0: 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
1bf0: 66 20 69 74 20 77 6f 75 6c 64 20 62 65 20 69 6c f it would be il
1c00: 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 legal for pBtree
1c10: 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 to write into t
1c20: 68 65 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 he.** table or i
1c30: 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74 20 69 ndex rooted at i
1c40: 52 6f 6f 74 20 62 65 63 61 75 73 65 20 6f 74 68 Root because oth
1c50: 65 72 20 73 68 61 72 65 64 20 63 6f 6e 6e 65 63 er shared connec
1c60: 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 69 6d tions are.** sim
1c70: 75 6c 74 61 6e 65 6f 75 73 6c 79 20 72 65 61 64 ultaneously read
1c80: 69 6e 67 20 74 68 61 74 20 73 61 6d 65 20 74 61 ing that same ta
1c90: 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a ble or index..**
1ca0: 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 .** It is illega
1cb0: 6c 20 66 6f 72 20 70 42 74 72 65 65 20 74 6f 20 l for pBtree to
1cc0: 77 72 69 74 65 20 69 66 20 73 6f 6d 65 20 6f 74 write if some ot
1cd0: 68 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 her Btree object
1ce0: 20 74 68 61 74 0a 2a 2a 20 73 68 61 72 65 73 20 that.** shares
1cf0: 74 68 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 the same BtShare
1d00: 64 20 6f 62 6a 65 63 74 20 69 73 20 63 75 72 72 d object is curr
1d10: 65 6e 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 ently reading or
1d20: 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 writing.** the
1d30: 69 52 6f 6f 74 20 74 61 62 6c 65 2e 20 20 45 78 iRoot table. Ex
1d40: 63 65 70 74 2c 20 69 66 20 74 68 65 20 6f 74 68 cept, if the oth
1d50: 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 er Btree object
1d60: 68 61 73 20 74 68 65 0a 2a 2a 20 72 65 61 64 2d has the.** read-
1d70: 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 uncommitted flag
1d80: 20 73 65 74 2c 20 74 68 65 6e 20 69 74 20 69 73 set, then it is
1d90: 20 4f 4b 20 66 6f 72 20 74 68 65 20 6f 74 68 65 OK for the othe
1da0: 72 20 6f 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 68 r object to.** h
1db0: 61 76 65 20 61 20 72 65 61 64 20 63 75 72 73 6f ave a read curso
1dc0: 72 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 r..**.** For exa
1dd0: 6d 70 6c 65 2c 20 62 65 66 6f 72 65 20 77 72 69 mple, before wri
1de0: 74 69 6e 67 20 74 6f 20 61 6e 79 20 70 61 72 74 ting to any part
1df0: 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 of the table or
1e00: 20 69 6e 64 65 78 0a 2a 2a 20 72 6f 6f 74 65 64 index.** rooted
1e10: 20 61 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 at page iRoot,
1e20: 6f 6e 65 20 73 68 6f 75 6c 64 20 63 61 6c 6c 3a one should call:
1e30: 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 .**.** assert
1e40: 28 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 ( !hasReadConfli
1e50: 63 74 73 28 70 42 74 72 65 65 2c 20 69 52 6f 6f cts(pBtree, iRoo
1e60: 74 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 t) );.*/.static
1e70: 69 6e 74 20 68 61 73 52 65 61 64 43 6f 6e 66 6c int hasReadConfl
1e80: 69 63 74 73 28 42 74 72 65 65 20 2a 70 42 74 72 icts(Btree *pBtr
1e90: 65 65 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 29 7b ee, Pgno iRoot){
1ea0: 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a . BtCursor *p;.
1eb0: 20 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e for(p=pBtree->
1ec0: 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
1ed0: 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
1ee0: 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f if( p->pgnoRoo
1ef0: 74 3d 3d 69 52 6f 6f 74 20 0a 20 20 20 20 20 26 t==iRoot . &
1f00: 26 20 70 2d 3e 70 42 74 72 65 65 21 3d 70 42 74 & p->pBtree!=pBt
1f10: 72 65 65 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 ree. && 0==(
1f20: 70 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 p->pBtree->db->f
1f30: 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 lags & SQLITE_Re
1f40: 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 0a 20 adUncommitted).
1f50: 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ){. retu
1f60: 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
1f70: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 return 0;.}.#e
1f80: 6e 64 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 ndif /* #ifde
1f90: 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a f SQLITE_DEBUG *
1fa0: 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 /../*.** Query t
1fb0: 6f 20 73 65 65 20 69 66 20 42 74 72 65 65 20 68 o see if Btree h
1fc0: 61 6e 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 andle p may obta
1fd0: 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 in a lock of typ
1fe0: 65 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 e eLock .** (REA
1ff0: 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f D_LOCK or WRITE_
2000: 4c 4f 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 LOCK) on the tab
2010: 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 le with root-pag
2020: 65 20 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a e iTab. Return.*
2030: 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 * SQLITE_OK if t
2040: 68 65 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f he lock may be o
2050: 62 74 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c btained (by call
2060: 69 6e 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 ing.** setShared
2070: 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 CacheTableLock()
2080: 29 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 ), or SQLITE_LOC
2090: 4b 45 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 KED if not..*/.s
20a0: 74 61 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 tatic int queryS
20b0: 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
20c0: 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 ock(Btree *p, Pg
20d0: 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 no iTab, u8 eLoc
20e0: 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a k){. BtShared *
20f0: 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
2100: 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a BtLock *pIter;..
2110: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
2120: 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
2130: 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 (p) );. assert(
2140: 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 eLock==READ_LOC
2150: 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 K || eLock==WRIT
2160: 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 E_LOCK );. asse
2170: 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a rt( p->db!=0 );.
2180: 20 20 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 assert( !(p->d
2190: 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f b->flags&SQLITE_
21a0: 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 ReadUncommitted)
21b0: 7c 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c ||eLock==WRITE_L
21c0: 4f 43 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a OCK||iTab==1 );.
21d0: 20 20 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 . /* If reque
21e0: 73 74 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f sting a write-lo
21f0: 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 ck, then the Btr
2200: 65 65 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 ee must have an
2210: 6f 70 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 open write. **
2220: 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 transaction on t
2230: 68 69 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f his file. And, o
2240: 62 76 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 bviously, for th
2250: 69 73 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 is to be so ther
2260: 65 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 e . ** must be
2270: 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 an open write tr
2280: 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 ansaction on the
2290: 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 file itself..
22a0: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f */. assert( eLo
22b0: 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c ck==READ_LOCK ||
22c0: 20 28 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 (p==pBt->pWrite
22d0: 72 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d r && p->inTrans=
22e0: 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b =TRANS_WRITE) );
22f0: 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
2300: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 ==READ_LOCK || p
2310: 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
2320: 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
2330: 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 72 ;. . /* This r
2340: 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
2350: 70 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d p if the shared-
2360: 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 cache is not ena
2370: 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 bled */. if( !p
2380: 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
2390: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
23a0: 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 OK;. }.. /* If
23b0: 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e some other conn
23c0: 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e ection is holdin
23d0: 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c g an exclusive l
23e0: 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65 ock, the. ** re
23f0: 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 quested lock may
2400: 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 not be obtained
2410: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 .. */. if( pBt
2420: 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 ->pWriter!=p &&
2430: 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 (pBt->btsFlags &
2440: 20 42 54 53 5f 45 58 43 4c 55 53 49 56 45 29 21 BTS_EXCLUSIVE)!
2450: 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
2460: 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
2470: 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 ed(p->db, pBt->p
2480: 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 Writer->db);.
2490: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c return SQLITE_L
24a0: 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
24b0: 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 E;. }.. for(pI
24c0: 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 ter=pBt->pLock;
24d0: 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 pIter; pIter=pIt
24e0: 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 er->pNext){.
24f0: 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e /* The condition
2500: 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d (pIter->eLock!=
2510: 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f eLock) in the fo
2520: 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 llowing if(...)
2530: 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e . ** statemen
2540: 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 t is a simplific
2550: 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a 2a ation of:. **
2560: 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 6b . ** (eLock
2570: 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 20 ==WRITE_LOCK ||
2580: 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 pIter->eLock==WR
2590: 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a 2a ITE_LOCK). **
25a0: 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 . ** since we
25b0: 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65 4c know that if eL
25c0: 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 2c ock==WRITE_LOCK,
25d0: 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 63 then no other c
25e0: 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a 2a onnection. **
25f0: 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49 54 may hold a WRIT
2600: 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 61 E_LOCK on any ta
2610: 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ble in this file
2620: 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63 61 (since there ca
2630: 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 65 n. ** only be
2640: 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 a single writer
2650: 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 ).. */. as
2660: 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f sert( pIter->eLo
2670: 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c ck==READ_LOCK ||
2680: 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 pIter->eLock==W
2690: 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 RITE_LOCK );.
26a0: 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
26b0: 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 READ_LOCK || pIt
26c0: 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c er->pBtree==p ||
26d0: 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 pIter->eLock==R
26e0: 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 EAD_LOCK);. i
26f0: 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 f( pIter->pBtree
2700: 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54 !=p && pIter->iT
2710: 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 49 able==iTab && pI
2720: 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 ter->eLock!=eLoc
2730: 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 k ){. sqlit
2740: 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
2750: 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 72 ked(p->db, pIter
2760: 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 ->pBtree->db);.
2770: 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d 3d if( eLock==
2780: 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 WRITE_LOCK ){.
2790: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 3d assert( p=
27a0: 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b =pBt->pWriter );
27b0: 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 62 74 . pBt->bt
27c0: 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 45 sFlags |= BTS_PE
27d0: 4e 44 49 4e 47 3b 0a 20 20 20 20 20 20 7d 0a 20 NDING;. }.
27e0: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
27f0: 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
2800: 43 41 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d CACHE;. }. }
2810: 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
2820: 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a _OK;.}.#endif /*
2830: 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 !SQLITE_OMIT_SH
2840: 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 ARED_CACHE */..#
2850: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
2860: 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
2870: 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b /*.** Add a lock
2880: 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 on the table wi
2890: 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
28a0: 62 6c 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 ble to the share
28b0: 64 2d 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 d-btree used.**
28c0: 62 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 by Btree handle
28d0: 70 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f p. Parameter eLo
28e0: 63 6b 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ck must be eithe
28f0: 72 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a r READ_LOCK or .
2900: 2a 2a 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a ** WRITE_LOCK..*
2910: 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
2920: 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 66 on assumes the f
2930: 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
2940: 20 20 28 61 29 20 54 68 65 20 73 70 65 63 69 66 (a) The specif
2950: 69 65 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74 ied Btree object
2960: 20 70 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 p is connected
2970: 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a to a sharable.**
2980: 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
2990: 28 6f 6e 65 20 77 69 74 68 20 74 68 65 20 42 74 (one with the Bt
29a0: 53 68 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20 Shared.sharable
29b0: 66 6c 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a flag set), and.*
29c0: 2a 0a 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 *.** (b) No ot
29d0: 68 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 her Btree object
29e0: 73 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 s hold a lock th
29f0: 61 74 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 at conflicts.**
2a00: 20 20 20 20 20 20 77 69 74 68 20 74 68 65 20 72 with the r
2a10: 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 equested lock (i
2a20: 2e 65 2e 20 71 75 65 72 79 53 68 61 72 65 64 43 .e. querySharedC
2a30: 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 acheTableLock()
2a40: 68 61 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 has.** alr
2a50: 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 eady been called
2a60: 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51 and returned SQ
2a70: 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK)..**.**
2a80: 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
2a90: 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 urned if the loc
2aa0: 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65 k is added succe
2ab0: 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f ssfully. SQLITE_
2ac0: 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 NOMEM .** is ret
2ad0: 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
2ae0: 63 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e c attempt fails.
2af0: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
2b00: 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
2b10: 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c leLock(Btree *p,
2b20: 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 Pgno iTable, u8
2b30: 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 eLock){. BtSha
2b40: 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
2b50: 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f t;. BtLock *pLo
2b60: 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b ck = 0;. BtLock
2b70: 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 *pIter;.. asse
2b80: 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
2b90: 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
2ba0: 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
2bb0: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 ==READ_LOCK || e
2bc0: 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
2bd0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
2be0: 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 >db!=0 );.. /*
2bf0: 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 A connection wit
2c00: 68 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d h the read-uncom
2c10: 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 20 mitted flag set
2c20: 77 69 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74 will never try t
2c30: 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 o. ** obtain a
2c40: 72 65 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 read-lock using
2c50: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 this function. T
2c60: 68 65 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 he only read-loc
2c70: 6b 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 k obtained. **
2c80: 62 79 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 by a connection
2c90: 69 6e 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 in read-uncommit
2ca0: 74 65 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 ted mode is on t
2cb0: 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
2cc0: 20 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e . ** table, an
2cd0: 64 20 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f d that lock is o
2ce0: 62 74 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65 btained in Btree
2cf0: 42 65 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a BeginTrans(). *
2d00: 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 /. assert( 0==(
2d10: 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c p->db->flags&SQL
2d20: 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
2d30: 74 65 64 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 ted) || eLock==W
2d40: 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 RITE_LOCK );..
2d50: 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
2d60: 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 should only be
2d70: 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 called on a shar
2d80: 61 62 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 able b-tree afte
2d90: 72 20 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 r it . ** has b
2da0: 65 65 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 een determined t
2db0: 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 hat no other b-t
2dc0: 72 65 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 ree holds a conf
2dd0: 6c 69 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a licting lock. *
2de0: 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 /. assert( p->s
2df0: 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 harable );. ass
2e00: 65 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d ert( SQLITE_OK==
2e10: 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
2e20: 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 TableLock(p, iTa
2e30: 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a ble, eLock) );..
2e40: 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 /* First searc
2e50: 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 h the list for a
2e60: 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 n existing lock
2e70: 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a on this table. *
2e80: 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 /. for(pIter=pB
2e90: 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b t->pLock; pIter;
2ea0: 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e pIter=pIter->pN
2eb0: 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 ext){. if( pI
2ec0: 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ter->iTable==iTa
2ed0: 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 ble && pIter->pB
2ee0: 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
2ef0: 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a pLock = pIter;.
2f00: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2f10: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
2f20: 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 the above search
2f30: 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 did not find a
2f40: 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 BtLock struct as
2f50: 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 sociating Btree
2f60: 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c p. ** with tabl
2f70: 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 e iTable, alloca
2f80: 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 te one and link
2f90: 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 it into the list
2fa0: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c .. */. if( !pL
2fb0: 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b ock ){. pLock
2fc0: 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c = (BtLock *)sql
2fd0: 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
2fe0: 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a izeof(BtLock));.
2ff0: 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 if( !pLock )
3000: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
3010: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
3020: 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 }. pLock->iT
3030: 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 able = iTable;.
3040: 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 pLock->pBtree
3050: 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d = p;. pLock-
3060: 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c >pNext = pBt->pL
3070: 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c ock;. pBt->pL
3080: 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d ock = pLock;. }
3090: 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 .. /* Set the B
30a0: 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 tLock.eLock vari
30b0: 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 able to the maxi
30c0: 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 mum of the curre
30d0: 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 nt lock. ** and
30e0: 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
30f0: 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 ock. This means
3100: 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 if a write-lock
3110: 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 was already held
3120: 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 . ** and a read
3130: 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c -lock requested,
3140: 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 we don't incorr
3150: 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 ectly downgrade
3160: 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 the lock.. */.
3170: 20 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c assert( WRITE_L
3180: 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b OCK>READ_LOCK );
3190: 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f . if( eLock>pLo
31a0: 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 ck->eLock ){.
31b0: 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 pLock->eLock =
31c0: 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 eLock;. }.. re
31d0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
31e0: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
31f0: 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
3200: 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
3210: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
3220: 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
3230: 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 Release all the
3240: 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f table locks (lo
3250: 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 cks obtained via
3260: 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 calls to.** the
3270: 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
3280: 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 ableLock() proce
3290: 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42 74 dure) held by Bt
32a0: 72 65 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a ree object p..**
32b0: 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
32c0: 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 42 n assumes that B
32d0: 74 72 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70 tree p has an op
32e0: 65 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 en read or write
32f0: 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
3300: 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 . If it does not
3310: 2c 20 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50 , then the BTS_P
3320: 45 4e 44 49 4e 47 20 66 6c 61 67 0a 2a 2a 20 6d ENDING flag.** m
3330: 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c ay be incorrectl
3340: 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 y cleared..*/.st
3350: 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 atic void clearA
3360: 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
3370: 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 leLocks(Btree *p
3380: 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
3390: 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
33a0: 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d tLock **ppIter =
33b0: 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 &pBt->pLock;..
33c0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
33d0: 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
33e0: 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
33f0: 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 p->sharable || 0
3400: 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 ==*ppIter );. a
3410: 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
3420: 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 s>0 );.. while(
3430: 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 *ppIter ){.
3440: 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 BtLock *pLock =
3450: 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 *ppIter;. ass
3460: 65 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c ert( (pBt->btsFl
3470: 61 67 73 20 26 20 42 54 53 5f 45 58 43 4c 55 53 ags & BTS_EXCLUS
3480: 49 56 45 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e IVE)==0 || pBt->
3490: 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e pWriter==pLock->
34a0: 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 pBtree );. as
34b0: 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 sert( pLock->pBt
34c0: 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c ree->inTrans>=pL
34d0: 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 ock->eLock );.
34e0: 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 if( pLock->pBt
34f0: 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
3500: 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d *ppIter = pLock-
3510: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 >pNext;. as
3520: 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 sert( pLock->iTa
3530: 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d ble!=1 || pLock=
3540: 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 =&p->lock );.
3550: 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 if( pLock->iT
3560: 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 20 able!=1 ){.
3570: 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
3580: 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a pLock);. }.
3590: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
35a0: 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b ppIter = &pLock
35b0: 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
35c0: 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 }.. assert( (p
35d0: 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
35e0: 54 53 5f 50 45 4e 44 49 4e 47 29 3d 3d 30 20 7c TS_PENDING)==0 |
35f0: 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 | pBt->pWriter )
3600: 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 ;. if( pBt->pWr
3610: 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 iter==p ){. p
3620: 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b Bt->pWriter = 0;
3630: 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 . pBt->btsFla
3640: 67 73 20 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c gs &= ~(BTS_EXCL
3650: 55 53 49 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e USIVE|BTS_PENDIN
3660: 47 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 G);. }else if(
3670: 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
3680: 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 n==2 ){. /* T
3690: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
36a0: 63 61 6c 6c 65 64 20 77 68 65 6e 20 42 74 72 65 called when Btre
36b0: 65 20 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e e p is concludin
36c0: 67 20 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72 g its . ** tr
36d0: 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68 ansaction. If th
36e0: 65 72 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 ere currently ex
36f0: 69 73 74 73 20 61 20 77 72 69 74 65 72 2c 20 61 ists a writer, a
3700: 6e 64 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20 nd p is not.
3710: 2a 2a 20 74 68 61 74 20 77 72 69 74 65 72 2c 20 ** that writer,
3720: 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 then the number
3730: 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 of locks held by
3740: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 connections oth
3750: 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 er. ** than t
3760: 68 65 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 he writer must b
3770: 65 20 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 e about to drop
3780: 74 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 to zero. In this
3790: 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74 case. ** set
37a0: 20 74 68 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 the BTS_PENDING
37b0: 20 66 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 flag to 0..
37c0: 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 **. ** If the
37d0: 72 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e re is not curren
37e0: 74 6c 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 tly a writer, th
37f0: 65 6e 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 6d en BTS_PENDING m
3800: 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 ust. ** be ze
3810: 72 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 ro already. So t
3820: 68 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 his next line is
3830: 20 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 harmless in tha
3840: 74 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 t case.. */.
3850: 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 pBt->btsFlags
3860: 20 26 3d 20 7e 42 54 53 5f 50 45 4e 44 49 4e 47 &= ~BTS_PENDING
3870: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
3880: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 his function cha
3890: 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c nges all write-l
38a0: 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 ocks held by Btr
38b0: 65 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c ee p into read-l
38c0: 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ocks..*/.static
38d0: 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c void downgradeAl
38e0: 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
38f0: 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 eLocks(Btree *p)
3900: 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
3910: 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 t = p->pBt;. if
3920: 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d ( pBt->pWriter==
3930: 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 p ){. BtLock
3940: 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d *pLock;. pBt-
3950: 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 >pWriter = 0;.
3960: 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 pBt->btsFlags
3970: 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 49 &= ~(BTS_EXCLUSI
3980: 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 3b VE|BTS_PENDING);
3990: 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 . for(pLock=p
39a0: 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b Bt->pLock; pLock
39b0: 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 ; pLock=pLock->p
39c0: 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 Next){. ass
39d0: 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 ert( pLock->eLoc
39e0: 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
39f0: 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 pLock->pBtree==p
3a00: 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d );. pLock-
3a10: 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f >eLock = READ_LO
3a20: 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a CK;. }. }.}.
3a30: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
3a40: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
3a50: 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 CHE */..static v
3a60: 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 oid releasePage(
3a70: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b MemPage *pPage);
3a80: 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 /* Forward ref
3a90: 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a erence */../*.**
3aa0: 2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 *** This routine
3ab0: 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 is used inside
3ac0: 6f 66 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 of assert() only
3ad0: 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 ****.**.** Veri
3ae0: 66 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 fy that the curs
3af0: 6f 72 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 or holds the mut
3b00: 65 78 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 ex on its BtShar
3b10: 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ed.*/.#ifdef SQL
3b20: 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 ITE_DEBUG.static
3b30: 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 int cursorHolds
3b40: 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a Mutex(BtCursor *
3b50: 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c p){. return sql
3b60: 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
3b70: 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a p->pBt->mutex);.
3b80: 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
3b90: 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f Invalidate the o
3ba0: 76 65 72 66 6c 6f 77 20 63 61 63 68 65 20 6f 66 verflow cache of
3bb0: 20 74 68 65 20 63 75 72 73 6f 72 20 70 61 73 73 the cursor pass
3bc0: 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
3bd0: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 6f 6e 20 argument..** on
3be0: 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 the shared btree
3bf0: 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a structure pBt..
3c00: 2a 2f 0a 23 64 65 66 69 6e 65 20 69 6e 76 61 6c */.#define inval
3c10: 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
3c20: 68 65 28 70 43 75 72 29 20 28 70 43 75 72 2d 3e he(pCur) (pCur->
3c30: 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 42 54 43 curFlags &= ~BTC
3c40: 46 5f 56 61 6c 69 64 4f 76 66 6c 29 0a 0a 2f 2a F_ValidOvfl)../*
3c50: 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 .** Invalidate t
3c60: 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
3c70: 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 -list cache for
3c80: 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e all cursors open
3c90: 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 ed.** on the sha
3ca0: 72 65 64 20 62 74 72 65 65 20 73 74 72 75 63 74 red btree struct
3cb0: 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 ure pBt..*/.stat
3cc0: 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 ic void invalida
3cd0: 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 teAllOverflowCac
3ce0: 68 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 he(BtShared *pBt
3cf0: 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
3d00: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
3d10: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
3d20: 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
3d30: 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 for(p=pBt->pCurs
3d40: 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
3d50: 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 t){. invalida
3d60: 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
3d70: 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 p);. }.}..#ifnd
3d80: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
3d90: 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 54 68 NCRBLOB./*.** Th
3da0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
3db0: 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 alled before mod
3dc0: 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 ifying the conte
3dd0: 6e 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a nts of a table.*
3de0: 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 * to invalidate
3df0: 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 any incrblob cur
3e00: 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6f 70 sors that are op
3e10: 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 en on the.** row
3e20: 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 or one of the r
3e30: 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 ows being modifi
3e40: 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 ed..**.** If arg
3e50: 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 ument isClearTab
3e60: 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e le is true, then
3e70: 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
3e80: 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 ents of the.** t
3e90: 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f able is about to
3ea0: 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 be deleted. In
3eb0: 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 this case invali
3ec0: 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f date all incrblo
3ed0: 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 b.** cursors ope
3ee0: 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 n on any row wit
3ef0: 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 hin the table wi
3f00: 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e th root-page pgn
3f10: 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 oRoot..**.** Oth
3f20: 65 72 77 69 73 65 2c 20 69 66 20 61 72 67 75 6d erwise, if argum
3f30: 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 ent isClearTable
3f40: 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 is false, then
3f50: 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 the row with.**
3f60: 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20 62 65 rowid iRow is be
3f70: 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f 72 20 ing replaced or
3f80: 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 deleted. In this
3f90: 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 case invalidate
3fa0: 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 .** only those i
3fb0: 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 ncrblob cursors
3fc0: 6f 70 65 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 open on that spe
3fd0: 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 cific row..*/.st
3fe0: 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 atic void invali
3ff0: 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
4000: 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a 70 42 ors(. Btree *pB
4010: 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 2f tree, /
4020: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 * The database f
4030: 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a ile to check */.
4040: 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20 20 20 i64 iRow,
4050: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
4060: 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 rowid that migh
4070: 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f t be changing */
4080: 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72 54 61 . int isClearTa
4090: 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ble /* Tr
40a0: 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 61 ue if all rows a
40b0: 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 re being deleted
40c0: 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f */.){. BtCurso
40d0: 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64 r *p;. BtShared
40e0: 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e *pBt = pBtree->
40f0: 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
4100: 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
4110: 4d 75 74 65 78 28 70 42 74 72 65 65 29 20 29 3b Mutex(pBtree) );
4120: 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 . for(p=pBt->pC
4130: 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
4140: 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 28 Next){. if( (
4150: 70 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 p->curFlags & BT
4160: 43 46 5f 49 6e 63 72 62 6c 6f 62 29 21 3d 30 20 CF_Incrblob)!=0
4170: 26 26 20 28 69 73 43 6c 65 61 72 54 61 62 6c 65 && (isClearTable
4180: 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 || p->info.nKey
4190: 3d 3d 69 52 6f 77 29 20 29 7b 0a 20 20 20 20 20 ==iRow) ){.
41a0: 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 p->eState = CUR
41b0: 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 SOR_INVALID;.
41c0: 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a }. }.}..#else.
41d0: 20 20 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69 /* Stub functi
41e0: 6f 6e 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 on when INCRBLOB
41f0: 20 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 is omitted */.
4200: 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 #define invalid
4210: 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f ateIncrblobCurso
4220: 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 rs(x,y,z).#endif
4230: 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
4240: 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 0a INCRBLOB */../*.
4250: 2a 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 ** Set bit pgno
4260: 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e of the BtShared.
4270: 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
4280: 65 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c ec. This is call
4290: 65 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 ed .** when a pa
42a0: 67 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 ge that previous
42b0: 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 ly contained dat
42c0: 61 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 a becomes a free
42d0: 2d 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 70 -list leaf .** p
42e0: 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 age..**.** The B
42f0: 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
4300: 65 6e 74 20 62 69 74 76 65 63 20 65 78 69 73 74 ent bitvec exist
4310: 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 s to work around
4320: 20 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 62 an obscure.** b
4330: 75 67 20 63 61 75 73 65 64 20 62 79 20 74 68 65 ug caused by the
4340: 20 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 20 interaction of
4350: 74 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f 70 two useful IO op
4360: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 72 timizations surr
4370: 6f 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 2d ounding.** free-
4380: 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 3a list leaf pages:
4390: 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e .**.** 1) When
43a0: 20 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c all data is del
43b0: 65 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67 65 eted from a page
43c0: 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 62 65 and the page be
43d0: 63 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 20 comes.** a
43e0: 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
43f0: 61 67 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 age, the page is
4400: 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 not written to
4410: 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
4420: 20 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c 69 (as free-li
4430: 73 74 20 6c 65 61 66 20 70 61 67 65 73 20 63 6f st leaf pages co
4440: 6e 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 ntain no meaning
4450: 66 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 74 ful data). Somet
4460: 69 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 63 imes.** suc
4470: 68 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74 20 h a page is not
4480: 65 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 even journalled
4490: 28 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 (as it will not
44a0: 62 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 be modified,.**
44b0: 20 20 20 20 20 77 68 79 20 62 6f 74 68 65 72 20 why bother
44c0: 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 journalling it?)
44d0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 ..**.** 2) Whe
44e0: 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 n a free-list le
44f0: 61 66 20 70 61 67 65 20 69 73 20 72 65 75 73 65 af page is reuse
4500: 64 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 69 d, its content i
4510: 73 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 20 s not read.**
4520: 20 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 from the data
4530: 62 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e 20 base or written
4540: 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
4550: 69 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 20 ile (why should
4560: 69 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 it.** be, i
4570: 66 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 f it is not at a
4580: 6c 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e ll meaningful?).
4590: 0a 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65 .**.** By themse
45a0: 6c 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69 lves, these opti
45b0: 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66 mizations work f
45c0: 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20 ine and provide
45d0: 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 6f a handy.** perfo
45e0: 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f 20 rmance boost to
45f0: 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 69 bulk delete or i
4600: 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 nsert operations
4610: 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a 2a . However, if.**
4620: 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 a page is moved
4630: 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 to the free-lis
4640: 74 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 t and then reuse
4650: 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d d within the sam
4660: 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
4670: 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 , a problem come
4680: 73 20 75 70 2e 20 49 66 20 74 68 65 20 70 61 67 s up. If the pag
4690: 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c e is not journal
46a0: 6c 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 69 led when.** it i
46b0: 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 s moved to the f
46c0: 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 20 ree-list and it
46d0: 69 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 is also not jour
46e0: 6e 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a 2a nalled when it.*
46f0: 2a 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 * is extracted f
4700: 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 rom the free-lis
4710: 74 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74 68 t and reused, th
4720: 65 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 en the original
4730: 64 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 6c data.** may be l
4740: 6f 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65 6e ost. In the even
4750: 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2c t of a rollback,
4760: 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 it may not be p
4770: 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 ossible.** to re
4780: 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 store the databa
4790: 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e se to its origin
47a0: 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e al configuration
47b0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 ..**.** The solu
47c0: 74 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53 68 tion is the BtSh
47d0: 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 ared.pHasContent
47e0: 20 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 bitvec. Wheneve
47f0: 72 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 r a page is .**
4800: 6d 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 moved to become
4810: 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 a free-list leaf
4820: 20 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72 65 page, the corre
4830: 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 0a sponding bit is.
4840: 2a 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62 69 ** set in the bi
4850: 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 tvec. Whenever a
4860: 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 65 78 leaf page is ex
4870: 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 tracted from the
4880: 20 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f free-list,.** o
4890: 70 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 62 ptimization 2 ab
48a0: 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 64 20 69 ove is omitted i
48b0: 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 f the correspond
48c0: 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65 61 ing bit is alrea
48d0: 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 dy.** set in BtS
48e0: 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
48f0: 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 t. The contents
4900: 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61 72 of the bitvec ar
4910: 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 e cleared.** at
4920: 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 the end of every
4930: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
4940: 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
4950: 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 eSetHasContent(B
4960: 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 tShared *pBt, Pg
4970: 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 no pgno){. int
4980: 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
4990: 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 if( !pBt->pHas
49a0: 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 61 Content ){. a
49b0: 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 70 42 74 ssert( pgno<=pBt
49c0: 2d 3e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 70 ->nPage );. p
49d0: 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
49e0: 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 = sqlite3BitvecC
49f0: 72 65 61 74 65 28 70 42 74 2d 3e 6e 50 61 67 65 reate(pBt->nPage
4a00: 29 3b 0a 20 20 20 20 69 66 28 20 21 70 42 74 2d );. if( !pBt-
4a10: 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b 0a >pHasContent ){.
4a20: 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
4a30: 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
4a40: 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
4a50: 49 54 45 5f 4f 4b 20 26 26 20 70 67 6e 6f 3c 3d ITE_OK && pgno<=
4a60: 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a sqlite3BitvecSiz
4a70: 65 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 e(pBt->pHasConte
4a80: 6e 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 nt) ){. rc =
4a90: 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
4aa0: 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e (pBt->pHasConten
4ab0: 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 t, pgno);. }.
4ac0: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
4ad0: 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 42 74 .** Query the Bt
4ae0: 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 Shared.pHasConte
4af0: 6e 74 20 76 65 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a nt vector..**.**
4b00: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
4b10: 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 s called when a
4b20: 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
4b30: 61 67 65 20 69 73 20 72 65 6d 6f 76 65 64 20 66 age is removed f
4b40: 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 72 65 65 2d rom the.** free-
4b50: 6c 69 73 74 20 66 6f 72 20 72 65 75 73 65 2e 20 list for reuse.
4b60: 49 74 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 It returns false
4b70: 20 69 66 20 69 74 20 69 73 20 73 61 66 65 20 74 if it is safe t
4b80: 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a o retrieve the.*
4b90: 2a 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 * page from the
4ba0: 70 61 67 65 72 20 6c 61 79 65 72 20 77 69 74 68 pager layer with
4bb0: 20 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 the 'no-content
4bc0: 27 20 66 6c 61 67 20 73 65 74 2e 20 54 72 75 65 ' flag set. True
4bd0: 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 otherwise..*/.s
4be0: 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 tatic int btreeG
4bf0: 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 etHasContent(BtS
4c00: 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f hared *pBt, Pgno
4c10: 20 70 67 6e 6f 29 7b 0a 20 20 42 69 74 76 65 63 pgno){. Bitvec
4c20: 20 2a 70 20 3d 20 70 42 74 2d 3e 70 48 61 73 43 *p = pBt->pHasC
4c30: 6f 6e 74 65 6e 74 3b 0a 20 20 72 65 74 75 72 6e ontent;. return
4c40: 20 28 70 20 26 26 20 28 70 67 6e 6f 3e 73 71 6c (p && (pgno>sql
4c50: 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 ite3BitvecSize(p
4c60: 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 ) || sqlite3Bitv
4c70: 65 63 54 65 73 74 28 70 2c 20 70 67 6e 6f 29 29 ecTest(p, pgno))
4c80: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 );.}../*.** Clea
4c90: 72 20 28 64 65 73 74 72 6f 79 29 20 74 68 65 20 r (destroy) the
4ca0: 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e BtShared.pHasCon
4cb0: 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 tent bitvec. Thi
4cc0: 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 s should be.** i
4cd0: 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f nvoked at the co
4ce0: 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61 63 68 nclusion of each
4cf0: 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
4d00: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
4d10: 69 64 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 id btreeClearHas
4d20: 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
4d30: 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 *pBt){. sqlite
4d40: 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
4d50: 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 Bt->pHasContent)
4d60: 3b 0a 20 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e ;. pBt->pHasCon
4d70: 74 65 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a tent = 0;.}../*.
4d80: 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 6f ** Release all o
4d90: 66 20 74 68 65 20 61 70 50 61 67 65 5b 5d 20 70 f the apPage[] p
4da0: 61 67 65 73 20 66 6f 72 20 61 20 63 75 72 73 6f ages for a curso
4db0: 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
4dc0: 64 20 62 74 72 65 65 52 65 6c 65 61 73 65 41 6c d btreeReleaseAl
4dd0: 6c 43 75 72 73 6f 72 50 61 67 65 73 28 42 74 43 lCursorPages(BtC
4de0: 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
4df0: 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
4e00: 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 ; i<=pCur->iPage
4e10: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 ; i++){. rele
4e20: 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
4e30: 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 70 43 Page[i]);. pC
4e40: 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 ur->apPage[i] =
4e50: 30 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 69 0;. }. pCur->i
4e60: 50 61 67 65 20 3d 20 2d 31 3b 0a 7d 0a 0a 0a 2f Page = -1;.}.../
4e70: 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 *.** Save the cu
4e80: 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 rrent cursor pos
4e90: 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 61 72 ition in the var
4ea0: 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e iables BtCursor.
4eb0: 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 74 43 nKey .** and BtC
4ec0: 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 65 20 ursor.pKey. The
4ed0: 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 20 69 cursor's state i
4ee0: 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52 5f s set to CURSOR_
4ef0: 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a 2a 0a REQUIRESEEK..**.
4f00: 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 ** The caller mu
4f10: 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 st ensure that t
4f20: 68 65 20 63 75 72 73 6f 72 20 69 73 20 76 61 6c he cursor is val
4f30: 69 64 20 28 68 61 73 20 65 53 74 61 74 65 3d 3d id (has eState==
4f40: 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 0a 2a 2a CURSOR_VALID).**
4f50: 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
4f60: 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 g this routine.
4f70: 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
4f80: 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 saveCursorPositi
4f90: 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 on(BtCursor *pCu
4fa0: 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 r){. int rc;..
4fb0: 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f assert( CURSOR_
4fc0: 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 VALID==pCur->eSt
4fd0: 61 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ate );. assert(
4fe0: 20 30 3d 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 0==pCur->pKey )
4ff0: 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
5000: 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
5010: 72 29 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 r) );.. rc = sq
5020: 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a lite3BtreeKeySiz
5030: 65 28 70 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e e(pCur, &pCur->n
5040: 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Key);. assert(
5050: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
5060: 20 20 2f 2a 20 4b 65 79 53 69 7a 65 28 29 20 63 /* KeySize() c
5070: 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 0a 20 annot fail */..
5080: 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
5090: 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20 n intKey table,
50a0: 74 68 65 6e 20 74 68 65 20 61 62 6f 76 65 20 63 then the above c
50b0: 61 6c 6c 20 74 6f 20 42 74 72 65 65 4b 65 79 53 all to BtreeKeyS
50c0: 69 7a 65 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65 ize(). ** store
50d0: 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 s the integer ke
50e0: 79 20 69 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e y in pCur->nKey.
50f0: 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
5100: 69 73 20 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a is value is. **
5110: 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71 all that is req
5120: 75 69 72 65 64 2e 20 4f 74 68 65 72 77 69 73 65 uired. Otherwise
5130: 2c 20 69 66 20 70 43 75 72 20 69 73 20 6e 6f 74 , if pCur is not
5140: 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b open on an intK
5150: 65 79 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74 ey. ** table, t
5160: 68 65 6e 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 hen malloc space
5170: 20 66 6f 72 20 61 6e 64 20 73 74 6f 72 65 20 74 for and store t
5180: 68 65 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 he pCur->nKey by
5190: 74 65 73 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a tes of key . **
51a0: 20 64 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 data.. */. if
51b0: 28 20 30 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 ( 0==pCur->apPag
51c0: 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a e[0]->intKey ){.
51d0: 20 20 20 20 76 6f 69 64 20 2a 70 4b 65 79 20 3d void *pKey =
51e0: 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 sqlite3Malloc(
51f0: 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 20 (int)pCur->nKey
5200: 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 20 );. if( pKey
5210: 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
5220: 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 lite3BtreeKey(pC
5230: 75 72 2c 20 30 2c 20 28 69 6e 74 29 70 43 75 72 ur, 0, (int)pCur
5240: 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 ->nKey, pKey);.
5250: 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
5260: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
5270: 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 pCur->pKey = p
5280: 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 Key;. }else
5290: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
52a0: 33 5f 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 3_free(pKey);.
52b0: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
52c0: 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
52d0: 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
52e0: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 }. assert( !p
52f0: 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
5300: 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d intKey || !pCur-
5310: 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 >pKey );.. if(
5320: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
5330: 0a 20 20 20 20 62 74 72 65 65 52 65 6c 65 61 73 . btreeReleas
5340: 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 65 73 28 eAllCursorPages(
5350: 70 43 75 72 29 3b 0a 20 20 20 20 70 43 75 72 2d pCur);. pCur-
5360: 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
5370: 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 20 _REQUIRESEEK;.
5380: 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f }.. invalidateO
5390: 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 verflowCache(pCu
53a0: 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b r);. return rc;
53b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 .}../*.** Save t
53c0: 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 he positions of
53d0: 61 6c 6c 20 63 75 72 73 6f 72 73 20 28 65 78 63 all cursors (exc
53e0: 65 70 74 20 70 45 78 63 65 70 74 29 20 74 68 61 ept pExcept) tha
53f0: 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 0a 2a 2a t are open on.**
5400: 20 74 68 65 20 74 61 62 6c 65 20 20 77 69 74 68 the table with
5410: 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 root-page iRoot
5420: 2e 20 55 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 . Usually, this
5430: 69 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 is called just b
5440: 65 66 6f 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 efore cursor.**
5450: 70 45 78 63 65 70 74 20 69 73 20 75 73 65 64 20 pExcept is used
5460: 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 74 61 to modify the ta
5470: 62 6c 65 20 28 42 74 72 65 65 44 65 6c 65 74 65 ble (BtreeDelete
5480: 28 29 20 6f 72 20 42 74 72 65 65 49 6e 73 65 72 () or BtreeInser
5490: 74 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 t())..*/.static
54a0: 69 6e 74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f int saveAllCurso
54b0: 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 rs(BtShared *pBt
54c0: 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 , Pgno iRoot, Bt
54d0: 43 75 72 73 6f 72 20 2a 70 45 78 63 65 70 74 29 Cursor *pExcept)
54e0: 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
54f0: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
5500: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
5510: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
5520: 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d 3d ssert( pExcept==
5530: 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 0 || pExcept->pB
5540: 74 3d 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 t==pBt );. for(
5550: 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 p=pBt->pCursor;
5560: 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
5570: 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63 65 if( p!=pExce
5580: 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 pt && (0==iRoot
5590: 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d || p->pgnoRoot==
55a0: 69 52 6f 6f 74 29 20 29 7b 0a 20 20 20 20 20 20 iRoot) ){.
55b0: 69 66 28 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 if( p->eState==C
55c0: 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 URSOR_VALID ){.
55d0: 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 int rc =
55e0: 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 saveCursorPositi
55f0: 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 on(p);. i
5600: 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 f( SQLITE_OK!=rc
5610: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
5620: 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
5630: 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
5640: 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
5650: 28 20 70 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a ( p->iPage>0 );.
5660: 20 20 20 20 20 20 20 20 62 74 72 65 65 52 65 6c btreeRel
5670: 65 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 easeAllCursorPag
5680: 65 73 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 es(p);. }.
5690: 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
56a0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
56b0: 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 /*.** Clear the
56c0: 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 current cursor p
56d0: 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 osition..*/.void
56e0: 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
56f0: 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f arCursor(BtCurso
5700: 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 r *pCur){. asse
5710: 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
5720: 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
5730: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 sqlite3_free(pCu
5740: 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 72 r->pKey);. pCur
5750: 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 43 ->pKey = 0;. pC
5760: 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
5770: 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a 0a SOR_INVALID;.}..
5780: 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76 65 /*.** In this ve
5790: 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d 6f rsion of BtreeMo
57a0: 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61 20 veto, pKey is a
57b0: 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65 63 packed index rec
57c0: 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20 69 ord.** such as i
57d0: 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 s generated by t
57e0: 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 he OP_MakeRecord
57f0: 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63 6b opcode. Unpack
5800: 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 61 the.** record a
5810: 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74 72 nd then call Btr
5820: 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
5830: 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 () to do the wor
5840: 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 k..*/.static int
5850: 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 btreeMoveto(.
5860: 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
5870: 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 /* Cursor op
5880: 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 65 20 en on the btree
5890: 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a to be searched *
58a0: 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
58b0: 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b 65 pKey, /* Packe
58c0: 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 74 72 d key if the btr
58d0: 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20 2a ee is an index *
58e0: 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 /. i64 nKey,
58f0: 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
5900: 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 6c 65 er key for table
5910: 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 s. Size of pKey
5920: 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a for indices */.
5930: 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 20 20 int bias,
5940: 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 73 65 /* Bias se
5950: 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 arch to the high
5960: 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 end */. int *p
5970: 52 65 73 20 20 20 20 20 20 20 20 20 20 20 2f 2a Res /*
5980: 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65 Write search re
5990: 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b sults here */.){
59a0: 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
59b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
59c0: 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a 2f 0a Status code */.
59d0: 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
59e0: 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 *pIdxKey; /*
59f0: 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b Unpacked index k
5a00: 65 79 20 2a 2f 0a 20 20 63 68 61 72 20 61 53 70 ey */. char aSp
5a10: 61 63 65 5b 32 30 30 5d 3b 20 20 20 20 20 20 20 ace[200];
5a20: 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 /* Temp space
5a30: 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 74 for pIdxKey - t
5a40: 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 o avoid a malloc
5a50: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 46 72 65 */. char *pFre
5a60: 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 4b e = 0;.. if( pK
5a70: 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ey ){. assert
5a80: 28 20 6e 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e ( nKey==(i64)(in
5a90: 74 29 6e 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 t)nKey );. pI
5aa0: 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 dxKey = sqlite3V
5ab0: 64 62 65 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 dbeAllocUnpacked
5ac0: 52 65 63 6f 72 64 28 0a 20 20 20 20 20 20 20 20 Record(.
5ad0: 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 pCur->pKeyInfo,
5ae0: 61 53 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 aSpace, sizeof(a
5af0: 53 70 61 63 65 29 2c 20 26 70 46 72 65 65 0a 20 Space), &pFree.
5b00: 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 );. if( pI
5b10: 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 dxKey==0 ) retur
5b20: 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
5b30: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 sqlite3VdbeR
5b40: 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 75 72 ecordUnpack(pCur
5b50: 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69 6e 74 ->pKeyInfo, (int
5b60: 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 20 70 49 64 )nKey, pKey, pId
5b70: 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 70 xKey);. if( p
5b80: 49 64 78 4b 65 79 2d 3e 6e 46 69 65 6c 64 3d 3d IdxKey->nField==
5b90: 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
5ba0: 65 33 44 62 46 72 65 65 28 70 43 75 72 2d 3e 70 e3DbFree(pCur->p
5bb0: 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 46 72 KeyInfo->db, pFr
5bc0: 65 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ee);. retur
5bd0: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
5be0: 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d _BKPT;. }. }
5bf0: 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 else{. pIdxKe
5c00: 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 y = 0;. }. rc
5c10: 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
5c20: 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 vetoUnpacked(pCu
5c30: 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 r, pIdxKey, nKey
5c40: 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 , bias, pRes);.
5c50: 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20 if( pFree ){.
5c60: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
5c70: 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e pCur->pKeyInfo->
5c80: 64 62 2c 20 70 46 72 65 65 29 3b 0a 20 20 7d 0a db, pFree);. }.
5c90: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
5ca0: 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 /*.** Restore th
5cb0: 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
5cc0: 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 position it was
5cd0: 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 in (or as close
5ce0: 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a to as possible).
5cf0: 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 ** when saveCurs
5d00: 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 orPosition() was
5d10: 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 called. Note th
5d20: 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c at this call del
5d30: 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 etes the .** sav
5d40: 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f ed position info
5d50: 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 stored by saveC
5d60: 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c ursorPosition(),
5d70: 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 so there can be
5d80: 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 .** at most one
5d90: 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 effective restor
5da0: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
5db0: 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 ) call after eac
5dc0: 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 h .** saveCursor
5dd0: 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 Position()..*/.s
5de0: 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 tatic int btreeR
5df0: 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
5e00: 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 tion(BtCursor *p
5e10: 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
5e20: 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
5e30: 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
5e40: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
5e50: 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
5e60: 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
5e70: 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
5e80: 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
5e90: 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e LT ){. return
5ea0: 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
5eb0: 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 . }. pCur->eSt
5ec0: 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
5ed0: 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 ALID;. rc = btr
5ee0: 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 eeMoveto(pCur, p
5ef0: 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d Cur->pKey, pCur-
5f00: 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d >nKey, 0, &pCur-
5f10: 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 >skipNext);. if
5f20: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
5f30: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
5f40: 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
5f50: 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 . pCur->pKey
5f60: 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
5f70: 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
5f80: 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
5f90: 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
5fa0: 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 SOR_INVALID );.
5fb0: 20 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 if( pCur->ski
5fc0: 70 4e 65 78 74 20 26 26 20 70 43 75 72 2d 3e 65 pNext && pCur->e
5fd0: 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
5fe0: 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 70 43 75 LID ){. pCu
5ff0: 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
6000: 4f 52 5f 53 4b 49 50 4e 45 58 54 3b 0a 20 20 20 OR_SKIPNEXT;.
6010: 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
6020: 72 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 rc;.}..#define r
6030: 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
6040: 74 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e tion(p) \. (p->
6050: 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 eState>=CURSOR_R
6060: 45 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 EQUIRESEEK ? \.
6070: 20 20 20 20 20 20 20 20 62 74 72 65 65 52 65 73 btreeRes
6080: 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
6090: 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 on(p) : \.
60a0: 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f SQLITE_OK)../
60b0: 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 *.** Determine w
60c0: 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 hether or not a
60d0: 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 cursor has moved
60e0: 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 from the positi
60f0: 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 on it.** was las
6100: 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75 t placed at. Cu
6110: 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 rsors can move w
6120: 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 hen the row they
6130: 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a are pointing.**
6140: 20 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f at is deleted o
6150: 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 ut from under th
6160: 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 em..**.** This r
6170: 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
6180: 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 n error code if
6190: 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
61a0: 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e rong. The.** in
61b0: 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 teger *pHasMoved
61c0: 20 69 73 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f is set as follo
61d0: 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a 20 ws:.**.** 0:
61e0: 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
61f0: 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 20 20 20 unchanged.**
6200: 31 3a 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 1: The cursor
6210: 69 73 20 73 74 69 6c 6c 20 70 6f 69 6e 74 69 6e is still pointin
6220: 67 20 61 74 20 74 68 65 20 73 61 6d 65 20 72 6f g at the same ro
6230: 77 2c 20 62 75 74 20 74 68 65 20 70 6f 69 6e 74 w, but the point
6240: 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 ers.** r
6250: 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
6260: 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
6270: 29 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 ) or sqlite3Btre
6280: 65 44 61 74 61 46 65 74 63 68 28 29 0a 2a 2a 20 eDataFetch().**
6290: 20 20 20 20 20 20 20 20 6d 69 67 68 74 20 6e 6f might no
62a0: 77 20 62 65 20 69 6e 76 61 6c 69 64 20 62 65 63 w be invalid bec
62b0: 61 75 73 65 20 6f 66 20 61 20 62 61 6c 61 6e 63 ause of a balanc
62c0: 65 28 29 20 6f 72 20 6f 74 68 65 72 20 63 68 61 e() or other cha
62d0: 6e 67 65 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 nge to the.**
62e0: 20 20 20 20 20 20 62 2d 74 72 65 65 2e 0a 2a 2a b-tree..**
62f0: 20 20 20 20 32 3a 20 20 20 54 68 65 20 63 75 72 2: The cur
6300: 73 6f 72 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 sor is no longer
6310: 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 pointing to the
6320: 20 72 6f 77 2e 20 20 54 68 65 20 72 6f 77 20 6d row. The row m
6330: 69 67 68 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 ight have.**
6340: 20 20 20 20 20 62 65 65 6e 20 64 65 6c 65 74 65 been delete
6350: 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 d out from under
6360: 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a the cursor..*/.
6370: 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
6380: 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 CursorHasMoved(B
6390: 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
63a0: 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a nt *pHasMoved){.
63b0: 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 int rc;.. if(
63c0: 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
63d0: 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 URSOR_VALID ){.
63e0: 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 *pHasMoved =
63f0: 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
6400: 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 LITE_OK;. }. r
6410: 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f c = restoreCurso
6420: 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
6430: 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
6440: 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 32 3b *pHasMoved = 2;
6450: 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
6460: 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
6470: 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
6480: 41 4c 49 44 20 7c 7c 20 4e 45 56 45 52 28 70 43 ALID || NEVER(pC
6490: 75 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 30 29 ur->skipNext!=0)
64a0: 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 ){. *pHasMov
64b0: 65 64 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b ed = 2;. }else{
64c0: 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 . *pHasMoved
64d0: 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
64e0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
64f0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
6500: 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f MIT_AUTOVACUUM./
6510: 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70 61 67 *.** Given a pag
6520: 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 72 65 e number of a re
6530: 67 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 70 gular database p
6540: 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 age, return the
6550: 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 page.** number f
6560: 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d or the pointer-m
6570: 61 70 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e ap page that con
6580: 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20 tains the entry
6590: 66 6f 72 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 for the.** input
65a0: 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a page number..**
65b0: 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 28 6e 6f .** Return 0 (no
65c0: 74 20 61 20 76 61 6c 69 64 20 70 61 67 65 29 20 t a valid page)
65d0: 66 6f 72 20 70 67 6e 6f 3d 3d 31 20 73 69 6e 63 for pgno==1 sinc
65e0: 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f e there is.** no
65f0: 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 61 73 73 pointer map ass
6600: 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61 67 ociated with pag
6610: 65 20 31 2e 20 20 54 68 65 20 69 6e 74 65 67 72 e 1. The integr
6620: 69 74 79 5f 63 68 65 63 6b 20 6c 6f 67 69 63 0a ity_check logic.
6630: 2a 2a 20 72 65 71 75 69 72 65 73 20 74 68 61 74 ** requires that
6640: 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 2a 2c ptrmapPageno(*,
6650: 31 29 21 3d 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 1)!=1..*/.static
6660: 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 65 Pgno ptrmapPage
6670: 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 no(BtShared *pBt
6680: 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
6690: 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 70 int nPagesPerMap
66a0: 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 Page;. Pgno iPt
66b0: 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 73 rMap, ret;. ass
66c0: 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
66d0: 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
66e0: 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e ex) );. if( pgn
66f0: 6f 3c 32 20 29 20 72 65 74 75 72 6e 20 30 3b 0a o<2 ) return 0;.
6700: 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 nPagesPerMapPa
6710: 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c ge = (pBt->usabl
6720: 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 69 50 eSize/5)+1;. iP
6730: 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29 trMap = (pgno-2)
6740: 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 /nPagesPerMapPag
6750: 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72 e;. ret = (iPtr
6760: 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70 Map*nPagesPerMap
6770: 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20 69 66 Page) + 2; . if
6780: 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 ( ret==PENDING_B
6790: 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
67a0: 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a . ret++;. }.
67b0: 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a return ret;.}.
67c0: 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 ./*.** Write an
67d0: 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 entry into the p
67e0: 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a ointer map..**.*
67f0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 * This routine u
6800: 70 64 61 74 65 73 20 74 68 65 20 70 6f 69 6e 74 pdates the point
6810: 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 er map entry for
6820: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 6b 65 page number 'ke
6830: 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 y'.** so that it
6840: 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20 27 65 maps to type 'e
6850: 54 79 70 65 27 20 61 6e 64 20 70 61 72 65 6e 74 Type' and parent
6860: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 70 67 page number 'pg
6870: 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 no'..**.** If *p
6880: 52 43 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 RC is initially
6890: 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f 6e 2d 53 51 non-zero (non-SQ
68a0: 4c 49 54 45 5f 4f 4b 29 20 74 68 65 6e 20 74 68 LITE_OK) then th
68b0: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a is routine is.**
68c0: 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 61 6e a no-op. If an
68d0: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
68e0: 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 he appropriate e
68f0: 72 72 6f 72 20 63 6f 64 65 20 69 73 20 77 72 69 rror code is wri
6900: 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 52 tten.** into *pR
6910: 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 C..*/.static voi
6920: 64 20 70 74 72 6d 61 70 50 75 74 28 42 74 53 68 d ptrmapPut(BtSh
6930: 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
6940: 6b 65 79 2c 20 75 38 20 65 54 79 70 65 2c 20 50 key, u8 eType, P
6950: 67 6e 6f 20 70 61 72 65 6e 74 2c 20 69 6e 74 20 gno parent, int
6960: 2a 70 52 43 29 7b 0a 20 20 44 62 50 61 67 65 20 *pRC){. DbPage
6970: 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 54 68 *pDbPage; /* Th
6980: 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
6990: 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 ge */. u8 *pPtr
69a0: 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 map; /* The
69b0: 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 61 74 pointer map dat
69c0: 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 a */. Pgno iPtr
69d0: 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 map; /* The
69e0: 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
69f0: 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 number */. int
6a00: 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f offset; /
6a10: 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f 69 6e * Offset in poin
6a20: 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a ter map page */.
6a30: 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
6a40: 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
6a50: 64 65 20 66 72 6f 6d 20 73 75 62 66 75 6e 63 74 de from subfunct
6a60: 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69 66 28 20 2a ions */.. if( *
6a70: 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 pRC ) return;..
6a80: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
6a90: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
6aa0: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 >mutex) );. /*
6ab0: 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e The master-journ
6ac0: 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6d al page number m
6ad0: 75 73 74 20 6e 65 76 65 72 20 62 65 20 75 73 65 ust never be use
6ae0: 64 20 61 73 20 61 20 70 6f 69 6e 74 65 72 20 6d d as a pointer m
6af0: 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 73 ap page */. ass
6b00: 65 72 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f 49 ert( 0==PTRMAP_I
6b10: 53 50 41 47 45 28 70 42 74 2c 20 50 45 4e 44 49 SPAGE(pBt, PENDI
6b20: 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
6b30: 29 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 )) );.. assert(
6b40: 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
6b50: 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 );. if( key==0
6b60: 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 ){. *pRC = S
6b70: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
6b80: 50 54 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a PT;. return;.
6b90: 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 }. iPtrmap =
6ba0: 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
6bb0: 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 t, key);. rc =
6bc0: 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
6bd0: 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 pBt->pPager, iPt
6be0: 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b rmap, &pDbPage);
6bf0: 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
6c00: 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 43 E_OK ){. *pRC
6c10: 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 75 72 = rc;. retur
6c20: 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 n;. }. offset
6c30: 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 = PTRMAP_PTROFFS
6c40: 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 ET(iPtrmap, key)
6c50: 3b 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 30 ;. if( offset<0
6c60: 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 ){. *pRC = S
6c70: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
6c80: 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 70 74 72 PT;. goto ptr
6c90: 6d 61 70 5f 65 78 69 74 3b 0a 20 20 7d 0a 20 20 map_exit;. }.
6ca0: 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 3c assert( offset <
6cb0: 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61 62 = (int)pBt->usab
6cc0: 6c 65 53 69 7a 65 2d 35 20 29 3b 0a 20 20 70 50 leSize-5 );. pP
6cd0: 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 trmap = (u8 *)sq
6ce0: 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
6cf0: 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 a(pDbPage);.. i
6d00: 66 28 20 65 54 79 70 65 21 3d 70 50 74 72 6d 61 f( eType!=pPtrma
6d10: 70 5b 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 p[offset] || get
6d20: 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 4byte(&pPtrmap[o
6d30: 66 66 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e ffset+1])!=paren
6d40: 74 20 29 7b 0a 20 20 20 20 54 52 41 43 45 28 28 t ){. TRACE((
6d50: 22 50 54 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 "PTRMAP_UPDATE:
6d60: 25 64 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 %d->(%d,%d)\n",
6d70: 6b 65 79 2c 20 65 54 79 70 65 2c 20 70 61 72 65 key, eType, pare
6d80: 6e 74 29 29 3b 0a 20 20 20 20 2a 70 52 43 3d 20 nt));. *pRC=
6d90: 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
6da0: 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b rWrite(pDbPage);
6db0: 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
6dc0: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
6dd0: 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 pPtrmap[offset]
6de0: 3d 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 70 = eType;. p
6df0: 75 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 ut4byte(&pPtrmap
6e00: 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 [offset+1], pare
6e10: 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a nt);. }. }..
6e20: 70 74 72 6d 61 70 5f 65 78 69 74 3a 0a 20 20 73 ptrmap_exit:. s
6e30: 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
6e40: 28 70 44 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a (pDbPage);.}../*
6e50: 0a 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 .** Read an entr
6e60: 79 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 y from the point
6e70: 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 er map..**.** Th
6e80: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72 69 is routine retri
6e90: 65 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 eves the pointer
6ea0: 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 map entry for p
6eb0: 61 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74 69 age 'key', writi
6ec0: 6e 67 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 61 ng.** the type a
6ed0: 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e nd parent page n
6ee0: 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70 65 umber to *pEType
6ef0: 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 and *pPgno resp
6f00: 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 ectively..** An
6f10: 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
6f20: 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 turned if someth
6f30: 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 ing goes wrong,
6f40: 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 otherwise SQLITE
6f50: 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 _OK..*/.static i
6f60: 6e 74 20 70 74 72 6d 61 70 47 65 74 28 42 74 53 nt ptrmapGet(BtS
6f70: 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f hared *pBt, Pgno
6f80: 20 6b 65 79 2c 20 75 38 20 2a 70 45 54 79 70 65 key, u8 *pEType
6f90: 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a , Pgno *pPgno){.
6fa0: 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
6fb0: 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e e; /* The poin
6fc0: 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a ter map page */.
6fd0: 20 20 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 int iPtrmap;
6fe0: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
6ff0: 6d 61 70 20 70 61 67 65 20 69 6e 64 65 78 20 2a map page index *
7000: 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b /. u8 *pPtrmap;
7010: 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
7020: 72 20 6d 61 70 20 70 61 67 65 20 64 61 74 61 20 r map page data
7030: 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b */. int offset;
7040: 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
7050: 74 20 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 6f t of entry in po
7060: 69 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 inter map */. i
7070: 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
7080: 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
7090: 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
70a0: 20 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d );.. iPtrmap =
70b0: 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 PTRMAP_PAGENO(p
70c0: 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d Bt, key);. rc =
70d0: 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
70e0: 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 (pBt->pPager, iP
70f0: 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 trmap, &pDbPage)
7100: 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b ;. if( rc!=0 ){
7110: 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
7120: 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 }. pPtrmap =
7130: 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 (u8 *)sqlite3Pag
7140: 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 erGetData(pDbPag
7150: 65 29 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 e);.. offset =
7160: 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 PTRMAP_PTROFFSET
7170: 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a (iPtrmap, key);.
7180: 20 20 69 66 28 20 6f 66 66 73 65 74 3c 30 20 29 if( offset<0 )
7190: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 {. sqlite3Pag
71a0: 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 erUnref(pDbPage)
71b0: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
71c0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
71d0: 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
71e0: 6f 66 66 73 65 74 20 3c 3d 20 28 69 6e 74 29 70 offset <= (int)p
71f0: 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 35 Bt->usableSize-5
7200: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
7210: 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 Type!=0 );. *pE
7220: 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f Type = pPtrmap[o
7230: 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 ffset];. if( pP
7240: 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 gno ) *pPgno = g
7250: 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 et4byte(&pPtrmap
7260: 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 [offset+1]);..
7270: 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
7280: 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 f(pDbPage);. if
7290: 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a ( *pEType<1 || *
72a0: 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 75 72 pEType>5 ) retur
72b0: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
72c0: 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 _BKPT;. return
72d0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 SQLITE_OK;.}..#e
72e0: 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 lse /* if define
72f0: 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 d SQLITE_OMIT_AU
7300: 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64 TOVACUUM */. #d
7310: 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 28 efine ptrmapPut(
7320: 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a 20 20 23 64 w,x,y,z,rc). #d
7330: 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 74 28 efine ptrmapGet(
7340: 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f w,x,y,z) SQLITE_
7350: 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 OK. #define ptr
7360: 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 78 2c mapPutOvflPtr(x,
7370: 20 79 2c 20 72 63 29 0a 23 65 6e 64 69 66 0a 0a y, rc).#endif..
7380: 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74 /*.** Given a bt
7390: 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 20 63 ree page and a c
73a0: 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61 ell index (0 mea
73b0: 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 65 6c ns the first cel
73c0: 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 l on.** the page
73d0: 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65 , 1 means the se
73e0: 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73 cond cell, and s
73f0: 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20 o forth) return
7400: 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
7410: 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
7420: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
7430: 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20 tine works only
7440: 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 20 64 for pages that d
7450: 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76 o not contain ov
7460: 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f erflow cells..*/
7470: 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c .#define findCel
7480: 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 2d l(P,I) \. ((P)-
7490: 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d 3e 6d >aData + ((P)->m
74a0: 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 62 79 askPage & get2by
74b0: 74 65 28 26 28 50 29 2d 3e 61 43 65 6c 6c 49 64 te(&(P)->aCellId
74c0: 78 5b 32 2a 28 49 29 5d 29 29 29 0a 23 64 65 66 x[2*(I)]))).#def
74d0: 69 6e 65 20 66 69 6e 64 43 65 6c 6c 76 32 28 44 ine findCellv2(D
74e0: 2c 4d 2c 4f 2c 49 29 20 28 44 2b 28 4d 26 67 65 ,M,O,I) (D+(M&ge
74f0: 74 32 62 79 74 65 28 44 2b 28 4f 2b 32 2a 28 49 t2byte(D+(O+2*(I
7500: 29 29 29 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 ))))).../*.** Th
7510: 69 73 20 61 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 is a more comple
7520: 78 20 76 65 72 73 69 6f 6e 20 6f 66 20 66 69 6e x version of fin
7530: 64 43 65 6c 6c 28 29 20 74 68 61 74 20 77 6f 72 dCell() that wor
7540: 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 67 65 73 20 ks for.** pages
7550: 74 68 61 74 20 64 6f 20 63 6f 6e 74 61 69 6e 20 that do contain
7560: 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a overflow cells..
7570: 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a 66 69 */.static u8 *fi
7580: 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 4d ndOverflowCell(M
7590: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
75a0: 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 nt iCell){. int
75b0: 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 i;. assert( sq
75c0: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
75d0: 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
75e0: 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 70 ex) );. for(i=p
75f0: 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d Page->nOverflow-
7600: 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 1; i>=0; i--){.
7610: 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 6b 20 int k;. k
7620: 3d 20 70 50 61 67 65 2d 3e 61 69 4f 76 66 6c 5b = pPage->aiOvfl[
7630: 69 5d 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 i];. if( k<=i
7640: 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 Cell ){. if
7650: 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 ( k==iCell ){.
7660: 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 return pPa
7670: 67 65 2d 3e 61 70 4f 76 66 6c 5b 69 5d 3b 0a 20 ge->apOvfl[i];.
7680: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 43 65 }. iCe
7690: 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ll--;. }. }.
76a0: 20 20 72 65 74 75 72 6e 20 66 69 6e 64 43 65 6c return findCel
76b0: 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 3b l(pPage, iCell);
76c0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 .}../*.** Parse
76d0: 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 a cell content b
76e0: 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e lock and fill in
76f0: 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f 20 73 74 the CellInfo st
7700: 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 0a ructure. There.
7710: 2a 2a 20 61 72 65 20 74 77 6f 20 76 65 72 73 69 ** are two versi
7720: 6f 6e 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 ons of this func
7730: 74 69 6f 6e 2e 20 20 62 74 72 65 65 50 61 72 73 tion. btreePars
7740: 65 43 65 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 eCell() takes a
7750: 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 .** cell index a
7760: 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
7770: 75 6d 65 6e 74 20 61 6e 64 20 62 74 72 65 65 50 ument and btreeP
7780: 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 0a 2a arseCellPtr() .*
7790: 2a 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 * takes a pointe
77a0: 72 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 r to the body of
77b0: 20 74 68 65 20 63 65 6c 6c 20 61 73 20 69 74 73 the cell as its
77c0: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
77d0: 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 ..**.** Within t
77e0: 68 69 73 20 66 69 6c 65 2c 20 74 68 65 20 70 61 his file, the pa
77f0: 72 73 65 43 65 6c 6c 28 29 20 6d 61 63 72 6f 20 rseCell() macro
7800: 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 69 6e can be called in
7810: 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62 74 72 65 stead of.** btre
7820: 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2e eParseCellPtr().
7830: 20 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 Using some comp
7840: 69 6c 65 72 73 2c 20 74 68 69 73 20 77 69 6c 6c ilers, this will
7850: 20 62 65 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 73 be faster..*/.s
7860: 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
7870: 50 61 72 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 ParseCellPtr(.
7880: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
7890: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
78a0: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 containing the c
78b0: 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 ell */. u8 *pCe
78c0: 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
78d0: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 /* Pointer to t
78e0: 68 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f he cell text. */
78f0: 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e . CellInfo *pIn
7900: 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 fo /* Fi
7910: 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ll in this struc
7920: 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 ture */.){. u16
7930: 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
7940: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 62 /* Number b
7950: 79 74 65 73 20 69 6e 20 63 65 6c 6c 20 63 6f 6e ytes in cell con
7960: 74 65 6e 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 tent header */.
7970: 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 u32 nPayload;
7980: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
7990: 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 63 er of bytes of c
79a0: 65 6c 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a ell payload */..
79b0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
79c0: 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
79d0: 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
79e0: 29 3b 0a 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 65 );.. pInfo->pCe
79f0: 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 61 73 ll = pCell;. as
7a00: 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 sert( pPage->lea
7a10: 66 3d 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6c f==0 || pPage->l
7a20: 65 61 66 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 eaf==1 );. n =
7a30: 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
7a40: 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6e ize;. assert( n
7a50: 3d 3d 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 ==4-4*pPage->lea
7a60: 66 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 f );. if( pPage
7a70: 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
7a80: 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
7a90: 74 61 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ta ){. asse
7aa0: 72 74 28 20 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 rt( n==0 );.
7ab0: 20 20 6e 20 3d 20 67 65 74 56 61 72 69 6e 74 33 n = getVarint3
7ac0: 32 28 70 43 65 6c 6c 2c 20 6e 50 61 79 6c 6f 61 2(pCell, nPayloa
7ad0: 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
7ae0: 20 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 nPayload =
7af0: 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 2b 0;. }. n +
7b00: 3d 20 67 65 74 56 61 72 69 6e 74 28 26 70 43 65 = getVarint(&pCe
7b10: 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a 29 26 70 49 ll[n], (u64*)&pI
7b20: 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 nfo->nKey);.
7b30: 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e pInfo->nData = n
7b40: 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 Payload;. }else
7b50: 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 {. pInfo->nDa
7b60: 74 61 20 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d ta = 0;. n +=
7b70: 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70 43 getVarint32(&pC
7b80: 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 ell[n], nPayload
7b90: 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b );. pInfo->nK
7ba0: 65 79 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 ey = nPayload;.
7bb0: 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 }. pInfo->nPay
7bc0: 6c 6f 61 64 20 3d 20 6e 50 61 79 6c 6f 61 64 3b load = nPayload;
7bd0: 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 . pInfo->nHeade
7be0: 72 20 3d 20 6e 3b 0a 20 20 74 65 73 74 63 61 73 r = n;. testcas
7bf0: 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 e( nPayload==pPa
7c00: 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a ge->maxLocal );.
7c10: 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 testcase( nPay
7c20: 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 load==pPage->max
7c30: 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 Local+1 );. if(
7c40: 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f 61 64 likely(nPayload
7c50: 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 <=pPage->maxLoca
7c60: 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 l) ){. /* Thi
7c70: 73 20 69 73 20 74 68 65 20 28 65 61 73 79 29 20 s is the (easy)
7c80: 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 common case wher
7c90: 65 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 79 e the entire pay
7ca0: 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20 2a 2a load fits. **
7cb0: 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 on the local pa
7cc0: 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 ge. No overflow
7cd0: 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 is required..
7ce0: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 70 49 */. if( (pI
7cf0: 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75 31 nfo->nSize = (u1
7d00: 36 29 28 6e 2b 6e 50 61 79 6c 6f 61 64 29 29 3c 6)(n+nPayload))<
7d10: 34 20 29 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 4 ) pInfo->nSize
7d20: 20 3d 20 34 3b 0a 20 20 20 20 70 49 6e 66 6f 2d = 4;. pInfo-
7d30: 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e >nLocal = (u16)n
7d40: 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e Payload;. pIn
7d50: 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 fo->iOverflow =
7d60: 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
7d70: 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 /* If the payloa
7d80: 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 63 d will not fit c
7d90: 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 68 65 ompletely on the
7da0: 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 65 20 local page, we
7db0: 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64 have. ** to d
7dc0: 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68 20 74 ecide how much t
7dd0: 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 o store locally
7de0: 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 and how much to
7df0: 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a spill onto. *
7e00: 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 * overflow pages
7e10: 2e 20 20 54 68 65 20 73 74 72 61 74 65 67 79 20 . The strategy
7e20: 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 is to minimize t
7e30: 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 he amount of unu
7e40: 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 sed. ** space
7e50: 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 on overflow pag
7e60: 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69 6e 67 es while keeping
7e70: 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6c the amount of l
7e80: 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20 20 20 ocal storage.
7e90: 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 6d ** in between m
7ea0: 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 78 4c inLocal and maxL
7eb0: 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 ocal.. **.
7ec0: 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 63 68 ** Warning: ch
7ed0: 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 20 6f anging the way o
7ee0: 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 verflow payload
7ef0: 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 69 is distributed i
7f00: 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77 61 79 n any. ** way
7f10: 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 will result in
7f20: 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 an incompatible
7f30: 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 20 20 file format..
7f40: 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c */. int minL
7f50: 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 ocal; /* Minimu
7f60: 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c m amount of payl
7f70: 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 oad held locally
7f80: 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 78 4c */. int maxL
7f90: 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69 6d 75 ocal; /* Maximu
7fa0: 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c m amount of payl
7fb0: 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 oad held locally
7fc0: 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75 72 70 */. int surp
7fd0: 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72 66 6c lus; /* Overfl
7fe0: 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61 69 6c ow payload avail
7ff0: 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c 20 73 able for local s
8000: 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20 20 6d torage */.. m
8010: 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d inLocal = pPage-
8020: 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d >minLocal;. m
8030: 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d axLocal = pPage-
8040: 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 73 >maxLocal;. s
8050: 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 urplus = minLoca
8060: 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 2d 20 l + (nPayload -
8070: 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61 67 65 minLocal)%(pPage
8080: 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
8090: 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 e - 4);. test
80a0: 63 61 73 65 28 20 73 75 72 70 6c 75 73 3d 3d 6d case( surplus==m
80b0: 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20 74 axLocal );. t
80c0: 65 73 74 63 61 73 65 28 20 73 75 72 70 6c 75 73 estcase( surplus
80d0: 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a ==maxLocal+1 );.
80e0: 20 20 20 20 69 66 28 20 73 75 72 70 6c 75 73 20 if( surplus
80f0: 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 <= maxLocal ){.
8100: 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 pInfo->nLoc
8110: 61 6c 20 3d 20 28 75 31 36 29 73 75 72 70 6c 75 al = (u16)surplu
8120: 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 s;. }else{.
8130: 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 pInfo->nLoca
8140: 6c 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f 63 61 l = (u16)minLoca
8150: 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e l;. }. pIn
8160: 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 fo->iOverflow =
8170: 28 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e 4c 6f (u16)(pInfo->nLo
8180: 63 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20 70 49 cal + n);. pI
8190: 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49 6e nfo->nSize = pIn
81a0: 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b 20 fo->iOverflow +
81b0: 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 4;. }.}.#define
81c0: 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 parseCell(pPage
81d0: 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 20 , iCell, pInfo)
81e0: 5c 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65 \. btreeParseCe
81f0: 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20 66 llPtr((pPage), f
8200: 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29 2c indCell((pPage),
8210: 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e 66 (iCell)), (pInf
8220: 6f 29 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 o)).static void
8230: 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a btreeParseCell(.
8240: 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
8250: 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 , /* Pag
8260: 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
8270: 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 cell */. int i
8280: 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Cell,
8290: 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 /* The cell i
82a0: 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c ndex. First cel
82b0: 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c l is 0 */. Cell
82c0: 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
82d0: 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
82e0: 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
82f0: 0a 29 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28 .){. parseCell(
8300: 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 pPage, iCell, pI
8310: 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 nfo);.}../*.** C
8320: 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c ompute the total
8330: 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
8340: 20 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 that a Cell nee
8350: 64 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a ds in the cell.*
8360: 2a 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 * data area of t
8370: 68 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 he btree-page.
8380: 54 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 The return numbe
8390: 72 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 r includes the c
83a0: 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 ell.** data head
83b0: 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c er and the local
83c0: 20 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f payload, but no
83d0: 74 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 t any overflow p
83e0: 61 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 age or.** the sp
83f0: 61 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20 ace used by the
8400: 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f cell pointer..*/
8410: 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c .static u16 cell
8420: 53 69 7a 65 50 74 72 28 4d 65 6d 50 61 67 65 20 SizePtr(MemPage
8430: 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c *pPage, u8 *pCel
8440: 6c 29 7b 0a 20 20 75 38 20 2a 70 49 74 65 72 20 l){. u8 *pIter
8450: 3d 20 26 70 43 65 6c 6c 5b 70 50 61 67 65 2d 3e = &pCell[pPage->
8460: 63 68 69 6c 64 50 74 72 53 69 7a 65 5d 3b 0a 20 childPtrSize];.
8470: 20 75 33 32 20 6e 53 69 7a 65 3b 0a 0a 23 69 66 u32 nSize;..#if
8480: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
8490: 0a 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 . /* The value
84a0: 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
84b0: 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
84c0: 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 73 always be the s
84d0: 61 6d 65 20 61 73 0a 20 20 2a 2a 20 74 68 65 20 ame as. ** the
84e0: 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 69 7a 65 29 (CellInfo.nSize)
84f0: 20 76 61 6c 75 65 20 66 6f 75 6e 64 20 62 79 20 value found by
8500: 64 6f 69 6e 67 20 61 20 66 75 6c 6c 20 70 61 72 doing a full par
8510: 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 se of the. ** c
8520: 65 6c 6c 2e 20 49 66 20 53 51 4c 49 54 45 5f 44 ell. If SQLITE_D
8530: 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2c EBUG is defined,
8540: 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 74 20 an assert() at
8550: 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 0a 20 20 the bottom of.
8560: 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
8570: 20 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74 verifies that t
8580: 68 69 73 20 69 6e 76 61 72 69 61 6e 74 20 69 73 his invariant is
8590: 20 6e 6f 74 20 76 69 6f 6c 61 74 65 64 2e 20 2a not violated. *
85a0: 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 64 65 62 /. CellInfo deb
85b0: 75 67 69 6e 66 6f 3b 0a 20 20 62 74 72 65 65 50 uginfo;. btreeP
85c0: 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
85d0: 65 2c 20 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 e, pCell, &debug
85e0: 69 6e 66 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 info);.#endif..
85f0: 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b if( pPage->intK
8600: 65 79 20 29 7b 0a 20 20 20 20 75 38 20 2a 70 45 ey ){. u8 *pE
8610: 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 nd;. if( pPag
8620: 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 e->hasData ){.
8630: 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 pIter += get
8640: 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 Varint32(pIter,
8650: 6e 53 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 nSize);. }els
8660: 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d e{. nSize =
8670: 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 0;. }.. /
8680: 2a 20 70 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e * pIter now poin
8690: 74 73 20 61 74 20 74 68 65 20 36 34 2d 62 69 74 ts at the 64-bit
86a0: 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c integer key val
86b0: 75 65 2c 20 61 20 76 61 72 69 61 62 6c 65 20 6c ue, a variable l
86c0: 65 6e 67 74 68 20 0a 20 20 20 20 2a 2a 20 69 6e ength . ** in
86d0: 74 65 67 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f teger. The follo
86e0: 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 wing block moves
86f0: 20 70 49 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 pIter to point
8700: 61 74 20 74 68 65 20 66 69 72 73 74 20 62 79 74 at the first byt
8710: 65 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 e. ** past th
8720: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6b 65 79 e end of the key
8730: 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 value. */. p
8740: 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b End = &pIter[9];
8750: 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 . while( (*pI
8760: 74 65 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 ter++)&0x80 && p
8770: 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d Iter<pEnd );. }
8780: 65 6c 73 65 7b 0a 20 20 20 20 70 49 74 65 72 20 else{. pIter
8790: 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 += getVarint32(p
87a0: 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 Iter, nSize);.
87b0: 7d 0a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e }.. testcase( n
87c0: 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 Size==pPage->max
87d0: 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 Local );. testc
87e0: 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 ase( nSize==pPag
87f0: 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b e->maxLocal+1 );
8800: 0a 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 . if( nSize>pPa
8810: 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a ge->maxLocal ){.
8820: 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c int minLocal
8830: 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 = pPage->minLoc
8840: 61 6c 3b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 al;. nSize =
8850: 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53 69 7a minLocal + (nSiz
8860: 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 e - minLocal) %
8870: 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 (pPage->pBt->usa
8880: 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 bleSize - 4);.
8890: 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
88a0: 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
88b0: 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 al );. testca
88c0: 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
88d0: 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a ->maxLocal+1 );.
88e0: 20 20 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 if( nSize>pP
88f0: 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b age->maxLocal ){
8900: 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d . nSize = m
8910: 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 inLocal;. }.
8920: 20 20 20 6e 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 nSize += 4;.
8930: 20 7d 0a 20 20 6e 53 69 7a 65 20 2b 3d 20 28 75 }. nSize += (u
8940: 33 32 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 32)(pIter - pCel
8950: 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 69 l);.. /* The mi
8960: 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e nimum size of an
8970: 79 20 63 65 6c 6c 20 69 73 20 34 20 62 79 74 65 y cell is 4 byte
8980: 73 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 53 69 7a s. */. if( nSiz
8990: 65 3c 34 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 e<4 ){. nSize
89a0: 20 3d 20 34 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 = 4;. }.. ass
89b0: 65 72 74 28 20 6e 53 69 7a 65 3d 3d 64 65 62 75 ert( nSize==debu
89c0: 67 69 6e 66 6f 2e 6e 53 69 7a 65 20 29 3b 0a 20 ginfo.nSize );.
89d0: 20 72 65 74 75 72 6e 20 28 75 31 36 29 6e 53 69 return (u16)nSi
89e0: 7a 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 ze;.}..#ifdef SQ
89f0: 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 20 54 68 LITE_DEBUG./* Th
8a00: 69 73 20 76 61 72 69 61 74 69 6f 6e 20 6f 6e 20 is variation on
8a10: 63 65 6c 6c 53 69 7a 65 50 74 72 28 29 20 69 73 cellSizePtr() is
8a20: 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 20 used inside of
8a30: 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
8a40: 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 2a 2f 0a nts.** only. */.
8a50: 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 static u16 cellS
8a60: 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ize(MemPage *pPa
8a70: 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a ge, int iCell){.
8a80: 20 20 72 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a return cellSiz
8a90: 65 50 74 72 28 70 50 61 67 65 2c 20 66 69 6e 64 ePtr(pPage, find
8aa0: 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
8ab0: 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 l));.}.#endif..#
8ac0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
8ad0: 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a IT_AUTOVACUUM./*
8ae0: 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 .** If the cell
8af0: 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 pCell, part of p
8b00: 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 age pPage contai
8b10: 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 ns a pointer.**
8b20: 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 to an overflow p
8b30: 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 age, insert an e
8b40: 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f ntry into the po
8b50: 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 inter-map.** for
8b60: 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
8b70: 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ge..*/.static vo
8b80: 69 64 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c id ptrmapPutOvfl
8b90: 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 Ptr(MemPage *pPa
8ba0: 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 2c 20 69 ge, u8 *pCell, i
8bb0: 6e 74 20 2a 70 52 43 29 7b 0a 20 20 43 65 6c 6c nt *pRC){. Cell
8bc0: 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 69 66 28 Info info;. if(
8bd0: 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a *pRC ) return;.
8be0: 20 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c 21 assert( pCell!
8bf0: 3d 30 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 =0 );. btreePar
8c00: 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
8c10: 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
8c20: 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e assert( (info.
8c30: 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e nData+(pPage->in
8c40: 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 tKey?0:info.nKey
8c50: 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 ))==info.nPayloa
8c60: 64 20 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e d );. if( info.
8c70: 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 iOverflow ){.
8c80: 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 Pgno ovfl = get
8c90: 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 4byte(&pCell[inf
8ca0: 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 o.iOverflow]);.
8cb0: 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 ptrmapPut(pPa
8cc0: 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 ge->pBt, ovfl, P
8cd0: 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c TRMAP_OVERFLOW1,
8ce0: 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 pPage->pgno, pR
8cf0: 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 C);. }.}.#endif
8d00: 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d .../*.** Defragm
8d10: 65 6e 74 20 74 68 65 20 70 61 67 65 20 67 69 76 ent the page giv
8d20: 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 en. All Cells a
8d30: 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a re moved to the.
8d40: 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 ** end of the pa
8d50: 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 ge and all free
8d60: 73 70 61 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 space is collect
8d70: 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 ed into one.** b
8d80: 69 67 20 46 72 65 65 42 6c 6b 20 74 68 61 74 20 ig FreeBlk that
8d90: 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 occurs in betwee
8da0: 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 n the header and
8db0: 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 cell.** pointer
8dc0: 20 61 72 72 61 79 20 61 6e 64 20 74 68 65 20 63 array and the c
8dd0: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
8de0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8df0: 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 4d defragmentPage(M
8e00: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a emPage *pPage){.
8e10: 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
8e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8e30: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
8e40: 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 int pc;
8e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8e60: 41 64 64 72 65 73 73 20 6f 66 20 61 20 69 2d 74 Address of a i-t
8e70: 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 h cell */. int
8e80: 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 hdr;
8e90: 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
8ea0: 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 to the page hea
8eb0: 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a der */. int siz
8ec0: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
8ed0: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 /* Size of a
8ee0: 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 cell */. int u
8ef0: 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 sableSize;
8f00: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
8f10: 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 of usable bytes
8f20: 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 on a page */. i
8f30: 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 nt cellOffset;
8f40: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
8f50: 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 set to the cell
8f60: 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f pointer array */
8f70: 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 . int cbrk;
8f80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8f90: 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 Offset to the c
8fa0: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
8fb0: 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b */. int nCell;
8fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8fd0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 /* Number of ce
8fe0: 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 lls on the page
8ff0: 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
9000: 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 ar *data;
9010: 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61 74 61 /* The page data
9020: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
9030: 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 har *temp;
9040: 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20 66 6f /* Temp area fo
9050: 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a r cell content *
9060: 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 /. int iCellFir
9070: 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f st; /
9080: 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c * First allowabl
9090: 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a e cell index */.
90a0: 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b int iCellLast;
90b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
90c0: 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 Last possible ce
90d0: 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20 20 ll index */...
90e0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
90f0: 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
9100: 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
9110: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
9120: 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 ge->pBt!=0 );.
9130: 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
9140: 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c Bt->usableSize <
9150: 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 = SQLITE_MAX_PAG
9160: 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 E_SIZE );. asse
9170: 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
9180: 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 flow==0 );. ass
9190: 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
91a0: 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
91b0: 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
91c0: 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 61 temp = sqlite3Pa
91d0: 67 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 61 gerTempSpace(pPa
91e0: 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 ge->pBt->pPager)
91f0: 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 ;. data = pPage
9200: 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d ->aData;. hdr =
9210: 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
9220: 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 t;. cellOffset
9230: 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 = pPage->cellOff
9240: 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 set;. nCell = p
9250: 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 Page->nCell;. a
9260: 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 ssert( nCell==ge
9270: 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
9280: 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 +3]) );. usable
9290: 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 Size = pPage->pB
92a0: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 t->usableSize;.
92b0: 20 63 62 72 6b 20 3d 20 67 65 74 32 62 79 74 65 cbrk = get2byte
92c0: 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a (&data[hdr+5]);.
92d0: 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 memcpy(&temp[c
92e0: 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b brk], &data[cbrk
92f0: 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 ], usableSize -
9300: 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 cbrk);. cbrk =
9310: 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 43 usableSize;. iC
9320: 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f ellFirst = cellO
9330: 66 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b ffset + 2*nCell;
9340: 0a 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 . iCellLast = u
9350: 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 sableSize - 4;.
9360: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
9370: 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 l; i++){. u8
9380: 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 *pAddr; /* T
9390: 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 he i-th cell poi
93a0: 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 nter */. pAdd
93b0: 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 r = &data[cellOf
93c0: 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 fset + i*2];.
93d0: 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 pc = get2byte(p
93e0: 41 64 64 72 29 3b 0a 20 20 20 20 74 65 73 74 63 Addr);. testc
93f0: 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 ase( pc==iCellFi
9400: 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 rst );. testc
9410: 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 ase( pc==iCellLa
9420: 73 74 20 29 3b 0a 23 69 66 20 21 64 65 66 69 6e st );.#if !defin
9430: 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
9440: 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 _OVERSIZE_CELL_C
9450: 48 45 43 4b 29 0a 20 20 20 20 2f 2a 20 54 68 65 HECK). /* The
9460: 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 se conditions ha
9470: 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
9480: 76 65 72 69 66 69 65 64 20 69 6e 20 62 74 72 65 verified in btre
9490: 65 49 6e 69 74 50 61 67 65 28 29 0a 20 20 20 20 eInitPage().
94a0: 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ** if SQLITE_ENA
94b0: 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c BLE_OVERSIZE_CEL
94c0: 4c 5f 43 48 45 43 4b 20 69 73 20 64 65 66 69 6e L_CHECK is defin
94d0: 65 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed . */. i
94e0: 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 f( pc<iCellFirst
94f0: 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 || pc>iCellLast
9500: 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
9510: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
9520: 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 BKPT;. }.#end
9530: 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 if. assert( p
9540: 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 26 26 c>=iCellFirst &&
9550: 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 pc<=iCellLast )
9560: 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c ;. size = cel
9570: 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
9580: 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 &temp[pc]);.
9590: 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23 69 cbrk -= size;.#i
95a0: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
95b0: 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 _ENABLE_OVERSIZE
95c0: 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 _CELL_CHECK).
95d0: 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 if( cbrk<iCellF
95e0: 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 72 65 irst ){. re
95f0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
9600: 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
9610: 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 63 62 #else. if( cb
9620: 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c rk<iCellFirst ||
9630: 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 pc+size>usableS
9640: 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ize ){. ret
9650: 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
9660: 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 PT_BKPT;. }.#
9670: 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 endif. assert
9680: 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 ( cbrk+size<=usa
9690: 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 6b 3e bleSize && cbrk>
96a0: 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 =iCellFirst );.
96b0: 20 20 20 74 65 73 74 63 61 73 65 28 20 63 62 72 testcase( cbr
96c0: 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 k+size==usableSi
96d0: 7a 65 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 ze );. testca
96e0: 73 65 28 20 70 63 2b 73 69 7a 65 3d 3d 75 73 61 se( pc+size==usa
96f0: 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d bleSize );. m
9700: 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62 72 6b emcpy(&data[cbrk
9710: 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20 73 69 ], &temp[pc], si
9720: 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 ze);. put2byt
9730: 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 3b 0a e(pAddr, cbrk);.
9740: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 62 }. assert( cb
9750: 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 rk>=iCellFirst )
9760: 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
9770: 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 ta[hdr+5], cbrk)
9780: 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 ;. data[hdr+1]
9790: 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b = 0;. data[hdr+
97a0: 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 2] = 0;. data[h
97b0: 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d dr+7] = 0;. mem
97c0: 73 65 74 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 set(&data[iCellF
97d0: 69 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 irst], 0, cbrk-i
97e0: 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 CellFirst);. as
97f0: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
9800: 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
9810: 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
9820: 0a 20 20 69 66 28 20 63 62 72 6b 2d 69 43 65 6c . if( cbrk-iCel
9830: 6c 46 69 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e lFirst!=pPage->n
9840: 46 72 65 65 20 29 7b 0a 20 20 20 20 72 65 74 75 Free ){. retu
9850: 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
9860: 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 T_BKPT;. }. re
9870: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
9880: 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
9890: 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 e nByte bytes of
98a0: 20 73 70 61 63 65 20 66 72 6f 6d 20 77 69 74 68 space from with
98b0: 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20 70 61 in the B-Tree pa
98c0: 67 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 ge passed.** as
98d0: 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
98e0: 6e 74 2e 20 57 72 69 74 65 20 69 6e 74 6f 20 2a nt. Write into *
98f0: 70 49 64 78 20 74 68 65 20 69 6e 64 65 78 20 69 pIdx the index i
9900: 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 nto pPage->aData
9910: 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 [].** of the fir
9920: 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 st byte of alloc
9930: 61 74 65 64 20 73 70 61 63 65 2e 20 52 65 74 75 ated space. Retu
9940: 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 rn either SQLITE
9950: 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 _OK or.** an err
9960: 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 or code (usually
9970: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 SQLITE_CORRUPT)
9980: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
9990: 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 er guarantees th
99a0: 61 74 20 74 68 65 72 65 20 69 73 20 73 75 66 66 at there is suff
99b0: 69 63 69 65 6e 74 20 73 70 61 63 65 20 74 6f 20 icient space to
99c0: 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f make the.** allo
99d0: 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 72 6f cation. This ro
99e0: 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65 65 64 utine might need
99f0: 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 69 to defragment i
9a00: 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69 6e 67 n order to bring
9a10: 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70 61 63 .** all the spac
9a20: 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f 77 65 e together, howe
9a30: 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ver. This routi
9a40: 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20 75 73 ne will avoid us
9a50: 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ing.** the first
9a60: 20 74 77 6f 20 62 79 74 65 73 20 70 61 73 74 20 two bytes past
9a70: 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 the cell pointer
9a80: 20 61 72 65 61 20 73 69 6e 63 65 20 70 72 65 73 area since pres
9a90: 75 6d 61 62 6c 79 20 74 68 69 73 0a 2a 2a 20 61 umably this.** a
9aa0: 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 62 65 69 llocation is bei
9ab0: 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72 64 65 72 ng made in order
9ac0: 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 65 77 to insert a new
9ad0: 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20 77 69 6c cell, so we wil
9ae0: 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e 64 20 75 70 l.** also end up
9af0: 20 6e 65 65 64 69 6e 67 20 61 20 6e 65 77 20 63 needing a new c
9b00: 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a ell pointer..*/.
9b10: 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 static int alloc
9b20: 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 ateSpace(MemPage
9b30: 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 *pPage, int nBy
9b40: 74 65 2c 20 69 6e 74 20 2a 70 49 64 78 29 7b 0a te, int *pIdx){.
9b50: 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 const int hdr
9b60: 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
9b70: 65 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 et; /* Local
9b80: 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d 3e cache of pPage->
9b90: 68 64 72 4f 66 66 73 65 74 20 2a 2f 0a 20 20 75 hdrOffset */. u
9ba0: 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 8 * const data =
9bb0: 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 pPage->aData;
9bc0: 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 /* Local cac
9bd0: 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 he of pPage->aDa
9be0: 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 ta */. int nFra
9bf0: 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
9c00: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9c10: 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 Number of fragme
9c20: 6e 74 65 64 20 62 79 74 65 73 20 6f 6e 20 70 50 nted bytes on pP
9c30: 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 age */. int top
9c40: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
9c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
9c60: 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 63 First byte of c
9c70: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
9c80: 20 2a 2f 0a 20 20 69 6e 74 20 67 61 70 3b 20 20 */. int gap;
9c90: 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
9ca0: 79 74 65 20 6f 66 20 67 61 70 20 62 65 74 77 65 yte of gap betwe
9cb0: 65 6e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 en cell pointers
9cc0: 20 61 6e 64 20 63 65 6c 6c 20 63 6f 6e 74 65 6e and cell conten
9cd0: 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 t */. int rc;
9ce0: 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
9cf0: 72 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f r return code */
9d00: 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a . int usableSiz
9d10: 65 3b 20 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a e; /* Usable siz
9d20: 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f e of the page */
9d30: 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 . . assert( sq
9d40: 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
9d50: 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
9d60: 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
9d70: 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b t( pPage->pBt );
9d80: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
9d90: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
9da0: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
9db0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 );. assert( nB
9dc0: 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 yte>=0 ); /* Mi
9dd0: 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 nimum cell size
9de0: 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 is 4 */. assert
9df0: 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d ( pPage->nFree>=
9e00: 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73 65 72 nByte );. asser
9e10: 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
9e20: 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 75 73 61 62 low==0 );. usab
9e30: 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e leSize = pPage->
9e40: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
9e50: 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
9e60: 20 3c 20 75 73 61 62 6c 65 53 69 7a 65 2d 38 20 < usableSize-8
9e70: 29 3b 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61 );.. nFrag = da
9e80: 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73 ta[hdr+7];. ass
9e90: 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c ert( pPage->cell
9ea0: 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20 Offset == hdr +
9eb0: 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 12 - 4*pPage->le
9ec0: 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50 af );. gap = pP
9ed0: 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
9ee0: 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c + 2*pPage->nCell
9ef0: 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 ;. top = get2by
9f00: 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61 5b teNotZero(&data[
9f10: 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67 hdr+5]);. if( g
9f20: 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72 6e 20 ap>top ) return
9f30: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
9f40: 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73 65 28 KPT;. testcase(
9f50: 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 gap+2==top );.
9f60: 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 31 testcase( gap+1
9f70: 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 ==top );. testc
9f80: 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b ase( gap==top );
9f90: 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 .. if( nFrag>=6
9fa0: 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 0 ){. /* Alwa
9fb0: 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20 68 69 ys defragment hi
9fc0: 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65 64 20 ghly fragmented
9fd0: 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 63 20 pages */. rc
9fe0: 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 = defragmentPage
9ff0: 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 (pPage);. if(
a000: 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
a010: 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
a020: 79 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61 yteNotZero(&data
a030: 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65 6c 73 [hdr+5]);. }els
a040: 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74 6f 70 e if( gap+2<=top
a050: 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 ){. /* Searc
a060: 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c h the freelist l
a070: 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 72 65 ooking for a fre
a080: 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f 75 67 e slot big enoug
a090: 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a 20 20 h to satisfy .
a0a0: 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 ** the request
a0b0: 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e . The allocation
a0c0: 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20 74 68 is made from th
a0d0: 65 20 66 69 72 73 74 20 66 72 65 65 20 73 6c 6f e first free slo
a0e0: 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 t in . ** the
a0f0: 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 6c 61 list that is la
a100: 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 61 63 rge enough to ac
a110: 63 6f 6d 6d 6f 64 61 74 65 20 69 74 2e 0a 20 20 commodate it..
a120: 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63 2c */. int pc,
a130: 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28 61 addr;. for(a
a140: 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 3d ddr=hdr+1; (pc =
a150: 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
a160: 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72 3d addr]))>0; addr=
a170: 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 pc){. int s
a180: 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
a190: 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 66 /* Size of the f
a1a0: 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 ree slot */.
a1b0: 20 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65 53 if( pc>usableS
a1c0: 69 7a 65 2d 34 20 7c 7c 20 70 63 3c 61 64 64 72 ize-4 || pc<addr
a1d0: 2b 34 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 +4 ){. re
a1e0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
a1f0: 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
a200: 7d 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 }. size = g
a210: 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
a220: 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
a230: 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 size>=nByte ){.
a240: 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 73 int x = s
a250: 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 ize - nByte;.
a260: 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 78 testcase( x
a270: 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20 74 ==4 );. t
a280: 65 73 74 63 61 73 65 28 20 78 3d 3d 33 20 29 3b estcase( x==3 );
a290: 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 3c 34 . if( x<4
a2a0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
a2b0: 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74 Remove the slot
a2c0: 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c from the free-l
a2d0: 69 73 74 2e 20 55 70 64 61 74 65 20 74 68 65 20 ist. Update the
a2e0: 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 number of.
a2f0: 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e 74 65 ** fragmente
a300: 64 20 62 79 74 65 73 20 77 69 74 68 69 6e 20 74 d bytes within t
a310: 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 he page. */.
a320: 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 memcpy(&da
a330: 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b ta[addr], &data[
a340: 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 pc], 2);.
a350: 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d data[hdr+7] =
a360: 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 78 29 (u8)(nFrag + x)
a370: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
a380: 69 66 28 20 73 69 7a 65 2b 70 63 20 3e 20 75 73 if( size+pc > us
a390: 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 ableSize ){.
a3a0: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
a3b0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
a3c0: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
a3d0: 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
a3e0: 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73 20 6f e slot remains o
a3f0: 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e n the free-list.
a400: 20 52 65 64 75 63 65 20 69 74 73 20 73 69 7a 65 Reduce its size
a410: 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20 20 20 to account.
a420: 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 ** for the
a430: 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 62 79 portion used by
a440: 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 the new allocat
a450: 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ion. */.
a460: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
a470: 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 [pc+2], x);.
a480: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 }. *p
a490: 49 64 78 20 3d 20 70 63 20 2b 20 78 3b 0a 20 20 Idx = pc + x;.
a4a0: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
a4b0: 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a ITE_OK;. }.
a4c0: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
a4d0: 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 Check to make su
a4e0: 72 65 20 74 68 65 72 65 20 69 73 20 65 6e 6f 75 re there is enou
a4f0: 67 68 20 73 70 61 63 65 20 69 6e 20 74 68 65 20 gh space in the
a500: 67 61 70 20 74 6f 20 73 61 74 69 73 66 79 0a 20 gap to satisfy.
a510: 20 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 ** the allocati
a520: 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c 20 64 65 66 on. If not, def
a530: 72 61 67 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 ragment.. */.
a540: 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 2b testcase( gap+2+
a550: 6e 42 79 74 65 3d 3d 74 6f 70 20 29 3b 0a 20 20 nByte==top );.
a560: 69 66 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3e if( gap+2+nByte>
a570: 74 6f 70 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 top ){. rc =
a580: 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70 defragmentPage(p
a590: 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
a5a0: 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
a5b0: 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 top = get2byt
a5c0: 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61 5b 68 eNotZero(&data[h
a5d0: 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 dr+5]);. asse
a5e0: 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 rt( gap+nByte<=t
a5f0: 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a op );. }... /*
a600: 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 Allocate memory
a610: 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 69 6e from the gap in
a620: 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 65 6c between the cel
a630: 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a l pointer array.
a640: 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 65 6c ** and the cel
a650: 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 l content area.
a660: 20 54 68 65 20 62 74 72 65 65 49 6e 69 74 50 61 The btreeInitPa
a670: 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20 61 6c ge() call has al
a680: 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c 69 64 ready. ** valid
a690: 61 74 65 64 20 74 68 65 20 66 72 65 65 6c 69 73 ated the freelis
a6a0: 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74 20 74 t. Given that t
a6b0: 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 20 76 he freelist is v
a6c0: 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20 2a 2a alid, there. **
a6d0: 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61 74 20 is no way that
a6e0: 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 the allocation c
a6f0: 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20 74 68 an extend off th
a700: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 e end of the pag
a710: 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73 73 65 e.. ** The asse
a720: 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72 69 66 rt() below verif
a730: 69 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 ies the previous
a740: 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f 0a sentence.. */.
a750: 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a top -= nByte;.
a760: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
a770: 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 [hdr+5], top);.
a780: 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e 42 79 assert( top+nBy
a790: 74 65 20 3c 3d 20 28 69 6e 74 29 70 50 61 67 65 te <= (int)pPage
a7a0: 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
a7b0: 65 20 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 e );. *pIdx = t
a7c0: 6f 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c op;. return SQL
a7d0: 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
a7e0: 20 52 65 74 75 72 6e 20 61 20 73 65 63 74 69 6f Return a sectio
a7f0: 6e 20 6f 66 20 74 68 65 20 70 50 61 67 65 2d 3e n of the pPage->
a800: 61 44 61 74 61 20 74 6f 20 74 68 65 20 66 72 65 aData to the fre
a810: 65 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 elist..** The fi
a820: 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 rst byte of the
a830: 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 new free block i
a840: 73 20 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 s pPage->aDisk[s
a850: 74 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 tart].** and the
a860: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f size of the blo
a870: 63 6b 20 69 73 20 22 73 69 7a 65 22 20 62 79 74 ck is "size" byt
a880: 65 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f es..**.** Most o
a890: 66 20 74 68 65 20 65 66 66 6f 72 74 20 68 65 72 f the effort her
a8a0: 65 20 69 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e e is involved in
a8b0: 20 63 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 coalesing adjac
a8c0: 65 6e 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 ent.** free bloc
a8d0: 6b 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 ks into a single
a8e0: 20 62 69 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e big free block.
a8f0: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
a900: 72 65 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 reeSpace(MemPage
a910: 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 73 74 61 *pPage, int sta
a920: 72 74 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 rt, int size){.
a930: 20 69 6e 74 20 61 64 64 72 2c 20 70 62 65 67 69 int addr, pbegi
a940: 6e 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c n, hdr;. int iL
a950: 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ast;
a960: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
a970: 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 argest possible
a980: 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 freeblock offset
a990: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
a9a0: 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 har *data = pPag
a9b0: 65 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 e->aData;.. ass
a9c0: 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 ert( pPage->pBt!
a9d0: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
a9e0: 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
a9f0: 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
aa00: 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
aa10: 65 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67 ert( start>=pPag
aa20: 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 e->hdrOffset+6+p
aa30: 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
aa40: 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ze );. assert(
aa50: 28 73 74 61 72 74 20 2b 20 73 69 7a 65 29 20 3c (start + size) <
aa60: 3d 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 = (int)pPage->pB
aa70: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b t->usableSize );
aa80: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
aa90: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
aaa0: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
aab0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 );. assert( si
aac0: 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d 69 ze>=0 ); /* Mi
aad0: 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 nimum cell size
aae0: 69 73 20 34 20 2a 2f 0a 0a 20 20 69 66 28 20 70 is 4 */.. if( p
aaf0: 50 61 67 65 2d 3e 70 42 74 2d 3e 62 74 73 46 6c Page->pBt->btsFl
ab00: 61 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 ags & BTS_SECURE
ab10: 5f 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 2f _DELETE ){. /
ab20: 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 * Overwrite dele
ab30: 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ted information
ab40: 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 with zeros when
ab50: 74 68 65 20 73 65 63 75 72 65 5f 64 65 6c 65 74 the secure_delet
ab60: 65 0a 20 20 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 e. ** option
ab70: 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 is enabled */.
ab80: 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 73 memset(&data[s
ab90: 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b tart], 0, size);
aba0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 . }.. /* Add t
abb0: 68 65 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e he space back in
abc0: 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 to the linked li
abd0: 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 st of freeblocks
abe0: 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a . Note that. *
abf0: 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 * even though th
ac00: 65 20 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 e freeblock list
ac10: 20 77 61 73 20 63 68 65 63 6b 65 64 20 62 79 20 was checked by
ac20: 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c btreeInitPage(),
ac30: 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 . ** btreeInitP
ac40: 61 67 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65 age() did not de
ac50: 74 65 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 tect overlapping
ac60: 20 63 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 cells or. ** f
ac70: 72 65 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f reeblocks that o
ac80: 76 65 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e verlapped cells.
ac90: 20 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 Nor does it d
aca0: 65 74 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20 etect when the.
acb0: 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 ** cell content
acc0: 20 61 72 65 61 20 65 78 63 65 65 64 73 20 74 68 area exceeds th
acd0: 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 e value in the p
ace0: 61 67 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 age header. If
acf0: 74 68 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 these. ** situa
ad00: 74 69 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 tions arise, the
ad10: 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 n subsequent ins
ad20: 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d ert operations m
ad30: 69 67 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a ight corrupt. *
ad40: 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 * the freelist.
ad50: 20 53 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74 So we do need t
ad60: 6f 20 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 o check for corr
ad70: 75 70 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 uption while sca
ad80: 6e 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 nning. ** the f
ad90: 72 65 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 reelist.. */.
ada0: 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
adb0: 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d Offset;. addr =
adc0: 20 68 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 hdr + 1;. iLas
add0: 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e t = pPage->pBt->
ade0: 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a usableSize - 4;.
adf0: 20 20 61 73 73 65 72 74 28 20 73 74 61 72 74 3c assert( start<
ae00: 3d 69 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c =iLast );. whil
ae10: 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 e( (pbegin = get
ae20: 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 2byte(&data[addr
ae30: 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 ]))<start && pbe
ae40: 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 gin>0 ){. if(
ae50: 20 70 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 pbegin<addr+4 )
ae60: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
ae70: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
ae80: 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 PT;. }. ad
ae90: 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d dr = pbegin;. }
aea0: 0a 20 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c . if( pbegin>iL
aeb0: 61 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ast ){. retur
aec0: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
aed0: 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 _BKPT;. }. ass
aee0: 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 ert( pbegin>addr
aef0: 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b || pbegin==0 );
af00: 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
af10: 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b a[addr], start);
af20: 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
af30: 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e a[start], pbegin
af40: 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 );. put2byte(&d
af50: 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 ata[start+2], si
af60: 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 ze);. pPage->nF
af70: 72 65 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 ree = pPage->nFr
af80: 65 65 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a ee + (u16)size;.
af90: 0a 20 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 . /* Coalesce a
afa0: 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f djacent free blo
afb0: 63 6b 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 cks */. addr =
afc0: 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 hdr + 1;. while
afd0: 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 ( (pbegin = get2
afe0: 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
aff0: 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 ))>0 ){. int
b000: 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b pnext, psize, x;
b010: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 . assert( pbe
b020: 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 gin>addr );.
b030: 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 20 3c assert( pbegin <
b040: 3d 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 = (int)pPage->pB
b050: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 t->usableSize-4
b060: 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 );. pnext = g
b070: 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 et2byte(&data[pb
b080: 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a egin]);. psiz
b090: 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 e = get2byte(&da
b0a0: 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 ta[pbegin+2]);.
b0b0: 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 if( pbegin +
b0c0: 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 psize + 3 >= pne
b0d0: 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b xt && pnext>0 ){
b0e0: 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 . int frag
b0f0: 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 = pnext - (pbegi
b100: 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 n+psize);.
b110: 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20 if( (frag<0) ||
b120: 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b (frag>(int)data[
b130: 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20 20 20 hdr+7]) ){.
b140: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
b150: 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
b160: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74 }. dat
b170: 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75 38 29 a[hdr+7] -= (u8)
b180: 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20 3d 20 frag;. x =
b190: 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
b1a0: 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20 70 75 next]);. pu
b1b0: 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 t2byte(&data[pbe
b1c0: 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 gin], x);.
b1d0: 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65 74 32 x = pnext + get2
b1e0: 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 byte(&data[pnext
b1f0: 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b 0a 20 +2]) - pbegin;.
b200: 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 put2byte(&d
b210: 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c 20 78 ata[pbegin+2], x
b220: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
b230: 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 addr = pbegi
b240: 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 n;. }. }..
b250: 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 /* If the cell c
b260: 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65 67 69 ontent area begi
b270: 6e 73 20 77 69 74 68 20 61 20 66 72 65 65 62 6c ns with a freebl
b280: 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 ock, remove it.
b290: 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64 */. if( data[hd
b2a0: 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 r+1]==data[hdr+5
b2b0: 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d ] && data[hdr+2]
b2c0: 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b ==data[hdr+6] ){
b2d0: 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 . int top;.
b2e0: 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 pbegin = get2b
b2f0: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
b300: 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
b310: 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74 ata[hdr+1], &dat
b320: 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20 a[pbegin], 2);.
b330: 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 top = get2byt
b340: 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 e(&data[hdr+5])
b350: 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 + get2byte(&data
b360: 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 [pbegin+2]);.
b370: 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
b380: 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 hdr+5], top);.
b390: 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 }. assert( sqli
b3a0: 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
b3b0: 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
b3c0: 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ge) );. return
b3d0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
b3e0: 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20 66 .** Decode the f
b3f0: 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 20 66 lags byte (the f
b400: 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
b410: 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 20 70 header) for a p
b420: 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 age.** and initi
b430: 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66 20 alize fields of
b440: 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75 the MemPage stru
b450: 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67 6c cture accordingl
b460: 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 y..**.** Only th
b470: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 e following comb
b480: 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75 70 inations are sup
b490: 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69 6e ported. Anythin
b4a0: 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 69 g different.** i
b4b0: 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 72 75 ndicates a corru
b4c0: 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
b4d0: 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 s:.**.**
b4e0: 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a 2a PTF_ZERODATA.**
b4f0: 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 PTF_ZER
b500: 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 46 ODATA | PTF_LEAF
b510: 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
b520: 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
b530: 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20 20 NTKEY.**
b540: 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 PTF_LEAFDATA |
b550: 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54 46 PTF_INTKEY | PTF
b560: 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63 20 _LEAF.*/.static
b570: 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73 28 int decodeFlags(
b580: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
b590: 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a 20 int flagByte){.
b5a0: 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 BtShared *pBt;
b5b0: 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 /* A copy of
b5c0: 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a 0a pPage->pBt */..
b5d0: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
b5e0: 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50 61 >hdrOffset==(pPa
b5f0: 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 ge->pgno==1 ? 10
b600: 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73 65 0 : 0) );. asse
b610: 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
b620: 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
b630: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 t->mutex) );. p
b640: 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 75 38 Page->leaf = (u8
b650: 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 3b 20 )(flagByte>>3);
b660: 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c 45 41 assert( PTF_LEA
b670: 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 20 66 F == 1<<3 );. f
b680: 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 46 5f lagByte &= ~PTF_
b690: 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d 3e 63 LEAF;. pPage->c
b6a0: 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 34 2d hildPtrSize = 4-
b6b0: 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 4*pPage->leaf;.
b6c0: 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 pBt = pPage->pB
b6d0: 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 79 74 t;. if( flagByt
b6e0: 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 54 41 e==(PTF_LEAFDATA
b6f0: 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 20 29 | PTF_INTKEY) )
b700: 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 {. pPage->int
b710: 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 Key = 1;. pPa
b720: 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 70 50 ge->hasData = pP
b730: 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 70 age->leaf;. p
b740: 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d Page->maxLocal =
b750: 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b 0a 20 pBt->maxLeaf;.
b760: 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 pPage->minLoc
b770: 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 al = pBt->minLea
b780: 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 f;. }else if( f
b790: 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a 45 52 lagByte==PTF_ZER
b7a0: 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70 50 61 ODATA ){. pPa
b7b0: 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 3b 0a ge->intKey = 0;.
b7c0: 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 pPage->hasDa
b7d0: 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ta = 0;. pPag
b7e0: 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 e->maxLocal = pB
b7f0: 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 t->maxLocal;.
b800: 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
b810: 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c = pBt->minLocal
b820: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
b830: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
b840: 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
b850: 20 70 50 61 67 65 2d 3e 6d 61 78 31 62 79 74 65 pPage->max1byte
b860: 50 61 79 6c 6f 61 64 20 3d 20 70 42 74 2d 3e 6d Payload = pBt->m
b870: 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 3b 0a ax1bytePayload;.
b880: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
b890: 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 OK;.}../*.** Ini
b8a0: 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 69 tialize the auxi
b8b0: 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f liary informatio
b8c0: 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c 6f n for a disk blo
b8d0: 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ck..**.** Return
b8e0: 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
b8f0: 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 65 ccess. If we se
b900: 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
b910: 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 does.** not cont
b920: 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 ain a well-forme
b930: 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c d database page,
b940: 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a 2a then return .**
b950: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e SQLITE_CORRUPT.
b960: 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 65 Note that a re
b970: 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f 4f turn of SQLITE_O
b980: 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 75 K does not.** gu
b990: 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 arantee that the
b9a0: 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 6f page is well-fo
b9b0: 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 73 rmed. It only s
b9c0: 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 20 hows that.** we
b9d0: 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 74 failed to detect
b9e0: 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 2e any corruption.
b9f0: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
ba00: 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d treeInitPage(Mem
ba10: 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a 20 Page *pPage){..
ba20: 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
ba30: 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pBt!=0 );. asse
ba40: 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
ba50: 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
ba60: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
ba70: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 ssert( pPage->pg
ba80: 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 no==sqlite3Pager
ba90: 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 Pagenumber(pPage
baa0: 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
bab0: 61 73 73 65 72 74 28 20 70 50 61 67 65 20 3d 3d assert( pPage ==
bac0: 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
bad0: 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 Extra(pPage->pDb
bae0: 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
baf0: 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 t( pPage->aData
bb00: 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 == sqlite3PagerG
bb10: 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 etData(pPage->pD
bb20: 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66 28 bPage) );.. if(
bb30: 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 !pPage->isInit
bb40: 29 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20 20 ){. u16 pc;
bb50: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
bb60: 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 62 6c ress of a freebl
bb70: 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67 65 ock within pPage
bb80: 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 ->aData[] */.
bb90: 20 75 38 20 68 64 72 3b 20 20 20 20 20 20 20 20 u8 hdr;
bba0: 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
bbb0: 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 61 beginning of pa
bbc0: 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 ge header */.
bbd0: 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 u8 *data;
bbe0: 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f 20 /* Equal to
bbf0: 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a pPage->aData */.
bc00: 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 BtShared *pB
bc10: 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 t; /* The
bc20: 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72 75 main btree stru
bc30: 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 69 6e 74 cture */. int
bc40: 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 usableSize;
bc50: 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 61 /* Amount of usa
bc60: 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 63 ble space on eac
bc70: 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 h page */. u1
bc80: 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 6 cellOffset;
bc90: 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 /* Offset from
bca0: 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 74 6f start of page to
bcb0: 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e first cell poin
bcc0: 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e ter */. int n
bcd0: 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Free; /*
bce0: 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 Number of unuse
bcf0: 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 d bytes on the p
bd00: 61 67 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 age */. int t
bd10: 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a op; /*
bd20: 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 First byte of t
bd30: 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
bd40: 61 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 area */. int
bd50: 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 2f iCellFirst; /
bd60: 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c * First allowabl
bd70: 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c e cell or freebl
bd80: 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 ock offset */.
bd90: 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b int iCellLast;
bda0: 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 /* Last pos
bdb0: 73 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 sible cell or fr
bdc0: 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a eeblock offset *
bdd0: 2f 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 /.. pBt = pPa
bde0: 67 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 ge->pBt;.. hd
bdf0: 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
be00: 66 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d fset;. data =
be10: 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
be20: 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 if( decodeFla
be30: 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 gs(pPage, data[h
be40: 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 dr]) ) return SQ
be50: 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
be60: 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 T;. assert( p
be70: 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 Bt->pageSize>=51
be80: 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 2 && pBt->pageSi
be90: 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 20 20 ze<=65536 );.
bea0: 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 pPage->maskPage
beb0: 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 70 61 = (u16)(pBt->pa
bec0: 67 65 53 69 7a 65 20 2d 20 31 29 3b 0a 20 20 20 geSize - 1);.
bed0: 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
bee0: 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c w = 0;. usabl
bef0: 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 eSize = pBt->usa
bf00: 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 bleSize;. pPa
bf10: 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d ge->cellOffset =
bf20: 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 cellOffset = hd
bf30: 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 r + 12 - 4*pPage
bf40: 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 ->leaf;. pPag
bf50: 65 2d 3e 61 44 61 74 61 45 6e 64 20 3d 20 26 64 e->aDataEnd = &d
bf60: 61 74 61 5b 75 73 61 62 6c 65 53 69 7a 65 5d 3b ata[usableSize];
bf70: 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 43 65 6c . pPage->aCel
bf80: 6c 49 64 78 20 3d 20 26 64 61 74 61 5b 63 65 6c lIdx = &data[cel
bf90: 6c 4f 66 66 73 65 74 5d 3b 0a 20 20 20 20 74 6f lOffset];. to
bfa0: 70 20 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a p = get2byteNotZ
bfb0: 65 72 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d ero(&data[hdr+5]
bfc0: 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 );. pPage->nC
bfd0: 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 ell = get2byte(&
bfe0: 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 data[hdr+3]);.
bff0: 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 if( pPage->nCe
c000: 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 ll>MX_CELL(pBt)
c010: 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d ){. /* To m
c020: 61 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 any cells for a
c030: 73 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 single page. Th
c040: 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 e page must be c
c050: 6f 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 orrupt */.
c060: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
c070: 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
c080: 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 }. testcase(
c090: 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 pPage->nCell==MX
c0a0: 5f 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a 20 _CELL(pBt) );..
c0b0: 20 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d 65 /* A malforme
c0c0: 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 d database page
c0d0: 6d 69 67 68 74 20 63 61 75 73 65 20 75 73 20 74 might cause us t
c0e0: 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65 20 o read past the
c0f0: 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70 61 end. ** of pa
c100: 67 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 ge when parsing
c110: 61 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a 2a a cell. . **
c120: 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c . ** The foll
c130: 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 63 owing block of c
c140: 6f 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c 79 ode checks early
c150: 20 74 6f 20 73 65 65 20 69 66 20 61 20 63 65 6c to see if a cel
c160: 6c 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a 2a l extends. **
c170: 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
c180: 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 79 a page boundary
c190: 20 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c 49 and causes SQLI
c1a0: 54 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62 65 TE_CORRUPT to be
c1b0: 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 . ** returne
c1c0: 64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20 20 d if it does..
c1d0: 20 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46 69 */. iCellFi
c1e0: 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 rst = cellOffset
c1f0: 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c + 2*pPage->nCel
c200: 6c 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73 74 l;. iCellLast
c210: 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 = usableSize -
c220: 34 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 4;.#if defined(S
c230: 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 QLITE_ENABLE_OVE
c240: 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b RSIZE_CELL_CHECK
c250: 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e ). {. in
c260: 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
c270: 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74 68 /* Index into th
c280: 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
c290: 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e rray */. in
c2a0: 74 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 t sz;
c2b0: 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c /* Size of a cel
c2c0: 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 l */.. if(
c2d0: 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 !pPage->leaf ) i
c2e0: 43 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20 20 CellLast--;.
c2f0: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
c300: 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ge->nCell; i++){
c310: 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 65 . pc = ge
c320: 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c t2byte(&data[cel
c330: 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a 20 lOffset+i*2]);.
c340: 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
c350: 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 pc==iCellFirst
c360: 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
c370: 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 ase( pc==iCellLa
c380: 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 st );. if
c390: 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 ( pc<iCellFirst
c3a0: 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 || pc>iCellLast
c3b0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
c3c0: 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
c3d0: 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
c3e0: 20 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 }. sz =
c3f0: 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
c400: 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 e, &data[pc]);.
c410: 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
c420: 20 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 69 pc+sz==usableSi
c430: 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ze );. if
c440: 28 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53 69 ( pc+sz>usableSi
c450: 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ze ){.
c460: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
c470: 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
c480: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
c490: 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
c4a0: 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 leaf ) iCellLast
c4b0: 2b 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e 64 ++;. } .#end
c4c0: 69 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 if.. /* Compu
c4d0: 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65 te the total fre
c4e0: 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 e space on the p
c4f0: 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 age */. pc =
c500: 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
c510: 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 dr+1]);. nFre
c520: 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 e = data[hdr+7]
c530: 2b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c 65 + top;. while
c540: 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 ( pc>0 ){.
c550: 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a u16 next, size;.
c560: 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 if( pc<iCe
c570: 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 llFirst || pc>iC
c580: 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 ellLast ){.
c590: 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 66 /* Start of f
c5a0: 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66 66 ree block is off
c5b0: 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
c5c0: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
c5d0: 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
c5e0: 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 . }.
c5f0: 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 next = get2byte(
c600: 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 &data[pc]);.
c610: 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 size = get2byt
c620: 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a e(&data[pc+2]);.
c630: 20 20 20 20 20 20 69 66 28 20 28 6e 65 78 74 3e if( (next>
c640: 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 69 0 && next<=pc+si
c650: 7a 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a 65 ze+3) || pc+size
c660: 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 >usableSize ){.
c670: 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 /* Free b
c680: 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e locks must be in
c690: 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
c6a0: 2e 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20 62 . And the last b
c6b0: 79 74 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a yte of. *
c6c0: 2a 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b * the free-block
c6d0: 20 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 must lie on the
c6e0: 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 database page.
c6f0: 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 */. retu
c700: 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
c710: 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d T_BKPT; . }
c720: 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e . nFree = n
c730: 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 Free + size;.
c740: 20 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 pc = next;.
c750: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 }.. /* At t
c760: 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 his point, nFree
c770: 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 contains the su
c780: 6d 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 m of the offset
c790: 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 to the start.
c7a0: 20 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d ** of the cell-
c7b0: 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 content area plu
c7c0: 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
c7d0: 66 72 65 65 20 62 79 74 65 73 20 77 69 74 68 69 free bytes withi
c7e0: 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c n. ** the cel
c7f0: 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 l-content area.
c800: 49 66 20 74 68 69 73 20 69 73 20 67 72 65 61 74 If this is great
c810: 65 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 er than the usab
c820: 6c 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f le-size. ** o
c830: 66 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e f the page, then
c840: 20 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 the page must b
c850: 65 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 e corrupted. Thi
c860: 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 s check also.
c870: 20 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 ** serves to ve
c880: 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 rify that the of
c890: 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 fset to the star
c8a0: 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f t of the cell-co
c8b0: 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 ntent. ** are
c8c0: 61 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 a, according to
c8d0: 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2c the page header,
c8e0: 20 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 lies within the
c8f0: 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
c900: 20 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 if( nFree>usab
c910: 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
c920: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
c930: 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 RRUPT_BKPT; .
c940: 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 }. pPage->nF
c950: 72 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 ree = (u16)(nFre
c960: 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b e - iCellFirst);
c970: 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
c980: 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 it = 1;. }. re
c990: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c9a0: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 }../*.** Set up
c9b0: 61 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 a raw page so th
c9c0: 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 at it looks like
c9d0: 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 a database page
c9e0: 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 holding.** no e
c9f0: 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ntries..*/.stati
ca00: 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 c void zeroPage(
ca10: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
ca20: 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e int flags){. un
ca30: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
ca40: 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
ca50: 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
ca60: 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
ca70: 20 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 u8 hdr = pPage
ca80: 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 ->hdrOffset;. u
ca90: 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 16 first;.. ass
caa0: 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
cab0: 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 rPagenumber(pPag
cac0: 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 e->pDbPage)==pPa
cad0: 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 ge->pgno );. as
cae0: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
caf0: 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 erGetExtra(pPage
cb00: 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 ->pDbPage) == (v
cb10: 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 oid*)pPage );.
cb20: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
cb30: 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 agerGetData(pPag
cb40: 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 e->pDbPage) == d
cb50: 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ata );. assert(
cb60: 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
cb70: 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
cb80: 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
cb90: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
cba0: 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
cbb0: 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42 tex) );. if( pB
cbc0: 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
cbd0: 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 S_SECURE_DELETE
cbe0: 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 64 ){. memset(&d
cbf0: 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 ata[hdr], 0, pBt
cc00: 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 ->usableSize - h
cc10: 64 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 5b dr);. }. data[
cc20: 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 hdr] = (char)fla
cc30: 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64 gs;. first = hd
cc40: 72 20 2b 20 28 28 66 6c 61 67 73 26 50 54 46 5f r + ((flags&PTF_
cc50: 4c 45 41 46 29 3d 3d 30 20 3f 20 31 32 20 3a 20 LEAF)==0 ? 12 :
cc60: 38 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 8);. memset(&da
cc70: 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 ta[hdr+1], 0, 4)
cc80: 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 ;. data[hdr+7]
cc90: 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 = 0;. put2byte(
cca0: 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 &data[hdr+5], pB
ccb0: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a t->usableSize);.
ccc0: 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d pPage->nFree =
ccd0: 20 28 75 31 36 29 28 70 42 74 2d 3e 75 73 61 62 (u16)(pBt->usab
cce0: 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 29 3b leSize - first);
ccf0: 0a 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 . decodeFlags(p
cd00: 50 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 Page, flags);.
cd10: 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
cd20: 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 50 61 t = first;. pPa
cd30: 67 65 2d 3e 61 44 61 74 61 45 6e 64 20 3d 20 26 ge->aDataEnd = &
cd40: 64 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 data[pBt->usable
cd50: 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 2d 3e Size];. pPage->
cd60: 61 43 65 6c 6c 49 64 78 20 3d 20 26 64 61 74 61 aCellIdx = &data
cd70: 5b 66 69 72 73 74 5d 3b 0a 20 20 70 50 61 67 65 [first];. pPage
cd80: 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->nOverflow = 0;
cd90: 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
cda0: 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 pageSize>=512 &&
cdb0: 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d pBt->pageSize<=
cdc0: 36 35 35 33 36 20 29 3b 0a 20 20 70 50 61 67 65 65536 );. pPage
cdd0: 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 28 75 31 ->maskPage = (u1
cde0: 36 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 6)(pBt->pageSize
cdf0: 20 2d 20 31 29 3b 0a 20 20 70 50 61 67 65 2d 3e - 1);. pPage->
ce00: 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 nCell = 0;. pPa
ce10: 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a ge->isInit = 1;.
ce20: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 }.../*.** Conver
ce30: 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 69 t a DbPage obtai
ce40: 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 ned from the pag
ce50: 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 er into a MemPag
ce60: 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 e used by.** the
ce70: 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f btree layer..*/
ce80: 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20 .static MemPage
ce90: 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 *btreePageFromDb
cea0: 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 62 Page(DbPage *pDb
ceb0: 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c Page, Pgno pgno,
cec0: 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b BtShared *pBt){
ced0: 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
cee0: 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 e = (MemPage*)sq
cef0: 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
cf00: 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ra(pDbPage);. p
cf10: 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 Page->aData = sq
cf20: 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
cf30: 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 a(pDbPage);. pP
cf40: 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 age->pDbPage = p
cf50: 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d DbPage;. pPage-
cf60: 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 >pBt = pBt;. pP
cf70: 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f age->pgno = pgno
cf80: 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 ;. pPage->hdrOf
cf90: 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 fset = pPage->pg
cfa0: 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b no==1 ? 100 : 0;
cfb0: 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b . return pPage;
cfc0: 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 .}../*.** Get a
cfd0: 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
cfe0: 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a ager. Initializ
cff0: 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 e the MemPage.pB
d000: 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 t and.** MemPage
d010: 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 .aData elements
d020: 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a if needed..**.**
d030: 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e If the noConten
d040: 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69 t flag is set, i
d050: 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 t means that we
d060: 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 do not care abou
d070: 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 t.** the content
d080: 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20 of the page at
d090: 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 this time. So d
d0a0: 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 o not go to the
d0b0: 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 disk.** to fetch
d0c0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a the content. J
d0d0: 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 ust fill in the
d0e0: 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 content with zer
d0f0: 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 os for now..** I
d100: 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 f in the future
d110: 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 we call sqlite3P
d120: 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 agerWrite() on t
d130: 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a his page, that.*
d140: 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20 * means we have
d150: 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f started to be co
d160: 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f ncerned about co
d170: 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69 ntent and the di
d180: 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c sk.** read shoul
d190: 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 20 d occur at that
d1a0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 point..*/.static
d1b0: 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61 67 int btreeGetPag
d1c0: 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
d1d0: 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 Bt, /* The
d1e0: 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f btree */. Pgno
d1f0: 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
d200: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 /* Number of th
d210: 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 e page to fetch
d220: 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 */. MemPage **p
d230: 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 pPage, /* Ret
d240: 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 urn the page in
d250: 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a this parameter *
d260: 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 /. int flags
d270: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 /* PAGE
d280: 52 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 R_GET_NOCONTENT
d290: 6f 72 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 or PAGER_GET_REA
d2a0: 44 4f 4e 4c 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e DONLY */.){. in
d2b0: 74 20 72 63 3b 0a 20 20 44 62 50 61 67 65 20 2a t rc;. DbPage *
d2c0: 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 pDbPage;.. asse
d2d0: 72 74 28 20 66 6c 61 67 73 3d 3d 30 20 7c 7c 20 rt( flags==0 ||
d2e0: 66 6c 61 67 73 3d 3d 50 41 47 45 52 5f 47 45 54 flags==PAGER_GET
d2f0: 5f 4e 4f 43 4f 4e 54 45 4e 54 20 7c 7c 20 66 6c _NOCONTENT || fl
d300: 61 67 73 3d 3d 50 41 47 45 52 5f 47 45 54 5f 52 ags==PAGER_GET_R
d310: 45 41 44 4f 4e 4c 59 20 29 3b 0a 20 20 61 73 73 EADONLY );. ass
d320: 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
d330: 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
d340: 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ex) );. rc = sq
d350: 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
d360: 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 e(pBt->pPager, p
d370: 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a 29 26 gno, (DbPage**)&
d380: 70 44 62 50 61 67 65 2c 20 66 6c 61 67 73 29 3b pDbPage, flags);
d390: 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
d3a0: 72 6e 20 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 rn rc;. *ppPage
d3b0: 20 3d 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d = btreePageFrom
d3c0: 44 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 DbPage(pDbPage,
d3d0: 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 pgno, pBt);. re
d3e0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d3f0: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 }../*.** Retriev
d400: 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 e a page from th
d410: 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 e pager cache. I
d420: 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
d430: 70 61 67 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 page is not.** a
d440: 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 lready in the pa
d450: 67 65 72 20 63 61 63 68 65 20 72 65 74 75 72 6e ger cache return
d460: 20 4e 55 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a NULL. Initializ
d470: 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 e the MemPage.pB
d480: 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 t and.** MemPage
d490: 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 .aData elements
d4a0: 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 if needed..*/.st
d4b0: 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 atic MemPage *bt
d4c0: 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74 reePageLookup(Bt
d4d0: 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
d4e0: 6f 20 70 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 o pgno){. DbPag
d4f0: 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 61 73 e *pDbPage;. as
d500: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
d510: 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
d520: 74 65 78 29 20 29 3b 0a 20 20 70 44 62 50 61 67 tex) );. pDbPag
d530: 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 e = sqlite3Pager
d540: 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 Lookup(pBt->pPag
d550: 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 er, pgno);. if(
d560: 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20 20 20 pDbPage ){.
d570: 72 65 74 75 72 6e 20 62 74 72 65 65 50 61 67 65 return btreePage
d580: 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61 FromDbPage(pDbPa
d590: 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a ge, pgno, pBt);.
d5a0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
d5b0: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
d5c0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
d5d0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e database file in
d5e0: 20 70 61 67 65 73 2e 20 49 66 20 74 68 65 72 65 pages. If there
d5f0: 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a is any kind of.
d600: 2a 2a 20 65 72 72 6f 72 2c 20 72 65 74 75 72 6e ** error, return
d610: 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 ((unsigned int)
d620: 2d 31 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 -1)..*/.static P
d630: 67 6e 6f 20 62 74 72 65 65 50 61 67 65 63 6f 75 gno btreePagecou
d640: 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 nt(BtShared *pBt
d650: 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 2d ){. return pBt-
d660: 3e 6e 50 61 67 65 3b 0a 7d 0a 75 33 32 20 73 71 >nPage;.}.u32 sq
d670: 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 50 61 lite3BtreeLastPa
d680: 67 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ge(Btree *p){.
d690: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
d6a0: 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
d6b0: 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
d6c0: 28 70 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 29 26 (p->pBt->nPage)&
d6d0: 30 78 38 30 30 30 30 30 30 29 3d 3d 30 20 29 3b 0x8000000)==0 );
d6e0: 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 62 . return (int)b
d6f0: 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 2d treePagecount(p-
d700: 3e 70 42 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 >pBt);.}../*.**
d710: 47 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 Get a page from
d720: 74 68 65 20 70 61 67 65 72 20 61 6e 64 20 69 6e the pager and in
d730: 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 itialize it. Th
d740: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 is routine is ju
d750: 73 74 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 st a.** convenie
d760: 6e 63 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 nce wrapper arou
d770: 6e 64 20 73 65 70 61 72 61 74 65 20 63 61 6c 6c nd separate call
d780: 73 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 s to btreeGetPag
d790: 65 28 29 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 e() and .** btre
d7a0: 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a eInitPage()..**.
d7b0: 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
d7c0: 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 ccurs, then the
d7d0: 76 61 6c 75 65 20 2a 70 70 50 61 67 65 20 69 73 value *ppPage is
d7e0: 20 73 65 74 20 74 6f 20 69 73 20 75 6e 64 65 66 set to is undef
d7f0: 69 6e 65 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 ined. It.** may
d800: 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 remain unchanged
d810: 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20 73 , or it may be s
d820: 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 et to an invalid
d830: 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 value..*/.stati
d840: 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 c int getAndInit
d850: 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
d860: 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 *pBt,
d870: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
d880: 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
d890: 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 Pgno pgno,
d8a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8b0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
d8c0: 68 65 20 70 61 67 65 20 74 6f 20 67 65 74 20 2a he page to get *
d8d0: 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
d8e0: 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
d8f0: 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
d900: 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 page pointer he
d910: 72 65 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 61 re */. int bRea
d920: 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 20 20 20 donly
d930: 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 /* PAGER
d940: 5f 47 45 54 5f 52 45 41 44 4f 4e 4c 59 20 6f 72 _GET_READONLY or
d950: 20 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 0 */.){. int r
d960: 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c c;. assert( sql
d970: 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
d980: 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
d990: 20 61 73 73 65 72 74 28 20 62 52 65 61 64 6f 6e assert( bReadon
d9a0: 6c 79 3d 3d 50 41 47 45 52 5f 47 45 54 5f 52 45 ly==PAGER_GET_RE
d9b0: 41 44 4f 4e 4c 59 20 7c 7c 20 62 52 65 61 64 6f ADONLY || bReado
d9c0: 6e 6c 79 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 nly==0 );.. if(
d9d0: 20 70 67 6e 6f 3e 62 74 72 65 65 50 61 67 65 63 pgno>btreePagec
d9e0: 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 ount(pBt) ){.
d9f0: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
da00: 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 65 6c RUPT_BKPT;. }el
da10: 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 se{. rc = btr
da20: 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 eeGetPage(pBt, p
da30: 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 62 52 65 gno, ppPage, bRe
da40: 61 64 6f 6e 6c 79 29 3b 0a 20 20 20 20 69 66 28 adonly);. if(
da50: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
da60: 26 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 & (*ppPage)->isI
da70: 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 nit==0 ){.
da80: 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 rc = btreeInitPa
da90: 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
daa0: 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
dab0: 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
dac0: 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 releasePage(*ppP
dad0: 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
dae0: 20 20 7d 0a 20 20 7d 0a 0a 20 20 74 65 73 74 63 }. }.. testc
daf0: 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20 29 3b 0a ase( pgno==0 );.
db00: 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d assert( pgno!=
db10: 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 0 || rc==SQLITE_
db20: 43 4f 52 52 55 50 54 20 29 3b 0a 20 20 72 65 74 CORRUPT );. ret
db30: 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
db40: 20 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 Release a MemPa
db50: 67 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 ge. This should
db60: 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 be called once
db70: 66 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a for each prior.*
db80: 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 47 * call to btreeG
db90: 65 74 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 etPage..*/.stati
dba0: 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 c void releasePa
dbb0: 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
dbc0: 65 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 20 e){. if( pPage
dbd0: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
dbe0: 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 Page->aData );.
dbf0: 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
dc00: 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
dc10: 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 62 50 ert( pPage->pDbP
dc20: 61 67 65 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 age!=0 );. as
dc30: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
dc40: 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 erGetExtra(pPage
dc50: 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 ->pDbPage) == (v
dc60: 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 oid*)pPage );.
dc70: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
dc80: 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 3PagerGetData(pP
dc90: 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 age->pDbPage)==p
dca0: 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 Page->aData );.
dcb0: 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
dcc0: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
dcd0: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
dce0: 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 );. sqlite3P
dcf0: 61 67 65 72 55 6e 72 65 66 4e 6f 74 4e 75 6c 6c agerUnrefNotNull
dd00: 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
dd10: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
dd20: 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b uring a rollback
dd30: 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 , when the pager
dd40: 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 reloads informa
dd50: 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 tion into the ca
dd60: 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 che.** so that t
dd70: 68 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 he cache is rest
dd80: 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
dd90: 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 inal state at th
dda0: 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 e start of.** th
ddb0: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 e transaction, f
ddc0: 6f 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 or each page res
ddd0: 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 tored this routi
dde0: 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
ddf0: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
de00: 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 needs to reset
de10: 74 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 the extra data s
de20: 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e ection at the en
de30: 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 d of the.** page
de40: 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 to agree with t
de50: 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 he restored data
de60: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
de70: 20 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 pageReinit(DbPa
de80: 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 ge *pData){. Me
de90: 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 mPage *pPage;.
dea0: 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 pPage = (MemPage
deb0: 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
dec0: 65 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a etExtra(pData);.
ded0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
dee0: 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
def0: 6e 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 nt(pData)>0 );.
df00: 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e if( pPage->isIn
df10: 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 it ){. assert
df20: 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
df30: 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
df40: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 >mutex) );. p
df50: 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
df60: 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
df70: 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
df80: 6e 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 nt(pData)>1 ){.
df90: 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 /* pPage mi
dfa0: 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 ght not be a btr
dfb0: 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 ee page; it mig
dfc0: 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f ht be an overflo
dfd0: 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 w page. **
dfe0: 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f or ptrmap page o
dff0: 72 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 r a free page.
e000: 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 In those cases,
e010: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
e020: 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 ** call to b
e030: 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 77 treeInitPage() w
e040: 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 ill likely retur
e050: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
e060: 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e .. ** But n
e070: 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 o harm is done b
e080: 79 20 74 68 69 73 2e 20 20 41 6e 64 20 69 74 20 y this. And it
e090: 69 73 20 76 65 72 79 20 69 6d 70 6f 72 74 61 6e is very importan
e0a0: 74 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 t that. **
e0b0: 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 btreeInitPage()
e0c0: 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 be called on eve
e0d0: 72 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f ry btree page so
e0e0: 20 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a we make. *
e0f0: 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 * the call for e
e100: 76 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 very page that c
e110: 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 omes in for re-i
e120: 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 niting. */.
e130: 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 btreeInitPage(p
e140: 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Page);. }. }
e150: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 .}../*.** Invoke
e160: 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
e170: 72 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a r for a btree..*
e180: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
e190: 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 eeInvokeBusyHand
e1a0: 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b ler(void *pArg){
e1b0: 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
e1c0: 20 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 = (BtShared*)pA
e1d0: 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 rg;. assert( pB
e1e0: 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 t->db );. asser
e1f0: 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
e200: 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d _held(pBt->db->m
e210: 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 utex) );. retur
e220: 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 n sqlite3InvokeB
e230: 75 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d usyHandler(&pBt-
e240: 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 >db->busyHandler
e250: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e );.}../*.** Open
e260: 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
e270: 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 ..** .** zFilena
e280: 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f me is the name o
e290: 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
e2a0: 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 ile. If zFilena
e2b0: 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 me is NULL.** th
e2c0: 65 6e 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 en an ephemeral
e2d0: 64 61 74 61 62 61 73 65 20 69 73 20 63 72 65 61 database is crea
e2e0: 74 65 64 2e 20 20 54 68 65 20 65 70 68 65 6d 65 ted. The epheme
e2f0: 72 61 6c 20 64 61 74 61 62 61 73 65 20 6d 69 67 ral database mig
e300: 68 74 0a 2a 2a 20 62 65 20 65 78 63 6c 75 73 69 ht.** be exclusi
e310: 76 65 6c 79 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 vely in memory,
e320: 6f 72 20 69 74 20 6d 69 67 68 74 20 75 73 65 20 or it might use
e330: 61 20 64 69 73 6b 2d 62 61 73 65 64 20 6d 65 6d a disk-based mem
e340: 6f 72 79 20 63 61 63 68 65 2e 0a 2a 2a 20 45 69 ory cache..** Ei
e350: 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 65 70 ther way, the ep
e360: 68 65 6d 65 72 61 6c 20 64 61 74 61 62 61 73 65 hemeral database
e370: 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 will be automat
e380: 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 0a ically deleted .
e390: 2a 2a 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 ** when sqlite3B
e3a0: 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 treeClose() is c
e3b0: 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 alled..**.** If
e3c0: 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d zFilename is ":m
e3d0: 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 emory:" then an
e3e0: 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
e3f0: 73 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a se is created.**
e400: 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 that is automat
e410: 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 ically destroyed
e420: 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 when it is clos
e430: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 66 ed..**.** The "f
e440: 6c 61 67 73 22 20 70 61 72 61 6d 65 74 65 72 20 lags" parameter
e450: 69 73 20 61 20 62 69 74 6d 61 73 6b 20 74 68 61 is a bitmask tha
e460: 74 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 t might contain
e470: 62 69 74 73 20 6c 69 6b 65 0a 2a 2a 20 42 54 52 bits like.** BTR
e480: 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 EE_OMIT_JOURNAL
e490: 61 6e 64 2f 6f 72 20 42 54 52 45 45 5f 4d 45 4d and/or BTREE_MEM
e4a0: 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ORY..**.** If th
e4b0: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c e database is al
e4c0: 72 65 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 ready opened in
e4d0: 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
e4e0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
e4f0: 61 6e 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 and we are in sh
e500: 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c ared cache mode,
e510: 20 74 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 then the open w
e520: 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e ill fail with an
e530: 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 .** SQLITE_CONST
e540: 52 41 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 RAINT error. We
e550: 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 cannot allow tw
e560: 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 o or more BtShar
e570: 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e ed.** objects in
e580: 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
e590: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 se connection si
e5a0: 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c nce doing so wil
e5b0: 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f l lead.** to pro
e5c0: 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 blems with locki
e5d0: 6e 67 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ng..*/.int sqlit
e5e0: 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 73 e3BtreeOpen(. s
e5f0: 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
e600: 2c 20 20 20 20 20 20 2f 2a 20 56 46 53 20 74 6f , /* VFS to
e610: 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 62 2d use for this b-
e620: 74 72 65 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tree */. const
e630: 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
e640: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
e650: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 file containing
e660: 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 the BTree datab
e670: 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ase */. sqlite3
e680: 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
e690: 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 /* Associated d
e6a0: 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
e6b0: 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 /. Btree **ppBt
e6c0: 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 ree, /* P
e6d0: 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 ointer to new Bt
e6e0: 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 ree object writt
e6f0: 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 en here */. int
e700: 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
e710: 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 /* Options
e720: 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 */. int vfsFlag
e730: 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s /*
e740: 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 Flags passed thr
e750: 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f ough to sqlite3_
e760: 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 vfs.xOpen() */.)
e770: 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
e780: 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 t = 0;
e790: 20 20 20 2f 2a 20 53 68 61 72 65 64 20 70 61 72 /* Shared par
e7a0: 74 20 6f 66 20 62 74 72 65 65 20 73 74 72 75 63 t of btree struc
e7b0: 74 75 72 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ture */. Btree
e7c0: 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *p;
e7d0: 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
e7e0: 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a le to return */.
e7f0: 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
e800: 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20 30 3b 20 *mutexOpen = 0;
e810: 20 2f 2a 20 50 72 65 76 65 6e 74 73 20 61 20 72 /* Prevents a r
e820: 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 2e 20 54 ace condition. T
e830: 69 63 6b 65 74 20 23 33 35 33 37 20 2a 2f 0a 20 icket #3537 */.
e840: 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
e850: 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
e860: 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 /* Result code f
e870: 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f rom this functio
e880: 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65 73 65 72 n */. u8 nReser
e890: 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ve;
e8a0: 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 /* Byte of
e8b0: 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 6f 6e unused space on
e8c0: 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
e8d0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 44 unsigned char zD
e8e0: 62 48 65 61 64 65 72 5b 31 30 30 5d 3b 20 20 2f bHeader[100]; /
e8f0: 2a 20 44 61 74 61 62 61 73 65 20 68 65 61 64 65 * Database heade
e900: 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 r content */..
e910: 2f 2a 20 54 72 75 65 20 69 66 20 6f 70 65 6e 69 /* True if openi
e920: 6e 67 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 2c ng an ephemeral,
e930: 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 temporary datab
e940: 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 ase */. const i
e950: 6e 74 20 69 73 54 65 6d 70 44 62 20 3d 20 7a 46 nt isTempDb = zF
e960: 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 ilename==0 || zF
e970: 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 3b 0a 0a ilename[0]==0;..
e980: 20 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61 72 /* Set the var
e990: 69 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74 6f iable isMemdb to
e9a0: 20 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 2d true for an in-
e9b0: 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
e9c0: 20 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65 20 or . ** false
e9d0: 66 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65 64 for a file-based
e9e0: 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a database.. */.
e9f0: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
ea00: 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 63 6f IT_MEMORYDB. co
ea10: 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 nst int isMemdb
ea20: 3d 20 30 3b 0a 23 65 6c 73 65 0a 20 20 63 6f 6e = 0;.#else. con
ea30: 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d st int isMemdb =
ea40: 20 28 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 73 (zFilename && s
ea50: 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c trcmp(zFilename,
ea60: 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 29 ":memory:")==0)
ea70: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ea80: 20 20 20 20 20 20 20 20 7c 7c 20 28 69 73 54 65 || (isTe
ea90: 6d 70 44 62 20 26 26 20 73 71 6c 69 74 65 33 54 mpDb && sqlite3T
eaa0: 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 64 62 29 29 empInMemory(db))
eab0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
eac0: 20 20 20 20 20 20 20 20 7c 7c 20 28 76 66 73 46 || (vfsF
ead0: 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
eae0: 45 4e 5f 4d 45 4d 4f 52 59 29 21 3d 30 3b 0a 23 EN_MEMORY)!=0;.#
eaf0: 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 endif.. assert(
eb00: 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 db!=0 );. asse
eb10: 72 74 28 20 70 56 66 73 21 3d 30 20 29 3b 0a 20 rt( pVfs!=0 );.
eb20: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
eb30: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
eb40: 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
eb50: 72 74 28 20 28 66 6c 61 67 73 26 30 78 66 66 29 rt( (flags&0xff)
eb60: 3d 3d 66 6c 61 67 73 20 29 3b 20 20 20 2f 2a 20 ==flags ); /*
eb70: 66 6c 61 67 73 20 66 69 74 20 69 6e 20 38 20 62 flags fit in 8 b
eb80: 69 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 6e 6c its */.. /* Onl
eb90: 79 20 61 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 y a BTREE_SINGLE
eba0: 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 database can be
ebb0: 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 BTREE_UNORDERED
ebc0: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 66 */. assert( (f
ebd0: 6c 61 67 73 20 26 20 42 54 52 45 45 5f 55 4e 4f lags & BTREE_UNO
ebe0: 52 44 45 52 45 44 29 3d 3d 30 20 7c 7c 20 28 66 RDERED)==0 || (f
ebf0: 6c 61 67 73 20 26 20 42 54 52 45 45 5f 53 49 4e lags & BTREE_SIN
ec00: 47 4c 45 29 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a GLE)!=0 );.. /*
ec10: 20 41 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 20 A BTREE_SINGLE
ec20: 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 77 61 database is alwa
ec30: 79 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 61 ys a temporary a
ec40: 6e 64 2f 6f 72 20 65 70 68 65 6d 65 72 61 6c 20 nd/or ephemeral
ec50: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 66 6c */. assert( (fl
ec60: 61 67 73 20 26 20 42 54 52 45 45 5f 53 49 4e 47 ags & BTREE_SING
ec70: 4c 45 29 3d 3d 30 20 7c 7c 20 69 73 54 65 6d 70 LE)==0 || isTemp
ec80: 44 62 20 29 3b 0a 0a 20 20 69 66 28 20 69 73 4d Db );.. if( isM
ec90: 65 6d 64 62 20 29 7b 0a 20 20 20 20 66 6c 61 67 emdb ){. flag
eca0: 73 20 7c 3d 20 42 54 52 45 45 5f 4d 45 4d 4f 52 s |= BTREE_MEMOR
ecb0: 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 76 66 Y;. }. if( (vf
ecc0: 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f sFlags & SQLITE_
ecd0: 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 OPEN_MAIN_DB)!=0
ece0: 20 26 26 20 28 69 73 4d 65 6d 64 62 20 7c 7c 20 && (isMemdb ||
ecf0: 69 73 54 65 6d 70 44 62 29 20 29 7b 0a 20 20 20 isTempDb) ){.
ed00: 20 76 66 73 46 6c 61 67 73 20 3d 20 28 76 66 73 vfsFlags = (vfs
ed10: 46 6c 61 67 73 20 26 20 7e 53 51 4c 49 54 45 5f Flags & ~SQLITE_
ed20: 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c 20 OPEN_MAIN_DB) |
ed30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
ed40: 5f 44 42 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 73 _DB;. }. p = s
ed50: 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
ed60: 28 73 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b (sizeof(Btree));
ed70: 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 . if( !p ){.
ed80: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
ed90: 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 OMEM;. }. p->i
eda0: 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e nTrans = TRANS_N
edb0: 4f 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 ONE;. p->db = d
edc0: 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 b;.#ifndef SQLIT
edd0: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
ede0: 43 48 45 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42 CHE. p->lock.pB
edf0: 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c tree = p;. p->l
ee00: 6f 63 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b 0a ock.iTable = 1;.
ee10: 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 #endif..#if !def
ee20: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
ee30: 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 _SHARED_CACHE) &
ee40: 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
ee50: 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 E_OMIT_DISKIO).
ee60: 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 /*. ** If this
ee70: 20 42 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 Btree is a cand
ee80: 69 64 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 idate for shared
ee90: 20 63 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 cache, try to f
eea0: 69 6e 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 ind an. ** exis
eeb0: 74 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 ting BtShared ob
eec0: 6a 65 63 74 20 74 68 61 74 20 77 65 20 63 61 6e ject that we can
eed0: 20 73 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f share with. */
eee0: 0a 20 20 69 66 28 20 69 73 54 65 6d 70 44 62 3d . if( isTempDb=
eef0: 3d 30 20 26 26 20 28 69 73 4d 65 6d 64 62 3d 3d =0 && (isMemdb==
ef00: 30 20 7c 7c 20 28 76 66 73 46 6c 61 67 73 26 53 0 || (vfsFlags&S
ef10: 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 21 QLITE_OPEN_URI)!
ef20: 3d 30 29 20 29 7b 0a 20 20 20 20 69 66 28 20 76 =0) ){. if( v
ef30: 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 fsFlags & SQLITE
ef40: 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 _OPEN_SHAREDCACH
ef50: 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e E ){. int n
ef60: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70 FullPathname = p
ef70: 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
ef80: 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 1;. char *z
ef90: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73 FullPathname = s
efa0: 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75 qlite3Malloc(nFu
efb0: 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
efc0: 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 MUTEX_LOGIC(
efd0: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
efe0: 75 74 65 78 53 68 61 72 65 64 3b 20 29 0a 20 20 utexShared; ).
eff0: 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 p->sharable
f000: 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 21 = 1;. if( !
f010: 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b zFullPathname ){
f020: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
f030: 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 _free(p);.
f040: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f050: 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 NOMEM;. }.
f060: 20 20 20 20 20 69 66 28 20 69 73 4d 65 6d 64 62 if( isMemdb
f070: 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
f080: 70 79 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 py(zFullPathname
f090: 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 73 71 6c , zFilename, sql
f0a0: 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 ite3Strlen30(zFi
f0b0: 6c 65 6e 61 6d 65 29 2b 31 29 3b 0a 20 20 20 20 lename)+1);.
f0c0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
f0d0: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 rc = sqlite3OsF
f0e0: 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 ullPathname(pVfs
f0f0: 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 , zFilename,.
f100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f110: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f120: 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a nFullPathname, z
f130: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
f140: 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
f150: 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
f160: 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 e3_free(zFullPat
f170: 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 hname);.
f180: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
f190: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
f1a0: 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
f1b0: 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 53 51 }. }.#if SQ
f1c0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
f1d0: 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65 6e 20 mutexOpen
f1e0: 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
f1f0: 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
f200: 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 _STATIC_OPEN);.
f210: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
f220: 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f 70 ex_enter(mutexOp
f230: 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 en);. mutex
f240: 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 Shared = sqlite3
f250: 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
f260: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
f270: 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 ASTER);. sq
f280: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
f290: 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a r(mutexShared);.
f2a0: 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 #endif. for
f2b0: 28 70 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68 (pBt=GLOBAL(BtSh
f2c0: 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 ared*,sqlite3Sha
f2d0: 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70 redCacheList); p
f2e0: 42 74 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65 Bt; pBt=pBt->pNe
f2f0: 78 74 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 xt){. ass
f300: 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30 ert( pBt->nRef>0
f310: 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
f320: 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 0==strcmp(zFullP
f330: 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 athname, sqlite3
f340: 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 PagerFilename(pB
f350: 74 2d 3e 70 50 61 67 65 72 2c 20 30 29 29 0a 20 t->pPager, 0)).
f360: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f370: 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 56 && sqlite3PagerV
f380: 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d fs(pBt->pPager)=
f390: 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 20 =pVfs ){.
f3a0: 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 int iDb;.
f3b0: 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d 64 62 for(iDb=db
f3c0: 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 3b ->nDb-1; iDb>=0;
f3d0: 20 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 20 iDb--){.
f3e0: 20 20 20 20 20 42 74 72 65 65 20 2a 70 45 78 69 Btree *pExi
f3f0: 73 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62 5b sting = db->aDb[
f400: 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 iDb].pBt;.
f410: 20 20 20 20 20 20 69 66 28 20 70 45 78 69 73 74 if( pExist
f420: 69 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e 67 ing && pExisting
f430: 2d 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a 20 20 ->pBt==pBt ){.
f440: 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
f450: 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
f460: 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
f470: 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
f480: 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
f490: 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 mutexOpen);.
f4a0: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
f4b0: 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 3_free(zFullPath
f4c0: 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 name);.
f4d0: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
f4e0: 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 e(p);.
f4f0: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
f500: 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 E_CONSTRAINT;.
f510: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
f520: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
f530: 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a p->pBt = pBt;.
f540: 20 20 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6e pBt->n
f550: 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 Ref++;.
f560: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
f570: 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
f580: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
f590: 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 ave(mutexShared)
f5a0: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
f5b0: 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 free(zFullPathna
f5c0: 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 me);. }.#ifde
f5d0: 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
f5e0: 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f else{. /
f5f0: 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c * In debug mode,
f600: 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65 72 we mark all per
f610: 73 69 73 74 65 6e 74 20 64 61 74 61 62 61 73 65 sistent database
f620: 73 20 61 73 20 73 68 61 72 61 62 6c 65 0a 20 20 s as sharable.
f630: 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e ** even when
f640: 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 they are not.
f650: 54 68 69 73 20 65 78 65 72 63 69 73 65 73 20 74 This exercises t
f660: 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 20 he locking code
f670: 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 and. ** giv
f680: 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 75 6e es more opportun
f690: 69 74 79 20 66 6f 72 20 61 73 73 65 72 74 73 28 ity for asserts(
f6a0: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
f6b0: 6c 64 28 29 29 0a 20 20 20 20 20 20 2a 2a 20 73 ld()). ** s
f6c0: 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 69 6e tatements to fin
f6d0: 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c 65 d locking proble
f6e0: 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ms.. */.
f6f0: 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d p->sharable =
f700: 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 1;. }.#endif
f710: 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
f720: 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ( pBt==0 ){.
f730: 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f /*. ** The fo
f740: 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 20 llowing asserts
f750: 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 73 make sure that s
f760: 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62 tructures used b
f770: 79 20 74 68 65 20 62 74 72 65 65 20 61 72 65 0a y the btree are.
f780: 20 20 20 20 2a 2a 20 74 68 65 20 72 69 67 68 74 ** the right
f790: 20 73 69 7a 65 2e 20 20 54 68 69 73 20 69 73 20 size. This is
f7a0: 74 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 to guard against
f7b0: 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 74 68 size changes th
f7c0: 61 74 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a at result. **
f7d0: 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 when compiling
f7e0: 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 on a different a
f7f0: 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20 20 20 rchitecture..
f800: 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
f810: 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20 7c sizeof(i64)==8 |
f820: 7c 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 34 | sizeof(i64)==4
f830: 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
f840: 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 20 7c sizeof(u64)==8 |
f850: 7c 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 34 | sizeof(u64)==4
f860: 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
f870: 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20 29 sizeof(u32)==4 )
f880: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 ;. assert( si
f890: 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 3b 0a zeof(u16)==2 );.
f8a0: 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
f8b0: 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 of(Pgno)==4 );.
f8c0: 20 0a 20 20 20 20 70 42 74 20 3d 20 73 71 6c 69 . pBt = sqli
f8d0: 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
f8e0: 69 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 izeof(*pBt) );.
f8f0: 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b if( pBt==0 ){
f900: 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
f910: 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
f920: 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f goto btree_open_
f930: 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 out;. }. r
f940: 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
f950: 4f 70 65 6e 28 70 56 66 73 2c 20 26 70 42 74 2d Open(pVfs, &pBt-
f960: 3e 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 6e 61 >pPager, zFilena
f970: 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 me,.
f980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 58 EX
f990: 54 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c TRA_SIZE, flags,
f9a0: 20 76 66 73 46 6c 61 67 73 2c 20 70 61 67 65 52 vfsFlags, pageR
f9b0: 65 69 6e 69 74 29 3b 0a 20 20 20 20 69 66 28 20 einit);. if(
f9c0: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
f9d0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 . sqlite3Pa
f9e0: 67 65 72 53 65 74 4d 6d 61 70 4c 69 6d 69 74 28 gerSetMmapLimit(
f9f0: 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 64 62 2d pBt->pPager, db-
fa00: 3e 73 7a 4d 6d 61 70 29 3b 0a 20 20 20 20 20 20 >szMmap);.
fa10: 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
fa20: 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 rReadFileheader(
fa30: 70 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 pBt->pPager,size
fa40: 6f 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 of(zDbHeader),zD
fa50: 62 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a bHeader);. }.
fa60: 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
fa70: 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 TE_OK ){. g
fa80: 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f oto btree_open_o
fa90: 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 ut;. }. pB
faa0: 74 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 28 t->openFlags = (
fab0: 75 38 29 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 u8)flags;. pB
fac0: 74 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 t->db = db;.
fad0: 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 sqlite3PagerSetB
fae0: 75 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e usyhandler(pBt->
faf0: 70 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 pPager, btreeInv
fb00: 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 okeBusyHandler,
fb10: 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 pBt);. p->pBt
fb20: 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 = pBt;. . p
fb30: 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b Bt->pCursor = 0;
fb40: 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 . pBt->pPage1
fb50: 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 = 0;. if( sq
fb60: 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 lite3PagerIsread
fb70: 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 only(pBt->pPager
fb80: 29 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 ) ) pBt->btsFlag
fb90: 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f 4e s |= BTS_READ_ON
fba0: 4c 59 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 LY;.#ifdef SQLIT
fbb0: 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a E_SECURE_DELETE.
fbc0: 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 pBt->btsFlag
fbd0: 73 20 7c 3d 20 42 54 53 5f 53 45 43 55 52 45 5f s |= BTS_SECURE_
fbe0: 44 45 4c 45 54 45 3b 0a 23 65 6e 64 69 66 0a 20 DELETE;.#endif.
fbf0: 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 pBt->pageSize
fc00: 20 3d 20 28 7a 44 62 48 65 61 64 65 72 5b 31 36 = (zDbHeader[16
fc10: 5d 3c 3c 38 29 20 7c 20 28 7a 44 62 48 65 61 64 ]<<8) | (zDbHead
fc20: 65 72 5b 31 37 5d 3c 3c 31 36 29 3b 0a 20 20 20 er[17]<<16);.
fc30: 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 if( pBt->pageSi
fc40: 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 ze<512 || pBt->p
fc50: 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
fc60: 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 AX_PAGE_SIZE.
fc70: 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e || ((pBt->
fc80: 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d pageSize-1)&pBt-
fc90: 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b >pageSize)!=0 ){
fca0: 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 . pBt->page
fcb0: 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 Size = 0;.#ifnde
fcc0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
fcd0: 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f TOVACUUM. /
fce0: 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e * If the magic n
fcf0: 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 ame ":memory:" w
fd00: 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e ill create an in
fd10: 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
fd20: 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 , then. **
fd30: 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 leave the autoVa
fd40: 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 cuum mode at 0 (
fd50: 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 do not auto-vacu
fd60: 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 um), even if.
fd70: 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 ** SQLITE_DEF
fd80: 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 AULT_AUTOVACUUM
fd90: 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 is true. On the
fda0: 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 other hand, if.
fdb0: 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f ** SQLITE_O
fdc0: 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 MIT_MEMORYDB has
fdd0: 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 been defined, t
fde0: 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 hen ":memory:" i
fdf0: 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a s just a. *
fe00: 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e * regular file-n
fe10: 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ame. In this cas
fe20: 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 e the auto-vacuu
fe30: 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 m applies as per
fe40: 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a normal.. *
fe50: 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c /. if( zFil
fe60: 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 ename && !isMemd
fe70: 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 b ){. pBt
fe80: 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 ->autoVacuum = (
fe90: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 SQLITE_DEFAULT_A
fea0: 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 UTOVACUUM ? 1 :
feb0: 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 0);. pBt-
fec0: 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 >incrVacuum = (S
fed0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
fee0: 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 TOVACUUM==2 ? 1
fef0: 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 : 0);. }.#e
ff00: 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 ndif. nRese
ff10: 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c rve = 0;. }el
ff20: 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 se{. nReser
ff30: 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 ve = zDbHeader[2
ff40: 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 0];. pBt->b
ff50: 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 tsFlags |= BTS_P
ff60: 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a 23 AGESIZE_FIXED;.#
ff70: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
ff80: 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
ff90: 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 pBt->autoVac
ffa0: 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 uum = (get4byte(
ffb0: 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 &zDbHeader[36 +
ffc0: 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 4*4])?1:0);.
ffd0: 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 pBt->incrVacuu
ffe0: 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a m = (get4byte(&z
fff0: 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37 2a DbHeader[36 + 7*
10000 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 4])?1:0);.#endif
10010 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
10020 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
10030 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 agesize(pBt->pPa
10040 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 ger, &pBt->pageS
10050 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a ize, nReserve);.
10060 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
10070 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 o btree_open_out
10080 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c ;. pBt->usabl
10090 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 eSize = pBt->pag
100a0 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 eSize - nReserve
100b0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
100c0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 Bt->pageSize & 7
100d0 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 )==0 ); /* 8-by
100e0 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 te alignment of
100f0 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 0a pageSize */. .
10100 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
10110 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
10120 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e CACHE) && !defin
10130 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ed(SQLITE_OMIT_D
10140 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 ISKIO). /* Ad
10150 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61 72 d the new BtShar
10160 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 ed object to the
10170 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 linked list sha
10180 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73 2e rable BtShareds.
10190 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
101a0 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
101b0 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 43 MUTEX_LOGIC
101c0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 ( sqlite3_mutex
101d0 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 20 29 0a *mutexShared; ).
101e0 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 pBt->nRef
101f0 3d 20 31 3b 0a 20 20 20 20 20 20 4d 55 54 45 58 = 1;. MUTEX
10200 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78 53 68 61 _LOGIC( mutexSha
10210 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 red = sqlite3Mut
10220 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
10230 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
10240 45 52 29 3b 29 0a 20 20 20 20 20 20 69 66 28 20 ER);). if(
10250 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
10260 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 E && sqlite3Glob
10270 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 alConfig.bCoreMu
10280 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 tex ){. p
10290 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 Bt->mutex = sqli
102a0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
102b0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29 LITE_MUTEX_FAST)
102c0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 42 ;. if( pB
102d0 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 t->mutex==0 ){.
102e0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
102f0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
10300 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db->malloc
10310 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 Failed = 0;.
10320 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 goto btree
10330 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 _open_out;.
10340 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
10350 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
10360 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 _enter(mutexShar
10370 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e ed);. pBt->
10380 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 42 pNext = GLOBAL(B
10390 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
103a0 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
103b0 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 ;. GLOBAL(B
103c0 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
103d0 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
103e0 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71 = pBt;. sq
103f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
10400 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a e(mutexShared);.
10410 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
10420 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
10430 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
10440 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 D_CACHE) && !def
10450 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
10460 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 _DISKIO). /* If
10470 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 75 the new Btree u
10480 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 70 ses a sharable p
10490 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c BtShared, then l
104a0 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a ink the new. **
104b0 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 Btree into the
104c0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 list of all shar
104d0 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20 able Btrees for
104e0 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 the same connect
104f0 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69 ion.. ** The li
10500 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 st is kept in as
10510 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 cending order by
10520 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20 pBt address..
10530 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 */. if( p->shar
10540 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 able ){. int
10550 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53 i;. Btree *pS
10560 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ib;. for(i=0;
10570 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
10580 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 69 {. if( (pSi
10590 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 b = db->aDb[i].p
105a0 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e Bt)!=0 && pSib->
105b0 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
105c0 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d while( pSib-
105d0 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d >pPrev ){ pSib =
105e0 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a pSib->pPrev; }.
105f0 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 if( p->p
10600 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a Bt<pSib->pBt ){.
10610 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 p->pNe
10620 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 xt = pSib;.
10630 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 p->pPrev =
10640 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 0;. pSi
10650 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 b->pPrev = p;.
10660 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
10670 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 while( pS
10680 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 69 ib->pNext && pSi
10690 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 2d b->pNext->pBt<p-
106a0 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 >pBt ){.
106b0 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 2d pSib = pSib-
106c0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
106d0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d }. p-
106e0 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70 >pNext = pSib->p
106f0 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 Next;.
10700 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b p->pPrev = pSib;
10710 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
10720 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 ->pNext ){.
10730 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d p->pNext-
10740 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 >pPrev = p;.
10750 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
10760 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d 20 pSib->pNext =
10770 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 p;. }.
10780 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
10790 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 }. }. }.#e
107a0 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 20 ndif. *ppBtree
107b0 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 6e = p;..btree_open
107c0 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d _out:. if( rc!=
107d0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
107e0 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74 2d if( pBt && pBt-
107f0 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20 >pPager ){.
10800 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
10810 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b se(pBt->pPager);
10820 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
10830 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 e3_free(pBt);.
10840 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
10850 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20 );. *ppBtree
10860 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
10870 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d 54 72 /* If the B-Tr
10880 65 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 ee was successfu
10890 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65 74 20 lly opened, set
108a0 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
108b0 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20 20 20 size to the.
108c0 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 ** default value
108d0 2e 20 45 78 63 65 70 74 2c 20 77 68 65 6e 20 6f . Except, when o
108e0 70 65 6e 69 6e 67 20 6f 6e 20 61 6e 20 65 78 69 pening on an exi
108f0 73 74 69 6e 67 20 73 68 61 72 65 64 20 70 61 67 sting shared pag
10900 65 72 2d 63 61 63 68 65 2c 0a 20 20 20 20 2a 2a er-cache,. **
10910 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 do not change t
10920 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 73 he pager-cache s
10930 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ize.. */.
10940 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 if( sqlite3Btree
10950 53 63 68 65 6d 61 28 70 2c 20 30 2c 20 30 29 3d Schema(p, 0, 0)=
10960 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
10970 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 te3PagerSetCache
10980 73 69 7a 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 size(p->pBt->pPa
10990 67 65 72 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 ger, SQLITE_DEFA
109a0 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b ULT_CACHE_SIZE);
109b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
109c0 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 mutexOpen ){.
109d0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
109e0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 3_mutex_held(mut
109f0 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 exOpen) );. s
10a00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
10a10 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 ve(mutexOpen);.
10a20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
10a30 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 }../*.** Decreme
10a40 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e nt the BtShared.
10a50 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 nRef counter. W
10a60 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a hen it reaches z
10a70 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 ero,.** remove t
10a80 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 he BtShared stru
10a90 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73 cture from the s
10aa0 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 haring list. Re
10ab0 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 turn.** true if
10ac0 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 the BtShared.nRe
10ad0 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65 f counter reache
10ae0 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 s zero and retur
10af0 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 n.** false if it
10b00 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 is still positi
10b10 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ve..*/.static in
10b20 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 t removeFromShar
10b30 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64 ingList(BtShared
10b40 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 *pBt){.#ifndef
10b50 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
10b60 45 44 5f 43 41 43 48 45 0a 20 20 4d 55 54 45 58 ED_CACHE. MUTEX
10b70 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f _LOGIC( sqlite3_
10b80 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20 mutex *pMaster;
10b90 29 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c ). BtShared *pL
10ba0 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 ist;. int remov
10bb0 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 ed = 0;.. asser
10bc0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
10bd0 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 _notheld(pBt->mu
10be0 74 65 78 29 20 29 3b 0a 20 20 4d 55 54 45 58 5f tex) );. MUTEX_
10bf0 4c 4f 47 49 43 28 20 70 4d 61 73 74 65 72 20 3d LOGIC( pMaster =
10c00 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
10c10 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
10c20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 20 STATIC_MASTER);
10c30 29 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ). sqlite3_mute
10c40 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 x_enter(pMaster)
10c50 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b ;. pBt->nRef--;
10c60 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 66 . if( pBt->nRef
10c70 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 47 <=0 ){. if( G
10c80 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c LOBAL(BtShared*,
10c90 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 sqlite3SharedCac
10ca0 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b 0a heList)==pBt ){.
10cb0 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 GLOBAL(BtS
10cc0 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 hared*,sqlite3Sh
10cd0 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d aredCacheList) =
10ce0 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pBt->pNext;.
10cf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c }else{. pL
10d00 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 ist = GLOBAL(BtS
10d10 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 hared*,sqlite3Sh
10d20 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a aredCacheList);.
10d30 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57 while( ALW
10d40 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70 4c AYS(pList) && pL
10d50 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 20 ist->pNext!=pBt
10d60 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 ){. pList
10d70 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 =pList->pNext;.
10d80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
10d90 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 29 ALWAYS(pList) )
10da0 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d {. pList-
10db0 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4e >pNext = pBt->pN
10dc0 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
10dd0 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 }. if( SQLIT
10de0 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b 0a E_THREADSAFE ){.
10df0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
10e00 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d 75 tex_free(pBt->mu
10e10 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 tex);. }.
10e20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d removed = 1;. }
10e30 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
10e40 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b _leave(pMaster);
10e50 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 65 . return remove
10e60 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 d;.#else. retur
10e70 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f n 1;.#endif.}../
10e80 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 *.** Make sure p
10e90 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 6f Bt->pTmpSpace po
10ea0 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 ints to an alloc
10eb0 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f ation of .** MX_
10ec0 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 62 CELL_SIZE(pBt) b
10ed0 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ytes..*/.static
10ee0 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 6d void allocateTem
10ef0 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 pSpace(BtShared
10f00 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70 42 *pBt){. if( !pB
10f10 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a t->pTmpSpace ){.
10f20 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 pBt->pTmpSpa
10f30 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ce = sqlite3Page
10f40 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 Malloc( pBt->pag
10f50 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a eSize );.. /*
10f60 20 4f 6e 65 20 6f 66 20 74 68 65 20 75 73 65 73 One of the uses
10f70 20 6f 66 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 of pBt->pTmpSpa
10f80 63 65 20 69 73 20 74 6f 20 66 6f 72 6d 61 74 20 ce is to format
10f90 63 65 6c 6c 73 20 62 65 66 6f 72 65 0a 20 20 20 cells before.
10fa0 20 2a 2a 20 69 6e 73 65 72 74 69 6e 67 20 74 68 ** inserting th
10fb0 65 6d 20 69 6e 74 6f 20 61 20 6c 65 61 66 20 70 em into a leaf p
10fc0 61 67 65 20 28 66 75 6e 63 74 69 6f 6e 20 66 69 age (function fi
10fd0 6c 6c 49 6e 43 65 6c 6c 28 29 29 2e 20 49 66 0a llInCell()). If.
10fe0 20 20 20 20 2a 2a 20 61 20 63 65 6c 6c 20 69 73 ** a cell is
10ff0 20 6c 65 73 73 20 74 68 61 6e 20 34 20 62 79 74 less than 4 byt
11000 65 73 20 69 6e 20 73 69 7a 65 2c 20 69 74 20 69 es in size, it i
11010 73 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 s rounded up to
11020 34 20 62 79 74 65 73 0a 20 20 20 20 2a 2a 20 62 4 bytes. ** b
11030 79 20 74 68 65 20 76 61 72 69 6f 75 73 20 72 6f y the various ro
11040 75 74 69 6e 65 73 20 74 68 61 74 20 6d 61 6e 69 utines that mani
11050 70 75 6c 61 74 65 20 62 69 6e 61 72 79 20 63 65 pulate binary ce
11060 6c 6c 73 2e 20 57 68 69 63 68 0a 20 20 20 20 2a lls. Which. *
11070 2a 20 63 61 6e 20 6d 65 61 6e 20 74 68 61 74 20 * can mean that
11080 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 20 6f 6e 6c fillInCell() onl
11090 79 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 y initializes th
110a0 65 20 66 69 72 73 74 20 32 20 6f 72 20 33 0a 20 e first 2 or 3.
110b0 20 20 20 2a 2a 20 62 79 74 65 73 20 6f 66 20 70 ** bytes of p
110c0 54 6d 70 53 70 61 63 65 2c 20 62 75 74 20 74 68 TmpSpace, but th
110d0 61 74 20 74 68 65 20 66 69 72 73 74 20 34 20 62 at the first 4 b
110e0 79 74 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 ytes are copied
110f0 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 69 74 20 69 from. ** it i
11100 6e 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 70 nto a database p
11110 61 67 65 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 age. This is not
11120 20 61 63 74 75 61 6c 6c 79 20 61 20 70 72 6f 62 actually a prob
11130 6c 65 6d 2c 20 62 75 74 20 69 74 0a 20 20 20 20 lem, but it.
11140 2a 2a 20 64 6f 65 73 20 63 61 75 73 65 20 61 20 ** does cause a
11150 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 20 77 valgrind error w
11160 68 65 6e 20 74 68 65 20 31 20 6f 72 20 32 20 62 hen the 1 or 2 b
11170 79 74 65 73 20 6f 66 20 75 6e 69 74 69 61 6c 69 ytes of unitiali
11180 7a 65 64 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 zed . ** data
11190 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 73 79 is passed to sy
111a0 73 74 65 6d 20 63 61 6c 6c 20 77 72 69 74 65 28 stem call write(
111b0 29 2e 20 53 6f 20 74 6f 20 61 76 6f 69 64 20 74 ). So to avoid t
111c0 68 69 73 20 65 72 72 6f 72 2c 0a 20 20 20 20 2a his error,. *
111d0 2a 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 74 * zero the first
111e0 20 34 20 62 79 74 65 73 20 6f 66 20 74 65 6d 70 4 bytes of temp
111f0 20 73 70 61 63 65 20 68 65 72 65 2e 20 20 2a 2f space here. */
11200 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 54 . if( pBt->pT
11210 6d 70 53 70 61 63 65 20 29 20 6d 65 6d 73 65 74 mpSpace ) memset
11220 28 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 2c (pBt->pTmpSpace,
11230 20 30 2c 20 34 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 0, 4);. }.}../
11240 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 70 42 *.** Free the pB
11250 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 6c 6c t->pTmpSpace all
11260 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 ocation.*/.stati
11270 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d 70 53 c void freeTempS
11280 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 pace(BtShared *p
11290 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 Bt){. sqlite3Pa
112a0 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70 54 6d geFree( pBt->pTm
112b0 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e pSpace);. pBt->
112c0 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d pTmpSpace = 0;.}
112d0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e ../*.** Close an
112e0 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61 open database a
112f0 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c nd invalidate al
11300 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 69 6e l cursors..*/.in
11310 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c t sqlite3BtreeCl
11320 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ose(Btree *p){.
11330 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
11340 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 p->pBt;. BtCur
11350 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a sor *pCur;.. /*
11360 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f Close all curso
11370 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 rs opened via th
11380 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 is handle. */.
11390 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
113a0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
113b0 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
113c0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
113d0 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 (p);. pCur = pB
113e0 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 t->pCursor;. wh
113f0 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 ile( pCur ){.
11400 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 BtCursor *pTmp
11410 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 = pCur;. pCur
11420 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a = pCur->pNext;.
11430 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 if( pTmp->pB
11440 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
11450 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
11460 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a seCursor(pTmp);.
11470 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
11480 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 Rollback any act
11490 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ive transaction
114a0 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e and free the han
114b0 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 dle structure..
114c0 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 ** The call to
114d0 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
114e0 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 back() drops any
114f0 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c table-locks hel
11500 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 d by. ** this h
11510 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 andle.. */. sq
11520 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
11530 63 6b 28 70 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 ck(p, SQLITE_OK)
11540 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
11550 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 Leave(p);.. /*
11560 49 66 20 74 68 65 72 65 20 61 72 65 20 73 74 69 If there are sti
11570 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61 6e ll other outstan
11580 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 ding references
11590 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 to the shared-bt
115a0 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 ree. ** structu
115b0 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 re, return now.
115c0 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 The remainder of
115d0 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 this procedure
115e0 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 cleans . ** up
115f0 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
11600 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
11610 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
11620 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 0 && p->locked==
11630 30 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 0 );. if( !p->s
11640 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 harable || remov
11650 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 eFromSharingList
11660 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 (pBt) ){. /*
11670 54 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f The pBt is no lo
11680 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61 72 nger on the shar
11690 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 ing list, so we
116a0 63 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20 2a can access. *
116b0 2a 20 69 74 20 77 69 74 68 6f 75 74 20 68 61 76 * it without hav
116c0 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ing to hold the
116d0 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 mutex.. **.
116e0 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 ** Clean out a
116f0 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 42 74 nd delete the Bt
11700 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 Shared object..
11710 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
11720 28 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 ( !pBt->pCursor
11730 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 );. sqlite3Pa
11740 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 gerClose(pBt->pP
11750 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 70 ager);. if( p
11760 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 Bt->xFreeSchema
11770 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 && pBt->pSchema
11780 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 46 ){. pBt->xF
11790 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 reeSchema(pBt->p
117a0 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 Schema);. }.
117b0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
117c0 28 30 2c 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 (0, pBt->pSchema
117d0 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 );. freeTempS
117e0 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 pace(pBt);. s
117f0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
11800 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
11810 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
11820 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 D_CACHE. assert
11830 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
11840 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
11850 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a p->locked==0 );.
11860 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 if( p->pPrev )
11870 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
11880 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 = p->pNext;. i
11890 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d f( p->pNext ) p-
118a0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
118b0 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 p->pPrev;.#endif
118c0 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .. sqlite3_free
118d0 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
118e0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
118f0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d * Change the lim
11900 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 it on the number
11910 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 of pages allowe
11920 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a d in the cache..
11930 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 **.** The maximu
11940 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 m number of cach
11950 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74 e pages is set t
11960 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a o the absolute.*
11970 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 * value of mxPag
11980 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 e. If mxPage is
11990 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 negative, the p
119a0 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 ager will.** ope
119b0 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 rate asynchronou
119c0 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f sly - it will no
119d0 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 t stop to do fsy
119e0 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 nc()s.** to insu
119f0 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 re data is writt
11a00 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 en to the disk s
11a10 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a urface before.**
11a20 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 continuing. Tr
11a30 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c ansactions still
11a40 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f work if synchro
11a50 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 nous is off,.**
11a60 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
11a70 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 cannot be corru
11a80 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f pted if this pro
11a90 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e gram.** crashes.
11aa0 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 But if the ope
11ab0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 rating system cr
11ac0 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 ashes or there i
11ad0 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 s.** an abrupt p
11ae0 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 ower failure whe
11af0 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 n synchronous is
11b00 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 off, the databa
11b10 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c se.** could be l
11b20 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 eft in an incons
11b30 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 istent and unrec
11b40 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a overable state..
11b50 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 ** Synchronous i
11b60 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 s on by default
11b70 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 so database corr
11b80 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a uption is not.**
11b90 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 normally a worr
11ba0 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 y..*/.int sqlite
11bb0 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 3BtreeSetCacheSi
11bc0 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ze(Btree *p, int
11bd0 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 mxPage){. BtSh
11be0 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
11bf0 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 Bt;. assert( sq
11c00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
11c10 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
11c20 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
11c30 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 Enter(p);. sqli
11c40 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 te3PagerSetCache
11c50 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 size(pBt->pPager
11c60 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c , mxPage);. sql
11c70 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
11c80 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
11c90 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 TE_OK;.}..#if SQ
11ca0 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 LITE_MAX_MMAP_SI
11cb0 5a 45 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ZE>0./*.** Chang
11cc0 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 e the limit on t
11cd0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 he amount of the
11ce0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
11cf0 68 61 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 65 hat may be.** me
11d00 6d 6f 72 79 20 6d 61 70 70 65 64 2e 0a 2a 2f 0a mory mapped..*/.
11d10 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
11d20 53 65 74 4d 6d 61 70 4c 69 6d 69 74 28 42 74 72 SetMmapLimit(Btr
11d30 65 65 20 2a 70 2c 20 73 71 6c 69 74 65 33 5f 69 ee *p, sqlite3_i
11d40 6e 74 36 34 20 73 7a 4d 6d 61 70 29 7b 0a 20 20 nt64 szMmap){.
11d50 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
11d60 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 p->pBt;. assert
11d70 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
11d80 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
11d90 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 x) );. sqlite3B
11da0 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
11db0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 4d sqlite3PagerSetM
11dc0 6d 61 70 4c 69 6d 69 74 28 70 42 74 2d 3e 70 50 mapLimit(pBt->pP
11dd0 61 67 65 72 2c 20 73 7a 4d 6d 61 70 29 3b 0a 20 ager, szMmap);.
11de0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
11df0 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
11e00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
11e10 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 dif /* SQLITE_MA
11e20 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 20 2a 2f X_MMAP_SIZE>0 */
11e30 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
11e40 68 65 20 77 61 79 20 64 61 74 61 20 69 73 20 73 he way data is s
11e50 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69 6e ynced to disk in
11e60 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65 61 order to increa
11e70 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a 2a se or decrease.*
11e80 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 * how well the d
11e90 61 74 61 62 61 73 65 20 72 65 73 69 73 74 73 20 atabase resists
11ea0 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 damage due to OS
11eb0 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77 crashes and pow
11ec0 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 er.** failures.
11ed0 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20 Level 1 is the
11ee0 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f same as asynchro
11ef0 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 nous (no syncs()
11f00 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 occur and.** th
11f10 65 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72 ere is a high pr
11f20 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d obability of dam
11f30 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73 age) Level 2 is
11f40 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 the default. T
11f50 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 here.** is a ver
11f60 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 y low but non-ze
11f70 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f ro probability o
11f80 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c f damage. Level
11f90 20 33 20 72 65 64 75 63 65 73 20 74 68 65 0a 2a 3 reduces the.*
11fa0 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 * probability of
11fb0 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 damage to near
11fc0 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 61 20 zero but with a
11fd0 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 write performanc
11fe0 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a e reduction..*/.
11ff0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
12000 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 MIT_PAGER_PRAGMA
12010 53 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 S.int sqlite3Btr
12020 65 65 53 65 74 50 61 67 65 72 46 6c 61 67 73 28 eeSetPagerFlags(
12030 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 . Btree *p,
12040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
12050 20 62 74 72 65 65 20 74 6f 20 73 65 74 20 74 68 btree to set th
12060 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 6f e safety level o
12070 6e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 n */. unsigned
12080 70 67 46 6c 61 67 73 20 20 20 20 20 20 20 2f 2a pgFlags /*
12090 20 56 61 72 69 6f 75 73 20 50 41 47 45 52 5f 2a Various PAGER_*
120a0 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 42 flags */.){. B
120b0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
120c0 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
120d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
120e0 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
120f0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ) );. sqlite3Bt
12100 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 reeEnter(p);. s
12110 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 46 6c qlite3PagerSetFl
12120 61 67 73 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ags(pBt->pPager,
12130 20 70 67 46 6c 61 67 73 29 3b 0a 20 20 73 71 6c pgFlags);. sql
12140 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
12150 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
12160 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a TE_OK;.}.#endif.
12170 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
12180 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 UE if the given
12190 62 74 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 btree is set to
121a0 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 2e 20 safety level 1.
121b0 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 In other.** wor
121c0 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 45 20 ds, return TRUE
121d0 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63 63 if no sync() occ
121e0 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 urs on the disk
121f0 66 69 6c 65 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 files..*/.int sq
12200 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 lite3BtreeSyncDi
12210 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70 29 sabled(Btree *p)
12220 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
12230 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e t = p->pBt;. in
12240 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
12250 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
12260 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
12270 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 42 ); . sqlite3B
12280 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
12290 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20 70 assert( pBt && p
122a0 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20 20 Bt->pPager );.
122b0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
122c0 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 rNosync(pBt->pPa
122d0 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ger);. sqlite3B
122e0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
122f0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12300 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 64 .** Change the d
12310 65 66 61 75 6c 74 20 70 61 67 65 73 20 73 69 7a efault pages siz
12320 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 e and the number
12330 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 79 74 of reserved byt
12340 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a 20 es per page..**
12350 4f 72 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 Or, if the page
12360 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79 size has already
12370 20 62 65 65 6e 20 66 69 78 65 64 2c 20 72 65 74 been fixed, ret
12380 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
12390 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 NLY .** without
123a0 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 68 69 6e changing anythin
123b0 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 g..**.** The pag
123c0 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 e size must be a
123d0 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 power of 2 betw
123e0 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 een 512 and 6553
123f0 36 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 0a 6. If the page.
12400 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c 69 65 64 ** size supplied
12410 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 does not meet t
12420 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 his constraint t
12430 68 65 6e 20 74 68 65 20 70 61 67 65 20 73 69 7a hen the page siz
12440 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e e is not.** chan
12450 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 ged..**.** Page
12460 73 69 7a 65 73 20 61 72 65 20 63 6f 6e 73 74 72 sizes are constr
12470 61 69 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f ained to be a po
12480 77 65 72 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 wer of two so th
12490 61 74 20 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a at the region.**
124a0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
124b0 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 6c file used for l
124c0 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e ocking (beginnin
124d0 67 20 61 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 g at PENDING_BYT
124e0 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 E,.** the first
124f0 62 79 74 65 20 70 61 73 74 20 74 68 65 20 31 47 byte past the 1G
12500 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 B boundary, 0x40
12510 30 30 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f 000000) needs to
12520 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 occur.** at the
12530 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 beginning of a
12540 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 page..**.** If p
12550 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65 72 76 arameter nReserv
12560 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a e is less than z
12570 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 ero, then the nu
12580 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 mber of reserved
12590 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20 70 61 .** bytes per pa
125a0 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 ge is left uncha
125b0 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 nged..**.** If t
125c0 68 65 20 69 46 69 78 21 3d 30 20 74 68 65 6e 20 he iFix!=0 then
125d0 74 68 65 20 42 54 53 5f 50 41 47 45 53 49 5a 45 the BTS_PAGESIZE
125e0 5f 46 49 58 45 44 20 66 6c 61 67 20 69 73 20 73 _FIXED flag is s
125f0 65 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 et so that the p
12600 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 age size.** and
12610 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 autovacuum mode
12620 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 can no longer be
12630 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 69 6e 74 changed..*/.int
12640 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
12650 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a PageSize(Btree *
12660 70 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c p, int pageSize,
12670 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 int nReserve, i
12680 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 nt iFix){. int
12690 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
126a0 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
126b0 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 = p->pBt;. asse
126c0 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 rt( nReserve>=-1
126d0 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 && nReserve<=25
126e0 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 5 );. sqlite3Bt
126f0 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 reeEnter(p);. i
12700 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 f( pBt->btsFlags
12710 20 26 20 42 54 53 5f 50 41 47 45 53 49 5a 45 5f & BTS_PAGESIZE_
12720 46 49 58 45 44 20 29 7b 0a 20 20 20 20 73 71 6c FIXED ){. sql
12730 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
12740 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
12750 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
12760 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 }. if( nReserv
12770 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 e<0 ){. nRese
12780 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 rve = pBt->pageS
12790 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c ize - pBt->usabl
127a0 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 eSize;. }. ass
127b0 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 ert( nReserve>=0
127c0 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 && nReserve<=25
127d0 35 20 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 5 );. if( pageS
127e0 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 ize>=512 && page
127f0 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 Size<=SQLITE_MAX
12800 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 _PAGE_SIZE &&.
12810 20 20 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 ((pageSize
12820 2d 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 -1)&pageSize)==0
12830 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
12840 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d (pageSize & 7)==
12850 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
12860 20 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 !pBt->pPage1 &&
12870 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 !pBt->pCursor )
12880 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 ;. pBt->pageS
12890 69 7a 65 20 3d 20 28 75 33 32 29 70 61 67 65 53 ize = (u32)pageS
128a0 69 7a 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d ize;. freeTem
128b0 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 7d pSpace(pBt);. }
128c0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
128d0 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
128e0 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 pBt->pPager, &pB
128f0 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 t->pageSize, nRe
12900 73 65 72 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 serve);. pBt->u
12910 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d sableSize = pBt-
12920 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 75 31 36 >pageSize - (u16
12930 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 66 28 )nReserve;. if(
12940 20 69 46 69 78 20 29 20 70 42 74 2d 3e 62 74 73 iFix ) pBt->bts
12950 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 Flags |= BTS_PAG
12960 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a 20 20 73 ESIZE_FIXED;. s
12970 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
12980 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
12990 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
129a0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 n the currently
129b0 64 65 66 69 6e 65 64 20 70 61 67 65 20 73 69 7a defined page siz
129c0 65 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 e.*/.int sqlite3
129d0 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 BtreeGetPageSize
129e0 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 (Btree *p){. re
129f0 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61 67 turn p->pBt->pag
12a00 65 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 20 64 65 eSize;.}..#if de
12a10 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 fined(SQLITE_HAS
12a20 5f 43 4f 44 45 43 29 20 7c 7c 20 64 65 66 69 6e _CODEC) || defin
12a30 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
12a40 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
12a50 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 tion is similar
12a60 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 to sqlite3BtreeG
12a70 65 74 52 65 73 65 72 76 65 28 29 2c 20 65 78 63 etReserve(), exc
12a80 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 6d ept that it.** m
12a90 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ay only be calle
12aa0 64 20 69 66 20 69 74 20 69 73 20 67 75 61 72 61 d if it is guara
12ab0 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 62 nteed that the b
12ac0 2d 74 72 65 65 20 6d 75 74 65 78 20 69 73 20 61 -tree mutex is a
12ad0 6c 72 65 61 64 79 0a 2a 2a 20 68 65 6c 64 2e 0a lready.** held..
12ae0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 73 **.** This is us
12af0 65 66 75 6c 20 69 6e 20 6f 6e 65 20 73 70 65 63 eful in one spec
12b00 69 61 6c 20 63 61 73 65 20 69 6e 20 74 68 65 20 ial case in the
12b10 62 61 63 6b 75 70 20 41 50 49 20 63 6f 64 65 20 backup API code
12b20 77 68 65 72 65 20 69 74 20 69 73 0a 2a 2a 20 6b where it is.** k
12b30 6e 6f 77 6e 20 74 68 61 74 20 74 68 65 20 73 68 nown that the sh
12b40 61 72 65 64 20 62 2d 74 72 65 65 20 6d 75 74 65 ared b-tree mute
12b50 78 20 69 73 20 68 65 6c 64 2c 20 62 75 74 20 74 x is held, but t
12b60 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 he mutex on the
12b70 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e .** database han
12b80 64 6c 65 20 74 68 61 74 20 6f 77 6e 73 20 2a 70 dle that owns *p
12b90 20 69 73 20 6e 6f 74 2e 20 49 6e 20 74 68 69 73 is not. In this
12ba0 20 63 61 73 65 20 69 66 20 73 71 6c 69 74 65 33 case if sqlite3
12bb0 42 74 72 65 65 45 6e 74 65 72 28 29 0a 2a 2a 20 BtreeEnter().**
12bc0 77 65 72 65 20 74 6f 20 62 65 20 63 61 6c 6c 65 were to be calle
12bd0 64 2c 20 69 74 20 6d 69 67 68 74 20 63 6f 6c 6c d, it might coll
12be0 69 64 65 20 77 69 74 68 20 73 6f 6d 65 20 6f 74 ide with some ot
12bf0 68 65 72 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e her operation on
12c00 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
12c10 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6f 77 6e handle that own
12c20 73 20 2a 70 2c 20 63 61 75 73 69 6e 67 20 75 6e s *p, causing un
12c30 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 defined behavior
12c40 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
12c50 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 4e BtreeGetReserveN
12c60 6f 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 oMutex(Btree *p)
12c70 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
12c80 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
12c90 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
12ca0 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 . return p->pBt
12cb0 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e ->pageSize - p->
12cc0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
12cd0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
12ce0 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 7c 7c ITE_HAS_CODEC ||
12cf0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f SQLITE_DEBUG */
12d00 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
12d10 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 QLITE_OMIT_PAGER
12d20 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 _PRAGMAS) || !de
12d30 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
12d40 54 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 T_VACUUM)./*.**
12d50 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
12d60 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
12d70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f ace at the end o
12d80 66 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 f every page tha
12d90 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74 75 t.** are intentu
12da0 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65 64 ally left unused
12db0 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22 . This is the "
12dc0 72 65 73 65 72 76 65 64 22 20 73 70 61 63 65 20 reserved" space
12dd0 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65 74 that is.** somet
12de0 69 6d 65 73 20 75 73 65 64 20 62 79 20 65 78 74 imes used by ext
12df0 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 69 6e 74 20 ensions..*/.int
12e00 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
12e10 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 eserve(Btree *p)
12e20 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c {. int n;. sql
12e30 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
12e40 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d );. n = p->pBt-
12e50 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 >pageSize - p->p
12e60 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
12e70 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
12e80 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
12e90 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 n;.}../*.** Set
12ea0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 the maximum pag
12eb0 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 e count for a da
12ec0 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65 tabase if mxPage
12ed0 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a is positive..**
12ee0 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 No changes are
12ef0 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 made if mxPage i
12f00 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e s 0 or negative.
12f10 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f .** Regardless o
12f20 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d f the value of m
12f30 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 xPage, return th
12f40 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 e maximum page c
12f50 6f 75 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ount..*/.int sql
12f60 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 ite3BtreeMaxPage
12f70 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 Count(Btree *p,
12f80 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 int mxPage){. i
12f90 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 nt n;. sqlite3B
12fa0 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
12fb0 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 n = sqlite3Pager
12fc0 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e MaxPageCount(p->
12fd0 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 pBt->pPager, mxP
12fe0 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 age);. sqlite3B
12ff0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
13000 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a return n;.}../*.
13010 2a 2a 20 53 65 74 20 74 68 65 20 42 54 53 5f 53 ** Set the BTS_S
13020 45 43 55 52 45 5f 44 45 4c 45 54 45 20 66 6c 61 ECURE_DELETE fla
13030 67 20 69 66 20 6e 65 77 46 6c 61 67 20 69 73 20 g if newFlag is
13040 30 20 6f 72 20 31 2e 20 20 49 66 20 6e 65 77 46 0 or 1. If newF
13050 6c 61 67 20 69 73 20 2d 31 2c 0a 2a 2a 20 74 68 lag is -1,.** th
13060 65 6e 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 en make no chang
13070 65 73 2e 20 20 41 6c 77 61 79 73 20 72 65 74 75 es. Always retu
13080 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 rn the value of
13090 74 68 65 20 42 54 53 5f 53 45 43 55 52 45 5f 44 the BTS_SECURE_D
130a0 45 4c 45 54 45 0a 2a 2a 20 73 65 74 74 69 6e 67 ELETE.** setting
130b0 20 61 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 after the chang
130c0 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 e..*/.int sqlite
130d0 33 42 74 72 65 65 53 65 63 75 72 65 44 65 6c 65 3BtreeSecureDele
130e0 74 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 te(Btree *p, int
130f0 20 6e 65 77 46 6c 61 67 29 7b 0a 20 20 69 6e 74 newFlag){. int
13100 20 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 b;. if( p==0 )
13110 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 6c return 0;. sql
13120 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
13130 29 3b 0a 20 20 69 66 28 20 6e 65 77 46 6c 61 67 );. if( newFlag
13140 3e 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 >=0 ){. p->pB
13150 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e t->btsFlags &= ~
13160 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 BTS_SECURE_DELET
13170 45 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 46 6c E;. if( newFl
13180 61 67 20 29 20 70 2d 3e 70 42 74 2d 3e 62 74 73 ag ) p->pBt->bts
13190 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53 45 43 Flags |= BTS_SEC
131a0 55 52 45 5f 44 45 4c 45 54 45 3b 0a 20 20 7d 20 URE_DELETE;. }
131b0 0a 20 20 62 20 3d 20 28 70 2d 3e 70 42 74 2d 3e . b = (p->pBt->
131c0 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 53 btsFlags & BTS_S
131d0 45 43 55 52 45 5f 44 45 4c 45 54 45 29 21 3d 30 ECURE_DELETE)!=0
131e0 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
131f0 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
13200 72 6e 20 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f rn b;.}.#endif /
13210 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 * !defined(SQLIT
13220 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
13230 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 GMAS) || !define
13240 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 d(SQLITE_OMIT_VA
13250 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 CUUM) */../*.**
13260 43 68 61 6e 67 65 20 74 68 65 20 27 61 75 74 6f Change the 'auto
13270 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 -vacuum' propert
13280 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 y of the databas
13290 65 2e 20 49 66 20 74 68 65 20 27 61 75 74 6f 56 e. If the 'autoV
132a0 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 61 6d 65 acuum'.** parame
132b0 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ter is non-zero,
132c0 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 63 75 75 then auto-vacuu
132d0 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 m mode is enable
132e0 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 74 0a 2a d. If zero, it.*
132f0 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2e 20 54 * is disabled. T
13300 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
13310 20 66 6f 72 20 74 68 65 20 61 75 74 6f 2d 76 61 for the auto-va
13320 63 75 75 6d 20 70 72 6f 70 65 72 74 79 20 69 73 cuum property is
13330 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 .** determined
13340 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 by the SQLITE_DE
13350 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
13360 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 69 6e 74 20 73 macro..*/.int s
13370 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 qlite3BtreeSetAu
13380 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a toVacuum(Btree *
13390 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 p, int autoVacuu
133a0 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 m){.#ifdef SQLIT
133b0 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
133c0 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 M. return SQLIT
133d0 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 6c 73 E_READONLY;.#els
133e0 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 e. BtShared *pB
133f0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e t = p->pBt;. in
13400 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
13410 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 38 29 ;. u8 av = (u8)
13420 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 20 73 autoVacuum;.. s
13430 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
13440 28 70 29 3b 0a 20 20 69 66 28 20 28 70 42 74 2d (p);. if( (pBt-
13450 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f >btsFlags & BTS_
13460 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 29 21 PAGESIZE_FIXED)!
13470 3d 30 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21 =0 && (av ?1:0)!
13480 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d =pBt->autoVacuum
13490 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
134a0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
134b0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e }else{. pBt->
134c0 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 autoVacuum = av
134d0 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 ?1:0;. pBt->i
134e0 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d ncrVacuum = av==
134f0 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 2 ?1:0;. }. sq
13500 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
13510 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
13520 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
13530 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 Return the valu
13540 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76 e of the 'auto-v
13550 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e acuum' property.
13560 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 If auto-vacuum
13570 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 is .** enabled 1
13580 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
13590 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 69 6e herwise 0..*/.in
135a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
135b0 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
135c0 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 e *p){.#ifdef SQ
135d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
135e0 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 CUUM. return BT
135f0 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e REE_AUTOVACUUM_N
13600 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 ONE;.#else. int
13610 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
13620 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
13630 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 c = (. (!p->p
13640 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f Bt->autoVacuum)?
13650 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
13660 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e _NONE:. (!p->
13670 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 pBt->incrVacuum)
13680 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 ?BTREE_AUTOVACUU
13690 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 M_FULL:. BTRE
136a0 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 E_AUTOVACUUM_INC
136b0 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 R. );. sqlite3
136c0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
136d0 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 return rc;.#end
136e0 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 if.}.../*.** Get
136f0 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
13700 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 pPage1 of the da
13710 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
13720 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 is will.** also
13730 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f acquire a readlo
13740 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e ck on that file.
13750 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
13760 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
13770 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 success. If the
13780 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a file is not a.*
13790 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 * well-formed da
137a0 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
137b0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
137c0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
137d0 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
137e0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
137f0 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b database is lock
13800 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ed. SQLITE_NOME
13810 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 M.** is returned
13820 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f if we run out o
13830 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 f memory. .*/.st
13840 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 atic int lockBtr
13850 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ee(BtShared *pBt
13860 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
13870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
13880 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 ult code from su
13890 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 bfunctions */.
138a0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
138b0 20 20 20 20 20 2f 2a 20 50 61 67 65 20 31 20 6f /* Page 1 o
138c0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
138d0 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 ile */. int nPa
138e0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ge; /*
138f0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
13900 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
13910 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 46 */. int nPageF
13920 69 6c 65 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 ile = 0; /* Nu
13930 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
13940 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13950 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 le */. int nPag
13960 65 48 65 61 64 65 72 3b 20 20 20 20 20 2f 2a 20 eHeader; /*
13970 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
13980 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
13990 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 68 64 72 according to hdr
139a0 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
139b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
139c0 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
139d0 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
139e0 70 50 61 67 65 31 3d 3d 30 20 29 3b 0a 20 20 72 pPage1==0 );. r
139f0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13a00 53 68 61 72 65 64 4c 6f 63 6b 28 70 42 74 2d 3e SharedLock(pBt->
13a10 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72 pPager);. if( r
13a20 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
13a30 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
13a40 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
13a50 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 t, 1, &pPage1, 0
13a60 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
13a70 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
13a80 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d rc;.. /* Do som
13a90 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 e checking to he
13aa0 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 lp insure the fi
13ab0 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 le we opened rea
13ac0 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 lly is. ** a va
13ad0 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 69 6c lid database fil
13ae0 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 50 61 67 65 e. . */. nPage
13af0 20 3d 20 6e 50 61 67 65 48 65 61 64 65 72 20 3d = nPageHeader =
13b00 20 67 65 74 34 62 79 74 65 28 32 38 2b 28 75 38 get4byte(28+(u8
13b10 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 74 61 29 *)pPage1->aData)
13b20 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 ;. sqlite3Pager
13b30 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
13b40 50 61 67 65 72 2c 20 26 6e 50 61 67 65 46 69 6c Pager, &nPageFil
13b50 65 29 3b 0a 20 20 69 66 28 20 6e 50 61 67 65 3d e);. if( nPage=
13b60 3d 30 20 7c 7c 20 6d 65 6d 63 6d 70 28 32 34 2b =0 || memcmp(24+
13b70 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 (u8*)pPage1->aDa
13b80 74 61 2c 20 39 32 2b 28 75 38 2a 29 70 50 61 67 ta, 92+(u8*)pPag
13b90 65 31 2d 3e 61 44 61 74 61 2c 34 29 21 3d 30 20 e1->aData,4)!=0
13ba0 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 6e ){. nPage = n
13bb0 50 61 67 65 46 69 6c 65 3b 0a 20 20 7d 0a 20 20 PageFile;. }.
13bc0 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 if( nPage>0 ){.
13bd0 20 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 3b u32 pageSize;
13be0 0a 20 20 20 20 75 33 32 20 75 73 61 62 6c 65 53 . u32 usableS
13bf0 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 ize;. u8 *pag
13c00 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 e1 = pPage1->aDa
13c10 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ta;. rc = SQL
13c20 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 ITE_NOTADB;.
13c30 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 if( memcmp(page1
13c40 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 , zMagicHeader,
13c50 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 16)!=0 ){.
13c60 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
13c70 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 23 failed;. }..#
13c80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
13c90 54 5f 57 41 4c 0a 20 20 20 20 69 66 28 20 70 61 T_WAL. if( pa
13ca0 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 ge1[18]>1 ){.
13cb0 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 pBt->btsFlags
13cc0 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c |= BTS_READ_ONL
13cd0 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 Y;. }. if(
13ce0 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a page1[19]>1 ){.
13cf0 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
13d00 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
13d10 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 }.#else. if
13d20 28 20 70 61 67 65 31 5b 31 38 5d 3e 32 20 29 7b ( page1[18]>2 ){
13d30 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 . pBt->btsF
13d40 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 lags |= BTS_READ
13d50 5f 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 _ONLY;. }.
13d60 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 32 if( page1[19]>2
13d70 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
13d80 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
13d90 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
13da0 49 66 20 74 68 65 20 77 72 69 74 65 20 76 65 72 If the write ver
13db0 73 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 20 32 sion is set to 2
13dc0 2c 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 , this database
13dd0 73 68 6f 75 6c 64 20 62 65 20 61 63 63 65 73 73 should be access
13de0 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 57 41 4c ed. ** in WAL
13df0 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 20 6c 6f mode. If the lo
13e00 67 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 g is not already
13e10 20 6f 70 65 6e 2c 20 6f 70 65 6e 20 69 74 20 6e open, open it n
13e20 6f 77 2e 20 54 68 65 6e 20 0a 20 20 20 20 2a 2a ow. Then . **
13e30 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
13e40 4b 20 61 6e 64 20 72 65 74 75 72 6e 20 77 69 74 K and return wit
13e50 68 6f 75 74 20 70 6f 70 75 6c 61 74 69 6e 67 20 hout populating
13e60 42 74 53 68 61 72 65 64 2e 70 50 61 67 65 31 2e BtShared.pPage1.
13e70 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c . ** The call
13e80 65 72 20 64 65 74 65 63 74 73 20 74 68 69 73 20 er detects this
13e90 61 6e 64 20 63 61 6c 6c 73 20 74 68 69 73 20 66 and calls this f
13ea0 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 2e 20 54 unction again. T
13eb0 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 his is. ** re
13ec0 71 75 69 72 65 64 20 61 73 20 74 68 65 20 76 65 quired as the ve
13ed0 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 rsion of page 1
13ee0 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
13ef0 20 70 61 67 65 31 20 62 75 66 66 65 72 0a 20 20 page1 buffer.
13f00 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 ** may not be
13f10 74 68 65 20 6c 61 74 65 73 74 20 76 65 72 73 69 the latest versi
13f20 6f 6e 20 2d 20 74 68 65 72 65 20 6d 61 79 20 62 on - there may b
13f30 65 20 61 20 6e 65 77 65 72 20 6f 6e 65 20 69 6e e a newer one in
13f40 20 74 68 65 20 6c 6f 67 0a 20 20 20 20 2a 2a 20 the log. **
13f50 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 file.. */.
13f60 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3d 3d if( page1[19]==
13f70 32 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46 6c 2 && (pBt->btsFl
13f80 61 67 73 20 26 20 42 54 53 5f 4e 4f 5f 57 41 4c ags & BTS_NO_WAL
13f90 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e )==0 ){. in
13fa0 74 20 69 73 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 t isOpen = 0;.
13fb0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13fc0 50 61 67 65 72 4f 70 65 6e 57 61 6c 28 70 42 74 PagerOpenWal(pBt
13fd0 2d 3e 70 50 61 67 65 72 2c 20 26 69 73 4f 70 65 ->pPager, &isOpe
13fe0 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 n);. if( rc
13ff0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
14000 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 goto page
14010 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
14020 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 }else if( i
14030 73 4f 70 65 6e 3d 3d 30 20 29 7b 0a 23 69 66 64 sOpen==0 ){.#ifd
14040 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
14050 54 5f 57 41 4c 5f 53 41 46 45 54 59 4c 45 56 45 T_WAL_SAFETYLEVE
14060 4c 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 L. /* Def
14070 61 75 6c 74 20 74 6f 20 73 70 65 63 69 66 69 65 ault to specifie
14080 64 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 66 d safety_level f
14090 6f 72 20 57 41 4c 20 6d 6f 64 65 20 2a 2f 0a 20 or WAL mode */.
140a0 20 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e if( pBt->
140b0 64 62 21 3d 30 20 26 26 20 70 42 74 2d 3e 64 62 db!=0 && pBt->db
140c0 2d 3e 61 44 62 21 3d 30 20 29 7b 0a 20 20 20 20 ->aDb!=0 ){.
140d0 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 int iDb;.
140e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
140f0 20 2a 64 62 20 3d 20 70 42 74 2d 3e 64 62 3b 0a *db = pBt->db;.
14100 20 20 20 20 20 20 20 20 20 20 44 62 20 2a 61 44 Db *aD
14110 62 20 3d 20 64 62 2d 3e 61 44 62 3b 0a 20 20 20 b = db->aDb;.
14120 20 20 20 20 20 20 20 75 38 20 6c 65 76 65 6c 20 u8 level
14130 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 66 = 0;. f
14140 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62 or(iDb=0; iDb<db
14150 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 ->nDb; iDb++){.
14160 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 if( a
14170 44 62 5b 69 44 62 5d 2e 70 42 74 20 26 26 20 61 Db[iDb].pBt && a
14180 44 62 5b 69 44 62 5d 2e 70 42 74 2d 3e 70 42 74 Db[iDb].pBt->pBt
14190 3d 3d 70 42 74 20 29 20 62 72 65 61 6b 3b 0a 20 ==pBt ) break;.
141a0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
141b0 20 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 assert( iDb
141c0 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 <db->nDb );.
141d0 20 20 20 20 20 20 6c 65 76 65 6c 20 3d 20 64 62 level = db
141e0 2d 3e 61 44 62 5b 69 44 62 5d 2e 73 61 66 65 74 ->aDb[iDb].safet
141f0 79 5f 6c 65 76 65 6c 3b 0a 20 20 20 20 20 20 20 y_level;.
14200 20 20 20 69 66 28 20 21 53 51 4c 49 54 45 5f 44 if( !SQLITE_D
14210 62 53 61 66 65 74 79 4c 65 76 65 6c 49 73 46 69 bSafetyLevelIsFi
14220 78 65 64 28 6c 65 76 65 6c 29 20 26 26 20 0a 20 xed(level) && .
14230 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
14240 49 54 45 5f 44 62 53 61 66 65 74 79 4c 65 76 65 ITE_DbSafetyLeve
14250 6c 56 61 6c 75 65 28 6c 65 76 65 6c 29 20 21 3d lValue(level) !=
14260 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
14270 57 41 4c 5f 53 41 46 45 54 59 4c 45 56 45 4c 29 WAL_SAFETYLEVEL)
14280 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
14290 61 44 62 5b 69 44 62 5d 2e 73 61 66 65 74 79 5f aDb[iDb].safety_
142a0 6c 65 76 65 6c 20 3d 20 53 51 4c 49 54 45 5f 44 level = SQLITE_D
142b0 45 46 41 55 4c 54 5f 57 41 4c 5f 53 41 46 45 54 EFAULT_WAL_SAFET
142c0 59 4c 45 56 45 4c 3b 0a 20 20 20 20 20 20 20 20 YLEVEL;.
142d0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
142e0 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 SetSafetyLevel(p
142f0 42 74 2d 3e 70 50 61 67 65 72 2c 20 53 51 4c 49 Bt->pPager, SQLI
14300 54 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 53 TE_DEFAULT_WAL_S
14310 41 46 45 54 59 4c 45 56 45 4c 2c 20 0a 20 20 20 AFETYLEVEL, .
14320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14340 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 (db->flags&S
14350 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 29 QLITE_FullFSync)
14360 21 3d 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 !=0,.
14370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14380 20 20 20 20 20 20 20 20 20 20 20 20 28 64 62 2d (db-
14390 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6b >flags&SQLITE_Ck
143a0 70 74 46 75 6c 6c 46 53 79 6e 63 29 21 3d 30 29 ptFullFSync)!=0)
143b0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
143c0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
143d0 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
143e0 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 ge(pPage1);.
143f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14400 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
14410 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
14420 4e 4f 54 41 44 42 3b 0a 20 20 20 20 7d 0a 23 65 NOTADB;. }.#e
14430 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ndif.. /* The
14440 20 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 maximum embedde
14450 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 d fraction must
14460 62 65 20 65 78 61 63 74 6c 79 20 32 35 25 2e 20 be exactly 25%.
14470 20 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d And the minimum
14480 0a 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 . ** embedded
14490 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 fraction must b
144a0 65 20 31 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 e 12.5% for both
144b0 20 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 20 6e leaf-data and n
144c0 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 on-leaf-data..
144d0 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 ** The origina
144e0 6c 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 l design allowed
144f0 20 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 these amounts t
14500 6f 20 76 61 72 79 2c 20 62 75 74 20 61 73 20 6f o vary, but as o
14510 66 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e f. ** version
14520 20 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 75 69 3.6.0, we requi
14530 72 65 20 74 68 65 6d 20 74 6f 20 62 65 20 66 69 re them to be fi
14540 78 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 xed.. */.
14550 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 if( memcmp(&page
14560 31 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 1[21], "\100\040
14570 5c 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 \040",3)!=0 ){.
14580 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
14590 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
145a0 20 7d 0a 20 20 20 20 70 61 67 65 53 69 7a 65 20 }. pageSize
145b0 3d 20 28 70 61 67 65 31 5b 31 36 5d 3c 3c 38 29 = (page1[16]<<8)
145c0 20 7c 20 28 70 61 67 65 31 5b 31 37 5d 3c 3c 31 | (page1[17]<<1
145d0 36 29 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 6);. if( ((pa
145e0 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 geSize-1)&pageSi
145f0 7a 65 29 21 3d 30 0a 20 20 20 20 20 7c 7c 20 70 ze)!=0. || p
14600 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
14610 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 AX_PAGE_SIZE .
14620 20 20 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 3d || pageSize<=
14630 32 35 36 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 256 . ){.
14640 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
14650 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
14660 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 67 assert( (pag
14670 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b eSize & 7)==0 );
14680 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 . usableSize
14690 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 = pageSize - pag
146a0 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 e1[20];. if(
146b0 28 75 33 32 29 70 61 67 65 53 69 7a 65 21 3d 70 (u32)pageSize!=p
146c0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a Bt->pageSize ){.
146d0 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 /* After r
146e0 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 eading the first
146f0 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
14700 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 abase assuming a
14710 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 page size.
14720 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e ** of BtShared.
14730 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 pageSize, we hav
14740 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 e discovered tha
14750 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 t the page-size
14760 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 is. ** actu
14770 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 ally pageSize. U
14780 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
14790 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 se, leave pBt->p
147a0 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a Page1 at. *
147b0 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 * zero and retur
147c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 n SQLITE_OK. The
147d0 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c caller will cal
147e0 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a l this function.
147f0 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 ** again w
14800 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 ith the correct
14810 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 page-size..
14820 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 */. releas
14830 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
14840 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 pBt->usable
14850 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 69 7a Size = usableSiz
14860 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 e;. pBt->pa
14870 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a geSize = pageSiz
14880 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d e;. freeTem
14890 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
148a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
148b0 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
148c0 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 pBt->pPager, &pB
148d0 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 t->pageSize,.
148e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
148f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14900 70 61 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 pageSize-usableS
14910 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ize);. retu
14920 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
14930 20 69 66 28 20 28 70 42 74 2d 3e 64 62 2d 3e 66 if( (pBt->db->f
14940 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 lags & SQLITE_Re
14950 63 6f 76 65 72 79 4d 6f 64 65 29 3d 3d 30 20 26 coveryMode)==0 &
14960 26 20 6e 50 61 67 65 3e 6e 50 61 67 65 46 69 6c & nPage>nPageFil
14970 65 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 e ){. rc =
14980 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
14990 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
149a0 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
149b0 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 d;. }. if(
149c0 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 usableSize<480
149d0 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
149e0 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
149f0 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e . }. pBt->
14a00 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 pageSize = pageS
14a10 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 ize;. pBt->us
14a20 61 62 6c 65 53 69 7a 65 20 3d 20 75 73 61 62 6c ableSize = usabl
14a30 65 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 eSize;.#ifndef S
14a40 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
14a50 41 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 ACUUM. pBt->a
14a60 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 utoVacuum = (get
14a70 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 4byte(&page1[36
14a80 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 + 4*4])?1:0);.
14a90 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 pBt->incrVacuu
14aa0 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 m = (get4byte(&p
14ab0 61 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f age1[36 + 7*4])?
14ac0 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 1:0);.#endif. }
14ad0 0a 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 .. /* maxLocal
14ae0 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 is the maximum a
14af0 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 mount of payload
14b00 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c to store locall
14b10 79 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c y for. ** a cel
14b20 6c 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 l. Make sure it
14b30 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 is small enough
14b40 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 so that at leas
14b50 74 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a t minFanout. **
14b60 20 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 cells can will
14b70 66 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e fit on one page.
14b80 20 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 We assume a 10
14b90 2d 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 -byte page heade
14ba0 72 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 r.. ** Besides
14bb0 74 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 the payload, the
14bc0 20 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 cell must store
14bd0 3a 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 :. ** 2-byt
14be0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 e pointer to the
14bf0 20 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 cell. ** 4
14c00 2d 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e -byte child poin
14c10 74 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 ter. ** 9-b
14c20 79 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 yte nKey value.
14c30 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e ** 4-byte n
14c40 44 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 Data value. **
14c50 20 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 4-byte overf
14c60 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 low page pointer
14c70 0a 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 . ** So a cell
14c80 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d consists of a 2-
14c90 62 79 74 65 20 70 6f 69 6e 74 65 72 2c 20 61 20 byte pointer, a
14ca0 68 65 61 64 65 72 20 77 68 69 63 68 20 69 73 20 header which is
14cb0 61 73 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 as much as. **
14cc0 31 37 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 17 bytes long, 0
14cd0 20 74 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 to N bytes of p
14ce0 61 79 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f ayload, and an o
14cf0 70 74 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f ptional 4 byte o
14d00 76 65 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 verflow. ** pag
14d10 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a e pointer.. */.
14d20 20 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pBt->maxLocal
14d30 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e 75 73 = (u16)((pBt->us
14d40 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f ableSize-12)*64/
14d50 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 70 42 74 255 - 23);. pBt
14d60 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 ->minLocal = (u1
14d70 36 29 28 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 6)((pBt->usableS
14d80 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d ize-12)*32/255 -
14d90 20 32 33 29 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 23);. pBt->max
14da0 4c 65 61 66 20 3d 20 28 75 31 36 29 28 70 42 74 Leaf = (u16)(pBt
14db0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 33 ->usableSize - 3
14dc0 35 29 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 5);. pBt->minLe
14dd0 61 66 20 3d 20 28 75 31 36 29 28 28 70 42 74 2d af = (u16)((pBt-
14de0 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a >usableSize-12)*
14df0 33 32 2f 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 32/255 - 23);.
14e00 69 66 28 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 if( pBt->maxLoca
14e10 6c 3e 31 32 37 20 29 7b 0a 20 20 20 20 70 42 74 l>127 ){. pBt
14e20 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61 ->max1bytePayloa
14e30 64 20 3d 20 31 32 37 3b 0a 20 20 7d 65 6c 73 65 d = 127;. }else
14e40 7b 0a 20 20 20 20 70 42 74 2d 3e 6d 61 78 31 62 {. pBt->max1b
14e50 79 74 65 50 61 79 6c 6f 61 64 20 3d 20 28 75 38 ytePayload = (u8
14e60 29 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a )pBt->maxLocal;.
14e70 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 42 }. assert( pB
14e80 74 2d 3e 6d 61 78 4c 65 61 66 20 2b 20 32 33 20 t->maxLeaf + 23
14e90 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 <= MX_CELL_SIZE(
14ea0 70 42 74 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70 pBt) );. pBt->p
14eb0 50 61 67 65 31 20 3d 20 70 50 61 67 65 31 3b 0a Page1 = pPage1;.
14ec0 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e pBt->nPage = n
14ed0 50 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 Page;. return S
14ee0 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 QLITE_OK;..page1
14ef0 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 _init_failed:.
14f00 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
14f10 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 e1);. pBt->pPag
14f20 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e e1 = 0;. return
14f30 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 rc;.}..#ifndef
14f40 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 NDEBUG./*.** Ret
14f50 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
14f60 66 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f f cursors open o
14f70 6e 20 70 42 74 2e 20 54 68 69 73 20 69 73 20 66 n pBt. This is f
14f80 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 or use.** in ass
14f90 65 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e ert() expression
14fa0 73 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 s, so it is only
14fb0 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 compiled if NDE
14fc0 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 BUG is not.** de
14fd0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c fined..**.** Onl
14fe0 79 20 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 y write cursors
14ff0 61 72 65 20 63 6f 75 6e 74 65 64 20 69 66 20 77 are counted if w
15000 72 4f 6e 6c 79 20 69 73 20 74 72 75 65 2e 20 20 rOnly is true.
15010 49 66 20 77 72 4f 6e 6c 79 20 69 73 0a 2a 2a 20 If wrOnly is.**
15020 66 61 6c 73 65 20 74 68 65 6e 20 61 6c 6c 20 63 false then all c
15030 75 72 73 6f 72 73 20 61 72 65 20 63 6f 75 6e 74 ursors are count
15040 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ed..**.** For th
15050 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
15060 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 63 75 is routine, a cu
15070 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73 rsor is any curs
15080 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61 or that.** is ca
15090 70 61 62 6c 65 20 6f 66 20 72 65 61 64 69 6e 67 pable of reading
150a0 20 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 20 74 or writing to t
150b0 68 65 20 64 61 74 61 62 73 65 2e 20 20 43 75 72 he databse. Cur
150c0 73 6f 72 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 sors that.** hav
150d0 65 20 62 65 65 6e 20 74 72 69 70 70 65 64 20 69 e been tripped i
150e0 6e 74 6f 20 74 68 65 20 43 55 52 53 4f 52 5f 46 nto the CURSOR_F
150f0 41 55 4c 54 20 73 74 61 74 65 20 61 72 65 20 6e AULT state are n
15100 6f 74 20 63 6f 75 6e 74 65 64 2e 0a 2a 2f 0a 73 ot counted..*/.s
15110 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 56 tatic int countV
15120 61 6c 69 64 43 75 72 73 6f 72 73 28 42 74 53 68 alidCursors(BtSh
15130 61 72 65 64 20 2a 70 42 74 2c 20 69 6e 74 20 77 ared *pBt, int w
15140 72 4f 6e 6c 79 29 7b 0a 20 20 42 74 43 75 72 73 rOnly){. BtCurs
15150 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 or *pCur;. int
15160 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 r = 0;. for(pCu
15170 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 r=pBt->pCursor;
15180 70 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d pCur; pCur=pCur-
15190 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
151a0 20 28 77 72 4f 6e 6c 79 3d 3d 30 20 7c 7c 20 28 (wrOnly==0 || (
151b0 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
151c0 20 42 54 43 46 5f 57 72 69 74 65 46 6c 61 67 29 BTCF_WriteFlag)
151d0 21 3d 30 29 0a 20 20 20 20 20 26 26 20 70 43 75 !=0). && pCu
151e0 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f r->eState!=CURSO
151f0 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a R_FAULT ) r++; .
15200 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a }. return r;.
15210 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
15220 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
15230 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73 outstanding curs
15240 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 6e ors and we are n
15250 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 ot in the middle
15260 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 63 .** of a transac
15270 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 69 tion but there i
15280 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e s a read lock on
15290 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 the database, t
152a0 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 hen.** this rout
152b0 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 66 ine unrefs the f
152c0 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
152d0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
152e0 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 hich .** has the
152f0 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 61 effect of relea
15300 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c 6f sing the read lo
15310 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ck..**.** If the
15320 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 re is a transact
15330 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2c ion in progress,
15340 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
15350 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
15360 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 tic void unlockB
15370 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74 53 treeIfUnused(BtS
15380 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 hared *pBt){. a
15390 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
153a0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
153b0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
153c0 74 28 20 63 6f 75 6e 74 56 61 6c 69 64 43 75 72 t( countValidCur
153d0 73 6f 72 73 28 70 42 74 2c 30 29 3d 3d 30 20 7c sors(pBt,0)==0 |
153e0 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 | pBt->inTransac
153f0 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 tion>TRANS_NONE
15400 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e );. if( pBt->in
15410 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
15420 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e NS_NONE && pBt->
15430 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 pPage1!=0 ){.
15440 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
15450 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 age1->aData );.
15460 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
15470 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 e3PagerRefcount(
15480 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 20 pBt->pPager)==1
15490 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
154a0 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
154b0 61 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 a );. release
154c0 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 Page(pBt->pPage1
154d0 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 );. pBt->pPag
154e0 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f e1 = 0;. }.}../
154f0 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e *.** If pBt poin
15500 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 ts to an empty f
15510 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 ile then convert
15520 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 that empty file
15530 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 .** into a new e
15540 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 mpty database by
15550 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
15560 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a e first page of.
15570 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ** the database.
15580 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e .*/.static int n
15590 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 ewDatabase(BtSha
155a0 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d red *pBt){. Mem
155b0 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 Page *pP1;. uns
155c0 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
155d0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
155e0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
155f0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
15600 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
15610 42 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 Bt->nPage>0 ){.
15620 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15630 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d _OK;. }. pP1 =
15640 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 pBt->pPage1;.
15650 61 73 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 assert( pP1!=0 )
15660 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e ;. data = pP1->
15670 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 aData;. rc = sq
15680 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
15690 70 50 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 pP1->pDbPage);.
156a0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
156b0 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 rc;. memcpy(da
156c0 74 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 ta, zMagicHeader
156d0 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 , sizeof(zMagicH
156e0 65 61 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 eader));. asser
156f0 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 t( sizeof(zMagic
15700 48 65 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 Header)==16 );.
15710 20 64 61 74 61 5b 31 36 5d 20 3d 20 28 75 38 29 data[16] = (u8)
15720 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e ((pBt->pageSize>
15730 3e 38 29 26 30 78 66 66 29 3b 0a 20 20 64 61 74 >8)&0xff);. dat
15740 61 5b 31 37 5d 20 3d 20 28 75 38 29 28 28 70 42 a[17] = (u8)((pB
15750 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 31 36 29 t->pageSize>>16)
15760 26 30 78 66 66 29 3b 0a 20 20 64 61 74 61 5b 31 &0xff);. data[1
15770 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 8] = 1;. data[1
15780 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 9] = 1;. assert
15790 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ( pBt->usableSiz
157a0 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e<=pBt->pageSize
157b0 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 && pBt->usableS
157c0 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 ize+255>=pBt->pa
157d0 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b geSize);. data[
157e0 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e 20] = (u8)(pBt->
157f0 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e pageSize - pBt->
15800 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64 usableSize);. d
15810 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 ata[21] = 64;.
15820 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 data[22] = 32;.
15830 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a data[23] = 32;.
15840 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 memset(&data[2
15850 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 4], 0, 100-24);.
15860 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 zeroPage(pP1,
15870 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c PTF_INTKEY|PTF_L
15880 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 EAF|PTF_LEAFDATA
15890 20 29 3b 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c );. pBt->btsFl
158a0 61 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 ags |= BTS_PAGES
158b0 49 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e 64 IZE_FIXED;.#ifnd
158c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
158d0 55 54 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 UTOVACUUM. asse
158e0 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 rt( pBt->autoVac
158f0 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 uum==1 || pBt->a
15900 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a utoVacuum==0 );.
15910 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
15920 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 ncrVacuum==1 ||
15930 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d pBt->incrVacuum=
15940 3d 30 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 =0 );. put4byte
15950 28 26 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d (&data[36 + 4*4]
15960 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 , pBt->autoVacuu
15970 6d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 m);. put4byte(&
15980 64 61 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 data[36 + 7*4],
15990 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 pBt->incrVacuum)
159a0 3b 0a 23 65 6e 64 69 66 0a 20 20 70 42 74 2d 3e ;.#endif. pBt->
159b0 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 64 61 74 nPage = 1;. dat
159c0 61 5b 33 31 5d 20 3d 20 31 3b 0a 20 20 72 65 74 a[31] = 1;. ret
159d0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
159e0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
159f0 7a 65 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ze the first pag
15a00 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
15a10 65 20 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 e file (creating
15a20 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 a database.** c
15a30 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 onsisting of a s
15a40 69 6e 67 6c 65 20 70 61 67 65 20 61 6e 64 20 6e ingle page and n
15a50 6f 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 o schema objects
15a60 29 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ). Return SQLITE
15a70 5f 4f 4b 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 _OK.** if succes
15a80 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 sful, or an SQLi
15a90 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 te error code ot
15aa0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 69 6e 74 20 herwise..*/.int
15ab0 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 77 44 sqlite3BtreeNewD
15ac0 62 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 b(Btree *p){. i
15ad0 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 nt rc;. sqlite3
15ae0 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
15af0 20 70 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 20 3d p->pBt->nPage =
15b00 20 30 3b 0a 20 20 72 63 20 3d 20 6e 65 77 44 61 0;. rc = newDa
15b10 74 61 62 61 73 65 28 70 2d 3e 70 42 74 29 3b 0a tabase(p->pBt);.
15b20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
15b30 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
15b40 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 rc;.}../*.** At
15b50 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 tempt to start a
15b60 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e new transaction
15b70 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 . A write-transa
15b80 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 ction.** is star
15b90 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e ted if the secon
15ba0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f d argument is no
15bb0 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 nzero, otherwise
15bc0 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e a read-.** tran
15bd0 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 saction. If the
15be0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
15bf0 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e is 2 or more an
15c00 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 d exclusive.** t
15c10 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 ransaction is st
15c20 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 arted, meaning t
15c30 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f hat no other pro
15c40 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a cess is allowed.
15c50 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ** to access the
15c60 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 database. A pr
15c70 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 eexisting transa
15c80 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 ction may not be
15c90 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 .** upgraded to
15ca0 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c exclusive by cal
15cb0 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
15cc0 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 e a second time
15cd0 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 - the.** exclusi
15ce0 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 vity flag only w
15cf0 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 orks for a new t
15d00 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
15d10 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 * A write-transa
15d20 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
15d30 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 arted before att
15d40 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 empting any .**
15d50 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 changes to the d
15d60 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f atabase. None o
15d70 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
15d80 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c routines .** wil
15d90 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 l work unless a
15da0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
15db0 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a tarted first:.**
15dc0 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
15dd0 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 BtreeCreateTable
15de0 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
15df0 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 e3BtreeCreateInd
15e00 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c ex().** sql
15e10 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 ite3BtreeClearTa
15e20 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ble().** sq
15e30 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
15e40 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ble().** sq
15e50 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 lite3BtreeInsert
15e60 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
15e70 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a e3BtreeDelete().
15e80 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
15e90 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 treeUpdateMeta()
15ea0 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 .**.** If an ini
15eb0 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 tial attempt to
15ec0 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b acquire the lock
15ed0 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f fails because o
15ee0 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f f lock contentio
15ef0 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 n.** and the dat
15f00 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69 6f abase was previo
15f10 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 usly unlocked, t
15f20 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 hen invoke the b
15f30 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 usy handler.** i
15f40 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 f there is one.
15f50 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77 61 But if there wa
15f60 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 s previously a r
15f70 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 ead-lock, do not
15f80 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 .** invoke the b
15f90 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 usy handler - ju
15fa0 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 st return SQLITE
15fb0 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 _BUSY. SQLITE_B
15fc0 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 USY is .** retur
15fd0 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 ned when there i
15fe0 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 s already a read
15ff0 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 -lock in order t
16000 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f o avoid a deadlo
16010 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 ck..**.** Suppos
16020 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 e there are two
16030 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 processes A and
16040 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61 64 B. A has a read
16050 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a lock and B has.
16060 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f ** a reserved lo
16070 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 ck. B tries to
16080 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 promote to exclu
16090 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 sive but is bloc
160a0 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f ked because.** o
160b0 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e f A's read lock.
160c0 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f A tries to pro
160d0 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 mote to reserved
160e0 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 but is blocked
160f0 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 by B..** One or
16100 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 the other of the
16110 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d two processes m
16120 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 ust give way or
16130 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 there can be.**
16140 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 no progress. By
16150 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 returning SQLIT
16160 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 E_BUSY and not i
16170 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 nvoking the busy
16180 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 callback.** whe
16190 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 n A already has
161a0 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 a read lock, we
161b0 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 encourage A to g
161c0 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 ive up and let B
161d0 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a .** proceed..*/.
161e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
161f0 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 BeginTrans(Btree
16200 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 *p, int wrflag)
16210 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c {. sqlite3 *pBl
16220 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 ock = 0;. BtSha
16230 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
16240 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
16250 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 LITE_OK;.. sqli
16260 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
16270 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 ;. btreeIntegri
16280 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ty(p);.. /* If
16290 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 the btree is alr
162a0 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d eady in a write-
162b0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 transaction, or
162c0 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 it. ** is alrea
162d0 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 dy in a read-tra
162e0 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 nsaction and a r
162f0 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a ead-transaction.
16300 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 ** is requeste
16310 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d d, this is a no-
16320 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 op.. */. if( p
16330 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
16340 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e _WRITE || (p->in
16350 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 Trans==TRANS_REA
16360 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b D && !wrflag) ){
16370 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f . goto trans_
16380 62 65 67 75 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 begun;. }. ass
16390 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
163a0 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
163b0 52 49 54 45 20 7c 7c 20 49 66 4e 6f 74 4f 6d 69 RITE || IfNotOmi
163c0 74 41 56 28 70 42 74 2d 3e 62 44 6f 54 72 75 6e tAV(pBt->bDoTrun
163d0 63 61 74 65 29 3d 3d 30 20 29 3b 0a 0a 20 20 2f cate)==0 );.. /
163e0 2a 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 * Write transact
163f0 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 ions are not pos
16400 73 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d sible on a read-
16410 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f only database */
16420 0a 20 20 69 66 28 20 28 70 42 74 2d 3e 62 74 73 . if( (pBt->bts
16430 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 Flags & BTS_READ
16440 5f 4f 4e 4c 59 29 21 3d 30 20 26 26 20 77 72 66 _ONLY)!=0 && wrf
16450 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 lag ){. rc =
16460 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
16470 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f . goto trans_
16480 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e begun;. }..#ifn
16490 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
164a0 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f SHARED_CACHE. /
164b0 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 * If another dat
164c0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 68 61 73 abase handle has
164d0 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 already opened
164e0 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 a write transact
164f0 69 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 ion . ** on thi
16500 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 s shared-btree s
16510 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 73 tructure and a s
16520 65 63 6f 6e 64 20 77 72 69 74 65 20 74 72 61 6e econd write tran
16530 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 saction is. **
16540 72 65 71 75 65 73 74 65 64 2c 20 72 65 74 75 72 requested, retur
16550 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e n SQLITE_LOCKED.
16560 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 77 72 66 . */. if( (wrf
16570 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 lag && pBt->inTr
16580 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
16590 5f 57 52 49 54 45 29 0a 20 20 20 7c 7c 20 28 70 _WRITE). || (p
165a0 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
165b0 54 53 5f 50 45 4e 44 49 4e 47 29 21 3d 30 0a 20 TS_PENDING)!=0.
165c0 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d ){. pBlock =
165d0 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 pBt->pWriter->d
165e0 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 b;. }else if( w
165f0 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 rflag>1 ){. B
16600 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 tLock *pIter;.
16610 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d for(pIter=pBt-
16620 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 >pLock; pIter; p
16630 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 Iter=pIter->pNex
16640 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 t){. if( pI
16650 74 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 ter->pBtree!=p )
16660 7b 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b {. pBlock
16670 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 = pIter->pBtree
16680 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 ->db;. br
16690 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
166a0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c }. }. if( pBl
166b0 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ock ){. sqlit
166c0 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
166d0 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 ked(p->db, pBloc
166e0 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c k);. rc = SQL
166f0 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
16700 44 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f DCACHE;. goto
16710 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 trans_begun;.
16720 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 }.#endif.. /* A
16730 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 ny read-only or
16740 72 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 read-write trans
16750 61 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 action implies a
16760 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 read-lock on .
16770 20 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f 20 69 ** page 1. So i
16780 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 f some other sha
16790 72 65 64 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 red-cache client
167a0 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 77 already has a w
167b0 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 rite-lock . **
167c0 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 on page 1, the t
167d0 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f ransaction canno
167e0 74 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a t be opened. */.
167f0 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 rc = queryShar
16800 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
16810 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c (p, MASTER_ROOT,
16820 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 READ_LOCK);. i
16830 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 f( SQLITE_OK!=rc
16840 20 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 ) goto trans_be
16850 67 75 6e 3b 0a 0a 20 20 70 42 74 2d 3e 62 74 73 gun;.. pBt->bts
16860 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 49 4e Flags &= ~BTS_IN
16870 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 3b 0a 20 ITIALLY_EMPTY;.
16880 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d if( pBt->nPage=
16890 3d 30 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 =0 ) pBt->btsFla
168a0 67 73 20 7c 3d 20 42 54 53 5f 49 4e 49 54 49 41 gs |= BTS_INITIA
168b0 4c 4c 59 5f 45 4d 50 54 59 3b 0a 20 20 64 6f 20 LLY_EMPTY;. do
168c0 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f {. /* Call lo
168d0 63 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 ckBtree() until
168e0 65 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 either pBt->pPag
168f0 65 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 e1 is populated
16900 6f 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 or. ** lockBt
16910 72 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f ree() returns so
16920 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 mething other th
16930 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f an SQLITE_OK. lo
16940 63 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a ckBtree(). **
16950 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
16960 54 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 TE_OK but leave
16970 70 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 pBt->pPage1 set
16980 74 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 to 0 if after.
16990 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 ** reading pag
169a0 65 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 e 1 it discovers
169b0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 that the page-s
169c0 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
169d0 61 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 ase . ** file
169e0 20 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 is not pBt->pag
169f0 65 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 eSize. In this c
16a00 61 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 ase lockBtree()
16a10 77 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 will update.
16a20 2a 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 ** pBt->pageSize
16a30 20 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a to the page-siz
16a40 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e e of the file on
16a50 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 disk.. */.
16a60 20 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 while( pBt->pP
16a70 61 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 age1==0 && SQLIT
16a80 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b E_OK==(rc = lock
16a90 42 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a Btree(pBt)) );..
16aa0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
16ab0 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 TE_OK && wrflag
16ac0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 42 ){. if( (pB
16ad0 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
16ae0 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d 30 20 S_READ_ONLY)!=0
16af0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
16b00 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
16b10 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
16b20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16b30 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 42 74 e3PagerBegin(pBt
16b40 2d 3e 70 50 61 67 65 72 2c 77 72 66 6c 61 67 3e ->pPager,wrflag>
16b50 31 2c 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 1,sqlite3TempInM
16b60 65 6d 6f 72 79 28 70 2d 3e 64 62 29 29 3b 0a 20 emory(p->db));.
16b70 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
16b80 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16b90 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 rc = newDa
16ba0 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 tabase(pBt);.
16bb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
16bc0 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 }. . if(
16bd0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
16be0 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
16bf0 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b eeIfUnused(pBt);
16c00 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 . }. }while(
16c10 20 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c 49 (rc&0xFF)==SQLI
16c20 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e TE_BUSY && pBt->
16c30 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
16c40 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 RANS_NONE &&.
16c50 20 20 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f btreeInvo
16c60 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 keBusyHandler(pB
16c70 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d t) );.. if( rc=
16c80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16c90 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
16ca0 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a ==TRANS_NONE ){.
16cb0 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e pBt->nTran
16cc0 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 saction++;.#ifnd
16cd0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
16ce0 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 HARED_CACHE.
16cf0 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c if( p->sharabl
16d00 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 e ){. ass
16d10 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 ert( p->lock.pBt
16d20 72 65 65 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 ree==p && p->loc
16d30 6b 2e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 k.iTable==1 );.
16d40 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 p->lock.e
16d50 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b Lock = READ_LOCK
16d60 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 ;. p->loc
16d70 6b 2e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 k.pNext = pBt->p
16d80 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 70 42 Lock;. pB
16d90 74 2d 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c t->pLock = &p->l
16da0 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e ock;. }.#en
16db0 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d dif. }. p-
16dc0 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c >inTrans = (wrfl
16dd0 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 ag?TRANS_WRITE:T
16de0 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 RANS_READ);.
16df0 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 if( p->inTrans>p
16e00 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
16e10 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e n ){. pBt->
16e20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
16e30 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 p->inTrans;.
16e40 7d 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 }. if( wrflag
16e50 20 29 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 ){. MemPag
16e60 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d e *pPage1 = pBt-
16e70 3e 70 50 61 67 65 31 3b 0a 23 69 66 6e 64 65 66 >pPage1;.#ifndef
16e80 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
16e90 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 RED_CACHE.
16ea0 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 57 assert( !pBt->pW
16eb0 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 70 riter );. p
16ec0 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 70 3b Bt->pWriter = p;
16ed0 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 . pBt->btsF
16ee0 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 45 58 43 lags &= ~BTS_EXC
16ef0 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 69 66 LUSIVE;. if
16f00 28 20 77 72 66 6c 61 67 3e 31 20 29 20 70 42 74 ( wrflag>1 ) pBt
16f10 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 ->btsFlags |= BT
16f20 53 5f 45 58 43 4c 55 53 49 56 45 3b 0a 23 65 6e S_EXCLUSIVE;.#en
16f30 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 dif.. /* If
16f40 20 74 68 65 20 64 62 2d 73 69 7a 65 20 68 65 61 the db-size hea
16f50 64 65 72 20 66 69 65 6c 64 20 69 73 20 69 6e 63 der field is inc
16f60 6f 72 72 65 63 74 20 28 61 73 20 69 74 20 6d 61 orrect (as it ma
16f70 79 20 62 65 20 69 66 20 61 6e 20 6f 6c 64 0a 20 y be if an old.
16f80 20 20 20 20 20 2a 2a 20 63 6c 69 65 6e 74 20 68 ** client h
16f90 61 73 20 62 65 65 6e 20 77 72 69 74 69 6e 67 20 as been writing
16fa0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
16fb0 65 29 2c 20 75 70 64 61 74 65 20 69 74 20 6e 6f e), update it no
16fc0 77 2e 20 44 6f 69 6e 67 0a 20 20 20 20 20 20 2a w. Doing. *
16fd0 2a 20 74 68 69 73 20 73 6f 6f 6e 65 72 20 72 61 * this sooner ra
16fe0 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 20 ther than later
16ff0 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 means the databa
17000 73 65 20 73 69 7a 65 20 63 61 6e 20 73 61 66 65 se size can safe
17010 6c 79 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 2d ly . ** re-
17020 72 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 read the databas
17030 65 20 73 69 7a 65 20 66 72 6f 6d 20 70 61 67 65 e size from page
17040 20 31 20 69 66 20 61 20 73 61 76 65 70 6f 69 6e 1 if a savepoin
17050 74 20 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e t or transaction
17060 0a 20 20 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 . ** rollba
17070 63 6b 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e ck occurs within
17080 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
17090 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
170a0 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 if( pBt->nPage!
170b0 3d 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 =get4byte(&pPage
170c0 31 2d 3e 61 44 61 74 61 5b 32 38 5d 29 20 29 7b 1->aData[28]) ){
170d0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
170e0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
170f0 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
17100 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
17110 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17120 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
17130 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
17140 5b 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 67 65 [28], pBt->nPage
17150 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
17160 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
17170 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 .trans_begun:.
17180 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17190 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 K && wrflag ){.
171a0 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 /* This call
171b0 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 makes sure that
171c0 74 68 65 20 70 61 67 65 72 20 68 61 73 20 74 68 the pager has th
171d0 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 e correct number
171e0 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 of. ** open
171f0 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 savepoints. If t
17200 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
17210 74 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 ter is greater t
17220 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a han 0 and. **
17230 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
17240 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
17250 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 open, then it wi
17260 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 ll be opened her
17270 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 e.. */. rc
17280 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f = sqlite3PagerO
17290 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 penSavepoint(pBt
172a0 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d ->pPager, p->db-
172b0 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 >nSavepoint);.
172c0 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 }.. btreeIntegr
172d0 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ity(p);. sqlite
172e0 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
172f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
17300 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17310 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a MIT_AUTOVACUUM..
17320 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f /*.** Set the po
17330 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 inter-map entrie
17340 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 s for all childr
17350 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 en of page pPage
17360 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 . Also, if.** pP
17370 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c age contains cel
17380 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f ls that point to
17390 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c overflow pages,
173a0 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 set the pointer
173b0 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 .** map entries
173c0 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 for the overflow
173d0 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a pages as well..
173e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 */.static int se
173f0 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 tChildPtrmaps(Me
17400 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 mPage *pPage){.
17410 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
17420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17430 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 /* Counter v
17440 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 ariable */. int
17450 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 nCell;
17460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17470 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c /* Number of cel
17480 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 ls in page pPage
17490 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
174a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
174b0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
174c0 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 rn code */. BtS
174d0 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 hared *pBt = pPa
174e0 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 ge->pBt;. u8 is
174f0 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 InitOrig = pPage
17500 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f ->isInit;. Pgno
17510 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 pgno = pPage->p
17520 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 gno;.. assert(
17530 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17540 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
17550 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 utex) );. rc =
17560 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 btreeInitPage(pP
17570 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
17580 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17590 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f goto set_child_
175a0 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 7d ptrmaps_out;. }
175b0 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 . nCell = pPage
175c0 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 ->nCell;.. for(
175d0 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
175e0 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c +){. u8 *pCel
175f0 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
17600 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70 74 72 ge, i);.. ptr
17610 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 mapPutOvflPtr(pP
17620 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 age, pCell, &rc)
17630 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 ;.. if( !pPag
17640 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
17650 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 Pgno childPgno
17660 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c = get4byte(pCell
17670 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 );. ptrmapP
17680 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e ut(pBt, childPgn
17690 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c o, PTRMAP_BTREE,
176a0 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 pgno, &rc);.
176b0 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 }. }.. if( !p
176c0 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
176d0 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f Pgno childPgno
176e0 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
176f0 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
17700 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
17710 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
17720 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 t, childPgno, PT
17730 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f RMAP_BTREE, pgno
17740 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 , &rc);. }..set
17750 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f _child_ptrmaps_o
17760 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 ut:. pPage->isI
17770 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 nit = isInitOrig
17780 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
17790 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 ../*.** Somewher
177a0 65 20 6f 6e 20 70 50 61 67 65 20 69 73 20 61 20 e on pPage is a
177b0 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 pointer to page
177c0 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 20 74 iFrom. Modify t
177d0 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f 0a 2a his pointer so.*
177e0 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 * that it points
177f0 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 to iTo. Paramet
17800 65 72 20 65 54 79 70 65 20 64 65 73 63 72 69 62 er eType describ
17810 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 es the type of p
17820 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 65 20 ointer to.** be
17830 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 20 66 6f modified, as fo
17840 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 llows:.**.** PTR
17850 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 MAP_BTREE: p
17860 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d Page is a btree-
17870 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 page. The pointe
17880 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 r points at a ch
17890 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ild .**
178a0 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 6f page o
178b0 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 f pPage..**.** P
178c0 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a TRMAP_OVERFLOW1:
178d0 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 pPage is a btre
178e0 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e e-page. The poin
178f0 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e ter points at an
17900 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 overflow.**
17910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
17920 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 age pointed to b
17930 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c y one of the cel
17940 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a ls on pPage..**.
17950 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ** PTRMAP_OVERFL
17960 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e OW2: pPage is an
17970 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 overflow-page.
17980 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e The pointer poin
17990 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a ts at the next.*
179a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
179b0 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 overflow pag
179c0 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a e in the list..*
179d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 /.static int mod
179e0 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d ifyPagePointer(M
179f0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 emPage *pPage, P
17a00 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 gno iFrom, Pgno
17a10 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a iTo, u8 eType){.
17a20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17a30 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
17a40 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
17a50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
17a60 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
17a70 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
17a80 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 age) );. if( eT
17a90 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
17aa0 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 FLOW2 ){. /*
17ab0 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 The pointer is a
17ac0 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 lways the first
17ad0 34 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 4 bytes of the p
17ae0 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 age in this case
17af0 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 . */. if( ge
17b00 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 t4byte(pPage->aD
17b10 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 ata)!=iFrom ){.
17b20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
17b30 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
17b40 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 . }. put4b
17b50 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 yte(pPage->aData
17b60 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b , iTo);. }else{
17b70 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 . u8 isInitOr
17b80 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e ig = pPage->isIn
17b90 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 it;. int i;.
17ba0 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 int nCell;..
17bb0 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 btreeInitPage
17bc0 28 70 50 61 67 65 29 3b 0a 20 20 20 20 6e 43 65 (pPage);. nCe
17bd0 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c ll = pPage->nCel
17be0 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b l;.. for(i=0;
17bf0 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
17c00 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 u8 *pCell
17c10 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
17c20 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , i);. if(
17c30 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
17c40 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20 20 ERFLOW1 ){.
17c50 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f CellInfo info
17c60 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 65 50 ;. btreeP
17c70 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
17c80 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
17c90 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e ;. if( in
17ca0 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 0a 20 20 20 fo.iOverflow.
17cb0 20 20 20 20 20 20 26 26 20 70 43 65 6c 6c 2b 69 && pCell+i
17cc0 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 2b 33 3c nfo.iOverflow+3<
17cd0 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 2b 70 50 =pPage->aData+pP
17ce0 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 0a 20 20 age->maskPage.
17cf0 20 20 20 20 20 20 20 26 26 20 69 46 72 6f 6d 3d && iFrom=
17d00 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c =get4byte(&pCell
17d10 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
17d20 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 ). ){.
17d30 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
17d40 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
17d50 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 rflow], iTo);.
17d60 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
17d70 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
17d80 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 else{. if
17d90 28 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c ( get4byte(pCell
17da0 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 )==iFrom ){.
17db0 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
17dc0 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 Cell, iTo);.
17dd0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
17de0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
17df0 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 }. . if(
17e00 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 i==nCell ){.
17e10 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 if( eType!=PTR
17e20 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 MAP_BTREE || .
17e30 20 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 get4byte
17e40 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
17e50 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
17e60 38 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 8])!=iFrom ){.
17e70 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
17e80 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
17e90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17ea0 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d put4byte(&pPage-
17eb0 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
17ec0 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 rOffset+8], iTo)
17ed0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 ;. }.. pPa
17ee0 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 ge->isInit = isI
17ef0 6e 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 nitOrig;. }. r
17f00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17f10 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 .}.../*.** Move
17f20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 the open databas
17f30 65 20 70 61 67 65 20 70 44 62 50 61 67 65 20 74 e page pDbPage t
17f40 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 o location iFree
17f50 50 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 Page in the .**
17f60 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 database. The pD
17f70 62 50 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 bPage reference
17f80 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a remains valid..*
17f90 2a 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 *.** The isCommi
17fa0 74 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 t flag indicates
17fb0 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e that there is n
17fc0 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 o need to rememb
17fd0 65 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a er that.** the j
17fe0 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 ournal needs to
17ff0 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f be sync()ed befo
18000 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 re database page
18010 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a pDbPage->pgno .
18020 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 ** can be writte
18030 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 n to. The caller
18040 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f has already pro
18050 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 mised not to wri
18060 74 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 te to that.** pa
18070 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ge..*/.static in
18080 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a t relocatePage(.
18090 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c BtShared *pBt,
180a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 /* Bt
180b0 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ree */. MemPage
180c0 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 *pDbPage,
180d0 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 /* Open page t
180e0 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 o move */. u8 e
180f0 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 Type,
18100 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
18110 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 map 'type' entry
18120 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a for pDbPage */.
18130 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c Pgno iPtrPage,
18140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
18150 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d inter map 'page-
18160 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 no' entry for pD
18170 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 bPage */. Pgno
18180 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 iFreePage,
18190 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 /* The locat
181a0 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 ion to move pDbP
181b0 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 age to */. int
181c0 69 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 isCommit
181d0 20 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 /* isCommit
181e0 20 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 flag passed to
181f0 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 sqlite3PagerMove
18200 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d page */.){. Mem
18210 50 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 Page *pPtrPage;
18220 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 /* The page th
18230 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f at contains a po
18240 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 inter to pDbPage
18250 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 */. Pgno iDbPa
18260 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 ge = pDbPage->pg
18270 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 no;. Pager *pPa
18280 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 ger = pBt->pPage
18290 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 r;. int rc;..
182a0 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 assert( eType==P
182b0 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 TRMAP_OVERFLOW2
182c0 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 || eType==PTRMAP
182d0 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 _OVERFLOW1 || .
182e0 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d eType==PTRM
182f0 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 AP_BTREE || eTyp
18300 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 e==PTRMAP_ROOTPA
18310 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 GE );. assert(
18320 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
18330 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
18340 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 50 ;. assert( pDbP
18350 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b age->pBt==pBt );
18360 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 .. /* Move page
18370 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 iDbPage from it
18380 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 s current locati
18390 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 on to page numbe
183a0 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 r iFreePage */.
183b0 20 54 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 TRACE(("AUTOVAC
183c0 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 UUM: Moving %d t
183d0 6f 20 66 72 65 65 20 70 61 67 65 20 25 64 20 28 o free page %d (
183e0 70 74 72 20 70 61 67 65 20 25 64 20 74 79 70 65 ptr page %d type
183f0 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 %d)\n", .
18400 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 iDbPage, iFreePa
18410 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 ge, iPtrPage, eT
18420 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 ype));. rc = sq
18430 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 lite3PagerMovepa
18440 67 65 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 ge(pPager, pDbPa
18450 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 ge->pDbPage, iFr
18460 65 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 eePage, isCommit
18470 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
18480 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
18490 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 turn rc;. }. p
184a0 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 DbPage->pgno = i
184b0 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 FreePage;.. /*
184c0 49 66 20 70 44 62 50 61 67 65 20 77 61 73 20 61 If pDbPage was a
184d0 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 btree-page, the
184e0 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 n it may have ch
184f0 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 ild pages and/or
18500 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 cells. ** that
18510 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c point to overfl
18520 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f ow pages. The po
18530 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 inter map entrie
18540 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a s for all these.
18550 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 ** pages need
18560 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 to be changed..
18570 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 **. ** If pDbP
18580 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c age is an overfl
18590 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 ow page, then th
185a0 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 e first 4 bytes
185b0 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a may store a. **
185c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 pointer to a su
185d0 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f bsequent overflo
185e0 77 20 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 w page. If this
185f0 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 is the case, the
18600 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 n. ** the point
18610 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 er map needs to
18620 62 65 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 be updated for t
18630 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 he subsequent ov
18640 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a erflow page.. *
18650 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 /. if( eType==P
18660 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 TRMAP_BTREE || e
18670 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f Type==PTRMAP_ROO
18680 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 TPAGE ){. rc
18690 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 = setChildPtrmap
186a0 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 s(pDbPage);.
186b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
186c0 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
186d0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 n rc;. }. }e
186e0 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 lse{. Pgno ne
186f0 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 xtOvfl = get4byt
18700 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 e(pDbPage->aData
18710 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f );. if( nextO
18720 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 vfl!=0 ){.
18730 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e ptrmapPut(pBt, n
18740 65 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f extOvfl, PTRMAP_
18750 4f 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 OVERFLOW2, iFree
18760 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 Page, &rc);.
18770 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18780 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
18790 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
187a0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f }. }. }.. /
187b0 2a 20 46 69 78 20 74 68 65 20 64 61 74 61 62 61 * Fix the databa
187c0 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 se pointer on pa
187d0 67 65 20 69 50 74 72 50 61 67 65 20 74 68 61 74 ge iPtrPage that
187e0 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 pointed at iDbP
187f0 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 age so. ** that
18800 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 it points at iF
18810 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 reePage. Also fi
18820 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 x the pointer ma
18830 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a p entry for. **
18840 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a iPtrPage.. */.
18850 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 if( eType!=PTR
18860 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a MAP_ROOTPAGE ){.
18870 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
18880 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 tPage(pBt, iPtrP
18890 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 age, &pPtrPage,
188a0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0);. if( rc!=
188b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
188c0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
188d0 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
188e0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
188f0 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 PtrPage->pDbPage
18900 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
18910 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18920 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
18930 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 trPage);. r
18940 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
18950 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 rc = modifyP
18960 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 agePointer(pPtrP
18970 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 age, iDbPage, iF
18980 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b reePage, eType);
18990 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
189a0 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 (pPtrPage);.
189b0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
189c0 4b 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 K ){. ptrma
189d0 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 pPut(pBt, iFreeP
189e0 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 age, eType, iPtr
189f0 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 Page, &rc);.
18a00 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
18a10 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 c;.}../* Forward
18a20 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 declaration req
18a30 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 uired by incrVac
18a40 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 uumStep(). */.st
18a50 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 atic int allocat
18a60 65 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61 eBtreePage(BtSha
18a70 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a red *, MemPage *
18a80 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c *, Pgno *, Pgno,
18a90 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 u8);../*.** Per
18aa0 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 form a single st
18ab0 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 ep of an increme
18ac0 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 ntal-vacuum. If
18ad0 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 successful, retu
18ae0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e rn.** SQLITE_OK.
18af0 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
18b00 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 work to do (and
18b10 74 68 65 72 65 66 6f 72 65 20 6e 6f 20 70 6f 69 therefore no poi
18b20 6e 74 20 69 6e 20 0a 2a 2a 20 63 61 6c 6c 69 6e nt in .** callin
18b30 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 g this function
18b40 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 again), return S
18b50 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 4f 72 2c 20 QLITE_DONE. Or,
18b60 69 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 if an error .**
18b70 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 73 occurs, return s
18b80 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 ome other error
18b90 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 code..**.** More
18ba0 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 specificly, thi
18bb0 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d s function attem
18bc0 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 pts to re-organi
18bd0 7a 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ze the database
18be0 73 6f 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 so .** that the
18bf0 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 last page of the
18c00 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 file currently
18c10 69 6e 20 75 73 65 20 69 73 20 6e 6f 20 6c 6f 6e in use is no lon
18c20 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a ger in use..**.*
18c30 2a 20 50 61 72 61 6d 65 74 65 72 20 6e 46 69 6e * Parameter nFin
18c40 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
18c50 66 20 70 61 67 65 73 20 74 68 61 74 20 74 68 69 f pages that thi
18c60 73 20 64 61 74 61 62 61 73 65 20 77 6f 75 6c 64 s database would
18c70 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 77 65 72 65 contain.** were
18c80 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 this function c
18c90 61 6c 6c 65 64 20 75 6e 74 69 6c 20 69 74 20 72 alled until it r
18ca0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f eturns SQLITE_DO
18cb0 4e 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 NE..**.** If the
18cc0 20 62 43 6f 6d 6d 69 74 20 70 61 72 61 6d 65 74 bCommit paramet
18cd0 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 er is non-zero,
18ce0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 this function as
18cf0 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 0a sumes that the .
18d00 2a 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b ** caller will k
18d10 65 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 eep calling incr
18d20 56 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 VacuumStep() unt
18d30 69 6c 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 il it returns SQ
18d40 4c 49 54 45 5f 44 4f 4e 45 20 0a 2a 2a 20 6f 72 LITE_DONE .** or
18d50 20 61 6e 20 65 72 72 6f 72 2e 20 62 43 6f 6d 6d an error. bComm
18d60 69 74 20 69 73 20 70 61 73 73 65 64 20 74 72 75 it is passed tru
18d70 65 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 e for an auto-va
18d80 63 75 75 6d 2d 6f 6e 2d 63 6f 6d 6d 6d 69 74 20 cuum-on-commmit
18d90 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20 6f .** operation, o
18da0 72 20 66 61 6c 73 65 20 66 6f 72 20 61 6e 20 69 r false for an i
18db0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
18dc0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
18dd0 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
18de0 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
18df0 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 gno nFin, Pgno i
18e00 4c 61 73 74 50 67 2c 20 69 6e 74 20 62 43 6f 6d LastPg, int bCom
18e10 6d 69 74 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 mit){. Pgno nFr
18e20 65 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 eeList;
18e30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
18e40 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 ages still on th
18e50 65 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 20 e free-list */.
18e60 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 int rc;.. asse
18e70 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
18e80 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
18e90 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
18ea0 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b 0a iLastPg>nFin );.
18eb0 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 . if( !PTRMAP_I
18ec0 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
18ed0 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d Pg) && iLastPg!=
18ee0 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
18ef0 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 75 38 E(pBt) ){. u8
18f00 20 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f eType;. Pgno
18f10 20 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 iPtrPage;..
18f20 6e 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 nFreeList = get4
18f30 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 byte(&pBt->pPage
18f40 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 1->aData[36]);.
18f50 20 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 if( nFreeList
18f60 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
18f70 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
18f80 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d . }.. rc =
18f90 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
18fa0 69 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c iLastPg, &eType,
18fb0 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 &iPtrPage);.
18fc0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18fd0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
18fe0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
18ff0 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
19000 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 AP_ROOTPAGE ){.
19010 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
19020 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
19030 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
19040 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 eType==PTRMAP_FR
19050 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 EEPAGE ){.
19060 69 66 28 20 62 43 6f 6d 6d 69 74 3d 3d 30 20 29 if( bCommit==0 )
19070 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d {. /* Rem
19080 6f 76 65 20 74 68 65 20 70 61 67 65 20 66 72 6f ove the page fro
19090 6d 20 74 68 65 20 66 69 6c 65 73 20 66 72 65 65 m the files free
190a0 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 6e -list. This is n
190b0 6f 74 20 72 65 71 75 69 72 65 64 0a 20 20 20 20 ot required.
190c0 20 20 20 20 2a 2a 20 69 66 20 62 43 6f 6d 6d 69 ** if bCommi
190d0 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 t is non-zero. I
190e0 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 n that case, the
190f0 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 free-list will
19100 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 be. ** tr
19110 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 uncated to zero
19120 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
19130 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 ion returns, so
19140 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 it doesn't .
19150 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 ** matter if
19160 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 it still contai
19170 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 ns some garbage
19180 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 entries..
19190 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f */. Pgno
191a0 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 iFreePg;.
191b0 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 MemPage *pFree
191c0 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d Pg;. rc =
191d0 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
191e0 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 ge(pBt, &pFreePg
191f0 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73 , &iFreePg, iLas
19200 74 50 67 2c 20 42 54 41 4c 4c 4f 43 5f 45 58 41 tPg, BTALLOC_EXA
19210 43 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 CT);. if(
19220 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
19230 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
19240 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
19250 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
19260 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 iFreePg==iLastP
19270 67 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c g );. rel
19280 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50 67 easePage(pFreePg
19290 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
192a0 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 50 67 else {. Pg
192b0 6e 6f 20 69 46 72 65 65 50 67 3b 20 20 20 20 20 no iFreePg;
192c0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
192d0 20 6f 66 20 66 72 65 65 20 70 61 67 65 20 74 6f of free page to
192e0 20 6d 6f 76 65 20 70 4c 61 73 74 50 67 20 74 6f move pLastPg to
192f0 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 */. MemPag
19300 65 20 2a 70 4c 61 73 74 50 67 3b 0a 20 20 20 20 e *pLastPg;.
19310 20 20 75 38 20 65 4d 6f 64 65 20 3d 20 42 54 41 u8 eMode = BTA
19320 4c 4c 4f 43 5f 41 4e 59 3b 20 20 20 2f 2a 20 4d LLOC_ANY; /* M
19330 6f 64 65 20 70 61 72 61 6d 65 74 65 72 20 66 6f ode parameter fo
19340 72 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 r allocateBtreeP
19350 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 50 age() */. P
19360 67 6e 6f 20 69 4e 65 61 72 20 3d 20 30 3b 20 20 gno iNear = 0;
19370 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 65 61 72 /* near
19380 62 79 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 by parameter for
19390 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
193a0 67 65 28 29 20 2a 2f 0a 0a 20 20 20 20 20 20 72 ge() */.. r
193b0 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
193c0 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 (pBt, iLastPg, &
193d0 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 pLastPg, 0);.
193e0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
193f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
19400 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
19410 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
19420 62 43 6f 6d 6d 69 74 20 69 73 20 7a 65 72 6f 2c bCommit is zero,
19430 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 this loop runs
19440 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 exactly once and
19450 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 page pLastPg.
19460 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 ** is swappe
19470 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 d with the first
19480 20 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 free page pulle
19490 64 20 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c d off the free l
194a0 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ist.. **.
194b0 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 ** On the ot
194c0 68 65 72 20 68 61 6e 64 2c 20 69 66 20 62 43 6f her hand, if bCo
194d0 6d 6d 69 74 20 69 73 20 67 72 65 61 74 65 72 20 mmit is greater
194e0 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 than zero, then
194f0 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f keep. ** lo
19500 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 oping until a fr
19510 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65 64 20 ee-page located
19520 77 69 74 68 69 6e 20 74 68 65 20 66 69 72 73 74 within the first
19530 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20 20 20 nFin pages.
19540 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 ** of the file
19550 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 is found..
19560 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 43 */. if( bC
19570 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ommit==0 ){.
19580 20 20 20 20 65 4d 6f 64 65 20 3d 20 42 54 41 4c eMode = BTAL
19590 4c 4f 43 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20 LOC_LE;.
195a0 69 4e 65 61 72 20 3d 20 6e 46 69 6e 3b 0a 20 20 iNear = nFin;.
195b0 20 20 20 20 7d 0a 20 20 20 20 20 20 64 6f 20 7b }. do {
195c0 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 . MemPage
195d0 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 *pFreePg;.
195e0 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
195f0 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
19600 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 pFreePg, &iFreeP
19610 67 2c 20 69 4e 65 61 72 2c 20 65 4d 6f 64 65 29 g, iNear, eMode)
19620 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
19630 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
19640 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 release
19650 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 Page(pLastPg);.
19660 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
19670 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
19680 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
19690 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 e(pFreePg);.
196a0 20 20 7d 77 68 69 6c 65 28 20 62 43 6f 6d 6d 69 }while( bCommi
196b0 74 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 t && iFreePg>nFi
196c0 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 n );. asser
196d0 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 t( iFreePg<iLast
196e0 50 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 Pg );. .
196f0 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 rc = relocate
19700 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 Page(pBt, pLastP
19710 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 g, eType, iPtrPa
19720 67 65 2c 20 69 46 72 65 65 50 67 2c 20 62 43 6f ge, iFreePg, bCo
19730 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 72 65 6c mmit);. rel
19740 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 easePage(pLastPg
19750 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
19760 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19770 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
19780 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
19790 20 7d 0a 0a 20 20 69 66 28 20 62 43 6f 6d 6d 69 }.. if( bCommi
197a0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 6f 20 7b t==0 ){. do {
197b0 0a 20 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d . iLastPg--
197c0 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 69 4c ;. }while( iL
197d0 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 astPg==PENDING_B
197e0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 7c 7c YTE_PAGE(pBt) ||
197f0 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
19800 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 3b 0a Bt, iLastPg) );.
19810 20 20 20 20 70 42 74 2d 3e 62 44 6f 54 72 75 6e pBt->bDoTrun
19820 63 61 74 65 20 3d 20 31 3b 0a 20 20 20 20 70 42 cate = 1;. pB
19830 74 2d 3e 6e 50 61 67 65 20 3d 20 69 4c 61 73 74 t->nPage = iLast
19840 50 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e Pg;. }. return
19850 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
19860 2a 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61 73 *.** The databas
19870 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 e opened by the
19880 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
19890 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
198a0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 6e 4f 72 database.** nOr
198b0 69 67 20 70 61 67 65 73 20 69 6e 20 73 69 7a 65 ig pages in size
198c0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 46 72 65 containing nFre
198d0 65 20 66 72 65 65 20 70 61 67 65 73 2e 20 52 65 e free pages. Re
198e0 74 75 72 6e 20 74 68 65 20 65 78 70 65 63 74 65 turn the expecte
198f0 64 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 d .** size of th
19900 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 70 61 e database in pa
19910 67 65 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e ges following an
19920 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 70 65 auto-vacuum ope
19930 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 ration..*/.stati
19940 63 20 50 67 6e 6f 20 66 69 6e 61 6c 44 62 53 69 c Pgno finalDbSi
19950 7a 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ze(BtShared *pBt
19960 2c 20 50 67 6e 6f 20 6e 4f 72 69 67 2c 20 50 67 , Pgno nOrig, Pg
19970 6e 6f 20 6e 46 72 65 65 29 7b 0a 20 20 69 6e 74 no nFree){. int
19980 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 nEntry;
19990 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
199a0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
199b0 73 20 6f 6e 20 6f 6e 65 20 70 74 72 6d 61 70 20 s on one ptrmap
199c0 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e page */. Pgno n
199d0 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 20 20 Ptrmap;
199e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
199f0 62 65 72 20 6f 66 20 50 74 72 4d 61 70 20 70 61 ber of PtrMap pa
19a00 67 65 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 ges to be freed
19a10 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 20 */. Pgno nFin;
19a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a30 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 /* Return v
19a40 61 6c 75 65 20 2a 2f 0a 0a 20 20 6e 45 6e 74 72 alue */.. nEntr
19a50 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 y = pBt->usableS
19a60 69 7a 65 2f 35 3b 0a 20 20 6e 50 74 72 6d 61 70 ize/5;. nPtrmap
19a70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b = (nFree-nOrig+
19a80 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
19a90 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e 74 72 79 t, nOrig)+nEntry
19aa0 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 6e 46 69 6e )/nEntry;. nFin
19ab0 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 72 65 65 = nOrig - nFree
19ac0 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20 20 69 66 - nPtrmap;. if
19ad0 28 20 6e 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f ( nOrig>PENDING_
19ae0 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 26 BYTE_PAGE(pBt) &
19af0 26 20 6e 46 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 & nFin<PENDING_B
19b00 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
19b10 0a 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 7d . nFin--;. }
19b20 0a 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 . while( PTRMAP
19b30 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 46 69 _ISPAGE(pBt, nFi
19b40 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 n) || nFin==PEND
19b50 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
19b60 74 29 20 29 7b 0a 20 20 20 20 6e 46 69 6e 2d 2d t) ){. nFin--
19b70 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
19b80 6e 46 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 nFin;.}../*.** A
19b90 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
19ba0 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 on must be opene
19bb0 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 d before calling
19bc0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
19bd0 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 ** It performs a
19be0 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 single unit of
19bf0 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 work towards an
19c00 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
19c10 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 um..**.** If the
19c20 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
19c30 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 uum is finished
19c40 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
19c50 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 ion has run,.**
19c60 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 SQLITE_DONE is r
19c70 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 eturned. If it i
19c80 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 s not finished,
19c90 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 but no error occ
19ca0 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 urred,.** SQLITE
19cb0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
19cc0 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 Otherwise an SQ
19cd0 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e Lite error code.
19ce0 20 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 .*/.int sqlite3
19cf0 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 BtreeIncrVacuum(
19d00 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
19d10 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 rc;. BtShared
19d20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a *pBt = p->pBt;..
19d30 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
19d40 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
19d50 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ( pBt->inTransac
19d60 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
19d70 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d E && p->inTrans=
19d80 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
19d90 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 6f if( !pBt->auto
19da0 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 Vacuum ){. rc
19db0 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a = SQLITE_DONE;.
19dc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e }else{. Pgn
19dd0 6f 20 6e 4f 72 69 67 20 3d 20 62 74 72 65 65 50 o nOrig = btreeP
19de0 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
19df0 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 20 3d 20 Pgno nFree =
19e00 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 get4byte(&pBt->p
19e10 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
19e20 29 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e );. Pgno nFin
19e30 20 3d 20 66 69 6e 61 6c 44 62 53 69 7a 65 28 70 = finalDbSize(p
19e40 42 74 2c 20 6e 4f 72 69 67 2c 20 6e 46 72 65 65 Bt, nOrig, nFree
19e50 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 );.. if( nOri
19e60 67 3c 6e 46 69 6e 20 29 7b 0a 20 20 20 20 20 20 g<nFin ){.
19e70 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
19e80 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 65 UPT_BKPT;. }e
19e90 6c 73 65 20 69 66 28 20 6e 46 72 65 65 3e 30 20 lse if( nFree>0
19ea0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 61 ){. rc = sa
19eb0 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 veAllCursors(pBt
19ec0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 , 0, 0);. i
19ed0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
19ee0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 ){. inva
19ef0 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f lidateAllOverflo
19f00 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 wCache(pBt);.
19f10 20 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 rc = incrVa
19f20 63 75 75 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 cuumStep(pBt, nF
19f30 69 6e 2c 20 6e 4f 72 69 67 2c 20 30 29 3b 0a 20 in, nOrig, 0);.
19f40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
19f50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
19f60 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
19f70 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
19f80 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
19f90 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
19fa0 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 put4byte(&pBt->p
19fb0 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d Page1->aData[28]
19fc0 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 , pBt->nPage);.
19fd0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
19fe0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
19ff0 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a ITE_DONE;. }.
1a000 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1a010 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1a020 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1a030 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1a040 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 s called prior t
1a050 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f o sqlite3PagerCo
1a060 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e mmit when a tran
1a070 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f saction.** is co
1a080 6d 6d 69 74 74 65 64 20 66 6f 72 20 61 6e 20 61 mmitted for an a
1a090 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
1a0a0 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 ase..**.** If SQ
1a0b0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
1a0c0 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 ned, then *pnTru
1a0d0 6e 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 nc is set to the
1a0e0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1a0f0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1a100 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
1a110 74 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 truncated to dur
1a120 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 ing the commit p
1a130 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e rocess. .** i.e.
1a140 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1a150 73 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a s been reorganiz
1a160 65 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 ed so that only
1a170 74 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 the first *pnTru
1a180 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 nc.** pages are
1a190 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 in use..*/.stati
1a1a0 63 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d c int autoVacuum
1a1b0 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 Commit(BtShared
1a1c0 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 *pBt){. int rc
1a1d0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 = SQLITE_OK;. P
1a1e0 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
1a1f0 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 Bt->pPager;. VV
1a200 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 A_ONLY( int nRef
1a210 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 = sqlite3PagerR
1a220 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 efcount(pPager)
1a230 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 );.. assert( sq
1a240 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1a250 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1a260 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f invalidateAllO
1a270 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 verflowCache(pBt
1a280 29 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d );. assert(pBt-
1a290 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 >autoVacuum);.
1a2a0 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 if( !pBt->incrVa
1a2b0 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f cuum ){. Pgno
1a2c0 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f nFin; /
1a2d0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
1a2e0 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 s in database af
1a2f0 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e ter autovacuumin
1a300 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 g */. Pgno nF
1a310 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ree; /* N
1a320 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
1a330 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 n the freelist i
1a340 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 nitially */.
1a350 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 Pgno iFree;
1a360 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 /* The next p
1a370 61 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 age to be freed
1a380 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 */. Pgno nOri
1a390 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 g; /* Dat
1a3a0 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 6f 72 abase size befor
1a3b0 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 20 e freeing */..
1a3c0 20 20 6e 4f 72 69 67 20 3d 20 62 74 72 65 65 50 nOrig = btreeP
1a3d0 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
1a3e0 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53 if( PTRMAP_IS
1a3f0 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 PAGE(pBt, nOrig)
1a400 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 || nOrig==PENDI
1a410 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1a420 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 ) ){. /* It
1a430 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1a440 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 61 74 to create a dat
1a450 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 68 20 abase for which
1a460 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 0a 20 the final page.
1a470 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 68 65 ** is eithe
1a480 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 r a pointer-map
1a490 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 6e 64 page or the pend
1a4a0 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e 20 49 ing-byte page. I
1a4b0 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 f one. ** i
1a4c0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 s encountered, t
1a4d0 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 63 6f his indicates co
1a4e0 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 rruption..
1a4f0 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
1a500 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1a510 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 KPT;. }..
1a520 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 nFree = get4byte
1a530 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
1a540 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e Data[36]);. n
1a550 46 69 6e 20 3d 20 66 69 6e 61 6c 44 62 53 69 7a Fin = finalDbSiz
1a560 65 28 70 42 74 2c 20 6e 4f 72 69 67 2c 20 6e 46 e(pBt, nOrig, nF
1a570 72 65 65 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 ree);. if( nF
1a580 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 72 in>nOrig ) retur
1a590 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1a5a0 5f 42 4b 50 54 3b 0a 20 20 20 20 69 66 28 20 6e _BKPT;. if( n
1a5b0 46 69 6e 3c 6e 4f 72 69 67 20 29 7b 0a 20 20 20 Fin<nOrig ){.
1a5c0 20 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 rc = saveAllC
1a5d0 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 ursors(pBt, 0, 0
1a5e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
1a5f0 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 (iFree=nOrig; iF
1a600 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d ree>nFin && rc==
1a610 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 SQLITE_OK; iFree
1a620 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 --){. rc =
1a630 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 incrVacuumStep(p
1a640 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 2c Bt, nFin, iFree,
1a650 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 1);. }. i
1a660 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 f( (rc==SQLITE_D
1a670 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 ONE || rc==SQLIT
1a680 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e 30 E_OK) && nFree>0
1a690 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1a6a0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1a6b0 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
1a6c0 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 75 bPage);. pu
1a6d0 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
1a6e0 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 ge1->aData[32],
1a6f0 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 0);. put4by
1a700 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
1a710 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b 0a >aData[36], 0);.
1a720 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
1a730 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
1a740 74 61 5b 32 38 5d 2c 20 6e 46 69 6e 29 3b 0a 20 ta[28], nFin);.
1a750 20 20 20 20 20 70 42 74 2d 3e 62 44 6f 54 72 75 pBt->bDoTru
1a760 6e 63 61 74 65 20 3d 20 31 3b 0a 20 20 20 20 20 ncate = 1;.
1a770 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e 46 pBt->nPage = nF
1a780 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 in;. }. if
1a790 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1a7a0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1a7b0 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 PagerRollback(pP
1a7c0 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
1a7d0 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 66 .. assert( nRef
1a7e0 3e 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 >=sqlite3PagerRe
1a7f0 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 fcount(pPager) )
1a800 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1a810 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 6e 64 65 ..#else /* ifnde
1a820 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1a830 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 20 64 65 TOVACUUM */.# de
1a840 66 69 6e 65 20 73 65 74 43 68 69 6c 64 50 74 72 fine setChildPtr
1a850 6d 61 70 73 28 78 29 20 53 51 4c 49 54 45 5f 4f maps(x) SQLITE_O
1a860 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 K.#endif../*.**
1a870 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
1a880 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61 73 s the first phas
1a890 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 73 65 e of a two-phase
1a8a0 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 20 72 commit. This r
1a8b0 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73 65 73 outine.** causes
1a8c0 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
1a8d0 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61 74 65 nal to be create
1a8e0 64 20 28 69 66 20 69 74 20 64 6f 65 73 20 6e 6f d (if it does no
1a8f0 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 29 t already exist)
1a900 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 .** and populate
1a910 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20 69 6e d with enough in
1a920 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74 68 61 formation so tha
1a930 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c 6f 73 t if a power los
1a940 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65 20 s occurs.** the
1a950 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 20 database can be
1a960 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 restored to its
1a970 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 62 original state b
1a980 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 0a 2a y playing back.*
1a990 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 * the journal.
1a9a0 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 Then the content
1a9b0 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s of the journal
1a9c0 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f 75 74 are flushed out
1a9d0 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73 6b 2e to.** the disk.
1a9e0 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 After the jour
1a9f0 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20 6f 6e nal is safely on
1aa00 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68 61 6e oxide, the chan
1aa10 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 ges to the.** da
1aa20 74 61 62 61 73 65 20 61 72 65 20 77 72 69 74 74 tabase are writt
1aa30 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 en into the data
1aa40 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 66 6c base file and fl
1aa50 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65 2e 0a ushed to oxide..
1aa60 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f 66 ** At the end of
1aa70 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68 65 20 this call, the
1aa80 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
1aa90 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 6f 6e still exists on
1aaa0 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 the.** disk and
1aab0 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20 68 6f we are still ho
1aac0 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c lding all locks,
1aad0 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 so the transact
1aae0 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 ion has not.** c
1aaf0 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65 20 73 ommitted. See s
1ab00 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1ab10 74 50 68 61 73 65 54 77 6f 28 29 20 66 6f 72 20 tPhaseTwo() for
1ab20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 65 the second phase
1ab30 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 of the.** commi
1ab40 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a t process..**.**
1ab50 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 This call is a
1ab60 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 72 69 74 no-op if no writ
1ab70 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e-transaction is
1ab80 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 currently activ
1ab90 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 e on pBt..**.**
1aba0 4f 74 68 65 72 77 69 73 65 2c 20 73 79 6e 63 20 Otherwise, sync
1abb0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1abc0 65 20 66 6f 72 20 74 68 65 20 62 74 72 65 65 20 e for the btree
1abd0 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 pBt. zMaster poi
1abe0 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 nts to.** the na
1abf0 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a me of a master j
1ac00 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 ournal file that
1ac10 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 should be writt
1ac20 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 en into the.** i
1ac30 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 ndividual journa
1ac40 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 20 4e 55 l file, or is NU
1ac50 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 6e LL, indicating n
1ac60 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c o master journal
1ac70 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 6e 67 6c file .** (singl
1ac80 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 e database trans
1ac90 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 action)..**.** W
1aca0 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c hen this is call
1acb0 65 64 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a ed, the master j
1acc0 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 20 61 6c ournal should al
1acd0 72 65 61 64 79 20 68 61 76 65 20 62 65 65 6e 0a ready have been.
1ace0 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 6f 70 75 ** created, popu
1acf0 6c 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 lated with this
1ad00 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 journal pointer
1ad10 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 64 69 and synced to di
1ad20 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 sk..**.** Once t
1ad30 68 69 73 20 69 73 20 72 6f 75 74 69 6e 65 20 68 his is routine h
1ad40 61 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 as returned, the
1ad50 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 65 71 75 only thing requ
1ad60 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a ired to commit.*
1ad70 2a 20 74 68 65 20 77 72 69 74 65 2d 74 72 61 6e * the write-tran
1ad80 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 68 69 73 saction for this
1ad90 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1ada0 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 s to delete the
1adb0 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 69 6e 74 20 journal..*/.int
1adc0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1add0 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65 itPhaseOne(Btree
1ade0 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
1adf0 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 *zMaster){. int
1ae00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1ae10 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
1ae20 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
1ae30 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
1ae40 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1ae50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
1ae60 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 ter(p);.#ifndef
1ae70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1ae80 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
1ae90 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
1aea0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 75 74 {. rc = aut
1aeb0 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 70 42 oVacuumCommit(pB
1aec0 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 t);. if( rc
1aed0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1aee0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
1aef0 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
1af00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1af10 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1af20 20 20 69 66 28 20 70 42 74 2d 3e 62 44 6f 54 72 if( pBt->bDoTr
1af30 75 6e 63 61 74 65 20 29 7b 0a 20 20 20 20 20 20 uncate ){.
1af40 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
1af50 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 cateImage(pBt->p
1af60 50 61 67 65 72 2c 20 70 42 74 2d 3e 6e 50 61 67 Pager, pBt->nPag
1af70 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
1af80 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1af90 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
1afa0 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 72 eOne(pBt->pPager
1afb0 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 , zMaster, 0);.
1afc0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1afd0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 eave(p);. }. r
1afe0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1aff0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1b000 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 is called from
1b010 62 6f 74 68 20 42 74 72 65 65 43 6f 6d 6d 69 74 both BtreeCommit
1b020 50 68 61 73 65 54 77 6f 28 29 20 61 6e 64 20 42 PhaseTwo() and B
1b030 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a treeRollback().*
1b040 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 * at the conclus
1b050 69 6f 6e 20 6f 66 20 61 20 74 72 61 6e 73 61 63 ion of a transac
1b060 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
1b070 76 6f 69 64 20 62 74 72 65 65 45 6e 64 54 72 61 void btreeEndTra
1b080 6e 73 61 63 74 69 6f 6e 28 42 74 72 65 65 20 2a nsaction(Btree *
1b090 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a p){. BtShared *
1b0a0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1b0b0 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1b0c0 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 >db;. assert( s
1b0d0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1b0e0 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 23 69 66 Mutex(p) );..#if
1b0f0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b100 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 70 42 _AUTOVACUUM. pB
1b110 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 20 3d t->bDoTruncate =
1b120 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 0;.#endif. if(
1b130 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e p->inTrans>TRAN
1b140 53 5f 4e 4f 4e 45 20 26 26 20 64 62 2d 3e 6e 56 S_NONE && db->nV
1b150 64 62 65 52 65 61 64 3e 31 20 29 7b 0a 20 20 20 dbeRead>1 ){.
1b160 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
1b170 20 6f 74 68 65 72 20 61 63 74 69 76 65 20 73 74 other active st
1b180 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 62 65 atements that be
1b190 6c 6f 6e 67 20 74 6f 20 74 68 69 73 20 64 61 74 long to this dat
1b1a0 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 68 61 6e abase. ** han
1b1b0 64 6c 65 2c 20 64 6f 77 6e 67 72 61 64 65 20 74 dle, downgrade t
1b1c0 6f 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 o a read-only tr
1b1d0 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 6f ansaction. The o
1b1e0 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 0a ther statements.
1b1f0 20 20 20 20 2a 2a 20 6d 61 79 20 73 74 69 6c 6c ** may still
1b200 20 62 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d be reading from
1b210 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
1b220 2a 2f 0a 20 20 20 20 64 6f 77 6e 67 72 61 64 65 */. downgrade
1b230 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
1b240 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 bleLocks(p);.
1b250 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 p->inTrans = TR
1b260 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 ANS_READ;. }els
1b270 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 e{. /* If the
1b280 20 68 61 6e 64 6c 65 20 68 61 64 20 61 6e 79 20 handle had any
1b290 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 61 63 74 kind of transact
1b2a0 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 72 65 6d ion open, decrem
1b2b0 65 6e 74 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 ent the . **
1b2c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e transaction coun
1b2d0 74 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 t of the shared
1b2e0 62 74 72 65 65 2e 20 49 66 20 74 68 65 20 74 72 btree. If the tr
1b2f0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 ansaction count
1b300 0a 20 20 20 20 2a 2a 20 72 65 61 63 68 65 73 20 . ** reaches
1b310 30 2c 20 73 65 74 20 74 68 65 20 73 68 61 72 65 0, set the share
1b320 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 d state to TRANS
1b330 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f 63 _NONE. The unloc
1b340 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 29 kBtreeIfUnused()
1b350 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 65 6c . ** call bel
1b360 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 ow will unlock t
1b370 68 65 20 70 61 67 65 72 2e 20 20 2a 2f 0a 20 20 he pager. */.
1b380 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
1b390 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a !=TRANS_NONE ){.
1b3a0 20 20 20 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 clearAllSh
1b3b0 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
1b3c0 63 6b 73 28 70 29 3b 0a 20 20 20 20 20 20 70 42 cks(p);. pB
1b3d0 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d t->nTransaction-
1b3e0 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d -;. if( 0==
1b3f0 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
1b400 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 n ){. pBt
1b410 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
1b420 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 = TRANS_NONE;.
1b430 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1b440 20 2f 2a 20 53 65 74 20 74 68 65 20 63 75 72 72 /* Set the curr
1b450 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1b460 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e state to TRANS_N
1b470 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 ONE and unlock t
1b480 68 65 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 he . ** pager
1b490 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c if this call cl
1b4a0 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 osed the only re
1b4b0 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e ad or write tran
1b4c0 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 saction. */.
1b4d0 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 p->inTrans = TR
1b4e0 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 75 6e ANS_NONE;. un
1b4f0 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 lockBtreeIfUnuse
1b500 64 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 d(pBt);. }.. b
1b510 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
1b520 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 ;.}../*.** Commi
1b530 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
1b540 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 n currently in p
1b550 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 rogress..**.** T
1b560 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c his routine impl
1b570 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f 6e ements the secon
1b580 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d 70 d phase of a 2-p
1b590 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 hase commit. Th
1b5a0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 e.** sqlite3Btre
1b5b0 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
1b5c0 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 ) routine does t
1b5d0 68 65 20 66 69 72 73 74 20 70 68 61 73 65 20 61 he first phase a
1b5e0 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 nd should.** be
1b5f0 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f invoked prior to
1b600 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f calling this ro
1b610 75 74 69 6e 65 2e 20 20 54 68 65 20 73 71 6c 69 utine. The sqli
1b620 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1b630 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 aseOne().** rout
1b640 69 6e 65 20 64 69 64 20 61 6c 6c 20 74 68 65 20 ine did all the
1b650 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 work of writing
1b660 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 information out
1b670 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 to disk and flus
1b680 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 hing the.** cont
1b690 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 ents so that the
1b6a0 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e y are written on
1b6b0 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 to the disk plat
1b6c0 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a ter. All this.*
1b6d0 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f * routine has to
1b6e0 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 do is delete or
1b6f0 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 truncate or zer
1b700 6f 20 74 68 65 20 68 65 61 64 65 72 20 69 6e 20 o the header in
1b710 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 the.** the rollb
1b720 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 ack journal (whi
1b730 63 68 20 63 61 75 73 65 73 20 74 68 65 20 74 72 ch causes the tr
1b740 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d ansaction to com
1b750 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 mit) and.** drop
1b760 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f locks..**.** No
1b770 72 6d 61 6c 6c 79 2c 20 69 66 20 61 6e 20 65 72 rmally, if an er
1b780 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
1b790 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
1b7a0 20 69 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 is attempting t
1b7b0 6f 20 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 20 74 o .** finalize t
1b7c0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6a 6f he underlying jo
1b7d0 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 69 73 urnal file, this
1b7e0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1b7f0 73 20 61 6e 20 65 72 72 6f 72 20 61 6e 64 0a 2a s an error and.*
1b800 2a 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 * the upper laye
1b810 72 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 61 r will attempt a
1b820 20 72 6f 6c 6c 62 61 63 6b 2e 20 48 6f 77 65 76 rollback. Howev
1b830 65 72 2c 20 69 66 20 74 68 65 20 73 65 63 6f 6e er, if the secon
1b840 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 d argument.** is
1b850 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 non-zero then t
1b860 68 69 73 20 62 2d 74 72 65 65 20 74 72 61 6e 73 his b-tree trans
1b870 61 63 74 69 6f 6e 20 69 73 20 70 61 72 74 20 6f action is part o
1b880 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 0a f a multi-file .
1b890 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
1b8a0 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 In this case, th
1b8b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 e transaction ha
1b8c0 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 s already been c
1b8d0 6f 6d 6d 69 74 74 65 64 20 0a 2a 2a 20 28 62 79 ommitted .** (by
1b8e0 20 64 65 6c 65 74 69 6e 67 20 61 20 6d 61 73 74 deleting a mast
1b8f0 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 er journal file)
1b900 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 65 72 20 and the caller
1b910 77 69 6c 6c 20 69 67 6e 6f 72 65 20 74 68 69 73 will ignore this
1b920 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 72 .** functions r
1b930 65 74 75 72 6e 20 63 6f 64 65 2e 20 53 6f 2c 20 eturn code. So,
1b940 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 even if an error
1b950 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20 70 occurs in the p
1b960 61 67 65 72 20 6c 61 79 65 72 2c 0a 2a 2a 20 72 ager layer,.** r
1b970 65 73 65 74 20 74 68 65 20 62 2d 74 72 65 65 20 eset the b-tree
1b980 6f 62 6a 65 63 74 73 20 69 6e 74 65 72 6e 61 6c objects internal
1b990 20 73 74 61 74 65 20 74 6f 20 69 6e 64 69 63 61 state to indica
1b9a0 74 65 20 74 68 61 74 20 74 68 65 20 77 72 69 74 te that the writ
1b9b0 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1b9c0 20 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 has been closed
1b9d0 2e 20 54 68 69 73 20 69 73 20 71 75 69 74 65 20 . This is quite
1b9e0 73 61 66 65 2c 20 61 73 20 74 68 65 20 70 61 67 safe, as the pag
1b9f0 65 72 20 77 69 6c 6c 20 68 61 76 65 0a 2a 2a 20 er will have.**
1ba00 74 72 61 6e 73 69 74 69 6f 6e 65 64 20 74 6f 20 transitioned to
1ba10 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e the error state.
1ba20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c .**.** This will
1ba30 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 release the wri
1ba40 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 te lock on the d
1ba50 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 atabase file. I
1ba60 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e f there.** are n
1ba70 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 o active cursors
1ba80 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 , it also releas
1ba90 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b es the read lock
1baa0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1bab0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1bac0 54 77 6f 28 42 74 72 65 65 20 2a 70 2c 20 69 6e Two(Btree *p, in
1bad0 74 20 62 43 6c 65 61 6e 75 70 29 7b 0a 0a 20 20 t bCleanup){..
1bae0 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d if( p->inTrans==
1baf0 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 20 72 65 74 TRANS_NONE ) ret
1bb00 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1bb10 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1bb20 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e er(p);. btreeIn
1bb30 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f tegrity(p);.. /
1bb40 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 * If the handle
1bb50 68 61 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e has a write-tran
1bb60 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f saction open, co
1bb70 6d 6d 69 74 20 74 68 65 20 73 68 61 72 65 64 2d mmit the shared-
1bb80 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 btrees . ** tra
1bb90 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 nsaction and set
1bba0 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 the shared stat
1bbb0 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e e to TRANS_READ.
1bbc0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 . */. if( p->i
1bbd0 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1bbe0 49 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ITE ){. int r
1bbf0 63 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 c;. BtShared
1bc00 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
1bc10 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
1bc20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
1bc30 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
1bc40 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e assert( pBt->n
1bc50 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b Transaction>0 );
1bc60 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1bc70 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
1bc80 65 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 eTwo(pBt->pPager
1bc90 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1bca0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 62 43 6c 65 QLITE_OK && bCle
1bcb0 61 6e 75 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 anup==0 ){.
1bcc0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1bcd0 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 ve(p);. ret
1bce0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1bcf0 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
1bd00 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 tion = TRANS_REA
1bd10 44 3b 0a 20 20 20 20 62 74 72 65 65 43 6c 65 61 D;. btreeClea
1bd20 72 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 rHasContent(pBt)
1bd30 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e ;. }.. btreeEn
1bd40 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b dTransaction(p);
1bd50 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
1bd60 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
1bd70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1bd80 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 /*.** Do both ph
1bd90 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 ases of a commit
1bda0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1bdb0 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 BtreeCommit(Btre
1bdc0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
1bdd0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1bde0 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
1bdf0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1be00 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 itPhaseOne(p, 0)
1be10 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1be20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
1be30 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
1be40 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2c 20 mmitPhaseTwo(p,
1be50 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0);. }. sqlite
1be60 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1be70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1be80 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
1be90 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 61 74 ne sets the stat
1bea0 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c e to CURSOR_FAUL
1beb0 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a T and the error.
1bec0 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f ** code to errCo
1bed0 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 de for every cur
1bee0 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 64 20 sor on BtShared
1bef0 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 that pBtree.** r
1bf00 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a eferences..**.**
1bf10 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 69 73 Every cursor is
1bf20 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 tripped, includ
1bf30 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 61 74 ing cursors that
1bf40 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 belong.** to ot
1bf50 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e her database con
1bf60 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 nections that ha
1bf70 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 72 69 ppen to be shari
1bf80 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 ng.** the cache
1bf90 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a with pBtree..**.
1bfa0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1bfb0 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e gets called when
1bfc0 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 a rollback occu
1bfd0 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f rs..** All curso
1bfe0 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d rs using the sam
1bff0 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 e cache must be
1c000 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 tripped.** to pr
1c010 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 event them from
1c020 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 74 68 trying to use th
1c030 65 20 62 74 72 65 65 20 61 66 74 65 72 0a 2a 2a e btree after.**
1c040 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 the rollback.
1c050 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 The rollback may
1c060 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 74 61 have deleted ta
1c070 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 bles.** or moved
1c080 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 root pages, so
1c090 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 it is not suffic
1c0a0 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 ient to.** save
1c0b0 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
1c0c0 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 cursor. The cu
1c0d0 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 rsor must be.**
1c0e0 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a invalidated..*/.
1c0f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
1c100 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 eTripAllCursors(
1c110 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 Btree *pBtree, i
1c120 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 nt errCode){. B
1c130 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 69 66 tCursor *p;. if
1c140 28 20 70 42 74 72 65 65 3d 3d 30 20 29 20 72 65 ( pBtree==0 ) re
1c150 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 turn;. sqlite3B
1c160 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 treeEnter(pBtree
1c170 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 );. for(p=pBtre
1c180 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b e->pBt->pCursor;
1c190 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
1c1a0 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1c1b0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
1c1c0 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 20 20 rCursor(p);.
1c1d0 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 p->eState = CURS
1c1e0 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d OR_FAULT;. p-
1c1f0 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 72 72 43 >skipNext = errC
1c200 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ode;. for(i=0
1c210 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 ; i<=p->iPage; i
1c220 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
1c230 73 65 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65 sePage(p->apPage
1c240 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 [i]);. p->a
1c250 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 pPage[i] = 0;.
1c260 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
1c270 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 3BtreeLeave(pBtr
1c280 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f ee);.}../*.** Ro
1c290 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 llback the trans
1c2a0 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 action in progre
1c2b0 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 ss. All cursors
1c2c0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 will be.** inva
1c2d0 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 lided by this op
1c2e0 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 eration. Any at
1c2f0 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63 tempt to use a c
1c300 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 ursor.** that wa
1c310 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65 s open at the be
1c320 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 ginning of this
1c330 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 operation will r
1c340 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 esult.** in an e
1c350 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rror..**.** This
1c360 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 will release th
1c370 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 e write lock on
1c380 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1c390 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 e. If there.**
1c3a0 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 are no active cu
1c3b0 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 rsors, it also r
1c3c0 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64 eleases the read
1c3d0 20 6c 6f 63 6b 2e 0a 2a 2f 0a 69 6e 74 20 73 71 lock..*/.int sq
1c3e0 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
1c3f0 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ck(Btree *p, int
1c400 20 74 72 69 70 43 6f 64 65 29 7b 0a 20 20 69 6e tripCode){. in
1c410 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
1c420 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1c430 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
1c440 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 1;.. sqlite3Btr
1c450 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
1c460 28 20 74 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 ( tripCode==SQLI
1c470 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
1c480 3d 20 74 72 69 70 43 6f 64 65 20 3d 20 73 61 76 = tripCode = sav
1c490 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
1c4a0 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0, 0);. }else{
1c4b0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1c4c0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 _OK;. }. if( t
1c4d0 72 69 70 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 ripCode ){. s
1c4e0 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
1c4f0 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 74 72 69 llCursors(p, tri
1c500 70 43 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 62 74 pCode);. }. bt
1c510 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b reeIntegrity(p);
1c520 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 .. if( p->inTra
1c530 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
1c540 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a ){. int rc2;.
1c550 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41 . assert( TRA
1c560 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 NS_WRITE==pBt->i
1c570 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a nTransaction );.
1c580 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 rc2 = sqlite
1c590 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
1c5a0 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
1c5b0 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 if( rc2!=SQLITE
1c5c0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
1c5d0 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 = rc2;. }..
1c5e0 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 /* The rollbac
1c5f0 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 k may have destr
1c600 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 2d oyed the pPage1-
1c610 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 >aData value. S
1c620 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 o. ** call bt
1c630 72 65 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 reeGetPage() on
1c640 70 61 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 page 1 again to
1c650 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 make. ** sure
1c660 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 pPage1->aData i
1c670 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e s set correctly.
1c680 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 */. if( btre
1c690 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c eGetPage(pBt, 1,
1c6a0 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 &pPage1, 0)==SQ
1c6b0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1c6c0 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 67 65 74 int nPage = get
1c6d0 34 62 79 74 65 28 32 38 2b 28 75 38 2a 29 70 50 4byte(28+(u8*)pP
1c6e0 61 67 65 31 2d 3e 61 44 61 74 61 29 3b 0a 20 20 age1->aData);.
1c6f0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 testcase( nP
1c700 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 age==0 );.
1c710 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 20 73 if( nPage==0 ) s
1c720 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
1c730 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
1c740 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 , &nPage);.
1c750 20 74 65 73 74 63 61 73 65 28 20 70 42 74 2d 3e testcase( pBt->
1c760 6e 50 61 67 65 21 3d 6e 50 61 67 65 20 29 3b 0a nPage!=nPage );.
1c770 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 pBt->nPage
1c780 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 = nPage;.
1c790 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
1c7a0 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 e1);. }. a
1c7b0 73 73 65 72 74 28 20 63 6f 75 6e 74 56 61 6c 69 ssert( countVali
1c7c0 64 43 75 72 73 6f 72 73 28 70 42 74 2c 20 31 29 dCursors(pBt, 1)
1c7d0 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e ==0 );. pBt->
1c7e0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
1c7f0 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20 TRANS_READ;.
1c800 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e btreeClearHasCon
1c810 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 7d 0a 0a tent(pBt);. }..
1c820 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 btreeEndTransa
1c830 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 ction(p);. sqli
1c840 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
1c850 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1c860 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 ../*.** Start a
1c870 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 statement subtra
1c880 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 nsaction. The su
1c890 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e btransaction can
1c8a0 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a can be rolled.*
1c8b0 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 * back independe
1c8c0 6e 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e ntly of the main
1c8d0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f transaction. Yo
1c8e0 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74 u must start a t
1c8f0 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 ransaction .** b
1c900 65 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 efore starting a
1c910 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e subtransaction.
1c920 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 The subtransact
1c930 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 ion is ended aut
1c940 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 omatically .** i
1c950 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 f the main trans
1c960 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f action commits o
1c970 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a r rolls back..**
1c980 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 .** Statement su
1c990 62 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 btransactions ar
1c9a0 65 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 6e e used around in
1c9b0 64 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 61 dividual SQL sta
1c9c0 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 tements.** that
1c9d0 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 are contained wi
1c9e0 74 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 thin a BEGIN...C
1c9f0 4f 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 OMMIT block. If
1ca00 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a a constraint.**
1ca10 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 error occurs wi
1ca20 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 thin the stateme
1ca30 6e 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f nt, the effect o
1ca40 66 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 f that one state
1ca50 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 ment.** can be r
1ca60 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f olled back witho
1ca70 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c ut having to rol
1ca80 6c 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65 lback the entire
1ca90 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
1caa0 0a 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 20 .** A statement
1cab0 73 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 sub-transaction
1cac0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 is implemented a
1cad0 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 s an anonymous s
1cae0 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a avepoint. The.**
1caf0 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 value passed as
1cb00 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
1cb10 6d 65 74 65 72 20 69 73 20 74 68 65 20 74 6f 74 meter is the tot
1cb20 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 al number of sav
1cb30 65 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c epoints,.** incl
1cb40 75 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 6e uding the new an
1cb50 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e onymous savepoin
1cb60 74 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 t, open on the B
1cb70 2d 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 -Tree. i.e. if t
1cb80 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 here.** are no a
1cb90 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
1cba0 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 and no other st
1cbb0 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 atement-transact
1cbc0 69 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 ions open,.** iS
1cbd0 74 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 tatement is 1. T
1cbe0 68 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 his anonymous sa
1cbf0 76 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 vepoint can be r
1cc00 65 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 eleased or rolle
1cc10 64 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 d back.** using
1cc20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 the sqlite3Btree
1cc30 53 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 Savepoint() func
1cc40 74 69 6f 6e 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c tion..*/.int sql
1cc50 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 ite3BtreeBeginSt
1cc60 6d 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 mt(Btree *p, int
1cc70 20 69 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 iStatement){.
1cc80 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
1cc90 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1cca0 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1ccb0 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 Enter(p);. asse
1ccc0 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
1ccd0 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
1cce0 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 62 assert( (pBt->b
1ccf0 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 tsFlags & BTS_RE
1cd00 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0a 20 AD_ONLY)==0 );.
1cd10 20 61 73 73 65 72 74 28 20 69 53 74 61 74 65 6d assert( iStatem
1cd20 65 6e 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 ent>0 );. asser
1cd30 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d t( iStatement>p-
1cd40 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 >db->nSavepoint
1cd50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
1cd60 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
1cd70 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
1cd80 20 20 2f 2a 20 41 74 20 74 68 65 20 70 61 67 65 /* At the page
1cd90 72 20 6c 65 76 65 6c 2c 20 61 20 73 74 61 74 65 r level, a state
1cda0 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1cdb0 20 69 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 is a savepoint
1cdc0 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 with. ** an ind
1cdd0 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ex greater than
1cde0 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 all savepoints c
1cdf0 72 65 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c reated explicitl
1ce00 79 20 75 73 69 6e 67 0a 20 20 2a 2a 20 53 51 4c y using. ** SQL
1ce10 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 statements. It
1ce20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 is illegal to op
1ce30 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 en, release or r
1ce40 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 2a 2a ollback any. **
1ce50 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 such savepoints
1ce60 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 while the state
1ce70 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1ce80 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 savepoint is ac
1ce90 74 69 76 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 tive.. */. rc
1cea0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 = sqlite3PagerOp
1ceb0 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d enSavepoint(pBt-
1cec0 3e 70 50 61 67 65 72 2c 20 69 53 74 61 74 65 6d >pPager, iStatem
1ced0 65 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ent);. sqlite3B
1cee0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1cef0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1cf00 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 .** The second a
1cf10 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
1cf20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 function, op, is
1cf30 20 61 6c 77 61 79 73 20 53 41 56 45 50 4f 49 4e always SAVEPOIN
1cf40 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 T_ROLLBACK.** or
1cf50 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
1cf60 53 45 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f SE. This functio
1cf70 6e 20 65 69 74 68 65 72 20 72 65 6c 65 61 73 65 n either release
1cf80 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b 20 s or rolls back
1cf90 74 68 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 the.** savepoint
1cfa0 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 70 identified by p
1cfb0 61 72 61 6d 65 74 65 72 20 69 53 61 76 65 70 6f arameter iSavepo
1cfc0 69 6e 74 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f int, depending o
1cfd0 6e 20 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a 20 n the value .**
1cfe0 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 of op..**.** Nor
1cff0 6d 61 6c 6c 79 2c 20 69 53 61 76 65 70 6f 69 6e mally, iSavepoin
1d000 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 t is greater tha
1d010 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 n or equal to ze
1d020 72 6f 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 ro. However, if
1d030 6f 70 20 69 73 0a 2a 2a 20 53 41 56 45 50 4f 49 op is.** SAVEPOI
1d040 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 NT_ROLLBACK, the
1d050 6e 20 69 53 61 76 65 70 6f 69 6e 74 20 6d 61 79 n iSavepoint may
1d060 20 61 6c 73 6f 20 62 65 20 2d 31 2e 20 49 6e 20 also be -1. In
1d070 74 68 69 73 20 63 61 73 65 20 74 68 65 20 0a 2a this case the .*
1d080 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 * contents of th
1d090 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 e entire transac
1d0a0 74 69 6f 6e 20 61 72 65 20 72 6f 6c 6c 65 64 20 tion are rolled
1d0b0 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 back. This is di
1d0c0 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 fferent.** from
1d0d0 61 20 6e 6f 72 6d 61 6c 20 74 72 61 6e 73 61 63 a normal transac
1d0e0 74 69 6f 6e 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 tion rollback, a
1d0f0 73 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 72 s no locks are r
1d100 65 6c 65 61 73 65 64 20 61 6e 64 20 74 68 65 0a eleased and the.
1d110 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 ** transaction r
1d120 65 6d 61 69 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a emains open..*/.
1d130 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1d140 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 Savepoint(Btree
1d150 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 *p, int op, int
1d160 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 iSavepoint){. i
1d170 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1d180 4b 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d K;. if( p && p-
1d190 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
1d1a0 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 WRITE ){. BtS
1d1b0 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1d1c0 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pBt;. assert(
1d1d0 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
1d1e0 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 ELEASE || op==SA
1d1f0 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
1d200 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1d210 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c iSavepoint>=0 ||
1d220 20 28 69 53 61 76 65 70 6f 69 6e 74 3d 3d 2d 31 (iSavepoint==-1
1d230 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e && op==SAVEPOIN
1d240 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 3b 0a 20 T_ROLLBACK) );.
1d250 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 sqlite3BtreeE
1d260 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20 nter(p);. rc
1d270 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 = sqlite3PagerSa
1d280 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 vepoint(pBt->pPa
1d290 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f ger, op, iSavepo
1d2a0 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 int);. if( rc
1d2b0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1d2c0 20 20 20 20 20 69 66 28 20 69 53 61 76 65 70 6f if( iSavepo
1d2d0 69 6e 74 3c 30 20 26 26 20 28 70 42 74 2d 3e 62 int<0 && (pBt->b
1d2e0 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 49 4e tsFlags & BTS_IN
1d2f0 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 29 21 3d ITIALLY_EMPTY)!=
1d300 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 0 ){. pBt
1d310 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 ->nPage = 0;.
1d320 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1d330 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29 newDatabase(pBt)
1d340 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 ;. pBt->nPa
1d350 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 32 38 ge = get4byte(28
1d360 20 2b 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e + pBt->pPage1->
1d370 61 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f aData);.. /
1d380 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 * The database s
1d390 69 7a 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 ize was written
1d3a0 69 6e 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 into the offset
1d3b0 32 38 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 28 of the header
1d3c0 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 . ** when t
1d3d0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 he transaction s
1d3e0 74 61 72 74 65 64 2c 20 73 6f 20 77 65 20 6b 6e tarted, so we kn
1d3f0 6f 77 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 ow that the valu
1d400 65 20 61 74 20 6f 66 66 73 65 74 0a 20 20 20 20 e at offset.
1d410 20 20 2a 2a 20 32 38 20 69 73 20 6e 6f 6e 7a 65 ** 28 is nonze
1d420 72 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 ro. */. ass
1d430 65 72 74 28 20 70 42 74 2d 3e 6e 50 61 67 65 3e ert( pBt->nPage>
1d440 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 0 );. }. s
1d450 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1d460 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 (p);. }. retur
1d470 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
1d480 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 reate a new curs
1d490 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 or for the BTree
1d4a0 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f whose root is o
1d4b0 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 n the page.** iT
1d4c0 61 62 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d able. If a read-
1d4d0 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 only cursor is r
1d4e0 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 equested, it is
1d4f0 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 assumed that.**
1d500 74 68 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 the caller alrea
1d510 64 79 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 dy has at least
1d520 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e a read-only tran
1d530 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 saction open.**
1d540 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1d550 61 6c 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 already. If a wr
1d560 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 ite-cursor is re
1d570 71 75 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a quested, then.**
1d580 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 the caller is a
1d590 73 73 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 ssumed to have a
1d5a0 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 n open write tra
1d5b0 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
1d5c0 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 If wrFlag==0, th
1d5d0 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 en the cursor ca
1d5e0 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 n only be used f
1d5f0 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 or reading..** I
1d600 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 f wrFlag==1, the
1d610 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e n the cursor can
1d620 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 be used for rea
1d630 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 ding or for.** w
1d640 72 69 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 riting if other
1d650 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 conditions for w
1d660 72 69 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 riting are also
1d670 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 met. These.** a
1d680 72 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e re the condition
1d690 73 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d s that must be m
1d6a0 65 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 et in order for
1d6b0 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 writing to.** be
1d6c0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 allowed:.**.**
1d6d0 31 3a 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 1: The cursor m
1d6e0 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 ust have been op
1d6f0 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 ened with wrFlag
1d700 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 ==1.**.** 2: Ot
1d710 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e her database con
1d720 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 nections that sh
1d730 61 72 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 are the same pag
1d740 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 er cache.**
1d750 62 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f but which are no
1d760 74 20 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e t in the READ_UN
1d770 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 COMMITTED state
1d780 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 may not have.**
1d790 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e cursors open
1d7a0 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 with wrFlag==0
1d7b0 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c on the same tabl
1d7c0 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a e. Otherwise.**
1d7d0 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 the changes
1d7e0 20 6d 61 64 65 20 62 79 20 74 68 69 73 20 77 72 made by this wr
1d7f0 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 ite cursor would
1d800 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a be visible to.*
1d810 2a 20 20 20 20 20 74 68 65 20 72 65 61 64 20 63 * the read c
1d820 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 ursors in the ot
1d830 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e her database con
1d840 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 nection..**.** 3
1d850 3a 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 : The database
1d860 6d 75 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 must be writable
1d870 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e (not on read-on
1d880 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 ly media).**.**
1d890 34 3a 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 4: There must b
1d8a0 65 20 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e e an active tran
1d8b0 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e saction..**.** N
1d8c0 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f o checking is do
1d8d0 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ne to make sure
1d8e0 74 68 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 that page iTable
1d8f0 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a really is the.*
1d900 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 * root page of a
1d910 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 b-tree. If it
1d920 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 is not, then the
1d930 20 63 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 cursor acquired
1d940 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 .** will not wor
1d950 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a k correctly..**.
1d960 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
1d970 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 that the sqlite
1d980 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 3BtreeCursorZero
1d990 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
1d9a0 65 64 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f ed.** on pCur to
1d9b0 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 initialize the
1d9c0 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69 memory space pri
1d9d0 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
1d9e0 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a his routine..*/.
1d9f0 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
1da00 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 Cursor(. Btree
1da10 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1da20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da30 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f /* The btree */
1da40 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
1da50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
1da70 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 t page of table
1da80 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 to open */. int
1da90 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 wrFlag,
1daa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dab0 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 /* 1 to writ
1dac0 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e. 0 read-only *
1dad0 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
1dae0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
1daf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
1db00 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 rst arg to compa
1db10 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a rison function *
1db20 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
1db30 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ur
1db40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
1db50 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 ace for new curs
1db60 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 or */.){. BtSha
1db70 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1db80 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1db90 20 20 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 /* Shared b-tr
1dba0 65 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 ee handle */..
1dbb0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1dbc0 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
1dbd0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 ) );. assert( w
1dbe0 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c rFlag==0 || wrFl
1dbf0 61 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 ag==1 );.. /* T
1dc00 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
1dc10 65 72 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 ert statements v
1dc20 65 72 69 66 79 20 74 68 61 74 20 69 66 20 74 68 erify that if th
1dc30 69 73 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 is is a sharable
1dc40 20 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 . ** b-tree da
1dc50 74 61 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e tabase, the conn
1dc60 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e ection is holdin
1dc70 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 g the required t
1dc80 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a able locks, . *
1dc90 2a 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 * and that no ot
1dca0 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 her connection h
1dcb0 61 73 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 as any open curs
1dcc0 6f 72 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 or that conflict
1dcd0 73 20 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 s with . ** thi
1dce0 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 s lock. */. as
1dcf0 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 sert( hasSharedC
1dd00 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
1dd10 20 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 iTable, pKeyInf
1dd20 6f 21 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 o!=0, wrFlag+1)
1dd30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 );. assert( wrF
1dd40 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 lag==0 || !hasRe
1dd50 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 adConflicts(p, i
1dd60 54 61 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 Table) );.. /*
1dd70 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 Assert that the
1dd80 63 61 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 caller has opene
1dd90 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 d the required t
1dda0 72 61 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 ransaction. */.
1ddb0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
1ddc0 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ans>TRANS_NONE )
1ddd0 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c ;. assert( wrFl
1dde0 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 ag==0 || p->inTr
1ddf0 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1de00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
1de10 74 2d 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 t->pPage1 && pBt
1de20 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 ->pPage1->aData
1de30 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );.. if( NEVER(
1de40 77 72 46 6c 61 67 20 26 26 20 28 70 42 74 2d 3e wrFlag && (pBt->
1de50 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 btsFlags & BTS_R
1de60 45 41 44 5f 4f 4e 4c 59 29 21 3d 30 29 20 29 7b EAD_ONLY)!=0) ){
1de70 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1de80 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d TE_READONLY;. }
1de90 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 . if( iTable==1
1dea0 20 26 26 20 62 74 72 65 65 50 61 67 65 63 6f 75 && btreePagecou
1deb0 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a 20 20 nt(pBt)==0 ){.
1dec0 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 assert( wrFlag
1ded0 3d 3d 30 20 29 3b 0a 20 20 20 20 69 54 61 62 6c ==0 );. iTabl
1dee0 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a e = 0;. }.. /*
1def0 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 Now that no oth
1df00 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 er errors can oc
1df10 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c cur, finish fill
1df20 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 ing in the BtCur
1df30 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c sor. ** variabl
1df40 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20 es and link the
1df50 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 cursor into the
1df60 42 74 53 68 61 72 65 64 20 6c 69 73 74 2e 20 20 BtShared list.
1df70 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 */. pCur->pgnoR
1df80 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 oot = (Pgno)iTab
1df90 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 le;. pCur->iPag
1dfa0 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72 2d 3e e = -1;. pCur->
1dfb0 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 pKeyInfo = pKeyI
1dfc0 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 nfo;. pCur->pBt
1dfd0 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d ree = p;. pCur-
1dfe0 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 61 73 >pBt = pBt;. as
1dff0 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 sert( wrFlag==0
1e000 7c 7c 20 77 72 46 6c 61 67 3d 3d 42 54 43 46 5f || wrFlag==BTCF_
1e010 57 72 69 74 65 46 6c 61 67 20 29 3b 0a 20 20 70 WriteFlag );. p
1e020 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 3d 20 Cur->curFlags =
1e030 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e wrFlag;. pCur->
1e040 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 pNext = pBt->pCu
1e050 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72 rsor;. if( pCur
1e060 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 ->pNext ){. p
1e070 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 Cur->pNext->pPre
1e080 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20 v = pCur;. }.
1e090 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 pBt->pCursor = p
1e0a0 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 Cur;. pCur->eSt
1e0b0 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
1e0c0 41 4c 49 44 3b 0a 20 20 72 65 74 75 72 6e 20 53 ALID;. return S
1e0d0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 69 6e 74 20 QLITE_OK;.}.int
1e0e0 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1e0f0 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 or(. Btree *p,
1e100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e120 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a /* The btree *
1e130 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
1e140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e160 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 /* Root page of
1e170 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f table to open */
1e180 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 . int wrFlag,
1e190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e1b0 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 * 1 to write. 0
1e1c0 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 read-only */. s
1e1d0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 truct KeyInfo *p
1e1e0 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 KeyInfo,
1e1f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
1e200 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 rst arg to xComp
1e210 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 are() */. BtCur
1e220 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 sor *pCur
1e230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e240 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
1e250 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 new cursor here
1e260 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
1e270 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
1e280 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 ter(p);. rc = b
1e290 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 54 treeCursor(p, iT
1e2a0 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b able, wrFlag, pK
1e2b0 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20 eyInfo, pCur);.
1e2c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1e2d0 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
1e2e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
1e2f0 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 urn the size of
1e300 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 a BtCursor objec
1e310 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a t in bytes..**.*
1e320 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
1e330 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 74 s is needed so t
1e340 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 72 hat users of cur
1e350 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f sors can preallo
1e360 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 cate.** sufficie
1e370 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 6f nt storage to ho
1e380 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 ld a cursor. Th
1e390 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 e BtCursor objec
1e3a0 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74 t is opaque.** t
1e3b0 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 20 o users so they
1e3c0 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 69 cannot do the si
1e3d0 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 65 zeof() themselve
1e3e0 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 61 s - they must ca
1e3f0 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 ll.** this routi
1e400 6e 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ne..*/.int sqlit
1e410 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a e3BtreeCursorSiz
1e420 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 e(void){. retur
1e430 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 n ROUND8(sizeof(
1e440 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a 2f BtCursor));.}../
1e450 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
1e460 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 69 6c 6c memory that will
1e470 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e be converted in
1e480 74 6f 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 to a BtCursor ob
1e490 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ject..**.** The
1e4a0 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 20 simple approach
1e4b0 68 65 72 65 20 77 6f 75 6c 64 20 62 65 20 74 6f here would be to
1e4c0 20 6d 65 6d 73 65 74 28 29 20 74 68 65 20 65 6e memset() the en
1e4d0 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 tire object.** t
1e4e0 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 74 20 o zero. But it
1e4f0 74 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 74 turns out that t
1e500 68 65 20 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 he apPage[] and
1e510 61 69 49 64 78 5b 5d 20 61 72 72 61 79 73 0a 2a aiIdx[] arrays.*
1e520 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f * do not need to
1e530 20 62 65 20 7a 65 72 6f 65 64 20 61 6e 64 20 74 be zeroed and t
1e540 68 65 79 20 61 72 65 20 6c 61 72 67 65 2c 20 73 hey are large, s
1e550 6f 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 o we can save a
1e560 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74 69 lot.** of run-ti
1e570 6d 65 20 62 79 20 73 6b 69 70 70 69 6e 67 20 74 me by skipping t
1e580 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f he initializatio
1e590 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c 65 6d 65 n of those eleme
1e5a0 6e 74 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c nts..*/.void sql
1e5b0 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a ite3BtreeCursorZ
1e5c0 65 72 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 29 ero(BtCursor *p)
1e5d0 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c {. memset(p, 0,
1e5e0 20 6f 66 66 73 65 74 6f 66 28 42 74 43 75 72 73 offsetof(BtCurs
1e5f0 6f 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a or, iPage));.}..
1e600 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 /*.** Close a cu
1e610 72 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64 20 rsor. The read
1e620 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
1e630 62 61 73 65 20 66 69 6c 65 20 69 73 20 72 65 6c base file is rel
1e640 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 eased.** when th
1e650 65 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69 73 e last cursor is
1e660 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 69 6e 74 20 closed..*/.int
1e670 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
1e680 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 eCursor(BtCursor
1e690 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 65 *pCur){. Btree
1e6a0 20 2a 70 42 74 72 65 65 20 3d 20 70 43 75 72 2d *pBtree = pCur-
1e6b0 3e 70 42 74 72 65 65 3b 0a 20 20 69 66 28 20 70 >pBtree;. if( p
1e6c0 42 74 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 Btree ){. int
1e6d0 20 69 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 i;. BtShared
1e6e0 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 *pBt = pCur->pB
1e6f0 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 t;. sqlite3Bt
1e700 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 reeEnter(pBtree)
1e710 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1e720 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 eeClearCursor(pC
1e730 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 ur);. if( pCu
1e740 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 r->pPrev ){.
1e750 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 pCur->pPrev->p
1e760 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 Next = pCur->pNe
1e770 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 xt;. }else{.
1e780 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f pBt->pCurso
1e790 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
1e7a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1e7b0 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 Cur->pNext ){.
1e7c0 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d pCur->pNext-
1e7d0 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 >pPrev = pCur->p
1e7e0 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
1e7f0 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 for(i=0; i<=pCur
1e800 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
1e810 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
1e820 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d (pCur->apPage[i]
1e830 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c );. }. unl
1e840 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
1e850 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 (pBt);. sqlit
1e860 65 33 44 62 46 72 65 65 28 70 42 74 72 65 65 2d e3DbFree(pBtree-
1e870 3e 64 62 2c 20 70 43 75 72 2d 3e 61 4f 76 65 72 >db, pCur->aOver
1e880 66 6c 6f 77 29 3b 0a 20 20 20 20 2f 2a 20 73 71 flow);. /* sq
1e890 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 lite3_free(pCur)
1e8a0 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 ; */. sqlite3
1e8b0 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 BtreeLeave(pBtre
1e8c0 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
1e8d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1e8e0 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
1e8f0 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 he BtCursor* giv
1e900 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 en in the argume
1e910 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a nt has a valid.*
1e920 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 * BtCursor.info
1e930 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 structure. If i
1e940 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 t is not already
1e950 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 valid, call.**
1e960 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 btreeParseCell()
1e970 20 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a to fill it in..
1e980 2a 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 **.** BtCursor.i
1e990 6e 66 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f nfo is a cache o
1e9a0 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f f the informatio
1e9b0 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 n in the current
1e9c0 20 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 cell..** Using
1e9d0 74 68 69 73 20 63 61 63 68 65 20 72 65 64 75 63 this cache reduc
1e9e0 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 es the number of
1e9f0 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 calls to btreeP
1ea00 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a arseCell()..**.*
1ea10 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 * 2007-06-25: T
1ea20 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e here is a bug in
1ea30 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f some versions o
1ea40 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 f MSVC that caus
1ea50 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 e the.** compile
1ea60 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 r to crash when
1ea70 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 getCellInfo() is
1ea80 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
1ea90 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 a macro..** But
1eaa0 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 there is a measu
1eab0 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 reable speed adv
1eac0 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 antage to using
1ead0 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 the macro on gcc
1eae0 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 .** (when less c
1eaf0 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 ompiler optimiza
1eb00 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f tions like -Os o
1eb10 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 r -O0 are used a
1eb20 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c nd the.** compil
1eb30 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 er is not doing
1eb40 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 agressive inlini
1eb50 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 ng.) So we use
1eb60 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a a real function.
1eb70 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 ** for MSVC and
1eb80 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 a macro for ever
1eb90 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 ything else. Ti
1eba0 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 cket #2457..*/.#
1ebb0 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
1ebc0 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 static void asse
1ebd0 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 rtCellInfo(BtCur
1ebe0 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 sor *pCur){.
1ebf0 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
1ec00 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 int iPage = p
1ec10 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 Cur->iPage;.
1ec20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c memset(&info, 0,
1ec30 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a sizeof(info));.
1ec40 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 btreeParseCe
1ec50 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ll(pCur->apPage[
1ec60 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 iPage], pCur->ai
1ec70 49 64 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 Idx[iPage], &inf
1ec80 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 o);. assert(
1ec90 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 6d 65 CORRUPT_DB || me
1eca0 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 mcmp(&info, &pCu
1ecb0 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 r->info, sizeof(
1ecc0 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d info))==0 );. }
1ecd0 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
1ece0 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
1ecf0 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 x).#endif.#ifdef
1ed00 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 _MSC_VER. /* U
1ed10 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 se a real functi
1ed20 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f on in MSVC to wo
1ed30 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 rk around bugs i
1ed40 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e n that compiler.
1ed50 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 */. static voi
1ed60 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 d getCellInfo(Bt
1ed70 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
1ed80 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
1ed90 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 o.nSize==0 ){.
1eda0 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 int iPage =
1edb0 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 pCur->iPage;.
1edc0 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
1edd0 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
1ede0 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 Page],pCur->aiId
1edf0 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e x[iPage],&pCur->
1ee00 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 info);. pCu
1ee10 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 r->curFlags |= B
1ee20 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 3b 0a 20 TCF_ValidNKey;.
1ee30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1ee40 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 assertCellInfo(p
1ee50 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Cur);. }. }.
1ee60 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 20 #else /* if not
1ee70 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a _MSC_VER */. /*
1ee80 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e 20 Use a macro in
1ee90 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 6c all other compil
1eea0 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 ers so that the
1eeb0 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c 69 function is inli
1eec0 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 67 ned */.#define g
1eed0 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
1eee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ef00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ef10 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70 43 \. if( pC
1ef20 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d ur->info.nSize==
1ef30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
1ef40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ef50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ef60 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 \. int
1ef70 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 iPage = pCur->i
1ef80 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
1ef90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1efa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1efb0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 62 74 \. bt
1efc0 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 reeParseCell(pCu
1efd0 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d r->apPage[iPage]
1efe0 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 ,pCur->aiIdx[iPa
1eff0 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 ge],&pCur->info)
1f000 3b 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 70 ; \. p
1f010 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d Cur->curFlags |=
1f020 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 3b BTCF_ValidNKey;
1f030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f050 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 \. }e
1f060 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 lse{
1f070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f0a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
1f0b0 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
1f0c0 70 43 75 72 29 3b 20 20 20 20 20 20 20 20 20 20 pCur);
1f0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f0f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
1f100 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 }.#endif /* _MSC
1f110 5f 56 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 _VER */..#ifndef
1f120 20 4e 44 45 42 55 47 20 20 2f 2a 20 54 68 65 20 NDEBUG /* The
1f130 6e 65 78 74 20 72 6f 75 74 69 6e 65 20 75 73 65 next routine use
1f140 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 d only within as
1f150 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
1f160 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 s */./*.** Retur
1f170 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 67 69 n true if the gi
1f180 76 65 6e 20 42 74 43 75 72 73 6f 72 20 69 73 20 ven BtCursor is
1f190 76 61 6c 69 64 2e 20 20 41 20 76 61 6c 69 64 20 valid. A valid
1f1a0 63 75 72 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a cursor is one.**
1f1b0 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e 74 that is current
1f1c0 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 ly pointing to a
1f1d0 20 72 6f 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 row in a (non-e
1f1e0 6d 70 74 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 mpty) table..**
1f1f0 54 68 69 73 20 69 73 20 61 20 76 65 72 69 66 69 This is a verifi
1f200 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 cation routine i
1f210 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 s used only with
1f220 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 in assert() stat
1f230 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 69 6e 74 20 73 ements..*/.int s
1f240 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1f250 72 49 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f rIsValid(BtCurso
1f260 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75 r *pCur){. retu
1f270 72 6e 20 70 43 75 72 20 26 26 20 70 43 75 72 2d rn pCur && pCur-
1f280 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1f290 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 VALID;.}.#endif
1f2a0 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a /* NDEBUG */../*
1f2b0 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 .** Set *pSize t
1f2c0 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 o the size of th
1f2d0 65 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 e buffer needed
1f2e0 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 to hold the valu
1f2f0 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 e of.** the key
1f300 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 for the current
1f310 65 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 entry. If the c
1f320 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 ursor is not poi
1f330 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 nting.** to a va
1f340 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a lid entry, *pSiz
1f350 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a e is set to 0. .
1f360 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c **.** For a tabl
1f370 65 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 e with the INTKE
1f380 59 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 Y flag set, this
1f390 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
1f3a0 20 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 the key.** itse
1f3b0 6c 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 lf, not the numb
1f3c0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
1f3d0 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 he key..**.** Th
1f3e0 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 70 6f e caller must po
1f3f0 73 69 74 69 6f 6e 20 74 68 65 20 63 75 72 73 6f sition the curso
1f400 72 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b r prior to invok
1f410 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
1f420 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f ..** .** This ro
1f430 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61 69 utine cannot fai
1f440 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 20 72 65 l. It always re
1f450 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
1f460 20 20 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 .*/.int sqlite
1f470 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 3BtreeKeySize(Bt
1f480 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 Cursor *pCur, i6
1f490 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 4 *pSize){. ass
1f4a0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1f4b0 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
1f4c0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
1f4d0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
1f4e0 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 VALID || pCur->e
1f4f0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
1f500 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 LID );. if( pCu
1f510 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f r->eState!=CURSO
1f520 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a R_VALID ){. *
1f530 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 65 6c pSize = 0;. }el
1f540 73 65 7b 0a 20 20 20 20 67 65 74 43 65 6c 6c 49 se{. getCellI
1f550 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 2a nfo(pCur);. *
1f560 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e pSize = pCur->in
1f570 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 72 fo.nKey;. }. r
1f580 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1f590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 .}../*.** Set *p
1f5a0 53 69 7a 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 Size to the numb
1f5b0 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 er of bytes of d
1f5c0 61 74 61 20 69 6e 20 74 68 65 20 65 6e 74 72 79 ata in the entry
1f5d0 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 the.** cursor c
1f5e0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 urrently points
1f5f0 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 to..**.** The ca
1f600 6c 6c 65 72 20 6d 75 73 74 20 67 75 61 72 61 6e ller must guaran
1f610 74 65 65 20 74 68 61 74 20 74 68 65 20 63 75 72 tee that the cur
1f620 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
1f630 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a to a non-NULL.**
1f640 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 20 49 valid entry. I
1f650 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
1f660 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 he calling proce
1f670 64 75 72 65 20 6d 75 73 74 20 67 75 61 72 61 6e dure must guaran
1f680 74 65 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 tee.** that the
1f690 63 75 72 73 6f 72 20 68 61 73 20 43 75 72 73 6f cursor has Curso
1f6a0 72 2e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 r.eState==CURSOR
1f6b0 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 _VALID..**.** Fa
1f6c0 69 6c 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 ilure is not pos
1f6d0 73 69 62 6c 65 2e 20 20 54 68 69 73 20 66 75 6e sible. This fun
1f6e0 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 72 65 74 ction always ret
1f6f0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a urns SQLITE_OK..
1f700 2a 2a 20 49 74 20 6d 69 67 68 74 20 6a 75 73 74 ** It might just
1f710 20 61 73 20 77 65 6c 6c 20 62 65 20 61 20 70 72 as well be a pr
1f720 6f 63 65 64 75 72 65 20 28 72 65 74 75 72 6e 69 ocedure (returni
1f730 6e 67 20 76 6f 69 64 29 20 62 75 74 20 77 65 20 ng void) but we
1f740 63 6f 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f 20 72 continue.** to r
1f750 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 eturn an integer
1f760 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 result code for
1f770 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 61 73 historical reas
1f780 6f 6e 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ons..*/.int sqli
1f790 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 te3BtreeDataSize
1f7a0 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
1f7b0 20 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 u32 *pSize){.
1f7c0 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1f7d0 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1f7e0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
1f7f0 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1f800 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65 74 43 _VALID );. getC
1f810 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
1f820 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e *pSize = pCur->
1f830 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 72 65 info.nData;. re
1f840 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1f850 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 }../*.** Given t
1f860 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
1f870 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 f an overflow pa
1f880 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
1f890 73 65 20 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a se (parameter.**
1f8a0 20 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 75 6e ovfl), this fun
1f8b0 63 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 ction finds the
1f8c0 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
1f8d0 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
1f8e0 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c the .** linked l
1f8f0 69 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 ist of overflow
1f900 70 61 67 65 73 2e 20 49 66 20 70 6f 73 73 69 62 pages. If possib
1f910 6c 65 2c 20 69 74 20 75 73 65 73 20 74 68 65 20 le, it uses the
1f920 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 auto-vacuum.** p
1f930 6f 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 61 20 ointer-map data
1f940 69 6e 73 74 65 61 64 20 6f 66 20 72 65 61 64 69 instead of readi
1f950 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f ng the content o
1f960 66 20 70 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 f page ovfl to d
1f970 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 o so. .**.** If
1f980 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1f990 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
1f9a0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
1f9b0 2e 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a . Otherwise:.**.
1f9c0 2a 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 ** The page numb
1f9d0 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f er of the next o
1f9e0 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 verflow page in
1f9f0 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
1fa00 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 is .** written t
1fa10 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 o *pPgnoNext. If
1fa20 20 70 61 67 65 20 6f 76 66 6c 20 69 73 20 74 68 page ovfl is th
1fa30 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 69 e last page in i
1fa40 74 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 ts linked .** li
1fa50 73 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 st, *pPgnoNext i
1fa60 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a s set to zero. .
1fa70 2a 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65 20 **.** If ppPage
1fa80 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 is not NULL, and
1fa90 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
1faa0 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 the MemPage obje
1fab0 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ct corresponding
1fac0 0a 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 .** to page numb
1fad0 65 72 20 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 er pOvfl was obt
1fae0 61 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 ained, then *ppP
1faf0 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 70 6f age is set to po
1fb00 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 int to that.** r
1fb10 65 66 65 72 65 6e 63 65 2e 20 49 74 20 69 73 20 eference. It is
1fb20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 the responsibili
1fb30 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 ty of the caller
1fb40 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 to call release
1fb50 50 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 Page().** on *pp
1fb60 50 61 67 65 20 74 6f 20 66 72 65 65 20 74 68 65 Page to free the
1fb70 20 72 65 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e reference. In n
1fb80 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20 o reference was
1fb90 6f 62 74 61 69 6e 65 64 20 28 62 65 63 61 75 73 obtained (becaus
1fba0 65 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 e.** the pointer
1fbb0 2d 6d 61 70 20 77 61 73 20 75 73 65 64 20 74 6f -map was used to
1fbc0 20 6f 62 74 61 69 6e 20 74 68 65 20 76 61 6c 75 obtain the valu
1fbd0 65 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 e for *pPgnoNext
1fbe0 29 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 ), then.** *ppPa
1fbf0 67 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 ge is set to zer
1fc00 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 o..*/.static int
1fc10 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 getOverflowPage
1fc20 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
1fc30 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
1fc40 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1fc50 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 file */. Pgno
1fc60 6f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20 20 ovfl,
1fc70 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
1fc80 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 nt overflow page
1fc90 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 4d 65 6d number */. Mem
1fca0 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 Page **ppPage,
1fcb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
1fcc0 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 : MemPage handle
1fcd0 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a (may be NULL) *
1fce0 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 4e /. Pgno *pPgnoN
1fcf0 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ext
1fd00 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 6f 76 /* OUT: Next ov
1fd10 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 erflow page numb
1fd20 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 er */.){. Pgno
1fd30 6e 65 78 74 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 next = 0;. MemP
1fd40 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a age *pPage = 0;.
1fd50 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1fd60 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 E_OK;.. assert(
1fd70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1fd80 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
1fd90 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50 67 6e );. assert(pPgn
1fda0 6f 4e 65 78 74 29 3b 0a 0a 23 69 66 6e 64 65 66 oNext);..#ifndef
1fdb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1fdc0 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79 OVACUUM. /* Try
1fdd0 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 65 78 to find the nex
1fde0 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 t page in the ov
1fdf0 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e erflow list usin
1fe00 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76 g the. ** autov
1fe10 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61 acuum pointer-ma
1fe20 70 20 70 61 67 65 73 2e 20 47 75 65 73 73 20 74 p pages. Guess t
1fe30 68 61 74 20 74 68 65 20 6e 65 78 74 20 70 61 67 hat the next pag
1fe40 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f e in . ** the o
1fe50 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 73 20 verflow list is
1fe60 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f 76 66 page number (ovf
1fe70 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20 67 75 l+1). If that gu
1fe80 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20 ess turns . **
1fe90 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c out to be wrong,
1fea0 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f fall back to lo
1feb0 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 6f ading the data o
1fec0 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d f page . ** num
1fed0 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 ber ovfl to dete
1fee0 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 20 70 rmine the next p
1fef0 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f age number.. */
1ff00 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f . if( pBt->auto
1ff10 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 Vacuum ){. Pg
1ff20 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e no pgno;. Pgn
1ff30 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b o iGuess = ovfl+
1ff40 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 1;. u8 eType;
1ff50 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 .. while( PTR
1ff60 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
1ff70 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75 65 73 iGuess) || iGues
1ff80 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f s==PENDING_BYTE_
1ff90 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
1ffa0 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20 iGuess++;.
1ffb0 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 75 65 }.. if( iGue
1ffc0 73 73 3c 3d 62 74 72 65 65 50 61 67 65 63 6f 75 ss<=btreePagecou
1ffd0 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 nt(pBt) ){.
1ffe0 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
1fff0 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 54 pBt, iGuess, &eT
20000 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 ype, &pgno);.
20010 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
20020 45 5f 4f 4b 20 26 26 20 65 54 79 70 65 3d 3d 50 E_OK && eType==P
20030 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 TRMAP_OVERFLOW2
20040 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b && pgno==ovfl ){
20050 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20 3d 20 . next =
20060 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20 20 20 iGuess;.
20070 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 rc = SQLITE_DONE
20080 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
20090 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 }.#endif.. as
200a0 73 65 72 74 28 20 6e 65 78 74 3d 3d 30 20 7c 7c sert( next==0 ||
200b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc==SQLITE_DONE
200c0 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 );. if( rc==SQ
200d0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
200e0 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
200f0 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 (pBt, ovfl, &pPa
20100 67 65 2c 20 28 70 70 50 61 67 65 3d 3d 30 29 20 ge, (ppPage==0)
20110 3f 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 ? PAGER_GET_READ
20120 4f 4e 4c 59 20 3a 20 30 29 3b 0a 20 20 20 20 61 ONLY : 0);. a
20130 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
20140 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 E_OK || pPage==0
20150 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d );. if( rc==
20160 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
20170 20 20 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 next = get4by
20180 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 te(pPage->aData)
20190 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a ;. }. }.. *
201a0 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 pPgnoNext = next
201b0 3b 0a 20 20 69 66 28 20 70 70 50 61 67 65 20 29 ;. if( ppPage )
201c0 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 {. *ppPage =
201d0 70 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a pPage;. }else{.
201e0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
201f0 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 pPage);. }. re
20200 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 turn (rc==SQLITE
20210 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f _DONE ? SQLITE_O
20220 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a K : rc);.}../*.*
20230 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d * Copy data from
20240 20 61 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 a buffer to a p
20250 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 age, or from a p
20260 61 67 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e age to a buffer.
20270 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 .**.** pPayload
20280 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
20290 64 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 data stored on d
202a0 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 atabase page pDb
202b0 50 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 Page..** If argu
202c0 6d 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 ment eOp is fals
202d0 65 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 e, then nByte by
202e0 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 tes of data are
202f0 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 copied.** from p
20300 50 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 Payload to the b
20310 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 uffer pointed at
20320 20 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 by pBuf. If eOp
20330 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 is true,.** the
20340 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 n sqlite3PagerWr
20350 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ite() is called
20360 6f 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e on pDbPage and n
20370 42 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 Byte bytes.** of
20380 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 data are copied
20390 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 from the buffer
203a0 20 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 pBuf to pPayloa
203b0 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f d..**.** SQLITE_
203c0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f OK is returned o
203d0 6e 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 n success, other
203e0 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f wise an error co
203f0 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e de..*/.static in
20400 74 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 t copyPayload(.
20410 20 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c void *pPayload,
20420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
20430 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 inter to page da
20440 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 ta */. void *pB
20450 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 uf,
20460 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
20470 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
20480 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 nByte,
20490 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
204a0 6f 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 of bytes to copy
204b0 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 */. int eOp,
204c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
204d0 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f /* 0 -> copy fro
204e0 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 m page, 1 -> cop
204f0 79 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 y to page */. D
20500 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 bPage *pDbPage
20510 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
20520 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 containing pPay
20530 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 load */.){. if(
20540 20 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 eOp ){. /* C
20550 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 opy data from bu
20560 66 66 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 ffer to page (a
20570 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 write operation)
20580 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d */. int rc =
20590 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
205a0 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 te(pDbPage);.
205b0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
205c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
205d0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
205e0 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 memcpy(pPayload
205f0 2c 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a , pBuf, nByte);.
20600 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
20610 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 Copy data from p
20620 61 67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 age to buffer (a
20630 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 read operation)
20640 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 */. memcpy(p
20650 42 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e Buf, pPayload, n
20660 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Byte);. }. ret
20670 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
20680 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
20690 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
206a0 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 read or overwri
206b0 74 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 te payload infor
206c0 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 mation.** for th
206d0 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 e entry that the
206e0 20 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 pCur cursor is
206f0 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 54 68 65 pointing to. The
20700 20 65 4f 70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 eOp.** argument
20710 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 is interpreted
20720 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
20730 2a 20 20 20 30 3a 20 54 68 65 20 6f 70 65 72 61 * 0: The opera
20740 74 69 6f 6e 20 69 73 20 61 20 72 65 61 64 2e 20 tion is a read.
20750 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 Populate the ove
20760 72 66 6c 6f 77 20 63 61 63 68 65 2e 0a 2a 2a 20 rflow cache..**
20770 20 20 31 3a 20 54 68 65 20 6f 70 65 72 61 74 69 1: The operati
20780 6f 6e 20 69 73 20 61 20 77 72 69 74 65 2e 20 50 on is a write. P
20790 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 opulate the over
207a0 66 6c 6f 77 20 63 61 63 68 65 2e 0a 2a 2a 20 20 flow cache..**
207b0 20 32 3a 20 54 68 65 20 6f 70 65 72 61 74 69 6f 2: The operatio
207c0 6e 20 69 73 20 61 20 72 65 61 64 2e 20 44 6f 20 n is a read. Do
207d0 6e 6f 74 20 70 6f 70 75 6c 61 74 65 20 74 68 65 not populate the
207e0 20 6f 76 65 72 66 6c 6f 77 20 63 61 63 68 65 2e overflow cache.
207f0 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f .**.** A total o
20800 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 61 72 f "amt" bytes ar
20810 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
20820 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 n beginning at "
20830 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 offset"..** Data
20840 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 is read to or f
20850 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 rom the buffer p
20860 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 Buf..**.** The c
20870 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 72 65 61 ontent being rea
20880 64 20 6f 72 20 77 72 69 74 74 65 6e 20 6d 69 67 d or written mig
20890 68 74 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 ht appear on the
208a0 20 6d 61 69 6e 20 70 61 67 65 0a 2a 2a 20 6f 72 main page.** or
208b0 20 62 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 be scattered ou
208c0 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 t on multiple ov
208d0 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a erflow pages..**
208e0 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 .** If the curre
208f0 6e 74 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20 nt cursor entry
20900 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 uses one or more
20910 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
20920 61 6e 64 20 74 68 65 0a 2a 2a 20 65 4f 70 20 61 and the.** eOp a
20930 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 32 rgument is not 2
20940 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
20950 6d 61 79 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 may allocate spa
20960 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c ce for and lazil
20970 79 20 0a 2a 2a 20 70 6f 70 6c 75 61 74 65 73 20 y .** popluates
20980 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
20990 65 2d 6c 69 73 74 20 63 61 63 68 65 20 61 72 72 e-list cache arr
209a0 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 ay (BtCursor.aOv
209b0 65 72 66 6c 6f 77 29 2e 20 0a 2a 2a 20 53 75 62 erflow). .** Sub
209c0 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 sequent calls us
209d0 65 20 74 68 69 73 20 63 61 63 68 65 20 74 6f 20 e this cache to
209e0 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 make seeking to
209f0 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66 66 the supplied off
20a00 73 65 74 20 0a 2a 2a 20 6d 6f 72 65 20 65 66 66 set .** more eff
20a10 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e icient..**.** On
20a20 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ce an overflow p
20a30 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 age-list cache h
20a40 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 as been allocate
20a50 64 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 d, it may be.**
20a60 69 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 invalidated if s
20a70 6f 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 ome other cursor
20a80 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 writes to the s
20a90 61 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 ame table, or if
20aa0 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 .** the cursor i
20ab0 73 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 s moved to a dif
20ac0 66 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 ferent row. Addi
20ad0 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 tionally, in aut
20ae0 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 o-vacuum.** mode
20af0 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 , the following
20b00 65 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c events may inval
20b10 69 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f idate an overflo
20b20 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
20b30 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 e..**.** * An
20b40 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
20b50 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d um,.** * A com
20b60 6d 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 mit in auto_vacu
20b70 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a um="full" mode,.
20b80 2a 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 ** * Creating
20b90 61 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 a table (may req
20ba0 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f uire moving an o
20bb0 76 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a verflow page)..*
20bc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 /.static int acc
20bd0 65 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 essPayload(. Bt
20be0 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 Cursor *pCur,
20bf0 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 /* Cursor poi
20c00 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 nting to entry t
20c10 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 o read from */.
20c20 20 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 u32 offset,
20c30 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 /* Begin r
20c40 65 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 eading this far
20c50 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a into payload */.
20c60 20 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 u32 amt,
20c70 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 /* Read t
20c80 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a his many bytes *
20c90 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
20ca0 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 r *pBuf, /* Writ
20cb0 65 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f e the bytes into
20cc0 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 this buffer */
20cd0 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 . int eOp
20ce0 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 /* zero
20cf0 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 to read. non-zer
20d00 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 o to write. */.)
20d10 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 {. unsigned cha
20d20 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 r *aPayload;. i
20d30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
20d40 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 K;. u32 nKey;.
20d50 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 int iIdx = 0;.
20d60 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
20d70 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
20d80 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 Cur->iPage]; /*
20d90 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 Btree page of cu
20da0 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 rrent entry */.
20db0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
20dc0 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 pCur->pBt;
20dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20de0 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f Btree this curso
20df0 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a r belongs to */.
20e00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 #ifdef SQLITE_DI
20e10 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 5f 52 45 RECT_OVERFLOW_RE
20e20 41 44 0a 20 20 69 6e 74 20 62 45 6e 64 3b 20 20 AD. int bEnd;
20e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20e50 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 61 64 /* True if read
20e60 69 6e 67 20 74 6f 20 65 6e 64 20 6f 66 20 64 61 ing to end of da
20e70 74 61 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 ta */.#endif..
20e80 61 73 73 65 72 74 28 20 70 50 61 67 65 20 29 3b assert( pPage );
20e90 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
20ea0 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
20eb0 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
20ec0 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
20ed0 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 Cur->iPage]<pPag
20ee0 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 e->nCell );. as
20ef0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
20f00 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
20f10 20 20 61 73 73 65 72 74 28 20 65 4f 70 21 3d 32 assert( eOp!=2
20f20 20 7c 7c 20 6f 66 66 73 65 74 3d 3d 30 20 29 3b || offset==0 );
20f30 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 /* Always
20f40 73 74 61 72 74 20 66 72 6f 6d 20 62 65 67 69 6e start from begin
20f50 6e 69 6e 67 20 66 6f 72 20 65 4f 70 3d 3d 32 20 ning for eOp==2
20f60 2a 2f 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 */.. getCellInf
20f70 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c o(pCur);. aPayl
20f80 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f oad = pCur->info
20f90 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 .pCell + pCur->i
20fa0 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e nfo.nHeader;. n
20fb0 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e Key = (pPage->in
20fc0 74 4b 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 tKey ? 0 : (int)
20fd0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 pCur->info.nKey)
20fe0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
20ff0 44 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 5f DIRECT_OVERFLOW_
21000 52 45 41 44 0a 20 20 62 45 6e 64 20 3d 20 28 6f READ. bEnd = (o
21010 66 66 73 65 74 2b 61 6d 74 3d 3d 6e 4b 65 79 2b ffset+amt==nKey+
21020 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 pCur->info.nData
21030 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 );.#endif.. if(
21040 20 4e 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d NEVER(offset+am
21050 74 20 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 t > nKey+pCur->i
21060 6e 66 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c nfo.nData) . |
21070 7c 20 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 | &aPayload[pCur
21080 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e ->info.nLocal] >
21090 20 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 &pPage->aData[p
210a0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a Bt->usableSize].
210b0 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 ){. /* Tryi
210c0 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 ng to read or wr
210d0 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 ite past the end
210e0 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 20 of the data is
210f0 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 an error */.
21100 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
21110 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
21120 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 . /* Check if d
21130 61 74 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 ata must be read
21140 2f 77 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d /written to/from
21150 20 74 68 65 20 62 74 72 65 65 20 70 61 67 65 20 the btree page
21160 69 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 itself. */. if(
21170 20 6f 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e offset<pCur->in
21180 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 fo.nLocal ){.
21190 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 int a = amt;.
211a0 20 20 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 if( a+offset>p
211b0 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
211c0 20 29 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 ){. a = pC
211d0 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 ur->info.nLocal
211e0 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a - offset;. }.
211f0 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 rc = copyPay
21200 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f load(&aPayload[o
21210 66 66 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c ffset], pBuf, a,
21220 20 28 65 4f 70 20 26 20 30 78 30 31 29 2c 20 70 (eOp & 0x01), p
21230 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
21240 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a offset = 0;.
21250 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 pBuf += a;.
21260 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d amt -= a;. }
21270 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 else{. offset
21280 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e -= pCur->info.n
21290 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 Local;. }.. if
212a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
212b0 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 && amt>0 ){.
212c0 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c 53 69 const u32 ovflSi
212d0 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ze = pBt->usable
212e0 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 Size - 4; /* By
212f0 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 tes content per
21300 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 ovfl page */.
21310 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a Pgno nextPage;.
21320 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 . nextPage =
21330 67 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f get4byte(&aPaylo
21340 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c ad[pCur->info.nL
21350 6f 63 61 6c 5d 29 3b 0a 0a 20 20 20 20 2f 2a 20 ocal]);.. /*
21360 49 66 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e If the BtCursor.
21370 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 68 61 73 20 aOverflow[] has
21380 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 not been allocat
21390 65 64 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 ed, allocate it
213a0 6e 6f 77 2e 0a 20 20 20 20 2a 2a 20 45 78 63 65 now.. ** Exce
213b0 70 74 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 pt, do not alloc
213c0 61 74 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 ate aOverflow[]
213d0 66 6f 72 20 65 4f 70 3d 3d 32 2e 0a 20 20 20 20 for eOp==2..
213e0 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 61 4f **. ** The aO
213f0 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 verflow[] array
21400 69 73 20 73 69 7a 65 64 20 61 74 20 6f 6e 65 20 is sized at one
21410 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f entry for each o
21420 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 verflow page.
21430 20 2a 2a 20 69 6e 20 74 68 65 20 6f 76 65 72 66 ** in the overf
21440 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 low chain. The p
21450 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
21460 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 e first overflow
21470 20 70 61 67 65 20 69 73 0a 20 20 20 20 2a 2a 20 page is. **
21480 73 74 6f 72 65 64 20 69 6e 20 61 4f 76 65 72 66 stored in aOverf
21490 6c 6f 77 5b 30 5d 2c 20 65 74 63 2e 20 41 20 76 low[0], etc. A v
214a0 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 alue of 0 in the
214b0 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 aOverflow[] arr
214c0 61 79 0a 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20 ay. ** means
214d0 22 6e 6f 74 20 79 65 74 20 6b 6e 6f 77 6e 22 20 "not yet known"
214e0 28 74 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 (the cache is la
214f0 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e zily populated).
21500 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
21510 65 4f 70 21 3d 32 20 26 26 20 28 70 43 75 72 2d eOp!=2 && (pCur-
21520 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 >curFlags & BTCF
21530 5f 56 61 6c 69 64 4f 76 66 6c 29 3d 3d 30 20 29 _ValidOvfl)==0 )
21540 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 {. int nOvf
21550 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e l = (pCur->info.
21560 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 nPayload-pCur->i
21570 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 nfo.nLocal+ovflS
21580 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b ize-1)/ovflSize;
21590 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 6c . if( nOvfl
215a0 3e 70 43 75 72 2d 3e 6e 4f 76 66 6c 41 6c 6c 6f >pCur->nOvflAllo
215b0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 50 67 6e c ){. Pgn
215c0 6f 20 2a 61 4e 65 77 20 3d 20 28 50 67 6e 6f 2a o *aNew = (Pgno*
215d0 29 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f )sqlite3DbReallo
215e0 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 c(. p
215f0 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2c Cur->pBtree->db,
21600 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
21610 2c 20 6e 4f 76 66 6c 2a 32 2a 73 69 7a 65 6f 66 , nOvfl*2*sizeof
21620 28 50 67 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 (Pgno). )
21630 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 4e ;. if( aN
21640 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ew==0 ){.
21650 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
21660 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d 65 OMEM;. }e
21670 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 lse{. p
21680 43 75 72 2d 3e 6e 4f 76 66 6c 41 6c 6c 6f 63 20 Cur->nOvflAlloc
21690 3d 20 6e 4f 76 66 6c 2a 32 3b 0a 20 20 20 20 20 = nOvfl*2;.
216a0 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 pCur->aOver
216b0 66 6c 6f 77 20 3d 20 61 4e 65 77 3b 0a 20 20 20 flow = aNew;.
216c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
216d0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
216e0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
216f0 20 20 6d 65 6d 73 65 74 28 70 43 75 72 2d 3e 61 memset(pCur->a
21700 4f 76 65 72 66 6c 6f 77 2c 20 30 2c 20 6e 4f 76 Overflow, 0, nOv
21710 66 6c 2a 73 69 7a 65 6f 66 28 50 67 6e 6f 29 29 fl*sizeof(Pgno))
21720 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e ;. pCur->
21730 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 46 curFlags |= BTCF
21740 5f 56 61 6c 69 64 4f 76 66 6c 3b 0a 20 20 20 20 _ValidOvfl;.
21750 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
21760 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c 6f * If the overflo
21770 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
21780 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 e has been alloc
21790 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20 20 ated and the.
217a0 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 ** entry for th
217b0 65 20 66 69 72 73 74 20 72 65 71 75 69 72 65 64 e first required
217c0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
217d0 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 s valid, skip.
217e0 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f ** directly to
217f0 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 it.. */.
21800 69 66 28 20 28 70 43 75 72 2d 3e 63 75 72 46 6c if( (pCur->curFl
21810 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c 69 64 ags & BTCF_Valid
21820 4f 76 66 6c 29 21 3d 30 20 26 26 20 70 43 75 72 Ovfl)!=0 && pCur
21830 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 ->aOverflow[offs
21840 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a et/ovflSize] ){.
21850 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f 66 iIdx = (of
21860 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a fset/ovflSize);.
21870 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d nextPage =
21880 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
21890 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 [iIdx];. of
218a0 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f fset = (offset%o
218b0 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a vflSize);. }.
218c0 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d . for( ; rc==
218d0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 SQLITE_OK && amt
218e0 3e 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b 20 >0 && nextPage;
218f0 69 49 64 78 2b 2b 29 7b 0a 0a 20 20 20 20 20 20 iIdx++){..
21900 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 /* If required,
21910 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 populate the ove
21920 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
21930 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 cache. */.
21940 69 66 28 20 28 70 43 75 72 2d 3e 63 75 72 46 6c if( (pCur->curFl
21950 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c 69 64 ags & BTCF_Valid
21960 4f 76 66 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 Ovfl)!=0 ){.
21970 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72 assert(!pCur
21980 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
21990 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 ] || pCur->aOver
219a0 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 flow[iIdx]==next
219b0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 Page);. p
219c0 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
219d0 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b Idx] = nextPage;
219e0 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
219f0 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c if( offset>=ovfl
21a00 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
21a10 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 /* The only reas
21a20 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 on to read this
21a30 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69 page is to obtai
21a40 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 n the page.
21a50 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 ** number for
21a60 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
21a70 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 n the overflow c
21a80 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20 hain. The page.
21a90 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69 ** data i
21aa0 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20 s not required.
21ab0 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 6f 20 So first try to
21ac0 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66 lookup the overf
21ad0 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 low. ** p
21ae0 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20 age-list cache,
21af0 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c if any, then fal
21b00 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65 l back to the ge
21b10 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a tOverflowPage().
21b20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 ** funct
21b30 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ion.. **.
21b40 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 ** Note
21b50 74 68 61 74 20 74 68 65 20 61 4f 76 65 72 66 6c that the aOverfl
21b60 6f 77 5b 5d 20 61 72 72 61 79 20 6d 75 73 74 20 ow[] array must
21b70 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 be allocated bec
21b80 61 75 73 65 20 65 4f 70 21 3d 32 0a 20 20 20 20 ause eOp!=2.
21b90 20 20 20 20 2a 2a 20 68 65 72 65 2e 20 20 49 66 ** here. If
21ba0 20 65 4f 70 3d 3d 32 2c 20 74 68 65 6e 20 6f 66 eOp==2, then of
21bb0 66 73 65 74 3d 3d 30 20 61 6e 64 20 74 68 69 73 fset==0 and this
21bc0 20 62 72 61 6e 63 68 20 69 73 20 6e 65 76 65 72 branch is never
21bd0 20 74 61 6b 65 6e 2e 0a 20 20 20 20 20 20 20 20 taken..
21be0 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
21bf0 74 28 20 65 4f 70 21 3d 32 20 29 3b 0a 20 20 20 t( eOp!=2 );.
21c00 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
21c10 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 r->curFlags & BT
21c20 43 46 5f 56 61 6c 69 64 4f 76 66 6c 20 29 3b 0a CF_ValidOvfl );.
21c30 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 if( pCur
21c40 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
21c50 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 +1] ){.
21c60 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 nextPage = pCur
21c70 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
21c80 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c +1];. }el
21c90 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 se{. rc
21ca0 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 = getOverflowPa
21cb0 67 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 ge(pBt, nextPage
21cc0 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b , 0, &nextPage);
21cd0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
21ce0 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 offset -= ovf
21cf0 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c lSize;. }el
21d00 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e se{. /* N
21d10 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 73 eed to read this
21d20 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 page properly.
21d30 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 It contains some
21d40 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 of the.
21d50 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61 ** range of data
21d60 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 72 that is being r
21d70 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 ead (eOp==0) or
21d80 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 written (eOp!=0)
21d90 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 .. */.#if
21da0 64 65 66 20 53 51 4c 49 54 45 5f 44 49 52 45 43 def SQLITE_DIREC
21db0 54 5f 4f 56 45 52 46 4c 4f 57 5f 52 45 41 44 0a T_OVERFLOW_READ.
21dc0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
21dd0 66 69 6c 65 20 2a 66 64 3b 0a 23 65 6e 64 69 66 file *fd;.#endif
21de0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 20 3d . int a =
21df0 20 61 6d 74 3b 0a 20 20 20 20 20 20 20 20 69 66 amt;. if
21e00 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f ( a + offset > o
21e10 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 vflSize ){.
21e20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a a = ovflSiz
21e30 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 e - offset;.
21e40 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 }..#ifdef SQ
21e50 4c 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 52 LITE_DIRECT_OVER
21e60 46 4c 4f 57 5f 52 45 41 44 0a 20 20 20 20 20 20 FLOW_READ.
21e70 20 20 2f 2a 20 49 66 20 61 6c 6c 20 74 68 65 20 /* If all the
21e80 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 following are tr
21e90 75 65 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 ue:. **.
21ea0 20 20 20 20 20 20 20 2a 2a 20 20 20 31 29 20 74 ** 1) t
21eb0 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70 his is a read op
21ec0 65 72 61 74 69 6f 6e 2c 20 61 6e 64 20 0a 20 20 eration, and .
21ed0 20 20 20 20 20 20 2a 2a 20 20 20 32 29 20 64 61 ** 2) da
21ee0 74 61 20 69 73 20 72 65 71 75 69 72 65 64 20 66 ta is required f
21ef0 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 rom the start of
21f00 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 this overflow p
21f10 61 67 65 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 age, and.
21f20 20 2a 2a 20 20 20 33 29 20 74 68 65 20 64 61 74 ** 3) the dat
21f30 61 62 61 73 65 20 69 73 20 66 69 6c 65 2d 62 61 abase is file-ba
21f40 63 6b 65 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 cked, and.
21f50 20 20 2a 2a 20 20 20 34 29 20 74 68 65 72 65 20 ** 4) there
21f60 69 73 20 6e 6f 20 6f 70 65 6e 20 77 72 69 74 65 is no open write
21f70 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e -transaction, an
21f80 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 35 d. ** 5
21f90 29 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 ) the database i
21fa0 73 20 6e 6f 74 20 61 20 57 41 4c 20 64 61 74 61 s not a WAL data
21fb0 62 61 73 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a base,. **
21fc0 20 20 20 36 29 20 61 6c 6c 20 64 61 74 61 20 66 6) all data f
21fd0 72 6f 6d 20 74 68 65 20 70 61 67 65 20 69 73 20 rom the page is
21fe0 62 65 69 6e 67 20 72 65 61 64 2e 0a 20 20 20 20 being read..
21ff0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
22000 2a 20 74 68 65 6e 20 64 61 74 61 20 63 61 6e 20 * then data can
22010 62 65 20 72 65 61 64 20 64 69 72 65 63 74 6c 79 be read directly
22020 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
22030 73 65 20 66 69 6c 65 20 69 6e 74 6f 20 74 68 65 se file into the
22040 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 74 70 . ** outp
22050 75 74 20 62 75 66 66 65 72 2c 20 62 79 70 61 73 ut buffer, bypas
22060 73 69 6e 67 20 74 68 65 20 70 61 67 65 2d 63 61 sing the page-ca
22070 63 68 65 20 61 6c 74 6f 67 65 74 68 65 72 2e 20 che altogether.
22080 54 68 69 73 20 73 70 65 65 64 73 0a 20 20 20 20 This speeds.
22090 20 20 20 20 2a 2a 20 75 70 20 6c 6f 61 64 69 6e ** up loadin
220a0 67 20 6c 61 72 67 65 20 72 65 63 6f 72 64 73 20 g large records
220b0 74 68 61 74 20 73 70 61 6e 20 6d 61 6e 79 20 6f that span many o
220c0 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 20 verflow pages..
220d0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
220e0 20 20 69 66 28 20 28 65 4f 70 26 30 78 30 31 29 if( (eOp&0x01)
220f0 3d 3d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 ==0
22100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22110 20 20 20 20 20 20 20 20 20 2f 2a 20 28 31 29 20 /* (1)
22120 2a 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 6f */. && o
22130 66 66 73 65 74 3d 3d 30 20 20 20 20 20 20 20 20 ffset==0
22140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22160 20 20 2f 2a 20 28 32 29 20 2a 2f 0a 20 20 20 20 /* (2) */.
22170 20 20 20 20 20 26 26 20 28 62 45 6e 64 20 7c 7c && (bEnd ||
22180 20 61 3d 3d 6f 76 66 6c 53 69 7a 65 29 20 20 20 a==ovflSize)
22190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
221a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 28 36 /* (6
221b0 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 26 26 ) */. &&
221c0 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
221d0 69 6f 6e 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 ion==TRANS_READ
221e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
221f0 20 20 20 20 2f 2a 20 28 34 29 20 2a 2f 0a 20 20 /* (4) */.
22200 20 20 20 20 20 20 20 26 26 20 28 66 64 20 3d 20 && (fd =
22210 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
22220 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 2d 3e (pBt->pPager))->
22230 70 4d 65 74 68 6f 64 73 20 20 20 20 20 2f 2a 20 pMethods /*
22240 28 33 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 (3) */.
22250 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e && pBt->pPage1->
22260 61 44 61 74 61 5b 31 39 5d 3d 3d 30 78 30 31 20 aData[19]==0x01
22270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22280 20 20 20 20 20 20 2f 2a 20 28 35 29 20 2a 2f 0a /* (5) */.
22290 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
222a0 20 20 20 20 20 75 38 20 61 53 61 76 65 5b 34 5d u8 aSave[4]
222b0 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 2a ;. u8 *
222c0 61 57 72 69 74 65 20 3d 20 26 70 42 75 66 5b 2d aWrite = &pBuf[-
222d0 34 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 4];. me
222e0 6d 63 70 79 28 61 53 61 76 65 2c 20 61 57 72 69 mcpy(aSave, aWri
222f0 74 65 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 te, 4);.
22300 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
22310 52 65 61 64 28 66 64 2c 20 61 57 72 69 74 65 2c Read(fd, aWrite,
22320 20 61 2b 34 2c 20 28 69 36 34 29 70 42 74 2d 3e a+4, (i64)pBt->
22330 70 61 67 65 53 69 7a 65 2a 28 6e 65 78 74 50 61 pageSize*(nextPa
22340 67 65 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 ge-1));.
22350 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 nextPage = get
22360 34 62 79 74 65 28 61 57 72 69 74 65 29 3b 0a 20 4byte(aWrite);.
22370 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
22380 61 57 72 69 74 65 2c 20 61 53 61 76 65 2c 20 34 aWrite, aSave, 4
22390 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
223a0 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 20 .#endif..
223b0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 44 62 50 {. DbP
223c0 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 age *pDbPage;.
223d0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
223e0 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
223f0 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 (pBt->pPager, ne
22400 78 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 xtPage, &pDbPage
22410 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
22420 28 28 65 4f 70 26 30 78 30 31 29 3d 3d 30 20 3f ((eOp&0x01)==0 ?
22430 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f PAGER_GET_READO
22440 4e 4c 59 20 3a 20 30 29 0a 20 20 20 20 20 20 20 NLY : 0).
22450 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 );.
22460 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
22470 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
22480 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69 aPayload = sqli
22490 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
224a0 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
224b0 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d nextPage =
224c0 20 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f get4byte(aPaylo
224d0 61 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ad);.
224e0 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 rc = copyPayloa
224f0 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 d(&aPayload[offs
22500 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c 20 et+4], pBuf, a,
22510 28 65 4f 70 26 30 78 30 31 29 2c 20 70 44 62 50 (eOp&0x01), pDbP
22520 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
22530 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
22540 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
22550 20 20 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 offset
22560 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
22570 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
22580 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 amt -= a;.
22590 20 20 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b pBuf += a;
225a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
225b0 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }.. if( rc==SQ
225c0 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 LITE_OK && amt>0
225d0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
225e0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
225f0 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e PT;. }. return
22600 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
22610 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b ad part of the k
22620 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ey associated wi
22630 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 th cursor pCur.
22640 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 Exactly.** "amt
22650 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 " bytes will be
22660 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 transfered into
22670 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 pBuf[]. The tra
22680 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 nsfer.** begins
22690 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a at "offset"..**.
226a0 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 ** The caller mu
226b0 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 st ensure that p
226c0 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 Cur is pointing
226d0 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a to a valid row.*
226e0 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a * in the table..
226f0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
22700 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
22710 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
22720 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 de if anything g
22730 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 oes.** wrong. A
22740 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
22750 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 ned if "offset+a
22760 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 mt" is larger th
22770 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 an.** the availa
22780 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a ble payload..*/.
22790 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
227a0 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 Key(BtCursor *pC
227b0 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 ur, u32 offset,
227c0 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 u32 amt, void *p
227d0 42 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Buf){. assert(
227e0 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
227f0 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
22800 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
22810 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
22820 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
22830 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 ->iPage>=0 && pC
22840 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
22850 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 >iPage] );. ass
22860 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
22870 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
22880 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
22890 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
228a0 3b 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 ;. return acces
228b0 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f sPayload(pCur, o
228c0 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 ffset, amt, (uns
228d0 69 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 igned char*)pBuf
228e0 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 , 0);.}../*.** R
228f0 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 ead part of the
22900 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
22910 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 with cursor pCur
22920 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 . Exactly.** "a
22930 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 mt" bytes will b
22940 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 e transfered int
22950 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 o pBuf[]. The t
22960 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e ransfer.** begin
22970 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a s at "offset"..*
22980 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
22990 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
229a0 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 or an error cod
229b0 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f e if anything go
229c0 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e es.** wrong. An
229d0 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
229e0 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d ed if "offset+am
229f0 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 t" is larger tha
22a00 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 n.** the availab
22a10 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 69 le payload..*/.i
22a20 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
22a30 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ata(BtCursor *pC
22a40 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 ur, u32 offset,
22a50 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 u32 amt, void *p
22a60 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Buf){. int rc;.
22a70 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
22a80 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
22a90 69 66 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if ( pCur->eStat
22aa0 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 e==CURSOR_INVALI
22ab0 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 D ){. return
22ac0 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 SQLITE_ABORT;.
22ad0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 }.#endif.. asse
22ae0 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
22af0 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
22b00 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
22b10 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
22b20 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
22b30 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 TE_OK ){. ass
22b40 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
22b50 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
22b60 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
22b70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 Cur->iPage>=0 &&
22b80 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
22b90 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 ur->iPage] );.
22ba0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
22bb0 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
22bc0 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b e]<pCur->apPage[
22bd0 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 pCur->iPage]->nC
22be0 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ell );. rc =
22bf0 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 accessPayload(pC
22c00 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c ur, offset, amt,
22c10 20 70 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 pBuf, 0);. }.
22c20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
22c30 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
22c40 69 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 inter to payload
22c50 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
22c60 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 m the entry that
22c70 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 the .** pCur cu
22c80 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 rsor is pointing
22c90 20 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 to. The pointe
22ca0 72 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 r is to the begi
22cb0 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 nning of.** the
22cc0 6b 65 79 20 69 66 20 69 6e 64 65 78 20 62 74 72 key if index btr
22cd0 65 65 73 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b ees (pPage->intK
22ce0 65 79 3d 3d 30 29 20 61 6e 64 20 69 73 20 74 68 ey==0) and is th
22cf0 65 20 64 61 74 61 20 66 6f 72 0a 2a 2a 20 74 61 e data for.** ta
22d00 62 6c 65 20 62 74 72 65 65 73 20 28 70 50 61 67 ble btrees (pPag
22d10 65 2d 3e 69 6e 74 4b 65 79 3d 3d 31 29 2e 20 54 e->intKey==1). T
22d20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
22d30 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 0a es of available.
22d40 2a 2a 20 6b 65 79 2f 64 61 74 61 20 69 73 20 77 ** key/data is w
22d50 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 41 6d ritten into *pAm
22d60 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 2c t. If *pAmt==0,
22d70 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 0a then the value.
22d80 2a 2a 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c ** returned will
22d90 20 6e 6f 74 20 62 65 20 61 20 76 61 6c 69 64 20 not be a valid
22da0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 pointer..**.** T
22db0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
22dc0 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 n optimization.
22dd0 20 49 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f It is common fo
22de0 72 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 r the entire key
22df0 0a 2a 2a 20 61 6e 64 20 64 61 74 61 20 74 6f 20 .** and data to
22e00 66 69 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c fit on the local
22e10 20 70 61 67 65 20 61 6e 64 20 66 6f 72 20 74 68 page and for th
22e20 65 72 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 65 ere to be no ove
22e30 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e 20 rflow.** pages.
22e40 20 57 68 65 6e 20 74 68 61 74 20 69 73 20 73 6f When that is so
22e50 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 , this routine c
22e60 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61 63 an be used to ac
22e70 63 65 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 20 cess the.** key
22e80 61 6e 64 20 64 61 74 61 20 77 69 74 68 6f 75 74 and data without
22e90 20 6d 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e 20 making a copy.
22ea0 20 49 66 20 74 68 65 20 6b 65 79 20 61 6e 64 2f If the key and/
22eb0 6f 72 20 64 61 74 61 20 73 70 69 6c 6c 73 0a 2a or data spills.*
22ec0 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 * onto overflow
22ed0 70 61 67 65 73 2c 20 74 68 65 6e 20 61 63 63 65 pages, then acce
22ee0 73 73 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 74 ssPayload() must
22ef0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 73 be used to reas
22f00 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 semble.** the ke
22f10 79 2f 64 61 74 61 20 61 6e 64 20 63 6f 70 79 20 y/data and copy
22f20 69 74 20 69 6e 74 6f 20 61 20 70 72 65 61 6c 6c it into a preall
22f30 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a ocated buffer..*
22f40 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 *.** The pointer
22f50 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
22f60 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 s routine looks
22f70 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 directly into th
22f80 65 20 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 65 e cached.** page
22f90 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
22fa0 2e 20 20 54 68 65 20 64 61 74 61 20 6d 69 67 68 . The data migh
22fb0 74 20 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65 t change or move
22fc0 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a 2a the next time.*
22fd0 2a 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74 * any btree rout
22fe0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ine is called..*
22ff0 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 76 /.static const v
23000 6f 69 64 20 2a 66 65 74 63 68 50 61 79 6c 6f 61 oid *fetchPayloa
23010 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 d(. BtCursor *p
23020 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 Cur, /* Cur
23030 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
23040 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 entry to read fr
23050 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 2a 70 41 6d om */. u32 *pAm
23060 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t /*
23070 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 Write the number
23080 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79 of available by
23090 74 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 tes here */.){.
230a0 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 assert( pCur!=0
230b0 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e && pCur->iPage>
230c0 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 =0 && pCur->apPa
230d0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
230e0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
230f0 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
23100 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
23110 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
23120 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 x_held(pCur->pBt
23130 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 ree->db->mutex)
23140 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 );. assert( cur
23150 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
23160 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
23170 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
23180 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e r->iPage]<pCur->
23190 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
231a0 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 ge]->nCell );.
231b0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 6e assert( pCur->in
231c0 66 6f 2e 6e 53 69 7a 65 3e 30 20 29 3b 0a 23 69 fo.nSize>0 );.#i
231d0 66 20 30 0a 20 20 69 66 28 20 70 43 75 72 2d 3e f 0. if( pCur->
231e0 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b info.nSize==0 ){
231f0 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 . btreeParseC
23200 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 ell(pCur->apPage
23210 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 20 70 [pCur->iPage], p
23220 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
23230 3e 69 50 61 67 65 5d 2c 0a 20 20 20 20 20 20 20 >iPage],.
23240 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43 75 &pCu
23250 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 7d 0a 23 65 r->info);. }.#e
23260 6e 64 69 66 0a 20 20 2a 70 41 6d 74 20 3d 20 70 ndif. *pAmt = p
23270 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
23280 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 ;. return (void
23290 2a 29 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 *)(pCur->info.pC
232a0 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66 6f ell + pCur->info
232b0 2e 6e 48 65 61 64 65 72 29 3b 0a 7d 0a 0a 0a 2f .nHeader);.}.../
232c0 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 *.** For the ent
232d0 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 ry that cursor p
232e0 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c Cur is point to,
232f0 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 return as.** ma
23300 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 ny bytes of the
23310 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 20 61 key or data as a
23320 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 re available on
23330 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 the local.** b-t
23340 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 74 65 ree page. Write
23350 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 the number of a
23360 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 69 vailable bytes i
23370 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a nto *pAmt..**.**
23380 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 The pointer ret
23390 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65 72 urned is ephemer
233a0 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61 74 al. The key/dat
233b0 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 a may move.** or
233c0 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e be destroyed on
233d0 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 the next call t
233e0 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74 o any Btree rout
233f0 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e ine,.** includin
23400 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 g calls from oth
23410 65 72 20 74 68 72 65 61 64 73 20 61 67 61 69 6e er threads again
23420 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 st the same cach
23430 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d e..** Hence, a m
23440 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68 utex on the BtSh
23450 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20 68 ared should be h
23460 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c eld prior to cal
23470 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ling.** this rou
23480 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 tine..**.** Thes
23490 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 75 73 e routines is us
234a0 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 6b 20 ed to get quick
234b0 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 61 6e access to key an
234c0 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 d data.** in the
234d0 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 common case whe
234e0 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 re no overflow p
234f0 61 67 65 73 20 61 72 65 20 75 73 65 64 2e 0a 2a ages are used..*
23500 2f 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 /.const void *sq
23510 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 lite3BtreeKeyFet
23520 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ch(BtCursor *pCu
23530 72 2c 20 75 33 32 20 2a 70 41 6d 74 29 7b 0a 20 r, u32 *pAmt){.
23540 20 72 65 74 75 72 6e 20 66 65 74 63 68 50 61 79 return fetchPay
23550 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 29 load(pCur, pAmt)
23560 3b 0a 7d 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a ;.}.const void *
23570 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
23580 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a Fetch(BtCursor *
23590 70 43 75 72 2c 20 75 33 32 20 2a 70 41 6d 74 29 pCur, u32 *pAmt)
235a0 7b 0a 20 20 72 65 74 75 72 6e 20 66 65 74 63 68 {. return fetch
235b0 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 Payload(pCur, pA
235c0 6d 74 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d mt);.}.../*.** M
235d0 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 ove the cursor d
235e0 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 68 69 own to a new chi
235f0 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 6e 65 ld page. The ne
23600 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 69 wPgno argument i
23610 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 s the.** page nu
23620 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c mber of the chil
23630 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 d page to move t
23640 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 o..**.** This fu
23650 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 nction returns S
23660 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 QLITE_CORRUPT if
23670 20 74 68 65 20 70 61 67 65 2d 68 65 61 64 65 72 the page-header
23680 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 0a flags field of.
23690 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 ** the new child
236a0 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 20 6d page does not m
236b0 61 74 63 68 20 74 68 65 20 66 6c 61 67 73 20 66 atch the flags f
236c0 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61 72 65 ield of the pare
236d0 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66 20 61 nt (i.e..** if a
236e0 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20 61 70 n intkey page ap
236f0 70 65 61 72 73 20 74 6f 20 62 65 20 74 68 65 20 pears to be the
23700 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f 6e 2d parent of a non-
23710 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f 72 0a intkey page, or.
23720 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29 2e 0a ** vice-versa)..
23730 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
23740 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72 73 veToChild(BtCurs
23750 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e 65 or *pCur, u32 ne
23760 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 wPgno){. int rc
23770 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75 72 ;. int i = pCur
23780 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50 61 ->iPage;. MemPa
23790 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 20 ge *pNewPage;.
237a0 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
237b0 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 pCur->pBt;.. as
237c0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
237d0 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
237e0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
237f0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
23800 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
23810 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 54 ( pCur->iPage<BT
23820 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 CURSOR_MAX_DEPTH
23830 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
23840 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 29 3b 0a ur->iPage>=0 );.
23850 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
23860 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58 e>=(BTCURSOR_MAX
23870 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20 _DEPTH-1) ){.
23880 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
23890 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
238a0 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e . rc = getAndIn
238b0 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50 itPage(pBt, newP
238c0 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65 2c 0a gno, &pNewPage,.
238d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
238e0 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
238f0 20 42 54 43 46 5f 57 72 69 74 65 46 6c 61 67 29 BTCF_WriteFlag)
23900 3d 3d 30 20 3f 20 50 41 47 45 52 5f 47 45 54 5f ==0 ? PAGER_GET_
23910 52 45 41 44 4f 4e 4c 59 20 3a 20 30 29 3b 0a 20 READONLY : 0);.
23920 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
23930 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 rc;. pCur->apP
23940 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 age[i+1] = pNewP
23950 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 age;. pCur->aiI
23960 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 dx[i+1] = 0;. p
23970 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20 Cur->iPage++;..
23980 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
23990 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 e = 0;. pCur->c
239a0 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 urFlags &= ~(BTC
239b0 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 F_ValidNKey|BTCF
239c0 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 69 _ValidOvfl);. i
239d0 66 28 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 65 f( pNewPage->nCe
239e0 6c 6c 3c 31 20 7c 7c 20 70 4e 65 77 50 61 67 65 ll<1 || pNewPage
239f0 2d 3e 69 6e 74 4b 65 79 21 3d 70 43 75 72 2d 3e ->intKey!=pCur->
23a00 61 70 50 61 67 65 5b 69 5d 2d 3e 69 6e 74 4b 65 apPage[i]->intKe
23a10 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 y ){. return
23a20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
23a30 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 KPT;. }. retur
23a40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
23a50 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 50 61 67 65 #if 0./*.** Page
23a60 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20 69 pParent is an i
23a70 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 nternal (non-lea
23a80 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 54 68 f) tree page. Th
23a90 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 is function .**
23aa0 61 73 73 65 72 74 73 20 74 68 61 74 20 70 61 67 asserts that pag
23ab0 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 e number iChild
23ac0 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69 6c is the left-chil
23ad0 64 20 69 66 20 74 68 65 20 69 49 64 78 27 74 68 d if the iIdx'th
23ae0 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67 65 .** cell in page
23af0 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66 pParent. Or, if
23b00 20 69 49 64 78 20 69 73 20 65 71 75 61 6c 20 74 iIdx is equal t
23b10 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 o the total numb
23b20 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 er of.** cells i
23b30 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74 20 n pParent, that
23b40 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 page number iChi
23b50 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 74 2d ld is the right-
23b60 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 child of.** the
23b70 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 page..*/.static
23b80 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 65 6e void assertParen
23b90 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20 2a tIndex(MemPage *
23ba0 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49 64 pParent, int iId
23bb0 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29 7b x, Pgno iChild){
23bc0 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3c . assert( iIdx<
23bd0 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 =pParent->nCell
23be0 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d 70 );. if( iIdx==p
23bf0 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b Parent->nCell ){
23c00 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 . assert( get
23c10 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 4byte(&pParent->
23c20 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 aData[pParent->h
23c30 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69 43 drOffset+8])==iC
23c40 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b hild );. }else{
23c50 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 . assert( get
23c60 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 4byte(findCell(p
23c70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d 3d Parent, iIdx))==
23c80 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a iChild );. }.}.
23c90 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 #else.# define
23ca0 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 assertParentInde
23cb0 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 66 x(x,y,z) .#endif
23cc0 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
23cd0 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74 68 cursor up to th
23ce0 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 2a e parent page..*
23cf0 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20 69 *.** pCur->idx i
23d00 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c s set to the cel
23d10 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f 6e l index that con
23d20 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 tains the pointe
23d30 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 r.** to the page
23d40 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 we are coming f
23d50 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65 20 rom. If we are
23d60 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65 0a coming from the.
23d70 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 ** right-most ch
23d80 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70 43 ild page then pC
23d90 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 ur->idx is set t
23da0 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e 0a o one more than.
23db0 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20 63 ** the largest c
23dc0 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 ell index..*/.st
23dd0 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 65 54 6f atic void moveTo
23de0 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72 20 Parent(BtCursor
23df0 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 *pCur){. assert
23e00 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
23e10 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
23e20 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
23e30 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
23e40 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
23e50 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 ur->iPage>0 );.
23e60 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
23e70 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
23e80 65 5d 20 29 3b 0a 0a 20 20 2f 2a 20 55 50 44 41 e] );.. /* UPDA
23e90 54 45 3a 20 49 74 20 69 73 20 61 63 74 75 61 6c TE: It is actual
23ea0 6c 79 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 ly possible for
23eb0 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 74 65 the condition te
23ec0 73 74 65 64 20 62 79 20 74 68 65 20 61 73 73 65 sted by the asse
23ed0 72 74 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 74 6f rt. ** below to
23ee0 20 62 65 20 75 6e 74 72 75 65 20 69 66 20 74 68 be untrue if th
23ef0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
23f00 69 73 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 is corrupt. This
23f10 20 63 61 6e 20 6f 63 63 75 72 20 69 66 0a 20 20 can occur if.
23f20 2a 2a 20 6f 6e 65 20 63 75 72 73 6f 72 20 68 61 ** one cursor ha
23f30 73 20 6d 6f 64 69 66 69 65 64 20 70 61 67 65 20 s modified page
23f40 70 50 61 72 65 6e 74 20 77 68 69 6c 65 20 61 20 pParent while a
23f50 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 20 reference to it
23f60 69 73 20 68 65 6c 64 20 0a 20 20 2a 2a 20 62 79 is held . ** by
23f70 20 61 20 73 65 63 6f 6e 64 20 63 75 72 73 6f 72 a second cursor
23f80 2e 20 57 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 . Which can only
23f90 20 68 61 70 70 65 6e 20 69 66 20 61 20 73 69 6e happen if a sin
23fa0 67 6c 65 20 70 61 67 65 20 69 73 20 6c 69 6e 6b gle page is link
23fb0 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 6d 6f 72 ed. ** into mor
23fc0 65 20 74 68 61 6e 20 6f 6e 65 20 62 2d 74 72 65 e than one b-tre
23fd0 65 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 61 e structure in a
23fe0 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
23ff0 65 2e 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 61 e. */.#if 0. a
24000 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 ssertParentIndex
24010 28 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 (. pCur->apPa
24020 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 ge[pCur->iPage-1
24030 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 ], . pCur->ai
24040 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d Idx[pCur->iPage-
24050 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 1], . pCur->a
24060 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
24070 65 5d 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 23 65 e]->pgno. );.#e
24080 6e 64 69 66 0a 20 20 74 65 73 74 63 61 73 65 28 ndif. testcase(
24090 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
240a0 72 2d 3e 69 50 61 67 65 2d 31 5d 20 3e 20 70 43 r->iPage-1] > pC
240b0 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
240c0 3e 69 50 61 67 65 2d 31 5d 2d 3e 6e 43 65 6c 6c >iPage-1]->nCell
240d0 20 29 3b 0a 0a 20 20 72 65 6c 65 61 73 65 50 61 );.. releasePa
240e0 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ge(pCur->apPage[
240f0 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 pCur->iPage]);.
24100 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a pCur->iPage--;.
24110 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
24120 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
24130 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 curFlags &= ~(BT
24140 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 CF_ValidNKey|BTC
24150 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 7d 0a F_ValidOvfl);.}.
24160 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
24170 63 75 72 73 6f 72 20 74 6f 20 70 6f 69 6e 74 20 cursor to point
24180 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 to the root page
24190 20 6f 66 20 69 74 73 20 62 2d 74 72 65 65 20 73 of its b-tree s
241a0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
241b0 49 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 If the table has
241c0 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 a virtual root
241d0 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 page, then the c
241e0 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 ursor is moved t
241f0 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 74 68 o point.** to th
24200 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 e virtual root p
24210 61 67 65 20 69 6e 73 74 65 61 64 20 6f 66 20 74 age instead of t
24220 68 65 20 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 he actual root p
24230 61 67 65 2e 20 41 20 74 61 62 6c 65 20 68 61 73 age. A table has
24240 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f a.** virtual ro
24250 6f 74 20 70 61 67 65 20 77 68 65 6e 20 74 68 65 ot page when the
24260 20 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 actual root pag
24270 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 e contains no ce
24280 6c 6c 73 20 61 6e 64 20 61 20 0a 2a 2a 20 73 69 lls and a .** si
24290 6e 67 6c 65 20 63 68 69 6c 64 20 70 61 67 65 2e ngle child page.
242a0 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 This can only h
242b0 61 70 70 65 6e 20 77 69 74 68 20 74 68 65 20 74 appen with the t
242c0 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 70 able rooted at p
242d0 61 67 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 age 1..**.** If
242e0 74 68 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 the b-tree struc
242f0 74 75 72 65 20 69 73 20 65 6d 70 74 79 2c 20 74 ture is empty, t
24300 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 he cursor state
24310 69 73 20 73 65 74 20 74 6f 20 0a 2a 2a 20 43 55 is set to .** CU
24320 52 53 4f 52 5f 49 4e 56 41 4c 49 44 2e 20 4f 74 RSOR_INVALID. Ot
24330 68 65 72 77 69 73 65 2c 20 74 68 65 20 63 75 72 herwise, the cur
24340 73 6f 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f sor is set to po
24350 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 int to the first
24360 0a 2a 2a 20 63 65 6c 6c 20 6c 6f 63 61 74 65 64 .** cell located
24370 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 28 6f 72 on the root (or
24380 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 29 20 70 virtual root) p
24390 61 67 65 20 61 6e 64 20 74 68 65 20 63 75 72 73 age and the curs
243a0 6f 72 20 73 74 61 74 65 0a 2a 2a 20 69 73 20 73 or state.** is s
243b0 65 74 20 74 6f 20 43 55 52 53 4f 52 5f 56 41 4c et to CURSOR_VAL
243c0 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 ID..**.** If thi
243d0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
243e0 6e 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ns successfully,
243f0 20 69 74 20 6d 61 79 20 62 65 20 61 73 73 75 6d it may be assum
24400 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 ed that the.** p
24410 61 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 age-header flags
24420 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 indicate that t
24430 68 65 20 5b 76 69 72 74 75 61 6c 5d 20 72 6f 6f he [virtual] roo
24440 74 2d 70 61 67 65 20 69 73 20 74 68 65 20 65 78 t-page is the ex
24450 70 65 63 74 65 64 20 0a 2a 2a 20 6b 69 6e 64 20 pected .** kind
24460 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 20 28 of b-tree page (
24470 69 2e 65 2e 20 69 66 20 77 68 65 6e 20 6f 70 65 i.e. if when ope
24480 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 ning the cursor
24490 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20 6e the caller did n
244a0 6f 74 0a 2a 2a 20 73 70 65 63 69 66 79 20 61 20 ot.** specify a
244b0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
244c0 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 e the flags byte
244d0 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 35 20 is set to 0x05
244e0 6f 72 20 30 78 30 44 2c 0a 2a 2a 20 69 6e 64 69 or 0x0D,.** indi
244f0 63 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 62 cating a table b
24500 2d 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 -tree, or if the
24510 20 63 61 6c 6c 65 72 20 64 69 64 20 73 70 65 63 caller did spec
24520 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a ify a KeyInfo .*
24530 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 * structure the
24540 66 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 flags byte is se
24550 74 20 74 6f 20 30 78 30 32 20 6f 72 20 30 78 30 t to 0x02 or 0x0
24560 41 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e A, indicating an
24570 20 69 6e 64 65 78 0a 2a 2a 20 62 2d 74 72 65 65 index.** b-tree
24580 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
24590 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 moveToRoot(BtCu
245a0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d rsor *pCur){. M
245b0 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 emPage *pRoot;.
245c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
245d0 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _OK;.. assert(
245e0 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
245f0 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
24600 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c rt( CURSOR_INVAL
24610 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 ID < CURSOR_REQU
24620 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 IRESEEK );. ass
24630 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 ert( CURSOR_VALI
24640 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 D < CURSOR_REQ
24650 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 UIRESEEK );. as
24660 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 sert( CURSOR_FAU
24670 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 LT > CURSOR_RE
24680 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 QUIRESEEK );. i
24690 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e f( pCur->eState>
246a0 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 =CURSOR_REQUIRES
246b0 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 EEK ){. if( p
246c0 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
246d0 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 SOR_FAULT ){.
246e0 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
246f0 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 >skipNext!=SQLIT
24700 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 E_OK );. re
24710 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e turn pCur->skipN
24720 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ext;. }. s
24730 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
24740 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 Cursor(pCur);.
24750 7d 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 }.. if( pCur->i
24760 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 77 Page>=0 ){. w
24770 68 69 6c 65 28 20 70 43 75 72 2d 3e 69 50 61 67 hile( pCur->iPag
24780 65 20 29 20 72 65 6c 65 61 73 65 50 61 67 65 28 e ) releasePage(
24790 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
247a0 72 2d 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 r->iPage--]);.
247b0 7d 65 6c 73 65 20 69 66 28 20 70 43 75 72 2d 3e }else if( pCur->
247c0 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 pgnoRoot==0 ){.
247d0 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
247e0 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
247f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
24800 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
24810 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 . rc = getAnd
24820 49 6e 69 74 50 61 67 65 28 70 43 75 72 2d 3e 70 InitPage(pCur->p
24830 42 74 72 65 65 2d 3e 70 42 74 2c 20 70 43 75 72 Btree->pBt, pCur
24840 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 ->pgnoRoot, &pCu
24850 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2c 0a 20 20 r->apPage[0],.
24860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
24870 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
24880 20 42 54 43 46 5f 57 72 69 74 65 46 6c 61 67 29 BTCF_WriteFlag)
24890 3d 3d 30 20 3f 20 50 41 47 45 52 5f 47 45 54 5f ==0 ? PAGER_GET_
248a0 52 45 41 44 4f 4e 4c 59 20 3a 20 30 29 3b 0a 20 READONLY : 0);.
248b0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
248c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 E_OK ){. pC
248d0 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
248e0 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 SOR_INVALID;.
248f0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
24900 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 }. pCur->iP
24910 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 age = 0;. }. p
24920 52 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 Root = pCur->apP
24930 61 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 age[0];. assert
24940 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 ( pRoot->pgno==p
24950 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b Cur->pgnoRoot );
24960 0a 0a 20 20 2f 2a 20 49 66 20 70 43 75 72 2d 3e .. /* If pCur->
24970 70 4b 65 79 49 6e 66 6f 20 69 73 20 6e 6f 74 20 pKeyInfo is not
24980 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 NULL, then the c
24990 61 6c 6c 65 72 20 74 68 61 74 20 6f 70 65 6e 65 aller that opene
249a0 64 20 74 68 69 73 20 63 75 72 73 6f 72 0a 20 20 d this cursor.
249b0 2a 2a 20 65 78 70 65 63 74 65 64 20 74 6f 20 6f ** expected to o
249c0 70 65 6e 20 69 74 20 6f 6e 20 61 6e 20 69 6e 64 pen it on an ind
249d0 65 78 20 62 2d 74 72 65 65 2e 20 4f 74 68 65 72 ex b-tree. Other
249e0 77 69 73 65 2c 20 69 66 20 70 4b 65 79 49 6e 66 wise, if pKeyInf
249f0 6f 20 69 73 0a 20 20 2a 2a 20 4e 55 4c 4c 2c 20 o is. ** NULL,
24a00 74 68 65 20 63 61 6c 6c 65 72 20 65 78 70 65 63 the caller expec
24a10 74 73 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 ts a table b-tre
24a20 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f e. If this is no
24a30 74 20 74 68 65 20 63 61 73 65 2c 0a 20 20 2a 2a t the case,. **
24a40 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 return an SQLIT
24a50 45 5f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e E_CORRUPT error.
24a60 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 72 6c . **. ** Earl
24a70 69 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ier versions of
24a80 53 51 4c 69 74 65 20 61 73 73 75 6d 65 64 20 74 SQLite assumed t
24a90 68 61 74 20 74 68 69 73 20 74 65 73 74 20 63 6f hat this test co
24aa0 75 6c 64 20 6e 6f 74 20 66 61 69 6c 0a 20 20 2a uld not fail. *
24ab0 2a 20 69 66 20 74 68 65 20 72 6f 6f 74 20 70 61 * if the root pa
24ac0 67 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 6c ge was already l
24ad0 6f 61 64 65 64 20 77 68 65 6e 20 74 68 69 73 20 oaded when this
24ae0 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c function was cal
24af0 6c 65 64 20 28 69 2e 65 2e 0a 20 20 2a 2a 20 69 led (i.e.. ** i
24b00 66 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 f pCur->iPage>=0
24b10 29 2e 20 42 75 74 20 74 68 69 73 20 69 73 20 6e ). But this is n
24b20 6f 74 20 73 6f 20 69 66 20 74 68 65 20 64 61 74 ot so if the dat
24b30 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 abase is corrupt
24b40 65 64 20 0a 20 20 2a 2a 20 69 6e 20 73 75 63 68 ed . ** in such
24b50 20 61 20 77 61 79 20 74 68 61 74 20 70 61 67 65 a way that page
24b60 20 70 52 6f 6f 74 20 69 73 20 6c 69 6e 6b 65 64 pRoot is linked
24b70 20 69 6e 74 6f 20 61 20 73 65 63 6f 6e 64 20 62 into a second b
24b80 2d 74 72 65 65 20 74 61 62 6c 65 20 0a 20 20 2a -tree table . *
24b90 2a 20 28 6f 72 20 74 68 65 20 66 72 65 65 6c 69 * (or the freeli
24ba0 73 74 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 st). */. asser
24bb0 74 28 20 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 t( pRoot->intKey
24bc0 3d 3d 31 20 7c 7c 20 70 52 6f 6f 74 2d 3e 69 6e ==1 || pRoot->in
24bd0 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 tKey==0 );. if(
24be0 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 3d 3d pRoot->isInit==
24bf0 30 20 7c 7c 20 28 70 43 75 72 2d 3e 70 4b 65 79 0 || (pCur->pKey
24c00 49 6e 66 6f 3d 3d 30 29 21 3d 70 52 6f 6f 74 2d Info==0)!=pRoot-
24c10 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 >intKey ){. r
24c20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
24c30 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a RUPT_BKPT;. }..
24c40 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d pCur->aiIdx[0]
24c50 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e = 0;. pCur->in
24c60 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
24c70 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
24c80 3d 20 7e 28 42 54 43 46 5f 41 74 4c 61 73 74 7c = ~(BTCF_AtLast|
24c90 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 BTCF_ValidNKey|B
24ca0 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a TCF_ValidOvfl);.
24cb0 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 . if( pRoot->nC
24cc0 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 ell>0 ){. pCu
24cd0 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
24ce0 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 7d 65 6c 73 OR_VALID;. }els
24cf0 65 20 69 66 28 20 21 70 52 6f 6f 74 2d 3e 6c 65 e if( !pRoot->le
24d00 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 af ){. Pgno s
24d10 75 62 70 61 67 65 3b 0a 20 20 20 20 69 66 28 20 ubpage;. if(
24d20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 pRoot->pgno!=1 )
24d30 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
24d40 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
24d50 20 73 75 62 70 61 67 65 20 3d 20 67 65 74 34 62 subpage = get4b
24d60 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 yte(&pRoot->aDat
24d70 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 a[pRoot->hdrOffs
24d80 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 et+8]);. pCur
24d90 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
24da0 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 R_VALID;. rc
24db0 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
24dc0 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20 ur, subpage);.
24dd0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d }else{. pCur-
24de0 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
24df0 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 7d 0a 20 20 _INVALID;. }.
24e00 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
24e10 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
24e20 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 sor down to the
24e30 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 left-most leaf e
24e40 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
24e50 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77 68 69 .** entry to whi
24e60 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 ch it is current
24e70 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a ly pointing..**.
24e80 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 ** The left-most
24e90 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f 6e 65 leaf is the one
24ea0 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 with the smalle
24eb0 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66 69 72 st key - the fir
24ec0 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69 st.** in ascendi
24ed0 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 ng order..*/.sta
24ee0 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 tic int moveToLe
24ef0 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20 ftmost(BtCursor
24f00 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 *pCur){. Pgno p
24f10 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 gno;. int rc =
24f20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d SQLITE_OK;. Mem
24f30 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 Page *pPage;..
24f40 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
24f50 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
24f60 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
24f70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
24f80 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c _VALID );. whil
24f90 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
24fa0 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 43 && !(pPage = pC
24fb0 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
24fc0 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 >iPage])->leaf )
24fd0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
24fe0 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
24ff0 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
25000 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 ell );. pgno
25010 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 = get4byte(findC
25020 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d ell(pPage, pCur-
25030 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
25040 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 ge]));. rc =
25050 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
25060 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 , pgno);. }. r
25070 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
25080 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
25090 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 or down to the r
250a0 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 ight-most leaf e
250b0 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
250c0 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 69 63 .** page to whic
250d0 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c h it is currentl
250e0 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 y pointing. Not
250f0 69 63 65 20 74 68 65 20 64 69 66 66 65 72 65 6e ice the differen
25100 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f ce.** between mo
25110 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 veToLeftmost() a
25120 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f nd moveToRightmo
25130 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 st(). moveToLef
25140 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 tmost().** finds
25150 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 the left-most e
25160 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
25170 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 61 73 *entry* whereas
25180 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
25190 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 ().** finds the
251a0 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 right-most entry
251b0 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 70 61 beneath the *pa
251c0 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ge*..**.** The r
251d0 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 ight-most entry
251e0 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 is the one with
251f0 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 the largest key
25200 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 - the last.** ke
25210 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f y in ascending o
25220 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rder..*/.static
25230 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d int moveToRightm
25240 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ost(BtCursor *pC
25250 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f ur){. Pgno pgno
25260 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
25270 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 ITE_OK;. MemPag
25280 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 e *pPage = 0;..
25290 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
252a0 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
252b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
252c0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
252d0 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 R_VALID );. whi
252e0 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
252f0 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 K && !(pPage = p
25300 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
25310 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 ->iPage])->leaf
25320 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 ){. pgno = ge
25330 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
25340 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
25350 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 ffset+8]);. p
25360 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
25370 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d >iPage] = pPage-
25380 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d >nCell;. rc =
25390 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
253a0 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 r, pgno);. }.
253b0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
253c0 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 61 K ){. pCur->a
253d0 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
253e0 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ] = pPage->nCell
253f0 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e -1;. pCur->in
25400 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
25410 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 pCur->curFlags
25420 20 26 3d 20 7e 42 54 43 46 5f 56 61 6c 69 64 4e &= ~BTCF_ValidN
25430 4b 65 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Key;. }. retur
25440 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 n rc;.}../* Move
25450 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
25460 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 he first entry i
25470 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 n the table. Re
25480 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a turn SQLITE_OK.*
25490 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 * on success. S
254a0 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 et *pRes to 0 if
254b0 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 the cursor actu
254c0 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 ally points to s
254d0 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 omething.** or s
254e0 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 et *pRes to 1 if
254f0 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
25500 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 pty..*/.int sqli
25510 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42 74 te3BtreeFirst(Bt
25520 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
25530 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
25540 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 rc;.. assert( c
25550 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
25560 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
25570 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
25580 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
25590 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
255a0 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 ;. rc = moveToR
255b0 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 oot(pCur);. if(
255c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
255d0 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e {. if( pCur->
255e0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
255f0 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 NVALID ){.
25600 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 assert( pCur->pg
25610 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 noRoot==0 || pCu
25620 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
25630 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 iPage]->nCell==0
25640 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
25650 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 1;. }else{.
25660 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
25670 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
25680 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 >iPage]->nCell>0
25690 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
256a0 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
256b0 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
256c0 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Cur);. }. }.
256d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
256e0 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 /* Move the curs
256f0 6f 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 or to the last e
25700 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c ntry in the tabl
25710 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 e. Return SQLIT
25720 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 E_OK.** on succe
25730 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 ss. Set *pRes t
25740 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f o 0 if the curso
25750 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 r actually point
25760 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a s to something.*
25770 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 * or set *pRes t
25780 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 o 1 if the table
25790 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e is empty..*/.in
257a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 t sqlite3BtreeLa
257b0 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 st(BtCursor *pCu
257c0 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
257d0 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 61 73 73 int rc;. . ass
257e0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
257f0 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
25800 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
25810 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
25820 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
25830 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 tex) );.. /* If
25840 20 74 68 65 20 63 75 72 73 6f 72 20 61 6c 72 65 the cursor alre
25850 61 64 79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ady points to th
25860 65 20 6c 61 73 74 20 65 6e 74 72 79 2c 20 74 68 e last entry, th
25870 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a is is a no-op. *
25880 2f 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 56 /. if( CURSOR_V
25890 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
258a0 74 65 20 26 26 20 28 70 43 75 72 2d 3e 63 75 72 te && (pCur->cur
258b0 46 6c 61 67 73 20 26 20 42 54 43 46 5f 41 74 4c Flags & BTCF_AtL
258c0 61 73 74 29 21 3d 30 20 29 7b 0a 23 69 66 64 65 ast)!=0 ){.#ifde
258d0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
258e0 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b /* This block
258f0 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 serves to asser
25900 74 28 29 20 74 68 61 74 20 74 68 65 20 63 75 72 t() that the cur
25910 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 sor really does
25920 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f point . ** to
25930 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
25940 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a in the b-tree. *
25950 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 /. int ii;.
25960 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 for(ii=0; ii<p
25970 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b Cur->iPage; ii++
25980 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
25990 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d pCur->aiIdx[ii]
259a0 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 ==pCur->apPage[i
259b0 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 i]->nCell );.
259c0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
259d0 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
259e0 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 >iPage]==pCur->a
259f0 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
25a00 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 e]->nCell-1 );.
25a10 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
25a20 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
25a30 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 age]->leaf );.#e
25a40 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 ndif. return
25a50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
25a60 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
25a70 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
25a80 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
25a90 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
25aa0 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
25ab0 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 tate ){. as
25ac0 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f sert( pCur->pgno
25ad0 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d Root==0 || pCur-
25ae0 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
25af0 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 age]->nCell==0 )
25b00 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
25b10 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
25b20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
25b30 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
25b40 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 _VALID );.
25b50 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
25b60 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 rc = moveToRigh
25b70 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 tmost(pCur);.
25b80 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
25b90 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
25ba0 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c pCur->curFlags |
25bb0 3d 20 42 54 43 46 5f 41 74 4c 61 73 74 3b 0a 20 = BTCF_AtLast;.
25bc0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
25bd0 20 20 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 pCur->curFla
25be0 67 73 20 26 3d 20 7e 42 54 43 46 5f 41 74 4c 61 gs &= ~BTCF_AtLa
25bf0 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0a st;. }. .
25c00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
25c10 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 rn rc;.}../* Mov
25c20 65 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 e the cursor so
25c30 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
25c40 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 o an entry near
25c50 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 65 63 the key .** spec
25c60 69 66 69 65 64 20 62 79 20 70 49 64 78 4b 65 79 ified by pIdxKey
25c70 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52 65 or intKey. Re
25c80 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 turn a success c
25c90 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 ode..**.** For I
25ca0 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 74 68 NTKEY tables, th
25cb0 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d 65 74 e intKey paramet
25cc0 65 72 20 69 73 20 75 73 65 64 2e 20 20 70 49 64 er is used. pId
25cd0 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 62 65 xKey .** must be
25ce0 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 65 NULL. For inde
25cf0 78 20 74 61 62 6c 65 73 2c 20 70 49 64 78 4b 65 x tables, pIdxKe
25d00 79 20 69 73 20 75 73 65 64 20 61 6e 64 20 69 6e y is used and in
25d10 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 tKey.** is ignor
25d20 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
25d30 65 78 61 63 74 20 6d 61 74 63 68 20 69 73 20 6e exact match is n
25d40 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 ot found, then t
25d50 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 he cursor is alw
25d60 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e ays.** left poin
25d70 74 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20 70 ting at a leaf p
25d80 61 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 age which would
25d90 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20 69 hold the entry i
25da0 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 f it.** were pre
25db0 73 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73 6f sent. The curso
25dc0 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f r might point to
25dd0 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 63 an entry that c
25de0 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f omes.** before o
25df0 72 20 61 66 74 65 72 20 74 68 65 20 6b 65 79 2e r after the key.
25e00 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 .**.** An intege
25e10 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 r is written int
25e20 6f 20 2a 70 52 65 73 20 77 68 69 63 68 20 69 73 o *pRes which is
25e30 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a the result of.*
25e40 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 * comparing the
25e50 6b 65 79 20 77 69 74 68 20 74 68 65 20 65 6e 74 key with the ent
25e60 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 ry to which the
25e70 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 70 6f cursor is .** po
25e80 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 inting. The mea
25e90 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 ning of the inte
25ea0 67 65 72 20 77 72 69 74 74 65 6e 20 69 6e 74 6f ger written into
25eb0 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 73 20 .** *pRes is as
25ec0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
25ed0 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20 20 *pRes<0
25ee0 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
25ef0 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 ft pointing at a
25f00 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 n entry that.**
25f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25f20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
25f30 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 20 intKey/pIdxKey
25f40 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 or if the table
25f50 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 is empty.**
25f60 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 and
25f70 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 74 the cursor is t
25f80 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 6f herefore left po
25f90 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a int to nothing..
25fa0 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d **.** *pRes=
25fb0 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73 6f =0 The curso
25fc0 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 r is left pointi
25fd0 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 ng at an entry t
25fe0 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 hat.**
25ff0 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 exactly
26000 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 79 2f 70 matches intKey/p
26010 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 IdxKey..**.**
26020 20 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20 54 *pRes>0 T
26030 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 he cursor is lef
26040 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e t pointing at an
26050 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 entry that.**
26060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26070 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 is larger than i
26080 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a ntKey/pIdxKey..*
26090 2a 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 *.*/.int sqlite3
260a0 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
260b0 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 ked(. BtCursor
260c0 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 *pCur,
260d0 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f /* The cursor to
260e0 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 be moved */. U
260f0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
26100 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 63 IdxKey, /* Unpac
26110 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f ked index key */
26120 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 20 . i64 intKey,
26130 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
26140 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0a he table key */.
26150 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c int biasRight,
26160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
26170 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65 20 true, bias the
26180 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 search to the hi
26190 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 gh end */. int
261a0 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 *pRes
261b0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
261c0 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
261d0 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
261e0 3b 0a 20 20 52 65 63 6f 72 64 43 6f 6d 70 61 72 ;. RecordCompar
261f0 65 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 e xRecordCompare
26200 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
26210 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
26220 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
26230 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
26240 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
26250 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
26260 20 20 61 73 73 65 72 74 28 20 70 52 65 73 20 29 assert( pRes )
26270 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 64 ;. assert( (pId
26280 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 72 2d xKey==0)==(pCur-
26290 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 29 3b >pKeyInfo==0) );
262a0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 .. /* If the cu
262b0 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 rsor is already
262c0 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74 68 positioned at th
262d0 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20 74 e point we are t
262e0 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f rying. ** to mo
262f0 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73 74 ve to, then just
26300 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 return without
26310 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20 2a doing any work *
26320 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 /. if( pCur->eS
26330 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
26340 49 44 20 26 26 20 28 70 43 75 72 2d 3e 63 75 72 ID && (pCur->cur
26350 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c Flags & BTCF_Val
26360 69 64 4e 4b 65 79 29 21 3d 30 0a 20 20 20 26 26 idNKey)!=0. &&
26370 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
26380 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 ->intKey . ){.
26390 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
263a0 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 o.nKey==intKey )
263b0 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 {. *pRes =
263c0 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
263d0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
263e0 0a 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e . if( (pCur->
263f0 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f curFlags & BTCF_
26400 41 74 4c 61 73 74 29 21 3d 30 20 26 26 20 70 43 AtLast)!=0 && pC
26410 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e ur->info.nKey<in
26420 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 tKey ){. *p
26430 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 Res = -1;.
26440 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
26450 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
26460 66 28 20 70 49 64 78 4b 65 79 20 29 7b 0a 20 20 f( pIdxKey ){.
26470 20 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 xRecordCompare
26480 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 = sqlite3VdbeFi
26490 6e 64 43 6f 6d 70 61 72 65 28 70 49 64 78 4b 65 ndCompare(pIdxKe
264a0 79 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 2d y);. pIdxKey-
264b0 3e 69 73 43 6f 72 72 75 70 74 20 3d 20 30 3b 0a >isCorrupt = 0;.
264c0 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 assert( pIdx
264d0 4b 65 79 2d 3e 64 65 66 61 75 6c 74 5f 72 63 3d Key->default_rc=
264e0 3d 31 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 =1 . ||
264f0 70 49 64 78 4b 65 79 2d 3e 64 65 66 61 75 6c 74 pIdxKey->default
26500 5f 72 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 _rc==0 .
26510 20 7c 7c 20 70 49 64 78 4b 65 79 2d 3e 64 65 66 || pIdxKey->def
26520 61 75 6c 74 5f 72 63 3d 3d 2d 31 0a 20 20 20 20 ault_rc==-1.
26530 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
26540 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 20 3d xRecordCompare =
26550 20 30 3b 20 2f 2a 20 41 6c 6c 20 6b 65 79 73 20 0; /* All keys
26560 61 72 65 20 69 6e 74 65 67 65 72 73 20 2a 2f 0a are integers */.
26570 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 }.. rc = move
26580 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
26590 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
265a0 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
265b0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e ssert( pCur->pgn
265c0 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 oRoot==0 || pCur
265d0 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
265e0 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 Page] );. asser
265f0 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t( pCur->pgnoRoo
26600 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 t==0 || pCur->ap
26610 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
26620 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 ]->isInit );. a
26630 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
26640 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
26650 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 LID || pCur->apP
26660 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
26670 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 69 ->nCell>0 );. i
26680 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d f( pCur->eState=
26690 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
266a0 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d ){. *pRes = -
266b0 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 1;. assert( p
266c0 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 Cur->pgnoRoot==0
266d0 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 || pCur->apPage
266e0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
266f0 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 Cell==0 );. r
26700 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
26710 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
26720 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
26730 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 intKey || pIdxKe
26740 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a y );. for(;;){.
26750 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 int lwr, upr
26760 2c 20 69 64 78 2c 20 63 3b 0a 20 20 20 20 50 67 , idx, c;. Pg
26770 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d no chldPg;. M
26780 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
26790 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
267a0 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 75 r->iPage];. u
267b0 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 8 *pCell;
267c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
267d0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
267e0 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e current cell in
267f0 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 2f pPage */.. /
26800 2a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d * pPage->nCell m
26810 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74 ust be greater t
26820 68 61 6e 20 7a 65 72 6f 2e 20 49 66 20 74 68 69 han zero. If thi
26830 73 20 69 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 s is the root-pa
26840 67 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 75 ge. ** the cu
26850 72 73 6f 72 20 77 6f 75 6c 64 20 68 61 76 65 20 rsor would have
26860 62 65 65 6e 20 49 4e 56 41 4c 49 44 20 61 62 6f been INVALID abo
26870 76 65 20 61 6e 64 20 74 68 69 73 20 66 6f 72 28 ve and this for(
26880 3b 3b 29 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 ;;) loop. **
26890 6e 6f 74 20 72 75 6e 2e 20 49 66 20 74 68 69 73 not run. If this
268a0 20 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f 74 is not the root
268b0 2d 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 -page, then the
268c0 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 72 6f moveToChild() ro
268d0 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 77 6f 75 utine. ** wou
268e0 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ld have already
268f0 64 65 74 65 63 74 65 64 20 64 62 20 63 6f 72 72 detected db corr
26900 75 70 74 69 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c uption. Similarl
26910 79 2c 20 70 50 61 67 65 20 6d 75 73 74 0a 20 20 y, pPage must.
26920 20 20 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 ** be the righ
26930 74 20 6b 69 6e 64 20 28 69 6e 64 65 78 20 6f 72 t kind (index or
26940 20 74 61 62 6c 65 29 20 6f 66 20 62 2d 74 72 65 table) of b-tre
26950 65 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 e page. Otherwis
26960 65 0a 20 20 20 20 2a 2a 20 61 20 6d 6f 76 65 54 e. ** a moveT
26970 6f 43 68 69 6c 64 28 29 20 6f 72 20 6d 6f 76 65 oChild() or move
26980 54 6f 52 6f 6f 74 28 29 20 63 61 6c 6c 20 77 6f ToRoot() call wo
26990 75 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 65 uld have detecte
269a0 64 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 2a d corruption. *
269b0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 /. assert( pP
269c0 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a age->nCell>0 );.
269d0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
269e0 65 2d 3e 69 6e 74 4b 65 79 3d 3d 28 70 49 64 78 e->intKey==(pIdx
269f0 4b 65 79 3d 3d 30 29 20 29 3b 0a 20 20 20 20 6c Key==0) );. l
26a00 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20 wr = 0;. upr
26a10 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 = pPage->nCell-1
26a20 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 62 69 ;. assert( bi
26a30 61 73 52 69 67 68 74 3d 3d 30 20 7c 7c 20 62 69 asRight==0 || bi
26a40 61 73 52 69 67 68 74 3d 3d 31 20 29 3b 0a 20 20 asRight==1 );.
26a50 20 20 69 64 78 20 3d 20 75 70 72 3e 3e 28 31 2d idx = upr>>(1-
26a60 62 69 61 73 52 69 67 68 74 29 3b 20 2f 2a 20 69 biasRight); /* i
26a70 64 78 20 3d 20 62 69 61 73 52 69 67 68 74 20 3f dx = biasRight ?
26a80 20 75 70 72 20 3a 20 28 6c 77 72 2b 75 70 72 29 upr : (lwr+upr)
26a90 2f 32 3b 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d /2; */. pCur-
26aa0 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
26ab0 67 65 5d 20 3d 20 28 75 31 36 29 69 64 78 3b 0a ge] = (u16)idx;.
26ac0 20 20 20 20 69 66 28 20 78 52 65 63 6f 72 64 43 if( xRecordC
26ad0 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 ompare==0 ){.
26ae0 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 for(;;){.
26af0 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 i64 nCellKey
26b00 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 ;. pCell
26b10 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
26b20 2c 20 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e , idx) + pPage->
26b30 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
26b40 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d if( pPage-
26b50 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 >hasData ){.
26b60 20 20 20 20 20 20 77 68 69 6c 65 28 20 30 78 38 while( 0x8
26b70 30 20 3c 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 20 0 <= *(pCell++)
26b80 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
26b90 66 28 20 70 43 65 6c 6c 3e 3d 70 50 61 67 65 2d f( pCell>=pPage-
26ba0 3e 61 44 61 74 61 45 6e 64 20 29 20 72 65 74 75 >aDataEnd ) retu
26bb0 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
26bc0 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 T_BKPT;.
26bd0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
26be0 20 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 28 getVarint(
26bf0 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43 pCell, (u64*)&nC
26c00 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 ellKey);.
26c10 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e if( nCellKey<in
26c20 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 tKey ){.
26c30 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 lwr = idx+1;.
26c40 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 77 72 if( lwr
26c50 3e 75 70 72 20 29 7b 20 63 20 3d 20 2d 31 3b 20 >upr ){ c = -1;
26c60 62 72 65 61 6b 3b 20 7d 0a 20 20 20 20 20 20 20 break; }.
26c70 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c 6c }else if( nCell
26c80 4b 65 79 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 Key>intKey ){.
26c90 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 upr = id
26ca0 78 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 x-1;. i
26cb0 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 20 63 20 f( lwr>upr ){ c
26cc0 3d 20 2b 31 3b 20 62 72 65 61 6b 3b 20 7d 0a 20 = +1; break; }.
26cd0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
26ce0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
26cf0 6e 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 nCellKey==intKey
26d00 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 );. pC
26d10 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 ur->curFlags |=
26d20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 3b 0a BTCF_ValidNKey;.
26d30 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e pCur->
26d40 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c info.nKey = nCel
26d50 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 lKey;.
26d60 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
26d70 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 ->iPage] = (u16)
26d80 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 69 idx;. i
26d90 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
26da0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c ){. l
26db0 77 72 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 wr = idx;.
26dc0 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
26dd0 6f 5f 6e 65 78 74 5f 6c 61 79 65 72 3b 0a 20 20 o_next_layer;.
26de0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
26df0 20 20 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 *pRes
26e00 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
26e10 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
26e20 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f ;. go
26e30 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 to moveto_finish
26e40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
26e50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
26e60 61 73 73 65 72 74 28 20 6c 77 72 2b 75 70 72 3e assert( lwr+upr>
26e70 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 64 =0 );. id
26e80 78 20 3d 20 28 6c 77 72 2b 75 70 72 29 3e 3e 31 x = (lwr+upr)>>1
26e90 3b 20 20 2f 2a 20 69 64 78 20 3d 20 28 6c 77 72 ; /* idx = (lwr
26ea0 2b 75 70 72 29 2f 32 3b 20 2a 2f 0a 20 20 20 20 +upr)/2; */.
26eb0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
26ec0 20 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 for(;;){.
26ed0 20 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b int nCell;
26ee0 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d . pCell =
26ef0 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
26f00 20 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 idx) + pPage->c
26f10 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 0a 20 20 hildPtrSize;..
26f20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 /* The max
26f30 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 imum supported p
26f40 61 67 65 2d 73 69 7a 65 20 69 73 20 36 35 35 33 age-size is 6553
26f50 36 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d 65 6 bytes. This me
26f60 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 ans that.
26f70 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 ** the maximum
26f80 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 number of record
26f90 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f 6e bytes stored on
26fa0 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65 65 an index B-Tree
26fb0 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 . ** page
26fc0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31 36 is less than 16
26fd0 33 38 34 20 62 79 74 65 73 20 61 6e 64 20 6d 61 384 bytes and ma
26fe0 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61 y be stored as a
26ff0 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20 20 2-byte.
27000 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73 20 ** varint. This
27010 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 information is u
27020 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74 sed to attempt t
27030 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67 20 o avoid parsing
27040 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
27050 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20 63 entire cell by c
27060 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 20 hecking for the
27070 63 61 73 65 73 20 77 68 65 72 65 20 74 68 65 20 cases where the
27080 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20 20 record is .
27090 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e 74 ** stored ent
270a0 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68 65 irely within the
270b0 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 79 20 b-tree page by
270c0 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 inspecting the f
270d0 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a 2a irst . **
270e0 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 2 bytes of the
270f0 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f cell.. */
27100 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d . nCell =
27110 20 70 43 65 6c 6c 5b 30 5d 3b 0a 20 20 20 20 20 pCell[0];.
27120 20 20 20 69 66 28 20 6e 43 65 6c 6c 3c 3d 70 50 if( nCell<=pP
27130 61 67 65 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 age->max1bytePay
27140 6c 6f 61 64 20 29 7b 0a 20 20 20 20 20 20 20 20 load ){.
27150 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
27160 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 63 runs if the rec
27170 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f ord-size field o
27180 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 0a f the cell is a.
27190 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e ** sin
271a0 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 20 gle byte varint
271b0 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 66 and the record f
271c0 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 its entirely on
271d0 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 20 the main.
271e0 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 ** b-tree pag
271f0 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 e. */.
27200 20 74 65 73 74 63 61 73 65 28 20 70 43 65 6c 6c testcase( pCell
27210 2b 6e 43 65 6c 6c 2b 31 3d 3d 70 50 61 67 65 2d +nCell+1==pPage-
27220 3e 61 44 61 74 61 45 6e 64 20 29 3b 0a 20 20 20 >aDataEnd );.
27230 20 20 20 20 20 20 20 63 20 3d 20 78 52 65 63 6f c = xReco
27240 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c rdCompare(nCell,
27250 20 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 31 (void*)&pCell[1
27260 5d 2c 20 70 49 64 78 4b 65 79 2c 20 30 29 3b 0a ], pIdxKey, 0);.
27270 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
27280 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20 26 20 30 ( !(pCell[1] & 0
27290 78 38 30 29 20 0a 20 20 20 20 20 20 20 20 20 20 x80) .
272a0 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28 28 6e 43 && (nCell = ((nC
272b0 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29 20 2b 20 ell&0x7f)<<7) +
272c0 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50 61 67 65 pCell[1])<=pPage
272d0 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20 20 20 20 ->maxLocal.
272e0 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ){.
272f0 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 2d 73 69 /* The record-si
27300 7a 65 20 66 69 65 6c 64 20 69 73 20 61 20 32 20 ze field is a 2
27310 62 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64 20 byte varint and
27320 74 68 65 20 72 65 63 6f 72 64 20 0a 20 20 20 20 the record .
27330 20 20 20 20 20 20 2a 2a 20 66 69 74 73 20 65 6e ** fits en
27340 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d 61 tirely on the ma
27350 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 in b-tree page.
27360 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74 65 */. te
27370 73 74 63 61 73 65 28 20 70 43 65 6c 6c 2b 6e 43 stcase( pCell+nC
27380 65 6c 6c 2b 32 3d 3d 70 50 61 67 65 2d 3e 61 44 ell+2==pPage->aD
27390 61 74 61 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 ataEnd );.
273a0 20 20 20 20 63 20 3d 20 78 52 65 63 6f 72 64 43 c = xRecordC
273b0 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 ompare(nCell, (v
273c0 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 32 5d 2c 20 oid*)&pCell[2],
273d0 70 49 64 78 4b 65 79 2c 20 30 29 3b 0a 20 20 20 pIdxKey, 0);.
273e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
273f0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 /* The rec
27400 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65 72 20 6f ord flows over o
27410 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 nto one or more
27420 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
27430 49 6e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 In. **
27440 74 68 69 73 20 63 61 73 65 20 74 68 65 20 77 68 this case the wh
27450 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64 73 20 74 ole cell needs t
27460 6f 20 62 65 20 70 61 72 73 65 64 2c 20 61 20 62 o be parsed, a b
27470 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a uffer allocated.
27480 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 ** and
27490 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 accessPayload()
274a0 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 used to retriev
274b0 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 74 e the record int
274c0 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 o the.
274d0 2a 2a 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 ** buffer before
274e0 20 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 VdbeRecordCompa
274f0 72 65 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c re() can be call
27500 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 ed. */.
27510 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b void *pCellKey;
27520 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 20 . u8 *
27530 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f 64 79 20 const pCellBody
27540 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61 67 65 2d = pCell - pPage-
27550 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 >childPtrSize;.
27560 20 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61 btreePa
27570 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
27580 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 26 70 43 , pCellBody, &pC
27590 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 ur->info);.
275a0 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 28 69 6e nCell = (in
275b0 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 t)pCur->info.nKe
275c0 79 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 y;. pCe
275d0 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d llKey = sqlite3M
275e0 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20 29 3b 0a alloc( nCell );.
275f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 if( pC
27600 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 ellKey==0 ){.
27610 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
27620 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
27630 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 goto mov
27640 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
27650 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
27660 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
27670 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 ur->iPage] = (u1
27680 36 29 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 6)idx;.
27690 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c rc = accessPayl
276a0 6f 61 64 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 oad(pCur, 0, nCe
276b0 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 ll, (unsigned ch
276c0 61 72 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 32 29 ar*)pCellKey, 2)
276d0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
276e0 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 rc ){.
276f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
27700 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 CellKey);.
27710 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
27720 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 o_finish;.
27730 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
27740 63 20 3d 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 c = xRecordCompa
27750 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b re(nCell, pCellK
27760 65 79 2c 20 70 49 64 78 4b 65 79 2c 20 30 29 3b ey, pIdxKey, 0);
27770 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
27780 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 e3_free(pCellKey
27790 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
277a0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 assert( pId
277b0 78 4b 65 79 2d 3e 69 73 43 6f 72 72 75 70 74 3d xKey->isCorrupt=
277c0 3d 30 20 7c 7c 20 63 3d 3d 30 20 29 3b 0a 20 20 =0 || c==0 );.
277d0 20 20 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b if( c<0 ){
277e0 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d . lwr =
277f0 20 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 20 20 idx+1;.
27800 7d 65 6c 73 65 20 69 66 28 20 63 3e 30 20 29 7b }else if( c>0 ){
27810 0a 20 20 20 20 20 20 20 20 20 20 75 70 72 20 3d . upr =
27820 20 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 20 20 idx-1;.
27830 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
27840 20 61 73 73 65 72 74 28 20 63 3d 3d 30 20 29 3b assert( c==0 );
27850 0a 20 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 . *pRes
27860 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
27870 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
27880 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e pCur->
27890 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
278a0 65 5d 20 3d 20 28 75 31 36 29 69 64 78 3b 0a 20 e] = (u16)idx;.
278b0 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 if( pId
278c0 78 4b 65 79 2d 3e 69 73 43 6f 72 72 75 70 74 20 xKey->isCorrupt
278d0 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f ) rc = SQLITE_CO
278e0 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20 RRUPT;.
278f0 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
27900 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ish;. }.
27910 20 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 if( lwr>u
27920 70 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 pr ) break;.
27930 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 2b assert( lwr+
27940 75 70 72 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 upr>=0 );.
27950 20 20 69 64 78 20 3d 20 28 6c 77 72 2b 75 70 72 idx = (lwr+upr
27960 29 3e 3e 31 3b 20 20 2f 2a 20 69 64 78 20 3d 20 )>>1; /* idx =
27970 28 6c 77 72 2b 75 70 72 29 2f 32 20 2a 2f 0a 20 (lwr+upr)/2 */.
27980 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
27990 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70 assert( lwr==up
279a0 72 2b 31 20 7c 7c 20 28 70 50 61 67 65 2d 3e 69 r+1 || (pPage->i
279b0 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d ntKey && !pPage-
279c0 3e 6c 65 61 66 29 20 29 3b 0a 20 20 20 20 61 73 >leaf) );. as
279d0 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
279e0 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 nit );. if( p
279f0 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
27a00 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
27a10 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
27a20 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 age]<pCur->apPag
27a30 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
27a40 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 20 70 nCell );. p
27a50 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
27a60 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 69 >iPage] = (u16)i
27a70 64 78 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 dx;. *pRes
27a80 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = c;. rc =
27a90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
27aa0 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
27ab0 69 73 68 3b 0a 20 20 20 20 7d 0a 6d 6f 76 65 74 ish;. }.movet
27ac0 6f 5f 6e 65 78 74 5f 6c 61 79 65 72 3a 0a 20 20 o_next_layer:.
27ad0 20 20 69 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 if( lwr>=pPage
27ae0 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 ->nCell ){.
27af0 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62 79 chldPg = get4by
27b00 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
27b10 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
27b20 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 t+8]);. }else
27b30 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d {. chldPg =
27b40 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 get4byte(findCe
27b50 6c 6c 28 70 50 61 67 65 2c 20 6c 77 72 29 29 3b ll(pPage, lwr));
27b60 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d . }. pCur-
27b70 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
27b80 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72 3b 0a ge] = (u16)lwr;.
27b90 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
27ba0 68 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 hild(pCur, chldP
27bb0 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 g);. if( rc )
27bc0 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 6d 6f 76 65 break;. }.move
27bd0 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 70 43 75 to_finish:. pCu
27be0 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
27bf0 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 0;. pCur->curFl
27c00 61 67 73 20 26 3d 20 7e 28 42 54 43 46 5f 56 61 ags &= ~(BTCF_Va
27c10 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 6c lidNKey|BTCF_Val
27c20 69 64 4f 76 66 6c 29 3b 0a 20 20 72 65 74 75 72 idOvfl);. retur
27c30 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
27c40 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
27c50 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 he cursor is not
27c60 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 pointing at an
27c70 65 6e 74 72 79 20 6f 66 20 74 68 65 20 74 61 62 entry of the tab
27c80 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 45 20 77 le..**.** TRUE w
27c90 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 ill be returned
27ca0 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 after a call to
27cb0 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
27cc0 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70 61 73 74 () moves.** past
27cd0 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
27ce0 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 in the table or
27cf0 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
27d00 28 29 20 6d 6f 76 65 73 20 70 61 73 74 0a 2a 2a () moves past.**
27d10 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
27d20 2e 20 20 54 52 55 45 20 69 73 20 61 6c 73 6f 20 . TRUE is also
27d30 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
27d40 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a table is empty..
27d50 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 */.int sqlite3Bt
27d60 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f 72 20 reeEof(BtCursor
27d70 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 *pCur){. /* TOD
27d80 4f 3a 20 57 68 61 74 20 69 66 20 74 68 65 20 63 O: What if the c
27d90 75 72 73 6f 72 20 69 73 20 69 6e 20 43 55 52 53 ursor is in CURS
27da0 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 62 OR_REQUIRESEEK b
27db0 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 65 6e 74 ut all table ent
27dc0 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 65 20 62 ries. ** have b
27dd0 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 54 68 69 een deleted? Thi
27de0 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 65 64 20 s API will need
27df0 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 72 65 74 to change to ret
27e00 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 urn an error cod
27e10 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c 20 61 e. ** as well a
27e20 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 72 65 s the boolean re
27e30 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20 2a 2f sult value.. */
27e40 0a 20 20 72 65 74 75 72 6e 20 28 43 55 52 53 4f . return (CURSO
27e50 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d 3e 65 R_VALID!=pCur->e
27e60 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a State);.}../*.**
27e70 20 41 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 Advance the cur
27e80 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 sor to the next
27e90 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 entry in the dat
27ea0 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 abase. If.** su
27eb0 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 ccessful then se
27ec0 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 t *pRes=0. If t
27ed0 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 he cursor.** was
27ee0 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e already pointin
27ef0 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e g to the last en
27f00 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
27f10 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 ase before.** th
27f20 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 is routine was c
27f30 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 alled, then set
27f40 2a 70 52 65 73 3d 31 2e 0a 2a 2a 0a 2a 2a 20 54 *pRes=1..**.** T
27f50 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
27f60 69 6f 6e 20 77 69 6c 6c 20 73 65 74 20 2a 70 52 ion will set *pR
27f70 65 73 20 74 6f 20 30 20 6f 72 20 31 2e 20 20 54 es to 0 or 1. T
27f80 68 65 20 69 6e 69 74 69 61 6c 20 2a 70 52 65 73 he initial *pRes
27f90 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 value.** will b
27fa0 65 20 31 20 69 66 20 74 68 65 20 63 75 72 73 6f e 1 if the curso
27fb0 72 20 62 65 69 6e 67 20 73 74 65 70 70 65 64 20 r being stepped
27fc0 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 corresponds to a
27fd0 6e 20 53 51 4c 20 69 6e 64 65 78 20 61 6e 64 0a n SQL index and.
27fe0 2a 2a 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 ** if this routi
27ff0 6e 65 20 63 6f 75 6c 64 20 68 61 76 65 20 62 65 ne could have be
28000 65 6e 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 en skipped if th
28010 61 74 20 53 51 4c 20 69 6e 64 65 78 20 68 61 64 at SQL index had
28020 20 62 65 65 6e 0a 2a 2a 20 61 20 75 6e 69 71 75 been.** a uniqu
28030 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 e index. Otherw
28040 69 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 77 ise the caller w
28050 69 6c 6c 20 68 61 76 65 20 73 65 74 20 2a 70 52 ill have set *pR
28060 65 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 5a es to zero..** Z
28070 65 72 6f 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f ero is the commo
28080 6e 20 63 61 73 65 2e 20 54 68 65 20 62 74 72 65 n case. The btre
28090 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
280a0 20 69 73 20 66 72 65 65 20 74 6f 20 75 73 65 20 is free to use
280b0 74 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 20 2a the.** initial *
280c0 70 52 65 73 20 76 61 6c 75 65 20 61 73 20 61 20 pRes value as a
280d0 68 69 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 hint to improve
280e0 70 65 72 66 6f 72 6d 61 6e 63 65 2c 20 62 75 74 performance, but
280f0 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
28100 53 51 4c 69 74 65 20 62 74 72 65 65 20 69 6d 70 SQLite btree imp
28110 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
28120 20 6e 6f 74 2e 20 28 4e 6f 74 65 20 74 68 61 74 not. (Note that
28130 20 74 68 65 20 63 6f 6d 64 62 32 20 62 74 72 65 the comdb2 btre
28140 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
28150 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 69 ion does use thi
28160 73 20 68 69 6e 74 2c 20 68 6f 77 65 76 65 72 2e s hint, however.
28170 29 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ).*/.int sqlite3
28180 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 BtreeNext(BtCurs
28190 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
281a0 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
281b0 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d int idx;. Mem
281c0 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 Page *pPage;..
281d0 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
281e0 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
281f0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73 ;. assert( pRes
28200 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
28210 20 2a 70 52 65 73 3d 3d 30 20 7c 7c 20 2a 70 52 *pRes==0 || *pR
28220 65 73 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 es==1 );. asser
28230 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 t( pCur->skipNex
28240 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 t==0 || pCur->eS
28250 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
28260 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 ID );. if( pCur
28270 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
28280 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 69 6e _VALID ){. in
28290 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
282a0 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 20 Cache(pCur);.
282b0 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
282c0 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 sorPosition(pCur
282d0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
282e0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
282f0 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
28300 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
28310 20 20 7d 0a 20 20 20 20 69 66 28 20 43 55 52 53 }. if( CURS
28320 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 OR_INVALID==pCur
28330 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 ->eState ){.
28340 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 *pRes = 1;.
28350 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
28360 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 _OK;. }. i
28370 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 f( pCur->skipNex
28380 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 t ){. asser
28390 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
283a0 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c =CURSOR_VALID ||
283b0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
283c0 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 URSOR_SKIPNEXT )
283d0 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 ;. pCur->eS
283e0 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 tate = CURSOR_VA
283f0 4c 49 44 3b 0a 20 20 20 20 20 20 69 66 28 20 70 LID;. if( p
28400 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3e 30 20 Cur->skipNext>0
28410 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d ){. pCur-
28420 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 >skipNext = 0;.
28430 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 *pRes = 0
28440 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
28450 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
28460 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e }. pCur->
28470 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 skipNext = 0;.
28480 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 }. }.. pPage
28490 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
284a0 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
284b0 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 idx = ++pCur->ai
284c0 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
284d0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
284e0 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 e->isInit );..
284f0 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
28500 73 65 20 66 69 6c 65 20 69 73 20 63 6f 72 72 75 se file is corru
28510 70 74 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 pt, it is possib
28520 6c 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 le for the value
28530 20 6f 66 20 69 64 78 20 0a 20 20 2a 2a 20 74 6f of idx . ** to
28540 20 62 65 20 69 6e 76 61 6c 69 64 20 68 65 72 65 be invalid here
28550 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 . This can only
28560 6f 63 63 75 72 20 69 66 20 61 20 73 65 63 6f 6e occur if a secon
28570 64 20 63 75 72 73 6f 72 20 6d 6f 64 69 66 69 65 d cursor modifie
28580 73 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 s. ** the page
28590 77 68 69 6c 65 20 63 75 72 73 6f 72 20 70 43 75 while cursor pCu
285a0 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 r is holding a r
285b0 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 20 eference to it.
285c0 57 68 69 63 68 20 63 61 6e 0a 20 20 2a 2a 20 6f Which can. ** o
285d0 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68 nly happen if th
285e0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f e database is co
285f0 72 72 75 70 74 20 69 6e 20 73 75 63 68 20 61 20 rrupt in such a
28600 77 61 79 20 61 73 20 74 6f 20 6c 69 6e 6b 20 74 way as to link t
28610 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 74 he. ** page int
28620 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 o more than one
28630 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 b-tree structure
28640 2e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 . */. testcase(
28650 20 69 64 78 3e 70 50 61 67 65 2d 3e 6e 43 65 6c idx>pPage->nCel
28660 6c 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e l );.. pCur->in
28670 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
28680 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
28690 3d 20 7e 28 42 54 43 46 5f 56 61 6c 69 64 4e 4b = ~(BTCF_ValidNK
286a0 65 79 7c 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 ey|BTCF_ValidOvf
286b0 6c 29 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 l);. if( idx>=p
286c0 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 Page->nCell ){.
286d0 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
286e0 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 eaf ){. rc
286f0 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
28700 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 ur, get4byte(&pP
28710 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
28720 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 ->hdrOffset+8]))
28730 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
28740 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 {. *pRes
28750 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 = 0;. ret
28760 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
28770 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
28780 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b oLeftmost(pCur);
28790 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 . *pRes = 0
287a0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
287b0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b c;. }. do{
287c0 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d . if( pCur-
287d0 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 >iPage==0 ){.
287e0 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a *pRes = 1;.
287f0 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 pCur->eS
28800 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
28810 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 72 VALID;. r
28820 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
28830 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
28840 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 oveToParent(pCur
28850 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d );. pPage =
28860 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
28870 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
28880 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 }while( pCur->ai
28890 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
288a0 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 >=pPage->nCell )
288b0 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b ;. *pRes = 0;
288c0 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
288d0 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
288e0 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
288f0 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 65 73 eNext(pCur, pRes
28900 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
28910 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
28920 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 OK;. }. re
28930 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a turn rc;. }. *
28940 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 pRes = 0;. if(
28950 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
28960 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
28970 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 _OK;. }. rc =
28980 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
28990 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 Cur);. return r
289a0 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 c;.}.../*.** Ste
289b0 70 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 p the cursor to
289c0 74 68 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 the back to the
289d0 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 previous entry i
289e0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
289f0 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 If.** successfu
28a00 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 l then set *pRes
28a10 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 =0. If the curs
28a20 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 or.** was alread
28a30 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 y pointing to th
28a40 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e e first entry in
28a50 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 the database be
28a60 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 fore.** this rou
28a70 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c tine was called,
28a80 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d then set *pRes=
28a90 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 1..**.** The cal
28aa0 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 77 69 ling function wi
28ab0 6c 6c 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 ll set *pRes to
28ac0 30 20 6f 72 20 31 2e 20 20 54 68 65 20 69 6e 69 0 or 1. The ini
28ad0 74 69 61 6c 20 2a 70 52 65 73 20 76 61 6c 75 65 tial *pRes value
28ae0 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 31 20 69 66 .** will be 1 if
28af0 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 69 6e the cursor bein
28b00 67 20 73 74 65 70 70 65 64 20 63 6f 72 72 65 73 g stepped corres
28b10 70 6f 6e 64 73 20 74 6f 20 61 6e 20 53 51 4c 20 ponds to an SQL
28b20 69 6e 64 65 78 20 61 6e 64 0a 2a 2a 20 69 66 20 index and.** if
28b30 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 75 this routine cou
28b40 6c 64 20 68 61 76 65 20 62 65 65 6e 20 73 6b 69 ld have been ski
28b50 70 70 65 64 20 69 66 20 74 68 61 74 20 53 51 4c pped if that SQL
28b60 20 69 6e 64 65 78 20 68 61 64 20 62 65 65 6e 0a index had been.
28b70 2a 2a 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 ** a unique inde
28b80 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68 x. Otherwise th
28b90 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 68 61 e caller will ha
28ba0 76 65 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 ve set *pRes to
28bb0 7a 65 72 6f 2e 0a 2a 2a 20 5a 65 72 6f 20 69 73 zero..** Zero is
28bc0 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 the common case
28bd0 2e 20 54 68 65 20 62 74 72 65 65 20 69 6d 70 6c . The btree impl
28be0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 66 72 ementation is fr
28bf0 65 65 20 74 6f 20 75 73 65 20 74 68 65 0a 2a 2a ee to use the.**
28c00 20 69 6e 69 74 69 61 6c 20 2a 70 52 65 73 20 76 initial *pRes v
28c10 61 6c 75 65 20 61 73 20 61 20 68 69 6e 74 20 74 alue as a hint t
28c20 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 o improve perfor
28c30 6d 61 6e 63 65 2c 20 62 75 74 20 74 68 65 20 63 mance, but the c
28c40 75 72 72 65 6e 74 0a 2a 2a 20 53 51 4c 69 74 65 urrent.** SQLite
28c50 20 62 74 72 65 65 20 69 6d 70 6c 65 6d 65 6e 74 btree implement
28c60 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 2e 20 ation does not.
28c70 28 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 (Note that the c
28c80 6f 6d 64 62 32 20 62 74 72 65 65 0a 2a 2a 20 69 omdb2 btree.** i
28c90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f mplementation do
28ca0 65 73 20 75 73 65 20 74 68 69 73 20 68 69 6e 74 es use this hint
28cb0 2c 20 68 6f 77 65 76 65 72 2e 29 0a 2a 2f 0a 69 , however.).*/.i
28cc0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
28cd0 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 revious(BtCursor
28ce0 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 *pCur, int *pRe
28cf0 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
28d00 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a MemPage *pPage;.
28d10 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
28d20 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
28d30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
28d40 52 65 73 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 Res!=0 );. asse
28d50 72 74 28 20 2a 70 52 65 73 3d 3d 30 20 7c 7c 20 rt( *pRes==0 ||
28d60 2a 70 52 65 73 3d 3d 31 20 29 3b 0a 20 20 61 73 *pRes==1 );. as
28d70 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 sert( pCur->skip
28d80 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d Next==0 || pCur-
28d90 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
28da0 56 41 4c 49 44 20 29 3b 0a 20 20 70 43 75 72 2d VALID );. pCur-
28db0 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 >curFlags &= ~(B
28dc0 54 43 46 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f TCF_AtLast|BTCF_
28dd0 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 69 66 ValidOvfl);. if
28de0 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d ( pCur->eState!=
28df0 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a CURSOR_VALID ){.
28e00 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 if( ALWAYS(p
28e10 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 Cur->eState>=CUR
28e20 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 29 SOR_REQUIRESEEK)
28e30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 ){. rc = b
28e40 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f treeRestoreCurso
28e50 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
28e60 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
28e70 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
28e80 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
28e90 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
28ea0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
28eb0 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
28ec0 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
28ed0 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 2a 70 tate ){. *p
28ee0 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 Res = 1;. r
28ef0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
28f00 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
28f10 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 29 7b Cur->skipNext ){
28f20 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
28f30 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
28f40 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 SOR_VALID || pCu
28f50 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
28f60 52 5f 53 4b 49 50 4e 45 58 54 20 29 3b 0a 20 20 R_SKIPNEXT );.
28f70 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
28f80 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b = CURSOR_VALID;
28f90 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d . if( pCur-
28fa0 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b 0a 20 >skipNext<0 ){.
28fb0 20 20 20 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 pCur->ski
28fc0 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 pNext = 0;.
28fd0 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
28fe0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
28ff0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a ITE_OK;. }.
29000 20 20 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 pCur->skip
29010 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Next = 0;. }.
29020 20 20 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 }.. pPage = p
29030 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
29040 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 ->iPage];. asse
29050 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
29060 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 t );. if( !pPag
29070 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 e->leaf ){. i
29080 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 nt idx = pCur->a
29090 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
290a0 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 ];. rc = move
290b0 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 ToChild(pCur, ge
290c0 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
290d0 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 pPage, idx)));.
290e0 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
290f0 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
29100 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
29110 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f }. rc = mo
29120 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 veToRightmost(pC
29130 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ur);. }else{.
29140 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 while( pCur->a
29150 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
29160 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 ]==0 ){. if
29170 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 ( pCur->iPage==0
29180 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 ){. pCur
29190 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
291a0 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 R_INVALID;.
291b0 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
291c0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
291d0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a ITE_OK;. }.
291e0 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 moveToPare
291f0 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a nt(pCur);. }.
29200 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e pCur->info.n
29210 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 Size = 0;. pC
29220 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 ur->curFlags &=
29230 7e 28 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 ~(BTCF_ValidNKey
29240 7c 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 |BTCF_ValidOvfl)
29250 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 ;.. pCur->aiI
29260 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d dx[pCur->iPage]-
29270 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 -;. pPage = p
29280 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
29290 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 ->iPage];. if
292a0 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
292b0 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 && !pPage->leaf
292c0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
292d0 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
292e0 75 73 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a us(pCur, pRes);.
292f0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
29300 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
29310 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 . }. }. *pR
29320 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e es = 0;. return
29330 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c rc;.}../*.** Al
29340 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 locate a new pag
29350 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 e from the datab
29360 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
29370 54 68 65 20 6e 65 77 20 70 61 67 65 20 69 73 20 The new page is
29380 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e marked as dirty.
29390 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 (In other word
293a0 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 s, sqlite3PagerW
293b0 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c rite().** has al
293c0 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 ready been calle
293d0 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 d on the new pag
293e0 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70 61 67 e.) The new pag
293f0 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 e has also.** be
29400 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e en referenced an
29410 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f d the calling ro
29420 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 utine is respons
29430 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 ible for calling
29440 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 .** sqlite3Pager
29450 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e Unref() on the n
29460 65 77 20 70 61 67 65 20 77 68 65 6e 20 69 74 20 ew page when it
29470 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 is done..**.** S
29480 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
29490 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e rned on success.
294a0 20 20 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75 Any other retu
294b0 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 rn value indicat
294c0 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 es.** an error.
294d0 20 2a 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50 *ppPage and *pP
294e0 67 6e 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 gno are undefine
294f0 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f d in the event o
29500 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 f an error..** D
29510 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c o not invoke sql
29520 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 ite3PagerUnref()
29530 20 6f 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61 on *ppPage if a
29540 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
29550 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ned..**.** If th
29560 65 20 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d e "nearby" param
29570 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 eter is not 0, t
29580 68 65 6e 20 61 6e 20 65 66 66 6f 72 74 20 69 73 hen an effort is
29590 20 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 made to .** loc
295a0 61 74 65 20 61 20 70 61 67 65 20 63 6c 6f 73 65 ate a page close
295b0 20 74 6f 20 74 68 65 20 70 61 67 65 20 6e 75 6d to the page num
295c0 62 65 72 20 22 6e 65 61 72 62 79 22 2e 20 20 54 ber "nearby". T
295d0 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
295e0 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 in an.** attempt
295f0 20 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 64 to keep related
29600 20 70 61 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 pages close to
29610 65 61 63 68 20 6f 74 68 65 72 20 69 6e 20 74 68 each other in th
29620 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
29630 0a 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 75 72 .** which in tur
29640 6e 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 n can make datab
29650 61 73 65 20 61 63 63 65 73 73 20 66 61 73 74 65 ase access faste
29660 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
29670 65 4d 6f 64 65 20 70 61 72 61 6d 65 74 65 72 20 eMode parameter
29680 69 73 20 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 is BTALLOC_EXACT
29690 20 61 6e 64 20 74 68 65 20 6e 65 61 72 62 79 20 and the nearby
296a0 70 61 67 65 20 65 78 69 73 74 73 0a 2a 2a 20 61 page exists.** a
296b0 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 nywhere on the f
296c0 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 ree-list, then i
296d0 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 t is guaranteed
296e0 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 to be returned.
296f0 20 49 66 0a 2a 2a 20 65 4d 6f 64 65 20 69 73 20 If.** eMode is
29700 42 54 41 4c 4c 4f 43 5f 4c 54 20 74 68 65 6e 20 BTALLOC_LT then
29710 74 68 65 20 70 61 67 65 20 72 65 74 75 72 6e 65 the page returne
29720 64 20 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74 d will be less t
29730 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 han or equal.**
29740 74 6f 20 6e 65 61 72 62 79 20 69 66 20 61 6e 79 to nearby if any
29750 20 73 75 63 68 20 70 61 67 65 20 65 78 69 73 74 such page exist
29760 73 2e 20 20 49 66 20 65 4d 6f 64 65 20 69 73 20 s. If eMode is
29770 42 54 41 4c 4c 4f 43 5f 41 4e 59 20 74 68 65 6e BTALLOC_ANY then
29780 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f there.** are no
29790 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20 6f 6e restrictions on
297a0 20 77 68 69 63 68 20 70 61 67 65 20 69 73 20 72 which page is r
297b0 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
297c0 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 ic int allocateB
297d0 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 68 treePage(. BtSh
297e0 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
297f0 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 /* The btree
29800 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 */. MemPage **p
29810 70 50 61 67 65 2c 20 20 20 20 20 20 2f 2a 20 53 pPage, /* S
29820 74 6f 72 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 tore pointer to
29830 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 the allocated pa
29840 67 65 20 68 65 72 65 20 2a 2f 0a 20 20 50 67 6e ge here */. Pgn
29850 6f 20 2a 70 50 67 6e 6f 2c 20 20 20 20 20 20 20 o *pPgno,
29860 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 /* Store the
29870 20 70 61 67 65 20 6e 75 6d 62 65 72 20 68 65 72 page number her
29880 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 61 72 e */. Pgno near
29890 62 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a by, /*
298a0 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 70 61 Search for a pa
298b0 67 65 20 6e 65 61 72 20 74 68 69 73 20 6f 6e 65 ge near this one
298c0 20 2a 2f 0a 20 20 75 38 20 65 4d 6f 64 65 20 20 */. u8 eMode
298d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
298e0 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 2c 20 42 BTALLOC_EXACT, B
298f0 54 41 4c 4c 4f 43 5f 4c 54 2c 20 6f 72 20 42 54 TALLOC_LT, or BT
29900 41 4c 4c 4f 43 5f 41 4e 59 20 2a 2f 0a 29 7b 0a ALLOC_ANY */.){.
29910 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
29920 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 1;. int rc;. u
29930 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 32 n; /* Num
29940 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 ber of pages on
29950 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a the freelist */.
29960 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 u32 k; /*
29970 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 Number of leaves
29980 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 on the trunk of
29990 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f the freelist */
299a0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 . MemPage *pTru
299b0 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 nk = 0;. MemPag
299c0 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 e *pPrevTrunk =
299d0 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 0;. Pgno mxPage
299e0 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 ; /* Total s
299f0 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
29a00 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 ase file */.. a
29a10 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
29a20 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
29a30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
29a40 74 28 20 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f t( eMode==BTALLO
29a50 43 5f 41 4e 59 20 7c 7c 20 28 6e 65 61 72 62 79 C_ANY || (nearby
29a60 3e 30 20 26 26 20 49 66 4e 6f 74 4f 6d 69 74 41 >0 && IfNotOmitA
29a70 56 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 V(pBt->autoVacuu
29a80 6d 29 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 m)) );. pPage1
29a90 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 = pBt->pPage1;.
29aa0 20 6d 78 50 61 67 65 20 3d 20 62 74 72 65 65 50 mxPage = btreeP
29ab0 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
29ac0 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 n = get4byte(&p
29ad0 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
29ae0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
29af0 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20 ==mxPage-1 );.
29b00 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 7b if( n>=mxPage ){
29b10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
29b20 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
29b30 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 29 . }. if( n>0 )
29b40 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 {. /* There a
29b50 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 re pages on the
29b60 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 freelist. Reuse
29b70 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 one of those pa
29b80 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f ges. */. Pgno
29b90 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 iTrunk;. u8
29ba0 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 searchList = 0;
29bb0 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c /* If the free-l
29bc0 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 ist must be sear
29bd0 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 ched for 'nearby
29be0 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a ' */. . /*
29bf0 20 49 66 20 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c If eMode==BTALL
29c00 4f 43 5f 45 58 41 43 54 20 61 6e 64 20 61 20 71 OC_EXACT and a q
29c10 75 65 72 79 20 6f 66 20 74 68 65 20 70 6f 69 6e uery of the poin
29c20 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73 ter-map. ** s
29c30 68 6f 77 73 20 74 68 61 74 20 74 68 65 20 70 61 hows that the pa
29c40 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 73 ge 'nearby' is s
29c50 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 68 65 20 omewhere on the
29c60 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a free-list, then.
29c70 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 ** the entir
29c80 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 20 73 e-list will be s
29c90 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 61 74 earched for that
29ca0 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 page.. */.#i
29cb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
29cc0 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
29cd0 20 69 66 28 20 65 4d 6f 64 65 3d 3d 42 54 41 4c if( eMode==BTAL
29ce0 4c 4f 43 5f 45 58 41 43 54 20 29 7b 0a 20 20 20 LOC_EXACT ){.
29cf0 20 20 20 69 66 28 20 6e 65 61 72 62 79 3c 3d 6d if( nearby<=m
29d00 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 xPage ){.
29d10 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 u8 eType;.
29d20 20 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 assert( nearb
29d30 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 y>0 );. a
29d40 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
29d50 56 61 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 Vacuum );.
29d60 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 rc = ptrmapGet
29d70 28 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 26 65 (pBt, nearby, &e
29d80 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Type, 0);.
29d90 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
29da0 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 n rc;. if
29db0 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
29dc0 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 FREEPAGE ){.
29dd0 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 searchList
29de0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
29df0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
29e00 65 20 69 66 28 20 65 4d 6f 64 65 3d 3d 42 54 41 e if( eMode==BTA
29e10 4c 4c 4f 43 5f 4c 45 20 29 7b 0a 20 20 20 20 20 LLOC_LE ){.
29e20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 31 3b searchList = 1;
29e30 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
29e40 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 /* Decrement
29e50 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63 6f the free-list co
29e60 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69 54 unt by 1. Set iT
29e70 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 runk to the inde
29e80 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 x of the. **
29e90 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74 20 first free-list
29ea0 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 trunk page. iPre
29eb0 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 vTrunk is initia
29ec0 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 lly 1.. */.
29ed0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
29ee0 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d gerWrite(pPage1-
29ef0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
29f00 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
29f10 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 c;. put4byte(
29f20 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
29f30 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 6], n-1);.. /
29f40 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68 69 * The code withi
29f50 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 n this loop is r
29f60 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 un only once if
29f70 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74 27 the 'searchList'
29f80 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a variable. **
29f90 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 is not true. Ot
29fa0 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e 73 herwise, it runs
29fb0 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 once for each t
29fc0 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 runk-page on the
29fd0 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 . ** free-lis
29fe0 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67 65 t until the page
29ff0 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63 'nearby' is loc
2a000 61 74 65 64 20 28 65 4d 6f 64 65 3d 3d 42 54 41 ated (eMode==BTA
2a010 4c 4c 4f 43 5f 45 58 41 43 54 29 0a 20 20 20 20 LLOC_EXACT).
2a020 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 61 20 70 61 ** or until a pa
2a030 67 65 20 6c 65 73 73 20 74 68 61 6e 20 27 6e 65 ge less than 'ne
2a040 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65 64 arby' is located
2a050 20 28 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 (eMode==BTALLOC
2a060 5f 4c 54 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 _LT). */.
2a070 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72 65 76 do {. pPrev
2a080 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b 0a Trunk = pTrunk;.
2a090 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76 54 if( pPrevT
2a0a0 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 runk ){.
2a0b0 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 iTrunk = get4byt
2a0c0 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 e(&pPrevTrunk->a
2a0d0 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 Data[0]);.
2a0e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
2a0f0 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 Trunk = get4byte
2a100 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
2a110 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 32]);. }.
2a120 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 54 testcase( iT
2a130 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a runk==mxPage );.
2a140 20 20 20 20 20 20 69 66 28 20 69 54 72 75 6e 6b if( iTrunk
2a150 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 >mxPage ){.
2a160 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
2a170 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
2a180 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2a190 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
2a1a0 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c age(pBt, iTrunk,
2a1b0 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 &pTrunk, 0);.
2a1c0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
2a1d0 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 rc ){. pT
2a1e0 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 runk = 0;.
2a1f0 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
2a200 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
2a210 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
2a220 70 54 72 75 6e 6b 21 3d 30 20 29 3b 0a 20 20 20 pTrunk!=0 );.
2a230 20 20 20 61 73 73 65 72 74 28 20 70 54 72 75 6e assert( pTrun
2a240 6b 2d 3e 61 44 61 74 61 21 3d 30 20 29 3b 0a 0a k->aData!=0 );..
2a250 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 k = get4by
2a260 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 te(&pTrunk->aDat
2a270 61 5b 34 5d 29 3b 20 2f 2a 20 23 20 6f 66 20 6c a[4]); /* # of l
2a280 65 61 76 65 73 20 6f 6e 20 74 68 69 73 20 74 72 eaves on this tr
2a290 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 unk page */.
2a2a0 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21 73 if( k==0 && !s
2a2b0 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20 20 earchList ){.
2a2c0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e /* The trun
2a2d0 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73 20 k has no leaves
2a2e0 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73 20 and the list is
2a2f0 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63 68 not being search
2a300 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ed. . **
2a310 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 74 So extract the t
2a320 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c 66 runk page itself
2a330 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20 74 and use it as t
2a340 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20 20 he newly .
2a350 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 ** allocated p
2a360 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 age */. a
2a370 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75 6e ssert( pPrevTrun
2a380 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 k==0 );.
2a390 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
2a3a0 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
2a3b0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
2a3c0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
2a3d0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
2a3e0 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
2a3f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a }. *
2a400 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a pPgno = iTrunk;.
2a410 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
2a420 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
2a430 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
2a440 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
2a450 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 *ppPage = pTru
2a460 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72 75 nk;. pTru
2a470 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nk = 0;.
2a480 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
2a490 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 : %d trunk - %d
2a4a0 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c free pages left\
2a4b0 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 n", *pPgno, n-1)
2a4c0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
2a4d0 66 28 20 6b 3e 28 75 33 32 29 28 70 42 74 2d 3e f( k>(u32)(pBt->
2a4e0 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 usableSize/4 - 2
2a4f0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
2a500 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 Value of k is ou
2a510 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 74 t of range. Dat
2a520 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
2a530 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d */. rc =
2a540 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
2a550 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f BKPT;. go
2a560 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
2a570 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 page;.#ifndef SQ
2a580 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
2a590 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 CUUM. }else
2a5a0 20 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 20 if( searchList
2a5b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 . &&
2a5c0 28 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 (nearby==iTrunk
2a5d0 7c 7c 20 28 69 54 72 75 6e 6b 3c 6e 65 61 72 62 || (iTrunk<nearb
2a5e0 79 20 26 26 20 65 4d 6f 64 65 3d 3d 42 54 41 4c y && eMode==BTAL
2a5f0 4c 4f 43 5f 4c 45 29 29 20 0a 20 20 20 20 20 20 LOC_LE)) .
2a600 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
2a610 65 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 e list is being
2a620 73 65 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 searched and thi
2a630 73 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 s trunk page is
2a640 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 the page.
2a650 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c ** to allocate,
2a660 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
2a670 68 65 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 hether it has le
2a680 61 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f aves.. */
2a690 0a 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 . *pPgno
2a6a0 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 = iTrunk;.
2a6b0 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 *ppPage = pTru
2a6c0 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61 72 nk;. sear
2a6d0 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 chList = 0;.
2a6e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
2a6f0 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
2a700 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
2a710 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
2a720 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
2a730 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
2a740 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2a750 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 if( k==0 ){.
2a760 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 if( !pP
2a770 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 revTrunk ){.
2a780 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
2a790 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
2a7a0 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
2a7b0 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
2a7c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
2a7d0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
2a7e0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
2a7f0 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 revTrunk->pDbPag
2a800 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
2a810 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
2a820 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
2a830 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
2a840 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
2a850 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2a860 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 72 memcpy(&pPr
2a870 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 evTrunk->aData[0
2a880 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
2a890 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
2a8a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
2a8b0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
2a8c0 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 61 67 65 * The trunk page
2a8d0 20 69 73 20 72 65 71 75 69 72 65 64 20 62 79 20 is required by
2a8e0 74 68 65 20 63 61 6c 6c 65 72 20 62 75 74 20 69 the caller but i
2a8f0 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 20 20 20 t contains .
2a900 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 ** pointer
2a910 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 6c s to free-list l
2a920 65 61 76 65 73 2e 20 54 68 65 20 66 69 72 73 74 eaves. The first
2a930 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 20 61 20 leaf becomes a
2a940 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 20 20 20 trunk.
2a950 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 ** page in this
2a960 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 case..
2a970 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4d 65 6d */. Mem
2a980 50 61 67 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b Page *pNewTrunk;
2a990 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 . Pgno
2a9a0 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 65 74 34 iNewTrunk = get4
2a9b0 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
2a9c0 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 ata[8]);.
2a9d0 20 20 20 69 66 28 20 69 4e 65 77 54 72 75 6e 6b if( iNewTrunk
2a9e0 3e 6d 78 50 61 67 65 20 29 7b 20 0a 20 20 20 20 >mxPage ){ .
2a9f0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
2aa00 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
2aa10 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f ;. go
2aa20 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
2aa30 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 page;.
2aa40 7d 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 }. test
2aa50 63 61 73 65 28 20 69 4e 65 77 54 72 75 6e 6b 3d case( iNewTrunk=
2aa60 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 =mxPage );.
2aa70 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
2aa80 65 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65 77 etPage(pBt, iNew
2aa90 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e Trunk, &pNewTrun
2aaa0 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 k, 0);.
2aab0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
2aac0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
2aad0 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
2aae0 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
2aaf0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
2ab00 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
2ab10 72 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e 6b rWrite(pNewTrunk
2ab20 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
2ab30 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
2ab40 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
2ab50 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
2ab60 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 ge(pNewTrunk);.
2ab70 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
2ab80 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
2ab90 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 e;. }.
2aba0 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
2abb0 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 &pNewTrunk->aDat
2abc0 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 a[0], &pTrunk->a
2abd0 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 Data[0], 4);.
2abe0 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
2abf0 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 &pNewTrunk->aDat
2ac00 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 a[4], k-1);.
2ac10 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e memcpy(&pN
2ac20 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 ewTrunk->aData[8
2ac30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
2ac40 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b a[12], (k-1)*4);
2ac50 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 . relea
2ac60 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b sePage(pNewTrunk
2ac70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
2ac80 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a !pPrevTrunk ){.
2ac90 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 asse
2aca0 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
2acb0 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
2acc0 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a e1->pDbPage) );.
2acd0 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 put4
2ace0 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 byte(&pPage1->aD
2acf0 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 ata[32], iNewTru
2ad00 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d nk);. }
2ad10 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
2ad20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2ad30 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54 72 gerWrite(pPrevTr
2ad40 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
2ad50 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
2ad60 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 c ){.
2ad70 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
2ad80 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
2ad90 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2ada0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
2adb0 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 PrevTrunk->aData
2adc0 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b [0], iNewTrunk);
2add0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2ade0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
2adf0 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 Trunk = 0;.
2ae00 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 TRACE(("ALLOC
2ae10 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 ATE: %d trunk -
2ae20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 %d free pages le
2ae30 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e ft\n", *pPgno, n
2ae40 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 -1));.#endif.
2ae50 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 30 }else if( k>0
2ae60 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 ){. /* E
2ae70 78 74 72 61 63 74 20 61 20 6c 65 61 66 20 66 72 xtract a leaf fr
2ae80 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a 2f 0a om the trunk */.
2ae90 20 20 20 20 20 20 20 20 75 33 32 20 63 6c 6f 73 u32 clos
2aea0 65 73 74 3b 0a 20 20 20 20 20 20 20 20 50 67 6e est;. Pgn
2aeb0 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 o iPage;.
2aec0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
2aed0 61 44 61 74 61 20 3d 20 70 54 72 75 6e 6b 2d 3e aData = pTrunk->
2aee0 61 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 69 aData;. i
2aef0 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b 0a 20 f( nearby>0 ){.
2af00 20 20 20 20 20 20 20 20 20 75 33 32 20 69 3b 0a u32 i;.
2af10 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
2af20 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
2af30 20 69 66 28 20 65 4d 6f 64 65 3d 3d 42 54 41 4c if( eMode==BTAL
2af40 4c 4f 43 5f 4c 45 20 29 7b 0a 20 20 20 20 20 20 LOC_LE ){.
2af50 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
2af60 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 <k; i++){.
2af70 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d 20 iPage =
2af80 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b get4byte(&aData[
2af90 38 2b 69 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 8+i*4]);.
2afa0 20 20 20 20 20 20 20 69 66 28 20 69 50 61 67 65 if( iPage
2afb0 3c 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 20 20 <=nearby ){.
2afc0 20 20 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 clos
2afd0 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 est = i;.
2afe0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
2aff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
2b000 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
2b010 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
2b020 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 int d
2b030 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ist;.
2b040 20 64 69 73 74 20 3d 20 73 71 6c 69 74 65 33 41 dist = sqlite3A
2b050 62 73 49 6e 74 33 32 28 67 65 74 34 62 79 74 65 bsInt32(get4byte
2b060 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65 (&aData[8]) - ne
2b070 61 72 62 79 29 3b 0a 20 20 20 20 20 20 20 20 20 arby);.
2b080 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b for(i=1; i<k;
2b090 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
2b0a0 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71 int d2 = sq
2b0b0 6c 69 74 65 33 41 62 73 49 6e 74 33 32 28 67 65 lite3AbsInt32(ge
2b0c0 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b t4byte(&aData[8+
2b0d0 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 29 3b i*4]) - nearby);
2b0e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 . i
2b0f0 66 28 20 64 32 3c 64 69 73 74 20 29 7b 0a 20 20 f( d2<dist ){.
2b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c cl
2b110 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 20 osest = i;.
2b120 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 dist
2b130 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 = d2;.
2b140 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
2b150 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
2b160 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
2b170 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 closest
2b180 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 0;. }.
2b190 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d . iPage =
2b1a0 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 get4byte(&aData
2b1b0 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a [8+closest*4]);.
2b1c0 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
2b1d0 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 ( iPage==mxPage
2b1e0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 );. if( i
2b1f0 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a 20 Page>mxPage ){.
2b200 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
2b210 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
2b220 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 T;. got
2b230 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
2b240 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
2b250 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
2b260 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29 iPage==mxPage )
2b270 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 ;. if( !s
2b280 65 61 72 63 68 4c 69 73 74 20 0a 20 20 20 20 20 earchList .
2b290 20 20 20 20 7c 7c 20 28 69 50 61 67 65 3d 3d 6e || (iPage==n
2b2a0 65 61 72 62 79 20 7c 7c 20 28 69 50 61 67 65 3c earby || (iPage<
2b2b0 6e 65 61 72 62 79 20 26 26 20 65 4d 6f 64 65 3d nearby && eMode=
2b2c0 3d 42 54 41 4c 4c 4f 43 5f 4c 45 29 29 20 0a 20 =BTALLOC_LE)) .
2b2d0 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ){.
2b2e0 20 20 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e int noConten
2b2f0 74 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 t;. *pP
2b300 67 6e 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 gno = iPage;.
2b310 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 TRACE(("A
2b320 4c 4c 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 LLOCATE: %d was
2b330 6c 65 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e leaf %d of %d on
2b340 20 74 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 trunk %d".
2b350 20 20 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 ": %
2b360 64 20 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 d more free page
2b370 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
2b380 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 *pPgno, c
2b390 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 losest+1, k, pTr
2b3a0 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 unk->pgno, n-1))
2b3b0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
2b3c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
2b3d0 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
2b3e0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
2b3f0 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 f( rc ) goto end
2b400 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
2b410 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c if( cl
2b420 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 osest<k-1 ){.
2b430 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
2b440 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 &aData[8+closest
2b450 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a *4], &aData[4+k*
2b460 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 4], 4);.
2b470 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75 }. pu
2b480 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d t4byte(&aData[4]
2b490 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 , k-1);.
2b4a0 20 20 6e 6f 43 6f 6e 74 65 6e 74 20 3d 20 21 62 noContent = !b
2b4b0 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65 6e treeGetHasConten
2b4c0 74 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 20 3f t(pBt, *pPgno) ?
2b4d0 20 50 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e PAGER_GET_NOCON
2b4e0 54 45 4e 54 20 3a 20 30 3b 0a 20 20 20 20 20 20 TENT : 0;.
2b4f0 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
2b500 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e tPage(pBt, *pPgn
2b510 6f 2c 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e o, ppPage, noCon
2b520 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 tent);.
2b530 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2b540 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
2b550 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2b560 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 gerWrite((*ppPag
2b570 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e)->pDbPage);.
2b580 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
2b590 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
2b5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c rel
2b5b0 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
2b5c0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d );. }
2b5d0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2b5e0 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 searchLis
2b5f0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d t = 0;. }
2b600 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
2b610 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76 eleasePage(pPrev
2b620 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50 Trunk);. pP
2b630 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 revTrunk = 0;.
2b640 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 63 68 }while( search
2b650 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b List );. }else{
2b660 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 . /* There ar
2b670 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 e no pages on th
2b680 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 61 e freelist, so a
2b690 70 70 65 6e 64 20 61 20 6e 65 77 20 70 61 67 65 ppend a new page
2b6a0 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 to the. ** d
2b6b0 61 74 61 62 61 73 65 20 69 6d 61 67 65 2e 0a 20 atabase image..
2b6c0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 72 **. ** Nor
2b6d0 6d 61 6c 6c 79 2c 20 6e 65 77 20 70 61 67 65 73 mally, new pages
2b6e0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 allocated by th
2b6f0 69 73 20 62 6c 6f 63 6b 20 63 61 6e 20 62 65 20 is block can be
2b700 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 requested from t
2b710 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 he. ** pager
2b720 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 27 layer with the '
2b730 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 no-content' flag
2b740 20 73 65 74 2e 20 54 68 69 73 20 70 72 65 76 65 set. This preve
2b750 6e 74 73 20 74 68 65 20 70 61 67 65 72 0a 20 20 nts the pager.
2b760 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 ** from trying
2b770 20 74 6f 20 72 65 61 64 20 74 68 65 20 70 61 67 to read the pag
2b780 65 73 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 es content from
2b790 64 69 73 6b 2e 20 48 6f 77 65 76 65 72 2c 20 69 disk. However, i
2b7a0 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 75 72 f the. ** cur
2b7b0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
2b7c0 20 68 61 73 20 61 6c 72 65 61 64 79 20 72 75 6e has already run
2b7d0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 63 one or more inc
2b7e0 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 0a remental-vacuum.
2b7f0 20 20 20 20 2a 2a 20 73 74 65 70 73 2c 20 74 68 ** steps, th
2b800 65 6e 20 74 68 65 20 70 61 67 65 20 77 65 20 61 en the page we a
2b810 72 65 20 61 62 6f 75 74 20 74 6f 20 61 6c 6c 6f re about to allo
2b820 63 61 74 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e cate may contain
2b830 20 63 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20 content. **
2b840 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 that is required
2b850 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 in the event of
2b860 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 6e 20 a rollback. In
2b870 74 68 69 73 20 63 61 73 65 2c 20 64 6f 0a 20 20 this case, do.
2b880 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20 74 68 65 ** not set the
2b890 20 6e 6f 2d 63 6f 6e 74 65 6e 74 20 66 6c 61 67 no-content flag
2b8a0 2e 20 54 68 69 73 20 63 61 75 73 65 73 20 74 68 . This causes th
2b8b0 65 20 70 61 67 65 72 20 74 6f 20 6c 6f 61 64 20 e pager to load
2b8c0 61 6e 64 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 and journal.
2b8d0 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 ** the current p
2b8e0 61 67 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f age content befo
2b8f0 72 65 20 6f 76 65 72 77 72 69 74 69 6e 67 20 69 re overwriting i
2b900 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a t.. **. **
2b910 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 70 Note that the p
2b920 61 67 65 72 20 77 69 6c 6c 20 6e 6f 74 20 61 63 ager will not ac
2b930 74 75 61 6c 6c 79 20 61 74 74 65 6d 70 74 20 74 tually attempt t
2b940 6f 20 6c 6f 61 64 20 6f 72 20 6a 6f 75 72 6e 61 o load or journa
2b950 6c 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 65 6e l . ** conten
2b960 74 20 66 6f 72 20 61 6e 79 20 70 61 67 65 20 74 t for any page t
2b970 68 61 74 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 hat really does
2b980 6c 69 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 lie past the end
2b990 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
2b9a0 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 6f 6e 20 . ** file on
2b9b0 64 69 73 6b 2e 20 53 6f 20 74 68 65 20 65 66 66 disk. So the eff
2b9c0 65 63 74 73 20 6f 66 20 64 69 73 61 62 6c 69 6e ects of disablin
2b9d0 67 20 74 68 65 20 6e 6f 2d 63 6f 6e 74 65 6e 74 g the no-content
2b9e0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 20 optimization.
2b9f0 20 20 2a 2a 20 68 65 72 65 20 61 72 65 20 63 6f ** here are co
2ba00 6e 66 69 6e 65 64 20 74 6f 20 74 68 6f 73 65 20 nfined to those
2ba10 70 61 67 65 73 20 74 68 61 74 20 6c 69 65 20 62 pages that lie b
2ba20 65 74 77 65 65 6e 20 74 68 65 20 65 6e 64 20 6f etween the end o
2ba30 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 f the. ** dat
2ba40 61 62 61 73 65 20 69 6d 61 67 65 20 61 6e 64 20 abase image and
2ba50 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 the end of the d
2ba60 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 atabase file..
2ba70 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 4e 6f */. int bNo
2ba80 43 6f 6e 74 65 6e 74 20 3d 20 28 30 3d 3d 49 66 Content = (0==If
2ba90 4e 6f 74 4f 6d 69 74 41 56 28 70 42 74 2d 3e 62 NotOmitAV(pBt->b
2baa0 44 6f 54 72 75 6e 63 61 74 65 29 29 20 3f 20 50 DoTruncate)) ? P
2bab0 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 AGER_GET_NOCONTE
2bac0 4e 54 20 3a 20 30 3b 0a 0a 20 20 20 20 72 63 20 NT : 0;.. rc
2bad0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
2bae0 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
2baf0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
2bb00 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
2bb10 63 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 c;. pBt->nPag
2bb20 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 42 74 e++;. if( pBt
2bb30 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e 47 ->nPage==PENDING
2bb40 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
2bb50 29 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0a ) pBt->nPage++;.
2bb60 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2bb70 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
2bb80 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
2bb90 6f 56 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 oVacuum && PTRMA
2bba0 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 42 P_ISPAGE(pBt, pB
2bbb0 74 2d 3e 6e 50 61 67 65 29 20 29 7b 0a 20 20 20 t->nPage) ){.
2bbc0 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 /* If *pPgno
2bbd0 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f 69 6e refers to a poin
2bbe0 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c ter-map page, al
2bbf0 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77 20 70 locate two new p
2bc00 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 ages. ** at
2bc10 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
2bc20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 file instead of
2bc30 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 one. The first a
2bc40 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a 20 20 llocated page.
2bc50 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 ** becomes a
2bc60 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 new pointer-map
2bc70 20 70 61 67 65 2c 20 74 68 65 20 73 65 63 6f 6e page, the secon
2bc80 64 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 d is used by the
2bc90 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a caller.. *
2bca0 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 /. MemPage
2bcb0 2a 70 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 *pPg = 0;.
2bcc0 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
2bcd0 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 : %d from end of
2bce0 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d file (pointer-m
2bcf0 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 70 42 74 ap page)\n", pBt
2bd00 2d 3e 6e 50 61 67 65 29 29 3b 0a 20 20 20 20 20 ->nPage));.
2bd10 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 50 assert( pBt->nP
2bd20 61 67 65 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 age!=PENDING_BYT
2bd30 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 E_PAGE(pBt) );.
2bd40 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
2bd50 65 74 50 61 67 65 28 70 42 74 2c 20 70 42 74 2d etPage(pBt, pBt-
2bd60 3e 6e 50 61 67 65 2c 20 26 70 50 67 2c 20 62 4e >nPage, &pPg, bN
2bd70 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 oContent);.
2bd80 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2bd90 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
2bda0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
2bdb0 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67 rite(pPg->pDbPag
2bdc0 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 e);. rele
2bdd0 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20 asePage(pPg);.
2bde0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
2bdf0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
2be00 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 pBt->nPage
2be10 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 ++;. if( pB
2be20 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e t->nPage==PENDIN
2be30 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
2be40 20 29 7b 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b ){ pBt->nPage++
2be50 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ; }. }.#endif
2be60 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 32 38 . put4byte(28
2be70 20 2b 20 28 75 38 2a 29 70 42 74 2d 3e 70 50 61 + (u8*)pBt->pPa
2be80 67 65 31 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d ge1->aData, pBt-
2be90 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 2a 70 50 >nPage);. *pP
2bea0 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 50 61 67 65 gno = pBt->nPage
2beb0 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a ;.. assert( *
2bec0 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
2bed0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
2bee0 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
2bef0 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 etPage(pBt, *pPg
2bf00 6e 6f 2c 20 70 70 50 61 67 65 2c 20 62 4e 6f 43 no, ppPage, bNoC
2bf10 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 ontent);. if(
2bf20 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
2bf30 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
2bf40 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 3PagerWrite((*pp
2bf50 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b Page)->pDbPage);
2bf60 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
2bf70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2bf80 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 releasePage(*ppP
2bf90 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 age);. }.
2bfa0 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
2bfb0 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 : %d from end of
2bfc0 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f file\n", *pPgno
2bfd0 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 ));. }.. asser
2bfe0 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 t( *pPgno!=PENDI
2bff0 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
2c000 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 ) );..end_alloca
2c010 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 te_page:. relea
2c020 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a sePage(pTrunk);.
2c030 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
2c040 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 revTrunk);. if(
2c050 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
2c060 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
2c070 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
2c080 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 nt((*ppPage)->pD
2c090 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 bPage)>1 ){.
2c0a0 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 releasePage(*p
2c0b0 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 2a 70 pPage);. *p
2c0c0 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 pPage = 0;.
2c0d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
2c0e0 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
2c0f0 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 }. (*ppPage)
2c100 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
2c110 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 }else{. *ppPa
2c120 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 ge = 0;. }. as
2c130 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
2c140 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 _OK || sqlite3Pa
2c150 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 28 gerIswriteable((
2c160 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 *ppPage)->pDbPag
2c170 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e) );. return r
2c180 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
2c190 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
2c1a0 64 20 74 6f 20 61 64 64 20 70 61 67 65 20 69 50 d to add page iP
2c1b0 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
2c1c0 61 73 65 20 66 69 6c 65 20 66 72 65 65 2d 6c 69 ase file free-li
2c1d0 73 74 2e 20 0a 2a 2a 20 49 74 20 69 73 20 61 73 st. .** It is as
2c1e0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 70 sumed that the p
2c1f0 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
2c200 64 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 dy a part of the
2c210 20 66 72 65 65 2d 6c 69 73 74 2e 0a 2a 2a 0a 2a free-list..**.*
2c220 2a 20 54 68 65 20 76 61 6c 75 65 20 70 61 73 73 * The value pass
2c230 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
2c240 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
2c250 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 70 s function is op
2c260 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 66 20 74 68 tional..** If th
2c270 65 20 63 61 6c 6c 65 72 20 68 61 70 70 65 6e 73 e caller happens
2c280 20 74 6f 20 68 61 76 65 20 61 20 70 6f 69 6e 74 to have a point
2c290 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 er to the MemPag
2c2a0 65 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 72 e object .** cor
2c2b0 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 responding to pa
2c2c0 67 65 20 69 50 61 67 65 20 68 61 6e 64 79 2c 20 ge iPage handy,
2c2d0 69 74 20 6d 61 79 20 70 61 73 73 20 69 74 20 61 it may pass it a
2c2e0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 76 61 6c s the second val
2c2f0 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ue. .** Otherwis
2c300 65 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 4e e, it may pass N
2c310 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ULL..**.** If a
2c320 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d pointer to a Mem
2c330 50 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 70 Page object is p
2c340 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
2c350 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a ond argument,.**
2c360 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 63 its reference c
2c370 6f 75 6e 74 20 69 73 20 6e 6f 74 20 61 6c 74 65 ount is not alte
2c380 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 red by this func
2c390 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
2c3a0 69 6e 74 20 66 72 65 65 50 61 67 65 32 28 42 74 int freePage2(Bt
2c3b0 53 68 61 72 65 64 20 2a 70 42 74 2c 20 4d 65 6d Shared *pBt, Mem
2c3c0 50 61 67 65 20 2a 70 4d 65 6d 50 61 67 65 2c 20 Page *pMemPage,
2c3d0 50 67 6e 6f 20 69 50 61 67 65 29 7b 0a 20 20 4d Pgno iPage){. M
2c3e0 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d emPage *pTrunk =
2c3f0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
2c400 20 20 20 2f 2a 20 46 72 65 65 2d 6c 69 73 74 20 /* Free-list
2c410 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 trunk page */.
2c420 50 67 6e 6f 20 69 54 72 75 6e 6b 20 3d 20 30 3b Pgno iTrunk = 0;
2c430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c440 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
2c450 65 72 20 6f 66 20 66 72 65 65 2d 6c 69 73 74 20 er of free-list
2c460 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 20 0a 20 trunk page */ .
2c470 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 MemPage *pPage1
2c480 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 20 = pBt->pPage1;
2c490 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 72 65 /* Local re
2c4a0 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 ference to page
2c4b0 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 1 */. MemPage *
2c4c0 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 pPage;
2c4d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
2c4e0 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 2e 20 ge being freed.
2c4f0 4d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a May be NULL. */.
2c500 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
2c510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c520 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
2c530 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 Code */. int nF
2c540 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ree;
2c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2c560 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 Initial number
2c570 6f 66 20 70 61 67 65 73 20 6f 6e 20 66 72 65 65 of pages on free
2c580 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 -list */.. asse
2c590 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
2c5a0 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
2c5b0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
2c5c0 69 50 61 67 65 3e 31 20 29 3b 0a 20 20 61 73 73 iPage>1 );. ass
2c5d0 65 72 74 28 20 21 70 4d 65 6d 50 61 67 65 20 7c ert( !pMemPage |
2c5e0 7c 20 70 4d 65 6d 50 61 67 65 2d 3e 70 67 6e 6f | pMemPage->pgno
2c5f0 3d 3d 69 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 ==iPage );.. if
2c600 28 20 70 4d 65 6d 50 61 67 65 20 29 7b 0a 20 20 ( pMemPage ){.
2c610 20 20 70 50 61 67 65 20 3d 20 70 4d 65 6d 50 61 pPage = pMemPa
2c620 67 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ge;. sqlite3P
2c630 61 67 65 72 52 65 66 28 70 50 61 67 65 2d 3e 70 agerRef(pPage->p
2c640 44 62 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 DbPage);. }else
2c650 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 62 74 {. pPage = bt
2c660 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 reePageLookup(pB
2c670 74 2c 20 69 50 61 67 65 29 3b 0a 20 20 7d 0a 0a t, iPage);. }..
2c680 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 /* Increment t
2c690 68 65 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 he free page cou
2c6a0 6e 74 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a nt on pPage1 */.
2c6b0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2c6c0 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d gerWrite(pPage1-
2c6d0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
2c6e0 20 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 rc ) goto freep
2c6f0 61 67 65 5f 6f 75 74 3b 0a 20 20 6e 46 72 65 65 age_out;. nFree
2c700 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
2c710 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b ge1->aData[36]);
2c720 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 . put4byte(&pPa
2c730 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 ge1->aData[36],
2c740 6e 46 72 65 65 2b 31 29 3b 0a 0a 20 20 69 66 28 nFree+1);.. if(
2c750 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 pBt->btsFlags &
2c760 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 BTS_SECURE_DELE
2c770 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 TE ){. /* If
2c780 74 68 65 20 73 65 63 75 72 65 5f 64 65 6c 65 74 the secure_delet
2c790 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 e option is enab
2c7a0 6c 65 64 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a led, then. **
2c7b0 20 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76 always fully ov
2c7c0 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 erwrite deleted
2c7d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 information with
2c7e0 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 zeros.. */.
2c7f0 20 20 20 69 66 28 20 28 21 70 50 61 67 65 20 26 if( (!pPage &
2c800 26 20 28 28 72 63 20 3d 20 62 74 72 65 65 47 65 & ((rc = btreeGe
2c810 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65 tPage(pBt, iPage
2c820 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 , &pPage, 0))!=0
2c830 29 20 29 0a 20 20 20 20 20 7c 7c 20 20 20 20 20 ) ). ||
2c840 20 20 20 20 20 20 20 28 28 72 63 20 3d 20 73 71 ((rc = sq
2c850 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
2c860 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 pPage->pDbPage))
2c870 21 3d 30 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 !=0). ){.
2c880 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
2c890 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d out;. }. m
2c8a0 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 emset(pPage->aDa
2c8b0 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 ta, 0, pPage->pB
2c8c0 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 t->pageSize);.
2c8d0 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 }.. /* If the d
2c8e0 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 atabase supports
2c8f0 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 auto-vacuum, wr
2c900 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 ite an entry in
2c910 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
2c920 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 ** to indicate
2c930 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 that the page i
2c940 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 s free.. */. i
2c950 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
2c960 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 ){. ptrmapPut
2c970 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 (pBt, iPage, PTR
2c980 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c MAP_FREEPAGE, 0,
2c990 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 &rc);. if( r
2c9a0 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 c ) goto freepag
2c9b0 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a e_out;. }.. /*
2c9c0 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 Now manipulate
2c9d0 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 the actual datab
2c9e0 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 ase free-list st
2c9f0 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 ructure. There a
2ca00 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 re two. ** poss
2ca10 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 ibilities. If th
2ca20 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 e free-list is c
2ca30 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 urrently empty,
2ca40 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74 0a or if the first.
2ca50 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 ** trunk page
2ca60 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 in the free-list
2ca70 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 is full, then t
2ca80 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 his page will be
2ca90 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 come a. ** new
2caa0 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 free-list trunk
2cab0 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c page. Otherwise,
2cac0 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 it will become
2cad0 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 a leaf of the.
2cae0 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ** first trunk p
2caf0 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 age in the curre
2cb00 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 nt free-list. Th
2cb10 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 is block tests i
2cb20 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 f it. ** is pos
2cb30 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 sible to add the
2cb40 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 page as a new f
2cb50 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 ree-list leaf..
2cb60 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 */. if( nFree!
2cb70 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c =0 ){. u32 nL
2cb80 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 eaf;
2cb90 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
2cba0 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 umber of leaf ce
2cbb0 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 lls on trunk pag
2cbc0 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b e */.. iTrunk
2cbd0 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
2cbe0 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b ge1->aData[32]);
2cbf0 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
2cc00 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 etPage(pBt, iTru
2cc10 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b nk, &pTrunk, 0);
2cc20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
2cc30 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2cc40 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 goto freepage_ou
2cc50 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c t;. }.. nL
2cc60 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 eaf = get4byte(&
2cc70 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d pTrunk->aData[4]
2cc80 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
2cc90 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 Bt->usableSize>3
2cca0 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 2 );. if( nLe
2ccb0 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 af > (u32)pBt->u
2ccc0 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 sableSize/4 - 2
2ccd0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
2cce0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
2ccf0 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 T;. goto fr
2cd00 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 eepage_out;.
2cd10 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 }. if( nLeaf
2cd20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 < (u32)pBt->usab
2cd30 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a leSize/4 - 8 ){.
2cd40 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 /* In this
2cd50 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 72 case there is r
2cd60 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b oom on the trunk
2cd70 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 page to insert
2cd80 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a the page. *
2cd90 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 * being freed as
2cda0 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 a new leaf..
2cdb0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e **. ** N
2cdc0 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75 ote that the tru
2cdd0 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 nk page is not r
2cde0 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c eally full until
2cdf0 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
2ce00 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 ** usableSize
2ce10 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 /4 - 2 entries,
2ce20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 not usableSize/4
2ce30 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 - 8 entries as
2ce40 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a we have. **
2ce50 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 coded. But due
2ce60 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 to a coding err
2ce70 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f or in versions o
2ce80 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 f SQLite prior t
2ce90 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 o. ** 3.6.0
2cea0 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 , databases with
2ceb0 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 freelist trunk
2cec0 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f pages holding mo
2ced0 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a re than. **
2cee0 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
2cef0 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 8 entries will b
2cf00 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f e reported as co
2cf10 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 rrupt. In order
2cf20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 . ** to mai
2cf30 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 ntain backwards
2cf40 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 compatibility wi
2cf50 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e th older version
2cf60 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 s of SQLite,.
2cf70 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f ** we will co
2cf80 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 ntinue to restri
2cf90 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ct the number of
2cfa0 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 entries to usab
2cfb0 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 leSize/4 - 8.
2cfc0 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 ** for now.
2cfd0 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e At some point in
2cfe0 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 the future (onc
2cff0 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 e everyone has u
2d000 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a pgraded. **
2d010 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 to 3.6.0 or lat
2d020 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f er) we should co
2d030 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 nsider fixing th
2d040 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 e conditional ab
2d050 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 ove. ** to
2d060 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 read "usableSize
2d070 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 /4-2" instead of
2d080 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 "usableSize/4-8
2d090 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ".. */.
2d0a0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2d0b0 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d gerWrite(pTrunk-
2d0c0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
2d0d0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2d0e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 OK ){. pu
2d0f0 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
2d100 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b aData[4], nLeaf+
2d110 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 1);. put4
2d120 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
2d130 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 ata[8+nLeaf*4],
2d140 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 iPage);.
2d150 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 42 if( pPage && (pB
2d160 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
2d170 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 S_SECURE_DELETE)
2d180 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
2d190 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
2d1a0 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 tWrite(pPage->pD
2d1b0 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
2d1c0 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 }. rc = b
2d1d0 74 72 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e treeSetHasConten
2d1e0 74 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 t(pBt, iPage);.
2d1f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 41 }. TRA
2d200 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 CE(("FREE-PAGE:
2d210 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b %d leaf on trunk
2d220 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 page %d\n",pPag
2d230 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e e->pgno,pTrunk->
2d240 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 67 6f pgno));. go
2d250 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
2d260 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
2d270 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 If control flow
2d280 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c s to this point,
2d290 20 74 68 65 6e 20 69 74 20 77 61 73 20 6e 6f 74 then it was not
2d2a0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 possible to add
2d2b0 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 the. ** the pa
2d2c0 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 ge being freed a
2d2d0 73 20 61 20 6c 65 61 66 20 70 61 67 65 20 6f 66 s a leaf page of
2d2e0 20 74 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b the first trunk
2d2f0 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
2d300 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 69 62 6c 79 t.. ** Possibly
2d310 20 62 65 63 61 75 73 65 20 74 68 65 20 66 72 65 because the fre
2d320 65 2d 6c 69 73 74 20 69 73 20 65 6d 70 74 79 2c e-list is empty,
2d330 20 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 63 or possibly bec
2d340 61 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 66 ause the . ** f
2d350 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 irst trunk in th
2d360 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 e free-list is f
2d370 75 6c 6c 2e 20 45 69 74 68 65 72 20 77 61 79 2c ull. Either way,
2d380 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 the page being
2d390 66 72 65 65 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 freed. ** will
2d3a0 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 66 become the new f
2d3b0 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 irst trunk page
2d3c0 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 in the free-list
2d3d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
2d3e0 67 65 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f ge==0 && SQLITE_
2d3f0 4f 4b 21 3d 28 72 63 20 3d 20 62 74 72 65 65 47 OK!=(rc = btreeG
2d400 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 etPage(pBt, iPag
2d410 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 20 29 e, &pPage, 0)) )
2d420 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 {. goto freep
2d430 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 72 age_out;. }. r
2d440 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
2d450 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
2d460 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 Page);. if( rc!
2d470 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
2d480 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
2d490 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 75 74 34 62 out;. }. put4b
2d4a0 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 yte(pPage->aData
2d4b0 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 20 70 75 74 , iTrunk);. put
2d4c0 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
2d4d0 61 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 20 70 75 ata[4], 0);. pu
2d4e0 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
2d4f0 61 44 61 74 61 5b 33 32 5d 2c 20 69 50 61 67 65 aData[32], iPage
2d500 29 3b 0a 20 20 54 52 41 43 45 28 28 22 46 52 45 );. TRACE(("FRE
2d510 45 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 74 E-PAGE: %d new t
2d520 72 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 63 runk page replac
2d530 69 6e 67 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 ing %d\n", pPage
2d540 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 6e 6b 29 29 ->pgno, iTrunk))
2d550 3b 0a 0a 66 72 65 65 70 61 67 65 5f 6f 75 74 3a ;..freepage_out:
2d560 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a . if( pPage ){.
2d570 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 pPage->isIni
2d580 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 6c t = 0;. }. rel
2d590 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
2d5a0 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
2d5b0 54 72 75 6e 6b 29 3b 0a 20 20 72 65 74 75 72 6e Trunk);. return
2d5c0 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f rc;.}.static vo
2d5d0 69 64 20 66 72 65 65 50 61 67 65 28 4d 65 6d 50 id freePage(MemP
2d5e0 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
2d5f0 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 *pRC){. if( (*p
2d600 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 RC)==SQLITE_OK )
2d610 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 66 72 65 {. *pRC = fre
2d620 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 42 ePage2(pPage->pB
2d630 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 2d t, pPage, pPage-
2d640 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f >pgno);. }.}../
2d650 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 *.** Free any ov
2d660 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 erflow pages ass
2d670 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
2d680 20 67 69 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a given Cell..*/.
2d690 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 static int clear
2d6a0 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 Cell(MemPage *pP
2d6b0 61 67 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 age, unsigned ch
2d6c0 61 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 ar *pCell){. Bt
2d6d0 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
2d6e0 61 67 65 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c age->pBt;. Cell
2d6f0 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e Info info;. Pgn
2d700 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e o ovflPgno;. in
2d710 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 t rc;. int nOvf
2d720 6c 3b 0a 20 20 75 33 32 20 6f 76 66 6c 50 61 67 l;. u32 ovflPag
2d730 65 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 eSize;.. assert
2d740 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
2d750 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
2d760 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 62 74 72 >mutex) );. btr
2d770 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
2d780 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
2d790 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e fo);. if( info.
2d7a0 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a iOverflow==0 ){.
2d7b0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
2d7c0 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 E_OK; /* No ove
2d7d0 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74 rflow pages. Ret
2d7e0 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e urn without doin
2d7f0 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 g anything */.
2d800 7d 0a 20 20 69 66 28 20 70 43 65 6c 6c 2b 69 6e }. if( pCell+in
2d810 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 2b 33 20 3e fo.iOverflow+3 >
2d820 20 70 50 61 67 65 2d 3e 61 44 61 74 61 2b 70 50 pPage->aData+pP
2d830 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 29 7b age->maskPage ){
2d840 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
2d850 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
2d860 20 20 2f 2a 20 43 65 6c 6c 20 65 78 74 65 6e 64 /* Cell extend
2d870 73 20 70 61 73 74 20 65 6e 64 20 6f 66 20 70 61 s past end of pa
2d880 67 65 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c ge */. }. ovfl
2d890 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 Pgno = get4byte(
2d8a0 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
2d8b0 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65 72 rflow]);. asser
2d8c0 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 t( pBt->usableSi
2d8d0 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66 6c ze > 4 );. ovfl
2d8e0 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e PageSize = pBt->
2d8f0 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a usableSize - 4;.
2d900 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e nOvfl = (info.
2d910 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e nPayload - info.
2d920 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 nLocal + ovflPag
2d930 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 eSize - 1)/ovflP
2d940 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 ageSize;. asser
2d950 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c t( ovflPgno==0 |
2d960 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 | nOvfl>0 );. w
2d970 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b hile( nOvfl-- ){
2d980 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 20 . Pgno iNext
2d990 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 = 0;. MemPage
2d9a0 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 20 *pOvfl = 0;.
2d9b0 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 if( ovflPgno<2
2d9c0 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 62 74 72 65 || ovflPgno>btre
2d9d0 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 ePagecount(pBt)
2d9e0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 73 ){. /* 0 is
2d9f0 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 67 not a legal pag
2da00 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 e number and pag
2da10 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e e 1 cannot be an
2da20 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 . ** overf
2da30 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 65 66 low page. Theref
2da40 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c ore if ovflPgno<
2da50 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 65 6e 2 or past the en
2da60 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 d of the .
2da70 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 74 61 ** file the data
2da80 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f 72 base must be cor
2da90 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 72 rupt. */. r
2daa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
2dab0 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
2dac0 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 29 . if( nOvfl )
2dad0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 {. rc = get
2dae0 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 OverflowPage(pBt
2daf0 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 , ovflPgno, &pOv
2db00 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 20 fl, &iNext);.
2db10 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
2db20 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
2db30 20 20 69 66 28 20 28 20 70 4f 76 66 6c 20 7c 7c if( ( pOvfl ||
2db40 20 28 28 70 4f 76 66 6c 20 3d 20 62 74 72 65 65 ((pOvfl = btree
2db50 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c 20 PageLookup(pBt,
2db60 6f 76 66 6c 50 67 6e 6f 29 29 21 3d 30 29 20 29 ovflPgno))!=0) )
2db70 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 . && sqlite3
2db80 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
2db90 74 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 t(pOvfl->pDbPage
2dba0 29 21 3d 31 0a 20 20 20 20 29 7b 0a 20 20 20 20 )!=1. ){.
2dbb0 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f /* There is no
2dbc0 20 72 65 61 73 6f 6e 20 61 6e 79 20 63 75 72 73 reason any curs
2dbd0 6f 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 or should have a
2dbe0 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 n outstanding re
2dbf0 66 65 72 65 6e 63 65 20 0a 20 20 20 20 20 20 2a ference . *
2dc00 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 * to an overflow
2dc10 20 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 page belonging
2dc20 74 6f 20 61 20 63 65 6c 6c 20 74 68 61 74 20 69 to a cell that i
2dc30 73 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2f s being deleted/
2dc40 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 2a updated.. *
2dc50 2a 20 53 6f 20 69 66 20 74 68 65 72 65 20 65 78 * So if there ex
2dc60 69 73 74 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f ists more than o
2dc70 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 ne reference to
2dc80 74 68 69 73 20 70 61 67 65 2c 20 74 68 65 6e 20 this page, then
2dc90 69 74 20 0a 20 20 20 20 20 20 2a 2a 20 6d 75 73 it . ** mus
2dca0 74 20 6e 6f 74 20 72 65 61 6c 6c 79 20 62 65 20 t not really be
2dcb0 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
2dcc0 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 and the databas
2dcd0 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 e must be corrup
2dce0 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 49 74 20 t. . ** It
2dcf0 69 73 20 68 65 6c 70 66 75 6c 20 74 6f 20 64 65 is helpful to de
2dd00 74 65 63 74 20 74 68 69 73 20 62 65 66 6f 72 65 tect this before
2dd10 20 63 61 6c 6c 69 6e 67 20 66 72 65 65 50 61 67 calling freePag
2dd20 65 32 28 29 2c 20 61 73 20 0a 20 20 20 20 20 20 e2(), as .
2dd30 2a 2a 20 66 72 65 65 50 61 67 65 32 28 29 20 6d ** freePage2() m
2dd40 61 79 20 7a 65 72 6f 20 74 68 65 20 70 61 67 65 ay zero the page
2dd50 20 63 6f 6e 74 65 6e 74 73 20 69 66 20 73 65 63 contents if sec
2dd60 75 72 65 2d 64 65 6c 65 74 65 20 6d 6f 64 65 20 ure-delete mode
2dd70 69 73 0a 20 20 20 20 20 20 2a 2a 20 65 6e 61 62 is. ** enab
2dd80 6c 65 64 2e 20 49 66 20 74 68 69 73 20 27 6f 76 led. If this 'ov
2dd90 65 72 66 6c 6f 77 27 20 70 61 67 65 20 68 61 70 erflow' page hap
2dda0 70 65 6e 73 20 74 6f 20 62 65 20 61 20 70 61 67 pens to be a pag
2ddb0 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 e that the.
2ddc0 20 2a 2a 20 63 61 6c 6c 65 72 20 69 73 20 69 74 ** caller is it
2ddd0 65 72 61 74 69 6e 67 20 74 68 72 6f 75 67 68 20 erating through
2dde0 6f 72 20 75 73 69 6e 67 20 69 6e 20 73 6f 6d 65 or using in some
2ddf0 20 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 other way, this
2de00 0a 20 20 20 20 20 20 2a 2a 20 63 61 6e 20 62 65 . ** can be
2de10 20 70 72 6f 62 6c 65 6d 61 74 69 63 2e 0a 20 20 problematic..
2de20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 */. rc
2de30 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
2de40 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 65 6c 73 65 _BKPT;. }else
2de50 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 72 65 {. rc = fre
2de60 65 50 61 67 65 32 28 70 42 74 2c 20 70 4f 76 66 ePage2(pBt, pOvf
2de70 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 l, ovflPgno);.
2de80 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f 76 }.. if( pOv
2de90 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 fl ){. sqli
2dea0 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f te3PagerUnref(pO
2deb0 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 vfl->pDbPage);.
2dec0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 }. if( rc
2ded0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
2dee0 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 ovflPgno = iNex
2def0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
2df00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
2df10 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 62 .** Create the b
2df20 79 74 65 20 73 65 71 75 65 6e 63 65 20 75 73 65 yte sequence use
2df30 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 d to represent a
2df40 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 cell on page pP
2df50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 age.** and write
2df60 20 74 68 61 74 20 62 79 74 65 20 73 65 71 75 65 that byte seque
2df70 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d nce into pCell[]
2df80 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 . Overflow page
2df90 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 s are.** allocat
2dfa0 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e ed and filled in
2dfb0 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20 20 as necessary.
2dfc0 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 The calling proc
2dfd0 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 edure.** is resp
2dfe0 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 onsible for maki
2dff0 6e 67 20 73 75 72 65 20 73 75 66 66 69 63 69 65 ng sure sufficie
2e000 6e 74 20 73 70 61 63 65 20 68 61 73 20 62 65 65 nt space has bee
2e010 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 n allocated.** f
2e020 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a or pCell[]..**.*
2e030 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 43 65 6c * Note that pCel
2e040 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 l does not neces
2e050 73 61 72 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 sary need to poi
2e060 6e 74 20 74 6f 20 74 68 65 20 70 50 61 67 65 2d nt to the pPage-
2e070 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 >aData.** area.
2e080 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 pCell might poi
2e090 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f nt to some tempo
2e0a0 72 61 72 79 20 73 74 6f 72 61 67 65 2e 20 20 54 rary storage. T
2e0b0 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 he cell will.**
2e0c0 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69 be constructed i
2e0d0 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 n this temporary
2e0e0 20 61 72 65 61 20 74 68 65 6e 20 63 6f 70 69 65 area then copie
2e0f0 64 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 d into pPage->aD
2e100 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f ata.** later..*/
2e110 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c .static int fill
2e120 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 InCell(. MemPag
2e130 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
2e140 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2e150 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 page that contai
2e160 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 ns the cell */.
2e170 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
2e180 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 pCell,
2e190 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
2e1a0 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a of the cell */.
2e1b0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b const void *pK
2e1c0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 ey, i64 nKey,
2e1d0 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 /* The key */.
2e1e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 const void *pDa
2e1f0 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 ta,int nData,
2e200 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 /* The data */.
2e210 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 int nZero,
2e220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e230 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f 20 62 79 /* Extra zero by
2e240 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f tes to append to
2e250 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 pData */. int
2e260 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 20 20 *pnSize
2e270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
2e280 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 ite cell size he
2e290 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e re */.){. int n
2e2a0 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 Payload;. const
2e2b0 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 u8 *pSrc;. int
2e2c0 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 nSrc, n, rc;.
2e2d0 69 6e 74 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 int spaceLeft;.
2e2e0 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 MemPage *pOvfl
2e2f0 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a = 0;. MemPage *
2e300 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a pToRelease = 0;.
2e310 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
2e320 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 *pPrior;. unsig
2e330 6e 65 64 20 63 68 61 72 20 2a 70 50 61 79 6c 6f ned char *pPaylo
2e340 61 64 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ad;. BtShared *
2e350 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 pBt = pPage->pBt
2e360 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 ;. Pgno pgnoOvf
2e370 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 l = 0;. int nHe
2e380 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f ader;. CellInfo
2e390 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74 info;.. assert
2e3a0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
2e3b0 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
2e3c0 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a >mutex) );.. /*
2e3d0 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 pPage is not ne
2e3e0 63 65 73 73 61 72 69 6c 79 20 77 72 69 74 65 61 cessarily writea
2e3f0 62 6c 65 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 ble since pCell
2e400 6d 69 67 68 74 20 62 65 20 61 75 78 69 6c 69 61 might be auxilia
2e410 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 ry. ** buffer s
2e420 70 61 63 65 20 74 68 61 74 20 69 73 20 73 65 70 pace that is sep
2e430 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 70 arate from the p
2e440 50 61 67 65 20 62 75 66 66 65 72 20 61 72 65 61 Page buffer area
2e450 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 */. assert( pC
2e460 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 ell<pPage->aData
2e470 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 || pCell>=&pPag
2e480 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 e->aData[pBt->pa
2e490 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 geSize].
2e4a0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 || sqlite3Pa
2e4b0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
2e4c0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
2e4d0 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 ;.. /* Fill in
2e4e0 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 the header. */.
2e4f0 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 nHeader = 0;.
2e500 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
2e510 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 ){. nHeader
2e520 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 += 4;. }. if(
2e530 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
2e540 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d {. nHeader +=
2e550 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 70 43 putVarint32(&pC
2e560 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44 ell[nHeader], nD
2e570 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 ata+nZero);. }e
2e580 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d lse{. nData =
2e590 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a nZero = 0;. }.
2e5a0 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 nHeader += put
2e5b0 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 Varint(&pCell[nH
2e5c0 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 eader], *(u64*)&
2e5d0 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 50 61 nKey);. btreePa
2e5e0 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
2e5f0 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
2e600 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e . assert( info.
2e610 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 65 72 nHeader==nHeader
2e620 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e );. assert( in
2e630 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29 3b fo.nKey==nKey );
2e640 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e . assert( info.
2e650 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e 44 61 nData==(u32)(nDa
2e660 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20 0a ta+nZero) );. .
2e670 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
2e680 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50 payload */. nP
2e690 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b ayload = nData +
2e6a0 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50 nZero;. if( pP
2e6b0 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
2e6c0 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b pSrc = pData;
2e6d0 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 . nSrc = nDat
2e6e0 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 30 a;. nData = 0
2e6f0 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20 ;. }else{ .
2e700 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 3e 30 if( NEVER(nKey>0
2e710 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b 65 x7fffffff || pKe
2e720 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 72 y==0) ){. r
2e730 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
2e740 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
2e750 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d . nPayload +=
2e760 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20 20 (int)nKey;.
2e770 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20 20 pSrc = pKey;.
2e780 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b 65 nSrc = (int)nKe
2e790 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a 65 y;. }. *pnSize
2e7a0 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20 = info.nSize;.
2e7b0 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e 66 spaceLeft = inf
2e7c0 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61 79 o.nLocal;. pPay
2e7d0 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 load = &pCell[nH
2e7e0 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f 72 eader];. pPrior
2e7f0 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 = &pCell[info.i
2e800 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68 Overflow];.. wh
2e810 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30 20 ile( nPayload>0
2e820 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63 65 ){. if( space
2e830 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 Left==0 ){.#ifnd
2e840 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
2e850 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
2e860 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70 20 Pgno pgnoPtrmap
2e870 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f = pgnoOvfl; /* O
2e880 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 verflow page poi
2e890 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 70 nter-map entry p
2e8a0 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 age */. if(
2e8b0 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
2e8c0 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b 0a ){. do{.
2e8d0 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f 76 pgnoOv
2e8e0 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 20 fl++;. }
2e8f0 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20 20 while( .
2e900 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 PTRMAP_ISPAGE(
2e910 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 7c pBt, pgnoOvfl) |
2e920 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44 | pgnoOvfl==PEND
2e930 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
2e940 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 t) . );.
2e950 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
2e960 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
2e970 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
2e980 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66 &pOvfl, &pgnoOvf
2e990 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29 3b l, pgnoOvfl, 0);
2e9a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2e9b0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
2e9c0 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
2e9d0 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
2e9e0 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61 s auto-vacuum, a
2e9f0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 nd the second or
2ea00 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20 subsequent.
2ea10 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 ** overflow pa
2ea20 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f ge is being allo
2ea30 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e cated, add an en
2ea40 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 try to the point
2ea50 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20 er-map. **
2ea60 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f for that page no
2ea70 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 w. . **.
2ea80 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 ** If this is
2ea90 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
2eaa0 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77 low page, then w
2eab0 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65 rite a partial e
2eac0 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74 ntry . ** t
2ead0 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
2eae0 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e p. If we write n
2eaf0 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70 othing to this p
2eb00 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c ointer-map slot,
2eb10 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 . ** then t
2eb20 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76 he optimistic ov
2eb30 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f erflow chain pro
2eb40 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72 cessing in clear
2eb50 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20 Cell(). **
2eb60 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74 may misinterpret
2eb70 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a the uninitializ
2eb80 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65 ed values and de
2eb90 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a lete the. *
2eba0 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72 * wrong pages fr
2ebb0 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e om the database.
2ebc0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
2ebd0 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
2ebe0 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 uum && rc==SQLIT
2ebf0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
2ec00 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f u8 eType = (pgno
2ec10 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56 Ptrmap?PTRMAP_OV
2ec20 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f ERFLOW2:PTRMAP_O
2ec30 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20 VERFLOW1);.
2ec40 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
2ec50 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 , pgnoOvfl, eTyp
2ec60 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c 20 26 e, pgnoPtrmap, &
2ec70 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 rc);. if(
2ec80 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
2ec90 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76 releasePage(pOv
2eca0 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 fl);. }.
2ecb0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
2ecc0 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
2ecd0 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
2ece0 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 e(pToRelease);.
2ecf0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
2ed00 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
2ed10 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 /* If pToReleas
2ed20 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 e is not zero th
2ed30 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74 73 an pPrior points
2ed40 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 into the data a
2ed50 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 rea. ** of
2ed60 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b pToRelease. Mak
2ed70 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 e sure pToReleas
2ed80 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 e is still write
2ed90 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 able. */. a
2eda0 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 ssert( pToReleas
2edb0 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 e==0 || sqlite3P
2edc0 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
2edd0 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 pToRelease->pDbP
2ede0 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 2f age) );.. /
2edf0 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20 70 * If pPrior is p
2ee00 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
2ee10 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74 area of pPage, t
2ee20 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50 hen make sure pP
2ee30 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 age. ** is
2ee40 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 20 still writeable
2ee50 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
2ee60 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e 61 pPrior<pPage->a
2ee70 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e 3d Data || pPrior>=
2ee80 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
2ee90 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 t->pageSize].
2eea0 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 || sqli
2eeb0 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
2eec0 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
2eed0 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70 75 ge) );.. pu
2eee0 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 70 t4byte(pPrior, p
2eef0 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 gnoOvfl);.
2ef00 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
2ef10 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 70 elease);. p
2ef20 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76 66 ToRelease = pOvf
2ef30 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 20 l;. pPrior
2ef40 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b 0a = pOvfl->aData;.
2ef50 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
2ef60 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20 Prior, 0);.
2ef70 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f 76 pPayload = &pOv
2ef80 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20 20 fl->aData[4];.
2ef90 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 spaceLeft =
2efa0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
2efb0 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e - 4;. }. n
2efc0 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 = nPayload;.
2efd0 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 74 if( n>spaceLeft
2efe0 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 74 ) n = spaceLeft
2eff0 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f ;.. /* If pTo
2f000 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a Release is not z
2f010 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f 61 ero than pPayloa
2f020 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 d points into th
2f030 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 e data area.
2f040 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 ** of pToRelease
2f050 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f . Make sure pTo
2f060 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c Release is still
2f070 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 writeable. */.
2f080 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 assert( pToRe
2f090 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 lease==0 || sqli
2f0a0 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
2f0b0 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e ble(pToRelease->
2f0c0 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 pDbPage) );..
2f0d0 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64 20 /* If pPayload
2f0e0 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 is part of the d
2f0f0 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 ata area of pPag
2f100 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 e, then make sur
2f110 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20 69 e pPage. ** i
2f120 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
2f130 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 e */. assert(
2f140 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65 2d pPayload<pPage-
2f150 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c 6f >aData || pPaylo
2f160 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 ad>=&pPage->aDat
2f170 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d a[pBt->pageSize]
2f180 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
2f190 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
2f1a0 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
2f1b0 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 DbPage) );..
2f1c0 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20 20 if( nSrc>0 ){.
2f1d0 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20 29 if( n>nSrc )
2f1e0 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 20 n = nSrc;.
2f1f0 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b assert( pSrc );
2f200 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 50 . memcpy(pP
2f210 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e 29 ayload, pSrc, n)
2f220 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
2f230 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c 6f memset(pPaylo
2f240 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20 7d ad, 0, n);. }
2f250 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d 3d . nPayload -=
2f260 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61 64 n;. pPayload
2f270 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63 20 += n;. pSrc
2f280 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20 2d += n;. nSrc -
2f290 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c 65 = n;. spaceLe
2f2a0 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66 28 ft -= n;. if(
2f2b0 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 nSrc==0 ){.
2f2c0 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a nSrc = nData;.
2f2d0 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 pSrc = pDa
2f2e0 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ta;. }. }.
2f2f0 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
2f300 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75 72 elease);. retur
2f310 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
2f320 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 /*.** Remove the
2f330 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20 i-th cell from
2f340 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75 pPage. This rou
2f350 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61 tine effects pPa
2f360 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 20 ge only..** The
2f370 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 cell content is
2f380 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 61 not freed or dea
2f390 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 73 llocated. It is
2f3a0 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a assumed that.**
2f3b0 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
2f3c0 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 65 t has been copie
2f3d0 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 65 d someplace else
2f3e0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
2f3f0 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 20 just.** removes
2f400 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f the reference to
2f410 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 70 the cell from p
2f420 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 Page..**.** "sz"
2f430 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 6d must be the num
2f440 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
2f450 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 the cell..*/.sta
2f460 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65 6c tic void dropCel
2f470 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 l(MemPage *pPage
2f480 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 73 , int idx, int s
2f490 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 z, int *pRC){.
2f4a0 75 33 32 20 70 63 3b 20 20 20 20 20 20 20 20 20 u32 pc;
2f4b0 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 65 6c /* Offset to cel
2f4c0 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63 65 6c l content of cel
2f4d0 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 l being deleted
2f4e0 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
2f4f0 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 61 /* pPage->a
2f500 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 70 74 Data */. u8 *pt
2f510 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 r; /* Use
2f520 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65 73 20 d to move bytes
2f530 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20 64 61 around within da
2f540 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ta[] */. int rc
2f550 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
2f560 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a return code */.
2f570 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 int hdr;
2f580 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f /* Beginning o
2f590 66 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 30 f the header. 0
2f5a0 20 6d 6f 73 74 20 70 61 67 65 73 2e 20 20 31 30 most pages. 10
2f5b0 30 20 70 61 67 65 20 31 20 2a 2f 0a 0a 20 20 69 0 page 1 */.. i
2f5c0 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e f( *pRC ) return
2f5d0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 ;.. assert( idx
2f5e0 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 65 >=0 && idx<pPage
2f5f0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 ->nCell );. ass
2f600 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a ert( sz==cellSiz
2f610 65 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 3b e(pPage, idx) );
2f620 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
2f630 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
2f640 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
2f650 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
2f660 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
2f670 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
2f680 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 20 utex) );. data
2f690 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
2f6a0 20 20 70 74 72 20 3d 20 26 70 50 61 67 65 2d 3e ptr = &pPage->
2f6b0 61 43 65 6c 6c 49 64 78 5b 32 2a 69 64 78 5d 3b aCellIdx[2*idx];
2f6c0 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 . pc = get2byte
2f6d0 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70 (ptr);. hdr = p
2f6e0 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
2f6f0 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d . testcase( pc=
2f700 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b =get2byte(&data[
2f710 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73 hdr+5]) );. tes
2f720 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 tcase( pc+sz==pP
2f730 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
2f740 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63 Size );. if( pc
2f750 20 3c 20 28 75 33 32 29 67 65 74 32 62 79 74 65 < (u32)get2byte
2f760 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 7c (&data[hdr+5]) |
2f770 7c 20 70 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d | pc+sz > pPage-
2f780 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
2f790 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 ){. *pRC = S
2f7a0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
2f7b0 50 54 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a PT;. return;.
2f7c0 20 20 7d 0a 20 20 72 63 20 3d 20 66 72 65 65 53 }. rc = freeS
2f7d0 70 61 63 65 28 70 50 61 67 65 2c 20 70 63 2c 20 pace(pPage, pc,
2f7e0 73 7a 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b sz);. if( rc ){
2f7f0 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a . *pRC = rc;.
2f800 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
2f810 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d pPage->nCell--
2f820 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28 70 74 72 2c ;. memmove(ptr,
2f830 20 70 74 72 2b 32 2c 20 32 2a 28 70 50 61 67 65 ptr+2, 2*(pPage
2f840 2d 3e 6e 43 65 6c 6c 20 2d 20 69 64 78 29 29 3b ->nCell - idx));
2f850 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
2f860 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d a[hdr+3], pPage-
2f870 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67 65 >nCell);. pPage
2f880 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 7d 0a ->nFree += 2;.}.
2f890 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 ./*.** Insert a
2f8a0 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 new cell on pPag
2f8b0 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 e at cell index
2f8c0 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e "i". pCell poin
2f8d0 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e ts to the.** con
2f8e0 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c tent of the cell
2f8f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
2f900 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c ell content will
2f910 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 fit on the page
2f920 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68 , then put it th
2f930 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77 ere. If it.** w
2f940 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65 ill not fit, the
2f950 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 n make a copy of
2f960 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
2f970 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a t into pTemp if.
2f980 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20 ** pTemp is not
2f990 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73 null. Regardles
2f9a0 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f s of pTemp, allo
2f9b0 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 cate a new entry
2f9c0 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 70 .** in pPage->ap
2f9d0 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 Ovfl[] and make
2f9e0 69 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 it point to the
2f9f0 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 cell content (ei
2fa00 74 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 ther.** in pTemp
2fa10 20 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c or the original
2fa20 20 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f pCell) and also
2fa30 20 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 record its inde
2fa40 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e x. .** Allocatin
2fa50 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e g a new entry in
2fa60 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 pPage->aCell[]
2fa70 69 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a implies that .**
2fa80 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
2fa90 77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 w is incremented
2faa0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 ..**.** If nSkip
2fab0 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
2fac0 65 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 en do not copy t
2fad0 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 he first nSkip b
2fae0 79 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 ytes of the.** c
2faf0 65 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 ell. The caller
2fb00 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 will overwrite t
2fb10 68 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 hem after this f
2fb20 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e unction returns.
2fb30 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 If.** nSkip is
2fb40 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 non-zero, then p
2fb50 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 Cell may not poi
2fb60 6e 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 nt to an invalid
2fb70 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
2fb80 20 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b .** (but pCell+
2fb90 6e 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 nSkip is always
2fba0 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 valid)..*/.stati
2fbb0 63 20 76 6f 69 64 20 69 6e 73 65 72 74 43 65 6c c void insertCel
2fbc0 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 l(. MemPage *pP
2fbd0 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 age, /* Page i
2fbe0 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 nto which we are
2fbf0 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e copying */. in
2fc00 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 t i,
2fc10 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f /* New cell beco
2fc20 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c mes the i-th cel
2fc30 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f l of the page */
2fc40 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 . u8 *pCell,
2fc50 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 /* Content
2fc60 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 of the new cell
2fc70 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 */. int sz,
2fc80 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
2fc90 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 of content in pC
2fca0 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 ell */. u8 *pTe
2fcb0 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 mp, /* Te
2fcc0 6d 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 mp storage space
2fcd0 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e for pCell, if n
2fce0 65 65 64 65 64 20 2a 2f 0a 20 20 50 67 6e 6f 20 eeded */. Pgno
2fcf0 69 43 68 69 6c 64 2c 20 20 20 20 20 20 2f 2a 20 iChild, /*
2fd00 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 72 65 70 If non-zero, rep
2fd10 6c 61 63 65 20 66 69 72 73 74 20 34 20 62 79 74 lace first 4 byt
2fd20 65 73 20 77 69 74 68 20 74 68 69 73 20 76 61 6c es with this val
2fd30 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 43 ue */. int *pRC
2fd40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 /* Rea
2fd50 64 20 61 6e 64 20 77 72 69 74 65 20 72 65 74 75 d and write retu
2fd60 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 68 65 72 rn code from her
2fd70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 e */.){. int id
2fd80 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 57 x = 0; /* W
2fd90 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e 65 here to write ne
2fda0 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 w cell content i
2fdb0 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e n data[] */. in
2fdc0 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 t j;
2fdd0 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
2fde0 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20 20 */. int end;
2fdf0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
2fe00 62 79 74 65 20 70 61 73 74 20 74 68 65 20 6c 61 byte past the la
2fe10 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 st cell pointer
2fe20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 in data[] */. i
2fe30 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20 20 nt ins;
2fe40 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61 74 /* Index in dat
2fe50 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63 65 a[] where new ce
2fe60 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69 6e ll pointer is in
2fe70 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 serted */. int
2fe80 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f 2a cellOffset; /*
2fe90 20 41 64 64 72 65 73 73 20 6f 66 20 66 69 72 73 Address of firs
2fea0 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 t cell pointer i
2feb0 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 n data[] */. u8
2fec0 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 *data;
2fed0 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f /* The content o
2fee0 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67 65 f the whole page
2fef0 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 */. int nSkip
2ff00 3d 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 = (iChild ? 4 :
2ff10 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 0);.. if( *pRC
2ff20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 ) return;.. ass
2ff30 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d ert( i>=0 && i<=
2ff40 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 pPage->nCell+pPa
2ff50 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b ge->nOverflow );
2ff60 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
2ff70 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c ->nCell<=MX_CELL
2ff80 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 (pPage->pBt) &&
2ff90 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
2ffa0 42 74 29 3c 3d 31 30 39 32 31 20 29 3b 0a 20 20 Bt)<=10921 );.
2ffb0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
2ffc0 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 Overflow<=ArrayS
2ffd0 69 7a 65 28 70 50 61 67 65 2d 3e 61 70 4f 76 66 ize(pPage->apOvf
2ffe0 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 l) );. assert(
2fff0 41 72 72 61 79 53 69 7a 65 28 70 50 61 67 65 2d ArraySize(pPage-
30000 3e 61 70 4f 76 66 6c 29 3d 3d 41 72 72 61 79 53 >apOvfl)==ArrayS
30010 69 7a 65 28 70 50 61 67 65 2d 3e 61 69 4f 76 66 ize(pPage->aiOvf
30020 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 l) );. assert(
30030 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
30040 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
30050 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 utex) );. /* Th
30060 65 20 63 65 6c 6c 20 73 68 6f 75 6c 64 20 6e 6f e cell should no
30070 72 6d 61 6c 6c 79 20 62 65 20 73 69 7a 65 64 20 rmally be sized
30080 63 6f 72 72 65 63 74 6c 79 2e 20 20 48 6f 77 65 correctly. Howe
30090 76 65 72 2c 20 77 68 65 6e 20 6d 6f 76 69 6e 67 ver, when moving
300a0 20 61 0a 20 20 2a 2a 20 6d 61 6c 66 6f 72 6d 65 a. ** malforme
300b0 64 20 63 65 6c 6c 20 66 72 6f 6d 20 61 20 6c 65 d cell from a le
300c0 61 66 20 70 61 67 65 20 74 6f 20 61 6e 20 69 6e af page to an in
300d0 74 65 72 69 6f 72 20 70 61 67 65 2c 20 69 66 20 terior page, if
300e0 74 68 65 20 63 65 6c 6c 20 73 69 7a 65 0a 20 20 the cell size.
300f0 2a 2a 20 77 61 6e 74 65 64 20 74 6f 20 62 65 20 ** wanted to be
30100 6c 65 73 73 20 74 68 61 6e 20 34 20 62 75 74 20 less than 4 but
30110 67 6f 74 20 72 6f 75 6e 64 65 64 20 75 70 20 74 got rounded up t
30120 6f 20 34 20 6f 6e 20 74 68 65 20 6c 65 61 66 2c o 4 on the leaf,
30130 20 74 68 65 6e 20 73 69 7a 65 0a 20 20 2a 2a 20 then size. **
30140 6d 69 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 might be less th
30150 61 6e 20 38 20 28 6c 65 61 66 2d 73 69 7a 65 20 an 8 (leaf-size
30160 2b 20 70 6f 69 6e 74 65 72 29 20 6f 6e 20 74 68 + pointer) on th
30170 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e e interior node.
30180 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 Hence. ** the
30190 20 74 65 72 6d 20 61 66 74 65 72 20 74 68 65 20 term after the
301a0 7c 7c 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 || in the follow
301b0 69 6e 67 20 61 73 73 65 72 74 28 29 2e 20 2a 2f ing assert(). */
301c0 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 . assert( sz==c
301d0 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
301e0 2c 20 70 43 65 6c 6c 29 20 7c 7c 20 28 73 7a 3d , pCell) || (sz=
301f0 3d 38 20 26 26 20 69 43 68 69 6c 64 3e 30 29 20 =8 && iChild>0)
30200 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e );. if( pPage->
30210 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b nOverflow || sz+
30220 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 2>pPage->nFree )
30230 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 {. if( pTemp
30240 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
30250 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 pTemp+nSkip, pCe
30260 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b ll+nSkip, sz-nSk
30270 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c ip);. pCell
30280 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a = pTemp;. }.
30290 20 20 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 if( iChild )
302a0 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 {. put4byte
302b0 28 70 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b (pCell, iChild);
302c0 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 . }. j = p
302d0 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b Page->nOverflow+
302e0 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a +;. assert( j
302f0 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 <(int)(sizeof(pP
30300 61 67 65 2d 3e 61 70 4f 76 66 6c 29 2f 73 69 7a age->apOvfl)/siz
30310 65 6f 66 28 70 50 61 67 65 2d 3e 61 70 4f 76 66 eof(pPage->apOvf
30320 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 l[0])) );. pP
30330 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 6a 5d 20 3d age->apOvfl[j] =
30340 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 pCell;. pPag
30350 65 2d 3e 61 69 4f 76 66 6c 5b 6a 5d 20 3d 20 28 e->aiOvfl[j] = (
30360 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a u16)i;. }else{.
30370 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c int rc = sql
30380 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
30390 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
303a0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
303b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2a TE_OK ){. *
303c0 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 pRC = rc;.
303d0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
303e0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
303f0 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
30400 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
30410 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 ) );. data =
30420 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
30430 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 cellOffset = p
30440 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
30450 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65 6c 6c ;. end = cell
30460 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 Offset + 2*pPage
30470 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 6e 73 ->nCell;. ins
30480 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 = cellOffset +
30490 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20 61 6c 2*i;. rc = al
304a0 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67 locateSpace(pPag
304b0 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a 20 20 e, sz, &idx);.
304c0 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70 52 43 if( rc ){ *pRC
304d0 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b 20 7d = rc; return; }
304e0 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c 6f . /* The allo
304f0 63 61 74 65 53 70 61 63 65 28 29 20 72 6f 75 74 cateSpace() rout
30500 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73 20 74 ine guarantees t
30510 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f he following two
30520 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20 20 20 properties.
30530 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72 6e 73 ** if it returns
30540 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 success */.
30550 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d 20 65 assert( idx >= e
30560 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 nd+2 );. asse
30570 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 28 69 rt( idx+sz <= (i
30580 6e 74 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 nt)pPage->pBt->u
30590 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 sableSize );.
305a0 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b pPage->nCell++;
305b0 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 . pPage->nFre
305c0 65 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 e -= (u16)(2 + s
305d0 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 z);. memcpy(&
305e0 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c data[idx+nSkip],
305f0 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a pCell+nSkip, sz
30600 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 -nSkip);. if(
30610 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 iChild ){.
30620 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b put4byte(&data[
30630 69 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 idx], iChild);.
30640 20 20 20 7d 0a 20 20 20 20 6d 65 6d 6d 6f 76 65 }. memmove
30650 28 26 64 61 74 61 5b 69 6e 73 2b 32 5d 2c 20 26 (&data[ins+2], &
30660 64 61 74 61 5b 69 6e 73 5d 2c 20 65 6e 64 2d 69 data[ins], end-i
30670 6e 73 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 ns);. put2byt
30680 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69 64 e(&data[ins], id
30690 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 x);. put2byte
306a0 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 (&data[pPage->hd
306b0 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67 rOffset+3], pPag
306c0 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 e->nCell);.#ifnd
306d0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
306e0 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
306f0 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 ( pPage->pBt->au
30700 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
30710 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 /* The cell ma
30720 79 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e y contain a poin
30730 74 65 72 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c ter to an overfl
30740 6f 77 20 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 ow page. If so,
30750 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 write. ** t
30760 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 he entry for the
30770 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
30780 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 nto the pointer
30790 6d 61 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 map.. */.
307a0 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 ptrmapPutOvf
307b0 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c lPtr(pPage, pCel
307c0 6c 2c 20 70 52 43 29 3b 0a 20 20 20 20 7d 0a 23 l, pRC);. }.#
307d0 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a endif. }.}../*.
307e0 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20 6f 66 ** Add a list of
307f0 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61 67 65 cells to a page
30800 2e 20 20 54 68 65 20 70 61 67 65 20 73 68 6f 75 . The page shou
30810 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c 79 20 ld be initially
30820 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20 63 65 empty..** The ce
30830 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 lls are guarante
30840 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 ed to fit on the
30850 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 page..*/.static
30860 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65 50 61 void assemblePa
30870 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ge(. MemPage *p
30880 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65 20 70 Page, /* The p
30890 61 67 65 20 74 6f 20 62 65 20 61 73 73 65 6d 62 age to be assemb
308a0 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 lied */. int nC
308b0 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 ell, /* T
308c0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c he number of cel
308d0 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74 68 69 ls to add to thi
308e0 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a s page */. u8 *
308f0 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 2f 2a *apCell, /*
30900 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63 65 6c Pointers to cel
30910 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20 75 31 l bodies */. u1
30920 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20 20 20 6 *aSize
30930 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68 65 20 /* Sizes of the
30940 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e cells */.){. in
30950 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
30960 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
30970 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 70 74 */. u8 *pCellpt
30980 72 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 r; /* Addres
30990 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 70 s of next cell p
309a0 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ointer */. int
309b0 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20 20 2f 2a cellbody; /*
309c0 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 Address of next
309d0 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f 0a 20 20 cell body */.
309e0 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20 u8 * const data
309f0 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20 = pPage->aData;
30a00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
30a10 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 66 ointer to data f
30a20 6f 72 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 6f or pPage */. co
30a30 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50 nst int hdr = pP
30a40 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 age->hdrOffset;
30a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
30a60 73 65 74 20 6f 66 20 68 65 61 64 65 72 20 6f 6e set of header on
30a70 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 6f 6e 73 pPage */. cons
30a80 74 20 69 6e 74 20 6e 55 73 61 62 6c 65 20 3d 20 t int nUsable =
30a90 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
30aa0 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73 61 62 6c leSize; /* Usabl
30ab0 65 20 73 69 7a 65 20 6f 66 20 70 61 67 65 20 2a e size of page *
30ac0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 /.. assert( pPa
30ad0 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ge->nOverflow==0
30ae0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
30af0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
30b00 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
30b10 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
30b20 20 6e 43 65 6c 6c 3e 3d 30 20 26 26 20 6e 43 65 nCell>=0 && nCe
30b30 6c 6c 3c 3d 28 69 6e 74 29 4d 58 5f 43 45 4c 4c ll<=(int)MX_CELL
30b40 28 70 50 61 67 65 2d 3e 70 42 74 29 0a 20 20 20 (pPage->pBt).
30b50 20 20 20 20 20 20 20 20 20 26 26 20 28 69 6e 74 && (int
30b60 29 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e )MX_CELL(pPage->
30b70 70 42 74 29 3c 3d 31 30 39 32 31 29 3b 0a 20 20 pBt)<=10921);.
30b80 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
30b90 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
30ba0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
30bb0 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 );.. /* Check t
30bc0 68 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 hat the page has
30bd0 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 just been zeroe
30be0 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 d by zeroPage()
30bf0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
30c00 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a ge->nCell==0 );.
30c10 20 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 assert( get2by
30c20 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61 5b teNotZero(&data[
30c30 68 64 72 2b 35 5d 29 3d 3d 6e 55 73 61 62 6c 65 hdr+5])==nUsable
30c40 20 29 3b 0a 0a 20 20 70 43 65 6c 6c 70 74 72 20 );.. pCellptr
30c50 3d 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 = &pPage->aCellI
30c60 64 78 5b 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 63 dx[nCell*2];. c
30c70 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62 6c ellbody = nUsabl
30c80 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c e;. for(i=nCell
30c90 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a -1; i>=0; i--){.
30ca0 20 20 20 20 75 31 36 20 73 7a 20 3d 20 61 53 69 u16 sz = aSi
30cb0 7a 65 5b 69 5d 3b 0a 20 20 20 20 70 43 65 6c 6c ze[i];. pCell
30cc0 70 74 72 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 ptr -= 2;. ce
30cd0 6c 6c 62 6f 64 79 20 2d 3d 20 73 7a 3b 0a 20 20 llbody -= sz;.
30ce0 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c put2byte(pCell
30cf0 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a ptr, cellbody);.
30d00 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
30d10 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65 [cellbody], apCe
30d20 6c 6c 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 7d 0a ll[i], sz);. }.
30d30 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
30d40 5b 68 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b [hdr+3], nCell);
30d50 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
30d60 61 5b 68 64 72 2b 35 5d 2c 20 63 65 6c 6c 62 6f a[hdr+5], cellbo
30d70 64 79 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 dy);. pPage->nF
30d80 72 65 65 20 2d 3d 20 28 6e 43 65 6c 6c 2a 32 20 ree -= (nCell*2
30d90 2b 20 6e 55 73 61 62 6c 65 20 2d 20 63 65 6c 6c + nUsable - cell
30da0 62 6f 64 79 29 3b 0a 20 20 70 50 61 67 65 2d 3e body);. pPage->
30db0 6e 43 65 6c 6c 20 3d 20 28 75 31 36 29 6e 43 65 nCell = (u16)nCe
30dc0 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ll;.}../*.** The
30dd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 72 61 6d following param
30de0 65 74 65 72 73 20 64 65 74 65 72 6d 69 6e 65 20 eters determine
30df0 68 6f 77 20 6d 61 6e 79 20 61 64 6a 61 63 65 6e how many adjacen
30e00 74 20 70 61 67 65 73 20 67 65 74 20 69 6e 76 6f t pages get invo
30e10 6c 76 65 64 0a 2a 2a 20 69 6e 20 61 20 62 61 6c lved.** in a bal
30e20 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ancing operation
30e30 2e 20 20 4e 4e 20 69 73 20 74 68 65 20 6e 75 6d . NN is the num
30e40 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 ber of neighbors
30e50 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 0a on either side.
30e60 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 ** of the page t
30e70 68 61 74 20 70 61 72 74 69 63 69 70 61 74 65 20 hat participate
30e80 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 in the balancing
30e90 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 42 20 operation. NB
30ea0 69 73 20 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 is the.** total
30eb0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
30ec0 74 68 61 74 20 70 61 72 74 69 63 69 70 61 74 65 that participate
30ed0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 , including the
30ee0 74 61 72 67 65 74 20 70 61 67 65 20 61 6e 64 0a target page and.
30ef0 2a 2a 20 4e 4e 20 6e 65 69 67 68 62 6f 72 73 20 ** NN neighbors
30f00 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 2e 0a on either side..
30f10 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 **.** The minimu
30f20 6d 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20 69 73 m value of NN is
30f30 20 31 20 28 6f 66 20 63 6f 75 72 73 65 29 2e 20 1 (of course).
30f40 20 49 6e 63 72 65 61 73 69 6e 67 20 4e 4e 20 61 Increasing NN a
30f50 62 6f 76 65 20 31 0a 2a 2a 20 28 74 6f 20 32 20 bove 1.** (to 2
30f60 6f 72 20 33 29 20 67 69 76 65 73 20 61 20 6d 6f or 3) gives a mo
30f70 64 65 73 74 20 69 6d 70 72 6f 76 65 6d 65 6e 74 dest improvement
30f80 20 69 6e 20 53 45 4c 45 43 54 20 61 6e 64 20 44 in SELECT and D
30f90 45 4c 45 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 ELETE performanc
30fa0 65 0a 2a 2a 20 69 6e 20 65 78 63 68 61 6e 67 65 e.** in exchange
30fb0 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 64 65 for a larger de
30fc0 67 72 61 64 61 74 69 6f 6e 20 69 6e 20 49 4e 53 gradation in INS
30fd0 45 52 54 20 61 6e 64 20 55 50 44 41 54 45 20 70 ERT and UPDATE p
30fe0 65 72 66 6f 72 6d 61 6e 63 65 2e 0a 2a 2a 20 54 erformance..** T
30ff0 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20 61 he value of NN a
31000 70 70 65 61 72 73 20 74 6f 20 67 69 76 65 20 74 ppears to give t
31010 68 65 20 62 65 73 74 20 72 65 73 75 6c 74 73 20 he best results
31020 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a 23 64 65 66 overall..*/.#def
31030 69 6e 65 20 4e 4e 20 31 20 20 20 20 20 20 20 20 ine NN 1
31040 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
31050 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 f neighbors on e
31060 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 ither side of pP
31070 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e age */.#define N
31080 42 20 28 4e 4e 2a 32 2b 31 29 20 20 20 20 20 20 B (NN*2+1)
31090 2f 2a 20 54 6f 74 61 6c 20 70 61 67 65 73 20 69 /* Total pages i
310a0 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 62 nvolved in the b
310b0 61 6c 61 6e 63 65 20 2a 2f 0a 0a 0a 23 69 66 6e alance */...#ifn
310c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
310d0 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a QUICKBALANCE./*.
310e0 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
310f0 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e of balance() han
31100 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 dles the common
31110 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 special case whe
31120 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 re.** a new entr
31130 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 y is being inser
31140 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65 ted on the extre
31150 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 me right-end of
31160 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 the.** tree, in
31170 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65 other words, whe
31180 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 n the new entry
31190 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 will become the
311a0 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 largest.** entry
311b0 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a in the tree..**
311c0 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74 .** Instead of t
311d0 72 79 69 6e 67 20 74 6f 20 62 61 6c 61 6e 63 65 rying to balance
311e0 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 the 3 right-mos
311f0 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75 t leaf pages, ju
31200 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 st add.** a new
31210 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 67 68 page to the righ
31220 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64 20 t-hand side and
31230 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 put the one new
31240 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 entry in.** that
31250 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65 61 page. This lea
31260 76 65 73 20 74 68 65 20 72 69 67 68 74 20 73 69 ves the right si
31270 64 65 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 de of the tree s
31280 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c omewhat.** unbal
31290 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64 73 anced. But odds
312a0 20 61 72 65 20 74 68 61 74 20 77 65 20 77 69 6c are that we wil
312b0 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e l be inserting n
312c0 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 ew entries.** at
312d0 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 the end soon af
312e0 74 65 72 77 61 72 64 73 20 73 6f 20 74 68 65 20 terwards so the
312f0 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61 67 nearly empty pag
31300 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a e will quickly.*
31310 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 * fill up. On a
31320 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 verage..**.** pP
31330 61 67 65 20 69 73 20 74 68 65 20 6c 65 61 66 20 age is the leaf
31340 70 61 67 65 20 77 68 69 63 68 20 69 73 20 74 68 page which is th
31350 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 e right-most pag
31360 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a e in the tree..*
31370 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69 74 73 * pParent is its
31380 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65 20 parent. pPage
31390 6d 75 73 74 20 68 61 76 65 20 61 20 73 69 6e 67 must have a sing
313a0 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 le overflow entr
313b0 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 6c y.** which is al
313c0 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 so the right-mos
313d0 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 t entry on the p
313e0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 age..**.** The p
313f0 53 70 61 63 65 20 62 75 66 66 65 72 20 69 73 20 Space buffer is
31400 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 used to store a
31410 74 65 6d 70 6f 72 61 72 79 20 63 6f 70 79 20 6f temporary copy o
31420 66 20 74 68 65 20 64 69 76 69 64 65 72 0a 2a 2a f the divider.**
31430 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 cell that will
31440 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f be inserted into
31450 20 70 50 61 72 65 6e 74 2e 20 53 75 63 68 20 61 pParent. Such a
31460 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f cell consists o
31470 66 20 61 20 34 0a 2a 2a 20 62 79 74 65 20 70 61 f a 4.** byte pa
31480 67 65 20 6e 75 6d 62 65 72 20 66 6f 6c 6c 6f 77 ge number follow
31490 65 64 20 62 79 20 61 20 76 61 72 69 61 62 6c 65 ed by a variable
314a0 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2e length integer.
314b0 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 In other.** wor
314c0 64 73 2c 20 61 74 20 6d 6f 73 74 20 31 33 20 62 ds, at most 13 b
314d0 79 74 65 73 2e 20 48 65 6e 63 65 20 74 68 65 20 ytes. Hence the
314e0 70 53 70 61 63 65 20 62 75 66 66 65 72 20 6d 75 pSpace buffer mu
314f0 73 74 20 62 65 20 61 74 0a 2a 2a 20 6c 65 61 73 st be at.** leas
31500 74 20 31 33 20 62 79 74 65 73 20 69 6e 20 73 69 t 13 bytes in si
31510 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ze..*/.static in
31520 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 t balance_quick(
31530 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 MemPage *pParent
31540 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 , MemPage *pPage
31550 2c 20 75 38 20 2a 70 53 70 61 63 65 29 7b 0a 20 , u8 *pSpace){.
31560 20 42 74 53 68 61 72 65 64 20 2a 63 6f 6e 73 74 BtShared *const
31570 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 pBt = pPage->pB
31580 74 3b 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 t; /* B-Tree
31590 44 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 4d 65 Database */. Me
315a0 6d 50 61 67 65 20 2a 70 4e 65 77 3b 20 20 20 20 mPage *pNew;
315b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
315c0 20 20 20 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c 6f /* Newly allo
315d0 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 cated page */.
315e0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
315f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
31600 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 /* Return C
31610 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ode */. Pgno pg
31620 6e 6f 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 noNew;
31630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
31640 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
31650 70 4e 65 77 20 2a 2f 0a 0a 20 20 61 73 73 65 72 pNew */.. asser
31660 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
31670 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
31680 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
31690 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
316a0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
316b0 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 arent->pDbPage)
316c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
316d0 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 ge->nOverflow==1
316e0 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 65 );.. /* This e
316f0 72 72 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 20 69 rror condition i
31700 73 20 6e 6f 77 20 63 61 75 67 68 74 20 70 72 69 s now caught pri
31710 6f 72 20 74 6f 20 72 65 61 63 68 69 6e 67 20 74 or to reaching t
31720 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a his function */.
31730 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 if( pPage->nCe
31740 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 ll==0 ) return S
31750 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
31760 50 54 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 PT;.. /* Alloca
31770 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 te a new page. T
31780 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 his page will be
31790 63 6f 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 come the right-s
317a0 69 62 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 ibling of . **
317b0 70 50 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 pPage. Make the
317c0 70 61 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 parent page writ
317d0 61 62 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 able, so that th
317e0 65 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 e new divider ce
317f0 6c 6c 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 ll. ** may be i
31800 6e 73 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 nserted. If both
31810 20 74 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e these operation
31820 73 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c s are successful
31830 2c 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a , proceed.. */.
31840 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
31850 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
31860 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 New, &pgnoNew, 0
31870 2c 20 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d , 0);.. if( rc=
31880 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 =SQLITE_OK ){..
31890 20 20 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 u8 *pOut = &p
318a0 53 70 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 Space[4];. u8
318b0 20 2a 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d *pCell = pPage-
318c0 3e 61 70 4f 76 66 6c 5b 30 5d 3b 0a 20 20 20 20 >apOvfl[0];.
318d0 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c u16 szCell = cel
318e0 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
318f0 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20 2a pCell);. u8 *
31900 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73 65 pStop;.. asse
31910 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
31920 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77 Iswriteable(pNew
31930 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
31940 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
31950 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46 5f >aData[0]==(PTF_
31960 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 INTKEY|PTF_LEAFD
31970 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29 3b ATA|PTF_LEAF) );
31980 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e . zeroPage(pN
31990 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 ew, PTF_INTKEY|P
319a0 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f TF_LEAFDATA|PTF_
319b0 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65 6d LEAF);. assem
319c0 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c blePage(pNew, 1,
319d0 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c &pCell, &szCell
319e0 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
319f0 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 is is an auto-va
31a00 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 cuum database, u
31a10 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
31a20 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 74 r map. ** wit
31a30 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 h entries for th
31a40 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20 e new page, and
31a50 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d any pointer from
31a60 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 6c the . ** cel
31a70 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f l on the page to
31a80 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
31a90 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 e. If either of
31aa0 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 65 these. ** ope
31ab0 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 74 rations fails, t
31ac0 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 he return code i
31ad0 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 63 s set, but the c
31ae0 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f ontents. ** o
31af0 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 f the parent pag
31b00 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e 69 e are still mani
31b10 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 63 pulated by thh c
31b20 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a ode below.. *
31b30 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 74 * That is Ok, at
31b40 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 this point the
31b50 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 67 parent page is g
31b60 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 20 uaranteed to.
31b70 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ** be marked as
31b80 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 6e dirty. Returnin
31b90 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 g an error code
31ba0 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 20 will cause a.
31bb0 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e ** rollback, un
31bc0 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 doing any change
31bd0 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 61 s made to the pa
31be0 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a rent page.. *
31bf0 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f /. if( ISAUTO
31c00 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
31c10 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 ptrmapPut(pBt, p
31c20 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 gnoNew, PTRMAP_B
31c30 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 TREE, pParent->p
31c40 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 gno, &rc);.
31c50 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77 if( szCell>pNew
31c60 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 ->minLocal ){.
31c70 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f ptrmapPutO
31c80 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 65 vflPtr(pNew, pCe
31c90 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 ll, &rc);.
31ca0 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
31cb0 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69 64 * Create a divid
31cc0 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 72 er cell to inser
31cd0 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 t into pParent.
31ce0 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c The divider cell
31cf0 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 73 . ** consists
31d00 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 67 of a 4-byte pag
31d10 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 61 e number (the pa
31d20 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 61 ge number of pPa
31d30 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 ge) and. ** a
31d40 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
31d50 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 63 key value (whic
31d60 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 h must be the sa
31d70 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a me value as the.
31d80 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b ** largest k
31d90 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 20 ey on pPage)..
31da0 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 66 **. ** To f
31db0 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 ind the largest
31dc0 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 61 key value on pPa
31dd0 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 74 ge, first find t
31de0 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a 20 he right-most .
31df0 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 ** cell on pP
31e00 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 74 age. The first t
31e10 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 wo fields of thi
31e20 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 0a s cell are the .
31e30 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c 65 ** record-le
31e40 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c 65 ngth (a variable
31e50 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 length integer
31e60 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 20 at most 32-bits
31e70 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a 20 in size). **
31e80 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c 75 and the key valu
31e90 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 e (a variable le
31ea0 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d 61 ngth integer, ma
31eb0 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 65 y have any value
31ec0 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 ).. ** The fi
31ed0 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65 rst of the while
31ee0 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f (...) loops belo
31ef0 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65 w skips over the
31f00 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a 20 record-length.
31f10 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65 ** field. The
31f20 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e second while(..
31f30 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74 .) loop copies t
31f40 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f he key value fro
31f50 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 6c m the. ** cel
31f60 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20 l on pPage into
31f70 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 the pSpace buffe
31f80 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 r.. */. pC
31f90 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
31fa0 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 Page, pPage->nCe
31fb0 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f 70 ll-1);. pStop
31fc0 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 = &pCell[9];.
31fd0 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 6c while( (*(pCel
31fe0 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 43 l++)&0x80) && pC
31ff0 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 20 ell<pStop );.
32000 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b pStop = &pCell[
32010 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 9];. while( (
32020 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 70 (*(pOut++) = *(p
32030 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 26 Cell++))&0x80) &
32040 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b & pCell<pStop );
32050 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 .. /* Insert
32060 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20 the new divider
32070 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 6e cell into pParen
32080 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 74 t. */. insert
32090 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 50 Cell(pParent, pP
320a0 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 53 arent->nCell, pS
320b0 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 74 pace, (int)(pOut
320c0 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 20 -pSpace),.
320d0 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 67 0, pPag
320e0 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 0a e->pgno, &rc);..
320f0 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 72 /* Set the r
32100 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74 ight-child point
32110 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 6f er of pParent to
32120 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 point to the ne
32130 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70 w page. */. p
32140 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 ut4byte(&pParent
32150 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
32160 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 >hdrOffset+8], p
32170 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 gnoNew);. .
32180 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72 /* Release the r
32190 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
321a0 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 new page. */.
321b0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 releasePage(pNe
321c0 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 w);. }.. retur
321d0 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
321e0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 * SQLITE_OMIT_QU
321f0 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 23 ICKBALANCE */..#
32200 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 if 0./*.** This
32210 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
32220 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e 79 t contribute any
32230 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 65 thing to the ope
32240 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ration of SQLite
32250 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 74 ..** it is somet
32260 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 74 imes activated t
32270 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c 65 emporarily while
32280 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 20 debugging code
32290 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a 20 responsible .**
322a0 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 6e for setting poin
322b0 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 2e ter-map entries.
322c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
322d0 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 trmapCheckPages(
322e0 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 65 MemPage **apPage
322f0 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 , int nPage){.
32300 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28 int i, j;. for(
32310 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b i=0; i<nPage; i+
32320 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0a +){. Pgno n;.
32330 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d 65 u8 e;. Me
32340 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 61 mPage *pPage = a
32350 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 74 pPage[i];. Bt
32360 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
32370 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 age->pBt;. as
32380 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
32390 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 nit );.. for(
323a0 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e 43 j=0; j<pPage->nC
323b0 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 ell; j++){.
323c0 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
323d0 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 20 u8 *z;.
323e0 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 6e . z = fin
323f0 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 3b dCell(pPage, j);
32400 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 . btreePars
32410 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
32420 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 z, &info);.
32430 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 if( info.iOverf
32440 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 50 low ){. P
32450 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 gno ovfl = get4b
32460 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65 yte(&z[info.iOve
32470 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 20 rflow]);.
32480 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
32490 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 ovfl, &e, &n);.
324a0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
324b0 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 ==pPage->pgno &&
324c0 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 e==PTRMAP_OVERF
324d0 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a LOW1 );. }.
324e0 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 if( !pPage
324f0 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
32500 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67 Pgno child = g
32510 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 20 et4byte(z);.
32520 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
32530 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e t, child, &e, &n
32540 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
32550 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e t( n==pPage->pgn
32560 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 o && e==PTRMAP_B
32570 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d 0a TREE );. }.
32580 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 }. if( !p
32590 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
325a0 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d Pgno child =
325b0 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
325c0 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
325d0 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
325e0 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
325f0 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e t, child, &e, &n
32600 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
32610 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 n==pPage->pgno
32620 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 && e==PTRMAP_BTR
32630 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a EE );. }. }.
32640 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
32650 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
32660 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
32670 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f d to copy the co
32680 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 2d ntents of the b-
32690 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 64 tree node stored
326a0 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 72 .** on page pFr
326b0 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 om to page pTo.
326c0 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 61 If page pFrom wa
326d0 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 s not a leaf pag
326e0 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 70 e, then.** the p
326f0 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
32700 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 6c es for each chil
32710 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61 74 d page are updat
32720 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a ed so that the.*
32730 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 74 * parent page st
32740 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 6e ored in the poin
32750 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 20 ter map is page
32760 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f pTo. If pFrom co
32770 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 63 ntained.** any c
32780 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 6c ells with overfl
32790 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73 ow page pointers
327a0 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 , then the corre
327b0 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 72 sponding pointer
327c0 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 .** map entries
327d0 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 64 are also updated
327e0 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 72 so that the par
327f0 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 65 ent page is page
32800 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 pTo..**.** If p
32810 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 6c From is currentl
32820 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 6f y carrying any o
32830 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 65 verflow cells (e
32840 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a ntries in the.**
32850 20 4d 65 6d 50 61 67 65 2e 61 70 4f 76 66 6c 5b MemPage.apOvfl[
32860 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 ] array), they a
32870 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f re not copied to
32880 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 pTo. .**.** Bef
32890 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 ore returning, p
328a0 61 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 age pTo is reini
328b0 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 tialized using b
328c0 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a treeInitPage()..
328d0 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 **.** The perfor
328e0 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 mance of this fu
328f0 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 nction is not cr
32900 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e itical. It is on
32910 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 ly used by .** t
32920 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c he balance_shall
32930 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e ower() and balan
32940 63 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 ce_deeper() proc
32950 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 edures, neither
32960 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 of.** which are
32970 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 called often und
32980 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d er normal circum
32990 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 stances..*/.stat
329a0 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 ic void copyNode
329b0 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 Content(MemPage
329c0 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 *pFrom, MemPage
329d0 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b *pTo, int *pRC){
329e0 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 . if( (*pRC)==S
329f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
32a00 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 BtShared * const
32a10 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 pBt = pFrom->pB
32a20 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 t;. u8 * cons
32a30 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d t aFrom = pFrom-
32a40 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a >aData;. u8 *
32a50 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f const aTo = pTo
32a60 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 ->aData;. int
32a70 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 const iFromHdr
32a80 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 = pFrom->hdrOffs
32a90 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 et;. int cons
32aa0 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f t iToHdr = ((pTo
32ab0 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 ->pgno==1) ? 100
32ac0 20 3a 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 72 : 0);. int r
32ad0 63 3b 0a 20 20 20 20 69 6e 74 20 69 44 61 74 61 c;. int iData
32ae0 3b 0a 20 20 0a 20 20 0a 20 20 20 20 61 73 73 65 ;. . . asse
32af0 72 74 28 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 rt( pFrom->isIni
32b00 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
32b10 20 70 46 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 pFrom->nFree>=i
32b20 54 6f 48 64 72 20 29 3b 0a 20 20 20 20 61 73 73 ToHdr );. ass
32b30 65 72 74 28 20 67 65 74 32 62 79 74 65 28 26 61 ert( get2byte(&a
32b40 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d From[iFromHdr+5]
32b50 29 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 ) <= (int)pBt->u
32b60 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 0a sableSize );. .
32b70 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 /* Copy the
32b80 62 2d 74 72 65 65 20 6e 6f 64 65 20 63 6f 6e 74 b-tree node cont
32b90 65 6e 74 20 66 72 6f 6d 20 70 61 67 65 20 70 46 ent from page pF
32ba0 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e rom to page pTo.
32bb0 20 2a 2f 0a 20 20 20 20 69 44 61 74 61 20 3d 20 */. iData =
32bc0 67 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d 5b get2byte(&aFrom[
32bd0 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3b 0a 20 20 iFromHdr+5]);.
32be0 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b 69 44 memcpy(&aTo[iD
32bf0 61 74 61 5d 2c 20 26 61 46 72 6f 6d 5b 69 44 61 ata], &aFrom[iDa
32c00 74 61 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 ta], pBt->usable
32c10 53 69 7a 65 2d 69 44 61 74 61 29 3b 0a 20 20 20 Size-iData);.
32c20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b 69 54 6f memcpy(&aTo[iTo
32c30 48 64 72 5d 2c 20 26 61 46 72 6f 6d 5b 69 46 72 Hdr], &aFrom[iFr
32c40 6f 6d 48 64 72 5d 2c 20 70 46 72 6f 6d 2d 3e 63 omHdr], pFrom->c
32c50 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 46 ellOffset + 2*pF
32c60 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 0a rom->nCell);. .
32c70 20 20 20 20 2f 2a 20 52 65 69 6e 69 74 69 61 6c /* Reinitial
32c80 69 7a 65 20 70 61 67 65 20 70 54 6f 20 73 6f 20 ize page pTo so
32c90 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 that the content
32ca0 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67 65 s of the MemPage
32cb0 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 2a structure. *
32cc0 2a 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 * match the new
32cd0 64 61 74 61 2e 20 54 68 65 20 69 6e 69 74 69 61 data. The initia
32ce0 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 70 54 6f 20 lization of pTo
32cf0 63 61 6e 20 61 63 74 75 61 6c 6c 79 20 66 61 69 can actually fai
32d00 6c 20 75 6e 64 65 72 0a 20 20 20 20 2a 2a 20 66 l under. ** f
32d10 61 69 72 6c 79 20 6f 62 73 63 75 72 65 20 63 69 airly obscure ci
32d20 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 65 76 65 rcumstances, eve
32d30 6e 20 74 68 6f 75 67 68 20 69 74 20 69 73 20 61 n though it is a
32d40 20 63 6f 70 79 20 6f 66 20 69 6e 69 74 69 61 6c copy of initial
32d50 69 7a 65 64 20 0a 20 20 20 20 2a 2a 20 70 61 67 ized . ** pag
32d60 65 20 70 46 72 6f 6d 2e 0a 20 20 20 20 2a 2f 0a e pFrom.. */.
32d70 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e 69 74 20 pTo->isInit
32d80 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 = 0;. rc = bt
32d90 72 65 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 reeInitPage(pTo)
32da0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
32db0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
32dc0 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 *pRC = rc;.
32dd0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
32de0 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 . /* If thi
32df0 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 s is an auto-vac
32e00 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 70 uum database, up
32e10 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 date the pointer
32e20 2d 6d 61 70 20 65 6e 74 72 69 65 73 0a 20 20 20 -map entries.
32e30 20 2a 2a 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 ** for any b-tr
32e40 65 65 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 ee or overflow p
32e50 61 67 65 73 20 74 68 61 74 20 70 54 6f 20 6e 6f ages that pTo no
32e60 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 w contains the p
32e70 6f 69 6e 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 ointers to..
32e80 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 */. if( ISAUT
32e90 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 OVACUUM ){.
32ea0 20 2a 70 52 43 20 3d 20 73 65 74 43 68 69 6c 64 *pRC = setChild
32eb0 50 74 72 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 Ptrmaps(pTo);.
32ec0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
32ed0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
32ee0 64 69 73 74 72 69 62 75 74 65 73 20 63 65 6c 6c distributes cell
32ef0 73 20 6f 6e 20 74 68 65 20 69 50 61 72 65 6e 74 s on the iParent
32f00 49 64 78 27 74 68 20 63 68 69 6c 64 20 6f 66 20 Idx'th child of
32f10 70 50 61 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 pParent.** (here
32f20 61 66 74 65 72 20 22 74 68 65 20 70 61 67 65 22 after "the page"
32f30 29 20 61 6e 64 20 75 70 20 74 6f 20 32 20 73 69 ) and up to 2 si
32f40 62 6c 69 6e 67 73 20 73 6f 20 74 68 61 74 20 61 blings so that a
32f50 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 61 62 ll pages have ab
32f60 6f 75 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 out the.** same
32f70 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 amount of free s
32f80 70 61 63 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 pace. Usually a
32f90 73 69 6e 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f single sibling o
32fa0 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 n either side of
32fb0 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 the.** page are
32fc0 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c used in the bal
32fd0 61 6e 63 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 ancing, though b
32fe0 6f 74 68 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 oth siblings mig
32ff0 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 ht come from one
33000 0a 2a 2a 20 73 69 64 65 20 69 66 20 74 68 65 20 .** side if the
33010 70 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 page is the firs
33020 74 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 t or last child
33030 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 49 of its parent. I
33040 66 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 f the page .** h
33050 61 73 20 66 65 77 65 72 20 74 68 61 6e 20 32 20 as fewer than 2
33060 73 69 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 siblings (someth
33070 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e ing which can on
33080 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 ly happen if the
33090 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f page.** is a ro
330a0 6f 74 20 70 61 67 65 20 6f 72 20 61 20 63 68 69 ot page or a chi
330b0 6c 64 20 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 ld of a root pag
330c0 65 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 e) then all avai
330d0 6c 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a lable siblings.*
330e0 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e * participate in
330f0 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a the balancing..
33100 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 **.** The number
33110 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 of siblings of
33120 74 68 65 20 70 61 67 65 20 6d 69 67 68 74 20 62 the page might b
33130 65 20 69 6e 63 72 65 61 73 65 64 20 6f 72 20 64 e increased or d
33140 65 63 72 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 ecreased by .**
33150 6f 6e 65 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e one or two in an
33160 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 effort to keep
33170 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75 6c pages nearly ful
33180 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 l but not over f
33190 75 6c 6c 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 ull. .**.** Note
331a0 20 74 68 61 74 20 77 68 65 6e 20 74 68 69 73 20 that when this
331b0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
331c0 64 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 d, some of the c
331d0 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65 ells on the page
331e0 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 .** might not ac
331f0 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 tually be stored
33200 20 69 6e 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 in MemPage.aDat
33210 61 5b 5d 2e 20 54 68 69 73 20 63 61 6e 20 68 61 a[]. This can ha
33220 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 ppen.** if the p
33230 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e age is overfull.
33240 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e This routine en
33250 73 75 72 65 73 20 74 68 61 74 20 61 6c 6c 20 63 sures that all c
33260 65 6c 6c 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a ells allocated.*
33270 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20 61 6e * to the page an
33280 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 66 d its siblings f
33290 69 74 20 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e it into MemPage.
332a0 61 44 61 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 aData[] before r
332b0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 eturning..**.**
332c0 49 6e 20 74 68 65 20 63 6f 75 72 73 65 20 6f 66 In the course of
332d0 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 balancing the p
332e0 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c age and its sibl
332f0 69 6e 67 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 ings, cells may
33300 62 65 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 be.** inserted i
33310 6e 74 6f 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 nto or removed f
33320 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 rom the parent p
33330 61 67 65 20 28 70 50 61 72 65 6e 74 29 2e 20 44 age (pParent). D
33340 6f 69 6e 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 oing so.** may c
33350 61 75 73 65 20 74 68 65 20 70 61 72 65 6e 74 20 ause the parent
33360 70 61 67 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f page to become o
33370 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 verfull or under
33380 66 75 6c 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a full. If this.**
33390 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73 20 happens, it is
333a0 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 the responsibili
333b0 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 ty of the caller
333c0 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 to invoke the c
333d0 6f 72 72 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 orrect.** balanc
333e0 69 6e 67 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 ing routine to f
333f0 69 78 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ix this problem
33400 28 73 65 65 20 74 68 65 20 62 61 6c 61 6e 63 65 (see the balance
33410 28 29 20 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a () routine). .**
33420 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 .** If this rout
33430 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e ine fails for an
33440 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 y reason, it mig
33450 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 ht leave the dat
33460 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f abase.** in a co
33470 72 72 75 70 74 65 64 20 73 74 61 74 65 2e 20 53 rrupted state. S
33480 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e o if this routin
33490 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 e fails, the dat
334a0 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 abase should.**
334b0 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a be rolled back..
334c0 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 **.** The third
334d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 argument to this
334e0 20 66 75 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c function, aOvfl
334f0 53 70 61 63 65 2c 20 69 73 20 61 20 70 6f 69 6e Space, is a poin
33500 74 65 72 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 ter to a.** buff
33510 65 72 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f er big enough to
33520 20 68 6f 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 hold one page.
33530 49 66 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 If while inserti
33540 6e 67 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 ng cells into th
33550 65 20 70 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 e parent.** page
33560 20 28 70 50 61 72 65 6e 74 29 20 74 68 65 20 70 (pParent) the p
33570 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d arent page becom
33580 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 es overfull, thi
33590 73 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 s buffer is.** u
335a0 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 sed to store the
335b0 20 70 61 72 65 6e 74 27 73 20 6f 76 65 72 66 6c parent's overfl
335c0 6f 77 20 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 ow cells. Becaus
335d0 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 e this function
335e0 69 6e 73 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 inserts.** a max
335f0 69 6d 75 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 imum of four div
33600 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 ider cells into
33610 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c the parent page,
33620 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d and the maximum
33630 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 61 20 63 65 .** size of a ce
33640 6c 6c 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e ll stored within
33650 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 an internal nod
33660 65 20 69 73 20 61 6c 77 61 79 73 20 6c 65 73 73 e is always less
33670 20 74 68 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 than 1/4.** of
33680 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 the page-size, t
33690 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 he aOvflSpace[]
336a0 62 75 66 66 65 72 20 69 73 20 67 75 61 72 61 6e buffer is guaran
336b0 74 65 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65 teed to be large
336c0 0a 2a 2a 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 .** enough for a
336d0 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c ll overflow cell
336e0 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 s..**.** If aOvf
336f0 6c 53 70 61 63 65 20 69 73 20 73 65 74 20 74 6f lSpace is set to
33700 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c a null pointer,
33710 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
33720 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 eturns .** SQLIT
33730 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 23 69 66 20 E_NOMEM..*/.#if
33740 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
33750 29 20 26 26 20 5f 4d 53 43 5f 56 45 52 20 3e 3d ) && _MSC_VER >=
33760 20 31 37 30 30 20 26 26 20 64 65 66 69 6e 65 64 1700 && defined
33770 28 5f 4d 5f 41 52 4d 29 0a 23 70 72 61 67 6d 61 (_M_ARM).#pragma
33780 20 6f 70 74 69 6d 69 7a 65 28 22 22 2c 20 6f 66 optimize("", of
33790 66 29 0a 23 65 6e 64 69 66 0a 73 74 61 74 69 63 f).#endif.static
337a0 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e int balance_non
337b0 72 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 root(. MemPage
337c0 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 *pParent,
337d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e /* Paren
337e0 74 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e t page of siblin
337f0 67 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 gs being balance
33800 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 d */. int iPare
33810 6e 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 ntIdx,
33820 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
33830 6f 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e of "the page" in
33840 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 pParent */. u8
33850 20 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 *aOvflSpace,
33860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33870 20 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73 page-size bytes
33880 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61 of space for pa
33890 72 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 rent ovfl */. i
338a0 6e 74 20 69 73 52 6f 6f 74 2c 20 20 20 20 20 20 nt isRoot,
338b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
338c0 2a 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e * True if pParen
338d0 74 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 t is a root-page
338e0 20 2a 2f 0a 20 20 69 6e 74 20 62 42 75 6c 6b 20 */. int bBulk
338f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33900 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
33910 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 70 61 this call is pa
33920 72 74 20 6f 66 20 61 20 62 75 6c 6b 20 6c 6f 61 rt of a bulk loa
33930 64 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 d */.){. BtShar
33940 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
33950 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 /* The wh
33960 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a ole database */.
33970 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b int nCell = 0;
33980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
33990 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c * Number of cell
339a0 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f s in apCell[] */
339b0 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 . int nMaxCells
339c0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
339d0 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a /* Allocated siz
339e0 65 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 e of apCell, szC
339f0 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 ell, aFrom. */.
33a00 20 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 int nNew = 0;
33a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33a20 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
33a30 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 in apNew[] */.
33a40 20 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 int nOld;
33a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33a60 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
33a70 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 in apOld[] */.
33a80 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 int i, j, k;
33a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33aa0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a Loop counters *
33ab0 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 /. int nxDiv;
33ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33ad0 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 /* Next divider
33ae0 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 slot in pParent
33af0 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 ->aCell[] */. i
33b00 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
33b10 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 K; /* T
33b20 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a he return code *
33b30 2f 0a 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 /. u16 leafCorr
33b40 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 ection;
33b50 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 /* 4 if pPage i
33b60 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 s a leaf. 0 if
33b70 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 not */. int lea
33b80 66 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 fData;
33b90 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
33ba0 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 pPage is a leaf
33bb0 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 of a LEAFDATA t
33bc0 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 ree */. int usa
33bd0 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 bleSpace;
33be0 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 /* Bytes i
33bf0 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 n pPage beyond t
33c00 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 he header */. i
33c10 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 nt pageFlags;
33c20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
33c30 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 alue of pPage->a
33c40 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 Data[0] */. int
33c50 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 subtotal;
33c60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 /* Sub
33c70 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 total of bytes i
33c80 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 n cells on one p
33c90 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 age */. int iSp
33ca0 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 ace1 = 0;
33cb0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 /* First u
33cc0 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 nused byte of aS
33cd0 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 pace1[] */. int
33ce0 20 69 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b iOvflSpace = 0;
33cf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
33d00 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f st unused byte o
33d10 66 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a f aOvflSpace[] *
33d20 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 /. int szScratc
33d30 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
33d40 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 /* Size of scra
33d50 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 tch memory reque
33d60 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 sted */. MemPag
33d70 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 e *apOld[NB];
33d80 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 /* pPage
33d90 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 and up to two si
33da0 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 blings */. MemP
33db0 61 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b age *apCopy[NB];
33dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 /* Priv
33dd0 61 74 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 ate copies of ap
33de0 4f 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 Old[] pages */.
33df0 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b MemPage *apNew[
33e00 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a NB+2]; /*
33e10 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f pPage and up to
33e20 20 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 NB siblings aft
33e30 65 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a er balancing */.
33e40 20 20 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 u8 *pRight;
33e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
33e60 2a 20 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 * Location in pa
33e70 72 65 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 rent of right-si
33e80 62 6c 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f bling pointer */
33e90 0a 20 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d . u8 *apDiv[NB-
33ea0 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1];
33eb0 2f 2a 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 /* Divider cells
33ec0 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 in pParent */.
33ed0 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 int cntNew[NB+2
33ee0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ]; /*
33ef0 20 49 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b Index in aCell[
33f00 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 ] of cell after
33f10 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 i-th page */. i
33f20 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 nt szNew[NB+2];
33f30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
33f40 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 ombined size of
33f50 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 cells place on i
33f60 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 -th page */. u8
33f70 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 **apCell = 0;
33f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c /* Al
33f90 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 l cells begin ba
33fa0 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 lanced */. u16
33fb0 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 *szCell;
33fc0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 /* Loca
33fd0 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 l size of all ce
33fe0 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 lls in apCell[]
33ff0 2a 2f 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 */. u8 *aSpace1
34000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
34010 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 /* Space for c
34020 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 opies of divider
34030 73 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e s cells */. Pgn
34040 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 o pgno;
34050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d /* Tem
34060 70 20 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 p var to store a
34070 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 page number in
34080 2a 2f 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 */.. pBt = pPar
34090 65 6e 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 ent->pBt;. asse
340a0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
340b0 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
340c0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
340d0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
340e0 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
340f0 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 >pDbPage) );..#i
34100 66 20 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 f 0. TRACE(("BA
34110 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 LANCE: begin pag
34120 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 e %d child of %d
34130 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f \n", pPage->pgno
34140 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 , pParent->pgno)
34150 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 );.#endif.. /*
34160 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 At this point pP
34170 61 72 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 arent may have a
34180 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 t most one overf
34190 6c 6f 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 low cell. And if
341a0 0a 20 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 . ** this overf
341b0 6c 6f 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 low cell is pres
341c0 65 6e 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 ent, it must be
341d0 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 the cell with .
341e0 20 2a 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e ** index iParen
341f0 74 49 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 tIdx. This scena
34200 72 69 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 rio comes about
34210 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
34220 6f 6e 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 on. ** is calle
34230 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 d (indirectly) f
34240 72 6f 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 rom sqlite3Btree
34250 44 65 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 Delete().. */.
34260 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
34270 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c ->nOverflow==0 |
34280 7c 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 | pParent->nOver
34290 66 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 flow==1 );. ass
342a0 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f ert( pParent->nO
342b0 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 verflow==0 || pP
342c0 61 72 65 6e 74 2d 3e 61 69 4f 76 66 6c 5b 30 5d arent->aiOvfl[0]
342d0 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b 0a ==iParentIdx );.
342e0 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70 61 . if( !aOvflSpa
342f0 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ce ){. return
34300 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
34310 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 }.. /* Find th
34320 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 e sibling pages
34330 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 6f to balance. Also
34340 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c 6c locate the cell
34350 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 20 s in pParent .
34360 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65 20 74 ** that divide t
34370 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e 20 he siblings. An
34380 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
34390 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 to find NN sibli
343a0 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 ngs on . ** eit
343b0 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 her side of pPag
343c0 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 e. More siblings
343d0 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 are taken from
343e0 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 one side, howeve
343f0 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 r, . ** if ther
34400 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e e are fewer than
34410 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 NN siblings on
34420 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 the other side.
34430 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 If pParent. **
34440 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 has NB or fewer
34450 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c children then al
34460 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 l children of pP
34470 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e arent are taken.
34480 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 . **. ** Thi
34490 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f 70 s loop also drop
344a0 73 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 s the divider ce
344b0 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61 72 lls from the par
344c0 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a 20 ent page. This.
344d0 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65 6d ** way, the rem
344e0 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66 75 ainder of the fu
344f0 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 nction does not
34500 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69 74 have to deal wit
34510 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 66 h any. ** overf
34520 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 low cells in the
34530 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73 69 parent page, si
34540 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69 73 74 nce if any exist
34550 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 2a ed they will. *
34560 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 * have already b
34570 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a een removed.. *
34580 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74 2d /. i = pParent-
34590 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 61 >nOverflow + pPa
345a0 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 rent->nCell;. i
345b0 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e 78 f( i<2 ){. nx
345c0 44 69 76 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 Div = 0;. }else
345d0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 62 42 {. assert( bB
345e0 75 6c 6b 3d 3d 30 20 7c 7c 20 62 42 75 6c 6b 3d ulk==0 || bBulk=
345f0 3d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 69 50 =1 );. if( iP
34600 61 72 65 6e 74 49 64 78 3d 3d 30 20 29 7b 20 20 arentIdx==0 ){
34610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0a .
34620 20 20 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b nxDiv = 0;
34630 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 . }else if( i
34640 50 61 72 65 6e 74 49 64 78 3d 3d 69 20 29 7b 0a ParentIdx==i ){.
34650 20 20 20 20 20 20 6e 78 44 69 76 20 3d 20 69 2d nxDiv = i-
34660 32 2b 62 42 75 6c 6b 3b 0a 20 20 20 20 7d 65 6c 2+bBulk;. }el
34670 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
34680 28 20 62 42 75 6c 6b 3d 3d 30 20 29 3b 0a 20 20 ( bBulk==0 );.
34690 20 20 20 20 6e 78 44 69 76 20 3d 20 69 50 61 72 nxDiv = iPar
346a0 65 6e 74 49 64 78 2d 31 3b 0a 20 20 20 20 7d 0a entIdx-1;. }.
346b0 20 20 20 20 69 20 3d 20 32 2d 62 42 75 6c 6b 3b i = 2-bBulk;
346c0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 69 2b . }. nOld = i+
346d0 31 3b 0a 20 20 69 66 28 20 28 69 2b 6e 78 44 69 1;. if( (i+nxDi
346e0 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 v-pParent->nOver
346f0 66 6c 6f 77 29 3d 3d 70 50 61 72 65 6e 74 2d 3e flow)==pParent->
34700 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 70 52 69 nCell ){. pRi
34710 67 68 74 20 3d 20 26 70 50 61 72 65 6e 74 2d 3e ght = &pParent->
34720 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 aData[pParent->h
34730 64 72 4f 66 66 73 65 74 2b 38 5d 3b 0a 20 20 7d drOffset+8];. }
34740 65 6c 73 65 7b 0a 20 20 20 20 70 52 69 67 68 74 else{. pRight
34750 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 = findCell(pPar
34760 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 ent, i+nxDiv-pPa
34770 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 rent->nOverflow)
34780 3b 0a 20 20 7d 0a 20 20 70 67 6e 6f 20 3d 20 67 ;. }. pgno = g
34790 65 74 34 62 79 74 65 28 70 52 69 67 68 74 29 3b et4byte(pRight);
347a0 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 . while( 1 ){.
347b0 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e rc = getAndIn
347c0 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f itPage(pBt, pgno
347d0 2c 20 26 61 70 4f 6c 64 5b 69 5d 2c 20 30 29 3b , &apOld[i], 0);
347e0 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
347f0 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f 6c memset(apOl
34800 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a 65 d, 0, (i+1)*size
34810 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0a 20 of(MemPage*));.
34820 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 goto balanc
34830 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d e_cleanup;. }
34840 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b . nMaxCells +
34850 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 = 1+apOld[i]->nC
34860 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f ell+apOld[i]->nO
34870 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 verflow;. if(
34880 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72 65 61 (i--)==0 ) brea
34890 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b 6e 78 k;.. if( i+nx
348a0 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 61 69 Div==pParent->ai
348b0 4f 76 66 6c 5b 30 5d 20 26 26 20 70 50 61 72 65 Ovfl[0] && pPare
348c0 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b nt->nOverflow ){
348d0 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 . apDiv[i]
348e0 3d 20 70 50 61 72 65 6e 74 2d 3e 61 70 4f 76 66 = pParent->apOvf
348f0 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f l[0];. pgno
34900 20 3d 20 67 65 74 34 62 79 74 65 28 61 70 44 69 = get4byte(apDi
34910 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e v[i]);. szN
34920 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 ew[i] = cellSize
34930 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 Ptr(pParent, apD
34940 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 50 iv[i]);. pP
34950 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 arent->nOverflow
34960 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
34970 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 . apDiv[i]
34980 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 = findCell(pPare
34990 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 nt, i+nxDiv-pPar
349a0 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b ent->nOverflow);
349b0 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 . pgno = ge
349c0 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 t4byte(apDiv[i])
349d0 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d ;. szNew[i]
349e0 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
349f0 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d Parent, apDiv[i]
34a00 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 72 6f );.. /* Dro
34a10 70 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 p the cell from
34a20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
34a30 20 61 70 44 69 76 5b 69 5d 20 73 74 69 6c 6c 20 apDiv[i] still
34a40 70 6f 69 6e 74 73 20 74 6f 0a 20 20 20 20 20 20 points to.
34a50 2a 2a 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 ** the cell with
34a60 69 6e 20 74 68 65 20 70 61 72 65 6e 74 2c 20 65 in the parent, e
34a70 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 68 61 ven though it ha
34a80 73 20 62 65 65 6e 20 64 72 6f 70 70 65 64 2e 0a s been dropped..
34a90 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 ** This is
34aa0 20 73 61 66 65 20 62 65 63 61 75 73 65 20 64 72 safe because dr
34ab0 6f 70 70 69 6e 67 20 61 20 63 65 6c 6c 20 6f 6e opping a cell on
34ac0 6c 79 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 ly overwrites th
34ad0 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a e first. **
34ae0 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 69 four bytes of i
34af0 74 2c 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 t, and this func
34b00 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 tion does not ne
34b10 65 64 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 ed the first.
34b20 20 20 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 ** four bytes
34b30 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 of the divider
34b40 63 65 6c 6c 2e 20 53 6f 20 74 68 65 20 70 6f 69 cell. So the poi
34b50 6e 74 65 72 20 69 73 20 73 61 66 65 20 74 6f 20 nter is safe to
34b60 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 6c 61 74 use. ** lat
34b70 65 72 20 6f 6e 2e 20 20 0a 20 20 20 20 20 20 2a er on. . *
34b80 2a 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e *. ** But n
34b90 6f 74 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 ot if we are in
34ba0 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d 6f secure-delete mo
34bb0 64 65 2e 20 49 6e 20 73 65 63 75 72 65 2d 64 65 de. In secure-de
34bc0 6c 65 74 65 20 6d 6f 64 65 2c 0a 20 20 20 20 20 lete mode,.
34bd0 20 2a 2a 20 74 68 65 20 64 72 6f 70 43 65 6c 6c ** the dropCell
34be0 28 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 () routine will
34bf0 6f 76 65 72 77 72 69 74 65 20 74 68 65 20 65 6e overwrite the en
34c00 74 69 72 65 20 63 65 6c 6c 20 77 69 74 68 20 7a tire cell with z
34c10 65 72 6f 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 eroes.. **
34c20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 65 In this case, te
34c30 6d 70 6f 72 61 72 69 6c 79 20 63 6f 70 79 20 74 mporarily copy t
34c40 68 65 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 he cell into the
34c50 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 0a 20 20 aOvflSpace[].
34c60 20 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 49 ** buffer. I
34c70 74 20 77 69 6c 6c 20 62 65 20 63 6f 70 69 65 64 t will be copied
34c80 20 6f 75 74 20 61 67 61 69 6e 20 61 73 20 73 6f out again as so
34c90 6f 6e 20 61 73 20 74 68 65 20 61 53 70 61 63 65 on as the aSpace
34ca0 5b 5d 20 62 75 66 66 65 72 0a 20 20 20 20 20 20 [] buffer.
34cb0 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e ** is allocated.
34cc0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 */. if( p
34cd0 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
34ce0 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TS_SECURE_DELETE
34cf0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
34d00 69 4f 66 66 3b 0a 0a 20 20 20 20 20 20 20 20 69 iOff;.. i
34d10 4f 66 66 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 Off = SQLITE_PTR
34d20 5f 54 4f 5f 49 4e 54 28 61 70 44 69 76 5b 69 5d _TO_INT(apDiv[i]
34d30 29 20 2d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 ) - SQLITE_PTR_T
34d40 4f 5f 49 4e 54 28 70 50 61 72 65 6e 74 2d 3e 61 O_INT(pParent->a
34d50 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 69 Data);. i
34d60 66 28 20 28 69 4f 66 66 2b 73 7a 4e 65 77 5b 69 f( (iOff+szNew[i
34d70 5d 29 3e 28 69 6e 74 29 70 42 74 2d 3e 75 73 61 ])>(int)pBt->usa
34d80 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 bleSize ){.
34d90 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
34da0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
34db0 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 memset(
34dc0 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a apOld, 0, (i+1)*
34dd0 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 sizeof(MemPage*)
34de0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 );. got
34df0 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
34e00 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 p;. }else
34e10 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 {. memc
34e20 70 79 28 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 py(&aOvflSpace[i
34e30 4f 66 66 5d 2c 20 61 70 44 69 76 5b 69 5d 2c 20 Off], apDiv[i],
34e40 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20 20 20 20 20 szNew[i]);.
34e50 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 apDiv[i] =
34e60 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 44 69 &aOvflSpace[apDi
34e70 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44 v[i]-pParent->aD
34e80 61 74 61 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a ata];. }.
34e90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 72 }. dr
34ea0 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 opCell(pParent,
34eb0 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d i+nxDiv-pParent-
34ec0 3e 6e 4f 76 65 72 66 6c 6f 77 2c 20 73 7a 4e 65 >nOverflow, szNe
34ed0 77 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 w[i], &rc);.
34ee0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 }. }.. /* Make
34ef0 20 6e 4d 61 78 43 65 6c 6c 73 20 61 20 6d 75 6c nMaxCells a mul
34f00 74 69 70 6c 65 20 6f 66 20 34 20 69 6e 20 6f 72 tiple of 4 in or
34f10 64 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20 der to preserve
34f20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 61 6c 69 67 8-byte. ** alig
34f30 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61 78 43 nment */. nMaxC
34f40 65 6c 6c 73 20 3d 20 28 6e 4d 61 78 43 65 6c 6c ells = (nMaxCell
34f50 73 20 2b 20 33 29 26 7e 33 3b 0a 0a 20 20 2f 2a s + 3)&~3;.. /*
34f60 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 . ** Allocate s
34f70 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f 72 79 20 pace for memory
34f80 73 74 72 75 63 74 75 72 65 73 0a 20 20 2a 2f 0a structures. */.
34f90 20 20 6b 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 k = pBt->pageS
34fa0 69 7a 65 20 2b 20 52 4f 55 4e 44 38 28 73 69 7a ize + ROUND8(siz
34fb0 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 eof(MemPage));.
34fc0 20 73 7a 53 63 72 61 74 63 68 20 3d 0a 20 20 20 szScratch =.
34fd0 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 nMaxCells*si
34fe0 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 20 20 20 zeof(u8*)
34ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
35000 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a 20 20 20 /* apCell */.
35010 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 + nMaxCells*si
35020 7a 65 6f 66 28 75 31 36 29 20 20 20 20 20 20 20 zeof(u16)
35030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
35040 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20 20 20 /* szCell */.
35050 20 20 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a + pBt->pageSiz
35060 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
35070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
35080 2f 2a 20 61 53 70 61 63 65 31 20 2a 2f 0a 20 20 /* aSpace1 */.
35090 20 20 20 2b 20 6b 2a 6e 4f 6c 64 3b 20 20 20 20 + k*nOld;
350a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
350b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
350c0 20 2f 2a 20 50 61 67 65 20 63 6f 70 69 65 73 20 /* Page copies
350d0 28 61 70 43 6f 70 79 29 20 2a 2f 0a 20 20 61 70 (apCopy) */. ap
350e0 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 63 Cell = sqlite3Sc
350f0 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20 73 7a 53 ratchMalloc( szS
35100 63 72 61 74 63 68 20 29 3b 20 0a 20 20 69 66 28 cratch ); . if(
35110 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 apCell==0 ){.
35120 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
35130 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 MEM;. goto ba
35140 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
35150 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 75 }. szCell = (u
35160 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d 61 78 16*)&apCell[nMax
35170 43 65 6c 6c 73 5d 3b 0a 20 20 61 53 70 61 63 65 Cells];. aSpace
35180 31 20 3d 20 28 75 38 2a 29 26 73 7a 43 65 6c 6c 1 = (u8*)&szCell
35190 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 [nMaxCells];. a
351a0 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
351b0 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61 E_ALIGNMENT(aSpa
351c0 63 65 31 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 ce1) );.. /*.
351d0 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 ** Load pointers
351e0 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e to all cells on
351f0 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 sibling pages a
35200 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63 nd the divider c
35210 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 ells. ** into t
35220 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b he local apCell[
35230 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 ] array. Make c
35240 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 opies of the div
35250 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 ider cells. **
35260 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 into space obtai
35270 6e 65 64 20 66 72 6f 6d 20 61 53 70 61 63 65 31 ned from aSpace1
35280 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 [] and remove th
35290 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a e divider cells.
352a0 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e ** from pParen
352b0 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 t.. **. ** If
352c0 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 the siblings are
352d0 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 on leaf pages,
352e0 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 then the child p
352f0 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 ointers of the.
35300 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c ** divider cell
35310 73 20 61 72 65 20 73 74 72 69 70 70 65 64 20 66 s are stripped f
35320 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 rom the cells be
35330 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 63 6f fore they are co
35340 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 pied. ** into a
35350 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 Space1[]. In th
35360 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c is way, all cell
35370 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 s in apCell[] ar
35380 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 e without. ** c
35390 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 hild pointers.
353a0 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 If siblings are
353b0 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e not leaves, then
353c0 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a all cell in. *
353d0 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 * apCell[] inclu
353e0 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 de child pointer
353f0 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 s. Either way,
35400 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 all cells in apC
35410 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 ell[]. ** are a
35420 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 like.. **. **
35430 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 leafCorrection:
35440 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20 61 4 if pPage is a
35450 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 leaf. 0 if pPa
35460 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 ge is not a leaf
35470 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 .. ** lea
35480 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 fData: 1 if pPa
35490 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 ge holds key+dat
354a0 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f a and pParent ho
354b0 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 lds only keys..
354c0 20 2a 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 */. leafCorrec
354d0 74 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d tion = apOld[0]-
354e0 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 >leaf*4;. leafD
354f0 61 74 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e ata = apOld[0]->
35500 68 61 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 hasData;. for(i
35510 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 =0; i<nOld; i++)
35520 7b 0a 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b {. int limit;
35530 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 . . /* Bef
35540 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 ore doing anythi
35550 6e 67 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 ng else, take a
35560 63 6f 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 copy of the i'th
35570 20 6f 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e original siblin
35580 67 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 g. ** The res
35590 74 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 t of this functi
355a0 6f 6e 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 on will use data
355b0 20 66 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 from the copies
355c0 20 72 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 rather. ** t
355d0 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hat the original
355e0 20 70 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 pages since the
355f0 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 original pages
35600 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 will be in the.
35610 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 ** process of
35620 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 being overwritt
35630 65 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 en. */. MemP
35640 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f age *pOld = apCo
35650 70 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 py[i] = (MemPage
35660 2a 29 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e *)&aSpace1[pBt->
35670 70 61 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b pageSize + k*i];
35680 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 . memcpy(pOld
35690 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 , apOld[i], size
356a0 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 of(MemPage));.
356b0 20 20 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 pOld->aData =
356c0 28 76 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b (void*)&pOld[1];
356d0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 . memcpy(pOld
356e0 2d 3e 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 ->aData, apOld[i
356f0 5d 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 ]->aData, pBt->p
35700 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c ageSize);.. l
35710 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 imit = pOld->nCe
35720 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c ll+pOld->nOverfl
35730 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 ow;. if( pOld
35740 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 7b ->nOverflow>0 ){
35750 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 . for(j=0;
35760 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 j<limit; j++){.
35770 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
35780 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 Cell<nMaxCells )
35790 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c ;. apCell
357a0 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 [nCell] = findOv
357b0 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c erflowCell(pOld,
357c0 20 6a 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 j);. szC
357d0 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c ell[nCell] = cel
357e0 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 lSizePtr(pOld, a
357f0 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 pCell[nCell]);.
35800 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a nCell++;.
35810 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
35820 65 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 44 61 e{. u8 *aDa
35830 74 61 20 3d 20 70 4f 6c 64 2d 3e 61 44 61 74 61 ta = pOld->aData
35840 3b 0a 20 20 20 20 20 20 75 31 36 20 6d 61 73 6b ;. u16 mask
35850 50 61 67 65 20 3d 20 70 4f 6c 64 2d 3e 6d 61 73 Page = pOld->mas
35860 6b 50 61 67 65 3b 0a 20 20 20 20 20 20 75 31 36 kPage;. u16
35870 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 4f cellOffset = pO
35880 6c 64 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a ld->cellOffset;.
35890 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
358a0 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 <limit; j++){.
358b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 assert( nC
358c0 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ell<nMaxCells );
358d0 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b . apCell[
358e0 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 43 65 6c nCell] = findCel
358f0 6c 76 32 28 61 44 61 74 61 2c 20 6d 61 73 6b 50 lv2(aData, maskP
35900 61 67 65 2c 20 63 65 6c 6c 4f 66 66 73 65 74 2c age, cellOffset,
35910 20 6a 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 j);. szC
35920 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c ell[nCell] = cel
35930 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 lSizePtr(pOld, a
35940 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 pCell[nCell]);.
35950 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a nCell++;.
35960 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 20 20 }. }
35970 20 20 20 20 0a 20 20 20 20 69 66 28 20 69 3c 6e . if( i<n
35980 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61 66 44 61 Old-1 && !leafDa
35990 74 61 29 7b 0a 20 20 20 20 20 20 75 31 36 20 73 ta){. u16 s
359a0 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65 77 5b 69 z = (u16)szNew[i
359b0 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65 ];. u8 *pTe
359c0 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 mp;. assert
359d0 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c ( nCell<nMaxCell
359e0 73 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c s );. szCel
359f0 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 l[nCell] = sz;.
35a00 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61 53 pTemp = &aS
35a10 70 61 63 65 31 5b 69 53 70 61 63 65 31 5d 3b 0a pace1[iSpace1];.
35a20 20 20 20 20 20 20 69 53 70 61 63 65 31 20 2b 3d iSpace1 +=
35a30 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 sz;. asser
35a40 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 6d 61 78 4c t( sz<=pBt->maxL
35a50 6f 63 61 6c 2b 32 33 20 29 3b 0a 20 20 20 20 20 ocal+23 );.
35a60 20 61 73 73 65 72 74 28 20 69 53 70 61 63 65 31 assert( iSpace1
35a70 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 70 61 <= (int)pBt->pa
35a80 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 geSize );.
35a90 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70 memcpy(pTemp, ap
35aa0 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 Div[i], sz);.
35ab0 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d apCell[nCell]
35ac0 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 = pTemp+leafCor
35ad0 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 rection;. a
35ae0 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 ssert( leafCorre
35af0 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 ction==0 || leaf
35b00 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b Correction==4 );
35b10 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 . szCell[nC
35b20 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 ell] = szCell[nC
35b30 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 ell] - leafCorre
35b40 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 ction;. if(
35b50 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a !pOld->leaf ){.
35b60 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
35b70 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
35b80 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 0 );. ass
35b90 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 ert( pOld->hdrOf
35ba0 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 fset==0 );.
35bb0 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 /* The right
35bc0 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 pointer of the c
35bd0 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62 hild page pOld b
35be0 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a ecomes the left.
35bf0 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 ** point
35c00 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 er of the divide
35c10 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 r cell */.
35c20 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b memcpy(apCell[
35c30 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 nCell], &pOld->a
35c40 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 Data[8], 4);.
35c50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
35c60 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f assert( leafCo
35c70 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 rrection==4 );.
35c80 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c if( szCel
35c90 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 l[nCell]<4 ){.
35ca0 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
35cb0 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c t allow any cell
35cc0 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 s smaller than 4
35cd0 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 bytes. */.
35ce0 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c szCell[nCel
35cf0 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 l] = 4;.
35d00 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
35d10 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 nCell++;. }.
35d20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 }.. /*. ** Fi
35d30 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d gure out the num
35d40 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 ber of pages nee
35d50 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 ded to hold all
35d60 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a nCell cells.. *
35d70 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d * Store this num
35d80 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 ber in "k". Als
35d90 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b o compute szNew[
35da0 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74 ] which is the t
35db0 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f otal. ** size o
35dc0 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 f all cells on t
35dd0 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 he i-th page and
35de0 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 cntNew[] which
35df0 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a is the index. *
35e00 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 * in apCell[] of
35e10 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 the cell that d
35e20 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72 ivides page i fr
35e30 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 om page i+1. .
35e40 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 ** cntNew[k] sh
35e50 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c ould equal nCell
35e60 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 .. **. ** Valu
35e70 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 es computed by t
35e80 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a his block:. **.
35e90 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b ** k
35ea0 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 : The total numb
35eb0 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 er of sibling pa
35ec0 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 ges. ** szNe
35ed0 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 w[i]: Spaced use
35ee0 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 d on the i-th si
35ef0 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a bling page.. **
35f00 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e cntNew[i]: In
35f10 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 dex in apCell[]
35f20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 and szCell[] for
35f30 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 the first cell
35f40 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 to. **
35f50 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f the right o
35f60 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 f the i-th sibli
35f70 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 ng page.. ** us
35f80 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 ableSpace: Numbe
35f90 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
35fa0 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e ace available on
35fb0 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 each sibling..
35fc0 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 ** . */. usab
35fd0 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 leSpace = pBt->u
35fe0 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b sableSize - 12 +
35ff0 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b leafCorrection;
36000 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d . for(subtotal=
36010 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 k=i=0; i<nCell;
36020 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 i++){. assert
36030 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( i<nMaxCells );
36040 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d . subtotal +=
36050 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a szCell[i] + 2;.
36060 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c if( subtotal
36070 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 > usableSpace )
36080 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d {. szNew[k]
36090 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a = subtotal - sz
360a0 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 Cell[i];. c
360b0 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 ntNew[k] = i;.
360c0 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 if( leafData
360d0 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ i--; }.
360e0 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 subtotal = 0;.
360f0 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 k++;.
36100 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 if( k>NB+1 ){ rc
36110 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
36120 54 5f 42 4b 50 54 3b 20 67 6f 74 6f 20 62 61 6c T_BKPT; goto bal
36130 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 20 7d 0a ance_cleanup; }.
36140 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 }. }. szNe
36150 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b w[k] = subtotal;
36160 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e . cntNew[k] = n
36170 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 Cell;. k++;..
36180 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b /*. ** The pack
36190 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20 ing computed by
361a0 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f the previous blo
361b0 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77 ck is biased tow
361c0 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 ard the siblings
361d0 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 . ** on the lef
361e0 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66 t side. The lef
361f0 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61 t siblings are a
36200 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c lways nearly ful
36210 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a l, while the. *
36220 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 * right-most sib
36230 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65 ling might be ne
36240 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 arly empty. Thi
36250 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 s block of code
36260 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f attempts. ** to
36270 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b adjust the pack
36280 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 ing of siblings
36290 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20 to get a better
362a0 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 balance.. **.
362b0 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 ** This adjustme
362c0 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 nt is more than
362d0 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e an optimization.
362e0 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62 The packing ab
362f0 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 ove might. ** b
36300 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 e so out of bala
36310 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c nce as to be ill
36320 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 egal. For examp
36330 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f le, the right-mo
36340 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 st. ** sibling
36350 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 might be complet
36360 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 ely empty. This
36370 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e adjustment is n
36380 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a ot optional.. *
36390 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 /. for(i=k-1; i
363a0 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e >0; i--){. in
363b0 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 t szRight = szNe
363c0 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f w[i]; /* Size o
363d0 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 f sibling on the
363e0 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e right */. in
363f0 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 t szLeft = szNew
36400 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f [i-1]; /* Size o
36410 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 f sibling on the
36420 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 left */. int
36430 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
36440 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 /* Index of rig
36450 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 ht-most cell in
36460 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a left sibling */.
36470 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 int d;
36480 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
36490 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 of first cell t
364a0 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 o the left of ri
364b0 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a ght sibling */..
364c0 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 r = cntNew[i
364d0 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d -1] - 1;. d =
364e0 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 r + 1 - leafDat
364f0 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 a;. assert( d
36500 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
36510 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 assert( r<nMax
36520 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 Cells );. whi
36530 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 0a le( szRight==0 .
36540 20 20 20 20 20 20 20 7c 7c 20 28 21 62 42 75 6c || (!bBul
36550 6b 20 26 26 20 73 7a 52 69 67 68 74 2b 73 7a 43 k && szRight+szC
36560 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 ell[d]+2<=szLeft
36570 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 29 20 -(szCell[r]+2))
36580 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 7a . ){. sz
36590 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b Right += szCell[
365a0 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a d] + 2;. sz
365b0 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 Left -= szCell[r
365c0 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 ] + 2;. cnt
365d0 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 New[i-1]--;.
365e0 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 r = cntNew[i-1
365f0 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d ] - 1;. d =
36600 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 r + 1 - leafDat
36610 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e a;. }. szN
36620 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b ew[i] = szRight;
36630 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 . szNew[i-1]
36640 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 = szLeft;. }..
36650 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f /* Either we fo
36660 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 und one or more
36670 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d cells (cntnew[0]
36680 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20 69 73 )>0) or pPage is
36690 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 . ** a virtual
366a0 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 76 69 root page. A vi
366b0 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 rtual root page
366c0 69 73 20 77 68 65 6e 20 74 68 65 20 72 65 61 6c is when the real
366d0 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20 root. ** page
366e0 69 73 20 70 61 67 65 20 31 20 61 6e 64 20 77 65 is page 1 and we
366f0 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 63 68 are the only ch
36700 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61 67 65 ild of that page
36710 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 50 44 41 .. **. ** UPDA
36720 54 45 3a 20 20 54 68 65 20 61 73 73 65 72 74 28 TE: The assert(
36730 29 20 62 65 6c 6f 77 20 69 73 20 6e 6f 74 20 6e ) below is not n
36740 65 63 65 73 73 61 72 69 6c 79 20 74 72 75 65 20 ecessarily true
36750 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a if the database.
36760 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 63 6f 72 ** file is cor
36770 72 75 70 74 2e 20 20 54 68 65 20 63 6f 72 72 75 rupt. The corru
36780 70 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 65 ption will be de
36790 74 65 63 74 65 64 20 61 6e 64 20 72 65 70 6f 72 tected and repor
367a0 74 65 64 20 6c 61 74 65 72 0a 20 20 2a 2a 20 69 ted later. ** i
367b0 6e 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 n this procedure
367c0 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 so there is no
367d0 6e 65 65 64 20 74 6f 20 61 63 74 20 75 70 6f 6e need to act upon
367e0 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 23 69 it now.. */.#i
367f0 66 20 30 0a 20 20 61 73 73 65 72 74 28 20 63 6e f 0. assert( cn
36800 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 tNew[0]>0 || (pP
36810 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 arent->pgno==1 &
36820 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c & pParent->nCell
36830 3d 3d 30 29 20 29 3b 0a 23 65 6e 64 69 66 0a 0a ==0) );.#endif..
36840 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 TRACE(("BALANC
36850 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 E: old: %d %d %d
36860 20 20 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 ",. apOld[0
36870 5d 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f ]->pgno, . nO
36880 6c 64 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d ld>=2 ? apOld[1]
36890 2d 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 ->pgno : 0,.
368a0 6e 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b nOld>=3 ? apOld[
368b0 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 2]->pgno : 0. )
368c0 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c );.. /*. ** Al
368d0 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 locate k new pag
368e0 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 es. Reuse old p
368f0 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 ages where possi
36900 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ble.. */. if(
36910 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d apOld[0]->pgno<=
36920 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 1 ){. rc = SQ
36930 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
36940 54 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 T;. goto bala
36950 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d nce_cleanup;. }
36960 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d 20 61 . pageFlags = a
36970 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 74 61 5b 30 pOld[0]->aData[0
36980 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c ];. for(i=0; i<
36990 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d k; i++){. Mem
369a0 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 Page *pNew;.
369b0 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 if( i<nOld ){.
369c0 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 pNew = apNew
369d0 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b 0a [i] = apOld[i];.
369e0 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d apOld[i] =
369f0 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
36a00 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
36a10 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 3b (pNew->pDbPage);
36a20 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 . nNew++;.
36a30 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
36a40 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
36a50 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 up;. }else{.
36a60 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 30 assert( i>0
36a70 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 );. rc = a
36a80 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
36a90 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 (pBt, &pNew, &pg
36aa0 6e 6f 2c 20 28 62 42 75 6c 6b 20 3f 20 31 20 3a no, (bBulk ? 1 :
36ab0 20 70 67 6e 6f 29 2c 20 30 29 3b 0a 20 20 20 20 pgno), 0);.
36ac0 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
36ad0 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
36ae0 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 . apNew[i]
36af0 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e 4e = pNew;. nN
36b00 65 77 2b 2b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ew++;.. /*
36b10 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d Set the pointer-
36b20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 74 68 map entry for th
36b30 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 e new sibling pa
36b40 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ge. */. if(
36b50 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
36b60 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 . ptrmapP
36b70 75 74 28 70 42 74 2c 20 70 4e 65 77 2d 3e 70 67 ut(pBt, pNew->pg
36b80 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 no, PTRMAP_BTREE
36b90 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c , pParent->pgno,
36ba0 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 &rc);. i
36bb0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
36bc0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
36bd0 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
36be0 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 up;. }.
36bf0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
36c00 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f . /* Free any o
36c10 6c 64 20 70 61 67 65 73 20 74 68 61 74 20 77 65 ld pages that we
36c20 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61 73 re not reused as
36c30 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2f new pages.. */
36c40 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 . while( i<nOld
36c50 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61 67 65 ){. freePage
36c60 28 61 70 4f 6c 64 5b 69 5d 2c 20 26 72 63 29 3b (apOld[i], &rc);
36c70 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
36c80 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
36c90 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 up;. releaseP
36ca0 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 age(apOld[i]);.
36cb0 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b apOld[i] = 0;
36cc0 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 . i++;. }..
36cd0 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68 65 /*. ** Put the
36ce0 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61 63 new pages in ac
36cf0 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 20 cending order.
36d00 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20 20 This helps to.
36d10 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73 20 ** keep entries
36d20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 in the disk file
36d30 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68 61 in order so tha
36d40 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 t a scan. ** of
36d50 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 the table is a
36d60 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f linear scan thro
36d70 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20 54 ugh the file. T
36d80 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72 6e hat. ** in turn
36d90 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72 61 helps the opera
36da0 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 64 ting system to d
36db0 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20 2a eliver pages. *
36dc0 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 * from the disk
36dd0 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20 20 more rapidly..
36de0 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 **. ** An O(n^2
36df0 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 ) insertion sort
36e00 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75 73 algorithm is us
36e10 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20 20 ed, but since.
36e20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f ** n is never mo
36e30 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73 6d re than NB (a sm
36e40 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74 all constant), t
36e50 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 hat should. **
36e60 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d not be a problem
36e70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e .. **. ** When
36e80 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 NB==3, this one
36e90 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 optimization ma
36ea0 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 kes the database
36eb0 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25 20 . ** about 25%
36ec0 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67 65 faster for large
36ed0 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64 20 insertions and
36ee0 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a deletions.. */.
36ef0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 for(i=0; i<k-1
36f00 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 ; i++){. int
36f10 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b 69 5d 2d minV = apNew[i]-
36f20 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 6e 74 20 6d >pgno;. int m
36f30 69 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 inI = i;. for
36f40 28 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b (j=i+1; j<k; j++
36f50 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 70 4e ){. if( apN
36f60 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28 75 6e 73 ew[j]->pgno<(uns
36f70 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b 0a 20 20 igned)minV ){.
36f80 20 20 20 20 20 20 6d 69 6e 49 20 3d 20 6a 3b 0a minI = j;.
36f90 20 20 20 20 20 20 20 20 6d 69 6e 56 20 3d 20 61 minV = a
36fa0 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 pNew[j]->pgno;.
36fb0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
36fc0 20 69 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 if( minI>i ){.
36fd0 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 MemPage *pT
36fe0 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 4e ;. pT = apN
36ff0 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e ew[i];. apN
37000 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 ew[i] = apNew[mi
37010 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 nI];. apNew
37020 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 [minI] = pT;.
37030 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 28 28 }. }. TRACE((
37040 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25 64 28 "new: %d(%d) %d(
37050 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64 %d) %d(%d) %d(%d
37060 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 ) %d(%d)\n",.
37070 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c apNew[0]->pgno,
37080 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e szNew[0],. n
37090 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31 New>=2 ? apNew[1
370a0 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 ]->pgno : 0, nNe
370b0 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 w>=2 ? szNew[1]
370c0 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 : 0,. nNew>=3
370d0 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e ? apNew[2]->pgn
370e0 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f o : 0, nNew>=3 ?
370f0 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 szNew[2] : 0,.
37100 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e nNew>=4 ? apN
37110 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c ew[3]->pgno : 0,
37120 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77 nNew>=4 ? szNew
37130 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 [3] : 0,. nNe
37140 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d w>=5 ? apNew[4]-
37150 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e >pgno : 0, nNew>
37160 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 =5 ? szNew[4] :
37170 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 0));.. assert(
37180 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
37190 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
371a0 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 70 >pDbPage) );. p
371b0 75 74 34 62 79 74 65 28 70 52 69 67 68 74 2c 20 ut4byte(pRight,
371c0 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 apNew[nNew-1]->p
371d0 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a gno);.. /*. **
371e0 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75 Evenly distribu
371f0 74 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61 te the data in a
37200 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 pCell[] across t
37210 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 he new pages..
37220 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65 ** Insert divide
37230 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 r cells into pPa
37240 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72 rent as necessar
37250 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b y.. */. j = 0;
37260 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e . for(i=0; i<nN
37270 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a ew; i++){. /*
37280 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 Assemble the ne
37290 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 w sibling page.
372a0 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a */. MemPage *
372b0 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b pNew = apNew[i];
372c0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e . assert( j<n
372d0 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 MaxCells );.
372e0 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 zeroPage(pNew, p
372f0 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 20 61 ageFlags);. a
37300 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 ssemblePage(pNew
37310 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26 , cntNew[i]-j, &
37320 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65 apCell[j], &szCe
37330 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65 ll[j]);. asse
37340 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e rt( pNew->nCell>
37350 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26 0 || (nNew==1 &&
37360 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29 cntNew[0]==0) )
37370 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e ;. assert( pN
37380 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ew->nOverflow==0
37390 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 );.. j = cnt
373a0 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 New[i];.. /*
373b0 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 If the sibling p
373c0 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 age assembled ab
373d0 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 ove was not the
373e0 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 right-most sibli
373f0 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 ng,. ** inser
37400 74 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c t a divider cell
37410 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 into the parent
37420 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
37430 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e 65 77 assert( i<nNew
37440 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29 -1 || j==nCell )
37450 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c ;. if( j<nCel
37460 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 l ){. u8 *p
37470 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a Cell;. u8 *
37480 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 pTemp;. int
37490 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 sz;.. asse
374a0 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 rt( j<nMaxCells
374b0 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
374c0 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 apCell[j];.
374d0 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d sz = szCell[j]
374e0 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f + leafCorrectio
374f0 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d n;. pTemp =
37500 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76 &aOvflSpace[iOv
37510 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 flSpace];.
37520 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 if( !pNew->leaf
37530 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
37540 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 y(&pNew->aData[8
37550 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 ], pCell, 4);.
37560 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 }else if( le
37570 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 afData ){.
37580 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 /* If the tree
37590 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 is a leaf-data
375a0 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 tree, and the si
375b0 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 blings are leave
375c0 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 s, . ** t
375d0 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hen there is no
375e0 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 divider cell in
375f0 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 apCell[]. Instea
37600 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a d, the divider .
37610 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 ** cell
37620 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 consists of the
37630 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 integer key for
37640 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 the right-most c
37650 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20 ell of .
37660 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 ** the sibling-p
37670 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 age assembled ab
37680 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 ove only..
37690 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c */. Cel
376a0 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 lInfo info;.
376b0 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 j--;.
376c0 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
376d0 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b tr(pNew, apCell[
376e0 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 j], &info);.
376f0 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d pCell = pTem
37700 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 p;. sz =
37710 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28 26 70 4 + putVarint(&p
37720 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b Cell[4], info.nK
37730 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 ey);. pTe
37740 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 mp = 0;. }e
37750 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 lse{. pCe
37760 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 ll -= 4;.
37770 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65 /* Obscure case
37780 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 for non-leaf-da
37790 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65 ta trees: If the
377a0 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 cell at pCell w
377b0 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 as. ** pr
377c0 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 eviously stored
377d0 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 on a leaf node,
377e0 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64 and its reported
377f0 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20 size was 4.
37800 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 ** bytes, th
37810 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c en it may actual
37820 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 ly be smaller th
37830 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 an this .
37840 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65 50 61 ** (see btreePa
37850 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 rseCellPtr(), 4
37860 62 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e bytes is the min
37870 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 imum size of.
37880 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c ** any cell
37890 29 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 ). But it is imp
378a0 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 ortant to pass t
378b0 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 he correct size
378c0 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 to . ** i
378d0 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 nsertCell(), so
378e0 72 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c reparse the cell
378f0 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a now.. **
37900 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 . ** Note
37910 20 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e that this can n
37920 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61 ever happen in a
37930 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69 n SQLite data fi
37940 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 le, as all.
37950 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 ** cells are
37960 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
37970 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e . It only happen
37980 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65 s in b-trees use
37990 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 d. ** to
379a0 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45 evaluate "IN (SE
379b0 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 LECT ...)" and s
379c0 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a imilar clauses..
379d0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
379e0 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d if( szCell[j]
379f0 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==4 ){.
37a00 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72 assert(leafCorr
37a10 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 ection==4);.
37a20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 sz = cellS
37a30 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 izePtr(pParent,
37a40 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 pCell);.
37a50 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
37a60 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a iOvflSpace += sz
37a70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
37a80 73 7a 3c 3d 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 sz<=pBt->maxLoca
37a90 6c 2b 32 33 20 29 3b 0a 20 20 20 20 20 20 61 73 l+23 );. as
37aa0 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63 65 sert( iOvflSpace
37ab0 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 70 61 <= (int)pBt->pa
37ac0 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 geSize );.
37ad0 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 insertCell(pPare
37ae0 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c nt, nxDiv, pCell
37af0 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 70 4e 65 , sz, pTemp, pNe
37b00 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 w->pgno, &rc);.
37b10 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
37b20 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 ITE_OK ) goto ba
37b30 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
37b40 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
37b50 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
37b60 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 able(pParent->pD
37b70 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 bPage) );..
37b80 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 44 69 j++;. nxDi
37b90 76 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 v++;. }. }.
37ba0 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e 43 65 6c assert( j==nCel
37bb0 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e l );. assert( n
37bc0 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 Old>0 );. asser
37bd0 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 20 69 t( nNew>0 );. i
37be0 66 28 20 28 70 61 67 65 46 6c 61 67 73 20 26 20 f( (pageFlags &
37bf0 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 29 7b 0a PTF_LEAF)==0 ){.
37c00 20 20 20 20 75 38 20 2a 7a 43 68 69 6c 64 20 3d u8 *zChild =
37c10 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d 31 5d &apCopy[nOld-1]
37c20 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20 20 20 20 ->aData[8];.
37c30 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b 6e 4e memcpy(&apNew[nN
37c40 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c ew-1]->aData[8],
37c50 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a 20 20 7d zChild, 4);. }
37c60 0a 0a 20 20 69 66 28 20 69 73 52 6f 6f 74 20 26 .. if( isRoot &
37c70 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c & pParent->nCell
37c80 3d 3d 30 20 26 26 20 70 50 61 72 65 6e 74 2d 3e ==0 && pParent->
37c90 68 64 72 4f 66 66 73 65 74 3c 3d 61 70 4e 65 77 hdrOffset<=apNew
37ca0 5b 30 5d 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 [0]->nFree ){.
37cb0 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 /* The root pa
37cc0 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 ge of the b-tree
37cd0 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 6e 6f now contains no
37ce0 20 63 65 6c 6c 73 2e 20 54 68 65 20 6f 6e 6c 79 cells. The only
37cf0 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 sibling. **
37d00 70 61 67 65 20 69 73 20 74 68 65 20 72 69 67 68 page is the righ
37d10 74 2d 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 t-child of the p
37d20 61 72 65 6e 74 2e 20 43 6f 70 79 20 74 68 65 20 arent. Copy the
37d30 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a contents of the.
37d40 20 20 20 20 2a 2a 20 63 68 69 6c 64 20 70 61 67 ** child pag
37d50 65 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e e into the paren
37d60 74 2c 20 64 65 63 72 65 61 73 69 6e 67 20 74 68 t, decreasing th
37d70 65 20 6f 76 65 72 61 6c 6c 20 68 65 69 67 68 74 e overall height
37d80 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 62 of the. ** b
37d90 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 -tree structure
37da0 62 79 20 6f 6e 65 2e 20 54 68 69 73 20 69 73 20 by one. This is
37db0 64 65 73 63 72 69 62 65 64 20 61 73 20 74 68 65 described as the
37dc0 20 22 62 61 6c 61 6e 63 65 2d 73 68 61 6c 6c 6f "balance-shallo
37dd0 77 65 72 22 0a 20 20 20 20 2a 2a 20 73 75 62 2d wer". ** sub-
37de0 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20 73 6f 6d algorithm in som
37df0 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 2e e documentation.
37e00 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
37e10 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
37e20 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
37e30 65 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 e, the call to c
37e40 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 29 opyNodeContent()
37e50 20 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 61 6c . ** sets al
37e60 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e l pointer-map en
37e70 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 tries correspond
37e80 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 ing to database
37e90 69 6d 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 image pages .
37ea0 20 2a 2a 20 66 6f 72 20 77 68 69 63 68 20 74 68 ** for which th
37eb0 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f e pointer is sto
37ec0 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 red within the c
37ed0 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 63 6f 70 ontent being cop
37ee0 69 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ied.. **.
37ef0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 73 ** The second as
37f00 73 65 72 74 20 62 65 6c 6f 77 20 76 65 72 69 66 sert below verif
37f10 69 65 73 20 74 68 61 74 20 74 68 65 20 63 68 69 ies that the chi
37f20 6c 64 20 70 61 67 65 20 69 73 20 64 65 66 72 61 ld page is defra
37f30 67 6d 65 6e 74 65 64 0a 20 20 20 20 2a 2a 20 28 gmented. ** (
37f40 69 74 20 6d 75 73 74 20 62 65 2c 20 61 73 20 69 it must be, as i
37f50 74 20 77 61 73 20 6a 75 73 74 20 72 65 63 6f 6e t was just recon
37f60 73 74 72 75 63 74 65 64 20 75 73 69 6e 67 20 61 structed using a
37f70 73 73 65 6d 62 6c 65 50 61 67 65 28 29 29 2e 20 ssemblePage()).
37f80 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 69 This. ** is i
37f90 6d 70 6f 72 74 61 6e 74 20 69 66 20 74 68 65 20 mportant if the
37fa0 70 61 72 65 6e 74 20 70 61 67 65 20 68 61 70 70 parent page happ
37fb0 65 6e 73 20 74 6f 20 62 65 20 70 61 67 65 20 31 ens to be page 1
37fc0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
37fd0 0a 20 20 20 20 2a 2a 20 69 6d 61 67 65 2e 20 20 . ** image.
37fe0 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e */. assert( n
37ff0 4e 65 77 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 New==1 );. as
38000 73 65 72 74 28 20 61 70 4e 65 77 5b 30 5d 2d 3e sert( apNew[0]->
38010 6e 46 72 65 65 20 3d 3d 20 0a 20 20 20 20 20 20 nFree == .
38020 20 20 28 67 65 74 32 62 79 74 65 28 26 61 70 4e (get2byte(&apN
38030 65 77 5b 30 5d 2d 3e 61 44 61 74 61 5b 35 5d 29 ew[0]->aData[5])
38040 2d 61 70 4e 65 77 5b 30 5d 2d 3e 63 65 6c 6c 4f -apNew[0]->cellO
38050 66 66 73 65 74 2d 61 70 4e 65 77 5b 30 5d 2d 3e ffset-apNew[0]->
38060 6e 43 65 6c 6c 2a 32 29 20 0a 20 20 20 20 29 3b nCell*2) . );
38070 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f 6e . copyNodeCon
38080 74 65 6e 74 28 61 70 4e 65 77 5b 30 5d 2c 20 70 tent(apNew[0], p
38090 50 61 72 65 6e 74 2c 20 26 72 63 29 3b 0a 20 20 Parent, &rc);.
380a0 20 20 66 72 65 65 50 61 67 65 28 61 70 4e 65 77 freePage(apNew
380b0 5b 30 5d 2c 20 26 72 63 29 3b 0a 20 20 7d 65 6c [0], &rc);. }el
380c0 73 65 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 se if( ISAUTOVAC
380d0 55 55 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 UUM ){. /* Fi
380e0 78 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 x the pointer-ma
380f0 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c p entries for al
38100 6c 20 74 68 65 20 63 65 6c 6c 73 20 74 68 61 74 l the cells that
38110 20 77 65 72 65 20 73 68 69 66 74 65 64 20 61 72 were shifted ar
38120 6f 75 6e 64 2e 20 0a 20 20 20 20 2a 2a 20 54 68 ound. . ** Th
38130 65 72 65 20 61 72 65 20 73 65 76 65 72 61 6c 20 ere are several
38140 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 20 different types
38150 6f 66 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 of pointer-map e
38160 6e 74 72 69 65 73 20 74 68 61 74 20 6e 65 65 64 ntries that need
38170 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 64 65 to. ** be de
38180 61 6c 74 20 77 69 74 68 20 62 79 20 74 68 69 73 alt with by this
38190 20 72 6f 75 74 69 6e 65 2e 20 53 6f 6d 65 20 6f routine. Some o
381a0 66 20 74 68 65 73 65 20 68 61 76 65 20 62 65 65 f these have bee
381b0 6e 20 73 65 74 20 61 6c 72 65 61 64 79 2c 20 62 n set already, b
381c0 75 74 0a 20 20 20 20 2a 2a 20 6d 61 6e 79 20 68 ut. ** many h
381d0 61 76 65 20 6e 6f 74 2e 20 54 68 65 20 66 6f 6c ave not. The fol
381e0 6c 6f 77 69 6e 67 20 69 73 20 61 20 73 75 6d 6d lowing is a summ
381f0 61 72 79 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ary:. **.
38200 2a 2a 20 20 20 31 29 20 54 68 65 20 65 6e 74 72 ** 1) The entr
38210 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ies associated w
38220 69 74 68 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 ith new sibling
38230 70 61 67 65 73 20 74 68 61 74 20 77 65 72 65 20 pages that were
38240 6e 6f 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 not. **
38250 73 69 62 6c 69 6e 67 73 20 77 68 65 6e 20 74 68 siblings when th
38260 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 is function was
38270 63 61 6c 6c 65 64 2e 20 54 68 65 73 65 20 68 61 called. These ha
38280 76 65 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a ve already. *
38290 2a 20 20 20 20 20 20 62 65 65 6e 20 73 65 74 2e * been set.
382a0 20 57 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74 We don't need t
382b0 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 6f 6c o worry about ol
382c0 64 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 d siblings that
382d0 77 65 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 were. **
382e0 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 moved to the fr
382f0 65 65 2d 6c 69 73 74 20 2d 20 74 68 65 20 66 72 ee-list - the fr
38300 65 65 50 61 67 65 28 29 20 63 6f 64 65 20 68 61 eePage() code ha
38310 73 20 74 61 6b 65 6e 20 63 61 72 65 0a 20 20 20 s taken care.
38320 20 2a 2a 20 20 20 20 20 20 6f 66 20 74 68 6f 73 ** of thos
38330 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
38340 20 20 20 32 29 20 54 68 65 20 70 6f 69 6e 74 65 2) The pointe
38350 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 61 73 r-map entries as
38360 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
38370 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 e first overflow
38380 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 70 61 67 . ** pag
38390 65 20 69 6e 20 61 6e 79 20 6f 76 65 72 66 6c 6f e in any overflo
383a0 77 20 63 68 61 69 6e 73 20 75 73 65 64 20 62 79 w chains used by
383b0 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c new divider cel
383c0 6c 73 2e 20 54 68 65 73 65 20 0a 20 20 20 20 2a ls. These . *
383d0 2a 20 20 20 20 20 20 68 61 76 65 20 61 6c 73 6f * have also
383e0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 74 61 already been ta
383f0 6b 65 6e 20 63 61 72 65 20 6f 66 20 62 79 20 74 ken care of by t
38400 68 65 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 20 he insertCell()
38410 63 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 code.. **.
38420 20 2a 2a 20 20 20 33 29 20 49 66 20 74 68 65 20 ** 3) If the
38430 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 sibling pages ar
38440 65 20 6