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 6e 20 65 78 74 mplements an ext
0190: 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 ernal (disk-base
01a0: 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e d) database usin
01b0: 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 g BTrees..** See
01c0: 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d the header comm
01d0: 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 ent on "btreeInt
01e0: 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e .h" for addition
01f0: 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
0200: 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 ** Including a d
0210: 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 escription of fi
0220: 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e le format and an
0230: 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 overview of ope
0240: 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 6e 63 6c ration..*/.#incl
0250: 75 64 65 20 22 62 74 72 65 65 49 6e 74 2e 68 22 ude "btreeInt.h"
0260: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 ../*.** The head
0270: 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 er string that a
0280: 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 ppears at the be
0290: 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 ginning of every
02a0: 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 .** SQLite datab
02b0: 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ase..*/.static c
02c0: 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 onst char zMagic
02d0: 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 Header[] = SQLIT
02e0: 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a E_FILE_HEADER;..
02f0: 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 /*.** Set this g
0300: 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 lobal variable t
0310: 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 o 1 to enable tr
0320: 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 acing using the
0330: 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a TRACE.** macro..
0340: 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c */.#if 0.int sql
0350: 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 31 ite3BtreeTrace=1
0360: 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e ; /* True to en
0370: 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a able tracing */.
0380: 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 # define TRACE(X
0390: 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 ) if(sqlite3Btr
03a0: 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 eeTrace){printf
03b0: 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 X;fflush(stdout)
03c0: 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e ;}.#else.# defin
03d0: 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 e TRACE(X).#endi
03e0: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 f../*.** Extract
03f0: 20 61 20 32 2d 62 79 74 65 20 62 69 67 2d 65 6e a 2-byte big-en
0400: 64 69 61 6e 20 69 6e 74 65 67 65 72 20 66 72 6f dian integer fro
0410: 6d 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e m an array of un
0420: 73 69 67 6e 65 64 20 62 79 74 65 73 2e 0a 2a 2a signed bytes..**
0430: 20 42 75 74 20 69 66 20 74 68 65 20 76 61 6c 75 But if the valu
0440: 65 20 69 73 20 7a 65 72 6f 2c 20 6d 61 6b 65 20 e is zero, make
0450: 69 74 20 36 35 35 33 36 2e 0a 2a 2a 0a 2a 2a 20 it 65536..**.**
0460: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
0470: 75 73 65 64 20 74 6f 20 65 78 74 72 61 63 74 20 used to extract
0480: 74 68 65 20 22 6f 66 66 73 65 74 20 74 6f 20 63 the "offset to c
0490: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
04a0: 22 20 76 61 6c 75 65 0a 2a 2a 20 66 72 6f 6d 20 " value.** from
04b0: 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 61 20 the header of a
04c0: 62 74 72 65 65 20 70 61 67 65 2e 20 20 49 66 20 btree page. If
04d0: 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 the page size is
04e0: 20 36 35 35 33 36 20 61 6e 64 20 74 68 65 20 70 65536 and the p
04f0: 61 67 65 0a 2a 2a 20 69 73 20 65 6d 70 74 79 2c age.** is empty,
0500: 20 74 68 65 20 6f 66 66 73 65 74 20 73 68 6f 75 the offset shou
0510: 6c 64 20 62 65 20 36 35 35 33 36 2c 20 62 75 74 ld be 65536, but
0520: 20 74 68 65 20 32 2d 62 79 74 65 20 76 61 6c 75 the 2-byte valu
0530: 65 20 73 74 6f 72 65 73 20 7a 65 72 6f 2e 0a 2a e stores zero..*
0540: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d * This routine m
0550: 61 6b 65 73 20 74 68 65 20 6e 65 63 65 73 73 61 akes the necessa
0560: 72 79 20 61 64 6a 75 73 74 6d 65 6e 74 20 74 6f ry adjustment to
0570: 20 36 35 35 33 36 2e 0a 2a 2f 0a 23 64 65 66 69 65536..*/.#defi
0580: 6e 65 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 ne get2byteNotZe
0590: 72 6f 28 58 29 20 20 28 28 28 28 28 69 6e 74 29 ro(X) (((((int)
05a0: 67 65 74 32 62 79 74 65 28 58 29 29 2d 31 29 26 get2byte(X))-1)&
05b0: 30 78 66 66 66 66 29 2b 31 29 0a 0a 2f 2a 0a 2a 0xffff)+1)../*.*
05c0: 2a 20 56 61 6c 75 65 73 20 70 61 73 73 65 64 20 * Values passed
05d0: 61 73 20 74 68 65 20 35 74 68 20 61 72 67 75 6d as the 5th argum
05e0: 65 6e 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 42 ent to allocateB
05f0: 74 72 65 65 50 61 67 65 28 29 0a 2a 2f 0a 23 64 treePage().*/.#d
0600: 65 66 69 6e 65 20 42 54 41 4c 4c 4f 43 5f 41 4e efine BTALLOC_AN
0610: 59 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 Y 0
0620: 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 79 20 /* Allocate any
0630: 70 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 page */.#define
0640: 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 31 20 BTALLOC_EXACT 1
0650: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c /* All
0660: 6f 63 61 74 65 20 65 78 61 63 74 20 70 61 67 65 ocate exact page
0670: 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a if possible */.
0680: 23 64 65 66 69 6e 65 20 42 54 41 4c 4c 4f 43 5f #define BTALLOC_
0690: 4c 45 20 20 20 20 32 20 20 20 20 20 20 20 20 20 LE 2
06a0: 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e /* Allocate an
06b0: 79 20 70 61 67 65 20 3c 3d 20 74 68 65 20 70 61 y page <= the pa
06c0: 72 61 6d 65 74 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a rameter */../*.*
06d0: 2a 20 4d 61 63 72 6f 20 49 66 4e 6f 74 4f 6d 69 * Macro IfNotOmi
06e0: 74 41 56 28 78 29 20 72 65 74 75 72 6e 73 20 28 tAV(x) returns (
06f0: 78 29 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 x) if SQLITE_OMI
0700: 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 T_AUTOVACUUM is
0710: 6e 6f 74 20 0a 2a 2a 20 64 65 66 69 6e 65 64 2c not .** defined,
0720: 20 6f 72 20 30 20 69 66 20 69 74 20 69 73 2e 20 or 0 if it is.
0730: 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a For example:.**.
0740: 2a 2a 20 20 20 62 49 6e 63 72 56 61 63 75 75 6d ** bIncrVacuum
0750: 20 3d 20 49 66 4e 6f 74 4f 6d 69 74 41 56 28 70 = IfNotOmitAV(p
0760: 42 74 53 68 61 72 65 64 2d 3e 69 6e 63 72 56 61 BtShared->incrVa
0770: 63 75 75 6d 29 3b 0a 2a 2f 0a 23 69 66 6e 64 65 cuum);.*/.#ifnde
0780: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
0790: 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 TOVACUUM.#define
07a0: 20 49 66 4e 6f 74 4f 6d 69 74 41 56 28 65 78 70 IfNotOmitAV(exp
07b0: 72 29 20 28 65 78 70 72 29 0a 23 65 6c 73 65 0a r) (expr).#else.
07c0: 23 64 65 66 69 6e 65 20 49 66 4e 6f 74 4f 6d 69 #define IfNotOmi
07d0: 74 41 56 28 65 78 70 72 29 20 30 0a 23 65 6e 64 tAV(expr) 0.#end
07e0: 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
07f0: 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
0800: 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 ACHE./*.** A lis
0810: 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 t of BtShared ob
0820: 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 jects that are e
0830: 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 ligible for part
0840: 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 icipation.** in
0850: 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 shared cache. T
0860: 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 his variable has
0870: 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 file scope duri
0880: 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 ng normal builds
0890: 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 ,.** but the tes
08a0: 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 t harness needs
08b0: 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 to access it so
08c0: 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 we make it globa
08d0: 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 l for .** test b
08e0: 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 uilds..**.** Acc
08f0: 65 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69 ess to this vari
0900: 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65 able is protecte
0910: 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 d by SQLITE_MUTE
0920: 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e X_STATIC_MASTER.
0930: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
0940: 45 5f 54 45 53 54 0a 42 74 53 68 61 72 65 64 20 E_TEST.BtShared
0950: 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 *SQLITE_WSD sqli
0960: 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
0970: 73 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 st = 0;.#else.st
0980: 61 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 atic BtShared *S
0990: 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 QLITE_WSD sqlite
09a0: 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
09b0: 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e = 0;.#endif.#en
09c0: 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
09d0: 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 IT_SHARED_CACHE
09e0: 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
09f0: 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
0a00: 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c ACHE./*.** Enabl
0a10: 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 e or disable the
0a20: 20 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e shared pager an
0a30: 64 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 d schema feature
0a40: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
0a50: 75 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 utine has no eff
0a60: 65 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 ect on existing
0a70: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
0a80: 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 ions..** The sha
0a90: 72 65 64 20 63 61 63 68 65 20 73 65 74 74 69 6e red cache settin
0aa0: 67 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 g effects only f
0ab0: 75 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a uture calls to.*
0ac0: 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
0ad0: 2c 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 , sqlite3_open16
0ae0: 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f (), or sqlite3_o
0af0: 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 69 6e 74 pen_v2()..*/.int
0b00: 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
0b10: 73 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 shared_cache(int
0b20: 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 enable){. sqli
0b30: 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
0b40: 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c sharedCacheEnabl
0b50: 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 ed = enable;. r
0b60: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
0b70: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 .}.#endif....#if
0b80: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
0b90: 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f SHARED_CACHE. /
0ba0: 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e 63 74 *. ** The funct
0bb0: 69 6f 6e 73 20 71 75 65 72 79 53 68 61 72 65 64 ions queryShared
0bc0: 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 CacheTableLock()
0bd0: 2c 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 , setSharedCache
0be0: 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a TableLock(),. *
0bf0: 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c 6c 53 68 * and clearAllSh
0c00: 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
0c10: 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 cks(). ** manip
0c20: 75 6c 61 74 65 20 65 6e 74 72 69 65 73 20 69 6e ulate entries in
0c30: 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 4c the BtShared.pL
0c40: 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 ock linked list
0c50: 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 20 20 used to store.
0c60: 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 ** shared-cache
0c70: 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c 6f 63 6b table level lock
0c80: 73 2e 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 s. If the librar
0c90: 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 y is compiled wi
0ca0: 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 68 61 72 th the. ** shar
0cb0: 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 ed-cache feature
0cc0: 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 disabled, then
0cd0: 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76 there is only ev
0ce0: 65 72 20 6f 6e 65 20 75 73 65 72 0a 20 20 2a 2a er one user. **
0cf0: 20 6f 66 20 65 61 63 68 20 42 74 53 68 61 72 65 of each BtShare
0d00: 64 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 d structure and
0d10: 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 so this locking
0d20: 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 is not necessary
0d30: 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65 66 69 6e . . ** So defin
0d40: 65 20 74 68 65 20 6c 6f 63 6b 20 72 65 6c 61 74 e the lock relat
0d50: 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 ed functions as
0d60: 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 no-ops.. */. #
0d70: 64 65 66 69 6e 65 20 71 75 65 72 79 53 68 61 72 define queryShar
0d80: 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
0d90: 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f (a,b,c) SQLITE_O
0da0: 4b 0a 20 20 23 64 65 66 69 6e 65 20 73 65 74 53 K. #define setS
0db0: 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
0dc0: 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 ock(a,b,c) SQLIT
0dd0: 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 63 E_OK. #define c
0de0: 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
0df0: 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a heTableLocks(a).
0e00: 20 20 23 64 65 66 69 6e 65 20 64 6f 77 6e 67 72 #define downgr
0e10: 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 adeAllSharedCach
0e20: 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 eTableLocks(a).
0e30: 20 23 64 65 66 69 6e 65 20 68 61 73 53 68 61 72 #define hasShar
0e40: 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
0e50: 28 61 2c 62 2c 63 2c 64 29 20 31 0a 20 20 23 64 (a,b,c,d) 1. #d
0e60: 65 66 69 6e 65 20 68 61 73 52 65 61 64 43 6f 6e efine hasReadCon
0e70: 66 6c 69 63 74 73 28 61 2c 20 62 29 20 30 0a 23 flicts(a, b) 0.#
0e80: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 endif../*.** Imp
0e90: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
0ea0: 68 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 he SQLITE_CORRUP
0eb0: 54 5f 50 41 47 45 28 29 20 6d 61 63 72 6f 2e 20 T_PAGE() macro.
0ec0: 54 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a Takes a single.*
0ed0: 2a 20 28 4d 65 6d 50 61 67 65 2a 29 20 61 73 20 * (MemPage*) as
0ee0: 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 an argument. The
0ef0: 20 28 4d 65 6d 50 61 67 65 2a 29 20 6d 75 73 74 (MemPage*) must
0f00: 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a not be NULL..**
0f10: 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 44 45 .** If SQLITE_DE
0f20: 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e BUG is not defin
0f30: 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 61 ed, then this ma
0f40: 63 72 6f 20 69 73 20 65 71 75 69 76 61 6c 65 6e cro is equivalen
0f50: 74 20 74 6f 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 t to.** SQLITE_C
0f60: 4f 52 52 55 50 54 5f 42 4b 50 54 2e 20 4f 72 2c ORRUPT_BKPT. Or,
0f70: 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 if SQLITE_DEBUG
0f80: 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 is set, then th
0f90: 65 20 6c 6f 67 20 6d 65 73 73 61 67 65 0a 2a 2a e log message.**
0fa0: 20 6e 6f 72 6d 61 6c 6c 79 20 70 72 6f 64 75 63 normally produc
0fb0: 65 64 20 61 73 20 61 20 73 69 64 65 2d 65 66 66 ed as a side-eff
0fc0: 65 63 74 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f ect of SQLITE_CO
0fd0: 52 52 55 50 54 5f 42 4b 50 54 20 69 73 20 61 75 RRUPT_BKPT is au
0fe0: 67 6d 65 6e 74 65 64 0a 2a 2a 20 77 69 74 68 20 gmented.** with
0ff0: 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
1000: 61 6e 64 20 66 69 6c 65 6e 61 6d 65 20 61 73 73 and filename ass
1010: 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
1020: 20 28 4d 65 6d 50 61 67 65 2a 29 2e 0a 2a 2f 0a (MemPage*)..*/.
1030: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1040: 42 55 47 0a 69 6e 74 20 63 6f 72 72 75 70 74 50 BUG.int corruptP
1050: 61 67 65 45 72 72 6f 72 28 69 6e 74 20 6c 69 6e ageError(int lin
1060: 65 6e 6f 2c 20 4d 65 6d 50 61 67 65 20 2a 70 29 eno, MemPage *p)
1070: 7b 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a {. char *zMsg;.
1080: 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1090: 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
10a0: 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d zMsg = sqlite3_m
10b0: 70 72 69 6e 74 66 28 22 64 61 74 61 62 61 73 65 printf("database
10c0: 20 63 6f 72 72 75 70 74 69 6f 6e 20 70 61 67 65 corruption page
10d0: 20 25 64 20 6f 66 20 25 73 22 2c 0a 20 20 20 20 %d of %s",.
10e0: 20 20 28 69 6e 74 29 70 2d 3e 70 67 6e 6f 2c 20 (int)p->pgno,
10f0: 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
1100: 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 name(p->pBt->pPa
1110: 67 65 72 2c 20 30 29 0a 20 20 29 3b 0a 20 20 73 ger, 0). );. s
1120: 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1130: 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 7a alloc();. if( z
1140: 4d 73 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Msg ){. sqlit
1150: 65 33 52 65 70 6f 72 74 45 72 72 6f 72 28 53 51 e3ReportError(SQ
1160: 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6c 69 LITE_CORRUPT, li
1170: 6e 65 6e 6f 2c 20 7a 4d 73 67 29 3b 0a 20 20 7d neno, zMsg);. }
1180: 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
1190: 7a 4d 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 zMsg);. return
11a0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
11b0: 4b 50 54 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 KPT;.}.# define
11c0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
11d0: 41 47 45 28 70 4d 65 6d 50 61 67 65 29 20 63 6f AGE(pMemPage) co
11e0: 72 72 75 70 74 50 61 67 65 45 72 72 6f 72 28 5f rruptPageError(_
11f0: 5f 4c 49 4e 45 5f 5f 2c 20 70 4d 65 6d 50 61 67 _LINE__, pMemPag
1200: 65 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e e).#else.# defin
1210: 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 e SQLITE_CORRUPT
1220: 5f 50 41 47 45 28 70 4d 65 6d 50 61 67 65 29 20 _PAGE(pMemPage)
1230: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
1240: 47 4e 4f 28 70 4d 65 6d 50 61 67 65 2d 3e 70 67 GNO(pMemPage->pg
1250: 6e 6f 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e no).#endif..#ifn
1260: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1270: 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69 SHARED_CACHE..#i
1280: 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1290: 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 G./*.**** This f
12a0: 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 unction is only
12b0: 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
12c0: 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 an assert() stat
12d0: 65 6d 65 6e 74 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a ement. ***.**.**
12e0: 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
12f0: 20 70 42 74 72 65 65 20 68 6f 6c 64 73 20 74 68 pBtree holds th
1300: 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 e required locks
1310: 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 to read or writ
1320: 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 74 61 62 e to the .** tab
1330: 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 le with root pag
1340: 65 20 69 52 6f 6f 74 2e 20 20 20 52 65 74 75 72 e iRoot. Retur
1350: 6e 20 31 20 69 66 20 69 74 20 64 6f 65 73 20 61 n 1 if it does a
1360: 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a nd 0 if not..**.
1370: 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 ** For example,
1380: 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 when writing to
1390: 61 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f a table with roo
13a0: 74 2d 70 61 67 65 20 69 52 6f 6f 74 20 76 69 61 t-page iRoot via
13b0: 20 0a 2a 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65 .** Btree conne
13c0: 63 74 69 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a ction pBtree:.**
13d0: 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 68 .** assert( h
13e0: 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 asSharedCacheTab
13f0: 6c 65 4c 6f 63 6b 28 70 42 74 72 65 65 2c 20 69 leLock(pBtree, i
1400: 52 6f 6f 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c Root, 0, WRITE_L
1410: 4f 43 4b 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 OCK) );.**.** Wh
1420: 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e en writing to an
1430: 20 69 6e 64 65 78 20 74 68 61 74 20 72 65 73 69 index that resi
1440: 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c des in a sharabl
1450: 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 e database, the
1460: 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c .** caller shoul
1470: 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74 d have first obt
1480: 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65 ained a lock spe
1490: 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74 cifying the root
14a0: 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 page of.** the
14b0: 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 corresponding ta
14c0: 62 6c 65 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 ble. This makes
14d0: 74 68 69 6e 67 73 20 61 20 62 69 74 20 6d 6f 72 things a bit mor
14e0: 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2c 0a 2a e complicated,.*
14f0: 2a 20 61 73 20 74 68 69 73 20 6d 6f 64 75 6c 65 * as this module
1500: 20 74 72 65 61 74 73 20 65 61 63 68 20 74 61 62 treats each tab
1510: 6c 65 20 61 73 20 61 20 73 65 70 61 72 61 74 65 le as a separate
1520: 20 73 74 72 75 63 74 75 72 65 2e 20 54 6f 20 64 structure. To d
1530: 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 74 68 65 20 etermine.** the
1540: 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 table correspond
1550: 69 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 ing to the index
1560: 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 being written,
1570: 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
1580: 20 68 61 73 20 74 6f 20 73 65 61 72 63 68 20 74 has to search t
1590: 68 72 6f 75 67 68 20 74 68 65 20 64 61 74 61 62 hrough the datab
15a0: 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a ase schema..**.*
15b0: 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 61 20 6c * Instead of a l
15c0: 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ock on the table
15d0: 2f 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74 /index rooted at
15e0: 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 65 page iRoot, the
15f0: 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 68 caller may.** h
1600: 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b old a write-lock
1610: 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 on the schema t
1620: 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 20 able (root page
1630: 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73 6f 1). This is also
1640: 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e 0a .** acceptable..
1650: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 */.static int ha
1660: 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
1670: 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 2a eLock(. Btree *
1680: 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 pBtree,
1690: 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20 6d /* Handle that m
16a0: 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a 2f ust hold lock */
16b0: 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 20 . Pgno iRoot,
16c0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
16d0: 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65 65 t page of b-tree
16e0: 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 65 */. int isInde
16f0: 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 x, /*
1700: 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69 73 True if iRoot is
1710: 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20 the root of an
1720: 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f 0a index b-tree */.
1730: 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 20 int eLockType
1740: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 /* Requ
1750: 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 28 ired lock type (
1760: 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 READ_LOCK or WRI
1770: 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a 20 TE_LOCK) */.){.
1780: 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
1790: 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42 74 = (Schema *)pBt
17a0: 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 6d ree->pBt->pSchem
17b0: 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 3d a;. Pgno iTab =
17c0: 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 0;. BtLock *pL
17d0: 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ock;.. /* If th
17e0: 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20 6e is database is n
17f0: 6f 74 20 73 68 61 72 65 61 62 6c 65 2c 20 6f 72 ot shareable, or
1800: 20 69 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 if the client i
1810: 73 20 72 65 61 64 69 6e 67 0a 20 20 2a 2a 20 61 s reading. ** a
1820: 6e 64 20 68 61 73 20 74 68 65 20 72 65 61 64 2d nd has the read-
1830: 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 uncommitted flag
1840: 20 73 65 74 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f set, then no lo
1850: 63 6b 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 ck is required.
1860: 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 . ** Return tru
1870: 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 e immediately..
1880: 20 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65 */. if( (pBtre
1890: 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a e->sharable==0).
18a0: 20 20 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65 || (eLockType
18b0: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28 ==READ_LOCK && (
18c0: 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 pBtree->db->flag
18d0: 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 s & SQLITE_ReadU
18e0: 6e 63 6f 6d 6d 69 74 29 29 0a 20 20 29 7b 0a 20 ncommit)). ){.
18f0: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
1900: 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6c .. /* If the cl
1910: 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 ient is reading
1920: 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 20 69 or writing an i
1930: 6e 64 65 78 20 61 6e 64 20 74 68 65 20 73 63 68 ndex and the sch
1940: 65 6d 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 ema is. ** not
1950: 6c 6f 61 64 65 64 2c 20 74 68 65 6e 20 69 74 20 loaded, then it
1960: 69 73 20 74 6f 6f 20 64 69 66 66 69 63 75 6c 74 is too difficult
1970: 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 63 68 65 to actually che
1980: 63 6b 20 74 6f 20 73 65 65 20 69 66 0a 20 20 2a ck to see if. *
1990: 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f * the correct lo
19a0: 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20 20 53 cks are held. S
19b0: 6f 20 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 o do not bother
19c0: 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 72 - just return tr
19d0: 75 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 63 61 ue.. ** This ca
19e0: 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 se does not come
19f0: 20 75 70 20 76 65 72 79 20 6f 66 74 65 6e 20 61 up very often a
1a00: 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 nyhow.. */. if
1a10: 28 20 69 73 49 6e 64 65 78 20 26 26 20 28 21 70 ( isIndex && (!p
1a20: 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 Schema || (pSche
1a30: 6d 61 2d 3e 73 63 68 65 6d 61 46 6c 61 67 73 26 ma->schemaFlags&
1a40: 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 DB_SchemaLoaded)
1a50: 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ==0) ){. retu
1a60: 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 1;. }.. /*
1a70: 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 Figure out the r
1a80: 6f 6f 74 2d 70 61 67 65 20 74 68 61 74 20 74 68 oot-page that th
1a90: 65 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 e lock should be
1aa0: 20 68 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 held on. For ta
1ab0: 62 6c 65 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 ble. ** b-trees
1ac0: 2c 20 74 68 69 73 20 69 73 20 6a 75 73 74 20 74 , this is just t
1ad0: 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
1ae0: 74 68 65 20 62 2d 74 72 65 65 20 62 65 69 6e 67 the b-tree being
1af0: 20 72 65 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 read or. ** wr
1b00: 69 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 itten. For index
1b10: 20 62 2d 74 72 65 65 73 2c 20 69 74 20 69 73 20 b-trees, it is
1b20: 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 the root page of
1b30: 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a the associated.
1b40: 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a ** table. */.
1b50: 20 20 69 66 28 20 69 73 49 6e 64 65 78 20 29 7b if( isIndex ){
1b60: 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 . HashElem *p
1b70: 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 ;. for(p=sqli
1b80: 74 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 teHashFirst(&pSc
1b90: 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 hema->idxHash);
1ba0: 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e p; p=sqliteHashN
1bb0: 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 49 ext(p)){. I
1bc0: 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 28 49 6e ndex *pIdx = (In
1bd0: 64 65 78 20 2a 29 73 71 6c 69 74 65 48 61 73 68 dex *)sqliteHash
1be0: 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 Data(p);. i
1bf0: 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 f( pIdx->tnum==(
1c00: 69 6e 74 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 int)iRoot ){.
1c10: 20 20 20 20 20 69 66 28 20 69 54 61 62 20 29 7b if( iTab ){
1c20: 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 77 . /* Tw
1c30: 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 64 65 78 65 o or more indexe
1c40: 73 20 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 s share the same
1c50: 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 65 root page. The
1c60: 72 65 20 6d 75 73 74 0a 20 20 20 20 20 20 20 20 re must.
1c70: 20 20 2a 2a 20 62 65 20 69 6d 70 6f 73 74 65 72 ** be imposter
1c80: 20 74 61 62 6c 65 73 2e 20 20 53 6f 20 6a 75 73 tables. So jus
1c90: 74 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 t return true.
1ca0: 54 68 65 20 61 73 73 65 72 74 20 69 73 20 6e 6f The assert is no
1cb0: 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 t. ** u
1cc0: 73 65 66 75 6c 20 69 6e 20 74 68 61 74 20 63 61 seful in that ca
1cd0: 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 se. */.
1ce0: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
1cf0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 54 61 }. iTa
1d00: 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 b = pIdx->pTable
1d10: 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a ->tnum;. }.
1d20: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1d30: 20 20 20 69 54 61 62 20 3d 20 69 52 6f 6f 74 3b iTab = iRoot;
1d40: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 . }.. /* Searc
1d50: 68 20 66 6f 72 20 74 68 65 20 72 65 71 75 69 72 h for the requir
1d60: 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68 65 72 20 ed lock. Either
1d70: 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
1d80: 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2c 20 root-page iTab,
1d90: 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 6c 6f a . ** write-lo
1da0: 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 ck on the schema
1db0: 20 74 61 62 6c 65 2c 20 6f 72 20 28 69 66 20 74 table, or (if t
1dc0: 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 he client is rea
1dd0: 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20 72 65 61 ding) a. ** rea
1de0: 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61 62 20 77 d-lock on iTab w
1df0: 69 6c 6c 20 73 75 66 66 69 63 65 2e 20 52 65 74 ill suffice. Ret
1e00: 75 72 6e 20 31 20 69 66 20 61 6e 79 20 6f 66 20 urn 1 if any of
1e10: 74 68 65 73 65 20 61 72 65 20 66 6f 75 6e 64 2e these are found.
1e20: 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 6f 63 6b */. for(pLock
1e30: 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 4c =pBtree->pBt->pL
1e40: 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 ock; pLock; pLoc
1e50: 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b k=pLock->pNext){
1e60: 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e . if( pLock->
1e70: 70 42 74 72 65 65 3d 3d 70 42 74 72 65 65 20 0a pBtree==pBtree .
1e80: 20 20 20 20 20 26 26 20 28 70 4c 6f 63 6b 2d 3e && (pLock->
1e90: 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 iTable==iTab ||
1ea0: 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 (pLock->eLock==W
1eb0: 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f RITE_LOCK && pLo
1ec0: 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31 29 29 0a ck->iTable==1)).
1ed0: 20 20 20 20 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 && pLock->e
1ee0: 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 Lock>=eLockType
1ef0: 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 . ){. re
1f00: 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
1f10: 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 }.. /* Failed t
1f20: 6f 20 66 69 6e 64 20 74 68 65 20 72 65 71 75 69 o find the requi
1f30: 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 red lock. */. r
1f40: 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 eturn 0;.}.#endi
1f50: 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 f /* SQLITE_DEBU
1f60: 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c G */..#ifdef SQL
1f70: 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a ITE_DEBUG./*.***
1f80: 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1f90: 6d 61 79 20 62 65 20 75 73 65 64 20 61 73 20 70 may be used as p
1fa0: 61 72 74 20 6f 66 20 61 73 73 65 72 74 28 29 20 art of assert()
1fb0: 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e statements only.
1fc0: 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 ****.**.** Retu
1fd0: 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 77 6f rn true if it wo
1fe0: 75 6c 64 20 62 65 20 69 6c 6c 65 67 61 6c 20 66 uld be illegal f
1ff0: 6f 72 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 or pBtree to wri
2000: 74 65 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 te into the.** t
2010: 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 72 6f able or index ro
2020: 6f 74 65 64 20 61 74 20 69 52 6f 6f 74 20 62 65 oted at iRoot be
2030: 63 61 75 73 65 20 6f 74 68 65 72 20 73 68 61 72 cause other shar
2040: 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 ed connections a
2050: 72 65 0a 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f re.** simultaneo
2060: 75 73 6c 79 20 72 65 61 64 69 6e 67 20 74 68 61 usly reading tha
2070: 74 20 73 61 6d 65 20 74 61 62 6c 65 20 6f 72 20 t same table or
2080: 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 index..**.** It
2090: 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 is illegal for p
20a0: 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 Btree to write i
20b0: 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72 f some other Btr
20c0: 65 65 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a ee object that.*
20d0: 2a 20 73 68 61 72 65 73 20 74 68 65 20 73 61 6d * shares the sam
20e0: 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
20f0: 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 t is currently r
2100: 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
2110: 67 0a 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20 74 g.** the iRoot t
2120: 61 62 6c 65 2e 20 20 45 78 63 65 70 74 2c 20 69 able. Except, i
2130: 66 20 74 68 65 20 6f 74 68 65 72 20 42 74 72 65 f the other Btre
2140: 65 20 6f 62 6a 65 63 74 20 68 61 73 20 74 68 65 e object has the
2150: 0a 2a 2a 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 .** read-uncommi
2160: 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 tted flag set, t
2170: 68 65 6e 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 hen it is OK for
2180: 20 74 68 65 20 6f 74 68 65 72 20 6f 62 6a 65 63 the other objec
2190: 74 20 74 6f 0a 2a 2a 20 68 61 76 65 20 61 20 72 t to.** have a r
21a0: 65 61 64 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a ead cursor..**.*
21b0: 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 * For example, b
21c0: 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f efore writing to
21d0: 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 any part of the
21e0: 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 0a table or index.
21f0: 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 ** rooted at pag
2200: 65 20 69 52 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f e iRoot, one sho
2210: 75 6c 64 20 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 uld call:.**.**
2220: 20 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52 assert( !hasR
2230: 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 42 74 eadConflicts(pBt
2240: 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b 0a 2a ree, iRoot) );.*
2250: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 /.static int has
2260: 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74 ReadConflicts(Bt
2270: 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e ree *pBtree, Pgn
2280: 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74 43 75 o iRoot){. BtCu
2290: 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 rsor *p;. for(p
22a0: 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 =pBtree->pBt->pC
22b0: 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
22c0: 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
22d0: 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f ->pgnoRoot==iRoo
22e0: 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42 t . && p->pB
22f0: 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20 20 20 tree!=pBtree.
2300: 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72 && 0==(p->pBtr
2310: 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 ee->db->flags &
2320: 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d SQLITE_ReadUncom
2330: 6d 69 74 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 mit). ){.
2340: 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
2350: 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
2360: 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a ;.}.#endif /*
2370: 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifdef SQLITE_D
2380: 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 EBUG */../*.** Q
2390: 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 20 42 uery to see if B
23a0: 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d 61 tree handle p ma
23b0: 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 y obtain a lock
23c0: 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a 2a of type eLock .*
23d0: 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 * (READ_LOCK or
23e0: 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 WRITE_LOCK) on t
23f0: 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
2400: 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52 65 ot-page iTab. Re
2410: 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f turn.** SQLITE_O
2420: 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 K if the lock ma
2430: 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28 62 y be obtained (b
2440: 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 74 y calling.** set
2450: 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
2460: 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c 49 Lock()), or SQLI
2470: 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 TE_LOCKED if not
2480: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
2490: 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
24a0: 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 TableLock(Btree
24b0: 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20 75 *p, Pgno iTab, u
24c0: 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 8 eLock){. BtSh
24d0: 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
24e0: 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 Bt;. BtLock *pI
24f0: 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ter;.. assert(
2500: 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
2510: 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 sMutex(p) );. a
2520: 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 ssert( eLock==RE
2530: 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b AD_LOCK || eLock
2540: 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a ==WRITE_LOCK );.
2550: 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 assert( p->db!
2560: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
2570: 21 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 !(p->db->flags&S
2580: 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
2590: 69 74 29 7c 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54 it)||eLock==WRIT
25a0: 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 3d 3d 31 20 E_LOCK||iTab==1
25b0: 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 72 65 );. . /* If re
25c0: 71 75 65 73 74 69 6e 67 20 61 20 77 72 69 74 65 questing a write
25d0: 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 -lock, then the
25e0: 42 74 72 65 65 20 6d 75 73 74 20 68 61 76 65 20 Btree must have
25f0: 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 0a 20 20 an open write.
2600: 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f ** transaction o
2610: 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 41 6e 64 n this file. And
2620: 2c 20 6f 62 76 69 6f 75 73 6c 79 2c 20 66 6f 72 , obviously, for
2630: 20 74 68 69 73 20 74 6f 20 62 65 20 73 6f 20 74 this to be so t
2640: 68 65 72 65 20 0a 20 20 2a 2a 20 6d 75 73 74 20 here . ** must
2650: 62 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 be an open write
2660: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 transaction on
2670: 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66 2e the file itself.
2680: 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
2690: 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
26a0: 20 7c 7c 20 28 70 3d 3d 70 42 74 2d 3e 70 57 72 || (p==pBt->pWr
26b0: 69 74 65 72 20 26 26 20 70 2d 3e 69 6e 54 72 61 iter && p->inTra
26c0: 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 ns==TRANS_WRITE)
26d0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c );. assert( eL
26e0: 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
26f0: 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 | pBt->inTransac
2700: 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
2710: 45 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 E );. . /* Thi
2720: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
2730: 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 o-op if the shar
2740: 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ed-cache is not
2750: 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 enabled */. if(
2760: 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b !p->sharable ){
2770: 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
2780: 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
2790: 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 If some other c
27a0: 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c onnection is hol
27b0: 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 ding an exclusiv
27c0: 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a e lock, the. **
27d0: 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 requested lock
27e0: 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 may not be obtai
27f0: 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ned.. */. if(
2800: 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 pBt->pWriter!=p
2810: 26 26 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 && (pBt->btsFlag
2820: 73 20 26 20 42 54 53 5f 45 58 43 4c 55 53 49 56 s & BTS_EXCLUSIV
2830: 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c E)!=0 ){. sql
2840: 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c ite3ConnectionBl
2850: 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 ocked(p->db, pBt
2860: 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a ->pWriter->db);.
2870: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
2880: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
2890: 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 ACHE;. }.. for
28a0: 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 (pIter=pBt->pLoc
28b0: 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d k; pIter; pIter=
28c0: 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 pIter->pNext){.
28d0: 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 /* The condit
28e0: 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 ion (pIter->eLoc
28f0: 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 k!=eLock) in the
2900: 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e following if(..
2910: 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 .) . ** state
2920: 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 ment is a simpli
2930: 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 fication of:.
2940: 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c **. ** (eL
2950: 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
2960: 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d || pIter->eLock=
2970: 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 =WRITE_LOCK).
2980: 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 **. ** since
2990: 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 we know that if
29a0: 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f eLock==WRITE_LO
29b0: 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 CK, then no othe
29c0: 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 r connection.
29d0: 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 ** may hold a W
29e0: 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 RITE_LOCK on any
29f0: 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 table in this f
2a00: 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 ile (since there
2a10: 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 can. ** only
2a20: 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 be a single wri
2a30: 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ter).. */.
2a40: 20 61 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e assert( pIter->
2a50: 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
2a60: 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b || pIter->eLock
2a70: 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a ==WRITE_LOCK );.
2a80: 20 20 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 assert( eLoc
2a90: 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
2aa0: 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 pIter->pBtree==p
2ab0: 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b || pIter->eLock
2ac0: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 ==READ_LOCK);.
2ad0: 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 if( pIter->pBt
2ae0: 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d ree!=p && pIter-
2af0: 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 >iTable==iTab &&
2b00: 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 pIter->eLock!=e
2b10: 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 Lock ){. sq
2b20: 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
2b30: 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 locked(p->db, pI
2b40: 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 ter->pBtree->db)
2b50: 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 ;. if( eLoc
2b60: 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b k==WRITE_LOCK ){
2b70: 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
2b80: 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 p==pBt->pWriter
2b90: 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d );. pBt-
2ba0: 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
2bb0: 5f 50 45 4e 44 49 4e 47 3b 0a 20 20 20 20 20 20 _PENDING;.
2bc0: 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 }. return S
2bd0: 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
2be0: 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 7d 0a REDCACHE;. }.
2bf0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
2c00: 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
2c10: 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
2c20: 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f _SHARED_CACHE */
2c30: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
2c40: 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
2c50: 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c HE./*.** Add a l
2c60: 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ock on the table
2c70: 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
2c80: 69 54 61 62 6c 65 20 74 6f 20 74 68 65 20 73 68 iTable to the sh
2c90: 61 72 65 64 2d 62 74 72 65 65 20 75 73 65 64 0a ared-btree used.
2ca0: 2a 2a 20 62 79 20 42 74 72 65 65 20 68 61 6e 64 ** by Btree hand
2cb0: 6c 65 20 70 2e 20 50 61 72 61 6d 65 74 65 72 20 le p. Parameter
2cc0: 65 4c 6f 63 6b 20 6d 75 73 74 20 62 65 20 65 69 eLock must be ei
2cd0: 74 68 65 72 20 52 45 41 44 5f 4c 4f 43 4b 20 6f ther READ_LOCK o
2ce0: 72 20 0a 2a 2a 20 57 52 49 54 45 5f 4c 4f 43 4b r .** WRITE_LOCK
2cf0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
2d00: 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
2d10: 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
2d20: 2a 2a 20 20 20 28 61 29 20 54 68 65 20 73 70 65 ** (a) The spe
2d30: 63 69 66 69 65 64 20 42 74 72 65 65 20 6f 62 6a cified Btree obj
2d40: 65 63 74 20 70 20 69 73 20 63 6f 6e 6e 65 63 74 ect p is connect
2d50: 65 64 20 74 6f 20 61 20 73 68 61 72 61 62 6c 65 ed to a sharable
2d60: 0a 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 .** databa
2d70: 73 65 20 28 6f 6e 65 20 77 69 74 68 20 74 68 65 se (one with the
2d80: 20 42 74 53 68 61 72 65 64 2e 73 68 61 72 61 62 BtShared.sharab
2d90: 6c 65 20 66 6c 61 67 20 73 65 74 29 2c 20 61 6e le flag set), an
2da0: 64 0a 2a 2a 0a 2a 2a 20 20 20 28 62 29 20 4e 6f d.**.** (b) No
2db0: 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 6a other Btree obj
2dc0: 65 63 74 73 20 68 6f 6c 64 20 61 20 6c 6f 63 6b ects hold a lock
2dd0: 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 0a that conflicts.
2de0: 2a 2a 20 20 20 20 20 20 20 77 69 74 68 20 74 68 ** with th
2df0: 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
2e00: 20 28 69 2e 65 2e 20 71 75 65 72 79 53 68 61 72 (i.e. queryShar
2e10: 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
2e20: 28 29 20 68 61 73 0a 2a 2a 20 20 20 20 20 20 20 () has.**
2e30: 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c already been cal
2e40: 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e 65 64 led and returned
2e50: 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a SQLITE_OK)..**.
2e60: 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
2e70: 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
2e80: 6c 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73 75 lock is added su
2e90: 63 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 ccessfully. SQLI
2ea0: 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73 20 TE_NOMEM .** is
2eb0: 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61 returned if a ma
2ec0: 6c 6c 6f 63 20 61 74 74 65 6d 70 74 20 66 61 69 lloc attempt fai
2ed0: 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ls..*/.static in
2ee0: 74 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 t setSharedCache
2ef0: 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 TableLock(Btree
2f00: 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c *p, Pgno iTable,
2f10: 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 u8 eLock){. Bt
2f20: 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
2f30: 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a >pBt;. BtLock *
2f40: 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c pLock = 0;. BtL
2f50: 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 ock *pIter;.. a
2f60: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
2f70: 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
2f80: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c );. assert( eL
2f90: 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
2fa0: 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c | eLock==WRITE_L
2fb0: 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
2fc0: 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 p->db!=0 );..
2fd0: 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 /* A connection
2fe0: 77 69 74 68 20 74 68 65 20 72 65 61 64 2d 75 6e with the read-un
2ff0: 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 committed flag s
3000: 65 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 72 et will never tr
3010: 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e y to. ** obtain
3020: 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 75 73 69 a read-lock usi
3030: 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ng this function
3040: 2e 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 64 2d . The only read-
3050: 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 20 20 lock obtained.
3060: 2a 2a 20 62 79 20 61 20 63 6f 6e 6e 65 63 74 69 ** by a connecti
3070: 6f 6e 20 69 6e 20 72 65 61 64 2d 75 6e 63 6f 6d on in read-uncom
3080: 6d 69 74 74 65 64 20 6d 6f 64 65 20 69 73 20 6f mitted mode is o
3090: 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 n the sqlite_mas
30a0: 74 65 72 20 0a 20 20 2a 2a 20 74 61 62 6c 65 2c ter . ** table,
30b0: 20 61 6e 64 20 74 68 61 74 20 6c 6f 63 6b 20 69 and that lock i
30c0: 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 42 74 s obtained in Bt
30d0: 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 29 2e reeBeginTrans().
30e0: 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 */. assert( 0
30f0: 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 ==(p->db->flags&
3100: 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d SQLITE_ReadUncom
3110: 6d 69 74 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 mit) || eLock==W
3120: 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 RITE_LOCK );..
3130: 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
3140: 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 should only be
3150: 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 called on a shar
3160: 61 62 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 able b-tree afte
3170: 72 20 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 r it . ** has b
3180: 65 65 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 een determined t
3190: 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 hat no other b-t
31a0: 72 65 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 ree holds a conf
31b0: 6c 69 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a licting lock. *
31c0: 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 /. assert( p->s
31d0: 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 harable );. ass
31e0: 65 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d ert( SQLITE_OK==
31f0: 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
3200: 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 TableLock(p, iTa
3210: 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a ble, eLock) );..
3220: 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 /* First searc
3230: 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 h the list for a
3240: 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 n existing lock
3250: 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a on this table. *
3260: 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 /. for(pIter=pB
3270: 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b t->pLock; pIter;
3280: 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e pIter=pIter->pN
3290: 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 ext){. if( pI
32a0: 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ter->iTable==iTa
32b0: 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 ble && pIter->pB
32c0: 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
32d0: 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a pLock = pIter;.
32e0: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
32f0: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
3300: 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 the above search
3310: 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 did not find a
3320: 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 BtLock struct as
3330: 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 sociating Btree
3340: 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c p. ** with tabl
3350: 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 e iTable, alloca
3360: 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 te one and link
3370: 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 it into the list
3380: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c .. */. if( !pL
3390: 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b ock ){. pLock
33a0: 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c = (BtLock *)sql
33b0: 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
33c0: 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a izeof(BtLock));.
33d0: 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 if( !pLock )
33e0: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
33f0: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 QLITE_NOMEM_BKPT
3400: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 ;. }. pLoc
3410: 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 k->iTable = iTab
3420: 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 le;. pLock->p
3430: 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 Btree = p;. p
3440: 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 Lock->pNext = pB
3450: 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 t->pLock;. pB
3460: 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b t->pLock = pLock
3470: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 ;. }.. /* Set
3480: 74 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b the BtLock.eLock
3490: 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 variable to the
34a0: 20 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 maximum of the
34b0: 63 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a current lock. *
34c0: 2a 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 * and the reques
34d0: 74 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d ted lock. This m
34e0: 65 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d eans if a write-
34f0: 6c 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 lock was already
3500: 20 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 held. ** and a
3510: 20 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 read-lock reque
3520: 73 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 sted, we don't i
3530: 6e 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 ncorrectly downg
3540: 72 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 rade the lock..
3550: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 */. assert( WR
3560: 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f ITE_LOCK>READ_LO
3570: 43 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 CK );. if( eLoc
3580: 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 k>pLock->eLock )
3590: 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f {. pLock->eLo
35a0: 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a ck = eLock;. }.
35b0: 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
35c0: 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a _OK;.}.#endif /*
35d0: 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 !SQLITE_OMIT_SH
35e0: 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 ARED_CACHE */..#
35f0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
3600: 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
3610: 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c /*.** Release al
3620: 6c 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b l the table lock
3630: 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 s (locks obtaine
3640: 64 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a d via calls to.*
3650: 2a 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 * the setSharedC
3660: 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 acheTableLock()
3670: 70 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 procedure) held
3680: 62 79 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 by Btree object
3690: 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 p..**.** This fu
36a0: 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 nction assumes t
36b0: 68 61 74 20 42 74 72 65 65 20 70 20 68 61 73 20 hat Btree p has
36c0: 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20 an open read or
36d0: 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61 write .** transa
36e0: 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65 ction. If it doe
36f0: 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 s not, then the
3700: 42 54 53 5f 50 45 4e 44 49 4e 47 20 66 6c 61 67 BTS_PENDING flag
3710: 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 .** may be incor
3720: 72 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a rectly cleared..
3730: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
3740: 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
3750: 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 heTableLocks(Btr
3760: 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
3770: 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
3780: 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 ;. BtLock **ppI
3790: 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 ter = &pBt->pLoc
37a0: 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 k;.. assert( sq
37b0: 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
37c0: 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
37d0: 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
37e0: 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 || 0==*ppIter )
37f0: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
3800: 6e 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 nTrans>0 );.. w
3810: 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b hile( *ppIter ){
3820: 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f . BtLock *pLo
3830: 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 ck = *ppIter;.
3840: 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e assert( (pBt->
3850: 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 45 btsFlags & BTS_E
3860: 58 43 4c 55 53 49 56 45 29 3d 3d 30 20 7c 7c 20 XCLUSIVE)==0 ||
3870: 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c pBt->pWriter==pL
3880: 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 ock->pBtree );.
3890: 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
38a0: 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e ->pBtree->inTran
38b0: 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 s>=pLock->eLock
38c0: 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b );. if( pLock
38d0: 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
38e0: 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70 *ppIter = p
38f0: 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
3900: 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
3910: 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70 ->iTable!=1 || p
3920: 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 Lock==&p->lock )
3930: 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 ;. if( pLoc
3940: 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a k->iTable!=1 ){.
3950: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
3960: 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 free(pLock);.
3970: 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
3980: 20 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 ppIter = &
3990: 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 pLock->pNext;.
39a0: 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 }. }.. asser
39b0: 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 t( (pBt->btsFlag
39c0: 73 20 26 20 42 54 53 5f 50 45 4e 44 49 4e 47 29 s & BTS_PENDING)
39d0: 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 ==0 || pBt->pWri
39e0: 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74 ter );. if( pBt
39f0: 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a ->pWriter==p ){.
3a00: 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 pBt->pWriter
3a10: 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 62 = 0;. pBt->b
3a20: 74 73 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 53 tsFlags &= ~(BTS
3a30: 5f 45 58 43 4c 55 53 49 56 45 7c 42 54 53 5f 50 _EXCLUSIVE|BTS_P
3a40: 45 4e 44 49 4e 47 29 3b 0a 20 20 7d 65 6c 73 65 ENDING);. }else
3a50: 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 if( pBt->nTrans
3a60: 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 action==2 ){.
3a70: 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f /* This functio
3a80: 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
3a90: 20 42 74 72 65 65 20 70 20 69 73 20 63 6f 6e 63 Btree p is conc
3aa0: 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20 20 luding its .
3ab0: 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
3ac0: 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e 74 If there current
3ad0: 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69 74 ly exists a writ
3ae0: 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f 74 er, and p is not
3af0: 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 69 . ** that wri
3b00: 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 ter, then the nu
3b10: 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 mber of locks he
3b20: 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e ld by connection
3b30: 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 s other. ** t
3b40: 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20 6d han the writer m
3b50: 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f 20 ust be about to
3b60: 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 6e drop to zero. In
3b70: 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20 2a this case. *
3b80: 2a 20 73 65 74 20 74 68 65 20 42 54 53 5f 50 45 * set the BTS_PE
3b90: 4e 44 49 4e 47 20 66 6c 61 67 20 74 6f 20 30 2e NDING flag to 0.
3ba0: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
3bb0: 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 f there is not c
3bc0: 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 urrently a write
3bd0: 72 2c 20 74 68 65 6e 20 42 54 53 5f 50 45 4e 44 r, then BTS_PEND
3be0: 49 4e 47 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 ING must. **
3bf0: 62 65 20 7a 65 72 6f 20 61 6c 72 65 61 64 79 2e be zero already.
3c00: 20 53 6f 20 74 68 69 73 20 6e 65 78 74 20 6c 69 So this next li
3c10: 6e 65 20 69 73 20 68 61 72 6d 6c 65 73 73 20 69 ne is harmless i
3c20: 6e 20 74 68 61 74 20 63 61 73 65 2e 0a 20 20 20 n that case..
3c30: 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 */. pBt->bts
3c40: 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 50 45 Flags &= ~BTS_PE
3c50: 4e 44 49 4e 47 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a NDING;. }.}../*
3c60: 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
3c70: 6e 20 63 68 61 6e 67 65 73 20 61 6c 6c 20 77 72 n changes all wr
3c80: 69 74 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 ite-locks held b
3c90: 79 20 42 74 72 65 65 20 70 20 69 6e 74 6f 20 72 y Btree p into r
3ca0: 65 61 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 ead-locks..*/.st
3cb0: 61 74 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 atic void downgr
3cc0: 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 adeAllSharedCach
3cd0: 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 eTableLocks(Btre
3ce0: 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
3cf0: 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
3d00: 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 . if( pBt->pWri
3d10: 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 ter==p ){. Bt
3d20: 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 Lock *pLock;.
3d30: 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 pBt->pWriter =
3d40: 30 3b 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 0;. pBt->btsF
3d50: 6c 61 67 73 20 26 3d 20 7e 28 42 54 53 5f 45 58 lags &= ~(BTS_EX
3d60: 43 4c 55 53 49 56 45 7c 42 54 53 5f 50 45 4e 44 CLUSIVE|BTS_PEND
3d70: 49 4e 47 29 3b 0a 20 20 20 20 66 6f 72 28 70 4c ING);. for(pL
3d80: 6f 63 6b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 ock=pBt->pLock;
3d90: 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f pLock; pLock=pLo
3da0: 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ck->pNext){.
3db0: 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d assert( pLock-
3dc0: 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 >eLock==READ_LOC
3dd0: 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 K || pLock->pBtr
3de0: 65 65 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 ee==p );. p
3df0: 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 Lock->eLock = RE
3e00: 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 AD_LOCK;. }.
3e10: 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}..#endif /*
3e20: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
3e30: 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 ED_CACHE */..sta
3e40: 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
3e50: 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
3e60: 61 67 65 29 3b 20 20 20 20 20 20 20 20 20 2f 2a age); /*
3e70: 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
3e80: 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ce */.static voi
3e90: 64 20 72 65 6c 65 61 73 65 50 61 67 65 4f 6e 65 d releasePageOne
3ea0: 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
3eb0: 3b 20 20 20 20 20 20 2f 2a 20 46 6f 72 77 61 72 ; /* Forwar
3ec0: 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 d reference */.s
3ed0: 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
3ee0: 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 4d 65 sePageNotNull(Me
3ef0: 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 mPage *pPage);
3f00: 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 /* Forward refer
3f10: 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a ence */../*.****
3f20: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
3f30: 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 s used inside of
3f40: 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a assert() only *
3f50: 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79 ***.**.** Verify
3f60: 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
3f70: 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 holds the mutex
3f80: 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65 64 on its BtShared
3f90: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
3fa0: 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 E_DEBUG.static i
3fb0: 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 nt cursorHoldsMu
3fc0: 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 tex(BtCursor *p)
3fd0: 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
3fe0: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
3ff0: 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a >pBt->mutex);.}.
4000: 0a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 ./* Verify that
4010: 74 68 65 20 63 75 72 73 6f 72 20 61 6e 64 20 74 the cursor and t
4020: 68 65 20 42 74 53 68 61 72 65 64 20 61 67 72 65 he BtShared agre
4030: 65 20 61 62 6f 75 74 20 77 68 61 74 20 69 73 20 e about what is
4040: 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 64 the current.** d
4050: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 74 69 6f atabase connetio
4060: 6e 2e 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 n. This is impor
4070: 74 61 6e 74 20 69 6e 20 73 68 61 72 65 64 2d 63 tant in shared-c
4080: 61 63 68 65 20 6d 6f 64 65 2e 20 49 66 20 74 68 ache mode. If th
4090: 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 63 e database .** c
40a0: 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 onnection pointe
40b0: 72 73 20 67 65 74 20 6f 75 74 2d 6f 66 2d 73 79 rs get out-of-sy
40c0: 6e 63 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 nc, it is possib
40d0: 6c 65 20 66 6f 72 20 72 6f 75 74 69 6e 65 73 20 le for routines
40e0: 6c 69 6b 65 0a 2a 2a 20 62 74 72 65 65 49 6e 69 like.** btreeIni
40f0: 74 50 61 67 65 28 29 20 74 6f 20 72 65 66 65 72 tPage() to refer
4100: 65 6e 63 65 20 61 6e 20 73 74 61 6c 65 20 63 6f ence an stale co
4110: 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 nnection pointer
4120: 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 73 that references
4130: 20 61 0a 2a 2a 20 61 20 63 6f 6e 6e 65 63 74 69 a.** a connecti
4140: 6f 6e 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 on that has alre
4150: 61 64 79 20 63 6c 6f 73 65 64 2e 20 20 54 68 69 ady closed. Thi
4160: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
4170: 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 d inside assert(
4180: 29 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 ).** statements
4190: 6f 6e 6c 79 20 61 6e 64 20 66 6f 72 20 74 68 65 only and for the
41a0: 20 70 75 72 70 6f 73 65 20 6f 66 20 64 6f 75 62 purpose of doub
41b0: 6c 65 2d 63 68 65 63 6b 69 6e 67 20 74 68 61 74 le-checking that
41c0: 20 74 68 65 20 62 74 72 65 65 20 63 6f 64 65 0a the btree code.
41d0: 2a 2a 20 64 6f 65 73 20 6b 65 65 70 20 74 68 65 ** does keep the
41e0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
41f0: 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 75 70 tion pointers up
4200: 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 -to-date..*/.sta
4210: 74 69 63 20 69 6e 74 20 63 75 72 73 6f 72 4f 77 tic int cursorOw
4220: 6e 73 42 74 53 68 61 72 65 64 28 42 74 43 75 72 nsBtShared(BtCur
4230: 73 6f 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 sor *p){. asser
4240: 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
4250: 74 65 78 28 70 29 20 29 3b 0a 20 20 72 65 74 75 tex(p) );. retu
4260: 72 6e 20 28 70 2d 3e 70 42 74 72 65 65 2d 3e 64 rn (p->pBtree->d
4270: 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 29 3b 0a b==p->pBt->db);.
4280: 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
4290: 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f Invalidate the o
42a0: 76 65 72 66 6c 6f 77 20 63 61 63 68 65 20 6f 66 verflow cache of
42b0: 20 74 68 65 20 63 75 72 73 6f 72 20 70 61 73 73 the cursor pass
42c0: 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
42d0: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 6f 6e 20 argument..** on
42e0: 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 the shared btree
42f0: 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a structure pBt..
4300: 2a 2f 0a 23 64 65 66 69 6e 65 20 69 6e 76 61 6c */.#define inval
4310: 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
4320: 68 65 28 70 43 75 72 29 20 28 70 43 75 72 2d 3e he(pCur) (pCur->
4330: 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 42 54 43 curFlags &= ~BTC
4340: 46 5f 56 61 6c 69 64 4f 76 66 6c 29 0a 0a 2f 2a F_ValidOvfl)../*
4350: 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 .** Invalidate t
4360: 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
4370: 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 -list cache for
4380: 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e all cursors open
4390: 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 ed.** on the sha
43a0: 72 65 64 20 62 74 72 65 65 20 73 74 72 75 63 74 red btree struct
43b0: 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 ure pBt..*/.stat
43c0: 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 ic void invalida
43d0: 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 teAllOverflowCac
43e0: 68 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 he(BtShared *pBt
43f0: 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
4400: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
4410: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
4420: 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
4430: 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 for(p=pBt->pCurs
4440: 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
4450: 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 t){. invalida
4460: 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
4470: 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 p);. }.}..#ifnd
4480: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
4490: 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 54 68 NCRBLOB./*.** Th
44a0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
44b0: 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 alled before mod
44c0: 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 ifying the conte
44d0: 6e 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a nts of a table.*
44e0: 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 * to invalidate
44f0: 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 any incrblob cur
4500: 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6f 70 sors that are op
4510: 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 en on the.** row
4520: 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 or one of the r
4530: 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 ows being modifi
4540: 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 ed..**.** If arg
4550: 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 ument isClearTab
4560: 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e le is true, then
4570: 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
4580: 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 ents of the.** t
4590: 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f able is about to
45a0: 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 be deleted. In
45b0: 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 this case invali
45c0: 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f date all incrblo
45d0: 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 b.** cursors ope
45e0: 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 n on any row wit
45f0: 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 hin the table wi
4600: 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e th root-page pgn
4610: 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 oRoot..**.** Oth
4620: 65 72 77 69 73 65 2c 20 69 66 20 61 72 67 75 6d erwise, if argum
4630: 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 ent isClearTable
4640: 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 is false, then
4650: 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 the row with.**
4660: 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20 62 65 rowid iRow is be
4670: 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f 72 20 ing replaced or
4680: 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 deleted. In this
4690: 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 case invalidate
46a0: 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 .** only those i
46b0: 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 ncrblob cursors
46c0: 6f 70 65 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 open on that spe
46d0: 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 cific row..*/.st
46e0: 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 atic void invali
46f0: 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
4700: 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a 70 42 ors(. Btree *pB
4710: 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 2f tree, /
4720: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 * The database f
4730: 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a ile to check */.
4740: 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 2c Pgno pgnoRoot,
4750: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
4760: 20 74 61 62 6c 65 20 74 68 61 74 20 6d 69 67 68 table that migh
4770: 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f t be changing */
4780: 0a 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20 20 . i64 iRow,
4790: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
47a0: 65 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 e rowid that mig
47b0: 68 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a ht be changing *
47c0: 2f 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72 54 /. int isClearT
47d0: 61 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 54 able /* T
47e0: 72 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 rue if all rows
47f0: 61 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74 65 are being delete
4800: 64 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 73 d */.){. BtCurs
4810: 6f 72 20 2a 70 3b 0a 20 20 69 66 28 20 70 42 74 or *p;. if( pBt
4820: 72 65 65 2d 3e 68 61 73 49 6e 63 72 62 6c 6f 62 ree->hasIncrblob
4830: 43 75 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Cur==0 ) return;
4840: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
4850: 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
4860: 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20 70 x(pBtree) );. p
4870: 42 74 72 65 65 2d 3e 68 61 73 49 6e 63 72 62 6c Btree->hasIncrbl
4880: 6f 62 43 75 72 20 3d 20 30 3b 0a 20 20 66 6f 72 obCur = 0;. for
4890: 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e (p=pBtree->pBt->
48a0: 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
48b0: 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
48c0: 20 28 70 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 (p->curFlags &
48d0: 42 54 43 46 5f 49 6e 63 72 62 6c 6f 62 29 21 3d BTCF_Incrblob)!=
48e0: 30 20 29 7b 0a 20 20 20 20 20 20 70 42 74 72 65 0 ){. pBtre
48f0: 65 2d 3e 68 61 73 49 6e 63 72 62 6c 6f 62 43 75 e->hasIncrblobCu
4900: 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 r = 1;. if(
4910: 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70 67 p->pgnoRoot==pg
4920: 6e 6f 52 6f 6f 74 20 26 26 20 28 69 73 43 6c 65 noRoot && (isCle
4930: 61 72 54 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e arTable || p->in
4940: 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 20 29 fo.nKey==iRow) )
4950: 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 {. p->eSt
4960: 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
4970: 41 4c 49 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ALID;. }.
4980: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 }. }.}..#else
4990: 0a 20 20 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 . /* Stub funct
49a0: 69 6f 6e 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f ion when INCRBLO
49b0: 42 20 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a B is omitted */.
49c0: 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 #define invali
49d0: 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
49e0: 6f 72 73 28 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e ors(w,x,y,z).#en
49f0: 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
4a00: 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a IT_INCRBLOB */..
4a10: 2f 2a 0a 2a 2a 20 53 65 74 20 62 69 74 20 70 67 /*.** Set bit pg
4a20: 6e 6f 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 no of the BtShar
4a30: 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 ed.pHasContent b
4a40: 69 74 76 65 63 2e 20 54 68 69 73 20 69 73 20 63 itvec. This is c
4a50: 61 6c 6c 65 64 20 0a 2a 2a 20 77 68 65 6e 20 61 alled .** when a
4a60: 20 70 61 67 65 20 74 68 61 74 20 70 72 65 76 69 page that previ
4a70: 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 ously contained
4a80: 64 61 74 61 20 62 65 63 6f 6d 65 73 20 61 20 66 data becomes a f
4a90: 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 0a 2a ree-list leaf .*
4aa0: 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 * page..**.** Th
4ab0: 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 e BtShared.pHasC
4ac0: 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 20 65 78 ontent bitvec ex
4ad0: 69 73 74 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f ists to work aro
4ae0: 75 6e 64 20 61 6e 20 6f 62 73 63 75 72 65 0a 2a und an obscure.*
4af0: 2a 20 62 75 67 20 63 61 75 73 65 64 20 62 79 20 * bug caused by
4b00: 74 68 65 20 69 6e 74 65 72 61 63 74 69 6f 6e 20 the interaction
4b10: 6f 66 20 74 77 6f 20 75 73 65 66 75 6c 20 49 4f of two useful IO
4b20: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73 optimizations s
4b30: 75 72 72 6f 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 urrounding.** fr
4b40: 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 ee-list leaf pag
4b50: 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 es:.**.** 1) W
4b60: 68 65 6e 20 61 6c 6c 20 64 61 74 61 20 69 73 20 hen all data is
4b70: 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 20 70 deleted from a p
4b80: 61 67 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 age and the page
4b90: 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 becomes.**
4ba0: 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 a free-list lea
4bb0: 66 20 70 61 67 65 2c 20 74 68 65 20 70 61 67 65 f page, the page
4bc0: 20 69 73 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 is not written
4bd0: 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 0a to the database.
4be0: 2a 2a 20 20 20 20 20 20 28 61 73 20 66 72 65 65 ** (as free
4bf0: 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 -list leaf pages
4c00: 20 63 6f 6e 74 61 69 6e 20 6e 6f 20 6d 65 61 6e contain no mean
4c10: 69 6e 67 66 75 6c 20 64 61 74 61 29 2e 20 53 6f ingful data). So
4c20: 6d 65 74 69 6d 65 73 0a 2a 2a 20 20 20 20 20 20 metimes.**
4c30: 73 75 63 68 20 61 20 70 61 67 65 20 69 73 20 6e such a page is n
4c40: 6f 74 20 65 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c ot even journall
4c50: 65 64 20 28 61 73 20 69 74 20 77 69 6c 6c 20 6e ed (as it will n
4c60: 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 0a ot be modified,.
4c70: 2a 2a 20 20 20 20 20 20 77 68 79 20 62 6f 74 68 ** why both
4c80: 65 72 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 er journalling i
4c90: 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 t?)..**.** 2)
4ca0: 57 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 When a free-list
4cb0: 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65 leaf page is re
4cc0: 75 73 65 64 2c 20 69 74 73 20 63 6f 6e 74 65 6e used, its conten
4cd0: 74 20 69 73 20 6e 6f 74 20 72 65 61 64 0a 2a 2a t is not read.**
4ce0: 20 20 20 20 20 20 66 72 6f 6d 20 74 68 65 20 64 from the d
4cf0: 61 74 61 62 61 73 65 20 6f 72 20 77 72 69 74 74 atabase or writt
4d00: 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 en to the journa
4d10: 6c 20 66 69 6c 65 20 28 77 68 79 20 73 68 6f 75 l file (why shou
4d20: 6c 64 20 69 74 0a 2a 2a 20 20 20 20 20 20 62 65 ld it.** be
4d30: 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 , if it is not a
4d40: 74 20 61 6c 6c 20 6d 65 61 6e 69 6e 67 66 75 6c t all meaningful
4d50: 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 ?)..**.** By the
4d60: 6d 73 65 6c 76 65 73 2c 20 74 68 65 73 65 20 6f mselves, these o
4d70: 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 ptimizations wor
4d80: 6b 20 66 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 k fine and provi
4d90: 64 65 20 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 de a handy.** pe
4da0: 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 rformance boost
4db0: 74 6f 20 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f to bulk delete o
4dc0: 72 20 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69 r insert operati
4dd0: 6f 6e 73 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 ons. However, if
4de0: 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 6d 6f .** a page is mo
4df0: 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d ved to the free-
4e00: 6c 69 73 74 20 61 6e 64 20 74 68 65 6e 20 72 65 list and then re
4e10: 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 used within the
4e20: 73 61 6d 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 same.** transact
4e30: 69 6f 6e 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63 ion, a problem c
4e40: 6f 6d 65 73 20 75 70 2e 20 49 66 20 74 68 65 20 omes up. If the
4e50: 70 61 67 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72 page is not jour
4e60: 6e 61 6c 6c 65 64 20 77 68 65 6e 0a 2a 2a 20 69 nalled when.** i
4e70: 74 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 t is moved to th
4e80: 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 e free-list and
4e90: 69 74 20 69 73 20 61 6c 73 6f 20 6e 6f 74 20 6a it is also not j
4ea0: 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e 20 69 ournalled when i
4eb0: 74 0a 2a 2a 20 69 73 20 65 78 74 72 61 63 74 65 t.** is extracte
4ec0: 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d d from the free-
4ed0: 6c 69 73 74 20 61 6e 64 20 72 65 75 73 65 64 2c list and reused,
4ee0: 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67 69 6e then the origin
4ef0: 61 6c 20 64 61 74 61 0a 2a 2a 20 6d 61 79 20 62 al data.** may b
4f00: 65 20 6c 6f 73 74 2e 20 49 6e 20 74 68 65 20 65 e lost. In the e
4f10: 76 65 6e 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 vent of a rollba
4f20: 63 6b 2c 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 ck, it may not b
4f30: 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f e possible.** to
4f40: 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 restore the dat
4f50: 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 abase to its ori
4f60: 67 69 6e 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 ginal configurat
4f70: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ion..**.** The s
4f80: 6f 6c 75 74 69 6f 6e 20 69 73 20 74 68 65 20 42 olution is the B
4f90: 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
4fa0: 65 6e 74 20 62 69 74 76 65 63 2e 20 57 68 65 6e ent bitvec. When
4fb0: 65 76 65 72 20 61 20 70 61 67 65 20 69 73 20 0a ever a page is .
4fc0: 2a 2a 20 6d 6f 76 65 64 20 74 6f 20 62 65 63 6f ** moved to beco
4fd0: 6d 65 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c me a free-list l
4fe0: 65 61 66 20 70 61 67 65 2c 20 74 68 65 20 63 6f eaf page, the co
4ff0: 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 rresponding bit
5000: 69 73 0a 2a 2a 20 73 65 74 20 69 6e 20 74 68 65 is.** set in the
5010: 20 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 bitvec. Wheneve
5020: 72 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 r a leaf page is
5030: 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 extracted from
5040: 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 0a 2a the free-list,.*
5050: 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 32 * optimization 2
5060: 20 61 62 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 above is omitte
5070: 64 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 d if the corresp
5080: 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 61 6c onding bit is al
5090: 72 65 61 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 ready.** set in
50a0: 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e BtShared.pHasCon
50b0: 74 65 6e 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e tent. The conten
50c0: 74 73 20 6f 66 20 74 68 65 20 62 69 74 76 65 63 ts of the bitvec
50d0: 20 61 72 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 are cleared.**
50e0: 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 at the end of ev
50f0: 65 72 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ery transaction.
5100: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
5110: 74 72 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e treeSetHasConten
5120: 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c t(BtShared *pBt,
5130: 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 Pgno pgno){. i
5140: 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
5150: 4b 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 K;. if( !pBt->p
5160: 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 HasContent ){.
5170: 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d assert( pgno<=
5180: 70 42 74 2d 3e 6e 50 61 67 65 20 29 3b 0a 20 20 pBt->nPage );.
5190: 20 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 pBt->pHasConte
51a0: 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 nt = sqlite3Bitv
51b0: 65 63 43 72 65 61 74 65 28 70 42 74 2d 3e 6e 50 ecCreate(pBt->nP
51c0: 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 age);. if( !p
51d0: 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
51e0: 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
51f0: 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b LITE_NOMEM_BKPT;
5200: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
5210: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
5220: 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 & pgno<=sqlite3B
5230: 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 itvecSize(pBt->p
5240: 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 HasContent) ){.
5250: 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
5260: 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 itvecSet(pBt->pH
5270: 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 asContent, pgno)
5280: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
5290: 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 c;.}../*.** Quer
52a0: 79 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 y the BtShared.p
52b0: 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f HasContent vecto
52c0: 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 r..**.** This fu
52d0: 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
52e0: 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 when a free-lis
52f0: 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 t leaf page is r
5300: 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a emoved from the.
5310: 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 ** free-list for
5320: 20 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 reuse. It retur
5330: 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 ns false if it i
5340: 73 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 s safe to retrie
5350: 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 ve the.** page f
5360: 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 rom the pager la
5370: 79 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f yer with the 'no
5380: 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 -content' flag s
5390: 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 et. True otherwi
53a0: 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e se..*/.static in
53b0: 74 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e t btreeGetHasCon
53c0: 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 tent(BtShared *p
53d0: 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
53e0: 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 Bitvec *p = pB
53f0: 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a t->pHasContent;.
5400: 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 return (p && (
5410: 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 pgno>sqlite3Bitv
5420: 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c ecSize(p) || sql
5430: 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
5440: 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a , pgno)));.}../*
5450: 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 .** Clear (destr
5460: 6f 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 oy) the BtShared
5470: 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
5480: 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 vec. This should
5490: 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 be.** invoked a
54a0: 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e t the conclusion
54b0: 20 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 of each write-t
54c0: 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
54d0: 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
54e0: 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 ClearHasContent(
54f0: 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
5500: 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
5510: 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 estroy(pBt->pHas
5520: 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d Content);. pBt-
5530: 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 >pHasContent = 0
5540: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
5550: 73 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 70 se all of the ap
5560: 50 61 67 65 5b 5d 20 70 61 67 65 73 20 66 6f 72 Page[] pages for
5570: 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 a cursor..*/.st
5580: 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 52 atic void btreeR
5590: 65 6c 65 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 eleaseAllCursorP
55a0: 61 67 65 73 28 42 74 43 75 72 73 6f 72 20 2a 70 ages(BtCursor *p
55b0: 43 75 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 Cur){. int i;.
55c0: 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
55d0: 3e 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 >=0 ){. for(i
55e0: 3d 30 3b 20 69 3c 70 43 75 72 2d 3e 69 50 61 67 =0; i<pCur->iPag
55f0: 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 e; i++){. r
5600: 65 6c 65 61 73 65 50 61 67 65 4e 6f 74 4e 75 6c eleasePageNotNul
5610: 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
5620: 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 ]);. }. re
5630: 6c 65 61 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c leasePageNotNull
5640: 28 70 43 75 72 2d 3e 70 50 61 67 65 29 3b 0a 20 (pCur->pPage);.
5650: 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d pCur->iPage =
5660: 20 2d 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a -1;. }.}../*.*
5670: 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 61 73 * The cursor pas
5680: 73 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 sed as the only
5690: 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 70 6f argument must po
56a0: 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 int to a valid e
56b0: 6e 74 72 79 0a 2a 2a 20 77 68 65 6e 20 74 68 69 ntry.** when thi
56c0: 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
56d0: 6c 6c 65 64 20 28 69 2e 65 2e 20 68 61 76 65 20 lled (i.e. have
56e0: 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
56f0: 41 4c 49 44 29 2e 20 54 68 69 73 0a 2a 2a 20 66 ALID). This.** f
5700: 75 6e 63 74 69 6f 6e 20 73 61 76 65 73 20 74 68 unction saves th
5710: 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 e current cursor
5720: 20 6b 65 79 20 69 6e 20 76 61 72 69 61 62 6c 65 key in variable
5730: 73 20 70 43 75 72 2d 3e 6e 4b 65 79 20 61 6e 64 s pCur->nKey and
5740: 0a 2a 2a 20 70 43 75 72 2d 3e 70 4b 65 79 2e 20 .** pCur->pKey.
5750: 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
5760: 75 72 6e 65 64 20 69 66 20 73 75 63 63 65 73 73 urned if success
5770: 66 75 6c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 ful or an SQLite
5780: 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 error .** code
5790: 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a otherwise..**.**
57a0: 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 If the cursor i
57b0: 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 s open on an int
57c0: 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 key table, then
57d0: 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 0a the integer key.
57e0: 2a 2a 20 28 74 68 65 20 72 6f 77 69 64 29 20 69 ** (the rowid) i
57f0: 73 20 73 74 6f 72 65 64 20 69 6e 20 70 43 75 72 s stored in pCur
5800: 2d 3e 6e 4b 65 79 20 61 6e 64 20 70 43 75 72 2d ->nKey and pCur-
5810: 3e 70 4b 65 79 20 69 73 20 6c 65 66 74 20 73 65 >pKey is left se
5820: 74 20 74 6f 0a 2a 2a 20 4e 55 4c 4c 2e 20 49 66 t to.** NULL. If
5830: 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f the cursor is o
5840: 70 65 6e 20 6f 6e 20 61 20 6e 6f 6e 2d 69 6e 74 pen on a non-int
5850: 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 key table, then
5860: 70 43 75 72 2d 3e 70 4b 65 79 20 69 73 20 0a 2a pCur->pKey is .*
5870: 2a 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 * set to point t
5880: 6f 20 61 20 6d 61 6c 6c 6f 63 65 64 20 62 75 66 o a malloced buf
5890: 66 65 72 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 fer pCur->nKey b
58a0: 79 74 65 73 20 69 6e 20 73 69 7a 65 20 63 6f 6e ytes in size con
58b0: 74 61 69 6e 69 6e 67 20 0a 2a 2a 20 74 68 65 20 taining .** the
58c0: 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 key..*/.static i
58d0: 6e 74 20 73 61 76 65 43 75 72 73 6f 72 4b 65 79 nt saveCursorKey
58e0: 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
58f0: 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
5900: 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 ITE_OK;. assert
5910: 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d ( CURSOR_VALID==
5920: 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a pCur->eState );.
5930: 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 assert( 0==pCu
5940: 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 r->pKey );. ass
5950: 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
5960: 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a Mutex(pCur) );..
5970: 20 20 69 66 28 20 70 43 75 72 2d 3e 63 75 72 49 if( pCur->curI
5980: 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 2f 2a 20 ntKey ){. /*
5990: 4f 6e 6c 79 20 74 68 65 20 72 6f 77 69 64 20 69 Only the rowid i
59a0: 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 s required for a
59b0: 20 74 61 62 6c 65 20 62 74 72 65 65 20 2a 2f 0a table btree */.
59c0: 20 20 20 20 70 43 75 72 2d 3e 6e 4b 65 79 20 3d pCur->nKey =
59d0: 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 sqlite3BtreeInt
59e0: 65 67 65 72 4b 65 79 28 70 43 75 72 29 3b 0a 20 egerKey(pCur);.
59f0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 }else{. /* F
5a00: 6f 72 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 or an index btre
5a10: 65 2c 20 73 61 76 65 20 74 68 65 20 63 6f 6d 70 e, save the comp
5a20: 6c 65 74 65 20 6b 65 79 20 63 6f 6e 74 65 6e 74 lete key content
5a30: 2e 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 . It is possible
5a40: 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 . ** that the
5a50: 20 63 75 72 72 65 6e 74 20 6b 65 79 20 69 73 20 current key is
5a60: 63 6f 72 72 75 70 74 2e 20 49 6e 20 74 68 61 74 corrupt. In that
5a70: 20 63 61 73 65 2c 20 69 74 20 69 73 20 70 6f 73 case, it is pos
5a80: 73 69 62 6c 65 20 74 68 61 74 0a 20 20 20 20 2a sible that. *
5a90: 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 * the sqlite3Vdb
5aa0: 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 29 20 eRecordUnpack()
5ab0: 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 76 65 function may ove
5ac0: 72 72 65 61 64 20 74 68 65 20 62 75 66 66 65 72 rread the buffer
5ad0: 20 62 79 0a 20 20 20 20 2a 2a 20 75 70 20 74 6f by. ** up to
5ae0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 31 20 76 the size of 1 v
5af0: 61 72 69 6e 74 20 70 6c 75 73 20 31 20 38 2d 62 arint plus 1 8-b
5b00: 79 74 65 20 76 61 6c 75 65 20 77 68 65 6e 20 74 yte value when t
5b10: 68 65 20 63 75 72 73 6f 72 20 0a 20 20 20 20 2a he cursor . *
5b20: 2a 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 72 65 * position is re
5b30: 73 74 6f 72 65 64 2e 20 48 65 6e 63 65 20 74 68 stored. Hence th
5b40: 65 20 31 37 20 62 79 74 65 73 20 6f 66 20 70 61 e 17 bytes of pa
5b50: 64 64 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 20 dding allocated
5b60: 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2e 20 2a . ** below. *
5b70: 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b 65 79 /. void *pKey
5b80: 3b 0a 20 20 20 20 70 43 75 72 2d 3e 6e 4b 65 79 ;. pCur->nKey
5b90: 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
5ba0: 61 79 6c 6f 61 64 53 69 7a 65 28 70 43 75 72 29 ayloadSize(pCur)
5bb0: 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c ;. pKey = sql
5bc0: 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 75 72 ite3Malloc( pCur
5bd0: 2d 3e 6e 4b 65 79 20 2b 20 39 20 2b 20 38 20 29 ->nKey + 9 + 8 )
5be0: 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 20 29 ;. if( pKey )
5bf0: 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
5c00: 69 74 65 33 42 74 72 65 65 50 61 79 6c 6f 61 64 ite3BtreePayload
5c10: 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 70 (pCur, 0, (int)p
5c20: 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 Cur->nKey, pKey)
5c30: 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
5c40: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
5c50: 20 20 20 20 20 6d 65 6d 73 65 74 28 28 28 75 38 memset(((u8
5c60: 2a 29 70 4b 65 79 29 2b 70 43 75 72 2d 3e 6e 4b *)pKey)+pCur->nK
5c70: 65 79 2c 20 30 2c 20 39 2b 38 29 3b 0a 20 20 20 ey, 0, 9+8);.
5c80: 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 pCur->pKey
5c90: 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 = pKey;. }e
5ca0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
5cb0: 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29 3b ite3_free(pKey);
5cc0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
5cd0: 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
5ce0: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 QLITE_NOMEM_BKPT
5cf0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 ;. }. }. as
5d00: 73 65 72 74 28 20 21 70 43 75 72 2d 3e 63 75 72 sert( !pCur->cur
5d10: 49 6e 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d IntKey || !pCur-
5d20: 3e 70 4b 65 79 20 29 3b 0a 20 20 72 65 74 75 72 >pKey );. retur
5d30: 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
5d40: 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ave the current
5d50: 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 cursor position
5d60: 69 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 in the variables
5d70: 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a BtCursor.nKey .
5d80: 2a 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e ** and BtCursor.
5d90: 70 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 pKey. The cursor
5da0: 27 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 's state is set
5db0: 74 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 to CURSOR_REQUIR
5dc0: 45 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ESEEK..**.** The
5dd0: 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 caller must ens
5de0: 75 72 65 20 74 68 61 74 20 74 68 65 20 63 75 72 ure that the cur
5df0: 73 6f 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 sor is valid (ha
5e00: 73 20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 s eState==CURSOR
5e10: 5f 56 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 _VALID).** prior
5e20: 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 to calling this
5e30: 20 72 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 routine. .*/.s
5e40: 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 tatic int saveCu
5e50: 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 rsorPosition(BtC
5e60: 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
5e70: 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
5e80: 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d t( CURSOR_VALID=
5e90: 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 7c 7c =pCur->eState ||
5ea0: 20 43 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 CURSOR_SKIPNEXT
5eb0: 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 ==pCur->eState )
5ec0: 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 ;. assert( 0==p
5ed0: 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 Cur->pKey );. a
5ee0: 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
5ef0: 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
5f00: 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 .. if( pCur->eS
5f10: 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 53 4b 49 tate==CURSOR_SKI
5f20: 50 4e 45 58 54 20 29 7b 0a 20 20 20 20 70 43 75 PNEXT ){. pCu
5f30: 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
5f40: 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 7d 65 6c 73 OR_VALID;. }els
5f50: 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 e{. pCur->ski
5f60: 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a pNext = 0;. }..
5f70: 20 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f rc = saveCurso
5f80: 72 4b 65 79 28 70 43 75 72 29 3b 0a 20 20 69 66 rKey(pCur);. if
5f90: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
5fa0: 29 7b 0a 20 20 20 20 62 74 72 65 65 52 65 6c 65 ){. btreeRele
5fb0: 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 65 aseAllCursorPage
5fc0: 73 28 70 43 75 72 29 3b 0a 20 20 20 20 70 43 75 s(pCur);. pCu
5fd0: 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
5fe0: 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a OR_REQUIRESEEK;.
5ff0: 20 20 7d 0a 0a 20 20 70 43 75 72 2d 3e 63 75 72 }.. pCur->cur
6000: 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 5f Flags &= ~(BTCF_
6010: 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 ValidNKey|BTCF_V
6020: 61 6c 69 64 4f 76 66 6c 7c 42 54 43 46 5f 41 74 alidOvfl|BTCF_At
6030: 4c 61 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Last);. return
6040: 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 rc;.}../* Forwar
6050: 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 d reference */.s
6060: 74 61 74 69 63 20 69 6e 74 20 53 51 4c 49 54 45 tatic int SQLITE
6070: 5f 4e 4f 49 4e 4c 49 4e 45 20 73 61 76 65 43 75 _NOINLINE saveCu
6080: 72 73 6f 72 73 4f 6e 4c 69 73 74 28 42 74 43 75 rsorsOnList(BtCu
6090: 72 73 6f 72 2a 2c 50 67 6e 6f 2c 42 74 43 75 72 rsor*,Pgno,BtCur
60a0: 73 6f 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 61 sor*);../*.** Sa
60b0: 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 ve the positions
60c0: 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 of all cursors
60d0: 28 65 78 63 65 70 74 20 70 45 78 63 65 70 74 29 (except pExcept)
60e0: 20 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20 6f that are open o
60f0: 6e 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 77 n.** the table w
6100: 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 ith root-page iR
6110: 6f 6f 74 2e 20 20 22 53 61 76 69 6e 67 20 74 68 oot. "Saving th
6120: 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f e cursor positio
6130: 6e 22 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a n" means that.**
6140: 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 69 6e the location in
6150: 20 74 68 65 20 62 74 72 65 65 20 69 73 20 72 65 the btree is re
6160: 6d 65 6d 62 65 72 65 64 20 69 6e 20 73 75 63 68 membered in such
6170: 20 61 20 77 61 79 20 74 68 61 74 20 69 74 20 63 a way that it c
6180: 61 6e 20 62 65 0a 2a 2a 20 6d 6f 76 65 64 20 62 an be.** moved b
6190: 61 63 6b 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ack to the same
61a0: 73 70 6f 74 20 61 66 74 65 72 20 74 68 65 20 62 spot after the b
61b0: 74 72 65 65 20 68 61 73 20 62 65 65 6e 20 6d 6f tree has been mo
61c0: 64 69 66 69 65 64 2e 20 20 54 68 69 73 0a 2a 2a dified. This.**
61d0: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
61e0: 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 ed just before c
61f0: 75 72 73 6f 72 20 70 45 78 63 65 70 74 20 69 73 ursor pExcept is
6200: 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20 used to modify
6210: 74 68 65 0a 2a 2a 20 74 61 62 6c 65 2c 20 66 6f the.** table, fo
6220: 72 20 65 78 61 6d 70 6c 65 20 69 6e 20 42 74 72 r example in Btr
6230: 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 eeDelete() or Bt
6240: 72 65 65 49 6e 73 65 72 74 28 29 2e 0a 2a 2a 0a reeInsert()..**.
6250: 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 ** If there are
6260: 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 75 72 73 two or more curs
6270: 6f 72 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 ors on the same
6280: 62 74 72 65 65 2c 20 74 68 65 6e 20 61 6c 6c 20 btree, then all
6290: 73 75 63 68 20 0a 2a 2a 20 63 75 72 73 6f 72 73 such .** cursors
62a0: 20 73 68 6f 75 6c 64 20 68 61 76 65 20 74 68 65 should have the
62b0: 69 72 20 42 54 43 46 5f 4d 75 6c 74 69 70 6c 65 ir BTCF_Multiple
62c0: 20 66 6c 61 67 20 73 65 74 2e 20 20 54 68 65 20 flag set. The
62d0: 62 74 72 65 65 43 75 72 73 6f 72 28 29 0a 2a 2a btreeCursor().**
62e0: 20 72 6f 75 74 69 6e 65 20 65 6e 66 6f 72 63 65 routine enforce
62f0: 73 20 74 68 61 74 20 72 75 6c 65 2e 20 20 54 68 s that rule. Th
6300: 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
6310: 6e 65 65 64 73 20 74 6f 20 62 65 20 63 61 6c 6c needs to be call
6320: 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75 6e 63 ed in.** the unc
6330: 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 6e 20 ommon case when
6340: 70 45 78 70 65 63 74 20 68 61 73 20 74 68 65 20 pExpect has the
6350: 42 54 43 46 5f 4d 75 6c 74 69 70 6c 65 20 66 6c BTCF_Multiple fl
6360: 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 ag set..**.** If
6370: 20 70 45 78 70 65 63 74 21 3d 4e 55 4c 4c 20 61 pExpect!=NULL a
6380: 6e 64 20 69 66 20 6e 6f 20 6f 74 68 65 72 20 63 nd if no other c
6390: 75 72 73 6f 72 73 20 61 72 65 20 66 6f 75 6e 64 ursors are found
63a0: 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 6f 6f on the same roo
63b0: 74 2d 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 t-page,.** then
63c0: 74 68 65 20 42 54 43 46 5f 4d 75 6c 74 69 70 6c the BTCF_Multipl
63d0: 65 20 66 6c 61 67 20 6f 6e 20 70 45 78 70 65 63 e flag on pExpec
63e0: 74 20 69 73 20 63 6c 65 61 72 65 64 2c 20 74 6f t is cleared, to
63f0: 20 61 76 6f 69 64 20 61 6e 6f 74 68 65 72 0a 2a avoid another.*
6400: 2a 20 70 6f 69 6e 74 6c 65 73 73 20 63 61 6c 6c * pointless call
6410: 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
6420: 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ..**.** Implemen
6430: 74 61 74 69 6f 6e 20 6e 6f 74 65 3a 20 20 54 68 tation note: Th
6440: 69 73 20 72 6f 75 74 69 6e 65 20 6d 65 72 65 6c is routine merel
6450: 79 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 y checks to see
6460: 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 0a 2a if any cursors.*
6470: 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 73 61 76 * need to be sav
6480: 65 64 2e 20 20 49 74 20 63 61 6c 6c 73 20 6f 75 ed. It calls ou
6490: 74 20 74 6f 20 73 61 76 65 43 75 72 73 6f 72 73 t to saveCursors
64a0: 4f 6e 4c 69 73 74 28 29 20 69 6e 20 74 68 65 20 OnList() in the
64b0: 28 75 6e 75 73 75 61 6c 29 0a 2a 2a 20 65 76 65 (unusual).** eve
64c0: 6e 74 20 74 68 61 74 20 63 75 72 73 6f 72 73 20 nt that cursors
64d0: 61 72 65 20 69 6e 20 6e 65 65 64 20 74 6f 20 62 are in need to b
64e0: 65 69 6e 67 20 73 61 76 65 64 2e 0a 2a 2f 0a 73 eing saved..*/.s
64f0: 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c tatic int saveAl
6500: 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 lCursors(BtShare
6510: 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f d *pBt, Pgno iRo
6520: 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45 ot, BtCursor *pE
6530: 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73 xcept){. BtCurs
6540: 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 or *p;. assert(
6550: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
6560: 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
6570: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 );. assert( pEx
6580: 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65 cept==0 || pExce
6590: 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a pt->pBt==pBt );.
65a0: 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 for(p=pBt->pCu
65b0: 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e rsor; p; p=p->pN
65c0: 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21 ext){. if( p!
65d0: 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d =pExcept && (0==
65e0: 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f iRoot || p->pgno
65f0: 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 29 20 62 Root==iRoot) ) b
6600: 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 reak;. }. if(
6610: 70 20 29 20 72 65 74 75 72 6e 20 73 61 76 65 43 p ) return saveC
6620: 75 72 73 6f 72 73 4f 6e 4c 69 73 74 28 70 2c 20 ursorsOnList(p,
6630: 69 52 6f 6f 74 2c 20 70 45 78 63 65 70 74 29 3b iRoot, pExcept);
6640: 0a 20 20 69 66 28 20 70 45 78 63 65 70 74 20 29 . if( pExcept )
6650: 20 70 45 78 63 65 70 74 2d 3e 63 75 72 46 6c 61 pExcept->curFla
6660: 67 73 20 26 3d 20 7e 42 54 43 46 5f 4d 75 6c 74 gs &= ~BTCF_Mult
6670: 69 70 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 iple;. return S
6680: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 QLITE_OK;.}../*
6690: 54 68 69 73 20 68 65 6c 70 65 72 20 72 6f 75 74 This helper rout
66a0: 69 6e 65 20 74 6f 20 73 61 76 65 41 6c 6c 43 75 ine to saveAllCu
66b0: 72 73 6f 72 73 20 64 6f 65 73 20 74 68 65 20 61 rsors does the a
66c0: 63 74 75 61 6c 20 77 6f 72 6b 20 6f 66 20 73 61 ctual work of sa
66d0: 76 69 6e 67 0a 2a 2a 20 74 68 65 20 63 75 72 73 ving.** the curs
66e0: 6f 72 73 20 69 66 20 61 6e 64 20 77 68 65 6e 20 ors if and when
66f0: 61 20 63 75 72 73 6f 72 20 69 73 20 66 6f 75 6e a cursor is foun
6700: 64 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 d that actually
6710: 72 65 71 75 69 72 65 73 20 73 61 76 69 6e 67 2e requires saving.
6720: 0a 2a 2a 20 54 68 65 20 63 6f 6d 6d 6f 6e 20 63 .** The common c
6730: 61 73 65 20 69 73 20 74 68 61 74 20 6e 6f 20 63 ase is that no c
6740: 75 72 73 6f 72 73 20 6e 65 65 64 20 74 6f 20 62 ursors need to b
6750: 65 20 73 61 76 65 64 2c 20 73 6f 20 74 68 69 73 e saved, so this
6760: 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 62 routine is.** b
6770: 72 6f 6b 65 6e 20 6f 75 74 20 66 72 6f 6d 20 69 roken out from i
6780: 74 73 20 63 61 6c 6c 65 72 20 74 6f 20 61 76 6f ts caller to avo
6790: 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 20 73 id unnecessary s
67a0: 74 61 63 6b 20 70 6f 69 6e 74 65 72 20 6d 6f 76 tack pointer mov
67b0: 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 ement..*/.static
67c0: 20 69 6e 74 20 53 51 4c 49 54 45 5f 4e 4f 49 4e int SQLITE_NOIN
67d0: 4c 49 4e 45 20 73 61 76 65 43 75 72 73 6f 72 73 LINE saveCursors
67e0: 4f 6e 4c 69 73 74 28 0a 20 20 42 74 43 75 72 73 OnList(. BtCurs
67f0: 6f 72 20 2a 70 2c 20 20 20 20 20 20 20 20 20 2f or *p, /
6800: 2a 20 54 68 65 20 66 69 72 73 74 20 63 75 72 73 * The first curs
6810: 6f 72 20 74 68 61 74 20 6e 65 65 64 73 20 73 61 or that needs sa
6820: 76 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 ving */. Pgno i
6830: 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 2f Root, /
6840: 2a 20 4f 6e 6c 79 20 73 61 76 65 20 63 75 72 73 * Only save curs
6850: 6f 72 20 77 69 74 68 20 74 68 69 73 20 69 52 6f or with this iRo
6860: 6f 74 2e 20 53 61 76 65 20 61 6c 6c 20 69 66 20 ot. Save all if
6870: 7a 65 72 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 zero */. BtCurs
6880: 6f 72 20 2a 70 45 78 63 65 70 74 20 20 20 20 2f or *pExcept /
6890: 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 * Do not save th
68a0: 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a is cursor */.){.
68b0: 20 20 64 6f 7b 0a 20 20 20 20 69 66 28 20 70 21 do{. if( p!
68c0: 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d =pExcept && (0==
68d0: 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f iRoot || p->pgno
68e0: 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 29 7b 0a Root==iRoot) ){.
68f0: 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 53 74 if( p->eSt
6900: 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
6910: 44 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d D || p->eState==
6920: 43 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 CURSOR_SKIPNEXT
6930: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 ){. int r
6940: 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f c = saveCursorPo
6950: 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 sition(p);.
6960: 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
6970: 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 !=rc ){.
6980: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
6990: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
69a0: 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 se{. test
69b0: 63 61 73 65 28 20 70 2d 3e 69 50 61 67 65 3e 3d case( p->iPage>=
69c0: 30 20 29 3b 0a 20 20 20 20 20 20 20 20 62 74 72 0 );. btr
69d0: 65 65 52 65 6c 65 61 73 65 41 6c 6c 43 75 72 73 eeReleaseAllCurs
69e0: 6f 72 50 61 67 65 73 28 70 29 3b 0a 20 20 20 20 orPages(p);.
69f0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 }. }. p
6a00: 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 77 = p->pNext;. }w
6a10: 68 69 6c 65 28 20 70 20 29 3b 0a 20 20 72 65 74 hile( p );. ret
6a20: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
6a30: 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
6a40: 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 e current cursor
6a50: 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f position..*/.vo
6a60: 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
6a70: 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 learCursor(BtCur
6a80: 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 sor *pCur){. as
6a90: 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
6aa0: 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
6ab0: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
6ac0: 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 Cur->pKey);. pC
6ad0: 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 ur->pKey = 0;.
6ae0: 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
6af0: 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d URSOR_INVALID;.}
6b00: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 ../*.** In this
6b10: 76 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 version of Btree
6b20: 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 Moveto, pKey is
6b30: 61 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 a packed index r
6b40: 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 ecord.** such as
6b50: 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 is generated by
6b60: 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f the OP_MakeReco
6b70: 72 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 rd opcode. Unpa
6b80: 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 ck the.** record
6b90: 20 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 and then call B
6ba0: 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
6bb0: 65 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 ed() to do the w
6bc0: 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ork..*/.static i
6bd0: 6e 74 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 0a nt btreeMoveto(.
6be0: 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
6bf0: 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 , /* Cursor
6c00: 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 open on the btre
6c10: 65 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 e to be searched
6c20: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
6c30: 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 *pKey, /* Pac
6c40: 6b 65 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 ked key if the b
6c50: 74 72 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 tree is an index
6c60: 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 */. i64 nKey,
6c70: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 /* Int
6c80: 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 eger key for tab
6c90: 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b les. Size of pK
6ca0: 65 79 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a ey for indices *
6cb0: 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 /. int bias,
6cc0: 20 20 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 /* Bias
6cd0: 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 search to the hi
6ce0: 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 gh end */. int
6cf0: 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 *pRes
6d00: 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 /* Write search
6d10: 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
6d20: 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
6d30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d40: 2f 2a 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a /* Status code *
6d50: 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f /. UnpackedReco
6d60: 72 64 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f rd *pIdxKey; /
6d70: 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 * Unpacked index
6d80: 20 6b 65 79 20 2a 2f 0a 0a 20 20 69 66 28 20 70 key */.. if( p
6d90: 4b 65 79 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e Key ){. KeyIn
6da0: 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 fo *pKeyInfo = p
6db0: 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 Cur->pKeyInfo;.
6dc0: 20 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d assert( nKey=
6dd0: 3d 28 69 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 =(i64)(int)nKey
6de0: 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d );. pIdxKey =
6df0: 20 73 71 6c 69 74 65 33 56 64 62 65 41 6c 6c 6f sqlite3VdbeAllo
6e00: 63 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 cUnpackedRecord(
6e10: 70 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 20 20 69 pKeyInfo);. i
6e20: 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 20 f( pIdxKey==0 )
6e30: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
6e40: 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 73 71 MEM_BKPT;. sq
6e50: 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 lite3VdbeRecordU
6e60: 6e 70 61 63 6b 28 70 4b 65 79 49 6e 66 6f 2c 20 npack(pKeyInfo,
6e70: 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c (int)nKey, pKey,
6e80: 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 pIdxKey);. i
6e90: 66 28 20 70 49 64 78 4b 65 79 2d 3e 6e 46 69 65 f( pIdxKey->nFie
6ea0: 6c 64 3d 3d 30 20 7c 7c 20 70 49 64 78 4b 65 79 ld==0 || pIdxKey
6eb0: 2d 3e 6e 46 69 65 6c 64 3e 70 4b 65 79 49 6e 66 ->nField>pKeyInf
6ec0: 6f 2d 3e 6e 41 6c 6c 46 69 65 6c 64 20 29 7b 0a o->nAllField ){.
6ed0: 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
6ee0: 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
6ef0: 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
6f00: 6f 5f 64 6f 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 o_done;. }.
6f10: 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
6f20: 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
6f30: 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
6f40: 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
6f50: 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
6f60: 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
6f70: 6d 6f 76 65 74 6f 5f 64 6f 6e 65 3a 0a 20 20 69 moveto_done:. i
6f80: 66 28 20 70 49 64 78 4b 65 79 20 29 7b 0a 20 20 f( pIdxKey ){.
6f90: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
6fa0: 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e pCur->pKeyInfo->
6fb0: 64 62 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 db, pIdxKey);.
6fc0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
6fd0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 ../*.** Restore
6fe0: 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
6ff0: 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 e position it wa
7000: 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 s in (or as clos
7010: 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 e to as possible
7020: 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 ).** when saveCu
7030: 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 rsorPosition() w
7040: 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 as called. Note
7050: 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 that this call d
7060: 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 eletes the .** s
7070: 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e aved position in
7080: 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 fo stored by sav
7090: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
70a0: 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 ), so there can
70b0: 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e be.** at most on
70c0: 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74 e effective rest
70d0: 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
70e0: 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 n() call after e
70f0: 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 ach .** saveCurs
7100: 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f orPosition()..*/
7110: 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
7120: 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f eRestoreCursorPo
7130: 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 sition(BtCursor
7140: 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 *pCur){. int rc
7150: 3b 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 ;. int skipNext
7160: 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
7170: 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 64 28 70 orOwnsBtShared(p
7180: 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
7190: 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d ( pCur->eState>=
71a0: 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
71b0: 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
71c0: 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
71d0: 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 _FAULT ){. re
71e0: 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e turn pCur->skipN
71f0: 65 78 74 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d ext;. }. pCur-
7200: 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
7210: 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d _INVALID;. rc =
7220: 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 btreeMoveto(pCu
7230: 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 r, pCur->pKey, p
7240: 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 73 Cur->nKey, 0, &s
7250: 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 kipNext);. if(
7260: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
7270: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
7280: 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
7290: 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 pCur->pKey =
72a0: 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 0;. assert( p
72b0: 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
72c0: 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 SOR_VALID || pCu
72d0: 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
72e0: 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 R_INVALID );.
72f0: 20 69 66 28 20 73 6b 69 70 4e 65 78 74 20 29 20 if( skipNext )
7300: 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d pCur->skipNext =
7310: 20 73 6b 69 70 4e 65 78 74 3b 0a 20 20 20 20 69 skipNext;. i
7320: 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 f( pCur->skipNex
7330: 74 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 t && pCur->eStat
7340: 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
7350: 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 ){. pCur->e
7360: 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 53 State = CURSOR_S
7370: 4b 49 50 4e 45 58 54 3b 0a 20 20 20 20 7d 0a 20 KIPNEXT;. }.
7380: 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
7390: 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f }..#define resto
73a0: 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
73b0: 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 (p) \. (p->eSta
73c0: 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 te>=CURSOR_REQUI
73d0: 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 RESEEK ? \.
73e0: 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 btreeRestore
73f0: 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
7400: 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 ) : \. S
7410: 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a QLITE_OK)../*.**
7420: 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 Determine wheth
7430: 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 er or not a curs
7440: 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f or has moved fro
7450: 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 77 m the position w
7460: 68 65 72 65 0a 2a 2a 20 69 74 20 77 61 73 20 6c here.** it was l
7470: 61 73 74 20 70 6c 61 63 65 64 2c 20 6f 72 20 68 ast placed, or h
7480: 61 73 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 as been invalida
7490: 74 65 64 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 ted for any othe
74a0: 72 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 43 75 72 r reason..** Cur
74b0: 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 sors can move wh
74c0: 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 20 en the row they
74d0: 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 are pointing at
74e0: 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 0a 2a is deleted out.*
74f0: 2a 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 * from under the
7500: 6d 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 20 m, for example.
7510: 20 43 75 72 73 6f 72 20 6d 69 67 68 74 20 61 6c Cursor might al
7520: 73 6f 20 6d 6f 76 65 20 69 66 20 61 20 62 74 72 so move if a btr
7530: 65 65 0a 2a 2a 20 69 73 20 72 65 62 61 6c 61 6e ee.** is rebalan
7540: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 ced..**.** Calli
7550: 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ng this routine
7560: 77 69 74 68 20 61 20 4e 55 4c 4c 20 63 75 72 73 with a NULL curs
7570: 6f 72 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 or pointer retur
7580: 6e 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 ns false..**.**
7590: 55 73 65 20 74 68 65 20 73 65 70 61 72 61 74 65 Use the separate
75a0: 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
75b0: 73 6f 72 52 65 73 74 6f 72 65 28 29 20 72 6f 75 sorRestore() rou
75c0: 74 69 6e 65 20 74 6f 20 72 65 73 74 6f 72 65 20 tine to restore
75d0: 61 20 63 75 72 73 6f 72 0a 2a 2a 20 62 61 63 6b a cursor.** back
75e0: 20 74 6f 20 77 68 65 72 65 20 69 74 20 6f 75 67 to where it oug
75f0: 68 74 20 74 6f 20 62 65 20 69 66 20 74 68 69 73 ht to be if this
7600: 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
7610: 20 74 72 75 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 true..*/.int sq
7620: 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
7630: 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f HasMoved(BtCurso
7640: 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 r *pCur){. asse
7650: 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
7660: 4c 49 47 4e 4d 45 4e 54 28 70 43 75 72 29 0a 20 LIGNMENT(pCur).
7670: 20 20 20 20 20 20 7c 7c 20 70 43 75 72 3d 3d 73 || pCur==s
7680: 71 6c 69 74 65 33 42 74 72 65 65 46 61 6b 65 56 qlite3BtreeFakeV
7690: 61 6c 69 64 43 75 72 73 6f 72 28 29 20 29 3b 0a alidCursor() );.
76a0: 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 assert( offset
76b0: 6f 66 28 42 74 43 75 72 73 6f 72 2c 20 65 53 74 of(BtCursor, eSt
76c0: 61 74 65 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 ate)==0 );. ass
76d0: 65 72 74 28 20 73 69 7a 65 6f 66 28 70 43 75 72 ert( sizeof(pCur
76e0: 2d 3e 65 53 74 61 74 65 29 3d 3d 31 20 29 3b 0a ->eState)==1 );.
76f0: 20 20 72 65 74 75 72 6e 20 43 55 52 53 4f 52 5f return CURSOR_
7700: 56 41 4c 49 44 20 21 3d 20 2a 28 75 38 2a 29 70 VALID != *(u8*)p
7710: 43 75 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 Cur;.}../*.** Re
7720: 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
7730: 6f 20 61 20 66 61 6b 65 20 42 74 43 75 72 73 6f o a fake BtCurso
7740: 72 20 6f 62 6a 65 63 74 20 74 68 61 74 20 77 69 r object that wi
7750: 6c 6c 20 61 6c 77 61 79 73 20 61 6e 73 77 65 72 ll always answer
7760: 0a 2a 2a 20 66 61 6c 73 65 20 74 6f 20 74 68 65 .** false to the
7770: 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
7780: 73 6f 72 48 61 73 4d 6f 76 65 64 28 29 20 72 6f sorHasMoved() ro
7790: 75 74 69 6e 65 20 61 62 6f 76 65 2e 20 20 54 68 utine above. Th
77a0: 65 20 66 61 6b 65 0a 2a 2a 20 63 75 72 73 6f 72 e fake.** cursor
77b0: 20 72 65 74 75 72 6e 65 64 20 6d 75 73 74 20 6e returned must n
77c0: 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68 20 ot be used with
77d0: 61 6e 79 20 6f 74 68 65 72 20 42 74 72 65 65 20 any other Btree
77e0: 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 42 74 interface..*/.Bt
77f0: 43 75 72 73 6f 72 20 2a 73 71 6c 69 74 65 33 42 Cursor *sqlite3B
7800: 74 72 65 65 46 61 6b 65 56 61 6c 69 64 43 75 72 treeFakeValidCur
7810: 73 6f 72 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 sor(void){. sta
7820: 74 69 63 20 75 38 20 66 61 6b 65 43 75 72 73 6f tic u8 fakeCurso
7830: 72 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 r = CURSOR_VALID
7840: 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 ;. assert( offs
7850: 65 74 6f 66 28 42 74 43 75 72 73 6f 72 2c 20 65 etof(BtCursor, e
7860: 53 74 61 74 65 29 3d 3d 30 20 29 3b 0a 20 20 72 State)==0 );. r
7870: 65 74 75 72 6e 20 28 42 74 43 75 72 73 6f 72 2a eturn (BtCursor*
7880: 29 26 66 61 6b 65 43 75 72 73 6f 72 3b 0a 7d 0a )&fakeCursor;.}.
7890: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
78a0: 69 6e 65 20 72 65 73 74 6f 72 65 73 20 61 20 63 ine restores a c
78b0: 75 72 73 6f 72 20 62 61 63 6b 20 74 6f 20 69 74 ursor back to it
78c0: 73 20 6f 72 69 67 69 6e 61 6c 20 70 6f 73 69 74 s original posit
78d0: 69 6f 6e 20 61 66 74 65 72 20 69 74 0a 2a 2a 20 ion after it.**
78e0: 68 61 73 20 62 65 65 6e 20 6d 6f 76 65 64 20 62 has been moved b
78f0: 79 20 73 6f 6d 65 20 6f 75 74 73 69 64 65 20 61 y some outside a
7900: 63 74 69 76 69 74 79 20 28 73 75 63 68 20 61 73 ctivity (such as
7910: 20 61 20 62 74 72 65 65 20 72 65 62 61 6c 61 6e a btree rebalan
7920: 63 65 20 6f 72 0a 2a 2a 20 61 20 72 6f 77 20 68 ce or.** a row h
7930: 61 76 69 6e 67 20 62 65 65 6e 20 64 65 6c 65 74 aving been delet
7940: 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 ed out from unde
7950: 72 20 74 68 65 20 63 75 72 73 6f 72 29 2e 20 20 r the cursor).
7960: 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 .**.** On succes
7970: 73 2c 20 74 68 65 20 2a 70 44 69 66 66 65 72 65 s, the *pDiffere
7980: 6e 74 52 6f 77 20 70 61 72 61 6d 65 74 65 72 20 ntRow parameter
7990: 69 73 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 is false if the
79a0: 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a cursor is left.*
79b0: 2a 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 78 * pointing at ex
79c0: 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 72 actly the same r
79d0: 6f 77 2e 20 20 2a 70 44 69 66 66 65 72 6e 74 52 ow. *pDifferntR
79e0: 6f 77 20 69 73 20 74 68 65 20 72 6f 77 20 74 68 ow is the row th
79f0: 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 e cursor.** was
7a00: 70 6f 69 6e 74 69 6e 67 20 74 6f 20 68 61 73 20 pointing to has
7a10: 62 65 65 6e 20 64 65 6c 65 74 65 64 2c 20 66 6f been deleted, fo
7a20: 72 63 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 rcing the cursor
7a30: 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d to point to som
7a40: 65 0a 2a 2a 20 6e 65 61 72 62 79 20 72 6f 77 2e e.** nearby row.
7a50: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
7a60: 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 ine should only
7a70: 62 65 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 be called for a
7a80: 63 75 72 73 6f 72 20 74 68 61 74 20 6a 75 73 74 cursor that just
7a90: 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 54 52 55 returned.** TRU
7aa0: 45 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 42 74 E from sqlite3Bt
7ab0: 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
7ac0: 64 28 29 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 d()..*/.int sqli
7ad0: 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 52 65 te3BtreeCursorRe
7ae0: 73 74 6f 72 65 28 42 74 43 75 72 73 6f 72 20 2a store(BtCursor *
7af0: 70 43 75 72 2c 20 69 6e 74 20 2a 70 44 69 66 66 pCur, int *pDiff
7b00: 65 72 65 6e 74 52 6f 77 29 7b 0a 20 20 69 6e 74 erentRow){. int
7b10: 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
7b20: 70 43 75 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 pCur!=0 );. ass
7b30: 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
7b40: 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
7b50: 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 );. rc = restor
7b60: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
7b70: 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 pCur);. if( rc
7b80: 29 7b 0a 20 20 20 20 2a 70 44 69 66 66 65 72 65 ){. *pDiffere
7b90: 6e 74 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 72 ntRow = 1;. r
7ba0: 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
7bb0: 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
7bc0: 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 !=CURSOR_VALID )
7bd0: 7b 0a 20 20 20 20 2a 70 44 69 66 66 65 72 65 6e {. *pDifferen
7be0: 74 52 6f 77 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 tRow = 1;. }els
7bf0: 65 7b 0a 20 20 20 20 2a 70 44 69 66 66 65 72 65 e{. *pDiffere
7c00: 6e 74 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 0a 20 ntRow = 0;. }.
7c10: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
7c20: 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c K;.}..#ifdef SQL
7c30: 49 54 45 5f 45 4e 41 42 4c 45 5f 43 55 52 53 4f ITE_ENABLE_CURSO
7c40: 52 5f 48 49 4e 54 53 0a 2f 2a 0a 2a 2a 20 50 72 R_HINTS./*.** Pr
7c50: 6f 76 69 64 65 20 68 69 6e 74 73 20 74 6f 20 74 ovide hints to t
7c60: 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 he cursor. The
7c70: 70 61 72 74 69 63 75 6c 61 72 20 68 69 6e 74 20 particular hint
7c80: 67 69 76 65 6e 20 28 61 6e 64 20 74 68 65 20 74 given (and the t
7c90: 79 70 65 0a 2a 2a 20 61 6e 64 20 6e 75 6d 62 65 ype.** and numbe
7ca0: 72 20 6f 66 20 74 68 65 20 76 61 72 61 72 67 73 r of the varargs
7cb0: 20 70 61 72 61 6d 65 74 65 72 73 29 20 69 73 20 parameters) is
7cc0: 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
7cd0: 65 20 65 48 69 6e 74 54 79 70 65 0a 2a 2a 20 70 e eHintType.** p
7ce0: 61 72 61 6d 65 74 65 72 2e 20 20 53 65 65 20 74 arameter. See t
7cf0: 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f he definitions o
7d00: 66 20 74 68 65 20 42 54 52 45 45 5f 48 49 4e 54 f the BTREE_HINT
7d10: 5f 2a 20 6d 61 63 72 6f 73 20 66 6f 72 20 64 65 _* macros for de
7d20: 74 61 69 6c 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 tails..*/.void s
7d30: 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
7d40: 72 48 69 6e 74 28 42 74 43 75 72 73 6f 72 20 2a rHint(BtCursor *
7d50: 70 43 75 72 2c 20 69 6e 74 20 65 48 69 6e 74 54 pCur, int eHintT
7d60: 79 70 65 2c 20 2e 2e 2e 29 7b 0a 20 20 2f 2a 20 ype, ...){. /*
7d70: 55 73 65 64 20 6f 6e 6c 79 20 62 79 20 73 79 73 Used only by sys
7d80: 74 65 6d 20 74 68 61 74 20 73 75 62 73 74 69 74 tem that substit
7d90: 75 74 65 20 74 68 65 69 72 20 6f 77 6e 20 73 74 ute their own st
7da0: 6f 72 61 67 65 20 65 6e 67 69 6e 65 20 2a 2f 0a orage engine */.
7db0: 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
7dc0: 50 72 6f 76 69 64 65 20 66 6c 61 67 20 68 69 6e Provide flag hin
7dd0: 74 73 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 ts to the cursor
7de0: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
7df0: 33 42 74 72 65 65 43 75 72 73 6f 72 48 69 6e 74 3BtreeCursorHint
7e00: 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 2a Flags(BtCursor *
7e10: 70 43 75 72 2c 20 75 6e 73 69 67 6e 65 64 20 78 pCur, unsigned x
7e20: 29 7b 0a 20 20 61 73 73 65 72 74 28 20 78 3d 3d ){. assert( x==
7e30: 42 54 52 45 45 5f 53 45 45 4b 5f 45 51 20 7c 7c BTREE_SEEK_EQ ||
7e40: 20 78 3d 3d 42 54 52 45 45 5f 42 55 4c 4b 4c 4f x==BTREE_BULKLO
7e50: 41 44 20 7c 7c 20 78 3d 3d 30 20 29 3b 0a 20 20 AD || x==0 );.
7e60: 70 43 75 72 2d 3e 68 69 6e 74 73 20 3d 20 78 3b pCur->hints = x;
7e70: 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c .}...#ifndef SQL
7e80: 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
7e90: 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 UUM./*.** Given
7ea0: 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 a page number of
7eb0: 20 61 20 72 65 67 75 6c 61 72 20 64 61 74 61 62 a regular datab
7ec0: 61 73 65 20 70 61 67 65 2c 20 72 65 74 75 72 6e ase page, return
7ed0: 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d the page.** num
7ee0: 62 65 72 20 66 6f 72 20 74 68 65 20 70 6f 69 6e ber for the poin
7ef0: 74 65 72 2d 6d 61 70 20 70 61 67 65 20 74 68 61 ter-map page tha
7f00: 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 t contains the e
7f10: 6e 74 72 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 ntry for the.**
7f20: 69 6e 70 75 74 20 70 61 67 65 20 6e 75 6d 62 65 input page numbe
7f30: 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 r..**.** Return
7f40: 30 20 28 6e 6f 74 20 61 20 76 61 6c 69 64 20 70 0 (not a valid p
7f50: 61 67 65 29 20 66 6f 72 20 70 67 6e 6f 3d 3d 31 age) for pgno==1
7f60: 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 0a since there is.
7f70: 2a 2a 20 6e 6f 20 70 6f 69 6e 74 65 72 20 6d 61 ** no pointer ma
7f80: 70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 p associated wit
7f90: 68 20 70 61 67 65 20 31 2e 20 20 54 68 65 20 69 h page 1. The i
7fa0: 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 6c ntegrity_check l
7fb0: 6f 67 69 63 0a 2a 2a 20 72 65 71 75 69 72 65 73 ogic.** requires
7fc0: 20 74 68 61 74 20 70 74 72 6d 61 70 50 61 67 65 that ptrmapPage
7fd0: 6e 6f 28 2a 2c 31 29 21 3d 31 2e 0a 2a 2f 0a 73 no(*,1)!=1..*/.s
7fe0: 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 tatic Pgno ptrma
7ff0: 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 pPageno(BtShared
8000: 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
8010: 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 ){. int nPagesP
8020: 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e erMapPage;. Pgn
8030: 6f 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a o iPtrMap, ret;.
8040: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8050: 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
8060: 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
8070: 28 20 70 67 6e 6f 3c 32 20 29 20 72 65 74 75 72 ( pgno<2 ) retur
8080: 6e 20 30 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 n 0;. nPagesPer
8090: 4d 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e MapPage = (pBt->
80a0: 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b usableSize/5)+1;
80b0: 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 . iPtrMap = (pg
80c0: 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d no-2)/nPagesPerM
80d0: 61 70 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 apPage;. ret =
80e0: 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 (iPtrMap*nPagesP
80f0: 65 72 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 erMapPage) + 2;
8100: 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 . if( ret==PEND
8110: 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
8120: 74 29 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b t) ){. ret++;
8130: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 . }. return re
8140: 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 t;.}../*.** Writ
8150: 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 e an entry into
8160: 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
8170: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
8180: 69 6e 65 20 75 70 64 61 74 65 73 20 74 68 65 20 ine updates the
8190: 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
81a0: 79 20 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 y for page numbe
81b0: 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 r 'key'.** so th
81c0: 61 74 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 at it maps to ty
81d0: 70 65 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 pe 'eType' and p
81e0: 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 arent page numbe
81f0: 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 r 'pgno'..**.**
8200: 49 66 20 2a 70 52 43 20 69 73 20 69 6e 69 74 69 If *pRC is initi
8210: 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e ally non-zero (n
8220: 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 on-SQLITE_OK) th
8230: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
8240: 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 is.** a no-op.
8250: 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
8260: 72 73 2c 20 74 68 65 20 61 70 70 72 6f 70 72 69 rs, the appropri
8270: 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ate error code i
8280: 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 s written.** int
8290: 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 o *pRC..*/.stati
82a0: 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 c void ptrmapPut
82b0: 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
82c0: 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 Pgno key, u8 eTy
82d0: 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c pe, Pgno parent,
82e0: 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 int *pRC){. Db
82f0: 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 Page *pDbPage;
8300: 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d /* The pointer m
8310: 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 ap page */. u8
8320: 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f *pPtrmap; /
8330: 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
8340: 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f p data */. Pgno
8350: 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a iPtrmap; /*
8360: 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
8370: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
8380: 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 int offset;
8390: 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
83a0: 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 pointer map pag
83b0: 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
83c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
83d0: 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 rn code from sub
83e0: 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 functions */..
83f0: 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 if( *pRC ) retur
8400: 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 n;.. assert( sq
8410: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
8420: 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
8430: 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 2d /* The master-
8440: 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d journal page num
8450: 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 62 ber must never b
8460: 65 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 6e e used as a poin
8470: 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a ter map page */.
8480: 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 52 assert( 0==PTR
8490: 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
84a0: 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
84b0: 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 E(pBt)) );.. as
84c0: 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 sert( pBt->autoV
84d0: 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b acuum );. if( k
84e0: 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 ey==0 ){. *pR
84f0: 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 C = SQLITE_CORRU
8500: 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 PT_BKPT;. ret
8510: 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d urn;. }. iPtrm
8520: 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 ap = PTRMAP_PAGE
8530: 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 NO(pBt, key);.
8540: 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
8550: 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 rGet(pBt->pPager
8560: 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 , iPtrmap, &pDbP
8570: 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 age, 0);. if( r
8580: 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
8590: 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 *pRC = rc;.
85a0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
85b0: 20 69 66 28 20 28 28 63 68 61 72 2a 29 73 71 6c if( ((char*)sql
85c0: 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
85d0: 61 28 70 44 62 50 61 67 65 29 29 5b 30 5d 21 3d a(pDbPage))[0]!=
85e0: 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 0 ){. /* The
85f0: 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
8600: 65 20 65 78 74 72 61 20 64 61 74 61 20 69 73 20 e extra data is
8610: 74 68 65 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e the MemPage.isIn
8620: 69 74 20 62 79 74 65 2e 0a 20 20 20 20 2a 2a 20 it byte.. **
8630: 49 66 20 74 68 61 74 20 62 79 74 65 20 69 73 20 If that byte is
8640: 73 65 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 set, it means th
8650: 69 73 20 70 61 67 65 20 69 73 20 61 6c 73 6f 20 is page is also
8660: 62 65 69 6e 67 20 75 73 65 64 0a 20 20 20 20 2a being used. *
8670: 2a 20 61 73 20 61 20 62 74 72 65 65 20 70 61 67 * as a btree pag
8680: 65 2e 20 2a 2f 0a 20 20 20 20 2a 70 52 43 20 3d e. */. *pRC =
8690: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
86a0: 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 70 BKPT;. goto p
86b0: 74 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20 7d 0a trmap_exit;. }.
86c0: 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 offset = PTRMA
86d0: 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 P_PTROFFSET(iPtr
86e0: 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 map, key);. if(
86f0: 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 offset<0 ){.
8700: 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 *pRC = SQLITE_C
8710: 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
8720: 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 goto ptrmap_exi
8730: 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 t;. }. assert(
8740: 20 6f 66 66 73 65 74 20 3c 3d 20 28 69 6e 74 29 offset <= (int)
8750: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
8760: 35 20 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 5 );. pPtrmap =
8770: 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 (u8 *)sqlite3Pa
8780: 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 gerGetData(pDbPa
8790: 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 ge);.. if( eTyp
87a0: 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e!=pPtrmap[offse
87b0: 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 t] || get4byte(&
87c0: 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 pPtrmap[offset+1
87d0: 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 ])!=parent ){.
87e0: 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 TRACE(("PTRMAP
87f0: 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 _UPDATE: %d->(%d
8800: 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 ,%d)\n", key, eT
8810: 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 ype, parent));.
8820: 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 *pRC= rc = sq
8830: 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
8840: 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
8850: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
8860: 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70 ){. pPtrmap
8870: 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65 [offset] = eType
8880: 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
8890: 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 (&pPtrmap[offset
88a0: 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 +1], parent);.
88b0: 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f }. }..ptrmap_
88c0: 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 exit:. sqlite3P
88d0: 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 agerUnref(pDbPag
88e0: 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 e);.}../*.** Rea
88f0: 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 d an entry from
8900: 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
8910: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
8920: 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68 ine retrieves th
8930: 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
8940: 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65 try for page 'ke
8950: 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 y', writing.** t
8960: 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65 he type and pare
8970: 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 nt page number t
8980: 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70 o *pEType and *p
8990: 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c Pgno respectivel
89a0: 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 y..** An error c
89b0: 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
89c0: 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 if something goe
89d0: 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 s wrong, otherwi
89e0: 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f se SQLITE_OK..*/
89f0: 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d .static int ptrm
8a00: 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a apGet(BtShared *
8a10: 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 pBt, Pgno key, u
8a20: 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 8 *pEType, Pgno
8a30: 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 *pPgno){. DbPag
8a40: 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a e *pDbPage; /*
8a50: 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
8a60: 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 page */. int i
8a70: 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a Ptrmap; /*
8a80: 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 Pointer map pag
8a90: 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 e index */. u8
8aa0: 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 *pPtrmap;
8ab0: 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 /* Pointer map p
8ac0: 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e age data */. in
8ad0: 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 t offset;
8ae0: 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e /* Offset of en
8af0: 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d try in pointer m
8b00: 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a ap */. int rc;.
8b10: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
8b20: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
8b30: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 t->mutex) );..
8b40: 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 iPtrmap = PTRMAP
8b50: 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 _PAGENO(pBt, key
8b60: 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
8b70: 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 3PagerGet(pBt->p
8b80: 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 Pager, iPtrmap,
8b90: 26 70 44 62 50 61 67 65 2c 20 30 29 3b 0a 20 20 &pDbPage, 0);.
8ba0: 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 if( rc!=0 ){.
8bb0: 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
8bc0: 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 pPtrmap = (u8
8bd0: 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
8be0: 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a tData(pDbPage);.
8bf0: 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d . offset = PTRM
8c00: 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 AP_PTROFFSET(iPt
8c10: 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 rmap, key);. if
8c20: 28 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 ( offset<0 ){.
8c30: 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
8c40: 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
8c50: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
8c60: 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
8c70: 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 }. assert( offs
8c80: 65 74 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e et <= (int)pBt->
8c90: 75 73 61 62 6c 65 53 69 7a 65 2d 35 20 29 3b 0a usableSize-5 );.
8ca0: 20 20 61 73 73 65 72 74 28 20 70 45 54 79 70 65 assert( pEType
8cb0: 21 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 !=0 );. *pEType
8cc0: 20 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 = pPtrmap[offse
8cd0: 74 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 t];. if( pPgno
8ce0: 29 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 ) *pPgno = get4b
8cf0: 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 yte(&pPtrmap[off
8d00: 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 set+1]);.. sqli
8d10: 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
8d20: 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 bPage);. if( *p
8d30: 45 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 EType<1 || *pETy
8d40: 70 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53 51 pe>5 ) return SQ
8d50: 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e LITE_CORRUPT_PGN
8d60: 4f 28 69 50 74 72 6d 61 70 29 3b 0a 20 20 72 65 O(iPtrmap);. re
8d70: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
8d80: 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 }..#else /* if d
8d90: 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d efined SQLITE_OM
8da0: 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f IT_AUTOVACUUM */
8db0: 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 . #define ptrma
8dc0: 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 pPut(w,x,y,z,rc)
8dd0: 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 . #define ptrma
8de0: 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 pGet(w,x,y,z) SQ
8df0: 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e LITE_OK. #defin
8e00: 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 e ptrmapPutOvflP
8e10: 74 72 28 78 2c 20 79 2c 20 7a 2c 20 72 63 29 0a tr(x, y, z, rc).
8e20: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 #endif../*.** Gi
8e30: 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 ven a btree page
8e40: 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 and a cell inde
8e50: 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 x (0 means the f
8e60: 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 irst cell on.**
8e70: 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e the page, 1 mean
8e80: 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c s the second cel
8e90: 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 l, and so forth)
8ea0: 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 return a pointe
8eb0: 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c r.** to the cell
8ec0: 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 content..**.**
8ed0: 66 69 6e 64 43 65 6c 6c 50 61 73 74 50 74 72 28 findCellPastPtr(
8ee0: 29 20 64 6f 65 73 20 74 68 65 20 73 61 6d 65 20 ) does the same
8ef0: 65 78 63 65 70 74 20 69 74 20 73 6b 69 70 73 20 except it skips
8f00: 70 61 73 74 20 74 68 65 20 69 6e 69 74 69 61 6c past the initial
8f10: 0a 2a 2a 20 34 2d 62 79 74 65 20 63 68 69 6c 64 .** 4-byte child
8f20: 20 70 6f 69 6e 74 65 72 20 66 6f 75 6e 64 20 6f pointer found o
8f30: 6e 20 69 6e 74 65 72 69 6f 72 20 70 61 67 65 73 n interior pages
8f40: 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e , if there is on
8f50: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
8f60: 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 utine works only
8f70: 20 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 20 for pages that
8f80: 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f do not contain o
8f90: 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a verflow cells..*
8fa0: 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 /.#define findCe
8fb0: 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 ll(P,I) \. ((P)
8fc0: 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d 3e ->aData + ((P)->
8fd0: 6d 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 62 maskPage & get2b
8fe0: 79 74 65 41 6c 69 67 6e 65 64 28 26 28 50 29 2d yteAligned(&(P)-
8ff0: 3e 61 43 65 6c 6c 49 64 78 5b 32 2a 28 49 29 5d >aCellIdx[2*(I)]
9000: 29 29 29 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 ))).#define find
9010: 43 65 6c 6c 50 61 73 74 50 74 72 28 50 2c 49 29 CellPastPtr(P,I)
9020: 20 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 \. ((P)->aData
9030: 4f 66 73 74 20 2b 20 28 28 50 29 2d 3e 6d 61 73 Ofst + ((P)->mas
9040: 6b 50 61 67 65 20 26 20 67 65 74 32 62 79 74 65 kPage & get2byte
9050: 41 6c 69 67 6e 65 64 28 26 28 50 29 2d 3e 61 43 Aligned(&(P)->aC
9060: 65 6c 6c 49 64 78 5b 32 2a 28 49 29 5d 29 29 29 ellIdx[2*(I)])))
9070: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 .../*.** This is
9080: 20 63 6f 6d 6d 6f 6e 20 74 61 69 6c 20 70 72 6f common tail pro
9090: 63 65 73 73 69 6e 67 20 66 6f 72 20 62 74 72 65 cessing for btre
90a0: 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 eParseCellPtr()
90b0: 61 6e 64 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 and.** btreePars
90c0: 65 43 65 6c 6c 50 74 72 49 6e 64 65 78 28 29 20 eCellPtrIndex()
90d0: 66 6f 72 20 74 68 65 20 63 61 73 65 20 77 68 65 for the case whe
90e0: 6e 20 74 68 65 20 63 65 6c 6c 20 64 6f 65 73 20 n the cell does
90f0: 6e 6f 74 20 66 69 74 20 65 6e 74 69 72 65 6c 79 not fit entirely
9100: 0a 2a 2a 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 .** on a single
9110: 42 2d 74 72 65 65 20 70 61 67 65 2e 20 20 4d 61 B-tree page. Ma
9120: 6b 65 20 6e 65 63 65 73 73 61 72 79 20 61 64 6a ke necessary adj
9130: 75 73 74 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 ustments to the
9140: 43 65 6c 6c 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 CellInfo.** stru
9150: 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 cture..*/.static
9160: 20 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e 45 SQLITE_NOINLINE
9170: 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 void btreeParse
9180: 43 65 6c 6c 41 64 6a 75 73 74 53 69 7a 65 46 6f CellAdjustSizeFo
9190: 72 4f 76 65 72 66 6c 6f 77 28 0a 20 20 4d 65 6d rOverflow(. Mem
91a0: 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
91b0: 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e /* Page con
91c0: 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c taining the cell
91d0: 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
91e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
91f0: 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
9200: 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 cell text. */.
9210: 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 CellInfo *pInfo
9220: 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 /* Fill
9230: 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
9240: 65 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 49 66 20 e */.){. /* If
9250: 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c the payload will
9260: 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 not fit complet
9270: 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c ely on the local
9280: 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 page, we have.
9290: 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f ** to decide ho
92a0: 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 w much to store
92b0: 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 locally and how
92c0: 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e much to spill on
92d0: 74 6f 0a 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 to. ** overflow
92e0: 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 74 72 pages. The str
92f0: 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 6e 69 ategy is to mini
9300: 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 mize the amount
9310: 6f 66 20 75 6e 75 73 65 64 0a 20 20 2a 2a 20 73 of unused. ** s
9320: 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 pace on overflow
9330: 20 70 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 pages while kee
9340: 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 ping the amount
9350: 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 of local storage
9360: 0a 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e . ** in between
9370: 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 minLocal and ma
9380: 78 4c 6f 63 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a xLocal.. **. *
9390: 2a 20 57 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e * Warning: chan
93a0: 67 69 6e 67 20 74 68 65 20 77 61 79 20 6f 76 65 ging the way ove
93b0: 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 rflow payload is
93c0: 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 distributed in
93d0: 61 6e 79 0a 20 20 2a 2a 20 77 61 79 20 77 69 6c any. ** way wil
93e0: 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 l result in an i
93f0: 6e 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 ncompatible file
9400: 20 66 6f 72 6d 61 74 2e 0a 20 20 2a 2f 0a 20 20 format.. */.
9410: 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f int minLocal; /
9420: 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Minimum amount
9430: 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
9440: 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 69 6e locally */. in
9450: 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 t maxLocal; /*
9460: 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f Maximum amount o
9470: 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c f payload held l
9480: 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 ocally */. int
9490: 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 surplus; /* Ov
94a0: 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 erflow payload a
94b0: 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 vailable for loc
94c0: 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 al storage */..
94d0: 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 minLocal = pPag
94e0: 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 6d e->minLocal;. m
94f0: 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d axLocal = pPage-
9500: 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 73 75 72 >maxLocal;. sur
9510: 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 plus = minLocal
9520: 2b 20 28 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f + (pInfo->nPaylo
9530: 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 ad - minLocal)%(
9540: 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
9550: 6c 65 53 69 7a 65 2d 34 29 3b 0a 20 20 74 65 73 leSize-4);. tes
9560: 74 63 61 73 65 28 20 73 75 72 70 6c 75 73 3d 3d tcase( surplus==
9570: 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 maxLocal );. te
9580: 73 74 63 61 73 65 28 20 73 75 72 70 6c 75 73 3d stcase( surplus=
9590: 3d 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 =maxLocal+1 );.
95a0: 20 69 66 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 if( surplus <=
95b0: 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 maxLocal ){.
95c0: 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 pInfo->nLocal =
95d0: 28 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 (u16)surplus;.
95e0: 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f }else{. pInfo
95f0: 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 ->nLocal = (u16)
9600: 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 20 20 minLocal;. }.
9610: 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 pInfo->nSize = (
9620: 75 31 36 29 28 26 70 49 6e 66 6f 2d 3e 70 50 61 u16)(&pInfo->pPa
9630: 79 6c 6f 61 64 5b 70 49 6e 66 6f 2d 3e 6e 4c 6f yload[pInfo->nLo
9640: 63 61 6c 5d 20 2d 20 70 43 65 6c 6c 29 20 2b 20 cal] - pCell) +
9650: 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4;.}../*.** The
9660: 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
9670: 65 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 es are implement
9680: 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 4d 65 ations of the Me
9690: 6d 50 61 67 65 2e 78 50 61 72 73 65 43 65 6c 6c mPage.xParseCell
96a0: 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a ().** method..**
96b0: 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c .** Parse a cell
96c0: 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 content block a
96d0: 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 nd fill in the C
96e0: 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ellInfo structur
96f0: 65 2e 0a 2a 2a 0a 2a 2a 20 62 74 72 65 65 50 61 e..**.** btreePa
9700: 72 73 65 43 65 6c 6c 50 74 72 28 29 20 20 20 20 rseCellPtr()
9710: 20 20 20 20 3d 3e 20 20 20 74 61 62 6c 65 20 62 => table b
9720: 74 72 65 65 20 6c 65 61 66 20 6e 6f 64 65 73 0a tree leaf nodes.
9730: 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 65 6c ** btreeParseCel
9740: 6c 4e 6f 50 61 79 6c 6f 61 64 28 29 20 20 3d 3e lNoPayload() =>
9750: 20 20 20 74 61 62 6c 65 20 62 74 72 65 65 20 69 table btree i
9760: 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 0a 2a 2a nternal nodes.**
9770: 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
9780: 74 72 49 6e 64 65 78 28 29 20 20 20 3d 3e 20 20 trIndex() =>
9790: 20 69 6e 64 65 78 20 62 74 72 65 65 20 6e 6f 64 index btree nod
97a0: 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 es.**.** There i
97b0: 73 20 61 6c 73 6f 20 61 20 77 72 61 70 70 65 72 s also a wrapper
97c0: 20 66 75 6e 63 74 69 6f 6e 20 62 74 72 65 65 50 function btreeP
97d0: 61 72 73 65 43 65 6c 6c 28 29 20 74 68 61 74 20 arseCell() that
97e0: 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 61 6c 6c works for.** all
97f0: 20 4d 65 6d 50 61 67 65 20 74 79 70 65 73 20 61 MemPage types a
9800: 6e 64 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 nd that referenc
9810: 65 73 20 74 68 65 20 63 65 6c 6c 20 62 79 20 69 es the cell by i
9820: 6e 64 65 78 20 72 61 74 68 65 72 20 74 68 61 6e ndex rather than
9830: 0a 2a 2a 20 62 79 20 70 6f 69 6e 74 65 72 2e 0a .** by pointer..
9840: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 */.static void b
9850: 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
9860: 4e 6f 50 61 79 6c 6f 61 64 28 0a 20 20 4d 65 6d NoPayload(. Mem
9870: 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
9880: 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e /* Page con
9890: 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c taining the cell
98a0: 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
98b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
98c0: 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
98d0: 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 cell text. */.
98e0: 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 CellInfo *pInfo
98f0: 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 /* Fill
9900: 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
9910: 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 e */.){. assert
9920: 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
9930: 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
9940: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
9950: 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 ert( pPage->leaf
9960: 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
9970: 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
9980: 53 69 7a 65 3d 3d 34 20 29 3b 0a 23 69 66 6e 64 Size==4 );.#ifnd
9990: 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
99a0: 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
99b0: 45 52 28 70 50 61 67 65 29 3b 0a 23 65 6e 64 69 ER(pPage);.#endi
99c0: 66 0a 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 f. pInfo->nSize
99d0: 20 3d 20 34 20 2b 20 67 65 74 56 61 72 69 6e 74 = 4 + getVarint
99e0: 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 28 75 36 34 (&pCell[4], (u64
99f0: 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b *)&pInfo->nKey);
9a00: 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f . pInfo->nPaylo
9a10: 61 64 20 3d 20 30 3b 0a 20 20 70 49 6e 66 6f 2d ad = 0;. pInfo-
9a20: 3e 6e 4c 6f 63 61 6c 20 3d 20 30 3b 0a 20 20 70 >nLocal = 0;. p
9a30: 49 6e 66 6f 2d 3e 70 50 61 79 6c 6f 61 64 20 3d Info->pPayload =
9a40: 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0;. return;.}.
9a50: 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 static void btre
9a60: 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 0a 20 eParseCellPtr(.
9a70: 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c MemPage *pPage,
9a80: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
9a90: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
9aa0: 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43 cell */. u8 *pC
9ab0: 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ell,
9ac0: 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
9ad0: 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a the cell text. *
9ae0: 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 /. CellInfo *pI
9af0: 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 nfo /* F
9b00: 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 ill in this stru
9b10: 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 38 cture */.){. u8
9b20: 20 2a 70 49 74 65 72 3b 20 20 20 20 20 20 20 20 *pIter;
9b30: 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 63 61 /* For sca
9b40: 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68 20 70 43 nning through pC
9b50: 65 6c 6c 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 ell */. u32 nPa
9b60: 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 20 yload;
9b70: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
9b80: 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c tes of cell payl
9b90: 6f 61 64 20 2a 2f 0a 20 20 75 36 34 20 69 4b 65 oad */. u64 iKe
9ba0: 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
9bb0: 20 2f 2a 20 45 78 74 72 61 63 74 65 64 20 4b 65 /* Extracted Ke
9bc0: 79 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 61 73 y value */.. as
9bd0: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
9be0: 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
9bf0: 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
9c00: 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
9c10: 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61 67 65 leaf==0 || pPage
9c20: 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 20 61 ->leaf==1 );. a
9c30: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e ssert( pPage->in
9c40: 74 4b 65 79 4c 65 61 66 20 29 3b 0a 20 20 61 73 tKeyLeaf );. as
9c50: 73 65 72 74 28 20 70 50 61 67 65 2d 3e 63 68 69 sert( pPage->chi
9c60: 6c 64 50 74 72 53 69 7a 65 3d 3d 30 20 29 3b 0a ldPtrSize==0 );.
9c70: 20 20 70 49 74 65 72 20 3d 20 70 43 65 6c 6c 3b pIter = pCell;
9c80: 0a 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 .. /* The next
9c90: 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 73 block of code is
9ca0: 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a equivalent to:.
9cb0: 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 70 49 **. ** pI
9cc0: 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 ter += getVarint
9cd0: 33 32 28 70 49 74 65 72 2c 20 6e 50 61 79 6c 6f 32(pIter, nPaylo
9ce0: 61 64 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ad);. **. ** T
9cf0: 68 65 20 63 6f 64 65 20 69 73 20 69 6e 6c 69 6e he code is inlin
9d00: 65 64 20 74 6f 20 61 76 6f 69 64 20 61 20 66 75 ed to avoid a fu
9d10: 6e 63 74 69 6f 6e 20 63 61 6c 6c 2e 0a 20 20 2a nction call.. *
9d20: 2f 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 2a /. nPayload = *
9d30: 70 49 74 65 72 3b 0a 20 20 69 66 28 20 6e 50 61 pIter;. if( nPa
9d40: 79 6c 6f 61 64 3e 3d 30 78 38 30 20 29 7b 0a 20 yload>=0x80 ){.
9d50: 20 20 20 75 38 20 2a 70 45 6e 64 20 3d 20 26 70 u8 *pEnd = &p
9d60: 49 74 65 72 5b 38 5d 3b 0a 20 20 20 20 6e 50 61 Iter[8];. nPa
9d70: 79 6c 6f 61 64 20 26 3d 20 30 78 37 66 3b 0a 20 yload &= 0x7f;.
9d80: 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 6e 50 61 do{. nPa
9d90: 79 6c 6f 61 64 20 3d 20 28 6e 50 61 79 6c 6f 61 yload = (nPayloa
9da0: 64 3c 3c 37 29 20 7c 20 28 2a 2b 2b 70 49 74 65 d<<7) | (*++pIte
9db0: 72 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 7d r & 0x7f);. }
9dc0: 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 29 3e while( (*pIter)>
9dd0: 3d 30 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 =0x80 && pIter<p
9de0: 45 6e 64 20 29 3b 0a 20 20 7d 0a 20 20 70 49 74 End );. }. pIt
9df0: 65 72 2b 2b 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 er++;.. /* The
9e00: 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f next block of co
9e10: 64 65 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 de is equivalent
9e20: 20 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 to:. **. **
9e30: 20 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 pIter += getV
9e40: 61 72 69 6e 74 28 70 49 74 65 72 2c 20 28 75 36 arint(pIter, (u6
9e50: 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 4*)&pInfo->nKey)
9e60: 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 ;. **. ** The
9e70: 63 6f 64 65 20 69 73 20 69 6e 6c 69 6e 65 64 20 code is inlined
9e80: 74 6f 20 61 76 6f 69 64 20 61 20 66 75 6e 63 74 to avoid a funct
9e90: 69 6f 6e 20 63 61 6c 6c 2e 0a 20 20 2a 2f 0a 20 ion call.. */.
9ea0: 20 69 4b 65 79 20 3d 20 2a 70 49 74 65 72 3b 0a iKey = *pIter;.
9eb0: 20 20 69 66 28 20 69 4b 65 79 3e 3d 30 78 38 30 if( iKey>=0x80
9ec0: 20 29 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 ){. u8 *pEnd
9ed0: 20 3d 20 26 70 49 74 65 72 5b 37 5d 3b 0a 20 20 = &pIter[7];.
9ee0: 20 20 69 4b 65 79 20 26 3d 20 30 78 37 66 3b 0a iKey &= 0x7f;.
9ef0: 20 20 20 20 77 68 69 6c 65 28 31 29 7b 0a 20 20 while(1){.
9f00: 20 20 20 20 69 4b 65 79 20 3d 20 28 69 4b 65 79 iKey = (iKey
9f10: 3c 3c 37 29 20 7c 20 28 2a 2b 2b 70 49 74 65 72 <<7) | (*++pIter
9f20: 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 20 20 & 0x7f);.
9f30: 69 66 28 20 28 2a 70 49 74 65 72 29 3c 30 78 38 if( (*pIter)<0x8
9f40: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 0 ) break;.
9f50: 20 69 66 28 20 70 49 74 65 72 3e 3d 70 45 6e 64 if( pIter>=pEnd
9f60: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4b 65 79 ){. iKey
9f70: 20 3d 20 28 69 4b 65 79 3c 3c 38 29 20 7c 20 2a = (iKey<<8) | *
9f80: 2b 2b 70 49 74 65 72 3b 0a 20 20 20 20 20 20 20 ++pIter;.
9f90: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
9fa0: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 49 74 65 }. }. pIte
9fb0: 72 2b 2b 3b 0a 0a 20 20 70 49 6e 66 6f 2d 3e 6e r++;.. pInfo->n
9fc0: 4b 65 79 20 3d 20 2a 28 69 36 34 2a 29 26 69 4b Key = *(i64*)&iK
9fd0: 65 79 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 ey;. pInfo->nPa
9fe0: 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c 6f 61 64 yload = nPayload
9ff0: 3b 0a 20 20 70 49 6e 66 6f 2d 3e 70 50 61 79 6c ;. pInfo->pPayl
a000: 6f 61 64 20 3d 20 70 49 74 65 72 3b 0a 20 20 74 oad = pIter;. t
a010: 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 estcase( nPayloa
a020: 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 d==pPage->maxLoc
a030: 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 al );. testcase
a040: 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 ( nPayload==pPag
a050: 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b e->maxLocal+1 );
a060: 0a 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 64 3c . if( nPayload<
a070: 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
a080: 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
a090: 69 73 20 74 68 65 20 28 65 61 73 79 29 20 63 6f is the (easy) co
a0a0: 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 mmon case where
a0b0: 74 68 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f the entire paylo
a0c0: 61 64 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f ad fits. ** o
a0d0: 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 n the local page
a0e0: 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 . No overflow i
a0f0: 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 s required..
a100: 2a 2f 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 */. pInfo->nS
a110: 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b ize = nPayload +
a120: 20 28 75 31 36 29 28 70 49 74 65 72 20 2d 20 70 (u16)(pIter - p
a130: 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 70 Cell);. if( p
a140: 49 6e 66 6f 2d 3e 6e 53 69 7a 65 3c 34 20 29 20 Info->nSize<4 )
a150: 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 34 pInfo->nSize = 4
a160: 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f ;. pInfo->nLo
a170: 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79 6c cal = (u16)nPayl
a180: 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 oad;. }else{.
a190: 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
a1a0: 41 64 6a 75 73 74 53 69 7a 65 46 6f 72 4f 76 65 AdjustSizeForOve
a1b0: 72 66 6c 6f 77 28 70 50 61 67 65 2c 20 70 43 65 rflow(pPage, pCe
a1c0: 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 7d 0a ll, pInfo);. }.
a1d0: 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 }.static void bt
a1e0: 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 49 reeParseCellPtrI
a1f0: 6e 64 65 78 28 0a 20 20 4d 65 6d 50 61 67 65 20 ndex(. MemPage
a200: 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 *pPage,
a210: 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 /* Page containi
a220: 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 ng the cell */.
a230: 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 u8 *pCell,
a240: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
a250: 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 ter to the cell
a260: 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 text. */. CellI
a270: 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 nfo *pInfo
a280: 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 /* Fill in th
a290: 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a is structure */.
a2a0: 29 7b 0a 20 20 75 38 20 2a 70 49 74 65 72 3b 20 ){. u8 *pIter;
a2b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a2c0: 46 6f 72 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 For scanning thr
a2d0: 6f 75 67 68 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 ough pCell */.
a2e0: 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 u32 nPayload;
a2f0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
a300: 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65 r of bytes of ce
a310: 6c 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 ll payload */..
a320: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
a330: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
a340: 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
a350: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
a360: 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 e->leaf==0 || pP
a370: 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a age->leaf==1 );.
a380: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
a390: 3e 69 6e 74 4b 65 79 4c 65 61 66 3d 3d 30 20 29 >intKeyLeaf==0 )
a3a0: 3b 0a 20 20 70 49 74 65 72 20 3d 20 70 43 65 6c ;. pIter = pCel
a3b0: 6c 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 l + pPage->child
a3c0: 50 74 72 53 69 7a 65 3b 0a 20 20 6e 50 61 79 6c PtrSize;. nPayl
a3d0: 6f 61 64 20 3d 20 2a 70 49 74 65 72 3b 0a 20 20 oad = *pIter;.
a3e0: 69 66 28 20 6e 50 61 79 6c 6f 61 64 3e 3d 30 78 if( nPayload>=0x
a3f0: 38 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 70 45 80 ){. u8 *pE
a400: 6e 64 20 3d 20 26 70 49 74 65 72 5b 38 5d 3b 0a nd = &pIter[8];.
a410: 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 26 3d 20 nPayload &=
a420: 30 78 37 66 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 0x7f;. do{.
a430: 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 28 nPayload = (
a440: 6e 50 61 79 6c 6f 61 64 3c 3c 37 29 20 7c 20 28 nPayload<<7) | (
a450: 2a 2b 2b 70 49 74 65 72 20 26 20 30 78 37 66 29 *++pIter & 0x7f)
a460: 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 2a 28 ;. }while( *(
a470: 70 49 74 65 72 29 3e 3d 30 78 38 30 20 26 26 20 pIter)>=0x80 &&
a480: 70 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 pIter<pEnd );.
a490: 7d 0a 20 20 70 49 74 65 72 2b 2b 3b 0a 20 20 70 }. pIter++;. p
a4a0: 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
a4b0: 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e yload;. pInfo->
a4c0: 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c nPayload = nPayl
a4d0: 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 70 50 oad;. pInfo->pP
a4e0: 61 79 6c 6f 61 64 20 3d 20 70 49 74 65 72 3b 0a ayload = pIter;.
a4f0: 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 testcase( nPay
a500: 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 load==pPage->max
a510: 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 Local );. testc
a520: 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 ase( nPayload==p
a530: 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 Page->maxLocal+1
a540: 20 29 3b 0a 20 20 69 66 28 20 6e 50 61 79 6c 6f );. if( nPaylo
a550: 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ad<=pPage->maxLo
a560: 63 61 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 cal ){. /* Th
a570: 69 73 20 69 73 20 74 68 65 20 28 65 61 73 79 29 is is the (easy)
a580: 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 common case whe
a590: 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 re the entire pa
a5a0: 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20 2a yload fits. *
a5b0: 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 * on the local p
a5c0: 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f age. No overflo
a5d0: 77 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 w is required..
a5e0: 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 66 6f 2d */. pInfo-
a5f0: 3e 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 >nSize = nPayloa
a600: 64 20 2b 20 28 75 31 36 29 28 70 49 74 65 72 20 d + (u16)(pIter
a610: 2d 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 - pCell);. if
a620: 28 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 3c 34 ( pInfo->nSize<4
a630: 20 29 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 ) pInfo->nSize
a640: 3d 20 34 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e = 4;. pInfo->
a650: 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 nLocal = (u16)nP
a660: 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 7b ayload;. }else{
a670: 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 . btreeParseC
a680: 65 6c 6c 41 64 6a 75 73 74 53 69 7a 65 46 6f 72 ellAdjustSizeFor
a690: 4f 76 65 72 66 6c 6f 77 28 70 50 61 67 65 2c 20 Overflow(pPage,
a6a0: 70 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 20 pCell, pInfo);.
a6b0: 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
a6c0: 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
a6d0: 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
a6e0: 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 e, /* Pa
a6f0: 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ge containing th
a700: 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 e cell */. int
a710: 69 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 iCell,
a720: 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 /* The cell
a730: 69 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 index. First ce
a740: 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c ll is 0 */. Cel
a750: 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 lInfo *pInfo
a760: 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 /* Fill in
a770: 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
a780: 2f 0a 29 7b 0a 20 20 70 50 61 67 65 2d 3e 78 50 /.){. pPage->xP
a790: 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 arseCell(pPage,
a7a0: 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
a7b0: 69 43 65 6c 6c 29 2c 20 70 49 6e 66 6f 29 3b 0a iCell), pInfo);.
a7c0: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
a7d0: 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 lowing routines
a7e0: 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 are implementati
a7f0: 6f 6e 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 ons of the MemPa
a800: 67 65 2e 78 43 65 6c 6c 53 69 7a 65 0a 2a 2a 20 ge.xCellSize.**
a810: 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f method..**.** Co
a820: 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 mpute the total
a830: 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
a840: 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 64 that a Cell need
a850: 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a s in the cell.**
a860: 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 68 data area of th
a870: 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 54 e btree-page. T
a880: 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 he return number
a890: 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 65 includes the ce
a8a0: 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 65 ll.** data heade
a8b0: 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20 r and the local
a8c0: 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74 payload, but not
a8d0: 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 any overflow pa
a8e0: 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61 ge or.** the spa
a8f0: 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ce used by the c
a900: 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ell pointer..**.
a910: 2a 2a 20 63 65 6c 6c 53 69 7a 65 50 74 72 4e 6f ** cellSizePtrNo
a920: 50 61 79 6c 6f 61 64 28 29 20 20 20 20 3d 3e 20 Payload() =>
a930: 20 20 74 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c table internal
a940: 20 6e 6f 64 65 73 0a 2a 2a 20 63 65 6c 6c 53 69 nodes.** cellSi
a950: 7a 65 50 74 72 28 29 20 20 20 20 20 20 20 20 20 zePtr()
a960: 20 20 20 20 3d 3e 20 20 20 61 6c 6c 20 69 6e 64 => all ind
a970: 65 78 20 6e 6f 64 65 73 20 26 20 74 61 62 6c 65 ex nodes & table
a980: 20 6c 65 61 66 20 6e 6f 64 65 73 0a 2a 2f 0a 73 leaf nodes.*/.s
a990: 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 tatic u16 cellSi
a9a0: 7a 65 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 zePtr(MemPage *p
a9b0: 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 Page, u8 *pCell)
a9c0: 7b 0a 20 20 75 38 20 2a 70 49 74 65 72 20 3d 20 {. u8 *pIter =
a9d0: 70 43 65 6c 6c 20 2b 20 70 50 61 67 65 2d 3e 63 pCell + pPage->c
a9e0: 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 2f 2a 20 hildPtrSize; /*
a9f0: 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 For looping over
aa00: 20 62 79 74 65 73 20 6f 66 20 70 43 65 6c 6c 20 bytes of pCell
aa10: 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 3b 20 20 */. u8 *pEnd;
aa20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aa30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
aa40: 20 45 6e 64 20 6d 61 72 6b 20 66 6f 72 20 61 20 End mark for a
aa50: 76 61 72 69 6e 74 20 2a 2f 0a 20 20 75 33 32 20 varint */. u32
aa60: 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 nSize;
aa70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aa80: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 76 61 6c /* Size val
aa90: 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a ue to return */.
aaa0: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
aab0: 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 61 EBUG. /* The va
aac0: 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
aad0: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 this function sh
aae0: 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74 ould always be t
aaf0: 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a 20 he same as. **
ab00: 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 the (CellInfo.nS
ab10: 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 6e 64 ize) value found
ab20: 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 6c 6c by doing a full
ab30: 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a 20 20 parse of the.
ab40: 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c 49 ** cell. If SQLI
ab50: 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 TE_DEBUG is defi
ab60: 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 ned, an assert()
ab70: 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f at the bottom o
ab80: 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 f. ** this func
ab90: 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 74 68 tion verifies th
aba0: 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 61 6e at this invarian
abb0: 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 65 t is not violate
abc0: 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f d. */. CellInfo
abd0: 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 70 50 debuginfo;. pP
abe0: 61 67 65 2d 3e 78 50 61 72 73 65 43 65 6c 6c 28 age->xParseCell(
abf0: 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 64 pPage, pCell, &d
ac00: 65 62 75 67 69 6e 66 6f 29 3b 0a 23 65 6e 64 69 ebuginfo);.#endi
ac10: 66 0a 0a 20 20 6e 53 69 7a 65 20 3d 20 2a 70 49 f.. nSize = *pI
ac20: 74 65 72 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 ter;. if( nSize
ac30: 3e 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 70 45 >=0x80 ){. pE
ac40: 6e 64 20 3d 20 26 70 49 74 65 72 5b 38 5d 3b 0a nd = &pIter[8];.
ac50: 20 20 20 20 6e 53 69 7a 65 20 26 3d 20 30 78 37 nSize &= 0x7
ac60: 66 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 f;. do{.
ac70: 20 6e 53 69 7a 65 20 3d 20 28 6e 53 69 7a 65 3c nSize = (nSize<
ac80: 3c 37 29 20 7c 20 28 2a 2b 2b 70 49 74 65 72 20 <7) | (*++pIter
ac90: 26 20 30 78 37 66 29 3b 0a 20 20 20 20 7d 77 68 & 0x7f);. }wh
aca0: 69 6c 65 28 20 2a 28 70 49 74 65 72 29 3e 3d 30 ile( *(pIter)>=0
acb0: 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e x80 && pIter<pEn
acc0: 64 20 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 72 d );. }. pIter
acd0: 2b 2b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d ++;. if( pPage-
ace0: 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 2f >intKey ){. /
acf0: 2a 20 70 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e * pIter now poin
ad00: 74 73 20 61 74 20 74 68 65 20 36 34 2d 62 69 74 ts at the 64-bit
ad10: 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c integer key val
ad20: 75 65 2c 20 61 20 76 61 72 69 61 62 6c 65 20 6c ue, a variable l
ad30: 65 6e 67 74 68 20 0a 20 20 20 20 2a 2a 20 69 6e ength . ** in
ad40: 74 65 67 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f teger. The follo
ad50: 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 wing block moves
ad60: 20 70 49 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 pIter to point
ad70: 61 74 20 74 68 65 20 66 69 72 73 74 20 62 79 74 at the first byt
ad80: 65 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 e. ** past th
ad90: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6b 65 79 e end of the key
ada0: 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 value. */. p
adb0: 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b End = &pIter[9];
adc0: 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 . while( (*pI
add0: 74 65 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 ter++)&0x80 && p
ade0: 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d Iter<pEnd );. }
adf0: 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 . testcase( nSi
ae00: 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ze==pPage->maxLo
ae10: 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 cal );. testcas
ae20: 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d e( nSize==pPage-
ae30: 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 >maxLocal+1 );.
ae40: 20 69 66 28 20 6e 53 69 7a 65 3c 3d 70 50 61 67 if( nSize<=pPag
ae50: 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 e->maxLocal ){.
ae60: 20 20 20 6e 53 69 7a 65 20 2b 3d 20 28 75 33 32 nSize += (u32
ae70: 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 )(pIter - pCell)
ae80: 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 7a 65 3c ;. if( nSize<
ae90: 34 20 29 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 4 ) nSize = 4;.
aea0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
aeb0: 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 minLocal = pPage
aec0: 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 ->minLocal;.
aed0: 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c nSize = minLocal
aee0: 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c + (nSize - minL
aef0: 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e ocal) % (pPage->
af00: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
af10: 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 - 4);. testca
af20: 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
af30: 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
af40: 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
af50: 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
af60: 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
af70: 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 nSize>pPage->max
af80: 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e Local ){. n
af90: 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b Size = minLocal;
afa0: 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 . }. nSize
afb0: 20 2b 3d 20 34 20 2b 20 28 75 31 36 29 28 70 49 += 4 + (u16)(pI
afc0: 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 20 20 ter - pCell);.
afd0: 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a }. assert( nSiz
afe0: 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 e==debuginfo.nSi
aff0: 7a 65 20 7c 7c 20 43 4f 52 52 55 50 54 5f 44 42 ze || CORRUPT_DB
b000: 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 31 );. return (u1
b010: 36 29 6e 53 69 7a 65 3b 0a 7d 0a 73 74 61 74 69 6)nSize;.}.stati
b020: 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 c u16 cellSizePt
b030: 72 4e 6f 50 61 79 6c 6f 61 64 28 4d 65 6d 50 61 rNoPayload(MemPa
b040: 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 ge *pPage, u8 *p
b050: 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 49 74 Cell){. u8 *pIt
b060: 65 72 20 3d 20 70 43 65 6c 6c 20 2b 20 34 3b 20 er = pCell + 4;
b070: 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f /* For looping o
b080: 76 65 72 20 62 79 74 65 73 20 6f 66 20 70 43 65 ver bytes of pCe
b090: 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 ll */. u8 *pEnd
b0a0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
b0b0: 2a 20 45 6e 64 20 6d 61 72 6b 20 66 6f 72 20 61 * End mark for a
b0c0: 20 76 61 72 69 6e 74 20 2a 2f 0a 0a 23 69 66 64 varint */..#ifd
b0d0: 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
b0e0: 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 /* The value r
b0f0: 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 eturned by this
b100: 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
b110: 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 73 61 always be the sa
b120: 6d 65 20 61 73 0a 20 20 2a 2a 20 74 68 65 20 28 me as. ** the (
b130: 43 65 6c 6c 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 CellInfo.nSize)
b140: 76 61 6c 75 65 20 66 6f 75 6e 64 20 62 79 20 64 value found by d
b150: 6f 69 6e 67 20 61 20 66 75 6c 6c 20 70 61 72 73 oing a full pars
b160: 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 65 e of the. ** ce
b170: 6c 6c 2e 20 49 66 20 53 51 4c 49 54 45 5f 44 45 ll. If SQLITE_DE
b180: 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2c 20 BUG is defined,
b190: 61 6e 20 61 73 73 65 72 74 28 29 20 61 74 20 74 an assert() at t
b1a0: 68 65 20 62 6f 74 74 6f 6d 20 6f 66 0a 20 20 2a he bottom of. *
b1b0: 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
b1c0: 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74 68 verifies that th
b1d0: 69 73 20 69 6e 76 61 72 69 61 6e 74 20 69 73 20 is invariant is
b1e0: 6e 6f 74 20 76 69 6f 6c 61 74 65 64 2e 20 2a 2f not violated. */
b1f0: 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 64 65 62 75 . CellInfo debu
b200: 67 69 6e 66 6f 3b 0a 20 20 70 50 61 67 65 2d 3e ginfo;. pPage->
b210: 78 50 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 xParseCell(pPage
b220: 2c 20 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 , pCell, &debugi
b230: 6e 66 6f 29 3b 0a 23 65 6c 73 65 0a 20 20 55 4e nfo);.#else. UN
b240: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
b250: 50 61 67 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 Page);.#endif..
b260: 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
b270: 63 68 69 6c 64 50 74 72 53 69 7a 65 3d 3d 34 20 childPtrSize==4
b280: 29 3b 0a 20 20 70 45 6e 64 20 3d 20 70 49 74 65 );. pEnd = pIte
b290: 72 20 2b 20 39 3b 0a 20 20 77 68 69 6c 65 28 20 r + 9;. while(
b2a0: 28 2a 70 49 74 65 72 2b 2b 29 26 30 78 38 30 20 (*pIter++)&0x80
b2b0: 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20 29 3b && pIter<pEnd );
b2c0: 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67 . assert( debug
b2d0: 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 28 75 31 36 info.nSize==(u16
b2e0: 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 )(pIter - pCell)
b2f0: 20 7c 7c 20 43 4f 52 52 55 50 54 5f 44 42 20 29 || CORRUPT_DB )
b300: 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 31 36 29 ;. return (u16)
b310: 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b (pIter - pCell);
b320: 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 .}...#ifdef SQLI
b330: 54 45 5f 44 45 42 55 47 0a 2f 2a 20 54 68 69 73 TE_DEBUG./* This
b340: 20 76 61 72 69 61 74 69 6f 6e 20 6f 6e 20 63 65 variation on ce
b350: 6c 6c 53 69 7a 65 50 74 72 28 29 20 69 73 20 75 llSizePtr() is u
b360: 73 65 64 20 69 6e 73 69 64 65 20 6f 66 20 61 73 sed inside of as
b370: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
b380: 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 2a 2f 0a 73 74 s.** only. */.st
b390: 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a atic u16 cellSiz
b3a0: 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
b3b0: 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 , int iCell){.
b3c0: 72 65 74 75 72 6e 20 70 50 61 67 65 2d 3e 78 43 return pPage->xC
b3d0: 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 66 ellSize(pPage, f
b3e0: 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 indCell(pPage, i
b3f0: 43 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 Cell));.}.#endif
b400: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
b410: 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
b420: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 ./*.** The cell
b430: 70 43 65 6c 6c 20 69 73 20 63 75 72 72 65 6e 74 pCell is current
b440: 6c 79 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 ly part of page
b450: 70 53 72 63 20 62 75 74 20 77 69 6c 6c 20 75 6c pSrc but will ul
b460: 74 69 6d 61 74 65 6c 79 20 62 65 20 70 61 72 74 timately be part
b470: 0a 2a 2a 20 6f 66 20 70 50 61 67 65 2e 20 20 28 .** of pPage. (
b480: 70 53 72 63 20 61 6e 64 20 70 50 61 67 65 72 20 pSrc and pPager
b490: 61 72 65 20 6f 66 74 65 6e 20 74 68 65 20 73 61 are often the sa
b4a0: 6d 65 2e 29 20 20 49 66 20 70 43 65 6c 6c 20 63 me.) If pCell c
b4b0: 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 70 6f 69 ontains a.** poi
b4c0: 6e 74 65 72 20 74 6f 20 61 6e 20 6f 76 65 72 66 nter to an overf
b4d0: 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 low page, insert
b4e0: 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 an entry into t
b4f0: 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 he pointer-map f
b500: 6f 72 0a 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c or.** the overfl
b510: 6f 77 20 70 61 67 65 20 74 68 61 74 20 77 69 6c ow page that wil
b520: 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72 l be valid after
b530: 20 70 43 65 6c 6c 20 68 61 73 20 62 65 65 6e 20 pCell has been
b540: 6d 6f 76 65 64 20 74 6f 20 70 50 61 67 65 2e 0a moved to pPage..
b550: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
b560: 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
b570: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
b580: 4d 65 6d 50 61 67 65 20 2a 70 53 72 63 2c 20 75 MemPage *pSrc, u
b590: 38 20 2a 70 43 65 6c 6c 2c 69 6e 74 20 2a 70 52 8 *pCell,int *pR
b5a0: 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 C){. CellInfo i
b5b0: 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 nfo;. if( *pRC
b5c0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
b5d0: 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a rt( pCell!=0 );.
b5e0: 20 20 70 50 61 67 65 2d 3e 78 50 61 72 73 65 43 pPage->xParseC
b5f0: 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c ell(pPage, pCell
b600: 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 , &info);. if(
b610: 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3c 69 6e 66 6f info.nLocal<info
b620: 2e 6e 50 61 79 6c 6f 61 64 20 29 7b 0a 20 20 20 .nPayload ){.
b630: 20 50 67 6e 6f 20 6f 76 66 6c 3b 0a 20 20 20 20 Pgno ovfl;.
b640: 69 66 28 20 53 51 4c 49 54 45 5f 57 49 54 48 49 if( SQLITE_WITHI
b650: 4e 28 70 53 72 63 2d 3e 61 44 61 74 61 45 6e 64 N(pSrc->aDataEnd
b660: 2c 20 70 43 65 6c 6c 2c 20 70 43 65 6c 6c 2b 69 , pCell, pCell+i
b670: 6e 66 6f 2e 6e 4c 6f 63 61 6c 29 20 29 7b 0a 20 nfo.nLocal) ){.
b680: 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
b690: 53 72 63 21 3d 70 50 61 67 65 20 29 3b 0a 20 20 Src!=pPage );.
b6a0: 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 *pRC = SQLIT
b6b0: 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
b6c0: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
b6d0: 20 20 7d 0a 20 20 20 20 6f 76 66 6c 20 3d 20 67 }. ovfl = g
b6e0: 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
b6f0: 6e 66 6f 2e 6e 53 69 7a 65 2d 34 5d 29 3b 0a 20 nfo.nSize-4]);.
b700: 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 ptrmapPut(pPa
b710: 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 ge->pBt, ovfl, P
b720: 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c TRMAP_OVERFLOW1,
b730: 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 pPage->pgno, pR
b740: 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 C);. }.}.#endif
b750: 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d .../*.** Defragm
b760: 65 6e 74 20 74 68 65 20 70 61 67 65 20 67 69 76 ent the page giv
b770: 65 6e 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 en. This routine
b780: 20 72 65 6f 72 67 61 6e 69 7a 65 73 20 63 65 6c reorganizes cel
b790: 6c 73 20 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a ls within the.**
b7a0: 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 74 68 page so that th
b7b0: 65 72 65 20 61 72 65 20 6e 6f 20 66 72 65 65 2d ere are no free-
b7c0: 62 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 72 blocks on the fr
b7d0: 65 65 2d 62 6c 6f 63 6b 20 6c 69 73 74 2e 0a 2a ee-block list..*
b7e0: 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 6e *.** Parameter n
b7f0: 4d 61 78 46 72 61 67 20 69 73 20 74 68 65 20 6d MaxFrag is the m
b800: 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 aximum amount of
b810: 20 66 72 61 67 6d 65 6e 74 65 64 20 73 70 61 63 fragmented spac
b820: 65 20 74 68 61 74 20 6d 61 79 20 62 65 0a 2a 2a e that may be.**
b830: 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 present in the
b840: 70 61 67 65 20 61 66 74 65 72 20 74 68 69 73 20 page after this
b850: 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e routine returns.
b860: 0a 2a 2a 0a 2a 2a 20 45 56 49 44 45 4e 43 45 2d .**.** EVIDENCE-
b870: 4f 46 3a 20 52 2d 34 34 35 38 32 2d 36 30 31 33 OF: R-44582-6013
b880: 38 20 53 51 4c 69 74 65 20 6d 61 79 20 66 72 6f 8 SQLite may fro
b890: 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 20 72 m time to time r
b8a0: 65 6f 72 67 61 6e 69 7a 65 20 61 0a 2a 2a 20 62 eorganize a.** b
b8b0: 2d 74 72 65 65 20 70 61 67 65 20 73 6f 20 74 68 -tree page so th
b8c0: 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 at there are no
b8d0: 66 72 65 65 62 6c 6f 63 6b 73 20 6f 72 20 66 72 freeblocks or fr
b8e0: 61 67 6d 65 6e 74 20 62 79 74 65 73 2c 20 61 6c agment bytes, al
b8f0: 6c 0a 2a 2a 20 75 6e 75 73 65 64 20 62 79 74 65 l.** unused byte
b900: 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 s are contained
b910: 69 6e 20 74 68 65 20 75 6e 61 6c 6c 6f 63 61 74 in the unallocat
b920: 65 64 20 73 70 61 63 65 20 72 65 67 69 6f 6e 2c ed space region,
b930: 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20 63 65 6c 6c and all.** cell
b940: 73 20 61 72 65 20 70 61 63 6b 65 64 20 74 69 67 s are packed tig
b950: 68 74 6c 79 20 61 74 20 74 68 65 20 65 6e 64 20 htly at the end
b960: 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a of the page..*/.
b970: 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 72 61 static int defra
b980: 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 gmentPage(MemPag
b990: 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 4d e *pPage, int nM
b9a0: 61 78 46 72 61 67 29 7b 0a 20 20 69 6e 74 20 69 axFrag){. int i
b9b0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b9c0: 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
b9d0: 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 unter */. int p
b9e0: 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
b9f0: 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
ba00: 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 65 6c of the i-th cel
ba10: 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 l */. int hdr;
ba20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ba30: 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 /* Offset to t
ba40: 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a he page header *
ba50: 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 /. int size;
ba60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ba70: 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c * Size of a cell
ba80: 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 */. int usable
ba90: 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
baa0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
bab0: 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20 able bytes on a
bac0: 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 page */. int ce
bad0: 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 llOffset;
bae0: 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
baf0: 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 o the cell point
bb00: 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e er array */. in
bb10: 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 t cbrk;
bb20: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
bb30: 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 et to the cell c
bb40: 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
bb50: 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 int nCell;
bb60: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
bb70: 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
bb80: 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 n the page */.
bb90: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 unsigned char *d
bba0: 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 ata; /* Th
bbb0: 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 e page data */.
bbc0: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
bbd0: 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 temp; /* T
bbe0: 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c emp area for cel
bbf0: 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 75 l content */. u
bc00: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 72 nsigned char *sr
bc10: 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 c; /* Sou
bc20: 72 63 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 2a rce of content *
bc30: 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 /. int iCellFir
bc40: 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f st; /
bc50: 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c * First allowabl
bc60: 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a e cell index */.
bc70: 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b int iCellLast;
bc80: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bc90: 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 Last possible ce
bca0: 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 61 ll index */.. a
bcb0: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
bcc0: 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
bcd0: 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
bce0: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
bcf0: 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 e->pBt!=0 );. a
bd00: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 ssert( pPage->pB
bd10: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d t->usableSize <=
bd20: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
bd30: 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 _SIZE );. asser
bd40: 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
bd50: 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 low==0 );. asse
bd60: 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
bd70: 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
bd80: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 t->mutex) );. t
bd90: 65 6d 70 20 3d 20 30 3b 0a 20 20 73 72 63 20 3d emp = 0;. src =
bda0: 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
bdb0: 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 Data;. hdr = pP
bdc0: 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
bdd0: 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 cellOffset = p
bde0: 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
bdf0: 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 ;. nCell = pPag
be00: 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 e->nCell;. asse
be10: 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 rt( nCell==get2b
be20: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
be30: 29 20 29 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 ) );. iCellFirs
be40: 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b t = cellOffset +
be50: 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 75 73 61 62 2*nCell;. usab
be60: 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e leSize = pPage->
be70: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
be80: 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 .. /* This bloc
be90: 6b 20 68 61 6e 64 6c 65 73 20 70 61 67 65 73 20 k handles pages
bea0: 77 69 74 68 20 74 77 6f 20 6f 72 20 66 65 77 65 with two or fewe
beb0: 72 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 6e r free blocks an
bec0: 64 20 6e 4d 61 78 46 72 61 67 0a 20 20 2a 2a 20 d nMaxFrag. **
bed0: 6f 72 20 66 65 77 65 72 20 66 72 61 67 6d 65 6e or fewer fragmen
bee0: 74 65 64 20 62 79 74 65 73 2e 20 49 6e 20 74 68 ted bytes. In th
bef0: 69 73 20 63 61 73 65 20 69 74 20 69 73 20 66 61 is case it is fa
bf00: 73 74 65 72 20 74 6f 20 6d 6f 76 65 20 74 68 65 ster to move the
bf10: 0a 20 20 2a 2a 20 74 77 6f 20 28 6f 72 20 6f 6e . ** two (or on
bf20: 65 29 20 62 6c 6f 63 6b 73 20 6f 66 20 63 65 6c e) blocks of cel
bf30: 6c 73 20 75 73 69 6e 67 20 6d 65 6d 6d 6f 76 65 ls using memmove
bf40: 28 29 20 61 6e 64 20 61 64 64 20 74 68 65 20 72 () and add the r
bf50: 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 6f 66 66 equired. ** off
bf60: 73 65 74 73 20 74 6f 20 65 61 63 68 20 70 6f 69 sets to each poi
bf70: 6e 74 65 72 20 69 6e 20 74 68 65 20 63 65 6c 6c nter in the cell
bf80: 2d 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 74 -pointer array t
bf90: 68 61 6e 20 69 74 20 69 73 20 74 6f 20 0a 20 20 han it is to .
bfa0: 2a 2a 20 72 65 63 6f 6e 73 74 72 75 63 74 20 74 ** reconstruct t
bfb0: 68 65 20 65 6e 74 69 72 65 20 70 61 67 65 2e 20 he entire page.
bfc0: 20 2a 2f 0a 20 20 69 66 28 20 28 69 6e 74 29 64 */. if( (int)d
bfd0: 61 74 61 5b 68 64 72 2b 37 5d 3c 3d 6e 4d 61 78 ata[hdr+7]<=nMax
bfe0: 46 72 61 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 Frag ){. int
bff0: 69 46 72 65 65 20 3d 20 67 65 74 32 62 79 74 65 iFree = get2byte
c000: 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a (&data[hdr+1]);.
c010: 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 . /* If the i
c020: 6e 69 74 69 61 6c 20 66 72 65 65 62 6c 6f 63 6b nitial freeblock
c030: 20 6f 66 66 73 65 74 20 77 65 72 65 20 6f 75 74 offset were out
c040: 20 6f 66 20 62 6f 75 6e 64 73 2c 20 74 68 61 74 of bounds, that
c050: 20 77 6f 75 6c 64 20 68 61 76 65 0a 20 20 20 20 would have.
c060: 2a 2a 20 62 65 65 6e 20 64 65 74 65 63 74 65 64 ** been detected
c070: 20 62 79 20 62 74 72 65 65 43 6f 6d 70 75 74 65 by btreeCompute
c080: 46 72 65 65 53 70 61 63 65 28 29 20 77 68 65 6e FreeSpace() when
c090: 20 69 74 20 77 61 73 20 63 6f 6d 70 75 74 69 6e it was computin
c0a0: 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6d g the. ** num
c0b0: 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 ber of free byte
c0c0: 73 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 2a s on the page. *
c0d0: 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 46 /. assert( iF
c0e0: 72 65 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d ree<=usableSize-
c0f0: 34 20 29 3b 0a 20 20 20 20 69 66 28 20 69 46 72 4 );. if( iFr
c100: 65 65 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ee ){. int
c110: 69 46 72 65 65 32 20 3d 20 67 65 74 32 62 79 74 iFree2 = get2byt
c120: 65 28 26 64 61 74 61 5b 69 46 72 65 65 5d 29 3b e(&data[iFree]);
c130: 0a 20 20 20 20 20 20 69 66 28 20 69 46 72 65 65 . if( iFree
c140: 32 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 2>usableSize-4 )
c150: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
c160: 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 ORRUPT_PAGE(pPag
c170: 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d e);. if( 0=
c180: 3d 69 46 72 65 65 32 20 7c 7c 20 28 64 61 74 61 =iFree2 || (data
c190: 5b 69 46 72 65 65 32 5d 3d 3d 30 20 26 26 20 64 [iFree2]==0 && d
c1a0: 61 74 61 5b 69 46 72 65 65 32 2b 31 5d 3d 3d 30 ata[iFree2+1]==0
c1b0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20 ) ){. u8
c1c0: 2a 70 45 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 *pEnd = &data[ce
c1d0: 6c 6c 4f 66 66 73 65 74 20 2b 20 6e 43 65 6c 6c llOffset + nCell
c1e0: 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 75 38 20 *2];. u8
c1f0: 2a 70 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 *pAddr;.
c200: 69 6e 74 20 73 7a 32 20 3d 20 30 3b 0a 20 20 20 int sz2 = 0;.
c210: 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 67 65 int sz = ge
c220: 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 46 72 t2byte(&data[iFr
c230: 65 65 2b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 ee+2]);.
c240: 69 6e 74 20 74 6f 70 20 3d 20 67 65 74 32 62 79 int top = get2by
c250: 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
c260: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 6f ;. if( to
c270: 70 3e 3d 69 46 72 65 65 20 29 7b 0a 20 20 20 20 p>=iFree ){.
c280: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
c290: 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 ITE_CORRUPT_PAGE
c2a0: 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
c2b0: 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 }. if( i
c2c0: 46 72 65 65 32 20 29 7b 0a 20 20 20 20 20 20 20 Free2 ){.
c2d0: 20 20 20 69 66 28 20 69 46 72 65 65 2b 73 7a 3e if( iFree+sz>
c2e0: 69 46 72 65 65 32 20 29 20 72 65 74 75 72 6e 20 iFree2 ) return
c2f0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
c300: 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 20 20 AGE(pPage);.
c310: 20 20 20 20 20 20 73 7a 32 20 3d 20 67 65 74 32 sz2 = get2
c320: 62 79 74 65 28 26 64 61 74 61 5b 69 46 72 65 65 byte(&data[iFree
c330: 32 2b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 2+2]);.
c340: 20 69 66 28 20 69 46 72 65 65 32 2b 73 7a 32 20 if( iFree2+sz2
c350: 3e 20 75 73 61 62 6c 65 53 69 7a 65 20 29 20 72 > usableSize ) r
c360: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
c370: 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 RUPT_PAGE(pPage)
c380: 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6d ;. memm
c390: 6f 76 65 28 26 64 61 74 61 5b 69 46 72 65 65 2b ove(&data[iFree+
c3a0: 73 7a 2b 73 7a 32 5d 2c 20 26 64 61 74 61 5b 69 sz+sz2], &data[i
c3b0: 46 72 65 65 2b 73 7a 5d 2c 20 69 46 72 65 65 32 Free+sz], iFree2
c3c0: 2d 28 69 46 72 65 65 2b 73 7a 29 29 3b 0a 20 20 -(iFree+sz));.
c3d0: 20 20 20 20 20 20 20 20 73 7a 20 2b 3d 20 73 7a sz += sz
c3e0: 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 2;. }.
c3f0: 20 20 20 20 20 63 62 72 6b 20 3d 20 74 6f 70 2b cbrk = top+
c400: 73 7a 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 sz;. asse
c410: 72 74 28 20 63 62 72 6b 2b 28 69 46 72 65 65 2d rt( cbrk+(iFree-
c420: 74 6f 70 29 20 3c 3d 20 75 73 61 62 6c 65 53 69 top) <= usableSi
c430: 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 ze );. me
c440: 6d 6d 6f 76 65 28 26 64 61 74 61 5b 63 62 72 6b mmove(&data[cbrk
c450: 5d 2c 20 26 64 61 74 61 5b 74 6f 70 5d 2c 20 69 ], &data[top], i
c460: 46 72 65 65 2d 74 6f 70 29 3b 0a 20 20 20 20 20 Free-top);.
c470: 20 20 20 66 6f 72 28 70 41 64 64 72 3d 26 64 61 for(pAddr=&da
c480: 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b 20 ta[cellOffset];
c490: 70 41 64 64 72 3c 70 45 6e 64 3b 20 70 41 64 64 pAddr<pEnd; pAdd
c4a0: 72 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 20 r+=2){.
c4b0: 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 pc = get2byte(p
c4c0: 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 Addr);.
c4d0: 20 69 66 28 20 70 63 3c 69 46 72 65 65 20 29 7b if( pc<iFree ){
c4e0: 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c put2byte(pAddr,
c4f0: 20 70 63 2b 73 7a 29 3b 20 7d 0a 20 20 20 20 20 pc+sz); }.
c500: 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 70 63 else if( pc
c510: 3c 69 46 72 65 65 32 20 29 7b 20 70 75 74 32 62 <iFree2 ){ put2b
c520: 79 74 65 28 70 41 64 64 72 2c 20 70 63 2b 73 7a yte(pAddr, pc+sz
c530: 32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 2); }. }.
c540: 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 66 goto def
c550: 72 61 67 6d 65 6e 74 5f 6f 75 74 3b 0a 20 20 20 ragment_out;.
c560: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
c570: 20 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 cbrk = usableS
c580: 69 7a 65 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 ize;. iCellLast
c590: 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 = usableSize -
c5a0: 34 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4;. for(i=0; i<
c5b0: 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 nCell; i++){.
c5c0: 20 75 38 20 2a 70 41 64 64 72 3b 20 20 20 20 20 u8 *pAddr;
c5d0: 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 65 6c 6c /* The i-th cell
c5e0: 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 pointer */.
c5f0: 70 41 64 64 72 20 3d 20 26 64 61 74 61 5b 63 65 pAddr = &data[ce
c600: 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b llOffset + i*2];
c610: 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 . pc = get2by
c620: 74 65 28 70 41 64 64 72 29 3b 0a 20 20 20 20 74 te(pAddr);. t
c630: 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 estcase( pc==iCe
c640: 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 llFirst );. t
c650: 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 estcase( pc==iCe
c660: 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 2f 2a llLast );. /*
c670: 20 54 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e These condition
c680: 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 s have already b
c690: 65 65 6e 20 76 65 72 69 66 69 65 64 20 69 6e 20 een verified in
c6a0: 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 0a btreeInitPage().
c6b0: 20 20 20 20 2a 2a 20 69 66 20 50 52 41 47 4d 41 ** if PRAGMA
c6c0: 20 63 65 6c 6c 5f 73 69 7a 65 5f 63 68 65 63 6b cell_size_check
c6d0: 3d 4f 4e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 =ON.. */.
c6e0: 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 if( pc<iCellFirs
c6f0: 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 t || pc>iCellLas
c700: 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t ){. retur
c710: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
c720: 5f 50 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 _PAGE(pPage);.
c730: 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
c740: 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 26 pc>=iCellFirst &
c750: 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 & pc<=iCellLast
c760: 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 70 50 );. size = pP
c770: 61 67 65 2d 3e 78 43 65 6c 6c 53 69 7a 65 28 70 age->xCellSize(p
c780: 50 61 67 65 2c 20 26 73 72 63 5b 70 63 5d 29 3b Page, &src[pc]);
c790: 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a . cbrk -= siz
c7a0: 65 3b 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c e;. if( cbrk<
c7b0: 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 iCellFirst || pc
c7c0: 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 +size>usableSize
c7d0: 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
c7e0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
c7f0: 50 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 20 PAGE(pPage);.
c800: 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 }. assert( c
c810: 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 brk+size<=usable
c820: 53 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 Size && cbrk>=iC
c830: 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ellFirst );.
c840: 74 65 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 testcase( cbrk+s
c850: 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize==usableSize
c860: 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
c870: 20 70 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 pc+size==usable
c880: 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 75 74 32 Size );. put2
c890: 62 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b byte(pAddr, cbrk
c8a0: 29 3b 0a 20 20 20 20 69 66 28 20 74 65 6d 70 3d );. if( temp=
c8b0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =0 ){. int
c8c0: 78 3b 0a 20 20 20 20 20 20 69 66 28 20 63 62 72 x;. if( cbr
c8d0: 6b 3d 3d 70 63 20 29 20 63 6f 6e 74 69 6e 75 65 k==pc ) continue
c8e0: 3b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 73 ;. temp = s
c8f0: 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 qlite3PagerTempS
c900: 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d pace(pPage->pBt-
c910: 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 >pPager);.
c920: 78 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 x = get2byte(&da
c930: 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 ta[hdr+5]);.
c940: 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 78 memcpy(&temp[x
c950: 5d 2c 20 26 64 61 74 61 5b 78 5d 2c 20 28 63 62 ], &data[x], (cb
c960: 72 6b 2b 73 69 7a 65 29 20 2d 20 78 29 3b 0a 20 rk+size) - x);.
c970: 20 20 20 20 20 73 72 63 20 3d 20 74 65 6d 70 3b src = temp;
c980: 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 . }. memcp
c990: 79 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 y(&data[cbrk], &
c9a0: 73 72 63 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a src[pc], size);.
c9b0: 20 20 7d 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 }. data[hdr+7
c9c0: 5d 20 3d 20 30 3b 0a 0a 20 64 65 66 72 61 67 6d ] = 0;.. defragm
c9d0: 65 6e 74 5f 6f 75 74 3a 0a 20 20 61 73 73 65 72 ent_out:. asser
c9e0: 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e t( pPage->nFree>
c9f0: 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 61 74 61 =0 );. if( data
ca00: 5b 68 64 72 2b 37 5d 2b 63 62 72 6b 2d 69 43 65 [hdr+7]+cbrk-iCe
ca10: 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 65 2d 3e llFirst!=pPage->
ca20: 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 72 65 74 nFree ){. ret
ca30: 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
ca40: 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 3b 0a PT_PAGE(pPage);.
ca50: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 62 }. assert( cb
ca60: 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 rk>=iCellFirst )
ca70: 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
ca80: 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 ta[hdr+5], cbrk)
ca90: 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 ;. data[hdr+1]
caa0: 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b = 0;. data[hdr+
cab0: 32 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 2] = 0;. memset
cac0: 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73 (&data[iCellFirs
cad0: 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c t], 0, cbrk-iCel
cae0: 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72 lFirst);. asser
caf0: 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
cb00: 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
cb10: 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
cb20: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
cb30: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 ;.}../*.** Searc
cb40: 68 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 h the free-list
cb50: 6f 6e 20 70 61 67 65 20 70 50 67 20 66 6f 72 20 on page pPg for
cb60: 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 61 space to store a
cb70: 20 63 65 6c 6c 20 6e 42 79 74 65 20 62 79 74 65 cell nByte byte
cb80: 73 20 69 6e 0a 2a 2a 20 73 69 7a 65 2e 20 49 66 s in.** size. If
cb90: 20 6f 6e 65 20 63 61 6e 20 62 65 20 66 6f 75 6e one can be foun
cba0: 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e d, return a poin
cbb0: 74 65 72 20 74 6f 20 74 68 65 20 73 70 61 63 65 ter to the space
cbc0: 20 61 6e 64 20 72 65 6d 6f 76 65 20 69 74 0a 2a and remove it.*
cbd0: 2a 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d * from the free-
cbe0: 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e list..**.** If n
cbf0: 6f 20 73 75 69 74 61 62 6c 65 20 73 70 61 63 65 o suitable space
cc00: 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 6f 6e can be found on
cc10: 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 the free-list,
cc20: 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a return NULL..**.
cc30: 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
cc40: 20 6d 61 79 20 64 65 74 65 63 74 20 63 6f 72 72 may detect corr
cc50: 75 70 74 69 6f 6e 20 77 69 74 68 69 6e 20 70 50 uption within pP
cc60: 67 2e 20 20 49 66 20 63 6f 72 72 75 70 74 69 6f g. If corruptio
cc70: 6e 20 69 73 0a 2a 2a 20 64 65 74 65 63 74 65 64 n is.** detected
cc80: 20 74 68 65 6e 20 2a 70 52 63 20 69 73 20 73 65 then *pRc is se
cc90: 74 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 52 52 t to SQLITE_CORR
cca0: 55 50 54 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 UPT and NULL is
ccb0: 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
ccc0: 53 6c 6f 74 73 20 6f 6e 20 74 68 65 20 66 72 65 Slots on the fre
ccd0: 65 20 6c 69 73 74 20 74 68 61 74 20 61 72 65 20 e list that are
cce0: 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 33 20 between 1 and 3
ccf0: 62 79 74 65 73 20 6c 61 72 67 65 72 20 74 68 61 bytes larger tha
cd00: 6e 20 6e 42 79 74 65 0a 2a 2a 20 77 69 6c 6c 20 n nByte.** will
cd10: 62 65 20 69 67 6e 6f 72 65 64 20 69 66 20 61 64 be ignored if ad
cd20: 64 69 6e 67 20 74 68 65 20 65 78 74 72 61 20 73 ding the extra s
cd30: 70 61 63 65 20 74 6f 20 74 68 65 20 66 72 61 67 pace to the frag
cd40: 6d 65 6e 74 61 74 69 6f 6e 20 63 6f 75 6e 74 0a mentation count.
cd50: 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 66 72 ** causes the fr
cd60: 61 67 6d 65 6e 74 61 74 69 6f 6e 20 63 6f 75 6e agmentation coun
cd70: 74 20 74 6f 20 65 78 63 65 65 64 20 36 30 2e 0a t to exceed 60..
cd80: 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a 70 61 */.static u8 *pa
cd90: 67 65 46 69 6e 64 53 6c 6f 74 28 4d 65 6d 50 61 geFindSlot(MemPa
cda0: 67 65 20 2a 70 50 67 2c 20 69 6e 74 20 6e 42 79 ge *pPg, int nBy
cdb0: 74 65 2c 20 69 6e 74 20 2a 70 52 63 29 7b 0a 20 te, int *pRc){.
cdc0: 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d const int hdr =
cdd0: 20 70 50 67 2d 3e 68 64 72 4f 66 66 73 65 74 3b pPg->hdrOffset;
cde0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
cdf0: 66 66 73 65 74 20 74 6f 20 70 61 67 65 20 68 65 ffset to page he
ce00: 61 64 65 72 20 2a 2f 0a 20 20 75 38 20 2a 20 63 ader */. u8 * c
ce10: 6f 6e 73 74 20 61 44 61 74 61 20 3d 20 70 50 67 onst aData = pPg
ce20: 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 ->aData;
ce30: 20 20 20 20 20 2f 2a 20 50 61 67 65 20 64 61 74 /* Page dat
ce40: 61 20 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64 72 a */. int iAddr
ce50: 20 3d 20 68 64 72 20 2b 20 31 3b 20 20 20 20 20 = hdr + 1;
ce60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce70: 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
ce80: 70 74 72 20 74 6f 20 70 63 20 2a 2f 0a 20 20 69 ptr to pc */. i
ce90: 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 nt pc = get2byte
cea0: 28 26 61 44 61 74 61 5b 69 41 64 64 72 5d 29 3b (&aData[iAddr]);
ceb0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
cec0: 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 20 73 ress of a free s
ced0: 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 78 3b 20 lot */. int x;
cee0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cef0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cf00: 20 20 20 20 2f 2a 20 45 78 63 65 73 73 20 73 69 /* Excess si
cf10: 7a 65 20 6f 66 20 74 68 65 20 73 6c 6f 74 20 2a ze of the slot *
cf20: 2f 0a 20 20 69 6e 74 20 6d 61 78 50 43 20 3d 20 /. int maxPC =
cf30: 70 50 67 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 pPg->pBt->usable
cf40: 53 69 7a 65 20 2d 20 6e 42 79 74 65 3b 20 20 2f Size - nByte; /
cf50: 2a 20 4d 61 78 20 61 64 64 72 65 73 73 20 66 6f * Max address fo
cf60: 72 20 61 20 75 73 61 62 6c 65 20 73 6c 6f 74 20 r a usable slot
cf70: 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 */. int size;
cf80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cf90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cfa0: 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 66 /* Size of the f
cfb0: 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 0a 20 20 61 ree slot */.. a
cfc0: 73 73 65 72 74 28 20 70 63 3e 30 20 29 3b 0a 20 ssert( pc>0 );.
cfd0: 20 77 68 69 6c 65 28 20 70 63 3c 3d 6d 61 78 50 while( pc<=maxP
cfe0: 43 20 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44 C ){. /* EVID
cff0: 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 32 37 31 30 ENCE-OF: R-22710
d000: 2d 35 33 33 32 38 20 54 68 65 20 74 68 69 72 64 -53328 The third
d010: 20 61 6e 64 20 66 6f 75 72 74 68 20 62 79 74 65 and fourth byte
d020: 73 20 6f 66 20 65 61 63 68 0a 20 20 20 20 2a 2a s of each. **
d030: 20 66 72 65 65 62 6c 6f 63 6b 20 66 6f 72 6d 20 freeblock form
d040: 61 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 a big-endian int
d050: 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
d060: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 72 e size of the fr
d070: 65 65 62 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 69 eeblock. ** i
d080: 6e 20 62 79 74 65 73 2c 20 69 6e 63 6c 75 64 69 n bytes, includi
d090: 6e 67 20 74 68 65 20 34 2d 62 79 74 65 20 68 65 ng the 4-byte he
d0a0: 61 64 65 72 2e 20 2a 2f 0a 20 20 20 20 73 69 7a ader. */. siz
d0b0: 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 61 44 e = get2byte(&aD
d0c0: 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 ata[pc+2]);.
d0d0: 69 66 28 20 28 78 20 3d 20 73 69 7a 65 20 2d 20 if( (x = size -
d0e0: 6e 42 79 74 65 29 3e 3d 30 20 29 7b 0a 20 20 20 nByte)>=0 ){.
d0f0: 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d testcase( x==
d100: 34 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 4 );. testc
d110: 61 73 65 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 ase( x==3 );.
d120: 20 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 if( x<4 ){.
d130: 20 20 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 /* EVIDENC
d140: 45 2d 4f 46 3a 20 52 2d 31 31 34 39 38 2d 35 38 E-OF: R-11498-58
d150: 30 32 32 20 49 6e 20 61 20 77 65 6c 6c 2d 66 6f 022 In a well-fo
d160: 72 6d 65 64 20 62 2d 74 72 65 65 20 70 61 67 65 rmed b-tree page
d170: 2c 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 20 20 , the total.
d180: 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 ** number of
d190: 20 62 79 74 65 73 20 69 6e 20 66 72 61 67 6d 65 bytes in fragme
d1a0: 6e 74 73 20 6d 61 79 20 6e 6f 74 20 65 78 63 65 nts may not exce
d1b0: 65 64 20 36 30 2e 20 2a 2f 0a 20 20 20 20 20 20 ed 60. */.
d1c0: 20 20 69 66 28 20 61 44 61 74 61 5b 68 64 72 2b if( aData[hdr+
d1d0: 37 5d 3e 35 37 20 29 20 72 65 74 75 72 6e 20 30 7]>57 ) return 0
d1e0: 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ;.. /* Re
d1f0: 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74 20 66 72 move the slot fr
d200: 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 om the free-list
d210: 2e 20 55 70 64 61 74 65 20 74 68 65 20 6e 75 6d . Update the num
d220: 62 65 72 20 6f 66 0a 20 20 20 20 20 20 20 20 2a ber of. *
d230: 2a 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 * fragmented byt
d240: 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 es within the pa
d250: 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d ge. */. m
d260: 65 6d 63 70 79 28 26 61 44 61 74 61 5b 69 41 64 emcpy(&aData[iAd
d270: 64 72 5d 2c 20 26 61 44 61 74 61 5b 70 63 5d 2c dr], &aData[pc],
d280: 20 32 29 3b 0a 20 20 20 20 20 20 20 20 61 44 61 2);. aDa
d290: 74 61 5b 68 64 72 2b 37 5d 20 2b 3d 20 28 75 38 ta[hdr+7] += (u8
d2a0: 29 78 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 )x;. }else
d2b0: 69 66 28 20 78 2b 70 63 20 3e 20 6d 61 78 50 43 if( x+pc > maxPC
d2c0: 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
d2d0: 68 69 73 20 73 6c 6f 74 20 65 78 74 65 6e 64 73 his slot extends
d2e0: 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20 off the end of
d2f0: 74 68 65 20 75 73 61 62 6c 65 20 70 61 72 74 20 the usable part
d300: 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 of the page */.
d310: 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20 53 51 *pRc = SQ
d320: 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 LITE_CORRUPT_PAG
d330: 45 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 E(pPg);.
d340: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 return 0;.
d350: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f }else{. /
d360: 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 * The slot remai
d370: 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c ns on the free-l
d380: 69 73 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 ist. Reduce its
d390: 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a size to account.
d3a0: 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 ** for t
d3b0: 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 he portion used
d3c0: 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 by the new alloc
d3d0: 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 ation. */.
d3e0: 20 20 70 75 74 32 62 79 74 65 28 26 61 44 61 74 put2byte(&aDat
d3f0: 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 a[pc+2], x);.
d400: 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
d410: 6e 20 26 61 44 61 74 61 5b 70 63 20 2b 20 78 5d n &aData[pc + x]
d420: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 41 64 64 ;. }. iAdd
d430: 72 20 3d 20 70 63 3b 0a 20 20 20 20 70 63 20 3d r = pc;. pc =
d440: 20 67 65 74 32 62 79 74 65 28 26 61 44 61 74 61 get2byte(&aData
d450: 5b 70 63 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 [pc]);. if( p
d460: 63 3c 69 41 64 64 72 2b 73 69 7a 65 20 29 7b 0a c<iAddr+size ){.
d470: 20 20 20 20 20 20 69 66 28 20 70 63 20 29 7b 0a if( pc ){.
d480: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
d490: 65 78 74 20 73 6c 6f 74 20 69 6e 20 74 68 65 20 ext slot in the
d4a0: 63 68 61 69 6e 20 69 73 20 6e 6f 74 20 70 61 73 chain is not pas
d4b0: 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
d4c0: 20 63 75 72 72 65 6e 74 20 73 6c 6f 74 20 2a 2f current slot */
d4d0: 0a 20 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20 . *pRc =
d4e0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
d4f0: 41 47 45 28 70 50 67 29 3b 0a 20 20 20 20 20 20 AGE(pPg);.
d500: 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 }. return 0
d510: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
d520: 28 20 70 63 3e 6d 61 78 50 43 2b 6e 42 79 74 65 ( pc>maxPC+nByte
d530: 2d 34 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 -4 ){. /* The
d540: 20 66 72 65 65 20 73 6c 6f 74 20 63 68 61 69 6e free slot chain
d550: 20 65 78 74 65 6e 64 73 20 6f 66 66 20 74 68 65 extends off the
d560: 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 end of the page
d570: 20 2a 2f 0a 20 20 20 20 2a 70 52 63 20 3d 20 53 */. *pRc = S
d580: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 QLITE_CORRUPT_PA
d590: 47 45 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 GE(pPg);. }. r
d5a0: 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
d5b0: 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 * Allocate nByte
d5c0: 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
d5d0: 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 from within the
d5e0: 42 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 B-Tree page pass
d5f0: 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 ed.** as the fir
d600: 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 st argument. Wri
d610: 74 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 te into *pIdx th
d620: 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 e index into pPa
d630: 67 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f ge->aData[].** o
d640: 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 f the first byte
d650: 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 of allocated sp
d660: 61 63 65 2e 20 52 65 74 75 72 6e 20 65 69 74 68 ace. Return eith
d670: 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a er SQLITE_OK or.
d680: 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 ** an error code
d690: 20 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 (usually SQLITE
d6a0: 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a _CORRUPT)..**.**
d6b0: 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 The caller guar
d6c0: 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 antees that ther
d6d0: 65 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 e is sufficient
d6e0: 73 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 space to make th
d6f0: 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e e.** allocation.
d700: 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d This routine m
d710: 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 65 66 ight need to def
d720: 72 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 ragment in order
d730: 20 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c to bring.** all
d740: 20 74 68 65 20 73 70 61 63 65 20 74 6f 67 65 74 the space toget
d750: 68 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 her, however. T
d760: 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
d770: 20 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 avoid using.**
d780: 74 68 65 20 66 69 72 73 74 20 74 77 6f 20 62 79 the first two by
d790: 74 65 73 20 70 61 73 74 20 74 68 65 20 63 65 6c tes past the cel
d7a0: 6c 20 70 6f 69 6e 74 65 72 20 61 72 65 61 20 73 l pointer area s
d7b0: 69 6e 63 65 20 70 72 65 73 75 6d 61 62 6c 79 20 ince presumably
d7c0: 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 this.** allocati
d7d0: 6f 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 64 65 on is being made
d7e0: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 in order to ins
d7f0: 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 ert a new cell,
d800: 73 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c so we will.** al
d810: 73 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 69 6e so end up needin
d820: 67 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 g a new cell poi
d830: 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nter..*/.static
d840: 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 int allocateSpac
d850: 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
d860: 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 , int nByte, int
d870: 20 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 *pIdx){. const
d880: 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 int hdr = pPage
d890: 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 ->hdrOffset;
d8a0: 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f /* Local cache o
d8b0: 66 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 f pPage->hdrOffs
d8c0: 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e et */. u8 * con
d8d0: 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d st data = pPage-
d8e0: 3e 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 >aData; /*
d8f0: 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 Local cache of p
d900: 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 Page->aData */.
d910: 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 int top;
d920: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d930: 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
d940: 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 yte of cell cont
d950: 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
d960: 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d970: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
d980: 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 65 /* Integer re
d990: 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
d9a0: 6e 74 20 67 61 70 3b 20 20 20 20 20 20 20 20 2f nt gap; /
d9b0: 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
d9c0: 67 61 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c gap between cell
d9d0: 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 63 65 pointers and ce
d9e0: 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 ll content */.
d9f0: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
da00: 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
da10: 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
da20: 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
da30: 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 pPage->pBt );.
da40: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
da50: 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
da60: 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
da70: 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
da80: 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d >=0 ); /* Minim
da90: 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
daa0: 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4 */. assert( p
dab0: 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 Page->nFree>=nBy
dac0: 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 te );. assert(
dad0: 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
dae0: 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
daf0: 20 6e 42 79 74 65 20 3c 20 28 69 6e 74 29 28 70 nByte < (int)(p
db00: 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
db10: 65 53 69 7a 65 2d 38 29 20 29 3b 0a 0a 20 20 61 eSize-8) );.. a
db20: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 ssert( pPage->ce
db30: 6c 6c 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 llOffset == hdr
db40: 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e + 12 - 4*pPage->
db50: 6c 65 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 leaf );. gap =
db60: 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
db70: 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 t + 2*pPage->nCe
db80: 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 67 61 ll;. assert( ga
db90: 70 3c 3d 36 35 35 33 36 20 29 3b 0a 20 20 2f 2a p<=65536 );. /*
dba0: 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d EVIDENCE-OF: R-
dbb0: 32 39 33 35 36 2d 30 32 33 39 31 20 49 66 20 74 29356-02391 If t
dbc0: 68 65 20 64 61 74 61 62 61 73 65 20 75 73 65 73 he database uses
dbd0: 20 61 20 36 35 35 33 36 2d 62 79 74 65 20 70 61 a 65536-byte pa
dbe0: 67 65 20 73 69 7a 65 0a 20 20 2a 2a 20 61 6e 64 ge size. ** and
dbf0: 20 74 68 65 20 72 65 73 65 72 76 65 64 20 73 70 the reserved sp
dc00: 61 63 65 20 69 73 20 7a 65 72 6f 20 28 74 68 65 ace is zero (the
dc10: 20 75 73 75 61 6c 20 76 61 6c 75 65 20 66 6f 72 usual value for
dc20: 20 72 65 73 65 72 76 65 64 20 73 70 61 63 65 29 reserved space)
dc30: 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 . ** then the c
dc40: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 66 73 ell content offs
dc50: 65 74 20 6f 66 20 61 6e 20 65 6d 70 74 79 20 70 et of an empty p
dc60: 61 67 65 20 77 61 6e 74 73 20 74 6f 20 62 65 20 age wants to be
dc70: 36 35 35 33 36 2e 0a 20 20 2a 2a 20 48 6f 77 65 65536.. ** Howe
dc80: 76 65 72 2c 20 74 68 61 74 20 69 6e 74 65 67 65 ver, that intege
dc90: 72 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 r is too large t
dca0: 6f 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 o be stored in a
dcb0: 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 2-byte unsigned
dcc0: 0a 20 20 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 . ** integer, s
dcd0: 6f 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20 69 o a value of 0 i
dce0: 73 20 75 73 65 64 20 69 6e 20 69 74 73 20 70 6c s used in its pl
dcf0: 61 63 65 2e 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 ace. */. top =
dd00: 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
dd10: 64 72 2b 35 5d 29 3b 0a 20 20 61 73 73 65 72 74 dr+5]);. assert
dd20: 28 20 74 6f 70 3c 3d 28 69 6e 74 29 70 50 61 67 ( top<=(int)pPag
dd30: 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
dd40: 7a 65 20 29 3b 20 2f 2a 20 50 72 65 76 65 6e 74 ze ); /* Prevent
dd50: 20 62 79 20 67 65 74 41 6e 64 49 6e 69 74 50 61 by getAndInitPa
dd60: 67 65 28 29 20 2a 2f 0a 20 20 69 66 28 20 67 61 ge() */. if( ga
dd70: 70 3e 74 6f 70 20 29 7b 0a 20 20 20 20 69 66 28 p>top ){. if(
dd80: 20 74 6f 70 3d 3d 30 20 26 26 20 70 50 61 67 65 top==0 && pPage
dd90: 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
dda0: 65 3d 3d 36 35 35 33 36 20 29 7b 0a 20 20 20 20 e==65536 ){.
ddb0: 20 20 74 6f 70 20 3d 20 36 35 35 33 36 3b 0a 20 top = 65536;.
ddc0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
ddd0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
dde0: 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 RRUPT_PAGE(pPage
ddf0: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
de00: 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 65 /* If there is e
de10: 6e 6f 75 67 68 20 73 70 61 63 65 20 62 65 74 77 nough space betw
de20: 65 65 6e 20 67 61 70 20 61 6e 64 20 74 6f 70 20 een gap and top
de30: 66 6f 72 20 6f 6e 65 20 6d 6f 72 65 20 63 65 6c for one more cel
de40: 6c 20 70 6f 69 6e 74 65 72 2c 0a 20 20 2a 2a 20 l pointer,. **
de50: 61 6e 64 20 69 66 20 74 68 65 20 66 72 65 65 6c and if the freel
de60: 69 73 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 ist is not empty
de70: 2c 20 74 68 65 6e 20 73 65 61 72 63 68 20 74 68 , then search th
de80: 65 0a 20 20 2a 2a 20 66 72 65 65 6c 69 73 74 20 e. ** freelist
de90: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c looking for a sl
dea0: 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f ot big enough to
deb0: 20 73 61 74 69 73 66 79 20 74 68 65 20 72 65 71 satisfy the req
dec0: 75 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 uest.. */. tes
ded0: 74 63 61 73 65 28 20 67 61 70 2b 32 3d 3d 74 6f tcase( gap+2==to
dee0: 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 p );. testcase(
def0: 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 3b 0a 20 gap+1==top );.
df00: 20 74 65 73 74 63 61 73 65 28 20 67 61 70 3d 3d testcase( gap==
df10: 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 28 64 61 top );. if( (da
df20: 74 61 5b 68 64 72 2b 32 5d 20 7c 7c 20 64 61 74 ta[hdr+2] || dat
df30: 61 5b 68 64 72 2b 31 5d 29 20 26 26 20 67 61 70 a[hdr+1]) && gap
df40: 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20 75 +2<=top ){. u
df50: 38 20 2a 70 53 70 61 63 65 20 3d 20 70 61 67 65 8 *pSpace = page
df60: 46 69 6e 64 53 6c 6f 74 28 70 50 61 67 65 2c 20 FindSlot(pPage,
df70: 6e 42 79 74 65 2c 20 26 72 63 29 3b 0a 20 20 20 nByte, &rc);.
df80: 20 69 66 28 20 70 53 70 61 63 65 20 29 7b 0a 20 if( pSpace ){.
df90: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 70 assert( pSp
dfa0: 61 63 65 3e 3d 64 61 74 61 20 26 26 20 28 70 53 ace>=data && (pS
dfb0: 70 61 63 65 20 2d 20 64 61 74 61 29 3c 36 35 35 pace - data)<655
dfc0: 33 36 20 29 3b 0a 20 20 20 20 20 20 2a 70 49 64 36 );. *pId
dfd0: 78 20 3d 20 28 69 6e 74 29 28 70 53 70 61 63 65 x = (int)(pSpace
dfe0: 20 2d 20 64 61 74 61 29 3b 0a 20 20 20 20 20 20 - data);.
dff0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e000: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
e010: 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 rc ){. retu
e020: 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d rn rc;. }. }
e030: 0a 0a 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 .. /* The reque
e040: 73 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 st could not be
e050: 66 75 6c 66 69 6c 6c 65 64 20 75 73 69 6e 67 20 fulfilled using
e060: 61 20 66 72 65 65 6c 69 73 74 20 73 6c 6f 74 2e a freelist slot.
e070: 20 20 43 68 65 63 6b 0a 20 20 2a 2a 20 74 6f 20 Check. ** to
e080: 73 65 65 20 69 66 20 64 65 66 72 61 67 6d 65 6e see if defragmen
e090: 74 61 74 69 6f 6e 20 69 73 20 6e 65 63 65 73 73 tation is necess
e0a0: 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 ary.. */. test
e0b0: 63 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 case( gap+2+nByt
e0c0: 65 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 e==top );. if(
e0d0: 67 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 gap+2+nByte>top
e0e0: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
e0f0: 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c Page->nCell>0 ||
e100: 20 43 4f 52 52 55 50 54 5f 44 42 20 29 3b 0a 20 CORRUPT_DB );.
e110: 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
e120: 2d 3e 6e 46 72 65 65 3e 3d 30 20 29 3b 0a 20 20 ->nFree>=0 );.
e130: 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e rc = defragmen
e140: 74 50 61 67 65 28 70 50 61 67 65 2c 20 4d 49 4e tPage(pPage, MIN
e150: 28 34 2c 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 (4, pPage->nFree
e160: 20 2d 20 28 32 2b 6e 42 79 74 65 29 29 29 3b 0a - (2+nByte)));.
e170: 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
e180: 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 urn rc;. top
e190: 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 = get2byteNotZer
e1a0: 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b o(&data[hdr+5]);
e1b0: 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 61 70 . assert( gap
e1c0: 2b 32 2b 6e 42 79 74 65 3c 3d 74 6f 70 20 29 3b +2+nByte<=top );
e1d0: 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c 6c 6f . }... /* Allo
e1e0: 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 6f 6d cate memory from
e1f0: 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 74 77 the gap in betw
e200: 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 een the cell poi
e210: 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a 2a 20 nter array. **
e220: 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e and the cell con
e230: 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 tent area. The
e240: 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 btreeInitPage()
e250: 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61 64 79 call has already
e260: 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65 64 20 . ** validated
e270: 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 47 the freelist. G
e280: 69 76 65 6e 20 74 68 61 74 20 74 68 65 20 66 72 iven that the fr
e290: 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69 64 2c eelist is valid,
e2a0: 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 6e there. ** is n
e2b0: 6f 20 77 61 79 20 74 68 61 74 20 74 68 65 20 61 o way that the a
e2c0: 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20 65 78 llocation can ex
e2d0: 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65 6e 64 tend off the end
e2e0: 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 20 20 of the page..
e2f0: 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28 29 20 ** The assert()
e300: 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 below verifies t
e310: 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 he previous sent
e320: 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 ence.. */. top
e330: 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70 75 74 -= nByte;. put
e340: 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
e350: 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73 73 65 5], top);. asse
e360: 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 3c 3d rt( top+nByte <=
e370: 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74 (int)pPage->pBt
e380: 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a ->usableSize );.
e390: 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b 0a 20 *pIdx = top;.
e3a0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e3b0: 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 K;.}../*.** Retu
e3c0: 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 rn a section of
e3d0: 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 the pPage->aData
e3e0: 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 to the freelist
e3f0: 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62 ..** The first b
e400: 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20 66 yte of the new f
e410: 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 ree block is pPa
e420: 67 65 2d 3e 61 44 61 74 61 5b 69 53 74 61 72 74 ge->aData[iStart
e430: 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a ].** and the siz
e440: 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69 e of the block i
e450: 73 20 69 53 69 7a 65 20 62 79 74 65 73 2e 0a 2a s iSize bytes..*
e460: 2a 0a 2a 2a 20 41 64 6a 61 63 65 6e 74 20 66 72 *.** Adjacent fr
e470: 65 65 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 61 eeblocks are coa
e480: 6c 65 73 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 6f lesced..**.** No
e490: 74 65 20 74 68 61 74 20 65 76 65 6e 20 74 68 6f te that even tho
e4a0: 75 67 68 20 74 68 65 20 66 72 65 65 62 6c 6f 63 ugh the freebloc
e4b0: 6b 20 6c 69 73 74 20 77 61 73 20 63 68 65 63 6b k list was check
e4c0: 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 ed by btreeInitP
e4d0: 61 67 65 28 29 2c 0a 2a 2a 20 74 68 61 74 20 72 age(),.** that r
e4e0: 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6e 6f 74 20 outine will not
e4f0: 64 65 74 65 63 74 20 6f 76 65 72 6c 61 70 20 62 detect overlap b
e500: 65 74 77 65 65 6e 20 63 65 6c 6c 73 20 6f 72 20 etween cells or
e510: 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 72 freeblocks. Nor
e520: 0a 2a 2a 20 64 6f 65 73 20 69 74 20 64 65 74 65 .** does it dete
e530: 63 74 20 63 65 6c 6c 73 20 6f 72 20 66 72 65 65 ct cells or free
e540: 62 6c 6f 63 6b 73 20 74 68 61 74 20 65 6e 63 72 blocks that encr
e550: 6f 75 63 68 20 69 6e 74 6f 20 74 68 65 20 72 65 ouch into the re
e560: 73 65 72 76 65 64 20 62 79 74 65 73 0a 2a 2a 20 served bytes.**
e570: 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
e580: 65 20 70 61 67 65 2e 20 20 53 6f 20 64 6f 20 61 e page. So do a
e590: 64 64 69 74 69 6f 6e 61 6c 20 63 6f 72 72 75 70 dditional corrup
e5a0: 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 6e 73 69 tion checks insi
e5b0: 64 65 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 de this.** routi
e5c0: 6e 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 ne and return SQ
e5d0: 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 LITE_CORRUPT if
e5e0: 61 6e 79 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 any problems are
e5f0: 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 found..*/.stati
e600: 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 c int freeSpace(
e610: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
e620: 75 31 36 20 69 53 74 61 72 74 2c 20 75 31 36 20 u16 iStart, u16
e630: 69 53 69 7a 65 29 7b 0a 20 20 75 31 36 20 69 50 iSize){. u16 iP
e640: 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 tr;
e650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e660: 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 70 74 /* Address of pt
e670: 72 20 74 6f 20 6e 65 78 74 20 66 72 65 65 62 6c r to next freebl
e680: 6f 63 6b 20 2a 2f 0a 20 20 75 31 36 20 69 46 72 ock */. u16 iFr
e690: 65 65 42 6c 6b 3b 20 20 20 20 20 20 20 20 20 20 eeBlk;
e6a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e6b0: 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 * Address of the
e6c0: 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b 20 next freeblock
e6d0: 2a 2f 0a 20 20 75 38 20 68 64 72 3b 20 20 20 20 */. u8 hdr;
e6e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6f0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
e700: 67 65 20 68 65 61 64 65 72 20 73 69 7a 65 2e 20 ge header size.
e710: 20 30 20 6f 72 20 31 30 30 20 2a 2f 0a 20 20 75 0 or 100 */. u
e720: 38 20 6e 46 72 61 67 20 3d 20 30 3b 20 20 20 20 8 nFrag = 0;
e730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e740: 20 20 20 20 20 2f 2a 20 52 65 64 75 63 74 69 6f /* Reductio
e750: 6e 20 69 6e 20 66 72 61 67 6d 65 6e 74 61 74 69 n in fragmentati
e760: 6f 6e 20 2a 2f 0a 20 20 75 31 36 20 69 4f 72 69 on */. u16 iOri
e770: 67 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 20 20 gSize = iSize;
e780: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e790: 20 4f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 Original value
e7a0: 6f 66 20 69 53 69 7a 65 20 2a 2f 0a 20 20 75 31 of iSize */. u1
e7b0: 36 20 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 6 x;
e7c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7d0: 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
e7e0: 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
e7f0: 65 61 20 2a 2f 0a 20 20 75 33 32 20 69 45 6e 64 ea */. u32 iEnd
e800: 20 3d 20 69 53 74 61 72 74 20 2b 20 69 53 69 7a = iStart + iSiz
e810: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
e820: 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74 First byte past
e830: 20 74 68 65 20 69 53 74 61 72 74 20 62 75 66 66 the iStart buff
e840: 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 er */. unsigned
e850: 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 char *data = pP
e860: 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 2f 2a age->aData; /*
e870: 20 50 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a 2f Page content */
e880: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
e890: 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 e->pBt!=0 );. a
e8a0: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
e8b0: 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
e8c0: 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
e8d0: 3b 0a 20 20 61 73 73 65 72 74 28 20 43 4f 52 52 ;. assert( CORR
e8e0: 55 50 54 5f 44 42 20 7c 7c 20 69 53 74 61 72 74 UPT_DB || iStart
e8f0: 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 >=pPage->hdrOffs
e900: 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 69 6c et+6+pPage->chil
e910: 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 61 73 dPtrSize );. as
e920: 73 65 72 74 28 20 43 4f 52 52 55 50 54 5f 44 42 sert( CORRUPT_DB
e930: 20 7c 7c 20 69 45 6e 64 20 3c 3d 20 70 50 61 67 || iEnd <= pPag
e940: 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
e950: 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ze );. assert(
e960: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
e970: 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
e980: 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
e990: 74 28 20 69 53 69 7a 65 3e 3d 34 20 29 3b 20 20 t( iSize>=4 );
e9a0: 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
e9b0: 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 size is 4 */.
e9c0: 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3c 3d assert( iStart<=
e9d0: 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
e9e0: 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 0a 20 20 2f leSize-4 );.. /
e9f0: 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 66 72 * The list of fr
ea00: 65 65 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 eeblocks must be
ea10: 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 in ascending or
ea20: 64 65 72 2e 20 20 46 69 6e 64 20 74 68 65 20 0a der. Find the .
ea30: 20 20 2a 2a 20 73 70 6f 74 20 6f 6e 20 74 68 65 ** spot on the
ea40: 20 6c 69 73 74 20 77 68 65 72 65 20 69 53 74 61 list where iSta
ea50: 72 74 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 rt should be ins
ea60: 65 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 68 64 erted.. */. hd
ea70: 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
ea80: 66 73 65 74 3b 0a 20 20 69 50 74 72 20 3d 20 68 fset;. iPtr = h
ea90: 64 72 20 2b 20 31 3b 0a 20 20 69 66 28 20 64 61 dr + 1;. if( da
eaa0: 74 61 5b 69 50 74 72 2b 31 5d 3d 3d 30 20 26 26 ta[iPtr+1]==0 &&
eab0: 20 64 61 74 61 5b 69 50 74 72 5d 3d 3d 30 20 29 data[iPtr]==0 )
eac0: 7b 0a 20 20 20 20 69 46 72 65 65 42 6c 6b 20 3d {. iFreeBlk =
ead0: 20 30 3b 20 20 2f 2a 20 53 68 6f 72 74 63 75 74 0; /* Shortcut
eae0: 20 66 6f 72 20 74 68 65 20 63 61 73 65 20 77 68 for the case wh
eaf0: 65 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 en the freelist
eb00: 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 7d 65 is empty */. }e
eb10: 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 lse{. while(
eb20: 28 69 46 72 65 65 42 6c 6b 20 3d 20 67 65 74 32 (iFreeBlk = get2
eb30: 62 79 74 65 28 26 64 61 74 61 5b 69 50 74 72 5d byte(&data[iPtr]
eb40: 29 29 3c 69 53 74 61 72 74 20 29 7b 0a 20 20 20 ))<iStart ){.
eb50: 20 20 20 69 66 28 20 69 46 72 65 65 42 6c 6b 3c if( iFreeBlk<
eb60: 69 50 74 72 2b 34 20 29 7b 0a 20 20 20 20 20 20 iPtr+4 ){.
eb70: 20 20 69 66 28 20 69 46 72 65 65 42 6c 6b 3d 3d if( iFreeBlk==
eb80: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 0 ) break;.
eb90: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
eba0: 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 _CORRUPT_PAGE(pP
ebb0: 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
ebc0: 20 20 20 20 69 50 74 72 20 3d 20 69 46 72 65 65 iPtr = iFree
ebd0: 42 6c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 Blk;. }. i
ebe0: 66 28 20 69 46 72 65 65 42 6c 6b 3e 70 50 61 67 f( iFreeBlk>pPag
ebf0: 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
ec00: 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 72 65 ze-4 ){. re
ec10: 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
ec20: 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 3b UPT_PAGE(pPage);
ec30: 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
ec40: 74 28 20 69 46 72 65 65 42 6c 6b 3e 69 50 74 72 t( iFreeBlk>iPtr
ec50: 20 7c 7c 20 69 46 72 65 65 42 6c 6b 3d 3d 30 20 || iFreeBlk==0
ec60: 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 41 74 20 );. . /* At
ec70: 74 68 69 73 20 70 6f 69 6e 74 3a 0a 20 20 20 20 this point:.
ec80: 2a 2a 20 20 20 20 69 46 72 65 65 42 6c 6b 3a 20 ** iFreeBlk:
ec90: 20 20 46 69 72 73 74 20 66 72 65 65 62 6c 6f 63 First freebloc
eca0: 6b 20 61 66 74 65 72 20 69 53 74 61 72 74 2c 20 k after iStart,
ecb0: 6f 72 20 7a 65 72 6f 20 69 66 20 6e 6f 6e 65 0a or zero if none.
ecc0: 20 20 20 20 2a 2a 20 20 20 20 69 50 74 72 3a 20 ** iPtr:
ecd0: 20 20 20 20 20 20 54 68 65 20 61 64 64 72 65 73 The addres
ece0: 73 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 s of a pointer t
ecf0: 6f 20 69 46 72 65 65 42 6c 6b 0a 20 20 20 20 2a o iFreeBlk. *
ed00: 2a 0a 20 20 20 20 2a 2a 20 43 68 65 63 6b 20 74 *. ** Check t
ed10: 6f 20 73 65 65 20 69 66 20 69 46 72 65 65 42 6c o see if iFreeBl
ed20: 6b 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 61 6c k should be coal
ed30: 65 73 63 65 64 20 6f 6e 74 6f 20 74 68 65 20 65 esced onto the e
ed40: 6e 64 20 6f 66 20 69 53 74 61 72 74 2e 0a 20 20 nd of iStart..
ed50: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 46 72 */. if( iFr
ed60: 65 65 42 6c 6b 20 26 26 20 69 45 6e 64 2b 33 3e eeBlk && iEnd+3>
ed70: 3d 69 46 72 65 65 42 6c 6b 20 29 7b 0a 20 20 20 =iFreeBlk ){.
ed80: 20 20 20 6e 46 72 61 67 20 3d 20 69 46 72 65 65 nFrag = iFree
ed90: 42 6c 6b 20 2d 20 69 45 6e 64 3b 0a 20 20 20 20 Blk - iEnd;.
eda0: 20 20 69 66 28 20 69 45 6e 64 3e 69 46 72 65 65 if( iEnd>iFree
edb0: 42 6c 6b 20 29 20 72 65 74 75 72 6e 20 53 51 4c Blk ) return SQL
edc0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 ITE_CORRUPT_PAGE
edd0: 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 (pPage);. i
ede0: 45 6e 64 20 3d 20 69 46 72 65 65 42 6c 6b 20 2b End = iFreeBlk +
edf0: 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
ee00: 69 46 72 65 65 42 6c 6b 2b 32 5d 29 3b 0a 20 20 iFreeBlk+2]);.
ee10: 20 20 20 20 69 66 28 20 69 45 6e 64 20 3e 20 70 if( iEnd > p
ee20: 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
ee30: 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 eSize ){.
ee40: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
ee50: 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 ORRUPT_PAGE(pPag
ee60: 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
ee70: 20 20 69 53 69 7a 65 20 3d 20 69 45 6e 64 20 2d iSize = iEnd -
ee80: 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 69 iStart;. i
ee90: 46 72 65 65 42 6c 6b 20 3d 20 67 65 74 32 62 79 FreeBlk = get2by
eea0: 74 65 28 26 64 61 74 61 5b 69 46 72 65 65 42 6c te(&data[iFreeBl
eeb0: 6b 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 k]);. }. .
eec0: 20 20 2f 2a 20 49 66 20 69 50 74 72 20 69 73 20 /* If iPtr is
eed0: 61 6e 6f 74 68 65 72 20 66 72 65 65 62 6c 6f 63 another freebloc
eee0: 6b 20 28 74 68 61 74 20 69 73 2c 20 69 66 20 69 k (that is, if i
eef0: 50 74 72 20 69 73 20 6e 6f 74 20 74 68 65 20 66 Ptr is not the f
ef00: 72 65 65 6c 69 73 74 0a 20 20 20 20 2a 2a 20 70 reelist. ** p
ef10: 6f 69 6e 74 65 72 20 69 6e 20 74 68 65 20 70 61 ointer in the pa
ef20: 67 65 20 68 65 61 64 65 72 29 20 74 68 65 6e 20 ge header) then
ef30: 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 check to see if
ef40: 69 53 74 61 72 74 20 73 68 6f 75 6c 64 20 62 65 iStart should be
ef50: 0a 20 20 20 20 2a 2a 20 63 6f 61 6c 65 73 63 65 . ** coalesce
ef60: 64 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f d onto the end o
ef70: 66 20 69 50 74 72 2e 0a 20 20 20 20 2a 2f 0a 20 f iPtr.. */.
ef80: 20 20 20 69 66 28 20 69 50 74 72 3e 68 64 72 2b if( iPtr>hdr+
ef90: 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 1 ){. int i
efa0: 50 74 72 45 6e 64 20 3d 20 69 50 74 72 20 2b 20 PtrEnd = iPtr +
efb0: 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 get2byte(&data[i
efc0: 50 74 72 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 Ptr+2]);. i
efd0: 66 28 20 69 50 74 72 45 6e 64 2b 33 3e 3d 69 53 f( iPtrEnd+3>=iS
efe0: 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 tart ){.
eff0: 69 66 28 20 69 50 74 72 45 6e 64 3e 69 53 74 61 if( iPtrEnd>iSta
f000: 72 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 rt ) return SQLI
f010: 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 28 TE_CORRUPT_PAGE(
f020: 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 pPage);.
f030: 6e 46 72 61 67 20 2b 3d 20 69 53 74 61 72 74 20 nFrag += iStart
f040: 2d 20 69 50 74 72 45 6e 64 3b 0a 20 20 20 20 20 - iPtrEnd;.
f050: 20 20 20 69 53 69 7a 65 20 3d 20 69 45 6e 64 20 iSize = iEnd
f060: 2d 20 69 50 74 72 3b 0a 20 20 20 20 20 20 20 20 - iPtr;.
f070: 69 53 74 61 72 74 20 3d 20 69 50 74 72 3b 0a 20 iStart = iPtr;.
f080: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
f090: 20 69 66 28 20 6e 46 72 61 67 3e 64 61 74 61 5b if( nFrag>data[
f0a0: 68 64 72 2b 37 5d 20 29 20 72 65 74 75 72 6e 20 hdr+7] ) return
f0b0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
f0c0: 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 20 20 AGE(pPage);.
f0d0: 64 61 74 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 6e data[hdr+7] -= n
f0e0: 46 72 61 67 3b 0a 20 20 7d 0a 20 20 78 20 3d 20 Frag;. }. x =
f0f0: 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
f100: 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 69 53 dr+5]);. if( iS
f110: 74 61 72 74 3c 3d 78 20 29 7b 0a 20 20 20 20 2f tart<=x ){. /
f120: 2a 20 54 68 65 20 6e 65 77 20 66 72 65 65 62 6c * The new freebl
f130: 6f 63 6b 20 69 73 20 61 74 20 74 68 65 20 62 65 ock is at the be
f140: 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 ginning of the c
f150: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
f160: 2c 0a 20 20 20 20 2a 2a 20 73 6f 20 6a 75 73 74 ,. ** so just
f170: 20 65 78 74 65 6e 64 20 74 68 65 20 63 65 6c 6c extend the cell
f180: 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 72 61 content area ra
f190: 74 68 65 72 20 74 68 61 6e 20 63 72 65 61 74 65 ther than create
f1a0: 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 another. **
f1b0: 66 72 65 65 6c 69 73 74 20 65 6e 74 72 79 20 2a freelist entry *
f1c0: 2f 0a 20 20 20 20 69 66 28 20 69 53 74 61 72 74 /. if( iStart
f1d0: 3c 78 20 7c 7c 20 69 50 74 72 21 3d 68 64 72 2b <x || iPtr!=hdr+
f1e0: 31 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 1 ) return SQLIT
f1f0: 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 28 70 E_CORRUPT_PAGE(p
f200: 50 61 67 65 29 3b 0a 20 20 20 20 70 75 74 32 62 Page);. put2b
f210: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
f220: 2c 20 69 46 72 65 65 42 6c 6b 29 3b 0a 20 20 20 , iFreeBlk);.
f230: 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
f240: 68 64 72 2b 35 5d 2c 20 69 45 6e 64 29 3b 0a 20 hdr+5], iEnd);.
f250: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
f260: 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 66 72 nsert the new fr
f270: 65 65 62 6c 6f 63 6b 20 69 6e 74 6f 20 74 68 65 eeblock into the
f280: 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 20 freelist */.
f290: 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
f2a0: 69 50 74 72 5d 2c 20 69 53 74 61 72 74 29 3b 0a iPtr], iStart);.
f2b0: 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d }. if( pPage-
f2c0: 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 >pBt->btsFlags &
f2d0: 20 42 54 53 5f 46 41 53 54 5f 53 45 43 55 52 45 BTS_FAST_SECURE
f2e0: 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 76 65 72 77 ){. /* Overw
f2f0: 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 rite deleted inf
f300: 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 ormation with ze
f310: 72 6f 73 20 77 68 65 6e 20 74 68 65 20 73 65 63 ros when the sec
f320: 75 72 65 5f 64 65 6c 65 74 65 0a 20 20 20 20 2a ure_delete. *
f330: 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 * option is enab
f340: 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 led */. memse
f350: 74 28 26 64 61 74 61 5b 69 53 74 61 72 74 5d 2c t(&data[iStart],
f360: 20 30 2c 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 0, iSize);. }.
f370: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
f380: 5b 69 53 74 61 72 74 5d 2c 20 69 46 72 65 65 42 [iStart], iFreeB
f390: 6c 6b 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 lk);. put2byte(
f3a0: 26 64 61 74 61 5b 69 53 74 61 72 74 2b 32 5d 2c &data[iStart+2],
f3b0: 20 69 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 iSize);. pPage
f3c0: 2d 3e 6e 46 72 65 65 20 2b 3d 20 69 4f 72 69 67 ->nFree += iOrig
f3d0: 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 Size;. return S
f3e0: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
f3f0: 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20 66 6c ** Decode the fl
f400: 61 67 73 20 62 79 74 65 20 28 74 68 65 20 66 69 ags byte (the fi
f410: 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 rst byte of the
f420: 68 65 61 64 65 72 29 20 66 6f 72 20 61 20 70 61 header) for a pa
f430: 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 ge.** and initia
f440: 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66 20 74 lize fields of t
f450: 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75 63 he MemPage struc
f460: 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67 6c 79 ture accordingly
f470: 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 ..**.** Only the
f480: 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 following combi
f490: 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75 70 70 nations are supp
f4a0: 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69 6e 67 orted. Anything
f4b0: 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 69 6e different.** in
f4c0: 64 69 63 61 74 65 73 20 61 20 63 6f 72 72 75 70 dicates a corrup
f4d0: 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 t database files
f4e0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 :.**.**
f4f0: 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a 2a 20 PTF_ZERODATA.**
f500: 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f PTF_ZERO
f510: 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 46 0a DATA | PTF_LEAF.
f520: 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c ** PTF_L
f530: 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e EAFDATA | PTF_IN
f540: 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20 20 20 TKEY.**
f550: 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 PTF_LEAFDATA | P
f560: 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54 46 5f TF_INTKEY | PTF_
f570: 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63 20 69 LEAF.*/.static i
f580: 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73 28 4d nt decodeFlags(M
f590: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
f5a0: 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a 20 20 nt flagByte){.
f5b0: 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
f5c0: 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20 /* A copy of
f5d0: 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a 0a 20 pPage->pBt */..
f5e0: 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
f5f0: 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50 61 67 hdrOffset==(pPag
f600: 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 e->pgno==1 ? 100
f610: 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73 65 72 : 0) );. asser
f620: 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
f630: 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
f640: 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 50 ->mutex) );. pP
f650: 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 75 38 29 age->leaf = (u8)
f660: 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 3b 20 20 (flagByte>>3);
f670: 61 73 73 65 72 74 28 20 50 54 46 5f 4c 45 41 46 assert( PTF_LEAF
f680: 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 20 66 6c == 1<<3 );. fl
f690: 61 67 42 79 74 65 20 26 3d 20 7e 50 54 46 5f 4c agByte &= ~PTF_L
f6a0: 45 41 46 3b 0a 20 20 70 50 61 67 65 2d 3e 63 68 EAF;. pPage->ch
f6b0: 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 34 2d 34 ildPtrSize = 4-4
f6c0: 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 *pPage->leaf;.
f6d0: 70 50 61 67 65 2d 3e 78 43 65 6c 6c 53 69 7a 65 pPage->xCellSize
f6e0: 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 3b 0a = cellSizePtr;.
f6f0: 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 pBt = pPage->p
f700: 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 79 Bt;. if( flagBy
f710: 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 54 te==(PTF_LEAFDAT
f720: 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 20 A | PTF_INTKEY)
f730: 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e ){. /* EVIDEN
f740: 43 45 2d 4f 46 3a 20 52 2d 30 37 32 39 31 2d 33 CE-OF: R-07291-3
f750: 35 33 32 38 20 41 20 76 61 6c 75 65 20 6f 66 20 5328 A value of
f760: 35 20 28 30 78 30 35 29 20 6d 65 61 6e 73 20 74 5 (0x05) means t
f770: 68 65 20 70 61 67 65 20 69 73 20 61 6e 0a 20 20 he page is an.
f780: 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20 74 61 ** interior ta
f790: 62 6c 65 20 62 2d 74 72 65 65 20 70 61 67 65 2e ble b-tree page.
f7a0: 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
f7b0: 28 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 (PTF_LEAFDATA|PT
f7c0: 46 5f 49 4e 54 4b 45 59 29 3d 3d 35 20 29 3b 0a F_INTKEY)==5 );.
f7d0: 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
f7e0: 4f 46 3a 20 52 2d 32 36 39 30 30 2d 30 39 31 37 OF: R-26900-0917
f7f0: 36 20 41 20 76 61 6c 75 65 20 6f 66 20 31 33 20 6 A value of 13
f800: 28 30 78 30 64 29 20 6d 65 61 6e 73 20 74 68 65 (0x0d) means the
f810: 20 70 61 67 65 20 69 73 20 61 0a 20 20 20 20 2a page is a. *
f820: 2a 20 6c 65 61 66 20 74 61 62 6c 65 20 62 2d 74 * leaf table b-t
f830: 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 ree page. */.
f840: 20 61 73 73 65 72 74 28 20 28 50 54 46 5f 4c 45 assert( (PTF_LE
f850: 41 46 44 41 54 41 7c 50 54 46 5f 49 4e 54 4b 45 AFDATA|PTF_INTKE
f860: 59 7c 50 54 46 5f 4c 45 41 46 29 3d 3d 31 33 20 Y|PTF_LEAF)==13
f870: 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e );. pPage->in
f880: 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 69 66 tKey = 1;. if
f890: 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
f8a0: 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69 6e . pPage->in
f8b0: 74 4b 65 79 4c 65 61 66 20 3d 20 31 3b 0a 20 20 tKeyLeaf = 1;.
f8c0: 20 20 20 20 70 50 61 67 65 2d 3e 78 50 61 72 73 pPage->xPars
f8d0: 65 43 65 6c 6c 20 3d 20 62 74 72 65 65 50 61 72 eCell = btreePar
f8e0: 73 65 43 65 6c 6c 50 74 72 3b 0a 20 20 20 20 7d seCellPtr;. }
f8f0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67 else{. pPag
f900: 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 3d 20 e->intKeyLeaf =
f910: 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 0;. pPage->
f920: 78 43 65 6c 6c 53 69 7a 65 20 3d 20 63 65 6c 6c xCellSize = cell
f930: 53 69 7a 65 50 74 72 4e 6f 50 61 79 6c 6f 61 64 SizePtrNoPayload
f940: 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 78 ;. pPage->x
f950: 50 61 72 73 65 43 65 6c 6c 20 3d 20 62 74 72 65 ParseCell = btre
f960: 65 50 61 72 73 65 43 65 6c 6c 50 74 72 4e 6f 50 eParseCellPtrNoP
f970: 61 79 6c 6f 61 64 3b 0a 20 20 20 20 7d 0a 20 20 ayload;. }.
f980: 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 pPage->maxLoca
f990: 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 l = pBt->maxLeaf
f9a0: 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e ;. pPage->min
f9b0: 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e Local = pBt->min
f9c0: 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Leaf;. }else if
f9d0: 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f ( flagByte==PTF_
f9e0: 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 ZERODATA ){.
f9f0: 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 /* EVIDENCE-OF:
fa00: 52 2d 34 33 33 31 36 2d 33 37 33 30 38 20 41 20 R-43316-37308 A
fa10: 76 61 6c 75 65 20 6f 66 20 32 20 28 30 78 30 32 value of 2 (0x02
fa20: 29 20 6d 65 61 6e 73 20 74 68 65 20 70 61 67 65 ) means the page
fa30: 20 69 73 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e is an. ** in
fa40: 74 65 72 69 6f 72 20 69 6e 64 65 78 20 62 2d 74 terior index b-t
fa50: 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 ree page. */.
fa60: 20 61 73 73 65 72 74 28 20 28 50 54 46 5f 5a 45 assert( (PTF_ZE
fa70: 52 4f 44 41 54 41 29 3d 3d 32 20 29 3b 0a 20 20 RODATA)==2 );.
fa80: 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 /* EVIDENCE-OF
fa90: 3a 20 52 2d 35 39 36 31 35 2d 34 32 38 32 38 20 : R-59615-42828
faa0: 41 20 76 61 6c 75 65 20 6f 66 20 31 30 20 28 30 A value of 10 (0
fab0: 78 30 61 29 20 6d 65 61 6e 73 20 74 68 65 20 70 x0a) means the p
fac0: 61 67 65 20 69 73 20 61 0a 20 20 20 20 2a 2a 20 age is a. **
fad0: 6c 65 61 66 20 69 6e 64 65 78 20 62 2d 74 72 65 leaf index b-tre
fae0: 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 61 e page. */. a
faf0: 73 73 65 72 74 28 20 28 50 54 46 5f 5a 45 52 4f ssert( (PTF_ZERO
fb00: 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 3d 3d DATA|PTF_LEAF)==
fb10: 31 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 10 );. pPage-
fb20: 3e 69 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 >intKey = 0;.
fb30: 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 4c 65 pPage->intKeyLe
fb40: 61 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 af = 0;. pPag
fb50: 65 2d 3e 78 50 61 72 73 65 43 65 6c 6c 20 3d 20 e->xParseCell =
fb60: 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
fb70: 72 49 6e 64 65 78 3b 0a 20 20 20 20 70 50 61 67 rIndex;. pPag
fb80: 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 e->maxLocal = pB
fb90: 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 t->maxLocal;.
fba0: 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
fbb0: 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c = pBt->minLocal
fbc0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
fbd0: 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 * EVIDENCE-OF: R
fbe0: 2d 34 37 36 30 38 2d 35 36 34 36 39 20 41 6e 79 -47608-56469 Any
fbf0: 20 6f 74 68 65 72 20 76 61 6c 75 65 20 66 6f 72 other value for
fc00: 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 the b-tree page
fc10: 20 74 79 70 65 20 69 73 0a 20 20 20 20 2a 2a 20 type is. **
fc20: 61 6e 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 an error. */.
fc30: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
fc40: 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 ORRUPT_PAGE(pPag
fc50: 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d e);. }. pPage-
fc60: 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 >max1bytePayload
fc70: 20 3d 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 65 = pBt->max1byte
fc80: 50 61 79 6c 6f 61 64 3b 0a 20 20 72 65 74 75 72 Payload;. retur
fc90: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fca0: 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
fcb0: 65 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 e amount of free
fcc0: 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 space on the pag
fcd0: 65 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 e. In other wor
fce0: 64 73 2c 20 66 69 6c 6c 0a 2a 2a 20 69 6e 20 74 ds, fill.** in t
fcf0: 68 65 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 he pPage->nFree
fd00: 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 field..*/.static
fd10: 20 69 6e 74 20 62 74 72 65 65 43 6f 6d 70 75 74 int btreeComput
fd20: 65 46 72 65 65 53 70 61 63 65 28 4d 65 6d 50 61 eFreeSpace(MemPa
fd30: 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e ge *pPage){. in
fd40: 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 t pc;
fd50: 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 /* Address of a
fd60: 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 freeblock withi
fd70: 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d n pPage->aData[]
fd80: 20 2a 2f 0a 20 20 75 38 20 68 64 72 3b 20 20 20 */. u8 hdr;
fd90: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
fda0: 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 et to beginning
fdb0: 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 2a of page header *
fdc0: 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 /. u8 *data;
fdd0: 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 /* Equal
fde0: 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 to pPage->aData
fdf0: 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 */. int usableS
fe00: 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e ize; /* Amoun
fe10: 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 t of usable spac
fe20: 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a e on each page *
fe30: 2f 0a 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 /. int nFree;
fe40: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
fe50: 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 of unused bytes
fe60: 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
fe70: 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 int top;
fe80: 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
fe90: 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 te of the cell c
fea0: 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
feb0: 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b int iCellFirst;
fec0: 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c /* First all
fed0: 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 owable cell or f
fee0: 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 reeblock offset
fef0: 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 */. int iCellLa
ff00: 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 st; /* Last
ff10: 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 6f 72 possible cell or
ff20: 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 freeblock offse
ff30: 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
ff40: 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b pPage->pBt!=0 );
ff50: 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
ff60: 2d 3e 70 42 74 2d 3e 64 62 21 3d 30 20 29 3b 0a ->pBt->db!=0 );.
ff70: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
ff80: 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
ff90: 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
ffa0: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
ffb0: 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 ge->pgno==sqlite
ffc0: 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
ffd0: 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
ffe0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
fff0: 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 age == sqlite3Pa
10000 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
10010 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
10020 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
10030 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 aData == sqlite3
10040 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
10050 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
10060 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
10070 3e 69 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 >isInit==1 );.
10080 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
10090 46 72 65 65 3c 30 20 29 3b 0a 0a 20 20 75 73 61 Free<0 );.. usa
100a0 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d bleSize = pPage-
100b0 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
100c0 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d ;. hdr = pPage-
100d0 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 64 61 >hdrOffset;. da
100e0 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
100f0 61 3b 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 a;. /* EVIDENCE
10100 2d 4f 46 3a 20 52 2d 35 38 30 31 35 2d 34 38 31 -OF: R-58015-481
10110 37 35 20 54 68 65 20 74 77 6f 2d 62 79 74 65 20 75 The two-byte
10120 69 6e 74 65 67 65 72 20 61 74 20 6f 66 66 73 65 integer at offse
10130 74 20 35 20 64 65 73 69 67 6e 61 74 65 73 0a 20 t 5 designates.
10140 20 2a 2a 20 74 68 65 20 73 74 61 72 74 20 6f 66 ** the start of
10150 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
10160 74 20 61 72 65 61 2e 20 41 20 7a 65 72 6f 20 76 t area. A zero v
10170 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 69 6e alue for this in
10180 74 65 67 65 72 20 69 73 0a 20 20 2a 2a 20 69 6e teger is. ** in
10190 74 65 72 70 72 65 74 65 64 20 61 73 20 36 35 35 terpreted as 655
101a0 33 36 2e 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 67 36. */. top = g
101b0 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 26 et2byteNotZero(&
101c0 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 data[hdr+5]);.
101d0 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 68 64 72 iCellFirst = hdr
101e0 20 2b 20 38 20 2b 20 70 50 61 67 65 2d 3e 63 68 + 8 + pPage->ch
101f0 69 6c 64 50 74 72 53 69 7a 65 20 2b 20 32 2a 70 ildPtrSize + 2*p
10200 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 Page->nCell;. i
10210 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c CellLast = usabl
10220 65 53 69 7a 65 20 2d 20 34 3b 0a 0a 20 20 2f 2a eSize - 4;.. /*
10230 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 Compute the tot
10240 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e al free space on
10250 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 45 the page. ** E
10260 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 33 VIDENCE-OF: R-23
10270 35 38 38 2d 33 34 34 35 30 20 54 68 65 20 74 77 588-34450 The tw
10280 6f 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 61 o-byte integer a
10290 74 20 6f 66 66 73 65 74 20 31 20 67 69 76 65 73 t offset 1 gives
102a0 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 72 74 20 the. ** start
102b0 6f 66 20 74 68 65 20 66 69 72 73 74 20 66 72 65 of the first fre
102c0 65 62 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 eblock on the pa
102d0 67 65 2c 20 6f 72 20 69 73 20 7a 65 72 6f 20 69 ge, or is zero i
102e0 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 0a 20 f there are no.
102f0 20 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 ** freeblocks.
10300 2a 2f 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 */. pc = get2by
10310 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 te(&data[hdr+1])
10320 3b 0a 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 ;. nFree = data
10330 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 20 20 [hdr+7] + top;
10340 2f 2a 20 49 6e 69 74 20 6e 46 72 65 65 20 74 6f /* Init nFree to
10350 20 6e 6f 6e 2d 66 72 65 65 62 6c 6f 63 6b 20 66 non-freeblock f
10360 72 65 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 ree space */. i
10370 66 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 75 f( pc>0 ){. u
10380 33 32 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 32 next, size;.
10390 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 if( pc<iCellF
103a0 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a irst ){. /*
103b0 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d EVIDENCE-OF: R-
103c0 35 35 35 33 30 2d 35 32 39 33 30 20 49 6e 20 61 55530-52930 In a
103d0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 62 2d 74 well-formed b-t
103e0 72 65 65 20 70 61 67 65 2c 20 74 68 65 72 65 20 ree page, there
103f0 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c will. ** al
10400 77 61 79 73 20 62 65 20 61 74 20 6c 65 61 73 74 ways be at least
10410 20 6f 6e 65 20 63 65 6c 6c 20 62 65 66 6f 72 65 one cell before
10420 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 the first freeb
10430 6c 6f 63 6b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 lock.. */.
10440 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
10450 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 28 TE_CORRUPT_PAGE(
10460 70 50 61 67 65 29 3b 20 0a 20 20 20 20 7d 0a 20 pPage); . }.
10470 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 while( 1 ){.
10480 20 20 20 20 20 69 66 28 20 70 63 3e 69 43 65 6c if( pc>iCel
10490 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20 lLast ){.
104a0 20 2f 2a 20 46 72 65 65 62 6c 6f 63 6b 20 6f 66 /* Freeblock of
104b0 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
104c0 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 page */.
104d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
104e0 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 ORRUPT_PAGE(pPag
104f0 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
10500 20 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 next = get2byt
10510 65 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 e(&data[pc]);.
10520 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 size = get2b
10530 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 yte(&data[pc+2])
10540 3b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 ;. nFree =
10550 6e 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 nFree + size;.
10560 20 20 20 20 69 66 28 20 6e 65 78 74 3c 3d 70 63 if( next<=pc
10570 2b 73 69 7a 65 2b 33 20 29 20 62 72 65 61 6b 3b +size+3 ) break;
10580 0a 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78 74 . pc = next
10590 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
105a0 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 next>0 ){.
105b0 2f 2a 20 46 72 65 65 62 6c 6f 63 6b 20 6e 6f 74 /* Freeblock not
105c0 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 in ascending or
105d0 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 der */. ret
105e0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
105f0 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 3b 0a PT_PAGE(pPage);.
10600 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 }. if( pc
10610 2b 73 69 7a 65 3e 28 75 6e 73 69 67 6e 65 64 20 +size>(unsigned
10620 69 6e 74 29 75 73 61 62 6c 65 53 69 7a 65 20 29 int)usableSize )
10630 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 {. /* Last
10640 66 72 65 65 62 6c 6f 63 6b 20 65 78 74 65 6e 64 freeblock extend
10650 73 20 70 61 73 74 20 70 61 67 65 20 65 6e 64 20 s past page end
10660 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
10670 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
10680 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 20 20 AGE(pPage);.
10690 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 }. }.. /* At t
106a0 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 his point, nFree
106b0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 contains the su
106c0 6d 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 m of the offset
106d0 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a to the start. *
106e0 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f * of the cell-co
106f0 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73 20 ntent area plus
10700 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 the number of fr
10710 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e 0a ee bytes within.
10720 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 2d 63 6f ** the cell-co
10730 6e 74 65 6e 74 20 61 72 65 61 2e 20 49 66 20 74 ntent area. If t
10740 68 69 73 20 69 73 20 67 72 65 61 74 65 72 20 74 his is greater t
10750 68 61 6e 20 74 68 65 20 75 73 61 62 6c 65 2d 73 han the usable-s
10760 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ize. ** of the
10770 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70 page, then the p
10780 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 age must be corr
10790 75 70 74 65 64 2e 20 54 68 69 73 20 63 68 65 63 upted. This chec
107a0 6b 20 61 6c 73 6f 0a 20 20 2a 2a 20 73 65 72 76 k also. ** serv
107b0 65 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 es to verify tha
107c0 74 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 t the offset to
107d0 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
107e0 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a 20 20 cell-content.
107f0 2a 2a 20 61 72 65 61 2c 20 61 63 63 6f 72 64 69 ** area, accordi
10800 6e 67 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 ng to the page h
10810 65 61 64 65 72 2c 20 6c 69 65 73 20 77 69 74 68 eader, lies with
10820 69 6e 20 74 68 65 20 70 61 67 65 2e 0a 20 20 2a in the page.. *
10830 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 3e 75 73 /. if( nFree>us
10840 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 ableSize ){.
10850 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
10860 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 RRUPT_PAGE(pPage
10870 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e );. }. pPage->
10880 6e 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e 46 nFree = (u16)(nF
10890 72 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 ree - iCellFirst
108a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
108b0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
108c0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 Initialize the a
108d0 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 uxiliary informa
108e0 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 tion for a disk
108f0 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 block..**.** Ret
10900 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
10910 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 success. If we
10920 20 73 65 65 20 74 68 61 74 20 74 68 65 20 70 61 see that the pa
10930 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 ge does.** not c
10940 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f ontain a well-fo
10950 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 70 61 rmed database pa
10960 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 ge, then return
10970 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 .** SQLITE_CORRU
10980 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61 PT. Note that a
10990 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 return of SQLIT
109a0 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a E_OK does not.**
109b0 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
109c0 74 68 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c the page is well
109d0 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c -formed. It onl
109e0 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 y shows that.**
109f0 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74 we failed to det
10a00 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69 ect any corrupti
10a10 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
10a20 74 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 t btreeInitPage(
10a30 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
10a40 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 . int pc;
10a50 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
10a60 20 6f 66 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 of a freeblock
10a70 77 69 74 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 within pPage->aD
10a80 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 20 68 64 ata[] */. u8 hd
10a90 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
10aa0 20 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e Offset to begin
10ab0 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 ning of page hea
10ac0 64 65 72 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 der */. u8 *dat
10ad0 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 a; /* E
10ae0 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 qual to pPage->a
10af0 44 61 74 61 20 2a 2f 0a 20 20 42 74 53 68 61 72 Data */. BtShar
10b00 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
10b10 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 /* The main btre
10b20 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
10b30 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b int usableSize;
10b40 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
10b50 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 6f 6e usable space on
10b60 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
10b70 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 u16 cellOffset;
10b80 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
10b90 6d 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 m start of page
10ba0 74 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f to first cell po
10bb0 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 inter */. int i
10bc0 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 2f 2a CellFirst; /*
10bd0 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 First allowable
10be0 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f cell or freeblo
10bf0 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 69 ck offset */. i
10c00 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 nt iCellLast;
10c10 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 /* Last possib
10c20 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 le cell or freeb
10c30 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 0a lock offset */..
10c40 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
10c50 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 >pBt!=0 );. ass
10c60 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d ert( pPage->pBt-
10c70 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 >db!=0 );. asse
10c80 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10c90 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
10ca0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
10cb0 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 ssert( pPage->pg
10cc0 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 no==sqlite3Pager
10cd0 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 Pagenumber(pPage
10ce0 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
10cf0 61 73 73 65 72 74 28 20 70 50 61 67 65 20 3d 3d assert( pPage ==
10d00 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
10d10 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 Extra(pPage->pDb
10d20 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
10d30 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 t( pPage->aData
10d40 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 == sqlite3PagerG
10d50 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 etData(pPage->pD
10d60 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
10d70 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
10d80 74 3d 3d 30 20 29 3b 0a 0a 20 20 70 42 74 20 3d t==0 );.. pBt =
10d90 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 68 pPage->pBt;. h
10da0 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
10db0 66 66 73 65 74 3b 0a 20 20 64 61 74 61 20 3d 20 ffset;. data =
10dc0 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
10dd0 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 /* EVIDENCE-OF:
10de0 52 2d 32 38 35 39 34 2d 30 32 38 39 30 20 54 68 R-28594-02890 Th
10df0 65 20 6f 6e 65 2d 62 79 74 65 20 66 6c 61 67 20 e one-byte flag
10e00 61 74 20 6f 66 66 73 65 74 20 30 20 69 6e 64 69 at offset 0 indi
10e10 63 61 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 cating. ** the
10e20 62 2d 74 72 65 65 20 70 61 67 65 20 74 79 70 65 b-tree page type
10e30 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 63 6f 64 . */. if( decod
10e40 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 64 61 eFlags(pPage, da
10e50 74 61 5b 68 64 72 5d 29 20 29 7b 0a 20 20 20 20 ta[hdr]) ){.
10e60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
10e70 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 RRUPT_PAGE(pPage
10e80 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
10e90 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d pBt->pageSize>=
10ea0 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 512 && pBt->page
10eb0 53 69 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 Size<=65536 );.
10ec0 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 pPage->maskPage
10ed0 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 70 61 = (u16)(pBt->pa
10ee0 67 65 53 69 7a 65 20 2d 20 31 29 3b 0a 20 20 70 geSize - 1);. p
10ef0 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
10f00 3d 20 30 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a = 0;. usableSiz
10f10 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
10f20 69 7a 65 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 ize;. pPage->ce
10f30 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f llOffset = cellO
10f40 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 38 20 ffset = hdr + 8
10f50 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 + pPage->childPt
10f60 72 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 2d 3e rSize;. pPage->
10f70 61 44 61 74 61 45 6e 64 20 3d 20 26 64 61 74 61 aDataEnd = &data
10f80 5b 75 73 61 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 [usableSize];.
10f90 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 20 pPage->aCellIdx
10fa0 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 = &data[cellOffs
10fb0 65 74 5d 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 et];. pPage->aD
10fc0 61 74 61 4f 66 73 74 20 3d 20 26 64 61 74 61 5b ataOfst = &data[
10fd0 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
10fe0 69 7a 65 5d 3b 0a 20 20 2f 2a 20 45 56 49 44 45 ize];. /* EVIDE
10ff0 4e 43 45 2d 4f 46 3a 20 52 2d 33 37 30 30 32 2d NCE-OF: R-37002-
11000 33 32 37 37 34 20 54 68 65 20 74 77 6f 2d 62 79 32774 The two-by
11010 74 65 20 69 6e 74 65 67 65 72 20 61 74 20 6f 66 te integer at of
11020 66 73 65 74 20 33 20 67 69 76 65 73 20 74 68 65 fset 3 gives the
11030 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 . ** number of
11040 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 cells on the pag
11050 65 2e 20 2a 2f 0a 20 20 70 50 61 67 65 2d 3e 6e e. */. pPage->n
11060 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 Cell = get2byte(
11070 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 &data[hdr+3]);.
11080 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c if( pPage->nCel
11090 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 l>MX_CELL(pBt) )
110a0 7b 0a 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 {. /* To many
110b0 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e cells for a sin
110c0 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 gle page. The p
110d0 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 age must be corr
110e0 75 70 74 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 upt */. retur
110f0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
11100 5f 50 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 _PAGE(pPage);.
11110 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 50 }. testcase( pP
11120 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f 43 age->nCell==MX_C
11130 45 4c 4c 28 70 42 74 29 20 29 3b 0a 20 20 2f 2a ELL(pBt) );. /*
11140 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d EVIDENCE-OF: R-
11150 32 34 30 38 39 2d 35 37 39 37 39 20 49 66 20 61 24089-57979 If a
11160 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 6e page contains n
11170 6f 20 63 65 6c 6c 73 20 28 77 68 69 63 68 20 69 o cells (which i
11180 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 70 6f 73 73 s only. ** poss
11190 69 62 6c 65 20 66 6f 72 20 61 20 72 6f 6f 74 20 ible for a root
111a0 70 61 67 65 20 6f 66 20 61 20 74 61 62 6c 65 20 page of a table
111b0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f that contains no
111c0 20 72 6f 77 73 29 20 74 68 65 6e 20 74 68 65 0a rows) then the.
111d0 20 20 2a 2a 20 6f 66 66 73 65 74 20 74 6f 20 74 ** offset to t
111e0 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
111f0 61 72 65 61 20 77 69 6c 6c 20 65 71 75 61 6c 20 area will equal
11200 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d 69 the page size mi
11210 6e 75 73 20 74 68 65 0a 20 20 2a 2a 20 62 79 74 nus the. ** byt
11220 65 73 20 6f 66 20 72 65 73 65 72 76 65 64 20 73 es of reserved s
11230 70 61 63 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 pace. */. asser
11240 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e t( pPage->nCell>
11250 30 0a 20 20 20 20 20 20 20 7c 7c 20 67 65 74 32 0. || get2
11260 62 79 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 byteNotZero(&dat
11270 61 5b 68 64 72 2b 35 5d 29 3d 3d 75 73 61 62 6c a[hdr+5])==usabl
11280 65 53 69 7a 65 0a 20 20 20 20 20 20 20 7c 7c 20 eSize. ||
11290 43 4f 52 52 55 50 54 5f 44 42 20 29 3b 0a 0a 20 CORRUPT_DB );..
112a0 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d 65 64 20 /* A malformed
112b0 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6d 69 database page mi
112c0 67 68 74 20 63 61 75 73 65 20 75 73 20 74 6f 20 ght cause us to
112d0 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e read past the en
112e0 64 0a 20 20 2a 2a 20 6f 66 20 70 61 67 65 20 77 d. ** of page w
112f0 68 65 6e 20 70 61 72 73 69 6e 67 20 61 20 63 65 hen parsing a ce
11300 6c 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ll. . **. **
11310 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
11320 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 ock of code chec
11330 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 ks early to see
11340 69 66 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 if a cell extend
11350 73 0a 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 s. ** past the
11360 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f end of a page bo
11370 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65 undary and cause
11380 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 s SQLITE_CORRUPT
11390 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20 72 65 74 to be . ** ret
113a0 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f 65 73 urned if it does
113b0 2e 0a 20 20 2a 2f 0a 20 20 69 43 65 6c 6c 46 69 .. */. iCellFi
113c0 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 rst = cellOffset
113d0 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c + 2*pPage->nCel
113e0 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d l;. iCellLast =
113f0 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b usableSize - 4;
11400 0a 20 20 69 66 28 20 70 42 74 2d 3e 64 62 2d 3e . if( pBt->db->
11410 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 flags & SQLITE_C
11420 65 6c 6c 53 69 7a 65 43 6b 20 29 7b 0a 20 20 20 ellSizeCk ){.
11430 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
11440 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f /* Index into
11450 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 the cell pointe
11460 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 69 r array */. i
11470 6e 74 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 nt sz;
11480 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 /* Size of a ce
11490 6c 6c 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 21 ll */.. if( !
114a0 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 pPage->leaf ) iC
114b0 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20 20 66 ellLast--;. f
114c0 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d or(i=0; i<pPage-
114d0 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 >nCell; i++){.
114e0 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 pc = get2byt
114f0 65 41 6c 69 67 6e 65 64 28 26 64 61 74 61 5b 63 eAligned(&data[c
11500 65 6c 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b ellOffset+i*2]);
11510 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
11520 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 pc==iCellFirst
11530 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
11540 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 e( pc==iCellLast
11550 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 );. if( pc
11560 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 <iCellFirst || p
11570 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 c>iCellLast ){.
11580 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
11590 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 LITE_CORRUPT_PAG
115a0 45 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 E(pPage);.
115b0 7d 0a 20 20 20 20 20 20 73 7a 20 3d 20 70 50 61 }. sz = pPa
115c0 67 65 2d 3e 78 43 65 6c 6c 53 69 7a 65 28 70 50 ge->xCellSize(pP
115d0 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b age, &data[pc]);
115e0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
115f0 20 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 69 pc+sz==usableSi
11600 7a 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ze );. if(
11610 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53 69 7a 65 pc+sz>usableSize
11620 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
11630 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
11640 54 5f 50 41 47 45 28 70 50 61 67 65 29 3b 0a 20 T_PAGE(pPage);.
11650 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
11660 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
11670 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2b 2b 3b f ) iCellLast++;
11680 0a 20 20 7d 20 20 0a 20 20 70 50 61 67 65 2d 3e . } . pPage->
11690 6e 46 72 65 65 20 3d 20 2d 31 3b 20 20 2f 2a 20 nFree = -1; /*
116a0 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 Indicate that th
116b0 69 73 20 76 61 6c 75 65 20 69 73 20 79 65 74 20 is value is yet
116c0 75 6e 63 6f 6d 70 75 74 65 64 20 2a 2f 0a 20 20 uncomputed */.
116d0 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
116e0 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 1;. return SQLI
116f0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
11700 53 65 74 20 75 70 20 61 20 72 61 77 20 70 61 67 Set up a raw pag
11710 65 20 73 6f 20 74 68 61 74 20 69 74 20 6c 6f 6f e so that it loo
11720 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 61 62 61 ks like a databa
11730 73 65 20 70 61 67 65 20 68 6f 6c 64 69 6e 67 0a se page holding.
11740 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 2e 0a 2a ** no entries..*
11750 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 /.static void ze
11760 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a roPage(MemPage *
11770 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 73 pPage, int flags
11780 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
11790 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 ar *data = pPage
117a0 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 53 68 61 ->aData;. BtSha
117b0 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
117c0 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 64 72 20 ->pBt;. u8 hdr
117d0 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
117e0 65 74 3b 0a 20 20 75 31 36 20 66 69 72 73 74 3b et;. u16 first;
117f0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
11800 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
11810 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 er(pPage->pDbPag
11820 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 e)==pPage->pgno
11830 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
11840 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
11850 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
11860 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 ) == (void*)pPag
11870 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 e );. assert( s
11880 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
11890 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 ta(pPage->pDbPag
118a0 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 20 e) == data );.
118b0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
118c0 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
118d0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
118e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
118f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
11900 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
11910 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61 if( pBt->btsFla
11920 67 73 20 26 20 42 54 53 5f 46 41 53 54 5f 53 45 gs & BTS_FAST_SE
11930 43 55 52 45 20 29 7b 0a 20 20 20 20 6d 65 6d 73 CURE ){. mems
11940 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 et(&data[hdr], 0
11950 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a , pBt->usableSiz
11960 65 20 2d 20 68 64 72 29 3b 0a 20 20 7d 0a 20 20 e - hdr);. }.
11970 64 61 74 61 5b 68 64 72 5d 20 3d 20 28 63 68 61 data[hdr] = (cha
11980 72 29 66 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 r)flags;. first
11990 20 3d 20 68 64 72 20 2b 20 28 28 66 6c 61 67 73 = hdr + ((flags
119a0 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 20 &PTF_LEAF)==0 ?
119b0 31 32 20 3a 20 38 29 3b 0a 20 20 6d 65 6d 73 65 12 : 8);. memse
119c0 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 t(&data[hdr+1],
119d0 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 0, 4);. data[hd
119e0 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 r+7] = 0;. put2
119f0 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
11a00 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 ], pBt->usableSi
11a10 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 ze);. pPage->nF
11a20 72 65 65 20 3d 20 28 75 31 36 29 28 70 42 74 2d ree = (u16)(pBt-
11a30 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 66 69 >usableSize - fi
11a40 72 73 74 29 3b 0a 20 20 64 65 63 6f 64 65 46 6c rst);. decodeFl
11a50 61 67 73 28 70 50 61 67 65 2c 20 66 6c 61 67 73 ags(pPage, flags
11a60 29 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c );. pPage->cell
11a70 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a Offset = first;.
11a80 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e pPage->aDataEn
11a90 64 20 3d 20 26 64 61 74 61 5b 70 42 74 2d 3e 75 d = &data[pBt->u
11aa0 73 61 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 70 50 sableSize];. pP
11ab0 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 age->aCellIdx =
11ac0 26 64 61 74 61 5b 66 69 72 73 74 5d 3b 0a 20 20 &data[first];.
11ad0 70 50 61 67 65 2d 3e 61 44 61 74 61 4f 66 73 74 pPage->aDataOfst
11ae0 20 3d 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e = &data[pPage->
11af0 63 68 69 6c 64 50 74 72 53 69 7a 65 5d 3b 0a 20 childPtrSize];.
11b00 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
11b10 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 w = 0;. assert(
11b20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d pBt->pageSize>=
11b30 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 512 && pBt->page
11b40 53 69 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 Size<=65536 );.
11b50 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 pPage->maskPage
11b60 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 70 61 = (u16)(pBt->pa
11b70 67 65 53 69 7a 65 20 2d 20 31 29 3b 0a 20 20 70 geSize - 1);. p
11b80 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b Page->nCell = 0;
11b90 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 . pPage->isInit
11ba0 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 = 1;.}.../*.**
11bb0 43 6f 6e 76 65 72 74 20 61 20 44 62 50 61 67 65 Convert a DbPage
11bc0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 obtained from t
11bd0 68 65 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20 he pager into a
11be0 4d 65 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a MemPage used by.
11bf0 2a 2a 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 ** the btree lay
11c00 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 er..*/.static Me
11c10 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 mPage *btreePage
11c20 46 72 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67 FromDbPage(DbPag
11c30 65 20 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f e *pDbPage, Pgno
11c40 20 70 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 pgno, BtShared
11c50 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 *pBt){. MemPage
11c60 20 2a 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 *pPage = (MemPa
11c70 67 65 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 ge*)sqlite3Pager
11c80 47 65 74 45 78 74 72 61 28 70 44 62 50 61 67 65 GetExtra(pDbPage
11c90 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 21 3d 70 );. if( pgno!=p
11ca0 50 61 67 65 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 Page->pgno ){.
11cb0 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d pPage->aData =
11cc0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
11cd0 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
11ce0 20 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 pPage->pDbPag
11cf0 65 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 20 e = pDbPage;.
11d00 20 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 pPage->pBt = pB
11d10 74 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 67 t;. pPage->pg
11d20 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 70 no = pgno;. p
11d30 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 Page->hdrOffset
11d40 3d 20 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 = pgno==1 ? 100
11d50 3a 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 : 0;. }. asser
11d60 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3d t( pPage->aData=
11d70 3d 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 =sqlite3PagerGet
11d80 44 61 74 61 28 70 44 62 50 61 67 65 29 20 29 3b Data(pDbPage) );
11d90 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b . return pPage;
11da0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 .}../*.** Get a
11db0 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
11dc0 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a ager. Initializ
11dd0 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 e the MemPage.pB
11de0 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 t and.** MemPage
11df0 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 .aData elements
11e00 69 66 20 6e 65 65 64 65 64 2e 20 20 53 65 65 20 if needed. See
11e10 61 6c 73 6f 3a 20 62 74 72 65 65 47 65 74 55 6e also: btreeGetUn
11e20 75 73 65 64 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a usedPage()..**.*
11e30 2a 20 49 66 20 74 68 65 20 50 41 47 45 52 5f 47 * If the PAGER_G
11e40 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 66 6c 61 ET_NOCONTENT fla
11e50 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d 65 61 g is set, it mea
11e60 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f ns that we do no
11e70 74 20 63 61 72 65 0a 2a 2a 20 61 62 6f 75 74 20 t care.** about
11e80 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
11e90 68 65 20 70 61 67 65 20 61 74 20 74 68 69 73 20 he page at this
11ea0 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 time. So do not
11eb0 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 0a go to the disk.
11ec0 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 ** to fetch the
11ed0 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 20 66 content. Just f
11ee0 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 ill in the conte
11ef0 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 66 6f nt with zeros fo
11f00 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 6e 20 r now..** If in
11f10 74 68 65 20 66 75 74 75 72 65 20 77 65 20 63 61 the future we ca
11f20 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 ll sqlite3PagerW
11f30 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 20 70 rite() on this p
11f40 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d 65 61 age, that.** mea
11f50 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 72 74 ns we have start
11f60 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 72 6e ed to be concern
11f70 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 6e 74 ed about content
11f80 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a 2a 2a and the disk.**
11f90 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 read should occ
11fa0 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 ur at that point
11fb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11fc0 62 74 72 65 65 47 65 74 50 61 67 65 28 0a 20 20 btreeGetPage(.
11fd0 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
11fe0 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 /* The btre
11ff0 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
12000 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
12010 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
12020 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 e to fetch */.
12030 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 MemPage **ppPage
12040 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 , /* Return t
12050 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 he page in this
12060 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 69 parameter */. i
12070 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 nt flags
12080 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 47 45 54 /* PAGER_GET
12090 5f 4e 4f 43 4f 4e 54 45 4e 54 20 6f 72 20 50 41 _NOCONTENT or PA
120a0 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c 59 GER_GET_READONLY
120b0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
120c0 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
120d0 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 66 ge;.. assert( f
120e0 6c 61 67 73 3d 3d 30 20 7c 7c 20 66 6c 61 67 73 lags==0 || flags
120f0 3d 3d 50 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f ==PAGER_GET_NOCO
12100 4e 54 45 4e 54 20 7c 7c 20 66 6c 61 67 73 3d 3d NTENT || flags==
12110 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e PAGER_GET_READON
12120 4c 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 LY );. assert(
12130 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
12140 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
12150 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
12160 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 PagerGet(pBt->pP
12170 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 ager, pgno, (DbP
12180 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 age**)&pDbPage,
12190 66 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 flags);. if( rc
121a0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
121b0 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 *ppPage = btreeP
121c0 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 ageFromDbPage(pD
121d0 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 bPage, pgno, pBt
121e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
121f0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
12200 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20 Retrieve a page
12210 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63 from the pager c
12220 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71 ache. If the req
12230 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e uested page is n
12240 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e ot.** already in
12250 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
12260 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e return NULL. In
12270 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d itialize the Mem
12280 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 Page.pBt and.**
12290 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c MemPage.aData el
122a0 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 ements if needed
122b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 ..*/.static MemP
122c0 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f age *btreePageLo
122d0 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 okup(BtShared *p
122e0 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
122f0 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
12300 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c e;. assert( sql
12310 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
12320 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
12330 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 pDbPage = sqlit
12340 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 e3PagerLookup(pB
12350 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 t->pPager, pgno)
12360 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20 ;. if( pDbPage
12370 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 ){. return bt
12380 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
12390 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
123a0 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 pBt);. }. ret
123b0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
123c0 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
123d0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
123e0 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 file in pages. I
123f0 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b f there is any k
12400 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c ind of.** error,
12410 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e return ((unsign
12420 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 ed int)-1)..*/.s
12430 74 61 74 69 63 20 50 67 6e 6f 20 62 74 72 65 65 tatic Pgno btree
12440 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 Pagecount(BtShar
12450 65 64 20 2a 70 42 74 29 7b 0a 20 20 72 65 74 75 ed *pBt){. retu
12460 72 6e 20 70 42 74 2d 3e 6e 50 61 67 65 3b 0a 7d rn pBt->nPage;.}
12470 0a 75 33 32 20 73 71 6c 69 74 65 33 42 74 72 65 .u32 sqlite3Btre
12480 65 4c 61 73 74 50 61 67 65 28 42 74 72 65 65 20 eLastPage(Btree
12490 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 *p){. assert( s
124a0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
124b0 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
124c0 73 65 72 74 28 20 28 28 70 2d 3e 70 42 74 2d 3e sert( ((p->pBt->
124d0 6e 50 61 67 65 29 26 30 78 38 30 30 30 30 30 30 nPage)&0x8000000
124e0 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 0)==0 );. retur
124f0 6e 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 n btreePagecount
12500 28 70 2d 3e 70 42 74 29 3b 0a 7d 0a 0a 2f 2a 0a (p->pBt);.}../*.
12510 2a 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72 ** Get a page fr
12520 6f 6d 20 74 68 65 20 70 61 67 65 72 20 61 6e 64 om the pager and
12530 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 0a initialize it..
12540 2a 2a 0a 2a 2a 20 49 66 20 70 43 75 72 21 3d 30 **.** If pCur!=0
12550 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 then the page i
12560 73 20 62 65 69 6e 67 20 66 65 74 63 68 65 64 20 s being fetched
12570 61 73 20 70 61 72 74 20 6f 66 20 61 20 6d 6f 76 as part of a mov
12580 65 54 6f 43 68 69 6c 64 28 29 0a 2a 2a 20 63 61 eToChild().** ca
12590 6c 6c 2e 20 20 44 6f 20 61 64 64 69 74 69 6f 6e ll. Do addition
125a0 61 6c 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 al sanity checki
125b0 6e 67 20 6f 6e 20 74 68 65 20 70 61 67 65 20 69 ng on the page i
125c0 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 20 n this case..**
125d0 41 6e 64 20 69 66 20 74 68 65 20 66 65 74 63 68 And if the fetch
125e0 20 66 61 69 6c 73 2c 20 74 68 69 73 20 72 6f 75 fails, this rou
125f0 74 69 6e 65 20 6d 75 73 74 20 64 65 63 72 65 6d tine must decrem
12600 65 6e 74 20 70 43 75 72 2d 3e 69 50 61 67 65 2e ent pCur->iPage.
12610 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
12620 69 73 20 66 65 74 63 68 65 64 20 61 73 20 72 65 is fetched as re
12630 61 64 2d 77 72 69 74 65 20 75 6e 6c 65 73 73 20 ad-write unless
12640 70 43 75 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c pCur is not NULL
12650 20 61 6e 64 20 69 73 0a 2a 2a 20 61 20 72 65 61 and is.** a rea
12660 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 2e 0a 2a d-only cursor..*
12670 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
12680 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 2a 70 occurs, then *p
12690 70 50 61 67 65 20 69 73 20 75 6e 64 65 66 69 6e pPage is undefin
126a0 65 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 ed. It.** may re
126b0 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 main unchanged,
126c0 6f 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 or it may be set
126d0 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 to an invalid v
126e0 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 alue..*/.static
126f0 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 int getAndInitPa
12700 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
12710 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 pBt,
12720 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
12730 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
12740 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 Pgno pgno,
12750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12760 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 /* Number of the
12770 20 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f 0a page to get */.
12780 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 MemPage **ppPa
12790 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ge,
127a0 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 /* Write the p
127b0 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 age pointer here
127c0 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
127d0 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 pCur,
127e0 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
127f0 74 6f 20 72 65 63 65 69 76 65 20 74 68 65 20 70 to receive the p
12800 61 67 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a age, or NULL */.
12810 20 20 69 6e 74 20 62 52 65 61 64 4f 6e 6c 79 20 int bReadOnly
12820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12830 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
12840 72 65 61 64 2d 6f 6e 6c 79 20 70 61 67 65 20 2a read-only page *
12850 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
12860 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
12870 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
12880 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
12890 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
128a0 61 73 73 65 72 74 28 20 70 43 75 72 3d 3d 30 20 assert( pCur==0
128b0 7c 7c 20 70 70 50 61 67 65 3d 3d 26 70 43 75 72 || ppPage==&pCur
128c0 2d 3e 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 ->pPage );. ass
128d0 65 72 74 28 20 70 43 75 72 3d 3d 30 20 7c 7c 20 ert( pCur==0 ||
128e0 62 52 65 61 64 4f 6e 6c 79 3d 3d 70 43 75 72 2d bReadOnly==pCur-
128f0 3e 63 75 72 50 61 67 65 72 46 6c 61 67 73 20 29 >curPagerFlags )
12900 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
12910 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 69 50 61 ==0 || pCur->iPa
12920 67 65 3e 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 ge>0 );.. if( p
12930 67 6e 6f 3e 62 74 72 65 65 50 61 67 65 63 6f 75 gno>btreePagecou
12940 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 nt(pBt) ){. r
12950 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
12960 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 PT_BKPT;. got
12970 6f 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 o getAndInitPage
12980 5f 65 72 72 6f 72 31 3b 0a 20 20 7d 0a 20 20 72 _error1;. }. r
12990 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
129a0 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c Get(pBt->pPager,
129b0 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a pgno, (DbPage**
129c0 29 26 70 44 62 50 61 67 65 2c 20 62 52 65 61 64 )&pDbPage, bRead
129d0 4f 6e 6c 79 29 3b 0a 20 20 69 66 28 20 72 63 20 Only);. if( rc
129e0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 67 65 74 41 ){. goto getA
129f0 6e 64 49 6e 69 74 50 61 67 65 5f 65 72 72 6f 72 ndInitPage_error
12a00 31 3b 0a 20 20 7d 0a 20 20 2a 70 70 50 61 67 65 1;. }. *ppPage
12a10 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c = (MemPage*)sql
12a20 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
12a30 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 a(pDbPage);. if
12a40 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 ( (*ppPage)->isI
12a50 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 62 74 nit==0 ){. bt
12a60 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
12a70 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
12a80 20 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 pBt);. rc =
12a90 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a 70 btreeInitPage(*p
12aa0 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 pPage);. if(
12ab0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12ac0 0a 20 20 20 20 20 20 67 6f 74 6f 20 67 65 74 41 . goto getA
12ad0 6e 64 49 6e 69 74 50 61 67 65 5f 65 72 72 6f 72 ndInitPage_error
12ae0 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 2;. }. }. a
12af0 73 73 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 ssert( (*ppPage)
12b00 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a ->pgno==pgno );.
12b10 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 assert( (*ppPa
12b20 67 65 29 2d 3e 61 44 61 74 61 3d 3d 73 71 6c 69 ge)->aData==sqli
12b30 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
12b40 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f pDbPage) );.. /
12b50 2a 20 49 66 20 6f 62 74 61 69 6e 69 6e 67 20 61 * If obtaining a
12b60 20 63 68 69 6c 64 20 70 61 67 65 20 66 6f 72 20 child page for
12b70 61 20 63 75 72 73 6f 72 2c 20 77 65 20 6d 75 73 a cursor, we mus
12b80 74 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 t verify that th
12b90 65 20 70 61 67 65 20 69 73 0a 20 20 2a 2a 20 63 e page is. ** c
12ba0 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74 ompatible with t
12bb0 68 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 2a 2f he root page. */
12bc0 0a 20 20 69 66 28 20 70 43 75 72 20 26 26 20 28 . if( pCur && (
12bd0 28 2a 70 70 50 61 67 65 29 2d 3e 6e 43 65 6c 6c (*ppPage)->nCell
12be0 3c 31 20 7c 7c 20 28 2a 70 70 50 61 67 65 29 2d <1 || (*ppPage)-
12bf0 3e 69 6e 74 4b 65 79 21 3d 70 43 75 72 2d 3e 63 >intKey!=pCur->c
12c00 75 72 49 6e 74 4b 65 79 29 20 29 7b 0a 20 20 20 urIntKey) ){.
12c10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
12c20 52 55 50 54 5f 50 47 4e 4f 28 70 67 6e 6f 29 3b RUPT_PGNO(pgno);
12c30 0a 20 20 20 20 67 6f 74 6f 20 67 65 74 41 6e 64 . goto getAnd
12c40 49 6e 69 74 50 61 67 65 5f 65 72 72 6f 72 32 3b InitPage_error2;
12c50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
12c60 4c 49 54 45 5f 4f 4b 3b 0a 0a 67 65 74 41 6e 64 LITE_OK;..getAnd
12c70 49 6e 69 74 50 61 67 65 5f 65 72 72 6f 72 32 3a InitPage_error2:
12c80 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a . releasePage(*
12c90 70 70 50 61 67 65 29 3b 0a 67 65 74 41 6e 64 49 ppPage);.getAndI
12ca0 6e 69 74 50 61 67 65 5f 65 72 72 6f 72 31 3a 0a nitPage_error1:.
12cb0 20 20 69 66 28 20 70 43 75 72 20 29 7b 0a 20 20 if( pCur ){.
12cc0 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b pCur->iPage--;
12cd0 0a 20 20 20 20 70 43 75 72 2d 3e 70 50 61 67 65 . pCur->pPage
12ce0 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
12cf0 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
12d00 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 67 }. testcase( pg
12d10 6e 6f 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 no==0 );. asser
12d20 74 28 20 70 67 6e 6f 21 3d 30 20 7c 7c 20 72 63 t( pgno!=0 || rc
12d30 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 ==SQLITE_CORRUPT
12d40 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
12d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 .}../*.** Releas
12d60 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 e a MemPage. Th
12d70 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c is should be cal
12d80 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 led once for eac
12d90 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 h prior.** call
12da0 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 2e to btreeGetPage.
12db0 0a 2a 2a 0a 2a 2a 20 50 61 67 65 31 20 69 73 20 .**.** Page1 is
12dc0 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 61 a special case a
12dd0 6e 64 20 6d 75 73 74 20 62 65 20 72 65 6c 65 61 nd must be relea
12de0 73 65 64 20 75 73 69 6e 67 20 72 65 6c 65 61 73 sed using releas
12df0 65 50 61 67 65 4f 6e 65 28 29 2e 0a 2a 2f 0a 73 ePageOne()..*/.s
12e00 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
12e10 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 4d 65 sePageNotNull(Me
12e20 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 mPage *pPage){.
12e30 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
12e40 61 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 aData );. asser
12e50 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b t( pPage->pBt );
12e60 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12e70 2d 3e 70 44 62 50 61 67 65 21 3d 30 20 29 3b 0a ->pDbPage!=0 );.
12e80 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
12e90 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 3PagerGetExtra(p
12ea0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d Page->pDbPage) =
12eb0 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 = (void*)pPage )
12ec0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
12ed0 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
12ee0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d pPage->pDbPage)=
12ef0 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b =pPage->aData );
12f00 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
12f10 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
12f20 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
12f30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 );. sqlite3Pag
12f40 65 72 55 6e 72 65 66 4e 6f 74 4e 75 6c 6c 28 70 erUnrefNotNull(p
12f50 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
12f60 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 }.static void re
12f70 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 leasePage(MemPag
12f80 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 e *pPage){. if(
12f90 20 70 50 61 67 65 20 29 20 72 65 6c 65 61 73 65 pPage ) release
12fa0 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 70 50 61 67 PageNotNull(pPag
12fb0 65 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 e);.}.static voi
12fc0 64 20 72 65 6c 65 61 73 65 50 61 67 65 4f 6e 65 d releasePageOne
12fd0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
12fe0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 {. assert( pPag
12ff0 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e!=0 );. assert
13000 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 ( pPage->aData )
13010 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
13020 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 e->pBt );. asse
13030 72 74 28 20 70 50 61 67 65 2d 3e 70 44 62 50 61 rt( pPage->pDbPa
13040 67 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ge!=0 );. asser
13050 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
13060 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 etExtra(pPage->p
13070 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 DbPage) == (void
13080 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 *)pPage );. ass
13090 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
130a0 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e rGetData(pPage->
130b0 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d pDbPage)==pPage-
130c0 3e 61 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 >aData );. asse
130d0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
130e0 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
130f0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 t->mutex) );. s
13100 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
13110 50 61 67 65 4f 6e 65 28 70 50 61 67 65 2d 3e 70 PageOne(pPage->p
13120 44 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a DbPage);.}../*.*
13130 2a 20 47 65 74 20 61 6e 20 75 6e 75 73 65 64 20 * Get an unused
13140 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 page..**.** This
13150 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 works just like
13160 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20 btreeGetPage()
13170 77 69 74 68 20 74 68 65 20 61 64 64 69 74 69 6f with the additio
13180 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 49 66 n:.**.** * If
13190 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 the page is alr
131a0 65 61 64 79 20 69 6e 20 75 73 65 20 66 6f 72 20 eady in use for
131b0 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f some other purpo
131c0 73 65 2c 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a se, immediately.
131d0 2a 2a 20 20 20 20 20 20 72 65 6c 65 61 73 65 20 ** release
131e0 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e it and return an
131f0 20 53 51 4c 49 54 45 5f 43 55 52 52 55 50 54 20 SQLITE_CURRUPT
13200 65 72 72 6f 72 2e 0a 2a 2a 20 20 20 2a 20 20 4d error..** * M
13210 61 6b 65 20 73 75 72 65 20 74 68 65 20 69 73 49 ake sure the isI
13220 6e 69 74 20 66 6c 61 67 20 69 73 20 63 6c 65 61 nit flag is clea
13230 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 r.*/.static int
13240 62 74 72 65 65 47 65 74 55 6e 75 73 65 64 50 61 btreeGetUnusedPa
13250 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
13260 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 pBt, /* Th
13270 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e e btree */. Pgn
13280 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
13290 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
132a0 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 he page to fetch
132b0 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
132c0 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 ppPage, /* Re
132d0 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e turn the page in
132e0 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 this parameter
132f0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 */. int flags
13300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 /* PAG
13310 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 ER_GET_NOCONTENT
13320 20 6f 72 20 50 41 47 45 52 5f 47 45 54 5f 52 45 or PAGER_GET_RE
13330 41 44 4f 4e 4c 59 20 2a 2f 0a 29 7b 0a 20 20 69 ADONLY */.){. i
13340 6e 74 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 nt rc = btreeGet
13350 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 Page(pBt, pgno,
13360 70 70 50 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a ppPage, flags);.
13370 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13380 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 _OK ){. if( s
13390 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 qlite3PagerPageR
133a0 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 efcount((*ppPage
133b0 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b )->pDbPage)>1 ){
133c0 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
133d0 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
133e0 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a *ppPage = 0;.
133f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
13400 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
13410 3b 0a 20 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 ;. }. (*pp
13420 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 Page)->isInit =
13430 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
13440 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d *ppPage = 0;. }
13450 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
13460 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 ../*.** During a
13470 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 rollback, when
13480 74 68 65 20 70 61 67 65 72 20 72 65 6c 6f 61 64 the pager reload
13490 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e s information in
134a0 74 6f 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 to the cache.**
134b0 73 6f 20 74 68 61 74 20 74 68 65 20 63 61 63 68 so that the cach
134c0 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 6f e is restored to
134d0 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 its original st
134e0 61 74 65 20 61 74 20 74 68 65 20 73 74 61 72 74 ate at the start
134f0 20 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 of.** the trans
13500 61 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 action, for each
13510 20 70 61 67 65 20 72 65 73 74 6f 72 65 64 20 74 page restored t
13520 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
13530 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 alled..**.** Thi
13540 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 s routine needs
13550 74 6f 20 72 65 73 65 74 20 74 68 65 20 65 78 74 to reset the ext
13560 72 61 20 64 61 74 61 20 73 65 63 74 69 6f 6e 20 ra data section
13570 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
13580 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 67 72 e.** page to agr
13590 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73 74 ee with the rest
135a0 6f 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 ored data..*/.st
135b0 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 52 65 atic void pageRe
135c0 69 6e 69 74 28 44 62 50 61 67 65 20 2a 70 44 61 init(DbPage *pDa
135d0 74 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ta){. MemPage *
135e0 70 50 61 67 65 3b 0a 20 20 70 50 61 67 65 20 3d pPage;. pPage =
135f0 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 (MemPage *)sqli
13600 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
13610 28 70 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72 (pData);. asser
13620 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 t( sqlite3PagerP
13630 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 ageRefcount(pDat
13640 61 29 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 50 a)>0 );. if( pP
13650 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 age->isInit ){.
13660 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
13670 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
13680 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
13690 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 );. pPage->i
136a0 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 sInit = 0;. i
136b0 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 f( sqlite3PagerP
136c0 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 ageRefcount(pDat
136d0 61 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a a)>1 ){. /*
136e0 20 70 50 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 pPage might not
136f0 20 62 65 20 61 20 62 74 72 65 65 20 70 61 67 65 be a btree page
13700 3b 20 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 ; it might be a
13710 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0a n overflow page.
13720 20 20 20 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d ** or ptrm
13730 61 70 20 70 61 67 65 20 6f 72 20 61 20 66 72 65 ap page or a fre
13740 65 20 70 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 e page. In thos
13750 65 20 63 61 73 65 73 2c 20 74 68 65 20 66 6f 6c e cases, the fol
13760 6c 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 lowing. **
13770 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 call to btreeIni
13780 74 50 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b tPage() will lik
13790 65 6c 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ely return SQLIT
137a0 45 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 E_CORRUPT..
137b0 20 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 ** But no harm
137c0 69 73 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e is done by this.
137d0 20 20 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 And it is very
137e0 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a important that.
137f0 20 20 20 20 20 20 2a 2a 20 62 74 72 65 65 49 6e ** btreeIn
13800 69 74 50 61 67 65 28 29 20 62 65 20 63 61 6c 6c itPage() be call
13810 65 64 20 6f 6e 20 65 76 65 72 79 20 62 74 72 65 ed on every btre
13820 65 20 70 61 67 65 20 73 6f 20 77 65 20 6d 61 6b e page so we mak
13830 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 e. ** the c
13840 61 6c 6c 20 66 6f 72 20 65 76 65 72 79 20 70 61 all for every pa
13850 67 65 20 74 68 61 74 20 63 6f 6d 65 73 20 69 6e ge that comes in
13860 20 66 6f 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e for re-initing.
13870 20 2a 2f 0a 20 20 20 20 20 20 62 74 72 65 65 49 */. btreeI
13880 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a nitPage(pPage);.
13890 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
138a0 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 ** Invoke the bu
138b0 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 sy handler for a
138c0 20 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 btree..*/.stati
138d0 63 20 69 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b c int btreeInvok
138e0 65 42 75 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 eBusyHandler(voi
138f0 64 20 2a 70 41 72 67 29 7b 0a 20 20 42 74 53 68 d *pArg){. BtSh
13900 61 72 65 64 20 2a 70 42 74 20 3d 20 28 42 74 53 ared *pBt = (BtS
13910 68 61 72 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 hared*)pArg;. a
13920 73 73 65 72 74 28 20 70 42 74 2d 3e 64 62 20 29 ssert( pBt->db )
13930 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
13940 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
13950 42 74 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 Bt->db->mutex) )
13960 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 ;. return sqlit
13970 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 e3InvokeBusyHand
13980 6c 65 72 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 ler(&pBt->db->bu
13990 73 79 48 61 6e 64 6c 65 72 2c 0a 20 20 20 20 20 syHandler,.
139a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139b0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
139c0 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 ite3PagerFile(pB
139d0 74 2d 3e 70 50 61 67 65 72 29 29 3b 0a 7d 0a 0a t->pPager));.}..
139e0 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 64 61 74 /*.** Open a dat
139f0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a abase file..** .
13a00 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 ** zFilename is
13a10 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
13a20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
13a30 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 If zFilename is
13a40 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 NULL.** then an
13a50 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 62 61 ephemeral databa
13a60 73 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 se is created.
13a70 54 68 65 20 65 70 68 65 6d 65 72 61 6c 20 64 61 The ephemeral da
13a80 74 61 62 61 73 65 20 6d 69 67 68 74 0a 2a 2a 20 tabase might.**
13a90 62 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20 69 be exclusively i
13aa0 6e 20 6d 65 6d 6f 72 79 2c 20 6f 72 20 69 74 20 n memory, or it
13ab0 6d 69 67 68 74 20 75 73 65 20 61 20 64 69 73 6b might use a disk
13ac0 2d 62 61 73 65 64 20 6d 65 6d 6f 72 79 20 63 61 -based memory ca
13ad0 63 68 65 2e 0a 2a 2a 20 45 69 74 68 65 72 20 77 che..** Either w
13ae0 61 79 2c 20 74 68 65 20 65 70 68 65 6d 65 72 61 ay, the ephemera
13af0 6c 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 l database will
13b00 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
13b10 20 64 65 6c 65 74 65 64 20 0a 2a 2a 20 77 68 65 deleted .** whe
13b20 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c n sqlite3BtreeCl
13b30 6f 73 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ose() is called.
13b40 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e .**.** If zFilen
13b50 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a ame is ":memory:
13b60 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d " then an in-mem
13b70 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20 ory database is
13b80 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 20 created.** that
13b90 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 is automatically
13ba0 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 destroyed when
13bb0 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a it is closed..**
13bc0 0a 2a 2a 20 54 68 65 20 22 66 6c 61 67 73 22 20 .** The "flags"
13bd0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 parameter is a b
13be0 69 74 6d 61 73 6b 20 74 68 61 74 20 6d 69 67 68 itmask that migh
13bf0 74 20 63 6f 6e 74 61 69 6e 20 62 69 74 73 20 6c t contain bits l
13c00 69 6b 65 0a 2a 2a 20 42 54 52 45 45 5f 4f 4d 49 ike.** BTREE_OMI
13c10 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 2f 6f 72 T_JOURNAL and/or
13c20 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 2e 0a 2a BTREE_MEMORY..*
13c30 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 *.** If the data
13c40 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 base is already
13c50 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61 opened in the sa
13c60 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
13c70 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 ection.** and we
13c80 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20 63 are in shared c
13c90 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20 ache mode, then
13ca0 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61 the open will fa
13cb0 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 il with an.** SQ
13cc0 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
13cd0 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f error. We canno
13ce0 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d t allow two or m
13cf0 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20 ore BtShared.**
13d00 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 objects in the s
13d10 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
13d20 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f nection since do
13d30 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64 ing so will lead
13d40 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20 .** to problems
13d50 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f with locking..*/
13d60 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
13d70 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 eOpen(. sqlite3
13d80 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
13d90 20 2f 2a 20 56 46 53 20 74 6f 20 75 73 65 20 66 /* VFS to use f
13da0 6f 72 20 74 68 69 73 20 62 2d 74 72 65 65 20 2a or this b-tree *
13db0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
13dc0 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e zFilename, /* N
13dd0 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ame of the file
13de0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 42 containing the B
13df0 54 72 65 65 20 64 61 74 61 62 61 73 65 20 2a 2f Tree database */
13e00 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
13e10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 /* As
13e20 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 sociated databas
13e30 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 e handle */. Bt
13e40 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 ree **ppBtree,
13e50 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
13e60 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20 6f 62 to new Btree ob
13e70 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68 65 72 ject written her
13e80 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
13e90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
13ea0 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 * Options */. i
13eb0 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 nt vfsFlags
13ec0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
13ed0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 passed through t
13ee0 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f o sqlite3_vfs.xO
13ef0 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 42 74 pen() */.){. Bt
13f00 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b Shared *pBt = 0;
13f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13f20 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62 Shared part of b
13f30 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a tree structure *
13f40 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 /. Btree *p;
13f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13f60 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 /* Handle to
13f70 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 return */. sqli
13f80 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
13f90 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 Open = 0; /* Pr
13fa0 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f events a race co
13fb0 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 ndition. Ticket
13fc0 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 #3537 */. int r
13fd0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
13fe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
13ff0 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 ult code from th
14000 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 is function */.
14010 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20 u8 nReserve;
14020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14030 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65 /* Byte of unuse
14040 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20 d space on each
14050 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e page */. unsign
14060 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 ed char zDbHeade
14070 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 r[100]; /* Data
14080 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74 base header cont
14090 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 54 72 75 ent */.. /* Tru
140a0 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20 e if opening an
140b0 65 70 68 65 6d 65 72 61 6c 2c 20 74 65 6d 70 6f ephemeral, tempo
140c0 72 61 72 79 20 64 61 74 61 62 61 73 65 20 2a 2f rary database */
140d0 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 54 . const int isT
140e0 65 6d 70 44 62 20 3d 20 7a 46 69 6c 65 6e 61 6d empDb = zFilenam
140f0 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d e==0 || zFilenam
14100 65 5b 30 5d 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 53 e[0]==0;.. /* S
14110 65 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 et the variable
14120 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 isMemdb to true
14130 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
14140 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 database, or .
14150 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 ** false for a
14160 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 file-based datab
14170 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 ase.. */.#ifdef
14180 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d SQLITE_OMIT_MEM
14190 4f 52 59 44 42 0a 20 20 63 6f 6e 73 74 20 69 6e ORYDB. const in
141a0 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a 23 t isMemdb = 0;.#
141b0 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 69 6e 74 else. const int
141c0 20 69 73 4d 65 6d 64 62 20 3d 20 28 7a 46 69 6c isMemdb = (zFil
141d0 65 6e 61 6d 65 20 26 26 20 73 74 72 63 6d 70 28 ename && strcmp(
141e0 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d zFilename, ":mem
141f0 6f 72 79 3a 22 29 3d 3d 30 29 0a 20 20 20 20 20 ory:")==0).
14200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14210 20 20 7c 7c 20 28 69 73 54 65 6d 70 44 62 20 26 || (isTempDb &
14220 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d & sqlite3TempInM
14230 65 6d 6f 72 79 28 64 62 29 29 0a 20 20 20 20 20 emory(db)).
14240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14250 20 20 7c 7c 20 28 76 66 73 46 6c 61 67 73 20 26 || (vfsFlags &
14260 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d SQLITE_OPEN_MEM
14270 4f 52 59 29 21 3d 30 3b 0a 23 65 6e 64 69 66 0a ORY)!=0;.#endif.
14280 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 . assert( db!=0
14290 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 );. assert( pV
142a0 66 73 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 fs!=0 );. asser
142b0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
142c0 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
142d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 66 );. assert( (f
142e0 6c 61 67 73 26 30 78 66 66 29 3d 3d 66 6c 61 67 lags&0xff)==flag
142f0 73 20 29 3b 20 20 20 2f 2a 20 66 6c 61 67 73 20 s ); /* flags
14300 66 69 74 20 69 6e 20 38 20 62 69 74 73 20 2a 2f fit in 8 bits */
14310 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20 61 20 42 54 .. /* Only a BT
14320 52 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 62 REE_SINGLE datab
14330 61 73 65 20 63 61 6e 20 62 65 20 42 54 52 45 45 ase can be BTREE
14340 5f 55 4e 4f 52 44 45 52 45 44 20 2a 2f 0a 20 20 _UNORDERED */.
14350 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 26 assert( (flags &
14360 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 BTREE_UNORDERED
14370 29 3d 3d 30 20 7c 7c 20 28 66 6c 61 67 73 20 26 )==0 || (flags &
14380 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 21 3d BTREE_SINGLE)!=
14390 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 42 54 52 0 );.. /* A BTR
143a0 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 62 61 EE_SINGLE databa
143b0 73 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 74 se is always a t
143c0 65 6d 70 6f 72 61 72 79 20 61 6e 64 2f 6f 72 20 emporary and/or
143d0 65 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 20 20 61 ephemeral */. a
143e0 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 26 20 ssert( (flags &
143f0 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 3d 3d 30 BTREE_SINGLE)==0
14400 20 7c 7c 20 69 73 54 65 6d 70 44 62 20 29 3b 0a || isTempDb );.
14410 0a 20 20 69 66 28 20 69 73 4d 65 6d 64 62 20 29 . if( isMemdb )
14420 7b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 42 {. flags |= B
14430 54 52 45 45 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d TREE_MEMORY;. }
14440 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61 67 73 . if( (vfsFlags
14450 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & SQLITE_OPEN_M
14460 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20 28 69 AIN_DB)!=0 && (i
14470 73 4d 65 6d 64 62 20 7c 7c 20 69 73 54 65 6d 70 sMemdb || isTemp
14480 44 62 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c Db) ){. vfsFl
14490 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20 ags = (vfsFlags
144a0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & ~SQLITE_OPEN_M
144b0 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 AIN_DB) | SQLITE
144c0 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 _OPEN_TEMP_DB;.
144d0 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 }. p = sqlite3
144e0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f MallocZero(sizeo
144f0 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 28 f(Btree));. if(
14500 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !p ){. retur
14510 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 n SQLITE_NOMEM_B
14520 4b 50 54 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e KPT;. }. p->in
14530 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
14540 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 NE;. p->db = db
14550 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
14560 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
14570 48 45 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 HE. p->lock.pBt
14580 72 65 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f ree = p;. p->lo
14590 63 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b 0a 23 ck.iTable = 1;.#
145a0 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 endif..#if !defi
145b0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
145c0 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 SHARED_CACHE) &&
145d0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
145e0 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 _OMIT_DISKIO).
145f0 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 /*. ** If this
14600 42 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 69 Btree is a candi
14610 64 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 20 date for shared
14620 63 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 69 cache, try to fi
14630 6e 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 74 nd an. ** exist
14640 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 6a ing BtShared obj
14650 65 63 74 20 74 68 61 74 20 77 65 20 63 61 6e 20 ect that we can
14660 73 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f 0a share with. */.
14670 20 20 69 66 28 20 69 73 54 65 6d 70 44 62 3d 3d if( isTempDb==
14680 30 20 26 26 20 28 69 73 4d 65 6d 64 62 3d 3d 30 0 && (isMemdb==0
14690 20 7c 7c 20 28 76 66 73 46 6c 61 67 73 26 53 51 || (vfsFlags&SQ
146a0 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 21 3d LITE_OPEN_URI)!=
146b0 30 29 20 29 7b 0a 20 20 20 20 69 66 28 20 76 66 0) ){. if( vf
146c0 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f sFlags & SQLITE_
146d0 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 OPEN_SHAREDCACHE
146e0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 ){. int nF
146f0 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 ilename = sqlite
14700 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 6e 3Strlen30(zFilen
14710 61 6d 65 29 2b 31 3b 0a 20 20 20 20 20 20 69 6e ame)+1;. in
14720 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 t nFullPathname
14730 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 = pVfs->mxPathna
14740 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 me+1;. char
14750 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 *zFullPathname
14760 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
14770 4d 41 58 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d MAX(nFullPathnam
14780 65 2c 6e 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 e,nFilename));.
14790 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 43 MUTEX_LOGIC
147a0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 ( sqlite3_mutex
147b0 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 20 29 0a *mutexShared; ).
147c0 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
147d0 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 le = 1;. if
147e0 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 ( !zFullPathname
147f0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
14800 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
14810 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
14820 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 TE_NOMEM_BKPT;.
14830 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
14840 20 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 isMemdb ){.
14850 20 20 20 20 6d 65 6d 63 70 79 28 7a 46 75 6c 6c memcpy(zFull
14860 50 61 74 68 6e 61 6d 65 2c 20 7a 46 69 6c 65 6e Pathname, zFilen
14870 61 6d 65 2c 20 6e 46 69 6c 65 6e 61 6d 65 29 3b ame, nFilename);
14880 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
14890 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
148a0 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e3OsFullPathname
148b0 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 (pVfs, zFilename
148c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
148d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
148e0 20 20 20 20 20 6e 46 75 6c 6c 50 61 74 68 6e 61 nFullPathna
148f0 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d me, zFullPathnam
14900 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
14910 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 rc ){.
14920 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 sqlite3_free(zFu
14930 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
14940 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
14950 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ree(p);.
14960 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
14970 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 }. }.#
14980 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
14990 53 41 46 45 0a 20 20 20 20 20 20 6d 75 74 65 78 SAFE. mutex
149a0 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 Open = sqlite3Mu
149b0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
149c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 MUTEX_STATIC_OPE
149d0 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 N);. sqlite
149e0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
149f0 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 texOpen);.
14a00 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 mutexShared = sq
14a10 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
14a20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
14a30 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 TIC_MASTER);.
14a40 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
14a50 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 _enter(mutexShar
14a60 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 ed);.#endif.
14a70 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c for(pBt=GLOBAL
14a80 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
14a90 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
14aa0 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74 t); pBt; pBt=pBt
14ab0 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
14ac0 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e assert( pBt->n
14ad0 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 Ref>0 );.
14ae0 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a if( 0==strcmp(z
14af0 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71 FullPathname, sq
14b00 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 lite3PagerFilena
14b10 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 me(pBt->pPager,
14b20 30 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 0)).
14b30 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 && sqlite3P
14b40 61 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 agerVfs(pBt->pPa
14b50 67 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 ger)==pVfs ){.
14b60 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b int iDb;
14b70 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 . for(i
14b80 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 Db=db->nDb-1; iD
14b90 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 b>=0; iDb--){.
14ba0 20 20 20 20 20 20 20 20 20 20 42 74 72 65 65 20 Btree
14bb0 2a 70 45 78 69 73 74 69 6e 67 20 3d 20 64 62 2d *pExisting = db-
14bc0 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 >aDb[iDb].pBt;.
14bd0 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
14be0 45 78 69 73 74 69 6e 67 20 26 26 20 70 45 78 69 Existing && pExi
14bf0 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 sting->pBt==pBt
14c00 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
14c10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
14c20 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 eave(mutexShared
14c30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
14c40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
14c50 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b eave(mutexOpen);
14c60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 . s
14c70 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c qlite3_free(zFul
14c80 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 lPathname);.
14c90 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
14ca0 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 3_free(p);.
14cb0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
14cc0 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
14cd0 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d T;. }
14ce0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
14cf0 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 p->pBt =
14d00 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 pBt;. p
14d10 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 Bt->nRef++;.
14d20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
14d30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
14d40 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
14d50 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 ex_leave(mutexSh
14d60 61 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c ared);. sql
14d70 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 ite3_free(zFullP
14d80 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a athname);. }.
14d90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
14da0 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 BUG. else{.
14db0 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 /* In debug
14dc0 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c mode, we mark al
14dd0 6c 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 l persistent dat
14de0 61 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 abases as sharab
14df0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e le. ** even
14e00 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e when they are n
14e10 6f 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 ot. This exerci
14e20 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ses the locking
14e30 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a code and. *
14e40 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 * gives more opp
14e50 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 ortunity for ass
14e60 65 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 erts(sqlite3_mut
14e70 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 ex_held()).
14e80 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 ** statements t
14e90 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 o find locking p
14ea0 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a roblems.. *
14eb0 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 /. p->shara
14ec0 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 ble = 1;. }.#
14ed0 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 endif. }.#endif
14ee0 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b . if( pBt==0 ){
14ef0 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 . /*. ** T
14f00 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
14f10 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 erts make sure t
14f20 68 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 hat structures u
14f30 73 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 sed by the btree
14f40 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 are. ** the
14f50 72 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 right size. Thi
14f60 73 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 s is to guard ag
14f70 61 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 ainst size chang
14f80 65 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 es that result.
14f90 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 ** when compi
14fa0 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 ling on a differ
14fb0 65 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 ent architecture
14fc0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
14fd0 65 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 ert( sizeof(i64)
14fe0 3d 3d 38 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==8 );. asser
14ff0 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d t( sizeof(u64)==
15000 38 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 8 );. assert(
15010 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20 sizeof(u32)==4
15020 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15030 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 3b izeof(u16)==2 );
15040 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
15050 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a eof(Pgno)==4 );.
15060 20 20 0a 20 20 20 20 70 42 74 20 3d 20 73 71 6c . pBt = sql
15070 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 ite3MallocZero(
15080 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b 0a sizeof(*pBt) );.
15090 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 if( pBt==0 )
150a0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
150b0 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a ITE_NOMEM_BKPT;.
150c0 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 goto btree
150d0 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d _open_out;. }
150e0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
150f0 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 3PagerOpen(pVfs,
15100 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a &pBt->pPager, z
15110 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 Filename,.
15120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15130 20 20 20 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 sizeof(MemPa
15140 67 65 29 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 ge), flags, vfsF
15150 6c 61 67 73 2c 20 70 61 67 65 52 65 69 6e 69 74 lags, pageReinit
15160 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
15170 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15180 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
15190 74 4d 6d 61 70 4c 69 6d 69 74 28 70 42 74 2d 3e tMmapLimit(pBt->
151a0 70 50 61 67 65 72 2c 20 64 62 2d 3e 73 7a 4d 6d pPager, db->szMm
151b0 61 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 ap);. rc =
151c0 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 sqlite3PagerRead
151d0 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e Fileheader(pBt->
151e0 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 pPager,sizeof(zD
151f0 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 bHeader),zDbHead
15200 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 er);. }. i
15210 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15220 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ){. goto b
15230 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
15240 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 6f 70 }. pBt->op
15250 65 6e 46 6c 61 67 73 20 3d 20 28 75 38 29 66 6c enFlags = (u8)fl
15260 61 67 73 3b 0a 20 20 20 20 70 42 74 2d 3e 64 62 ags;. pBt->db
15270 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 = db;. sqlit
15280 65 33 50 61 67 65 72 53 65 74 42 75 73 79 48 61 e3PagerSetBusyHa
15290 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 ndler(pBt->pPage
152a0 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 r, btreeInvokeBu
152b0 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b syHandler, pBt);
152c0 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 . p->pBt = pB
152d0 74 3b 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70 t;. . pBt->p
152e0 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
152f0 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b pBt->pPage1 = 0;
15300 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
15310 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 PagerIsreadonly(
15320 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29 20 70 pBt->pPager) ) p
15330 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 Bt->btsFlags |=
15340 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0a 23 BTS_READ_ONLY;.#
15350 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
15360 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 E_SECURE_DELETE)
15370 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 . pBt->btsFla
15380 67 73 20 7c 3d 20 42 54 53 5f 53 45 43 55 52 45 gs |= BTS_SECURE
15390 5f 44 45 4c 45 54 45 3b 0a 23 65 6c 69 66 20 64 _DELETE;.#elif d
153a0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 46 41 efined(SQLITE_FA
153b0 53 54 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 ST_SECURE_DELETE
153c0 29 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c ). pBt->btsFl
153d0 61 67 73 20 7c 3d 20 42 54 53 5f 4f 56 45 52 57 ags |= BTS_OVERW
153e0 52 49 54 45 3b 0a 23 65 6e 64 69 66 0a 20 20 20 RITE;.#endif.
153f0 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a /* EVIDENCE-OF:
15400 20 52 2d 35 31 38 37 33 2d 33 39 36 31 38 20 54 R-51873-39618 T
15410 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 he page size for
15420 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
15430 20 69 73 0a 20 20 20 20 2a 2a 20 64 65 74 65 72 is. ** deter
15440 6d 69 6e 65 64 20 62 79 20 74 68 65 20 32 2d 62 mined by the 2-b
15450 79 74 65 20 69 6e 74 65 67 65 72 20 6c 6f 63 61 yte integer loca
15460 74 65 64 20 61 74 20 61 6e 20 6f 66 66 73 65 74 ted at an offset
15470 20 6f 66 20 31 36 20 62 79 74 65 73 20 66 72 6f of 16 bytes fro
15480 6d 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 65 67 m. ** the beg
15490 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 inning of the da
154a0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
154b0 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a pBt->pageSiz
154c0 65 20 3d 20 28 7a 44 62 48 65 61 64 65 72 5b 31 e = (zDbHeader[1
154d0 36 5d 3c 3c 38 29 20 7c 20 28 7a 44 62 48 65 61 6]<<8) | (zDbHea
154e0 64 65 72 5b 31 37 5d 3c 3c 31 36 29 3b 0a 20 20 der[17]<<16);.
154f0 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 if( pBt->pageS
15500 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e ize<512 || pBt->
15510 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f pageSize>SQLITE_
15520 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 MAX_PAGE_SIZE.
15530 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d || ((pBt-
15540 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 >pageSize-1)&pBt
15550 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 ->pageSize)!=0 )
15560 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 {. pBt->pag
15570 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 eSize = 0;.#ifnd
15580 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
15590 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
155a0 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 /* If the magic
155b0 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 name ":memory:"
155c0 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 will create an i
155d0 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
155e0 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a e, then. **
155f0 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 leave the autoV
15600 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 acuum mode at 0
15610 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 (do not auto-vac
15620 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 uum), even if.
15630 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 ** SQLITE_DE
15640 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
15650 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 is true. On the
15660 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a other hand, if.
15670 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f ** SQLITE_
15680 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 OMIT_MEMORYDB ha
15690 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 s been defined,
156a0 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 then ":memory:"
156b0 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 is just a.
156c0 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d ** regular file-
156d0 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 name. In this ca
156e0 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 se the auto-vacu
156f0 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 um applies as pe
15700 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 r normal..
15710 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 */. if( zFi
15720 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d lename && !isMem
15730 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 db ){. pB
15740 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 t->autoVacuum =
15750 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
15760 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a AUTOVACUUM ? 1 :
15770 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 0);. pBt
15780 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
15790 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 SQLITE_DEFAULT_A
157a0 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 UTOVACUUM==2 ? 1
157b0 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 : 0);. }.#
157c0 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 endif. nRes
157d0 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 erve = 0;. }e
157e0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 45 56 lse{. /* EV
157f0 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 33 37 34 IDENCE-OF: R-374
15800 39 37 2d 34 32 34 31 32 20 54 68 65 20 73 69 7a 97-42412 The siz
15810 65 20 6f 66 20 74 68 65 20 72 65 73 65 72 76 65 e of the reserve
15820 64 20 72 65 67 69 6f 6e 20 69 73 0a 20 20 20 20 d region is.
15830 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 ** determined
15840 62 79 20 74 68 65 20 6f 6e 65 2d 62 79 74 65 20 by the one-byte
15850 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
15860 20 66 6f 75 6e 64 20 61 74 20 61 6e 20 6f 66 66 found at an off
15870 73 65 74 20 6f 66 20 32 30 0a 20 20 20 20 20 20 set of 20.
15880 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ** into the data
15890 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 base file header
158a0 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 52 65 73 65 . */. nRese
158b0 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b rve = zDbHeader[
158c0 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 20];. pBt->
158d0 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f btsFlags |= BTS_
158e0 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a PAGESIZE_FIXED;.
158f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15900 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
15910 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 pBt->autoVa
15920 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 cuum = (get4byte
15930 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b (&zDbHeader[36 +
15940 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 4*4])?1:0);.
15950 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
15960 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 um = (get4byte(&
15970 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37 zDbHeader[36 + 7
15980 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 *4])?1:0);.#endi
15990 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d f. }. rc =
159a0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
159b0 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 Pagesize(pBt->pP
159c0 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 ager, &pBt->page
159d0 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b Size, nReserve);
159e0 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
159f0 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
15a00 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 t;. pBt->usab
15a10 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 leSize = pBt->pa
15a20 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 geSize - nReserv
15a30 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 e;. assert( (
15a40 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 pBt->pageSize &
15a50 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 7)==0 ); /* 8-b
15a60 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 yte alignment of
15a70 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 pageSize */.
15a80 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
15a90 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
15aa0 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
15ab0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15ac0 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 DISKIO). /* A
15ad0 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61 dd the new BtSha
15ae0 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 red object to th
15af0 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 e linked list sh
15b00 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73 arable BtShareds
15b10 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 .. */. pBt
15b20 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 ->nRef = 1;.
15b30 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
15b40 29 7b 0a 20 20 20 20 20 20 4d 55 54 45 58 5f 4c ){. MUTEX_L
15b50 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f 6d 75 OGIC( sqlite3_mu
15b60 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 tex *mutexShared
15b70 3b 20 29 0a 20 20 20 20 20 20 4d 55 54 45 58 5f ; ). MUTEX_
15b80 4c 4f 47 49 43 28 20 6d 75 74 65 78 53 68 61 72 LOGIC( mutexShar
15b90 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ed = sqlite3Mute
15ba0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
15bb0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
15bc0 52 29 3b 29 0a 20 20 20 20 20 20 69 66 28 20 53 R);). if( S
15bd0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
15be0 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
15bf0 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 lConfig.bCoreMut
15c00 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 ex ){. pB
15c10 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 t->mutex = sqlit
15c20 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
15c30 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b ITE_MUTEX_FAST);
15c40 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 42 74 . if( pBt
15c50 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 ->mutex==0 ){.
15c60 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
15c70 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a ITE_NOMEM_BKPT;.
15c80 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 goto b
15c90 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
15ca0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
15cb0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15cc0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
15cd0 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 Shared);. p
15ce0 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 Bt->pNext = GLOB
15cf0 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
15d00 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
15d10 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 ist);. GLOB
15d20 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
15d30 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
15d40 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 ist) = pBt;.
15d50 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15d60 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 leave(mutexShare
15d70 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 d);. }.#endif
15d80 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e . }..#if !defin
15d90 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
15da0 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
15db0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15dc0 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f OMIT_DISKIO). /
15dd0 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 72 * If the new Btr
15de0 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 62 ee uses a sharab
15df0 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 68 le pBtShared, th
15e00 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a en link the new.
15e10 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 ** Btree into
15e20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 the list of all
15e30 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 sharable Btrees
15e40 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f 6e for the same con
15e50 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 nection.. ** Th
15e60 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 69 e list is kept i
15e70 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
15e80 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 73 r by pBt address
15e90 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
15ea0 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
15eb0 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 65 int i;. Btree
15ec0 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 *pSib;. for(
15ed0 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
15ee0 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
15ef0 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b (pSib = db->aDb[
15f00 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 53 i].pBt)!=0 && pS
15f10 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a ib->sharable ){.
15f20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
15f30 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 Sib->pPrev ){ pS
15f40 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 ib = pSib->pPrev
15f50 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
15f60 28 75 70 74 72 29 70 2d 3e 70 42 74 3c 28 75 70 (uptr)p->pBt<(up
15f70 74 72 29 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a tr)pSib->pBt ){.
15f80 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 p->pNe
15f90 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 xt = pSib;.
15fa0 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 p->pPrev =
15fb0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 0;. pSi
15fc0 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 b->pPrev = p;.
15fd0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
15fe0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 while( pS
15ff0 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 28 75 70 ib->pNext && (up
16000 74 72 29 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e tr)pSib->pNext->
16010 70 42 74 3c 28 75 70 74 72 29 70 2d 3e 70 42 74 pBt<(uptr)p->pBt
16020 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
16030 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 pSib = pSib->pNe
16040 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a xt;. }.
16050 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 p->pNe
16060 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 xt = pSib->pNext
16070 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 ;. p->p
16080 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 Prev = pSib;.
16090 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e if( p->pN
160a0 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ext ){.
160b0 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
160c0 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 ev = p;.
160d0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 }. pS
160e0 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 ib->pNext = p;.
160f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
16100 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
16110 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
16120 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b . *ppBtree = p;
16130 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 ..btree_open_out
16140 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 :. if( rc!=SQLI
16150 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
16160 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 pBt && pBt->pPa
16170 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ger ){. sql
16180 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 ite3PagerClose(p
16190 42 74 2d 3e 70 50 61 67 65 72 2c 20 30 29 3b 0a Bt->pPager, 0);.
161a0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
161b0 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 20 3_free(pBt);.
161c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
161d0 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20 3d ;. *ppBtree =
161e0 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
161f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
16200 46 69 6c 65 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 File;.. /* If
16210 20 74 68 65 20 42 2d 54 72 65 65 20 77 61 73 20 the B-Tree was
16220 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 successfully ope
16230 6e 65 64 2c 20 73 65 74 20 74 68 65 20 70 61 67 ned, set the pag
16240 65 72 2d 63 61 63 68 65 20 73 69 7a 65 20 74 6f er-cache size to
16250 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 66 61 the. ** defa
16260 75 6c 74 20 76 61 6c 75 65 2e 20 45 78 63 65 70 ult value. Excep
16270 74 2c 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 t, when opening
16280 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 on an existing s
16290 68 61 72 65 64 20 70 61 67 65 72 2d 63 61 63 68 hared pager-cach
162a0 65 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 e,. ** do not
162b0 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 change the page
162c0 72 2d 63 61 63 68 65 20 73 69 7a 65 2e 0a 20 20 r-cache size..
162d0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c */. if( sql
162e0 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 ite3BtreeSchema(
162f0 70 2c 20 30 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 p, 0, 0)==0 ){.
16300 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
16310 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 2d rSetCachesize(p-
16320 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 53 51 >pBt->pPager, SQ
16330 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 LITE_DEFAULT_CAC
16340 48 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d 0a HE_SIZE);. }.
16350 0a 20 20 20 20 70 46 69 6c 65 20 3d 20 73 71 6c . pFile = sql
16360 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 ite3PagerFile(pB
16370 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 t->pPager);.
16380 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 if( pFile->pMeth
16390 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ods ){. sql
163a0 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
163b0 6c 48 69 6e 74 28 70 46 69 6c 65 2c 20 53 51 4c lHint(pFile, SQL
163c0 49 54 45 5f 46 43 4e 54 4c 5f 50 44 42 2c 20 28 ITE_FCNTL_PDB, (
163d0 76 6f 69 64 2a 29 26 70 42 74 2d 3e 64 62 29 3b void*)&pBt->db);
163e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
163f0 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 mutexOpen ){.
16400 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16410 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 3_mutex_held(mut
16420 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 exOpen) );. s
16430 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
16440 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 ve(mutexOpen);.
16450 20 7d 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 }. assert( rc!
16460 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 =SQLITE_OK || sq
16470 6c 69 74 65 33 42 74 72 65 65 43 6f 6e 6e 65 63 lite3BtreeConnec
16480 74 69 6f 6e 43 6f 75 6e 74 28 2a 70 70 42 74 72 tionCount(*ppBtr
16490 65 65 29 3e 30 20 29 3b 0a 20 20 72 65 74 75 72 ee)>0 );. retur
164a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 n rc;.}../*.** D
164b0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42 74 53 ecrement the BtS
164c0 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 hared.nRef count
164d0 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72 65 61 er. When it rea
164e0 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 ches zero,.** re
164f0 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 72 65 move the BtShare
16500 64 20 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d d structure from
16510 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73 the sharing lis
16520 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 t. Return.** tr
16530 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72 ue if the BtShar
16540 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 ed.nRef counter
16550 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 reaches zero and
16560 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 return.** false
16570 20 69 66 20 69 74 20 69 73 20 73 74 69 6c 6c 20 if it is still
16580 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 positive..*/.sta
16590 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 tic int removeFr
165a0 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 42 74 omSharingList(Bt
165b0 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 Shared *pBt){.#i
165c0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
165d0 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
165e0 20 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 73 71 MUTEX_LOGIC( sq
165f0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 lite3_mutex *pMa
16600 73 74 65 72 3b 20 29 0a 20 20 42 74 53 68 61 72 ster; ). BtShar
16610 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 ed *pList;. int
16620 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 removed = 0;..
16630 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16640 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 _mutex_notheld(p
16650 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
16660 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 70 4d 61 MUTEX_LOGIC( pMa
16670 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 ster = sqlite3Mu
16680 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
16690 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
166a0 54 45 52 29 3b 20 29 0a 20 20 73 71 6c 69 74 65 TER); ). sqlite
166b0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 3_mutex_enter(pM
166c0 61 73 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e aster);. pBt->n
166d0 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 Ref--;. if( pBt
166e0 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 ->nRef<=0 ){.
166f0 20 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 if( GLOBAL(BtSh
16700 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 ared*,sqlite3Sha
16710 72 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 redCacheList)==p
16720 42 74 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 Bt ){. GLOB
16730 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
16740 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
16750 69 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 ist) = pBt->pNex
16760 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
16770 20 20 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 pList = GLOB
16780 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
16790 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
167a0 69 73 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c ist);. whil
167b0 65 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 e( ALWAYS(pList)
167c0 20 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 && pList->pNext
167d0 21 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 !=pBt ){.
167e0 20 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e pList=pList->pN
167f0 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
16800 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c if( ALWAYS(pL
16810 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ist) ){.
16820 70 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 pList->pNext = p
16830 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 Bt->pNext;.
16840 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
16850 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
16860 46 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 FE ){. sqli
16870 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 te3_mutex_free(p
16880 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 Bt->mutex);.
16890 7d 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 }. removed =
168a0 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 1;. }. sqlite3
168b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 _mutex_leave(pMa
168c0 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 ster);. return
168d0 72 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 removed;.#else.
168e0 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 return 1;.#endi
168f0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 f.}../*.** Make
16900 73 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 sure pBt->pTmpSp
16910 61 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e ace points to an
16920 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a allocation of .
16930 2a 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 ** MX_CELL_SIZE(
16940 70 42 74 29 20 62 79 74 65 73 20 77 69 74 68 20 pBt) bytes with
16950 61 20 34 2d 62 79 74 65 20 70 72 65 66 69 78 20 a 4-byte prefix
16960 66 6f 72 20 61 20 6c 65 66 74 2d 63 68 69 6c 64 for a left-child
16970 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a .** pointer..*/.
16980 73 74 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f static void allo
16990 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 42 74 cateTempSpace(Bt
169a0 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
169b0 69 66 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 if( !pBt->pTmpSp
169c0 61 63 65 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e ace ){. pBt->
169d0 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 pTmpSpace = sqli
169e0 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 te3PageMalloc( p
169f0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a Bt->pageSize );.
16a00 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 . /* One of t
16a10 68 65 20 75 73 65 73 20 6f 66 20 70 42 74 2d 3e he uses of pBt->
16a20 70 54 6d 70 53 70 61 63 65 20 69 73 20 74 6f 20 pTmpSpace is to
16a30 66 6f 72 6d 61 74 20 63 65 6c 6c 73 20 62 65 66 format cells bef
16a40 6f 72 65 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 ore. ** inser
16a50 74 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 20 61 ting them into a
16a60 20 6c 65 61 66 20 70 61 67 65 20 28 66 75 6e 63 leaf page (func
16a70 74 69 6f 6e 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 tion fillInCell(
16a80 29 29 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 20 )). If. ** a
16a90 63 65 6c 6c 20 69 73 20 6c 65 73 73 20 74 68 61 cell is less tha
16aa0 6e 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a n 4 bytes in siz
16ab0 65 2c 20 69 74 20 69 73 20 72 6f 75 6e 64 65 64 e, it is rounded
16ac0 20 75 70 20 74 6f 20 34 20 62 79 74 65 73 0a 20 up to 4 bytes.
16ad0 20 20 20 2a 2a 20 62 79 20 74 68 65 20 76 61 72 ** by the var
16ae0 69 6f 75 73 20 72 6f 75 74 69 6e 65 73 20 74 68 ious routines th
16af0 61 74 20 6d 61 6e 69 70 75 6c 61 74 65 20 62 69 at manipulate bi
16b00 6e 61 72 79 20 63 65 6c 6c 73 2e 20 57 68 69 63 nary cells. Whic
16b10 68 0a 20 20 20 20 2a 2a 20 63 61 6e 20 6d 65 61 h. ** can mea
16b20 6e 20 74 68 61 74 20 66 69 6c 6c 49 6e 43 65 6c n that fillInCel
16b30 6c 28 29 20 6f 6e 6c 79 20 69 6e 69 74 69 61 6c l() only initial
16b40 69 7a 65 73 20 74 68 65 20 66 69 72 73 74 20 32 izes the first 2
16b50 20 6f 72 20 33 0a 20 20 20 20 2a 2a 20 62 79 74 or 3. ** byt
16b60 65 73 20 6f 66 20 70 54 6d 70 53 70 61 63 65 2c es of pTmpSpace,
16b70 20 62 75 74 20 74 68 61 74 20 74 68 65 20 66 69 but that the fi
16b80 72 73 74 20 34 20 62 79 74 65 73 20 61 72 65 20 rst 4 bytes are
16b90 63 6f 70 69 65 64 20 66 72 6f 6d 0a 20 20 20 20 copied from.
16ba0 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 64 61 74 ** it into a dat
16bb0 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 69 73 abase page. This
16bc0 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 is not actually
16bd0 20 61 20 70 72 6f 62 6c 65 6d 2c 20 62 75 74 20 a problem, but
16be0 69 74 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 63 it. ** does c
16bf0 61 75 73 65 20 61 20 76 61 6c 67 72 69 6e 64 20 ause a valgrind
16c00 65 72 72 6f 72 20 77 68 65 6e 20 74 68 65 20 31 error when the 1
16c10 20 6f 72 20 32 20 62 79 74 65 73 20 6f 66 20 75 or 2 bytes of u
16c20 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20 20 20 nitialized .
16c30 2a 2a 20 64 61 74 61 20 69 73 20 70 61 73 73 65 ** data is passe
16c40 64 20 74 6f 20 73 79 73 74 65 6d 20 63 61 6c 6c d to system call
16c50 20 77 72 69 74 65 28 29 2e 20 53 6f 20 74 6f 20 write(). So to
16c60 61 76 6f 69 64 20 74 68 69 73 20 65 72 72 6f 72 avoid this error
16c70 2c 0a 20 20 20 20 2a 2a 20 7a 65 72 6f 20 74 68 ,. ** zero th
16c80 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 e first 4 bytes
16c90 6f 66 20 74 65 6d 70 20 73 70 61 63 65 20 68 65 of temp space he
16ca0 72 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a re.. **. *
16cb0 2a 20 41 6c 73 6f 3a 20 20 50 72 6f 76 69 64 65 * Also: Provide
16cc0 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 69 four bytes of i
16cd0 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 nitialized space
16ce0 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20 20 before the.
16cf0 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 ** beginning of
16d00 70 54 6d 70 53 70 61 63 65 20 61 73 20 61 6e 20 pTmpSpace as an
16d10 61 72 65 61 20 61 76 61 69 6c 61 62 6c 65 20 74 area available t
16d20 6f 20 70 72 65 70 65 6e 64 20 74 68 65 0a 20 20 o prepend the.
16d30 20 20 2a 2a 20 6c 65 66 74 2d 63 68 69 6c 64 20 ** left-child
16d40 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 pointer to the b
16d50 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 63 65 eginning of a ce
16d60 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ll.. */. i
16d70 66 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 f( pBt->pTmpSpac
16d80 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 e ){. memse
16d90 74 28 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 t(pBt->pTmpSpace
16da0 2c 20 30 2c 20 38 29 3b 0a 20 20 20 20 20 20 70 , 0, 8);. p
16db0 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 2b 3d Bt->pTmpSpace +=
16dc0 20 34 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 4;. }. }.}.
16dd0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
16de0 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 pBt->pTmpSpace a
16df0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
16e00 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d tic void freeTem
16e10 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 pSpace(BtShared
16e20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 70 42 74 *pBt){. if( pBt
16e30 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 ->pTmpSpace ){.
16e40 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 pBt->pTmpSpac
16e50 65 20 2d 3d 20 34 3b 0a 20 20 20 20 73 71 6c 69 e -= 4;. sqli
16e60 74 65 33 50 61 67 65 46 72 65 65 28 70 42 74 2d te3PageFree(pBt-
16e70 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 >pTmpSpace);.
16e80 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
16e90 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 0;. }.}../*.*
16ea0 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e 20 * Close an open
16eb0 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 6e 76 database and inv
16ec0 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 75 72 73 alidate all curs
16ed0 6f 72 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ors..*/.int sqli
16ee0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 te3BtreeClose(Bt
16ef0 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
16f00 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
16f10 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 t;. BtCursor *p
16f20 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 Cur;.. /* Close
16f30 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 all cursors ope
16f40 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 61 6e ned via this han
16f50 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 dle. */. asser
16f60 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
16f70 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
16f80 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
16f90 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
16fa0 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 pCur = pBt->pCu
16fb0 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 rsor;. while( p
16fc0 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72 Cur ){. BtCur
16fd0 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 sor *pTmp = pCur
16fe0 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70 43 75 ;. pCur = pCu
16ff0 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 r->pNext;. if
17000 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d ( pTmp->pBtree==
17010 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
17020 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
17030 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a or(pTmp);. }.
17040 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 }.. /* Rollba
17050 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20 74 72 ck any active tr
17060 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 ansaction and fr
17070 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74 ee the handle st
17080 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 ructure.. ** Th
17090 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
170a0 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 3BtreeRollback()
170b0 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 drops any table
170c0 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 -locks held by.
170d0 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e ** this handle.
170e0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 . */. sqlite3B
170f0 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2c 20 treeRollback(p,
17100 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 SQLITE_OK, 0);.
17110 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
17120 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ve(p);.. /* If
17130 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 there are still
17140 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64 69 6e other outstandin
17150 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 g references to
17160 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
17170 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 2c . ** structure,
17180 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 return now. The
17190 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 remainder of th
171a0 69 73 20 70 72 6f 63 65 64 75 72 65 20 63 6c 65 is procedure cle
171b0 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 ans . ** up the
171c0 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 shared-btree..
171d0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
171e0 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 >wantToLock==0 &
171f0 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 & p->locked==0 )
17200 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 ;. if( !p->shar
17210 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 able || removeFr
17220 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 70 42 omSharingList(pB
17230 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 t) ){. /* The
17240 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 pBt is no longe
17250 72 20 6f 6e 20 74 68 65 20 73 68 61 72 69 6e 67 r on the sharing
17260 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 63 61 6e list, so we can
17270 20 61 63 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 access. ** i
17280 74 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 t without having
17290 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 to hold the mut
172a0 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ex.. **. *
172b0 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 * Clean out and
172c0 64 65 6c 65 74 65 20 74 68 65 20 42 74 53 68 61 delete the BtSha
172d0 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 red object..
172e0 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 */. assert( !
172f0 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a pBt->pCursor );.
17300 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
17310 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 Close(pBt->pPage
17320 72 2c 20 70 2d 3e 64 62 29 3b 0a 20 20 20 20 69 r, p->db);. i
17330 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 f( pBt->xFreeSch
17340 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 ema && pBt->pSch
17350 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ema ){. pBt
17360 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 ->xFreeSchema(pB
17370 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 t->pSchema);.
17380 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 }. sqlite3Db
17390 46 72 65 65 28 30 2c 20 70 42 74 2d 3e 70 53 63 Free(0, pBt->pSc
173a0 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 54 hema);. freeT
173b0 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 empSpace(pBt);.
173c0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
173d0 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 pBt);. }..#ifnd
173e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
173f0 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 HARED_CACHE. as
17400 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
17410 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ock==0 );. asse
17420 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 rt( p->locked==0
17430 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 );. if( p->pPr
17440 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 ev ) p->pPrev->p
17450 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b Next = p->pNext;
17460 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 . if( p->pNext
17470 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 ) p->pNext->pPre
17480 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 v = p->pPrev;.#e
17490 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f ndif.. sqlite3_
174a0 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 free(p);. retur
174b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
174c0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
174d0 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 20 6f 6e "soft" limit on
174e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
174f0 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 ages in the cach
17500 65 2e 0a 2a 2a 20 55 6e 75 73 65 64 20 61 6e 64 e..** Unused and
17510 20 75 6e 6d 6f 64 69 66 69 65 64 20 70 61 67 65 unmodified page
17520 73 20 77 69 6c 6c 20 62 65 20 72 65 63 79 63 6c s will be recycl
17530 65 64 20 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 ed when the numb
17540 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 73 20 69 er of.** pages i
17550 6e 20 74 68 65 20 63 61 63 68 65 20 65 78 63 65 n the cache exce
17560 65 64 73 20 74 68 69 73 20 73 6f 66 74 20 6c 69 eds this soft li
17570 6d 69 74 2e 20 20 42 75 74 20 74 68 65 20 73 69 mit. But the si
17580 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 61 63 ze of the.** cac
17590 68 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f he is allowed to
175a0 20 67 72 6f 77 20 6c 61 72 67 65 72 20 74 68 61 grow larger tha
175b0 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 69 66 20 n this limit if
175c0 69 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 64 it contains.** d
175d0 69 72 74 79 20 70 61 67 65 73 20 6f 72 20 70 61 irty pages or pa
175e0 67 65 73 20 73 74 69 6c 6c 20 69 6e 20 61 63 74 ges still in act
175f0 69 76 65 20 75 73 65 2e 0a 2a 2f 0a 69 6e 74 20 ive use..*/.int
17600 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
17610 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a acheSize(Btree *
17620 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a p, int mxPage){.
17630 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
17640 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 = p->pBt;. asse
17650 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17660 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
17670 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
17680 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
17690 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
176a0 74 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e tCachesize(pBt->
176b0 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b pPager, mxPage);
176c0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
176d0 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
176e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
176f0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
17700 20 22 73 70 69 6c 6c 22 20 6c 69 6d 69 74 20 6f "spill" limit o
17710 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
17720 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 pages in the cac
17730 68 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 he..** If the nu
17740 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 65 78 mber of pages ex
17750 63 65 65 64 73 20 74 68 69 73 20 6c 69 6d 69 74 ceeds this limit
17760 20 64 75 72 69 6e 67 20 61 20 77 72 69 74 65 20 during a write
17770 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 transaction,.**
17780 74 68 65 20 70 61 67 65 72 20 6d 69 67 68 74 20 the pager might
17790 61 74 74 65 6d 70 74 20 74 6f 20 22 73 70 69 6c attempt to "spil
177a0 6c 22 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 l" pages to the
177b0 6a 6f 75 72 6e 61 6c 20 65 61 72 6c 79 20 69 6e journal early in
177c0 0a 2a 2a 20 6f 72 64 65 72 20 74 6f 20 66 72 65 .** order to fre
177d0 65 20 75 70 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a e up memory..**.
177e0 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 ** The value ret
177f0 75 72 6e 65 64 20 69 73 20 74 68 65 20 63 75 72 urned is the cur
17800 72 65 6e 74 20 73 70 69 6c 6c 20 73 69 7a 65 2e rent spill size.
17810 20 20 49 66 20 7a 65 72 6f 20 69 73 20 70 61 73 If zero is pas
17820 73 65 64 0a 2a 2a 20 61 73 20 61 6e 20 61 72 67 sed.** as an arg
17830 75 6d 65 6e 74 2c 20 6e 6f 20 63 68 61 6e 67 65 ument, no change
17840 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 s are made to th
17850 65 20 73 70 69 6c 6c 20 73 69 7a 65 20 73 65 74 e spill size set
17860 74 69 6e 67 2c 20 73 6f 0a 2a 2a 20 75 73 69 6e ting, so.** usin
17870 67 20 6d 78 50 61 67 65 20 6f 66 20 30 20 69 73 g mxPage of 0 is
17880 20 61 20 77 61 79 20 74 6f 20 71 75 65 72 79 20 a way to query
17890 74 68 65 20 63 75 72 72 65 6e 74 20 73 70 69 6c the current spil
178a0 6c 20 73 69 7a 65 2e 0a 2a 2f 0a 69 6e 74 20 73 l size..*/.int s
178b0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 70 qlite3BtreeSetSp
178c0 69 6c 6c 53 69 7a 65 28 42 74 72 65 65 20 2a 70 illSize(Btree *p
178d0 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
178e0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
178f0 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 p->pBt;. int r
17900 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 es;. assert( sq
17910 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17920 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
17930 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
17940 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 65 73 20 Enter(p);. res
17950 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
17960 74 53 70 69 6c 6c 73 69 7a 65 28 70 42 74 2d 3e tSpillsize(pBt->
17970 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b pPager, mxPage);
17980 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
17990 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
179a0 6e 20 72 65 73 3b 0a 7d 0a 0a 23 69 66 20 53 51 n res;.}..#if SQ
179b0 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 LITE_MAX_MMAP_SI
179c0 5a 45 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ZE>0./*.** Chang
179d0 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 e the limit on t
179e0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 he amount of the
179f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
17a00 68 61 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 65 hat may be.** me
17a10 6d 6f 72 79 20 6d 61 70 70 65 64 2e 0a 2a 2f 0a mory mapped..*/.
17a20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
17a30 53 65 74 4d 6d 61 70 4c 69 6d 69 74 28 42 74 72 SetMmapLimit(Btr
17a40 65 65 20 2a 70 2c 20 73 71 6c 69 74 65 33 5f 69 ee *p, sqlite3_i
17a50 6e 74 36 34 20 73 7a 4d 6d 61 70 29 7b 0a 20 20 nt64 szMmap){.
17a60 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
17a70 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 p->pBt;. assert
17a80 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17a90 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
17aa0 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 x) );. sqlite3B
17ab0 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
17ac0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 4d sqlite3PagerSetM
17ad0 6d 61 70 4c 69 6d 69 74 28 70 42 74 2d 3e 70 50 mapLimit(pBt->pP
17ae0 61 67 65 72 2c 20 73 7a 4d 6d 61 70 29 3b 0a 20 ager, szMmap);.
17af0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
17b00 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
17b10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
17b20 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 dif /* SQLITE_MA
17b30 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 20 2a 2f X_MMAP_SIZE>0 */
17b40 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
17b50 68 65 20 77 61 79 20 64 61 74 61 20 69 73 20 73 he way data is s
17b60 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69 6e ynced to disk in
17b70 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65 61 order to increa
17b80 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a 2a se or decrease.*
17b90 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 * how well the d
17ba0 61 74 61 62 61 73 65 20 72 65 73 69 73 74 73 20 atabase resists
17bb0 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 damage due to OS
17bc0 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77 crashes and pow
17bd0 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 er.** failures.
17be0 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20 Level 1 is the
17bf0 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f same as asynchro
17c00 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 nous (no syncs()
17c10 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 occur and.** th
17c20 65 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72 ere is a high pr
17c30 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d obability of dam
17c40 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73 age) Level 2 is
17c50 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 the default. T
17c60 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 here.** is a ver
17c70 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 y low but non-ze
17c80 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f ro probability o
17c90 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c f damage. Level
17ca0 20 33 20 72 65 64 75 63 65 73 20 74 68 65 0a 2a 3 reduces the.*
17cb0 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 * probability of
17cc0 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 damage to near
17cd0 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 61 20 zero but with a
17ce0 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 write performanc
17cf0 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a e reduction..*/.
17d00 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17d10 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 MIT_PAGER_PRAGMA
17d20 53 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 S.int sqlite3Btr
17d30 65 65 53 65 74 50 61 67 65 72 46 6c 61 67 73 28 eeSetPagerFlags(
17d40 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 . Btree *p,
17d50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
17d60 20 62 74 72 65 65 20 74 6f 20 73 65 74 20 74 68 btree to set th
17d70 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 6f e safety level o
17d80 6e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 n */. unsigned
17d90 70 67 46 6c 61 67 73 20 20 20 20 20 20 20 2f 2a pgFlags /*
17da0 20 56 61 72 69 6f 75 73 20 50 41 47 45 52 5f 2a Various PAGER_*
17db0 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 42 flags */.){. B
17dc0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
17dd0 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
17de0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
17df0 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
17e00 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ) );. sqlite3Bt
17e10 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 reeEnter(p);. s
17e20 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 46 6c qlite3PagerSetFl
17e30 61 67 73 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ags(pBt->pPager,
17e40 20 70 67 46 6c 61 67 73 29 3b 0a 20 20 73 71 6c pgFlags);. sql
17e50 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
17e60 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
17e70 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a TE_OK;.}.#endif.
17e80 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
17e90 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 e default pages
17ea0 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d size and the num
17eb0 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 ber of reserved
17ec0 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a bytes per page..
17ed0 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 70 61 ** Or, if the pa
17ee0 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 ge size has alre
17ef0 61 64 79 20 62 65 65 6e 20 66 69 78 65 64 2c 20 ady been fixed,
17f00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 return SQLITE_RE
17f10 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f ADONLY .** witho
17f20 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 ut changing anyt
17f30 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 hing..**.** The
17f40 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62 page size must b
17f50 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62 e a power of 2 b
17f60 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 etween 512 and 6
17f70 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61 5536. If the pa
17f80 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c ge.** size suppl
17f90 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 ied does not mee
17fa0 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e t this constrain
17fb0 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 t then the page
17fc0 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 size is not.** c
17fd0 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 hanged..**.** Pa
17fe0 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e ge sizes are con
17ff0 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61 strained to be a
18000 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f power of two so
18010 20 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e that the region
18020 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 .** of the datab
18030 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f ase file used fo
18040 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e r locking (begin
18050 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f ning at PENDING_
18060 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 BYTE,.** the fir
18070 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 st byte past the
18080 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 1GB boundary, 0
18090 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73 x40000000) needs
180a0 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 to occur.** at
180b0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
180c0 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 a page..**.** I
180d0 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 f parameter nRes
180e0 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 erve is less tha
180f0 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n zero, then the
18100 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 number of reser
18110 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 ved.** bytes per
18120 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e page is left un
18130 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 changed..**.** I
18140 66 20 74 68 65 20 69 46 69 78 21 3d 30 20 74 68 f the iFix!=0 th
18150 65 6e 20 74 68 65 20 42 54 53 5f 50 41 47 45 53 en the BTS_PAGES
18160 49 5a 45 5f 46 49 58 45 44 20 66 6c 61 67 20 69 IZE_FIXED flag i
18170 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74 68 s set so that th
18180 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 e page size.** a
18190 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f nd autovacuum mo
181a0 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 de can no longer
181b0 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a be changed..*/.
181c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
181d0 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 SetPageSize(Btre
181e0 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69 e *p, int pageSi
181f0 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ze, int nReserve
18200 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 , int iFix){. i
18210 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
18220 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 K;. BtShared *p
18230 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 Bt = p->pBt;. a
18240 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e ssert( nReserve>
18250 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c =-1 && nReserve<
18260 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 =255 );. sqlite
18270 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18280 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 #if SQLITE_HAS_C
18290 4f 44 45 43 0a 20 20 69 66 28 20 6e 52 65 73 65 ODEC. if( nRese
182a0 72 76 65 3e 70 42 74 2d 3e 6f 70 74 69 6d 61 6c rve>pBt->optimal
182b0 52 65 73 65 72 76 65 20 29 20 70 42 74 2d 3e 6f Reserve ) pBt->o
182c0 70 74 69 6d 61 6c 52 65 73 65 72 76 65 20 3d 20 ptimalReserve =
182d0 28 75 38 29 6e 52 65 73 65 72 76 65 3b 0a 23 65 (u8)nReserve;.#e
182e0 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 2d 3e ndif. if( pBt->
182f0 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50 btsFlags & BTS_P
18300 41 47 45 53 49 5a 45 5f 46 49 58 45 44 20 29 7b AGESIZE_FIXED ){
18310 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
18320 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 eLeave(p);. r
18330 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
18340 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 DONLY;. }. if(
18350 20 6e 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 nReserve<0 ){.
18360 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 nReserve = pB
18370 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 t->pageSize - pB
18380 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 t->usableSize;.
18390 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 }. assert( nRe
183a0 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 serve>=0 && nRes
183b0 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 erve<=255 );. i
183c0 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 f( pageSize>=512
183d0 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 && pageSize<=SQ
183e0 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
183f0 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 ZE &&. ((
18400 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 pageSize-1)&page
18410 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Size)==0 ){.
18420 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a assert( (pageSiz
18430 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 e & 7)==0 );.
18440 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 assert( !pBt->p
18450 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 Cursor );. pB
18460 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 t->pageSize = (u
18470 33 32 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 32)pageSize;.
18480 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 freeTempSpace(p
18490 42 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 Bt);. }. rc =
184a0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
184b0 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 agesize(pBt->pPa
184c0 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 ger, &pBt->pageS
184d0 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a ize, nReserve);.
184e0 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a pBt->usableSiz
184f0 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
18500 65 20 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 e - (u16)nReserv
18510 65 3b 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 e;. if( iFix )
18520 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d pBt->btsFlags |=
18530 20 42 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 BTS_PAGESIZE_FI
18540 58 45 44 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 XED;. sqlite3Bt
18550 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
18560 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
18570 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 ** Return the cu
18580 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 rrently defined
18590 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 69 6e 74 page size.*/.int
185a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
185b0 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a PageSize(Btree *
185c0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
185d0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d pBt->pageSize;.}
185e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
185f0 63 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 ction is similar
18600 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
18610 47 65 74 52 65 73 65 72 76 65 28 29 2c 20 65 78 GetReserve(), ex
18620 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 cept that it.**
18630 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c may only be call
18640 65 64 20 69 66 20 69 74 20 69 73 20 67 75 61 72 ed if it is guar
18650 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 anteed that the
18660 62 2d 74 72 65 65 20 6d 75 74 65 78 20 69 73 20 b-tree mutex is
18670 61 6c 72 65 61 64 79 0a 2a 2a 20 68 65 6c 64 2e already.** held.
18680 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 .**.** This is u
18690 73 65 66 75 6c 20 69 6e 20 6f 6e 65 20 73 70 65 seful in one spe
186a0 63 69 61 6c 20 63 61 73 65 20 69 6e 20 74 68 65 cial case in the
186b0 20 62 61 63 6b 75 70 20 41 50 49 20 63 6f 64 65 backup API code
186c0 20 77 68 65 72 65 20 69 74 20 69 73 0a 2a 2a 20 where it is.**
186d0 6b 6e 6f 77 6e 20 74 68 61 74 20 74 68 65 20 73 known that the s
186e0 68 61 72 65 64 20 62 2d 74 72 65 65 20 6d 75 74 hared b-tree mut
186f0 65 78 20 69 73 20 68 65 6c 64 2c 20 62 75 74 20 ex is held, but
18700 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 the mutex on the
18710 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 .** database ha
18720 6e 64 6c 65 20 74 68 61 74 20 6f 77 6e 73 20 2a ndle that owns *
18730 70 20 69 73 20 6e 6f 74 2e 20 49 6e 20 74 68 69 p is not. In thi
18740 73 20 63 61 73 65 20 69 66 20 73 71 6c 69 74 65 s case if sqlite
18750 33 42 74 72 65 65 45 6e 74 65 72 28 29 0a 2a 2a 3BtreeEnter().**
18760 20 77 65 72 65 20 74 6f 20 62 65 20 63 61 6c 6c were to be call
18770 65 64 2c 20 69 74 20 6d 69 67 68 74 20 63 6f 6c ed, it might col
18780 6c 69 64 65 20 77 69 74 68 20 73 6f 6d 65 20 6f lide with some o
18790 74 68 65 72 20 6f 70 65 72 61 74 69 6f 6e 20 6f ther operation o
187a0 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 n the.** databas
187b0 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6f 77 e handle that ow
187c0 6e 73 20 2a 70 2c 20 63 61 75 73 69 6e 67 20 75 ns *p, causing u
187d0 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f ndefined behavio
187e0 72 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 r..*/.int sqlite
187f0 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 3BtreeGetReserve
18800 4e 6f 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 NoMutex(Btree *p
18810 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 61 73 ){. int n;. as
18820 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
18830 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d tex_held(p->pBt-
18840 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 20 3d >mutex) );. n =
18850 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a p->pBt->pageSiz
18860 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 e - p->pBt->usab
18870 6c 65 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e leSize;. return
18880 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 n;.}../*.** Ret
18890 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
188a0 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 f bytes of space
188b0 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 at the end of e
188c0 76 65 72 79 20 70 61 67 65 20 74 68 61 74 0a 2a very page that.*
188d0 2a 20 61 72 65 20 69 6e 74 65 6e 74 75 61 6c 6c * are intentuall
188e0 79 20 6c 65 66 74 20 75 6e 75 73 65 64 2e 20 20 y left unused.
188f0 54 68 69 73 20 69 73 20 74 68 65 20 22 72 65 73 This is the "res
18900 65 72 76 65 64 22 20 73 70 61 63 65 20 74 68 61 erved" space tha
18910 74 20 69 73 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 t is.** sometime
18920 73 20 75 73 65 64 20 62 79 20 65 78 74 65 6e 73 s used by extens
18930 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 ions..**.** If S
18940 51 4c 49 54 45 5f 48 41 53 5f 4d 55 54 45 58 20 QLITE_HAS_MUTEX
18950 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 6e 20 is defined then
18960 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 the number retur
18970 6e 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 67 72 ned is the.** gr
18980 65 61 74 65 72 20 6f 66 20 74 68 65 20 63 75 72 eater of the cur
18990 72 65 6e 74 20 72 65 73 65 72 76 65 64 20 73 70 rent reserved sp
189a0 61 63 65 20 61 6e 64 20 74 68 65 20 6d 61 78 69 ace and the maxi
189b0 6d 75 6d 20 72 65 71 75 65 73 74 65 64 0a 2a 2a mum requested.**
189c0 20 72 65 73 65 72 76 65 20 73 70 61 63 65 2e 0a reserve space..
189d0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 */.int sqlite3Bt
189e0 72 65 65 47 65 74 4f 70 74 69 6d 61 6c 52 65 73 reeGetOptimalRes
189f0 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a erve(Btree *p){.
18a00 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
18a10 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18a20 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 42 74 . n = sqlite3Bt
18a30 72 65 65 47 65 74 52 65 73 65 72 76 65 4e 6f 4d reeGetReserveNoM
18a40 75 74 65 78 28 70 29 3b 0a 23 69 66 64 65 66 20 utex(p);.#ifdef
18a50 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
18a60 0a 20 20 69 66 28 20 6e 3c 70 2d 3e 70 42 74 2d . if( n<p->pBt-
18a70 3e 6f 70 74 69 6d 61 6c 52 65 73 65 72 76 65 20 >optimalReserve
18a80 29 20 6e 20 3d 20 70 2d 3e 70 42 74 2d 3e 6f 70 ) n = p->pBt->op
18a90 74 69 6d 61 6c 52 65 73 65 72 76 65 3b 0a 23 65 timalReserve;.#e
18aa0 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 42 74 ndif. sqlite3Bt
18ab0 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
18ac0 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn n;.}.../*.
18ad0 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d ** Set the maxim
18ae0 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f um page count fo
18af0 72 20 61 20 64 61 74 61 62 61 73 65 20 69 66 20 r a database if
18b00 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 mxPage is positi
18b10 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 ve..** No change
18b20 73 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 s are made if mx
18b30 50 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 Page is 0 or neg
18b40 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 ative..** Regard
18b50 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 less of the valu
18b60 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 e of mxPage, ret
18b70 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 urn the maximum
18b80 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 69 page count..*/.i
18b90 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
18ba0 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 axPageCount(Btre
18bb0 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 e *p, int mxPage
18bc0 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 ){. int n;. sq
18bd0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
18be0 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 p);. n = sqlite
18bf0 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 3PagerMaxPageCou
18c00 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 nt(p->pBt->pPage
18c10 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 r, mxPage);. sq
18c20 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18c30 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a p);. return n;.
18c40 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
18c50 74 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20 74 the values for t
18c60 68 65 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 he BTS_SECURE_DE
18c70 4c 45 54 45 20 61 6e 64 20 42 54 53 5f 4f 56 45 LETE and BTS_OVE
18c80 52 57 52 49 54 45 20 66 6c 61 67 73 3a 0a 2a 2a RWRITE flags:.**
18c90 0a 2a 2a 20 20 20 20 6e 65 77 46 6c 61 67 3d 3d .** newFlag==
18ca0 30 20 20 20 20 20 20 20 42 6f 74 68 20 42 54 53 0 Both BTS
18cb0 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 61 _SECURE_DELETE a
18cc0 6e 64 20 42 54 53 5f 4f 56 45 52 57 52 49 54 45 nd BTS_OVERWRITE
18cd0 20 61 72 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 are cleared.**
18ce0 20 20 20 6e 65 77 46 6c 61 67 3d 3d 31 20 20 20 newFlag==1
18cf0 20 20 20 20 42 54 53 5f 53 45 43 55 52 45 5f 44 BTS_SECURE_D
18d00 45 4c 45 54 45 20 73 65 74 20 61 6e 64 20 42 54 ELETE set and BT
18d10 53 5f 4f 56 45 52 57 52 49 54 45 20 69 73 20 63 S_OVERWRITE is c
18d20 6c 65 61 72 65 64 0a 2a 2a 20 20 20 20 6e 65 77 leared.** new
18d30 46 6c 61 67 3d 3d 32 20 20 20 20 20 20 20 42 54 Flag==2 BT
18d40 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 S_SECURE_DELETE
18d50 63 6c 65 61 72 65 64 20 61 6e 64 20 42 54 53 5f cleared and BTS_
18d60 4f 56 45 52 57 52 49 54 45 20 69 73 20 73 65 74 OVERWRITE is set
18d70 0a 2a 2a 20 20 20 20 6e 65 77 46 6c 61 67 3d 3d .** newFlag==
18d80 28 2d 31 29 20 20 20 20 4e 6f 20 63 68 61 6e 67 (-1) No chang
18d90 65 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f es.**.** This ro
18da0 75 74 69 6e 65 20 61 63 74 73 20 61 73 20 61 20 utine acts as a
18db0 71 75 65 72 79 20 69 66 20 6e 65 77 46 6c 61 67 query if newFlag
18dc0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 is less than ze
18dd0 72 6f 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 42 54 ro.**.** With BT
18de0 53 5f 4f 56 45 52 57 52 49 54 45 20 73 65 74 2c S_OVERWRITE set,
18df0 20 64 65 6c 65 74 65 64 20 63 6f 6e 74 65 6e 74 deleted content
18e00 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 is overwritten
18e10 62 79 20 7a 65 72 6f 73 2c 20 62 75 74 0a 2a 2a by zeros, but.**
18e20 20 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 70 freelist leaf p
18e30 61 67 65 73 20 61 72 65 20 6e 6f 74 20 77 72 69 ages are not wri
18e40 74 74 65 6e 20 62 61 63 6b 20 74 6f 20 74 68 65 tten back to the
18e50 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 75 73 database. Thus
18e60 20 69 6e 2d 70 61 67 65 0a 2a 2a 20 64 65 6c 65 in-page.** dele
18e70 74 65 64 20 63 6f 6e 74 65 6e 74 20 69 73 20 63 ted content is c
18e80 6c 65 61 72 65 64 2c 20 62 75 74 20 66 72 65 65 leared, but free
18e90 6c 69 73 74 20 64 65 6c 65 74 65 64 20 63 6f 6e list deleted con
18ea0 74 65 6e 74 20 69 73 20 6e 6f 74 2e 0a 2a 2a 0a tent is not..**.
18eb0 2a 2a 20 57 69 74 68 20 42 54 53 5f 53 45 43 55 ** With BTS_SECU
18ec0 52 45 5f 44 45 4c 45 54 45 2c 20 6f 70 65 72 61 RE_DELETE, opera
18ed0 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 42 54 53 tion is like BTS
18ee0 5f 4f 56 45 52 57 52 49 54 45 20 77 69 74 68 20 _OVERWRITE with
18ef0 74 68 65 20 61 64 64 69 74 69 6f 6e 0a 2a 2a 20 the addition.**
18f00 74 68 61 74 20 66 72 65 65 6c 69 73 74 20 6c 65 that freelist le
18f10 61 66 20 70 61 67 65 73 20 61 72 65 20 77 72 69 af pages are wri
18f20 74 74 65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 tten back into t
18f30 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 6e 63 he database, inc
18f40 72 65 61 73 69 6e 67 0a 2a 2a 20 74 68 65 20 61 reasing.** the a
18f50 6d 6f 75 6e 74 20 6f 66 20 64 69 73 6b 20 49 2f mount of disk I/
18f60 4f 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 O..*/.int sqlite
18f70 33 42 74 72 65 65 53 65 63 75 72 65 44 65 6c 65 3BtreeSecureDele
18f80 74 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 te(Btree *p, int
18f90 20 6e 65 77 46 6c 61 67 29 7b 0a 20 20 69 6e 74 newFlag){. int
18fa0 20 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 b;. if( p==0 )
18fb0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 6c return 0;. sql
18fc0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
18fd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 42 54 53 );. assert( BTS
18fe0 5f 4f 56 45 52 57 52 49 54 45 3d 3d 42 54 53 5f _OVERWRITE==BTS_
18ff0 53 45 43 55 52 45 5f 44 45 4c 45 54 45 2a 32 20 SECURE_DELETE*2
19000 29 3b 0a 20 20 61 73 73 65 72 74 28 20 42 54 53 );. assert( BTS
19010 5f 46 41 53 54 5f 53 45 43 55 52 45 3d 3d 28 42 _FAST_SECURE==(B
19020 54 53 5f 4f 56 45 52 57 52 49 54 45 7c 42 54 53 TS_OVERWRITE|BTS
19030 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 20 _SECURE_DELETE)
19040 29 3b 0a 20 20 69 66 28 20 6e 65 77 46 6c 61 67 );. if( newFlag
19050 3e 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 >=0 ){. p->pB
19060 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e t->btsFlags &= ~
19070 42 54 53 5f 46 41 53 54 5f 53 45 43 55 52 45 3b BTS_FAST_SECURE;
19080 0a 20 20 20 20 70 2d 3e 70 42 74 2d 3e 62 74 73 . p->pBt->bts
19090 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53 45 43 Flags |= BTS_SEC
190a0 55 52 45 5f 44 45 4c 45 54 45 2a 6e 65 77 46 6c URE_DELETE*newFl
190b0 61 67 3b 0a 20 20 7d 0a 20 20 62 20 3d 20 28 70 ag;. }. b = (p
190c0 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 ->pBt->btsFlags
190d0 26 20 42 54 53 5f 46 41 53 54 5f 53 45 43 55 52 & BTS_FAST_SECUR
190e0 45 29 2f 42 54 53 5f 53 45 43 55 52 45 5f 44 45 E)/BTS_SECURE_DE
190f0 4c 45 54 45 3b 0a 20 20 73 71 6c 69 74 65 33 42 LETE;. sqlite3B
19100 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
19110 72 65 74 75 72 6e 20 62 3b 0a 7d 0a 0a 2f 2a 0a return b;.}../*.
19120 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 ** Change the 'a
19130 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 uto-vacuum' prop
19140 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 erty of the data
19150 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 base. If the 'au
19160 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 toVacuum'.** par
19170 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
19180 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 ro, then auto-va
19190 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 cuum mode is ena
191a0 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 bled. If zero, i
191b0 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 t.** is disabled
191c0 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 . The default va
191d0 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f lue for the auto
191e0 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 -vacuum property
191f0 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e is .** determin
19200 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 ed by the SQLITE
19210 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
19220 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 69 6e UUM macro..*/.in
19230 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
19240 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
19250 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 e *p, int autoVa
19260 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 cuum){.#ifdef SQ
19270 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
19280 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 CUUM. return SQ
19290 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 LITE_READONLY;.#
192a0 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 else. BtShared
192b0 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
192c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
192d0 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 _OK;. u8 av = (
192e0 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a u8)autoVacuum;..
192f0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
19300 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 28 70 ter(p);. if( (p
19310 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
19320 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 TS_PAGESIZE_FIXE
19330 44 29 21 3d 30 20 26 26 20 28 61 76 20 3f 31 3a D)!=0 && (av ?1:
19340 30 29 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 0)!=pBt->autoVac
19350 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 uum ){. rc =
19360 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
19370 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 . }else{. pB
19380 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 t->autoVacuum =
19390 61 76 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 av ?1:0;. pBt
193a0 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 ->incrVacuum = a
193b0 76 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 v==2 ?1:0;. }.
193c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
193d0 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
193e0 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a rc;.#endif.}../*
193f0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 .** Return the v
19400 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 alue of the 'aut
19410 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 o-vacuum' proper
19420 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 ty. If auto-vacu
19430 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 um is .** enable
19440 64 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e d 1 is returned.
19450 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f Otherwise 0..*/
19460 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
19470 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 eGetAutoVacuum(B
19480 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 tree *p){.#ifdef
19490 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
194a0 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e OVACUUM. return
194b0 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
194c0 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 M_NONE;.#else.
194d0 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
194e0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
194f0 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 rc = (. (!p
19500 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ->pBt->autoVacuu
19510 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 m)?BTREE_AUTOVAC
19520 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 UUM_NONE:. (!
19530 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 p->pBt->incrVacu
19540 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 um)?BTREE_AUTOVA
19550 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 CUUM_FULL:. B
19560 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
19570 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 INCR. );. sqli
19580 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
19590 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 ;. return rc;.#
195a0 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 endif.}../*.** I
195b0 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e f the user has n
195c0 6f 74 20 73 65 74 20 74 68 65 20 73 61 66 65 74 ot set the safet
195d0 79 2d 6c 65 76 65 6c 20 66 6f 72 20 74 68 69 73 y-level for this
195e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
195f0 74 69 6f 6e 0a 2a 2a 20 75 73 69 6e 67 20 22 50 tion.** using "P
19600 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 RAGMA synchronou
19610 73 22 2c 20 61 6e 64 20 69 66 20 74 68 65 20 73 s", and if the s
19620 61 66 65 74 79 2d 6c 65 76 65 6c 20 69 73 20 6e afety-level is n
19630 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 65 ot already.** se
19640 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 t to the value p
19650 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 assed to this fu
19660 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 73 65 nction as the se
19670 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 0a cond parameter,.
19680 2a 2a 20 73 65 74 20 69 74 20 73 6f 2e 0a 2a 2f ** set it so..*/
19690 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 .#if SQLITE_DEFA
196a0 55 4c 54 5f 53 59 4e 43 48 52 4f 4e 4f 55 53 21 ULT_SYNCHRONOUS!
196b0 3d 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f =SQLITE_DEFAULT_
196c0 57 41 4c 5f 53 59 4e 43 48 52 4f 4e 4f 55 53 20 WAL_SYNCHRONOUS
196d0 5c 0a 20 20 20 20 26 26 20 21 64 65 66 69 6e 65 \. && !define
196e0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 d(SQLITE_OMIT_WA
196f0 4c 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 L).static void s
19700 65 74 44 65 66 61 75 6c 74 53 79 6e 63 46 6c 61 etDefaultSyncFla
19710 67 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c g(BtShared *pBt,
19720 20 75 38 20 73 61 66 65 74 79 5f 6c 65 76 65 6c u8 safety_level
19730 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
19740 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 69 ;. Db *pDb;. i
19750 66 28 20 28 64 62 3d 70 42 74 2d 3e 64 62 29 21 f( (db=pBt->db)!
19760 3d 30 20 26 26 20 28 70 44 62 3d 64 62 2d 3e 61 =0 && (pDb=db->a
19770 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 77 68 Db)!=0 ){. wh
19780 69 6c 65 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 ile( pDb->pBt==0
19790 20 7c 7c 20 70 44 62 2d 3e 70 42 74 2d 3e 70 42 || pDb->pBt->pB
197a0 74 21 3d 70 42 74 20 29 7b 20 70 44 62 2b 2b 3b t!=pBt ){ pDb++;
197b0 20 7d 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e }. if( pDb->
197c0 62 53 79 6e 63 53 65 74 3d 3d 30 20 0a 20 20 20 bSyncSet==0 .
197d0 20 20 26 26 20 70 44 62 2d 3e 73 61 66 65 74 79 && pDb->safety
197e0 5f 6c 65 76 65 6c 21 3d 73 61 66 65 74 79 5f 6c _level!=safety_l
197f0 65 76 65 6c 20 0a 20 20 20 20 20 26 26 20 70 44 evel . && pD
19800 62 21 3d 26 64 62 2d 3e 61 44 62 5b 31 5d 20 0a b!=&db->aDb[1] .
19810 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 44 62 ){. pDb
19820 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d ->safety_level =
19830 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 0a 20 safety_level;.
19840 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
19850 72 53 65 74 46 6c 61 67 73 28 70 42 74 2d 3e 70 rSetFlags(pBt->p
19860 50 61 67 65 72 2c 0a 20 20 20 20 20 20 20 20 20 Pager,.
19870 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 pDb->safety_lev
19880 65 6c 20 7c 20 28 64 62 2d 3e 66 6c 61 67 73 20 el | (db->flags
19890 26 20 50 41 47 45 52 5f 46 4c 41 47 53 5f 4d 41 & PAGER_FLAGS_MA
198a0 53 4b 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a SK));. }. }.
198b0 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
198c0 20 73 65 74 44 65 66 61 75 6c 74 53 79 6e 63 46 setDefaultSyncF
198d0 6c 61 67 28 70 42 74 2c 73 61 66 65 74 79 5f 6c lag(pBt,safety_l
198e0 65 76 65 6c 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a evel).#endif../*
198f0 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 Forward declara
19900 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 tion */.static i
19910 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 42 nt newDatabase(B
19920 74 53 68 61 72 65 64 2a 29 3b 0a 0a 0a 2f 2a 0a tShared*);.../*.
19930 2a 2a 20 47 65 74 20 61 20 72 65 66 65 72 65 6e ** Get a referen
19940 63 65 20 74 6f 20 70 50 61 67 65 31 20 6f 66 20 ce to pPage1 of
19950 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
19960 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a e. This will.**
19970 20 61 6c 73 6f 20 61 63 71 75 69 72 65 20 61 20 also acquire a
19980 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 readlock on that
19990 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c file..**.** SQL
199a0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
199b0 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ed on success.
199c0 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e If the file is n
199d0 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 ot a.** well-for
199e0 6d 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c med database fil
199f0 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 e, then SQLITE_C
19a00 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 72 6e ORRUPT is return
19a10 65 64 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 ed..** SQLITE_BU
19a20 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 SY is returned i
19a30 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
19a40 73 20 6c 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 s locked. SQLIT
19a50 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 E_NOMEM.** is re
19a60 74 75 72 6e 65 64 20 69 66 20 77 65 20 72 75 6e turned if we run
19a70 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 out of memory.
19a80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c .*/.static int l
19a90 6f 63 6b 42 74 72 65 65 28 42 74 53 68 61 72 65 ockBtree(BtShare
19aa0 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 d *pBt){. int r
19ab0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
19ac0 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 /* Result code f
19ad0 72 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 rom subfunctions
19ae0 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 */. MemPage *p
19af0 50 61 67 65 31 3b 20 20 20 20 20 2f 2a 20 50 61 Page1; /* Pa
19b00 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 ge 1 of the data
19b10 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 base file */. u
19b20 33 32 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 32 nPage;
19b30 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
19b40 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 pages in the da
19b50 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 33 32 20 tabase */. u32
19b60 6e 50 61 67 65 46 69 6c 65 20 3d 20 30 3b 20 20 nPageFile = 0;
19b70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
19b80 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
19b90 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 33 ase file */. u3
19ba0 32 20 6e 50 61 67 65 48 65 61 64 65 72 3b 20 20 2 nPageHeader;
19bb0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
19bc0 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
19bd0 61 62 61 73 65 20 61 63 63 6f 72 64 69 6e 67 20 abase according
19be0 74 6f 20 68 64 72 20 2a 2f 0a 0a 20 20 61 73 73 to hdr */.. ass
19bf0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
19c00 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
19c10 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
19c20 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 pBt->pPage1==0
19c30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
19c40 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 3PagerSharedLock
19c50 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 (pBt->pPager);.
19c60 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
19c70 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
19c80 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
19c90 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 age(pBt, 1, &pPa
19ca0 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 ge1, 0);. if( r
19cb0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
19cc0 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 eturn rc;.. /*
19cd0 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 Do some checking
19ce0 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 to help insure
19cf0 74 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e the file we open
19d00 65 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a ed really is. *
19d10 2a 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 * a valid databa
19d20 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 se file. . */.
19d30 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 48 65 nPage = nPageHe
19d40 61 64 65 72 20 3d 20 67 65 74 34 62 79 74 65 28 ader = get4byte(
19d50 32 38 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 28+(u8*)pPage1->
19d60 61 44 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 aData);. sqlite
19d70 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
19d80 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 28 69 6e pBt->pPager, (in
19d90 74 2a 29 26 6e 50 61 67 65 46 69 6c 65 29 3b 0a t*)&nPageFile);.
19da0 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 7c if( nPage==0 |
19db0 7c 20 6d 65 6d 63 6d 70 28 32 34 2b 28 75 38 2a | memcmp(24+(u8*
19dc0 29 70 50 61 67 65 31 2d 3e 61 44 61 74 61 2c 20 )pPage1->aData,
19dd0 39 32 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 92+(u8*)pPage1->
19de0 61 44 61 74 61 2c 34 29 21 3d 30 20 29 7b 0a 20 aData,4)!=0 ){.
19df0 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 nPage = nPage
19e00 46 69 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 File;. }. if(
19e10 28 70 42 74 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 (pBt->db->flags
19e20 26 20 53 51 4c 49 54 45 5f 52 65 73 65 74 44 61 & SQLITE_ResetDa
19e30 74 61 62 61 73 65 29 21 3d 30 20 29 7b 0a 20 20 tabase)!=0 ){.
19e40 20 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d nPage = 0;. }
19e50 0a 20 20 69 66 28 20 6e 50 61 67 65 3e 30 20 29 . if( nPage>0 )
19e60 7b 0a 20 20 20 20 75 33 32 20 70 61 67 65 53 69 {. u32 pageSi
19e70 7a 65 3b 0a 20 20 20 20 75 33 32 20 75 73 61 62 ze;. u32 usab
19e80 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a leSize;. u8 *
19e90 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d 3e page1 = pPage1->
19ea0 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 aData;. rc =
19eb0 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 SQLITE_NOTADB;.
19ec0 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f /* EVIDENCE-O
19ed0 46 3a 20 52 2d 34 33 37 33 37 2d 33 39 39 39 39 F: R-43737-39999
19ee0 20 45 76 65 72 79 20 76 61 6c 69 64 20 53 51 4c Every valid SQL
19ef0 69 74 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ite database fil
19f00 65 20 62 65 67 69 6e 73 0a 20 20 20 20 2a 2a 20 e begins. **
19f10 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 with the followi
19f20 6e 67 20 31 36 20 62 79 74 65 73 20 28 69 6e 20 ng 16 bytes (in
19f30 68 65 78 29 3a 20 35 33 20 35 31 20 34 63 20 36 hex): 53 51 4c 6
19f40 39 20 37 34 20 36 35 20 32 30 20 36 36 20 36 66 9 74 65 20 66 6f
19f50 20 37 32 20 36 64 0a 20 20 20 20 2a 2a 20 36 31 72 6d. ** 61
19f60 20 37 34 20 32 30 20 33 33 20 30 30 2e 20 2a 2f 74 20 33 00. */
19f70 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 . if( memcmp(
19f80 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 page1, zMagicHea
19f90 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 der, 16)!=0 ){.
19fa0 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
19fb0 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
19fc0 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }..#ifdef SQLIT
19fd0 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 20 20 69 E_OMIT_WAL. i
19fe0 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 f( page1[18]>1 )
19ff0 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 {. pBt->bts
1a000 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 Flags |= BTS_REA
1a010 44 5f 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 D_ONLY;. }.
1a020 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e if( page1[19]>
1a030 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 1 ){. goto
1a040 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
1a050 64 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 d;. }.#else.
1a060 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d if( page1[18]
1a070 3e 32 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d >2 ){. pBt-
1a080 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
1a090 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0a 20 20 20 20 _READ_ONLY;.
1a0a0 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b }. if( page1[
1a0b0 31 39 5d 3e 32 20 29 7b 0a 20 20 20 20 20 20 67 19]>2 ){. g
1a0c0 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 oto page1_init_f
1a0d0 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 ailed;. }..
1a0e0 20 20 2f 2a 20 49 66 20 74 68 65 20 77 72 69 74 /* If the writ
1a0f0 65 20 76 65 72 73 69 6f 6e 20 69 73 20 73 65 74 e version is set
1a100 20 74 6f 20 32 2c 20 74 68 69 73 20 64 61 74 61 to 2, this data
1a110 62 61 73 65 20 73 68 6f 75 6c 64 20 62 65 20 61 base should be a
1a120 63 63 65 73 73 65 64 0a 20 20 20 20 2a 2a 20 69 ccessed. ** i
1a130 6e 20 57 41 4c 20 6d 6f 64 65 2e 20 49 66 20 74 n WAL mode. If t
1a140 68 65 20 6c 6f 67 20 69 73 20 6e 6f 74 20 61 6c he log is not al
1a150 72 65 61 64 79 20 6f 70 65 6e 2c 20 6f 70 65 6e ready open, open
1a160 20 69 74 20 6e 6f 77 2e 20 54 68 65 6e 20 0a 20 it now. Then .
1a170 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c ** return SQL
1a180 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 74 75 72 ITE_OK and retur
1a190 6e 20 77 69 74 68 6f 75 74 20 70 6f 70 75 6c 61 n without popula
1a1a0 74 69 6e 67 20 42 74 53 68 61 72 65 64 2e 70 50 ting BtShared.pP
1a1b0 61 67 65 31 2e 0a 20 20 20 20 2a 2a 20 54 68 65 age1.. ** The
1a1c0 20 63 61 6c 6c 65 72 20 64 65 74 65 63 74 73 20 caller detects
1a1d0 74 68 69 73 20 61 6e 64 20 63 61 6c 6c 73 20 74 this and calls t
1a1e0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 his function aga
1a1f0 69 6e 2e 20 54 68 69 73 20 69 73 0a 20 20 20 20 in. This is.
1a200 2a 2a 20 72 65 71 75 69 72 65 64 20 61 73 20 74 ** required as t
1a210 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 he version of pa
1a220 67 65 20 31 20 63 75 72 72 65 6e 74 6c 79 20 69 ge 1 currently i
1a230 6e 20 74 68 65 20 70 61 67 65 31 20 62 75 66 66 n the page1 buff
1a240 65 72 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f er. ** may no
1a250 74 20 62 65 20 74 68 65 20 6c 61 74 65 73 74 20 t be the latest
1a260 76 65 72 73 69 6f 6e 20 2d 20 74 68 65 72 65 20 version - there
1a270 6d 61 79 20 62 65 20 61 20 6e 65 77 65 72 20 6f may be a newer o
1a280 6e 65 20 69 6e 20 74 68 65 20 6c 6f 67 0a 20 20 ne in the log.
1a290 20 20 2a 2a 20 66 69 6c 65 2e 0a 20 20 20 20 2a ** file.. *
1a2a0 2f 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b /. if( page1[
1a2b0 31 39 5d 3d 3d 32 20 26 26 20 28 70 42 74 2d 3e 19]==2 && (pBt->
1a2c0 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 4e btsFlags & BTS_N
1a2d0 4f 5f 57 41 4c 29 3d 3d 30 20 29 7b 0a 20 20 20 O_WAL)==0 ){.
1a2e0 20 20 20 69 6e 74 20 69 73 4f 70 65 6e 20 3d 20 int isOpen =
1a2f0 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 0;. rc = sq
1a300 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 57 61 lite3PagerOpenWa
1a310 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 l(pBt->pPager, &
1a320 69 73 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 69 isOpen);. i
1a330 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1a340 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
1a350 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
1a360 65 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ed;. }else{
1a370 0a 20 20 20 20 20 20 20 20 73 65 74 44 65 66 61 . setDefa
1a380 75 6c 74 53 79 6e 63 46 6c 61 67 28 70 42 74 2c ultSyncFlag(pBt,
1a390 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1a3a0 57 41 4c 5f 53 59 4e 43 48 52 4f 4e 4f 55 53 2b WAL_SYNCHRONOUS+
1a3b0 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1);. if(
1a3c0 69 73 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 isOpen==0 ){.
1a3d0 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1a3e0 67 65 4f 6e 65 28 70 50 61 67 65 31 29 3b 0a 20 geOne(pPage1);.
1a3f0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1a400 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
1a410 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1a420 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
1a430 4f 54 41 44 42 3b 0a 20 20 20 20 7d 65 6c 73 65 OTADB;. }else
1a440 7b 0a 20 20 20 20 20 20 73 65 74 44 65 66 61 75 {. setDefau
1a450 6c 74 53 79 6e 63 46 6c 61 67 28 70 42 74 2c 20 ltSyncFlag(pBt,
1a460 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 SQLITE_DEFAULT_S
1a470 59 4e 43 48 52 4f 4e 4f 55 53 2b 31 29 3b 0a 20 YNCHRONOUS+1);.
1a480 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
1a490 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a /* EVIDENCE-OF:
1a4a0 20 52 2d 31 35 34 36 35 2d 32 30 38 31 33 20 54 R-15465-20813 T
1a4b0 68 65 20 6d 61 78 69 6d 75 6d 20 61 6e 64 20 6d he maximum and m
1a4c0 69 6e 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 inimum embedded
1a4d0 70 61 79 6c 6f 61 64 0a 20 20 20 20 2a 2a 20 66 payload. ** f
1a4e0 72 61 63 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 ractions and the
1a4f0 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 leaf payload fr
1a500 61 63 74 69 6f 6e 20 76 61 6c 75 65 73 20 6d 75 action values mu
1a510 73 74 20 62 65 20 36 34 2c 20 33 32 2c 20 61 6e st be 64, 32, an
1a520 64 20 33 32 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 d 32.. **.
1a530 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c ** The original
1a540 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 design allowed
1a550 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f these amounts to
1a560 20 76 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 vary, but as of
1a570 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 . ** version
1a580 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 3.6.0, we requir
1a590 65 20 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 e them to be fix
1a5a0 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
1a5b0 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 f( memcmp(&page1
1a5c0 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c [21], "\100\040\
1a5d0 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 040",3)!=0 ){.
1a5e0 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 goto page1_i
1a5f0 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
1a600 7d 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 }. /* EVIDENC
1a610 45 2d 4f 46 3a 20 52 2d 35 31 38 37 33 2d 33 39 E-OF: R-51873-39
1a620 36 31 38 20 54 68 65 20 70 61 67 65 20 73 69 7a 618 The page siz
1a630 65 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 e for a database
1a640 20 66 69 6c 65 20 69 73 0a 20 20 20 20 2a 2a 20 file is. **
1a650 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
1a660 65 20 32 2d 62 79 74 65 20 69 6e 74 65 67 65 72 e 2-byte integer
1a670 20 6c 6f 63 61 74 65 64 20 61 74 20 61 6e 20 6f located at an o
1a680 66 66 73 65 74 20 6f 66 20 31 36 20 62 79 74 65 ffset of 16 byte
1a690 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 68 s from. ** th
1a6a0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
1a6b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1a6c0 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 53 69 7a . */. pageSiz
1a6d0 65 20 3d 20 28 70 61 67 65 31 5b 31 36 5d 3c 3c e = (page1[16]<<
1a6e0 38 29 20 7c 20 28 70 61 67 65 31 5b 31 37 5d 3c 8) | (page1[17]<
1a6f0 3c 31 36 29 3b 0a 20 20 20 20 2f 2a 20 45 56 49 <16);. /* EVI
1a700 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 35 30 30 DENCE-OF: R-2500
1a710 38 2d 32 31 36 38 38 20 54 68 65 20 73 69 7a 65 8-21688 The size
1a720 20 6f 66 20 61 20 70 61 67 65 20 69 73 20 61 20 of a page is a
1a730 70 6f 77 65 72 20 6f 66 20 74 77 6f 0a 20 20 20 power of two.
1a740 20 2a 2a 20 62 65 74 77 65 65 6e 20 35 31 32 20 ** between 512
1a750 61 6e 64 20 36 35 35 33 36 20 69 6e 63 6c 75 73 and 65536 inclus
1a760 69 76 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ive. */. if(
1a770 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 ((pageSize-1)&pa
1a780 67 65 53 69 7a 65 29 21 3d 30 0a 20 20 20 20 20 geSize)!=0.
1a790 7c 7c 20 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 || pageSize>SQLI
1a7a0 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
1a7b0 20 0a 20 20 20 20 20 7c 7c 20 70 61 67 65 53 69 . || pageSi
1a7c0 7a 65 3c 3d 32 35 36 20 0a 20 20 20 20 29 7b 0a ze<=256 . ){.
1a7d0 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
1a7e0 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
1a7f0 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 }. pBt->bts
1a800 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 Flags |= BTS_PAG
1a810 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a 20 20 20 ESIZE_FIXED;.
1a820 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 assert( (pageSi
1a830 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 ze & 7)==0 );.
1a840 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 /* EVIDENCE-OF
1a850 3a 20 52 2d 35 39 33 31 30 2d 35 31 32 30 35 20 : R-59310-51205
1a860 54 68 65 20 22 72 65 73 65 72 76 65 64 20 73 70 The "reserved sp
1a870 61 63 65 22 20 73 69 7a 65 20 69 6e 20 74 68 65 ace" size in the
1a880 20 31 2d 62 79 74 65 0a 20 20 20 20 2a 2a 20 69 1-byte. ** i
1a890 6e 74 65 67 65 72 20 61 74 20 6f 66 66 73 65 74 nteger at offset
1a8a0 20 32 30 20 69 73 20 74 68 65 20 6e 75 6d 62 65 20 is the numbe
1a8b0 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
1a8c0 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f ace at the end o
1a8d0 66 0a 20 20 20 20 2a 2a 20 65 61 63 68 20 70 61 f. ** each pa
1a8e0 67 65 20 74 6f 20 72 65 73 65 72 76 65 20 66 6f ge to reserve fo
1a8f0 72 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 0a 20 r extensions. .
1a900 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 56 49 **. ** EVI
1a910 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 33 37 34 39 DENCE-OF: R-3749
1a920 37 2d 34 32 34 31 32 20 54 68 65 20 73 69 7a 65 7-42412 The size
1a930 20 6f 66 20 74 68 65 20 72 65 73 65 72 76 65 64 of the reserved
1a940 20 72 65 67 69 6f 6e 20 69 73 0a 20 20 20 20 2a region is. *
1a950 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 * determined by
1a960 74 68 65 20 6f 6e 65 2d 62 79 74 65 20 75 6e 73 the one-byte uns
1a970 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 66 6f igned integer fo
1a980 75 6e 64 20 61 74 20 61 6e 20 6f 66 66 73 65 74 und at an offset
1a990 20 6f 66 20 32 30 0a 20 20 20 20 2a 2a 20 69 6e of 20. ** in
1a9a0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1a9b0 66 69 6c 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a file header. */.
1a9c0 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d usableSize =
1a9d0 20 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 pageSize - page
1a9e0 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 28 1[20];. if( (
1a9f0 75 33 32 29 70 61 67 65 53 69 7a 65 21 3d 70 42 u32)pageSize!=pB
1aa00 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 t->pageSize ){.
1aa10 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 /* After re
1aa20 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 ading the first
1aa30 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
1aa40 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 base assuming a
1aa50 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 page size.
1aa60 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 ** of BtShared.p
1aa70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 ageSize, we have
1aa80 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 discovered that
1aa90 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 the page-size i
1aaa0 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 s. ** actua
1aab0 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e lly pageSize. Un
1aac0 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
1aad0 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 e, leave pBt->pP
1aae0 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a age1 at. **
1aaf0 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e zero and return
1ab00 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 SQLITE_OK. The
1ab10 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c caller will call
1ab20 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 this function.
1ab30 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 ** again wi
1ab40 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 th the correct p
1ab50 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 age-size..
1ab60 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 */. release
1ab70 50 61 67 65 4f 6e 65 28 70 50 61 67 65 31 29 3b PageOne(pPage1);
1ab80 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 . pBt->usab
1ab90 6c 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 leSize = usableS
1aba0 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e ize;. pBt->
1abb0 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 pageSize = pageS
1abc0 69 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 ize;. freeT
1abd0 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 empSpace(pBt);.
1abe0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1abf0 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
1ac00 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 e(pBt->pPager, &
1ac10 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 pBt->pageSize,.
1ac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ac30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ac40 20 20 70 61 67 65 53 69 7a 65 2d 75 73 61 62 6c pageSize-usabl
1ac50 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 65 eSize);. re
1ac60 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
1ac70 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 72 if( sqlite3Wr
1ac80 69 74 61 62 6c 65 53 63 68 65 6d 61 28 70 42 74 itableSchema(pBt
1ac90 2d 3e 64 62 29 3d 3d 30 20 26 26 20 6e 50 61 67 ->db)==0 && nPag
1aca0 65 3e 6e 50 61 67 65 46 69 6c 65 20 29 7b 0a 20 e>nPageFile ){.
1acb0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1acc0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1acd0 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
1ace0 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
1acf0 20 7d 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e }. /* EVIDEN
1ad00 43 45 2d 4f 46 3a 20 52 2d 32 38 33 31 32 2d 36 CE-OF: R-28312-6
1ad10 34 37 30 34 20 48 6f 77 65 76 65 72 2c 20 74 68 4704 However, th
1ad20 65 20 75 73 61 62 6c 65 20 73 69 7a 65 20 69 73 e usable size is
1ad30 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a not allowed to.
1ad40 20 20 20 20 2a 2a 20 62 65 20 6c 65 73 73 20 74 ** be less t
1ad50 68 61 6e 20 34 38 30 2e 20 49 6e 20 6f 74 68 65 han 480. In othe
1ad60 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 r words, if the
1ad70 70 61 67 65 20 73 69 7a 65 20 69 73 20 35 31 32 page size is 512
1ad80 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a , then the. *
1ad90 2a 20 72 65 73 65 72 76 65 64 20 73 70 61 63 65 * reserved space
1ada0 20 73 69 7a 65 20 63 61 6e 6e 6f 74 20 65 78 63 size cannot exc
1adb0 65 65 64 20 33 32 2e 20 2a 2f 0a 20 20 20 20 69 eed 32. */. i
1adc0 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38 f( usableSize<48
1add0 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
1ade0 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
1adf0 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 d;. }. pBt
1ae00 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67 ->pageSize = pag
1ae10 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d 3e eSize;. pBt->
1ae20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 75 73 61 usableSize = usa
1ae30 62 6c 65 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 bleSize;.#ifndef
1ae40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1ae50 4f 56 41 43 55 55 4d 0a 20 20 20 20 70 42 74 2d OVACUUM. pBt-
1ae60 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 >autoVacuum = (g
1ae70 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 et4byte(&page1[3
1ae80 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 6 + 4*4])?1:0);.
1ae90 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 pBt->incrVac
1aea0 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 uum = (get4byte(
1aeb0 26 70 61 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d &page1[36 + 7*4]
1aec0 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 )?1:0);.#endif.
1aed0 20 7d 0a 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 }.. /* maxLoca
1aee0 6c 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d l is the maximum
1aef0 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f amount of paylo
1af00 61 64 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 ad to store loca
1af10 6c 6c 79 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 lly for. ** a c
1af20 65 6c 6c 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 ell. Make sure
1af30 69 74 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 it is small enou
1af40 67 68 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 gh so that at le
1af50 61 73 74 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 ast minFanout.
1af60 2a 2a 20 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c ** cells can wil
1af70 6c 20 66 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 l fit on one pag
1af80 65 2e 20 20 57 65 20 61 73 73 75 6d 65 20 61 20 e. We assume a
1af90 31 30 2d 62 79 74 65 20 70 61 67 65 20 68 65 61 10-byte page hea
1afa0 64 65 72 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 der.. ** Beside
1afb0 73 20 74 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 s the payload, t
1afc0 68 65 20 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f he cell must sto
1afd0 72 65 3a 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 re:. ** 2-b
1afe0 79 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 yte pointer to t
1aff0 68 65 20 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 he cell. **
1b000 20 34 2d 62 79 74 65 20 63 68 69 6c 64 20 70 6f 4-byte child po
1b010 69 6e 74 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 inter. ** 9
1b020 2d 62 79 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 -byte nKey value
1b030 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 . ** 4-byte
1b040 20 6e 44 61 74 61 20 76 61 6c 75 65 0a 20 20 2a nData value. *
1b050 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6f 76 65 * 4-byte ove
1b060 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 rflow page point
1b070 65 72 0a 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c er. ** So a cel
1b080 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 l consists of a
1b090 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72 2c 20 2-byte pointer,
1b0a0 61 20 68 65 61 64 65 72 20 77 68 69 63 68 20 69 a header which i
1b0b0 73 20 61 73 20 6d 75 63 68 20 61 73 0a 20 20 2a s as much as. *
1b0c0 2a 20 31 37 20 62 79 74 65 73 20 6c 6f 6e 67 2c * 17 bytes long,
1b0d0 20 30 20 74 6f 20 4e 20 62 79 74 65 73 20 6f 66 0 to N bytes of
1b0e0 20 70 61 79 6c 6f 61 64 2c 20 61 6e 64 20 61 6e payload, and an
1b0f0 20 6f 70 74 69 6f 6e 61 6c 20 34 20 62 79 74 65 optional 4 byte
1b100 20 6f 76 65 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 overflow. ** p
1b110 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 2a age pointer.. *
1b120 2f 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 /. pBt->maxLoca
1b130 6c 20 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e l = (u16)((pBt->
1b140 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 usableSize-12)*6
1b150 34 2f 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 70 4/255 - 23);. p
1b160 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 Bt->minLocal = (
1b170 75 31 36 29 28 28 70 42 74 2d 3e 75 73 61 62 6c u16)((pBt->usabl
1b180 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 eSize-12)*32/255
1b190 20 2d 20 32 33 29 3b 0a 20 20 70 42 74 2d 3e 6d - 23);. pBt->m
1b1a0 61 78 4c 65 61 66 20 3d 20 28 75 31 36 29 28 70 axLeaf = (u16)(p
1b1b0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
1b1c0 20 33 35 29 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 35);. pBt->min
1b1d0 4c 65 61 66 20 3d 20 28 75 31 36 29 28 28 70 42 Leaf = (u16)((pB
1b1e0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
1b1f0 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 29 3b 0a )*32/255 - 23);.
1b200 20 20 69 66 28 20 70 42 74 2d 3e 6d 61 78 4c 6f if( pBt->maxLo
1b210 63 61 6c 3e 31 32 37 20 29 7b 0a 20 20 20 20 70 cal>127 ){. p
1b220 42 74 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c Bt->max1bytePayl
1b230 6f 61 64 20 3d 20 31 32 37 3b 0a 20 20 7d 65 6c oad = 127;. }el
1b240 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 6d 61 78 se{. pBt->max
1b250 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d 20 28 1bytePayload = (
1b260 75 38 29 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c u8)pBt->maxLocal
1b270 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1b280 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 2b 20 32 pBt->maxLeaf + 2
1b290 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 3 <= MX_CELL_SIZ
1b2a0 45 28 70 42 74 29 20 29 3b 0a 20 20 70 42 74 2d E(pBt) );. pBt-
1b2b0 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67 65 31 >pPage1 = pPage1
1b2c0 3b 0a 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d ;. pBt->nPage =
1b2d0 20 6e 50 61 67 65 3b 0a 20 20 72 65 74 75 72 6e nPage;. return
1b2e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 SQLITE_OK;..pag
1b2f0 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a e1_init_failed:.
1b300 20 20 72 65 6c 65 61 73 65 50 61 67 65 4f 6e 65 releasePageOne
1b310 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d (pPage1);. pBt-
1b320 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 >pPage1 = 0;. r
1b330 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 eturn rc;.}..#if
1b340 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
1b350 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1b360 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 6f ber of cursors o
1b370 70 65 6e 20 6f 6e 20 70 42 74 2e 20 54 68 69 73 pen on pBt. This
1b380 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 is for use.** i
1b390 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72 65 n assert() expre
1b3a0 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 ssions, so it is
1b3b0 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 only compiled i
1b3c0 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a f NDEBUG is not.
1b3d0 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a ** defined..**.*
1b3e0 2a 20 4f 6e 6c 79 20 77 72 69 74 65 20 63 75 72 * Only write cur
1b3f0 73 6f 72 73 20 61 72 65 20 63 6f 75 6e 74 65 64 sors are counted
1b400 20 69 66 20 77 72 4f 6e 6c 79 20 69 73 20 74 72 if wrOnly is tr
1b410 75 65 2e 20 20 49 66 20 77 72 4f 6e 6c 79 20 69 ue. If wrOnly i
1b420 73 0a 2a 2a 20 66 61 6c 73 65 20 74 68 65 6e 20 s.** false then
1b430 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 20 all cursors are
1b440 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 counted..**.** F
1b450 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 or the purposes
1b460 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c of this routine,
1b470 20 61 20 63 75 72 73 6f 72 20 69 73 20 61 6e 79 a cursor is any
1b480 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 cursor that.**
1b490 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 72 65 is capable of re
1b4a0 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
1b4b0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1b4c0 2e 20 20 43 75 72 73 6f 72 73 20 74 68 61 74 0a . Cursors that.
1b4d0 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 74 72 69 ** have been tri
1b4e0 70 70 65 64 20 69 6e 74 6f 20 74 68 65 20 43 55 pped into the CU
1b4f0 52 53 4f 52 5f 46 41 55 4c 54 20 73 74 61 74 65 RSOR_FAULT state
1b500 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 are not counted
1b510 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1b520 63 6f 75 6e 74 56 61 6c 69 64 43 75 72 73 6f 72 countValidCursor
1b530 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c s(BtShared *pBt,
1b540 20 69 6e 74 20 77 72 4f 6e 6c 79 29 7b 0a 20 20 int wrOnly){.
1b550 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a BtCursor *pCur;.
1b560 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 int r = 0;. f
1b570 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 or(pCur=pBt->pCu
1b580 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 rsor; pCur; pCur
1b590 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 =pCur->pNext){.
1b5a0 20 20 20 69 66 28 20 28 77 72 4f 6e 6c 79 3d 3d if( (wrOnly==
1b5b0 30 20 7c 7c 20 28 70 43 75 72 2d 3e 63 75 72 46 0 || (pCur->curF
1b5c0 6c 61 67 73 20 26 20 42 54 43 46 5f 57 72 69 74 lags & BTCF_Writ
1b5d0 65 46 6c 61 67 29 21 3d 30 29 0a 20 20 20 20 20 eFlag)!=0).
1b5e0 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 && pCur->eState!
1b5f0 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 =CURSOR_FAULT )
1b600 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 r++; . }. retu
1b610 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a rn r;.}.#endif..
1b620 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 /*.** If there a
1b630 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e re no outstandin
1b640 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 g cursors and we
1b650 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 are not in the
1b660 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 middle.** of a t
1b670 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 ransaction but t
1b680 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20 6c here is a read l
1b690 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
1b6a0 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 ase, then.** thi
1b6b0 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 s routine unrefs
1b6c0 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
1b6d0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1b6e0 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 file which .** h
1b6f0 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 as the effect of
1b700 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 releasing the r
1b710 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 ead lock..**.**
1b720 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72 If there is a tr
1b730 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f ansaction in pro
1b740 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 gress, this rout
1b750 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
1b760 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
1b770 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
1b780 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ed(BtShared *pBt
1b790 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b7a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b7b0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1b7c0 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 56 61 assert( countVa
1b7d0 6c 69 64 43 75 72 73 6f 72 73 28 70 42 74 2c 30 lidCursors(pBt,0
1b7e0 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 )==0 || pBt->inT
1b7f0 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 ransaction>TRANS
1b800 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 _NONE );. if( p
1b810 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1b820 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 n==TRANS_NONE &&
1b830 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 pBt->pPage1!=0
1b840 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a ){. MemPage *
1b850 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 pPage1 = pBt->pP
1b860 61 67 65 31 3b 0a 20 20 20 20 61 73 73 65 72 74 age1;. assert
1b870 28 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 ( pPage1->aData
1b880 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
1b890 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
1b8a0 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
1b8b0 3d 3d 31 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e ==1 );. pBt->
1b8c0 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 pPage1 = 0;.
1b8d0 72 65 6c 65 61 73 65 50 61 67 65 4f 6e 65 28 70 releasePageOne(p
1b8e0 50 61 67 65 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f Page1);. }.}../
1b8f0 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e *.** If pBt poin
1b900 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 ts to an empty f
1b910 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 ile then convert
1b920 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 that empty file
1b930 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 .** into a new e
1b940 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 mpty database by
1b950 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
1b960 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a e first page of.
1b970 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ** the database.
1b980 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e .*/.static int n
1b990 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 ewDatabase(BtSha
1b9a0 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d red *pBt){. Mem
1b9b0 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 Page *pP1;. uns
1b9c0 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
1b9d0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
1b9e0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b9f0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1ba00 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
1ba10 42 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 Bt->nPage>0 ){.
1ba20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1ba30 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d _OK;. }. pP1 =
1ba40 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 pBt->pPage1;.
1ba50 61 73 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 assert( pP1!=0 )
1ba60 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e ;. data = pP1->
1ba70 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 aData;. rc = sq
1ba80 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1ba90 70 50 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 pP1->pDbPage);.
1baa0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
1bab0 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 rc;. memcpy(da
1bac0 74 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 ta, zMagicHeader
1bad0 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 , sizeof(zMagicH
1bae0 65 61 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 eader));. asser
1baf0 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 t( sizeof(zMagic
1bb00 48 65 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 Header)==16 );.
1bb10 20 64 61 74 61 5b 31 36 5d 20 3d 20 28 75 38 29 data[16] = (u8)
1bb20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e ((pBt->pageSize>
1bb30 3e 38 29 26 30 78 66 66 29 3b 0a 20 20 64 61 74 >8)&0xff);. dat
1bb40 61 5b 31 37 5d 20 3d 20 28 75 38 29 28 28 70 42 a[17] = (u8)((pB
1bb50 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 31 36 29 t->pageSize>>16)
1bb60 26 30 78 66 66 29 3b 0a 20 20 64 61 74 61 5b 31 &0xff);. data[1
1bb70 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 8] = 1;. data[1
1bb80 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 9] = 1;. assert
1bb90 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ( pBt->usableSiz
1bba0 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e<=pBt->pageSize
1bbb0 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 && pBt->usableS
1bbc0 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 ize+255>=pBt->pa
1bbd0 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b geSize);. data[
1bbe0 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e 20] = (u8)(pBt->
1bbf0 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e pageSize - pBt->
1bc00 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64 usableSize);. d
1bc10 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 ata[21] = 64;.
1bc20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 data[22] = 32;.
1bc30 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a data[23] = 32;.
1bc40 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 memset(&data[2
1bc50 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 4], 0, 100-24);.
1bc60 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 zeroPage(pP1,
1bc70 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c PTF_INTKEY|PTF_L
1bc80 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 EAF|PTF_LEAFDATA
1bc90 20 29 3b 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c );. pBt->btsFl
1bca0 61 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 ags |= BTS_PAGES
1bcb0 49 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e 64 IZE_FIXED;.#ifnd
1bcc0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1bcd0 55 54 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 UTOVACUUM. asse
1bce0 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 rt( pBt->autoVac
1bcf0 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 uum==1 || pBt->a
1bd00 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a utoVacuum==0 );.
1bd10 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
1bd20 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 ncrVacuum==1 ||
1bd30 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d pBt->incrVacuum=
1bd40 3d 30 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 =0 );. put4byte
1bd50 28 26 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d (&data[36 + 4*4]
1bd60 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 , pBt->autoVacuu
1bd70 6d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 m);. put4byte(&
1bd80 64 61 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 data[36 + 7*4],
1bd90 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 pBt->incrVacuum)
1bda0 3b 0a 23 65 6e 64 69 66 0a 20 20 70 42 74 2d 3e ;.#endif. pBt->
1bdb0 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 64 61 74 nPage = 1;. dat
1bdc0 61 5b 33 31 5d 20 3d 20 31 3b 0a 20 20 72 65 74 a[31] = 1;. ret
1bdd0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1bde0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
1bdf0 7a 65 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ze the first pag
1be00 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1be10 65 20 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 e file (creating
1be20 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 a database.** c
1be30 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 onsisting of a s
1be40 69 6e 67 6c 65 20 70 61 67 65 20 61 6e 64 20 6e ingle page and n
1be50 6f 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 o schema objects
1be60 29 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ). Return SQLITE
1be70 5f 4f 4b 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 _OK.** if succes
1be80 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 sful, or an SQLi
1be90 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 te error code ot
1bea0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 69 6e 74 20 herwise..*/.int
1beb0 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 77 44 sqlite3BtreeNewD
1bec0 62 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 b(Btree *p){. i
1bed0 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 nt rc;. sqlite3
1bee0 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
1bef0 20 70 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 20 3d p->pBt->nPage =
1bf00 20 30 3b 0a 20 20 72 63 20 3d 20 6e 65 77 44 61 0;. rc = newDa
1bf10 74 61 62 61 73 65 28 70 2d 3e 70 42 74 29 3b 0a tabase(p->pBt);.
1bf20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1bf30 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1bf40 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 rc;.}../*.** At
1bf50 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 tempt to start a
1bf60 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e new transaction
1bf70 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 . A write-transa
1bf80 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 ction.** is star
1bf90 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e ted if the secon
1bfa0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f d argument is no
1bfb0 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 nzero, otherwise
1bfc0 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e a read-.** tran
1bfd0 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 saction. If the
1bfe0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1bff0 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e is 2 or more an
1c000 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 d exclusive.** t
1c010 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 ransaction is st
1c020 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 arted, meaning t
1c030 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f hat no other pro
1c040 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a cess is allowed.
1c050 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ** to access the
1c060 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 database. A pr
1c070 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 eexisting transa
1c080 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 ction may not be
1c090 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 .** upgraded to
1c0a0 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c exclusive by cal
1c0b0 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
1c0c0 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 e a second time
1c0d0 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 - the.** exclusi
1c0e0 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 vity flag only w
1c0f0 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 orks for a new t
1c100 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
1c110 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 * A write-transa
1c120 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
1c130 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 arted before att
1c140 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 empting any .**
1c150 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 changes to the d
1c160 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f atabase. None o
1c170 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
1c180 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c routines .** wil
1c190 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 l work unless a
1c1a0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1c1b0 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a tarted first:.**
1c1c0 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
1c1d0 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 BtreeCreateTable
1c1e0 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
1c1f0 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 e3BtreeCreateInd
1c200 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c ex().** sql
1c210 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 ite3BtreeClearTa
1c220 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ble().** sq
1c230 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
1c240 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ble().** sq
1c250 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 lite3BtreeInsert
1c260 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
1c270 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a e3BtreeDelete().
1c280 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
1c290 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 treeUpdateMeta()
1c2a0 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 .**.** If an ini
1c2b0 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 tial attempt to
1c2c0 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b acquire the lock
1c2d0 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f fails because o
1c2e0 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f f lock contentio
1c2f0 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 n.** and the dat
1c300 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69 6f abase was previo
1c310 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 usly unlocked, t
1c320 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 hen invoke the b
1c330 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 usy handler.** i
1c340 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 f there is one.
1c350 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77 61 But if there wa
1c360 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 s previously a r
1c370 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 ead-lock, do not
1c380 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 .** invoke the b
1c390 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 usy handler - ju
1c3a0 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 st return SQLITE
1c3b0 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 _BUSY. SQLITE_B
1c3c0 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 USY is .** retur
1c3d0 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 ned when there i
1c3e0 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 s already a read
1c3f0 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 -lock in order t
1c400 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f o avoid a deadlo
1c410 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 ck..**.** Suppos
1c420 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 e there are two
1c430 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 processes A and
1c440 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61 64 B. A has a read
1c450 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a lock and B has.
1c460 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f ** a reserved lo
1c470 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 ck. B tries to
1c480 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 promote to exclu
1c490 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 sive but is bloc
1c4a0 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f ked because.** o
1c4b0 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e f A's read lock.
1c4c0 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f A tries to pro
1c4d0 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 mote to reserved
1c4e0 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 but is blocked
1c4f0 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 by B..** One or
1c500 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 the other of the
1c510 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d two processes m
1c520 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 ust give way or
1c530 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 there can be.**
1c540 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 no progress. By
1c550 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 returning SQLIT
1c560 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 E_BUSY and not i
1c570 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 nvoking the busy
1c580 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 callback.** whe
1c590 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 n A already has
1c5a0 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 a read lock, we
1c5b0 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 encourage A to g
1c5c0 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 ive up and let B
1c5d0 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a .** proceed..*/.
1c5e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1c5f0 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 BeginTrans(Btree
1c600 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 2c *p, int wrflag,
1c610 20 69 6e 74 20 2a 70 53 63 68 65 6d 61 56 65 72 int *pSchemaVer
1c620 73 69 6f 6e 29 7b 0a 20 20 42 74 53 68 61 72 65 sion){. BtShare
1c630 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1c640 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1c650 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 TE_OK;.. sqlite
1c660 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1c670 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 btreeIntegrity
1c680 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 (p);.. /* If th
1c690 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 e btree is alrea
1c6a0 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 dy in a write-tr
1c6b0 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 ansaction, or it
1c6c0 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 . ** is already
1c6d0 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 in a read-trans
1c6e0 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 action and a rea
1c6f0 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 d-transaction.
1c700 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ** is requested,
1c710 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 this is a no-op
1c720 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
1c730 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
1c740 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 RITE || (p->inTr
1c750 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 ans==TRANS_READ
1c760 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 && !wrflag) ){.
1c770 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 goto trans_be
1c780 67 75 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 gun;. }. asser
1c790 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
1c7a0 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
1c7b0 54 45 20 7c 7c 20 49 66 4e 6f 74 4f 6d 69 74 41 TE || IfNotOmitA
1c7c0 56 28 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 V(pBt->bDoTrunca
1c7d0 74 65 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 te)==0 );.. if(
1c7e0 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 (p->db->flags &
1c7f0 20 53 51 4c 49 54 45 5f 52 65 73 65 74 44 61 74 SQLITE_ResetDat
1c800 61 62 61 73 65 29 20 0a 20 20 20 26 26 20 73 71 abase) . && sq
1c810 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 lite3PagerIsread
1c820 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 only(pBt->pPager
1c830 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20 20 70 )==0 . ){. p
1c840 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 Bt->btsFlags &=
1c850 7e 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0a ~BTS_READ_ONLY;.
1c860 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 }.. /* Write
1c870 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 transactions are
1c880 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 6f 6e not possible on
1c890 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 a read-only dat
1c8a0 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 20 28 abase */. if( (
1c8b0 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
1c8c0 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d BTS_READ_ONLY)!=
1c8d0 30 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 0 && wrflag ){.
1c8e0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 rc = SQLITE_R
1c8f0 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 EADONLY;. got
1c900 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 o trans_begun;.
1c910 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
1c920 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
1c930 41 43 48 45 0a 20 20 7b 0a 20 20 20 20 73 71 6c ACHE. {. sql
1c940 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 3d 20 30 ite3 *pBlock = 0
1c950 3b 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 6f 74 ;. /* If anot
1c960 68 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e her database han
1c970 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 dle has already
1c980 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 opened a write t
1c990 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 20 20 ransaction .
1c9a0 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 ** on this share
1c9b0 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75 72 d-btree structur
1c9c0 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 e and a second w
1c9d0 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
1c9e0 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 is. ** reque
1c9f0 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c sted, return SQL
1ca00 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 20 20 ITE_LOCKED..
1ca10 2a 2f 0a 20 20 20 20 69 66 28 20 28 77 72 66 6c */. if( (wrfl
1ca20 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 ag && pBt->inTra
1ca30 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
1ca40 57 52 49 54 45 29 0a 20 20 20 20 20 7c 7c 20 28 WRITE). || (
1ca50 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
1ca60 42 54 53 5f 50 45 4e 44 49 4e 47 29 21 3d 30 0a BTS_PENDING)!=0.
1ca70 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 42 6c ){. pBl
1ca80 6f 63 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 ock = pBt->pWrit
1ca90 65 72 2d 3e 64 62 3b 0a 20 20 20 20 7d 65 6c 73 er->db;. }els
1caa0 65 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 e if( wrflag>1 )
1cab0 7b 0a 20 20 20 20 20 20 42 74 4c 6f 63 6b 20 2a {. BtLock *
1cac0 70 49 74 65 72 3b 0a 20 20 20 20 20 20 66 6f 72 pIter;. for
1cad0 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 (pIter=pBt->pLoc
1cae0 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d k; pIter; pIter=
1caf0 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 pIter->pNext){.
1cb00 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72 if( pIter
1cb10 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20 ->pBtree!=p ){.
1cb20 20 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 pBlock
1cb30 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d = pIter->pBtree-
1cb40 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 20 20 62 >db;. b
1cb50 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
1cb60 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1cb70 20 20 69 66 28 20 70 42 6c 6f 63 6b 20 29 7b 0a if( pBlock ){.
1cb80 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e sqlite3Con
1cb90 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 nectionBlocked(p
1cba0 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a 20 ->db, pBlock);.
1cbb0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1cbc0 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
1cbd0 43 48 45 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 CHE;. goto
1cbe0 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 20 trans_begun;.
1cbf0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
1cc00 20 2f 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c /* Any read-onl
1cc10 79 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 y or read-write
1cc20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c transaction impl
1cc30 69 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 ies a read-lock
1cc40 6f 6e 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e on . ** page 1.
1cc50 20 53 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 So if some othe
1cc60 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 r shared-cache c
1cc70 6c 69 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 lient already ha
1cc80 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a s a write-lock .
1cc90 20 20 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 ** on page 1,
1cca0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
1ccb0 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 cannot be opened
1ccc0 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 . */. rc = quer
1ccd0 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
1cce0 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f eLock(p, MASTER_
1ccf0 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 ROOT, READ_LOCK)
1cd00 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f ;. if( SQLITE_O
1cd10 4b 21 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 K!=rc ) goto tra
1cd20 6e 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 70 42 74 ns_begun;.. pBt
1cd30 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 ->btsFlags &= ~B
1cd40 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 TS_INITIALLY_EMP
1cd50 54 59 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e TY;. if( pBt->n
1cd60 50 61 67 65 3d 3d 30 20 29 20 70 42 74 2d 3e 62 Page==0 ) pBt->b
1cd70 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 49 tsFlags |= BTS_I
1cd80 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 3b 0a NITIALLY_EMPTY;.
1cd90 20 20 64 6f 20 7b 0a 20 20 20 20 2f 2a 20 43 61 do {. /* Ca
1cda0 6c 6c 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 75 ll lockBtree() u
1cdb0 6e 74 69 6c 20 65 69 74 68 65 72 20 70 42 74 2d ntil either pBt-
1cdc0 3e 70 50 61 67 65 31 20 69 73 20 70 6f 70 75 6c >pPage1 is popul
1cdd0 61 74 65 64 20 6f 72 0a 20 20 20 20 2a 2a 20 6c ated or. ** l
1cde0 6f 63 6b 42 74 72 65 65 28 29 20 72 65 74 75 72 ockBtree() retur
1cdf0 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 ns something oth
1ce00 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f er than SQLITE_O
1ce10 4b 2e 20 6c 6f 63 6b 42 74 72 65 65 28 29 0a 20 K. lockBtree().
1ce20 20 20 20 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e ** may return
1ce30 20 53 51 4c 49 54 45 5f 4f 4b 20 62 75 74 20 6c SQLITE_OK but l
1ce40 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 eave pBt->pPage1
1ce50 20 73 65 74 20 74 6f 20 30 20 69 66 20 61 66 74 set to 0 if aft
1ce60 65 72 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e er. ** readin
1ce70 67 20 70 61 67 65 20 31 20 69 74 20 64 69 73 63 g page 1 it disc
1ce80 6f 76 65 72 73 20 74 68 61 74 20 74 68 65 20 70 overs that the p
1ce90 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 68 65 20 age-size of the
1cea0 64 61 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a database . **
1ceb0 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 42 74 file is not pBt
1cec0 2d 3e 70 61 67 65 53 69 7a 65 2e 20 49 6e 20 74 ->pageSize. In t
1ced0 68 69 73 20 63 61 73 65 20 6c 6f 63 6b 42 74 72 his case lockBtr
1cee0 65 65 28 29 20 77 69 6c 6c 20 75 70 64 61 74 65 ee() will update
1cef0 0a 20 20 20 20 2a 2a 20 70 42 74 2d 3e 70 61 67 . ** pBt->pag
1cf00 65 53 69 7a 65 20 74 6f 20 74 68 65 20 70 61 67 eSize to the pag
1cf10 65 2d 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e-size of the fi
1cf20 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 20 20 le on disk..
1cf30 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 70 42 */. while( pB
1cf40 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 26 26 20 t->pPage1==0 &&
1cf50 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
1cf60 20 6c 6f 63 6b 42 74 72 65 65 28 70 42 74 29 29 lockBtree(pBt))
1cf70 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 3d );.. if( rc=
1cf80 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 =SQLITE_OK && wr
1cf90 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66 flag ){. if
1cfa0 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 ( (pBt->btsFlags
1cfb0 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 & BTS_READ_ONLY
1cfc0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
1cfd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 rc = SQLITE_READ
1cfe0 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ONLY;. }els
1cff0 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
1d000 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 sqlite3PagerBegi
1d010 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 n(pBt->pPager,wr
1d020 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 flag>1,sqlite3Te
1d030 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 mpInMemory(p->db
1d040 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
1d050 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1d060 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1d070 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29 newDatabase(pBt)
1d080 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
1d090 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 if( rc==SQLITE_B
1d0a0 55 53 59 5f 53 4e 41 50 53 48 4f 54 20 26 26 20 USY_SNAPSHOT &&
1d0b0 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
1d0c0 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 on==TRANS_NONE )
1d0d0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 {. /* i
1d0e0 66 20 74 68 65 72 65 20 77 61 73 20 6e 6f 20 74 f there was no t
1d0f0 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 ransaction opene
1d100 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 d when this func
1d110 74 69 6f 6e 20 77 61 73 0a 20 20 20 20 20 20 20 tion was.
1d120 20 20 20 2a 2a 20 63 61 6c 6c 65 64 20 61 6e 64 ** called and
1d130 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 53 4e 41 SQLITE_BUSY_SNA
1d140 50 53 48 4f 54 20 69 73 20 72 65 74 75 72 6e 65 PSHOT is returne
1d150 64 2c 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 d, change the er
1d160 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ror. **
1d170 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 5f code to SQLITE_
1d180 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 20 20 20 BUSY. */.
1d190 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1d1a0 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 USY;. }.
1d1b0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
1d1c0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1d1d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 TE_OK ){. u
1d1e0 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
1d1f0 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 ed(pBt);. }.
1d200 20 7d 77 68 69 6c 65 28 20 28 72 63 26 30 78 46 }while( (rc&0xF
1d210 46 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 F)==SQLITE_BUSY
1d220 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 && pBt->inTransa
1d230 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e ction==TRANS_NON
1d240 45 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 62 E &&. b
1d250 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 treeInvokeBusyHa
1d260 6e 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 20 20 ndler(pBt) );.
1d270 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 73 65 sqlite3PagerRese
1d280 74 4c 6f 63 6b 54 69 6d 65 6f 75 74 28 70 42 74 tLockTimeout(pBt
1d290 2d 3e 70 50 61 67 65 72 29 3b 0a 0a 20 20 69 66 ->pPager);.. if
1d2a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1d2b0 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e ){. if( p->in
1d2c0 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e Trans==TRANS_NON
1d2d0 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e E ){. pBt->
1d2e0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a nTransaction++;.
1d2f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d300 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1d310 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 73 68 . if( p->sh
1d320 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 arable ){.
1d330 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 assert( p->loc
1d340 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26 20 70 k.pBtree==p && p
1d350 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d 3d 31 ->lock.iTable==1
1d360 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c );. p->l
1d370 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 ock.eLock = READ
1d380 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 _LOCK;. p
1d390 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d 20 70 ->lock.pNext = p
1d3a0 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 Bt->pLock;.
1d3b0 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 pBt->pLock =
1d3c0 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 &p->lock;.
1d3d0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 }.#endif. }.
1d3e0 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 p->inTrans =
1d3f0 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52 (wrflag?TRANS_WR
1d400 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b ITE:TRANS_READ);
1d410 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 . if( p->inTr
1d420 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 ans>pBt->inTrans
1d430 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
1d440 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
1d450 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 3b on = p->inTrans;
1d460 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 . }. if( w
1d470 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 4d rflag ){. M
1d480 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 20 3d emPage *pPage1 =
1d490 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 23 69 pBt->pPage1;.#i
1d4a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d4b0 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
1d4c0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 assert( !pB
1d4d0 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 t->pWriter );.
1d4e0 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 pBt->pWriter
1d4f0 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74 2d = p;. pBt-
1d500 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 >btsFlags &= ~BT
1d510 53 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 S_EXCLUSIVE;.
1d520 20 20 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 if( wrflag>1
1d530 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 ) pBt->btsFlags
1d540 7c 3d 20 42 54 53 5f 45 58 43 4c 55 53 49 56 45 |= BTS_EXCLUSIVE
1d550 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 ;.#endif..
1d560 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 73 69 7a /* If the db-siz
1d570 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 69 e header field i
1d580 73 20 69 6e 63 6f 72 72 65 63 74 20 28 61 73 20 s incorrect (as
1d590 69 74 20 6d 61 79 20 62 65 20 69 66 20 61 6e 20 it may be if an
1d5a0 6f 6c 64 0a 20 20 20 20 20 20 2a 2a 20 63 6c 69 old. ** cli
1d5b0 65 6e 74 20 68 61 73 20 62 65 65 6e 20 77 72 69 ent has been wri
1d5c0 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ting the databas
1d5d0 65 20 66 69 6c 65 29 2c 20 75 70 64 61 74 65 20 e file), update
1d5e0 69 74 20 6e 6f 77 2e 20 44 6f 69 6e 67 0a 20 20 it now. Doing.
1d5f0 20 20 20 20 2a 2a 20 74 68 69 73 20 73 6f 6f 6e ** this soon
1d600 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c er rather than l
1d610 61 74 65 72 20 6d 65 61 6e 73 20 74 68 65 20 64 ater means the d
1d620 61 74 61 62 61 73 65 20 73 69 7a 65 20 63 61 6e atabase size can
1d630 20 73 61 66 65 6c 79 20 0a 20 20 20 20 20 20 2a safely . *
1d640 2a 20 72 65 2d 72 65 61 64 20 74 68 65 20 64 61 * re-read the da
1d650 74 61 62 61 73 65 20 73 69 7a 65 20 66 72 6f 6d tabase size from
1d660 20 70 61 67 65 20 31 20 69 66 20 61 20 73 61 76 page 1 if a sav
1d670 65 70 6f 69 6e 74 20 6f 72 20 74 72 61 6e 73 61 epoint or transa
1d680 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72 ction. ** r
1d690 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 20 77 ollback occurs w
1d6a0 69 74 68 69 6e 20 74 68 65 20 74 72 61 6e 73 61 ithin the transa
1d6b0 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a ction.. */.
1d6c0 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6e if( pBt->n
1d6d0 50 61 67 65 21 3d 67 65 74 34 62 79 74 65 28 26 Page!=get4byte(&
1d6e0 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 pPage1->aData[28
1d6f0 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 ]) ){. rc
1d700 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1d710 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 rite(pPage1->pDb
1d720 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Page);. i
1d730 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1d740 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 ){. pu
1d750 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
1d760 61 44 61 74 61 5b 32 38 5d 2c 20 70 42 74 2d 3e aData[28], pBt->
1d770 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 nPage);.
1d780 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1d790 20 20 7d 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e }..trans_begun
1d7a0 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 :. if( rc==SQLI
1d7b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1d7c0 20 70 53 63 68 65 6d 61 56 65 72 73 69 6f 6e 20 pSchemaVersion
1d7d0 29 7b 0a 20 20 20 20 20 20 2a 70 53 63 68 65 6d ){. *pSchem
1d7e0 61 56 65 72 73 69 6f 6e 20 3d 20 67 65 74 34 62 aVersion = get4b
1d7f0 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
1d800 2d 3e 61 44 61 74 61 5b 34 30 5d 29 3b 0a 20 20 ->aData[40]);.
1d810 20 20 7d 0a 20 20 20 20 69 66 28 20 77 72 66 6c }. if( wrfl
1d820 61 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 ag ){. /* T
1d830 68 69 73 20 63 61 6c 6c 20 6d 61 6b 65 73 20 73 his call makes s
1d840 75 72 65 20 74 68 61 74 20 74 68 65 20 70 61 67 ure that the pag
1d850 65 72 20 68 61 73 20 74 68 65 20 63 6f 72 72 65 er has the corre
1d860 63 74 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 ct number of.
1d870 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76 65 70 ** open savep
1d880 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20 73 65 oints. If the se
1d890 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 cond parameter i
1d8a0 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 s greater than 0
1d8b0 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 74 68 and. ** th
1d8c0 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 e sub-journal is
1d8d0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 not already ope
1d8e0 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 n, then it will
1d8f0 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a be opened here..
1d900 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
1d910 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1d920 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 OpenSavepoint(pB
1d930 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 t->pPager, p->db
1d940 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 ->nSavepoint);.
1d950 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 74 72 65 }. }.. btre
1d960 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 eIntegrity(p);.
1d970 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1d980 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
1d990 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 rc;.}..#ifndef S
1d9a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1d9b0 41 43 55 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 ACUUM../*.** Set
1d9c0 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
1d9d0 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c entries for all
1d9e0 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 children of pag
1d9f0 65 20 70 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 e pPage. Also, i
1da00 66 0a 2a 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 f.** pPage conta
1da10 69 6e 73 20 63 65 6c 6c 73 20 74 68 61 74 20 70 ins cells that p
1da20 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 oint to overflow
1da30 20 70 61 67 65 73 2c 20 73 65 74 20 74 68 65 20 pages, set the
1da40 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 pointer.** map e
1da50 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20 6f ntries for the o
1da60 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 verflow pages as
1da70 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 well..*/.static
1da80 20 69 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72 int setChildPtr
1da90 6d 61 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 maps(MemPage *pP
1daa0 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 age){. int i;
1dab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dac0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
1dad0 75 6e 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a unter variable *
1dae0 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 /. int nCell;
1daf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1db00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1db10 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 of cells in pag
1db20 65 20 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 e pPage */. int
1db30 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1db40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1db50 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
1db60 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
1db70 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
1db80 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 Pgno pgno = pP
1db90 61 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 age->pgno;.. as
1dba0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1dbb0 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1dbc0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1dbd0 20 72 63 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 rc = pPage->isI
1dbe0 6e 69 74 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 nit ? SQLITE_OK
1dbf0 3a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 : btreeInitPage(
1dc00 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 pPage);. if( rc
1dc10 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
1dc20 74 75 72 6e 20 72 63 3b 0a 20 20 6e 43 65 6c 6c turn rc;. nCell
1dc30 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
1dc40 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e .. for(i=0; i<n
1dc50 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
1dc60 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 u8 *pCell = find
1dc70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a Cell(pPage, i);.
1dc80 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 . ptrmapPutOv
1dc90 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 50 61 flPtr(pPage, pPa
1dca0 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b ge, pCell, &rc);
1dcb0 0a 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 .. if( !pPage
1dcc0 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
1dcd0 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d Pgno childPgno =
1dce0 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 get4byte(pCell)
1dcf0 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 ;. ptrmapPu
1dd00 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f t(pBt, childPgno
1dd10 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
1dd20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 pgno, &rc);.
1dd30 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 }. }.. if( !pP
1dd40 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
1dd50 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 Pgno childPgno
1dd60 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
1dd70 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
1dd80 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
1dd90 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
1dda0 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 , childPgno, PTR
1ddb0 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c MAP_BTREE, pgno,
1ddc0 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 &rc);. }.. re
1ddd0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1dde0 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e 20 70 * Somewhere on p
1ddf0 50 61 67 65 20 69 73 20 61 20 70 6f 69 6e 74 65 Page is a pointe
1de00 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f 6d 2e r to page iFrom.
1de10 20 20 4d 6f 64 69 66 79 20 74 68 69 73 20 70 6f Modify this po
1de20 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 74 68 61 74 inter so.** that
1de30 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 69 54 it points to iT
1de40 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65 54 79 o. Parameter eTy
1de50 70 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 pe describes the
1de60 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74 65 72 type of pointer
1de70 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f 64 69 66 69 to.** be modifi
1de80 65 64 2c 20 61 73 20 20 66 6f 6c 6c 6f 77 73 3a ed, as follows:
1de90 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 .**.** PTRMAP_BT
1dea0 52 45 45 3a 20 20 20 20 20 70 50 61 67 65 20 69 REE: pPage i
1deb0 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2e 20 s a btree-page.
1dec0 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e The pointer poin
1ded0 74 73 20 61 74 20 61 20 63 68 69 6c 64 20 0a 2a ts at a child .*
1dee0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1def0 20 20 20 20 70 61 67 65 20 6f 66 20 70 50 61 67 page of pPag
1df00 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f e..**.** PTRMAP_
1df10 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61 67 65 OVERFLOW1: pPage
1df20 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 is a btree-page
1df30 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
1df40 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65 72 66 ints at an overf
1df50 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 low.**
1df60 20 20 20 20 20 20 20 20 20 70 61 67 65 20 70 6f page po
1df70 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e 65 20 inted to by one
1df80 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 of the cells on
1df90 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 pPage..**.** PTR
1dfa0 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 70 MAP_OVERFLOW2: p
1dfb0 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66 Page is an overf
1dfc0 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20 70 6f low-page. The po
1dfd0 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 inter points at
1dfe0 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 the next.**
1dff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 ov
1e000 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 erflow page in t
1e010 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 he list..*/.stat
1e020 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50 61 67 ic int modifyPag
1e030 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61 67 65 ePointer(MemPage
1e040 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20 69 46 *pPage, Pgno iF
1e050 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c 20 75 rom, Pgno iTo, u
1e060 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73 73 65 8 eType){. asse
1e070 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1e080 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
1e090 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
1e0a0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
1e0b0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
1e0c0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
1e0d0 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 ;. if( eType==P
1e0e0 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 TRMAP_OVERFLOW2
1e0f0 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 6f ){. /* The po
1e100 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79 73 20 inter is always
1e110 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 the first 4 byte
1e120 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e s of the page in
1e130 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a this case. */.
1e140 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65 if( get4byte
1e150 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 21 3d (pPage->aData)!=
1e160 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 72 iFrom ){. r
1e170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1e180 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 RUPT_PAGE(pPage)
1e190 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 ;. }. put4
1e1a0 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
1e1b0 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 a, iTo);. }else
1e1c0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
1e1d0 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 20 20 int nCell;.
1e1e0 69 6e 74 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 int rc;.. rc
1e1f0 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 = pPage->isInit
1e200 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 62 74 ? SQLITE_OK : bt
1e210 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
1e220 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
1e230 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1e240 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e nCell = pPage->n
1e250 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 Cell;.. for(i
1e260 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b =0; i<nCell; i++
1e270 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 ){. u8 *pCe
1e280 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
1e290 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 age, i);. i
1e2a0 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
1e2b0 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 _OVERFLOW1 ){.
1e2c0 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 CellInfo i
1e2d0 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 70 50 61 nfo;. pPa
1e2e0 67 65 2d 3e 78 50 61 72 73 65 43 65 6c 6c 28 70 ge->xParseCell(p
1e2f0 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
1e300 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 fo);. if(
1e310 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3c 69 6e 66 info.nLocal<inf
1e320 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 7b 0a 20 20 o.nPayload ){.
1e330 20 20 20 20 20 20 20 20 69 66 28 20 70 43 65 6c if( pCel
1e340 6c 2b 69 6e 66 6f 2e 6e 53 69 7a 65 20 3e 20 70 l+info.nSize > p
1e350 50 61 67 65 2d 3e 61 44 61 74 61 2b 70 50 61 67 Page->aData+pPag
1e360 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
1e370 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ze ){.
1e380 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1e390 43 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 CORRUPT_PAGE(pPa
1e3a0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ge);. }
1e3b0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
1e3c0 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28 70 From==get4byte(p
1e3d0 43 65 6c 6c 2b 69 6e 66 6f 2e 6e 53 69 7a 65 2d Cell+info.nSize-
1e3e0 34 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 4) ){.
1e3f0 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c put4byte(pCell
1e400 2b 69 6e 66 6f 2e 6e 53 69 7a 65 2d 34 2c 20 69 +info.nSize-4, i
1e410 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 To);.
1e420 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1e430 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1e440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e450 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65 28 if( get4byte(
1e460 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b pCell)==iFrom ){
1e470 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
1e480 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29 3b yte(pCell, iTo);
1e490 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
1e4a0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1e4b0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1e4c0 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b if( i==nCell ){
1e4d0 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 . if( eType
1e4e0 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c !=PTRMAP_BTREE |
1e4f0 7c 20 0a 20 20 20 20 20 20 20 20 20 20 67 65 74 | . get
1e500 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
1e510 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
1e520 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 fset+8])!=iFrom
1e530 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1e540 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1e550 5f 50 41 47 45 28 70 50 61 67 65 29 3b 0a 20 20 _PAGE(pPage);.
1e560 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 }. put4
1e570 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
1e580 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
1e590 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 set+8], iTo);.
1e5a0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1e5b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
1e5c0 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f /*.** Move the o
1e5d0 70 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 pen database pag
1e5e0 65 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 e pDbPage to loc
1e5f0 61 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 ation iFreePage
1e600 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 in the .** datab
1e610 61 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 ase. The pDbPage
1e620 20 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 reference remai
1e630 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 ns valid..**.**
1e640 54 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 The isCommit fla
1e650 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 g indicates that
1e660 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
1e670 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 d to remember th
1e680 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 at.** the journa
1e690 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 l needs to be sy
1e6a0 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 nc()ed before da
1e6b0 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 tabase page pDbP
1e6c0 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 age->pgno .** ca
1e6d0 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e n be written to.
1e6e0 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 The caller has
1e6f0 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 already promised
1e700 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f not to write to
1e710 20 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a that.** page..*
1e720 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6c /.static int rel
1e730 6f 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 53 ocatePage(. BtS
1e740 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
1e750 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 2a /* Btree *
1e760 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 /. MemPage *pDb
1e770 50 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 Page, /*
1e780 4f 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 Open page to mov
1e790 65 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c e */. u8 eType,
1e7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e7b0 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 /* Pointer map '
1e7c0 74 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 20 type' entry for
1e7d0 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e pDbPage */. Pgn
1e7e0 6f 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 20 o iPtrPage,
1e7f0 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1e800 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 map 'page-no' e
1e810 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 ntry for pDbPage
1e820 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 */. Pgno iFree
1e830 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f Page, /
1e840 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 * The location t
1e850 6f 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 o move pDbPage t
1e860 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d o */. int isCom
1e870 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 mit
1e880 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 /* isCommit flag
1e890 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 passed to sqlit
1e8a0 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 e3PagerMovepage
1e8b0 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 */.){. MemPage
1e8c0 2a 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 *pPtrPage; /*
1e8d0 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f The page that co
1e8e0 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 ntains a pointer
1e8f0 20 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 to pDbPage */.
1e900 20 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 Pgno iDbPage =
1e910 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 pDbPage->pgno;.
1e920 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
1e930 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pBt->pPager;.
1e940 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
1e950 74 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 t( eType==PTRMAP
1e960 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 _OVERFLOW2 || eT
1e970 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
1e980 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 FLOW1 || .
1e990 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 eType==PTRMAP_BT
1e9a0 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 REE || eType==PT
1e9b0 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b RMAP_ROOTPAGE );
1e9c0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1e9d0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
1e9e0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
1e9f0 73 73 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e ssert( pDbPage->
1ea00 70 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20 69 66 pBt==pBt );. if
1ea10 28 20 69 44 62 50 61 67 65 3c 33 20 29 20 72 65 ( iDbPage<3 ) re
1ea20 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1ea30 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20 UPT_BKPT;.. /*
1ea40 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 67 Move page iDbPag
1ea50 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 65 e from its curre
1ea60 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 nt location to p
1ea70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 65 age number iFree
1ea80 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 Page */. TRACE(
1ea90 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f ("AUTOVACUUM: Mo
1eaa0 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 20 ving %d to free
1eab0 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61 67 page %d (ptr pag
1eac0 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e 22 e %d type %d)\n"
1ead0 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 65 , . iDbPage
1eae0 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 74 , iFreePage, iPt
1eaf0 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b 0a rPage, eType));.
1eb00 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1eb10 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 67 gerMovepage(pPag
1eb20 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 62 er, pDbPage->pDb
1eb30 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c Page, iFreePage,
1eb40 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 isCommit);. if
1eb50 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1eb60 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1eb70 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d ;. }. pDbPage-
1eb80 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 >pgno = iFreePag
1eb90 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 e;.. /* If pDbP
1eba0 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d age was a btree-
1ebb0 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 page, then it ma
1ebc0 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67 y have child pag
1ebd0 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a es and/or cells.
1ebe0 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 ** that point
1ebf0 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
1ec00 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d s. The pointer m
1ec10 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 ap entries for a
1ec20 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 ll these. ** pa
1ec30 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63 ges need to be c
1ec40 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a hanged.. **. *
1ec50 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20 * If pDbPage is
1ec60 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
1ec70 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 , then the first
1ec80 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f 4 bytes may sto
1ec90 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 re a. ** pointe
1eca0 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e r to a subsequen
1ecb0 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e t overflow page.
1ecc0 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
1ecd0 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 case, then. **
1ece0 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
1ecf0 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 needs to be upda
1ed00 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73 ted for the subs
1ed10 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 equent overflow
1ed20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 page.. */. if(
1ed30 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 eType==PTRMAP_B
1ed40 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 TREE || eType==P
1ed50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
1ed60 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68 {. rc = setCh
1ed70 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61 ildPtrmaps(pDbPa
1ed80 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
1ed90 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1eda0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1edb0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1edc0 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 Pgno nextOvfl
1edd0 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61 = get4byte(pDbPa
1ede0 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 ge->aData);.
1edf0 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 if( nextOvfl!=0
1ee00 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 ){. ptrmapP
1ee10 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c ut(pBt, nextOvfl
1ee20 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
1ee30 57 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20 26 W2, iFreePage, &
1ee40 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 rc);. if( r
1ee50 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1ee60 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
1ee70 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
1ee80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 . }.. /* Fix t
1ee90 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e he database poin
1eea0 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72 ter on page iPtr
1eeb0 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65 Page that pointe
1eec0 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a d at iDbPage so.
1eed0 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 ** that it poi
1eee0 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65 nts at iFreePage
1eef0 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70 . Also fix the p
1ef00 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
1ef10 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 for. ** iPtrPa
1ef20 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 ge.. */. if( e
1ef30 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f Type!=PTRMAP_ROO
1ef40 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 TPAGE ){. rc
1ef50 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
1ef60 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 70 Bt, iPtrPage, &p
1ef70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 PtrPage, 0);.
1ef80 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1ef90 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
1efa0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
1efb0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1efc0 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 65 erWrite(pPtrPage
1efd0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1efe0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1eff0 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 K ){. relea
1f000 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29 sePage(pPtrPage)
1f010 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
1f020 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
1f030 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e = modifyPagePoin
1f040 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 44 ter(pPtrPage, iD
1f050 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
1f060 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 65 , eType);. re
1f070 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 leasePage(pPtrPa
1f080 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ge);. if( rc=
1f090 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1f0a0 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
1f0b0 74 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 t, iFreePage, eT
1f0c0 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 26 ype, iPtrPage, &
1f0d0 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rc);. }. }.
1f0e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f0f0 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
1f100 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 62 ation required b
1f110 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 y incrVacuumStep
1f120 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e (). */.static in
1f130 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 t allocateBtreeP
1f140 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c 20 age(BtShared *,
1f150 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e 6f MemPage **, Pgno
1f160 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a 0a *, Pgno, u8);..
1f170 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 /*.** Perform a
1f180 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 61 single step of a
1f190 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 n incremental-va
1f1a0 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 73 cuum. If success
1f1b0 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 ful, return.** S
1f1c0 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 QLITE_OK. If the
1f1d0 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 6f re is no work to
1f1e0 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 6f do (and therefo
1f1f0 72 65 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 0a re no point in .
1f200 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 ** calling this
1f210 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c function again),
1f220 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
1f230 4f 4e 45 2e 20 4f 72 2c 20 69 66 20 61 6e 20 65 ONE. Or, if an e
1f240 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c rror .** occurs,
1f250 20 72 65 74 75 72 6e 20 73 6f 6d 65 20 6f 74 68 return some oth
1f260 65 72 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a er error code..*
1f270 2a 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 *.** More specif
1f280 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 66 75 6e ically, this fun
1f290 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 ction attempts t
1f2a0 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65 20 74 68 o re-organize th
1f2b0 65 20 64 61 74 61 62 61 73 65 20 73 6f 20 0a 2a e database so .*
1f2c0 2a 20 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 * that the last
1f2d0 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 page of the file
1f2e0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 currently in us
1f2f0 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 e is no longer i
1f300 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 n use..**.** Par
1f310 61 6d 65 74 65 72 20 6e 46 69 6e 20 69 73 20 74 ameter nFin is t
1f320 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
1f330 65 73 20 74 68 61 74 20 74 68 69 73 20 64 61 74 es that this dat
1f340 61 62 61 73 65 20 77 6f 75 6c 64 20 63 6f 6e 74 abase would cont
1f350 61 69 6e 0a 2a 2a 20 77 65 72 65 20 74 68 69 73 ain.** were this
1f360 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 function called
1f370 20 75 6e 74 69 6c 20 69 74 20 72 65 74 75 72 6e until it return
1f380 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a s SQLITE_DONE..*
1f390 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 43 6f 6d *.** If the bCom
1f3a0 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 mit parameter is
1f3b0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20 non-zero, this
1f3c0 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 function assumes
1f3d0 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 63 61 that the .** ca
1f3e0 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 ller will keep c
1f3f0 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 alling incrVacuu
1f400 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 20 69 74 mStep() until it
1f410 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1f420 44 4f 4e 45 20 0a 2a 2a 20 6f 72 20 61 6e 20 65 DONE .** or an e
1f430 72 72 6f 72 2e 20 62 43 6f 6d 6d 69 74 20 69 73 rror. bCommit is
1f440 20 70 61 73 73 65 64 20 74 72 75 65 20 66 6f 72 passed true for
1f450 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 2d an auto-vacuum-
1f460 6f 6e 2d 63 6f 6d 6d 69 74 20 0a 2a 2a 20 6f 70 on-commit .** op
1f470 65 72 61 74 69 6f 6e 2c 20 6f 72 20 66 61 6c 73 eration, or fals
1f480 65 20 66 6f 72 20 61 6e 20 69 6e 63 72 65 6d 65 e for an increme
1f490 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2f 0a ntal vacuum..*/.
1f4a0 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 72 56 static int incrV
1f4b0 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 61 72 acuumStep(BtShar
1f4c0 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 ed *pBt, Pgno nF
1f4d0 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 in, Pgno iLastPg
1f4e0 2c 20 69 6e 74 20 62 43 6f 6d 6d 69 74 29 7b 0a , int bCommit){.
1f4f0 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74 Pgno nFreeList
1f500 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1f510 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
1f520 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65 till on the free
1f530 2d 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 72 -list */. int r
1f540 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 c;.. assert( sq
1f550 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1f560 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1f570 20 20 61 73 73 65 72 74 28 20 69 4c 61 73 74 50 assert( iLastP
1f580 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 g>nFin );.. if(
1f590 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 !PTRMAP_ISPAGE(
1f5a0 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 pBt, iLastPg) &&
1f5b0 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e iLastPg!=PENDIN
1f5c0 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
1f5d0 20 29 7b 0a 20 20 20 20 75 38 20 65 54 79 70 65 ){. u8 eType
1f5e0 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 ;. Pgno iPtrP
1f5f0 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c age;.. nFreeL
1f600 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 ist = get4byte(&
1f610 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
1f620 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 ta[36]);. if(
1f630 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b nFreeList==0 ){
1f640 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1f650 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
1f660 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 .. rc = ptrma
1f670 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 pGet(pBt, iLastP
1f680 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 g, &eType, &iPtr
1f690 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
1f6a0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1f6b0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1f6c0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 . }. if( e
1f6d0 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f Type==PTRMAP_ROO
1f6e0 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 TPAGE ){. r
1f6f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1f700 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
1f710 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 3d .. if( eType=
1f720 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 =PTRMAP_FREEPAGE
1f730 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 62 43 ){. if( bC
1f740 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ommit==0 ){.
1f750 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 /* Remove th
1f760 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 e page from the
1f770 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e files free-list.
1f780 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 This is not req
1f790 75 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a uired. **
1f7a0 20 69 66 20 62 43 6f 6d 6d 69 74 20 69 73 20 6e if bCommit is n
1f7b0 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 on-zero. In that
1f7c0 20 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d case, the free-
1f7d0 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 list will be.
1f7e0 20 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 ** truncate
1f7f0 64 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 d to zero after
1f800 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 this function re
1f810 74 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 turns, so it doe
1f820 73 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a sn't . **
1f830 20 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74 matter if it st
1f840 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d ill contains som
1f850 65 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65 e garbage entrie
1f860 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 s.. */.
1f870 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 Pgno iFree
1f880 50 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 Pg;. MemP
1f890 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 age *pFreePg;.
1f8a0 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 rc = alloc
1f8b0 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
1f8c0 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 , &pFreePg, &iFr
1f8d0 65 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 42 eePg, iLastPg, B
1f8e0 54 41 4c 4c 4f 43 5f 45 58 41 43 54 29 3b 0a 20 TALLOC_EXACT);.
1f8f0 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1f900 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1f910 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1f920 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1f930 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 assert( iFree
1f940 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20 Pg==iLastPg );.
1f950 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1f960 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 ge(pFreePg);.
1f970 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
1f980 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 {. Pgno iFr
1f990 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 eePg;
1f9a0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72 /* Index of fr
1f9b0 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 ee page to move
1f9c0 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20 pLastPg to */.
1f9d0 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61 MemPage *pLa
1f9e0 73 74 50 67 3b 0a 20 20 20 20 20 20 75 38 20 65 stPg;. u8 e
1f9f0 4d 6f 64 65 20 3d 20 42 54 41 4c 4c 4f 43 5f 41 Mode = BTALLOC_A
1fa00 4e 59 3b 20 20 20 2f 2a 20 4d 6f 64 65 20 70 61 NY; /* Mode pa
1fa10 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6c 6c 6f rameter for allo
1fa20 63 61 74 65 42 74 72 65 65 50 61 67 65 28 29 20 cateBtreePage()
1fa30 2a 2f 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 4e */. Pgno iN
1fa40 65 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ear = 0;
1fa50 20 20 20 2f 2a 20 6e 65 61 72 62 79 20 70 61 72 /* nearby par
1fa60 61 6d 65 74 65 72 20 66 6f 72 20 61 6c 6c 6f 63 ameter for alloc
1fa70 61 74 65 42 74 72 65 65 50 61 67 65 28 29 20 2a ateBtreePage() *
1fa80 2f 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 /.. rc = bt
1fa90 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1faa0 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 iLastPg, &pLastP
1fab0 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 g, 0);. if(
1fac0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1fad0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
1fae0 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
1faf0 20 20 20 20 2f 2a 20 49 66 20 62 43 6f 6d 6d 69 /* If bCommi
1fb00 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 t is zero, this
1fb10 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 6c loop runs exactl
1fb20 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 20 y once and page
1fb30 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a 2a pLastPg. **
1fb40 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74 68 is swapped with
1fb50 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 the first free
1fb60 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 page pulled off
1fb70 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 the free list..
1fb80 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
1fb90 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 On the other ha
1fba0 6e 64 2c 20 69 66 20 62 43 6f 6d 6d 69 74 20 69 nd, if bCommit i
1fbb0 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a s greater than z
1fbc0 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 ero, then keep.
1fbd0 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 ** looping
1fbe0 75 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 until a free-pag
1fbf0 65 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e e located within
1fc00 20 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 the first nFin
1fc10 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f pages. ** o
1fc20 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f f the file is fo
1fc30 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 und.. */.
1fc40 20 20 20 20 69 66 28 20 62 43 6f 6d 6d 69 74 3d if( bCommit=
1fc50 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65 4d =0 ){. eM
1fc60 6f 64 65 20 3d 20 42 54 41 4c 4c 4f 43 5f 4c 45 ode = BTALLOC_LE
1fc70 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 61 72 20 ;. iNear
1fc80 3d 20 6e 46 69 6e 3b 0a 20 20 20 20 20 20 7d 0a = nFin;. }.
1fc90 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 do {.
1fca0 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 MemPage *pFre
1fcb0 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ePg;. rc
1fcc0 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
1fcd0 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 age(pBt, &pFreeP
1fce0 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4e 65 g, &iFreePg, iNe
1fcf0 61 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 ar, eMode);.
1fd00 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1fd10 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1fd20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1fd30 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 LastPg);.
1fd40 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1fd50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fd60 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 releasePage(pFre
1fd70 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 ePg);. }whi
1fd80 6c 65 28 20 62 43 6f 6d 6d 69 74 20 26 26 20 69 le( bCommit && i
1fd90 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 FreePg>nFin );.
1fda0 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 assert( iFr
1fdb0 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a eePg<iLastPg );.
1fdc0 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 . rc
1fdd0 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 = relocatePage(p
1fde0 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 79 Bt, pLastPg, eTy
1fdf0 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 46 pe, iPtrPage, iF
1fe00 72 65 65 50 67 2c 20 62 43 6f 6d 6d 69 74 29 3b reePg, bCommit);
1fe10 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1fe20 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 ge(pLastPg);.
1fe30 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1fe40 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1fe50 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1fe60 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
1fe70 69 66 28 20 62 43 6f 6d 6d 69 74 3d 3d 30 20 29 if( bCommit==0 )
1fe80 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 {. do {.
1fe90 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 iLastPg--;.
1fea0 7d 77 68 69 6c 65 28 20 69 4c 61 73 74 50 67 3d }while( iLastPg=
1feb0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
1fec0 47 45 28 70 42 74 29 20 7c 7c 20 50 54 52 4d 41 GE(pBt) || PTRMA
1fed0 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c P_ISPAGE(pBt, iL
1fee0 61 73 74 50 67 29 20 29 3b 0a 20 20 20 20 70 42 astPg) );. pB
1fef0 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 20 3d t->bDoTruncate =
1ff00 20 31 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 50 61 1;. pBt->nPa
1ff10 67 65 20 3d 20 69 4c 61 73 74 50 67 3b 0a 20 20 ge = iLastPg;.
1ff20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
1ff30 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
1ff40 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 6e he database open
1ff50 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
1ff60 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 61 argument is an a
1ff70 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
1ff80 61 73 65 0a 2a 2a 20 6e 4f 72 69 67 20 70 61 67 ase.** nOrig pag
1ff90 65 73 20 69 6e 20 73 69 7a 65 20 63 6f 6e 74 61 es in size conta
1ffa0 69 6e 69 6e 67 20 6e 46 72 65 65 20 66 72 65 65 ining nFree free
1ffb0 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 74 pages. Return t
1ffc0 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a 20 he expected .**
1ffd0 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
1ffe0 62 61 73 65 20 69 6e 20 70 61 67 65 73 20 66 6f base in pages fo
1fff0 6c 6c 6f 77 69 6e 67 20 61 6e 20 61 75 74 6f 2d llowing an auto-
20000 76 61 63 75 75 6d 20 6f 70 65 72 61 74 69 6f 6e vacuum operation
20010 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f ..*/.static Pgno
20020 20 66 69 6e 61 6c 44 62 53 69 7a 65 28 42 74 53 finalDbSize(BtS
20030 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f hared *pBt, Pgno
20040 20 6e 4f 72 69 67 2c 20 50 67 6e 6f 20 6e 46 72 nOrig, Pgno nFr
20050 65 65 29 7b 0a 20 20 69 6e 74 20 6e 45 6e 74 72 ee){. int nEntr
20060 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
20070 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
20080 20 6f 66 20 65 6e 74 72 69 65 73 20 6f 6e 20 6f of entries on o
20090 6e 65 20 70 74 72 6d 61 70 20 70 61 67 65 20 2a ne ptrmap page *
200a0 2f 0a 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 /. Pgno nPtrmap
200b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
200c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
200d0 20 50 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f PtrMap pages to
200e0 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 50 be freed */. P
200f0 67 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 gno nFin;
20100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20110 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a * Return value *
20120 2f 0a 0a 20 20 6e 45 6e 74 72 79 20 3d 20 70 42 /.. nEntry = pB
20130 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 3b t->usableSize/5;
20140 0a 20 20 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 . nPtrmap = (nF
20150 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 ree-nOrig+PTRMAP
20160 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 _PAGENO(pBt, nOr
20170 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 ig)+nEntry)/nEnt
20180 72 79 3b 0a 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 ry;. nFin = nOr
20190 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 ig - nFree - nPt
201a0 72 6d 61 70 3b 0a 20 20 69 66 28 20 6e 4f 72 69 rmap;. if( nOri
201b0 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 g>PENDING_BYTE_P
201c0 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 6e AGE(pBt) && nFin
201d0 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 <PENDING_BYTE_PA
201e0 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 6e GE(pBt) ){. n
201f0 46 69 6e 2d 2d 3b 0a 20 20 7d 0a 20 20 77 68 69 Fin--;. }. whi
20200 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 le( PTRMAP_ISPAG
20210 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 E(pBt, nFin) ||
20220 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 nFin==PENDING_BY
20230 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a TE_PAGE(pBt) ){.
20240 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 7d 0a nFin--;. }.
20250 0a 20 20 72 65 74 75 72 6e 20 6e 46 69 6e 3b 0a . return nFin;.
20260 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 }../*.** A write
20270 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 -transaction mus
20280 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f t be opened befo
20290 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 re calling this
202a0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 function..** It
202b0 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c performs a singl
202c0 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 e unit of work t
202d0 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d owards an increm
202e0 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a ental vacuum..**
202f0 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65 .** If the incre
20300 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73 mental vacuum is
20310 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20 finished after
20320 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 this function ha
20330 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 s run,.** SQLITE
20340 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 _DONE is returne
20350 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 d. If it is not
20360 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f finished, but no
20370 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c error occurred,
20380 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
20390 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
203a0 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 wise an SQLite e
203b0 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 69 rror code. .*/.i
203c0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
203d0 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 ncrVacuum(Btree
203e0 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 *p){. int rc;.
203f0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
20400 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 p->pBt;.. sqli
20410 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
20420 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
20430 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
20440 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 TRANS_WRITE && p
20450 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
20460 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 _WRITE );. if(
20470 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d !pBt->autoVacuum
20480 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
20490 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 ITE_DONE;. }els
204a0 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 e{. Pgno nOri
204b0 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f 75 g = btreePagecou
204c0 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 50 67 6e nt(pBt);. Pgn
204d0 6f 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 o nFree = get4by
204e0 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
204f0 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 >aData[36]);.
20500 20 50 67 6e 6f 20 6e 46 69 6e 20 3d 20 66 69 6e Pgno nFin = fin
20510 61 6c 44 62 53 69 7a 65 28 70 42 74 2c 20 6e 4f alDbSize(pBt, nO
20520 72 69 67 2c 20 6e 46 72 65 65 29 3b 0a 0a 20 20 rig, nFree);..
20530 20 20 69 66 28 20 6e 4f 72 69 67 3c 6e 46 69 6e if( nOrig<nFin
20540 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
20550 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
20560 50 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 PT;. }else if
20570 28 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 20 20 ( nFree>0 ){.
20580 20 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 rc = saveAllC
20590 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 ursors(pBt, 0, 0
205a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
205b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
205c0 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 invalidate
205d0 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
205e0 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 72 (pBt);. r
205f0 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 c = incrVacuumSt
20600 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 6e 4f ep(pBt, nFin, nO
20610 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d rig, 0);. }
20620 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
20630 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
20640 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
20650 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
20660 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
20670 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 ;. put4by
20680 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
20690 3e 61 44 61 74 61 5b 32 38 5d 2c 20 70 42 74 2d >aData[28], pBt-
206a0 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d >nPage);. }
206b0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
206c0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f rc = SQLITE_DO
206d0 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 NE;. }. }.
206e0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
206f0 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
20700 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
20710 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
20720 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c 69 ed prior to sqli
20730 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 77 te3PagerCommit w
20740 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f hen a transactio
20750 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 65 n.** is committe
20760 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 d for an auto-va
20770 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a cuum database..*
20780 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f *.** If SQLITE_O
20790 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 K is returned, t
207a0 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20 hen *pnTrunc is
207b0 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
207c0 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68 r of pages.** th
207d0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
207e0 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 should be trunca
207f0 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68 ted to during th
20800 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 e commit process
20810 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64 . .** i.e. the d
20820 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e atabase has been
20830 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20 reorganized so
20840 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69 that only the fi
20850 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 rst *pnTrunc.**
20860 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65 pages are in use
20870 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
20880 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 autoVacuumCommit
20890 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
208a0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
208b0 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a TE_OK;. Pager *
208c0 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 pPager = pBt->pP
208d0 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 ager;. VVA_ONLY
208e0 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c ( int nRef = sql
208f0 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
20900 74 28 70 50 61 67 65 72 29 3b 20 29 0a 0a 20 20 t(pPager); )..
20910 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
20920 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
20930 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 mutex) );. inva
20940 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f lidateAllOverflo
20950 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 wCache(pBt);. a
20960 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 ssert(pBt->autoV
20970 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 acuum);. if( !p
20980 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 Bt->incrVacuum )
20990 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 3b {. Pgno nFin;
209a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
209b0 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 er of pages in d
209c0 61 74 61 62 61 73 65 20 61 66 74 65 72 20 61 75 atabase after au
209d0 74 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a 20 tovacuuming */.
209e0 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 20 Pgno nFree;
209f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
20a00 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 of pages on the
20a10 66 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 6c freelist initial
20a20 6c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 ly */. Pgno i
20a30 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 Free; /*
20a40 54 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 6f The next page to
20a50 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 be freed */.
20a60 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20 Pgno nOrig;
20a70 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
20a80 73 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65 65 size before free
20a90 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e 4f 72 69 ing */.. nOri
20aa0 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f 75 g = btreePagecou
20ab0 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 69 66 28 nt(pBt);. if(
20ac0 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
20ad0 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e 4f Bt, nOrig) || nO
20ae0 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 rig==PENDING_BYT
20af0 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
20b00 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f /* It is no
20b10 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 72 t possible to cr
20b20 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65 20 eate a database
20b30 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 69 for which the fi
20b40 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 2a nal page. *
20b50 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70 6f * is either a po
20b60 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 6f inter-map page o
20b70 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 79 r the pending-by
20b80 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 0a te page. If one.
20b90 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 6f ** is enco
20ba0 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 6e untered, this in
20bb0 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 69 dicates corrupti
20bc0 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 on.. */.
20bd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
20be0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
20bf0 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 20 }.. nFree
20c00 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d = get4byte(&pBt-
20c10 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
20c20 36 5d 29 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6]);. nFin =
20c30 66 69 6e 61 6c 44 62 53 69 7a 65 28 70 42 74 2c finalDbSize(pBt,
20c40 20 6e 4f 72 69 67 2c 20 6e 46 72 65 65 29 3b 0a nOrig, nFree);.
20c50 20 20 20 20 69 66 28 20 6e 46 69 6e 3e 6e 4f 72 if( nFin>nOr
20c60 69 67 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 ig ) return SQLI
20c70 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
20c80 0a 20 20 20 20 69 66 28 20 6e 46 69 6e 3c 6e 4f . if( nFin<nO
20c90 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rig ){. rc
20ca0 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 = saveAllCursors
20cb0 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 (pBt, 0, 0);.
20cc0 20 7d 0a 20 20 20 20 66 6f 72 28 69 46 72 65 65 }. for(iFree
20cd0 3d 6e 4f 72 69 67 3b 20 69 46 72 65 65 3e 6e 46 =nOrig; iFree>nF
20ce0 69 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 in && rc==SQLITE
20cf0 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d 29 7b 0a 20 _OK; iFree--){.
20d00 20 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 rc = incrVa
20d10 63 75 75 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 cuumStep(pBt, nF
20d20 69 6e 2c 20 69 46 72 65 65 2c 20 31 29 3b 0a 20 in, iFree, 1);.
20d30 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72 63 }. if( (rc
20d40 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c 7c ==SQLITE_DONE ||
20d50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 rc==SQLITE_OK)
20d60 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 20 && nFree>0 ){.
20d70 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
20d80 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
20d90 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
20da0 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
20db0 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
20dc0 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 Data[32], 0);.
20dd0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 42 put4byte(&pB
20de0 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
20df0 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 [36], 0);.
20e00 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 put4byte(&pBt->p
20e10 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d Page1->aData[28]
20e20 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 20 20 70 , nFin);. p
20e30 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 20 Bt->bDoTruncate
20e40 3d 20 31 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e = 1;. pBt->
20e50 6e 50 61 67 65 20 3d 20 6e 46 69 6e 3b 0a 20 20 nPage = nFin;.
20e60 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
20e70 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
20e80 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 sqlite3PagerR
20e90 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
20ea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 . }. }.. as
20eb0 73 65 72 74 28 20 6e 52 65 66 3e 3d 73 71 6c 69 sert( nRef>=sqli
20ec0 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
20ed0 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 (pPager) );. re
20ee0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 turn rc;.}..#els
20ef0 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 e /* ifndef SQLI
20f00 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
20f10 55 4d 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 UM */.# define s
20f20 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 78 etChildPtrmaps(x
20f30 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 ) SQLITE_OK.#end
20f40 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 if../*.** This r
20f50 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 outine does the
20f60 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 20 61 first phase of a
20f70 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 two-phase commi
20f80 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 t. This routine
20f90 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 6f 6c .** causes a rol
20fa0 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f lback journal to
20fb0 20 62 65 20 63 72 65 61 74 65 64 20 28 69 66 20 be created (if
20fc0 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 it does not alre
20fd0 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e ady exist).** an
20fe0 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 d populated with
20ff0 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 enough informat
21000 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 20 61 ion so that if a
21010 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 power loss occu
21020 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 rs.** the databa
21030 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 se can be restor
21040 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e ed to its origin
21050 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c 61 79 al state by play
21060 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 ing back.** the
21070 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 journal. Then t
21080 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
21090 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 he journal are f
210a0 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a lushed out to.**
210b0 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 74 65 the disk. Afte
210c0 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 r the journal is
210d0 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 safely on oxide
210e0 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 74 6f , the changes to
210f0 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
21100 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74 are written int
21110 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
21120 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 ile and flushed
21130 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 to oxide..** At
21140 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 the end of this
21150 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 call, the rollba
21160 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c ck journal still
21170 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a exists on the.*
21180 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 61 72 * disk and we ar
21190 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 e still holding
211a0 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 all locks, so th
211b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 e transaction ha
211c0 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 s not.** committ
211d0 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 ed. See sqlite3
211e0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
211f0 54 77 6f 28 29 20 66 6f 72 20 74 68 65 20 73 65 Two() for the se
21200 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68 cond phase of th
21210 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 e.** commit proc
21220 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ess..**.** This
21230 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 call is a no-op
21240 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e if no write-tran
21250 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 saction is curre
21260 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70 ntly active on p
21270 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 Bt..**.** Otherw
21280 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61 ise, sync the da
21290 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 tabase file for
212a0 74 68 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a the btree pBt. z
212b0 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f Master points to
212c0 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 .** the name of
212d0 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
212e0 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c file that shoul
212f0 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 d be written int
21300 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 o the.** individ
21310 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ual journal file
21320 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e , or is NULL, in
21330 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 dicating no mast
21340 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
21350 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61 .** (single data
21360 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
21370 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 )..**.** When th
21380 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 is is called, th
21390 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
213a0 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
213b0 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 have been.** cre
213c0 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 ated, populated
213d0 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 with this journa
213e0 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 l pointer and sy
213f0 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a nced to disk..**
21400 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73 .** Once this is
21410 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74 routine has ret
21420 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 urned, the only
21430 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 20 74 thing required t
21440 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 o commit.** the
21450 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
21460 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 n for this datab
21470 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64 ase file is to d
21480 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
21490 6c 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 l..*/.int sqlite
214a0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
214b0 65 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 63 eOne(Btree *p, c
214c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 onst char *zMast
214d0 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
214e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
214f0 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
21500 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 NS_WRITE ){.
21510 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
21520 70 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 p->pBt;. sqli
21530 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
21540 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
21550 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
21560 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 . if( pBt->au
21570 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
21580 20 20 72 63 20 3d 20 61 75 74 6f 56 61 63 75 75 rc = autoVacuu
21590 6d 43 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 mCommit(pBt);.
215a0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
215b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
215c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
215d0 76 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 ve(p);. r
215e0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
215f0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
21600 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 pBt->bDoTruncate
21610 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
21620 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
21630 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c age(pBt->pPager,
21640 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 pBt->nPage);.
21650 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 }.#endif. r
21660 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
21670 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
21680 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 Bt->pPager, zMas
21690 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c ter, 0);. sql
216a0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
216b0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
216c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
216d0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
216e0 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20 42 lled from both B
216f0 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
21700 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52 6f wo() and BtreeRo
21710 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20 74 llback().** at t
21720 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 he conclusion of
21730 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a a transaction..
21740 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 */.static void b
21750 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 treeEndTransacti
21760 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 on(Btree *p){.
21770 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
21780 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 p->pBt;. sqlite
21790 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 3 *db = p->db;.
217a0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
217b0 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
217c0 70 29 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 p) );..#ifndef S
217d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
217e0 41 43 55 55 4d 0a 20 20 70 42 74 2d 3e 62 44 6f ACUUM. pBt->bDo
217f0 54 72 75 6e 63 61 74 65 20 3d 20 30 3b 0a 23 65 Truncate = 0;.#e
21800 6e 64 69 66 0a 20 20 69 66 28 20 70 2d 3e 69 6e ndif. if( p->in
21810 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
21820 20 26 26 20 64 62 2d 3e 6e 56 64 62 65 52 65 61 && db->nVdbeRea
21830 64 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 d>1 ){. /* If
21840 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 there are other
21850 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e active statemen
21860 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 ts that belong t
21870 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 0a o this database.
21880 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c 20 64 ** handle, d
21890 6f 77 6e 67 72 61 64 65 20 74 6f 20 61 20 72 65 owngrade to a re
218a0 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 ad-only transact
218b0 69 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 20 73 ion. The other s
218c0 74 61 74 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a tatements. **
218d0 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 may still be re
218e0 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 64 ading from the d
218f0 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 20 atabase. */.
21900 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 downgradeAllSha
21910 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
21920 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e ks(p);. p->in
21930 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45 Trans = TRANS_RE
21940 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 AD;. }else{.
21950 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c /* If the handl
21960 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f e had any kind o
21970 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 f transaction op
21980 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 en, decrement th
21990 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 e . ** transa
219a0 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74 ction count of t
219b0 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e he shared btree.
219c0 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 If the transact
219d0 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a ion count . *
219e0 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74 * reaches 0, set
219f0 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 the shared stat
21a00 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e e to TRANS_NONE.
21a10 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65 The unlockBtree
21a20 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a IfUnused(). *
21a30 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c * call below wil
21a40 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 l unlock the pag
21a50 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 er. */. if(
21a60 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e p->inTrans!=TRAN
21a70 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 S_NONE ){.
21a80 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 clearAllSharedCa
21a90 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 cheTableLocks(p)
21aa0 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 ;. pBt->nTr
21ab0 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 ansaction--;.
21ac0 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e if( 0==pBt->n
21ad0 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 Transaction ){.
21ae0 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 pBt->inTr
21af0 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
21b00 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a S_NONE;. }.
21b10 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 }.. /* Se
21b20 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 t the current tr
21b30 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 ansaction state
21b40 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e to TRANS_NONE an
21b50 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20 d unlock the .
21b60 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68 ** pager if th
21b70 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 is call closed t
21b80 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 he only read or
21b90 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
21ba0 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e n. */. p->in
21bb0 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
21bc0 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 NE;. unlockBt
21bd0 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
21be0 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e ;. }.. btreeIn
21bf0 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f tegrity(p);.}../
21c00 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 *.** Commit the
21c10 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 transaction curr
21c20 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 ently in progres
21c30 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
21c40 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 utine implements
21c50 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 the second phas
21c60 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 e of a 2-phase c
21c70 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73 ommit. The.** s
21c80 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
21c90 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 tPhaseOne() rout
21ca0 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
21cb0 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f st phase and sho
21cc0 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 uld.** be invoke
21cd0 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 d prior to calli
21ce0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
21cf0 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 The sqlite3Btr
21d00 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
21d10 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69 ().** routine di
21d20 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f d all the work o
21d30 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d f writing inform
21d40 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 ation out to dis
21d50 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74 k and flushing t
21d60 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73 he.** contents s
21d70 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 o that they are
21d80 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65 written onto the
21d90 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20 disk platter.
21da0 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 All this.** rout
21db0 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73 ine has to do is
21dc0 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63 delete or trunc
21dd0 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 ate or zero the
21de0 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a header in the.**
21df0 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
21e00 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75 urnal (which cau
21e10 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 ses the transact
21e20 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61 ion to commit) a
21e30 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 nd.** drop locks
21e40 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 ..**.** Normally
21e50 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 , if an error oc
21e60 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 20 70 curs while the p
21e70 61 67 65 72 20 6c 61 79 65 72 20 69 73 20 61 74 ager layer is at
21e80 74 65 6d 70 74 69 6e 67 20 74 6f 20 0a 2a 2a 20 tempting to .**
21e90 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 75 6e 64 finalize the und
21ea0 65 72 6c 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 erlying journal
21eb0 66 69 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 file, this funct
21ec0 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 ion returns an e
21ed0 72 72 6f 72 20 61 6e 64 0a 2a 2a 20 74 68 65 20 rror and.** the
21ee0 75 70 70 65 72 20 6c 61 79 65 72 20 77 69 6c 6c upper layer will
21ef0 20 61 74 74 65 6d 70 74 20 61 20 72 6f 6c 6c 62 attempt a rollb
21f00 61 63 6b 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 ack. However, if
21f10 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
21f20 6d 65 6e 74 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 7a ment.** is non-z
21f30 65 72 6f 20 74 68 65 6e 20 74 68 69 73 20 62 2d ero then this b-
21f40 74 72 65 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tree transaction
21f50 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6d 75 is part of a mu
21f60 6c 74 69 2d 66 69 6c 65 20 0a 2a 2a 20 74 72 61 lti-file .** tra
21f70 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 nsaction. In thi
21f80 73 20 63 61 73 65 2c 20 74 68 65 20 74 72 61 6e s case, the tran
21f90 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 saction has alre
21fa0 61 64 79 20 62 65 65 6e 20 63 6f 6d 6d 69 74 74 ady been committ
21fb0 65 64 20 0a 2a 2a 20 28 62 79 20 64 65 6c 65 74 ed .** (by delet
21fc0 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ing a master jou
21fd0 72 6e 61 6c 20 66 69 6c 65 29 20 61 6e 64 20 74 rnal file) and t
21fe0 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 69 he caller will i
21ff0 67 6e 6f 72 65 20 74 68 69 73 20 0a 2a 2a 20 66 gnore this .** f
22000 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
22010 63 6f 64 65 2e 20 53 6f 2c 20 65 76 65 6e 20 69 code. So, even i
22020 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
22030 73 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 6c s in the pager l
22040 61 79 65 72 2c 0a 2a 2a 20 72 65 73 65 74 20 74 ayer,.** reset t
22050 68 65 20 62 2d 74 72 65 65 20 6f 62 6a 65 63 74 he b-tree object
22060 73 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 s internal state
22070 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 to indicate tha
22080 74 20 74 68 65 20 77 72 69 74 65 0a 2a 2a 20 74 t the write.** t
22090 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62 ransaction has b
220a0 65 65 6e 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 een closed. This
220b0 20 69 73 20 71 75 69 74 65 20 73 61 66 65 2c 20 is quite safe,
220c0 61 73 20 74 68 65 20 70 61 67 65 72 20 77 69 6c as the pager wil
220d0 6c 20 68 61 76 65 0a 2a 2a 20 74 72 61 6e 73 69 l have.** transi
220e0 74 69 6f 6e 65 64 20 74 6f 20 74 68 65 20 65 72 tioned to the er
220f0 72 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ror state..**.**
22100 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 This will relea
22110 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 se the write loc
22120 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
22130 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 e file. If ther
22140 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 e.** are no acti
22150 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 ve cursors, it a
22160 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 lso releases the
22170 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 69 read lock..*/.i
22180 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
22190 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 ommitPhaseTwo(Bt
221a0 72 65 65 20 2a 70 2c 20 69 6e 74 20 62 43 6c 65 ree *p, int bCle
221b0 61 6e 75 70 29 7b 0a 0a 20 20 69 66 28 20 70 2d anup){.. if( p-
221c0 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
221d0 4e 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51 NONE ) return SQ
221e0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 LITE_OK;. sqlit
221f0 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
22200 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
22210 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 y(p);.. /* If t
22220 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 20 he handle has a
22230 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
22240 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 n open, commit t
22250 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 73 he shared-btrees
22260 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 . ** transacti
22270 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 73 on and set the s
22280 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 hared state to T
22290 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a RANS_READ.. */.
222a0 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
222b0 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b ==TRANS_WRITE ){
222c0 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 . int rc;.
222d0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
222e0 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 p->pBt;. ass
222f0 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
22300 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
22310 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 RITE );. asse
22320 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 rt( pBt->nTransa
22330 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 ction>0 );. r
22340 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
22350 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
22360 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
22370 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
22380 4f 4b 20 26 26 20 62 43 6c 65 61 6e 75 70 3d 3d OK && bCleanup==
22390 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
223a0 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
223b0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
223c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 ;. }. p->i
223d0 44 61 74 61 56 65 72 73 69 6f 6e 2d 2d 3b 20 20 DataVersion--;
223e0 2f 2a 20 43 6f 6d 70 65 6e 73 61 74 65 20 66 6f /* Compensate fo
223f0 72 20 70 50 61 67 65 72 2d 3e 69 44 61 74 61 56 r pPager->iDataV
22400 65 72 73 69 6f 6e 2b 2b 3b 20 2a 2f 0a 20 20 20 ersion++; */.
22410 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
22420 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 ion = TRANS_READ
22430 3b 0a 20 20 20 20 62 74 72 65 65 43 6c 65 61 72 ;. btreeClear
22440 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b HasContent(pBt);
22450 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 . }.. btreeEnd
22460 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a Transaction(p);.
22470 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
22480 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
22490 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
224a0 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 *.** Do both pha
224b0 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e ses of a commit.
224c0 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
224d0 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 treeCommit(Btree
224e0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
224f0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
22500 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 ter(p);. rc = s
22510 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
22520 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b tPhaseOne(p, 0);
22530 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
22540 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
22550 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
22560 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2c 20 30 mitPhaseTwo(p, 0
22570 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
22580 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
22590 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
225a0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
225b0 65 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 e sets the state
225c0 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 to CURSOR_FAULT
225d0 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a and the error.*
225e0 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 * code to errCod
225f0 65 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 e for every curs
22600 6f 72 20 6f 6e 20 61 6e 79 20 42 74 53 68 61 72 or on any BtShar
22610 65 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a ed that pBtree.*
22620 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 20 4f * references. O
22630 72 20 69 66 20 74 68 65 20 77 72 69 74 65 4f 6e r if the writeOn
22640 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 ly flag is set t
22650 6f 20 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 0a 2a o 1, then only.*
22660 2a 20 74 72 69 70 20 77 72 69 74 65 20 63 75 72 * trip write cur
22670 73 6f 72 73 20 61 6e 64 20 6c 65 61 76 65 20 72 sors and leave r
22680 65 61 64 20 63 75 72 73 6f 72 73 20 75 6e 63 68 ead cursors unch
22690 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 anged..**.** Eve
226a0 72 79 20 63 75 72 73 6f 72 20 69 73 20 61 20 63 ry cursor is a c
226b0 61 6e 64 69 64 61 74 65 20 74 6f 20 62 65 20 74 andidate to be t
226c0 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e ripped, includin
226d0 67 20 63 75 72 73 6f 72 73 0a 2a 2a 20 74 68 61 g cursors.** tha
226e0 74 20 62 65 6c 6f 6e 67 20 74 6f 20 6f 74 68 65 t belong to othe
226f0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
22700 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 ctions that happ
22710 65 6e 20 74 6f 20 62 65 0a 2a 2a 20 73 68 61 72 en to be.** shar
22720 69 6e 67 20 74 68 65 20 63 61 63 68 65 20 77 69 ing the cache wi
22730 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a th pBtree..**.**
22740 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 This routine ge
22750 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 ts called when a
22760 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
22770 2e 20 49 66 20 74 68 65 20 77 72 69 74 65 4f 6e . If the writeOn
22780 6c 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72 ly.** flag is tr
22790 75 65 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 ue, then only wr
227a0 69 74 65 2d 63 75 72 73 6f 72 73 20 6e 65 65 64 ite-cursors need
227b0 20 62 65 20 74 72 69 70 70 65 64 20 2d 20 72 65 be tripped - re
227c0 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f ad-only.** curso
227d0 72 73 20 73 61 76 65 20 74 68 65 69 72 20 63 75 rs save their cu
227e0 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 73 20 rrent positions
227f0 73 6f 20 74 68 61 74 20 74 68 65 79 20 6d 61 79 so that they may
22800 20 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 66 6f continue .** fo
22810 6c 6c 6f 77 69 6e 67 20 74 68 65 20 72 6f 6c 6c llowing the roll
22820 62 61 63 6b 2e 20 4f 72 2c 20 69 66 20 77 72 69 back. Or, if wri
22830 74 65 4f 6e 6c 79 20 69 73 20 66 61 6c 73 65 2c teOnly is false,
22840 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 all cursors are
22850 20 0a 2a 2a 20 74 72 69 70 70 65 64 2e 20 49 6e .** tripped. In
22860 20 67 65 6e 65 72 61 6c 2c 20 77 72 69 74 65 4f general, writeO
22870 6e 6c 79 20 69 73 20 66 61 6c 73 65 20 69 66 20 nly is false if
22880 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
22890 62 65 69 6e 67 0a 2a 2a 20 72 6f 6c 6c 65 64 20 being.** rolled
228a0 62 61 63 6b 20 6d 6f 64 69 66 69 65 64 20 74 68 back modified th
228b0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
228c0 61 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 a. In this case
228d0 62 2d 74 72 65 65 20 72 6f 6f 74 0a 2a 2a 20 70 b-tree root.** p
228e0 61 67 65 73 20 6d 61 79 20 62 65 20 6d 6f 76 65 ages may be move
228f0 64 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f d or deleted fro
22900 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 m the database a
22910 6c 74 6f 67 65 74 68 65 72 2c 20 6d 61 6b 69 6e ltogether, makin
22920 67 0a 2a 2a 20 69 74 20 75 6e 73 61 66 65 20 66 g.** it unsafe f
22930 6f 72 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 or read cursors
22940 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2a 0a to continue..**.
22950 2a 2a 20 49 66 20 74 68 65 20 77 72 69 74 65 4f ** If the writeO
22960 6e 6c 79 20 66 6c 61 67 20 69 73 20 74 72 75 65 nly flag is true
22970 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 69 73 and an error is
22980 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 encountered whi
22990 6c 65 20 0a 2a 2a 20 73 61 76 69 6e 67 20 74 68 le .** saving th
229a0 65 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 e current positi
229b0 6f 6e 20 6f 66 20 61 20 72 65 61 64 2d 6f 6e 6c on of a read-onl
229c0 79 20 63 75 72 73 6f 72 2c 20 61 6c 6c 20 63 75 y cursor, all cu
229d0 72 73 6f 72 73 2c 20 0a 2a 2a 20 69 6e 63 6c 75 rsors, .** inclu
229e0 64 69 6e 67 20 61 6c 6c 20 72 65 61 64 2d 63 75 ding all read-cu
229f0 72 73 6f 72 73 20 61 72 65 20 74 72 69 70 70 65 rsors are trippe
22a00 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f d..**.** SQLITE_
22a10 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
22a20 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 f successful, or
22a30 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
22a40 75 72 73 20 77 68 69 6c 65 0a 2a 2a 20 73 61 76 urs while.** sav
22a50 69 6e 67 20 61 20 63 75 72 73 6f 72 20 70 6f 73 ing a cursor pos
22a60 69 74 69 6f 6e 2c 20 61 6e 20 53 51 4c 69 74 65 ition, an SQLite
22a70 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a error code..*/.
22a80 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
22a90 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 TripAllCursors(B
22aa0 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e tree *pBtree, in
22ab0 74 20 65 72 72 43 6f 64 65 2c 20 69 6e 74 20 77 t errCode, int w
22ac0 72 69 74 65 4f 6e 6c 79 29 7b 0a 20 20 42 74 43 riteOnly){. BtC
22ad0 75 72 73 6f 72 20 2a 70 3b 0a 20 20 69 6e 74 20 ursor *p;. int
22ae0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
22af0 0a 20 20 61 73 73 65 72 74 28 20 28 77 72 69 74 . assert( (writ
22b00 65 4f 6e 6c 79 3d 3d 30 20 7c 7c 20 77 72 69 74 eOnly==0 || writ
22b10 65 4f 6e 6c 79 3d 3d 31 29 20 26 26 20 42 54 43 eOnly==1) && BTC
22b20 46 5f 57 72 69 74 65 46 6c 61 67 3d 3d 31 20 29 F_WriteFlag==1 )
22b30 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29 ;. if( pBtree )
22b40 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
22b50 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b eeEnter(pBtree);
22b60 0a 20 20 20 20 66 6f 72 28 70 3d 70 42 74 72 65 . for(p=pBtre
22b70 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b e->pBt->pCursor;
22b80 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
22b90 0a 20 20 20 20 20 20 69 66 28 20 77 72 69 74 65 . if( write
22ba0 4f 6e 6c 79 20 26 26 20 28 70 2d 3e 63 75 72 46 Only && (p->curF
22bb0 6c 61 67 73 20 26 20 42 54 43 46 5f 57 72 69 74 lags & BTCF_Writ
22bc0 65 46 6c 61 67 29 3d 3d 30 20 29 7b 0a 20 20 20 eFlag)==0 ){.
22bd0 20 20 20 20 20 69 66 28 20 70 2d 3e 65 53 74 61 if( p->eSta
22be0 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
22bf0 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 || p->eState==C
22c00 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 URSOR_SKIPNEXT )
22c10 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
22c20 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 saveCursorPosit
22c30 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ion(p);.
22c40 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
22c50 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
22c60 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
22c70 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 BtreeTripAllCurs
22c80 6f 72 73 28 70 42 74 72 65 65 2c 20 72 63 2c 20 ors(pBtree, rc,
22c90 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0);.
22ca0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
22cb0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
22cc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
22cd0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
22ce0 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 earCursor(p);.
22cf0 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 p->eState
22d00 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a = CURSOR_FAULT;.
22d10 20 20 20 20 20 20 20 20 70 2d 3e 73 6b 69 70 4e p->skipN
22d20 65 78 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 ext = errCode;.
22d30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 74 72 }. btr
22d40 65 65 52 65 6c 65 61 73 65 41 6c 6c 43 75 72 73 eeReleaseAllCurs
22d50 6f 72 50 61 67 65 73 28 70 29 3b 0a 20 20 20 20 orPages(p);.
22d60 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
22d70 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b eeLeave(pBtree);
22d80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
22d90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 ;.}../*.** Rollb
22da0 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 ack the transact
22db0 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e ion in progress.
22dc0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 69 70 43 6f .**.** If tripCo
22dd0 64 65 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 de is not SQLITE
22de0 5f 4f 4b 20 74 68 65 6e 20 63 75 72 73 6f 72 73 _OK then cursors
22df0 20 77 69 6c 6c 20 62 65 20 69 6e 76 61 6c 69 64 will be invalid
22e00 61 74 65 64 20 28 74 72 69 70 70 65 64 29 2e 0a ated (tripped)..
22e10 2a 2a 20 4f 6e 6c 79 20 77 72 69 74 65 20 63 75 ** Only write cu
22e20 72 73 6f 72 73 20 61 72 65 20 74 72 69 70 70 65 rsors are trippe
22e30 64 20 69 66 20 77 72 69 74 65 4f 6e 6c 79 20 69 d if writeOnly i
22e40 73 20 74 72 75 65 20 62 75 74 20 61 6c 6c 20 63 s true but all c
22e50 75 72 73 6f 72 73 20 61 72 65 0a 2a 2a 20 74 72 ursors are.** tr
22e60 69 70 70 65 64 20 69 66 20 77 72 69 74 65 4f 6e ipped if writeOn
22e70 6c 79 20 69 73 20 66 61 6c 73 65 2e 20 20 41 6e ly is false. An
22e80 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 y attempt to use
22e90 0a 2a 2a 20 61 20 74 72 69 70 70 65 64 20 63 75 .** a tripped cu
22ea0 72 73 6f 72 20 77 69 6c 6c 20 72 65 73 75 6c 74 rsor will result
22eb0 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a in an error..**
22ec0 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 .** This will re
22ed0 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 lease the write
22ee0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
22ef0 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 base file. If t
22f00 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 here.** are no a
22f10 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 ctive cursors, i
22f20 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 t also releases
22f30 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
22f40 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
22f50 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 eeRollback(Btree
22f60 20 2a 70 2c 20 69 6e 74 20 74 72 69 70 43 6f 64 *p, int tripCod
22f70 65 2c 20 69 6e 74 20 77 72 69 74 65 4f 6e 6c 79 e, int writeOnly
22f80 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
22f90 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
22fa0 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 ->pBt;. MemPage
22fb0 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 61 73 73 *pPage1;.. ass
22fc0 65 72 74 28 20 77 72 69 74 65 4f 6e 6c 79 3d 3d ert( writeOnly==
22fd0 31 20 7c 7c 20 77 72 69 74 65 4f 6e 6c 79 3d 3d 1 || writeOnly==
22fe0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 0 );. assert( t
22ff0 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f ripCode==SQLITE_
23000 41 42 4f 52 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c ABORT_ROLLBACK |
23010 7c 20 74 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 | tripCode==SQLI
23020 54 45 5f 4f 4b 20 29 3b 0a 20 20 73 71 6c 69 74 TE_OK );. sqlit
23030 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
23040 0a 20 20 69 66 28 20 74 72 69 70 43 6f 64 65 3d . if( tripCode=
23050 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
23060 20 20 72 63 20 3d 20 74 72 69 70 43 6f 64 65 20 rc = tripCode
23070 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 = saveAllCursors
23080 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 (pBt, 0, 0);.
23090 20 69 66 28 20 72 63 20 29 20 77 72 69 74 65 4f if( rc ) writeO
230a0 6e 6c 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 nly = 0;. }else
230b0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
230c0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
230d0 74 72 69 70 43 6f 64 65 20 29 7b 0a 20 20 20 20 tripCode ){.
230e0 69 6e 74 20 72 63 32 20 3d 20 73 71 6c 69 74 65 int rc2 = sqlite
230f0 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 3BtreeTripAllCur
23100 73 6f 72 73 28 70 2c 20 74 72 69 70 43 6f 64 65 sors(p, tripCode
23110 2c 20 77 72 69 74 65 4f 6e 6c 79 29 3b 0a 20 20 , writeOnly);.
23120 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
23130 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 77 72 69 74 LITE_OK || (writ
23140 65 4f 6e 6c 79 3d 3d 30 20 26 26 20 72 63 32 3d eOnly==0 && rc2=
23150 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 =SQLITE_OK) );.
23160 20 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 if( rc2!=SQLI
23170 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 TE_OK ) rc = rc2
23180 3b 0a 20 20 7d 0a 20 20 62 74 72 65 65 49 6e 74 ;. }. btreeInt
23190 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 egrity(p);.. if
231a0 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
231b0 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
231c0 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 int rc2;.. a
231d0 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49 ssert( TRANS_WRI
231e0 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 TE==pBt->inTrans
231f0 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 action );. rc
23200 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 2 = sqlite3Pager
23210 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 Rollback(pBt->pP
23220 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ager);. if( r
23230 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b c2!=SQLITE_OK ){
23240 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b . rc = rc2;
23250 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
23260 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 he rollback may
23270 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20 74 have destroyed t
23280 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 he pPage1->aData
23290 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 value. So.
232a0 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 47 65 74 ** call btreeGet
232b0 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31 Page() on page 1
232c0 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 again to make.
232d0 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65 ** sure pPage
232e0 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20 1->aData is set
232f0 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
23300 20 20 69 66 28 20 62 74 72 65 65 47 65 74 50 61 if( btreeGetPa
23310 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
23320 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f e1, 0)==SQLITE_O
23330 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e K ){. int n
23340 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
23350 32 38 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 28+(u8*)pPage1->
23360 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 74 65 aData);. te
23370 73 74 63 61 73 65 28 20 6e 50 61 67 65 3d 3d 30 stcase( nPage==0
23380 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 50 );. if( nP
23390 61 67 65 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 age==0 ) sqlite3
233a0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
233b0 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 Bt->pPager, &nPa
233c0 67 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 ge);. testc
233d0 61 73 65 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 ase( pBt->nPage!
233e0 3d 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 =nPage );.
233f0 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e 50 61 pBt->nPage = nPa
23400 67 65 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 ge;. releas
23410 65 50 61 67 65 4f 6e 65 28 70 50 61 67 65 31 29 ePageOne(pPage1)
23420 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
23430 72 74 28 20 63 6f 75 6e 74 56 61 6c 69 64 43 75 rt( countValidCu
23440 72 73 6f 72 73 28 70 42 74 2c 20 31 29 3d 3d 30 rsors(pBt, 1)==0
23450 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 );. pBt->inT
23460 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 ransaction = TRA
23470 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20 62 74 72 NS_READ;. btr
23480 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e eeClearHasConten
23490 74 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 t(pBt);. }.. b
234a0 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 treeEndTransacti
234b0 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 on(p);. sqlite3
234c0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
234d0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
234e0 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 *.** Start a sta
234f0 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
23500 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 ction. The subtr
23510 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 62 65 ansaction can be
23520 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 rolled.** back
23530 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 independently of
23540 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 the main transa
23550 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20 ction. You must
23560 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 start a transact
23570 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73 ion .** before s
23580 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61 tarting a subtra
23590 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 nsaction. The su
235a0 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 btransaction is
235b0 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61 ended automatica
235c0 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d lly .** if the m
235d0 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ain transaction
235e0 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 commits or rolls
235f0 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 back..**.** Sta
23600 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
23610 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 ctions are used
23620 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61 around individua
23630 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 l SQL statements
23640 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e .** that are con
23650 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20 tained within a
23660 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 BEGIN...COMMIT b
23670 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 lock. If a cons
23680 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 traint.** error
23690 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 occurs within th
236a0 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 e statement, the
236b0 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 74 20 effect of that
236c0 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a one statement.**
236d0 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 can be rolled b
236e0 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 ack without havi
236f0 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ng to rollback t
23700 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 he entire transa
23710 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 ction..**.** A s
23720 74 61 74 65 6d 65 6e 74 20 73 75 62 2d 74 72 61 tatement sub-tra
23730 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c nsaction is impl
23740 65 6d 65 6e 74 65 64 20 61 73 20 61 6e 20 61 6e emented as an an
23750 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e onymous savepoin
23760 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 t. The.** value
23770 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
23780 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 cond parameter i
23790 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 s the total numb
237a0 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 er of savepoints
237b0 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 ,.** including t
237c0 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 he new anonymous
237d0 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e savepoint, open
237e0 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 on the B-Tree.
237f0 69 2e 65 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a i.e. if there.**
23800 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 73 are no active s
23810 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f avepoints and no
23820 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 other statement
23830 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 -transactions op
23840 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e en,.** iStatemen
23850 74 20 69 73 20 31 2e 20 54 68 69 73 20 61 6e 6f t is 1. This ano
23860 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 nymous savepoint
23870 20 63 61 6e 20 62 65 20 72 65 6c 65 61 73 65 64 can be released
23880 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a or rolled back.
23890 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c ** using the sql
238a0 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
238b0 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a nt() function..*
238c0 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
238d0 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 eeBeginStmt(Btre
238e0 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 74 65 e *p, int iState
238f0 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b ment){. int rc;
23900 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
23910 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c = p->pBt;. sql
23920 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
23930 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
23940 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
23950 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
23960 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 ( (pBt->btsFlags
23970 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 & BTS_READ_ONLY
23980 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )==0 );. assert
23990 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 ( iStatement>0 )
239a0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 ;. assert( iSta
239b0 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 tement>p->db->nS
239c0 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 61 73 avepoint );. as
239d0 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 sert( pBt->inTra
239e0 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
239f0 57 52 49 54 45 20 29 3b 0a 20 20 2f 2a 20 41 74 WRITE );. /* At
23a00 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 6c the pager level
23a10 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 , a statement tr
23a20 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 73 ansaction is a s
23a30 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 avepoint with.
23a40 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 ** an index grea
23a50 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 61 76 ter than all sav
23a60 65 70 6f 69 6e 74 73 20 63 72 65 61 74 65 64 20 epoints created
23a70 65 78 70 6c 69 63 69 74 6c 79 20 75 73 69 6e 67 explicitly using
23a80 0a 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d . ** SQL statem
23a90 65 6e 74 73 2e 20 49 74 20 69 73 20 69 6c 6c 65 ents. It is ille
23aa0 67 61 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65 6c gal to open, rel
23ab0 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b ease or rollback
23ac0 20 61 6e 79 0a 20 20 2a 2a 20 73 75 63 68 20 73 any. ** such s
23ad0 61 76 65 70 6f 69 6e 74 73 20 77 68 69 6c 65 20 avepoints while
23ae0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 the statement tr
23af0 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f ansaction savepo
23b00 69 6e 74 20 69 73 20 61 63 74 69 76 65 2e 0a 20 int is active..
23b10 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
23b20 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 e3PagerOpenSavep
23b30 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 oint(pBt->pPager
23b40 2c 20 69 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 , iStatement);.
23b50 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
23b60 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
23b70 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
23b80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
23b90 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
23ba0 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 n, op, is always
23bb0 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
23bc0 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f ACK.** or SAVEPO
23bd0 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 INT_RELEASE. Thi
23be0 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 s function eithe
23bf0 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f r releases or ro
23c00 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 lls back the.**
23c10 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 savepoint identi
23c20 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
23c30 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 r iSavepoint, de
23c40 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 pending on the v
23c50 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a alue .** of op..
23c60 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 **.** Normally,
23c70 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 iSavepoint is gr
23c80 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
23c90 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 ual to zero. How
23ca0 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a ever, if op is.*
23cb0 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c * SAVEPOINT_ROLL
23cc0 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 BACK, then iSave
23cd0 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 point may also b
23ce0 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 e -1. In this ca
23cf0 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 se the .** conte
23d00 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 nts of the entir
23d10 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 e transaction ar
23d20 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 e rolled back. T
23d30 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 his is different
23d40 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 .** from a norma
23d50 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f l transaction ro
23d60 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f llback, as no lo
23d70 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 cks are released
23d80 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e and the.** tran
23d90 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 saction remains
23da0 6f 70 65 6e 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c open..*/.int sql
23db0 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
23dc0 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 nt(Btree *p, int
23dd0 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f op, int iSavepo
23de0 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
23df0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
23e00 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e ( p && p->inTran
23e10 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
23e20 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
23e30 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
23e40 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 assert( op==SA
23e50 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 VEPOINT_RELEASE
23e60 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 || op==SAVEPOINT
23e70 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 _ROLLBACK );.
23e80 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f assert( iSavepo
23e90 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 int>=0 || (iSave
23ea0 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d point==-1 && op=
23eb0 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
23ec0 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 ACK) );. sqli
23ed0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
23ee0 3b 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 ;. if( op==SA
23ef0 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
23f00 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
23f10 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 aveAllCursors(pB
23f20 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a t, 0, 0);. }.
23f30 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
23f40 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
23f50 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
23f60 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 Savepoint(pBt->p
23f70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 65 Pager, op, iSave
23f80 70 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 point);. }.
23f90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
23fa0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 _OK ){. if(
23fb0 20 69 53 61 76 65 70 6f 69 6e 74 3c 30 20 26 26 iSavepoint<0 &&
23fc0 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 (pBt->btsFlags
23fd0 26 20 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f & BTS_INITIALLY_
23fe0 45 4d 50 54 59 29 21 3d 30 20 29 7b 0a 20 20 20 EMPTY)!=0 ){.
23ff0 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 pBt->nPage
24000 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
24010 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 rc = newDatab
24020 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 ase(pBt);.
24030 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 67 65 74 pBt->nPage = get
24040 34 62 79 74 65 28 32 38 20 2b 20 70 42 74 2d 3e 4byte(28 + pBt->
24050 70 50 61 67 65 31 2d 3e 61 44 61 74 61 29 3b 0a pPage1->aData);.
24060 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 . /* The da
24070 74 61 62 61 73 65 20 73 69 7a 65 20 77 61 73 20 tabase size was
24080 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
24090 20 6f 66 66 73 65 74 20 32 38 20 6f 66 20 74 68 offset 28 of th
240a0 65 20 68 65 61 64 65 72 0a 20 20 20 20 20 20 2a e header. *
240b0 2a 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 * when the trans
240c0 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 action started,
240d0 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 so we know that
240e0 74 68 65 20 76 61 6c 75 65 20 61 74 20 6f 66 66 the value at off
240f0 73 65 74 0a 20 20 20 20 20 20 2a 2a 20 32 38 20 set. ** 28
24100 69 73 20 6e 6f 6e 7a 65 72 6f 2e 20 2a 2f 0a 20 is nonzero. */.
24110 20 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 assert( pBt
24120 2d 3e 6e 50 61 67 65 3e 30 20 29 3b 0a 20 20 20 ->nPage>0 );.
24130 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 }. sqlite3Bt
24140 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d reeLeave(p);. }
24150 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
24160 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
24170 6e 65 77 20 63 75 72 73 6f 72 20 66 6f 72 20 74 new cursor for t
24180 68 65 20 42 54 72 65 65 20 77 68 6f 73 65 20 72 he BTree whose r
24190 6f 6f 74 20 69 73 20 6f 6e 20 74 68 65 20 70 61 oot is on the pa
241a0 67 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20 49 66 ge.** iTable. If
241b0 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 a read-only cur
241c0 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 65 64 sor is requested
241d0 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 , it is assumed
241e0 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c that.** the call
241f0 65 72 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 er already has a
24200 74 20 6c 65 61 73 74 20 61 20 72 65 61 64 2d 6f t least a read-o
24210 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 nly transaction
24220 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20 64 open.** on the d
24230 61 74 61 62 61 73 65 20 61 6c 72 65 61 64 79 2e atabase already.
24240 20 49 66 20 61 20 77 72 69 74 65 2d 63 75 72 73 If a write-curs
24250 6f 72 20 69 73 20 72 65 71 75 65 73 74 65 64 2c or is requested,
24260 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c then.** the cal
24270 6c 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 ler is assumed t
24280 6f 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 o have an open w
24290 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
242a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 ..**.** If the B
242b0 54 52 45 45 5f 57 52 43 53 52 20 62 69 74 20 6f TREE_WRCSR bit o
242c0 66 20 77 72 46 6c 61 67 20 69 73 20 63 6c 65 61 f wrFlag is clea
242d0 72 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 r, then the curs
242e0 6f 72 20 63 61 6e 20 6f 6e 6c 79 0a 2a 2a 20 62 or can only.** b
242f0 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 e used for readi
24300 6e 67 2e 20 20 49 66 20 74 68 65 20 42 54 52 45 ng. If the BTRE
24310 45 5f 57 52 43 53 52 20 62 69 74 20 69 73 20 73 E_WRCSR bit is s
24320 65 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 et, then the cur
24330 73 6f 72 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 sor.** can be us
24340 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f ed for reading o
24350 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 69 66 r for writing if
24360 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e other condition
24370 73 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a s for writing.**
24380 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 are also met.
24390 54 68 65 73 65 20 61 72 65 20 74 68 65 20 63 6f These are the co
243a0 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 nditions that mu
243b0 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 st be met in ord
243c0 65 72 0a 2a 2a 20 66 6f 72 20 77 72 69 74 69 6e er.** for writin
243d0 67 20 74 6f 20 62 65 20 61 6c 6c 6f 77 65 64 3a g to be allowed:
243e0 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 .**.** 1: The c
243f0 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 ursor must have
24400 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 been opened with
24410 20 77 72 46 6c 61 67 20 63 6f 6e 74 61 69 6e 69 wrFlag containi
24420 6e 67 20 42 54 52 45 45 5f 57 52 43 53 52 0a 2a ng BTREE_WRCSR.*
24430 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 *.** 2: Other d
24440 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
24450 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 ons that share t
24460 68 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 he same pager ca
24470 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 che.** but w
24480 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 hich are not in
24490 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 the READ_UNCOMMI
244a0 54 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e TTED state may n
244b0 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 ot have.** c
244c0 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 ursors open with
244d0 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 wrFlag==0 on th
244e0 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f e same table. O
244f0 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 therwise.**
24500 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 the changes made
24510 20 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 by this write c
24520 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 ursor would be v
24530 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 isible to.**
24540 20 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 the read cursor
24550 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 s in the other d
24560 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
24570 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 on..**.** 3: Th
24580 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
24590 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 be writable (not
245a0 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 on read-only me
245b0 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 dia).**.** 4: T
245c0 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 here must be an
245d0 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 active transacti
245e0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 54 on..**.** The BT
245f0 52 45 45 5f 46 4f 52 44 45 4c 45 54 45 20 62 69 REE_FORDELETE bi
24600 74 20 6f 66 20 77 72 46 6c 61 67 20 6d 61 79 20 t of wrFlag may
24610 6f 70 74 69 6f 6e 61 6c 6c 79 20 62 65 20 73 65 optionally be se
24620 74 20 69 66 20 42 54 52 45 45 5f 57 52 43 53 52 t if BTREE_WRCSR
24630 0a 2a 2a 20 69 73 20 73 65 74 2e 20 20 49 66 20 .** is set. If
24640 46 4f 52 44 45 4c 45 54 45 20 69 73 20 73 65 74 FORDELETE is set
24650 2c 20 74 68 61 74 20 69 73 20 61 20 68 69 6e 74 , that is a hint
24660 20 74 6f 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e to the implemen
24670 74 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 tation that.** t
24680 68 69 73 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 his cursor will
24690 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 74 6f 20 only be used to
246a0 73 65 65 6b 20 74 6f 20 61 6e 64 20 64 65 6c 65 seek to and dele
246b0 74 65 20 65 6e 74 72 69 65 73 20 6f 66 20 61 6e te entries of an
246c0 20 69 6e 64 65 78 0a 2a 2a 20 61 73 20 70 61 72 index.** as par
246d0 74 20 6f 66 20 61 20 6c 61 72 67 65 72 20 44 45 t of a larger DE
246e0 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 LETE statement.
246f0 20 54 68 65 20 46 4f 52 44 45 4c 45 54 45 20 68 The FORDELETE h
24700 69 6e 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 int is not used
24710 62 79 0a 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 by.** this imple
24720 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 42 75 74 20 mentation. But
24730 69 6e 20 61 20 68 79 70 6f 74 68 65 74 69 63 61 in a hypothetica
24740 6c 20 61 6c 74 65 72 6e 61 74 69 76 65 20 73 74 l alternative st
24750 6f 72 61 67 65 20 65 6e 67 69 6e 65 20 0a 2a 2a orage engine .**
24760 20 69 6e 20 77 68 69 63 68 20 69 6e 64 65 78 20 in which index
24770 65 6e 74 72 69 65 73 20 61 72 65 20 61 75 74 6f entries are auto
24780 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 matically delete
24790 64 20 77 68 65 6e 20 63 6f 72 72 65 73 70 6f 6e d when correspon
247a0 64 69 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 72 6f ding table.** ro
247b0 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 2c 20 ws are deleted,
247c0 74 68 65 20 46 4f 52 44 45 4c 45 54 45 20 66 6c the FORDELETE fl
247d0 61 67 20 69 73 20 61 20 68 69 6e 74 20 74 68 61 ag is a hint tha
247e0 74 20 61 6c 6c 20 53 45 45 4b 20 61 6e 64 20 44 t all SEEK and D
247f0 45 4c 45 54 45 0a 2a 2a 20 6f 70 65 72 61 74 69 ELETE.** operati
24800 6f 6e 73 20 6f 6e 20 74 68 69 73 20 63 75 72 73 ons on this curs
24810 6f 72 20 63 61 6e 20 62 65 20 6e 6f 2d 6f 70 73 or can be no-ops
24820 20 61 6e 64 20 61 6c 6c 20 52 45 41 44 20 6f 70 and all READ op
24830 65 72 61 74 69 6f 6e 73 20 63 61 6e 20 0a 2a 2a erations can .**
24840 20 72 65 74 75 72 6e 20 61 20 6e 75 6c 6c 20 72 return a null r
24850 6f 77 20 28 32 2d 62 79 74 65 73 3a 20 30 78 30 ow (2-bytes: 0x0
24860 31 20 30 78 30 30 29 2e 0a 2a 2a 0a 2a 2a 20 4e 1 0x00)..**.** N
24870 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f o checking is do
24880 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ne to make sure
24890 74 68 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 that page iTable
248a0 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a really is the.*
248b0 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 * root page of a
248c0 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 b-tree. If it
248d0 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 is not, then the
248e0 20 63 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 cursor acquired
248f0 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 .** will not wor
24900 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a k correctly..**.
24910 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
24920 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 that the sqlite
24930 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 3BtreeCursorZero
24940 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
24950 65 64 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f ed.** on pCur to
24960 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 initialize the
24970 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69 memory space pri
24980 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
24990 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a his routine..*/.
249a0 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
249b0 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 Cursor(. Btree
249c0 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
249d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
249e0 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f /* The btree */
249f0 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
24a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
24a20 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 t page of table
24a30 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 to open */. int
24a40 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 wrFlag,
24a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24a60 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 /* 1 to writ
24a70 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e. 0 read-only *
24a80 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
24a90 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
24aa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
24ab0 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 rst arg to compa
24ac0 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a rison function *
24ad0 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
24ae0 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ur
24af0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
24b00 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 ace for new curs
24b10 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 or */.){. BtSha
24b20 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
24b30 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
24b40 20 20 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 /* Shared b-tr
24b50 65 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 ee handle */. B
24b60 74 43 75 72 73 6f 72 20 2a 70 58 3b 20 20 20 20 tCursor *pX;
24b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24b80 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 69 6e 67 /* Looping
24b90 20 6f 76 65 72 20 6f 74 68 65 72 20 61 6c 6c 20 over other all
24ba0 63 75 72 73 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 cursors */.. as
24bb0 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
24bc0 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 eeHoldsMutex(p)
24bd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 );. assert( wrF
24be0 6c 61 67 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c lag==0 . |
24bf0 7c 20 77 72 46 6c 61 67 3d 3d 42 54 52 45 45 5f | wrFlag==BTREE_
24c00 57 52 43 53 52 20 0a 20 20 20 20 20 20 20 7c 7c WRCSR . ||
24c10 20 77 72 46 6c 61 67 3d 3d 28 42 54 52 45 45 5f wrFlag==(BTREE_
24c20 57 52 43 53 52 7c 42 54 52 45 45 5f 46 4f 52 44 WRCSR|BTREE_FORD
24c30 45 4c 45 54 45 29 20 0a 20 20 29 3b 0a 0a 20 20 ELETE) . );..
24c40 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
24c50 20 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e assert statemen
24c60 74 73 20 76 65 72 69 66 79 20 74 68 61 74 20 69 ts verify that i
24c70 66 20 74 68 69 73 20 69 73 20 61 20 73 68 61 72 f this is a shar
24c80 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d 74 72 65 able . ** b-tre
24c90 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 e database, the
24ca0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f connection is ho
24cb0 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 72 lding the requir
24cc0 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 ed table locks,
24cd0 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61 74 20 6e . ** and that n
24ce0 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 o other connecti
24cf0 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70 65 6e 20 on has any open
24d00 63 75 72 73 6f 72 20 74 68 61 74 20 63 6f 6e 66 cursor that conf
24d10 6c 69 63 74 73 20 77 69 74 68 20 0a 20 20 2a 2a licts with . **
24d20 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a this lock. */.
24d30 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 assert( hasSha
24d40 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
24d50 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 4b 65 k(p, iTable, pKe
24d60 79 49 6e 66 6f 21 3d 30 2c 20 28 77 72 46 6c 61 yInfo!=0, (wrFla
24d70 67 3f 32 3a 31 29 29 20 29 3b 0a 20 20 61 73 73 g?2:1)) );. ass
24d80 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c ert( wrFlag==0 |
24d90 7c 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 | !hasReadConfli
24da0 63 74 73 28 70 2c 20 69 54 61 62 6c 65 29 20 29 cts(p, iTable) )
24db0 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 ;.. /* Assert t
24dc0 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 hat the caller h
24dd0 61 73 20 6f 70 65 6e 65 64 20 74 68 65 20 72 65 as opened the re
24de0 71 75 69 72 65 64 20 74 72 61 6e 73 61 63 74 69 quired transacti
24df0 6f 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 on. */. assert(
24e00 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e p->inTrans>TRAN
24e10 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 S_NONE );. asse
24e20 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c rt( wrFlag==0 ||
24e30 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
24e40 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 NS_WRITE );. as
24e50 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 sert( pBt->pPage
24e60 31 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 1 && pBt->pPage1
24e70 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 61 73 73 ->aData );. ass
24e80 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c ert( wrFlag==0 |
24e90 7c 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 | (pBt->btsFlags
24ea0 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 & BTS_READ_ONLY
24eb0 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 77 )==0 );.. if( w
24ec0 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 61 6c 6c rFlag ){. all
24ed0 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 70 ocateTempSpace(p
24ee0 42 74 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 Bt);. if( pBt
24ef0 2d 3e 70 54 6d 70 53 70 61 63 65 3d 3d 30 20 29 ->pTmpSpace==0 )
24f00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
24f10 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 OMEM_BKPT;. }.
24f20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 if( iTable==1 &
24f30 26 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 & btreePagecount
24f40 28 70 42 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 (pBt)==0 ){.
24f50 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d assert( wrFlag==
24f60 30 20 29 3b 0a 20 20 20 20 69 54 61 62 6c 65 20 0 );. iTable
24f70 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e = 0;. }.. /* N
24f80 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 ow that no other
24f90 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63 75 errors can occu
24fa0 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69 6e r, finish fillin
24fb0 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73 6f g in the BtCurso
24fc0 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 r. ** variables
24fd0 20 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20 63 75 and link the cu
24fe0 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 rsor into the Bt
24ff0 53 68 61 72 65 64 20 6c 69 73 74 2e 20 20 2a 2f Shared list. */
25000 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f . pCur->pgnoRoo
25010 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 t = (Pgno)iTable
25020 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 ;. pCur->iPage
25030 3d 20 2d 31 3b 0a 20 20 70 43 75 72 2d 3e 70 4b = -1;. pCur->pK
25040 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 eyInfo = pKeyInf
25050 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65 o;. pCur->pBtre
25060 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 e = p;. pCur->p
25070 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 Bt = pBt;. pCur
25080 2d 3e 63 75 72 46 6c 61 67 73 20 3d 20 77 72 46 ->curFlags = wrF
25090 6c 61 67 20 3f 20 42 54 43 46 5f 57 72 69 74 65 lag ? BTCF_Write
250a0 46 6c 61 67 20 3a 20 30 3b 0a 20 20 70 43 75 72 Flag : 0;. pCur
250b0 2d 3e 63 75 72 50 61 67 65 72 46 6c 61 67 73 20 ->curPagerFlags
250c0 3d 20 77 72 46 6c 61 67 20 3f 20 30 20 3a 20 50 = wrFlag ? 0 : P
250d0 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c AGER_GET_READONL
250e0 59 3b 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 Y;. /* If there
250f0 20 61 72 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 are two or more
25100 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 cursors on the
25110 73 61 6d 65 20 62 74 72 65 65 2c 20 74 68 65 6e same btree, then
25120 20 61 6c 6c 20 73 75 63 68 0a 20 20 2a 2a 20 63 all such. ** c
25130 75 72 73 6f 72 73 20 2a 6d 75 73 74 2a 20 68 61 ursors *must* ha
25140 76 65 20 74 68 65 20 42 54 43 46 5f 4d 75 6c 74 ve the BTCF_Mult
25150 69 70 6c 65 20 66 6c 61 67 20 73 65 74 2e 20 2a iple flag set. *
25160 2f 0a 20 20 66 6f 72 28 70 58 3d 70 42 74 2d 3e /. for(pX=pBt->
25170 70 43 75 72 73 6f 72 3b 20 70 58 3b 20 70 58 3d pCursor; pX; pX=
25180 70 58 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 pX->pNext){.
25190 69 66 28 20 70 58 2d 3e 70 67 6e 6f 52 6f 6f 74 if( pX->pgnoRoot
251a0 3d 3d 28 50 67 6e 6f 29 69 54 61 62 6c 65 20 29 ==(Pgno)iTable )
251b0 7b 0a 20 20 20 20 20 20 70 58 2d 3e 63 75 72 46 {. pX->curF
251c0 6c 61 67 73 20 7c 3d 20 42 54 43 46 5f 4d 75 6c lags |= BTCF_Mul
251d0 74 69 70 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 tiple;. pCu
251e0 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 r->curFlags |= B
251f0 54 43 46 5f 4d 75 6c 74 69 70 6c 65 3b 0a 20 20 TCF_Multiple;.
25200 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e }. }. pCur->
25210 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 pNext = pBt->pCu
25220 72 73 6f 72 3b 0a 20 20 70 42 74 2d 3e 70 43 75 rsor;. pBt->pCu
25230 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 rsor = pCur;. p
25240 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
25250 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 RSOR_INVALID;.
25260 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
25270 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 ;.}.int sqlite3B
25280 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 treeCursor(. Bt
25290 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 ree *p,
252a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
252b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
252c0 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 btree */. int
252d0 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 iTable,
252e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
252f0 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
25300 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f page of table to
25310 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 open */. int w
25320 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 rFlag,
25330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25340 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 /* 1 to w
25350 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c rite. 0 read-onl
25360 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 y */. struct Ke
25370 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c yInfo *pKeyInfo,
25380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25390 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 /* First arg
253a0 74 6f 20 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f to xCompare() */
253b0 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
253c0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
253d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
253e0 2a 20 57 72 69 74 65 20 6e 65 77 20 63 75 72 73 * Write new curs
253f0 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 or here */.){.
25400 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69 54 int rc;. if( iT
25410 61 62 6c 65 3c 31 20 29 7b 0a 20 20 20 20 72 63 able<1 ){. rc
25420 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
25430 54 5f 42 4b 50 54 3b 0a 20 20 7d 65 6c 73 65 7b T_BKPT;. }else{
25440 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
25450 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 eEnter(p);. r
25460 63 20 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 c = btreeCursor(
25470 70 2c 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 p, iTable, wrFla
25480 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 g, pKeyInfo, pCu
25490 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 r);. sqlite3B
254a0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
254b0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
254c0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
254d0 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43 he size of a BtC
254e0 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 ursor object in
254f0 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 bytes..**.** Thi
25500 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 s interfaces is
25510 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75 needed so that u
25520 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20 sers of cursors
25530 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a can preallocate.
25540 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74 ** sufficient st
25550 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 orage to hold a
25560 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43 cursor. The BtC
25570 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 ursor object is
25580 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 opaque.** to use
25590 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f rs so they canno
255a0 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 t do the sizeof(
255b0 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 ) themselves - t
255c0 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a hey must call.**
255d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
255e0 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
255f0 65 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 eeCursorSize(voi
25600 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 d){. return ROU
25610 4e 44 38 28 73 69 7a 65 6f 66 28 42 74 43 75 72 ND8(sizeof(BtCur
25620 73 6f 72 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 sor));.}../*.**
25630 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 Initialize memor
25640 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 63 y that will be c
25650 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 onverted into a
25660 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e BtCursor object.
25670 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 6d 70 6c .**.** The simpl
25680 65 20 61 70 70 72 6f 61 63 68 20 68 65 72 65 20 e approach here
25690 77 6f 75 6c 64 20 62 65 20 74 6f 20 6d 65 6d 73 would be to mems
256a0 65 74 28 29 20 74 68 65 20 65 6e 74 69 72 65 20 et() the entire
256b0 6f 62 6a 65 63 74 0a 2a 2a 20 74 6f 20 7a 65 72 object.** to zer
256c0 6f 2e 20 20 42 75 74 20 69 74 20 74 75 72 6e 73 o. But it turns
256d0 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 61 70 out that the ap
256e0 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49 64 78 Page[] and aiIdx
256f0 5b 5d 20 61 72 72 61 79 73 0a 2a 2a 20 64 6f 20 [] arrays.** do
25700 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 7a not need to be z
25710 65 72 6f 65 64 20 61 6e 64 20 74 68 65 79 20 61 eroed and they a
25720 72 65 20 6c 61 72 67 65 2c 20 73 6f 20 77 65 20 re large, so we
25730 63 61 6e 20 73 61 76 65 20 61 20 6c 6f 74 0a 2a can save a lot.*
25740 2a 20 6f 66 20 72 75 6e 2d 74 69 6d 65 20 62 79 * of run-time by
25750 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 69 6e skipping the in
25760 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 itialization of
25770 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74 73 2e 0a those elements..
25780 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 */.void sqlite3B
25790 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 treeCursorZero(B
257a0 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 6d tCursor *p){. m
257b0 65 6d 73 65 74 28 70 2c 20 30 2c 20 6f 66 66 73 emset(p, 0, offs
257c0 65 74 6f 66 28 42 74 43 75 72 73 6f 72 2c 20 42 etof(BtCursor, B
257d0 54 43 55 52 53 4f 52 5f 46 49 52 53 54 5f 55 4e TCURSOR_FIRST_UN
257e0 49 4e 49 54 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a INIT));.}../*.**
257f0 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e Close a cursor.
25800 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 The read lock
25810 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
25820 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 file is released
25830 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 .** when the las
25840 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 t cursor is clos
25850 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ed..*/.int sqlit
25860 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
25870 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
25880 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 r){. Btree *pBt
25890 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 ree = pCur->pBtr
258a0 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 ee;. if( pBtree
258b0 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
258c0 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 *pBt = pCur->pB
258d0 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 t;. sqlite3Bt
258e0 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 reeEnter(pBtree)
258f0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
25900 74 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b t->pCursor!=0 );
25910 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 43 . if( pBt->pC
25920 75 72 73 6f 72 3d 3d 70 43 75 72 20 29 7b 0a 20 ursor==pCur ){.
25930 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f pBt->pCurso
25940 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
25950 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
25960 20 20 42 74 43 75 72 73 6f 72 20 2a 70 50 72 65 BtCursor *pPre
25970 76 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 v = pBt->pCursor
25980 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 ;. do{.
25990 20 20 20 20 69 66 28 20 70 50 72 65 76 2d 3e 70 if( pPrev->p
259a0 4e 65 78 74 3d 3d 70 43 75 72 20 29 7b 0a 20 20 Next==pCur ){.
259b0 20 20 20 20 20 20 20 20 70 50 72 65 76 2d 3e 70 pPrev->p
259c0 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 Next = pCur->pNe
259d0 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 xt;. br
259e0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
259f0 20 20 20 20 20 20 20 70 50 72 65 76 20 3d 20 70 pPrev = p
25a00 50 72 65 76 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Prev->pNext;.
25a10 20 20 20 7d 77 68 69 6c 65 28 20 41 4c 57 41 59 }while( ALWAY
25a20 53 28 70 50 72 65 76 29 20 29 3b 0a 20 20 20 20 S(pPrev) );.
25a30 7d 0a 20 20 20 20 62 74 72 65 65 52 65 6c 65 61 }. btreeRelea
25a40 73 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 65 73 seAllCursorPages
25a50 28 70 43 75 72 29 3b 0a 20 20 20 20 75 6e 6c 6f (pCur);. unlo
25a60 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
25a70 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pBt);. sqlite
25a80 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 3_free(pCur->aOv
25a90 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 73 71 6c erflow);. sql
25aa0 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e ite3_free(pCur->
25ab0 70 4b 65 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 pKey);. sqlit
25ac0 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 e3BtreeLeave(pBt
25ad0 72 65 65 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e ree);. pCur->
25ae0 70 42 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a pBtree = 0;. }.
25af0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
25b00 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b OK;.}../*.** Mak
25b10 65 20 73 75 72 65 20 74 68 65 20 42 74 43 75 72 e sure the BtCur
25b20 73 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 sor* given in th
25b30 65 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 e argument has a
25b40 20 76 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 valid.** BtCurs
25b50 6f 72 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 or.info structur
25b60 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 e. If it is not
25b70 20 61 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 already valid,
25b80 63 61 6c 6c 0a 2a 2a 20 62 74 72 65 65 50 61 72 call.** btreePar
25b90 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c seCell() to fill
25ba0 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 it in..**.** Bt
25bb0 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 Cursor.info is a
25bc0 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e cache of the in
25bd0 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 formation in the
25be0 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a current cell..*
25bf0 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 * Using this cac
25c00 68 65 20 72 65 64 75 63 65 73 20 74 68 65 20 6e he reduces the n
25c10 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 umber of calls t
25c20 6f 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c o btreeParseCell
25c30 28 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e ()..*/.#ifndef N
25c40 44 45 42 55 47 0a 20 20 73 74 61 74 69 63 20 69 DEBUG. static i
25c50 6e 74 20 63 65 6c 6c 49 6e 66 6f 45 71 75 61 6c nt cellInfoEqual
25c60 28 43 65 6c 6c 49 6e 66 6f 20 2a 61 2c 20 43 65 (CellInfo *a, Ce
25c70 6c 6c 49 6e 66 6f 20 2a 62 29 7b 0a 20 20 20 20 llInfo *b){.
25c80 69 66 28 20 61 2d 3e 6e 4b 65 79 21 3d 62 2d 3e if( a->nKey!=b->
25c90 6e 4b 65 79 20 29 20 72 65 74 75 72 6e 20 30 3b nKey ) return 0;
25ca0 0a 20 20 20 20 69 66 28 20 61 2d 3e 70 50 61 79 . if( a->pPay
25cb0 6c 6f 61 64 21 3d 62 2d 3e 70 50 61 79 6c 6f 61 load!=b->pPayloa
25cc0 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 d ) return 0;.
25cd0 20 20 69 66 28 20 61 2d 3e 6e 50 61 79 6c 6f 61 if( a->nPayloa
25ce0 64 21 3d 62 2d 3e 6e 50 61 79 6c 6f 61 64 20 29 d!=b->nPayload )
25cf0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 return 0;. i
25d00 66 28 20 61 2d 3e 6e 4c 6f 63 61 6c 21 3d 62 2d f( a->nLocal!=b-
25d10 3e 6e 4c 6f 63 61 6c 20 29 20 72 65 74 75 72 6e >nLocal ) return
25d20 20 30 3b 0a 20 20 20 20 69 66 28 20 61 2d 3e 6e 0;. if( a->n
25d30 53 69 7a 65 21 3d 62 2d 3e 6e 53 69 7a 65 20 29 Size!=b->nSize )
25d40 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 72 return 0;. r
25d50 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 eturn 1;. }. s
25d60 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 tatic void asser
25d70 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 tCellInfo(BtCurs
25d80 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 or *pCur){. C
25d90 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
25da0 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 memset(&info,
25db0 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 0, sizeof(info))
25dc0 3b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 ;. btreeParse
25dd0 43 65 6c 6c 28 70 43 75 72 2d 3e 70 50 61 67 65 Cell(pCur->pPage
25de0 2c 20 70 43 75 72 2d 3e 69 78 2c 20 26 69 6e 66 , pCur->ix, &inf
25df0 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 o);. assert(
25e00 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 63 65 CORRUPT_DB || ce
25e10 6c 6c 49 6e 66 6f 45 71 75 61 6c 28 26 69 6e 66 llInfoEqual(&inf
25e20 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 20 o, &pCur->info)
25e30 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 23 );. }.#else. #
25e40 64 65 66 69 6e 65 20 61 73 73 65 72 74 43 65 6c define assertCel
25e50 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 69 66 0a lInfo(x).#endif.
25e60 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 4e 4f static SQLITE_NO
25e70 49 4e 4c 49 4e 45 20 76 6f 69 64 20 67 65 74 43 INLINE void getC
25e80 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 ellInfo(BtCursor
25e90 20 2a 70 43 75 72 29 7b 0a 20 20 69 66 28 20 70 *pCur){. if( p
25ea0 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
25eb0 3d 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e =0 ){. pCur->
25ec0 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 46 curFlags |= BTCF
25ed0 5f 56 61 6c 69 64 4e 4b 65 79 3b 0a 20 20 20 20 _ValidNKey;.
25ee0 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 btreeParseCell(p
25ef0 43 75 72 2d 3e 70 50 61 67 65 2c 70 43 75 72 2d Cur->pPage,pCur-
25f00 3e 69 78 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 >ix,&pCur->info)
25f10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
25f20 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 ssertCellInfo(pC
25f30 75 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e ur);. }.}..#ifn
25f40 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a 20 54 def NDEBUG /* T
25f50 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e 65 20 he next routine
25f60 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e used only within
25f70 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
25f80 65 6e 74 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52 65 ents */./*.** Re
25f90 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
25fa0 20 67 69 76 65 6e 20 42 74 43 75 72 73 6f 72 20 given BtCursor
25fb0 69 73 20 76 61 6c 69 64 2e 20 20 41 20 76 61 6c is valid. A val
25fc0 69 64 20 63 75 72 73 6f 72 20 69 73 20 6f 6e 65 id cursor is one
25fd0 0a 2a 2a 20 74 68 61 74 20 69 73 20 63 75 72 72 .** that is curr
25fe0 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
25ff0 6f 20 61 20 72 6f 77 20 69 6e 20 61 20 28 6e 6f o a row in a (no
26000 6e 2d 65 6d 70 74 79 29 20 74 61 62 6c 65 2e 0a n-empty) table..
26010 2a 2a 20 54 68 69 73 20 69 73 20 61 20 76 65 72 ** This is a ver
26020 69 66 69 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e ification routin
26030 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 e is used only w
26040 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20 73 ithin assert() s
26050 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 69 6e tatements..*/.in
26060 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
26070 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75 rsorIsValid(BtCu
26080 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 rsor *pCur){. r
26090 65 74 75 72 6e 20 70 43 75 72 20 26 26 20 70 43 eturn pCur && pC
260a0 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
260b0 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64 OR_VALID;.}.#end
260c0 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
260d0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
260e0 43 75 72 73 6f 72 49 73 56 61 6c 69 64 4e 4e 28 CursorIsValidNN(
260f0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
26100 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 . assert( pCur!
26110 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 =0 );. return p
26120 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
26130 53 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a SOR_VALID;.}../*
26140 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 .** Return the v
26150 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 alue of the inte
26160 67 65 72 20 6b 65 79 20 6f 72 20 22 72 6f 77 69 ger key or "rowi
26170 64 22 20 66 6f 72 20 61 20 74 61 62 6c 65 20 62 d" for a table b
26180 74 72 65 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f tree..** This ro
26190 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 76 61 utine is only va
261a0 6c 69 64 20 66 6f 72 20 61 20 63 75 72 73 6f 72 lid for a cursor
261b0 20 74 68 61 74 20 69 73 20 70 6f 69 6e 74 69 6e that is pointin
261c0 67 20 69 6e 74 6f 20 61 0a 2a 2a 20 6f 72 64 69 g into a.** ordi
261d0 6e 61 72 79 20 74 61 62 6c 65 20 62 74 72 65 65 nary table btree
261e0 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
261f0 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 69 6e points to an in
26200 64 65 78 20 62 74 72 65 65 20 6f 72 0a 2a 2a 20 dex btree or.**
26210 69 73 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 20 is invalid, the
26220 72 65 73 75 6c 74 20 6f 66 20 74 68 69 73 20 72 result of this r
26230 6f 75 74 69 6e 65 20 69 73 20 75 6e 64 65 66 69 outine is undefi
26240 6e 65 64 2e 0a 2a 2f 0a 69 36 34 20 73 71 6c 69 ned..*/.i64 sqli
26250 74 65 33 42 74 72 65 65 49 6e 74 65 67 65 72 4b te3BtreeIntegerK
26260 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ey(BtCursor *pCu
26270 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 r){. assert( cu
26280 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
26290 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
262a0 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
262b0 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
262c0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
262d0 63 75 72 49 6e 74 4b 65 79 20 29 3b 0a 20 20 67 curIntKey );. g
262e0 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
262f0 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d ;. return pCur-
26300 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 7d 0a 0a 23 >info.nKey;.}..#
26310 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
26320 42 4c 45 5f 4f 46 46 53 45 54 5f 53 51 4c 5f 46 BLE_OFFSET_SQL_F
26330 55 4e 43 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e UNC./*.** Return
26340 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f the offset into
26350 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
26360 6c 65 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 le for the start
26370 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 79 6c 6f of the.** paylo
26380 61 64 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 ad to which the
26390 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 cursor is pointi
263a0 6e 67 2e 0a 2a 2f 0a 69 36 34 20 73 71 6c 69 74 ng..*/.i64 sqlit
263b0 65 33 42 74 72 65 65 4f 66 66 73 65 74 28 42 74 e3BtreeOffset(Bt
263c0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
263d0 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
263e0 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
263f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
26400 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
26410 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65 74 R_VALID );. get
26420 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a CellInfo(pCur);.
26430 20 20 72 65 74 75 72 6e 20 28 69 36 34 29 70 43 return (i64)pC
26440 75 72 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a ur->pBt->pageSiz
26450 65 2a 28 28 69 36 34 29 70 43 75 72 2d 3e 70 50 e*((i64)pCur->pP
26460 61 67 65 2d 3e 70 67 6e 6f 20 2d 20 31 29 20 2b age->pgno - 1) +
26470 0a 20 20 20 20 20 20 20 20 20 28 69 36 34 29 28 . (i64)(
26480 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c pCur->info.pPayl
26490 6f 61 64 20 2d 20 70 43 75 72 2d 3e 70 50 61 67 oad - pCur->pPag
264a0 65 2d 3e 61 44 61 74 61 29 3b 0a 7d 0a 23 65 6e e->aData);.}.#en
264b0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
264c0 41 42 4c 45 5f 4f 46 46 53 45 54 5f 53 51 4c 5f ABLE_OFFSET_SQL_
264d0 46 55 4e 43 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 FUNC */../*.** R
264e0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
264f0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 79 of bytes of pay
26500 6c 6f 61 64 20 66 6f 72 20 74 68 65 20 65 6e 74 load for the ent
26510 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73 0a ry that pCur is.
26520 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 ** currently poi
26530 6e 74 69 6e 67 20 74 6f 2e 20 20 46 6f 72 20 74 nting to. For t
26540 61 62 6c 65 20 62 74 72 65 65 73 2c 20 74 68 69 able btrees, thi
26550 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 61 6d s will be the am
26560 6f 75 6e 74 0a 2a 2a 20 6f 66 20 64 61 74 61 2e ount.** of data.
26570 20 20 46 6f 72 20 69 6e 64 65 78 20 62 74 72 65 For index btre
26580 65 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 es, this will be
26590 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
265a0 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 key..**.** The
265b0 63 61 6c 6c 65 72 20 6d 75 73 74 20 67 75 61 72 caller must guar
265c0 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 antee that the c
265d0 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
265e0 67 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a g to a non-NULL.
265f0 2a 2a 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 ** valid entry.
26600 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
26610 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f the calling pro
26620 63 65 64 75 72 65 20 6d 75 73 74 20 67 75 61 72 cedure must guar
26630 61 6e 74 65 65 0a 2a 2a 20 74 68 61 74 20 74 68 antee.** that th
26640 65 20 63 75 72 73 6f 72 20 68 61 73 20 43 75 72 e cursor has Cur
26650 73 6f 72 2e 65 53 74 61 74 65 3d 3d 43 55 52 53 sor.eState==CURS
26660 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2f 0a 75 33 32 OR_VALID..*/.u32
26670 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 79 sqlite3BtreePay
26680 6c 6f 61 64 53 69 7a 65 28 42 74 43 75 72 73 6f loadSize(BtCurso
26690 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 r *pCur){. asse
266a0 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
266b0 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
266c0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
266d0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
266e0 49 44 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 ID );. getCellI
266f0 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 72 65 74 nfo(pCur);. ret
26700 75 72 6e 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e urn pCur->info.n
26710 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 2f 2a 0a 2a Payload;.}../*.*
26720 2a 20 52 65 74 75 72 6e 20 61 6e 20 75 70 70 65 * Return an uppe
26730 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 r bound on the s
26740 69 7a 65 20 6f 66 20 61 6e 79 20 72 65 63 6f 72 ize of any recor
26750 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 0a d for the table.
26760 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 73 ** that the curs
26770 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 69 or is pointing i
26780 6e 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 nto..**.** This
26790 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 is an optimizati
267a0 6f 6e 2e 20 20 45 76 65 72 79 74 68 69 6e 67 20 on. Everything
267b0 77 69 6c 6c 20 73 74 69 6c 6c 20 77 6f 72 6b 20 will still work
267c0 69 66 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 if this.** routi
267d0 6e 65 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e ne always return
267e0 73 20 32 31 34 37 34 38 33 36 34 37 20 28 77 68 s 2147483647 (wh
267f0 69 63 68 20 69 73 20 74 68 65 20 6c 61 72 67 65 ich is the large
26800 73 74 20 72 65 63 6f 72 64 0a 2a 2a 20 74 68 61 st record.** tha
26810 74 20 53 51 4c 69 74 65 20 63 61 6e 20 68 61 6e t SQLite can han
26820 64 6c 65 29 20 6f 72 20 6d 6f 72 65 2e 20 20 42 dle) or more. B
26830 75 74 20 72 65 74 75 72 6e 69 6e 67 20 61 20 73 ut returning a s
26840 6d 61 6c 6c 65 72 20 76 61 6c 75 65 20 6d 69 67 maller value mig
26850 68 74 0a 2a 2a 20 70 72 65 76 65 6e 74 20 6c 61 ht.** prevent la
26860 72 67 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 rge memory alloc
26870 61 74 69 6f 6e 73 20 77 68 65 6e 20 74 72 79 69 ations when tryi
26880 6e 67 20 74 6f 20 69 6e 74 65 72 70 72 65 74 20 ng to interpret
26890 61 0a 2a 2a 20 63 6f 72 72 75 70 74 20 64 61 74 a.** corrupt dat
268a0 72 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 rabase..**.** Th
268b0 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d e current implem
268c0 65 6e 74 61 74 69 6f 6e 20 6d 65 72 65 6c 79 20 entation merely
268d0 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 returns the size
268e0 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 of the underlyi
268f0 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ng.** database f
26900 69 6c 65 2e 0a 2a 2f 0a 73 71 6c 69 74 65 33 5f ile..*/.sqlite3_
26910 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 int64 sqlite3Btr
26920 65 65 4d 61 78 52 65 63 6f 72 64 53 69 7a 65 28 eeMaxRecordSize(
26930 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
26940 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
26950 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
26960 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
26970 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
26980 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 72 SOR_VALID );. r
26990 65 74 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 2d eturn pCur->pBt-
269a0 3e 70 61 67 65 53 69 7a 65 20 2a 20 28 73 71 6c >pageSize * (sql
269b0 69 74 65 33 5f 69 6e 74 36 34 29 70 43 75 72 2d ite3_int64)pCur-
269c0 3e 70 42 74 2d 3e 6e 50 61 67 65 3b 0a 7d 0a 0a >pBt->nPage;.}..
269d0 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
269e0 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 page number of a
269f0 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 n overflow page
26a00 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
26a10 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 (parameter.** ov
26a20 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 fl), this functi
26a30 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 on finds the pag
26a40 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
26a50 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
26a60 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 .** linked list
26a70 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 of overflow pag
26a80 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c es. If possible,
26a90 20 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 it uses the aut
26aa0 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e o-vacuum.** poin
26ab0 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 ter-map data ins
26ac0 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 tead of reading
26ad0 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 the content of p
26ae0 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 age ovfl to do s
26af0 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 o. .**.** If an
26b00 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 error occurs an
26b10 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
26b20 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f e is returned. O
26b30 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 therwise:.**.**
26b40 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 The page number
26b50 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 72 of the next over
26b60 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 flow page in the
26b70 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 linked list is
26b80 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a .** written to *
26b90 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 pPgnoNext. If pa
26ba0 67 65 20 6f 76 66 6c 20 69 73 20 74 68 65 20 6c ge ovfl is the l
26bb0 61 73 74 20 70 61 67 65 20 69 6e 20 69 74 73 20 ast page in its
26bc0 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c linked .** list,
26bd0 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 *pPgnoNext is s
26be0 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a et to zero. .**.
26bf0 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20 ** If ppPage is
26c00 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 not NULL, and a
26c10 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
26c20 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 MemPage object
26c30 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
26c40 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 to page number
26c50 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 69 6e pOvfl was obtain
26c60 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 67 65 ed, then *ppPage
26c70 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 is set to point
26c80 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 to that.** refe
26c90 72 65 6e 63 65 2e 20 49 74 20 69 73 20 74 68 65 rence. It is the
26ca0 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 responsibility
26cb0 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f of the caller to
26cc0 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 call releasePag
26cd0 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 e().** on *ppPag
26ce0 65 20 74 6f 20 66 72 65 65 20 74 68 65 20 72 65 e to free the re
26cf0 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 ference. In no r
26d00 65 66 65 72 65 6e 63 65 20 77 61 73 20 6f 62 74 eference was obt
26d10 61 69 6e 65 64 20 28 62 65 63 61 75 73 65 0a 2a ained (because.*
26d20 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 * the pointer-ma
26d30 70 20 77 61 73 20 75 73 65 64 20 74 6f 20 6f 62 p was used to ob
26d40 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 20 66 tain the value f
26d50 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 or *pPgnoNext),
26d60 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 then.** *ppPage
26d70 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a is set to zero..
26d80 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
26d90 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 tOverflowPage(.
26da0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
26db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
26dc0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
26dd0 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 le */. Pgno ovf
26de0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
26df0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
26e00 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 overflow page nu
26e10 6d 62 65 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 mber */. MemPag
26e20 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 e **ppPage,
26e30 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d /* OUT: M
26e40 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 20 28 6d emPage handle (m
26e50 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 ay be NULL) */.
26e60 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 Pgno *pPgnoNext
26e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
26e80 20 4f 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 OUT: Next overf
26e90 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 low page number
26ea0 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 */.){. Pgno nex
26eb0 74 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 t = 0;. MemPage
26ec0 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 *pPage = 0;. i
26ed0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
26ee0 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 K;.. assert( sq
26ef0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
26f00 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
26f10 20 20 61 73 73 65 72 74 28 70 50 67 6e 6f 4e 65 assert(pPgnoNe
26f20 78 74 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 xt);..#ifndef SQ
26f30 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
26f40 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f CUUM. /* Try to
26f50 20 66 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 find the next p
26f60 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 age in the overf
26f70 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74 low list using t
26f80 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 he. ** autovacu
26f90 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 um pointer-map p
26fa0 61 67 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 ages. Guess that
26fb0 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
26fc0 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 n . ** the over
26fd0 66 6c 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 flow list is pag
26fe0 65 20 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 e number (ovfl+1
26ff0 29 2e 20 49 66 20 74 68 61 74 20 67 75 65 73 73 ). If that guess
27000 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 turns . ** out
27010 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 to be wrong, fa
27020 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 ll back to loadi
27030 6e 67 20 74 68 65 20 64 61 74 61 20 6f 66 20 70 ng the data of p
27040 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 age . ** number
27050 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 ovfl to determi
27060 6e 65 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 ne the next page
27070 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 number.. */.
27080 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
27090 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 uum ){. Pgno
270a0 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 pgno;. Pgno i
270b0 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a Guess = ovfl+1;.
270c0 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 u8 eType;..
270d0 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 while( PTRMAP
270e0 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 47 75 _ISPAGE(pBt, iGu
270f0 65 73 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d ess) || iGuess==
27100 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
27110 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
27120 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a iGuess++;. }.
27130 0a 20 20 20 20 69 66 28 20 69 47 75 65 73 73 3c . if( iGuess<
27140 3d 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 =btreePagecount(
27150 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 pBt) ){. rc
27160 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 = ptrmapGet(pBt
27170 2c 20 69 47 75 65 73 73 2c 20 26 65 54 79 70 65 , iGuess, &eType
27180 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 , &pgno);.
27190 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
271a0 4b 20 26 26 20 65 54 79 70 65 3d 3d 50 54 52 4d K && eType==PTRM
271b0 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 AP_OVERFLOW2 &&
271c0 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 pgno==ovfl ){.
271d0 20 20 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75 next = iGu
271e0 65 73 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ess;. rc
271f0 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
27200 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
27210 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
27220 74 28 20 6e 65 78 74 3d 3d 30 20 7c 7c 20 72 63 t( next==0 || rc
27230 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b ==SQLITE_DONE );
27240 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
27250 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
27260 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
27270 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c t, ovfl, &pPage,
27280 20 28 70 70 50 61 67 65 3d 3d 30 29 20 3f 20 50 (ppPage==0) ? P
27290 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c AGER_GET_READONL
272a0 59 20 3a 20 30 29 3b 0a 20 20 20 20 61 73 73 65 Y : 0);. asse
272b0 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
272c0 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 20 29 3b K || pPage==0 );
272d0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
272e0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
272f0 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65 28 next = get4byte(
27300 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 pPage->aData);.
27310 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 }. }.. *pPg
27320 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 noNext = next;.
27330 20 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 if( ppPage ){.
27340 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 *ppPage = pPa
27350 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ge;. }else{.
27360 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
27370 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ge);. }. retur
27380 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f n (rc==SQLITE_DO
27390 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a NE ? SQLITE_OK :
273a0 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rc);.}../*.** C
273b0 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20 opy data from a
273c0 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 65 buffer to a page
273d0 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65 , or from a page
273e0 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a to a buffer..**
273f0 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20 .** pPayload is
27400 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 a pointer to dat
27410 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 a stored on data
27420 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 base page pDbPag
27430 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e e..** If argumen
27440 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 t eOp is false,
27450 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73 then nByte bytes
27460 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 of data are cop
27470 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 ied.** from pPay
27480 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 66 load to the buff
27490 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79 er pointed at by
274a0 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73 pBuf. If eOp is
274b0 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 true,.** then s
274c0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
274d0 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 () is called on
274e0 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74 pDbPage and nByt
274f0 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 e bytes.** of da
27500 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 ta are copied fr
27510 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 om the buffer pB
27520 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a uf to pPayload..
27530 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
27540 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
27550 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73 uccess, otherwis
27560 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e e an error code.
27570 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
27580 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f opyPayload(. vo
27590 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 id *pPayload,
275a0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
275b0 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 20 er to page data
275c0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c */. void *pBuf,
275d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
275e0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 * Pointer to buf
275f0 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 fer */. int nBy
27600 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
27610 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
27620 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f bytes to copy */
27630 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 . int eOp,
27640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
27650 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 0 -> copy from p
27660 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 age, 1 -> copy t
27670 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 o page */. DbPa
27680 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 20 ge *pDbPage
27690 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
276a0 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 ntaining pPayloa
276b0 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f d */.){. if( eO
276c0 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 p ){. /* Copy
276d0 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 data from buffe
276e0 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 69 r to page (a wri
276f0 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f te operation) */
27700 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 . int rc = sq
27710 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
27720 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
27730 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
27740 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
27750 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 rc;. }. me
27760 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 mcpy(pPayload, p
27770 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d Buf, nByte);. }
27780 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 else{. /* Cop
27790 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65 y data from page
277a0 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 65 to buffer (a re
277b0 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f ad operation) */
277c0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66 . memcpy(pBuf
277d0 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 , pPayload, nByt
277e0 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
277f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
27800 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
27810 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 on is used to re
27820 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20 ad or overwrite
27830 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 payload informat
27840 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 ion.** for the e
27850 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 43 ntry that the pC
27860 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 ur cursor is poi
27870 6e 74 69 6e 67 20 74 6f 2e 20 54 68 65 20 65 4f nting to. The eO
27880 70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 p.** argument is
27890 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
278a0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
278b0 20 30 3a 20 54 68 65 20 6f 70 65 72 61 74 69 6f 0: The operatio
278c0 6e 20 69 73 20 61 20 72 65 61 64 2e 20 50 6f 70 n is a read. Pop
278d0 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c ulate the overfl
278e0 6f 77 20 63 61 63 68 65 2e 0a 2a 2a 20 20 20 31 ow cache..** 1
278f0 3a 20 54 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 : The operation
27900 69 73 20 61 20 77 72 69 74 65 2e 20 50 6f 70 75 is a write. Popu
27910 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f late the overflo
27920 77 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 41 w cache..**.** A
27930 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20 total of "amt"
27940 62 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f bytes are read o
27950 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e r written beginn
27960 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e ing at "offset".
27970 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64 .** Data is read
27980 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20 to or from the
27990 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a buffer pBuf..**.
279a0 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 62 ** The content b
279b0 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 69 eing read or wri
279c0 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 61 tten might appea
279d0 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61 r on the main pa
279e0 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 74 ge.** or be scat
279f0 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c tered out on mul
27a00 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 70 tiple overflow p
27a10 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ages..**.** If t
27a20 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
27a30 72 20 65 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 r entry uses one
27a40 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f or more overflo
27a50 77 20 70 61 67 65 73 0a 2a 2a 20 74 68 69 73 20 w pages.** this
27a60 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 61 6c 6c function may all
27a70 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
27a80 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c and lazily popul
27a90 61 74 65 0a 2a 2a 20 74 68 65 20 6f 76 65 72 66 ate.** the overf
27aa0 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
27ab0 63 68 65 20 61 72 72 61 79 20 28 42 74 43 75 72 che array (BtCur
27ac0 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 sor.aOverflow).
27ad0 0a 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 63 .** Subsequent c
27ae0 61 6c 6c 73 20 75 73 65 20 74 68 69 73 20 63 61 alls use this ca
27af0 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b che to make seek
27b00 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c ing to the suppl
27b10 69 65 64 20 6f 66 66 73 65 74 20 0a 2a 2a 20 6d ied offset .** m
27b20 6f 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a ore efficient..*
27b30 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 *.** Once an ove
27b40 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
27b50 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 cache has been a
27b60 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 75 73 llocated, it mus
27b70 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 t be.** invalida
27b80 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 ted if some othe
27b90 72 20 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 r cursor writes
27ba0 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c to the same tabl
27bb0 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 e, or if.** the
27bc0 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 cursor is moved
27bd0 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 to a different r
27be0 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 ow. Additionally
27bf0 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d , in auto-vacuum
27c00 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f .** mode, the fo
27c10 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d llowing events m
27c20 61 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e ay invalidate an
27c30 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
27c40 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a ist cache..**.**
27c50 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e * An incremen
27c60 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 tal vacuum,.**
27c70 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 * A commit in a
27c80 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c uto_vacuum="full
27c90 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 " mode,.** * C
27ca0 72 65 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 reating a table
27cb0 28 6d 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 (may require mov
27cc0 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 ing an overflow
27cd0 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 page)..*/.static
27ce0 20 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f int accessPaylo
27cf0 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a ad(. BtCursor *
27d00 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 pCur, /* Cu
27d10 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
27d20 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 entry to read f
27d30 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 rom */. u32 off
27d40 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a set, /*
27d50 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 Begin reading t
27d60 68 69 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 his far into pay
27d70 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d load */. u32 am
27d80 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t, /
27d90 2a 20 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 * Read this many
27da0 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 bytes */. unsi
27db0 67 6e 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c gned char *pBuf,
27dc0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 /* Write the by
27dd0 74 65 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 tes into this bu
27de0 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 ffer */ . int e
27df0 4f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Op
27e00 2f 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e /* zero to read.
27e10 20 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 non-zero to wri
27e20 74 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 te. */.){. unsi
27e30 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c gned char *aPayl
27e40 6f 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 oad;. int rc =
27e50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
27e60 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d iIdx = 0;. Mem
27e70 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 Page *pPage = pC
27e80 75 72 2d 3e 70 50 61 67 65 3b 20 20 20 20 20 20 ur->pPage;
27e90 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 /* Btre
27ea0 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 65 6e e page of curren
27eb0 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 74 53 t entry */. BtS
27ec0 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 hared *pBt = pCu
27ed0 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 r->pBt;
27ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 /* Btre
27ef0 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 e this cursor be
27f00 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 23 69 66 64 longs to */.#ifd
27f10 65 66 20 53 51 4c 49 54 45 5f 44 49 52 45 43 54 ef SQLITE_DIRECT
27f20 5f 4f 56 45 52 46 4c 4f 57 5f 52 45 41 44 0a 20 _OVERFLOW_READ.
27f30 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
27f40 20 63 6f 6e 73 74 20 70 42 75 66 53 74 61 72 74 const pBufStart
27f50 20 3d 20 70 42 75 66 3b 20 20 20 20 20 2f 2a 20 = pBuf; /*
27f60 53 74 61 72 74 20 6f 66 20 6f 72 69 67 69 6e 61 Start of origina
27f70 6c 20 6f 75 74 20 62 75 66 66 65 72 20 2a 2f 0a l out buffer */.
27f80 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 #endif.. assert
27f90 28 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 ( pPage );. ass
27fa0 65 72 74 28 20 65 4f 70 3d 3d 30 20 7c 7c 20 65 ert( eOp==0 || e
27fb0 4f 70 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 Op==1 );. asser
27fc0 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
27fd0 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
27fe0 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
27ff0 3e 69 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c >ix<pPage->nCell
28000 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 );. assert( cu
28010 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
28020 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 Cur) );.. getCe
28030 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 llInfo(pCur);.
28040 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d aPayload = pCur-
28050 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 64 3b 0a >info.pPayload;.
28060 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 assert( offset
28070 2b 61 6d 74 20 3c 3d 20 70 43 75 72 2d 3e 69 6e +amt <= pCur->in
28080 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 0a fo.nPayload );..
28090 20 20 61 73 73 65 72 74 28 20 61 50 61 79 6c 6f assert( aPaylo
280a0 61 64 20 3e 20 70 50 61 67 65 2d 3e 61 44 61 74 ad > pPage->aDat
280b0 61 20 29 3b 0a 20 20 69 66 28 20 28 75 70 74 72 a );. if( (uptr
280c0 29 28 61 50 61 79 6c 6f 61 64 20 2d 20 70 50 61 )(aPayload - pPa
280d0 67 65 2d 3e 61 44 61 74 61 29 20 3e 20 28 70 42 ge->aData) > (pB
280e0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
280f0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
28100 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 l) ){. /* Try
28110 69 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 ing to read or w
28120 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e rite past the en
28130 64 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 d of the data is
28140 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 0a an error. The.
28150 20 20 20 20 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e ** condition
28160 61 6c 20 61 62 6f 76 65 20 69 73 20 72 65 61 6c al above is real
28170 6c 79 3a 0a 20 20 20 20 2a 2a 20 20 20 20 26 61 ly:. ** &a
28180 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e Payload[pCur->in
28190 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 fo.nLocal] > &pP
281a0 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
281b0 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 20 20 usableSize].
281c0 2a 2a 20 62 75 74 20 69 73 20 72 65 63 61 73 74 ** but is recast
281d0 20 69 6e 74 6f 20 69 74 73 20 63 75 72 72 65 6e into its curren
281e0 74 20 66 6f 72 6d 20 74 6f 20 61 76 6f 69 64 20 t form to avoid
281f0 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 integer overflow
28200 20 70 72 6f 62 6c 65 6d 73 0a 20 20 20 20 2a 2f problems. */
28210 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
28220 54 45 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 28 TE_CORRUPT_PAGE(
28230 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f pPage);. }.. /
28240 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 * Check if data
28250 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 must be read/wri
28260 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 tten to/from the
28270 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73 65 btree page itse
28280 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 lf. */. if( off
28290 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e set<pCur->info.n
282a0 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 Local ){. int
282b0 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 a = amt;. if
282c0 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d ( a+offset>pCur-
282d0 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a >info.nLocal ){.
282e0 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e a = pCur->
282f0 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 info.nLocal - of
28300 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 fset;. }.
28310 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 rc = copyPayload
28320 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 (&aPayload[offse
28330 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 t], pBuf, a, eOp
28340 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 , pPage->pDbPage
28350 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 );. offset =
28360 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 0;. pBuf += a
28370 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a ;. amt -= a;.
28380 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 }else{. off
28390 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 set -= pCur->inf
283a0 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 0a o.nLocal;. }...
283b0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
283c0 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a _OK && amt>0 ){.
283d0 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 const u32 ov
283e0 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 flSize = pBt->us
283f0 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f ableSize - 4; /
28400 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 * Bytes content
28410 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f per ovfl page */
28420 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 . Pgno nextPa
28430 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 ge;.. nextPag
28440 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 e = get4byte(&aP
28450 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 ayload[pCur->inf
28460 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 20 20 20 o.nLocal]);..
28470 20 2f 2a 20 49 66 20 74 68 65 20 42 74 43 75 72 /* If the BtCur
28480 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 sor.aOverflow[]
28490 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c has not been all
284a0 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61 74 65 ocated, allocate
284b0 20 69 74 20 6e 6f 77 2e 0a 20 20 20 20 2a 2a 0a it now.. **.
284c0 20 20 20 20 2a 2a 20 54 68 65 20 61 4f 76 65 72 ** The aOver
284d0 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 69 73 20 flow[] array is
284e0 73 69 7a 65 64 20 61 74 20 6f 6e 65 20 65 6e 74 sized at one ent
284f0 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 ry for each over
28500 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 2a 2a flow page. **
28510 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 in the overflow
28520 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 chain. The page
28530 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 number of the f
28540 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 irst overflow pa
28550 67 65 20 69 73 0a 20 20 20 20 2a 2a 20 73 74 6f ge is. ** sto
28560 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 red in aOverflow
28570 5b 30 5d 2c 20 65 74 63 2e 20 41 20 76 61 6c 75 [0], etc. A valu
28580 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f e of 0 in the aO
28590 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 0a verflow[] array.
285a0 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20 22 6e 6f ** means "no
285b0 74 20 79 65 74 20 6b 6e 6f 77 6e 22 20 28 74 68 t yet known" (th
285c0 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c e cache is lazil
285d0 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 y populated)..
285e0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 70 43 */. if( (pC
285f0 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 ur->curFlags & B
28600 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3d 3d TCF_ValidOvfl)==
28610 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 0 ){. int n
28620 4f 76 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e Ovfl = (pCur->in
28630 66 6f 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 fo.nPayload-pCur
28640 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 ->info.nLocal+ov
28650 66 6c 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 flSize-1)/ovflSi
28660 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 ze;. if( pC
28670 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 3d 3d 30 ur->aOverflow==0
28680 0a 20 20 20 20 20 20 20 7c 7c 20 6e 4f 76 66 6c . || nOvfl
28690 2a 28 69 6e 74 29 73 69 7a 65 6f 66 28 50 67 6e *(int)sizeof(Pgn
286a0 6f 29 20 3e 20 73 71 6c 69 74 65 33 4d 61 6c 6c o) > sqlite3Mall
286b0 6f 63 53 69 7a 65 28 70 43 75 72 2d 3e 61 4f 76 ocSize(pCur->aOv
286c0 65 72 66 6c 6f 77 29 0a 20 20 20 20 20 20 29 7b erflow). ){
286d0 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 2a 61 . Pgno *a
286e0 4e 65 77 20 3d 20 28 50 67 6e 6f 2a 29 73 71 6c New = (Pgno*)sql
286f0 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 ite3Realloc(.
28700 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 pCur->a
28710 4f 76 65 72 66 6c 6f 77 2c 20 6e 4f 76 66 6c 2a Overflow, nOvfl*
28720 32 2a 73 69 7a 65 6f 66 28 50 67 6e 6f 29 0a 20 2*sizeof(Pgno).
28730 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
28740 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b if( aNew==0 ){
28750 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
28760 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 n SQLITE_NOMEM_B
28770 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c KPT;. }el
28780 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 se{. pC
28790 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 ur->aOverflow =
287a0 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 0a aNew;. }.
287b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 }. me
287c0 6d 73 65 74 28 70 43 75 72 2d 3e 61 4f 76 65 72 mset(pCur->aOver
287d0 66 6c 6f 77 2c 20 30 2c 20 6e 4f 76 66 6c 2a 73 flow, 0, nOvfl*s
287e0 69 7a 65 6f 66 28 50 67 6e 6f 29 29 3b 0a 20 20 izeof(Pgno));.
287f0 20 20 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 pCur->curFla
28800 67 73 20 7c 3d 20 42 54 43 46 5f 56 61 6c 69 64 gs |= BTCF_Valid
28810 4f 76 66 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Ovfl;. }else{
28820 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
28830 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
28840 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 ist cache has be
28850 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
28860 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 65 6e the. ** en
28870 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 try for the firs
28880 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 t required overf
28890 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 low page is vali
288a0 64 2c 20 73 6b 69 70 0a 20 20 20 20 20 20 2a 2a d, skip. **
288b0 20 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e directly to it.
288c0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
288d0 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 if( pCur->aOverf
288e0 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 low[offset/ovflS
288f0 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 ize] ){.
28900 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f iIdx = (offset/o
28910 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 vflSize);.
28920 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 nextPage = pCu
28930 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 r->aOverflow[iId
28940 78 5d 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 x];. offs
28950 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 et = (offset%ovf
28960 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a lSize);. }.
28970 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 }.. asser
28980 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
28990 20 26 26 20 61 6d 74 3e 30 20 29 3b 0a 20 20 20 && amt>0 );.
289a0 20 77 68 69 6c 65 28 20 6e 65 78 74 50 61 67 65 while( nextPage
289b0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
289c0 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 required, popula
289d0 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 te the overflow
289e0 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e page-list cache.
289f0 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
28a00 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f ( pCur->aOverflo
28a10 77 5b 69 49 64 78 5d 3d 3d 30 0a 20 20 20 20 20 w[iIdx]==0.
28a20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 75 72 || pCur
28a30 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
28a40 5d 3d 3d 6e 65 78 74 50 61 67 65 0a 20 20 20 20 ]==nextPage.
28a50 20 20 20 20 20 20 20 20 20 20 7c 7c 20 43 4f 52 || COR
28a60 52 55 50 54 5f 44 42 20 29 3b 0a 20 20 20 20 20 RUPT_DB );.
28a70 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
28a80 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 [iIdx] = nextPag
28a90 65 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66 e;.. if( of
28aa0 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 fset>=ovflSize )
28ab0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
28ac0 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 only reason to
28ad0 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69 read this page i
28ae0 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 s to obtain the
28af0 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 page. **
28b00 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e number for the n
28b10 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ext page in the
28b20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 overflow chain.
28b30 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 The page.
28b40 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20 ** data is not
28b50 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72 required. So fir
28b60 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 st try to lookup
28b70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 the overflow.
28b80 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 ** page-li
28b90 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 st cache, if any
28ba0 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b , then fall back
28bb0 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 to the getOverf
28bc0 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20 lowPage().
28bd0 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 ** function..
28be0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
28bf0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
28c00 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f curFlags & BTCF_
28c10 56 61 6c 69 64 4f 76 66 6c 20 29 3b 0a 20 20 20 ValidOvfl );.
28c20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
28c30 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3d 3d 70 r->pBtree->db==p
28c40 42 74 2d 3e 64 62 20 29 3b 0a 20 20 20 20 20 20 Bt->db );.
28c50 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 if( pCur->aOve
28c60 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b rflow[iIdx+1] ){
28c70 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 . nextP
28c80 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 age = pCur->aOve
28c90 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 rflow[iIdx+1];.
28ca0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
28cb0 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 rc = get
28cc0 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 OverflowPage(pBt
28cd0 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 , nextPage, 0, &
28ce0 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 nextPage);.
28cf0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6f 66 66 }. off
28d00 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b set -= ovflSize;
28d10 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
28d20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f /* Need to
28d30 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 read this page
28d40 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e properly. It con
28d50 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 tains some of th
28d60 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e e. ** ran
28d70 67 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 ge of data that
28d80 69 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 is being read (e
28d90 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 Op==0) or writte
28da0 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 n (eOp!=0)..
28db0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 */. i
28dc0 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 nt a = amt;.
28dd0 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 if( a + offs
28de0 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b et > ovflSize ){
28df0 0a 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f . a = o
28e00 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 vflSize - offset
28e10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 23 69 66 ;. }..#if
28e20 64 65 66 20 53 51 4c 49 54 45 5f 44 49 52 45 43 def SQLITE_DIREC
28e30 54 5f 4f 56 45 52 46 4c 4f 57 5f 52 45 41 44 0a T_OVERFLOW_READ.
28e40 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6c /* If al
28e50 6c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 l the following
28e60 61 72 65 20 74 72 75 65 3a 0a 20 20 20 20 20 20 are true:.
28e70 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
28e80 20 20 31 29 20 74 68 69 73 20 69 73 20 61 20 72 1) this is a r
28e90 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 2c 20 61 ead operation, a
28ea0 6e 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 nd . **
28eb0 20 32 29 20 64 61 74 61 20 69 73 20 72 65 71 75 2) data is requ
28ec0 69 72 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 ired from the st
28ed0 61 72 74 20 6f 66 20 74 68 69 73 20 6f 76 65 72 art of this over
28ee0 66 6c 6f 77 20 70 61 67 65 2c 20 61 6e 64 0a 20 flow page, and.
28ef0 20 20 20 20 20 20 20 2a 2a 20 20 20 33 29 20 74 ** 3) t
28f00 68 65 72 65 20 61 72 65 20 6e 6f 20 64 69 72 74 here are no dirt
28f10 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 y pages in the p
28f20 61 67 65 2d 63 61 63 68 65 0a 20 20 20 20 20 20 age-cache.
28f30 20 20 2a 2a 20 20 20 34 29 20 74 68 65 20 64 61 ** 4) the da
28f40 74 61 62 61 73 65 20 69 73 20 66 69 6c 65 2d 62 tabase is file-b
28f50 61 63 6b 65 64 2c 20 61 6e 64 0a 20 20 20 20 20 acked, and.
28f60 20 20 20 2a 2a 20 20 20 35 29 20 74 68 65 20 70 ** 5) the p
28f70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 age is not in th
28f80 65 20 57 41 4c 20 66 69 6c 65 0a 20 20 20 20 20 e WAL file.
28f90 20 20 20 2a 2a 20 20 20 36 29 20 61 74 20 6c 65 ** 6) at le
28fa0 61 73 74 20 34 20 62 79 74 65 73 20 68 61 76 65 ast 4 bytes have
28fb0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65 already been re
28fc0 61 64 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 ad into the outp
28fd0 75 74 20 62 75 66 66 65 72 20 0a 20 20 20 20 20 ut buffer .
28fe0 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
28ff0 20 74 68 65 6e 20 64 61 74 61 20 63 61 6e 20 62 then data can b
29000 65 20 72 65 61 64 20 64 69 72 65 63 74 6c 79 20 e read directly
29010 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
29020 65 20 66 69 6c 65 20 69 6e 74 6f 20 74 68 65 0a e file into the.
29030 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 74 70 75 ** outpu
29040 74 20 62 75 66 66 65 72 2c 20 62 79 70 61 73 73 t buffer, bypass
29050 69 6e 67 20 74 68 65 20 70 61 67 65 2d 63 61 63 ing the page-cac
29060 68 65 20 61 6c 74 6f 67 65 74 68 65 72 2e 20 54 he altogether. T
29070 68 69 73 20 73 70 65 65 64 73 0a 20 20 20 20 20 his speeds.
29080 20 20 20 2a 2a 20 75 70 20 6c 6f 61 64 69 6e 67 ** up loading
29090 20 6c 61 72 67 65 20 72 65 63 6f 72 64 73 20 74 large records t
290a0 68 61 74 20 73 70 61 6e 20 6d 61 6e 79 20 6f 76 hat span many ov
290b0 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 20 20 erflow pages..
290c0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
290d0 20 69 66 28 20 65 4f 70 3d 3d 30 20 20 20 20 20 if( eOp==0
290e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
290f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29100 20 20 20 20 20 20 20 20 2f 2a 20 28 31 29 20 2a /* (1) *
29110 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 6f 66 /. && of
29120 66 73 65 74 3d 3d 30 20 20 20 20 20 20 20 20 20 fset==0
29130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29150 20 2f 2a 20 28 32 29 20 2a 2f 0a 20 20 20 20 20 /* (2) */.
29160 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 && sqlite3Pa
29170 67 65 72 44 69 72 65 63 74 52 65 61 64 4f 6b 28 gerDirectReadOk(
29180 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 pBt->pPager, nex
29190 74 50 61 67 65 29 20 20 20 20 2f 2a 20 28 33 2c tPage) /* (3,
291a0 34 2c 35 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4,5) */.
291b0 20 26 26 20 26 70 42 75 66 5b 2d 34 5d 3e 3d 70 && &pBuf[-4]>=p
291c0 42 75 66 53 74 61 72 74 20 20 20 20 20 20 20 20 BufStart
291d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
291e0 20 20 20 20 20 20 20 2f 2a 20 28 36 29 20 2a 2f /* (6) */
291f0 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
29200 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 sqlite3_fi
29210 6c 65 20 2a 66 64 20 3d 20 73 71 6c 69 74 65 33 le *fd = sqlite3
29220 50 61 67 65 72 46 69 6c 65 28 70 42 74 2d 3e 70 PagerFile(pBt->p
29230 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 Pager);.
29240 20 20 75 38 20 61 53 61 76 65 5b 34 5d 3b 0a 20 u8 aSave[4];.
29250 20 20 20 20 20 20 20 20 20 75 38 20 2a 61 57 72 u8 *aWr
29260 69 74 65 20 3d 20 26 70 42 75 66 5b 2d 34 5d 3b ite = &pBuf[-4];
29270 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
29280 74 28 20 61 57 72 69 74 65 3e 3d 70 42 75 66 53 t( aWrite>=pBufS
29290 74 61 72 74 20 29 3b 20 20 20 20 20 20 20 20 20 tart );
292a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
292b0 2f 2a 20 64 75 65 20 74 6f 20 28 36 29 20 2a 2f /* due to (6) */
292c0 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
292d0 79 28 61 53 61 76 65 2c 20 61 57 72 69 74 65 2c y(aSave, aWrite,
292e0 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 4);. r
292f0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
29300 64 28 66 64 2c 20 61 57 72 69 74 65 2c 20 61 2b d(fd, aWrite, a+
29310 34 2c 20 28 69 36 34 29 70 42 74 2d 3e 70 61 67 4, (i64)pBt->pag
29320 65 53 69 7a 65 2a 28 6e 65 78 74 50 61 67 65 2d eSize*(nextPage-
29330 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 1));. n
29340 65 78 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 extPage = get4by
29350 74 65 28 61 57 72 69 74 65 29 3b 0a 20 20 20 20 te(aWrite);.
29360 20 20 20 20 20 20 6d 65 6d 63 70 79 28 61 57 72 memcpy(aWr
29370 69 74 65 2c 20 61 53 61 76 65 2c 20 34 29 3b 0a ite, aSave, 4);.
29380 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 }else.#e
29390 6e 64 69 66 0a 0a 20 20 20 20 20 20 20 20 7b 0a ndif.. {.
293a0 20 20 20 20 20 20 20 20 20 20 44 62 50 61 67 65 DbPage
293b0 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 *pDbPage;.
293c0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
293d0 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 3PagerGet(pBt->p
293e0 50 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c Pager, nextPage,
293f0 20 26 70 44 62 50 61 67 65 2c 0a 20 20 20 20 20 &pDbPage,.
29400 20 20 20 20 20 20 20 20 20 28 65 4f 70 3d 3d 30 (eOp==0
29410 20 3f 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 ? PAGER_GET_REA
29420 44 4f 4e 4c 59 20 3a 20 30 29 0a 20 20 20 20 20 DONLY : 0).
29430 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 );.
29440 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
29450 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
29460 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 aPayload = sq
29470 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
29480 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 a(pDbPage);.
29490 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 nextPage
294a0 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61 79 = get4byte(aPay
294b0 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20 20 load);.
294c0 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c rc = copyPayl
294d0 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 oad(&aPayload[of
294e0 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 fset+4], pBuf, a
294f0 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b , eOp, pDbPage);
29500 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
29510 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
29520 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
29530 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b offset = 0;
29540 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
29550 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
29560 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 mt -= a;.
29570 20 69 66 28 20 61 6d 74 3d 3d 30 20 29 20 72 65 if( amt==0 ) re
29580 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
29590 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 pBuf += a;.
295a0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
295b0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
295c0 69 49 64 78 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 iIdx++;. }.
295d0 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }.. if( rc==SQL
295e0 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 ITE_OK && amt>0
295f0 29 7b 0a 20 20 20 20 2f 2a 20 4f 76 65 72 66 6c ){. /* Overfl
29600 6f 77 20 63 68 61 69 6e 20 65 6e 64 73 20 70 72 ow chain ends pr
29610 65 6d 61 74 75 72 65 6c 79 20 2a 2f 0a 20 20 20 ematurely */.
29620 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
29630 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 ORRUPT_PAGE(pPag
29640 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
29650 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
29660 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 ad part of the p
29670 61 79 6c 6f 61 64 20 66 6f 72 20 74 68 65 20 72 ayload for the r
29680 6f 77 20 61 74 20 77 68 69 63 68 20 74 68 61 74 ow at which that
29690 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 cursor pCur is
296a0 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 70 6f 69 currently.** poi
296b0 6e 74 69 6e 67 2e 20 20 22 61 6d 74 22 20 62 79 nting. "amt" by
296c0 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 6e tes will be tran
296d0 73 66 65 72 72 65 64 20 69 6e 74 6f 20 70 42 75 sferred into pBu
296e0 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 f[]. The transf
296f0 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 er.** begins at
29700 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 "offset"..**.**
29710 70 43 75 72 20 63 61 6e 20 62 65 20 70 6f 69 6e pCur can be poin
29720 74 69 6e 67 20 74 6f 20 65 69 74 68 65 72 20 61 ting to either a
29730 20 74 61 62 6c 65 20 6f 72 20 61 6e 20 69 6e 64 table or an ind
29740 65 78 20 62 2d 74 72 65 65 2e 0a 2a 2a 20 49 66 ex b-tree..** If
29750 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 74 pointing to a t
29760 61 62 6c 65 20 62 74 72 65 65 2c 20 74 68 65 6e able btree, then
29770 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 73 65 63 the content sec
29780 74 69 6f 6e 20 69 73 20 72 65 61 64 2e 20 20 49 tion is read. I
29790 66 0a 2a 2a 20 70 43 75 72 20 69 73 20 70 6f 69 f.** pCur is poi
297a0 6e 74 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 nting to an inde
297b0 78 20 62 2d 74 72 65 65 20 74 68 65 6e 20 74 68 x b-tree then th
297c0 65 20 6b 65 79 20 73 65 63 74 69 6f 6e 20 69 73 e key section is
297d0 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 read..**.** For
297e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 79 sqlite3BtreePay
297f0 6c 6f 61 64 28 29 2c 20 74 68 65 20 63 61 6c 6c load(), the call
29800 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
29810 68 61 74 20 70 43 75 72 20 69 73 20 70 6f 69 6e hat pCur is poin
29820 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c ting.** to a val
29830 69 64 20 72 6f 77 20 69 6e 20 74 68 65 20 74 61 id row in the ta
29840 62 6c 65 2e 20 20 46 6f 72 20 73 71 6c 69 74 65 ble. For sqlite
29850 33 42 74 72 65 65 50 61 79 6c 6f 61 64 43 68 65 3BtreePayloadChe
29860 63 6b 65 64 28 29 2c 20 74 68 65 0a 2a 2a 20 63 cked(), the.** c
29870 75 72 73 6f 72 20 6d 69 67 68 74 20 62 65 20 69 ursor might be i
29880 6e 76 61 6c 69 64 20 6f 72 20 6d 69 67 68 74 20 nvalid or might
29890 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 74 6f need to be resto
298a0 72 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 red before being
298b0 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 read..**.** Ret
298c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
298d0 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 success or an e
298e0 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 rror code if any
298f0 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
29900 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 ong. An error i
29910 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f s returned if "o
29920 66 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 ffset+amt" is la
29930 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 rger than.** the
29940 20 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f available paylo
29950 61 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ad..*/.int sqlit
29960 65 33 42 74 72 65 65 50 61 79 6c 6f 61 64 28 42 e3BtreePayload(B
29970 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
29980 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
29990 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b mt, void *pBuf){
299a0 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
299b0 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
299c0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
299d0 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
299e0 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
299f0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 ssert( pCur->iPa
29a00 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 70 ge>=0 && pCur->p
29a10 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Page );. assert
29a20 28 20 70 43 75 72 2d 3e 69 78 3c 70 43 75 72 2d ( pCur->ix<pCur-
29a30 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b >pPage->nCell );
29a40 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 . return access
29a50 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 Payload(pCur, of
29a60 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 fset, amt, (unsi
29a70 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c gned char*)pBuf,
29a80 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 0);.}../*.** Th
29a90 69 73 20 76 61 72 69 61 6e 74 20 6f 66 20 73 71 is variant of sq
29aa0 6c 69 74 65 33 42 74 72 65 65 50 61 79 6c 6f 61 lite3BtreePayloa
29ab0 64 28 29 20 77 6f 72 6b 73 20 65 76 65 6e 20 69 d() works even i
29ac0 66 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 f the cursor has
29ad0 20 6e 6f 74 0a 2a 2a 20 69 6e 20 74 68 65 20 43 not.** in the C
29ae0 55 52 53 4f 52 5f 56 41 4c 49 44 20 73 74 61 74 URSOR_VALID stat
29af0 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 75 e. It is only u
29b00 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 sed by the sqlit
29b10 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 0a 2a e3_blob_read().*
29b20 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a * interface..*/.
29b30 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
29b40 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 73 74 61 MIT_INCRBLOB.sta
29b50 74 69 63 20 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c tic SQLITE_NOINL
29b60 49 4e 45 20 69 6e 74 20 61 63 63 65 73 73 50 61 INE int accessPa
29b70 79 6c 6f 61 64 43 68 65 63 6b 65 64 28 0a 20 20 yloadChecked(.
29b80 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 0a BtCursor *pCur,.
29b90 20 20 75 33 32 20 6f 66 66 73 65 74 2c 0a 20 20 u32 offset,.
29ba0 75 33 32 20 61 6d 74 2c 0a 20 20 76 6f 69 64 20 u32 amt,. void
29bb0 2a 70 42 75 66 0a 29 7b 0a 20 20 69 6e 74 20 72 *pBuf.){. int r
29bc0 63 3b 0a 20 20 69 66 20 28 20 70 43 75 72 2d 3e c;. if ( pCur->
29bd0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
29be0 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 NVALID ){. re
29bf0 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 turn SQLITE_ABOR
29c00 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 T;. }. assert(
29c10 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 cursorOwnsBtSha
29c20 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 72 red(pCur) );. r
29c30 63 20 3d 20 62 74 72 65 65 52 65 73 74 6f 72 65 c = btreeRestore
29c40 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
29c50 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 Cur);. return r
29c60 63 20 3f 20 72 63 20 3a 20 61 63 63 65 73 73 50 c ? rc : accessP
29c70 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 ayload(pCur, off
29c80 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 2c 20 set, amt, pBuf,
29c90 30 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 0);.}.int sqlite
29ca0 33 42 74 72 65 65 50 61 79 6c 6f 61 64 43 68 65 3BtreePayloadChe
29cb0 63 6b 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70 cked(BtCursor *p
29cc0 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c Cur, u32 offset,
29cd0 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a u32 amt, void *
29ce0 70 42 75 66 29 7b 0a 20 20 69 66 28 20 70 43 75 pBuf){. if( pCu
29cf0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
29d00 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 61 R_VALID ){. a
29d10 73 73 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e ssert( cursorOwn
29d20 73 42 74 53 68 61 72 65 64 28 70 43 75 72 29 20 sBtShared(pCur)
29d30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 63 );. return ac
29d40 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 cessPayload(pCur
29d50 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 , offset, amt, p
29d60 42 75 66 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 Buf, 0);. }else
29d70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 63 63 {. return acc
29d80 65 73 73 50 61 79 6c 6f 61 64 43 68 65 63 6b 65 essPayloadChecke
29d90 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 d(pCur, offset,
29da0 61 6d 74 2c 20 70 42 75 66 29 3b 0a 20 20 7d 0a amt, pBuf);. }.
29db0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
29dc0 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
29dd0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 */../*.** Retur
29de0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 n a pointer to p
29df0 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 ayload informati
29e00 6f 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 on from the entr
29e10 79 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 y that the .** p
29e20 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f Cur cursor is po
29e30 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 inting to. The
29e40 70 6f 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 pointer is to th
29e50 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a e beginning of.*
29e60 2a 20 74 68 65 20 6b 65 79 20 69 66 20 69 6e 64 * the key if ind
29e70 65 78 20 62 74 72 65 65 73 20 28 70 50 61 67 65 ex btrees (pPage
29e80 2d 3e 69 6e 74 4b 65 79 3d 3d 30 29 20 61 6e 64 ->intKey==0) and
29e90 20 69 73 20 74 68 65 20 64 61 74 61 20 66 6f 72 is the data for
29ea0 0a 2a 2a 20 74 61 62 6c 65 20 62 74 72 65 65 73 .** table btrees
29eb0 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d (pPage->intKey=
29ec0 3d 31 29 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 =1). The number
29ed0 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 of bytes of avai
29ee0 6c 61 62 6c 65 0a 2a 2a 20 6b 65 79 2f 64 61 74 lable.** key/dat
29ef0 61 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 a is written int
29f00 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 o *pAmt. If *pA
29f10 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 mt==0, then the
29f20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 value.** returne
29f30 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 d will not be a
29f40 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a valid pointer..*
29f50 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
29f60 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 e is an optimiza
29f70 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d tion. It is com
29f80 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69 mon for the enti
29f90 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 re key.** and da
29fa0 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 ta to fit on the
29fb0 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e 64 20 local page and
29fc0 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 for there to be
29fd0 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 no overflow.** p
29fe0 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68 61 74 ages. When that
29ff0 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 is so, this rou
2a000 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 tine can be used
2a010 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 0a 2a to access the.*
2a020 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 77 * key and data w
2a030 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 ithout making a
2a040 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20 6b 65 copy. If the ke
2a050 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 y and/or data sp
2a060 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 ills.** onto ove
2a070 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 65 rflow pages, the
2a080 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 n accessPayload(
2a090 29 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 74 ) must be used t
2a0a0 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a 2a 2a 20 o reassemble.**
2a0b0 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 the key/data and
2a0c0 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20 61 20 copy it into a
2a0d0 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 preallocated buf
2a0e0 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 fer..**.** The p
2a0f0 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 ointer returned
2a100 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 by this routine
2a110 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79 20 69 looks directly i
2a120 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64 0a 2a nto the cached.*
2a130 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 * page of the da
2a140 74 61 62 61 73 65 2e 20 20 54 68 65 20 64 61 74 tabase. The dat
2a150 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 6f a might change o
2a160 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78 74 20 r move the next
2a170 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 time.** any btre
2a180 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c e routine is cal
2a190 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 led..*/.static c
2a1a0 6f 6e 73 74 20 76 6f 69 64 20 2a 66 65 74 63 68 onst void *fetch
2a1b0 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 Payload(. BtCur
2a1c0 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
2a1d0 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 /* Cursor pointi
2a1e0 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 ng to entry to r
2a1f0 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 ead from */. u3
2a200 32 20 2a 70 41 6d 74 20 20 20 20 20 20 20 20 20 2 *pAmt
2a210 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
2a220 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 number of availa
2a230 62 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a ble bytes here *
2a240 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 74 3b 0a /.){. int amt;.
2a250 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d assert( pCur!=
2a260 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 0 && pCur->iPage
2a270 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 70 50 61 >=0 && pCur->pPa
2a280 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ge);. assert( p
2a290 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
2a2a0 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
2a2b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
2a2c0 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e utex_held(pCur->
2a2d0 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 pBtree->db->mute
2a2e0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
2a2f0 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 cursorOwnsBtShar
2a300 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ed(pCur) );. as
2a310 73 65 72 74 28 20 70 43 75 72 2d 3e 69 78 3c 70 sert( pCur->ix<p
2a320 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c Cur->pPage->nCel
2a330 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 l );. assert( p
2a340 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3e Cur->info.nSize>
2a350 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
2a360 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f Cur->info.pPaylo
2a370 61 64 3e 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e ad>pCur->pPage->
2a380 61 44 61 74 61 20 7c 7c 20 43 4f 52 52 55 50 54 aData || CORRUPT
2a390 5f 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 _DB );. assert(
2a3a0 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 pCur->info.pPay
2a3b0 6c 6f 61 64 3c 70 43 75 72 2d 3e 70 50 61 67 65 load<pCur->pPage
2a3c0 2d 3e 61 44 61 74 61 45 6e 64 20 7c 7c 43 4f 52 ->aDataEnd ||COR
2a3d0 52 55 50 54 5f 44 42 29 3b 0a 20 20 61 6d 74 20 RUPT_DB);. amt
2a3e0 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f = pCur->info.nLo
2a3f0 63 61 6c 3b 0a 20 20 69 66 28 20 61 6d 74 3e 28 cal;. if( amt>(
2a400 69 6e 74 29 28 70 43 75 72 2d 3e 70 50 61 67 65 int)(pCur->pPage
2a410 2d 3e 61 44 61 74 61 45 6e 64 20 2d 20 70 43 75 ->aDataEnd - pCu
2a420 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 64 r->info.pPayload
2a430 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 ) ){. /* Ther
2a440 65 20 69 73 20 74 6f 6f 20 6c 69 74 74 6c 65 20 e is too little
2a450 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 space on the pag
2a460 65 20 66 6f 72 20 74 68 65 20 65 78 70 65 63 74 e for the expect
2a470 65 64 20 61 6d 6f 75 6e 74 0a 20 20 20 20 2a 2a ed amount. **
2a480 20 6f 66 20 6c 6f 63 61 6c 20 63 6f 6e 74 65 6e of local conten
2a490 74 2e 20 44 61 74 61 62 61 73 65 20 6d 75 73 74 t. Database must
2a4a0 20 62 65 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a be corrupt. */.
2a4b0 20 20 20 20 61 73 73 65 72 74 28 20 43 4f 52 52 assert( CORR
2a4c0 55 50 54 5f 44 42 20 29 3b 0a 20 20 20 20 61 6d UPT_DB );. am
2a4d0 74 20 3d 20 4d 41 58 28 30 2c 20 28 69 6e 74 29 t = MAX(0, (int)
2a4e0 28 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 61 44 (pCur->pPage->aD
2a4f0 61 74 61 45 6e 64 20 2d 20 70 43 75 72 2d 3e 69 ataEnd - pCur->i
2a500 6e 66 6f 2e 70 50 61 79 6c 6f 61 64 29 29 3b 0a nfo.pPayload));.
2a510 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 28 75 }. *pAmt = (u
2a520 33 32 29 61 6d 74 3b 0a 20 20 72 65 74 75 72 6e 32)amt;. return
2a530 20 28 76 6f 69 64 2a 29 70 43 75 72 2d 3e 69 6e (void*)pCur->in
2a540 66 6f 2e 70 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a fo.pPayload;.}..
2a550 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 ./*.** For the e
2a560 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 ntry that cursor
2a570 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 pCur is point t
2a580 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 o, return as.**
2a590 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 many bytes of th
2a5a0 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 e key or data as
2a5b0 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f are available o
2a5c0 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 n the local.** b
2a5d0 2d 74 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 -tree page. Wri
2a5e0 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 te the number of
2a5f0 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 available bytes
2a600 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a into *pAmt..**.
2a610 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 ** The pointer r
2a620 65 74 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d eturned is ephem
2a630 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 eral. The key/d
2a640 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 ata may move.**
2a650 6f 72 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 or be destroyed
2a660 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c on the next call
2a670 20 74 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f to any Btree ro
2a680 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 utine,.** includ
2a690 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f ing calls from o
2a6a0 74 68 65 72 20 74 68 72 65 61 64 73 20 61 67 61 ther threads aga
2a6b0 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 inst the same ca
2a6c0 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 che..** Hence, a
2a6d0 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 mutex on the Bt
2a6e0 53 68 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 Shared should be
2a6f0 20 68 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 held prior to c
2a700 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 alling.** this r
2a710 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 outine..**.** Th
2a720 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 ese routines is
2a730 75 73 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 used to get quic
2a740 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 k access to key
2a750 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 and data.** in t
2a760 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 he common case w
2a770 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 here no overflow
2a780 20 70 61 67 65 73 20 61 72 65 20 75 73 65 64 2e pages are used.
2a790 0a 2a 2f 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a .*/.const void *
2a7a0 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 79 6c sqlite3BtreePayl
2a7b0 6f 61 64 46 65 74 63 68 28 42 74 43 75 72 73 6f oadFetch(BtCurso
2a7c0 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 41 r *pCur, u32 *pA
2a7d0 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 66 65 mt){. return fe
2a7e0 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c tchPayload(pCur,
2a7f0 20 70 41 6d 74 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a pAmt);.}.../*.*
2a800 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
2a810 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 r down to a new
2a820 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 child page. The
2a830 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e newPgno argumen
2a840 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 t is the.** page
2a850 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 number of the c
2a860 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 hild page to mov
2a870 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 e to..**.** This
2a880 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
2a890 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 s SQLITE_CORRUPT
2a8a0 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 if the page-hea
2a8b0 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 der flags field
2a8c0 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 of.** the new ch
2a8d0 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f ild page does no
2a8e0 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 t match the flag
2a8f0 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 s field of the p
2a900 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 arent (i.e..** i
2a910 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 f an intkey page
2a920 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 appears to be t
2a930 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e he parent of a n
2a940 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 on-intkey page,
2a950 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 or.** vice-versa
2a960 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
2a970 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 moveToChild(BtC
2a980 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 ursor *pCur, u32
2a990 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 42 74 53 newPgno){. BtS
2a9a0 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 hared *pBt = pCu
2a9b0 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 r->pBt;.. asser
2a9c0 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 t( cursorOwnsBtS
2a9d0 68 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 hared(pCur) );.
2a9e0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
2a9f0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
2aa00 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 LID );. assert(
2aa10 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 pCur->iPage<BTC
2aa20 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 URSOR_MAX_DEPTH
2aa30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
2aa40 72 2d 3e 69 50 61 67 65 3e 3d 30 20 29 3b 0a 20 r->iPage>=0 );.
2aa50 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
2aa60 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f >=(BTCURSOR_MAX_
2aa70 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20 20 DEPTH-1) ){.
2aa80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
2aa90 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
2aaa0 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
2aab0 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
2aac0 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 curFlags &= ~(BT
2aad0 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 CF_ValidNKey|BTC
2aae0 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 F_ValidOvfl);.
2aaf0 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
2ab00 2d 3e 69 50 61 67 65 5d 20 3d 20 70 43 75 72 2d ->iPage] = pCur-
2ab10 3e 69 78 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 >ix;. pCur->apP
2ab20 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
2ab30 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a = pCur->pPage;.
2ab40 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 30 3b 0a pCur->ix = 0;.
2ab50 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b pCur->iPage++;
2ab60 0a 20 20 72 65 74 75 72 6e 20 67 65 74 41 6e 64 . return getAnd
2ab70 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 InitPage(pBt, ne
2ab80 77 50 67 6e 6f 2c 20 26 70 43 75 72 2d 3e 70 50 wPgno, &pCur->pP
2ab90 61 67 65 2c 20 70 43 75 72 2c 20 70 43 75 72 2d age, pCur, pCur-
2aba0 3e 63 75 72 50 61 67 65 72 46 6c 61 67 73 29 3b >curPagerFlags);
2abb0 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
2abc0 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 E_DEBUG./*.** Pa
2abd0 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e ge pParent is an
2abe0 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c internal (non-l
2abf0 65 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 eaf) tree page.
2ac00 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a This function .*
2ac10 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70 * asserts that p
2ac20 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c age number iChil
2ac30 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 d is the left-ch
2ac40 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27 ild if the iIdx'
2ac50 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 th.** cell in pa
2ac60 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 ge pParent. Or,
2ac70 69 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c if iIdx is equal
2ac80 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 to the total nu
2ac90 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 mber of.** cells
2aca0 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 in pParent, tha
2acb0 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 t page number iC
2acc0 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 hild is the righ
2acd0 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 t-child of.** th
2ace0 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 e page..*/.stati
2acf0 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 c void assertPar
2ad00 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 entIndex(MemPage
2ad10 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 *pParent, int i
2ad20 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 Idx, Pgno iChild
2ad30 29 7b 0a 20 20 69 66 28 20 43 4f 52 52 55 50 54 ){. if( CORRUPT
2ad40 5f 44 42 20 29 20 72 65 74 75 72 6e 3b 20 20 2f _DB ) return; /
2ad50 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 * The conditions
2ad60 20 74 65 73 74 65 64 20 62 65 6c 6f 77 20 6d 69 tested below mi
2ad70 67 68 74 20 6e 6f 74 20 62 65 20 74 72 75 65 0a ght not be true.
2ad80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ad90 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 ** i
2ada0 6e 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 n a corrupt data
2adb0 62 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 base */. assert
2adc0 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d ( iIdx<=pParent-
2add0 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 >nCell );. if(
2ade0 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e iIdx==pParent->n
2adf0 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 Cell ){. asse
2ae00 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70 50 rt( get4byte(&pP
2ae10 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 arent->aData[pPa
2ae20 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b rent->hdrOffset+
2ae30 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 8])==iChild );.
2ae40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
2ae50 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69 6e rt( get4byte(fin
2ae60 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 dCell(pParent, i
2ae70 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b Idx))==iChild );
2ae80 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 . }.}.#else.#
2ae90 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61 72 define assertPar
2aea0 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 entIndex(x,y,z)
2aeb0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
2aec0 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 75 ove the cursor u
2aed0 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 p to the parent
2aee0 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 page..**.** pCur
2aef0 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 ->idx is set to
2af00 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 the cell index t
2af10 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
2af20 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 pointer.** to t
2af30 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20 63 he page we are c
2af40 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 oming from. If
2af50 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 we are coming fr
2af60 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d om the.** right-
2af70 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65 20 most child page
2af80 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 then pCur->idx i
2af90 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 s set to one mor
2afa0 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 e than.** the la
2afb0 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 rgest cell index
2afc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
2afd0 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 moveToParent(Bt
2afe0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
2aff0 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 3b MemPage *pLeaf;
2b000 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
2b010 72 4f 77 6e 73 42 74 53 68 61 72 65 64 28 70 43 rOwnsBtShared(pC
2b020 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
2b030 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
2b040 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
2b050 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
2b060 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 Page>0 );. asse
2b070 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65 20 rt( pCur->pPage
2b080 29 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e );. assertParen
2b090 74 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75 72 tIndex(. pCur
2b0a0 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
2b0b0 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 Page-1], . pC
2b0c0 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
2b0d0 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 iPage-1], . p
2b0e0 43 75 72 2d 3e 70 50 61 67 65 2d 3e 70 67 6e 6f Cur->pPage->pgno
2b0f0 0a 20 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 . );. testcase
2b100 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
2b110 75 72 2d 3e 69 50 61 67 65 2d 31 5d 20 3e 20 70 ur->iPage-1] > p
2b120 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
2b130 2d 3e 69 50 61 67 65 2d 31 5d 2d 3e 6e 43 65 6c ->iPage-1]->nCel
2b140 6c 20 29 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 l );. pCur->inf
2b150 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
2b160 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d Cur->curFlags &=
2b170 20 7e 28 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 ~(BTCF_ValidNKe
2b180 79 7c 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c y|BTCF_ValidOvfl
2b190 29 3b 0a 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 );. pCur->ix =
2b1a0 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
2b1b0 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 70 4c ->iPage-1];. pL
2b1c0 65 61 66 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 eaf = pCur->pPag
2b1d0 65 3b 0a 20 20 70 43 75 72 2d 3e 70 50 61 67 65 e;. pCur->pPage
2b1e0 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
2b1f0 2d 2d 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a --pCur->iPage];.
2b200 20 20 72 65 6c 65 61 73 65 50 61 67 65 4e 6f 74 releasePageNot
2b210 4e 75 6c 6c 28 70 4c 65 61 66 29 3b 0a 7d 0a 0a Null(pLeaf);.}..
2b220 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 /*.** Move the c
2b230 75 72 73 6f 72 20 74 6f 20 70 6f 69 6e 74 20 74 ursor to point t
2b240 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 o the root page
2b250 6f 66 20 69 74 73 20 62 2d 74 72 65 65 20 73 74 of its b-tree st
2b260 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 ructure..**.** I
2b270 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 f the table has
2b280 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 a virtual root p
2b290 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 age, then the cu
2b2a0 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f rsor is moved to
2b2b0 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 point.** to the
2b2c0 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 virtual root pa
2b2d0 67 65 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 ge instead of th
2b2e0 65 20 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 e actual root pa
2b2f0 67 65 2e 20 41 20 74 61 62 6c 65 20 68 61 73 20 ge. A table has
2b300 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f 6f a.** virtual roo
2b310 74 20 70 61 67 65 20 77 68 65 6e 20 74 68 65 20 t page when the
2b320 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 actual root page
2b330 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c contains no cel
2b340 6c 73 20 61 6e 64 20 61 20 0a 2a 2a 20 73 69 6e ls and a .** sin
2b350 67 6c 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20 gle child page.
2b360 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 This can only ha
2b370 70 70 65 6e 20 77 69 74 68 20 74 68 65 20 74 61 ppen with the ta
2b380 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 70 61 ble rooted at pa
2b390 67 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ge 1..**.** If t
2b3a0 68 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 he b-tree struct
2b3b0 75 72 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68 ure is empty, th
2b3c0 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 69 e cursor state i
2b3d0 73 20 73 65 74 20 74 6f 20 0a 2a 2a 20 43 55 52 s set to .** CUR
2b3e0 53 4f 52 5f 49 4e 56 41 4c 49 44 20 61 6e 64 20 SOR_INVALID and
2b3f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
2b400 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 urns SQLITE_EMPT
2b410 59 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a Y. Otherwise,.**
2b420 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 the cursor is s
2b430 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 et to point to t
2b440 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 6c 6f he first cell lo
2b450 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f cated on the roo
2b460 74 0a 2a 2a 20 28 6f 72 20 76 69 72 74 75 61 6c t.** (or virtual
2b470 20 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 root) page and
2b480 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 the cursor state
2b490 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f is set to CURSO
2b4a0 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 R_VALID..**.** I
2b4b0 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 f this function
2b4c0 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 66 returns successf
2b4d0 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65 20 ully, it may be
2b4e0 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
2b4f0 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72 20 .** page-header
2b500 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20 74 flags indicate t
2b510 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 61 6c hat the [virtual
2b520 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 74 ] root-page is t
2b530 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a 20 he expected .**
2b540 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20 70 kind of b-tree p
2b550 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68 65 age (i.e. if whe
2b560 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63 75 n opening the cu
2b570 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72 20 rsor the caller
2b580 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63 69 did not.** speci
2b590 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 fy a KeyInfo str
2b5a0 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 ucture the flags
2b5b0 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 byte is set to
2b5c0 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a 2a 0x05 or 0x0D,.**
2b5d0 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 61 indicating a ta
2b5e0 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 69 ble b-tree, or i
2b5f0 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 f the caller did
2b600 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e specify a KeyIn
2b610 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 fo .** structure
2b620 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
2b630 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 6f is set to 0x02 o
2b640 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 69 r 0x0A, indicati
2b650 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 62 ng an index.** b
2b660 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 -tree)..*/.stati
2b670 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74 c int moveToRoot
2b680 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
2b690 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f {. MemPage *pRo
2b6a0 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ot;. int rc = S
2b6b0 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
2b6c0 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 ert( cursorOwnsB
2b6d0 74 53 68 61 72 65 64 28 70 43 75 72 29 20 29 3b tShared(pCur) );
2b6e0 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f . assert( CURSO
2b6f0 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 55 52 53 R_INVALID < CURS
2b700 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
2b710 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53 ;. assert( CURS
2b720 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 43 55 52 OR_VALID < CUR
2b730 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
2b740 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 );. assert( CUR
2b750 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20 43 55 SOR_FAULT > CU
2b760 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
2b770 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
2b780 75 72 2d 3e 65 53 74 61 74 65 20 3c 20 43 55 52 ur->eState < CUR
2b790 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
2b7a0 7c 7c 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 30 || pCur->iPage<0
2b7b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
2b7c0 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3e 30 20 7c ur->pgnoRoot>0 |
2b7d0 7c 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 30 20 | pCur->iPage<0
2b7e0 29 3b 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e );.. if( pCur->
2b7f0 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 iPage>=0 ){.
2b800 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 20 if( pCur->iPage
2b810 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
2b820 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 70 43 75 72 PageNotNull(pCur
2b830 2d 3e 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 ->pPage);.
2b840 77 68 69 6c 65 28 20 2d 2d 70 43 75 72 2d 3e 69 while( --pCur->i
2b850 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
2b860 72 65 6c 65 61 73 65 50 61 67 65 4e 6f 74 4e 75 releasePageNotNu
2b870 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ll(pCur->apPage[
2b880 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 pCur->iPage]);.
2b890 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 }. pCu
2b8a0 72 2d 3e 70 50 61 67 65 20 3d 20 70 43 75 72 2d r->pPage = pCur-
2b8b0 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 20 20 >apPage[0];.
2b8c0 20 20 67 6f 74 6f 20 73 6b 69 70 5f 69 6e 69 74 goto skip_init
2b8d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
2b8e0 69 66 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f if( pCur->pgnoRo
2b8f0 6f 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 75 ot==0 ){. pCu
2b900 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
2b910 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
2b920 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 4d return SQLITE_EM
2b930 50 54 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 PTY;. }else{.
2b940 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
2b950 69 50 61 67 65 3d 3d 28 2d 31 29 20 29 3b 0a 20 iPage==(-1) );.
2b960 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 if( pCur->eSt
2b970 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 ate>=CURSOR_REQU
2b980 49 52 45 53 45 45 4b 20 29 7b 0a 20 20 20 20 20 IRESEEK ){.
2b990 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
2b9a0 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e==CURSOR_FAULT
2b9b0 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
2b9c0 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 t( pCur->skipNex
2b9d0 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a t!=SQLITE_OK );.
2b9e0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 return p
2b9f0 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 Cur->skipNext;.
2ba00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
2ba10 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
2ba20 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 20 rsor(pCur);.
2ba30 7d 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e }. rc = getAn
2ba40 64 49 6e 69 74 50 61 67 65 28 70 43 75 72 2d 3e dInitPage(pCur->
2ba50 70 42 74 72 65 65 2d 3e 70 42 74 2c 20 70 43 75 pBtree->pBt, pCu
2ba60 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 r->pgnoRoot, &pC
2ba70 75 72 2d 3e 70 50 61 67 65 2c 0a 20 20 20 20 20 ur->pPage,.
2ba80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ba90 20 20 20 30 2c 20 70 43 75 72 2d 3e 63 75 72 50 0, pCur->curP
2baa0 61 67 65 72 46 6c 61 67 73 29 3b 0a 20 20 20 20 agerFlags);.
2bab0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
2bac0 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d K ){. pCur-
2bad0 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
2bae0 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 _INVALID;.
2baf0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
2bb00 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 . pCur->iPage
2bb10 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
2bb20 63 75 72 49 6e 74 4b 65 79 20 3d 20 70 43 75 72 curIntKey = pCur
2bb30 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3b ->pPage->intKey;
2bb40 0a 20 20 7d 0a 20 20 70 52 6f 6f 74 20 3d 20 70 . }. pRoot = p
2bb50 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73 Cur->pPage;. as
2bb60 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e sert( pRoot->pgn
2bb70 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f o==pCur->pgnoRoo
2bb80 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 43 t );.. /* If pC
2bb90 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69 73 20 ur->pKeyInfo is
2bba0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 not NULL, then t
2bbb0 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 6f he caller that o
2bbc0 70 65 6e 65 64 20 74 68 69 73 20 63 75 72 73 6f pened this curso
2bbd0 72 0a 20 20 2a 2a 20 65 78 70 65 63 74 65 64 20 r. ** expected
2bbe0 74 6f 20 6f 70 65 6e 20 69 74 20 6f 6e 20 61 6e to open it on an
2bbf0 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 20 4f index b-tree. O
2bc00 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 4b 65 therwise, if pKe
2bc10 79 49 6e 66 6f 20 69 73 0a 20 20 2a 2a 20 4e 55 yInfo is. ** NU
2bc20 4c 4c 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 65 LL, the caller e
2bc30 78 70 65 63 74 73 20 61 20 74 61 62 6c 65 20 62 xpects a table b
2bc40 2d 74 72 65 65 2e 20 49 66 20 74 68 69 73 20 69 -tree. If this i
2bc50 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2c 0a s not the case,.
2bc60 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 53 ** return an S
2bc70 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 65 72 QLITE_CORRUPT er
2bc80 72 6f 72 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ror. . **. **
2bc90 45 61 72 6c 69 65 72 20 76 65 72 73 69 6f 6e 73 Earlier versions
2bca0 20 6f 66 20 53 51 4c 69 74 65 20 61 73 73 75 6d of SQLite assum
2bcb0 65 64 20 74 68 61 74 20 74 68 69 73 20 74 65 73 ed that this tes
2bcc0 74 20 63 6f 75 6c 64 20 6e 6f 74 20 66 61 69 6c t could not fail
2bcd0 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 72 6f 6f . ** if the roo
2bce0 74 20 70 61 67 65 20 77 61 73 20 61 6c 72 65 61 t page was alrea
2bcf0 64 79 20 6c 6f 61 64 65 64 20 77 68 65 6e 20 74 dy loaded when t
2bd00 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 his function was
2bd10 20 63 61 6c 6c 65 64 20 28 69 2e 65 2e 0a 20 20 called (i.e..
2bd20 2a 2a 20 69 66 20 70 43 75 72 2d 3e 69 50 61 67 ** if pCur->iPag
2bd30 65 3e 3d 30 29 2e 20 42 75 74 20 74 68 69 73 20 e>=0). But this
2bd40 69 73 20 6e 6f 74 20 73 6f 20 69 66 20 74 68 65 is not so if the
2bd50 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 database is cor
2bd60 72 75 70 74 65 64 20 0a 20 20 2a 2a 20 69 6e 20 rupted . ** in
2bd70 73 75 63 68 20 61 20 77 61 79 20 74 68 61 74 20 such a way that
2bd80 70 61 67 65 20 70 52 6f 6f 74 20 69 73 20 6c 69 page pRoot is li
2bd90 6e 6b 65 64 20 69 6e 74 6f 20 61 20 73 65 63 6f nked into a seco
2bda0 6e 64 20 62 2d 74 72 65 65 20 74 61 62 6c 65 20 nd b-tree table
2bdb0 0a 20 20 2a 2a 20 28 6f 72 20 74 68 65 20 66 72 . ** (or the fr
2bdc0 65 65 6c 69 73 74 29 2e 20 20 2a 2f 0a 20 20 61 eelist). */. a
2bdd0 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 69 6e ssert( pRoot->in
2bde0 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 52 6f 6f 74 tKey==1 || pRoot
2bdf0 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 ->intKey==0 );.
2be00 20 69 66 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e if( pRoot->isIn
2be10 69 74 3d 3d 30 20 7c 7c 20 28 70 43 75 72 2d 3e it==0 || (pCur->
2be20 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 21 3d 70 52 pKeyInfo==0)!=pR
2be30 6f 6f 74 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 oot->intKey ){.
2be40 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
2be50 5f 43 4f 52 52 55 50 54 5f 50 41 47 45 28 70 43 _CORRUPT_PAGE(pC
2be60 75 72 2d 3e 70 50 61 67 65 29 3b 0a 20 20 7d 0a ur->pPage);. }.
2be70 0a 73 6b 69 70 5f 69 6e 69 74 3a 20 20 0a 20 20 .skip_init: .
2be80 70 43 75 72 2d 3e 69 78 20 3d 20 30 3b 0a 20 20 pCur->ix = 0;.
2be90 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
2bea0 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 = 0;. pCur->cu
2beb0 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 rFlags &= ~(BTCF
2bec0 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f 56 61 6c _AtLast|BTCF_Val
2bed0 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 6c 69 idNKey|BTCF_Vali
2bee0 64 4f 76 66 6c 29 3b 0a 0a 20 20 70 52 6f 6f 74 dOvfl);.. pRoot
2bef0 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a = pCur->pPage;.
2bf00 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 if( pRoot->nCe
2bf10 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 ll>0 ){. pCur
2bf20 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
2bf30 52 5f 56 41 4c 49 44 3b 0a 20 20 7d 65 6c 73 65 R_VALID;. }else
2bf40 20 69 66 28 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 if( !pRoot->lea
2bf50 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 f ){. Pgno su
2bf60 62 70 61 67 65 3b 0a 20 20 20 20 69 66 28 20 70 bpage;. if( p
2bf70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 20 Root->pgno!=1 )
2bf80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
2bf90 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
2bfa0 73 75 62 70 61 67 65 20 3d 20 67 65 74 34 62 79 subpage = get4by
2bfb0 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 te(&pRoot->aData
2bfc0 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 [pRoot->hdrOffse
2bfd0 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d t+8]);. pCur-
2bfe0 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
2bff0 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d _VALID;. rc =
2c000 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
2c010 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20 7d r, subpage);. }
2c020 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e else{. pCur->
2c030 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
2c040 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 INVALID;. rc
2c050 3d 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a = SQLITE_EMPTY;.
2c060 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
2c070 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 .}../*.** Move t
2c080 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 he cursor down t
2c090 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 o the left-most
2c0a0 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 leaf entry benea
2c0b0 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 th the.** entry
2c0c0 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 to which it is c
2c0d0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
2c0e0 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 g..**.** The lef
2c0f0 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 t-most leaf is t
2c100 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 he one with the
2c110 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 smallest key - t
2c120 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 he first.** in a
2c130 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a scending order..
2c140 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
2c150 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 veToLeftmost(BtC
2c160 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
2c170 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 Pgno pgno;. int
2c180 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
2c190 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
2c1a0 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 e;.. assert( cu
2c1b0 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 64 rsorOwnsBtShared
2c1c0 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
2c1d0 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
2c1e0 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
2c1f0 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 ;. while( rc==S
2c200 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 QLITE_OK && !(pP
2c210 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 age = pCur->pPag
2c220 65 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 e)->leaf ){.
2c230 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 78 assert( pCur->ix
2c240 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b <pPage->nCell );
2c250 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 . pgno = get4
2c260 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
2c270 61 67 65 2c 20 70 43 75 72 2d 3e 69 78 29 29 3b age, pCur->ix));
2c280 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
2c290 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f Child(pCur, pgno
2c2a0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
2c2b0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 rc;.}../*.** Mov
2c2c0 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 e the cursor dow
2c2d0 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d n to the right-m
2c2e0 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 ost leaf entry b
2c2f0 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 eneath the.** pa
2c300 67 65 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 ge to which it i
2c310 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
2c320 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 ting. Notice th
2c330 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 e difference.**
2c340 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 between moveToLe
2c350 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 ftmost() and mov
2c360 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 eToRightmost().
2c370 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 moveToLeftmost(
2c380 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c ).** finds the l
2c390 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 eft-most entry b
2c3a0 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 eneath the *entr
2c3b0 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 y* whereas moveT
2c3c0 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 oRightmost().**
2c3d0 66 69 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d finds the right-
2c3e0 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 most entry benea
2c3f0 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a th the *page*..*
2c400 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d *.** The right-m
2c410 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 ost entry is the
2c420 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 one with the la
2c430 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 rgest key - the
2c440 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 last.** key in a
2c450 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a scending order..
2c460 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
2c470 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 veToRightmost(Bt
2c480 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
2c490 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e Pgno pgno;. in
2c4a0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
2c4b0 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
2c4c0 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 ge = 0;.. asser
2c4d0 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 t( cursorOwnsBtS
2c4e0 68 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 hared(pCur) );.
2c4f0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
2c500 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
2c510 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 LID );. while(
2c520 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e !(pPage = pCur->
2c530 70 50 61 67 65 29 2d 3e 6c 65 61 66 20 29 7b 0a pPage)->leaf ){.
2c540 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 pgno = get4b
2c550 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
2c560 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
2c570 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 et+8]);. pCur
2c580 2d 3e 69 78 20 3d 20 70 50 61 67 65 2d 3e 6e 43 ->ix = pPage->nC
2c590 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f ell;. rc = mo
2c5a0 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
2c5b0 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 pgno);. if( r
2c5c0 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
2c5d0 20 7d 0a 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 }. pCur->ix =
2c5e0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a pPage->nCell-1;.
2c5f0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
2c600 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 3b info.nSize==0 );
2c610 0a 20 20 61 73 73 65 72 74 28 20 28 70 43 75 72 . assert( (pCur
2c620 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 ->curFlags & BTC
2c630 46 5f 56 61 6c 69 64 4e 4b 65 79 29 3d 3d 30 20 F_ValidNKey)==0
2c640 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
2c650 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 TE_OK;.}../* Mov
2c660 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
2c670 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
2c680 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 in the table. R
2c690 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a eturn SQLITE_OK.
2c6a0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ** on success.
2c6b0 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 Set *pRes to 0 i
2c6c0 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 f the cursor act
2c6d0 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 ually points to
2c6e0 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 something.** or
2c6f0 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 set *pRes to 1 i
2c700 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
2c710 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c mpty..*/.int sql
2c720 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42 ite3BtreeFirst(B
2c730 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
2c740 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 nt *pRes){. int
2c750 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
2c760 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 cursorOwnsBtShar
2c770 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ed(pCur) );. as
2c780 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
2c790 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
2c7a0 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
2c7b0 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 ) );. rc = move
2c7c0 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
2c7d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
2c7e0 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
2c7f0 20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 pCur->pPage->nC
2c800 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 2a 70 52 ell>0 );. *pR
2c810 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d es = 0;. rc =
2c820 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 moveToLeftmost(
2c830 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 pCur);. }else i
2c840 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d f( rc==SQLITE_EM
2c850 50 54 59 20 29 7b 0a 20 20 20 20 61 73 73 65 72 PTY ){. asser
2c860 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t( pCur->pgnoRoo
2c870 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 70 50 t==0 || pCur->pP
2c880 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b age->nCell==0 );
2c890 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a . *pRes = 1;.
2c8a0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
2c8b0 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e OK;. }. return
2c8c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
2c8d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
2c8e0 20 6e 6f 2d 6f 70 20 69 66 20 63 75 72 73 6f 72 no-op if cursor
2c8f0 20 70 43 75 72 20 64 6f 65 73 20 6e 6f 74 20 70 pCur does not p
2c900 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 oint to a valid
2c910 72 6f 77 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 row..** Otherwis
2c920 65 2c 20 69 66 20 70 43 75 72 20 69 73 20 76 61 e, if pCur is va
2c930 6c 69 64 2c 20 63 6f 6e 66 69 67 75 72 65 20 69 lid, configure i
2c940 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 t so that the ne
2c950 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 xt call to.** sq
2c960 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 29 lite3BtreeNext()
2c970 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
2c980 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
2c990 4d 49 54 5f 57 49 4e 44 4f 57 46 55 4e 43 0a 76 MIT_WINDOWFUNC.v
2c9a0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
2c9b0 53 6b 69 70 4e 65 78 74 28 42 74 43 75 72 73 6f SkipNext(BtCurso
2c9c0 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 57 r *pCur){. /* W
2c9d0 65 20 62 65 6c 69 65 76 65 20 74 68 61 74 20 74 e believe that t
2c9e0 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 61 he cursor must a
2c9f0 6c 77 61 79 73 20 62 65 20 69 6e 20 74 68 65 20 lways be in the
2ca00 76 61 6c 69 64 20 73 74 61 74 65 20 77 68 65 6e valid state when
2ca10 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 . ** this routi
2ca20 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 62 75 ne is called, bu
2ca30 74 20 74 68 65 20 70 72 6f 6f 66 20 69 73 20 64 t the proof is d
2ca40 69 66 66 69 63 75 6c 74 2c 20 73 6f 20 77 65 20 ifficult, so we
2ca50 61 64 64 20 61 6e 0a 20 20 2a 2a 20 41 4c 57 61 add an. ** ALWa
2ca60 59 53 28 29 20 74 65 73 74 20 6a 75 73 74 20 69 YS() test just i
2ca70 6e 20 63 61 73 65 20 77 65 20 61 72 65 20 77 72 n case we are wr
2ca80 6f 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c ong. */. if( AL
2ca90 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 74 WAYS(pCur->eStat
2caa0 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 e==CURSOR_VALID)
2cab0 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 ){. pCur->eS
2cac0 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 53 4b tate = CURSOR_SK
2cad0 49 50 4e 45 58 54 3b 0a 20 20 20 20 70 43 75 72 IPNEXT;. pCur
2cae0 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 31 3b 0a ->skipNext = 1;.
2caf0 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}.#endif /*
2cb00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 49 4e 44 SQLITE_OMIT_WIND
2cb10 4f 57 46 55 4e 43 20 2a 2f 0a 0a 2f 2a 20 4d 6f OWFUNC */../* Mo
2cb20 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
2cb30 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
2cb40 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 in the table. R
2cb50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a eturn SQLITE_OK.
2cb60 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ** on success.
2cb70 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 Set *pRes to 0 i
2cb80 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 f the cursor act
2cb90 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 ually points to
2cba0 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 something.** or
2cbb0 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 set *pRes to 1 i
2cbc0 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
2cbd0 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c mpty..*/.int sql
2cbe0 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 ite3BtreeLast(Bt
2cbf0 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
2cc00 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
2cc10 72 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 rc;. . assert(
2cc20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 cursorOwnsBtShar
2cc30 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ed(pCur) );. as
2cc40 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
2cc50 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
2cc60 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
2cc70 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ) );.. /* If th
2cc80 65 20 63 75 72 73 6f 72 20 61 6c 72 65 61 64 79 e cursor already
2cc90 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c points to the l
2cca0 61 73 74 20 65 6e 74 72 79 2c 20 74 68 69 73 20 ast entry, this
2ccb0 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 is a no-op. */.
2ccc0 20 69 66 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 if( CURSOR_VALI
2ccd0 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 D==pCur->eState
2cce0 26 26 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 && (pCur->curFla
2ccf0 67 73 20 26 20 42 54 43 46 5f 41 74 4c 61 73 74 gs & BTCF_AtLast
2cd00 29 21 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 )!=0 ){.#ifdef S
2cd10 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
2cd20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 /* This block se
2cd30 72 76 65 73 20 74 6f 20 61 73 73 65 72 74 28 29 rves to assert()
2cd40 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
2cd50 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f 69 really does poi
2cd60 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 nt . ** to th
2cd70 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 e last entry in
2cd80 74 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a 20 the b-tree. */.
2cd90 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 int ii;. f
2cda0 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72 or(ii=0; ii<pCur
2cdb0 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a ->iPage; ii++){.
2cdc0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
2cdd0 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d 70 ur->aiIdx[ii]==p
2cde0 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d 2d Cur->apPage[ii]-
2cdf0 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a >nCell );. }.
2ce00 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
2ce10 2d 3e 69 78 3d 3d 70 43 75 72 2d 3e 70 50 61 67 ->ix==pCur->pPag
2ce20 65 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 e->nCell-1 );.
2ce30 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
2ce40 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 23 pPage->leaf );.#
2ce50 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e endif. return
2ce60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
2ce70 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f . rc = moveToRo
2ce80 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 ot(pCur);. if(
2ce90 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
2cea0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
2ceb0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
2cec0 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 2a R_VALID );. *
2ced0 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 63 pRes = 0;. rc
2cee0 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f = moveToRightmo
2cef0 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 st(pCur);. if
2cf00 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
2cf10 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 63 ){. pCur->c
2cf20 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 46 5f urFlags |= BTCF_
2cf30 41 74 4c 61 73 74 3b 0a 20 20 20 20 7d 65 6c 73 AtLast;. }els
2cf40 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 63 e{. pCur->c
2cf50 75 72 46 6c 61 67 73 20 26 3d 20 7e 42 54 43 46 urFlags &= ~BTCF
2cf60 5f 41 74 4c 61 73 74 3b 0a 20 20 20 20 7d 0a 20 _AtLast;. }.
2cf70 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 }else if( rc==S
2cf80 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 QLITE_EMPTY ){.
2cf90 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
2cfa0 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 >pgnoRoot==0 ||
2cfb0 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 pCur->pPage->nCe
2cfc0 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 2a 70 52 ll==0 );. *pR
2cfd0 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d es = 1;. rc =
2cfe0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
2cff0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
2d000 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 /* Move the curs
2d010 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f or so that it po
2d020 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 ints to an entry
2d030 20 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a near the key .*
2d040 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 * specified by p
2d050 49 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 IdxKey or intKey
2d060 2e 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 . Return a suc
2d070 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a cess code..**.**
2d080 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c For INTKEY tabl
2d090 65 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 es, the intKey p
2d0a0 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 arameter is used
2d0b0 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d . pIdxKey .** m
2d0c0 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f ust be NULL. Fo
2d0d0 72 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 r index tables,
2d0e0 70 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 pIdxKey is used
2d0f0 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 and intKey.** is
2d100 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 ignored..**.**
2d110 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 If an exact matc
2d120 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 h is not found,
2d130 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
2d140 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 is always.** lef
2d150 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 t pointing at a
2d160 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 leaf page which
2d170 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 would hold the e
2d180 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 ntry if it.** we
2d190 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 re present. The
2d1a0 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f cursor might po
2d1b0 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 int to an entry
2d1c0 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 that comes.** be
2d1d0 66 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 fore or after th
2d1e0 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 e key..**.** An
2d1f0 69 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 integer is writt
2d200 65 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 en into *pRes wh
2d210 69 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c ich is the resul
2d220 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e t of.** comparin
2d230 67 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 g the key with t
2d240 68 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 he entry to whic
2d250 68 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 h the cursor is
2d260 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 .** pointing. T
2d270 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 he meaning of th
2d280 65 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 e integer writte
2d290 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 n into.** *pRes
2d2a0 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
2d2b0 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 *.** *pRes<0
2d2c0 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 The cursor
2d2d0 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
2d2e0 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 g at an entry th
2d2f0 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
2d300 20 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 is smalle
2d310 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 r than intKey/pI
2d320 64 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 dxKey or if the
2d330 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a table is empty.*
2d340 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
2d350 20 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f and the curso
2d360 72 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c r is therefore l
2d370 65 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 eft point to not
2d380 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 hing..**.**
2d390 2a 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 *pRes==0 The
2d3a0 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 cursor is left
2d3b0 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 pointing at an e
2d3c0 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 ntry that.**
2d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 ex
2d3e0 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e actly matches in
2d3f0 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a tKey/pIdxKey..**
2d400 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 .** *pRes>0
2d410 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 The cursor
2d420 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
2d430 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 at an entry tha
2d440 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
2d450 20 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 is larger
2d460 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 than intKey/pIdx
2d470 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 69 Key..**.** For i
2d480 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 74 68 65 ndex tables, the
2d490 20 70 49 64 78 4b 65 79 2d 3e 65 71 53 65 65 6e pIdxKey->eqSeen
2d4a0 20 66 69 65 6c 64 20 69 73 20 73 65 74 20 74 6f field is set to
2d4b0 20 31 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 65 1 if there.** e
2d4c0 78 69 73 74 73 20 61 6e 20 65 6e 74 72 79 20 69 xists an entry i
2d4d0 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 n the table that
2d4e0 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 exactly matches
2d4f0 20 70 49 64 78 4b 65 79 2e 20 20 0a 2a 2f 0a 69 pIdxKey. .*/.i
2d500 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
2d510 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 ovetoUnpacked(.
2d520 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
2d530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
2d540 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f cursor to be mo
2d550 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 ved */. Unpacke
2d560 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
2d570 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e , /* Unpacked in
2d580 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 dex key */. i64
2d590 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 intKey,
2d5a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 /* The tab
2d5b0 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 le key */. int
2d5c0 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 biasRight,
2d5d0 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c /* If true,
2d5e0 20 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 bias the search
2d5f0 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 to the high end
2d600 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 */. int *pRes
2d610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2d620 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 * Write search r
2d630 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 esults here */.)
2d640 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 65 {. int rc;. Re
2d650 63 6f 72 64 43 6f 6d 70 61 72 65 20 78 52 65 63 cordCompare xRec
2d660 6f 72 64 43 6f 6d 70 61 72 65 3b 0a 0a 20 20 61 ordCompare;.. a
2d670 73 73 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e ssert( cursorOwn
2d680 73 42 74 53 68 61 72 65 64 28 70 43 75 72 29 20 sBtShared(pCur)
2d690 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
2d6a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
2d6b0 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
2d6c0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
2d6d0 73 65 72 74 28 20 70 52 65 73 20 29 3b 0a 20 20 sert( pRes );.
2d6e0 61 73 73 65 72 74 28 20 28 70 49 64 78 4b 65 79 assert( (pIdxKey
2d6f0 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 ==0)==(pCur->pKe
2d700 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 20 20 61 yInfo==0) );. a
2d710 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
2d720 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
2d730 44 20 7c 7c 20 28 70 49 64 78 4b 65 79 3d 3d 30 D || (pIdxKey==0
2d740 29 3d 3d 28 70 43 75 72 2d 3e 63 75 72 49 6e 74 )==(pCur->curInt
2d750 4b 65 79 21 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a Key!=0) );.. /*
2d760 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 If the cursor i
2d770 73 20 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 s already positi
2d780 6f 6e 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e oned at the poin
2d790 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a t we are trying.
2d7a0 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c ** to move to,
2d7b0 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 then just retur
2d7c0 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 n without doing
2d7d0 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 any work */. if
2d7e0 28 20 70 49 64 78 4b 65 79 3d 3d 30 0a 20 20 20 ( pIdxKey==0.
2d7f0 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d && pCur->eState=
2d800 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 =CURSOR_VALID &&
2d810 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 (pCur->curFlags
2d820 20 26 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 & BTCF_ValidNKe
2d830 79 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 69 y)!=0. ){. i
2d840 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b f( pCur->info.nK
2d850 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ey==intKey ){.
2d860 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
2d870 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
2d880 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
2d890 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e if( pCur->info.
2d8a0 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 nKey<intKey ){.
2d8b0 20 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e if( (pCur->
2d8c0 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f curFlags & BTCF_
2d8d0 41 74 4c 61 73 74 29 21 3d 30 20 29 7b 0a 20 20 AtLast)!=0 ){.
2d8e0 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 *pRes = -1
2d8f0 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
2d900 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
2d910 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 }. /* If
2d920 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6b 65 the requested ke
2d930 79 20 69 73 20 6f 6e 65 20 6d 6f 72 65 20 74 68 y is one more th
2d940 61 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 an the previous
2d950 6b 65 79 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 key, then.
2d960 2a 2a 20 74 72 79 20 74 6f 20 67 65 74 20 74 68 ** try to get th
2d970 65 72 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ere using sqlite
2d980 33 42 74 72 65 65 4e 65 78 74 28 29 20 72 61 74 3BtreeNext() rat
2d990 68 65 72 20 74 68 61 6e 20 61 20 66 75 6c 6c 0a her than a full.
2d9a0 20 20 20 20 20 20 2a 2a 20 62 69 6e 61 72 79 20 ** binary
2d9b0 73 65 61 72 63 68 2e 20 20 54 68 69 73 20 69 73 search. This is
2d9c0 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
2d9d0 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 72 72 only. The corr
2d9e0 65 63 74 20 61 6e 73 77 65 72 0a 20 20 20 20 20 ect answer.
2d9f0 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 6f 62 74 ** is still obt
2da00 61 69 6e 65 64 20 77 69 74 68 6f 75 74 20 74 68 ained without th
2da10 69 73 20 63 61 73 65 2c 20 6f 6e 6c 79 20 61 20 is case, only a
2da20 6c 69 74 74 6c 65 20 6d 6f 72 65 20 73 6c 6f 77 little more slow
2da30 65 6c 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ely */. if(
2da40 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 pCur->info.nKey
2da50 2b 31 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 +1==intKey ){.
2da60 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b *pRes = 0;
2da70 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
2da80 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 lite3BtreeNext(p
2da90 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 Cur, 0);.
2daa0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2dab0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
2dac0 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
2dad0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
2dae0 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 pCur->info.nKey
2daf0 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ==intKey ){.
2db00 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
2db10 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
2db20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
2db30 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 lse if( rc==SQLI
2db40 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 TE_DONE ){.
2db50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
2db60 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c _OK;. }el
2db70 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 se{. re
2db80 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
2db90 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
2dba0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 49 64 78 . }.. if( pIdx
2dbb0 4b 65 79 20 29 7b 0a 20 20 20 20 78 52 65 63 6f Key ){. xReco
2dbc0 72 64 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c 69 rdCompare = sqli
2dbd0 74 65 33 56 64 62 65 46 69 6e 64 43 6f 6d 70 61 te3VdbeFindCompa
2dbe0 72 65 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 re(pIdxKey);.
2dbf0 20 70 49 64 78 4b 65 79 2d 3e 65 72 72 43 6f 64 pIdxKey->errCod
2dc00 65 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 e = 0;. asser
2dc10 74 28 20 70 49 64 78 4b 65 79 2d 3e 64 65 66 61 t( pIdxKey->defa
2dc20 75 6c 74 5f 72 63 3d 3d 31 20 0a 20 20 20 20 20 ult_rc==1 .
2dc30 20 20 20 20 7c 7c 20 70 49 64 78 4b 65 79 2d 3e || pIdxKey->
2dc40 64 65 66 61 75 6c 74 5f 72 63 3d 3d 30 20 0a 20 default_rc==0 .
2dc50 20 20 20 20 20 20 20 20 7c 7c 20 70 49 64 78 4b || pIdxK
2dc60 65 79 2d 3e 64 65 66 61 75 6c 74 5f 72 63 3d 3d ey->default_rc==
2dc70 2d 31 0a 20 20 20 20 29 3b 0a 20 20 7d 65 6c 73 -1. );. }els
2dc80 65 7b 0a 20 20 20 20 78 52 65 63 6f 72 64 43 6f e{. xRecordCo
2dc90 6d 70 61 72 65 20 3d 20 30 3b 20 2f 2a 20 41 6c mpare = 0; /* Al
2dca0 6c 20 6b 65 79 73 20 61 72 65 20 69 6e 74 65 67 l keys are integ
2dcb0 65 72 73 20 2a 2f 0a 20 20 7d 0a 0a 20 20 72 63 ers */. }.. rc
2dcc0 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
2dcd0 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b ur);. if( rc ){
2dce0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
2dcf0 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 ITE_EMPTY ){.
2dd00 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
2dd10 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 >pgnoRoot==0 ||
2dd20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 pCur->pPage->nCe
2dd30 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a ll==0 );. *
2dd40 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 pRes = -1;.
2dd50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
2dd60 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 K;. }. ret
2dd70 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 urn rc;. }. as
2dd80 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67 sert( pCur->pPag
2dd90 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
2dda0 43 75 72 2d 3e 70 50 61 67 65 2d 3e 69 73 49 6e Cur->pPage->isIn
2ddb0 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 it );. assert(
2ddc0 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
2ddd0 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
2dde0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 assert( pCur->pP
2ddf0 61 67 65 2d 3e 6e 43 65 6c 6c 20 3e 20 30 20 29 age->nCell > 0 )
2de00 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
2de10 2d 3e 69 50 61 67 65 3d 3d 30 20 7c 7c 20 70 43 ->iPage==0 || pC
2de20 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
2de30 6e 74 4b 65 79 3d 3d 70 43 75 72 2d 3e 63 75 72 ntKey==pCur->cur
2de40 49 6e 74 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 IntKey );. asse
2de50 72 74 28 20 70 43 75 72 2d 3e 63 75 72 49 6e 74 rt( pCur->curInt
2de60 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 Key || pIdxKey )
2de70 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 ;. for(;;){.
2de80 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 2c 20 69 int lwr, upr, i
2de90 64 78 2c 20 63 3b 0a 20 20 20 20 50 67 6e 6f 20 dx, c;. Pgno
2dea0 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 chldPg;. MemP
2deb0 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 age *pPage = pCu
2dec0 72 2d 3e 70 50 61 67 65 3b 0a 20 20 20 20 75 38 r->pPage;. u8
2ded0 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 *pCell;
2dee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2def0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
2df00 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20 current cell in
2df10 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a pPage */.. /*
2df20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d 75 pPage->nCell mu
2df30 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 st be greater th
2df40 61 6e 20 7a 65 72 6f 2e 20 49 66 20 74 68 69 73 an zero. If this
2df50 20 69 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 is the root-pag
2df60 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 e. ** the cur
2df70 73 6f 72 20 77 6f 75 6c 64 20 68 61 76 65 20 62 sor would have b
2df80 65 65 6e 20 49 4e 56 41 4c 49 44 20 61 62 6f 76 een INVALID abov
2df90 65 20 61 6e 64 20 74 68 69 73 20 66 6f 72 28 3b e and this for(;
2dfa0 3b 29 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 6e ;) loop. ** n
2dfb0 6f 74 20 72 75 6e 2e 20 49 66 20 74 68 69 73 20 ot run. If this
2dfc0 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f 74 2d is not the root-
2dfd0 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 6d page, then the m
2dfe0 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 72 6f 75 oveToChild() rou
2dff0 74 69 6e 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c tine. ** woul
2e000 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 64 d have already d
2e010 65 74 65 63 74 65 64 20 64 62 20 63 6f 72 72 75 etected db corru
2e020 70 74 69 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c 79 ption. Similarly
2e030 2c 20 70 50 61 67 65 20 6d 75 73 74 0a 20 20 20 , pPage must.
2e040 20 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74 ** be the right
2e050 20 6b 69 6e 64 20 28 69 6e 64 65 78 20 6f 72 20 kind (index or
2e060 74 61 62 6c 65 29 20 6f 66 20 62 2d 74 72 65 65 table) of b-tree
2e070 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 page. Otherwise
2e080 0a 20 20 20 20 2a 2a 20 61 20 6d 6f 76 65 54 6f . ** a moveTo
2e090 43 68 69 6c 64 28 29 20 6f 72 20 6d 6f 76 65 54 Child() or moveT
2e0a0 6f 52 6f 6f 74 28 29 20 63 61 6c 6c 20 77 6f 75 oRoot() call wou
2e0b0 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 65 64 ld have detected
2e0c0 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 2a 2f corruption. */
2e0d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
2e0e0 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 ge->nCell>0 );.
2e0f0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
2e100 2d 3e 69 6e 74 4b 65 79 3d 3d 28 70 49 64 78 4b ->intKey==(pIdxK
2e110 65 79 3d 3d 30 29 20 29 3b 0a 20 20 20 20 6c 77 ey==0) );. lw
2e120 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20 3d r = 0;. upr =
2e130 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b pPage->nCell-1;
2e140 0a 20 20 20 20 61 73 73 65 72 74 28 20 62 69 61 . assert( bia
2e150 73 52 69 67 68 74 3d 3d 30 20 7c 7c 20 62 69 61 sRight==0 || bia
2e160 73 52 69 67 68 74 3d 3d 31 20 29 3b 0a 20 20 20 sRight==1 );.
2e170 20 69 64 78 20 3d 20 75 70 72 3e 3e 28 31 2d 62 idx = upr>>(1-b
2e180 69 61 73 52 69 67 68 74 29 3b 20 2f 2a 20 69 64 iasRight); /* id
2e190 78 20 3d 20 62 69 61 73 52 69 67 68 74 20 3f 20 x = biasRight ?
2e1a0 75 70 72 20 3a 20 28 6c 77 72 2b 75 70 72 29 2f upr : (lwr+upr)/
2e1b0 32 3b 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d 3e 2; */. pCur->
2e1c0 69 78 20 3d 20 28 75 31 36 29 69 64 78 3b 0a 20 ix = (u16)idx;.
2e1d0 20 20 20 69 66 28 20 78 52 65 63 6f 72 64 43 6f if( xRecordCo
2e1e0 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 mpare==0 ){.
2e1f0 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 for(;;){.
2e200 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 3b i64 nCellKey;
2e210 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d . pCell =
2e220 20 66 69 6e 64 43 65 6c 6c 50 61 73 74 50 74 72 findCellPastPtr
2e230 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 (pPage, idx);.
2e240 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d if( pPage-
2e250 3e 69 6e 74 4b 65 79 4c 65 61 66 20 29 7b 0a 20 >intKeyLeaf ){.
2e260 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
2e270 30 78 38 30 20 3c 3d 20 2a 28 70 43 65 6c 6c 2b 0x80 <= *(pCell+
2e280 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 +) ){.
2e290 20 20 69 66 28 20 70 43 65 6c 6c 3e 3d 70 50 61 if( pCell>=pPa
2e2a0 67 65 2d 3e 61 44 61 74 61 45 6e 64 20 29 7b 0a ge->aDataEnd ){.
2e2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
2e2c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
2e2d0 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 3b UPT_PAGE(pPage);
2e2e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
2e2f0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
2e300 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 65 74 }. get
2e310 56 61 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 Varint(pCell, (u
2e320 36 34 2a 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 64*)&nCellKey);.
2e330 20 20 20 20 20 20 20 20 69 66 28 20 6e 43 65 6c if( nCel
2e340 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 lKey<intKey ){.
2e350 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 lwr = i
2e360 64 78 2b 31 3b 0a 20 20 20 20 20 20 20 20 20 20 dx+1;.
2e370 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 20 63 if( lwr>upr ){ c
2e380 20 3d 20 2d 31 3b 20 62 72 65 61 6b 3b 20 7d 0a = -1; break; }.
2e390 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
2e3a0 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b 65 ( nCellKey>intKe
2e3b0 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 y ){. u
2e3c0 70 72 20 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 pr = idx-1;.
2e3d0 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 if( lwr>up
2e3e0 72 20 29 7b 20 63 20 3d 20 2b 31 3b 20 62 72 65 r ){ c = +1; bre
2e3f0 61 6b 3b 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 ak; }. }e
2e400 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 lse{. a
2e410 73 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3d ssert( nCellKey=
2e420 3d 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 =intKey );.
2e430 20 20 20 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 pCur->ix =
2e440 28 75 31 36 29 69 64 78 3b 0a 20 20 20 20 20 20 (u16)idx;.
2e450 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
2e460 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 leaf ){.
2e470 20 20 20 20 6c 77 72 20 3d 20 69 64 78 3b 0a 20 lwr = idx;.
2e480 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
2e490 6d 6f 76 65 74 6f 5f 6e 65 78 74 5f 6c 61 79 65 moveto_next_laye
2e4a0 72 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c r;. }el
2e4b0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
2e4c0 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c pCur->curFlags |
2e4d0 3d 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 = BTCF_ValidNKey
2e4e0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 ;. pC
2e4f0 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 ur->info.nKey =
2e500 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 nCellKey;.
2e510 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f pCur->info
2e520 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
2e530 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
2e540 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 0;. r
2e550 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
2e560 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2e570 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
2e580 73 73 65 72 74 28 20 6c 77 72 2b 75 70 72 3e 3d ssert( lwr+upr>=
2e590 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 64 78 0 );. idx
2e5a0 20 3d 20 28 6c 77 72 2b 75 70 72 29 3e 3e 31 3b = (lwr+upr)>>1;
2e5b0 20 20 2f 2a 20 69 64 78 20 3d 20 28 6c 77 72 2b /* idx = (lwr+
2e5c0 75 70 72 29 2f 32 3b 20 2a 2f 0a 20 20 20 20 20 upr)/2; */.
2e5d0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
2e5e0 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 for(;;){.
2e5f0 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 int nCell;
2e600 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
2e610 70 43 65 6c 6c 20 63 65 6c 6c 20 69 6e 20 62 79 pCell cell in by
2e620 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 tes */. p
2e630 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 50 Cell = findCellP
2e640 61 73 74 50 74 72 28 70 50 61 67 65 2c 20 69 64 astPtr(pPage, id
2e650 78 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 x);.. /*
2e660 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 The maximum supp
2e670 6f 72 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20 orted page-size
2e680 69 73 20 36 35 35 33 36 20 62 79 74 65 73 2e 20 is 65536 bytes.
2e690 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a This means that.
2e6a0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d ** the m
2e6b0 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
2e6c0 20 72 65 63 6f 72 64 20 62 79 74 65 73 20 73 74 record bytes st
2e6d0 6f 72 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 ored on an index
2e6e0 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20 B-Tree.
2e6f0 2a 2a 20 70 61 67 65 20 69 73 20 6c 65 73 73 20 ** page is less
2e700 74 68 61 6e 20 31 36 33 38 34 20 62 79 74 65 73 than 16384 bytes
2e710 20 61 6e 64 20 6d 61 79 20 62 65 20 73 74 6f 72 and may be stor
2e720 65 64 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20 ed as a 2-byte.
2e730 20 20 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 ** varint
2e740 2e 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 . This informati
2e750 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 on is used to at
2e760 74 65 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 tempt to avoid p
2e770 61 72 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20 arsing .
2e780 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 ** the entire ce
2e790 6c 6c 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 ll by checking f
2e7a0 6f 72 20 74 68 65 20 63 61 73 65 73 20 77 68 65 or the cases whe
2e7b0 72 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 re the record is
2e7c0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f . ** sto
2e7d0 72 65 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74 red entirely wit
2e7e0 68 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70 hin the b-tree p
2e7f0 61 67 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e age by inspectin
2e800 67 20 74 68 65 20 66 69 72 73 74 20 0a 20 20 20 g the first .
2e810 20 20 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20 ** 2 bytes
2e820 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 of the cell..
2e830 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
2e840 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d nCell = pCell[0]
2e850 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 43 ;. if( nC
2e860 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 31 ell<=pPage->max1
2e870 62 79 74 65 50 61 79 6c 6f 61 64 20 29 7b 0a 20 bytePayload ){.
2e880 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
2e890 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 branch runs if
2e8a0 74 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 the record-size
2e8b0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 63 65 6c field of the cel
2e8c0 6c 20 69 73 20 61 0a 20 20 20 20 20 20 20 20 20 l is a.
2e8d0 20 2a 2a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 ** single byte
2e8e0 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 72 varint and the r
2e8f0 65 63 6f 72 64 20 66 69 74 73 20 65 6e 74 69 72 ecord fits entir
2e900 65 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a ely on the main.
2e910 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 2d 74 ** b-t
2e920 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 ree page. */.
2e930 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
2e940 28 20 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b 31 3d ( pCell+nCell+1=
2e950 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e 64 =pPage->aDataEnd
2e960 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 );. c
2e970 3d 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 = xRecordCompare
2e980 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 (nCell, (void*)&
2e990 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 pCell[1], pIdxKe
2e9a0 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
2e9b0 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d e if( !(pCell[1]
2e9c0 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 & 0x80) .
2e9d0 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 && (nCell =
2e9e0 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 ((nCell&0x7f)<<7
2e9f0 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 ) + pCell[1])<=p
2ea00 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 Page->maxLocal.
2ea10 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ){.
2ea20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 /* The recor
2ea30 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 d-size field is
2ea40 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 a 2 byte varint
2ea50 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a and the record .
2ea60 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 ** fit
2ea70 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 s entirely on th
2ea80 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 e main b-tree pa
2ea90 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
2eaa0 20 20 74 65 73 74 63 61 73 65 28 20 70 43 65 6c testcase( pCel
2eab0 6c 2b 6e 43 65 6c 6c 2b 32 3d 3d 70 50 61 67 65 l+nCell+2==pPage
2eac0 2d 3e 61 44 61 74 61 45 6e 64 20 29 3b 0a 20 20 ->aDataEnd );.
2ead0 20 20 20 20 20 20 20 20 63 20 3d 20 78 52 65 63 c = xRec
2eae0 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c ordCompare(nCell
2eaf0 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c 5b , (void*)&pCell[
2eb00 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 2], pIdxKey);.
2eb10 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2eb20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
2eb30 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65 72 20 cord flows over
2eb40 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 onto one or more
2eb50 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
2eb60 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a In. **
2eb70 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 77 this case the w
2eb80 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64 73 20 hole cell needs
2eb90 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20 61 20 to be parsed, a
2eba0 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 buffer allocated
2ebb0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e . ** an
2ebc0 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 d accessPayload(
2ebd0 29 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 ) used to retrie
2ebe0 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e ve the record in
2ebf0 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 to the.
2ec00 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66 6f 72 ** buffer befor
2ec10 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e VdbeRecordComp
2ec20 61 72 65 28 29 20 63 61 6e 20 62 65 20 63 61 6c are() can be cal
2ec30 6c 65 64 2e 20 0a 20 20 20 20 20 20 20 20 20 20 led. .
2ec40 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **. **
2ec50 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 If the record is
2ec60 20 63 6f 72 72 75 70 74 2c 20 74 68 65 20 78 52 corrupt, the xR
2ec70 65 63 6f 72 64 43 6f 6d 70 61 72 65 20 72 6f 75 ecordCompare rou
2ec80 74 69 6e 65 20 6d 61 79 20 72 65 61 64 0a 20 20 tine may read.
2ec90 20 20 20 20 20 20 20 20 2a 2a 20 75 70 20 74 6f ** up to
2eca0 20 74 77 6f 20 76 61 72 69 6e 74 73 20 70 61 73 two varints pas
2ecb0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
2ecc0 20 62 75 66 66 65 72 2e 20 41 6e 20 65 78 74 72 buffer. An extr
2ecd0 61 20 31 38 20 0a 20 20 20 20 20 20 20 20 20 20 a 18 .
2ece0 2a 2a 20 62 79 74 65 73 20 6f 66 20 70 61 64 64 ** bytes of padd
2ecf0 69 6e 67 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ing is allocated
2ed00 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
2ed10 68 65 20 62 75 66 66 65 72 20 69 6e 0a 20 20 20 he buffer in.
2ed20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20 74 ** case t
2ed30 68 69 73 20 68 61 70 70 65 6e 73 2e 20 20 2a 2f his happens. */
2ed40 0a 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 . void
2ed50 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 *pCellKey;.
2ed60 20 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 u8 * const
2ed70 70 43 65 6c 6c 42 6f 64 79 20 3d 20 70 43 65 6c pCellBody = pCel
2ed80 6c 20 2d 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 l - pPage->child
2ed90 50 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 PtrSize;.
2eda0 20 20 20 70 50 61 67 65 2d 3e 78 50 61 72 73 65 pPage->xParse
2edb0 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c Cell(pPage, pCel
2edc0 6c 42 6f 64 79 2c 20 26 70 43 75 72 2d 3e 69 6e lBody, &pCur->in
2edd0 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e fo);. n
2ede0 43 65 6c 6c 20 3d 20 28 69 6e 74 29 70 43 75 72 Cell = (int)pCur
2edf0 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 ->info.nKey;.
2ee00 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
2ee10 20 6e 43 65 6c 6c 3c 30 20 29 3b 20 20 20 2f 2a nCell<0 ); /*
2ee20 20 54 72 75 65 20 69 66 20 6b 65 79 20 73 69 7a True if key siz
2ee30 65 20 69 73 20 32 5e 33 32 20 6f 72 20 6d 6f 72 e is 2^32 or mor
2ee40 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74 e */. t
2ee50 65 73 74 63 61 73 65 28 20 6e 43 65 6c 6c 3d 3d estcase( nCell==
2ee60 30 20 29 3b 20 20 2f 2a 20 49 6e 76 61 6c 69 64 0 ); /* Invalid
2ee70 20 6b 65 79 20 73 69 7a 65 3a 20 20 30 78 38 30 key size: 0x80
2ee80 20 30 78 38 30 20 30 78 30 30 20 2a 2f 0a 20 20 0x80 0x00 */.
2ee90 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
2eea0 28 20 6e 43 65 6c 6c 3d 3d 31 20 29 3b 20 20 2f ( nCell==1 ); /
2eeb0 2a 20 49 6e 76 61 6c 69 64 20 6b 65 79 20 73 69 * Invalid key si
2eec0 7a 65 3a 20 20 30 78 38 30 20 30 78 38 30 20 30 ze: 0x80 0x80 0
2eed0 78 30 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 x01 */.
2eee0 20 74 65 73 74 63 61 73 65 28 20 6e 43 65 6c 6c testcase( nCell
2eef0 3d 3d 32 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d ==2 ); /* Minim
2ef00 75 6d 20 6c 65 67 61 6c 20 69 6e 64 65 78 20 6b um legal index k
2ef10 65 79 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 ey size */.
2ef20 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 3c 32 if( nCell<2
2ef30 20 7c 7c 20 6e 43 65 6c 6c 2f 70 43 75 72 2d 3e || nCell/pCur->
2ef40 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e pBt->usableSize>
2ef50 70 43 75 72 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 pCur->pBt->nPage
2ef60 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2ef70 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
2ef80 55 50 54 5f 50 41 47 45 28 70 50 61 67 65 29 3b UPT_PAGE(pPage);
2ef90 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
2efa0 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b o moveto_finish;
2efb0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2efc0 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 pCellKey
2efd0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
2efe0 20 6e 43 65 6c 6c 2b 31 38 20 29 3b 0a 20 20 20 nCell+18 );.
2eff0 20 20 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c if( pCell
2f000 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Key==0 ){.
2f010 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
2f020 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 E_NOMEM_BKPT;.
2f030 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d goto m
2f040 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
2f050 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2f060 20 20 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 28 pCur->ix = (
2f070 75 31 36 29 69 64 78 3b 0a 20 20 20 20 20 20 20 u16)idx;.
2f080 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 rc = accessPa
2f090 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c 20 6e yload(pCur, 0, n
2f0a0 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 64 20 Cell, (unsigned
2f0b0 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 char*)pCellKey,
2f0c0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 0);. pC
2f0d0 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 ur->curFlags &=
2f0e0 7e 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 3b ~BTCF_ValidOvfl;
2f0f0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
2f100 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 c ){.
2f110 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
2f120 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 ellKey);.
2f130 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f goto moveto
2f140 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 _finish;.
2f150 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 }. c
2f160 20 3d 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 = xRecordCompar
2f170 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 e(nCell, pCellKe
2f180 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 y, pIdxKey);.
2f190 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
2f1a0 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 ree(pCellKey);.
2f1b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2f1c0 20 61 73 73 65 72 74 28 20 0a 20 20 20 20 20 20 assert( .
2f1d0 20 20 20 20 20 20 28 70 49 64 78 4b 65 79 2d 3e (pIdxKey->
2f1e0 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f errCode!=SQLITE_
2f1f0 43 4f 52 52 55 50 54 20 7c 7c 20 63 3d 3d 30 29 CORRUPT || c==0)
2f200 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70 49 . && (pI
2f210 64 78 4b 65 79 2d 3e 65 72 72 43 6f 64 65 21 3d dxKey->errCode!=
2f220 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
2f230 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
2f240 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 0a ->mallocFailed).
2f250 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
2f260 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 if( c<0 ){.
2f270 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 lwr = id
2f280 78 2b 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c x+1;. }el
2f290 73 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 se if( c>0 ){.
2f2a0 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 upr = id
2f2b0 78 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c x-1;. }el
2f2c0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 se{. as
2f2d0 73 65 72 74 28 20 63 3d 3d 30 20 29 3b 0a 20 20 sert( c==0 );.
2f2e0 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
2f2f0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 0;. rc
2f300 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
2f310 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 78 20 pCur->ix
2f320 3d 20 28 75 31 36 29 69 64 78 3b 0a 20 20 20 20 = (u16)idx;.
2f330 20 20 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 if( pIdxKe
2f340 79 2d 3e 65 72 72 43 6f 64 65 20 29 20 72 63 20 y->errCode ) rc
2f350 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
2f360 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 _BKPT;.
2f370 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
2f380 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ish;. }.
2f390 20 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 if( lwr>u
2f3a0 70 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 pr ) break;.
2f3b0 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 2b assert( lwr+
2f3c0 75 70 72 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 upr>=0 );.
2f3d0 20 20 69 64 78 20 3d 20 28 6c 77 72 2b 75 70 72 idx = (lwr+upr
2f3e0 29 3e 3e 31 3b 20 20 2f 2a 20 69 64 78 20 3d 20 )>>1; /* idx =
2f3f0 28 6c 77 72 2b 75 70 72 29 2f 32 20 2a 2f 0a 20 (lwr+upr)/2 */.
2f400 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
2f410 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70 assert( lwr==up
2f420 72 2b 31 20 7c 7c 20 28 70 50 61 67 65 2d 3e 69 r+1 || (pPage->i
2f430 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d ntKey && !pPage-
2f440 3e 6c 65 61 66 29 20 29 3b 0a 20 20 20 20 61 73 >leaf) );. as
2f450 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
2f460 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 nit );. if( p
2f470 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
2f480 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
2f490 2d 3e 69 78 3c 70 43 75 72 2d 3e 70 50 61 67 65 ->ix<pCur->pPage
2f4a0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 ->nCell );.
2f4b0 20 70 43 75 72 2d 3e 69 78 20 3d 20 28 75 31 36 pCur->ix = (u16
2f4c0 29 69 64 78 3b 0a 20 20 20 20 20 20 2a 70 52 65 )idx;. *pRe
2f4d0 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 s = c;. rc
2f4e0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
2f4f0 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
2f500 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 6d 6f 76 inish;. }.mov
2f510 65 74 6f 5f 6e 65 78 74 5f 6c 61 79 65 72 3a 0a eto_next_layer:.
2f520 20 20 20 20 69 66 28 20 6c 77 72 3e 3d 70 50 61 if( lwr>=pPa
2f530 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 ge->nCell ){.
2f540 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 chldPg = get4
2f550 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
2f560 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
2f570 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65 6c set+8]);. }el
2f580 73 65 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 se{. chldPg
2f590 20 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 = get4byte(find
2f5a0 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77 72 29 Cell(pPage, lwr)
2f5b0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 );. }. pCu
2f5c0 72 2d 3e 69 78 20 3d 20 28 75 31 36 29 6c 77 72 r->ix = (u16)lwr
2f5d0 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 ;. rc = moveT
2f5e0 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 63 68 6c oChild(pCur, chl
2f5f0 64 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 dPg);. if( rc
2f600 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 6d 6f ) break;. }.mo
2f610 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 70 veto_finish:. p
2f620 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
2f630 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 = 0;. assert( (
2f640 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
2f650 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 BTCF_ValidOvfl)
2f660 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==0 );. return
2f670 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}.../*.** Re
2f680 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
2f690 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 cursor is not p
2f6a0 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
2f6b0 74 72 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 try of the table
2f6c0 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 45 20 77 69 6c ..**.** TRUE wil
2f6d0 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 66 l be returned af
2f6e0 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ter a call to sq
2f6f0 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 29 lite3BtreeNext()
2f700 20 6d 6f 76 65 73 0a 2a 2a 20 70 61 73 74 20 74 moves.** past t
2f710 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
2f720 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 73 71 the table or sq
2f730 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 28 29 lite3BtreePrev()
2f740 20 6d 6f 76 65 73 20 70 61 73 74 0a 2a 2a 20 74 moves past.** t
2f750 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 2e 20 he first entry.
2f760 20 54 52 55 45 20 69 73 20 61 6c 73 6f 20 72 65 TRUE is also re
2f770 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 74 61 turned if the ta
2f780 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f ble is empty..*/
2f790 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
2f7a0 65 45 6f 66 28 42 74 43 75 72 73 6f 72 20 2a 70 eEof(BtCursor *p
2f7b0 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a Cur){. /* TODO:
2f7c0 20 57 68 61 74 20 69 66 20 74 68 65 20 63 75 72 What if the cur
2f7d0 73 6f 72 20 69 73 20 69 6e 20 43 55 52 53 4f 52 sor is in CURSOR
2f7e0 5f 52 45 51 55 49 52 45 53 45 45 4b 20 62 75 74 _REQUIRESEEK but
2f7f0 20 61 6c 6c 20 74 61 62 6c 65 20 65 6e 74 72 69 all table entri
2f800 65 73 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 65 es. ** have bee
2f810 6e 20 64 65 6c 65 74 65 64 3f 20 54 68 69 73 20 n deleted? This
2f820 41 50 49 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f API will need to
2f830 20 63 68 61 6e 67 65 20 74 6f 20 72 65 74 75 72 change to retur
2f840 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a n an error code.
2f850 20 20 2a 2a 20 61 73 20 77 65 6c 6c 20 61 73 20 ** as well as
2f860 74 68 65 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 the boolean resu
2f870 6c 74 20 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a 20 lt value.. */.
2f880 20 72 65 74 75 72 6e 20 28 43 55 52 53 4f 52 5f return (CURSOR_
2f890 56 41 4c 49 44 21 3d 70 43 75 72 2d 3e 65 53 74 VALID!=pCur->eSt
2f8a0 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ate);.}../*.** R
2f8b0 65 74 75 72 6e 20 61 6e 20 65 73 74 69 6d 61 74 eturn an estimat
2f8c0 65 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 e for the number
2f8d0 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 of rows in the
2f8e0 74 61 62 6c 65 20 74 68 61 74 20 70 43 75 72 20 table that pCur
2f8f0 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 is.** pointing t
2f900 6f 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 o. Return a neg
2f910 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 ative number if
2f920 6e 6f 20 65 73 74 69 6d 61 74 65 20 69 73 20 63 no estimate is c
2f930 75 72 72 65 6e 74 6c 79 20 0a 2a 2a 20 61 76 61 urrently .** ava
2f940 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 69 36 34 20 73 ilable..*/.i64 s
2f950 71 6c 69 74 65 33 42 74 72 65 65 52 6f 77 43 6f qlite3BtreeRowCo
2f960 75 6e 74 45 73 74 28 42 74 43 75 72 73 6f 72 20 untEst(BtCursor
2f970 2a 70 43 75 72 29 7b 0a 20 20 69 36 34 20 6e 3b *pCur){. i64 n;
2f980 0a 20 20 75 38 20 69 3b 0a 0a 20 20 61 73 73 65 . u8 i;.. asse
2f990 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 rt( cursorOwnsBt
2f9a0 53 68 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a Shared(pCur) );.
2f9b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
2f9c0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
2f9d0 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
2f9e0 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 utex) );.. /* C
2f9f0 75 72 72 65 6e 74 6c 79 20 74 68 69 73 20 69 6e urrently this in
2fa00 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 terface is only
2fa10 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 4f 50 called by the OP
2fa20 5f 49 66 53 6d 61 6c 6c 65 72 0a 20 20 2a 2a 20 _IfSmaller. **
2fa30 6f 70 63 6f 64 65 2c 20 61 6e 64 20 69 74 20 74 opcode, and it t
2fa40 68 61 74 20 63 61 73 65 20 74 68 65 20 63 75 72 hat case the cur
2fa50 73 6f 72 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 sor will always
2fa60 62 65 20 76 61 6c 69 64 20 61 6e 64 0a 20 20 2a be valid and. *
2fa70 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 70 6f * will always po
2fa80 69 6e 74 20 74 6f 20 61 20 6c 65 61 66 20 6e 6f int to a leaf no
2fa90 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 de. */. if( NEV
2faa0 45 52 28 70 43 75 72 2d 3e 65 53 74 61 74 65 21 ER(pCur->eState!
2fab0 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 20 29 =CURSOR_VALID) )
2fac0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 return -1;. if
2fad0 28 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 70 50 ( NEVER(pCur->pP
2fae0 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 29 20 29 20 age->leaf==0) )
2faf0 72 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 6e 20 return -1;.. n
2fb00 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e = pCur->pPage->n
2fb10 43 65 6c 6c 3b 0a 20 20 66 6f 72 28 69 3d 30 3b Cell;. for(i=0;
2fb20 20 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 i<pCur->iPage;
2fb30 69 2b 2b 29 7b 0a 20 20 20 20 6e 20 2a 3d 20 70 i++){. n *= p
2fb40 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d 3e Cur->apPage[i]->
2fb50 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 72 65 74 nCell;. }. ret
2fb60 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn n;.}../*.**
2fb70 41 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 Advance the curs
2fb80 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 or to the next e
2fb90 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 ntry in the data
2fba0 62 61 73 65 2e 20 0a 2a 2a 20 52 65 74 75 72 6e base. .** Return
2fbb0 20 76 61 6c 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 value:.**.**
2fbc0 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20 SQLITE_OK
2fbd0 20 20 73 75 63 63 65 73 73 0a 2a 2a 20 20 20 20 success.**
2fbe0 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20 20 SQLITE_DONE
2fbf0 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 cursor is alrea
2fc00 64 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 dy pointing at t
2fc10 68 65 20 6c 61 73 74 20 65 6c 65 6d 65 6e 74 0a he last element.
2fc20 2a 2a 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 ** otherwise
2fc30 20 20 20 20 20 20 20 73 6f 6d 65 20 6b 69 6e 64 some kind
2fc40 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72 72 of error occurr
2fc50 65 64 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 69 ed.**.** The mai
2fc60 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 n entry point is
2fc70 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
2fc80 74 28 29 2e 20 20 54 68 61 74 20 72 6f 75 74 69 t(). That routi
2fc90 6e 65 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 0a ne is optimized.
2fca0 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f ** for the commo
2fcb0 6e 20 63 61 73 65 20 6f 66 20 6d 65 72 65 6c 79 n case of merely
2fcc0 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 incrementing th
2fcd0 65 20 63 65 6c 6c 20 63 6f 75 6e 74 65 72 20 42 e cell counter B
2fce0 74 43 75 72 73 6f 72 2e 61 69 49 64 78 0a 2a 2a tCursor.aiIdx.**
2fcf0 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 65 6c to the next cel
2fd00 6c 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74 l on the current
2fd10 20 70 61 67 65 2e 20 20 54 68 65 20 28 73 6c 6f page. The (slo
2fd20 77 65 72 29 20 62 74 72 65 65 4e 65 78 74 28 29 wer) btreeNext()
2fd30 20 68 65 6c 70 65 72 0a 2a 2a 20 72 6f 75 74 69 helper.** routi
2fd40 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 ne is called whe
2fd50 6e 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 n it is necessar
2fd60 79 20 74 6f 20 6d 6f 76 65 20 74 6f 20 61 20 64 y to move to a d
2fd70 69 66 66 65 72 65 6e 74 20 70 61 67 65 20 6f 72 ifferent page or
2fd80 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 .** to restore t
2fd90 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a he cursor..**.**
2fda0 20 49 66 20 62 69 74 20 30 78 30 31 20 6f 66 20 If bit 0x01 of
2fdb0 74 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 69 the F argument i
2fdc0 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 n sqlite3BtreeNe
2fdd0 78 74 28 43 2c 46 29 20 69 73 20 31 2c 20 74 68 xt(C,F) is 1, th
2fde0 65 6e 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 en the.** cursor
2fdf0 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
2fe00 61 6e 20 53 51 4c 20 69 6e 64 65 78 20 61 6e 64 an SQL index and
2fe10 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f this routine co
2fe20 75 6c 64 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a uld have been.**
2fe30 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 65 20 skipped if the
2fe40 53 51 4c 20 69 6e 64 65 78 20 68 61 64 20 62 65 SQL index had be
2fe50 65 6e 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 en a unique inde
2fe60 78 2e 20 20 54 68 65 20 46 20 61 72 67 75 6d 65 x. The F argume
2fe70 6e 74 0a 2a 2a 20 69 73 20 61 20 68 69 6e 74 20 nt.** is a hint
2fe80 74 6f 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 to the implement
2fe90 2e 20 20 53 51 4c 69 74 65 20 62 74 72 65 65 20 . SQLite btree
2fea0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 implementation d
2feb0 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 74 oes not use.** t
2fec0 68 69 73 20 68 69 6e 74 2c 20 62 75 74 20 43 4f his hint, but CO
2fed0 4d 44 42 32 20 64 6f 65 73 2e 0a 2a 2f 0a 73 74 MDB2 does..*/.st
2fee0 61 74 69 63 20 53 51 4c 49 54 45 5f 4e 4f 49 4e atic SQLITE_NOIN
2fef0 4c 49 4e 45 20 69 6e 74 20 62 74 72 65 65 4e 65 LINE int btreeNe
2ff00 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 xt(BtCursor *pCu
2ff10 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 r){. int rc;.
2ff20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 int idx;. MemPa
2ff30 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 ge *pPage;.. as
2ff40 73 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 sert( cursorOwns
2ff50 42 74 53 68 61 72 65 64 28 70 43 75 72 29 20 29 BtShared(pCur) )
2ff60 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
2ff70 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
2ff80 49 44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ID ){. assert
2ff90 28 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 ( (pCur->curFlag
2ffa0 73 20 26 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 s & BTCF_ValidOv
2ffb0 66 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 fl)==0 );. rc
2ffc0 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
2ffd0 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
2ffe0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
2fff0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
30000 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
30010 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
30020 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
30030 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 72 65 tate ){. re
30040 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
30050 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
30060 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
30070 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 7b RSOR_SKIPNEXT ){
30080 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 . pCur->eSt
30090 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c ate = CURSOR_VAL
300a0 49 44 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 ID;. if( pC
300b0 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3e 30 20 29 ur->skipNext>0 )
300c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
300d0 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 K;. }. }..
300e0 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 pPage = pCur->pP
300f0 61 67 65 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 age;. idx = ++p
30100 43 75 72 2d 3e 69 78 3b 0a 20 20 69 66 28 20 21 Cur->ix;. if( !
30110 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b pPage->isInit ){
30120 0a 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 . /* The only
30130 20 6b 6e 6f 77 6e 20 77 61 79 20 66 6f 72 20 74 known way for t
30140 68 69 73 20 74 6f 20 68 61 70 70 65 6e 20 69 73 his to happen is
30150 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 for there to be
30160 20 61 0a 20 20 20 20 2a 2a 20 72 65 63 75 72 73 a. ** recurs
30170 69 76 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ive SQL function
30180 20 74 68 61 74 20 64 6f 65 73 20 61 20 44 45 4c that does a DEL
30190 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 20 61 73 ETE operation as
301a0 20 70 61 72 74 20 6f 66 20 61 0a 20 20 20 20 2a part of a. *
301b0 2a 20 53 45 4c 45 43 54 20 77 68 69 63 68 20 64 * SELECT which d
301c0 65 6c 65 74 65 73 20 63 6f 6e 74 65 6e 74 20 6f eletes content o
301d0 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 61 6e ut from under an
301e0 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 0a 20 active cursor.
301f0 20 20 20 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 ** in a corru
30200 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
30210 20 77 68 65 72 65 20 74 68 65 20 74 61 62 6c 65 where the table
30220 20 62 65 69 6e 67 20 44 45 4c 45 54 45 2d 65 64 being DELETE-ed
30230 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 68 61 73 from. ** has
30240 20 70 61 67 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e pages in common
30250 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 with the table
30260 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e 20 20 being queried.
30270 53 65 65 20 54 48 33 0a 20 20 20 20 2a 2a 20 6d See TH3. ** m
30280 6f 64 75 6c 65 20 63 6f 76 31 2f 62 74 72 65 65 odule cov1/btree
30290 37 38 2e 74 65 73 74 20 74 65 73 74 63 61 73 65 78.test testcase
302a0 20 32 32 30 20 28 32 30 31 38 2d 30 36 2d 30 38 220 (2018-06-08
302b0 29 20 66 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 ) for an. **
302c0 65 78 61 6d 70 6c 65 2e 20 2a 2f 0a 20 20 20 20 example. */.
302d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
302e0 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
302f0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 . /* If the dat
30300 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 63 6f abase file is co
30310 72 72 75 70 74 2c 20 69 74 20 69 73 20 70 6f 73 rrupt, it is pos
30320 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 76 61 sible for the va
30330 6c 75 65 20 6f 66 20 69 64 78 20 0a 20 20 2a 2a lue of idx . **
30340 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 20 68 to be invalid h
30350 65 72 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e ere. This can on
30360 6c 79 20 6f 63 63 75 72 20 69 66 20 61 20 73 65 ly occur if a se
30370 63 6f 6e 64 20 63 75 72 73 6f 72 20 6d 6f 64 69 cond cursor modi
30380 66 69 65 73 0a 20 20 2a 2a 20 74 68 65 20 70 61 fies. ** the pa
30390 67 65 20 77 68 69 6c 65 20 63 75 72 73 6f 72 20 ge while cursor
303a0 70 43 75 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 pCur is holding
303b0 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 a reference to i
303c0 74 2e 20 57 68 69 63 68 20 63 61 6e 0a 20 20 2a t. Which can. *
303d0 2a 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 * only happen if
303e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
303f0 20 63 6f 72 72 75 70 74 20 69 6e 20 73 75 63 68 corrupt in such
30400 20 61 20 77 61 79 20 61 73 20 74 6f 20 6c 69 6e a way as to lin
30410 6b 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 k the. ** page
30420 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f into more than o
30430 6e 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 ne b-tree struct
30440 75 72 65 2e 20 2a 2f 0a 20 20 74 65 73 74 63 61 ure. */. testca
30450 73 65 28 20 69 64 78 3e 70 50 61 67 65 2d 3e 6e se( idx>pPage->n
30460 43 65 6c 6c 20 29 3b 0a 0a 20 20 69 66 28 20 69 Cell );.. if( i
30470 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c dx>=pPage->nCell
30480 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50 61 ){. if( !pPa
30490 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
304a0 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
304b0 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 ld(pCur, get4byt
304c0 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b e(&pPage->aData[
304d0 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
304e0 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 +8]));. if(
304f0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
30500 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 6f . return mo
30510 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 veToLeftmost(pCu
30520 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6f r);. }. do
30530 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 {. if( pCur
30540 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 ->iPage==0 ){.
30550 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 pCur->eSta
30560 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
30570 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 LID;. ret
30580 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
30590 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
305a0 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 oveToParent(pCur
305b0 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d );. pPage =
305c0 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 pCur->pPage;.
305d0 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e }while( pCur->
305e0 69 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ix>=pPage->nCell
305f0 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 );. if( pPag
30600 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
30610 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
30620 33 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 3BtreeNext(pCur,
30630 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0);. }else{.
30640 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
30650 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
30660 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c }. if( pPage->l
30670 65 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 eaf ){. retur
30680 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
30690 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
306a0 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 moveToLeftmost(
306b0 70 43 75 72 29 3b 0a 20 20 7d 0a 7d 0a 69 6e 74 pCur);. }.}.int
306c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
306d0 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
306e0 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
306f0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a MemPage *pPage;.
30700 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
30710 45 52 28 20 66 6c 61 67 73 20 29 3b 20 20 2f 2a ER( flags ); /*
30720 20 55 73 65 64 20 69 6e 20 43 4f 4d 44 42 32 20 Used in COMDB2
30730 62 75 74 20 6e 6f 74 20 6e 61 74 69 76 65 20 53 but not native S
30740 51 4c 69 74 65 20 2a 2f 0a 20 20 61 73 73 65 72 QLite */. asser
30750 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 t( cursorOwnsBtS
30760 68 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 hared(pCur) );.
30770 20 61 73 73 65 72 74 28 20 66 6c 61 67 73 3d 3d assert( flags==
30780 30 20 7c 7c 20 66 6c 61 67 73 3d 3d 31 20 29 3b 0 || flags==1 );
30790 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 . pCur->info.nS
307a0 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ize = 0;. pCur-
307b0 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 >curFlags &= ~(B
307c0 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 TCF_ValidNKey|BT
307d0 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 CF_ValidOvfl);.
307e0 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
307f0 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
30800 29 20 72 65 74 75 72 6e 20 62 74 72 65 65 4e 65 ) return btreeNe
30810 78 74 28 70 43 75 72 29 3b 0a 20 20 70 50 61 67 xt(pCur);. pPag
30820 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b e = pCur->pPage;
30830 0a 20 20 69 66 28 20 28 2b 2b 70 43 75 72 2d 3e . if( (++pCur->
30840 69 78 29 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c ix)>=pPage->nCel
30850 6c 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 l ){. pCur->i
30860 78 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 x--;. return
30870 62 74 72 65 65 4e 65 78 74 28 70 43 75 72 29 3b btreeNext(pCur);
30880 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 . }. if( pPage
30890 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65 ->leaf ){. re
308a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
308b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
308c0 75 72 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f urn moveToLeftmo
308d0 73 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 7d 0a st(pCur);. }.}.
308e0 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 ./*.** Step the
308f0 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 61 cursor to the ba
30900 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f ck to the previo
30910 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 us entry in the
30920 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 52 65 74 database..** Ret
30930 75 72 6e 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a urn values:.**.*
30940 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 * SQLITE_OK
30950 20 20 20 20 73 75 63 63 65 73 73 0a 2a 2a 20 20 success.**
30960 20 20 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 SQLITE_DONE
30970 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 the cursor is a
30980 6c 72 65 61 64 79 20 6f 6e 20 74 68 65 20 66 69 lready on the fi
30990 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 rst element of t
309a0 68 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 he table.**
309b0 6f 74 68 65 72 77 69 73 65 20 20 20 20 20 73 6f otherwise so
309c0 6d 65 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 me kind of error
309d0 20 6f 63 63 75 72 72 65 64 0a 2a 2a 0a 2a 2a 20 occurred.**.**
309e0 54 68 65 20 6d 61 69 6e 20 65 6e 74 72 79 20 70 The main entry p
309f0 6f 69 6e 74 20 69 73 20 73 71 6c 69 74 65 33 42 oint is sqlite3B
30a00 74 72 65 65 50 72 65 76 69 6f 75 73 28 29 2e 20 treePrevious().
30a10 20 54 68 61 74 20 72 6f 75 74 69 6e 65 20 69 73 That routine is
30a20 20 6f 70 74 69 6d 69 7a 65 64 0a 2a 2a 20 66 6f optimized.** fo
30a30 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 r the common cas
30a40 65 20 6f 66 20 6d 65 72 65 6c 79 20 64 65 63 72 e of merely decr
30a50 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 65 6c ementing the cel
30a60 6c 20 63 6f 75 6e 74 65 72 20 42 74 43 75 72 73 l counter BtCurs
30a70 6f 72 2e 61 69 49 64 78 0a 2a 2a 20 74 6f 20 74 or.aiIdx.** to t
30a80 68 65 20 70 72 65 76 69 6f 75 73 20 63 65 6c 6c he previous cell
30a90 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 on the current
30aa0 70 61 67 65 2e 20 20 54 68 65 20 28 73 6c 6f 77 page. The (slow
30ab0 65 72 29 20 62 74 72 65 65 50 72 65 76 69 6f 75 er) btreePreviou
30ac0 73 28 29 0a 2a 2a 20 68 65 6c 70 65 72 20 72 6f s().** helper ro
30ad0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
30ae0 77 68 65 6e 20 69 74 20 69 73 20 6e 65 63 65 73 when it is neces
30af0 73 61 72 79 20 74 6f 20 6d 6f 76 65 20 74 6f 20 sary to move to
30b00 61 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 a different page
30b10 0a 2a 2a 20 6f 72 20 74 6f 20 72 65 73 74 6f 72 .** or to restor
30b20 65 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2a e the cursor..**
30b30 0a 2a 2a 20 49 66 20 62 69 74 20 30 78 30 31 20 .** If bit 0x01
30b40 6f 66 20 74 68 65 20 46 20 61 72 67 75 6d 65 6e of the F argumen
30b50 74 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 t to sqlite3Btre
30b60 65 50 72 65 76 69 6f 75 73 28 43 2c 46 29 20 69 ePrevious(C,F) i
30b70 73 20 31 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 s 1, then.** the
30b80 20 63 75 72 73 6f 72 20 63 6f 72 72 65 73 70 6f cursor correspo
30b90 6e 64 73 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e nds to an SQL in
30ba0 64 65 78 20 61 6e 64 20 74 68 69 73 20 72 6f 75 dex and this rou
30bb0 74 69 6e 65 20 63 6f 75 6c 64 20 68 61 76 65 20 tine could have
30bc0 62 65 65 6e 0a 2a 2a 20 73 6b 69 70 70 65 64 20 been.** skipped
30bd0 69 66 20 74 68 65 20 53 51 4c 20 69 6e 64 65 78 if the SQL index
30be0 20 68 61 64 20 62 65 65 6e 20 61 20 75 6e 69 71 had been a uniq
30bf0 75 65 20 69 6e 64 65 78 2e 20 20 54 68 65 20 46 ue index. The F
30c00 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a argument is a.*
30c10 2a 20 68 69 6e 74 20 74 6f 20 74 68 65 20 69 6d * hint to the im
30c20 70 6c 65 6d 65 6e 74 2e 20 20 54 68 65 20 6e 61 plement. The na
30c30 74 69 76 65 20 53 51 4c 69 74 65 20 62 74 72 65 tive SQLite btre
30c40 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
30c50 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 75 73 65 does not.** use
30c60 20 74 68 69 73 20 68 69 6e 74 2c 20 62 75 74 20 this hint, but
30c70 43 4f 4d 44 42 32 20 64 6f 65 73 2e 0a 2a 2f 0a COMDB2 does..*/.
30c80 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 4e 4f static SQLITE_NO
30c90 49 4e 4c 49 4e 45 20 69 6e 74 20 62 74 72 65 65 INLINE int btree
30ca0 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f Previous(BtCurso
30cb0 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 r *pCur){. int
30cc0 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 rc;. MemPage *p
30cd0 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
30ce0 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 cursorOwnsBtSha
30cf0 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 red(pCur) );. a
30d00 73 73 65 72 74 28 20 28 70 43 75 72 2d 3e 63 75 ssert( (pCur->cu
30d10 72 46 6c 61 67 73 20 26 20 28 42 54 43 46 5f 41 rFlags & (BTCF_A
30d20 74 4c 61 73 74 7c 42 54 43 46 5f 56 61 6c 69 64 tLast|BTCF_Valid
30d30 4f 76 66 6c 7c 42 54 43 46 5f 56 61 6c 69 64 4e Ovfl|BTCF_ValidN
30d40 4b 65 79 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 Key))==0 );. as
30d50 73 65 72 74 28 20 70 43 75 72 2d 3e 69 6e 66 6f sert( pCur->info
30d60 2e 6e 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 69 .nSize==0 );. i
30d70 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 f( pCur->eState!
30d80 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b =CURSOR_VALID ){
30d90 0a 20 20 20 20 72 63 20 3d 20 72 65 73 74 6f 72 . rc = restor
30da0 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
30db0 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 20 72 pCur);. if( r
30dc0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
30dd0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
30de0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 43 . }. if( C
30df0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 URSOR_INVALID==p
30e00 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 Cur->eState ){.
30e10 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
30e20 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 TE_DONE;. }.
30e30 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 53 4b if( CURSOR_SK
30e40 49 50 4e 45 58 54 3d 3d 70 43 75 72 2d 3e 65 53 IPNEXT==pCur->eS
30e50 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 70 43 tate ){. pC
30e60 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
30e70 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 20 SOR_VALID;.
30e80 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e if( pCur->skipN
30e90 65 78 74 3c 30 20 29 20 72 65 74 75 72 6e 20 53 ext<0 ) return S
30ea0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
30eb0 20 20 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 }.. pPage = p
30ec0 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73 Cur->pPage;. as
30ed0 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
30ee0 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 nit );. if( !pP
30ef0 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
30f00 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
30f10 3e 69 78 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f >ix;. rc = mo
30f20 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
30f30 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c get4byte(findCel
30f40 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b l(pPage, idx)));
30f50 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
30f60 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 turn rc;. rc
30f70 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 = moveToRightmos
30f80 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 t(pCur);. }else
30f90 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 {. while( pCu
30fa0 72 2d 3e 69 78 3d 3d 30 20 29 7b 0a 20 20 20 20 r->ix==0 ){.
30fb0 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
30fc0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
30fd0 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
30fe0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
30ff0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
31000 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
31010 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 }. moveToP
31020 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 arent(pCur);.
31030 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
31040 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
31050 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
31060 28 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 ( (pCur->curFlag
31070 73 20 26 20 28 42 54 43 46 5f 56 61 6c 69 64 4f s & (BTCF_ValidO
31080 76 66 6c 29 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 vfl))==0 );..
31090 20 70 43 75 72 2d 3e 69 78 2d 2d 3b 0a 20 20 20 pCur->ix--;.
310a0 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 pPage = pCur->p
310b0 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 70 50 Page;. if( pP
310c0 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 age->intKey && !
310d0 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
310e0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
310f0 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 3BtreePrevious(p
31100 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c Cur, 0);. }el
31110 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
31120 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
31130 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
31140 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 .}.int sqlite3Bt
31150 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 reePrevious(BtCu
31160 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
31170 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65 72 74 flags){. assert
31180 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 ( cursorOwnsBtSh
31190 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 ared(pCur) );.
311a0 61 73 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 30 assert( flags==0
311b0 20 7c 7c 20 66 6c 61 67 73 3d 3d 31 20 29 3b 0a || flags==1 );.
311c0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
311d0 45 52 28 20 66 6c 61 67 73 20 29 3b 20 20 2f 2a ER( flags ); /*
311e0 20 55 73 65 64 20 69 6e 20 43 4f 4d 44 42 32 20 Used in COMDB2
311f0 62 75 74 20 6e 6f 74 20 6e 61 74 69 76 65 20 53 but not native S
31200 51 4c 69 74 65 20 2a 2f 0a 20 20 70 43 75 72 2d QLite */. pCur-
31210 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 >curFlags &= ~(B
31220 54 43 46 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f TCF_AtLast|BTCF_
31230 56 61 6c 69 64 4f 76 66 6c 7c 42 54 43 46 5f 56 ValidOvfl|BTCF_V
31240 61 6c 69 64 4e 4b 65 79 29 3b 0a 20 20 70 43 75 alidNKey);. pCu
31250 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
31260 30 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 0;. if( pCur->e
31270 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 State!=CURSOR_VA
31280 4c 49 44 0a 20 20 20 7c 7c 20 70 43 75 72 2d 3e LID. || pCur->
31290 69 78 3d 3d 30 0a 20 20 20 7c 7c 20 70 43 75 72 ix==0. || pCur
312a0 2d 3e 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 ->pPage->leaf==0
312b0 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
312c0 20 62 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 btreePrevious(p
312d0 43 75 72 29 3b 0a 20 20 7d 0a 20 20 70 43 75 72 Cur);. }. pCur
312e0 2d 3e 69 78 2d 2d 3b 0a 20 20 72 65 74 75 72 6e ->ix--;. return
312f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
31300 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 *.** Allocate a
31310 6e 65 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 new page from th
31320 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
31330 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 .**.** The new p
31340 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 age is marked as
31350 20 64 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 dirty. (In oth
31360 65 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 er words, sqlite
31370 33 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 3PagerWrite().**
31380 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
31390 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 n called on the
313a0 6e 65 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 new page.) The
313b0 6e 65 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 new page has als
313c0 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 o.** been refere
313d0 6e 63 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c nced and the cal
313e0 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 ling routine is
313f0 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
31400 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 calling.** sqlit
31410 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f e3PagerUnref() o
31420 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 n the new page w
31430 68 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a hen it is done..
31440 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
31450 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
31460 75 63 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 uccess. Any oth
31470 65 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 er return value
31480 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 indicates.** an
31490 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 error. *ppPage
314a0 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 69 is set to NULL i
314b0 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
314c0 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 n error..**.** I
314d0 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 f the "nearby" p
314e0 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 arameter is not
314f0 30 2c 20 74 68 65 6e 20 61 6e 20 65 66 66 6f 72 0, then an effor
31500 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a t is made to .**
31510 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 locate a page c
31520 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 lose to the page
31530 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 number "nearby"
31540 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
31550 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 sed in an.** att
31560 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c empt to keep rel
31570 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 ated pages close
31580 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 to each other i
31590 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
315a0 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e ile,.** which in
315b0 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 turn can make d
315c0 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66 atabase access f
315d0 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 aster..**.** If
315e0 74 68 65 20 65 4d 6f 64 65 20 70 61 72 61 6d 65 the eMode parame
315f0 74 65 72 20 69 73 20 42 54 41 4c 4c 4f 43 5f 45 ter is BTALLOC_E
31600 58 41 43 54 20 61 6e 64 20 74 68 65 20 6e 65 61 XACT and the nea
31610 72 62 79 20 70 61 67 65 20 65 78 69 73 74 73 0a rby page exists.
31620 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 ** anywhere on t
31630 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 he free-list, th
31640 65 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 en it is guarant
31650 65 65 64 20 74 6f 20 62 65 20 72 65 74 75 72 6e eed to be return
31660 65 64 2e 20 20 49 66 0a 2a 2a 20 65 4d 6f 64 65 ed. If.** eMode
31670 20 69 73 20 42 54 41 4c 4c 4f 43 5f 4c 54 20 74 is BTALLOC_LT t
31680 68 65 6e 20 74 68 65 20 70 61 67 65 20 72 65 74 hen the page ret
31690 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 6c 65 urned will be le
316a0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
316b0 0a 2a 2a 20 74 6f 20 6e 65 61 72 62 79 20 69 66 .** to nearby if
316c0 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 20 65 any such page e
316d0 78 69 73 74 73 2e 20 20 49 66 20 65 4d 6f 64 65 xists. If eMode
316e0 20 69 73 20 42 54 41 4c 4c 4f 43 5f 41 4e 59 20 is BTALLOC_ANY
316f0 74 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61 72 then there.** ar
31700 65 20 6e 6f 20 72 65 73 74 72 69 63 74 69 6f 6e e no restriction
31710 73 20 6f 6e 20 77 68 69 63 68 20 70 61 67 65 20 s on which page
31720 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
31730 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 static int alloc
31740 61 74 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 ateBtreePage(.
31750 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
31760 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
31770 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ree */. MemPage
31780 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 20 **ppPage,
31790 2f 2a 20 53 74 6f 72 65 20 70 6f 69 6e 74 65 72 /* Store pointer
317a0 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 to the allocate
317b0 64 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 d page here */.
317c0 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 20 20 Pgno *pPgno,
317d0 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 /* Store
317e0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
317f0 20 68 65 72 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 here */. Pgno
31800 6e 65 61 72 62 79 2c 20 20 20 20 20 20 20 20 20 nearby,
31810 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 /* Search for
31820 61 20 70 61 67 65 20 6e 65 61 72 20 74 68 69 73 a page near this
31830 20 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 65 4d 6f one */. u8 eMo
31840 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 de
31850 20 2f 2a 20 42 54 41 4c 4c 4f 43 5f 45 58 41 43 /* BTALLOC_EXAC
31860 54 2c 20 42 54 41 4c 4c 4f 43 5f 4c 54 2c 20 6f T, BTALLOC_LT, o
31870 72 20 42 54 41 4c 4c 4f 43 5f 41 4e 59 20 2a 2f r BTALLOC_ANY */
31880 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
31890 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b Page1;. int rc;
318a0 0a 20 20 75 33 32 20 6e 3b 20 20 20 20 20 2f 2a . u32 n; /*
318b0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
318c0 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
318d0 20 2a 2f 0a 20 20 75 33 32 20 6b 3b 20 20 20 20 */. u32 k;
318e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 /* Number of le
318f0 61 76 65 73 20 6f 6e 20 74 68 65 20 74 72 75 6e aves on the trun
31900 6b 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 k of the freelis
31910 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a t */. MemPage *
31920 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 pTrunk = 0;. Me
31930 6d 50 61 67 65 20 2a 70 50 72 65 76 54 72 75 6e mPage *pPrevTrun
31940 6b 20 3d 20 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 k = 0;. Pgno mx
31950 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 Page; /* Tot
31960 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 al size of the d
31970 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
31980 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
31990 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
319a0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
319b0 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 42 54 ssert( eMode==BT
319c0 41 4c 4c 4f 43 5f 41 4e 59 20 7c 7c 20 28 6e 65 ALLOC_ANY || (ne
319d0 61 72 62 79 3e 30 20 26 26 20 49 66 4e 6f 74 4f arby>0 && IfNotO
319e0 6d 69 74 41 56 28 70 42 74 2d 3e 61 75 74 6f 56 mitAV(pBt->autoV
319f0 61 63 75 75 6d 29 29 20 29 3b 0a 20 20 70 50 61 acuum)) );. pPa
31a00 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 ge1 = pBt->pPage
31a10 31 3b 0a 20 20 6d 78 50 61 67 65 20 3d 20 62 74 1;. mxPage = bt
31a20 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 reePagecount(pBt
31a30 29 3b 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 );. /* EVIDENCE
31a40 2d 4f 46 3a 20 52 2d 30 35 31 31 39 2d 30 32 36 -OF: R-05119-026
31a50 33 37 20 54 68 65 20 34 2d 62 79 74 65 20 62 69 37 The 4-byte bi
31a60 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
31a70 20 61 74 20 6f 66 66 73 65 74 20 33 36 0a 20 20 at offset 36.
31a80 2a 2a 20 73 74 6f 72 65 73 20 73 74 6f 72 65 73 ** stores stores
31a90 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
31aa0 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 r of pages on th
31ab0 65 20 66 72 65 65 6c 69 73 74 2e 20 2a 2f 0a 20 e freelist. */.
31ac0 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 n = get4byte(&p
31ad0 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
31ae0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
31af0 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20 ==mxPage-1 );.
31b00 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 7b if( n>=mxPage ){
31b10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
31b20 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
31b30 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 29 . }. if( n>0 )
31b40 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 {. /* There a
31b50 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 re pages on the
31b60 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 freelist. Reuse
31b70 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 one of those pa
31b80 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f ges. */. Pgno
31b90 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 iTrunk;. u8
31ba0 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 searchList = 0;
31bb0 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c /* If the free-l
31bc0 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 ist must be sear
31bd0 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 ched for 'nearby
31be0 27 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 53 65 ' */. u32 nSe
31bf0 61 72 63 68 20 3d 20 30 3b 20 20 20 2f 2a 20 43 arch = 0; /* C
31c00 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 ount of the numb
31c10 65 72 20 6f 66 20 73 65 61 72 63 68 20 61 74 74 er of search att
31c20 65 6d 70 74 73 20 2a 2f 0a 20 20 20 20 0a 20 20 empts */. .
31c30 20 20 2f 2a 20 49 66 20 65 4d 6f 64 65 3d 3d 42 /* If eMode==B
31c40 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 61 6e 64 TALLOC_EXACT and
31c50 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 a query of the
31c60 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 pointer-map.
31c70 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 ** shows that th
31c80 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 e page 'nearby'
31c90 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 is somewhere on
31ca0 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 the free-list, t
31cb0 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 hen. ** the e
31cc0 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 ntire-list will
31cd0 62 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 be searched for
31ce0 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a that page.. *
31cf0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
31d00 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
31d10 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d . if( eMode==
31d20 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 29 7b BTALLOC_EXACT ){
31d30 0a 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 . if( nearb
31d40 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 y<=mxPage ){.
31d50 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 u8 eType;.
31d60 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
31d70 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 earby>0 );.
31d80 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
31d90 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 autoVacuum );.
31da0 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
31db0 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 pGet(pBt, nearby
31dc0 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 , &eType, 0);.
31dd0 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
31de0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
31df0 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
31e00 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
31e10 20 20 20 20 20 20 20 20 20 20 73 65 61 72 63 68 search
31e20 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 List = 1;.
31e30 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
31e40 7d 65 6c 73 65 20 69 66 28 20 65 4d 6f 64 65 3d }else if( eMode=
31e50 3d 42 54 41 4c 4c 4f 43 5f 4c 45 20 29 7b 0a 20 =BTALLOC_LE ){.
31e60 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 searchList
31e70 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 = 1;. }.#endi
31e80 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d f.. /* Decrem
31e90 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 ent the free-lis
31ea0 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 t count by 1. Se
31eb0 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 t iTrunk to the
31ec0 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 index of the.
31ed0 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c ** first free-l
31ee0 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 ist trunk page.
31ef0 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e iPrevTrunk is in
31f00 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a itially 1.. *
31f10 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
31f20 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
31f30 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
31f40 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
31f50 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62 rn rc;. put4b
31f60 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
31f70 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 ta[36], n-1);..
31f80 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 /* The code w
31f90 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 ithin this loop
31fa0 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 is run only once
31fb0 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c if the 'searchL
31fc0 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20 ist' variable.
31fd0 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 ** is not true
31fe0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
31ff0 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 runs once for ea
32000 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e ch trunk-page on
32010 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 the. ** free
32020 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20 -list until the
32030 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 page 'nearby' is
32040 20 6c 6f 63 61 74 65 64 20 28 65 4d 6f 64 65 3d located (eMode=
32050 3d 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 29 0a =BTALLOC_EXACT).
32060 20 20 20 20 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 ** or until
32070 61 20 70 61 67 65 20 6c 65 73 73 20 74 68 61 6e a page less than
32080 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63 'nearby' is loc
32090 61 74 65 64 20 28 65 4d 6f 64 65 3d 3d 42 54 41 ated (eMode==BTA
320a0 4c 4c 4f 43 5f 4c 54 29 0a 20 20 20 20 2a 2f 0a LLOC_LT). */.
320b0 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 do {. p
320c0 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 PrevTrunk = pTru
320d0 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 nk;. if( pP
320e0 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 revTrunk ){.
320f0 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
32100 4f 46 3a 20 52 2d 30 31 35 30 36 2d 31 31 30 35 OF: R-01506-1105
32110 33 20 54 68 65 20 66 69 72 73 74 20 69 6e 74 65 3 The first inte
32120 67 65 72 20 6f 6e 20 61 20 66 72 65 65 6c 69 73 ger on a freelis
32130 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 20 20 20 t trunk page.
32140 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 70 ** is the p
32150 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
32160 65 20 6e 65 78 74 20 66 72 65 65 6c 69 73 74 20 e next freelist
32170 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 trunk page in th
32180 65 20 6c 69 73 74 20 6f 72 0a 20 20 20 20 20 20 e list or.
32190 20 20 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 69 ** zero if thi
321a0 73 20 69 73 20 74 68 65 20 6c 61 73 74 20 66 72 s is the last fr
321b0 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 eelist trunk pag
321c0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 54 e. */. iT
321d0 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 runk = get4byte(
321e0 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
321f0 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 ta[0]);. }e
32200 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
32210 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 35 EVIDENCE-OF: R-5
32220 39 38 34 31 2d 31 33 37 39 38 20 54 68 65 20 34 9841-13798 The 4
32230 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e -byte big-endian
32240 20 69 6e 74 65 67 65 72 20 61 74 20 6f 66 66 73 integer at offs
32250 65 74 20 33 32 0a 20 20 20 20 20 20 20 20 2a 2a et 32. **
32260 20 73 74 6f 72 65 73 20 74 68 65 20 70 61 67 65 stores the page
32270 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 number of the f
32280 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
32290 20 66 72 65 65 6c 69 73 74 2c 20 6f 72 20 7a 65 freelist, or ze
322a0 72 6f 20 69 66 0a 20 20 20 20 20 20 20 20 2a 2a ro if. **
322b0 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 the freelist is
322c0 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20 20 20 empty. */.
322d0 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 iTrunk = get4
322e0 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 byte(&pPage1->aD
322f0 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 ata[32]);.
32300 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 }. testcase
32310 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 ( iTrunk==mxPage
32320 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 54 );. if( iT
32330 72 75 6e 6b 3e 6d 78 50 61 67 65 20 7c 7c 20 6e runk>mxPage || n
32340 53 65 61 72 63 68 2b 2b 20 3e 20 6e 20 29 7b 0a Search++ > n ){.
32350 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
32360 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f ITE_CORRUPT_PGNO
32370 28 70 50 72 65 76 54 72 75 6e 6b 20 3f 20 70 50 (pPrevTrunk ? pP
32380 72 65 76 54 72 75 6e 6b 2d 3e 70 67 6e 6f 20 3a revTrunk->pgno :
32390 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 1);. }else
323a0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 {. rc = b
323b0 74 72 65 65 47 65 74 55 6e 75 73 65 64 50 61 67 treeGetUnusedPag
323c0 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 e(pBt, iTrunk, &
323d0 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 pTrunk, 0);.
323e0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
323f0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72 75 ){. pTru
32400 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nk = 0;.
32410 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
32420 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a e_page;. }.
32430 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 assert( pT
32440 72 75 6e 6b 21 3d 30 20 29 3b 0a 20 20 20 20 20 runk!=0 );.
32450 20 61 73 73 65 72 74 28 20 70 54 72 75 6e 6b 2d assert( pTrunk-
32460 3e 61 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 20 >aData!=0 );.
32470 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f /* EVIDENCE-O
32480 46 3a 20 52 2d 31 33 35 32 33 2d 30 34 33 39 34 F: R-13523-04394
32490 20 54 68 65 20 73 65 63 6f 6e 64 20 69 6e 74 65 The second inte
324a0 67 65 72 20 6f 6e 20 61 20 66 72 65 65 6c 69 73 ger on a freelis
324b0 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 20 20 20 t trunk page.
324c0 20 20 20 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d ** is the num
324d0 62 65 72 20 6f 66 20 6c 65 61 66 20 70 61 67 65 ber of leaf page
324e0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 6f 6c pointers to fol
324f0 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 6b 20 low. */. k
32500 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 = get4byte(&pTru
32510 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 nk->aData[4]);.
32520 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 if( k==0 &&
32530 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b 0a !searchList ){.
32540 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
32550 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 runk has no leav
32560 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74 20 es and the list
32570 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 is not being sea
32580 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20 20 rched. .
32590 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74 68 ** So extract th
325a0 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74 73 e trunk page its
325b0 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20 61 elf and use it a
325c0 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 s the newly .
325d0 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 ** allocate
325e0 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 d page */.
325f0 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76 54 assert( pPrevT
32600 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 runk==0 );.
32610 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
32620 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b agerWrite(pTrunk
32630 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
32640 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
32650 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
32660 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
32670 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
32680 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e *pPgno = iTrun
32690 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 k;. memcp
326a0 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 y(&pPage1->aData
326b0 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 [32], &pTrunk->a
326c0 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 Data[0], 4);.
326d0 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 *ppPage = p
326e0 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 Trunk;. p
326f0 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 Trunk = 0;.
32700 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 TRACE(("ALLOC
32710 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 ATE: %d trunk -
32720 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 %d free pages le
32730 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e ft\n", *pPgno, n
32740 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 -1));. }els
32750 65 20 69 66 28 20 6b 3e 28 75 33 32 29 28 70 42 e if( k>(u32)(pB
32760 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 t->usableSize/4
32770 2d 20 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 - 2) ){.
32780 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 /* Value of k is
32790 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 out of range.
327a0 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 Database corrupt
327b0 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 ion */. r
327c0 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
327d0 50 54 5f 50 47 4e 4f 28 69 54 72 75 6e 6b 29 3b PT_PGNO(iTrunk);
327e0 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
327f0 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
32800 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
32810 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
32820 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
32830 73 65 61 72 63 68 4c 69 73 74 20 0a 20 20 20 20 searchList .
32840 20 20 20 20 20 20 20 20 26 26 20 28 6e 65 61 72 && (near
32850 62 79 3d 3d 69 54 72 75 6e 6b 20 7c 7c 20 28 69 by==iTrunk || (i
32860 54 72 75 6e 6b 3c 6e 65 61 72 62 79 20 26 26 20 Trunk<nearby &&
32870 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c eMode==BTALLOC_L
32880 45 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 E)) . ){.
32890 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 /* The lis
328a0 74 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 t is being searc
328b0 68 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 hed and this tru
328c0 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 nk page is the p
328d0 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 age. ** t
328e0 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 o allocate, rega
328f0 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
32900 72 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e r it has leaves.
32910 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
32920 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 *pPgno = iTr
32930 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 unk;. *pp
32940 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 Page = pTrunk;.
32950 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 searchLis
32960 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 t = 0;. r
32970 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
32980 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 Write(pTrunk->pD
32990 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
329a0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
329b0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
329c0 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
329d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
329e0 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( k==0 ){.
329f0 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 if( !pPrevTr
32a00 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 unk ){.
32a10 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
32a20 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 1->aData[32], &p
32a30 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c Trunk->aData[0],
32a40 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 4);. }
32a50 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
32a60 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
32a70 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54 72 gerWrite(pPrevTr
32a80 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
32a90 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
32aa0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
32ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f go
32ac0 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
32ad0 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 page;.
32ae0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
32af0 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 memcpy(&pPrevTru
32b00 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 nk->aData[0], &p
32b10 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c Trunk->aData[0],
32b20 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 4);. }
32b30 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
32b40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
32b50 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 trunk page is r
32b60 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63 equired by the c
32b70 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e aller but it con
32b80 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 tains .
32b90 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ** pointers to
32ba0 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 free-list leaves
32bb0 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 . The first leaf
32bc0 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b becomes a trunk
32bd0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 . ** pa
32be0 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ge in this case.
32bf0 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 . */.
32c00 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 MemPage
32c10 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 *pNewTrunk;.
32c20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 Pgno iNewT
32c30 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 runk = get4byte(
32c40 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 &pTrunk->aData[8
32c50 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ]);. if
32c60 28 20 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 50 61 ( iNewTrunk>mxPa
32c70 67 65 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 ge ){ .
32c80 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
32c90 4f 52 52 55 50 54 5f 50 47 4e 4f 28 69 54 72 75 ORRUPT_PGNO(iTru
32ca0 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nk);.
32cb0 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
32cc0 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
32cd0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 74 }. t
32ce0 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 75 estcase( iNewTru
32cf0 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 nk==mxPage );.
32d00 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 rc = btr
32d10 65 65 47 65 74 55 6e 75 73 65 64 50 61 67 65 28 eeGetUnusedPage(
32d20 70 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 pBt, iNewTrunk,
32d30 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a &pNewTrunk, 0);.
32d40 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
32d50 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
32d60 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
32d70 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
32d80 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 e;. }.
32d90 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
32da0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
32db0 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 pNewTrunk->pDbPa
32dc0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
32dd0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
32de0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
32df0 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
32e00 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
32e10 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
32e20 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
32e30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
32e40 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 memcpy(&pNewTr
32e50 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 unk->aData[0], &
32e60 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
32e70 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 4);.
32e80 70 75 74 34 62 79 74 65 28 26 70 4e 65 77 54 72 put4byte(&pNewTr
32e90 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b unk->aData[4], k
32ea0 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d -1);. m
32eb0 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b emcpy(&pNewTrunk
32ec0 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 ->aData[8], &pTr
32ed0 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 unk->aData[12],
32ee0 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 (k-1)*4);.
32ef0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
32f00 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 pNewTrunk);.
32f10 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 if( !pPrev
32f20 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 Trunk ){.
32f30 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
32f40 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
32f50 61 62 6c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 able(pPage1->pDb
32f60 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 Page) );.
32f70 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
32f80 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
32f90 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 , iNewTrunk);.
32fa0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
32fb0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
32fc0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
32fd0 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 e(pPrevTrunk->pD
32fe0 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
32ff0 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
33000 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
33010 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
33020 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge;.
33030 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 }. pu
33040 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 t4byte(&pPrevTru
33050 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e nk->aData[0], iN
33060 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
33070 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
33080 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
33090 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 0;. TRAC
330a0 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 E(("ALLOCATE: %d
330b0 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 trunk - %d free
330c0 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 pages left\n",
330d0 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 *pPgno, n-1));.#
330e0 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 endif. }els
330f0 65 20 69 66 28 20 6b 3e 30 20 29 7b 0a 20 20 20 e if( k>0 ){.
33100 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 /* Extract
33110 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20 a leaf from the
33120 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 trunk */.
33130 20 75 33 32 20 63 6c 6f 73 65 73 74 3b 0a 20 20 u32 closest;.
33140 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65 Pgno iPage
33150 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e ;. unsign
33160 65 64 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d ed char *aData =
33170 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a pTrunk->aData;.
33180 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 if( near
33190 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 by>0 ){.
331a0 20 20 75 33 32 20 69 3b 0a 20 20 20 20 20 20 20 u32 i;.
331b0 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a closest = 0;.
331c0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 4d if( eM
331d0 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 45 20 ode==BTALLOC_LE
331e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 ){. f
331f0 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b or(i=0; i<k; i++
33200 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
33210 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 iPage = get4byt
33220 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 e(&aData[8+i*4])
33230 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
33240 69 66 28 20 69 50 61 67 65 3c 3d 6e 65 61 72 62 if( iPage<=nearb
33250 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 y ){.
33260 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 closest = i
33270 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
33280 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
33290 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
332a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
332b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
332c0 20 20 20 20 69 6e 74 20 64 69 73 74 3b 0a 20 20 int dist;.
332d0 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d dist =
332e0 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 32 sqlite3AbsInt32
332f0 28 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 (get4byte(&aData
33300 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 29 3b 0a [8]) - nearby);.
33310 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 for(
33320 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a i=1; i<k; i++){.
33330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
33340 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 41 62 t d2 = sqlite3Ab
33350 73 49 6e 74 33 32 28 67 65 74 34 62 79 74 65 28 sInt32(get4byte(
33360 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d &aData[8+i*4]) -
33370 20 6e 65 61 72 62 79 29 3b 0a 20 20 20 20 20 20 nearby);.
33380 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 if( d2<d
33390 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ist ){.
333a0 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d closest =
333b0 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 i;.
333c0 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 dist = d2;.
333d0 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
333e0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
333f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
33400 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
33410 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 closest = 0;.
33420 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
33430 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 79 iPage = get4by
33440 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 te(&aData[8+clos
33450 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 est*4]);.
33460 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67 65 testcase( iPage
33470 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 ==mxPage );.
33480 20 20 20 20 69 66 28 20 69 50 61 67 65 3e 6d 78 if( iPage>mx
33490 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
334a0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
334b0 52 52 55 50 54 5f 50 47 4e 4f 28 69 54 72 75 6e RRUPT_PGNO(iTrun
334c0 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f k);. go
334d0 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
334e0 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a page;. }.
334f0 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
33500 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 ( iPage==mxPage
33510 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 );. if( !
33520 73 65 61 72 63 68 4c 69 73 74 20 0a 20 20 20 20 searchList .
33530 20 20 20 20 20 7c 7c 20 28 69 50 61 67 65 3d 3d || (iPage==
33540 6e 65 61 72 62 79 20 7c 7c 20 28 69 50 61 67 65 nearby || (iPage
33550 3c 6e 65 61 72 62 79 20 26 26 20 65 4d 6f 64 65 <nearby && eMode
33560 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 45 29 29 20 0a ==BTALLOC_LE)) .
33570 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
33580 20 20 20 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 int noConte
33590 6e 74 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 nt;. *p
335a0 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 Pgno = iPage;.
335b0 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
335c0 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 77 61 73 ALLOCATE: %d was
335d0 20 6c 65 61 66 20 25 64 20 6f 66 20 25 64 20 6f leaf %d of %d o
335e0 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 n trunk %d".
335f0 20 20 20 20 20 20 20 20 20 20 20 20 20 22 3a 20 ":
33600 25 64 20 6d 6f 72 65 20 66 72 65 65 20 70 61 67 %d more free pag
33610 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 es\n",.
33620 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 *pPgno,
33630 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 closest+1, k, pT
33640 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 runk->pgno, n-1)
33650 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
33660 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
33670 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 ite(pTrunk->pDbP
33680 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
33690 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e if( rc ) goto en
336a0 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
336b0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 . if( c
336c0 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 losest<k-1 ){.
336d0 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
336e0 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 (&aData[8+closes
336f0 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b t*4], &aData[4+k
33700 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 *4], 4);.
33710 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
33720 75 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 ut4byte(&aData[4
33730 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 ], k-1);.
33740 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 20 3d 20 21 noContent = !
33750 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65 btreeGetHasConte
33760 6e 74 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 3f nt(pBt, *pPgno)?
33770 20 50 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e PAGER_GET_NOCON
33780 54 45 4e 54 20 3a 20 30 3b 0a 20 20 20 20 20 20 TENT : 0;.
33790 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
337a0 74 55 6e 75 73 65 64 50 61 67 65 28 70 42 74 2c tUnusedPage(pBt,
337b0 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c *pPgno, ppPage,
337c0 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 noContent);.
337d0 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
337e0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
337f0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
33800 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28 ite3PagerWrite((
33810 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 *ppPage)->pDbPag
33820 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
33830 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
33840 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
33850 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
33860 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 ppPage);.
33870 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d *ppPage =
33880 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
33890 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
338a0 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 searchLi
338b0 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 st = 0;.
338c0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
338d0 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 releasePage(pPre
338e0 76 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 vTrunk);. p
338f0 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 PrevTrunk = 0;.
33900 20 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 63 }while( searc
33910 68 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 hList );. }else
33920 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 {. /* There a
33930 72 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 re no pages on t
33940 68 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 he freelist, so
33950 61 70 70 65 6e 64 20 61 20 6e 65 77 20 70 61 67 append a new pag
33960 65 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 e to the. **
33970 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2e 0a database image..
33980 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
33990 72 6d 61 6c 6c 79 2c 20 6e 65 77 20 70 61 67 65 rmally, new page
339a0 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 s allocated by t
339b0 68 69 73 20 62 6c 6f 63 6b 20 63 61 6e 20 62 65 his block can be
339c0 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
339d0 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 the. ** pager
339e0 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 layer with the
339f0 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 'no-content' fla
33a00 67 20 73 65 74 2e 20 54 68 69 73 20 70 72 65 76 g set. This prev
33a10 65 6e 74 73 20 74 68 65 20 70 61 67 65 72 0a 20 ents the pager.
33a20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e ** from tryin
33a30 67 20 74 6f 20 72 65 61 64 20 74 68 65 20 70 61 g to read the pa
33a40 67 65 73 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d ges content from
33a50 20 64 69 73 6b 2e 20 48 6f 77 65 76 65 72 2c 20 disk. However,
33a60 69 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 75 if the. ** cu
33a70 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
33a80 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 72 75 n has already ru
33a90 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e n one or more in
33aa0 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d cremental-vacuum
33ab0 0a 20 20 20 20 2a 2a 20 73 74 65 70 73 2c 20 74 . ** steps, t
33ac0 68 65 6e 20 74 68 65 20 70 61 67 65 20 77 65 20 hen the page we
33ad0 61 72 65 20 61 62 6f 75 74 20 74 6f 20 61 6c 6c are about to all
33ae0 6f 63 61 74 65 20 6d 61 79 20 63 6f 6e 74 61 69 ocate may contai
33af0 6e 20 63 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a n content. **
33b00 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 that is require
33b10 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f d in the event o
33b20 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 6e f a rollback. In
33b30 20 74 68 69 73 20 63 61 73 65 2c 20 64 6f 0a 20 this case, do.
33b40 20 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20 74 68 ** not set th
33b50 65 20 6e 6f 2d 63 6f 6e 74 65 6e 74 20 66 6c 61 e no-content fla
33b60 67 2e 20 54 68 69 73 20 63 61 75 73 65 73 20 74 g. This causes t
33b70 68 65 20 70 61 67 65 72 20 74 6f 20 6c 6f 61 64 he pager to load
33b80 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 and journal.
33b90 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 ** the current
33ba0 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 page content bef
33bb0 6f 72 65 20 6f 76 65 72 77 72 69 74 69 6e 67 20 ore overwriting
33bc0 69 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a it.. **. *
33bd0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
33be0 70 61 67 65 72 20 77 69 6c 6c 20 6e 6f 74 20 61 pager will not a
33bf0 63 74 75 61 6c 6c 79 20 61 74 74 65 6d 70 74 20 ctually attempt
33c00 74 6f 20 6c 6f 61 64 20 6f 72 20 6a 6f 75 72 6e to load or journ
33c10 61 6c 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 65 al . ** conte
33c20 6e 74 20 66 6f 72 20 61 6e 79 20 70 61 67 65 20 nt for any page
33c30 74 68 61 74 20 72 65 61 6c 6c 79 20 64 6f 65 73 that really does
33c40 20 6c 69 65 20 70 61 73 74 20 74 68 65 20 65 6e lie past the en
33c50 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 d of the databas
33c60 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 6f 6e e. ** file on
33c70 20 64 69 73 6b 2e 20 53 6f 20 74 68 65 20 65 66 disk. So the ef
33c80 66 65 63 74 73 20 6f 66 20 64 69 73 61 62 6c 69 fects of disabli
33c90 6e 67 20 74 68 65 20 6e 6f 2d 63 6f 6e 74 65 6e ng the no-conten
33ca0 74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 t optimization.
33cb0 20 20 20 2a 2a 20 68 65 72 65 20 61 72 65 20 63 ** here are c
33cc0 6f 6e 66 69 6e 65 64 20 74 6f 20 74 68 6f 73 65 onfined to those
33cd0 20 70 61 67 65 73 20 74 68 61 74 20 6c 69 65 20 pages that lie
33ce0 62 65 74 77 65 65 6e 20 74 68 65 20 65 6e 64 20 between the end
33cf0 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 of the. ** da
33d00 74 61 62 61 73 65 20 69 6d 61 67 65 20 61 6e 64 tabase image and
33d10 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
33d20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 database file..
33d30 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 4e */. int bN
33d40 6f 43 6f 6e 74 65 6e 74 20 3d 20 28 30 3d 3d 49 oContent = (0==I
33d50 66 4e 6f 74 4f 6d 69 74 41 56 28 70 42 74 2d 3e fNotOmitAV(pBt->
33d60 62 44 6f 54 72 75 6e 63 61 74 65 29 29 3f 20 50 bDoTruncate))? P
33d70 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 AGER_GET_NOCONTE
33d80 4e 54 3a 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20 NT:0;.. rc =
33d90 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
33da0 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 e(pBt->pPage1->p
33db0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
33dc0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
33dd0 0a 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 2b . pBt->nPage+
33de0 2b 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e +;. if( pBt->
33df0 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e 47 5f 42 nPage==PENDING_B
33e00 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 20 YTE_PAGE(pBt) )
33e10 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 0a 23 pBt->nPage++;..#
33e20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
33e30 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
33e40 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
33e50 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f acuum && PTRMAP_
33e60 49 53 50 41 47 45 28 70 42 74 2c 20 70 42 74 2d ISPAGE(pBt, pBt-
33e70 3e 6e 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 >nPage) ){.
33e80 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 /* If *pPgno re
33e90 66 65 72 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 fers to a pointe
33ea0 72 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f r-map page, allo
33eb0 63 61 74 65 20 74 77 6f 20 6e 65 77 20 70 61 67 cate two new pag
33ec0 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 74 es. ** at t
33ed0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 he end of the fi
33ee0 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e le instead of on
33ef0 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 6c 6c e. The first all
33f00 6f 63 61 74 65 64 20 70 61 67 65 0a 20 20 20 20 ocated page.
33f10 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e ** becomes a n
33f20 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 ew pointer-map p
33f30 61 67 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 age, the second
33f40 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 63 is used by the c
33f50 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a aller.. */.
33f60 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
33f70 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 54 52 Pg = 0;. TR
33f80 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 ACE(("ALLOCATE:
33f90 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 %d from end of f
33fa0 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d 61 70 ile (pointer-map
33fb0 20 70 61 67 65 29 5c 6e 22 2c 20 70 42 74 2d 3e page)\n", pBt->
33fc0 6e 50 61 67 65 29 29 3b 0a 20 20 20 20 20 20 61 nPage));. a
33fd0 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 50 61 67 ssert( pBt->nPag
33fe0 65 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f e!=PENDING_BYTE_
33ff0 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 PAGE(pBt) );.
34000 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
34010 55 6e 75 73 65 64 50 61 67 65 28 70 42 74 2c 20 UnusedPage(pBt,
34020 70 42 74 2d 3e 6e 50 61 67 65 2c 20 26 70 50 67 pBt->nPage, &pPg
34030 2c 20 62 4e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 , bNoContent);.
34040 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
34050 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
34060 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
34070 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 gerWrite(pPg->pD
34080 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
34090 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 releasePage(pPg)
340a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
340b0 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
340c0 72 63 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e rc;. pBt->n
340d0 50 61 67 65 2b 2b 3b 0a 20 20 20 20 20 20 69 66 Page++;. if
340e0 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 ( pBt->nPage==PE
340f0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
34100 70 42 74 29 20 29 7b 20 70 42 74 2d 3e 6e 50 61 pBt) ){ pBt->nPa
34110 67 65 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 ge++; }. }.#e
34120 6e 64 69 66 0a 20 20 20 20 70 75 74 34 62 79 74 ndif. put4byt
34130 65 28 32 38 20 2b 20 28 75 38 2a 29 70 42 74 2d e(28 + (u8*)pBt-
34140 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 2c 20 >pPage1->aData,
34150 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 20 pBt->nPage);.
34160 20 2a 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e *pPgno = pBt->n
34170 50 61 67 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 Page;.. asser
34180 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 t( *pPgno!=PENDI
34190 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
341a0 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 ) );. rc = bt
341b0 72 65 65 47 65 74 55 6e 75 73 65 64 50 61 67 65 reeGetUnusedPage
341c0 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 (pBt, *pPgno, pp
341d0 50 61 67 65 2c 20 62 4e 6f 43 6f 6e 74 65 6e 74 Page, bNoContent
341e0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
341f0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 return rc;. r
34200 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
34210 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d Write((*ppPage)-
34220 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
34230 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
34240 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 ){. releas
34250 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a ePage(*ppPage);.
34260 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
34270 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41 0;. }. TRA
34280 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 CE(("ALLOCATE: %
34290 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 d from end of fi
342a0 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b le\n", *pPgno));
342b0 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
342c0 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 2a 70 CORRUPT_DB || *p
342d0 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
342e0 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
342f0 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 .end_allocate_pa
34300 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 ge:. releasePag
34310 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 6c e(pTrunk);. rel
34320 65 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72 easePage(pPrevTr
34330 75 6e 6b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 unk);. assert(
34340 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
34350 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
34360 65 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 eRefcount((*ppPa
34370 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3c 3d 31 ge)->pDbPage)<=1
34380 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 );. assert( rc
34390 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 !=SQLITE_OK || (
343a0 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 *ppPage)->isInit
343b0 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==0 );. return
343c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
343d0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
343e0 65 64 20 74 6f 20 61 64 64 20 70 61 67 65 20 69 ed to add page i
343f0 50 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 Page to the data
34400 62 61 73 65 20 66 69 6c 65 20 66 72 65 65 2d 6c base file free-l
34410 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69 73 20 61 ist. .** It is a
34420 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 ssumed that the
34430 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 page is not alre
34440 61 64 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 ady a part of th
34450 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 2a 2a 0a e free-list..**.
34460 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 70 61 73 ** The value pas
34470 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
34480 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
34490 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
344a0 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 66 20 74 ptional..** If t
344b0 68 65 20 63 61 6c 6c 65 72 20 68 61 70 70 65 6e he caller happen
344c0 73 20 74 6f 20 68 61 76 65 20 61 20 70 6f 69 6e s to have a poin
344d0 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 ter to the MemPa
344e0 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 63 6f ge object .** co
344f0 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 rresponding to p
34500 61 67 65 20 69 50 61 67 65 20 68 61 6e 64 79 2c age iPage handy,
34510 20 69 74 20 6d 61 79 20 70 61 73 73 20 69 74 20 it may pass it
34520 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 76 61 as the second va
34530 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 lue. .** Otherwi
34540 73 65 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 se, it may pass
34550 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 NULL..**.** If a
34560 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
34570 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 mPage object is
34580 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
34590 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 0a 2a cond argument,.*
345a0 2a 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 * its reference
345b0 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20 61 6c 74 count is not alt
345c0 65 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e ered by this fun
345d0 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
345e0 20 69 6e 74 20 66 72 65 65 50 61 67 65 32 28 42 int freePage2(B
345f0 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 4d 65 tShared *pBt, Me
34600 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61 67 65 2c mPage *pMemPage,
34610 20 50 67 6e 6f 20 69 50 61 67 65 29 7b 0a 20 20 Pgno iPage){.
34620 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 MemPage *pTrunk
34630 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
34640 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c 69 73 74 /* Free-list
34650 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 trunk page */.
34660 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20 3d 20 30 Pgno iTrunk = 0
34670 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
34680 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
34690 62 65 72 20 6f 66 20 66 72 65 65 2d 6c 69 73 74 ber of free-list
346a0 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 20 0a trunk page */ .
346b0 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
346c0 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
346d0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 72 /* Local r
346e0 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 eference to page
346f0 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 1 */. MemPage
34700 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 *pPage;
34710 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
34720 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 2e age being freed.
34730 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f May be NULL. */
34740 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
34750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34760 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
34770 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e Code */. int n
34780 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 Free;
34790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
347a0 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 * Initial number
347b0 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 66 72 65 of pages on fre
347c0 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 e-list */.. ass
347d0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
347e0 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
347f0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
34800 20 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 69 CORRUPT_DB || i
34810 50 61 67 65 3e 31 20 29 3b 0a 20 20 61 73 73 65 Page>1 );. asse
34820 72 74 28 20 21 70 4d 65 6d 50 61 67 65 20 7c 7c rt( !pMemPage ||
34830 20 70 4d 65 6d 50 61 67 65 2d 3e 70 67 6e 6f 3d pMemPage->pgno=
34840 3d 69 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 =iPage );.. if(
34850 20 69 50 61 67 65 3c 32 20 29 20 72 65 74 75 72 iPage<2 ) retur
34860 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
34870 5f 42 4b 50 54 3b 0a 20 20 69 66 28 20 70 4d 65 _BKPT;. if( pMe
34880 6d 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 mPage ){. pPa
34890 67 65 20 3d 20 70 4d 65 6d 50 61 67 65 3b 0a 20 ge = pMemPage;.
348a0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 sqlite3PagerR
348b0 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 ef(pPage->pDbPag
348c0 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e);. }else{.
348d0 20 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 pPage = btreePa
348e0 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c 20 69 50 geLookup(pBt, iP
348f0 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 age);. }.. /*
34900 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 Increment the fr
34910 65 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e ee page count on
34920 20 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 pPage1 */. rc
34930 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
34940 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 ite(pPage1->pDbP
34950 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
34960 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
34970 75 74 3b 0a 20 20 6e 46 72 65 65 20 3d 20 67 65 ut;. nFree = ge
34980 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
34990 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 70 75 aData[36]);. pu
349a0 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
349b0 61 44 61 74 61 5b 33 36 5d 2c 20 6e 46 72 65 65 aData[36], nFree
349c0 2b 31 29 3b 0a 0a 20 20 69 66 28 20 70 42 74 2d +1);.. if( pBt-
349d0 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f >btsFlags & BTS_
349e0 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 29 7b SECURE_DELETE ){
349f0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
34a00 65 63 75 72 65 5f 64 65 6c 65 74 65 20 6f 70 74 ecure_delete opt
34a10 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 ion is enabled,
34a20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 61 6c 77 61 then. ** alwa
34a30 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 69 ys fully overwri
34a40 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 te deleted infor
34a50 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f mation with zero
34a60 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
34a70 28 20 28 21 70 50 61 67 65 20 26 26 20 28 28 72 ( (!pPage && ((r
34a80 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
34a90 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 (pBt, iPage, &pP
34aa0 61 67 65 2c 20 30 29 29 21 3d 30 29 20 29 0a 20 age, 0))!=0) ).
34ab0 20 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 ||
34ac0 20 20 28 28 72 63 20 3d 20 73 71 6c 69 74 65 33 ((rc = sqlite3
34ad0 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
34ae0 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 29 0a ->pDbPage))!=0).
34af0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ){. got
34b00 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
34b10 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 }. memset
34b20 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30 (pPage->aData, 0
34b30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 61 , pPage->pBt->pa
34b40 67 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 geSize);. }..
34b50 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
34b60 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f se supports auto
34b70 2d 76 61 63 75 75 6d 2c 20 77 72 69 74 65 20 61 -vacuum, write a
34b80 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 n entry in the p
34b90 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 ointer-map. **
34ba0 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 to indicate that
34bb0 20 74 68 65 20 70 61 67 65 20 69 73 20 66 72 65 the page is fre
34bc0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 e.. */. if( IS
34bd0 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 AUTOVACUUM ){.
34be0 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
34bf0 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 iPage, PTRMAP_F
34c00 52 45 45 50 41 47 45 2c 20 30 2c 20 26 72 63 29 REEPAGE, 0, &rc)
34c10 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
34c20 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 oto freepage_out
34c30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 ;. }.. /* Now
34c40 6d 61 6e 69 70 75 6c 61 74 65 20 74 68 65 20 61 manipulate the a
34c50 63 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 66 ctual database f
34c60 72 65 65 2d 6c 69 73 74 20 73 74 72 75 63 74 75 ree-list structu
34c70 72 65 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 re. There are tw
34c80 6f 0a 20 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 o. ** possibili
34c90 74 69 65 73 2e 20 49 66 20 74 68 65 20 66 72 65 ties. If the fre
34ca0 65 2d 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e e-list is curren
34cb0 74 6c 79 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 tly empty, or if
34cc0 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 the first. **
34cd0 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 trunk page in th
34ce0 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 e free-list is f
34cf0 75 6c 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 70 ull, then this p
34d00 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 age will become
34d10 61 0a 20 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d a. ** new free-
34d20 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e list trunk page.
34d30 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 77 Otherwise, it w
34d40 69 6c 6c 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 ill become a lea
34d50 66 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 f of the. ** fi
34d60 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69 rst trunk page i
34d70 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 n the current fr
34d80 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 62 6c ee-list. This bl
34d90 6f 63 6b 20 74 65 73 74 73 20 69 66 20 69 74 0a ock tests if it.
34da0 20 20 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 ** is possible
34db0 20 74 6f 20 61 64 64 20 74 68 65 20 70 61 67 65 to add the page
34dc0 20 61 73 20 61 20 6e 65 77 20 66 72 65 65 2d 6c as a new free-l
34dd0 69 73 74 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 ist leaf.. */.
34de0 20 69 66 28 20 6e 46 72 65 65 21 3d 30 20 29 7b if( nFree!=0 ){
34df0 0a 20 20 20 20 75 33 32 20 6e 4c 65 61 66 3b 20 . u32 nLeaf;
34e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
34e10 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 * Initial number
34e20 20 6f 66 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f of leaf cells o
34e30 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a n trunk page */.
34e40 0a 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 . iTrunk = ge
34e50 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
34e60 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 aData[32]);.
34e70 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
34e80 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 e(pBt, iTrunk, &
34e90 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 pTrunk, 0);.
34ea0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
34eb0 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 K ){. goto
34ec0 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
34ed0 20 20 7d 0a 0a 20 20 20 20 6e 4c 65 61 66 20 3d }.. nLeaf =
34ee0 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e get4byte(&pTrun
34ef0 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 k->aData[4]);.
34f00 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 assert( pBt->u
34f10 73 61 62 6c 65 53 69 7a 65 3e 33 32 20 29 3b 0a sableSize>32 );.
34f20 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 3e 20 if( nLeaf >
34f30 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 6c 65 (u32)pBt->usable
34f40 53 69 7a 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 Size/4 - 2 ){.
34f50 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
34f60 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
34f70 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 goto freepag
34f80 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 e_out;. }.
34f90 20 69 66 28 20 6e 4c 65 61 66 20 3c 20 28 75 33 if( nLeaf < (u3
34fa0 32 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 2)pBt->usableSiz
34fb0 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20 e/4 - 8 ){.
34fc0 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
34fd0 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f there is room o
34fe0 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 n the trunk page
34ff0 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70 to insert the p
35000 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 age. ** bei
35010 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6e 65 ng freed as a ne
35020 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a w leaf.. **
35030 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 . ** Note t
35040 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 hat the trunk pa
35050 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 ge is not really
35060 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 full until it c
35070 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a ontains. **
35080 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
35090 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75 2 entries, not u
350a0 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 sableSize/4 - 8
350b0 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 61 entries as we ha
350c0 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 ve. ** code
350d0 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 61 d. But due to a
350e0 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e coding error in
350f0 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
35100 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20 ite prior to.
35110 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74 ** 3.6.0, dat
35120 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 65 abases with free
35130 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73 list trunk pages
35140 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68 holding more th
35150 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 an. ** usab
35160 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 leSize/4 - 8 ent
35170 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70 ries will be rep
35180 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 74 orted as corrupt
35190 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20 . In order.
351a0 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e ** to maintain
351b0 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
351c0 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c tibility with ol
351d0 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 der versions of
351e0 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a SQLite,. **
351f0 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 we will continu
35200 65 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 e to restrict th
35210 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
35220 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a ies to usableSiz
35230 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a e/4 - 8. **
35240 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f for now. At so
35250 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 me point in the
35260 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 future (once eve
35270 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 ryone has upgrad
35280 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 ed. ** to 3
35290 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 .6.0 or later) w
352a0 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 e should conside
352b0 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e r fixing the con
352c0 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 ditional above.
352d0 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 ** to read
352e0 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 "usableSize/4-2"
352f0 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 instead of "usa
35300 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 bleSize/4-8"..
35310 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
35320 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 EVIDENCE-OF: R-1
35330 39 39 32 30 2d 31 31 35 37 36 20 48 6f 77 65 76 9920-11576 Howev
35340 65 72 2c 20 6e 65 77 65 72 20 76 65 72 73 69 6f er, newer versio
35350 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 73 74 69 ns of SQLite sti
35360 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 76 6f 69 ll. ** avoi
35370 64 20 75 73 69 6e 67 20 74 68 65 20 6c 61 73 74 d using the last
35380 20 73 69 78 20 65 6e 74 72 69 65 73 20 69 6e 20 six entries in
35390 74 68 65 20 66 72 65 65 6c 69 73 74 20 74 72 75 the freelist tru
353a0 6e 6b 20 70 61 67 65 20 61 72 72 61 79 20 69 6e nk page array in
353b0 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72 20 . ** order
353c0 74 68 61 74 20 64 61 74 61 62 61 73 65 20 66 69 that database fi
353d0 6c 65 73 20 63 72 65 61 74 65 64 20 62 79 20 6e les created by n
353e0 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 ewer versions of
353f0 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 0a 20 SQLite can be.
35400 20 20 20 20 20 2a 2a 20 72 65 61 64 20 62 79 20 ** read by
35410 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f older versions o
35420 66 20 53 51 4c 69 74 65 2e 0a 20 20 20 20 20 20 f SQLite..
35430 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
35440 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
35450 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 pTrunk->pDbPage)
35460 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
35470 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
35480 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
35490 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c Trunk->aData[4],
354a0 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20 20 20 20 nLeaf+1);.
354b0 20 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72 put4byte(&pTr
354c0 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c 65 unk->aData[8+nLe
354d0 61 66 2a 34 5d 2c 20 69 50 61 67 65 29 3b 0a 20 af*4], iPage);.
354e0 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
354f0 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 && (pBt->btsFla
35500 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f gs & BTS_SECURE_
35510 44 45 4c 45 54 45 29 3d 3d 30 20 29 7b 0a 20 20 DELETE)==0 ){.
35520 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
35530 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 70 50 agerDontWrite(pP
35540 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
35550 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
35560 20 72 63 20 3d 20 62 74 72 65 65 53 65 74 48 61 rc = btreeSetHa
35570 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 sContent(pBt, iP
35580 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
35590 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45 TRACE(("FREE
355a0 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 66 20 6f -PAGE: %d leaf o
355b0 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 25 64 5c n trunk page %d\
355c0 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 n",pPage->pgno,p
355d0 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 Trunk->pgno));.
355e0 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 goto freepa
355f0 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 ge_out;. }.
35600 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 }.. /* If contr
35610 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 ol flows to this
35620 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 point, then it
35630 77 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 was not possible
35640 20 74 6f 20 61 64 64 20 74 68 65 0a 20 20 2a 2a to add the. **
35650 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 the page being
35660 66 72 65 65 64 20 61 73 20 61 20 6c 65 61 66 20 freed as a leaf
35670 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 72 73 page of the firs
35680 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 t trunk in the f
35690 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 ree-list.. ** P
356a0 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 ossibly because
356b0 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 the free-list is
356c0 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 empty, or possi
356d0 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 20 bly because the
356e0 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e . ** first trun
356f0 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 k in the free-li
35700 73 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 st is full. Eith
35710 65 72 20 77 61 79 2c 20 74 68 65 20 70 61 67 65 er way, the page
35720 20 62 65 69 6e 67 20 66 72 65 65 64 0a 20 20 2a being freed. *
35730 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 * will become th
35740 65 20 6e 65 77 20 66 69 72 73 74 20 74 72 75 6e e new first trun
35750 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 72 k page in the fr
35760 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 ee-list.. */.
35770 69 66 28 20 70 50 61 67 65 3d 3d 30 20 26 26 20 if( pPage==0 &&
35780 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
35790 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
357a0 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67 65 t, iPage, &pPage
357b0 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 , 0)) ){. got
357c0 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
357d0 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
357e0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
357f0 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
35800 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
35810 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 K ){. goto fr
35820 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a eepage_out;. }.
35830 20 20 70 75 74 34 62 79 74 65 28 70 50 61 67 65 put4byte(pPage
35840 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e 6b 29 ->aData, iTrunk)
35850 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 50 ;. put4byte(&pP
35860 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 30 age->aData[4], 0
35870 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 );. put4byte(&p
35880 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
35890 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52 41 43 , iPage);. TRAC
358a0 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 E(("FREE-PAGE: %
358b0 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 67 65 d new trunk page
358c0 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c 6e 22 replacing %d\n"
358d0 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 , pPage->pgno, i
358e0 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65 70 61 Trunk));..freepa
358f0 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 50 ge_out:. if( pP
35900 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 age ){. pPage
35910 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
35920 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 }. releasePage(
35930 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 61 73 pPage);. releas
35940 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 ePage(pTrunk);.
35950 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 return rc;.}.st
35960 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 61 atic void freePa
35970 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
35980 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 e, int *pRC){.
35990 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 4c 49 if( (*pRC)==SQLI
359a0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 TE_OK ){. *pR
359b0 43 20 3d 20 66 72 65 65 50 61 67 65 32 28 70 50 C = freePage2(pP
359c0 61 67 65 2d 3e 70 42 74 2c 20 70 50 61 67 65 2c age->pBt, pPage,
359d0 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 pPage->pgno);.
359e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 }.}../*.** Free
359f0 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 any overflow pa
35a00 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ges associated w
35a10 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 43 65 ith the given Ce
35a20 6c 6c 2e 20 20 53 74 6f 72 65 0a 2a 2a 20 73 69 ll. Store.** si
35a30 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ze information a
35a40 62 6f 75 74 20 74 68 65 20 63 65 6c 6c 20 69 6e bout the cell in
35a50 20 70 49 6e 66 6f 2e 0a 2a 2f 0a 73 74 61 74 69 pInfo..*/.stati
35a60 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 c int clearCell(
35a70 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
35a80 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e, /* T
35a90 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e he page that con
35aa0 74 61 69 6e 73 20 74 68 65 20 43 65 6c 6c 20 2a tains the Cell *
35ab0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
35ac0 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 2f 2a 20 r *pCell, /*
35ad0 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 First byte of th
35ae0 65 20 43 65 6c 6c 20 2a 2f 0a 20 20 43 65 6c 6c e Cell */. Cell
35af0 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
35b00 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 69 6e 66 /* Size inf
35b10 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
35b20 68 65 20 63 65 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 he cell */.){.
35b30 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 BtShared *pBt;.
35b40 20 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a Pgno ovflPgno;.
35b50 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 int rc;. int
35b60 6e 4f 76 66 6c 3b 0a 20 20 75 33 32 20 6f 76 66 nOvfl;. u32 ovf
35b70 6c 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73 lPageSize;.. as
35b80 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
35b90 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
35ba0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
35bb0 20 70 50 61 67 65 2d 3e 78 50 61 72 73 65 43 65 pPage->xParseCe
35bc0 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c ll(pPage, pCell,
35bd0 20 70 49 6e 66 6f 29 3b 0a 20 20 69 66 28 20 70 pInfo);. if( p
35be0 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 3d 3d 70 49 Info->nLocal==pI
35bf0 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 29 7b nfo->nPayload ){
35c00 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
35c10 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 TE_OK; /* No ov
35c20 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 erflow pages. Re
35c30 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 turn without doi
35c40 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 ng anything */.
35c50 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 }. testcase( p
35c60 43 65 6c 6c 20 2b 20 70 49 6e 66 6f 2d 3e 6e 53 Cell + pInfo->nS
35c70 69 7a 65 20 3d 3d 20 70 50 61 67 65 2d 3e 61 44 ize == pPage->aD
35c80 61 74 61 45 6e 64 20 29 3b 0a 20 20 74 65 73 74 ataEnd );. test
35c90 63 61 73 65 28 20 70 43 65 6c 6c 20 2b 20 28 70 case( pCell + (p
35ca0 49 6e 66 6f 2d 3e 6e 53 69 7a 65 2d 31 29 20 3d Info->nSize-1) =
35cb0 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e = pPage->aDataEn
35cc0 64 20 29 3b 0a 20 20 69 66 28 20 70 43 65 6c 6c d );. if( pCell
35cd0 20 2b 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 + pInfo->nSize
35ce0 3e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e > pPage->aDataEn
35cf0 64 20 29 7b 0a 20 20 20 20 2f 2a 20 43 65 6c 6c d ){. /* Cell
35d00 20 65 78 74 65 6e 64 73 20 70 61 73 74 20 65 6e extends past en
35d10 64 20 6f 66 20 70 61 67 65 20 2a 2f 0a 20 20 20 d of page */.
35d20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
35d30 4f 52 52 55 50 54 5f 50 41 47 45 28 70 50 61 67 ORRUPT_PAGE(pPag
35d40 65 29 3b 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 e);. }. ovflPg
35d50 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 no = get4byte(pC
35d60 65 6c 6c 20 2b 20 70 49 6e 66 6f 2d 3e 6e 53 69 ell + pInfo->nSi
35d70 7a 65 20 2d 20 34 29 3b 0a 20 20 70 42 74 20 3d ze - 4);. pBt =
35d80 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 61 pPage->pBt;. a
35d90 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 ssert( pBt->usab
35da0 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 leSize > 4 );.
35db0 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 ovflPageSize = p
35dc0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
35dd0 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 70 4;. nOvfl = (p
35de0 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 2d Info->nPayload -
35df0 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b pInfo->nLocal +
35e00 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 ovflPageSize -
35e10 31 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 1)/ovflPageSize;
35e20 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 76 66 6c . assert( nOvfl
35e30 3e 30 20 7c 7c 20 0a 20 20 20 20 28 43 4f 52 52 >0 || . (CORR
35e40 55 50 54 5f 44 42 20 26 26 20 28 70 49 6e 66 6f UPT_DB && (pInfo
35e50 2d 3e 6e 50 61 79 6c 6f 61 64 20 2b 20 6f 76 66 ->nPayload + ovf
35e60 6c 50 61 67 65 53 69 7a 65 29 3c 6f 76 66 6c 50 lPageSize)<ovflP
35e70 61 67 65 53 69 7a 65 29 0a 20 20 29 3b 0a 20 20 ageSize). );.
35e80 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 while( nOvfl-- )
35e90 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 {. Pgno iNext
35ea0 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 = 0;. MemPag
35eb0 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 e *pOvfl = 0;.
35ec0 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 if( ovflPgno<2
35ed0 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 62 74 72 || ovflPgno>btr
35ee0 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 eePagecount(pBt)
35ef0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 ){. /* 0 i
35f00 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 s not a legal pa
35f10 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 ge number and pa
35f20 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 ge 1 cannot be a
35f30 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 n . ** over
35f40 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 65 flow page. There
35f50 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f fore if ovflPgno
35f60 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 65 <2 or past the e
35f70 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 nd of the .
35f80 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 74 ** file the dat
35f90 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f abase must be co
35fa0 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 rrupt. */.
35fb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
35fc0 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
35fd0 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 }. if( nOvfl
35fe0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 ){. rc = ge
35ff0 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 tOverflowPage(pB
36000 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f t, ovflPgno, &pO
36010 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 vfl, &iNext);.
36020 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
36030 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 urn rc;. }..
36040 20 20 20 69 66 28 20 28 20 70 4f 76 66 6c 20 7c if( ( pOvfl |
36050 7c 20 28 28 70 4f 76 66 6c 20 3d 20 62 74 72 65 | ((pOvfl = btre
36060 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c ePageLookup(pBt,
36070 20 6f 76 66 6c 50 67 6e 6f 29 29 21 3d 30 29 20 ovflPgno))!=0)
36080 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 ). && sqlite
36090 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
360a0 6e 74 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 nt(pOvfl->pDbPag
360b0 65 29 21 3d 31 0a 20 20 20 20 29 7b 0a 20 20 20 e)!=1. ){.
360c0 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e /* There is n
360d0 6f 20 72 65 61 73 6f 6e 20 61 6e 79 20 63 75 72 o reason any cur
360e0 73 6f 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 sor should have
360f0 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 an outstanding r
36100 65 66 65 72 65 6e 63 65 20 0a 20 20 20 20 20 20 eference .
36110 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f ** to an overflo
36120 77 20 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 w page belonging
36130 20 74 6f 20 61 20 63 65 6c 6c 20 74 68 61 74 20 to a cell that
36140 69 73 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 is being deleted
36150 2f 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 /updated..
36160 2a 2a 20 53 6f 20 69 66 20 74 68 65 72 65 20 65 ** So if there e
36170 78 69 73 74 73 20 6d 6f 72 65 20 74 68 61 6e 20 xists more than
36180 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f one reference to
36190 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 65 6e this page, then
361a0 20 69 74 20 0a 20 20 20 20 20 20 2a 2a 20 6d 75 it . ** mu
361b0 73 74 20 6e 6f 74 20 72 65 61 6c 6c 79 20 62 65 st not really be
361c0 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
361d0 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 e and the databa
361e0 73 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 se must be corru
361f0 70 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 49 74 pt. . ** It
36200 20 69 73 20 68 65 6c 70 66 75 6c 20 74 6f 20 64 is helpful to d
36210 65 74 65 63 74 20 74 68 69 73 20 62 65 66 6f 72 etect this befor
36220 65 20 63 61 6c 6c 69 6e 67 20 66 72 65 65 50 61 e calling freePa
36230 67 65 32 28 29 2c 20 61 73 20 0a 20 20 20 20 20 ge2(), as .
36240 20 2a 2a 20 66 72 65 65 50 61 67 65 32 28 29 20 ** freePage2()
36250 6d 61 79 20 7a 65 72 6f 20 74 68 65 20 70 61 67 may zero the pag
36260 65 20 63 6f 6e 74 65 6e 74 73 20 69 66 20 73 65 e contents if se
36270 63 75 72 65 2d 64 65 6c 65 74 65 20 6d 6f 64 65 cure-delete mode
36280 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 65 6e 61 is. ** ena
36290 62 6c 65 64 2e 20 49 66 20 74 68 69 73 20 27 6f bled. If this 'o
362a0 76 65 72 66 6c 6f 77 27 20 70 61 67 65 20 68 61 verflow' page ha
362b0 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20 70 61 ppens to be a pa
362c0 67 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 ge that the.
362d0 20 20 2a 2a 20 63 61 6c 6c 65 72 20 69 73 20 69 ** caller is i
362e0 74 65 72 61 74 69 6e 67 20 74 68 72 6f 75 67 68 terating through
362f0 20 6f 72 20 75 73 69 6e 67 20 69 6e 20 73 6f 6d or using in som
36300 65 20 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 e other way, thi
36310 73 0a 20 20 20 20 20 20 2a 2a 20 63 61 6e 20 62 s. ** can b
36320 65 20 70 72 6f 62 6c 65 6d 61 74 69 63 2e 0a 20 e problematic..
36330 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
36340 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
36350 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 65 6c 73 T_BKPT;. }els
36360 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 72 e{. rc = fr
36370 65 65 50 61 67 65 32 28 70 42 74 2c 20 70 4f 76 eePage2(pBt, pOv
36380 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 fl, ovflPgno);.
36390 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f }.. if( pO
363a0 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c vfl ){. sql
363b0 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
363c0 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a Ovfl->pDbPage);.
363d0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
363e0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
363f0 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 ovflPgno = iNe
36400 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e xt;. }. return
36410 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
36420 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 *.** Create the
36430 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 75 73 byte sequence us
36440 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 ed to represent
36450 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 a cell on page p
36460 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 69 74 Page.** and writ
36470 65 20 74 68 61 74 20 62 79 74 65 20 73 65 71 75 e that byte sequ
36480 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b ence into pCell[
36490 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 ]. Overflow pag
364a0 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 es are.** alloca
364b0 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20 69 ted and filled i
364c0 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20 n as necessary.
364d0 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f The calling pro
364e0 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 65 73 cedure.** is res
364f0 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b ponsible for mak
36500 69 6e 67 20 73 75 72 65 20 73 75 66 66 69 63 69 ing sure suffici
36510 65 6e 74 20 73 70 61 63 65 20 68 61 73 20 62 65 ent space has be
36520 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 en allocated.**
36530 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a for pCell[]..**.
36540 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 43 65 ** Note that pCe
36550 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 63 65 ll does not nece
36560 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20 70 6f ssary need to po
36570 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61 67 65 int to the pPage
36580 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 61 2e ->aData.** area.
36590 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 70 6f pCell might po
365a0 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 6d 70 int to some temp
365b0 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e 20 20 orary storage.
365c0 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a The cell will.**
365d0 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 be constructed
365e0 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 in this temporar
365f0 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f 70 69 y area then copi
36600 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 ed into pPage->a
36610 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a Data.** later..*
36620 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
36630 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 lInCell(. MemPa
36640 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 ge *pPage,
36650 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
36660 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 page that conta
36670 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ins the cell */.
36680 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
36690 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 *pCell,
366a0 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 /* Complete tex
366b0 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f t of the cell */
366c0 0a 20 20 63 6f 6e 73 74 20 42 74 72 65 65 50 61 . const BtreePa
366d0 79 6c 6f 61 64 20 2a 70 58 2c 20 20 20 20 20 20 yload *pX,
366e0 20 20 2f 2a 20 50 61 79 6c 6f 61 64 20 77 69 74 /* Payload wit
366f0 68 20 77 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 h which to const
36700 72 75 63 74 20 74 68 65 20 63 65 6c 6c 20 2a 2f ruct the cell */
36710 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 . int *pnSize
36720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36730 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 /* Write cell
36740 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a size here */.){.
36750 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a int nPayload;.
36760 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 const u8 *pSrc
36770 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c ;. int nSrc, n,
36780 20 72 63 2c 20 6d 6e 3b 0a 20 20 69 6e 74 20 73 rc, mn;. int s
36790 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 paceLeft;. MemP
367a0 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 3b age *pToRelease;
367b0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
367c0 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e 73 69 *pPrior;. unsi
367d0 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61 79 6c gned char *pPayl
367e0 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65 64 20 oad;. BtShared
367f0 2a 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e *pBt;. Pgno pgn
36800 6f 4f 76 66 6c 3b 0a 20 20 69 6e 74 20 6e 48 65 oOvfl;. int nHe
36810 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 ader;.. assert(
36820 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
36830 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
36840 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 mutex) );.. /*
36850 70 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 63 pPage is not nec
36860 65 73 73 61 72 69 6c 79 20 77 72 69 74 65 61 62 essarily writeab
36870 6c 65 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 6d le since pCell m
36880 69 67 68 74 20 62 65 20 61 75 78 69 6c 69 61 72 ight be auxiliar
36890 79 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 70 y. ** buffer sp
368a0 61 63 65 20 74 68 61 74 20 69 73 20 73 65 70 61 ace that is sepa
368b0 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 70 50 rate from the pP
368c0 61 67 65 20 62 75 66 66 65 72 20 61 72 65 61 20 age buffer area
368d0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 */. assert( pCe
368e0 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 ll<pPage->aData
368f0 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 65 || pCell>=&pPage
36900 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 70 ->aData[pPage->p
36910 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 Bt->pageSize].
36920 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c || sql
36930 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
36940 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
36950 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 age) );.. /* Fi
36960 6c 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ll in the header
36970 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d . */. nHeader =
36980 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
36990 53 69 7a 65 3b 0a 20 20 69 66 28 20 70 50 61 67 Size;. if( pPag
369a0 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
369b0 20 6e 50 61 79 6c 6f 61 64 20 3d 20 70 58 2d 3e nPayload = pX->
369c0 6e 44 61 74 61 20 2b 20 70 58 2d 3e 6e 5a 65 72 nData + pX->nZer
369d0 6f 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 58 o;. pSrc = pX
369e0 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 ->pData;. nSr
369f0 63 20 3d 20 70 58 2d 3e 6e 44 61 74 61 3b 0a 20 c = pX->nData;.
36a00 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
36a10 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 29 3b 20 ->intKeyLeaf );
36a20 2f 2a 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 20 /* fillInCell()
36a30 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 6f 72 20 only called for
36a40 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 6e 48 leaves */. nH
36a50 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69 eader += putVari
36a60 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 48 65 61 nt32(&pCell[nHea
36a70 64 65 72 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b der], nPayload);
36a80 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 . nHeader +=
36a90 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c putVarint(&pCell
36aa0 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 [nHeader], *(u64
36ab0 2a 29 26 70 58 2d 3e 6e 4b 65 79 29 3b 0a 20 20 *)&pX->nKey);.
36ac0 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
36ad0 74 28 20 70 58 2d 3e 6e 4b 65 79 3c 3d 30 78 37 t( pX->nKey<=0x7
36ae0 66 66 66 66 66 66 66 20 26 26 20 70 58 2d 3e 70 fffffff && pX->p
36af0 4b 65 79 21 3d 30 20 29 3b 0a 20 20 20 20 6e 53 Key!=0 );. nS
36b00 72 63 20 3d 20 6e 50 61 79 6c 6f 61 64 20 3d 20 rc = nPayload =
36b10 28 69 6e 74 29 70 58 2d 3e 6e 4b 65 79 3b 0a 20 (int)pX->nKey;.
36b20 20 20 20 70 53 72 63 20 3d 20 70 58 2d 3e 70 4b pSrc = pX->pK
36b30 65 79 3b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 ey;. nHeader
36b40 2b 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 += putVarint32(&
36b50 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 pCell[nHeader],
36b60 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 7d 0a 20 nPayload);. }.
36b70 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 . /* Fill in t
36b80 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 he payload */.
36b90 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c pPayload = &pCel
36ba0 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 69 66 l[nHeader];. if
36bb0 28 20 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 ( nPayload<=pPag
36bc0 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 e->maxLocal ){.
36bd0 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 /* This is th
36be0 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 e common case wh
36bf0 65 72 65 20 65 76 65 72 79 74 68 69 6e 67 20 66 ere everything f
36c00 69 74 73 20 6f 6e 20 74 68 65 20 62 74 72 65 65 its on the btree
36c10 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 61 6e 64 page. ** and
36c20 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 no overflow pag
36c30 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 2e es are required.
36c40 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 6e 48 65 61 */. n = nHea
36c50 64 65 72 20 2b 20 6e 50 61 79 6c 6f 61 64 3b 0a der + nPayload;.
36c60 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d testcase( n=
36c70 3d 33 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 =3 );. testca
36c80 73 65 28 20 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 se( n==4 );.
36c90 69 66 28 20 6e 3c 34 20 29 20 6e 20 3d 20 34 3b if( n<4 ) n = 4;
36ca0 0a 20 20 20 20 2a 70 6e 53 69 7a 65 20 3d 20 6e . *pnSize = n
36cb0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 53 ;. assert( nS
36cc0 72 63 3c 3d 6e 50 61 79 6c 6f 61 64 20 29 3b 0a rc<=nPayload );.
36cd0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 testcase( nS
36ce0 72 63 3c 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 rc<nPayload );.
36cf0 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f memcpy(pPaylo
36d00 61 64 2c 20 70 53 72 63 2c 20 6e 53 72 63 29 3b ad, pSrc, nSrc);
36d10 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 . memset(pPay
36d20 6c 6f 61 64 2b 6e 53 72 63 2c 20 30 2c 20 6e 50 load+nSrc, 0, nP
36d30 61 79 6c 6f 61 64 2d 6e 53 72 63 29 3b 0a 20 20 ayload-nSrc);.
36d40 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
36d50 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 OK;. }.. /* If
36d60 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 we reach this p
36d70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 oint, it means t
36d80 68 61 74 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 hat some of the
36d90 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6e 65 65 content will nee
36da0 64 0a 20 20 2a 2a 20 74 6f 20 73 70 69 6c 6c 20 d. ** to spill
36db0 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 onto overflow pa
36dc0 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 6d 6e 20 3d ges.. */. mn =
36dd0 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
36de0 3b 0a 20 20 6e 20 3d 20 6d 6e 20 2b 20 28 6e 50 ;. n = mn + (nP
36df0 61 79 6c 6f 61 64 20 2d 20 6d 6e 29 20 25 20 28 ayload - mn) % (
36e00 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
36e10 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 74 leSize - 4);. t
36e20 65 73 74 63 61 73 65 28 20 6e 3d 3d 70 50 61 67 estcase( n==pPag
36e30 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 e->maxLocal );.
36e40 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 70 50 testcase( n==pP
36e50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 age->maxLocal+1
36e60 29 3b 0a 20 20 69 66 28 20 6e 20 3e 20 70 50 61 );. if( n > pPa
36e70 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 20 6e ge->maxLocal ) n
36e80 20 3d 20 6d 6e 3b 0a 20 20 73 70 61 63 65 4c 65 = mn;. spaceLe
36e90 66 74 20 3d 20 6e 3b 0a 20 20 2a 70 6e 53 69 7a ft = n;. *pnSiz
36ea0 65 20 3d 20 6e 20 2b 20 6e 48 65 61 64 65 72 20 e = n + nHeader
36eb0 2b 20 34 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 + 4;. pPrior =
36ec0 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 2b 6e &pCell[nHeader+n
36ed0 5d 3b 0a 20 20 70 54 6f 52 65 6c 65 61 73 65 20 ];. pToRelease
36ee0 3d 20 30 3b 0a 20 20 70 67 6e 6f 4f 76 66 6c 20 = 0;. pgnoOvfl
36ef0 3d 20 30 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 = 0;. pBt = pPa
36f00 67 65 2d 3e 70 42 74 3b 0a 0a 20 20 2f 2a 20 41 ge->pBt;.. /* A
36f10 74 20 74 68 69 73 20 70 6f 69 6e 74 20 76 61 72 t this point var
36f20 69 61 62 6c 65 73 20 73 68 6f 75 6c 64 20 62 65 iables should be
36f30 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a set as follows:
36f40 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 6e 50 61 . **. ** nPa
36f50 79 6c 6f 61 64 20 20 20 20 20 20 20 20 20 20 20 yload
36f60 54 6f 74 61 6c 20 70 61 79 6c 6f 61 64 20 73 69 Total payload si
36f70 7a 65 20 69 6e 20 62 79 74 65 73 0a 20 20 2a 2a ze in bytes. **
36f80 20 20 20 70 50 61 79 6c 6f 61 64 20 20 20 20 20 pPayload
36f90 20 20 20 20 20 20 42 65 67 69 6e 20 77 72 69 74 Begin writ
36fa0 69 6e 67 20 70 61 79 6c 6f 61 64 20 68 65 72 65 ing payload here
36fb0 0a 20 20 2a 2a 20 20 20 73 70 61 63 65 4c 65 66 . ** spaceLef
36fc0 74 20 20 20 20 20 20 20 20 20 20 53 70 61 63 65 t Space
36fd0 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 50 available at pP
36fe0 61 79 6c 6f 61 64 2e 20 20 49 66 20 6e 50 61 79 ayload. If nPay
36ff0 6c 6f 61 64 3e 73 70 61 63 65 4c 65 66 74 2c 0a load>spaceLeft,.
37000 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
37010 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 6d that m
37020 65 61 6e 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 eans content mus
37030 74 20 73 70 69 6c 6c 20 69 6e 74 6f 20 6f 76 65 t spill into ove
37040 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 20 20 2a rflow pages.. *
37050 2a 20 20 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 * *pnSize
37060 20 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20 74 Size of t
37070 68 65 20 6c 6f 63 61 6c 20 63 65 6c 6c 20 28 6e he local cell (n
37080 6f 74 20 63 6f 75 6e 74 69 6e 67 20 6f 76 65 72 ot counting over
37090 66 6c 6f 77 20 70 61 67 65 73 29 0a 20 20 2a 2a flow pages). **
370a0 20 20 20 70 50 72 69 6f 72 20 20 20 20 20 20 20 pPrior
370b0 20 20 20 20 20 20 57 68 65 72 65 20 74 6f 20 77 Where to w
370c0 72 69 74 65 20 74 68 65 20 70 67 6e 6f 20 6f 66 rite the pgno of
370d0 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
370e0 6c 6f 77 20 70 61 67 65 0a 20 20 2a 2a 0a 20 20 low page. **.
370f0 2a 2a 20 55 73 65 20 61 20 63 61 6c 6c 20 74 6f ** Use a call to
37100 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
37110 74 72 28 29 20 74 6f 20 76 65 72 69 66 79 20 74 tr() to verify t
37120 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 61 hat the values a
37130 62 6f 76 65 0a 20 20 2a 2a 20 77 65 72 65 20 63 bove. ** were c
37140 6f 6d 70 75 74 65 64 20 63 6f 72 72 65 63 74 6c omputed correctl
37150 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 y.. */.#ifdef S
37160 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 7b 0a QLITE_DEBUG. {.
37170 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 CellInfo inf
37180 6f 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 78 50 o;. pPage->xP
37190 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 arseCell(pPage,
371a0 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 pCell, &info);.
371b0 20 20 20 61 73 73 65 72 74 28 20 6e 48 65 61 64 assert( nHead
371c0 65 72 3d 3d 28 69 6e 74 29 28 69 6e 66 6f 2e 70 er==(int)(info.p
371d0 50 61 79 6c 6f 61 64 20 2d 20 70 43 65 6c 6c 29 Payload - pCell)
371e0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
371f0 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 70 58 2d 3e 6e info.nKey==pX->n
37200 4b 65 79 20 29 3b 0a 20 20 20 20 61 73 73 65 72 Key );. asser
37210 74 28 20 2a 70 6e 53 69 7a 65 20 3d 3d 20 69 6e t( *pnSize == in
37220 66 6f 2e 6e 53 69 7a 65 20 29 3b 0a 20 20 20 20 fo.nSize );.
37230 61 73 73 65 72 74 28 20 73 70 61 63 65 4c 65 66 assert( spaceLef
37240 74 20 3d 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c t == info.nLocal
37250 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a );. }.#endif..
37260 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 /* Write the p
37270 61 79 6c 6f 61 64 20 69 6e 74 6f 20 74 68 65 20 ayload into the
37280 6c 6f 63 61 6c 20 43 65 6c 6c 20 61 6e 64 20 61 local Cell and a
37290 6e 79 20 65 78 74 72 61 20 69 6e 74 6f 20 6f 76 ny extra into ov
372a0 65 72 66 6c 6f 77 20 70 61 67 65 73 20 2a 2f 0a erflow pages */.
372b0 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 while( 1 ){.
372c0 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a n = nPayload;.
372d0 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c if( n>spaceL
372e0 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c eft ) n = spaceL
372f0 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 eft;.. /* If
37300 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f pToRelease is no
37310 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 t zero than pPay
37320 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f load points into
37330 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 the data area.
37340 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 ** of pToRele
37350 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 ase. Make sure
37360 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 pToRelease is st
37370 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a ill writeable. *
37380 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 /. assert( pT
37390 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 oRelease==0 || s
373a0 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
373b0 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 teable(pToReleas
373c0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
373d0 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f /* If pPaylo
373e0 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 ad is part of th
373f0 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 e data area of p
37400 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 Page, then make
37410 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a sure pPage. *
37420 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 * is still write
37430 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 able */. asse
37440 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 rt( pPayload<pPa
37450 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 ge->aData || pPa
37460 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 yload>=&pPage->a
37470 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
37480 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
37490 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
374a0 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
374b0 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
374c0 20 20 20 69 66 28 20 6e 53 72 63 3e 3d 6e 20 29 if( nSrc>=n )
374d0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 {. memcpy(p
374e0 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e Payload, pSrc, n
374f0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
37500 20 6e 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 nSrc>0 ){.
37510 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 20 n = nSrc;.
37520 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 memcpy(pPayload
37530 2c 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 , pSrc, n);.
37540 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
37550 73 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c set(pPayload, 0,
37560 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e n);. }. n
37570 50 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 Payload -= n;.
37580 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 64 3c 3d if( nPayload<=
37590 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 70 0 ) break;. p
375a0 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 Payload += n;.
375b0 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 pSrc += n;.
375c0 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 nSrc -= n;.
375d0 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a spaceLeft -= n;.
375e0 20 20 20 20 69 66 28 20 73 70 61 63 65 4c 65 66 if( spaceLef
375f0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 4d 65 t==0 ){. Me
37600 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 mPage *pOvfl = 0
37610 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
37620 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
37630 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f . Pgno pgno
37640 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66 Ptrmap = pgnoOvf
37650 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 l; /* Overflow p
37660 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 age pointer-map
37670 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 entry page */.
37680 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
37690 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
376a0 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 do{.
376b0 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 pgnoOvfl++;.
376c0 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 0a 20 } while( .
376d0 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 50 5f PTRMAP_
376e0 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f ISPAGE(pBt, pgno
376f0 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 Ovfl) || pgnoOvf
37700 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f l==PENDING_BYTE_
37710 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 20 20 PAGE(pBt) .
37720 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 );. }.#e
37730 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 ndif. rc =
37740 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
37750 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 e(pBt, &pOvfl, &
37760 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 pgnoOvfl, pgnoOv
37770 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 fl, 0);.#ifndef
37780 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
37790 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 VACUUM. /*
377a0 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
377b0 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
377c0 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 73 65 cuum, and the se
377d0 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 75 65 cond or subseque
377e0 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 nt. ** over
377f0 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 65 69 flow page is bei
37800 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64 ng allocated, ad
37810 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 d an entry to th
37820 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 e pointer-map.
37830 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 74 20 ** for that
37840 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20 page now. .
37850 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 **. ** If
37860 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
37870 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c t overflow page,
37880 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 70 61 then write a pa
37890 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 20 20 rtial entry .
378a0 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69 ** to the poi
378b0 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 65 20 nter-map. If we
378c0 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f write nothing to
378d0 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61 this pointer-ma
378e0 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a p slot,. **
378f0 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 6d 69 then the optimi
37900 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 63 68 stic overflow ch
37910 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69 ain processing i
37920 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20 n clearCell().
37930 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e ** may misin
37940 74 65 72 70 72 65 74 20 74 68 65 20 75 6e 69 6e terpret the unin
37950 69 74 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 itialized values
37960 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 0a and delete the.
37970 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70 ** wrong p
37980 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 64 61 ages from the da
37990 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f tabase.. */
379a0 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
379b0 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 72 63 autoVacuum && rc
379c0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
379d0 20 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20 u8 eType
379e0 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54 = (pgnoPtrmap?PT
379f0 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 RMAP_OVERFLOW2:P
37a00 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29 TRMAP_OVERFLOW1)
37a10 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 ;. ptrmap
37a20 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 Put(pBt, pgnoOvf
37a30 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 l, eType, pgnoPt
37a40 72 6d 61 70 2c 20 26 72 63 29 3b 0a 20 20 20 20 rmap, &rc);.
37a50 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
37a60 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
37a70 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 age(pOvfl);.
37a80 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 }. }.#e
37a90 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 72 ndif. if( r
37aa0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c c ){. rel
37ab0 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 easePage(pToRele
37ac0 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 ase);. re
37ad0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
37ae0 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 54 .. /* If pT
37af0 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 oRelease is not
37b00 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 6f 72 zero than pPrior
37b10 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 65 points into the
37b20 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 20 data area.
37b30 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 ** of pToReleas
37b40 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 e. Make sure pT
37b50 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c oRelease is stil
37b60 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a l writeable. */.
37b70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 assert( pT
37b80 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 oRelease==0 || s
37b90 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
37ba0 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 teable(pToReleas
37bb0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
37bc0 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 72 69 /* If pPri
37bd0 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 or is part of th
37be0 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 e data area of p
37bf0 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 Page, then make
37c00 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 20 sure pPage.
37c10 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 ** is still wri
37c20 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 teable */.
37c30 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 3c 70 assert( pPrior<p
37c40 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 Page->aData || p
37c50 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61 Prior>=&pPage->a
37c60 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
37c70 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
37c80 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
37c90 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
37ca0 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
37cb0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50 put4byte(pP
37cc0 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b rior, pgnoOvfl);
37cd0 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
37ce0 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a ge(pToRelease);.
37cf0 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65 pToRelease
37d00 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 = pOvfl;.
37d10 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e pPrior = pOvfl->
37d20 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74 aData;. put
37d30 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29 4byte(pPrior, 0)
37d40 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64 ;. pPayload
37d50 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 = &pOvfl->aData
37d60 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65 [4];. space
37d70 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 Left = pBt->usab
37d80 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 leSize - 4;.
37d90 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 }. }. releaseP
37da0 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b age(pToRelease);
37db0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
37dc0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 _OK;.}../*.** Re
37dd0 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65 move the i-th ce
37de0 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20 ll from pPage.
37df0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66 This routine eff
37e00 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e ects pPage only.
37e10 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e .** The cell con
37e20 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65 tent is not free
37e30 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 d or deallocated
37e40 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 . It is assumed
37e50 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c that.** the cel
37e60 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 l content has be
37e70 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c en copied somepl
37e80 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20 ace else. This
37e90 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 routine just.**
37ea0 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65 removes the refe
37eb0 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c rence to the cel
37ec0 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a l from pPage..**
37ed0 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65 .** "sz" must be
37ee0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
37ef0 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c ytes in the cell
37f00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
37f10 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 dropCell(MemPag
37f20 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 e *pPage, int id
37f30 78 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a x, int sz, int *
37f40 70 52 43 29 7b 0a 20 20 75 33 32 20 70 63 3b 20 pRC){. u32 pc;
37f50 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
37f60 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e t to cell conten
37f70 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 t of cell being
37f80 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 deleted */. u8
37f90 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 *data; /*
37fa0 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a pPage->aData */.
37fb0 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 u8 *ptr;
37fc0 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 /* Used to mov
37fd0 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 e bytes around w
37fe0 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a ithin data[] */.
37ff0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
38000 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 /* The return
38010 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 code */. int hd
38020 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 r; /* Beg
38030 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 inning of the he
38040 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 ader. 0 most pa
38050 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 ges. 100 page 1
38060 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 */.. if( *pRC
38070 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
38080 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 rt( idx>=0 && id
38090 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 x<pPage->nCell )
380a0 3b 0a 20 20 61 73 73 65 72 74 28 20 43 4f 52 52 ;. assert( CORR
380b0 55 50 54 5f 44 42 20 7c 7c 20 73 7a 3d 3d 63 65 UPT_DB || sz==ce
380c0 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69 64 llSize(pPage, id
380d0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
380e0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
380f0 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
38100 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
38110 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
38120 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
38130 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
38140 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
38150 46 72 65 65 3e 3d 30 20 29 3b 0a 20 20 64 61 74 Free>=0 );. dat
38160 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
38170 3b 0a 20 20 70 74 72 20 3d 20 26 70 50 61 67 65 ;. ptr = &pPage
38180 2d 3e 61 43 65 6c 6c 49 64 78 5b 32 2a 69 64 78 ->aCellIdx[2*idx
38190 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 ];. pc = get2by
381a0 74 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d te(ptr);. hdr =
381b0 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
381c0 74 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 t;. testcase( p
381d0 63 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 c==get2byte(&dat
381e0 61 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 a[hdr+5]) );. t
381f0 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d estcase( pc+sz==
38200 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
38210 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 leSize );. if(
38220 70 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 pc+sz > pPage->p
38230 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
38240 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c {. *pRC = SQL
38250 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
38260 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
38270 7d 0a 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 }. rc = freeSpa
38280 63 65 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a ce(pPage, pc, sz
38290 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
382a0 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 *pRC = rc;.
382b0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
382c0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a pPage->nCell--;.
382d0 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 if( pPage->nCe
382e0 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d ll==0 ){. mem
382f0 73 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d set(&data[hdr+1]
38300 2c 20 30 2c 20 34 29 3b 0a 20 20 20 20 64 61 74 , 0, 4);. dat
38310 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 a[hdr+7] = 0;.
38320 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
38330 5b 68 64 72 2b 35 5d 2c 20 70 50 61 67 65 2d 3e [hdr+5], pPage->
38340 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 pBt->usableSize)
38350 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 ;. pPage->nFr
38360 65 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d ee = pPage->pBt-
38370 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 70 50 >usableSize - pP
38380 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 0a 20 age->hdrOffset.
38390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
383a0 20 20 20 20 20 20 2d 20 70 50 61 67 65 2d 3e 63 - pPage->c
383b0 68 69 6c 64 50 74 72 53 69 7a 65 20 2d 20 38 3b hildPtrSize - 8;
383c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 . }else{. me
383d0 6d 6d 6f 76 65 28 70 74 72 2c 20 70 74 72 2b 32 mmove(ptr, ptr+2
383e0 2c 20 32 2a 28 70 50 61 67 65 2d 3e 6e 43 65 6c , 2*(pPage->nCel
383f0 6c 20 2d 20 69 64 78 29 29 3b 0a 20 20 20 20 70 l - idx));. p
38400 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
38410 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 r+3], pPage->nCe
38420 6c 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e ll);. pPage->
38430 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 20 20 7d 0a nFree += 2;. }.
38440 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 }../*.** Insert
38450 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 a new cell on pP
38460 61 67 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 age at cell inde
38470 78 20 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f x "i". pCell po
38480 69 6e 74 73 20 74 6f 20 74 6