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 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
0e90: 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
0ea0: 44 5f 43 41 43 48 45 0a 0a 23 69 66 64 65 66 20 D_CACHE..#ifdef
0eb0: 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
0ec0: 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 **** This functi
0ed0: 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 on is only used
0ee0: 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 as part of an as
0ef0: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
0f00: 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 . ***.**.** Chec
0f10: 6b 20 74 6f 20 73 65 65 20 69 66 20 70 42 74 72 k to see if pBtr
0f20: 65 65 20 68 6f 6c 64 73 20 74 68 65 20 72 65 71 ee holds the req
0f30: 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 72 uired locks to r
0f40: 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f 20 ead or write to
0f50: 74 68 65 20 0a 2a 2a 20 74 61 62 6c 65 20 77 69 the .** table wi
0f60: 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69 52 6f th root page iRo
0f70: 6f 74 2e 20 20 20 52 65 74 75 72 6e 20 31 20 69 ot. Return 1 i
0f80: 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 30 20 f it does and 0
0f90: 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f if not..**.** Fo
0fa0: 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 r example, when
0fb0: 77 72 69 74 69 6e 67 20 74 6f 20 61 20 74 61 62 writing to a tab
0fc0: 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 le with root-pag
0fd0: 65 20 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 e iRoot via .**
0fe0: 42 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e Btree connection
0ff0: 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 pBtree:.**.**
1000: 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 assert( hasSha
1010: 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
1020: 6b 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c k(pBtree, iRoot,
1030: 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 0, WRITE_LOCK)
1040: 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 );.**.** When wr
1050: 69 74 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 iting to an inde
1060: 78 20 74 68 61 74 20 72 65 73 69 64 65 73 20 69 x that resides i
1070: 6e 20 61 20 73 68 61 72 61 62 6c 65 20 64 61 74 n a sharable dat
1080: 61 62 61 73 65 2c 20 74 68 65 20 0a 2a 2a 20 63 abase, the .** c
1090: 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 68 61 76 aller should hav
10a0: 65 20 66 69 72 73 74 20 6f 62 74 61 69 6e 65 64 e first obtained
10b0: 20 61 20 6c 6f 63 6b 20 73 70 65 63 69 66 79 69 a lock specifyi
10c0: 6e 67 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ng the root page
10d0: 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 of.** the corre
10e0: 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 2e 20 sponding table.
10f0: 54 68 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67 This makes thing
1100: 73 20 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d s a bit more com
1110: 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 plicated,.** as
1120: 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61 this module trea
1130: 74 73 20 65 61 63 68 20 74 61 62 6c 65 20 61 73 ts each table as
1140: 20 61 20 73 65 70 61 72 61 74 65 20 73 74 72 75 a separate stru
1150: 63 74 75 72 65 2e 20 54 6f 20 64 65 74 65 72 6d cture. To determ
1160: 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 ine.** the table
1170: 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1180: 6f 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e o the index bein
1190: 67 20 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a g written, this.
11a0: 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 ** function has
11b0: 74 6f 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 to search throug
11c0: 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 h the database s
11d0: 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 chema..**.** Ins
11e0: 74 65 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f tead of a lock o
11f0: 6e 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 n the table/inde
1200: 78 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 x rooted at page
1210: 20 69 52 6f 6f 74 2c 20 74 68 65 20 63 61 6c 6c iRoot, the call
1220: 65 72 20 6d 61 79 0a 2a 2a 20 68 6f 6c 64 20 61 er may.** hold a
1230: 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 write-lock on t
1240: 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 20 he schema table
1250: 28 72 6f 6f 74 20 70 61 67 65 20 31 29 2e 20 54 (root page 1). T
1260: 68 69 73 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 61 his is also.** a
1270: 63 63 65 70 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 cceptable..*/.st
1280: 61 74 69 63 20 69 6e 74 20 68 61 73 53 68 61 72 atic int hasShar
1290: 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
12a0: 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 (. Btree *pBtre
12b0: 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 e, /* Ha
12c0: 6e 64 6c 65 20 74 68 61 74 20 6d 75 73 74 20 68 ndle that must h
12d0: 6f 6c 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 old lock */. Pg
12e0: 6e 6f 20 69 52 6f 6f 74 2c 20 20 20 20 20 20 20 no iRoot,
12f0: 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
1300: 65 20 6f 66 20 62 2d 74 72 65 65 20 2a 2f 0a 20 e of b-tree */.
1310: 20 69 6e 74 20 69 73 49 6e 64 65 78 2c 20 20 20 int isIndex,
1320: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1330: 69 66 20 69 52 6f 6f 74 20 69 73 20 74 68 65 20 if iRoot is the
1340: 72 6f 6f 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 root of an index
1350: 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 b-tree */. int
1360: 20 65 4c 6f 63 6b 54 79 70 65 20 20 20 20 20 20 eLockType
1370: 20 20 20 20 2f 2a 20 52 65 71 75 69 72 65 64 20 /* Required
1380: 6c 6f 63 6b 20 74 79 70 65 20 28 52 45 41 44 5f lock type (READ_
1390: 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f LOCK or WRITE_LO
13a0: 43 4b 29 20 2a 2f 0a 29 7b 0a 20 20 53 63 68 65 CK) */.){. Sche
13b0: 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 ma *pSchema = (S
13c0: 63 68 65 6d 61 20 2a 29 70 42 74 72 65 65 2d 3e chema *)pBtree->
13d0: 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 pBt->pSchema;.
13e0: 50 67 6e 6f 20 69 54 61 62 20 3d 20 30 3b 0a 20 Pgno iTab = 0;.
13f0: 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a BtLock *pLock;.
1400: 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 . /* If this da
1410: 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 73 68 tabase is not sh
1420: 61 72 65 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 areable, or if t
1430: 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 he client is rea
1440: 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 ding. ** and ha
1450: 73 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d s the read-uncom
1460: 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c mitted flag set,
1470: 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 then no lock is
1480: 20 72 65 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a required. . **
1490: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d Return true imm
14a0: 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 ediately.. */.
14b0: 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68 if( (pBtree->sh
14c0: 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c arable==0). ||
14d0: 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41 (eLockType==REA
14e0: 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65 D_LOCK && (pBtre
14f0: 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 e->db->flags & S
1500: 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
1510: 69 74 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 it)). ){. re
1520: 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f turn 1;. }.. /
1530: 2a 20 49 66 20 74 68 65 20 63 6c 69 65 6e 74 20 * If the client
1540: 69 73 20 72 65 61 64 69 6e 67 20 20 6f 72 20 77 is reading or w
1550: 72 69 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 riting an index
1560: 61 6e 64 20 74 68 65 20 73 63 68 65 6d 61 20 69 and the schema i
1570: 73 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64 65 s. ** not loade
1580: 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 6f d, then it is to
1590: 6f 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 61 o difficult to a
15a0: 63 74 75 61 6c 6c 79 20 63 68 65 63 6b 20 74 6f ctually check to
15b0: 20 73 65 65 20 69 66 0a 20 20 2a 2a 20 74 68 65 see if. ** the
15c0: 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b 73 20 61 correct locks a
15d0: 72 65 20 68 65 6c 64 2e 20 20 53 6f 20 64 6f 20 re held. So do
15e0: 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20 6a 75 73 not bother - jus
15f0: 74 20 72 65 74 75 72 6e 20 74 72 75 65 2e 0a 20 t return true..
1600: 20 2a 2a 20 54 68 69 73 20 63 61 73 65 20 64 6f ** This case do
1610: 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75 70 20 76 es not come up v
1620: 65 72 79 20 6f 66 74 65 6e 20 61 6e 79 68 6f 77 ery often anyhow
1630: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 .. */. if( isI
1640: 6e 64 65 78 20 26 26 20 28 21 70 53 63 68 65 6d ndex && (!pSchem
1650: 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d 3e 73 a || (pSchema->s
1660: 63 68 65 6d 61 46 6c 61 67 73 26 44 42 5f 53 63 chemaFlags&DB_Sc
1670: 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 29 20 hemaLoaded)==0)
1680: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
1690: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 . }.. /* Figur
16a0: 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70 e out the root-p
16b0: 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63 age that the loc
16c0: 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 k should be held
16d0: 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20 on. For table.
16e0: 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69 ** b-trees, thi
16f0: 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f s is just the ro
1700: 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 ot page of the b
1710: 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64 -tree being read
1720: 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e or. ** written
1730: 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72 . For index b-tr
1740: 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 ees, it is the r
1750: 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
1760: 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 associated. **
1770: 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 table. */. if(
1780: 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 isIndex ){.
1790: 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 HashElem *p;.
17a0: 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 for(p=sqliteHas
17b0: 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d hFirst(&pSchema-
17c0: 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d >idxHash); p; p=
17d0: 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
17e0: 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 )){. Index
17f0: 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a *pIdx = (Index *
1800: 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 )sqliteHashData(
1810: 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 p);. if( pI
1820: 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69 dx->tnum==(int)i
1830: 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Root ){.
1840: 69 66 28 20 69 54 61 62 20 29 7b 0a 20 20 20 20 if( iTab ){.
1850: 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 /* Two or
1860: 6d 6f 72 65 20 69 6e 64 65 78 65 73 20 73 68 61 more indexes sha
1870: 72 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 re the same root
1880: 20 70 61 67 65 2e 20 20 54 68 65 72 65 20 6d 75 page. There mu
1890: 73 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 st. **
18a0: 62 65 20 69 6d 70 6f 73 74 65 72 20 74 61 62 6c be imposter tabl
18b0: 65 73 2e 20 20 53 6f 20 6a 75 73 74 20 72 65 74 es. So just ret
18c0: 75 72 6e 20 74 72 75 65 2e 20 20 54 68 65 20 61 urn true. The a
18d0: 73 73 65 72 74 20 69 73 20 6e 6f 74 0a 20 20 20 ssert is not.
18e0: 20 20 20 20 20 20 20 2a 2a 20 75 73 65 66 75 6c ** useful
18f0: 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 2a in that case. *
1900: 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 /. retu
1910: 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a rn 1;. }.
1920: 20 20 20 20 20 20 20 20 69 54 61 62 20 3d 20 70 iTab = p
1930: 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 74 6e 75 Idx->pTable->tnu
1940: 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d m;. }. }
1950: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 54 . }else{. iT
1960: 61 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 20 7d 0a ab = iRoot;. }.
1970: 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 . /* Search for
1980: 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f the required lo
1990: 63 6b 2e 20 45 69 74 68 65 72 20 61 20 77 72 69 ck. Either a wri
19a0: 74 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f 6f 74 2d te-lock on root-
19b0: 70 61 67 65 20 69 54 61 62 2c 20 61 20 0a 20 20 page iTab, a .
19c0: 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e ** write-lock on
19d0: 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c the schema tabl
19e0: 65 2c 20 6f 72 20 28 69 66 20 74 68 65 20 63 6c e, or (if the cl
19f0: 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 29 ient is reading)
1a00: 20 61 0a 20 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 a. ** read-loc
1a10: 6b 20 6f 6e 20 69 54 61 62 20 77 69 6c 6c 20 73 k on iTab will s
1a20: 75 66 66 69 63 65 2e 20 52 65 74 75 72 6e 20 31 uffice. Return 1
1a30: 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 if any of these
1a40: 20 61 72 65 20 66 6f 75 6e 64 2e 20 20 2a 2f 0a are found. */.
1a50: 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 72 for(pLock=pBtr
1a60: 65 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 ee->pBt->pLock;
1a70: 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f pLock; pLock=pLo
1a80: 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ck->pNext){.
1a90: 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 if( pLock->pBtre
1aa0: 65 3d 3d 70 42 74 72 65 65 20 0a 20 20 20 20 20 e==pBtree .
1ab0: 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c && (pLock->iTabl
1ac0: 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70 4c 6f 63 e==iTab || (pLoc
1ad0: 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f k->eLock==WRITE_
1ae0: 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 69 LOCK && pLock->i
1af0: 54 61 62 6c 65 3d 3d 31 29 29 0a 20 20 20 20 20 Table==1)).
1b00: 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e && pLock->eLock>
1b10: 3d 65 4c 6f 63 6b 54 79 70 65 20 0a 20 20 20 20 =eLockType .
1b20: 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1b30: 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 1;. }. }..
1b40: 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 66 69 6e /* Failed to fin
1b50: 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c d the required l
1b60: 6f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e ock. */. return
1b70: 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0;.}.#endif /*
1b80: 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a SQLITE_DEBUG */.
1b90: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1ba0: 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 EBUG./*.**** Thi
1bb0: 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 s function may b
1bc0: 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f e used as part o
1bd0: 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 f assert() state
1be0: 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2a 2a 2a ments only. ****
1bf0: 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 .**.** Return tr
1c00: 75 65 20 69 66 20 69 74 20 77 6f 75 6c 64 20 62 ue if it would b
1c10: 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 e illegal for pB
1c20: 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 6e tree to write in
1c30: 74 6f 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 to the.** table
1c40: 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 or index rooted
1c50: 61 74 20 69 52 6f 6f 74 20 62 65 63 61 75 73 65 at iRoot because
1c60: 20 6f 74 68 65 72 20 73 68 61 72 65 64 20 63 6f other shared co
1c70: 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a nnections are.**
1c80: 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 20 simultaneously
1c90: 72 65 61 64 69 6e 67 20 74 68 61 74 20 73 61 6d reading that sam
1ca0: 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1cb0: 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c ..**.** It is il
1cc0: 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 legal for pBtree
1cd0: 20 74 6f 20 77 72 69 74 65 20 69 66 20 73 6f 6d to write if som
1ce0: 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 e other Btree ob
1cf0: 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 73 68 61 ject that.** sha
1d00: 72 65 73 20 74 68 65 20 73 61 6d 65 20 42 74 53 res the same BtS
1d10: 68 61 72 65 64 20 6f 62 6a 65 63 74 20 69 73 20 hared object is
1d20: 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e currently readin
1d30: 67 20 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 g or writing.**
1d40: 74 68 65 20 69 52 6f 6f 74 20 74 61 62 6c 65 2e the iRoot table.
1d50: 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 Except, if the
1d60: 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 6a other Btree obj
1d70: 65 63 74 20 68 61 73 20 74 68 65 0a 2a 2a 20 72 ect has the.** r
1d80: 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 ead-uncommitted
1d90: 66 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20 69 flag set, then i
1da0: 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 t is OK for the
1db0: 6f 74 68 65 72 20 6f 62 6a 65 63 74 20 74 6f 0a other object to.
1dc0: 2a 2a 20 68 61 76 65 20 61 20 72 65 61 64 20 63 ** have a read c
1dd0: 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ursor..**.** For
1de0: 20 65 78 61 6d 70 6c 65 2c 20 62 65 66 6f 72 65 example, before
1df0: 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 writing to any
1e00: 70 61 72 74 20 6f 66 20 74 68 65 20 74 61 62 6c part of the tabl
1e10: 65 20 6f 72 20 69 6e 64 65 78 0a 2a 2a 20 72 6f e or index.** ro
1e20: 6f 74 65 64 20 61 74 20 70 61 67 65 20 69 52 6f oted at page iRo
1e30: 6f 74 2c 20 6f 6e 65 20 73 68 6f 75 6c 64 20 63 ot, one should c
1e40: 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 all:.**.** as
1e50: 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43 6f sert( !hasReadCo
1e60: 6e 66 6c 69 63 74 73 28 70 42 74 72 65 65 2c 20 nflicts(pBtree,
1e70: 69 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a 73 74 61 iRoot) );.*/.sta
1e80: 74 69 63 20 69 6e 74 20 68 61 73 52 65 61 64 43 tic int hasReadC
1e90: 6f 6e 66 6c 69 63 74 73 28 42 74 72 65 65 20 2a onflicts(Btree *
1ea0: 70 42 74 72 65 65 2c 20 50 67 6e 6f 20 69 52 6f pBtree, Pgno iRo
1eb0: 6f 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 ot){. BtCursor
1ec0: 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 *p;. for(p=pBtr
1ed0: 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 ee->pBt->pCursor
1ee0: 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
1ef0: 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e {. if( p->pgn
1f00: 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 0a 20 20 oRoot==iRoot .
1f10: 20 20 20 26 26 20 70 2d 3e 70 42 74 72 65 65 21 && p->pBtree!
1f20: 3d 70 42 74 72 65 65 0a 20 20 20 20 20 26 26 20 =pBtree. &&
1f30: 30 3d 3d 28 70 2d 3e 70 42 74 72 65 65 2d 3e 64 0==(p->pBtree->d
1f40: 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
1f50: 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 29 0a E_ReadUncommit).
1f60: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ){. ret
1f70: 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
1f80: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
1f90: 65 6e 64 69 66 20 20 20 20 2f 2a 20 23 69 66 64 endif /* #ifd
1fa0: 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 ef SQLITE_DEBUG
1fb0: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 */../*.** Query
1fc0: 74 6f 20 73 65 65 20 69 66 20 42 74 72 65 65 20 to see if Btree
1fd0: 68 61 6e 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 handle p may obt
1fe0: 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 ain a lock of ty
1ff0: 70 65 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 pe eLock .** (RE
2000: 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 AD_LOCK or WRITE
2010: 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 _LOCK) on the ta
2020: 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 ble with root-pa
2030: 67 65 20 69 54 61 62 2e 20 52 65 74 75 72 6e 0a ge iTab. Return.
2040: 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 ** SQLITE_OK if
2050: 74 68 65 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 the lock may be
2060: 6f 62 74 61 69 6e 65 64 20 28 62 79 20 63 61 6c obtained (by cal
2070: 6c 69 6e 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 ling.** setShare
2080: 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
2090: 29 29 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f )), or SQLITE_LO
20a0: 43 4b 45 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a CKED if not..*/.
20b0: 73 74 61 74 69 63 20 69 6e 74 20 71 75 65 72 79 static int query
20c0: 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
20d0: 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 Lock(Btree *p, P
20e0: 67 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f gno iTab, u8 eLo
20f0: 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ck){. BtShared
2100: 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
2110: 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a BtLock *pIter;.
2120: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
2130: 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
2140: 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
2150: 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ( eLock==READ_LO
2160: 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 CK || eLock==WRI
2170: 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 TE_LOCK );. ass
2180: 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b ert( p->db!=0 );
2190: 0a 20 20 61 73 73 65 72 74 28 20 21 28 70 2d 3e . assert( !(p->
21a0: 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 db->flags&SQLITE
21b0: 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 29 7c 7c _ReadUncommit)||
21c0: 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
21d0: 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 K||iTab==1 );.
21e0: 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 73 74 . /* If request
21f0: 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
2200: 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 65 65 , then the Btree
2210: 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 6f 70 must have an op
2220: 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 en write. ** tr
2230: 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 ansaction on thi
2240: 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 s file. And, obv
2250: 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 iously, for this
2260: 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 65 20 to be so there
2270: 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e . ** must be an
2280: 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e open write tran
2290: 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 saction on the f
22a0: 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f ile itself.. */
22b0: 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
22c0: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 ==READ_LOCK || (
22d0: 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 p==pBt->pWriter
22e0: 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 && p->inTrans==T
22f0: 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 RANS_WRITE) );.
2300: 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
2310: 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 READ_LOCK || pBt
2320: 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
2330: 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
2340: 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 . /* This rou
2350: 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 tine is a no-op
2360: 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 if the shared-ca
2370: 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c che is not enabl
2380: 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e ed */. if( !p->
2390: 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
23a0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
23b0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 ;. }.. /* If s
23c0: 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 ome other connec
23d0: 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 tion is holding
23e0: 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
23f0: 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 k, the. ** requ
2400: 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e ested lock may n
2410: 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a ot be obtained..
2420: 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e */. if( pBt->
2430: 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 28 70 pWriter!=p && (p
2440: 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
2450: 54 53 5f 45 58 43 4c 55 53 49 56 45 29 21 3d 30 TS_EXCLUSIVE)!=0
2460: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 ){. sqlite3C
2470: 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
2480: 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 57 72 (p->db, pBt->pWr
2490: 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 20 72 iter->db);. r
24a0: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
24b0: 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b KED_SHAREDCACHE;
24c0: 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 74 65 . }.. for(pIte
24d0: 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 r=pBt->pLock; pI
24e0: 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 ter; pIter=pIter
24f0: 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f 2a ->pNext){. /*
2500: 20 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28 The condition (
2510: 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c pIter->eLock!=eL
2520: 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c ock) in the foll
2530: 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20 owing if(...) .
2540: 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 ** statement
2550: 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 74 is a simplificat
2560: 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 ion of:. **.
2570: 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 6b 3d 3d ** (eLock==
2580: 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 20 70 49 WRITE_LOCK || pI
2590: 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 ter->eLock==WRIT
25a0: 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a 2a 0a 20 E_LOCK). **.
25b0: 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 20 6b ** since we k
25c0: 6e 6f 77 20 74 68 61 74 20 69 66 20 65 4c 6f 63 now that if eLoc
25d0: 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 2c 20 74 k==WRITE_LOCK, t
25e0: 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e hen no other con
25f0: 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6d nection. ** m
2600: 61 79 20 68 6f 6c 64 20 61 20 57 52 49 54 45 5f ay hold a WRITE_
2610: 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 61 62 6c LOCK on any tabl
2620: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 28 e in this file (
2630: 73 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 0a since there can.
2640: 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 ** only be a
2650: 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 29 2e single writer).
2660: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
2670: 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b rt( pIter->eLock
2680: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 ==READ_LOCK || p
2690: 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 Iter->eLock==WRI
26a0: 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 TE_LOCK );. a
26b0: 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 ssert( eLock==RE
26c0: 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 AD_LOCK || pIter
26d0: 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20 70 ->pBtree==p || p
26e0: 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 Iter->eLock==REA
26f0: 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 D_LOCK);. if(
2700: 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d pIter->pBtree!=
2710: 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54 61 62 p && pIter->iTab
2720: 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 49 74 65 le==iTab && pIte
2730: 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20 r->eLock!=eLock
2740: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
2750: 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 ConnectionBlocke
2760: 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 72 2d 3e d(p->db, pIter->
2770: 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 pBtree->db);.
2780: 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d 3d 57 52 if( eLock==WR
2790: 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 ITE_LOCK ){.
27a0: 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 assert( p==p
27b0: 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 Bt->pWriter );.
27c0: 20 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 pBt->btsF
27d0: 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 45 4e 44 lags |= BTS_PEND
27e0: 49 4e 47 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ING;. }.
27f0: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
2800: 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
2810: 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 CHE;. }. }.
2820: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
2830: 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 K;.}.#endif /* !
2840: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
2850: 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 ED_CACHE */..#if
2860: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2870: 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
2880: 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f .** Add a lock o
2890: 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 n the table with
28a0: 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c root-page iTabl
28b0: 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d e to the shared-
28c0: 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 btree used.** by
28d0: 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e Btree handle p.
28e0: 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b Parameter eLock
28f0: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
2900: 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a READ_LOCK or .**
2910: 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a WRITE_LOCK..**.
2920: 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
2930: 20 61 73 73 75 6d 65 73 20 74 68 65 20 66 6f 6c assumes the fol
2940: 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
2950: 28 61 29 20 54 68 65 20 73 70 65 63 69 66 69 65 (a) The specifie
2960: 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 70 d Btree object p
2970: 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f is connected to
2980: 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20 a sharable.**
2990: 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 6f database (o
29a0: 6e 65 20 77 69 74 68 20 74 68 65 20 42 74 53 68 ne with the BtSh
29b0: 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20 66 6c ared.sharable fl
29c0: 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a ag set), and.**.
29d0: 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 ** (b) No othe
29e0: 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20 r Btree objects
29f0: 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 61 74 hold a lock that
2a00: 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20 conflicts.**
2a10: 20 20 20 20 77 69 74 68 20 74 68 65 20 72 65 71 with the req
2a20: 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65 uested lock (i.e
2a30: 2e 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 . querySharedCac
2a40: 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61 heTableLock() ha
2a50: 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 65 61 s.** alrea
2a60: 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 dy been called a
2a70: 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 nd returned SQLI
2a80: 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 TE_OK)..**.** SQ
2a90: 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
2aa0: 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 ned if the lock
2ab0: 69 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 is added success
2ac0: 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f fully. SQLITE_NO
2ad0: 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 75 72 MEM .** is retur
2ae0: 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 ned if a malloc
2af0: 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a attempt fails..*
2b00: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 /.static int set
2b10: 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
2b20: 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 Lock(Btree *p, P
2b30: 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 gno iTable, u8 e
2b40: 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 Lock){. BtShare
2b50: 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
2b60: 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b . BtLock *pLock
2b70: 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a = 0;. BtLock *
2b80: 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 pIter;.. assert
2b90: 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
2ba0: 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
2bb0: 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
2bc0: 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f READ_LOCK || eLo
2bd0: 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
2be0: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
2bf0: 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 b!=0 );.. /* A
2c00: 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 connection with
2c10: 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 the read-uncommi
2c20: 74 74 65 64 20 66 6c 61 67 20 73 65 74 20 77 69 tted flag set wi
2c30: 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74 6f 0a ll never try to.
2c40: 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 72 65 ** obtain a re
2c50: 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68 ad-lock using th
2c60: 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 is function. The
2c70: 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20 only read-lock
2c80: 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79 obtained. ** by
2c90: 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e a connection in
2ca0: 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 read-uncommitte
2cb0: 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 68 65 d mode is on the
2cc0: 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a sqlite_master .
2cd0: 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 ** table, and
2ce0: 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 that lock is obt
2cf0: 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65 42 65 ained in BtreeBe
2d00: 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a ginTrans(). */.
2d10: 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d assert( 0==(p-
2d20: 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 >db->flags&SQLIT
2d30: 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 29 20 E_ReadUncommit)
2d40: 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f || eLock==WRITE_
2d50: 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 LOCK );.. /* Th
2d60: 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
2d70: 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ld only be calle
2d80: 64 20 6f 6e 20 61 20 73 68 61 72 61 62 6c 65 20 d on a sharable
2d90: 62 2d 74 72 65 65 20 61 66 74 65 72 20 69 74 20 b-tree after it
2da0: 0a 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 64 . ** has been d
2db0: 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e etermined that n
2dc0: 6f 20 6f 74 68 65 72 20 62 2d 74 72 65 65 20 68 o other b-tree h
2dd0: 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69 63 74 69 olds a conflicti
2de0: 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 ng lock. */. a
2df0: 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
2e00: 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 le );. assert(
2e10: 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 SQLITE_OK==query
2e20: 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
2e30: 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 Lock(p, iTable,
2e40: 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 eLock) );.. /*
2e50: 46 69 72 73 74 20 73 65 61 72 63 68 20 74 68 65 First search the
2e60: 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 65 78 69 list for an exi
2e70: 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 sting lock on th
2e80: 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 is table. */. f
2e90: 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c or(pIter=pBt->pL
2ea0: 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 ock; pIter; pIte
2eb0: 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b r=pIter->pNext){
2ec0: 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e . if( pIter->
2ed0: 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 iTable==iTable &
2ee0: 26 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d & pIter->pBtree=
2ef0: 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 =p ){. pLoc
2f00: 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 20 20 20 k = pIter;.
2f10: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
2f20: 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 }.. /* If the a
2f30: 62 6f 76 65 20 73 65 61 72 63 68 20 64 69 64 20 bove search did
2f40: 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63 not find a BtLoc
2f50: 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63 69 61 k struct associa
2f60: 74 69 6e 67 20 42 74 72 65 65 20 70 0a 20 20 2a ting Btree p. *
2f70: 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69 54 61 * with table iTa
2f80: 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e ble, allocate on
2f90: 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e e and link it in
2fa0: 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a to the list.. *
2fb0: 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 /. if( !pLock )
2fc0: 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42 {. pLock = (B
2fd0: 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d tLock *)sqlite3M
2fe0: 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 allocZero(sizeof
2ff0: 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69 (BtLock));. i
3000: 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 f( !pLock ){.
3010: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
3020: 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 _NOMEM_BKPT;.
3030: 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 }. pLock->iT
3040: 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 able = iTable;.
3050: 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 pLock->pBtree
3060: 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d = p;. pLock-
3070: 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c >pNext = pBt->pL
3080: 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c ock;. pBt->pL
3090: 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d ock = pLock;. }
30a0: 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 .. /* Set the B
30b0: 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 tLock.eLock vari
30c0: 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 able to the maxi
30d0: 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 mum of the curre
30e0: 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 nt lock. ** and
30f0: 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
3100: 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 ock. This means
3110: 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 if a write-lock
3120: 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 was already held
3130: 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 . ** and a read
3140: 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c -lock requested,
3150: 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 we don't incorr
3160: 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 ectly downgrade
3170: 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 the lock.. */.
3180: 20 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c assert( WRITE_L
3190: 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b OCK>READ_LOCK );
31a0: 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f . if( eLock>pLo
31b0: 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 ck->eLock ){.
31c0: 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 pLock->eLock =
31d0: 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 eLock;. }.. re
31e0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
31f0: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
3200: 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
3210: 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
3220: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
3230: 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
3240: 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 Release all the
3250: 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f table locks (lo
3260: 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 cks obtained via
3270: 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 calls to.** the
3280: 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
3290: 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 ableLock() proce
32a0: 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42 74 dure) held by Bt
32b0: 72 65 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a ree object p..**
32c0: 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
32d0: 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 42 n assumes that B
32e0: 74 72 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70 tree p has an op
32f0: 65 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 en read or write
3300: 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
3310: 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 . If it does not
3320: 2c 20 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50 , then the BTS_P
3330: 45 4e 44 49 4e 47 20 66 6c 61 67 0a 2a 2a 20 6d ENDING flag.** m
3340: 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c ay be incorrectl
3350: 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 y cleared..*/.st
3360: 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 atic void clearA
3370: 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
3380: 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 leLocks(Btree *p
3390: 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
33a0: 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
33b0: 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d tLock **ppIter =
33c0: 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 &pBt->pLock;..
33d0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
33e0: 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
33f0: 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
3400: 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 p->sharable || 0
3410: 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 ==*ppIter );. a
3420: 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
3430: 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 s>0 );.. while(
3440: 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 *ppIter ){.
3450: 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 BtLock *pLock =
3460: 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 *ppIter;. ass
3470: 65 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c ert( (pBt->btsFl
3480: 61 67 73 20 26 20 42 54 53 5f 45 58 43 4c 55 53 ags & BTS_EXCLUS
3490: 49 56 45 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e IVE)==0 || pBt->
34a0: 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e pWriter==pLock->
34b0: 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 pBtree );. as
34c0: 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 sert( pLock->pBt
34d0: 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c ree->inTrans>=pL
34e0: 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 ock->eLock );.
34f0: 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 if( pLock->pBt
3500: 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
3510: 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d *ppIter = pLock-
3520: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 >pNext;. as
3530: 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 sert( pLock->iTa
3540: 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d ble!=1 || pLock=
3550: 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 =&p->lock );.
3560: 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 if( pLock->iT
3570: 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 20 able!=1 ){.
3580: 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
3590: 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a pLock);. }.
35a0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
35b0: 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b ppIter = &pLock
35c0: 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
35d0: 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 }.. assert( (p
35e0: 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
35f0: 54 53 5f 50 45 4e 44 49 4e 47 29 3d 3d 30 20 7c TS_PENDING)==0 |
3600: 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 | pBt->pWriter )
3610: 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 ;. if( pBt->pWr
3620: 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 iter==p ){. p
3630: 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b Bt->pWriter = 0;
3640: 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 . pBt->btsFla
3650: 67 73 20 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c gs &= ~(BTS_EXCL
3660: 55 53 49 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e USIVE|BTS_PENDIN
3670: 47 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 G);. }else if(
3680: 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
3690: 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 n==2 ){. /* T
36a0: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
36b0: 63 61 6c 6c 65 64 20 77 68 65 6e 20 42 74 72 65 called when Btre
36c0: 65 20 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e e p is concludin
36d0: 67 20 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72 g its . ** tr
36e0: 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68 ansaction. If th
36f0: 65 72 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 ere currently ex
3700: 69 73 74 73 20 61 20 77 72 69 74 65 72 2c 20 61 ists a writer, a
3710: 6e 64 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20 nd p is not.
3720: 2a 2a 20 74 68 61 74 20 77 72 69 74 65 72 2c 20 ** that writer,
3730: 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 then the number
3740: 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 of locks held by
3750: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 connections oth
3760: 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 er. ** than t
3770: 68 65 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 he writer must b
3780: 65 20 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 e about to drop
3790: 74 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 to zero. In this
37a0: 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74 case. ** set
37b0: 20 74 68 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 the BTS_PENDING
37c0: 20 66 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 flag to 0..
37d0: 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 **. ** If the
37e0: 72 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e re is not curren
37f0: 74 6c 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 tly a writer, th
3800: 65 6e 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 6d en BTS_PENDING m
3810: 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 ust. ** be ze
3820: 72 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 ro already. So t
3830: 68 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 his next line is
3840: 20 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 harmless in tha
3850: 74 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 t case.. */.
3860: 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 pBt->btsFlags
3870: 20 26 3d 20 7e 42 54 53 5f 50 45 4e 44 49 4e 47 &= ~BTS_PENDING
3880: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
3890: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 his function cha
38a0: 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c nges all write-l
38b0: 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 ocks held by Btr
38c0: 65 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c ee p into read-l
38d0: 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ocks..*/.static
38e0: 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c void downgradeAl
38f0: 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
3900: 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 eLocks(Btree *p)
3910: 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
3920: 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 t = p->pBt;. if
3930: 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d ( pBt->pWriter==
3940: 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 p ){. BtLock
3950: 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d *pLock;. pBt-
3960: 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 >pWriter = 0;.
3970: 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 pBt->btsFlags
3980: 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 49 &= ~(BTS_EXCLUSI
3990: 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 3b VE|BTS_PENDING);
39a0: 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 . for(pLock=p
39b0: 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b Bt->pLock; pLock
39c0: 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 ; pLock=pLock->p
39d0: 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 Next){. ass
39e0: 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 ert( pLock->eLoc
39f0: 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
3a00: 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 pLock->pBtree==p
3a10: 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d );. pLock-
3a20: 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f >eLock = READ_LO
3a30: 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a CK;. }. }.}.
3a40: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
3a50: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
3a60: 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 CHE */..#ifndef
3a70: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 SQLITE_OMIT_CONC
3a80: 55 52 52 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 URRENT./*.** The
3a90: 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
3aa0: 74 75 72 65 20 2d 20 42 74 72 65 65 50 74 72 6d ture - BtreePtrm
3ab0: 61 70 20 2d 20 73 74 6f 72 65 73 20 74 68 65 20 ap - stores the
3ac0: 69 6e 2d 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 in-memory pointe
3ad0: 72 20 6d 61 70 0a 2a 2a 20 75 73 65 64 20 66 6f r map.** used fo
3ae0: 72 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 r newly allocate
3af0: 64 20 70 61 67 65 73 20 69 6e 20 43 4f 4e 43 55 d pages in CONCU
3b00: 52 52 45 4e 54 20 74 72 61 6e 73 61 63 74 69 6f RRENT transactio
3b10: 6e 73 2e 20 53 75 63 68 20 70 61 67 65 73 20 61 ns. Such pages a
3b20: 72 65 0a 2a 2a 20 61 6c 77 61 79 73 20 61 6c 6c re.** always all
3b30: 6f 63 61 74 65 64 20 69 6e 20 61 20 63 6f 6e 74 ocated in a cont
3b40: 69 67 75 6f 75 73 20 62 6c 6f 63 6b 20 28 66 72 iguous block (fr
3b50: 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 om the end of th
3b60: 65 20 66 69 6c 65 29 20 73 74 61 72 74 69 6e 67 e file) starting
3b70: 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 42 74 .** with page Bt
3b80: 72 65 65 50 74 72 6d 61 70 2e 69 46 69 72 73 74 reePtrmap.iFirst
3b90: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
3ba0: 75 63 74 20 52 6f 6c 6c 62 61 63 6b 45 6e 74 72 uct RollbackEntr
3bb0: 79 20 52 6f 6c 6c 62 61 63 6b 45 6e 74 72 79 3b y RollbackEntry;
3bc0: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
3bd0: 50 74 72 6d 61 70 45 6e 74 72 79 20 50 74 72 6d PtrmapEntry Ptrm
3be0: 61 70 45 6e 74 72 79 3b 0a 73 74 72 75 63 74 20 apEntry;.struct
3bf0: 50 74 72 6d 61 70 45 6e 74 72 79 20 7b 0a 20 20 PtrmapEntry {.
3c00: 50 67 6e 6f 20 70 61 72 65 6e 74 3b 0a 20 20 75 Pgno parent;. u
3c10: 38 20 65 54 79 70 65 3b 0a 7d 3b 0a 73 74 72 75 8 eType;.};.stru
3c20: 63 74 20 52 6f 6c 6c 62 61 63 6b 45 6e 74 72 79 ct RollbackEntry
3c30: 20 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a {. Pgno pgno;.
3c40: 20 20 50 67 6e 6f 20 70 61 72 65 6e 74 3b 0a 20 Pgno parent;.
3c50: 20 75 38 20 65 54 79 70 65 3b 0a 7d 3b 0a 73 74 u8 eType;.};.st
3c60: 72 75 63 74 20 42 74 72 65 65 50 74 72 6d 61 70 ruct BtreePtrmap
3c70: 20 7b 0a 20 20 50 67 6e 6f 20 69 46 69 72 73 74 {. Pgno iFirst
3c80: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
3c90: 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 65 /* First ne
3ca0: 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 50 w page number aP
3cb0: 74 72 5b 30 5d 20 2a 2f 0a 0a 20 20 69 6e 74 20 tr[0] */.. int
3cc0: 6e 50 74 72 41 6c 6c 6f 63 3b 20 20 20 20 20 20 nPtrAlloc;
3cd0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
3ce0: 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
3cf0: 20 61 50 74 72 5b 5d 20 61 72 72 61 79 20 2a 2f aPtr[] array */
3d00: 0a 20 20 50 74 72 6d 61 70 45 6e 74 72 79 20 2a . PtrmapEntry *
3d10: 61 50 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 aPtr;
3d20: 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 70 /* Array of p
3d30: 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 arent page numbe
3d40: 72 73 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 76 rs */.. int nSv
3d50: 70 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pt;
3d60: 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
3d70: 20 73 69 7a 65 20 6f 66 20 61 53 76 70 74 5b 5d size of aSvpt[]
3d80: 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 array */. int
3d90: 6e 53 76 70 74 41 6c 6c 6f 63 3b 20 20 20 20 20 nSvptAlloc;
3da0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
3db0: 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
3dc0: 20 61 53 76 70 74 5b 5d 20 2a 2f 0a 20 20 69 6e aSvpt[] */. in
3dd0: 74 20 2a 61 53 76 70 74 3b 20 20 20 20 20 20 20 t *aSvpt;
3de0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
3df0: 20 46 69 72 73 74 20 61 52 6f 6c 6c 62 61 63 6b First aRollback
3e00: 5b 5d 20 65 6e 74 72 79 20 66 6f 72 20 73 61 76 [] entry for sav
3e10: 65 70 6f 69 6e 74 20 69 20 2a 2f 0a 0a 20 20 69 epoint i */.. i
3e20: 6e 74 20 6e 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 nt nRollback;
3e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
3e40: 2a 20 55 73 65 64 20 73 69 7a 65 20 6f 66 20 61 * Used size of a
3e50: 52 6f 6c 6c 62 61 63 6b 5b 5d 20 61 72 72 61 79 Rollback[] array
3e60: 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6c 6c 62 */. int nRollb
3e70: 61 63 6b 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 ackAlloc;
3e80: 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 /* Allocat
3e90: 65 64 20 73 69 7a 65 20 6f 66 20 61 52 6f 6c 6c ed size of aRoll
3ea0: 62 61 63 6b 5b 5d 20 61 72 72 61 79 20 2a 2f 0a back[] array */.
3eb0: 20 20 52 6f 6c 6c 62 61 63 6b 45 6e 74 72 79 20 RollbackEntry
3ec0: 2a 61 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20 20 *aRollback;
3ed0: 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 6f /* Array of ro
3ee0: 6c 6c 62 61 63 6b 20 65 6e 74 72 69 65 73 20 2a llback entries *
3ef0: 2f 0a 7d 3b 0a 0a 2f 2a 20 21 64 65 66 69 6e 65 /.};../* !define
3f00: 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f d(SQLITE_OMIT_CO
3f10: 4e 43 55 52 52 45 4e 54 29 0a 2a 2a 0a 2a 2a 20 NCURRENT).**.**
3f20: 49 66 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 If page number p
3f30: 67 6e 6f 20 69 73 20 67 72 65 61 74 65 72 20 74 gno is greater t
3f40: 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
3f50: 42 74 72 65 65 50 74 72 6d 61 70 2e 69 46 69 72 BtreePtrmap.iFir
3f60: 73 74 2c 20 0a 2a 2a 20 73 74 6f 72 65 20 61 6e st, .** store an
3f70: 20 65 6e 74 72 79 20 66 6f 72 20 69 74 20 69 6e entry for it in
3f80: 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
3f90: 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
3fa0: 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 50 tatic int btreeP
3fb0: 74 72 6d 61 70 53 74 6f 72 65 28 0a 20 20 42 74 trmapStore(. Bt
3fc0: 53 68 61 72 65 64 20 2a 70 42 74 2c 0a 20 20 50 Shared *pBt,. P
3fd0: 67 6e 6f 20 70 67 6e 6f 2c 0a 20 20 75 38 20 65 gno pgno,. u8 e
3fe0: 54 79 70 65 2c 20 0a 20 20 50 67 6e 6f 20 70 61 Type, . Pgno pa
3ff0: 72 65 6e 74 0a 29 7b 0a 20 20 42 74 72 65 65 50 rent.){. BtreeP
4000: 74 72 6d 61 70 20 2a 70 4d 61 70 20 3d 20 70 42 trmap *pMap = pB
4010: 74 2d 3e 70 4d 61 70 3b 0a 20 20 69 66 28 20 70 t->pMap;. if( p
4020: 67 6e 6f 3e 3d 70 4d 61 70 2d 3e 69 46 69 72 73 gno>=pMap->iFirs
4030: 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 45 6e t ){. int iEn
4040: 74 72 79 20 3d 20 70 67 6e 6f 20 2d 20 70 4d 61 try = pgno - pMa
4050: 70 2d 3e 69 46 69 72 73 74 3b 0a 0a 20 20 20 20 p->iFirst;..
4060: 2f 2a 20 47 72 6f 77 20 74 68 65 20 61 50 74 72 /* Grow the aPtr
4070: 5b 5d 20 61 72 72 61 79 20 61 73 20 72 65 71 75 [] array as requ
4080: 69 72 65 64 20 2a 2f 0a 20 20 20 20 77 68 69 6c ired */. whil
4090: 65 28 20 69 45 6e 74 72 79 3e 3d 70 4d 61 70 2d e( iEntry>=pMap-
40a0: 3e 6e 50 74 72 41 6c 6c 6f 63 20 29 7b 0a 20 20 >nPtrAlloc ){.
40b0: 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 70 int nNew = p
40c0: 4d 61 70 2d 3e 6e 50 74 72 41 6c 6c 6f 63 20 3f Map->nPtrAlloc ?
40d0: 20 70 4d 61 70 2d 3e 6e 50 74 72 41 6c 6c 6f 63 pMap->nPtrAlloc
40e0: 2a 32 20 3a 20 31 36 3b 0a 20 20 20 20 20 20 50 *2 : 16;. P
40f0: 74 72 6d 61 70 45 6e 74 72 79 20 2a 61 4e 65 77 trmapEntry *aNew
4100: 20 3d 20 28 50 74 72 6d 61 70 45 6e 74 72 79 2a = (PtrmapEntry*
4110: 29 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 )sqlite3_realloc
4120: 28 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 70 (. pMap
4130: 2d 3e 61 50 74 72 2c 20 6e 4e 65 77 2a 73 69 7a ->aPtr, nNew*siz
4140: 65 6f 66 28 50 74 72 6d 61 70 45 6e 74 72 79 29 eof(PtrmapEntry)
4150: 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
4160: 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 if( aNew==0 ){.
4170: 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
4180: 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
4190: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
41a0: 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e 4e int nByte = (nN
41b0: 65 77 2d 70 4d 61 70 2d 3e 6e 50 74 72 41 6c 6c ew-pMap->nPtrAll
41c0: 6f 63 29 2a 73 69 7a 65 6f 66 28 50 74 72 6d 61 oc)*sizeof(Ptrma
41d0: 70 45 6e 74 72 79 29 3b 0a 20 20 20 20 20 20 20 pEntry);.
41e0: 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 5b 70 4d memset(&aNew[pM
41f0: 61 70 2d 3e 6e 50 74 72 41 6c 6c 6f 63 5d 2c 20 ap->nPtrAlloc],
4200: 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 0, nByte);.
4210: 20 20 20 70 4d 61 70 2d 3e 61 50 74 72 20 3d 20 pMap->aPtr =
4220: 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 4d aNew;. pM
4230: 61 70 2d 3e 6e 50 74 72 41 6c 6c 6f 63 20 3d 20 ap->nPtrAlloc =
4240: 6e 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 nNew;. }.
4250: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 }.. /* Add
4260: 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 an entry to the
4270: 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 20 69 66 20 rollback log if
4280: 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20 required */.
4290: 69 66 28 20 70 4d 61 70 2d 3e 6e 53 76 70 74 3e if( pMap->nSvpt>
42a0: 30 20 26 26 20 70 4d 61 70 2d 3e 61 50 74 72 5b 0 && pMap->aPtr[
42b0: 69 45 6e 74 72 79 5d 2e 70 61 72 65 6e 74 20 29 iEntry].parent )
42c0: 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 61 70 {. if( pMap
42d0: 2d 3e 6e 52 6f 6c 6c 62 61 63 6b 3e 3d 70 4d 61 ->nRollback>=pMa
42e0: 70 2d 3e 6e 52 6f 6c 6c 62 61 63 6b 41 6c 6c 6f p->nRollbackAllo
42f0: 63 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 c ){. int
4300: 20 6e 4e 65 77 20 3d 20 70 4d 61 70 2d 3e 6e 52 nNew = pMap->nR
4310: 6f 6c 6c 62 61 63 6b 20 3f 20 70 4d 61 70 2d 3e ollback ? pMap->
4320: 6e 52 6f 6c 6c 62 61 63 6b 2a 32 20 3a 20 31 36 nRollback*2 : 16
4330: 3b 0a 20 20 20 20 20 20 20 20 52 6f 6c 6c 62 61 ;. Rollba
4340: 63 6b 45 6e 74 72 79 20 2a 61 4e 65 77 20 3d 20 ckEntry *aNew =
4350: 28 52 6f 6c 6c 62 61 63 6b 45 6e 74 72 79 2a 29 (RollbackEntry*)
4360: 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
4370: 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 61 . pMa
4380: 70 2d 3e 61 52 6f 6c 6c 62 61 63 6b 2c 20 6e 4e p->aRollback, nN
4390: 65 77 2a 73 69 7a 65 6f 66 28 52 6f 6c 6c 62 61 ew*sizeof(Rollba
43a0: 63 6b 45 6e 74 72 79 29 0a 20 20 20 20 20 20 20 ckEntry).
43b0: 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
43c0: 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 aNew==0 ){.
43d0: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
43e0: 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
43f0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
4400: 20 20 20 70 4d 61 70 2d 3e 61 52 6f 6c 6c 62 61 pMap->aRollba
4410: 63 6b 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 ck = aNew;.
4420: 20 20 20 20 20 70 4d 61 70 2d 3e 6e 52 6f 6c 6c pMap->nRoll
4430: 62 61 63 6b 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77 backAlloc = nNew
4440: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
4450: 20 20 7d 0a 0a 20 20 20 20 20 20 70 4d 61 70 2d }.. pMap-
4460: 3e 61 52 6f 6c 6c 62 61 63 6b 5b 70 4d 61 70 2d >aRollback[pMap-
4470: 3e 6e 52 6f 6c 6c 62 61 63 6b 5d 2e 70 67 6e 6f >nRollback].pgno
4480: 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 70 = pgno;. p
4490: 4d 61 70 2d 3e 61 52 6f 6c 6c 62 61 63 6b 5b 70 Map->aRollback[p
44a0: 4d 61 70 2d 3e 6e 52 6f 6c 6c 62 61 63 6b 5d 2e Map->nRollback].
44b0: 70 61 72 65 6e 74 20 3d 20 70 4d 61 70 2d 3e 61 parent = pMap->a
44c0: 50 74 72 5b 69 45 6e 74 72 79 5d 2e 70 61 72 65 Ptr[iEntry].pare
44d0: 6e 74 3b 0a 20 20 20 20 20 20 70 4d 61 70 2d 3e nt;. pMap->
44e0: 61 52 6f 6c 6c 62 61 63 6b 5b 70 4d 61 70 2d 3e aRollback[pMap->
44f0: 6e 52 6f 6c 6c 62 61 63 6b 5d 2e 65 54 79 70 65 nRollback].eType
4500: 20 3d 20 70 4d 61 70 2d 3e 61 50 74 72 5b 69 45 = pMap->aPtr[iE
4510: 6e 74 72 79 5d 2e 65 54 79 70 65 3b 0a 20 20 20 ntry].eType;.
4520: 20 20 20 70 4d 61 70 2d 3e 6e 52 6f 6c 6c 62 61 pMap->nRollba
4530: 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ck++;. }..
4540: 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 /* Update the a
4550: 50 74 72 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 Ptr[] array */.
4560: 20 20 20 70 4d 61 70 2d 3e 61 50 74 72 5b 69 45 pMap->aPtr[iE
4570: 6e 74 72 79 5d 2e 70 61 72 65 6e 74 20 3d 20 70 ntry].parent = p
4580: 61 72 65 6e 74 3b 0a 20 20 20 20 70 4d 61 70 2d arent;. pMap-
4590: 3e 61 50 74 72 5b 69 45 6e 74 72 79 5d 2e 65 54 >aPtr[iEntry].eT
45a0: 79 70 65 20 3d 20 65 54 79 70 65 3b 0a 20 20 7d ype = eType;. }
45b0: 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
45c0: 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 21 64 65 66 E_OK;.}../* !def
45d0: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
45e0: 5f 43 4f 4e 43 55 52 52 45 4e 54 29 0a 2a 2a 0a _CONCURRENT).**.
45f0: 2a 2a 20 4f 70 65 6e 20 73 61 76 65 70 6f 69 6e ** Open savepoin
4600: 74 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 69 66 t iSavepoint, if
4610: 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
4620: 64 79 20 6f 70 65 6e 2e 0a 2a 2f 0a 73 74 61 74 dy open..*/.stat
4630: 69 63 20 69 6e 74 20 62 74 72 65 65 50 74 72 6d ic int btreePtrm
4640: 61 70 42 65 67 69 6e 28 42 74 53 68 61 72 65 64 apBegin(BtShared
4650: 20 2a 70 42 74 2c 20 69 6e 74 20 6e 53 76 70 74 *pBt, int nSvpt
4660: 29 7b 0a 20 20 42 74 72 65 65 50 74 72 6d 61 70 ){. BtreePtrmap
4670: 20 2a 70 4d 61 70 20 3d 20 70 42 74 2d 3e 70 4d *pMap = pBt->pM
4680: 61 70 3b 0a 20 20 69 66 28 20 70 4d 61 70 20 26 ap;. if( pMap &
4690: 26 20 6e 53 76 70 74 3e 70 4d 61 70 2d 3e 6e 53 & nSvpt>pMap->nS
46a0: 76 70 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 vpt ){. int i
46b0: 3b 0a 20 20 20 20 69 66 28 20 6e 53 76 70 74 3e ;. if( nSvpt>
46c0: 3d 70 4d 61 70 2d 3e 6e 53 76 70 74 41 6c 6c 6f =pMap->nSvptAllo
46d0: 63 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e c ){. int n
46e0: 4e 65 77 20 3d 20 70 4d 61 70 2d 3e 6e 53 76 70 New = pMap->nSvp
46f0: 74 41 6c 6c 6f 63 20 3f 20 70 4d 61 70 2d 3e 6e tAlloc ? pMap->n
4700: 53 76 70 74 41 6c 6c 6f 63 2a 32 20 3a 20 31 36 SvptAlloc*2 : 16
4710: 3b 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 4e 65 ;. int *aNe
4720: 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c w = sqlite3_real
4730: 6c 6f 63 28 70 4d 61 70 2d 3e 61 53 76 70 74 2c loc(pMap->aSvpt,
4740: 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2a 20 6e sizeof(int) * n
4750: 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66 28 20 New);. if(
4760: 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 aNew==0 ){.
4770: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
4780: 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 _NOMEM;. }e
4790: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4d 61 lse{. pMa
47a0: 70 2d 3e 61 53 76 70 74 20 3d 20 61 4e 65 77 3b p->aSvpt = aNew;
47b0: 0a 20 20 20 20 20 20 20 20 70 4d 61 70 2d 3e 6e . pMap->n
47c0: 53 76 70 74 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77 SvptAlloc = nNew
47d0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
47e0: 0a 20 20 20 20 66 6f 72 28 69 3d 70 4d 61 70 2d . for(i=pMap-
47f0: 3e 6e 53 76 70 74 3b 20 69 3c 6e 53 76 70 74 3b >nSvpt; i<nSvpt;
4800: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 4d 61 i++){. pMa
4810: 70 2d 3e 61 53 76 70 74 5b 69 5d 20 3d 20 70 4d p->aSvpt[i] = pM
4820: 61 70 2d 3e 6e 52 6f 6c 6c 62 61 63 6b 3b 0a 20 ap->nRollback;.
4830: 20 20 20 7d 0a 20 20 20 20 70 4d 61 70 2d 3e 6e }. pMap->n
4840: 53 76 70 74 20 3d 20 6e 53 76 70 74 3b 0a 20 20 Svpt = nSvpt;.
4850: 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
4860: 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 21 64 65 TE_OK;.}../* !de
4870: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
4880: 54 5f 43 4f 4e 43 55 52 52 45 4e 54 29 0a 2a 2a T_CONCURRENT).**
4890: 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 28 69 66 .** Rollback (if
48a0: 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
48b0: 4f 4c 4c 42 41 43 4b 29 20 6f 72 20 72 65 6c 65 OLLBACK) or rele
48c0: 61 73 65 20 28 69 66 20 6f 70 3d 3d 53 41 56 45 ase (if op==SAVE
48d0: 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 0a 2a POINT_RELEASE).*
48e0: 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 53 76 70 * savepoint iSvp
48f0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
4900: 64 20 62 74 72 65 65 50 74 72 6d 61 70 45 6e 64 d btreePtrmapEnd
4910: 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
4920: 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 76 70 int op, int iSvp
4930: 74 29 7b 0a 20 20 42 74 72 65 65 50 74 72 6d 61 t){. BtreePtrma
4940: 70 20 2a 70 4d 61 70 20 3d 20 70 42 74 2d 3e 70 p *pMap = pBt->p
4950: 4d 61 70 3b 0a 20 20 69 66 28 20 70 4d 61 70 20 Map;. if( pMap
4960: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f ){. assert( o
4970: 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
4980: 4c 42 41 43 4b 20 7c 7c 20 6f 70 3d 3d 53 41 56 LBACK || op==SAV
4990: 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 EPOINT_RELEASE )
49a0: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 ;. assert( iS
49b0: 76 70 74 3e 3d 30 20 7c 7c 20 28 69 53 76 70 74 vpt>=0 || (iSvpt
49c0: 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 ==-1 && op==SAVE
49d0: 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 POINT_ROLLBACK)
49e0: 29 3b 0a 20 20 20 20 69 66 28 20 69 53 76 70 74 );. if( iSvpt
49f0: 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 4d 61 70 <0 ){. pMap
4a00: 2d 3e 6e 53 76 70 74 20 3d 20 30 3b 0a 20 20 20 ->nSvpt = 0;.
4a10: 20 20 20 70 4d 61 70 2d 3e 6e 52 6f 6c 6c 62 61 pMap->nRollba
4a20: 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65 ck = 0;. me
4a30: 6d 73 65 74 28 70 4d 61 70 2d 3e 61 50 74 72 2c mset(pMap->aPtr,
4a40: 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 0, sizeof(Pgno)
4a50: 20 2a 20 70 4d 61 70 2d 3e 6e 50 74 72 41 6c 6c * pMap->nPtrAll
4a60: 6f 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 oc);. }else i
4a70: 66 28 20 69 53 76 70 74 3c 70 4d 61 70 2d 3e 6e f( iSvpt<pMap->n
4a80: 53 76 70 74 20 29 7b 0a 20 20 20 20 20 20 69 66 Svpt ){. if
4a90: 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f ( op==SAVEPOINT_
4aa0: 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 ROLLBACK ){.
4ab0: 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 int ii;.
4ac0: 20 20 20 20 66 6f 72 28 69 69 3d 70 4d 61 70 2d for(ii=pMap-
4ad0: 3e 6e 52 6f 6c 6c 62 61 63 6b 2d 31 3b 20 69 69 >nRollback-1; ii
4ae0: 3e 3d 70 4d 61 70 2d 3e 61 53 76 70 74 5b 69 53 >=pMap->aSvpt[iS
4af0: 76 70 74 5d 3b 20 69 69 2d 2d 29 7b 0a 20 20 20 vpt]; ii--){.
4b00: 20 20 20 20 20 20 20 52 6f 6c 6c 62 61 63 6b 45 RollbackE
4b10: 6e 74 72 79 20 2a 70 20 3d 20 26 70 4d 61 70 2d ntry *p = &pMap-
4b20: 3e 61 52 6f 6c 6c 62 61 63 6b 5b 69 69 5d 3b 0a >aRollback[ii];.
4b30: 20 20 20 20 20 20 20 20 20 20 50 74 72 6d 61 70 Ptrmap
4b40: 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 20 3d 20 Entry *pEntry =
4b50: 26 70 4d 61 70 2d 3e 61 50 74 72 5b 70 2d 3e 70 &pMap->aPtr[p->p
4b60: 67 6e 6f 20 2d 20 70 4d 61 70 2d 3e 69 46 69 72 gno - pMap->iFir
4b70: 73 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 st];. p
4b80: 45 6e 74 72 79 2d 3e 70 61 72 65 6e 74 20 3d 20 Entry->parent =
4b90: 70 2d 3e 70 61 72 65 6e 74 3b 0a 20 20 20 20 20 p->parent;.
4ba0: 20 20 20 20 20 70 45 6e 74 72 79 2d 3e 65 54 79 pEntry->eTy
4bb0: 70 65 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20 pe = p->eType;.
4bc0: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
4bd0: 0a 20 20 20 20 20 20 70 4d 61 70 2d 3e 6e 53 76 . pMap->nSv
4be0: 70 74 20 3d 20 69 53 76 70 74 20 2b 20 28 6f 70 pt = iSvpt + (op
4bf0: 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
4c00: 42 41 43 4b 29 3b 0a 20 20 20 20 20 20 70 4d 61 BACK);. pMa
4c10: 70 2d 3e 6e 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 p->nRollback = p
4c20: 4d 61 70 2d 3e 61 53 76 70 74 5b 69 53 76 70 74 Map->aSvpt[iSvpt
4c30: 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a ];. }. }.}..
4c40: 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 /* !defined(SQLI
4c50: 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 55 52 52 45 TE_OMIT_CONCURRE
4c60: 4e 54 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 NT).**.** This f
4c70: 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
4c80: 64 20 61 66 74 65 72 20 61 6e 20 43 4f 4e 43 55 d after an CONCU
4c90: 52 52 45 4e 54 20 74 72 61 6e 73 61 63 74 69 6f RRENT transactio
4ca0: 6e 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 n is opened on t
4cb0: 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 he.** database.
4cc0: 49 74 20 61 6c 6c 6f 63 61 74 65 73 20 74 68 65 It allocates the
4cd0: 20 42 74 72 65 65 50 74 72 6d 61 70 20 73 74 72 BtreePtrmap str
4ce0: 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 74 ucture used to t
4cf0: 72 61 63 6b 20 70 6f 69 6e 74 65 72 73 0a 2a 2a rack pointers.**
4d00: 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 to allocated pa
4d10: 67 65 73 20 61 6e 64 20 7a 65 72 6f 65 73 20 74 ges and zeroes t
4d20: 68 65 20 6e 46 72 65 65 2f 69 54 72 75 6e 6b 20 he nFree/iTrunk
4d30: 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 64 61 fields in the da
4d40: 74 61 62 61 73 65 20 0a 2a 2a 20 68 65 61 64 65 tabase .** heade
4d50: 72 20 6f 6e 20 70 61 67 65 20 31 2e 0a 2a 2f 0a r on page 1..*/.
4d60: 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
4d70: 50 74 72 6d 61 70 41 6c 6c 6f 63 61 74 65 28 42 PtrmapAllocate(B
4d80: 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
4d90: 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
4da0: 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e _OK;. if( pBt->
4db0: 70 4d 61 70 3d 3d 30 20 29 7b 0a 20 20 20 20 42 pMap==0 ){. B
4dc0: 74 72 65 65 50 74 72 6d 61 70 20 2a 70 4d 61 70 treePtrmap *pMap
4dd0: 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
4de0: 63 28 73 69 7a 65 6f 66 28 42 74 72 65 65 50 74 c(sizeof(BtreePt
4df0: 72 6d 61 70 29 29 3b 0a 20 20 20 20 69 66 28 20 rmap));. if(
4e00: 70 4d 61 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pMap==0 ){.
4e10: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
4e20: 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 EM;. }else{.
4e30: 20 20 20 20 20 6d 65 6d 73 65 74 28 26 70 42 74 memset(&pBt
4e40: 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
4e50: 33 32 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 32], 0, sizeof(u
4e60: 33 32 29 2a 32 29 3b 0a 20 20 20 20 20 20 6d 65 32)*2);. me
4e70: 6d 73 65 74 28 70 4d 61 70 2c 20 30 2c 20 73 69 mset(pMap, 0, si
4e80: 7a 65 6f 66 28 42 74 72 65 65 50 74 72 6d 61 70 zeof(BtreePtrmap
4e90: 29 29 3b 0a 20 20 20 20 20 20 70 4d 61 70 2d 3e ));. pMap->
4ea0: 69 46 69 72 73 74 20 3d 20 70 42 74 2d 3e 6e 50 iFirst = pBt->nP
4eb0: 61 67 65 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 age + 1;. p
4ec0: 42 74 2d 3e 70 4d 61 70 20 3d 20 70 4d 61 70 3b Bt->pMap = pMap;
4ed0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
4ee0: 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 21 64 urn rc;.}../* !d
4ef0: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
4f00: 49 54 5f 43 4f 4e 43 55 52 52 45 4e 54 29 0a 2a IT_CONCURRENT).*
4f10: 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 42 74 *.** Free any Bt
4f20: 72 65 65 50 74 72 6d 61 70 20 73 74 72 75 63 74 reePtrmap struct
4f30: 75 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ure allocated by
4f40: 20 61 6e 20 65 61 72 6c 69 65 72 20 63 61 6c 6c an earlier call
4f50: 20 74 6f 0a 2a 2a 20 62 74 72 65 65 50 74 72 6d to.** btreePtrm
4f60: 61 70 41 6c 6c 6f 63 61 74 65 28 29 2e 0a 2a 2f apAllocate()..*/
4f70: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 .static void btr
4f80: 65 65 50 74 72 6d 61 70 44 65 6c 65 74 65 28 42 eePtrmapDelete(B
4f90: 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
4fa0: 20 42 74 72 65 65 50 74 72 6d 61 70 20 2a 70 4d BtreePtrmap *pM
4fb0: 61 70 20 3d 20 70 42 74 2d 3e 70 4d 61 70 3b 0a ap = pBt->pMap;.
4fc0: 20 20 69 66 28 20 70 4d 61 70 20 29 7b 0a 20 20 if( pMap ){.
4fd0: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
4fe0: 4d 61 70 2d 3e 61 52 6f 6c 6c 62 61 63 6b 29 3b Map->aRollback);
4ff0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
5000: 65 28 70 4d 61 70 2d 3e 61 50 74 72 29 3b 0a 20 e(pMap->aPtr);.
5010: 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
5020: 70 4d 61 70 2d 3e 61 53 76 70 74 29 3b 0a 20 20 pMap->aSvpt);.
5030: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
5040: 4d 61 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 Map);. pBt->p
5050: 4d 61 70 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 Map = 0;. }.}.#
5060: 65 6c 73 65 20 20 2f 2a 20 53 51 4c 49 54 45 5f else /* SQLITE_
5070: 4f 4d 49 54 5f 43 4f 4e 43 55 52 52 45 4e 54 20 OMIT_CONCURRENT
5080: 2a 2f 0a 23 20 64 65 66 69 6e 65 20 62 74 72 65 */.# define btre
5090: 65 50 74 72 6d 61 70 41 6c 6c 6f 63 61 74 65 28 ePtrmapAllocate(
50a0: 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 x) SQLITE_OK.# d
50b0: 65 66 69 6e 65 20 62 74 72 65 65 50 74 72 6d 61 efine btreePtrma
50c0: 70 44 65 6c 65 74 65 28 78 29 20 0a 23 20 64 65 pDelete(x) .# de
50d0: 66 69 6e 65 20 62 74 72 65 65 50 74 72 6d 61 70 fine btreePtrmap
50e0: 42 65 67 69 6e 28 78 2c 79 29 20 20 53 51 4c 49 Begin(x,y) SQLI
50f0: 54 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 62 TE_OK.# define b
5100: 74 72 65 65 50 74 72 6d 61 70 45 6e 64 28 78 2c treePtrmapEnd(x,
5110: 79 2c 7a 29 20 0a 23 65 6e 64 69 66 20 2f 2a 20 y,z) .#endif /*
5120: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 SQLITE_OMIT_CONC
5130: 55 52 52 45 4e 54 20 2a 2f 0a 0a 73 74 61 74 69 URRENT */..stati
5140: 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 c void releasePa
5150: 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
5160: 65 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 e); /* Forward
5170: 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 reference */.sta
5180: 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
5190: 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 4d 65 6d 50 PageNotNull(MemP
51a0: 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a age *pPage); /*
51b0: 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
51c0: 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 20 ce */../*.*****
51d0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
51e0: 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 20 61 used inside of a
51f0: 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a 2a 2a ssert() only ***
5200: 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79 20 74 *.**.** Verify t
5210: 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 hat the cursor h
5220: 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 20 6f olds the mutex o
5230: 6e 20 69 74 73 20 42 74 53 68 61 72 65 64 0a 2a n its BtShared.*
5240: 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
5250: 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 DEBUG.static int
5260: 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
5270: 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a x(BtCursor *p){.
5280: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
5290: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 _mutex_held(p->p
52a0: 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f Bt->mutex);.}../
52b0: 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 * Verify that th
52c0: 65 20 63 75 72 73 6f 72 20 61 6e 64 20 74 68 65 e cursor and the
52d0: 20 42 74 53 68 61 72 65 64 20 61 67 72 65 65 20 BtShared agree
52e0: 61 62 6f 75 74 20 77 68 61 74 20 69 73 20 74 68 about what is th
52f0: 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 64 61 74 e current.** dat
5300: 61 62 61 73 65 20 63 6f 6e 6e 65 74 69 6f 6e 2e abase connetion.
5310: 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 This is importa
5320: 6e 74 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 nt in shared-cac
5330: 68 65 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 20 he mode. If the
5340: 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e database .** con
5350: 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 nection pointers
5360: 20 67 65 74 20 6f 75 74 2d 6f 66 2d 73 79 6e 63 get out-of-sync
5370: 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 , it is possible
5380: 20 66 6f 72 20 72 6f 75 74 69 6e 65 73 20 6c 69 for routines li
5390: 6b 65 0a 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 ke.** btreeInitP
53a0: 61 67 65 28 29 20 74 6f 20 72 65 66 65 72 65 6e age() to referen
53b0: 63 65 20 61 6e 20 73 74 61 6c 65 20 63 6f 6e 6e ce an stale conn
53c0: 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 ection pointer t
53d0: 68 61 74 20 72 65 66 65 72 65 6e 63 65 73 20 61 hat references a
53e0: 0a 2a 2a 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e .** a connection
53f0: 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 that has alread
5400: 79 20 63 6c 6f 73 65 64 2e 20 20 54 68 69 73 20 y closed. This
5410: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
5420: 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 0a inside assert().
5430: 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e ** statements on
5440: 6c 79 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70 ly and for the p
5450: 75 72 70 6f 73 65 20 6f 66 20 64 6f 75 62 6c 65 urpose of double
5460: 2d 63 68 65 63 6b 69 6e 67 20 74 68 61 74 20 74 -checking that t
5470: 68 65 20 62 74 72 65 65 20 63 6f 64 65 0a 2a 2a he btree code.**
5480: 20 64 6f 65 73 20 6b 65 65 70 20 74 68 65 20 64 does keep the d
5490: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
54a0: 6f 6e 20 70 6f 69 6e 74 65 72 73 20 75 70 2d 74 on pointers up-t
54b0: 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 o-date..*/.stati
54c0: 63 20 69 6e 74 20 63 75 72 73 6f 72 4f 77 6e 73 c int cursorOwns
54d0: 42 74 53 68 61 72 65 64 28 42 74 43 75 72 73 6f BtShared(BtCurso
54e0: 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 r *p){. assert(
54f0: 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
5500: 78 28 70 29 20 29 3b 0a 20 20 72 65 74 75 72 6e x(p) );. return
5510: 20 28 70 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3d (p->pBtree->db=
5520: 3d 70 2d 3e 70 42 74 2d 3e 64 62 29 3b 0a 7d 0a =p->pBt->db);.}.
5530: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
5540: 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 validate the ove
5550: 72 66 6c 6f 77 20 63 61 63 68 65 20 6f 66 20 74 rflow cache of t
5560: 68 65 20 63 75 72 73 6f 72 20 70 61 73 73 65 64 he cursor passed
5570: 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
5580: 67 75 6d 65 6e 74 2e 0a 2a 2a 20 6f 6e 20 74 68 gument..** on th
5590: 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20 73 e shared btree s
55a0: 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f tructure pBt..*/
55b0: 0a 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 .#define invalid
55c0: 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 ateOverflowCache
55d0: 28 70 43 75 72 29 20 28 70 43 75 72 2d 3e 63 75 (pCur) (pCur->cu
55e0: 72 46 6c 61 67 73 20 26 3d 20 7e 42 54 43 46 5f rFlags &= ~BTCF_
55f0: 56 61 6c 69 64 4f 76 66 6c 29 0a 0a 2f 2a 0a 2a ValidOvfl)../*.*
5600: 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 * Invalidate the
5610: 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
5620: 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c ist cache for al
5630: 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 l cursors opened
5640: 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 .** on the share
5650: 64 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 d btree structur
5660: 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 e pBt..*/.static
5670: 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 void invalidate
5680: 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
5690: 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
56a0: 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a . BtCursor *p;.
56b0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
56c0: 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
56d0: 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f ->mutex) );. fo
56e0: 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 r(p=pBt->pCursor
56f0: 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
5700: 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 {. invalidate
5710: 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 OverflowCache(p)
5720: 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 ;. }.}..#ifndef
5730: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
5740: 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 54 68 69 73 RBLOB./*.** This
5750: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
5760: 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 led before modif
5770: 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 ying the content
5780: 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 s of a table.**
5790: 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e to invalidate an
57a0: 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f y incrblob curso
57b0: 72 73 20 74 68 61 74 20 61 72 65 20 6f 70 65 6e rs that are open
57c0: 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 20 6f on the.** row o
57d0: 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f 77 r one of the row
57e0: 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 s being modified
57f0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d ..**.** If argum
5800: 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 ent isClearTable
5810: 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
5820: 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e he entire conten
5830: 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62 ts of the.** tab
5840: 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 le is about to b
5850: 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 e deleted. In th
5860: 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 is case invalida
5870: 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 0a te all incrblob.
5880: 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 ** cursors open
5890: 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 68 69 on any row withi
58a0: 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 n the table with
58b0: 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e 6f 52 root-page pgnoR
58c0: 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 oot..**.** Other
58d0: 77 69 73 65 2c 20 69 66 20 61 72 67 75 6d 65 6e wise, if argumen
58e0: 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 69 t isClearTable i
58f0: 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 s false, then th
5900: 65 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 72 6f e row with.** ro
5910: 77 69 64 20 69 52 6f 77 20 69 73 20 62 65 69 6e wid iRow is bein
5920: 67 20 72 65 70 6c 61 63 65 64 20 6f 72 20 64 65 g replaced or de
5930: 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 leted. In this c
5940: 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 0a 2a ase invalidate.*
5950: 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 6e 63 * only those inc
5960: 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 rblob cursors op
5970: 65 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 63 69 en on that speci
5980: 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 fic row..*/.stat
5990: 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 ic void invalida
59a0: 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 teIncrblobCursor
59b0: 73 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 s(. Btree *pBtr
59c0: 65 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ee, /*
59d0: 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
59e0: 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 e to check */.
59f0: 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 2c 20 20 Pgno pgnoRoot,
5a00: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
5a10: 61 62 6c 65 20 74 68 61 74 20 6d 69 67 68 74 20 able that might
5a20: 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 20 be changing */.
5a30: 20 69 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 i64 iRow,
5a40: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
5a50: 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 rowid that might
5a60: 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a be changing */.
5a70: 20 20 69 6e 74 20 69 73 43 6c 65 61 72 54 61 62 int isClearTab
5a80: 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 le /* Tru
5a90: 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 61 72 e if all rows ar
5aa0: 65 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 e being deleted
5ab0: 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 */.){. BtCursor
5ac0: 20 2a 70 3b 0a 20 20 69 66 28 20 70 42 74 72 65 *p;. if( pBtre
5ad0: 65 2d 3e 68 61 73 49 6e 63 72 62 6c 6f 62 43 75 e->hasIncrblobCu
5ae0: 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 r==0 ) return;.
5af0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
5b00: 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
5b10: 70 42 74 72 65 65 29 20 29 3b 0a 20 20 70 42 74 pBtree) );. pBt
5b20: 72 65 65 2d 3e 68 61 73 49 6e 63 72 62 6c 6f 62 ree->hasIncrblob
5b30: 43 75 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 Cur = 0;. for(p
5b40: 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 =pBtree->pBt->pC
5b50: 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
5b60: 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 28 Next){. if( (
5b70: 70 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 p->curFlags & BT
5b80: 43 46 5f 49 6e 63 72 62 6c 6f 62 29 21 3d 30 20 CF_Incrblob)!=0
5b90: 29 7b 0a 20 20 20 20 20 20 70 42 74 72 65 65 2d ){. pBtree-
5ba0: 3e 68 61 73 49 6e 63 72 62 6c 6f 62 43 75 72 20 >hasIncrblobCur
5bb0: 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 = 1;. if( p
5bc0: 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70 67 6e 6f ->pgnoRoot==pgno
5bd0: 52 6f 6f 74 20 26 26 20 28 69 73 43 6c 65 61 72 Root && (isClear
5be0: 54 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f Table || p->info
5bf0: 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a .nKey==iRow) ){.
5c00: 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 p->eStat
5c10: 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
5c20: 49 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ID;. }.
5c30: 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 }. }.}..#else.
5c40: 20 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69 6f /* Stub functio
5c50: 6e 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20 n when INCRBLOB
5c60: 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20 is omitted */.
5c70: 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 #define invalida
5c80: 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 teIncrblobCursor
5c90: 73 28 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 s(w,x,y,z).#endi
5ca0: 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
5cb0: 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a _INCRBLOB */../*
5cc0: 0a 2a 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f .** Set bit pgno
5cd0: 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64 of the BtShared
5ce0: 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
5cf0: 76 65 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c vec. This is cal
5d00: 6c 65 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 led .** when a p
5d10: 61 67 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 age that previou
5d20: 73 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 sly contained da
5d30: 74 61 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 ta becomes a fre
5d40: 65 2d 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 e-list leaf .**
5d50: 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
5d60: 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e BtShared.pHasCon
5d70: 74 65 6e 74 20 62 69 74 76 65 63 20 65 78 69 73 tent bitvec exis
5d80: 74 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e ts to work aroun
5d90: 64 20 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 d an obscure.**
5da0: 62 75 67 20 63 61 75 73 65 64 20 62 79 20 74 68 bug caused by th
5db0: 65 20 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 e interaction of
5dc0: 20 74 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f two useful IO o
5dd0: 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 ptimizations sur
5de0: 72 6f 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 rounding.** free
5df0: 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 -list leaf pages
5e00: 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 :.**.** 1) Whe
5e10: 6e 20 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65 n all data is de
5e20: 6c 65 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67 leted from a pag
5e30: 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 62 e and the page b
5e40: 65 63 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 ecomes.** a
5e50: 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
5e60: 70 61 67 65 2c 20 74 68 65 20 70 61 67 65 20 69 page, the page i
5e70: 73 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f s not written to
5e80: 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
5e90: 20 20 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c (as free-l
5ea0: 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 20 63 ist leaf pages c
5eb0: 6f 6e 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e ontain no meanin
5ec0: 67 66 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 gful data). Some
5ed0: 74 69 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 times.** su
5ee0: 63 68 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74 ch a page is not
5ef0: 20 65 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 even journalled
5f00: 20 28 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 (as it will not
5f10: 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a be modified,.**
5f20: 20 20 20 20 20 20 77 68 79 20 62 6f 74 68 65 72 why bother
5f30: 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f journalling it?
5f40: 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 )..**.** 2) Wh
5f50: 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c en a free-list l
5f60: 65 61 66 20 70 61 67 65 20 69 73 20 72 65 75 73 eaf page is reus
5f70: 65 64 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 ed, its content
5f80: 69 73 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 is not read.**
5f90: 20 20 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74 from the dat
5fa0: 61 62 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e abase or written
5fb0: 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
5fc0: 66 69 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 file (why should
5fd0: 20 69 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 it.** be,
5fe0: 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20 if it is not at
5ff0: 61 6c 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 all meaningful?)
6000: 2e 0a 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 ..**.** By thems
6010: 65 6c 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 elves, these opt
6020: 69 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 imizations work
6030: 66 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 fine and provide
6040: 20 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 a handy.** perf
6050: 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f ormance boost to
6060: 20 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 bulk delete or
6070: 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e insert operation
6080: 73 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a s. However, if.*
6090: 2a 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 * a page is move
60a0: 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 d to the free-li
60b0: 73 74 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 st and then reus
60c0: 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 ed within the sa
60d0: 6d 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f me.** transactio
60e0: 6e 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d n, a problem com
60f0: 65 73 20 75 70 2e 20 49 66 20 74 68 65 20 70 61 es up. If the pa
6100: 67 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 ge is not journa
6110: 6c 6c 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 lled when.** it
6120: 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
6130: 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 free-list and it
6140: 20 69 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 is also not jou
6150: 72 6e 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a rnalled when it.
6160: 2a 2a 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ** is extracted
6170: 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
6180: 73 74 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74 st and reused, t
6190: 68 65 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hen the original
61a0: 20 64 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 data.** may be
61b0: 6c 6f 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65 lost. In the eve
61c0: 6e 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b nt of a rollback
61d0: 2c 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 , it may not be
61e0: 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 possible.** to r
61f0: 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 estore the datab
6200: 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 ase to its origi
6210: 6e 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f nal configuratio
6220: 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c n..**.** The sol
6230: 75 74 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53 ution is the BtS
6240: 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
6250: 74 20 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 t bitvec. Whenev
6260: 65 72 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a er a page is .**
6270: 20 6d 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 moved to become
6280: 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 a free-list lea
6290: 66 20 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72 f page, the corr
62a0: 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 esponding bit is
62b0: 0a 2a 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62 .** set in the b
62c0: 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 itvec. Whenever
62d0: 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 65 a leaf page is e
62e0: 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 xtracted from th
62f0: 65 20 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 e free-list,.**
6300: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 optimization 2 a
6310: 62 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 64 20 bove is omitted
6320: 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e if the correspon
6330: 64 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65 ding bit is alre
6340: 61 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 ady.** set in Bt
6350: 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 Shared.pHasConte
6360: 6e 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 nt. The contents
6370: 20 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61 of the bitvec a
6380: 72 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 re cleared.** at
6390: 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 the end of ever
63a0: 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a y transaction..*
63b0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
63c0: 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 eeSetHasContent(
63d0: 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
63e0: 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 gno pgno){. int
63f0: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
6400: 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 . if( !pBt->pHa
6410: 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 sContent ){.
6420: 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 70 42 assert( pgno<=pB
6430: 74 2d 3e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 t->nPage );.
6440: 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
6450: 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
6460: 43 72 65 61 74 65 28 70 42 74 2d 3e 6e 50 61 67 Create(pBt->nPag
6470: 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 42 74 e);. if( !pBt
6480: 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b ->pHasContent ){
6490: 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
64a0: 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 TE_NOMEM_BKPT;.
64b0: 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 }. }. if( r
64c0: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
64d0: 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 74 pgno<=sqlite3Bit
64e0: 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 61 vecSize(pBt->pHa
64f0: 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 20 sContent) ){.
6500: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 rc = sqlite3Bit
6510: 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61 73 vecSet(pBt->pHas
6520: 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a Content, pgno);.
6530: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
6540: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 .}../*.** Query
6550: 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
6560: 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 2e sContent vector.
6570: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
6580: 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 tion is called w
6590: 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 hen a free-list
65a0: 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65 6d leaf page is rem
65b0: 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a oved from the.**
65c0: 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 72 free-list for r
65d0: 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e 73 euse. It returns
65e0: 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 false if it is
65f0: 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76 65 safe to retrieve
6600: 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 6f the.** page fro
6610: 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 m the pager laye
6620: 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63 r with the 'no-c
6630: 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74 ontent' flag set
6640: 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73 65 . True otherwise
6650: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
6660: 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65 btreeGetHasConte
6670: 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 nt(BtShared *pBt
6680: 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
6690: 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74 2d Bitvec *p = pBt-
66a0: 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 20 >pHasContent;.
66b0: 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 67 return (p && (pg
66c0: 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65 63 no>sqlite3Bitvec
66d0: 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 74 Size(p) || sqlit
66e0: 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c 20 e3BitvecTest(p,
66f0: 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pgno)));.}../*.*
6700: 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f 79 * Clear (destroy
6710: 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 ) the BtShared.p
6720: 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 HasContent bitve
6730: 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 c. This should b
6740: 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 20 e.** invoked at
6750: 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f the conclusion o
6760: 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72 61 f each write-tra
6770: 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 nsaction..*/.sta
6780: 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43 6c tic void btreeCl
6790: 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 earHasContent(Bt
67a0: 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
67b0: 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
67c0: 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 6f troy(pBt->pHasCo
67d0: 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e 70 ntent);. pBt->p
67e0: 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a HasContent = 0;.
67f0: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 }../*.** Release
6800: 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 70 50 61 all of the apPa
6810: 67 65 5b 5d 20 70 61 67 65 73 20 66 6f 72 20 61 ge[] pages for a
6820: 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 cursor..*/.stat
6830: 69 63 20 76 6f 69 64 20 62 74 72 65 65 52 65 6c ic void btreeRel
6840: 65 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 easeAllCursorPag
6850: 65 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 es(BtCursor *pCu
6860: 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 r){. int i;. i
6870: 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d f( pCur->iPage>=
6880: 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 0 ){. for(i=0
6890: 3b 20 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b ; i<pCur->iPage;
68a0: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c i++){. rel
68b0: 65 61 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 easePageNotNull(
68c0: 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 pCur->apPage[i])
68d0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 ;. }. rele
68e0: 61 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 70 asePageNotNull(p
68f0: 43 75 72 2d 3e 70 50 61 67 65 29 3b 0a 20 20 20 Cur->pPage);.
6900: 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d pCur->iPage = -
6910: 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 1;. }.}../*.**
6920: 54 68 65 20 63 75 72 73 6f 72 20 70 61 73 73 65 The cursor passe
6930: 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 61 72 d as the only ar
6940: 67 75 6d 65 6e 74 20 6d 75 73 74 20 70 6f 69 6e gument must poin
6950: 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 t to a valid ent
6960: 72 79 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 ry.** when this
6970: 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
6980: 65 64 20 28 69 2e 65 2e 20 68 61 76 65 20 65 53 ed (i.e. have eS
6990: 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
69a0: 49 44 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e ID). This.** fun
69b0: 63 74 69 6f 6e 20 73 61 76 65 73 20 74 68 65 20 ction saves the
69c0: 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 6b current cursor k
69d0: 65 79 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 ey in variables
69e0: 70 43 75 72 2d 3e 6e 4b 65 79 20 61 6e 64 0a 2a pCur->nKey and.*
69f0: 2a 20 70 43 75 72 2d 3e 70 4b 65 79 2e 20 53 51 * pCur->pKey. SQ
6a00: 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
6a10: 6e 65 64 20 69 66 20 73 75 63 63 65 73 73 66 75 ned if successfu
6a20: 6c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 l or an SQLite e
6a30: 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 6f 74 rror .** code ot
6a40: 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 herwise..**.** I
6a50: 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
6a60: 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 6b 65 open on an intke
6a70: 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 y table, then th
6a80: 65 20 69 6e 74 65 67 65 72 20 6b 65 79 0a 2a 2a e integer key.**
6a90: 20 28 74 68 65 20 72 6f 77 69 64 29 20 69 73 20 (the rowid) is
6aa0: 73 74 6f 72 65 64 20 69 6e 20 70 43 75 72 2d 3e stored in pCur->
6ab0: 6e 4b 65 79 20 61 6e 64 20 70 43 75 72 2d 3e 70 nKey and pCur->p
6ac0: 4b 65 79 20 69 73 20 6c 65 66 74 20 73 65 74 20 Key is left set
6ad0: 74 6f 0a 2a 2a 20 4e 55 4c 4c 2e 20 49 66 20 74 to.** NULL. If t
6ae0: 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 he cursor is ope
6af0: 6e 20 6f 6e 20 61 20 6e 6f 6e 2d 69 6e 74 6b 65 n on a non-intke
6b00: 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 43 y table, then pC
6b10: 75 72 2d 3e 70 4b 65 79 20 69 73 20 0a 2a 2a 20 ur->pKey is .**
6b20: 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 set to point to
6b30: 61 20 6d 61 6c 6c 6f 63 65 64 20 62 75 66 66 65 a malloced buffe
6b40: 72 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 r pCur->nKey byt
6b50: 65 73 20 69 6e 20 73 69 7a 65 20 63 6f 6e 74 61 es in size conta
6b60: 69 6e 69 6e 67 20 0a 2a 2a 20 74 68 65 20 6b 65 ining .** the ke
6b70: 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
6b80: 20 73 61 76 65 43 75 72 73 6f 72 4b 65 79 28 42 saveCursorKey(B
6b90: 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
6ba0: 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
6bb0: 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
6bc0: 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 CURSOR_VALID==pC
6bd0: 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 ur->eState );.
6be0: 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d assert( 0==pCur-
6bf0: 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 >pKey );. asser
6c00: 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
6c10: 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 tex(pCur) );..
6c20: 69 66 28 20 70 43 75 72 2d 3e 63 75 72 49 6e 74 if( pCur->curInt
6c30: 4b 65 79 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e Key ){. /* On
6c40: 6c 79 20 74 68 65 20 72 6f 77 69 64 20 69 73 20 ly the rowid is
6c50: 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 20 74 required for a t
6c60: 61 62 6c 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 able btree */.
6c70: 20 20 70 43 75 72 2d 3e 6e 4b 65 79 20 3d 20 73 pCur->nKey = s
6c80: 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 qlite3BtreeInteg
6c90: 65 72 4b 65 79 28 70 43 75 72 29 3b 0a 20 20 7d erKey(pCur);. }
6ca0: 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 else{. /* For
6cb0: 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 65 2c an index btree,
6cc0: 20 73 61 76 65 20 74 68 65 20 63 6f 6d 70 6c 65 save the comple
6cd0: 74 65 20 6b 65 79 20 63 6f 6e 74 65 6e 74 20 2a te key content *
6ce0: 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b 65 79 /. void *pKey
6cf0: 3b 0a 20 20 20 20 70 43 75 72 2d 3e 6e 4b 65 79 ;. pCur->nKey
6d00: 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
6d10: 61 79 6c 6f 61 64 53 69 7a 65 28 70 43 75 72 29 ayloadSize(pCur)
6d20: 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c ;. pKey = sql
6d30: 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 75 72 ite3Malloc( pCur
6d40: 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 ->nKey );. if
6d50: 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 ( pKey ){.
6d60: 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
6d70: 65 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 ePayload(pCur, 0
6d80: 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 , (int)pCur->nKe
6d90: 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 y, pKey);.
6da0: 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
6db0: 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 K ){. pCu
6dc0: 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a r->pKey = pKey;.
6dd0: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
6de0: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
6df0: 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d e(pKey);. }
6e00: 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
6e10: 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
6e20: 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a MEM_BKPT;. }.
6e30: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 }. assert( !p
6e40: 43 75 72 2d 3e 63 75 72 49 6e 74 4b 65 79 20 7c Cur->curIntKey |
6e50: 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b | !pCur->pKey );
6e60: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
6e70: 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 ./*.** Save the
6e80: 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 current cursor p
6e90: 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 osition in the v
6ea0: 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f ariables BtCurso
6eb0: 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 r.nKey .** and B
6ec0: 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 tCursor.pKey. Th
6ed0: 65 20 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 e cursor's state
6ee0: 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f is set to CURSO
6ef0: 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a R_REQUIRESEEK..*
6f00: 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
6f10: 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 must ensure that
6f20: 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 76 the cursor is v
6f30: 61 6c 69 64 20 28 68 61 73 20 65 53 74 61 74 65 alid (has eState
6f40: 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 0a ==CURSOR_VALID).
6f50: 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c ** prior to call
6f60: 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
6f70: 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e . .*/.static in
6f80: 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 t saveCursorPosi
6f90: 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 tion(BtCursor *p
6fa0: 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
6fb0: 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f . assert( CURSO
6fc0: 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 R_VALID==pCur->e
6fd0: 53 74 61 74 65 20 7c 7c 20 43 55 52 53 4f 52 5f State || CURSOR_
6fe0: 53 4b 49 50 4e 45 58 54 3d 3d 70 43 75 72 2d 3e SKIPNEXT==pCur->
6ff0: 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73 65 eState );. asse
7000: 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b 65 rt( 0==pCur->pKe
7010: 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 y );. assert( c
7020: 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
7030: 70 43 75 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 pCur) );.. if(
7040: 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
7050: 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 7b RSOR_SKIPNEXT ){
7060: 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 . pCur->eStat
7070: 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 e = CURSOR_VALID
7080: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
7090: 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 Cur->skipNext =
70a0: 30 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 0;. }.. rc = s
70b0: 61 76 65 43 75 72 73 6f 72 4b 65 79 28 70 43 75 aveCursorKey(pCu
70c0: 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
70d0: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 62 LITE_OK ){. b
70e0: 74 72 65 65 52 65 6c 65 61 73 65 41 6c 6c 43 75 treeReleaseAllCu
70f0: 72 73 6f 72 50 61 67 65 73 28 70 43 75 72 29 3b rsorPages(pCur);
7100: 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 . pCur->eStat
7110: 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 e = CURSOR_REQUI
7120: 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 70 RESEEK;. }.. p
7130: 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d Cur->curFlags &=
7140: 20 7e 28 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 ~(BTCF_ValidNKe
7150: 79 7c 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c y|BTCF_ValidOvfl
7160: 7c 42 54 43 46 5f 41 74 4c 61 73 74 29 3b 0a 20 |BTCF_AtLast);.
7170: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
7180: 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 * Forward refere
7190: 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e nce */.static in
71a0: 74 20 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e t SQLITE_NOINLIN
71b0: 45 20 73 61 76 65 43 75 72 73 6f 72 73 4f 6e 4c E saveCursorsOnL
71c0: 69 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 50 67 ist(BtCursor*,Pg
71d0: 6e 6f 2c 42 74 43 75 72 73 6f 72 2a 29 3b 0a 0a no,BtCursor*);..
71e0: 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 /*.** Save the p
71f0: 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 ositions of all
7200: 63 75 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 cursors (except
7210: 70 45 78 63 65 70 74 29 20 74 68 61 74 20 61 72 pExcept) that ar
7220: 65 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 e open on.** the
7230: 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
7240: 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 20 22 53 -page iRoot. "S
7250: 61 76 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 aving the cursor
7260: 20 70 6f 73 69 74 69 6f 6e 22 20 6d 65 61 6e 73 position" means
7270: 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6c 6f 63 that.** the loc
7280: 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 74 72 ation in the btr
7290: 65 65 20 69 73 20 72 65 6d 65 6d 62 65 72 65 64 ee is remembered
72a0: 20 69 6e 20 73 75 63 68 20 61 20 77 61 79 20 74 in such a way t
72b0: 68 61 74 20 69 74 20 63 61 6e 20 62 65 0a 2a 2a hat it can be.**
72c0: 20 6d 6f 76 65 64 20 62 61 63 6b 20 74 6f 20 74 moved back to t
72d0: 68 65 20 73 61 6d 65 20 73 70 6f 74 20 61 66 74 he same spot aft
72e0: 65 72 20 74 68 65 20 62 74 72 65 65 20 68 61 73 er the btree has
72f0: 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 been modified.
7300: 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 This.** routine
7310: 20 69 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 is called just
7320: 62 65 66 6f 72 65 20 63 75 72 73 6f 72 20 70 45 before cursor pE
7330: 78 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f xcept is used to
7340: 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a 2a 20 74 modify the.** t
7350: 61 62 6c 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c able, for exampl
7360: 65 20 69 6e 20 42 74 72 65 65 44 65 6c 65 74 65 e in BtreeDelete
7370: 28 29 20 6f 72 20 42 74 72 65 65 49 6e 73 65 72 () or BtreeInser
7380: 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 t()..**.** If th
7390: 65 72 65 20 61 72 65 20 74 77 6f 20 6f 72 20 6d ere are two or m
73a0: 6f 72 65 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 ore cursors on t
73b0: 68 65 20 73 61 6d 65 20 62 74 72 65 65 2c 20 74 he same btree, t
73c0: 68 65 6e 20 61 6c 6c 20 73 75 63 68 20 0a 2a 2a hen all such .**
73d0: 20 63 75 72 73 6f 72 73 20 73 68 6f 75 6c 64 20 cursors should
73e0: 68 61 76 65 20 74 68 65 69 72 20 42 54 43 46 5f have their BTCF_
73f0: 4d 75 6c 74 69 70 6c 65 20 66 6c 61 67 20 73 65 Multiple flag se
7400: 74 2e 20 20 54 68 65 20 62 74 72 65 65 43 75 72 t. The btreeCur
7410: 73 6f 72 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 sor().** routine
7420: 20 65 6e 66 6f 72 63 65 73 20 74 68 61 74 20 72 enforces that r
7430: 75 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ule. This routi
7440: 6e 65 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f ne only needs to
7450: 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 0a 2a 2a be called in.**
7460: 20 74 68 65 20 75 6e 63 6f 6d 6d 6f 6e 20 63 61 the uncommon ca
7470: 73 65 20 77 68 65 6e 20 70 45 78 70 65 63 74 20 se when pExpect
7480: 68 61 73 20 74 68 65 20 42 54 43 46 5f 4d 75 6c has the BTCF_Mul
7490: 74 69 70 6c 65 20 66 6c 61 67 20 73 65 74 2e 0a tiple flag set..
74a0: 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 65 63 74 **.** If pExpect
74b0: 21 3d 4e 55 4c 4c 20 61 6e 64 20 69 66 20 6e 6f !=NULL and if no
74c0: 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 61 other cursors a
74d0: 72 65 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 re found on the
74e0: 73 61 6d 65 20 72 6f 6f 74 2d 70 61 67 65 2c 0a same root-page,.
74f0: 2a 2a 20 74 68 65 6e 20 74 68 65 20 42 54 43 46 ** then the BTCF
7500: 5f 4d 75 6c 74 69 70 6c 65 20 66 6c 61 67 20 6f _Multiple flag o
7510: 6e 20 70 45 78 70 65 63 74 20 69 73 20 63 6c 65 n pExpect is cle
7520: 61 72 65 64 2c 20 74 6f 20 61 76 6f 69 64 20 61 ared, to avoid a
7530: 6e 6f 74 68 65 72 0a 2a 2a 20 70 6f 69 6e 74 6c nother.** pointl
7540: 65 73 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 ess call to this
7550: 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
7560: 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6e Implementation n
7570: 6f 74 65 3a 20 20 54 68 69 73 20 72 6f 75 74 69 ote: This routi
7580: 6e 65 20 6d 65 72 65 6c 79 20 63 68 65 63 6b 73 ne merely checks
7590: 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20 63 to see if any c
75a0: 75 72 73 6f 72 73 0a 2a 2a 20 6e 65 65 64 20 74 ursors.** need t
75b0: 6f 20 62 65 20 73 61 76 65 64 2e 20 20 49 74 20 o be saved. It
75c0: 63 61 6c 6c 73 20 6f 75 74 20 74 6f 20 73 61 76 calls out to sav
75d0: 65 43 75 72 73 6f 72 73 4f 6e 4c 69 73 74 28 29 eCursorsOnList()
75e0: 20 69 6e 20 74 68 65 20 28 75 6e 75 73 75 61 6c in the (unusual
75f0: 29 0a 2a 2a 20 65 76 65 6e 74 20 74 68 61 74 20 ).** event that
7600: 63 75 72 73 6f 72 73 20 61 72 65 20 69 6e 20 6e cursors are in n
7610: 65 65 64 20 74 6f 20 62 65 69 6e 67 20 73 61 76 eed to being sav
7620: 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
7630: 74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 t saveAllCursors
7640: 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
7650: 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 Pgno iRoot, BtCu
7660: 72 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a rsor *pExcept){.
7670: 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
7680: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
7690: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
76a0: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
76b0: 65 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 ert( pExcept==0
76c0: 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d || pExcept->pBt=
76d0: 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d =pBt );. for(p=
76e0: 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
76f0: 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
7700: 20 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 if( p!=pExcept
7710: 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c && (0==iRoot ||
7720: 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 p->pgnoRoot==iR
7730: 6f 6f 74 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 oot) ) break;.
7740: 7d 0a 20 20 69 66 28 20 70 20 29 20 72 65 74 75 }. if( p ) retu
7750: 72 6e 20 73 61 76 65 43 75 72 73 6f 72 73 4f 6e rn saveCursorsOn
7760: 4c 69 73 74 28 70 2c 20 69 52 6f 6f 74 2c 20 70 List(p, iRoot, p
7770: 45 78 63 65 70 74 29 3b 0a 20 20 69 66 28 20 70 Except);. if( p
7780: 45 78 63 65 70 74 20 29 20 70 45 78 63 65 70 74 Except ) pExcept
7790: 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 42 ->curFlags &= ~B
77a0: 54 43 46 5f 4d 75 6c 74 69 70 6c 65 3b 0a 20 20 TCF_Multiple;.
77b0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
77c0: 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 68 65 6c ;.}../* This hel
77d0: 70 65 72 20 72 6f 75 74 69 6e 65 20 74 6f 20 73 per routine to s
77e0: 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 20 64 6f aveAllCursors do
77f0: 65 73 20 74 68 65 20 61 63 74 75 61 6c 20 77 6f es the actual wo
7800: 72 6b 20 6f 66 20 73 61 76 69 6e 67 0a 2a 2a 20 rk of saving.**
7810: 74 68 65 20 63 75 72 73 6f 72 73 20 69 66 20 61 the cursors if a
7820: 6e 64 20 77 68 65 6e 20 61 20 63 75 72 73 6f 72 nd when a cursor
7830: 20 69 73 20 66 6f 75 6e 64 20 74 68 61 74 20 61 is found that a
7840: 63 74 75 61 6c 6c 79 20 72 65 71 75 69 72 65 73 ctually requires
7850: 20 73 61 76 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 saving..** The
7860: 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 69 73 20 74 common case is t
7870: 68 61 74 20 6e 6f 20 63 75 72 73 6f 72 73 20 6e hat no cursors n
7880: 65 65 64 20 74 6f 20 62 65 20 73 61 76 65 64 2c eed to be saved,
7890: 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 so this routine
78a0: 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e 20 6f 75 is.** broken ou
78b0: 74 20 66 72 6f 6d 20 69 74 73 20 63 61 6c 6c 65 t from its calle
78c0: 72 20 74 6f 20 61 76 6f 69 64 20 75 6e 6e 65 63 r to avoid unnec
78d0: 65 73 73 61 72 79 20 73 74 61 63 6b 20 70 6f 69 essary stack poi
78e0: 6e 74 65 72 20 6d 6f 76 65 6d 65 6e 74 2e 0a 2a nter movement..*
78f0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 53 51 4c /.static int SQL
7900: 49 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 73 61 76 ITE_NOINLINE sav
7910: 65 43 75 72 73 6f 72 73 4f 6e 4c 69 73 74 28 0a eCursorsOnList(.
7920: 20 20 42 74 43 75 72 73 6f 72 20 2a 70 2c 20 20 BtCursor *p,
7930: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 /* The fi
7940: 72 73 74 20 63 75 72 73 6f 72 20 74 68 61 74 20 rst cursor that
7950: 6e 65 65 64 73 20 73 61 76 69 6e 67 20 2a 2f 0a needs saving */.
7960: 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 20 20 Pgno iRoot,
7970: 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 73 /* Only s
7980: 61 76 65 20 63 75 72 73 6f 72 20 77 69 74 68 20 ave cursor with
7990: 74 68 69 73 20 69 52 6f 6f 74 2e 20 53 61 76 65 this iRoot. Save
79a0: 20 61 6c 6c 20 69 66 20 7a 65 72 6f 20 2a 2f 0a all if zero */.
79b0: 20 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78 63 BtCursor *pExc
79c0: 65 70 74 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 ept /* Do not
79d0: 20 73 61 76 65 20 74 68 69 73 20 63 75 72 73 6f save this curso
79e0: 72 20 2a 2f 0a 29 7b 0a 20 20 64 6f 7b 0a 20 20 r */.){. do{.
79f0: 20 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 if( p!=pExcept
7a00: 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c && (0==iRoot ||
7a10: 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 p->pgnoRoot==iR
7a20: 6f 6f 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 oot) ){. if
7a30: 28 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 ( p->eState==CUR
7a40: 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 2d 3e SOR_VALID || p->
7a50: 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 53 eState==CURSOR_S
7a60: 4b 49 50 4e 45 58 54 20 29 7b 0a 20 20 20 20 20 KIPNEXT ){.
7a70: 20 20 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 int rc = save
7a80: 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
7a90: 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 53 );. if( S
7aa0: 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a QLITE_OK!=rc ){.
7ab0: 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
7ac0: 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
7ad0: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
7ae0: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
7af0: 3e 69 50 61 67 65 3e 3d 30 20 29 3b 0a 20 20 20 >iPage>=0 );.
7b00: 20 20 20 20 20 62 74 72 65 65 52 65 6c 65 61 73 btreeReleas
7b10: 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 65 73 28 eAllCursorPages(
7b20: 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
7b30: 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 }. p = p->pNe
7b40: 78 74 3b 0a 20 20 7d 77 68 69 6c 65 28 20 70 20 xt;. }while( p
7b50: 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
7b60: 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
7b70: 43 6c 65 61 72 20 74 68 65 20 63 75 72 72 65 6e Clear the curren
7b80: 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f t cursor positio
7b90: 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 n..*/.void sqlit
7ba0: 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
7bb0: 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
7bc0: 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 r){. assert( cu
7bd0: 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
7be0: 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 Cur) );. sqlite
7bf0: 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 3_free(pCur->pKe
7c00: 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 y);. pCur->pKey
7c10: 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 = 0;. pCur->eS
7c20: 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
7c30: 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 VALID;.}../*.**
7c40: 49 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 In this version
7c50: 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 of BtreeMoveto,
7c60: 70 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65 64 pKey is a packed
7c70: 20 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a index record.**
7c80: 20 73 75 63 68 20 61 73 20 69 73 20 67 65 6e 65 such as is gene
7c90: 72 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f rated by the OP_
7ca0: 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
7cb0: 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a e. Unpack the.*
7cc0: 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65 * record and the
7cd0: 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 n call BtreeMove
7ce0: 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 toUnpacked() to
7cf0: 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a do the work..*/.
7d00: 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
7d10: 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 Moveto(. BtCurs
7d20: 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a or *pCur, /*
7d30: 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 Cursor open on
7d40: 74 68 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 the btree to be
7d50: 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f searched */. co
7d60: 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
7d70: 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 /* Packed key
7d80: 69 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 if the btree is
7d90: 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 an index */. i6
7da0: 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 4 nKey,
7db0: 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 /* Integer key
7dc0: 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 69 for tables. Si
7dd0: 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 ze of pKey for i
7de0: 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 ndices */. int
7df0: 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 20 bias,
7e00: 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 20 74 /* Bias search t
7e10: 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a o the high end *
7e20: 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20 /. int *pRes
7e30: 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
7e40: 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 search results
7e50: 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
7e60: 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
7e70: 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 /* Statu
7e80: 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 s code */. Unpa
7e90: 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 ckedRecord *pIdx
7ea0: 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b Key; /* Unpack
7eb0: 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a ed index key */.
7ec0: 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 . if( pKey ){.
7ed0: 20 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d assert( nKey=
7ee0: 3d 28 69 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 =(i64)(int)nKey
7ef0: 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d );. pIdxKey =
7f00: 20 73 71 6c 69 74 65 33 56 64 62 65 41 6c 6c 6f sqlite3VdbeAllo
7f10: 63 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 cUnpackedRecord(
7f20: 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 29 3b pCur->pKeyInfo);
7f30: 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 79 . if( pIdxKey
7f40: 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
7f50: 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a ITE_NOMEM_BKPT;.
7f60: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 sqlite3VdbeR
7f70: 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 75 72 ecordUnpack(pCur
7f80: 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69 6e 74 ->pKeyInfo, (int
7f90: 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 20 70 49 64 )nKey, pKey, pId
7fa0: 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 70 xKey);. if( p
7fb0: 49 64 78 4b 65 79 2d 3e 6e 46 69 65 6c 64 3d 3d IdxKey->nField==
7fc0: 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
7fd0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a SQLITE_CORRUPT;.
7fe0: 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
7ff0: 6f 5f 64 6f 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 o_done;. }.
8000: 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
8010: 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
8020: 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
8030: 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
8040: 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
8050: 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
8060: 6d 6f 76 65 74 6f 5f 64 6f 6e 65 3a 0a 20 20 69 moveto_done:. i
8070: 66 28 20 70 49 64 78 4b 65 79 20 29 7b 0a 20 20 f( pIdxKey ){.
8080: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
8090: 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e pCur->pKeyInfo->
80a0: 64 62 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 db, pIdxKey);.
80b0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
80c0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 ../*.** Restore
80d0: 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
80e0: 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 e position it wa
80f0: 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 s in (or as clos
8100: 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 e to as possible
8110: 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 ).** when saveCu
8120: 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 rsorPosition() w
8130: 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 as called. Note
8140: 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 that this call d
8150: 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 eletes the .** s
8160: 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e aved position in
8170: 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 fo stored by sav
8180: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
8190: 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 ), so there can
81a0: 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e be.** at most on
81b0: 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74 e effective rest
81c0: 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
81d0: 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 n() call after e
81e0: 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 ach .** saveCurs
81f0: 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f orPosition()..*/
8200: 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
8210: 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f eRestoreCursorPo
8220: 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 sition(BtCursor
8230: 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 *pCur){. int rc
8240: 3b 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 ;. int skipNext
8250: 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
8260: 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 64 28 70 orOwnsBtShared(p
8270: 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
8280: 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d ( pCur->eState>=
8290: 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
82a0: 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
82b0: 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
82c0: 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 _FAULT ){. re
82d0: 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e turn pCur->skipN
82e0: 65 78 74 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d ext;. }. pCur-
82f0: 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
8300: 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d _INVALID;. rc =
8310: 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 btreeMoveto(pCu
8320: 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 r, pCur->pKey, p
8330: 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 73 Cur->nKey, 0, &s
8340: 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 kipNext);. if(
8350: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
8360: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
8370: 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
8380: 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 pCur->pKey =
8390: 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 0;. assert( p
83a0: 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
83b0: 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 SOR_VALID || pCu
83c0: 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
83d0: 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 R_INVALID );.
83e0: 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 pCur->skipNext
83f0: 7c 3d 20 73 6b 69 70 4e 65 78 74 3b 0a 20 20 20 |= skipNext;.
8400: 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e if( pCur->skipN
8410: 65 78 74 20 26 26 20 70 43 75 72 2d 3e 65 53 74 ext && pCur->eSt
8420: 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
8430: 44 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d D ){. pCur-
8440: 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
8450: 5f 53 4b 49 50 4e 45 58 54 3b 0a 20 20 20 20 7d _SKIPNEXT;. }
8460: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
8470: 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 ;.}..#define res
8480: 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
8490: 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 on(p) \. (p->eS
84a0: 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 tate>=CURSOR_REQ
84b0: 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 UIRESEEK ? \.
84c0: 20 20 20 20 20 20 62 74 72 65 65 52 65 73 74 6f btreeResto
84d0: 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
84e0: 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 (p) : \.
84f0: 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a SQLITE_OK)../*.
8500: 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 ** Determine whe
8510: 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 ther or not a cu
8520: 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 rsor has moved f
8530: 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e rom the position
8540: 20 77 68 65 72 65 0a 2a 2a 20 69 74 20 77 61 73 where.** it was
8550: 20 6c 61 73 74 20 70 6c 61 63 65 64 2c 20 6f 72 last placed, or
8560: 20 68 61 73 20 62 65 65 6e 20 69 6e 76 61 6c 69 has been invali
8570: 64 61 74 65 64 20 66 6f 72 20 61 6e 79 20 6f 74 dated for any ot
8580: 68 65 72 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 43 her reason..** C
8590: 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 ursors can move
85a0: 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 when the row the
85b0: 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 y are pointing a
85c0: 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 t is deleted out
85d0: 0a 2a 2a 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 .** from under t
85e0: 68 65 6d 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 hem, for example
85f0: 2e 20 20 43 75 72 73 6f 72 20 6d 69 67 68 74 20 . Cursor might
8600: 61 6c 73 6f 20 6d 6f 76 65 20 69 66 20 61 20 62 also move if a b
8610: 74 72 65 65 0a 2a 2a 20 69 73 20 72 65 62 61 6c tree.** is rebal
8620: 61 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c anced..**.** Cal
8630: 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
8640: 65 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63 75 e with a NULL cu
8650: 72 73 6f 72 20 70 6f 69 6e 74 65 72 20 72 65 74 rsor pointer ret
8660: 75 72 6e 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a urns false..**.*
8670: 2a 20 55 73 65 20 74 68 65 20 73 65 70 61 72 61 * Use the separa
8680: 74 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 te sqlite3BtreeC
8690: 75 72 73 6f 72 52 65 73 74 6f 72 65 28 29 20 72 ursorRestore() r
86a0: 6f 75 74 69 6e 65 20 74 6f 20 72 65 73 74 6f 72 outine to restor
86b0: 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 62 61 e a cursor.** ba
86c0: 63 6b 20 74 6f 20 77 68 65 72 65 20 69 74 20 6f ck to where it o
86d0: 75 67 68 74 20 74 6f 20 62 65 20 69 66 20 74 68 ught to be if th
86e0: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
86f0: 6e 73 20 74 72 75 65 2e 0a 2a 2f 0a 69 6e 74 20 ns true..*/.int
8700: 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
8710: 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 orHasMoved(BtCur
8720: 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 sor *pCur){. re
8730: 74 75 72 6e 20 70 43 75 72 2d 3e 65 53 74 61 74 turn pCur->eStat
8740: 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b e!=CURSOR_VALID;
8750: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
8760: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
8770: 66 61 6b 65 20 42 74 43 75 72 73 6f 72 20 6f 62 fake BtCursor ob
8780: 6a 65 63 74 20 74 68 61 74 20 77 69 6c 6c 20 61 ject that will a
8790: 6c 77 61 79 73 20 61 6e 73 77 65 72 0a 2a 2a 20 lways answer.**
87a0: 66 61 6c 73 65 20 74 6f 20 74 68 65 20 73 71 6c false to the sql
87b0: 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 ite3BtreeCursorH
87c0: 61 73 4d 6f 76 65 64 28 29 20 72 6f 75 74 69 6e asMoved() routin
87d0: 65 20 61 62 6f 76 65 2e 20 20 54 68 65 20 66 61 e above. The fa
87e0: 6b 65 0a 2a 2a 20 63 75 72 73 6f 72 20 72 65 74 ke.** cursor ret
87f0: 75 72 6e 65 64 20 6d 75 73 74 20 6e 6f 74 20 62 urned must not b
8800: 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 79 20 e used with any
8810: 6f 74 68 65 72 20 42 74 72 65 65 20 69 6e 74 65 other Btree inte
8820: 72 66 61 63 65 2e 0a 2a 2f 0a 42 74 43 75 72 73 rface..*/.BtCurs
8830: 6f 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 or *sqlite3Btree
8840: 46 61 6b 65 56 61 6c 69 64 43 75 72 73 6f 72 28 FakeValidCursor(
8850: 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
8860: 75 38 20 66 61 6b 65 43 75 72 73 6f 72 20 3d 20 u8 fakeCursor =
8870: 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 CURSOR_VALID;.
8880: 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 6f 66 assert( offsetof
8890: 28 42 74 43 75 72 73 6f 72 2c 20 65 53 74 61 74 (BtCursor, eStat
88a0: 65 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 e)==0 );. retur
88b0: 6e 20 28 42 74 43 75 72 73 6f 72 2a 29 26 66 61 n (BtCursor*)&fa
88c0: 6b 65 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f 2a 0a keCursor;.}../*.
88d0: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
88e0: 72 65 73 74 6f 72 65 73 20 61 20 63 75 72 73 6f restores a curso
88f0: 72 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 r back to its or
8900: 69 67 69 6e 61 6c 20 70 6f 73 69 74 69 6f 6e 20 iginal position
8910: 61 66 74 65 72 20 69 74 0a 2a 2a 20 68 61 73 20 after it.** has
8920: 62 65 65 6e 20 6d 6f 76 65 64 20 62 79 20 73 6f been moved by so
8930: 6d 65 20 6f 75 74 73 69 64 65 20 61 63 74 69 76 me outside activ
8940: 69 74 79 20 28 73 75 63 68 20 61 73 20 61 20 62 ity (such as a b
8950: 74 72 65 65 20 72 65 62 61 6c 61 6e 63 65 20 6f tree rebalance o
8960: 72 0a 2a 2a 20 61 20 72 6f 77 20 68 61 76 69 6e r.** a row havin
8970: 67 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f g been deleted o
8980: 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 ut from under th
8990: 65 20 63 75 72 73 6f 72 29 2e 20 20 0a 2a 2a 0a e cursor). .**.
89a0: 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 74 ** On success, t
89b0: 68 65 20 2a 70 44 69 66 66 65 72 65 6e 74 52 6f he *pDifferentRo
89c0: 77 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 66 w parameter is f
89d0: 61 6c 73 65 20 69 66 20 74 68 65 20 63 75 72 73 alse if the curs
89e0: 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20 70 6f or is left.** po
89f0: 69 6e 74 69 6e 67 20 61 74 20 65 78 61 63 74 6c inting at exactl
8a00: 79 20 74 68 65 20 73 61 6d 65 20 72 6f 77 2e 20 y the same row.
8a10: 20 2a 70 44 69 66 66 65 72 6e 74 52 6f 77 20 69 *pDifferntRow i
8a20: 73 20 74 68 65 20 72 6f 77 20 74 68 65 20 63 75 s the row the cu
8a30: 72 73 6f 72 0a 2a 2a 20 77 61 73 20 70 6f 69 6e rsor.** was poin
8a40: 74 69 6e 67 20 74 6f 20 68 61 73 20 62 65 65 6e ting to has been
8a50: 20 64 65 6c 65 74 65 64 2c 20 66 6f 72 63 69 6e deleted, forcin
8a60: 67 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 g the cursor to
8a70: 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 0a 2a 2a point to some.**
8a80: 20 6e 65 61 72 62 79 20 72 6f 77 2e 0a 2a 2a 0a nearby row..**.
8a90: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
8aa0: 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 should only be c
8ab0: 61 6c 6c 65 64 20 66 6f 72 20 61 20 63 75 72 73 alled for a curs
8ac0: 6f 72 20 74 68 61 74 20 6a 75 73 74 20 72 65 74 or that just ret
8ad0: 75 72 6e 65 64 0a 2a 2a 20 54 52 55 45 20 66 72 urned.** TRUE fr
8ae0: 6f 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 om sqlite3BtreeC
8af0: 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 29 2e ursorHasMoved().
8b00: 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
8b10: 74 72 65 65 43 75 72 73 6f 72 52 65 73 74 6f 72 treeCursorRestor
8b20: 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
8b30: 2c 20 69 6e 74 20 2a 70 44 69 66 66 65 72 65 6e , int *pDifferen
8b40: 74 52 6f 77 29 7b 0a 20 20 69 6e 74 20 72 63 3b tRow){. int rc;
8b50: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 .. assert( pCur
8b60: 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
8b70: 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCur->eState!=C
8b80: 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
8b90: 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
8ba0: 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 sorPosition(pCur
8bb0: 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
8bc0: 20 20 20 2a 70 44 69 66 66 65 72 65 6e 74 52 6f *pDifferentRo
8bd0: 77 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 w = 1;. retur
8be0: 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 n rc;. }. if(
8bf0: 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCur->eState!=CU
8c00: 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 RSOR_VALID ){.
8c10: 20 20 2a 70 44 69 66 66 65 72 65 6e 74 52 6f 77 *pDifferentRow
8c20: 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
8c30: 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
8c40: 3e 73 6b 69 70 4e 65 78 74 3d 3d 30 20 29 3b 0a >skipNext==0 );.
8c50: 20 20 20 20 2a 70 44 69 66 66 65 72 65 6e 74 52 *pDifferentR
8c60: 6f 77 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ow = 0;. }. re
8c70: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
8c80: 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
8c90: 5f 45 4e 41 42 4c 45 5f 43 55 52 53 4f 52 5f 48 _ENABLE_CURSOR_H
8ca0: 49 4e 54 53 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 INTS./*.** Provi
8cb0: 64 65 20 68 69 6e 74 73 20 74 6f 20 74 68 65 20 de hints to the
8cc0: 63 75 72 73 6f 72 2e 20 20 54 68 65 20 70 61 72 cursor. The par
8cd0: 74 69 63 75 6c 61 72 20 68 69 6e 74 20 67 69 76 ticular hint giv
8ce0: 65 6e 20 28 61 6e 64 20 74 68 65 20 74 79 70 65 en (and the type
8cf0: 0a 2a 2a 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f .** and number o
8d00: 66 20 74 68 65 20 76 61 72 61 72 67 73 20 70 61 f the varargs pa
8d10: 72 61 6d 65 74 65 72 73 29 20 69 73 20 64 65 74 rameters) is det
8d20: 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 65 ermined by the e
8d30: 48 69 6e 74 54 79 70 65 0a 2a 2a 20 70 61 72 61 HintType.** para
8d40: 6d 65 74 65 72 2e 20 20 53 65 65 20 74 68 65 20 meter. See the
8d50: 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 74 definitions of t
8d60: 68 65 20 42 54 52 45 45 5f 48 49 4e 54 5f 2a 20 he BTREE_HINT_*
8d70: 6d 61 63 72 6f 73 20 66 6f 72 20 64 65 74 61 69 macros for detai
8d80: 6c 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 ls..*/.void sqli
8d90: 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 69 te3BtreeCursorHi
8da0: 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 nt(BtCursor *pCu
8db0: 72 2c 20 69 6e 74 20 65 48 69 6e 74 54 79 70 65 r, int eHintType
8dc0: 2c 20 2e 2e 2e 29 7b 0a 20 20 2f 2a 20 55 73 65 , ...){. /* Use
8dd0: 64 20 6f 6e 6c 79 20 62 79 20 73 79 73 74 65 6d d only by system
8de0: 20 74 68 61 74 20 73 75 62 73 74 69 74 75 74 65 that substitute
8df0: 20 74 68 65 69 72 20 6f 77 6e 20 73 74 6f 72 61 their own stora
8e00: 67 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 7d 0a 23 ge engine */.}.#
8e10: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f endif../*.** Pro
8e20: 76 69 64 65 20 66 6c 61 67 20 68 69 6e 74 73 20 vide flag hints
8e30: 74 6f 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 2a to the cursor..*
8e40: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 /.void sqlite3Bt
8e50: 72 65 65 43 75 72 73 6f 72 48 69 6e 74 46 6c 61 reeCursorHintFla
8e60: 67 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 gs(BtCursor *pCu
8e70: 72 2c 20 75 6e 73 69 67 6e 65 64 20 78 29 7b 0a r, unsigned x){.
8e80: 20 20 61 73 73 65 72 74 28 20 78 3d 3d 42 54 52 assert( x==BTR
8e90: 45 45 5f 53 45 45 4b 5f 45 51 20 7c 7c 20 78 3d EE_SEEK_EQ || x=
8ea0: 3d 42 54 52 45 45 5f 42 55 4c 4b 4c 4f 41 44 20 =BTREE_BULKLOAD
8eb0: 7c 7c 20 78 3d 3d 30 20 29 3b 0a 20 20 70 43 75 || x==0 );. pCu
8ec0: 72 2d 3e 68 69 6e 74 73 20 3d 20 78 3b 0a 7d 0a r->hints = x;.}.
8ed0: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
8ee0: 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
8ef0: 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70 ./*.** Given a p
8f00: 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 age number of a
8f10: 72 65 67 75 6c 61 72 20 64 61 74 61 62 61 73 65 regular database
8f20: 20 70 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 page, return th
8f30: 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 e page.** number
8f40: 20 66 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72 for the pointer
8f50: 2d 6d 61 70 20 70 61 67 65 20 74 68 61 74 20 63 -map page that c
8f60: 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 ontains the entr
8f70: 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 69 6e 70 y for the.** inp
8f80: 75 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a ut page number..
8f90: 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 28 **.** Return 0 (
8fa0: 6e 6f 74 20 61 20 76 61 6c 69 64 20 70 61 67 65 not a valid page
8fb0: 29 20 66 6f 72 20 70 67 6e 6f 3d 3d 31 20 73 69 ) for pgno==1 si
8fc0: 6e 63 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 nce there is.**
8fd0: 6e 6f 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 61 no pointer map a
8fe0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 ssociated with p
8ff0: 61 67 65 20 31 2e 20 20 54 68 65 20 69 6e 74 65 age 1. The inte
9000: 67 72 69 74 79 5f 63 68 65 63 6b 20 6c 6f 67 69 grity_check logi
9010: 63 0a 2a 2a 20 72 65 71 75 69 72 65 73 20 74 68 c.** requires th
9020: 61 74 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 at ptrmapPageno(
9030: 2a 2c 31 29 21 3d 31 2e 0a 2a 2f 0a 73 74 61 74 *,1)!=1..*/.stat
9040: 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 ic Pgno ptrmapPa
9050: 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 geno(BtShared *p
9060: 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
9070: 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d int nPagesPerM
9080: 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 apPage;. Pgno i
9090: 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 PtrMap, ret;. a
90a0: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
90b0: 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
90c0: 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
90d0: 67 6e 6f 3c 32 20 29 20 72 65 74 75 72 6e 20 30 gno<2 ) return 0
90e0: 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70 ;. nPagesPerMap
90f0: 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61 Page = (pBt->usa
9100: 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 bleSize/5)+1;.
9110: 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d iPtrMap = (pgno-
9120: 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50 2)/nPagesPerMapP
9130: 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50 age;. ret = (iP
9140: 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d trMap*nPagesPerM
9150: 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20 apPage) + 2; .
9160: 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 if( ret==PENDING
9170: 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
9180: 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20 ){. ret++;.
9190: 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a }. return ret;.
91a0: 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 }../*.** Write a
91b0: 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 n entry into the
91c0: 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a pointer map..**
91d0: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
91e0: 20 75 70 64 61 74 65 73 20 74 68 65 20 70 6f 69 updates the poi
91f0: 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 nter map entry f
9200: 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 or page number '
9210: 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20 key'.** so that
9220: 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20 it maps to type
9230: 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 72 65 'eType' and pare
9240: 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 nt page number '
9250: 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pgno'..**.** If
9260: 2a 70 52 43 20 69 73 20 69 6e 69 74 69 61 6c 6c *pRC is initiall
9270: 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f 6e 2d y non-zero (non-
9280: 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 65 6e 20 SQLITE_OK) then
9290: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a this routine is.
92a0: 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 ** a no-op. If
92b0: 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
92c0: 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
92d0: 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 77 error code is w
92e0: 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a ritten.** into *
92f0: 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 pRC..*/.static v
9300: 6f 69 64 20 70 74 72 6d 61 70 50 75 74 28 42 74 oid ptrmapPut(Bt
9310: 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
9320: 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 65 2c o key, u8 eType,
9330: 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c 20 69 6e Pgno parent, in
9340: 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 50 61 67 t *pRC){. DbPag
9350: 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 e *pDbPage; /*
9360: 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
9370: 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50 page */. u8 *pP
9380: 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 trmap; /* T
9390: 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 he pointer map d
93a0: 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 ata */. Pgno iP
93b0: 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68 trmap; /* Th
93c0: 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
93d0: 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 ge number */. i
93e0: 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 nt offset;
93f0: 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f /* Offset in po
9400: 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a inter map page *
9410: 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
9420: 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
9430: 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66 75 6e code from subfun
9440: 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69 66 28 ctions */.. if(
9450: 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a *pRC ) return;.
9460: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
9470: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
9480: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f t->mutex) );. /
9490: 2a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 * The master-jou
94a0: 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 rnal page number
94b0: 20 69 73 20 6e 65 76 65 72 20 61 64 64 65 64 20 is never added
94c0: 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 to a pointer-map
94d0: 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 page */. asser
94e0: 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 t( 0==PTRMAP_ISP
94f0: 41 47 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 AGE(pBt, PENDING
9500: 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 29 _BYTE_PAGE(pBt))
9510: 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c );..#ifndef SQL
9520: 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 55 52 52 ITE_OMIT_CONCURR
9530: 45 4e 54 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 ENT. if( pBt->p
9540: 4d 61 70 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 Map ){. *pRC
9550: 3d 20 62 74 72 65 65 50 74 72 6d 61 70 53 74 6f = btreePtrmapSto
9560: 72 65 28 70 42 74 2c 20 6b 65 79 2c 20 65 54 79 re(pBt, key, eTy
9570: 70 65 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 pe, parent);.
9580: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e return;. }.#en
9590: 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 dif.. assert( p
95a0: 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
95b0: 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 ;. if( key==0 )
95c0: 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c {. *pRC = SQL
95d0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
95e0: 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
95f0: 7d 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 }. iPtrmap = PT
9600: 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
9610: 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 key);. rc = sq
9620: 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 lite3PagerGet(pB
9630: 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d t->pPager, iPtrm
9640: 61 70 2c 20 26 70 44 62 50 61 67 65 2c 20 30 29 ap, &pDbPage, 0)
9650: 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
9660: 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 TE_OK ){. *pR
9670: 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 75 C = rc;. retu
9680: 72 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 rn;. }. offset
9690: 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 = PTRMAP_PTROFF
96a0: 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 SET(iPtrmap, key
96b0: 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c );. if( offset<
96c0: 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 0 ){. *pRC =
96d0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
96e0: 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 70 74 KPT;. goto pt
96f0: 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20 7d 0a 20 rmap_exit;. }.
9700: 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 assert( offset
9710: 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61 <= (int)pBt->usa
9720: 62 6c 65 53 69 7a 65 2d 35 20 29 3b 0a 20 20 70 bleSize-5 );. p
9730: 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 Ptrmap = (u8 *)s
9740: 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
9750: 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 ta(pDbPage);..
9760: 69 66 28 20 65 54 79 70 65 21 3d 70 50 74 72 6d if( eType!=pPtrm
9770: 61 70 5b 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 ap[offset] || ge
9780: 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b t4byte(&pPtrmap[
9790: 6f 66 66 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 offset+1])!=pare
97a0: 6e 74 20 29 7b 0a 20 20 20 20 54 52 41 43 45 28 nt ){. TRACE(
97b0: 28 22 50 54 52 4d 41 50 5f 55 50 44 41 54 45 3a ("PTRMAP_UPDATE:
97c0: 20 25 64 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c %d->(%d,%d)\n",
97d0: 20 6b 65 79 2c 20 65 54 79 70 65 2c 20 70 61 72 key, eType, par
97e0: 65 6e 74 29 29 3b 0a 20 20 20 20 2a 70 52 43 3d ent));. *pRC=
97f0: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
9800: 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 erWrite(pDbPage)
9810: 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
9820: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
9830: 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d pPtrmap[offset]
9840: 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 = eType;.
9850: 70 75 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 put4byte(&pPtrma
9860: 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 p[offset+1], par
9870: 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ent);. }. }.
9880: 0a 70 74 72 6d 61 70 5f 65 78 69 74 3a 0a 20 20 .ptrmap_exit:.
9890: 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
98a0: 66 28 70 44 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f f(pDbPage);.}../
98b0: 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 *.** Read an ent
98c0: 72 79 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e ry from the poin
98d0: 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 ter map..**.** T
98e0: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72 his routine retr
98f0: 69 65 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65 ieves the pointe
9900: 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 r map entry for
9910: 70 61 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74 page 'key', writ
9920: 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 ing.** the type
9930: 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 and parent page
9940: 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70 number to *pETyp
9950: 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 e and *pPgno res
9960: 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e pectively..** An
9970: 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
9980: 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 eturned if somet
9990: 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c hing goes wrong,
99a0: 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 otherwise SQLIT
99b0: 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 E_OK..*/.static
99c0: 69 6e 74 20 70 74 72 6d 61 70 47 65 74 28 42 74 int ptrmapGet(Bt
99d0: 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
99e0: 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 54 79 70 o key, u8 *pETyp
99f0: 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b e, Pgno *pPgno){
9a00: 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
9a10: 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 ge; /* The poi
9a20: 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f nter map page */
9a30: 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 . int iPtrmap;
9a40: 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
9a50: 20 6d 61 70 20 70 61 67 65 20 69 6e 64 65 78 20 map page index
9a60: 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 */. u8 *pPtrmap
9a70: 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 ; /* Point
9a80: 65 72 20 6d 61 70 20 70 61 67 65 20 64 61 74 61 er map page data
9a90: 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 */. int offset
9aa0: 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 ; /* Offs
9ab0: 65 74 20 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 et of entry in p
9ac0: 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 ointer map */.
9ad0: 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
9ae0: 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
9af0: 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
9b00: 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 ) );.. iPtrmap
9b10: 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 = PTRMAP_PAGENO(
9b20: 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 pBt, key);. rc
9b30: 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
9b40: 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
9b50: 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 Ptrmap, &pDbPage
9b60: 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d , 0);. if( rc!=
9b70: 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
9b80: 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 rc;. }. pPtrma
9b90: 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 p = (u8 *)sqlite
9ba0: 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
9bb0: 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 bPage);.. offse
9bc0: 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 t = PTRMAP_PTROF
9bd0: 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 FSET(iPtrmap, ke
9be0: 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 65 74 y);. if( offset
9bf0: 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 <0 ){. sqlite
9c00: 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 3PagerUnref(pDbP
9c10: 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e age);. return
9c20: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
9c30: 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 BKPT;. }. asse
9c40: 72 74 28 20 6f 66 66 73 65 74 20 3c 3d 20 28 69 rt( offset <= (i
9c50: 6e 74 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 nt)pBt->usableSi
9c60: 7a 65 2d 35 20 29 3b 0a 20 20 61 73 73 65 72 74 ze-5 );. assert
9c70: 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 ( pEType!=0 );.
9c80: 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d *pEType = pPtrm
9c90: 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 ap[offset];. if
9ca0: 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f ( pPgno ) *pPgno
9cb0: 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 74 = get4byte(&pPt
9cc0: 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b rmap[offset+1]);
9cd0: 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 .. sqlite3Pager
9ce0: 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a Unref(pDbPage);.
9cf0: 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 if( *pEType<1
9d00: 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 || *pEType>5 ) r
9d10: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
9d20: 52 55 50 54 5f 50 47 4e 4f 28 69 50 74 72 6d 61 RUPT_PGNO(iPtrma
9d30: 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c p);. return SQL
9d40: 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 ITE_OK;.}..#else
9d50: 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 20 53 /* if defined S
9d60: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
9d70: 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69 ACUUM */. #defi
9d80: 6e 65 20 70 74 72 6d 61 70 50 75 74 28 77 2c 78 ne ptrmapPut(w,x
9d90: 2c 79 2c 7a 2c 72 63 29 0a 20 20 23 64 65 66 69 ,y,z,rc). #defi
9da0: 6e 65 20 70 74 72 6d 61 70 47 65 74 28 77 2c 78 ne ptrmapGet(w,x
9db0: 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a ,y,z) SQLITE_OK.
9dc0: 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 #define ptrmap
9dd0: 50 75 74 4f 76 66 6c 50 74 72 28 78 2c 20 79 2c PutOvflPtr(x, y,
9de0: 20 72 63 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a rc).#endif../*.
9df0: 2a 2a 20 47 69 76 65 6e 20 61 20 62 74 72 65 65 ** Given a btree
9e00: 20 70 61 67 65 20 61 6e 64 20 61 20 63 65 6c 6c page and a cell
9e10: 20 69 6e 64 65 78 20 28 30 20 6d 65 61 6e 73 20 index (0 means
9e20: 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 6f the first cell o
9e30: 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 2c 20 31 n.** the page, 1
9e40: 20 6d 65 61 6e 73 20 74 68 65 20 73 65 63 6f 6e means the secon
9e50: 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 d cell, and so f
9e60: 6f 72 74 68 29 20 72 65 74 75 72 6e 20 61 20 70 orth) return a p
9e70: 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ointer.** to the
9e80: 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a cell content..*
9e90: 2a 0a 2a 2a 20 66 69 6e 64 43 65 6c 6c 50 61 73 *.** findCellPas
9ea0: 74 50 74 72 28 29 20 64 6f 65 73 20 74 68 65 20 tPtr() does the
9eb0: 73 61 6d 65 20 65 78 63 65 70 74 20 69 74 20 73 same except it s
9ec0: 6b 69 70 73 20 70 61 73 74 20 74 68 65 20 69 6e kips past the in
9ed0: 69 74 69 61 6c 0a 2a 2a 20 34 2d 62 79 74 65 20 itial.** 4-byte
9ee0: 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 66 6f child pointer fo
9ef0: 75 6e 64 20 6f 6e 20 69 6e 74 65 72 69 6f 72 20 und on interior
9f00: 70 61 67 65 73 2c 20 69 66 20 74 68 65 72 65 20 pages, if there
9f10: 69 73 20 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 is one..**.** Th
9f20: 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 is routine works
9f30: 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 only for pages
9f40: 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 that do not cont
9f50: 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
9f60: 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 ls..*/.#define f
9f70: 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 indCell(P,I) \.
9f80: 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 ((P)->aData + (
9f90: 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 (P)->maskPage &
9fa0: 67 65 74 32 62 79 74 65 41 6c 69 67 6e 65 64 28 get2byteAligned(
9fb0: 26 28 50 29 2d 3e 61 43 65 6c 6c 49 64 78 5b 32 &(P)->aCellIdx[2
9fc0: 2a 28 49 29 5d 29 29 29 0a 23 64 65 66 69 6e 65 *(I)]))).#define
9fd0: 20 66 69 6e 64 43 65 6c 6c 50 61 73 74 50 74 72 findCellPastPtr
9fe0: 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 2d 3e (P,I) \. ((P)->
9ff0: 61 44 61 74 61 4f 66 73 74 20 2b 20 28 28 50 29 aDataOfst + ((P)
a000: 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 74 ->maskPage & get
a010: 32 62 79 74 65 41 6c 69 67 6e 65 64 28 26 28 50 2byteAligned(&(P
a020: 29 2d 3e 61 43 65 6c 6c 49 64 78 5b 32 2a 28 49 )->aCellIdx[2*(I
a030: 29 5d 29 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 )]))).../*.** Th
a040: 69 73 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 61 69 is is common tai
a050: 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 l processing for
a060: 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
a070: 74 72 28 29 20 61 6e 64 0a 2a 2a 20 62 74 72 65 tr() and.** btre
a080: 65 50 61 72 73 65 43 65 6c 6c 50 74 72 49 6e 64 eParseCellPtrInd
a090: 65 78 28 29 20 66 6f 72 20 74 68 65 20 63 61 73 ex() for the cas
a0a0: 65 20 77 68 65 6e 20 74 68 65 20 63 65 6c 6c 20 e when the cell
a0b0: 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 65 6e 74 does not fit ent
a0c0: 69 72 65 6c 79 0a 2a 2a 20 6f 6e 20 61 20 73 69 irely.** on a si
a0d0: 6e 67 6c 65 20 42 2d 74 72 65 65 20 70 61 67 65 ngle B-tree page
a0e0: 2e 20 20 4d 61 6b 65 20 6e 65 63 65 73 73 61 72 . Make necessar
a0f0: 79 20 61 64 6a 75 73 74 6d 65 6e 74 73 20 74 6f y adjustments to
a100: 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f 0a 2a 2a the CellInfo.**
a110: 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
a120: 74 61 74 69 63 20 53 51 4c 49 54 45 5f 4e 4f 49 tatic SQLITE_NOI
a130: 4e 4c 49 4e 45 20 76 6f 69 64 20 62 74 72 65 65 NLINE void btree
a140: 50 61 72 73 65 43 65 6c 6c 41 64 6a 75 73 74 53 ParseCellAdjustS
a150: 69 7a 65 46 6f 72 4f 76 65 72 66 6c 6f 77 28 0a izeForOverflow(.
a160: 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
a170: 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 , /* Pag
a180: 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
a190: 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 cell */. u8 *p
a1a0: 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Cell,
a1b0: 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
a1c0: 20 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 the cell text.
a1d0: 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 */. CellInfo *p
a1e0: 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 Info /*
a1f0: 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 Fill in this str
a200: 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 2f ucture */.){. /
a210: 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 * If the payload
a220: 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 63 6f will not fit co
a230: 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 68 65 20 mpletely on the
a240: 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 65 20 68 local page, we h
a250: 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 65 63 69 ave. ** to deci
a260: 64 65 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 de how much to s
a270: 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 tore locally and
a280: 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 how much to spi
a290: 6c 6c 20 6f 6e 74 6f 0a 20 20 2a 2a 20 6f 76 65 ll onto. ** ove
a2a0: 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 rflow pages. Th
a2b0: 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f e strategy is to
a2c0: 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d minimize the am
a2d0: 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 ount of unused.
a2e0: 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 ** space on ove
a2f0: 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c rflow pages whil
a300: 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d e keeping the am
a310: 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 ount of local st
a320: 6f 72 61 67 65 0a 20 20 2a 2a 20 69 6e 20 62 65 orage. ** in be
a330: 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 tween minLocal a
a340: 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 2a nd maxLocal.. *
a350: 2a 0a 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 *. ** Warning:
a360: 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 changing the wa
a370: 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f y overflow paylo
a380: 61 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 ad is distribute
a390: 64 20 69 6e 20 61 6e 79 0a 20 20 2a 2a 20 77 61 d in any. ** wa
a3a0: 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e y will result in
a3b0: 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 an incompatible
a3c0: 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 20 file format..
a3d0: 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 */. int minLoca
a3e0: 6c 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 l; /* Minimum a
a3f0: 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 mount of payload
a400: 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f held locally */
a410: 0a 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b . int maxLocal;
a420: 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f /* Maximum amo
a430: 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 unt of payload h
a440: 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 eld locally */.
a450: 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 int surplus;
a460: 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c /* Overflow payl
a470: 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f oad available fo
a480: 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 r local storage
a490: 2a 2f 0a 0a 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d */.. minLocal =
a4a0: 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
a4b0: 3b 0a 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 ;. maxLocal = p
a4c0: 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a Page->maxLocal;.
a4d0: 20 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c surplus = minL
a4e0: 6f 63 61 6c 20 2b 20 28 70 49 6e 66 6f 2d 3e 6e ocal + (pInfo->n
a4f0: 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 Payload - minLoc
a500: 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d al)%(pPage->pBt-
a510: 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 29 3b 0a >usableSize-4);.
a520: 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70 testcase( surp
a530: 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b lus==maxLocal );
a540: 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 . testcase( sur
a550: 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31 plus==maxLocal+1
a560: 20 29 3b 0a 20 20 69 66 28 20 73 75 72 70 6c 75 );. if( surplu
a570: 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b s <= maxLocal ){
a580: 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 . pInfo->nLoc
a590: 61 6c 20 3d 20 28 75 31 36 29 73 75 72 70 6c 75 al = (u16)surplu
a5a0: 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
a5b0: 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 pInfo->nLocal =
a5c0: 28 75 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 (u16)minLocal;.
a5d0: 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a }. pInfo->nSiz
a5e0: 65 20 3d 20 28 75 31 36 29 28 26 70 49 6e 66 6f e = (u16)(&pInfo
a5f0: 2d 3e 70 50 61 79 6c 6f 61 64 5b 70 49 6e 66 6f ->pPayload[pInfo
a600: 2d 3e 6e 4c 6f 63 61 6c 5d 20 2d 20 70 43 65 6c ->nLocal] - pCel
a610: 6c 29 20 2b 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a l) + 4;.}../*.**
a620: 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
a630: 6f 75 74 69 6e 65 73 20 61 72 65 20 69 6d 70 6c outines are impl
a640: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 ementations of t
a650: 68 65 20 4d 65 6d 50 61 67 65 2e 78 50 61 72 73 he MemPage.xPars
a660: 65 43 65 6c 6c 28 29 0a 2a 2a 20 6d 65 74 68 6f eCell().** metho
a670: 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 73 65 20 61 d..**.** Parse a
a680: 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c cell content bl
a690: 6f 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 ock and fill in
a6a0: 74 68 65 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 the CellInfo str
a6b0: 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 62 74 ucture..**.** bt
a6c0: 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
a6d0: 29 20 20 20 20 20 20 20 20 3d 3e 20 20 20 74 61 ) => ta
a6e0: 62 6c 65 20 62 74 72 65 65 20 6c 65 61 66 20 6e ble btree leaf n
a6f0: 6f 64 65 73 0a 2a 2a 20 62 74 72 65 65 50 61 72 odes.** btreePar
a700: 73 65 43 65 6c 6c 4e 6f 50 61 79 6c 6f 61 64 28 seCellNoPayload(
a710: 29 20 20 3d 3e 20 20 20 74 61 62 6c 65 20 62 74 ) => table bt
a720: 72 65 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 ree internal nod
a730: 65 73 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 es.** btreeParse
a740: 43 65 6c 6c 50 74 72 49 6e 64 65 78 28 29 20 20 CellPtrIndex()
a750: 20 3d 3e 20 20 20 69 6e 64 65 78 20 62 74 72 65 => index btre
a760: 65 20 6e 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 54 68 e nodes.**.** Th
a770: 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 77 72 ere is also a wr
a780: 61 70 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 62 apper function b
a790: 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 treeParseCell()
a7a0: 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a that works for.*
a7b0: 2a 20 61 6c 6c 20 4d 65 6d 50 61 67 65 20 74 79 * all MemPage ty
a7c0: 70 65 73 20 61 6e 64 20 74 68 61 74 20 72 65 66 pes and that ref
a7d0: 65 72 65 6e 63 65 73 20 74 68 65 20 63 65 6c 6c erences the cell
a7e0: 20 62 79 20 69 6e 64 65 78 20 72 61 74 68 65 72 by index rather
a7f0: 20 74 68 61 6e 0a 2a 2a 20 62 79 20 70 6f 69 6e than.** by poin
a800: 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ter..*/.static v
a810: 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 oid btreeParseCe
a820: 6c 6c 50 74 72 4e 6f 50 61 79 6c 6f 61 64 28 0a llPtrNoPayload(.
a830: 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
a840: 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 , /* Pag
a850: 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
a860: 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 cell */. u8 *p
a870: 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Cell,
a880: 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
a890: 20 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 the cell text.
a8a0: 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 */. CellInfo *p
a8b0: 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 Info /*
a8c0: 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 Fill in this str
a8d0: 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 61 ucture */.){. a
a8e0: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
a8f0: 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
a900: 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
a910: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
a920: 3e 6c 65 61 66 3d 3d 30 20 29 3b 0a 20 20 61 73 >leaf==0 );. as
a930: 73 65 72 74 28 20 70 50 61 67 65 2d 3e 63 68 69 sert( pPage->chi
a940: 6c 64 50 74 72 53 69 7a 65 3d 3d 34 20 29 3b 0a ldPtrSize==4 );.
a950: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
a960: 45 42 55 47 0a 20 20 55 4e 55 53 45 44 5f 50 41 EBUG. UNUSED_PA
a970: 52 41 4d 45 54 45 52 28 70 50 61 67 65 29 3b 0a RAMETER(pPage);.
a980: 23 65 6e 64 69 66 0a 20 20 70 49 6e 66 6f 2d 3e #endif. pInfo->
a990: 6e 53 69 7a 65 20 3d 20 34 20 2b 20 67 65 74 56 nSize = 4 + getV
a9a0: 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 34 5d 2c arint(&pCell[4],
a9b0: 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e (u64*)&pInfo->n
a9c0: 4b 65 79 29 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e Key);. pInfo->n
a9d0: 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 70 Payload = 0;. p
a9e0: 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 30 Info->nLocal = 0
a9f0: 3b 0a 20 20 70 49 6e 66 6f 2d 3e 70 50 61 79 6c ;. pInfo->pPayl
aa00: 6f 61 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 oad = 0;. retur
aa10: 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 n;.}.static void
aa20: 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
aa30: 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 tr(. MemPage *p
aa40: 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a Page, /*
aa50: 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 Page containing
aa60: 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 the cell */. u
aa70: 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 8 *pCell,
aa80: 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
aa90: 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 r to the cell te
aaa0: 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 xt. */. CellInf
aab0: 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 o *pInfo
aac0: 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 /* Fill in this
aad0: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b structure */.){
aae0: 0a 20 20 75 38 20 2a 70 49 74 65 72 3b 20 20 20 . u8 *pIter;
aaf0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f /* Fo
ab00: 72 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 r scanning throu
ab10: 67 68 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 33 gh pCell */. u3
ab20: 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 2 nPayload;
ab30: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ab40: 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c of bytes of cell
ab50: 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 36 payload */. u6
ab60: 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 4 iKey;
ab70: 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 /* Extract
ab80: 65 64 20 4b 65 79 20 76 61 6c 75 65 20 2a 2f 0a ed Key value */.
ab90: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
aba0: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
abb0: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
abc0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
abd0: 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 age->leaf==0 ||
abe0: 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 pPage->leaf==1 )
abf0: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
ac00: 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 29 3b e->intKeyLeaf );
ac10: 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
ac20: 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3d 3d ->childPtrSize==
ac30: 30 20 29 3b 0a 20 20 70 49 74 65 72 20 3d 20 70 0 );. pIter = p
ac40: 43 65 6c 6c 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 Cell;.. /* The
ac50: 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f next block of co
ac60: 64 65 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 de is equivalent
ac70: 20 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 to:. **. **
ac80: 20 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 pIter += getV
ac90: 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e arint32(pIter, n
aca0: 50 61 79 6c 6f 61 64 29 3b 0a 20 20 2a 2a 0a 20 Payload);. **.
acb0: 20 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 73 20 ** The code is
acc0: 69 6e 6c 69 6e 65 64 20 74 6f 20 61 76 6f 69 64 inlined to avoid
acd0: 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c a function call
ace0: 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61 .. */. nPayloa
acf0: 64 20 3d 20 2a 70 49 74 65 72 3b 0a 20 20 69 66 d = *pIter;. if
ad00: 28 20 6e 50 61 79 6c 6f 61 64 3e 3d 30 78 38 30 ( nPayload>=0x80
ad10: 20 29 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 ){. u8 *pEnd
ad20: 20 3d 20 26 70 49 74 65 72 5b 38 5d 3b 0a 20 20 = &pIter[8];.
ad30: 20 20 6e 50 61 79 6c 6f 61 64 20 26 3d 20 30 78 nPayload &= 0x
ad40: 37 66 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 7f;. do{.
ad50: 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 28 6e 50 nPayload = (nP
ad60: 61 79 6c 6f 61 64 3c 3c 37 29 20 7c 20 28 2a 2b ayload<<7) | (*+
ad70: 2b 70 49 74 65 72 20 26 20 30 78 37 66 29 3b 0a +pIter & 0x7f);.
ad80: 20 20 20 20 7d 77 68 69 6c 65 28 20 28 2a 70 49 }while( (*pI
ad90: 74 65 72 29 3e 3d 30 78 38 30 20 26 26 20 70 49 ter)>=0x80 && pI
ada0: 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 0a ter<pEnd );. }.
adb0: 20 20 70 49 74 65 72 2b 2b 3b 0a 0a 20 20 2f 2a pIter++;.. /*
adc0: 20 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 The next block
add0: 6f 66 20 63 6f 64 65 20 69 73 20 65 71 75 69 76 of code is equiv
ade0: 61 6c 65 6e 74 20 74 6f 3a 0a 20 20 2a 2a 0a 20 alent to:. **.
adf0: 20 2a 2a 20 20 20 20 20 70 49 74 65 72 20 2b 3d ** pIter +=
ae00: 20 67 65 74 56 61 72 69 6e 74 28 70 49 74 65 72 getVarint(pIter
ae10: 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e , (u64*)&pInfo->
ae20: 6e 4b 65 79 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a nKey);. **. **
ae30: 20 54 68 65 20 63 6f 64 65 20 69 73 20 69 6e 6c The code is inl
ae40: 69 6e 65 64 20 74 6f 20 61 76 6f 69 64 20 61 20 ined to avoid a
ae50: 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 2e 0a 20 function call..
ae60: 20 2a 2f 0a 20 20 69 4b 65 79 20 3d 20 2a 70 49 */. iKey = *pI
ae70: 74 65 72 3b 0a 20 20 69 66 28 20 69 4b 65 79 3e ter;. if( iKey>
ae80: 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 75 38 20 =0x80 ){. u8
ae90: 2a 70 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 37 *pEnd = &pIter[7
aea0: 5d 3b 0a 20 20 20 20 69 4b 65 79 20 26 3d 20 30 ];. iKey &= 0
aeb0: 78 37 66 3b 0a 20 20 20 20 77 68 69 6c 65 28 31 x7f;. while(1
aec0: 29 7b 0a 20 20 20 20 20 20 69 4b 65 79 20 3d 20 ){. iKey =
aed0: 28 69 4b 65 79 3c 3c 37 29 20 7c 20 28 2a 2b 2b (iKey<<7) | (*++
aee0: 70 49 74 65 72 20 26 20 30 78 37 66 29 3b 0a 20 pIter & 0x7f);.
aef0: 20 20 20 20 20 69 66 28 20 28 2a 70 49 74 65 72 if( (*pIter
af00: 29 3c 30 78 38 30 20 29 20 62 72 65 61 6b 3b 0a )<0x80 ) break;.
af10: 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72 3e if( pIter>
af20: 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 =pEnd ){.
af30: 20 69 4b 65 79 20 3d 20 28 69 4b 65 79 3c 3c 38 iKey = (iKey<<8
af40: 29 20 7c 20 2a 2b 2b 70 49 74 65 72 3b 0a 20 20 ) | *++pIter;.
af50: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
af60: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
af70: 20 70 49 74 65 72 2b 2b 3b 0a 0a 20 20 70 49 6e pIter++;.. pIn
af80: 66 6f 2d 3e 6e 4b 65 79 20 3d 20 2a 28 69 36 34 fo->nKey = *(i64
af90: 2a 29 26 69 4b 65 79 3b 0a 20 20 70 49 6e 66 6f *)&iKey;. pInfo
afa0: 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 ->nPayload = nPa
afb0: 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e yload;. pInfo->
afc0: 70 50 61 79 6c 6f 61 64 20 3d 20 70 49 74 65 72 pPayload = pIter
afd0: 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 ;. testcase( nP
afe0: 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d ayload==pPage->m
aff0: 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 axLocal );. tes
b000: 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d tcase( nPayload=
b010: 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
b020: 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 50 61 79 +1 );. if( nPay
b030: 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 load<=pPage->max
b040: 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 Local ){. /*
b050: 54 68 69 73 20 69 73 20 74 68 65 20 28 65 61 73 This is the (eas
b060: 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 y) common case w
b070: 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 here the entire
b080: 70 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 payload fits.
b090: 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c ** on the local
b0a0: 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 page. No overf
b0b0: 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 2e low is required.
b0c0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 66 . */. pInf
b0d0: 6f 2d 3e 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c o->nSize = nPayl
b0e0: 6f 61 64 20 2b 20 28 75 31 36 29 28 70 49 74 65 oad + (u16)(pIte
b0f0: 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 r - pCell);.
b100: 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 if( pInfo->nSize
b110: 3c 34 20 29 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a <4 ) pInfo->nSiz
b120: 65 20 3d 20 34 3b 0a 20 20 20 20 70 49 6e 66 6f e = 4;. pInfo
b130: 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 ->nLocal = (u16)
b140: 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 nPayload;. }els
b150: 65 7b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 e{. btreePars
b160: 65 43 65 6c 6c 41 64 6a 75 73 74 53 69 7a 65 46 eCellAdjustSizeF
b170: 6f 72 4f 76 65 72 66 6c 6f 77 28 70 50 61 67 65 orOverflow(pPage
b180: 2c 20 70 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b , pCell, pInfo);
b190: 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f . }.}.static vo
b1a0: 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c id btreeParseCel
b1b0: 6c 50 74 72 49 6e 64 65 78 28 0a 20 20 4d 65 6d lPtrIndex(. Mem
b1c0: 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
b1d0: 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e /* Page con
b1e0: 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c taining the cell
b1f0: 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
b200: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
b210: 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
b220: 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 cell text. */.
b230: 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 CellInfo *pInfo
b240: 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 /* Fill
b250: 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
b260: 65 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 49 e */.){. u8 *pI
b270: 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ter;
b280: 20 20 2f 2a 20 46 6f 72 20 73 63 61 6e 6e 69 6e /* For scannin
b290: 67 20 74 68 72 6f 75 67 68 20 70 43 65 6c 6c 20 g through pCell
b2a0: 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 */. u32 nPayloa
b2b0: 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d; /*
b2c0: 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
b2d0: 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 of cell payload
b2e0: 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
b2f0: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
b300: 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
b310: 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
b320: 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 pPage->leaf==0
b330: 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d || pPage->leaf==
b340: 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 1 );. assert( p
b350: 50 61 67 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 Page->intKeyLeaf
b360: 3d 3d 30 20 29 3b 0a 20 20 70 49 74 65 72 20 3d ==0 );. pIter =
b370: 20 70 43 65 6c 6c 20 2b 20 70 50 61 67 65 2d 3e pCell + pPage->
b380: 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
b390: 6e 50 61 79 6c 6f 61 64 20 3d 20 2a 70 49 74 65 nPayload = *pIte
b3a0: 72 3b 0a 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 r;. if( nPayloa
b3b0: 64 3e 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 75 d>=0x80 ){. u
b3c0: 38 20 2a 70 45 6e 64 20 3d 20 26 70 49 74 65 72 8 *pEnd = &pIter
b3d0: 5b 38 5d 3b 0a 20 20 20 20 6e 50 61 79 6c 6f 61 [8];. nPayloa
b3e0: 64 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 64 d &= 0x7f;. d
b3f0: 6f 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61 o{. nPayloa
b400: 64 20 3d 20 28 6e 50 61 79 6c 6f 61 64 3c 3c 37 d = (nPayload<<7
b410: 29 20 7c 20 28 2a 2b 2b 70 49 74 65 72 20 26 20 ) | (*++pIter &
b420: 30 78 37 66 29 3b 0a 20 20 20 20 7d 77 68 69 6c 0x7f);. }whil
b430: 65 28 20 2a 28 70 49 74 65 72 29 3e 3d 30 78 38 e( *(pIter)>=0x8
b440: 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20 0 && pIter<pEnd
b450: 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 72 2b 2b );. }. pIter++
b460: 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 ;. pInfo->nKey
b470: 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 = nPayload;. pI
b480: 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 nfo->nPayload =
b490: 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 nPayload;. pInf
b4a0: 6f 2d 3e 70 50 61 79 6c 6f 61 64 20 3d 20 70 49 o->pPayload = pI
b4b0: 74 65 72 3b 0a 20 20 74 65 73 74 63 61 73 65 28 ter;. testcase(
b4c0: 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 nPayload==pPage
b4d0: 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
b4e0: 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f testcase( nPaylo
b4f0: 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ad==pPage->maxLo
b500: 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e cal+1 );. if( n
b510: 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e Payload<=pPage->
b520: 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 maxLocal ){.
b530: 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 28 /* This is the (
b540: 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 easy) common cas
b550: 65 20 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 e where the enti
b560: 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a re payload fits.
b570: 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f ** on the lo
b580: 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 cal page. No ov
b590: 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 erflow is requir
b5a0: 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 ed.. */. p
b5b0: 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 6e 50 Info->nSize = nP
b5c0: 61 79 6c 6f 61 64 20 2b 20 28 75 31 36 29 28 70 ayload + (u16)(p
b5d0: 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 20 Iter - pCell);.
b5e0: 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 53 if( pInfo->nS
b5f0: 69 7a 65 3c 34 20 29 20 70 49 6e 66 6f 2d 3e 6e ize<4 ) pInfo->n
b600: 53 69 7a 65 20 3d 20 34 3b 0a 20 20 20 20 70 49 Size = 4;. pI
b610: 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 nfo->nLocal = (u
b620: 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 16)nPayload;. }
b630: 65 6c 73 65 7b 0a 20 20 20 20 62 74 72 65 65 50 else{. btreeP
b640: 61 72 73 65 43 65 6c 6c 41 64 6a 75 73 74 53 69 arseCellAdjustSi
b650: 7a 65 46 6f 72 4f 76 65 72 66 6c 6f 77 28 70 50 zeForOverflow(pP
b660: 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70 49 6e 66 age, pCell, pInf
b670: 6f 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 o);. }.}.static
b680: 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 void btreeParse
b690: 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 Cell(. MemPage
b6a0: 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 *pPage,
b6b0: 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 /* Page containi
b6c0: 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 ng the cell */.
b6d0: 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20 int iCell,
b6e0: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
b6f0: 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72 cell index. Fir
b700: 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a st cell is 0 */.
b710: 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 CellInfo *pInf
b720: 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c o /* Fil
b730: 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 l in this struct
b740: 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 50 61 67 ure */.){. pPag
b750: 65 2d 3e 78 50 61 72 73 65 43 65 6c 6c 28 70 50 e->xParseCell(pP
b760: 61 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 age, findCell(pP
b770: 61 67 65 2c 20 69 43 65 6c 6c 29 2c 20 70 49 6e age, iCell), pIn
b780: 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 fo);.}../*.** Th
b790: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
b7a0: 69 6e 65 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 ines are impleme
b7b0: 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
b7c0: 4d 65 6d 50 61 67 65 2e 78 43 65 6c 6c 53 69 7a MemPage.xCellSiz
b7d0: 65 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a e.** method..**.
b7e0: 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 ** Compute the t
b7f0: 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 otal number of b
b800: 79 74 65 73 20 74 68 61 74 20 61 20 43 65 6c 6c ytes that a Cell
b810: 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20 63 65 needs in the ce
b820: 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65 61 20 ll.** data area
b830: 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70 61 67 of the btree-pag
b840: 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6e e. The return n
b850: 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73 20 74 umber includes t
b860: 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 he cell.** data
b870: 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 20 6c header and the l
b880: 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 75 ocal payload, bu
b890: 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 6c t not any overfl
b8a0: 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 68 ow page or.** th
b8b0: 65 20 73 70 61 63 65 20 75 73 65 64 20 62 79 20 e space used by
b8c0: 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 the cell pointer
b8d0: 2e 0a 2a 2a 0a 2a 2a 20 63 65 6c 6c 53 69 7a 65 ..**.** cellSize
b8e0: 50 74 72 4e 6f 50 61 79 6c 6f 61 64 28 29 20 20 PtrNoPayload()
b8f0: 20 20 3d 3e 20 20 20 74 61 62 6c 65 20 69 6e 74 => table int
b900: 65 72 6e 61 6c 20 6e 6f 64 65 73 0a 2a 2a 20 63 ernal nodes.** c
b910: 65 6c 6c 53 69 7a 65 50 74 72 28 29 20 20 20 20 ellSizePtr()
b920: 20 20 20 20 20 20 20 20 20 3d 3e 20 20 20 61 6c => al
b930: 6c 20 69 6e 64 65 78 20 6e 6f 64 65 73 20 26 20 l index nodes &
b940: 74 61 62 6c 65 20 6c 65 61 66 20 6e 6f 64 65 73 table leaf nodes
b950: 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 .*/.static u16 c
b960: 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50 61 ellSizePtr(MemPa
b970: 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 ge *pPage, u8 *p
b980: 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 49 74 Cell){. u8 *pIt
b990: 65 72 20 3d 20 70 43 65 6c 6c 20 2b 20 70 50 61 er = pCell + pPa
b9a0: 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
b9b0: 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 ; /* For looping
b9c0: 20 6f 76 65 72 20 62 79 74 65 73 20 6f 66 20 70 over bytes of p
b9d0: 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 45 Cell */. u8 *pE
b9e0: 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nd;
b9f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ba00: 20 20 20 2f 2a 20 45 6e 64 20 6d 61 72 6b 20 66 /* End mark f
ba10: 6f 72 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 or a varint */.
ba20: 20 75 33 32 20 6e 53 69 7a 65 3b 20 20 20 20 20 u32 nSize;
ba30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ba40: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
ba50: 65 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 e value to retur
ba60: 6e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c n */..#ifdef SQL
ba70: 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 ITE_DEBUG. /* T
ba80: 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
ba90: 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
baa0: 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 on should always
bab0: 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a be the same as.
bac0: 20 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e ** the (CellIn
bad0: 66 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 fo.nSize) value
bae0: 66 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 found by doing a
baf0: 20 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 full parse of t
bb00: 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 he. ** cell. If
bb10: 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 SQLITE_DEBUG is
bb20: 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 defined, an ass
bb30: 65 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 ert() at the bot
bb40: 74 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 tom of. ** this
bb50: 20 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 function verifi
bb60: 65 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 es that this inv
bb70: 61 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 ariant is not vi
bb80: 6f 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c olated. */. Cel
bb90: 6c 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b lInfo debuginfo;
bba0: 0a 20 20 70 50 61 67 65 2d 3e 78 50 61 72 73 65 . pPage->xParse
bbb0: 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c Cell(pPage, pCel
bbc0: 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a l, &debuginfo);.
bbd0: 23 65 6e 64 69 66 0a 0a 20 20 6e 53 69 7a 65 20 #endif.. nSize
bbe0: 3d 20 2a 70 49 74 65 72 3b 0a 20 20 69 66 28 20 = *pIter;. if(
bbf0: 6e 53 69 7a 65 3e 3d 30 78 38 30 20 29 7b 0a 20 nSize>=0x80 ){.
bc00: 20 20 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72 pEnd = &pIter
bc10: 5b 38 5d 3b 0a 20 20 20 20 6e 53 69 7a 65 20 26 [8];. nSize &
bc20: 3d 20 30 78 37 66 3b 0a 20 20 20 20 64 6f 7b 0a = 0x7f;. do{.
bc30: 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 28 6e nSize = (n
bc40: 53 69 7a 65 3c 3c 37 29 20 7c 20 28 2a 2b 2b 70 Size<<7) | (*++p
bc50: 49 74 65 72 20 26 20 30 78 37 66 29 3b 0a 20 20 Iter & 0x7f);.
bc60: 20 20 7d 77 68 69 6c 65 28 20 2a 28 70 49 74 65 }while( *(pIte
bc70: 72 29 3e 3d 30 78 38 30 20 26 26 20 70 49 74 65 r)>=0x80 && pIte
bc80: 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 0a 20 20 r<pEnd );. }.
bc90: 70 49 74 65 72 2b 2b 3b 0a 20 20 69 66 28 20 70 pIter++;. if( p
bca0: 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a Page->intKey ){.
bcb0: 20 20 20 20 2f 2a 20 70 49 74 65 72 20 6e 6f 77 /* pIter now
bcc0: 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 36 points at the 6
bcd0: 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 6b 65 4-bit integer ke
bce0: 79 20 76 61 6c 75 65 2c 20 61 20 76 61 72 69 61 y value, a varia
bcf0: 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 20 20 20 ble length .
bd00: 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 ** integer. The
bd10: 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 following block
bd20: 6d 6f 76 65 73 20 70 49 74 65 72 20 74 6f 20 70 moves pIter to p
bd30: 6f 69 6e 74 20 61 74 20 74 68 65 20 66 69 72 73 oint at the firs
bd40: 74 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 70 61 t byte. ** pa
bd50: 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 st the end of th
bd60: 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 2a 2f 0a e key value. */.
bd70: 20 20 20 20 70 45 6e 64 20 3d 20 26 70 49 74 65 pEnd = &pIte
bd80: 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 r[9];. while(
bd90: 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 78 38 30 (*pIter++)&0x80
bda0: 20 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20 29 && pIter<pEnd )
bdb0: 3b 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 73 65 ;. }. testcase
bdc0: 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e ( nSize==pPage->
bdd0: 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 maxLocal );. te
bde0: 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 stcase( nSize==p
bdf0: 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 Page->maxLocal+1
be00: 20 29 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 3c );. if( nSize<
be10: 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
be20: 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 2b 3d ){. nSize +=
be30: 20 28 75 33 32 29 28 70 49 74 65 72 20 2d 20 70 (u32)(pIter - p
be40: 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 6e Cell);. if( n
be50: 53 69 7a 65 3c 34 20 29 20 6e 53 69 7a 65 20 3d Size<4 ) nSize =
be60: 20 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 4;. }else{.
be70: 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 int minLocal =
be80: 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b pPage->minLocal;
be90: 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e . nSize = min
bea0: 4c 6f 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 2d Local + (nSize -
beb0: 20 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 50 minLocal) % (pP
bec0: 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
bed0: 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 Size - 4);. t
bee0: 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d estcase( nSize==
bef0: 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pPage->maxLocal
bf00: 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
bf10: 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d nSize==pPage->m
bf20: 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 axLocal+1 );.
bf30: 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 if( nSize>pPage
bf40: 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 ->maxLocal ){.
bf50: 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c nSize = minL
bf60: 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ocal;. }.
bf70: 6e 53 69 7a 65 20 2b 3d 20 34 20 2b 20 28 75 31 nSize += 4 + (u1
bf80: 36 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 6)(pIter - pCell
bf90: 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
bfa0: 20 6e 53 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 nSize==debuginf
bfb0: 6f 2e 6e 53 69 7a 65 20 7c 7c 20 43 4f 52 52 55 o.nSize || CORRU
bfc0: 50 54 5f 44 42 20 29 3b 0a 20 20 72 65 74 75 72 PT_DB );. retur
bfd0: 6e 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a n (u16)nSize;.}.
bfe0: 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 static u16 cellS
bff0: 69 7a 65 50 74 72 4e 6f 50 61 79 6c 6f 61 64 28 izePtrNoPayload(
c000: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
c010: 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 u8 *pCell){. u8
c020: 20 2a 70 49 74 65 72 20 3d 20 70 43 65 6c 6c 20 *pIter = pCell
c030: 2b 20 34 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 + 4; /* For loop
c040: 69 6e 67 20 6f 76 65 72 20 62 79 74 65 73 20 6f ing over bytes o
c050: 66 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 f pCell */. u8
c060: 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 *pEnd;
c070: 20 20 20 20 2f 2a 20 45 6e 64 20 6d 61 72 6b 20 /* End mark
c080: 66 6f 72 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a for a varint */.
c090: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
c0a0: 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 61 EBUG. /* The va
c0b0: 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
c0c0: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 this function sh
c0d0: 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74 ould always be t
c0e0: 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a 20 he same as. **
c0f0: 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 the (CellInfo.nS
c100: 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 6e 64 ize) value found
c110: 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 6c 6c by doing a full
c120: 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a 20 20 parse of the.
c130: 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c 49 ** cell. If SQLI
c140: 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 TE_DEBUG is defi
c150: 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 ned, an assert()
c160: 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f at the bottom o
c170: 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 f. ** this func
c180: 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 74 68 tion verifies th
c190: 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 61 6e at this invarian
c1a0: 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 65 t is not violate
c1b0: 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f d. */. CellInfo
c1c0: 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 70 50 debuginfo;. pP
c1d0: 61 67 65 2d 3e 78 50 61 72 73 65 43 65 6c 6c 28 age->xParseCell(
c1e0: 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 64 pPage, pCell, &d
c1f0: 65 62 75 67 69 6e 66 6f 29 3b 0a 23 65 6c 73 65 ebuginfo);.#else
c200: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
c210: 54 45 52 28 70 50 61 67 65 29 3b 0a 23 65 6e 64 TER(pPage);.#end
c220: 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 if.. assert( pP
c230: 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
c240: 65 3d 3d 34 20 29 3b 0a 20 20 70 45 6e 64 20 3d e==4 );. pEnd =
c250: 20 70 49 74 65 72 20 2b 20 39 3b 0a 20 20 77 68 pIter + 9;. wh
c260: 69 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 ile( (*pIter++)&
c270: 30 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 0x80 && pIter<pE
c280: 6e 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nd );. assert(
c290: 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 7a 65 3d debuginfo.nSize=
c2a0: 3d 28 75 31 36 29 28 70 49 74 65 72 20 2d 20 70 =(u16)(pIter - p
c2b0: 43 65 6c 6c 29 20 7c 7c 20 43 4f 52 52 55 50 54 Cell) || CORRUPT
c2c0: 5f 44 42 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 _DB );. return
c2d0: 28 75 31 36 29 28 70 49 74 65 72 20 2d 20 70 43 (u16)(pIter - pC
c2e0: 65 6c 6c 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 ell);.}...#ifdef
c2f0: 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
c300: 20 54 68 69 73 20 76 61 72 69 61 74 69 6f 6e 20 This variation
c310: 6f 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 29 on cellSizePtr()
c320: 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 is used inside
c330: 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74 of assert() stat
c340: 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 ements.** only.
c350: 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 */.static u16 ce
c360: 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a llSize(MemPage *
c370: 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c pPage, int iCell
c380: 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
c390: 65 2d 3e 78 43 65 6c 6c 53 69 7a 65 28 70 50 61 e->xCellSize(pPa
c3a0: 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 ge, findCell(pPa
c3b0: 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 ge, iCell));.}.#
c3c0: 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
c3d0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
c3e0: 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ACUUM./*.** If t
c3f0: 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 he cell pCell, p
c400: 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 67 art of page pPag
c410: 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 e contains a poi
c420: 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 nter.** to an ov
c430: 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 erflow page, ins
c440: 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 ert an entry int
c450: 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
c460: 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 p.** for the ove
c470: 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 rflow page..*/.s
c480: 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 tatic void ptrma
c490: 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 pPutOvflPtr(MemP
c4a0: 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
c4b0: 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 pCell, int *pRC)
c4c0: 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 {. CellInfo inf
c4d0: 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 o;. if( *pRC )
c4e0: 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
c4f0: 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 ( pCell!=0 );.
c500: 70 50 61 67 65 2d 3e 78 50 61 72 73 65 43 65 6c pPage->xParseCel
c510: 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 l(pPage, pCell,
c520: 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e &info);. if( in
c530: 66 6f 2e 6e 4c 6f 63 61 6c 3c 69 6e 66 6f 2e 6e fo.nLocal<info.n
c540: 50 61 79 6c 6f 61 64 20 29 7b 0a 20 20 20 20 50 Payload ){. P
c550: 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 gno ovfl = get4b
c560: 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e yte(&pCell[info.
c570: 6e 53 69 7a 65 2d 34 5d 29 3b 0a 20 20 20 20 70 nSize-4]);. p
c580: 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e trmapPut(pPage->
c590: 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d 41 pBt, ovfl, PTRMA
c5a0: 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 P_OVERFLOW1, pPa
c5b0: 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b 0a ge->pgno, pRC);.
c5c0: 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f }.}.#endif.../
c5d0: 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 *.** Defragment
c5e0: 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e 20 the page given.
c5f0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 6f This routine reo
c600: 72 67 61 6e 69 7a 65 73 20 63 65 6c 6c 73 20 77 rganizes cells w
c610: 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 70 61 67 ithin the.** pag
c620: 65 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 20 e so that there
c630: 61 72 65 20 6e 6f 20 66 72 65 65 2d 62 6c 6f 63 are no free-bloc
c640: 6b 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 62 ks on the free-b
c650: 6c 6f 63 6b 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a lock list..**.**
c660: 20 50 61 72 61 6d 65 74 65 72 20 6e 4d 61 78 46 Parameter nMaxF
c670: 72 61 67 20 69 73 20 74 68 65 20 6d 61 78 69 6d rag is the maxim
c680: 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 61 um amount of fra
c690: 67 6d 65 6e 74 65 64 20 73 70 61 63 65 20 74 68 gmented space th
c6a0: 61 74 20 6d 61 79 20 62 65 0a 2a 2a 20 70 72 65 at may be.** pre
c6b0: 73 65 6e 74 20 69 6e 20 74 68 65 20 70 61 67 65 sent in the page
c6c0: 20 61 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 after this rout
c6d0: 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a ine returns..**.
c6e0: 2a 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 ** EVIDENCE-OF:
c6f0: 52 2d 34 34 35 38 32 2d 36 30 31 33 38 20 53 51 R-44582-60138 SQ
c700: 4c 69 74 65 20 6d 61 79 20 66 72 6f 6d 20 74 69 Lite may from ti
c710: 6d 65 20 74 6f 20 74 69 6d 65 20 72 65 6f 72 67 me to time reorg
c720: 61 6e 69 7a 65 20 61 0a 2a 2a 20 62 2d 74 72 65 anize a.** b-tre
c730: 65 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 74 e page so that t
c740: 68 65 72 65 20 61 72 65 20 6e 6f 20 66 72 65 65 here are no free
c750: 62 6c 6f 63 6b 73 20 6f 72 20 66 72 61 67 6d 65 blocks or fragme
c760: 6e 74 20 62 79 74 65 73 2c 20 61 6c 6c 0a 2a 2a nt bytes, all.**
c770: 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61 72 unused bytes ar
c780: 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 e contained in t
c790: 68 65 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 73 he unallocated s
c7a0: 70 61 63 65 20 72 65 67 69 6f 6e 2c 20 61 6e 64 pace region, and
c7b0: 20 61 6c 6c 0a 2a 2a 20 63 65 6c 6c 73 20 61 72 all.** cells ar
c7c0: 65 20 70 61 63 6b 65 64 20 74 69 67 68 74 6c 79 e packed tightly
c7d0: 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
c7e0: 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 he page..*/.stat
c7f0: 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e ic int defragmen
c800: 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 tPage(MemPage *p
c810: 50 61 67 65 2c 20 69 6e 74 20 6e 4d 61 78 46 72 Page, int nMaxFr
c820: 61 67 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 ag){. int i;
c830: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c840: 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
c850: 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 r */. int pc;
c860: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c870: 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
c880: 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f the i-th cell */
c890: 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 . int hdr;
c8a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c8b0: 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 Offset to the p
c8c0: 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 age header */.
c8d0: 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 int size;
c8e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
c8f0: 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a ze of a cell */.
c900: 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 int usableSize
c910: 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
c920: 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 Number of usable
c930: 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 bytes on a page
c940: 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 */. int cellOf
c950: 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
c960: 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 /* Offset to th
c970: 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
c980: 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 rray */. int cb
c990: 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rk;
c9a0: 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
c9b0: 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
c9c0: 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 nt area */. int
c9d0: 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 nCell;
c9e0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
c9f0: 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 r of cells on th
ca00: 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 e page */. unsi
ca10: 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
ca20: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
ca30: 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 ge data */. uns
ca40: 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 igned char *temp
ca50: 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 ; /* Temp
ca60: 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f area for cell co
ca70: 6e 74 65 6e 74 20 2a 2f 0a 20 20 75 6e 73 69 67 ntent */. unsig
ca80: 6e 65 64 20 63 68 61 72 20 2a 73 72 63 3b 20 20 ned char *src;
ca90: 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
caa0: 6f 66 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 of content */.
cab0: 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 int iCellFirst;
cac0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
cad0: 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 rst allowable ce
cae0: 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e ll index */. in
caf0: 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 t iCellLast;
cb00: 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
cb10: 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 69 possible cell i
cb20: 6e 64 65 78 20 2a 2f 0a 0a 20 20 61 73 73 65 72 ndex */.. asser
cb30: 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
cb40: 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
cb50: 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
cb60: 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
cb70: 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 Bt!=0 );. asser
cb80: 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 t( pPage->pBt->u
cb90: 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c sableSize <= SQL
cba0: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
cbb0: 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 E );. assert( p
cbc0: 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d Page->nOverflow=
cbd0: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
cbe0: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
cbf0: 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
cc00: 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 utex) );. temp
cc10: 3d 20 30 3b 0a 20 20 73 72 63 20 3d 20 64 61 74 = 0;. src = dat
cc20: 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
cc30: 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d ;. hdr = pPage-
cc40: 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 >hdrOffset;. ce
cc50: 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 llOffset = pPage
cc60: 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 ->cellOffset;.
cc70: 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e nCell = pPage->n
cc80: 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 Cell;. assert(
cc90: 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 nCell==get2byte(
cca0: 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b &data[hdr+3]) );
ccb0: 0a 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 . iCellFirst =
ccc0: 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e cellOffset + 2*n
ccd0: 43 65 6c 6c 3b 0a 20 20 75 73 61 62 6c 65 53 69 Cell;. usableSi
cce0: 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d ze = pPage->pBt-
ccf0: 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 0a 20 20 >usableSize;..
cd00: 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 68 61 /* This block ha
cd10: 6e 64 6c 65 73 20 70 61 67 65 73 20 77 69 74 68 ndles pages with
cd20: 20 74 77 6f 20 6f 72 20 66 65 77 65 72 20 66 72 two or fewer fr
cd30: 65 65 20 62 6c 6f 63 6b 73 20 61 6e 64 20 6e 4d ee blocks and nM
cd40: 61 78 46 72 61 67 0a 20 20 2a 2a 20 6f 72 20 66 axFrag. ** or f
cd50: 65 77 65 72 20 66 72 61 67 6d 65 6e 74 65 64 20 ewer fragmented
cd60: 62 79 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 bytes. In this c
cd70: 61 73 65 20 69 74 20 69 73 20 66 61 73 74 65 72 ase it is faster
cd80: 20 74 6f 20 6d 6f 76 65 20 74 68 65 0a 20 20 2a to move the. *
cd90: 2a 20 74 77 6f 20 28 6f 72 20 6f 6e 65 29 20 62 * two (or one) b
cda0: 6c 6f 63 6b 73 20 6f 66 20 63 65 6c 6c 73 20 75 locks of cells u
cdb0: 73 69 6e 67 20 6d 65 6d 6d 6f 76 65 28 29 20 61 sing memmove() a
cdc0: 6e 64 20 61 64 64 20 74 68 65 20 72 65 71 75 69 nd add the requi
cdd0: 72 65 64 0a 20 20 2a 2a 20 6f 66 66 73 65 74 73 red. ** offsets
cde0: 20 74 6f 20 65 61 63 68 20 70 6f 69 6e 74 65 72 to each pointer
cdf0: 20 69 6e 20 74 68 65 20 63 65 6c 6c 2d 70 6f 69 in the cell-poi
ce00: 6e 74 65 72 20 61 72 72 61 79 20 74 68 61 6e 20 nter array than
ce10: 69 74 20 69 73 20 74 6f 20 0a 20 20 2a 2a 20 72 it is to . ** r
ce20: 65 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 65 econstruct the e
ce30: 6e 74 69 72 65 20 70 61 67 65 2e 20 20 2a 2f 0a ntire page. */.
ce40: 20 20 69 66 28 20 28 69 6e 74 29 64 61 74 61 5b if( (int)data[
ce50: 68 64 72 2b 37 5d 3c 3d 6e 4d 61 78 46 72 61 67 hdr+7]<=nMaxFrag
ce60: 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 46 72 65 ){. int iFre
ce70: 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 e = get2byte(&da
ce80: 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 ta[hdr+1]);.
ce90: 69 66 28 20 69 46 72 65 65 20 29 7b 0a 20 20 20 if( iFree ){.
cea0: 20 20 20 69 6e 74 20 69 46 72 65 65 32 20 3d 20 int iFree2 =
ceb0: 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 get2byte(&data[i
cec0: 46 72 65 65 5d 29 3b 0a 0a 20 20 20 20 20 20 2f Free]);.. /
ced0: 2a 20 70 61 67 65 46 69 6e 64 53 6c 6f 74 28 29 * pageFindSlot()
cee0: 20 68 61 73 20 61 6c 72 65 61 64 79 20 76 65 72 has already ver
cef0: 69 66 69 65 64 20 74 68 61 74 20 66 72 65 65 20 ified that free
cf00: 62 6c 6f 63 6b 73 20 61 72 65 20 73 6f 72 74 65 blocks are sorte
cf10: 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 6f 72 d. ** in or
cf20: 64 65 72 20 6f 66 20 6f 66 66 73 65 74 20 77 69 der of offset wi
cf30: 74 68 69 6e 20 74 68 65 20 70 61 67 65 2c 20 61 thin the page, a
cf40: 6e 64 20 74 68 61 74 20 6e 6f 20 62 6c 6f 63 6b nd that no block
cf50: 20 65 78 74 65 6e 64 73 0a 20 20 20 20 20 20 2a extends. *
cf60: 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f * past the end o
cf70: 66 20 74 68 65 20 70 61 67 65 2e 20 50 72 6f 76 f the page. Prov
cf80: 69 64 65 64 20 74 68 65 20 74 77 6f 20 66 72 65 ided the two fre
cf90: 65 20 73 6c 6f 74 73 20 64 6f 20 6e 6f 74 20 0a e slots do not .
cfa0: 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 6c 61 70 ** overlap
cfb0: 2c 20 74 68 69 73 20 67 75 61 72 61 6e 74 65 65 , this guarantee
cfc0: 73 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6d 6f s that the memmo
cfd0: 76 65 28 29 20 63 61 6c 6c 73 20 62 65 6c 6f 77 ve() calls below
cfe0: 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 20 20 20 20 will not.
cff0: 2a 2a 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 ** overwrite the
d000: 20 75 73 61 62 6c 65 53 69 7a 65 20 62 79 74 65 usableSize byte
d010: 20 62 75 66 66 65 72 2c 20 65 76 65 6e 20 69 66 buffer, even if
d020: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
d030: 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 63 ge. ** is c
d040: 6f 72 72 75 70 74 2e 20 20 2a 2f 0a 20 20 20 20 orrupt. */.
d050: 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 32 assert( iFree2
d060: 3d 3d 30 20 7c 7c 20 69 46 72 65 65 32 3e 69 46 ==0 || iFree2>iF
d070: 72 65 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ree );. ass
d080: 65 72 74 28 20 69 46 72 65 65 2b 67 65 74 32 62 ert( iFree+get2b
d090: 79 74 65 28 26 64 61 74 61 5b 69 46 72 65 65 2b yte(&data[iFree+
d0a0: 32 5d 29 20 3c 3d 20 75 73 61 62 6c 65 53 69 7a 2]) <= usableSiz
d0b0: 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 e );. asser
d0c0: 74 28 20 69 46 72 65 65 32 3d 3d 30 20 7c 7c 20 t( iFree2==0 ||
d0d0: 69 46 72 65 65 32 2b 67 65 74 32 62 79 74 65 28 iFree2+get2byte(
d0e0: 26 64 61 74 61 5b 69 46 72 65 65 32 2b 32 5d 29 &data[iFree2+2])
d0f0: 20 3c 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 29 <= usableSize )
d100: 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d ;.. if( 0==
d110: 69 46 72 65 65 32 20 7c 7c 20 28 64 61 74 61 5b iFree2 || (data[
d120: 69 46 72 65 65 32 5d 3d 3d 30 20 26 26 20 64 61 iFree2]==0 && da
d130: 74 61 5b 69 46 72 65 65 32 2b 31 5d 3d 3d 30 29 ta[iFree2+1]==0)
d140: 20 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20 2a ){. u8 *
d150: 70 45 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c pEnd = &data[cel
d160: 6c 4f 66 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a lOffset + nCell*
d170: 32 5d 3b 0a 20 20 20 20 20 20 20 20 75 38 20 2a 2];. u8 *
d180: 70 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 69 pAddr;. i
d190: 6e 74 20 73 7a 32 20 3d 20 30 3b 0a 20 20 20 20 nt sz2 = 0;.
d1a0: 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 67 65 74 int sz = get
d1b0: 32 62 79 74 65 28 26 64 61 74 61 5b 69 46 72 65 2byte(&data[iFre
d1c0: 65 2b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 e+2]);. i
d1d0: 6e 74 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 nt top = get2byt
d1e0: 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
d1f0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 . if( iFr
d200: 65 65 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ee2 ){.
d210: 20 61 73 73 65 72 74 28 20 69 46 72 65 65 2b 73 assert( iFree+s
d220: 7a 3c 3d 69 46 72 65 65 32 20 29 3b 20 2f 2a 20 z<=iFree2 ); /*
d230: 56 65 72 69 66 69 65 64 20 62 79 20 70 61 67 65 Verified by page
d240: 46 69 6e 64 53 6c 6f 74 28 29 20 2a 2f 0a 20 20 FindSlot() */.
d250: 20 20 20 20 20 20 20 20 73 7a 32 20 3d 20 67 65 sz2 = ge
d260: 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 46 72 t2byte(&data[iFr
d270: 65 65 32 2b 32 5d 29 3b 0a 20 20 20 20 20 20 20 ee2+2]);.
d280: 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 assert( iFree
d290: 2b 73 7a 2b 73 7a 32 2b 69 46 72 65 65 32 2d 28 +sz+sz2+iFree2-(
d2a0: 69 46 72 65 65 2b 73 7a 29 20 3c 3d 20 75 73 61 iFree+sz) <= usa
d2b0: 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 bleSize );.
d2c0: 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 26 64 61 memmove(&da
d2d0: 74 61 5b 69 46 72 65 65 2b 73 7a 2b 73 7a 32 5d ta[iFree+sz+sz2]
d2e0: 2c 20 26 64 61 74 61 5b 69 46 72 65 65 2b 73 7a , &data[iFree+sz
d2f0: 5d 2c 20 69 46 72 65 65 32 2d 28 69 46 72 65 65 ], iFree2-(iFree
d300: 2b 73 7a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 +sz));.
d310: 20 73 7a 20 2b 3d 20 73 7a 32 3b 0a 20 20 20 20 sz += sz2;.
d320: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 62 }. cb
d330: 72 6b 20 3d 20 74 6f 70 2b 73 7a 3b 0a 20 20 20 rk = top+sz;.
d340: 20 20 20 20 20 61 73 73 65 72 74 28 20 63 62 72 assert( cbr
d350: 6b 2b 28 69 46 72 65 65 2d 74 6f 70 29 20 3c 3d k+(iFree-top) <=
d360: 20 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 usableSize );.
d370: 20 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 26 memmove(&
d380: 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 64 61 74 data[cbrk], &dat
d390: 61 5b 74 6f 70 5d 2c 20 69 46 72 65 65 2d 74 6f a[top], iFree-to
d3a0: 70 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 p);. for(
d3b0: 70 41 64 64 72 3d 26 64 61 74 61 5b 63 65 6c 6c pAddr=&data[cell
d3c0: 4f 66 66 73 65 74 5d 3b 20 70 41 64 64 72 3c 70 Offset]; pAddr<p
d3d0: 45 6e 64 3b 20 70 41 64 64 72 2b 3d 32 29 7b 0a End; pAddr+=2){.
d3e0: 20 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 pc = g
d3f0: 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a et2byte(pAddr);.
d400: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 63 if( pc
d410: 3c 69 46 72 65 65 20 29 7b 20 70 75 74 32 62 79 <iFree ){ put2by
d420: 74 65 28 70 41 64 64 72 2c 20 70 63 2b 73 7a 29 te(pAddr, pc+sz)
d430: 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 65 6c ; }. el
d440: 73 65 20 69 66 28 20 70 63 3c 69 46 72 65 65 32 se if( pc<iFree2
d450: 20 29 7b 20 70 75 74 32 62 79 74 65 28 70 41 64 ){ put2byte(pAd
d460: 64 72 2c 20 70 63 2b 73 7a 32 29 3b 20 7d 0a 20 dr, pc+sz2); }.
d470: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
d480: 20 67 6f 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 goto defragment
d490: 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 _out;. }.
d4a0: 20 20 7d 0a 20 20 7d 0a 0a 20 20 63 62 72 6b 20 }. }.. cbrk
d4b0: 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 = usableSize;.
d4c0: 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 iCellLast = usab
d4d0: 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f leSize - 4;. fo
d4e0: 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
d4f0: 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 i++){. u8 *pA
d500: 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ddr; /* The
d510: 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 i-th cell pointe
d520: 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d r */. pAddr =
d530: 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 &data[cellOffse
d540: 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 t + i*2];. pc
d550: 20 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 = get2byte(pAdd
d560: 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 r);. testcase
d570: 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 ( pc==iCellFirst
d580: 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
d590: 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 ( pc==iCellLast
d5a0: 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 );. /* These
d5b0: 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 conditions have
d5c0: 61 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 already been ver
d5d0: 69 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e ified in btreeIn
d5e0: 69 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 itPage(). **
d5f0: 69 66 20 50 52 41 47 4d 41 20 63 65 6c 6c 5f 73 if PRAGMA cell_s
d600: 69 7a 65 5f 63 68 65 63 6b 3d 4f 4e 2e 0a 20 20 ize_check=ON..
d610: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 3c */. if( pc<
d620: 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 iCellFirst || pc
d630: 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 >iCellLast ){.
d640: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d650: 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 E_CORRUPT_PGNO(p
d660: 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 Page->pgno);.
d670: 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
d680: 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 26 26 c>=iCellFirst &&
d690: 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 pc<=iCellLast )
d6a0: 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 70 50 61 ;. size = pPa
d6b0: 67 65 2d 3e 78 43 65 6c 6c 53 69 7a 65 28 70 50 ge->xCellSize(pP
d6c0: 61 67 65 2c 20 26 73 72 63 5b 70 63 5d 29 3b 0a age, &src[pc]);.
d6d0: 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 cbrk -= size
d6e0: 3b 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 ;. if( cbrk<i
d6f0: 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 2b CellFirst || pc+
d700: 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 size>usableSize
d710: 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
d720: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
d730: 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 GNO(pPage->pgno)
d740: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
d750: 72 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 rt( cbrk+size<=u
d760: 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 sableSize && cbr
d770: 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b k>=iCellFirst );
d780: 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 63 . testcase( c
d790: 62 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 brk+size==usable
d7a0: 53 69 7a 65 20 29 3b 0a 20 20 20 20 74 65 73 74 Size );. test
d7b0: 63 61 73 65 28 20 70 63 2b 73 69 7a 65 3d 3d 75 case( pc+size==u
d7c0: 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 sableSize );.
d7d0: 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c put2byte(pAddr,
d7e0: 20 63 62 72 6b 29 3b 0a 20 20 20 20 69 66 28 20 cbrk);. if(
d7f0: 74 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 temp==0 ){.
d800: 20 69 6e 74 20 78 3b 0a 20 20 20 20 20 20 69 66 int x;. if
d810: 28 20 63 62 72 6b 3d 3d 70 63 20 29 20 63 6f 6e ( cbrk==pc ) con
d820: 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 74 65 6d tinue;. tem
d830: 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 p = sqlite3Pager
d840: 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d TempSpace(pPage-
d850: 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 >pBt->pPager);.
d860: 20 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 x = get2byt
d870: 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
d880: 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 74 . memcpy(&t
d890: 65 6d 70 5b 78 5d 2c 20 26 64 61 74 61 5b 78 5d emp[x], &data[x]
d8a0: 2c 20 28 63 62 72 6b 2b 73 69 7a 65 29 20 2d 20 , (cbrk+size) -
d8b0: 78 29 3b 0a 20 20 20 20 20 20 73 72 63 20 3d 20 x);. src =
d8c0: 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 temp;. }.
d8d0: 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62 72 memcpy(&data[cbr
d8e0: 6b 5d 2c 20 26 73 72 63 5b 70 63 5d 2c 20 73 69 k], &src[pc], si
d8f0: 7a 65 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 5b ze);. }. data[
d900: 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 0a 20 64 65 hdr+7] = 0;.. de
d910: 66 72 61 67 6d 65 6e 74 5f 6f 75 74 3a 0a 20 20 fragment_out:.
d920: 69 66 28 20 64 61 74 61 5b 68 64 72 2b 37 5d 2b if( data[hdr+7]+
d930: 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 cbrk-iCellFirst!
d940: 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b =pPage->nFree ){
d950: 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d960: 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 TE_CORRUPT_PGNO(
d970: 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pPage->pgno);.
d980: 7d 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 6b }. assert( cbrk
d990: 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a >=iCellFirst );.
d9a0: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
d9b0: 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a [hdr+5], cbrk);.
d9c0: 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 data[hdr+1] =
d9d0: 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 0;. data[hdr+2]
d9e0: 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 = 0;. memset(&
d9f0: 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d data[iCellFirst]
da00: 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46 , 0, cbrk-iCellF
da10: 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72 74 28 irst);. assert(
da20: 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
da30: 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
da40: 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 72 65 pDbPage) );. re
da50: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
da60: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 }../*.** Search
da70: 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6f 6e the free-list on
da80: 20 70 61 67 65 20 70 50 67 20 66 6f 72 20 73 70 page pPg for sp
da90: 61 63 65 20 74 6f 20 73 74 6f 72 65 20 61 20 63 ace to store a c
daa0: 65 6c 6c 20 6e 42 79 74 65 20 62 79 74 65 73 20 ell nByte bytes
dab0: 69 6e 0a 2a 2a 20 73 69 7a 65 2e 20 49 66 20 6f in.** size. If o
dac0: 6e 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c ne can be found,
dad0: 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 return a pointe
dae0: 72 20 74 6f 20 74 68 65 20 73 70 61 63 65 20 61 r to the space a
daf0: 6e 64 20 72 65 6d 6f 76 65 20 69 74 0a 2a 2a 20 nd remove it.**
db00: 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
db10: 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 st..**.** If no
db20: 73 75 69 74 61 62 6c 65 20 73 70 61 63 65 20 63 suitable space c
db30: 61 6e 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 74 an be found on t
db40: 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 72 65 he free-list, re
db50: 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a turn NULL..**.**
db60: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
db70: 61 79 20 64 65 74 65 63 74 20 63 6f 72 72 75 70 ay detect corrup
db80: 74 69 6f 6e 20 77 69 74 68 69 6e 20 70 50 67 2e tion within pPg.
db90: 20 20 49 66 20 63 6f 72 72 75 70 74 69 6f 6e 20 If corruption
dba0: 69 73 0a 2a 2a 20 64 65 74 65 63 74 65 64 20 74 is.** detected t
dbb0: 68 65 6e 20 2a 70 52 63 20 69 73 20 73 65 74 20 hen *pRc is set
dbc0: 74 6f 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 to SQLITE_CORRUP
dbd0: 54 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 T and NULL is re
dbe0: 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 6c turned..**.** Sl
dbf0: 6f 74 73 20 6f 6e 20 74 68 65 20 66 72 65 65 20 ots on the free
dc00: 6c 69 73 74 20 74 68 61 74 20 61 72 65 20 62 65 list that are be
dc10: 74 77 65 65 6e 20 31 20 61 6e 64 20 33 20 62 79 tween 1 and 3 by
dc20: 74 65 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 tes larger than
dc30: 6e 42 79 74 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 nByte.** will be
dc40: 20 69 67 6e 6f 72 65 64 20 69 66 20 61 64 64 69 ignored if addi
dc50: 6e 67 20 74 68 65 20 65 78 74 72 61 20 73 70 61 ng the extra spa
dc60: 63 65 20 74 6f 20 74 68 65 20 66 72 61 67 6d 65 ce to the fragme
dc70: 6e 74 61 74 69 6f 6e 20 63 6f 75 6e 74 0a 2a 2a ntation count.**
dc80: 20 63 61 75 73 65 73 20 74 68 65 20 66 72 61 67 causes the frag
dc90: 6d 65 6e 74 61 74 69 6f 6e 20 63 6f 75 6e 74 20 mentation count
dca0: 74 6f 20 65 78 63 65 65 64 20 36 30 2e 0a 2a 2f to exceed 60..*/
dcb0: 0a 73 74 61 74 69 63 20 75 38 20 2a 70 61 67 65 .static u8 *page
dcc0: 46 69 6e 64 53 6c 6f 74 28 4d 65 6d 50 61 67 65 FindSlot(MemPage
dcd0: 20 2a 70 50 67 2c 20 69 6e 74 20 6e 42 79 74 65 *pPg, int nByte
dce0: 2c 20 69 6e 74 20 2a 70 52 63 29 7b 0a 20 20 63 , int *pRc){. c
dcf0: 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 onst int hdr = p
dd00: 50 67 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 Pg->hdrOffset;.
dd10: 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 44 61 74 u8 * const aDat
dd20: 61 20 3d 20 70 50 67 2d 3e 61 44 61 74 61 3b 0a a = pPg->aData;.
dd30: 20 20 69 6e 74 20 69 41 64 64 72 20 3d 20 68 64 int iAddr = hd
dd40: 72 20 2b 20 31 3b 0a 20 20 69 6e 74 20 70 63 20 r + 1;. int pc
dd50: 3d 20 67 65 74 32 62 79 74 65 28 26 61 44 61 74 = get2byte(&aDat
dd60: 61 5b 69 41 64 64 72 5d 29 3b 0a 20 20 69 6e 74 a[iAddr]);. int
dd70: 20 78 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 x;. int usable
dd80: 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 42 74 2d Size = pPg->pBt-
dd90: 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 >usableSize;. i
dda0: 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 nt size;
ddb0: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
ddc0: 68 65 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a he free slot */.
ddd0: 0a 20 20 61 73 73 65 72 74 28 20 70 63 3e 30 20 . assert( pc>0
dde0: 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 3c 3d );. while( pc<=
ddf0: 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0a usableSize-4 ){.
de00: 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
de10: 4f 46 3a 20 52 2d 32 32 37 31 30 2d 35 33 33 32 OF: R-22710-5332
de20: 38 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 8 The third and
de30: 66 6f 75 72 74 68 20 62 79 74 65 73 20 6f 66 20 fourth bytes of
de40: 65 61 63 68 0a 20 20 20 20 2a 2a 20 66 72 65 65 each. ** free
de50: 62 6c 6f 63 6b 20 66 6f 72 6d 20 61 20 62 69 67 block form a big
de60: 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
de70: 77 68 69 63 68 20 69 73 20 74 68 65 20 73 69 7a which is the siz
de80: 65 20 6f 66 20 74 68 65 20 66 72 65 65 62 6c 6f e of the freeblo
de90: 63 6b 0a 20 20 20 20 2a 2a 20 69 6e 20 62 79 74 ck. ** in byt
dea0: 65 73 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 es, including th
deb0: 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72 2e e 4-byte header.
dec0: 20 2a 2f 0a 20 20 20 20 73 69 7a 65 20 3d 20 67 */. size = g
ded0: 65 74 32 62 79 74 65 28 26 61 44 61 74 61 5b 70 et2byte(&aData[p
dee0: 63 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 c+2]);. if( (
def0: 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 x = size - nByte
df00: 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 65 )>=0 ){. te
df10: 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a stcase( x==4 );.
df20: 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
df30: 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 69 66 x==3 );. if
df40: 28 20 73 69 7a 65 2b 70 63 20 3e 20 75 73 61 62 ( size+pc > usab
df50: 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
df60: 20 20 2a 70 52 63 20 3d 20 53 51 4c 49 54 45 5f *pRc = SQLITE_
df70: 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 50 67 CORRUPT_PGNO(pPg
df80: 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
df90: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
dfa0: 20 7d 65 6c 73 65 20 69 66 28 20 78 3c 34 20 29 }else if( x<4 )
dfb0: 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 56 49 {. /* EVI
dfc0: 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 31 34 39 DENCE-OF: R-1149
dfd0: 38 2d 35 38 30 32 32 20 49 6e 20 61 20 77 65 6c 8-58022 In a wel
dfe0: 6c 2d 66 6f 72 6d 65 64 20 62 2d 74 72 65 65 20 l-formed b-tree
dff0: 70 61 67 65 2c 20 74 68 65 20 74 6f 74 61 6c 0a page, the total.
e000: 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ** numbe
e010: 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 66 72 r of bytes in fr
e020: 61 67 6d 65 6e 74 73 20 6d 61 79 20 6e 6f 74 20 agments may not
e030: 65 78 63 65 65 64 20 36 30 2e 20 2a 2f 0a 20 20 exceed 60. */.
e040: 20 20 20 20 20 20 69 66 28 20 61 44 61 74 61 5b if( aData[
e050: 68 64 72 2b 37 5d 3e 35 37 20 29 20 72 65 74 75 hdr+7]>57 ) retu
e060: 72 6e 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f rn 0;.. /
e070: 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c 6f * Remove the slo
e080: 74 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d t from the free-
e090: 6c 69 73 74 2e 20 55 70 64 61 74 65 20 74 68 65 list. Update the
e0a0: 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 number of.
e0b0: 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e 74 65 64 ** fragmented
e0c0: 20 62 79 74 65 73 20 77 69 74 68 69 6e 20 74 68 bytes within th
e0d0: 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 e page. */.
e0e0: 20 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 memcpy(&aData
e0f0: 5b 69 41 64 64 72 5d 2c 20 26 61 44 61 74 61 5b [iAddr], &aData[
e100: 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 pc], 2);.
e110: 20 61 44 61 74 61 5b 68 64 72 2b 37 5d 20 2b 3d aData[hdr+7] +=
e120: 20 28 75 38 29 78 3b 0a 20 20 20 20 20 20 7d 65 (u8)x;. }e
e130: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
e140: 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73 The slot remains
e150: 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
e160: 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 73 69 t. Reduce its si
e170: 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20 ze to account.
e180: 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 ** for th
e190: 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 62 e portion used b
e1a0: 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 y the new alloca
e1b0: 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 tion. */.
e1c0: 20 70 75 74 32 62 79 74 65 28 26 61 44 61 74 61 put2byte(&aData
e1d0: 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 [pc+2], x);.
e1e0: 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
e1f0: 20 26 61 44 61 74 61 5b 70 63 20 2b 20 78 5d 3b &aData[pc + x];
e200: 0a 20 20 20 20 7d 0a 20 20 20 20 69 41 64 64 72 . }. iAddr
e210: 20 3d 20 70 63 3b 0a 20 20 20 20 70 63 20 3d 20 = pc;. pc =
e220: 67 65 74 32 62 79 74 65 28 26 61 44 61 74 61 5b get2byte(&aData[
e230: 70 63 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 63 pc]);. if( pc
e240: 3c 69 41 64 64 72 2b 73 69 7a 65 20 29 20 62 72 <iAddr+size ) br
e250: 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 eak;. }. if( p
e260: 63 20 29 7b 0a 20 20 20 20 2a 70 52 63 20 3d 20 c ){. *pRc =
e270: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
e280: 47 4e 4f 28 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a GNO(pPg->pgno);.
e290: 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b }.. return 0;
e2a0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
e2b0: 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f te nByte bytes o
e2c0: 66 20 73 70 61 63 65 20 66 72 6f 6d 20 77 69 74 f space from wit
e2d0: 68 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20 70 hin the B-Tree p
e2e0: 61 67 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 age passed.** as
e2f0: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
e300: 65 6e 74 2e 20 57 72 69 74 65 20 69 6e 74 6f 20 ent. Write into
e310: 2a 70 49 64 78 20 74 68 65 20 69 6e 64 65 78 20 *pIdx the index
e320: 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 into pPage->aDat
e330: 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 a[].** of the fi
e340: 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f rst byte of allo
e350: 63 61 74 65 64 20 73 70 61 63 65 2e 20 52 65 74 cated space. Ret
e360: 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54 urn either SQLIT
e370: 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 E_OK or.** an er
e380: 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c ror code (usuall
e390: 79 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 y SQLITE_CORRUPT
e3a0: 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c )..**.** The cal
e3b0: 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 ler guarantees t
e3c0: 68 61 74 20 74 68 65 72 65 20 69 73 20 73 75 66 hat there is suf
e3d0: 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 74 6f ficient space to
e3e0: 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 61 6c 6c make the.** all
e3f0: 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 72 ocation. This r
e400: 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65 65 outine might nee
e410: 64 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 d to defragment
e420: 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69 6e in order to brin
e430: 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70 61 g.** all the spa
e440: 63 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f 77 ce together, how
e450: 65 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75 74 ever. This rout
e460: 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20 75 ine will avoid u
e470: 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 72 73 sing.** the firs
e480: 74 20 74 77 6f 20 62 79 74 65 73 20 70 61 73 74 t two bytes past
e490: 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 the cell pointe
e4a0: 72 20 61 72 65 61 20 73 69 6e 63 65 20 70 72 65 r area since pre
e4b0: 73 75 6d 61 62 6c 79 20 74 68 69 73 0a 2a 2a 20 sumably this.**
e4c0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 62 65 allocation is be
e4d0: 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72 64 65 ing made in orde
e4e0: 72 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 65 r to insert a ne
e4f0: 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20 77 69 w cell, so we wi
e500: 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e 64 20 75 ll.** also end u
e510: 70 20 6e 65 65 64 69 6e 67 20 61 20 6e 65 77 20 p needing a new
e520: 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f cell pointer..*/
e530: 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f .static int allo
e540: 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 cateSpace(MemPag
e550: 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 e *pPage, int nB
e560: 79 74 65 2c 20 69 6e 74 20 2a 70 49 64 78 29 7b yte, int *pIdx){
e570: 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 . const int hdr
e580: 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
e590: 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c set; /* Local
e5a0: 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d cache of pPage-
e5b0: 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f 0a 20 20 >hdrOffset */.
e5c0: 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20 u8 * const data
e5d0: 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20 = pPage->aData;
e5e0: 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 /* Local ca
e5f0: 63 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 che of pPage->aD
e600: 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 ata */. int top
e610: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
e620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e630: 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 63 First byte of c
e640: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
e650: 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 */. int rc = S
e660: 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
e670: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
e680: 74 65 67 65 72 20 72 65 74 75 72 6e 20 63 6f 64 teger return cod
e690: 65 20 2a 2f 0a 20 20 69 6e 74 20 67 61 70 3b 20 e */. int gap;
e6a0: 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
e6b0: 62 79 74 65 20 6f 66 20 67 61 70 20 62 65 74 77 byte of gap betw
e6c0: 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 een cell pointer
e6d0: 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f 6e 74 65 s and cell conte
e6e0: 6e 74 20 2a 2f 0a 20 20 0a 20 20 61 73 73 65 72 nt */. . asser
e6f0: 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
e700: 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
e710: 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
e720: 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
e730: 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Bt );. assert(
e740: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
e750: 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
e760: 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
e770: 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20 20 t( nByte>=0 );
e780: 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 /* Minimum cell
e790: 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 61 size is 4 */. a
e7a0: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 ssert( pPage->nF
e7b0: 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 20 ree>=nByte );.
e7c0: 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
e7d0: 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 Overflow==0 );.
e7e0: 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 20 3c assert( nByte <
e7f0: 20 28 69 6e 74 29 28 70 50 61 67 65 2d 3e 70 42 (int)(pPage->pB
e800: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 29 t->usableSize-8)
e810: 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
e820: 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
e830: 20 3d 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 == hdr + 12 - 4
e840: 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a *pPage->leaf );.
e850: 20 20 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 gap = pPage->c
e860: 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 ellOffset + 2*pP
e870: 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 age->nCell;. as
e880: 73 65 72 74 28 20 67 61 70 3c 3d 36 35 35 33 36 sert( gap<=65536
e890: 20 29 3b 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 );. /* EVIDENC
e8a0: 45 2d 4f 46 3a 20 52 2d 32 39 33 35 36 2d 30 32 E-OF: R-29356-02
e8b0: 33 39 31 20 49 66 20 74 68 65 20 64 61 74 61 62 391 If the datab
e8c0: 61 73 65 20 75 73 65 73 20 61 20 36 35 35 33 36 ase uses a 65536
e8d0: 2d 62 79 74 65 20 70 61 67 65 20 73 69 7a 65 0a -byte page size.
e8e0: 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 73 ** and the res
e8f0: 65 72 76 65 64 20 73 70 61 63 65 20 69 73 20 7a erved space is z
e900: 65 72 6f 20 28 74 68 65 20 75 73 75 61 6c 20 76 ero (the usual v
e910: 61 6c 75 65 20 66 6f 72 20 72 65 73 65 72 76 65 alue for reserve
e920: 64 20 73 70 61 63 65 29 0a 20 20 2a 2a 20 74 68 d space). ** th
e930: 65 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 en the cell cont
e940: 65 6e 74 20 6f 66 66 73 65 74 20 6f 66 20 61 6e ent offset of an
e950: 20 65 6d 70 74 79 20 70 61 67 65 20 77 61 6e 74 empty page want
e960: 73 20 74 6f 20 62 65 20 36 35 35 33 36 2e 0a 20 s to be 65536..
e970: 20 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 61 ** However, tha
e980: 74 20 69 6e 74 65 67 65 72 20 69 73 20 74 6f 6f t integer is too
e990: 20 6c 61 72 67 65 20 74 6f 20 62 65 20 73 74 6f large to be sto
e9a0: 72 65 64 20 69 6e 20 61 20 32 2d 62 79 74 65 20 red in a 2-byte
e9b0: 75 6e 73 69 67 6e 65 64 0a 20 20 2a 2a 20 69 6e unsigned. ** in
e9c0: 74 65 67 65 72 2c 20 73 6f 20 61 20 76 61 6c 75 teger, so a valu
e9d0: 65 20 6f 66 20 30 20 69 73 20 75 73 65 64 20 69 e of 0 is used i
e9e0: 6e 20 69 74 73 20 70 6c 61 63 65 2e 20 2a 2f 0a n its place. */.
e9f0: 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 top = get2byte
ea00: 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a (&data[hdr+5]);.
ea10: 20 20 61 73 73 65 72 74 28 20 74 6f 70 3c 3d 28 assert( top<=(
ea20: 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e int)pPage->pBt->
ea30: 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 2f 2a usableSize ); /*
ea40: 20 50 72 65 76 65 6e 74 20 62 79 20 67 65 74 41 Prevent by getA
ea50: 6e 64 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a ndInitPage() */.
ea60: 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 29 7b if( gap>top ){
ea70: 0a 20 20 20 20 69 66 28 20 74 6f 70 3d 3d 30 20 . if( top==0
ea80: 26 26 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 && pPage->pBt->u
ea90: 73 61 62 6c 65 53 69 7a 65 3d 3d 36 35 35 33 36 sableSize==65536
eaa0: 20 29 7b 0a 20 20 20 20 20 20 74 6f 70 20 3d 20 ){. top =
eab0: 36 35 35 33 36 3b 0a 20 20 20 20 7d 65 6c 73 65 65536;. }else
eac0: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
ead0: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 QLITE_CORRUPT_PG
eae0: 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b NO(pPage->pgno);
eaf0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
eb00: 20 49 66 20 74 68 65 72 65 20 69 73 20 65 6e 6f If there is eno
eb10: 75 67 68 20 73 70 61 63 65 20 62 65 74 77 65 65 ugh space betwee
eb20: 6e 20 67 61 70 20 61 6e 64 20 74 6f 70 20 66 6f n gap and top fo
eb30: 72 20 6f 6e 65 20 6d 6f 72 65 20 63 65 6c 6c 20 r one more cell
eb40: 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 61 72 72 pointer. ** arr
eb50: 61 79 20 65 6e 74 72 79 20 6f 66 66 73 65 74 2c ay entry offset,
eb60: 20 61 6e 64 20 69 66 20 74 68 65 20 66 72 65 65 and if the free
eb70: 6c 69 73 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 list is not empt
eb80: 79 2c 20 74 68 65 6e 20 73 65 61 72 63 68 20 74 y, then search t
eb90: 68 65 0a 20 20 2a 2a 20 66 72 65 65 6c 69 73 74 he. ** freelist
eba0: 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 looking for a f
ebb0: 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f ree slot big eno
ebc0: 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 74 ugh to satisfy t
ebd0: 68 65 20 72 65 71 75 65 73 74 2e 0a 20 20 2a 2f he request.. */
ebe0: 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 . testcase( gap
ebf0: 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 +2==top );. tes
ec00: 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d 74 6f tcase( gap+1==to
ec10: 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 p );. testcase(
ec20: 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 gap==top );. i
ec30: 66 28 20 28 64 61 74 61 5b 68 64 72 2b 32 5d 20 f( (data[hdr+2]
ec40: 7c 7c 20 64 61 74 61 5b 68 64 72 2b 31 5d 29 20 || data[hdr+1])
ec50: 26 26 20 67 61 70 2b 32 3c 3d 74 6f 70 20 29 7b && gap+2<=top ){
ec60: 0a 20 20 20 20 75 38 20 2a 70 53 70 61 63 65 20 . u8 *pSpace
ec70: 3d 20 70 61 67 65 46 69 6e 64 53 6c 6f 74 28 70 = pageFindSlot(p
ec80: 50 61 67 65 2c 20 6e 42 79 74 65 2c 20 26 72 63 Page, nByte, &rc
ec90: 29 3b 0a 20 20 20 20 69 66 28 20 70 53 70 61 63 );. if( pSpac
eca0: 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 e ){. asser
ecb0: 74 28 20 70 53 70 61 63 65 3e 3d 64 61 74 61 20 t( pSpace>=data
ecc0: 26 26 20 28 70 53 70 61 63 65 20 2d 20 64 61 74 && (pSpace - dat
ecd0: 61 29 3c 36 35 35 33 36 20 29 3b 0a 20 20 20 20 a)<65536 );.
ece0: 20 20 2a 70 49 64 78 20 3d 20 28 69 6e 74 29 28 *pIdx = (int)(
ecf0: 70 53 70 61 63 65 20 2d 20 64 61 74 61 29 3b 0a pSpace - data);.
ed00: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
ed10: 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 ITE_OK;. }els
ed20: 65 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 e if( rc ){.
ed30: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
ed40: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 }. }.. /* The
ed50: 20 72 65 71 75 65 73 74 20 63 6f 75 6c 64 20 6e request could n
ed60: 6f 74 20 62 65 20 66 75 6c 66 69 6c 6c 65 64 20 ot be fulfilled
ed70: 75 73 69 6e 67 20 61 20 66 72 65 65 6c 69 73 74 using a freelist
ed80: 20 73 6c 6f 74 2e 20 20 43 68 65 63 6b 0a 20 20 slot. Check.
ed90: 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 64 65 66 ** to see if def
eda0: 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 ragmentation is
edb0: 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a necessary.. */.
edc0: 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b testcase( gap+
edd0: 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29 3b 0a 2+nByte==top );.
ede0: 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42 79 74 if( gap+2+nByt
edf0: 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 61 73 73 e>top ){. ass
ee00: 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ert( pPage->nCel
ee10: 6c 3e 30 20 7c 7c 20 43 4f 52 52 55 50 54 5f 44 l>0 || CORRUPT_D
ee20: 42 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 65 B );. rc = de
ee30: 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 fragmentPage(pPa
ee40: 67 65 2c 20 4d 49 4e 28 34 2c 20 70 50 61 67 65 ge, MIN(4, pPage
ee50: 2d 3e 6e 46 72 65 65 20 2d 20 28 32 2b 6e 42 79 ->nFree - (2+nBy
ee60: 74 65 29 29 29 3b 0a 20 20 20 20 69 66 28 20 72 te)));. if( r
ee70: 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
ee80: 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 top = get2byt
ee90: 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61 5b 68 eNotZero(&data[h
eea0: 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 dr+5]);. asse
eeb0: 72 74 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3c rt( gap+2+nByte<
eec0: 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 =top );. }...
eed0: 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f /* Allocate memo
eee0: 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 ry from the gap
eef0: 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 in between the c
ef00: 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
ef10: 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 y. ** and the c
ef20: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
ef30: 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74 . The btreeInit
ef40: 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20 Page() call has
ef50: 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c already. ** val
ef60: 69 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c idated the freel
ef70: 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74 ist. Given that
ef80: 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 the freelist is
ef90: 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20 valid, there.
efa0: 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61 ** is no way tha
efb0: 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e t the allocation
efc0: 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20 can extend off
efd0: 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 the end of the p
efe0: 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73 age.. ** The as
eff0: 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72 sert() below ver
f000: 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f ifies the previo
f010: 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a us sentence.. *
f020: 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 /. top -= nByte
f030: 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
f040: 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b ta[hdr+5], top);
f050: 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e . assert( top+n
f060: 42 79 74 65 20 3c 3d 20 28 69 6e 74 29 70 50 61 Byte <= (int)pPa
f070: 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
f080: 69 7a 65 20 29 3b 0a 20 20 2a 70 49 64 78 20 3d ize );. *pIdx =
f090: 20 74 6f 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 top;. return S
f0a0: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
f0b0: 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 65 63 74 ** Return a sect
f0c0: 69 6f 6e 20 6f 66 20 74 68 65 20 70 50 61 67 65 ion of the pPage
f0d0: 2d 3e 61 44 61 74 61 20 74 6f 20 74 68 65 20 66 ->aData to the f
f0e0: 72 65 65 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20 reelist..** The
f0f0: 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
f100: 65 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b e new free block
f110: 20 69 73 20 70 50 61 67 65 2d 3e 61 44 61 74 61 is pPage->aData
f120: 5b 69 53 74 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 [iStart].** and
f130: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
f140: 62 6c 6f 63 6b 20 69 73 20 69 53 69 7a 65 20 62 block is iSize b
f150: 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 64 6a 61 ytes..**.** Adja
f160: 63 65 6e 74 20 66 72 65 65 62 6c 6f 63 6b 73 20 cent freeblocks
f170: 61 72 65 20 63 6f 61 6c 65 73 63 65 64 2e 0a 2a are coalesced..*
f180: 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 65 *.** Note that e
f190: 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 66 ven though the f
f1a0: 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77 61 reeblock list wa
f1b0: 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74 72 s checked by btr
f1c0: 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 2a 2a eeInitPage(),.**
f1d0: 20 74 68 61 74 20 72 6f 75 74 69 6e 65 20 77 69 that routine wi
f1e0: 6c 6c 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 ll not detect ov
f1f0: 65 72 6c 61 70 20 62 65 74 77 65 65 6e 20 63 65 erlap between ce
f200: 6c 6c 73 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b lls or freeblock
f210: 73 2e 20 20 4e 6f 72 0a 2a 2a 20 64 6f 65 73 20 s. Nor.** does
f220: 69 74 20 64 65 74 65 63 74 20 63 65 6c 6c 73 20 it detect cells
f230: 6f 72 20 66 72 65 65 62 6c 6f 63 6b 73 20 74 68 or freeblocks th
f240: 61 74 20 65 6e 63 72 6f 75 63 68 20 69 6e 74 6f at encrouch into
f250: 20 74 68 65 20 72 65 73 65 72 76 65 64 20 62 79 the reserved by
f260: 74 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e tes.** at the en
f270: 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 d of the page.
f280: 53 6f 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c So do additional
f290: 20 63 6f 72 72 75 70 74 69 6f 6e 20 63 68 65 63 corruption chec
f2a0: 6b 73 20 69 6e 73 69 64 65 20 74 68 69 73 0a 2a ks inside this.*
f2b0: 2a 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 72 65 * routine and re
f2c0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
f2d0: 55 50 54 20 69 66 20 61 6e 79 20 70 72 6f 62 6c UPT if any probl
f2e0: 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2e 0a 2a ems are found..*
f2f0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 /.static int fre
f300: 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a eSpace(MemPage *
f310: 70 50 61 67 65 2c 20 75 31 36 20 69 53 74 61 72 pPage, u16 iStar
f320: 74 2c 20 75 31 36 20 69 53 69 7a 65 29 7b 0a 20 t, u16 iSize){.
f330: 20 75 31 36 20 69 50 74 72 3b 20 20 20 20 20 20 u16 iPtr;
f340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f350: 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 /* Addres
f360: 73 20 6f 66 20 70 74 72 20 74 6f 20 6e 65 78 74 s of ptr to next
f370: 20 66 72 65 65 62 6c 6f 63 6b 20 2a 2f 0a 20 20 freeblock */.
f380: 75 31 36 20 69 46 72 65 65 42 6c 6b 3b 20 20 20 u16 iFreeBlk;
f390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3a0: 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
f3b0: 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65 of the next fre
f3c0: 65 62 6c 6f 63 6b 20 2a 2f 0a 20 20 75 38 20 68 eblock */. u8 h
f3d0: 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
f3e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3f0: 20 20 2f 2a 20 50 61 67 65 20 68 65 61 64 65 72 /* Page header
f400: 20 73 69 7a 65 2e 20 20 30 20 6f 72 20 31 30 30 size. 0 or 100
f410: 20 2a 2f 0a 20 20 75 38 20 6e 46 72 61 67 20 3d */. u8 nFrag =
f420: 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
f430: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
f440: 65 64 75 63 74 69 6f 6e 20 69 6e 20 66 72 61 67 eduction in frag
f450: 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 mentation */. u
f460: 31 36 20 69 4f 72 69 67 53 69 7a 65 20 3d 20 69 16 iOrigSize = i
f470: 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
f480: 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c /* Original
f490: 20 76 61 6c 75 65 20 6f 66 20 69 53 69 7a 65 20 value of iSize
f4a0: 2a 2f 0a 20 20 75 31 36 20 78 3b 20 20 20 20 20 */. u16 x;
f4b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4c0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
f4d0: 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e fset to cell con
f4e0: 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 75 tent area */. u
f4f0: 33 32 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 32 iEnd = iStart
f500: 20 2b 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20 + iSize;
f510: 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
f520: 74 65 20 70 61 73 74 20 74 68 65 20 69 53 74 61 te past the iSta
f530: 72 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 rt buffer */. u
f540: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 nsigned char *da
f550: 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
f560: 61 3b 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e a; /* Page con
f570: 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 tent */.. asser
f580: 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 t( pPage->pBt!=0
f590: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
f5a0: 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
f5b0: 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
f5c0: 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
f5d0: 74 28 20 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c t( CORRUPT_DB ||
f5e0: 20 69 53 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e iStart>=pPage->
f5f0: 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67 hdrOffset+6+pPag
f600: 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 e->childPtrSize
f610: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 4f 52 );. assert( COR
f620: 52 55 50 54 5f 44 42 20 7c 7c 20 69 45 6e 64 20 RUPT_DB || iEnd
f630: 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 <= pPage->pBt->u
f640: 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 61 sableSize );. a
f650: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
f660: 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
f670: 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
f680: 20 20 61 73 73 65 72 74 28 20 69 53 69 7a 65 3e assert( iSize>
f690: 3d 34 20 29 3b 20 20 20 2f 2a 20 4d 69 6e 69 6d =4 ); /* Minim
f6a0: 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
f6b0: 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 4 */. assert( i
f6c0: 53 74 61 72 74 3c 3d 70 50 61 67 65 2d 3e 70 42 Start<=pPage->pB
f6d0: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 t->usableSize-4
f6e0: 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 69 73 );.. /* The lis
f6f0: 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 20 t of freeblocks
f700: 6d 75 73 74 20 62 65 20 69 6e 20 61 73 63 65 6e must be in ascen
f710: 64 69 6e 67 20 6f 72 64 65 72 2e 20 20 46 69 6e ding order. Fin
f720: 64 20 74 68 65 20 0a 20 20 2a 2a 20 73 70 6f 74 d the . ** spot
f730: 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 77 68 65 on the list whe
f740: 72 65 20 69 53 74 61 72 74 20 73 68 6f 75 6c 64 re iStart should
f750: 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 20 20 be inserted..
f760: 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 */. hdr = pPage
f770: 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 69 ->hdrOffset;. i
f780: 50 74 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a 20 Ptr = hdr + 1;.
f790: 20 69 66 28 20 64 61 74 61 5b 69 50 74 72 2b 31 if( data[iPtr+1
f7a0: 5d 3d 3d 30 20 26 26 20 64 61 74 61 5b 69 50 74 ]==0 && data[iPt
f7b0: 72 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 69 46 72 r]==0 ){. iFr
f7c0: 65 65 42 6c 6b 20 3d 20 30 3b 20 20 2f 2a 20 53 eeBlk = 0; /* S
f7d0: 68 6f 72 74 63 75 74 20 66 6f 72 20 74 68 65 20 hortcut for the
f7e0: 63 61 73 65 20 77 68 65 6e 20 74 68 65 20 66 72 case when the fr
f7f0: 65 65 6c 69 73 74 20 69 73 20 65 6d 70 74 79 20 eelist is empty
f800: 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 */. }else{.
f810: 77 68 69 6c 65 28 20 28 69 46 72 65 65 42 6c 6b while( (iFreeBlk
f820: 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
f830: 61 5b 69 50 74 72 5d 29 29 3c 69 53 74 61 72 74 a[iPtr]))<iStart
f840: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 46 ){. if( iF
f850: 72 65 65 42 6c 6b 3c 69 50 74 72 2b 34 20 29 7b reeBlk<iPtr+4 ){
f860: 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 . if( iFr
f870: 65 65 42 6c 6b 3d 3d 30 20 29 20 62 72 65 61 6b eeBlk==0 ) break
f880: 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
f890: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
f8a0: 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f PGNO(pPage->pgno
f8b0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
f8c0: 20 69 50 74 72 20 3d 20 69 46 72 65 65 42 6c 6b iPtr = iFreeBlk
f8d0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
f8e0: 69 46 72 65 65 42 6c 6b 3e 70 50 61 67 65 2d 3e iFreeBlk>pPage->
f8f0: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
f900: 34 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 4 ){. retur
f910: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
f920: 5f 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e _PGNO(pPage->pgn
f930: 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 o);. }. as
f940: 73 65 72 74 28 20 69 46 72 65 65 42 6c 6b 3e 69 sert( iFreeBlk>i
f950: 50 74 72 20 7c 7c 20 69 46 72 65 65 42 6c 6b 3d Ptr || iFreeBlk=
f960: 3d 30 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 =0 );. . /*
f970: 41 74 20 74 68 69 73 20 70 6f 69 6e 74 3a 0a 20 At this point:.
f980: 20 20 20 2a 2a 20 20 20 20 69 46 72 65 65 42 6c ** iFreeBl
f990: 6b 3a 20 20 20 46 69 72 73 74 20 66 72 65 65 62 k: First freeb
f9a0: 6c 6f 63 6b 20 61 66 74 65 72 20 69 53 74 61 72 lock after iStar
f9b0: 74 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 6e 6f t, or zero if no
f9c0: 6e 65 0a 20 20 20 20 2a 2a 20 20 20 20 69 50 74 ne. ** iPt
f9d0: 72 3a 20 20 20 20 20 20 20 54 68 65 20 61 64 64 r: The add
f9e0: 72 65 73 73 20 6f 66 20 61 20 70 6f 69 6e 74 65 ress of a pointe
f9f0: 72 20 74 6f 20 69 46 72 65 65 42 6c 6b 0a 20 20 r to iFreeBlk.
fa00: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 68 65 63 **. ** Chec
fa10: 6b 20 74 6f 20 73 65 65 20 69 66 20 69 46 72 65 k to see if iFre
fa20: 65 42 6c 6b 20 73 68 6f 75 6c 64 20 62 65 20 63 eBlk should be c
fa30: 6f 61 6c 65 73 63 65 64 20 6f 6e 74 6f 20 74 68 oalesced onto th
fa40: 65 20 65 6e 64 20 6f 66 20 69 53 74 61 72 74 2e e end of iStart.
fa50: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
fa60: 69 46 72 65 65 42 6c 6b 20 26 26 20 69 45 6e 64 iFreeBlk && iEnd
fa70: 2b 33 3e 3d 69 46 72 65 65 42 6c 6b 20 29 7b 0a +3>=iFreeBlk ){.
fa80: 20 20 20 20 20 20 6e 46 72 61 67 20 3d 20 69 46 nFrag = iF
fa90: 72 65 65 42 6c 6b 20 2d 20 69 45 6e 64 3b 0a 20 reeBlk - iEnd;.
faa0: 20 20 20 20 20 69 66 28 20 69 45 6e 64 3e 69 46 if( iEnd>iF
fab0: 72 65 65 42 6c 6b 20 29 20 72 65 74 75 72 6e 20 reeBlk ) return
fac0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
fad0: 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 GNO(pPage->pgno)
fae0: 3b 0a 20 20 20 20 20 20 69 45 6e 64 20 3d 20 69 ;. iEnd = i
faf0: 46 72 65 65 42 6c 6b 20 2b 20 67 65 74 32 62 79 FreeBlk + get2by
fb00: 74 65 28 26 64 61 74 61 5b 69 46 72 65 65 42 6c te(&data[iFreeBl
fb10: 6b 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 k+2]);. if(
fb20: 20 69 45 6e 64 20 3e 20 70 50 61 67 65 2d 3e 70 iEnd > pPage->p
fb30: 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
fb40: 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
fb50: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
fb60: 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f PGNO(pPage->pgno
fb70: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
fb80: 20 69 53 69 7a 65 20 3d 20 69 45 6e 64 20 2d 20 iSize = iEnd -
fb90: 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 69 46 iStart;. iF
fba0: 72 65 65 42 6c 6b 20 3d 20 67 65 74 32 62 79 74 reeBlk = get2byt
fbb0: 65 28 26 64 61 74 61 5b 69 46 72 65 65 42 6c 6b e(&data[iFreeBlk
fbc0: 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 ]);. }. .
fbd0: 20 2f 2a 20 49 66 20 69 50 74 72 20 69 73 20 61 /* If iPtr is a
fbe0: 6e 6f 74 68 65 72 20 66 72 65 65 62 6c 6f 63 6b nother freeblock
fbf0: 20 28 74 68 61 74 20 69 73 2c 20 69 66 20 69 50 (that is, if iP
fc00: 74 72 20 69 73 20 6e 6f 74 20 74 68 65 20 66 72 tr is not the fr
fc10: 65 65 6c 69 73 74 0a 20 20 20 20 2a 2a 20 70 6f eelist. ** po
fc20: 69 6e 74 65 72 20 69 6e 20 74 68 65 20 70 61 67 inter in the pag
fc30: 65 20 68 65 61 64 65 72 29 20 74 68 65 6e 20 63 e header) then c
fc40: 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 heck to see if i
fc50: 53 74 61 72 74 20 73 68 6f 75 6c 64 20 62 65 0a Start should be.
fc60: 20 20 20 20 2a 2a 20 63 6f 61 6c 65 73 63 65 64 ** coalesced
fc70: 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 onto the end of
fc80: 20 69 50 74 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 iPtr.. */.
fc90: 20 20 69 66 28 20 69 50 74 72 3e 68 64 72 2b 31 if( iPtr>hdr+1
fca0: 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 50 ){. int iP
fcb0: 74 72 45 6e 64 20 3d 20 69 50 74 72 20 2b 20 67 trEnd = iPtr + g
fcc0: 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 50 et2byte(&data[iP
fcd0: 74 72 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 tr+2]);. if
fce0: 28 20 69 50 74 72 45 6e 64 2b 33 3e 3d 69 53 74 ( iPtrEnd+3>=iSt
fcf0: 61 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 art ){. i
fd00: 66 28 20 69 50 74 72 45 6e 64 3e 69 53 74 61 72 f( iPtrEnd>iStar
fd10: 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 t ) return SQLIT
fd20: 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 E_CORRUPT_PGNO(p
fd30: 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 Page->pgno);.
fd40: 20 20 20 20 20 6e 46 72 61 67 20 2b 3d 20 69 53 nFrag += iS
fd50: 74 61 72 74 20 2d 20 69 50 74 72 45 6e 64 3b 0a tart - iPtrEnd;.
fd60: 20 20 20 20 20 20 20 20 69 53 69 7a 65 20 3d 20 iSize =
fd70: 69 45 6e 64 20 2d 20 69 50 74 72 3b 0a 20 20 20 iEnd - iPtr;.
fd80: 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 69 50 iStart = iP
fd90: 74 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 tr;. }.
fda0: 7d 0a 20 20 20 20 69 66 28 20 6e 46 72 61 67 3e }. if( nFrag>
fdb0: 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 20 72 65 data[hdr+7] ) re
fdc0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
fdd0: 55 50 54 5f 50 47 4e 4f 28 70 50 61 67 65 2d 3e UPT_PGNO(pPage->
fde0: 70 67 6e 6f 29 3b 0a 20 20 20 20 64 61 74 61 5b pgno);. data[
fdf0: 68 64 72 2b 37 5d 20 2d 3d 20 6e 46 72 61 67 3b hdr+7] -= nFrag;
fe00: 0a 20 20 7d 0a 20 20 78 20 3d 20 67 65 74 32 62 . }. x = get2b
fe10: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
fe20: 29 3b 0a 20 20 69 66 28 20 69 53 74 61 72 74 3c );. if( iStart<
fe30: 3d 78 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =x ){. /* The
fe40: 20 6e 65 77 20 66 72 65 65 62 6c 6f 63 6b 20 69 new freeblock i
fe50: 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 s at the beginni
fe60: 6e 67 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 ng of the cell c
fe70: 6f 6e 74 65 6e 74 20 61 72 65 61 2c 0a 20 20 20 ontent area,.
fe80: 20 2a 2a 20 73 6f 20 6a 75 73 74 20 65 78 74 65 ** so just exte
fe90: 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 nd the cell cont
fea0: 65 6e 74 20 61 72 65 61 20 72 61 74 68 65 72 20 ent area rather
feb0: 74 68 61 6e 20 63 72 65 61 74 65 20 61 6e 6f 74 than create anot
fec0: 68 65 72 0a 20 20 20 20 2a 2a 20 66 72 65 65 6c her. ** freel
fed0: 69 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20 ist entry */.
fee0: 20 69 66 28 20 69 53 74 61 72 74 3c 78 20 7c 7c if( iStart<x ||
fef0: 20 69 50 74 72 21 3d 68 64 72 2b 31 20 29 20 72 iPtr!=hdr+1 ) r
ff00: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
ff10: 52 55 50 54 5f 50 47 4e 4f 28 70 50 61 67 65 2d RUPT_PGNO(pPage-
ff20: 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 32 >pgno);. put2
ff30: 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 byte(&data[hdr+1
ff40: 5d 2c 20 69 46 72 65 65 42 6c 6b 29 3b 0a 20 20 ], iFreeBlk);.
ff50: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
ff60: 5b 68 64 72 2b 35 5d 2c 20 69 45 6e 64 29 3b 0a [hdr+5], iEnd);.
ff70: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
ff80: 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 66 Insert the new f
ff90: 72 65 65 62 6c 6f 63 6b 20 69 6e 74 6f 20 74 68 reeblock into th
ffa0: 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 e freelist */.
ffb0: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
ffc0: 5b 69 50 74 72 5d 2c 20 69 53 74 61 72 74 29 3b [iPtr], iStart);
ffd0: 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 . }. if( pPage
ffe0: 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 ->pBt->btsFlags
fff0: 26 20 42 54 53 5f 46 41 53 54 5f 53 45 43 55 52 & BTS_FAST_SECUR
10000 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 76 65 72 E ){. /* Over
10010 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e write deleted in
10020 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a formation with z
10030 65 72 6f 73 20 77 68 65 6e 20 74 68 65 20 73 65 eros when the se
10040 63 75 72 65 5f 64 65 6c 65 74 65 0a 20 20 20 20 cure_delete.
10050 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 ** option is ena
10060 62 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 bled */. mems
10070 65 74 28 26 64 61 74 61 5b 69 53 74 61 72 74 5d et(&data[iStart]
10080 2c 20 30 2c 20 69 53 69 7a 65 29 3b 0a 20 20 7d , 0, iSize);. }
10090 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
100a0 61 5b 69 53 74 61 72 74 5d 2c 20 69 46 72 65 65 a[iStart], iFree
100b0 42 6c 6b 29 3b 0a 20 20 70 75 74 32 62 79 74 65 Blk);. put2byte
100c0 28 26 64 61 74 61 5b 69 53 74 61 72 74 2b 32 5d (&data[iStart+2]
100d0 2c 20 69 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 , iSize);. pPag
100e0 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 69 4f 72 69 e->nFree += iOri
100f0 67 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 gSize;. return
10100 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
10110 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20 66 .** Decode the f
10120 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 20 66 lags byte (the f
10130 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
10140 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 20 70 header) for a p
10150 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 age.** and initi
10160 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66 20 alize fields of
10170 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75 the MemPage stru
10180 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67 6c cture accordingl
10190 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 y..**.** Only th
101a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 e following comb
101b0 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75 70 inations are sup
101c0 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69 6e ported. Anythin
101d0 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 69 g different.** i
101e0 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 72 75 ndicates a corru
101f0 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
10200 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 s:.**.**
10210 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a 2a PTF_ZERODATA.**
10220 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 PTF_ZER
10230 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 46 ODATA | PTF_LEAF
10240 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
10250 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
10260 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20 20 NTKEY.**
10270 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 PTF_LEAFDATA |
10280 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54 46 PTF_INTKEY | PTF
10290 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63 20 _LEAF.*/.static
102a0 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73 28 int decodeFlags(
102b0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
102c0 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a 20 int flagByte){.
102d0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 BtShared *pBt;
102e0 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 /* A copy of
102f0 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a 0a pPage->pBt */..
10300 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
10310 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50 61 >hdrOffset==(pPa
10320 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 ge->pgno==1 ? 10
10330 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73 65 0 : 0) );. asse
10340 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10350 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
10360 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 t->mutex) );. p
10370 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 75 38 Page->leaf = (u8
10380 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 3b 20 )(flagByte>>3);
10390 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c 45 41 assert( PTF_LEA
103a0 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 20 66 F == 1<<3 );. f
103b0 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 46 5f lagByte &= ~PTF_
103c0 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d 3e 63 LEAF;. pPage->c
103d0 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 34 2d hildPtrSize = 4-
103e0 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 4*pPage->leaf;.
103f0 20 70 50 61 67 65 2d 3e 78 43 65 6c 6c 53 69 7a pPage->xCellSiz
10400 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 3b e = cellSizePtr;
10410 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e . pBt = pPage->
10420 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 pBt;. if( flagB
10430 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 yte==(PTF_LEAFDA
10440 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 TA | PTF_INTKEY)
10450 20 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 ){. /* EVIDE
10460 4e 43 45 2d 4f 46 3a 20 52 2d 30 37 32 39 31 2d NCE-OF: R-07291-
10470 33 35 33 32 38 20 41 20 76 61 6c 75 65 20 6f 66 35328 A value of
10480 20 35 20 28 30 78 30 35 29 20 6d 65 61 6e 73 20 5 (0x05) means
10490 74 68 65 20 70 61 67 65 20 69 73 20 61 6e 0a 20 the page is an.
104a0 20 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20 74 ** interior t
104b0 61 62 6c 65 20 62 2d 74 72 65 65 20 70 61 67 65 able b-tree page
104c0 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 . */. assert(
104d0 20 28 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 (PTF_LEAFDATA|P
104e0 54 46 5f 49 4e 54 4b 45 59 29 3d 3d 35 20 29 3b TF_INTKEY)==5 );
104f0 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 . /* EVIDENCE
10500 2d 4f 46 3a 20 52 2d 32 36 39 30 30 2d 30 39 31 -OF: R-26900-091
10510 37 36 20 41 20 76 61 6c 75 65 20 6f 66 20 31 33 76 A value of 13
10520 20 28 30 78 30 64 29 20 6d 65 61 6e 73 20 74 68 (0x0d) means th
10530 65 20 70 61 67 65 20 69 73 20 61 0a 20 20 20 20 e page is a.
10540 2a 2a 20 6c 65 61 66 20 74 61 62 6c 65 20 62 2d ** leaf table b-
10550 74 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 tree page. */.
10560 20 20 61 73 73 65 72 74 28 20 28 50 54 46 5f 4c assert( (PTF_L
10570 45 41 46 44 41 54 41 7c 50 54 46 5f 49 4e 54 4b EAFDATA|PTF_INTK
10580 45 59 7c 50 54 46 5f 4c 45 41 46 29 3d 3d 31 33 EY|PTF_LEAF)==13
10590 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 );. pPage->i
105a0 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 69 ntKey = 1;. i
105b0 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
105c0 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69 {. pPage->i
105d0 6e 74 4b 65 79 4c 65 61 66 20 3d 20 31 3b 0a 20 ntKeyLeaf = 1;.
105e0 20 20 20 20 20 70 50 61 67 65 2d 3e 78 50 61 72 pPage->xPar
105f0 73 65 43 65 6c 6c 20 3d 20 62 74 72 65 65 50 61 seCell = btreePa
10600 72 73 65 43 65 6c 6c 50 74 72 3b 0a 20 20 20 20 rseCellPtr;.
10610 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 }else{. pPa
10620 67 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 3d ge->intKeyLeaf =
10630 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 0;. pPage-
10640 3e 78 43 65 6c 6c 53 69 7a 65 20 3d 20 63 65 6c >xCellSize = cel
10650 6c 53 69 7a 65 50 74 72 4e 6f 50 61 79 6c 6f 61 lSizePtrNoPayloa
10660 64 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e d;. pPage->
10670 78 50 61 72 73 65 43 65 6c 6c 20 3d 20 62 74 72 xParseCell = btr
10680 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 4e 6f eeParseCellPtrNo
10690 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 7d 0a 20 Payload;. }.
106a0 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 pPage->maxLoc
106b0 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 al = pBt->maxLea
106c0 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 f;. pPage->mi
106d0 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 nLocal = pBt->mi
106e0 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 nLeaf;. }else i
106f0 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 f( flagByte==PTF
10700 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 _ZERODATA ){.
10710 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a /* EVIDENCE-OF:
10720 20 52 2d 34 33 33 31 36 2d 33 37 33 30 38 20 41 R-43316-37308 A
10730 20 76 61 6c 75 65 20 6f 66 20 32 20 28 30 78 30 value of 2 (0x0
10740 32 29 20 6d 65 61 6e 73 20 74 68 65 20 70 61 67 2) means the pag
10750 65 20 69 73 20 61 6e 0a 20 20 20 20 2a 2a 20 69 e is an. ** i
10760 6e 74 65 72 69 6f 72 20 69 6e 64 65 78 20 62 2d nterior index b-
10770 74 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 tree page. */.
10780 20 20 61 73 73 65 72 74 28 20 28 50 54 46 5f 5a assert( (PTF_Z
10790 45 52 4f 44 41 54 41 29 3d 3d 32 20 29 3b 0a 20 ERODATA)==2 );.
107a0 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f /* EVIDENCE-O
107b0 46 3a 20 52 2d 35 39 36 31 35 2d 34 32 38 32 38 F: R-59615-42828
107c0 20 41 20 76 61 6c 75 65 20 6f 66 20 31 30 20 28 A value of 10 (
107d0 30 78 30 61 29 20 6d 65 61 6e 73 20 74 68 65 20 0x0a) means the
107e0 70 61 67 65 20 69 73 20 61 0a 20 20 20 20 2a 2a page is a. **
107f0 20 6c 65 61 66 20 69 6e 64 65 78 20 62 2d 74 72 leaf index b-tr
10800 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 ee page. */.
10810 61 73 73 65 72 74 28 20 28 50 54 46 5f 5a 45 52 assert( (PTF_ZER
10820 4f 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 3d ODATA|PTF_LEAF)=
10830 3d 31 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 =10 );. pPage
10840 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 ->intKey = 0;.
10850 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 4c pPage->intKeyL
10860 65 61 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 eaf = 0;. pPa
10870 67 65 2d 3e 78 50 61 72 73 65 43 65 6c 6c 20 3d ge->xParseCell =
10880 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
10890 74 72 49 6e 64 65 78 3b 0a 20 20 20 20 70 50 61 trIndex;. pPa
108a0 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 ge->maxLocal = p
108b0 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 Bt->maxLocal;.
108c0 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 pPage->minLoca
108d0 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 l = pBt->minLoca
108e0 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 l;. }else{.
108f0 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 /* EVIDENCE-OF:
10900 52 2d 34 37 36 30 38 2d 35 36 34 36 39 20 41 6e R-47608-56469 An
10910 79 20 6f 74 68 65 72 20 76 61 6c 75 65 20 66 6f y other value fo
10920 72 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 r the b-tree pag
10930 65 20 74 79 70 65 20 69 73 0a 20 20 20 20 2a 2a e type is. **
10940 20 61 6e 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 an error. */.
10950 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10960 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 50 61 CORRUPT_PGNO(pPa
10970 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 ge->pgno);. }.
10980 20 70 50 61 67 65 2d 3e 6d 61 78 31 62 79 74 65 pPage->max1byte
10990 50 61 79 6c 6f 61 64 20 3d 20 70 42 74 2d 3e 6d Payload = pBt->m
109a0 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 3b 0a ax1bytePayload;.
109b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
109c0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 OK;.}../*.** Ini
109d0 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 69 tialize the auxi
109e0 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f liary informatio
109f0 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c 6f n for a disk blo
10a00 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ck..**.** Return
10a10 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
10a20 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 65 ccess. If we se
10a30 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
10a40 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 does.** not cont
10a50 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 ain a well-forme
10a60 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c d database page,
10a70 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a 2a then return .**
10a80 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e SQLITE_CORRUPT.
10a90 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 65 Note that a re
10aa0 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f 4f turn of SQLITE_O
10ab0 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 75 K does not.** gu
10ac0 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 arantee that the
10ad0 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 6f page is well-fo
10ae0 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 73 rmed. It only s
10af0 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 20 hows that.** we
10b00 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 74 failed to detect
10b10 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 2e any corruption.
10b20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
10b30 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d treeInitPage(Mem
10b40 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
10b50 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 int pc;
10b60 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
10b70 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 a freeblock wit
10b80 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 hin pPage->aData
10b90 5b 5d 20 2a 2f 0a 20 20 75 38 20 68 64 72 3b 20 [] */. u8 hdr;
10ba0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
10bb0 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e fset to beginnin
10bc0 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 g of page header
10bd0 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 */. u8 *data;
10be0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 /* Equa
10bf0 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 l to pPage->aDat
10c00 61 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 a */. BtShared
10c10 2a 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 *pBt; /*
10c20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 The main btree s
10c30 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e tructure */. in
10c40 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 t usableSize;
10c50 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 /* Amount of us
10c60 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 able space on ea
10c70 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 ch page */. u16
10c80 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 cellOffset;
10c90 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 73 /* Offset from s
10ca0 74 61 72 74 20 6f 66 20 70 61 67 65 20 74 6f 20 tart of page to
10cb0 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 first cell point
10cc0 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 65 er */. int nFre
10cd0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 e; /* Nu
10ce0 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 mber of unused b
10cf0 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 ytes on the page
10d00 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 */. int top;
10d10 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
10d20 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 t byte of the ce
10d30 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
10d40 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 */. int iCellFi
10d50 72 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 rst; /* First
10d60 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 allowable cell
10d70 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 or freeblock off
10d80 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 set */. int iCe
10d90 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c llLast; /* L
10da0 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c ast possible cel
10db0 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f l or freeblock o
10dc0 66 66 73 65 74 20 2a 2f 0a 0a 20 20 61 73 73 65 ffset */.. asse
10dd0 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d rt( pPage->pBt!=
10de0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
10df0 50 61 67 65 2d 3e 70 42 74 2d 3e 64 62 21 3d 30 Page->pBt->db!=0
10e00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
10e10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
10e20 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
10e30 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
10e40 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 pPage->pgno==sq
10e50 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 lite3PagerPagenu
10e60 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 mber(pPage->pDbP
10e70 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
10e80 28 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 ( pPage == sqlit
10e90 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
10ea0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
10eb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
10ec0 67 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c ge->aData == sql
10ed0 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
10ee0 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
10ef0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
10f00 61 67 65 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 29 age->isInit==0 )
10f10 3b 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 ;.. pBt = pPage
10f20 2d 3e 70 42 74 3b 0a 20 20 68 64 72 20 3d 20 70 ->pBt;. hdr = p
10f30 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
10f40 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
10f50 3e 61 44 61 74 61 3b 0a 20 20 2f 2a 20 45 56 49 >aData;. /* EVI
10f60 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 38 35 39 DENCE-OF: R-2859
10f70 34 2d 30 32 38 39 30 20 54 68 65 20 6f 6e 65 2d 4-02890 The one-
10f80 62 79 74 65 20 66 6c 61 67 20 61 74 20 6f 66 66 byte flag at off
10f90 73 65 74 20 30 20 69 6e 64 69 63 61 74 69 6e 67 set 0 indicating
10fa0 0a 20 20 2a 2a 20 74 68 65 20 62 2d 74 72 65 65 . ** the b-tree
10fb0 20 70 61 67 65 20 74 79 70 65 2e 20 2a 2f 0a 20 page type. */.
10fc0 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67 73 if( decodeFlags
10fd0 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64 72 (pPage, data[hdr
10fe0 5d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ]) ){. return
10ff0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
11000 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f PGNO(pPage->pgno
11010 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
11020 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d pBt->pageSize>=
11030 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 512 && pBt->page
11040 53 69 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 Size<=65536 );.
11050 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 pPage->maskPage
11060 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 70 61 = (u16)(pBt->pa
11070 67 65 53 69 7a 65 20 2d 20 31 29 3b 0a 20 20 70 geSize - 1);. p
11080 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
11090 3d 20 30 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a = 0;. usableSiz
110a0 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
110b0 69 7a 65 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 ize;. pPage->ce
110c0 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f llOffset = cellO
110d0 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 38 20 ffset = hdr + 8
110e0 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 + pPage->childPt
110f0 72 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 2d 3e rSize;. pPage->
11100 61 44 61 74 61 45 6e 64 20 3d 20 26 64 61 74 61 aDataEnd = &data
11110 5b 75 73 61 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 [usableSize];.
11120 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 20 pPage->aCellIdx
11130 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 = &data[cellOffs
11140 65 74 5d 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 et];. pPage->aD
11150 61 74 61 4f 66 73 74 20 3d 20 26 64 61 74 61 5b ataOfst = &data[
11160 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
11170 69 7a 65 5d 3b 0a 20 20 2f 2a 20 45 56 49 44 45 ize];. /* EVIDE
11180 4e 43 45 2d 4f 46 3a 20 52 2d 35 38 30 31 35 2d NCE-OF: R-58015-
11190 34 38 31 37 35 20 54 68 65 20 74 77 6f 2d 62 79 48175 The two-by
111a0 74 65 20 69 6e 74 65 67 65 72 20 61 74 20 6f 66 te integer at of
111b0 66 73 65 74 20 35 20 64 65 73 69 67 6e 61 74 65 fset 5 designate
111c0 73 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 72 74 s. ** the start
111d0 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e of the cell con
111e0 74 65 6e 74 20 61 72 65 61 2e 20 41 20 7a 65 72 tent area. A zer
111f0 6f 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 o value for this
11200 20 69 6e 74 65 67 65 72 20 69 73 0a 20 20 2a 2a integer is. **
11210 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
11220 36 35 35 33 36 2e 20 2a 2f 0a 20 20 74 6f 70 20 65536. */. top
11230 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 = get2byteNotZer
11240 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b o(&data[hdr+5]);
11250 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f . /* EVIDENCE-O
11260 46 3a 20 52 2d 33 37 30 30 32 2d 33 32 37 37 34 F: R-37002-32774
11270 20 54 68 65 20 74 77 6f 2d 62 79 74 65 20 69 6e The two-byte in
11280 74 65 67 65 72 20 61 74 20 6f 66 66 73 65 74 20 teger at offset
11290 33 20 67 69 76 65 73 20 74 68 65 0a 20 20 2a 2a 3 gives the. **
112a0 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 number of cells
112b0 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 2a 2f on the page. */
112c0 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 . pPage->nCell
112d0 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
112e0 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 69 66 28 20 [hdr+3]);. if(
112f0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f pPage->nCell>MX_
11300 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 CELL(pBt) ){.
11310 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c /* To many cell
11320 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 s for a single p
11330 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d age. The page m
11340 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a ust be corrupt *
11350 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
11360 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f ITE_CORRUPT_PGNO
11370 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 (pPage->pgno);.
11380 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 }. testcase( p
11390 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f Page->nCell==MX_
113a0 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 20 20 2f CELL(pBt) );. /
113b0 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 * EVIDENCE-OF: R
113c0 2d 32 34 30 38 39 2d 35 37 39 37 39 20 49 66 20 -24089-57979 If
113d0 61 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 a page contains
113e0 6e 6f 20 63 65 6c 6c 73 20 28 77 68 69 63 68 20 no cells (which
113f0 69 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 70 6f 73 is only. ** pos
11400 73 69 62 6c 65 20 66 6f 72 20 61 20 72 6f 6f 74 sible for a root
11410 20 70 61 67 65 20 6f 66 20 61 20 74 61 62 6c 65 page of a table
11420 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6e that contains n
11430 6f 20 72 6f 77 73 29 20 74 68 65 6e 20 74 68 65 o rows) then the
11440 0a 20 20 2a 2a 20 6f 66 66 73 65 74 20 74 6f 20 . ** offset to
11450 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
11460 20 61 72 65 61 20 77 69 6c 6c 20 65 71 75 61 6c area will equal
11470 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d the page size m
11480 69 6e 75 73 20 74 68 65 0a 20 20 2a 2a 20 62 79 inus the. ** by
11490 74 65 73 20 6f 66 20 72 65 73 65 72 76 65 64 20 tes of reserved
114a0 73 70 61 63 65 2e 20 2a 2f 0a 20 20 61 73 73 65 space. */. asse
114b0 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c rt( pPage->nCell
114c0 3e 30 20 7c 7c 20 74 6f 70 3d 3d 75 73 61 62 6c >0 || top==usabl
114d0 65 53 69 7a 65 20 7c 7c 20 43 4f 52 52 55 50 54 eSize || CORRUPT
114e0 5f 44 42 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 6d _DB );.. /* A m
114f0 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 alformed databas
11500 65 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 e page might cau
11510 73 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 se us to read pa
11520 73 74 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 st the end. **
11530 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61 72 of page when par
11540 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a 20 sing a cell. .
11550 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c **. ** The fol
11560 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 lowing block of
11570 63 6f 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c code checks earl
11580 79 20 74 6f 20 73 65 65 20 69 66 20 61 20 63 65 y to see if a ce
11590 6c 6c 20 65 78 74 65 6e 64 73 0a 20 20 2a 2a 20 ll extends. **
115a0 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
115b0 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 79 20 a page boundary
115c0 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c 49 54 and causes SQLIT
115d0 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62 65 20 E_CORRUPT to be
115e0 0a 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 . ** returned i
115f0 66 20 69 74 20 64 6f 65 73 2e 0a 20 20 2a 2f 0a f it does.. */.
11600 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 iCellFirst = c
11610 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 ellOffset + 2*pP
11620 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 43 age->nCell;. iC
11630 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 ellLast = usable
11640 53 69 7a 65 20 2d 20 34 3b 0a 20 20 69 66 28 20 Size - 4;. if(
11650 70 42 74 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 pBt->db->flags &
11660 20 53 51 4c 49 54 45 5f 43 65 6c 6c 53 69 7a 65 SQLITE_CellSize
11670 43 6b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b Ck ){. int i;
11680 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
11690 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 63 65 ndex into the ce
116a0 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
116b0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 3b 20 */. int sz;
116c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
116d0 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a e of a cell */..
116e0 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
116f0 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 leaf ) iCellLast
11700 2d 2d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b --;. for(i=0;
11710 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b i<pPage->nCell;
11720 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 63 20 i++){. pc
11730 3d 20 67 65 74 32 62 79 74 65 41 6c 69 67 6e 65 = get2byteAligne
11740 64 28 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 d(&data[cellOffs
11750 65 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 et+i*2]);.
11760 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 testcase( pc==iC
11770 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ellFirst );.
11780 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d testcase( pc==
11790 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 iCellLast );.
117a0 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 if( pc<iCellF
117b0 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c irst || pc>iCell
117c0 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Last ){.
117d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
117e0 52 52 55 50 54 5f 50 47 4e 4f 28 70 50 61 67 65 RRUPT_PGNO(pPage
117f0 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 7d ->pgno);. }
11800 0a 20 20 20 20 20 20 73 7a 20 3d 20 70 50 61 67 . sz = pPag
11810 65 2d 3e 78 43 65 6c 6c 53 69 7a 65 28 70 50 61 e->xCellSize(pPa
11820 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a ge, &data[pc]);.
11830 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
11840 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 69 7a pc+sz==usableSiz
11850 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 e );. if( p
11860 63 2b 73 7a 3e 75 73 61 62 6c 65 53 69 7a 65 20 c+sz>usableSize
11870 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
11880 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
11890 5f 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e _PGNO(pPage->pgn
118a0 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 o);. }.
118b0 7d 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 }. if( !pPage
118c0 2d 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 ->leaf ) iCellLa
118d0 73 74 2b 2b 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f st++;. } .. /
118e0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f * Compute the to
118f0 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f tal free space o
11900 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 n the page. **
11910 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 EVIDENCE-OF: R-2
11920 33 35 38 38 2d 33 34 34 35 30 20 54 68 65 20 74 3588-34450 The t
11930 77 6f 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 wo-byte integer
11940 61 74 20 6f 66 66 73 65 74 20 31 20 67 69 76 65 at offset 1 give
11950 73 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 72 74 s the. ** start
11960 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 66 72 of the first fr
11970 65 65 62 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 eeblock on the p
11980 61 67 65 2c 20 6f 72 20 69 73 20 7a 65 72 6f 20 age, or is zero
11990 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 0a if there are no.
119a0 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e ** freeblocks.
119b0 20 2a 2f 0a 20 20 70 63 20 3d 20 67 65 74 32 62 */. pc = get2b
119c0 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
119d0 29 3b 0a 20 20 6e 46 72 65 65 20 3d 20 64 61 74 );. nFree = dat
119e0 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 20 a[hdr+7] + top;
119f0 20 2f 2a 20 49 6e 69 74 20 6e 46 72 65 65 20 74 /* Init nFree t
11a00 6f 20 6e 6f 6e 2d 66 72 65 65 62 6c 6f 63 6b 20 o non-freeblock
11a10 66 72 65 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 free space */.
11a20 69 66 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 if( pc>0 ){.
11a30 75 33 32 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a u32 next, size;.
11a40 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c if( pc<iCell
11a50 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 2f First ){. /
11a60 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 * EVIDENCE-OF: R
11a70 2d 35 35 35 33 30 2d 35 32 39 33 30 20 49 6e 20 -55530-52930 In
11a80 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 62 2d a well-formed b-
11a90 74 72 65 65 20 70 61 67 65 2c 20 74 68 65 72 65 tree page, there
11aa0 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 will. ** a
11ab0 6c 77 61 79 73 20 62 65 20 61 74 20 6c 65 61 73 lways be at leas
11ac0 74 20 6f 6e 65 20 63 65 6c 6c 20 62 65 66 6f 72 t one cell befor
11ad0 65 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 e the first free
11ae0 62 6c 6f 63 6b 2e 0a 20 20 20 20 20 20 2a 2f 0a block.. */.
11af0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
11b00 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f ITE_CORRUPT_PGNO
11b10 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 20 0a (pPage->pgno); .
11b20 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 }. while(
11b30 20 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 1 ){. if(
11b40 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a pc>iCellLast ){.
11b50 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 62 /* Freeb
11b60 6c 6f 63 6b 20 6f 66 66 20 74 68 65 20 65 6e 64 lock off the end
11b70 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a of the page */.
11b80 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
11b90 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 QLITE_CORRUPT_PG
11ba0 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b NO(pPage->pgno);
11bb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e . }. n
11bc0 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 ext = get2byte(&
11bd0 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 data[pc]);.
11be0 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 size = get2byte
11bf0 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 (&data[pc+2]);.
11c00 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46 72 nFree = nFr
11c10 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 20 ee + size;.
11c20 20 69 66 28 20 6e 65 78 74 3c 3d 70 63 2b 73 69 if( next<=pc+si
11c30 7a 65 2b 33 20 29 20 62 72 65 61 6b 3b 0a 20 20 ze+3 ) break;.
11c40 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 pc = next;.
11c50 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 65 78 }. if( nex
11c60 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 t>0 ){. /*
11c70 46 72 65 65 62 6c 6f 63 6b 20 6e 6f 74 20 69 6e Freeblock not in
11c80 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
11c90 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
11ca0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
11cb0 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f PGNO(pPage->pgno
11cc0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
11cd0 20 70 63 2b 73 69 7a 65 3e 28 75 6e 73 69 67 6e pc+size>(unsign
11ce0 65 64 20 69 6e 74 29 75 73 61 62 6c 65 53 69 7a ed int)usableSiz
11cf0 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 61 e ){. /* La
11d00 73 74 20 66 72 65 65 62 6c 6f 63 6b 20 65 78 74 st freeblock ext
11d10 65 6e 64 73 20 70 61 73 74 20 70 61 67 65 20 65 ends past page e
11d20 6e 64 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 nd */. retu
11d30 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
11d40 54 5f 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 T_PGNO(pPage->pg
11d50 6e 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a no);. }. }..
11d60 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 /* At this poi
11d70 6e 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 69 nt, nFree contai
11d80 6e 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 ns the sum of th
11d90 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 e offset to the
11da0 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 start. ** of th
11db0 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 e cell-content a
11dc0 72 65 61 20 70 6c 75 73 20 74 68 65 20 6e 75 6d rea plus the num
11dd0 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 ber of free byte
11de0 73 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20 74 68 s within. ** th
11df0 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 e cell-content a
11e00 72 65 61 2e 20 49 66 20 74 68 69 73 20 69 73 20 rea. If this is
11e10 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
11e20 20 75 73 61 62 6c 65 2d 73 69 7a 65 0a 20 20 2a usable-size. *
11e30 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 74 * of the page, t
11e40 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 75 73 hen the page mus
11e50 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 t be corrupted.
11e60 54 68 69 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a This check also.
11e70 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 ** serves to v
11e80 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6f erify that the o
11e90 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 ffset to the sta
11ea0 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 rt of the cell-c
11eb0 6f 6e 74 65 6e 74 0a 20 20 2a 2a 20 61 72 65 61 ontent. ** area
11ec0 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 , according to t
11ed0 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20 he page header,
11ee0 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 lies within the
11ef0 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 page.. */. if(
11f00 20 6e 46 72 65 65 3e 75 73 61 62 6c 65 53 69 7a nFree>usableSiz
11f10 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
11f20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
11f30 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 GNO(pPage->pgno)
11f40 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e ;. }. pPage->n
11f50 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 Free = (u16)(nFr
11f60 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 ee - iCellFirst)
11f70 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 ;. pPage->isIni
11f80 74 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 t = 1;. return
11f90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
11fa0 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 .** Set up a raw
11fb0 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 page so that it
11fc0 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 looks like a da
11fd0 74 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 tabase page hold
11fe0 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 ing.** no entrie
11ff0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
12000 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 d zeroPage(MemPa
12010 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 ge *pPage, int f
12020 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 lags){. unsigne
12030 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 d char *data = p
12040 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 Page->aData;. B
12050 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
12060 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 Page->pBt;. u8
12070 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
12080 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 Offset;. u16 fi
12090 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rst;.. assert(
120a0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
120b0 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 number(pPage->pD
120c0 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 bPage)==pPage->p
120d0 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 gno );. assert(
120e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
120f0 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 Extra(pPage->pDb
12100 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 Page) == (void*)
12110 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 pPage );. asser
12120 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
12130 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 etData(pPage->pD
12140 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 bPage) == data )
12150 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
12160 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
12170 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
12180 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
12190 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
121a0 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
121b0 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 74 );. if( pBt->bt
121c0 73 46 6c 61 67 73 20 26 20 42 54 53 5f 46 41 53 sFlags & BTS_FAS
121d0 54 5f 53 45 43 55 52 45 20 29 7b 0a 20 20 20 20 T_SECURE ){.
121e0 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 memset(&data[hdr
121f0 5d 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c ], 0, pBt->usabl
12200 65 53 69 7a 65 20 2d 20 68 64 72 29 3b 0a 20 20 eSize - hdr);.
12210 7d 0a 20 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 }. data[hdr] =
12220 28 63 68 61 72 29 66 6c 61 67 73 3b 0a 20 20 66 (char)flags;. f
12230 69 72 73 74 20 3d 20 68 64 72 20 2b 20 28 28 66 irst = hdr + ((f
12240 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d lags&PTF_LEAF)==
12250 30 20 3f 20 31 32 20 3a 20 38 29 3b 0a 20 20 6d 0 ? 12 : 8);. m
12260 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 2b emset(&data[hdr+
12270 31 5d 2c 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 1], 0, 4);. dat
12280 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 a[hdr+7] = 0;.
12290 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
122a0 64 72 2b 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 dr+5], pBt->usab
122b0 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 leSize);. pPage
122c0 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 29 28 ->nFree = (u16)(
122d0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
122e0 2d 20 66 69 72 73 74 29 3b 0a 20 20 64 65 63 6f - first);. deco
122f0 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 deFlags(pPage, f
12300 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e lags);. pPage->
12310 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 cellOffset = fir
12320 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 st;. pPage->aDa
12330 74 61 45 6e 64 20 3d 20 26 64 61 74 61 5b 70 42 taEnd = &data[pB
12340 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 3b 0a t->usableSize];.
12350 20 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 pPage->aCellId
12360 78 20 3d 20 26 64 61 74 61 5b 66 69 72 73 74 5d x = &data[first]
12370 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 ;. pPage->aData
12380 4f 66 73 74 20 3d 20 26 64 61 74 61 5b 70 50 61 Ofst = &data[pPa
12390 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
123a0 5d 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ];. pPage->nOve
123b0 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 rflow = 0;. ass
123c0 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 ert( pBt->pageSi
123d0 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e ze>=512 && pBt->
123e0 70 61 67 65 53 69 7a 65 3c 3d 36 35 35 33 36 20 pageSize<=65536
123f0 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b );. pPage->mask
12400 50 61 67 65 20 3d 20 28 75 31 36 29 28 70 42 74 Page = (u16)(pBt
12410 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 29 3b ->pageSize - 1);
12420 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 . pPage->nCell
12430 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73 = 0;. pPage->is
12440 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a Init = 1;.}.../*
12450 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 44 62 .** Convert a Db
12460 50 61 67 65 20 6f 62 74 61 69 6e 65 64 20 66 72 Page obtained fr
12470 6f 6d 20 74 68 65 20 70 61 67 65 72 20 69 6e 74 om the pager int
12480 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 73 65 64 o a MemPage used
12490 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 72 65 65 by.** the btree
124a0 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 layer..*/.stati
124b0 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 c MemPage *btree
124c0 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 44 PageFromDbPage(D
124d0 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 bPage *pDbPage,
124e0 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 53 68 61 Pgno pgno, BtSha
124f0 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d red *pBt){. Mem
12500 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 28 4d Page *pPage = (M
12510 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 65 33 50 emPage*)sqlite3P
12520 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 62 agerGetExtra(pDb
12530 50 61 67 65 29 3b 0a 20 20 69 66 28 20 70 67 6e Page);. if( pgn
12540 6f 21 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 29 o!=pPage->pgno )
12550 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 44 61 {. pPage->aDa
12560 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ta = sqlite3Page
12570 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
12580 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 );. pPage->pD
12590 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 3b bPage = pDbPage;
125a0 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 42 74 20 . pPage->pBt
125b0 3d 20 70 42 74 3b 0a 20 20 20 20 70 50 61 67 65 = pBt;. pPage
125c0 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 ->pgno = pgno;.
125d0 20 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 pPage->hdrOff
125e0 73 65 74 20 3d 20 70 67 6e 6f 3d 3d 31 20 3f 20 set = pgno==1 ?
125f0 31 30 30 20 3a 20 30 3b 0a 20 20 7d 0a 20 20 61 100 : 0;. }. a
12600 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 ssert( pPage->aD
12610 61 74 61 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 ata==sqlite3Page
12620 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
12630 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 ) );. return pP
12640 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 age; .}../*.** G
12650 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 et a page from t
12660 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 he pager. Initi
12670 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 alize the MemPag
12680 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d e.pBt and.** Mem
12690 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 Page.aData eleme
126a0 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 20 20 nts if needed.
126b0 53 65 65 20 61 6c 73 6f 3a 20 62 74 72 65 65 47 See also: btreeG
126c0 65 74 55 6e 75 73 65 64 50 61 67 65 28 29 2e 0a etUnusedPage()..
126d0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 41 47 **.** If the PAG
126e0 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 ER_GET_NOCONTENT
126f0 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 flag is set, it
12700 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 means that we d
12710 6f 20 6e 6f 74 20 63 61 72 65 0a 2a 2a 20 61 62 o not care.** ab
12720 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 out the content
12730 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20 74 of the page at t
12740 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f his time. So do
12750 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 not go to the d
12760 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 isk.** to fetch
12770 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 the content. Ju
12780 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 st fill in the c
12790 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f ontent with zero
127a0 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 s for now..** If
127b0 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 in the future w
127c0 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 e call sqlite3Pa
127d0 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 gerWrite() on th
127e0 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a is page, that.**
127f0 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 means we have s
12800 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e tarted to be con
12810 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e cerned about con
12820 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 tent and the dis
12830 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 k.** read should
12840 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 20 70 occur at that p
12850 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 oint..*/.static
12860 69 6e 74 20 62 74 72 65 65 47 65 74 50 61 67 65 int btreeGetPage
12870 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
12880 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 t, /* The
12890 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 btree */. Pgno
128a0 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
128b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 /* Number of the
128c0 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a page to fetch *
128d0 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
128e0 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 Page, /* Retu
128f0 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 rn the page in t
12900 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f his parameter */
12910 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20 . int flags
12920 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 /* PAGER
12930 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 6f _GET_NOCONTENT o
12940 72 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 r PAGER_GET_READ
12950 4f 4e 4c 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ONLY */.){. int
12960 20 72 63 3b 0a 20 20 44 62 50 61 67 65 20 2a 70 rc;. DbPage *p
12970 44 62 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 DbPage;.. asser
12980 74 28 20 66 6c 61 67 73 3d 3d 30 20 7c 7c 20 66 t( flags==0 || f
12990 6c 61 67 73 3d 3d 50 41 47 45 52 5f 47 45 54 5f lags==PAGER_GET_
129a0 4e 4f 43 4f 4e 54 45 4e 54 20 7c 7c 20 66 6c 61 NOCONTENT || fla
129b0 67 73 3d 3d 50 41 47 45 52 5f 47 45 54 5f 52 45 gs==PAGER_GET_RE
129c0 41 44 4f 4e 4c 59 20 29 3b 0a 20 20 61 73 73 65 ADONLY );. asse
129d0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
129e0 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
129f0 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c x) );. rc = sql
12a00 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 ite3PagerGet(pBt
12a10 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 ->pPager, pgno,
12a20 28 44 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 (DbPage**)&pDbPa
12a30 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 69 66 ge, flags);. if
12a40 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
12a50 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 ;. *ppPage = 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 72 65 74 75 72 6e 20 pBt);. return
12a90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
12aa0 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 .** Retrieve a p
12ab0 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
12ac0 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 er cache. If the
12ad0 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
12ae0 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 is not.** alread
12af0 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 y in the pager c
12b00 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c ache return NULL
12b10 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 . Initialize the
12b20 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
12b30 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
12b40 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
12b50 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
12b60 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 MemPage *btreePa
12b70 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 geLookup(BtShare
12b80 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
12b90 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
12ba0 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 bPage;. assert(
12bb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
12bc0 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
12bd0 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 );. pDbPage = s
12be0 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
12bf0 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 p(pBt->pPager, p
12c00 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 gno);. if( pDbP
12c10 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 age ){. retur
12c20 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 n btreePageFromD
12c30 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 bPage(pDbPage, p
12c40 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 gno, pBt);. }.
12c50 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
12c60 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
12c70 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
12c80 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 ase file in page
12c90 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 s. If there is a
12ca0 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 ny kind of.** er
12cb0 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e ror, return ((un
12cc0 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a signed int)-1)..
12cd0 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 62 */.static Pgno b
12ce0 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 42 74 treePagecount(Bt
12cf0 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
12d00 72 65 74 75 72 6e 20 70 42 74 2d 3e 6e 50 61 67 return pBt->nPag
12d10 65 3b 0a 7d 0a 75 33 32 20 73 71 6c 69 74 65 33 e;.}.u32 sqlite3
12d20 42 74 72 65 65 4c 61 73 74 50 61 67 65 28 42 74 BtreeLastPage(Bt
12d30 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
12d40 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
12d50 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
12d60 20 20 61 73 73 65 72 74 28 20 28 28 70 2d 3e 70 assert( ((p->p
12d70 42 74 2d 3e 6e 50 61 67 65 29 26 30 78 38 30 30 Bt->nPage)&0x800
12d80 30 30 30 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 0000)==0 );. re
12d90 74 75 72 6e 20 62 74 72 65 65 50 61 67 65 63 6f turn btreePageco
12da0 75 6e 74 28 70 2d 3e 70 42 74 29 3b 0a 7d 0a 0a unt(p->pBt);.}..
12db0 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 65 /*.** Get a page
12dc0 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
12dd0 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 and initialize i
12de0 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 43 75 72 t..**.** If pCur
12df0 21 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67 !=0 then the pag
12e00 65 20 69 73 20 62 65 69 6e 67 20 66 65 74 63 68 e is being fetch
12e10 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 ed as part of a
12e20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 0a 2a 2a moveToChild().**
12e30 20 63 61 6c 6c 2e 20 20 44 6f 20 61 64 64 69 74 call. Do addit
12e40 69 6f 6e 61 6c 20 73 61 6e 69 74 79 20 63 68 65 ional sanity che
12e50 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 67 cking on the pag
12e60 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a e in this case..
12e70 2a 2a 20 41 6e 64 20 69 66 20 74 68 65 20 66 65 ** And if the fe
12e80 74 63 68 20 66 61 69 6c 73 2c 20 74 68 69 73 20 tch fails, this
12e90 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 64 65 63 routine must dec
12ea0 72 65 6d 65 6e 74 20 70 43 75 72 2d 3e 69 50 61 rement pCur->iPa
12eb0 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 ge..**.** The pa
12ec0 67 65 20 69 73 20 66 65 74 63 68 65 64 20 61 73 ge is fetched as
12ed0 20 72 65 61 64 2d 77 72 69 74 65 20 75 6e 6c 65 read-write unle
12ee0 73 73 20 70 43 75 72 20 69 73 20 6e 6f 74 20 4e ss pCur is not N
12ef0 55 4c 4c 20 61 6e 64 20 69 73 0a 2a 2a 20 61 20 ULL and is.** a
12f00 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 read-only cursor
12f10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 ..**.** If an er
12f20 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e ror occurs, then
12f30 20 2a 70 70 50 61 67 65 20 69 73 20 75 6e 64 65 *ppPage is unde
12f40 66 69 6e 65 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 fined. It.** may
12f50 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 remain unchange
12f60 64 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20 d, or it may be
12f70 73 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 set to an invali
12f80 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 d value..*/.stat
12f90 69 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 ic int getAndIni
12fa0 74 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 tPage(. BtShare
12fb0 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 d *pBt,
12fc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
12fd0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
12fe0 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 . Pgno pgno,
12ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13000 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
13010 74 68 65 20 70 61 67 65 20 74 6f 20 67 65 74 20 the page to get
13020 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 */. MemPage **p
13030 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 pPage,
13040 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
13050 65 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 e page pointer h
13060 65 72 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ere */. BtCurso
13070 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 r *pCur,
13080 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 /* Curs
13090 6f 72 20 74 6f 20 72 65 63 65 69 76 65 20 74 68 or to receive th
130a0 65 20 70 61 67 65 2c 20 6f 72 20 4e 55 4c 4c 20 e page, or NULL
130b0 2a 2f 0a 20 20 69 6e 74 20 62 52 65 61 64 4f 6e */. int bReadOn
130c0 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ly
130d0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
130e0 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 70 61 67 a read-only pag
130f0 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
13100 3b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 ;. DbPage *pDbP
13110 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 age;. assert( s
13120 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
13130 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
13140 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 3d . assert( pCur=
13150 3d 30 20 7c 7c 20 70 70 50 61 67 65 3d 3d 26 70 =0 || ppPage==&p
13160 43 75 72 2d 3e 70 50 61 67 65 20 29 3b 0a 20 20 Cur->pPage );.
13170 61 73 73 65 72 74 28 20 70 43 75 72 3d 3d 30 20 assert( pCur==0
13180 7c 7c 20 62 52 65 61 64 4f 6e 6c 79 3d 3d 70 43 || bReadOnly==pC
13190 75 72 2d 3e 63 75 72 50 61 67 65 72 46 6c 61 67 ur->curPagerFlag
131a0 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 s );. assert( p
131b0 43 75 72 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e Cur==0 || pCur->
131c0 69 50 61 67 65 3e 30 20 29 3b 0a 0a 20 20 69 66 iPage>0 );.. if
131d0 28 20 70 67 6e 6f 3e 62 74 72 65 65 50 61 67 65 ( pgno>btreePage
131e0 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 count(pBt) ){.
131f0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
13200 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
13210 67 6f 74 6f 20 67 65 74 41 6e 64 49 6e 69 74 50 goto getAndInitP
13220 61 67 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 age_error;. }.
13230 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13240 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 erGet(pBt->pPage
13250 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 r, pgno, (DbPage
13260 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 62 52 65 **)&pDbPage, bRe
13270 61 64 4f 6e 6c 79 29 3b 0a 20 20 69 66 28 20 72 adOnly);. if( r
13280 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 67 65 c ){. goto ge
13290 74 41 6e 64 49 6e 69 74 50 61 67 65 5f 65 72 72 tAndInitPage_err
132a0 6f 72 3b 0a 20 20 7d 0a 20 20 2a 70 70 50 61 67 or;. }. *ppPag
132b0 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 e = (MemPage*)sq
132c0 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
132d0 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 ra(pDbPage);. i
132e0 66 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 f( (*ppPage)->is
132f0 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 62 Init==0 ){. b
13300 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 treePageFromDbPa
13310 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f ge(pDbPage, pgno
13320 2c 20 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d , pBt);. rc =
13330 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a btreeInitPage(*
13340 70 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 ppPage);. if(
13350 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13360 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
13370 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 age(*ppPage);.
13380 20 20 20 20 67 6f 74 6f 20 67 65 74 41 6e 64 49 goto getAndI
13390 6e 69 74 50 61 67 65 5f 65 72 72 6f 72 3b 0a 20 nitPage_error;.
133a0 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
133b0 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 t( (*ppPage)->pg
133c0 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 no==pgno );. as
133d0 73 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 2d sert( (*ppPage)-
133e0 3e 61 44 61 74 61 3d 3d 73 71 6c 69 74 65 33 50 >aData==sqlite3P
133f0 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
13400 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 age) );.. /* If
13410 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 63 68 69 obtaining a chi
13420 6c 64 20 70 61 67 65 20 66 6f 72 20 61 20 63 75 ld page for a cu
13430 72 73 6f 72 2c 20 77 65 20 6d 75 73 74 20 76 65 rsor, we must ve
13440 72 69 66 79 20 74 68 61 74 20 74 68 65 20 70 61 rify that the pa
13450 67 65 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 70 61 ge is. ** compa
13460 74 69 62 6c 65 20 77 69 74 68 20 74 68 65 20 72 tible with the r
13470 6f 6f 74 20 70 61 67 65 2e 20 2a 2f 0a 20 20 69 oot page. */. i
13480 66 28 20 70 43 75 72 20 26 26 20 28 28 2a 70 70 f( pCur && ((*pp
13490 50 61 67 65 29 2d 3e 6e 43 65 6c 6c 3c 31 20 7c Page)->nCell<1 |
134a0 7c 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 6e 74 | (*ppPage)->int
134b0 4b 65 79 21 3d 70 43 75 72 2d 3e 63 75 72 49 6e Key!=pCur->curIn
134c0 74 4b 65 79 29 20 29 7b 0a 20 20 20 20 72 63 20 tKey) ){. rc
134d0 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
134e0 5f 50 47 4e 4f 28 70 67 6e 6f 29 3b 0a 20 20 20 _PGNO(pgno);.
134f0 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 releasePage(*pp
13500 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Page);. goto
13510 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 5f 65 getAndInitPage_e
13520 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rror;. }. retu
13530 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 67 rn SQLITE_OK;..g
13540 65 74 41 6e 64 49 6e 69 74 50 61 67 65 5f 65 72 etAndInitPage_er
13550 72 6f 72 3a 0a 20 20 69 66 28 20 70 43 75 72 20 ror:. if( pCur
13560 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 ){. pCur->iPa
13570 67 65 2d 2d 3b 0a 20 20 20 20 70 43 75 72 2d 3e ge--;. pCur->
13580 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
13590 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
135a0 5d 3b 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 73 ];. }. testcas
135b0 65 28 20 70 67 6e 6f 3d 3d 30 20 29 3b 0a 20 20 e( pgno==0 );.
135c0 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 assert( pgno!=0
135d0 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f || rc==SQLITE_CO
135e0 52 52 55 50 54 20 29 3b 0a 20 20 72 65 74 75 72 RRUPT );. retur
135f0 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 n rc;.}..#ifndef
13600 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e SQLITE_OMIT_CON
13610 43 55 52 52 45 4e 54 0a 2f 2a 20 0a 2a 2a 20 53 CURRENT./* .** S
13620 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
13630 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 67 6e 6f the MemPage.pgno
13640 52 6f 6f 74 20 76 61 72 69 61 62 6c 65 2c 20 69 Root variable, i
13650 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2f 0a f it exists..*/.
13660 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 4d static void setM
13670 65 6d 70 61 67 65 52 6f 6f 74 28 4d 65 6d 50 61 empageRoot(MemPa
13680 67 65 20 2a 70 50 67 2c 20 75 33 32 20 70 67 6e ge *pPg, u32 pgn
13690 6f 52 6f 6f 74 29 7b 0a 20 20 70 50 67 2d 3e 70 oRoot){. pPg->p
136a0 67 6e 6f 52 6f 6f 74 20 3d 20 70 67 6e 6f 52 6f gnoRoot = pgnoRo
136b0 6f 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 ot;.}.#else.# de
136c0 66 69 6e 65 20 73 65 74 4d 65 6d 70 61 67 65 52 fine setMempageR
136d0 6f 6f 74 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a oot(x,y).#endif.
136e0 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
136f0 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 MemPage. This
13700 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 should be called
13710 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 once for each p
13720 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 rior.** call to
13730 62 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f btreeGetPage..*/
13740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
13750 65 61 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 easePageNotNull(
13760 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
13770 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13780 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 61 73 73 ->aData );. ass
13790 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 ert( pPage->pBt
137a0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
137b0 67 65 2d 3e 70 44 62 50 61 67 65 21 3d 30 20 29 ge->pDbPage!=0 )
137c0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
137d0 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
137e0 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
137f0 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 == (void*)pPage
13800 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
13810 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
13820 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
13830 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20 )==pPage->aData
13840 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
13850 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
13860 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
13870 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 x) );. sqlite3P
13880 61 67 65 72 55 6e 72 65 66 4e 6f 74 4e 75 6c 6c agerUnrefNotNull
13890 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
138a0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
138b0 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 releasePage(MemP
138c0 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 age *pPage){. i
138d0 66 28 20 70 50 61 67 65 20 29 20 72 65 6c 65 61 f( pPage ) relea
138e0 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 70 50 sePageNotNull(pP
138f0 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 age);.}../*.** G
13900 65 74 20 61 6e 20 75 6e 75 73 65 64 20 70 61 67 et an unused pag
13910 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f e..**.** This wo
13920 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 62 74 rks just like bt
13930 72 65 65 47 65 74 50 61 67 65 28 29 20 77 69 74 reeGetPage() wit
13940 68 20 74 68 65 20 61 64 64 69 74 69 6f 6e 3a 0a h the addition:.
13950 2a 2a 0a 2a 2a 20 20 20 2a 20 20 49 66 20 74 68 **.** * If th
13960 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 e page is alread
13970 79 20 69 6e 20 75 73 65 20 66 6f 72 20 73 6f 6d y in use for som
13980 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 2c e other purpose,
13990 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 immediately.**
139a0 20 20 20 20 20 72 65 6c 65 61 73 65 20 69 74 20 release it
139b0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53 51 and return an SQ
139c0 4c 49 54 45 5f 43 55 52 52 55 50 54 20 65 72 72 LITE_CURRUPT err
139d0 6f 72 2e 0a 2a 2a 20 20 20 2a 20 20 4d 61 6b 65 or..** * Make
139e0 20 73 75 72 65 20 74 68 65 20 69 73 49 6e 69 74 sure the isInit
139f0 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 0a 2a flag is clear.*
13a00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
13a10 65 65 47 65 74 55 6e 75 73 65 64 50 61 67 65 28 eeGetUnusedPage(
13a20 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
13a30 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 , /* The b
13a40 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 tree */. Pgno p
13a50 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f gno, /
13a60 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 * Number of the
13a70 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f page to fetch */
13a80 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 . MemPage **ppP
13a90 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 age, /* Retur
13aa0 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 n the page in th
13ab0 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a is parameter */.
13ac0 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 int flags
13ad0 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f /* PAGER_
13ae0 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 6f 72 GET_NOCONTENT or
13af0 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f PAGER_GET_READO
13b00 4e 4c 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 NLY */.){. int
13b10 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
13b20 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 e(pBt, pgno, ppP
13b30 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 69 age, flags);. i
13b40 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13b50 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
13b60 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
13b70 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e ount((*ppPage)->
13b80 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 pDbPage)>1 ){.
13b90 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
13ba0 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 *ppPage);.
13bb0 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 *ppPage = 0;.
13bc0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13bd0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
13be0 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 }. (*ppPag
13bf0 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a e)->isInit = 0;.
13c00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 }else{. *pp
13c10 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Page = 0;. }.
13c20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
13c30 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f *.** During a ro
13c40 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 llback, when the
13c50 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69 pager reloads i
13c60 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
13c70 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 the cache.** so
13c80 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69 that the cache i
13c90 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 s restored to it
13ca0 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 s original state
13cb0 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
13cc0 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 .** the transact
13cd0 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61 ion, for each pa
13ce0 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73 ge restored this
13cf0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
13d00 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
13d10 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 outine needs to
13d20 72 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20 reset the extra
13d30 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20 data section at
13d40 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
13d50 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20 * page to agree
13d60 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65 with the restore
13d70 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 d data..*/.stati
13d80 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69 c void pageReini
13d90 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 29 t(DbPage *pData)
13da0 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
13db0 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d ge;. pPage = (M
13dc0 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 emPage *)sqlite3
13dd0 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 PagerGetExtra(pD
13de0 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ata);. assert(
13df0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
13e00 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e Refcount(pData)>
13e10 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 0 );. if( pPage
13e20 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 ->isInit ){.
13e30 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
13e40 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
13e50 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
13e60 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
13e70 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 it = 0;. if(
13e80 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
13e90 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e Refcount(pData)>
13ea0 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 1 ){. /* pP
13eb0 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 age might not be
13ec0 20 61 20 62 74 72 65 65 20 70 61 67 65 3b 20 20 a btree page;
13ed0 69 74 20 6d 69 67 68 74 20 62 65 20 61 6e 20 6f it might be an o
13ee0 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 verflow page.
13ef0 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 ** or ptrmap
13f00 70 61 67 65 20 6f 72 20 61 20 66 72 65 65 20 70 page or a free p
13f10 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 age. In those c
13f20 61 73 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 ases, the follow
13f30 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c ing. ** cal
13f40 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 74 50 61 l to btreeInitPa
13f50 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 ge() will likely
13f60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
13f70 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a ORRUPT.. **
13f80 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 But no harm is
13f90 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 41 done by this. A
13fa0 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 69 6d nd it is very im
13fb0 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 20 portant that.
13fc0 20 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 ** btreeInitP
13fd0 61 67 65 28 29 20 62 65 20 63 61 6c 6c 65 64 20 age() be called
13fe0 6f 6e 20 65 76 65 72 79 20 62 74 72 65 65 20 70 on every btree p
13ff0 61 67 65 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 age so we make.
14000 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c ** the call
14010 20 66 6f 72 20 65 76 65 72 79 20 70 61 67 65 20 for every page
14020 74 68 61 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f that comes in fo
14030 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f r re-initing. */
14040 0a 20 20 20 20 20 20 62 74 72 65 65 49 6e 69 74 . btreeInit
14050 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
14060 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
14070 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 Invoke the busy
14080 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 handler for a bt
14090 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ree..*/.static i
140a0 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 nt btreeInvokeBu
140b0 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a syHandler(void *
140c0 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 pArg){. BtShare
140d0 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 d *pBt = (BtShar
140e0 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 ed*)pArg;. asse
140f0 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 rt( pBt->db );.
14100 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14110 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
14120 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
14130 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
14140 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
14150 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 (&pBt->db->busyH
14160 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a andler);.}../*.*
14170 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 * Open a databas
14180 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a e file..** .** z
14190 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 Filename is the
141a0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
141b0 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a base file. If z
141c0 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c Filename is NULL
141d0 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 65 70 68 65 .** then an ephe
141e0 6d 65 72 61 6c 20 64 61 74 61 62 61 73 65 20 69 meral database i
141f0 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 s created. The
14200 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 62 61 ephemeral databa
14210 73 65 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 65 se might.** be e
14220 78 63 6c 75 73 69 76 65 6c 79 20 69 6e 20 6d 65 xclusively in me
14230 6d 6f 72 79 2c 20 6f 72 20 69 74 20 6d 69 67 68 mory, or it migh
14240 74 20 75 73 65 20 61 20 64 69 73 6b 2d 62 61 73 t use a disk-bas
14250 65 64 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e ed memory cache.
14260 0a 2a 2a 20 45 69 74 68 65 72 20 77 61 79 2c 20 .** Either way,
14270 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 64 61 the ephemeral da
14280 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 61 tabase will be a
14290 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c utomatically del
142a0 65 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20 73 71 eted .** when sq
142b0 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 lite3BtreeClose(
142c0 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a ) is called..**.
142d0 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 ** If zFilename
142e0 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 is ":memory:" th
142f0 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 en an in-memory
14300 64 61 74 61 62 61 73 65 20 69 73 20 63 72 65 61 database is crea
14310 74 65 64 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 ted.** that is a
14320 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 73 utomatically des
14330 74 72 6f 79 65 64 20 77 68 65 6e 20 69 74 20 69 troyed when it i
14340 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 s closed..**.**
14350 54 68 65 20 22 66 6c 61 67 73 22 20 70 61 72 61 The "flags" para
14360 6d 65 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 meter is a bitma
14370 73 6b 20 74 68 61 74 20 6d 69 67 68 74 20 63 6f sk that might co
14380 6e 74 61 69 6e 20 62 69 74 73 20 6c 69 6b 65 0a ntain bits like.
14390 2a 2a 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f ** BTREE_OMIT_JO
143a0 55 52 4e 41 4c 20 61 6e 64 2f 6f 72 20 42 54 52 URNAL and/or BTR
143b0 45 45 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a EE_MEMORY..**.**
143c0 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
143d0 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e is already open
143e0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 ed in the same d
143f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14400 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 on.** and we are
14410 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 in shared cache
14420 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 mode, then the
14430 6f 70 65 6e 20 77 69 6c 6c 20 66 61 69 6c 20 77 open will fail w
14440 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 ith an.** SQLITE
14450 5f 43 4f 4e 53 54 52 41 49 4e 54 20 65 72 72 6f _CONSTRAINT erro
14460 72 2e 20 20 57 65 20 63 61 6e 6e 6f 74 20 61 6c r. We cannot al
14470 6c 6f 77 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 low two or more
14480 42 74 53 68 61 72 65 64 0a 2a 2a 20 6f 62 6a 65 BtShared.** obje
14490 63 74 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 cts in the same
144a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
144b0 69 6f 6e 20 73 69 6e 63 65 20 64 6f 69 6e 67 20 ion since doing
144c0 73 6f 20 77 69 6c 6c 20 6c 65 61 64 0a 2a 2a 20 so will lead.**
144d0 74 6f 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 to problems with
144e0 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 69 6e 74 locking..*/.int
144f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 sqlite3BtreeOpe
14500 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
14510 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 *pVfs, /*
14520 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 VFS to use for t
14530 68 69 73 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 his b-tree */.
14540 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
14550 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 ename, /* Name
14560 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 of the file cont
14570 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 aining the BTree
14580 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 database */. s
14590 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
145a0 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 /* Associ
145b0 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61 ated database ha
145c0 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ndle */. Btree
145d0 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 **ppBtree,
145e0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
145f0 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 new Btree object
14600 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
14610 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
14620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
14630 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 tions */. int v
14640 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
14650 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 /* Flags pass
14660 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 ed through to sq
14670 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
14680 29 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 ) */.){. BtShar
14690 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 ed *pBt = 0;
146a0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72 /* Shar
146b0 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 65 ed part of btree
146c0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
146d0 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20 Btree *p;
146e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
146f0 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 * Handle to retu
14700 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f rn */. sqlite3_
14710 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e mutex *mutexOpen
14720 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e = 0; /* Preven
14730 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 ts a race condit
14740 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 33 ion. Ticket #353
14750 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 7 */. int rc =
14760 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
14770 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
14780 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 66 code from this f
14790 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 unction */. u8
147a0 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 20 nReserve;
147b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
147c0 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 70 yte of unused sp
147d0 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 ace on each page
147e0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
147f0 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 30 har zDbHeader[10
14800 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 0]; /* Database
14810 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 header content
14820 2a 2f 0a 0a 20 20 2f 2a 20 54 72 75 65 20 69 66 */.. /* True if
14830 20 6f 70 65 6e 69 6e 67 20 61 6e 20 65 70 68 65 opening an ephe
14840 6d 65 72 61 6c 2c 20 74 65 6d 70 6f 72 61 72 79 meral, temporary
14850 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 database */. c
14860 6f 6e 73 74 20 69 6e 74 20 69 73 54 65 6d 70 44 onst int isTempD
14870 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 b = zFilename==0
14880 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d || zFilename[0]
14890 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 ==0;.. /* Set t
148a0 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 he variable isMe
148b0 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 mdb to true for
148c0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
148d0 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 abase, or . **
148e0 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 false for a file
148f0 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e -based database.
14900 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
14910 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 ITE_OMIT_MEMORYD
14920 42 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 B. const int is
14930 4d 65 6d 64 62 20 3d 20 30 3b 0a 23 65 6c 73 65 Memdb = 0;.#else
14940 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d . const int isM
14950 65 6d 64 62 20 3d 20 28 7a 46 69 6c 65 6e 61 6d emdb = (zFilenam
14960 65 20 26 26 20 73 74 72 63 6d 70 28 7a 46 69 6c e && strcmp(zFil
14970 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a ename, ":memory:
14980 22 29 3d 3d 30 29 0a 20 20 20 20 20 20 20 20 20 ")==0).
14990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
149a0 20 28 69 73 54 65 6d 70 44 62 20 26 26 20 73 71 (isTempDb && sq
149b0 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 lite3TempInMemor
149c0 79 28 64 62 29 29 0a 20 20 20 20 20 20 20 20 20 y(db)).
149d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
149e0 20 28 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c (vfsFlags & SQL
149f0 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d 4f 52 59 29 ITE_OPEN_MEMORY)
14a00 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 !=0;.#endif.. a
14a10 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
14a20 20 20 61 73 73 65 72 74 28 20 70 56 66 73 21 3d assert( pVfs!=
14a30 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
14a40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14a50 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
14a60 20 20 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 assert( (flags
14a70 26 30 78 66 66 29 3d 3d 66 6c 61 67 73 20 29 3b &0xff)==flags );
14a80 20 20 20 2f 2a 20 66 6c 61 67 73 20 66 69 74 20 /* flags fit
14a90 69 6e 20 38 20 62 69 74 73 20 2a 2f 0a 0a 20 20 in 8 bits */..
14aa0 2f 2a 20 4f 6e 6c 79 20 61 20 42 54 52 45 45 5f /* Only a BTREE_
14ab0 53 49 4e 47 4c 45 20 64 61 74 61 62 61 73 65 20 SINGLE database
14ac0 63 61 6e 20 62 65 20 42 54 52 45 45 5f 55 4e 4f can be BTREE_UNO
14ad0 52 44 45 52 45 44 20 2a 2f 0a 20 20 61 73 73 65 RDERED */. asse
14ae0 72 74 28 20 28 66 6c 61 67 73 20 26 20 42 54 52 rt( (flags & BTR
14af0 45 45 5f 55 4e 4f 52 44 45 52 45 44 29 3d 3d 30 EE_UNORDERED)==0
14b00 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 42 54 52 || (flags & BTR
14b10 45 45 5f 53 49 4e 47 4c 45 29 21 3d 30 20 29 3b EE_SINGLE)!=0 );
14b20 0a 0a 20 20 2f 2a 20 41 20 42 54 52 45 45 5f 53 .. /* A BTREE_S
14b30 49 4e 47 4c 45 20 64 61 74 61 62 61 73 65 20 69 INGLE database i
14b40 73 20 61 6c 77 61 79 73 20 61 20 74 65 6d 70 6f s always a tempo
14b50 72 61 72 79 20 61 6e 64 2f 6f 72 20 65 70 68 65 rary and/or ephe
14b60 6d 65 72 61 6c 20 2a 2f 0a 20 20 61 73 73 65 72 meral */. asser
14b70 74 28 20 28 66 6c 61 67 73 20 26 20 42 54 52 45 t( (flags & BTRE
14b80 45 5f 53 49 4e 47 4c 45 29 3d 3d 30 20 7c 7c 20 E_SINGLE)==0 ||
14b90 69 73 54 65 6d 70 44 62 20 29 3b 0a 0a 20 20 69 isTempDb );.. i
14ba0 66 28 20 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 f( isMemdb ){.
14bb0 20 20 66 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 flags |= BTREE
14bc0 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 69 _MEMORY;. }. i
14bd0 66 28 20 28 76 66 73 46 6c 61 67 73 20 26 20 53 f( (vfsFlags & S
14be0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
14bf0 44 42 29 21 3d 30 20 26 26 20 28 69 73 4d 65 6d DB)!=0 && (isMem
14c00 64 62 20 7c 7c 20 69 73 54 65 6d 70 44 62 29 20 db || isTempDb)
14c10 29 7b 0a 20 20 20 20 76 66 73 46 6c 61 67 73 20 ){. vfsFlags
14c20 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e 53 = (vfsFlags & ~S
14c30 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
14c40 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 DB) | SQLITE_OPE
14c50 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a 20 N_TEMP_DB;. }.
14c60 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
14c70 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 ocZero(sizeof(Bt
14c80 72 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70 20 ree));. if( !p
14c90 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
14ca0 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b LITE_NOMEM_BKPT;
14cb0 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e . }. p->inTran
14cc0 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
14cd0 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 p->db = db;.#i
14ce0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
14cf0 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
14d00 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 p->lock.pBtree
14d10 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 = p;. p->lock.i
14d20 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 Table = 1;.#endi
14d30 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 f..#if !defined(
14d40 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
14d50 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
14d60 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
14d70 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 T_DISKIO). /*.
14d80 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 ** If this Btre
14d90 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 e is a candidate
14da0 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 for shared cach
14db0 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 e, try to find a
14dc0 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 n. ** existing
14dd0 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
14de0 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 that we can shar
14df0 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 e with. */. if
14e00 28 20 69 73 54 65 6d 70 44 62 3d 3d 30 20 26 26 ( isTempDb==0 &&
14e10 20 28 69 73 4d 65 6d 64 62 3d 3d 30 20 7c 7c 20 (isMemdb==0 ||
14e20 28 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 (vfsFlags&SQLITE
14e30 5f 4f 50 45 4e 5f 55 52 49 29 21 3d 30 29 20 29 _OPEN_URI)!=0) )
14e40 7b 0a 20 20 20 20 69 66 28 20 76 66 73 46 6c 61 {. if( vfsFla
14e50 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
14e60 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a _SHAREDCACHE ){.
14e70 20 20 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e int nFilen
14e80 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ame = sqlite3Str
14e90 6c 65 6e 33 30 28 7a 46 69 6c 65 6e 61 6d 65 29 len30(zFilename)
14ea0 2b 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 +1;. int nF
14eb0 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 ullPathname = pV
14ec0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
14ed0 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 ;. char *zF
14ee0 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 ullPathname = sq
14ef0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 4d 41 58 28 lite3Malloc(MAX(
14f00 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 6e 46 nFullPathname,nF
14f10 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 20 20 20 ilename));.
14f20 20 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 73 71 MUTEX_LOGIC( sq
14f30 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
14f40 65 78 53 68 61 72 65 64 3b 20 29 0a 0a 20 20 20 exShared; )..
14f50 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d p->sharable =
14f60 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 1;. if( !z
14f70 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a FullPathname ){.
14f80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
14f90 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 free(p);.
14fa0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
14fb0 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 20 OMEM_BKPT;.
14fc0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 4d }. if( isM
14fd0 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 emdb ){.
14fe0 6d 65 6d 63 70 79 28 7a 46 75 6c 6c 50 61 74 68 memcpy(zFullPath
14ff0 6e 61 6d 65 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c name, zFilename,
15000 20 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 nFilename);.
15010 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
15020 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
15030 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 FullPathname(pVf
15040 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 s, zFilename,.
15050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15070 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 nFullPathname,
15080 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a zFullPathname);.
15090 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
150a0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
150b0 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 te3_free(zFullPa
150c0 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 thname);.
150d0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
150e0 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 p);. re
150f0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
15100 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 53 }. }.#if S
15110 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
15120 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65 6e . mutexOpen
15130 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
15140 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
15150 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a X_STATIC_OPEN);.
15160 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
15170 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f tex_enter(mutexO
15180 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 pen);. mute
15190 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 xShared = sqlite
151a0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
151b0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
151c0 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 73 MASTER);. s
151d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
151e0 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b er(mutexShared);
151f0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 6f .#endif. fo
15200 72 28 70 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 r(pBt=GLOBAL(BtS
15210 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 hared*,sqlite3Sh
15220 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 20 aredCacheList);
15230 70 42 74 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e pBt; pBt=pBt->pN
15240 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ext){. as
15250 73 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e sert( pBt->nRef>
15260 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0 );. if(
15270 20 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 0==strcmp(zFull
15280 50 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 Pathname, sqlite
15290 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 3PagerFilename(p
152a0 42 74 2d 3e 70 50 61 67 65 72 2c 20 30 29 29 0a Bt->pPager, 0)).
152b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
152c0 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 && sqlite3Pager
152d0 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72 29 Vfs(pBt->pPager)
152e0 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 ==pVfs ){.
152f0 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 int iDb;.
15300 20 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d 64 for(iDb=d
15310 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 b->nDb-1; iDb>=0
15320 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 ; iDb--){.
15330 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 45 78 Btree *pEx
15340 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62 isting = db->aDb
15350 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 20 [iDb].pBt;.
15360 20 20 20 20 20 20 20 69 66 28 20 70 45 78 69 73 if( pExis
15370 74 69 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e ting && pExistin
15380 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a 20 g->pBt==pBt ){.
15390 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
153a0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
153b0 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 (mutexShared);.
153c0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
153d0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
153e0 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 (mutexOpen);.
153f0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
15400 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 e3_free(zFullPat
15410 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 hname);.
15420 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
15430 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 ee(p);.
15440 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15450 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 TE_CONSTRAINT;.
15460 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
15470 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15480 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b p->pBt = pBt;
15490 0a 20 20 20 20 20 20 20 20 20 20 70 42 74 2d 3e . pBt->
154a0 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nRef++;.
154b0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
154c0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
154d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
154e0 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 eave(mutexShared
154f0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
15500 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e _free(zFullPathn
15510 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 ame);. }.#ifd
15520 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
15530 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 else{.
15540 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65 /* In debug mode
15550 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65 , we mark all pe
15560 72 73 69 73 74 65 6e 74 20 64 61 74 61 62 61 73 rsistent databas
15570 65 73 20 61 73 20 73 68 61 72 61 62 6c 65 0a 20 es as sharable.
15580 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 ** even whe
15590 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 n they are not.
155a0 20 54 68 69 73 20 65 78 65 72 63 69 73 65 73 20 This exercises
155b0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 the locking code
155c0 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 69 and. ** gi
155d0 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 75 ves more opportu
155e0 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72 74 73 nity for asserts
155f0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 (sqlite3_mutex_h
15600 65 6c 64 28 29 29 0a 20 20 20 20 20 20 2a 2a 20 eld()). **
15610 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 69 statements to fi
15620 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c nd locking probl
15630 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ems.. */.
15640 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 p->sharable
15650 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 = 1;. }.#endi
15660 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 f. }.#endif. i
15670 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 f( pBt==0 ){.
15680 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 /*. ** The f
15690 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 ollowing asserts
156a0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
156b0 73 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 structures used
156c0 62 79 20 74 68 65 20 62 74 72 65 65 20 61 72 65 by the btree are
156d0 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 69 67 68 . ** the righ
156e0 74 20 73 69 7a 65 2e 20 20 54 68 69 73 20 69 73 t size. This is
156f0 20 74 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 to guard agains
15700 74 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 74 t size changes t
15710 68 61 74 20 72 65 73 75 6c 74 0a 20 20 20 20 2a hat result. *
15720 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 * when compiling
15730 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 on a different
15740 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20 20 architecture..
15750 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
15760 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20 sizeof(i64)==8
15770 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15780 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 20 29 3b izeof(u64)==8 );
15790 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
157a0 65 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 eof(u32)==4 );.
157b0 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
157c0 66 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 f(u16)==2 );.
157d0 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
157e0 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 Pgno)==4 );. .
157f0 20 20 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33 pBt = sqlite3
15800 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
15810 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 of(*pBt) );.
15820 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 if( pBt==0 ){.
15830 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
15840 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 NOMEM_BKPT;.
15850 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 goto btree_ope
15860 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 n_out;. }.
15870 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15880 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 70 42 erOpen(pVfs, &pB
15890 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 t->pPager, zFile
158a0 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 name,.
158b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
158c0 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 2c sizeof(MemPage),
158d0 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 flags, vfsFlags
158e0 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 , pageReinit);.
158f0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
15900 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 E_OK ){. sq
15910 6c 69 74 65 33 50 61 67 65 72 53 65 74 4d 6d 61 lite3PagerSetMma
15920 70 4c 69 6d 69 74 28 70 42 74 2d 3e 70 50 61 67 pLimit(pBt->pPag
15930 65 72 2c 20 64 62 2d 3e 73 7a 4d 6d 61 70 29 3b er, db->szMmap);
15940 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
15950 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 te3PagerReadFile
15960 68 65 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 header(pBt->pPag
15970 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 er,sizeof(zDbHea
15980 64 65 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b der),zDbHeader);
15990 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
159a0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
159b0 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 goto btree
159c0 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d _open_out;. }
159d0 0a 20 20 20 20 70 42 74 2d 3e 6f 70 65 6e 46 6c . pBt->openFl
159e0 61 67 73 20 3d 20 28 75 38 29 66 6c 61 67 73 3b ags = (u8)flags;
159f0 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d 20 64 . pBt->db = d
15a00 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 b;. sqlite3Pa
15a10 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 gerSetBusyhandle
15a20 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 62 r(pBt->pPager, b
15a30 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 treeInvokeBusyHa
15a40 6e 64 6c 65 72 2c 20 70 42 74 29 3b 0a 20 20 20 ndler, pBt);.
15a50 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 p->pBt = pBt;.
15a60 20 0a 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 . pBt->pCurs
15a70 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d or = 0;. pBt-
15a80 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 >pPage1 = 0;.
15a90 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 if( sqlite3Page
15aa0 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d rIsreadonly(pBt-
15ab0 3e 70 50 61 67 65 72 29 20 29 20 70 42 74 2d 3e >pPager) ) pBt->
15ac0 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f btsFlags |= BTS_
15ad0 52 45 41 44 5f 4f 4e 4c 59 3b 0a 23 69 66 20 64 READ_ONLY;.#if d
15ae0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 45 efined(SQLITE_SE
15af0 43 55 52 45 5f 44 45 4c 45 54 45 29 0a 20 20 20 CURE_DELETE).
15b00 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c pBt->btsFlags |
15b10 3d 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c = BTS_SECURE_DEL
15b20 45 54 45 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e ETE;.#elif defin
15b30 65 64 28 53 51 4c 49 54 45 5f 46 41 53 54 5f 53 ed(SQLITE_FAST_S
15b40 45 43 55 52 45 5f 44 45 4c 45 54 45 29 0a 20 20 ECURE_DELETE).
15b50 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 pBt->btsFlags
15b60 7c 3d 20 42 54 53 5f 4f 56 45 52 57 52 49 54 45 |= BTS_OVERWRITE
15b70 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 ;.#endif. /*
15b80 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 35 EVIDENCE-OF: R-5
15b90 31 38 37 33 2d 33 39 36 31 38 20 54 68 65 20 70 1873-39618 The p
15ba0 61 67 65 20 73 69 7a 65 20 66 6f 72 20 61 20 64 age size for a d
15bb0 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 0a atabase file is.
15bc0 20 20 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e 65 ** determine
15bd0 64 20 62 79 20 74 68 65 20 32 2d 62 79 74 65 20 d by the 2-byte
15be0 69 6e 74 65 67 65 72 20 6c 6f 63 61 74 65 64 20 integer located
15bf0 61 74 20 61 6e 20 6f 66 66 73 65 74 20 6f 66 20 at an offset of
15c00 31 36 20 62 79 74 65 73 20 66 72 6f 6d 0a 20 20 16 bytes from.
15c10 20 20 2a 2a 20 74 68 65 20 62 65 67 69 6e 6e 69 ** the beginni
15c20 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ng of the databa
15c30 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 se file. */.
15c40 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
15c50 28 7a 44 62 48 65 61 64 65 72 5b 31 36 5d 3c 3c (zDbHeader[16]<<
15c60 38 29 20 7c 20 28 7a 44 62 48 65 61 64 65 72 5b 8) | (zDbHeader[
15c70 31 37 5d 3c 3c 31 36 29 3b 0a 20 20 20 20 69 66 17]<<16);. if
15c80 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c ( pBt->pageSize<
15c90 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 512 || pBt->page
15ca0 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
15cb0 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 PAGE_SIZE.
15cc0 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 || ((pBt->pag
15cd0 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 eSize-1)&pBt->pa
15ce0 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 geSize)!=0 ){.
15cf0 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a pBt->pageSiz
15d00 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 e = 0;.#ifndef S
15d10 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
15d20 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 ACUUM. /* I
15d30 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 f the magic name
15d40 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c ":memory:" will
15d50 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 create an in-me
15d60 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 mory database, t
15d70 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 hen. ** lea
15d80 76 65 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 ve the autoVacuu
15d90 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 m mode at 0 (do
15da0 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 not auto-vacuum)
15db0 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 20 20 , even if.
15dc0 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ** SQLITE_DEFAUL
15dd0 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 T_AUTOVACUUM is
15de0 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68 true. On the oth
15df0 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 20 er hand, if.
15e00 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ** SQLITE_OMIT
15e10 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 62 65 _MEMORYDB has be
15e20 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e en defined, then
15e30 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 6a ":memory:" is j
15e40 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 72 ust a. ** r
15e50 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d 65 egular file-name
15e60 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
15e70 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 he auto-vacuum a
15e80 70 70 6c 69 65 73 20 61 73 20 70 65 72 20 6e 6f pplies as per no
15e90 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 rmal.. */.
15ea0 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 if( zFilena
15eb0 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29 me && !isMemdb )
15ec0 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 61 {. pBt->a
15ed0 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c utoVacuum = (SQL
15ee0 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
15ef0 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b VACUUM ? 1 : 0);
15f00 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e . pBt->in
15f10 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 crVacuum = (SQLI
15f20 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 TE_DEFAULT_AUTOV
15f30 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 ACUUM==2 ? 1 : 0
15f40 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
15f50 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 f. nReserve
15f60 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
15f70 0a 20 20 20 20 20 20 2f 2a 20 45 56 49 44 45 4e . /* EVIDEN
15f80 43 45 2d 4f 46 3a 20 52 2d 33 37 34 39 37 2d 34 CE-OF: R-37497-4
15f90 32 34 31 32 20 54 68 65 20 73 69 7a 65 20 6f 66 2412 The size of
15fa0 20 74 68 65 20 72 65 73 65 72 76 65 64 20 72 65 the reserved re
15fb0 67 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a 2a gion is. **
15fc0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
15fd0 68 65 20 6f 6e 65 2d 62 79 74 65 20 75 6e 73 69 he one-byte unsi
15fe0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 66 6f 75 gned integer fou
15ff0 6e 64 20 61 74 20 61 6e 20 6f 66 66 73 65 74 20 nd at an offset
16000 6f 66 20 32 30 0a 20 20 20 20 20 20 2a 2a 20 69 of 20. ** i
16010 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
16020 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 2a 2f file header. */
16030 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 . nReserve
16040 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30 5d 3b = zDbHeader[20];
16050 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 . pBt->btsF
16060 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 lags |= BTS_PAGE
16070 53 49 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e SIZE_FIXED;.#ifn
16080 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16090 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
160a0 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
160b0 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 = (get4byte(&zD
160c0 62 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 bHeader[36 + 4*4
160d0 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 ])?1:0);. p
160e0 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d Bt->incrVacuum =
160f0 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 (get4byte(&zDbH
16100 65 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 eader[36 + 7*4])
16110 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 ?1:0);.#endif.
16120 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
16130 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 ite3PagerSetPage
16140 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 size(pBt->pPager
16150 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 , &pBt->pageSize
16160 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 , nReserve);.
16170 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 if( rc ) goto b
16180 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
16190 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 pBt->usableSi
161a0 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ze = pBt->pageSi
161b0 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 ze - nReserve;.
161c0 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d assert( (pBt-
161d0 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d >pageSize & 7)==
161e0 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 0 ); /* 8-byte
161f0 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 alignment of pag
16200 65 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 66 eSize */. .#if
16210 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
16220 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
16230 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 HE) && !defined(
16240 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
16250 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 IO). /* Add t
16260 68 65 20 6e 65 77 20 42 74 53 68 61 72 65 64 20 he new BtShared
16270 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 object to the li
16280 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61 62 nked list sharab
16290 6c 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20 20 le BtShareds..
162a0 20 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 6e 52 */. pBt->nR
162b0 65 66 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 ef = 1;. if(
162c0 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
162d0 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 43 MUTEX_LOGIC
162e0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 ( sqlite3_mutex
162f0 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 20 29 0a *mutexShared; ).
16300 20 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 MUTEX_LOGI
16310 43 28 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d C( mutexShared =
16320 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
16330 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
16340 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 29 STATIC_MASTER);)
16350 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
16360 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
16370 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
16380 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
16390 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6d {. pBt->m
163a0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
163b0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
163c0 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 MUTEX_FAST);.
163d0 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 if( pBt->mu
163e0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 tex==0 ){.
163f0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
16400 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 NOMEM_BKPT;.
16410 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 goto btree
16420 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 _open_out;.
16430 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
16440 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
16450 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 _enter(mutexShar
16460 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e ed);. pBt->
16470 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 42 pNext = GLOBAL(B
16480 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
16490 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
164a0 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 ;. GLOBAL(B
164b0 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
164c0 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
164d0 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71 = pBt;. sq
164e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
164f0 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a e(mutexShared);.
16500 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
16510 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
16520 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
16530 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 D_CACHE) && !def
16540 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
16550 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 _DISKIO). /* If
16560 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 75 the new Btree u
16570 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 70 ses a sharable p
16580 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c BtShared, then l
16590 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a ink the new. **
165a0 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 Btree into the
165b0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 list of all shar
165c0 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20 able Btrees for
165d0 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 the same connect
165e0 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69 ion.. ** The li
165f0 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 st is kept in as
16600 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 cending order by
16610 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20 pBt address..
16620 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 */. if( p->shar
16630 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 able ){. int
16640 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53 i;. Btree *pS
16650 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ib;. for(i=0;
16660 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
16670 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 69 {. if( (pSi
16680 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 b = db->aDb[i].p
16690 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e Bt)!=0 && pSib->
166a0 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
166b0 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d while( pSib-
166c0 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d >pPrev ){ pSib =
166d0 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a pSib->pPrev; }.
166e0 20 20 20 20 20 20 20 20 69 66 28 20 28 75 70 74 if( (upt
166f0 72 29 70 2d 3e 70 42 74 3c 28 75 70 74 72 29 70 r)p->pBt<(uptr)p
16700 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 Sib->pBt ){.
16710 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
16720 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 pSib;.
16730 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 p->pPrev = 0;.
16740 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 pSib->p
16750 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 Prev = p;.
16760 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
16770 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e while( pSib->
16780 70 4e 65 78 74 20 26 26 20 28 75 70 74 72 29 70 pNext && (uptr)p
16790 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c Sib->pNext->pBt<
167a0 28 75 70 74 72 29 70 2d 3e 70 42 74 20 29 7b 0a (uptr)p->pBt ){.
167b0 20 20 20 20 20 20 20 20 20 20 20 20 70 53 69 62 pSib
167c0 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a = pSib->pNext;.
167d0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
167e0 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
167f0 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 pSib->pNext;.
16800 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 p->pPrev
16810 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 = pSib;.
16820 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 if( p->pNext
16830 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
16840 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d ->pNext->pPrev =
16850 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a p;. }.
16860 20 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e pSib->
16870 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 pNext = p;.
16880 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 }. bre
16890 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
168a0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a }. }.#endif. *
168b0 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 ppBtree = p;..bt
168c0 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 ree_open_out:.
168d0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
168e0 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 74 K ){. if( pBt
168f0 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 && pBt->pPager
16900 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
16910 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e PagerClose(pBt->
16920 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 pPager, 0);.
16930 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
16940 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c ee(pBt);. sql
16950 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
16960 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b 0a *ppBtree = 0;.
16970 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
16980 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 ite3_file *pFile
16990 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
169a0 20 42 2d 54 72 65 65 20 77 61 73 20 73 75 63 63 B-Tree was succ
169b0 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c essfully opened,
169c0 20 73 65 74 20 74 68 65 20 70 61 67 65 72 2d 63 set the pager-c
169d0 61 63 68 65 20 73 69 7a 65 20 74 6f 20 74 68 65 ache size to the
169e0 0a 20 20 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 . ** default
169f0 76 61 6c 75 65 2e 20 45 78 63 65 70 74 2c 20 77 value. Except, w
16a00 68 65 6e 20 6f 70 65 6e 69 6e 67 20 6f 6e 20 61 hen opening on a
16a10 6e 20 65 78 69 73 74 69 6e 67 20 73 68 61 72 65 n existing share
16a20 64 20 70 61 67 65 72 2d 63 61 63 68 65 2c 0a 20 d pager-cache,.
16a30 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 63 68 61 ** do not cha
16a40 6e 67 65 20 74 68 65 20 70 61 67 65 72 2d 63 61 nge the pager-ca
16a50 63 68 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f che size.. */
16a60 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
16a70 42 74 72 65 65 53 63 68 65 6d 61 28 70 2c 20 30 BtreeSchema(p, 0
16a80 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 , 0)==0 ){.
16a90 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
16aa0 43 61 63 68 65 73 69 7a 65 28 70 2d 3e 70 42 74 Cachesize(p->pBt
16ab0 2d 3e 70 50 61 67 65 72 2c 20 53 51 4c 49 54 45 ->pPager, SQLITE
16ac0 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
16ad0 49 5a 45 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 IZE);. }..
16ae0 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 pFile = sqlite3
16af0 50 61 67 65 72 46 69 6c 65 28 70 42 74 2d 3e 70 PagerFile(pBt->p
16b00 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
16b10 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 20 pFile->pMethods
16b20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
16b30 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e OsFileControlHin
16b40 74 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f t(pFile, SQLITE_
16b50 46 43 4e 54 4c 5f 50 44 42 2c 20 28 76 6f 69 64 FCNTL_PDB, (void
16b60 2a 29 26 70 42 74 2d 3e 64 62 29 3b 0a 20 20 20 *)&pBt->db);.
16b70 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 }. }. if( mut
16b80 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 exOpen ){. as
16b90 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
16ba0 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 tex_held(mutexOp
16bb0 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 en) );. sqlit
16bc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
16bd0 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 utexOpen);. }.
16be0 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
16bf0 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74 65 ITE_OK || sqlite
16c00 33 42 74 72 65 65 43 6f 6e 6e 65 63 74 69 6f 6e 3BtreeConnection
16c10 43 6f 75 6e 74 28 2a 70 70 42 74 72 65 65 29 3e Count(*ppBtree)>
16c20 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 0 );. return rc
16c30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 ;.}../*.** Decre
16c40 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 ment the BtShare
16c50 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 d.nRef counter.
16c60 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 When it reaches
16c70 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 zero,.** remove
16c80 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 the BtShared st
16c90 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 ructure from the
16ca0 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 sharing list.
16cb0 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 Return.** true i
16cc0 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e f the BtShared.n
16cd0 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 Ref counter reac
16ce0 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 hes zero and ret
16cf0 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 urn.** false if
16d00 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 it is still posi
16d10 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tive..*/.static
16d20 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 int removeFromSh
16d30 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 aringList(BtShar
16d40 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 ed *pBt){.#ifnde
16d50 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
16d60 41 52 45 44 5f 43 41 43 48 45 0a 20 20 4d 55 54 ARED_CACHE. MUT
16d70 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 EX_LOGIC( sqlite
16d80 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3_mutex *pMaster
16d90 3b 20 29 0a 20 20 42 74 53 68 61 72 65 64 20 2a ; ). BtShared *
16da0 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d pList;. int rem
16db0 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 oved = 0;.. ass
16dc0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
16dd0 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e ex_notheld(pBt->
16de0 6d 75 74 65 78 29 20 29 3b 0a 20 20 4d 55 54 45 mutex) );. MUTE
16df0 58 5f 4c 4f 47 49 43 28 20 70 4d 61 73 74 65 72 X_LOGIC( pMaster
16e00 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
16e10 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
16e20 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
16e30 3b 20 29 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 ; ). sqlite3_mu
16e40 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 tex_enter(pMaste
16e50 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d r);. pBt->nRef-
16e60 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 -;. if( pBt->nR
16e70 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 ef<=0 ){. if(
16e80 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 GLOBAL(BtShared
16e90 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 *,sqlite3SharedC
16ea0 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 acheList)==pBt )
16eb0 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 {. GLOBAL(B
16ec0 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
16ed0 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
16ee0 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 = pBt->pNext;.
16ef0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16f00 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 pList = GLOBAL(B
16f10 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
16f20 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
16f30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 ;. while( A
16f40 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 LWAYS(pList) &&
16f50 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 pList->pNext!=pB
16f60 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 t ){. pLi
16f70 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b st=pList->pNext;
16f80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
16f90 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 f( ALWAYS(pList)
16fa0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 ){. pLis
16fb0 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e t->pNext = pBt->
16fc0 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 pNext;. }.
16fd0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c }. if( SQL
16fe0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 ITE_THREADSAFE )
16ff0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
17000 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e mutex_free(pBt->
17010 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 mutex);. }.
17020 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 removed = 1;.
17030 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 }. sqlite3_mut
17040 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 ex_leave(pMaster
17050 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f );. return remo
17060 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 ved;.#else. ret
17070 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a urn 1;.#endif.}.
17080 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
17090 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
170a0 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c points to an all
170b0 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d ocation of .** M
170c0 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
170d0 20 62 79 74 65 73 20 77 69 74 68 20 61 20 34 2d bytes with a 4-
170e0 62 79 74 65 20 70 72 65 66 69 78 20 66 6f 72 20 byte prefix for
170f0 61 20 6c 65 66 74 2d 63 68 69 6c 64 0a 2a 2a 20 a left-child.**
17100 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 pointer..*/.stat
17110 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 ic void allocate
17120 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 TempSpace(BtShar
17130 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 ed *pBt){. if(
17140 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 !pBt->pTmpSpace
17150 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 ){. pBt->pTmp
17160 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 Space = sqlite3P
17170 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e ageMalloc( pBt->
17180 70 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 pageSize );..
17190 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 75 /* One of the u
171a0 73 65 73 20 6f 66 20 70 42 74 2d 3e 70 54 6d 70 ses of pBt->pTmp
171b0 53 70 61 63 65 20 69 73 20 74 6f 20 66 6f 72 6d Space is to form
171c0 61 74 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 0a at cells before.
171d0 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 69 6e 67 ** inserting
171e0 20 74 68 65 6d 20 69 6e 74 6f 20 61 20 6c 65 61 them into a lea
171f0 66 20 70 61 67 65 20 28 66 75 6e 63 74 69 6f 6e f page (function
17200 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 29 2e 20 fillInCell()).
17210 49 66 0a 20 20 20 20 2a 2a 20 61 20 63 65 6c 6c If. ** a cell
17220 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 34 20 is less than 4
17230 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 69 bytes in size, i
17240 74 20 69 73 20 72 6f 75 6e 64 65 64 20 75 70 20 t is rounded up
17250 74 6f 20 34 20 62 79 74 65 73 0a 20 20 20 20 2a to 4 bytes. *
17260 2a 20 62 79 20 74 68 65 20 76 61 72 69 6f 75 73 * by the various
17270 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6d routines that m
17280 61 6e 69 70 75 6c 61 74 65 20 62 69 6e 61 72 79 anipulate binary
17290 20 63 65 6c 6c 73 2e 20 57 68 69 63 68 0a 20 20 cells. Which.
172a0 20 20 2a 2a 20 63 61 6e 20 6d 65 61 6e 20 74 68 ** can mean th
172b0 61 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 20 at fillInCell()
172c0 6f 6e 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 73 only initializes
172d0 20 74 68 65 20 66 69 72 73 74 20 32 20 6f 72 20 the first 2 or
172e0 33 0a 20 20 20 20 2a 2a 20 62 79 74 65 73 20 6f 3. ** bytes o
172f0 66 20 70 54 6d 70 53 70 61 63 65 2c 20 62 75 74 f pTmpSpace, but
17300 20 74 68 61 74 20 74 68 65 20 66 69 72 73 74 20 that the first
17310 34 20 62 79 74 65 73 20 61 72 65 20 63 6f 70 69 4 bytes are copi
17320 65 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 69 ed from. ** i
17330 74 20 69 6e 74 6f 20 61 20 64 61 74 61 62 61 73 t into a databas
17340 65 20 70 61 67 65 2e 20 54 68 69 73 20 69 73 20 e page. This is
17350 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 61 20 70 not actually a p
17360 72 6f 62 6c 65 6d 2c 20 62 75 74 20 69 74 0a 20 roblem, but it.
17370 20 20 20 2a 2a 20 64 6f 65 73 20 63 61 75 73 65 ** does cause
17380 20 61 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f a valgrind erro
17390 72 20 77 68 65 6e 20 74 68 65 20 31 20 6f 72 20 r when the 1 or
173a0 32 20 62 79 74 65 73 20 6f 66 20 75 6e 69 74 69 2 bytes of uniti
173b0 61 6c 69 7a 65 64 20 0a 20 20 20 20 2a 2a 20 64 alized . ** d
173c0 61 74 61 20 69 73 20 70 61 73 73 65 64 20 74 6f ata is passed to
173d0 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 77 72 69 system call wri
173e0 74 65 28 29 2e 20 53 6f 20 74 6f 20 61 76 6f 69 te(). So to avoi
173f0 64 20 74 68 69 73 20 65 72 72 6f 72 2c 0a 20 20 d this error,.
17400 20 20 2a 2a 20 7a 65 72 6f 20 74 68 65 20 66 69 ** zero the fi
17410 72 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 rst 4 bytes of t
17420 65 6d 70 20 73 70 61 63 65 20 68 65 72 65 2e 0a emp space here..
17430 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c **. ** Al
17440 73 6f 3a 20 20 50 72 6f 76 69 64 65 20 66 6f 75 so: Provide fou
17450 72 20 62 79 74 65 73 20 6f 66 20 69 6e 69 74 69 r bytes of initi
17460 61 6c 69 7a 65 64 20 73 70 61 63 65 20 62 65 66 alized space bef
17470 6f 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 62 ore the. ** b
17480 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 54 6d 70 eginning of pTmp
17490 53 70 61 63 65 20 61 73 20 61 6e 20 61 72 65 61 Space as an area
174a0 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 70 72 available to pr
174b0 65 70 65 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a epend the. **
174c0 20 6c 65 66 74 2d 63 68 69 6c 64 20 70 6f 69 6e left-child poin
174d0 74 65 72 20 74 6f 20 74 68 65 20 62 65 67 69 6e ter to the begin
174e0 6e 69 6e 67 20 6f 66 20 61 20 63 65 6c 6c 2e 0a ning of a cell..
174f0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
17500 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b Bt->pTmpSpace ){
17510 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 42 . memset(pB
17520 74 2d 3e 70 54 6d 70 53 70 61 63 65 2c 20 30 2c t->pTmpSpace, 0,
17530 20 38 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 8);. pBt->
17540 70 54 6d 70 53 70 61 63 65 20 2b 3d 20 34 3b 0a pTmpSpace += 4;.
17550 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
17560 2a 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d ** Free the pBt-
17570 3e 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 >pTmpSpace alloc
17580 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 ation.*/.static
17590 76 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 void freeTempSpa
175a0 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ce(BtShared *pBt
175b0 29 7b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 54 ){. if( pBt->pT
175c0 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 mpSpace ){. p
175d0 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 2d 3d Bt->pTmpSpace -=
175e0 20 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 4;. sqlite3P
175f0 61 67 65 46 72 65 65 28 70 42 74 2d 3e 70 54 6d ageFree(pBt->pTm
17600 70 53 70 61 63 65 29 3b 0a 20 20 20 20 70 42 74 pSpace);. pBt
17610 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b ->pTmpSpace = 0;
17620 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c . }.}../*.** Cl
17630 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 ose an open data
17640 62 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 base and invalid
17650 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e ate all cursors.
17660 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
17670 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 treeClose(Btree
17680 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
17690 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
176a0 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b BtCursor *pCur;
176b0 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c .. /* Close all
176c0 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 cursors opened
176d0 76 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e via this handle.
176e0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
176f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17700 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
17710 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
17720 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 eEnter(p);. pCu
17730 72 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 r = pBt->pCursor
17740 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 ;. while( pCur
17750 29 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 ){. BtCursor
17760 2a 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 *pTmp = pCur;.
17770 20 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 pCur = pCur->p
17780 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 Next;. if( pT
17790 6d 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b mp->pBtree==p ){
177a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
177b0 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 reeCloseCursor(p
177c0 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Tmp);. }. }.
177d0 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 . /* Rollback a
177e0 6e 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 ny active transa
177f0 63 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 ction and free t
17800 68 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 he handle struct
17810 75 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 ure.. ** The ca
17820 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ll to sqlite3Btr
17830 65 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f eeRollback() dro
17840 70 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 ps any table-loc
17850 6b 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 ks held by. **
17860 74 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a this handle.. *
17870 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 /. sqlite3Btree
17880 52 6f 6c 6c 62 61 63 6b 28 70 2c 20 53 51 4c 49 Rollback(p, SQLI
17890 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73 71 6c TE_OK, 0);. sql
178a0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
178b0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 );.. /* If ther
178c0 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 e are still othe
178d0 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 r outstanding re
178e0 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
178f0 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 2a shared-btree. *
17900 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 74 * structure, ret
17910 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d urn now. The rem
17920 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 70 ainder of this p
17930 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 20 rocedure cleans
17940 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 61 . ** up the sha
17950 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a red-btree.. */.
17960 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
17970 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d tToLock==0 && p-
17980 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 >locked==0 );.
17990 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 if( !p->sharable
179a0 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 || removeFromSh
179b0 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 29 aringList(pBt) )
179c0 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 74 {. /* The pBt
179d0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e is no longer on
179e0 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73 the sharing lis
179f0 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 63 t, so we can acc
17a00 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 ess. ** it wi
17a10 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
17a20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e 0a hold the mutex..
17a30 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c **. ** Cl
17a40 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 ean out and dele
17a50 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 te the BtShared
17a60 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 object.. */.
17a70 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d assert( !pBt-
17a80 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 >pCursor );.
17a90 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 sqlite3PagerClos
17aa0 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 e(pBt->pPager, p
17ab0 2d 3e 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 ->db);. if( p
17ac0 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 Bt->xFreeSchema
17ad0 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 && pBt->pSchema
17ae0 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 46 ){. pBt->xF
17af0 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 reeSchema(pBt->p
17b00 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 Schema);. }.
17b10 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
17b20 28 30 2c 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 (0, pBt->pSchema
17b30 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 );. freeTempS
17b40 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 pace(pBt);. s
17b50 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
17b60 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
17b70 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
17b80 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 D_CACHE. assert
17b90 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
17ba0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
17bb0 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a p->locked==0 );.
17bc0 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 if( p->pPrev )
17bd0 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
17be0 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 = p->pNext;. i
17bf0 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d f( p->pNext ) p-
17c00 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
17c10 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 p->pPrev;.#endif
17c20 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .. sqlite3_free
17c30 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
17c40 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
17c50 2a 20 43 68 61 6e 67 65 20 74 68 65 20 22 73 6f * Change the "so
17c60 66 74 22 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 ft" limit on the
17c70 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
17c80 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a in the cache..*
17c90 2a 20 55 6e 75 73 65 64 20 61 6e 64 20 75 6e 6d * Unused and unm
17ca0 6f 64 69 66 69 65 64 20 70 61 67 65 73 20 77 69 odified pages wi
17cb0 6c 6c 20 62 65 20 72 65 63 79 63 6c 65 64 20 77 ll be recycled w
17cc0 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hen the number o
17cd0 66 0a 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68 f.** pages in th
17ce0 65 20 63 61 63 68 65 20 65 78 63 65 65 64 73 20 e cache exceeds
17cf0 74 68 69 73 20 73 6f 66 74 20 6c 69 6d 69 74 2e this soft limit.
17d00 20 20 42 75 74 20 74 68 65 20 73 69 7a 65 20 6f But the size o
17d10 66 20 74 68 65 0a 2a 2a 20 63 61 63 68 65 20 69 f the.** cache i
17d20 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 67 72 6f s allowed to gro
17d30 77 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 w larger than th
17d40 69 73 20 6c 69 6d 69 74 20 69 66 20 69 74 20 63 is limit if it c
17d50 6f 6e 74 61 69 6e 73 0a 2a 2a 20 64 69 72 74 79 ontains.** dirty
17d60 20 70 61 67 65 73 20 6f 72 20 70 61 67 65 73 20 pages or pages
17d70 73 74 69 6c 6c 20 69 6e 20 61 63 74 69 76 65 20 still in active
17d80 75 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 use..*/.int sqli
17d90 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 te3BtreeSetCache
17da0 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 Size(Btree *p, i
17db0 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 nt mxPage){. Bt
17dc0 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
17dd0 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 >pBt;. assert(
17de0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17df0 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
17e00 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
17e10 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 eeEnter(p);. sq
17e20 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 lite3PagerSetCac
17e30 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 hesize(pBt->pPag
17e40 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 er, mxPage);. s
17e50 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
17e60 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
17e70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
17e80 2a 20 43 68 61 6e 67 65 20 74 68 65 20 22 73 70 * Change the "sp
17e90 69 6c 6c 22 20 6c 69 6d 69 74 20 6f 6e 20 74 68 ill" limit on th
17ea0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
17eb0 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a s in the cache..
17ec0 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 ** If the number
17ed0 20 6f 66 20 70 61 67 65 73 20 65 78 63 65 65 64 of pages exceed
17ee0 73 20 74 68 69 73 20 6c 69 6d 69 74 20 64 75 72 s this limit dur
17ef0 69 6e 67 20 61 20 77 72 69 74 65 20 74 72 61 6e ing a write tran
17f00 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 saction,.** the
17f10 70 61 67 65 72 20 6d 69 67 68 74 20 61 74 74 65 pager might atte
17f20 6d 70 74 20 74 6f 20 22 73 70 69 6c 6c 22 20 70 mpt to "spill" p
17f30 61 67 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 ages to the jour
17f40 6e 61 6c 20 65 61 72 6c 79 20 69 6e 0a 2a 2a 20 nal early in.**
17f50 6f 72 64 65 72 20 74 6f 20 66 72 65 65 20 75 70 order to free up
17f60 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 memory..**.** T
17f70 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
17f80 64 20 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 d is the current
17f90 20 73 70 69 6c 6c 20 73 69 7a 65 2e 20 20 49 66 spill size. If
17fa0 20 7a 65 72 6f 20 69 73 20 70 61 73 73 65 64 0a zero is passed.
17fb0 2a 2a 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e ** as an argumen
17fc0 74 2c 20 6e 6f 20 63 68 61 6e 67 65 73 20 61 72 t, no changes ar
17fd0 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 73 70 e made to the sp
17fe0 69 6c 6c 20 73 69 7a 65 20 73 65 74 74 69 6e 67 ill size setting
17ff0 2c 20 73 6f 0a 2a 2a 20 75 73 69 6e 67 20 6d 78 , so.** using mx
18000 50 61 67 65 20 6f 66 20 30 20 69 73 20 61 20 77 Page of 0 is a w
18010 61 79 20 74 6f 20 71 75 65 72 79 20 74 68 65 20 ay to query the
18020 63 75 72 72 65 6e 74 20 73 70 69 6c 6c 20 73 69 current spill si
18030 7a 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ze..*/.int sqlit
18040 65 33 42 74 72 65 65 53 65 74 53 70 69 6c 6c 53 e3BtreeSetSpillS
18050 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ize(Btree *p, in
18060 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 t mxPage){. BtS
18070 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
18080 70 42 74 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a pBt;. int res;.
18090 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
180a0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
180b0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
180c0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
180d0 72 28 70 29 3b 0a 20 20 72 65 73 20 3d 20 73 71 r(p);. res = sq
180e0 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 70 69 lite3PagerSetSpi
180f0 6c 6c 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 llsize(pBt->pPag
18100 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 er, mxPage);. s
18110 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
18120 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 (p);. return re
18130 73 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 s;.}..#if SQLITE
18140 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 _MAX_MMAP_SIZE>0
18150 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
18160 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 61 e limit on the a
18170 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 64 61 74 mount of the dat
18180 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 abase file that
18190 6d 61 79 20 62 65 0a 2a 2a 20 6d 65 6d 6f 72 79 may be.** memory
181a0 20 6d 61 70 70 65 64 2e 0a 2a 2f 0a 69 6e 74 20 mapped..*/.int
181b0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 4d sqlite3BtreeSetM
181c0 6d 61 70 4c 69 6d 69 74 28 42 74 72 65 65 20 2a mapLimit(Btree *
181d0 70 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 p, sqlite3_int64
181e0 20 73 7a 4d 6d 61 70 29 7b 0a 20 20 42 74 53 68 szMmap){. BtSh
181f0 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
18200 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 Bt;. assert( sq
18210 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
18220 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
18230 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
18240 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 Enter(p);. sqli
18250 74 65 33 50 61 67 65 72 53 65 74 4d 6d 61 70 4c te3PagerSetMmapL
18260 69 6d 69 74 28 70 42 74 2d 3e 70 50 61 67 65 72 imit(pBt->pPager
18270 2c 20 73 7a 4d 6d 61 70 29 3b 0a 20 20 73 71 6c , szMmap);. sql
18280 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18290 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
182a0 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 TE_OK;.}.#endif
182b0 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d /* SQLITE_MAX_MM
182c0 41 50 5f 53 49 5a 45 3e 30 20 2a 2f 0a 0a 2f 2a AP_SIZE>0 */../*
182d0 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 77 .** Change the w
182e0 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 65 ay data is synce
182f0 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 d to disk in ord
18300 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 6f er to increase o
18310 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f r decrease.** ho
18320 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 62 w well the datab
18330 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d 61 ase resists dama
18340 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 ge due to OS cra
18350 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a shes and power.*
18360 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 * failures. Lev
18370 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d 65 el 1 is the same
18380 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 as asynchronous
18390 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 (no syncs() occ
183a0 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 ur and.** there
183b0 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 62 is a high probab
183c0 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 29 ility of damage)
183d0 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 65 Level 2 is the
183e0 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 65 default. There
183f0 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c 6f .** is a very lo
18400 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 w but non-zero p
18410 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 robability of da
18420 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 mage. Level 3 r
18430 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 educes the.** pr
18440 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d obability of dam
18450 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f age to near zero
18460 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 74 but with a writ
18470 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 e performance re
18480 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e duction..*/.#ifn
18490 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
184a0 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 69 6e PAGER_PRAGMAS.in
184b0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
184c0 74 50 61 67 65 72 46 6c 61 67 73 28 0a 20 20 42 tPagerFlags(. B
184d0 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 tree *p,
184e0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 /* The btr
184f0 65 65 20 74 6f 20 73 65 74 20 74 68 65 20 73 61 ee to set the sa
18500 66 65 74 79 20 6c 65 76 65 6c 20 6f 6e 20 2a 2f fety level on */
18510 0a 20 20 75 6e 73 69 67 6e 65 64 20 70 67 46 6c . unsigned pgFl
18520 61 67 73 20 20 20 20 20 20 20 2f 2a 20 56 61 72 ags /* Var
18530 69 6f 75 73 20 50 41 47 45 52 5f 2a 20 66 6c 61 ious PAGER_* fla
18540 67 73 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 gs */.){. BtSha
18550 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
18560 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c t;. assert( sql
18570 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18580 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
18590 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
185a0 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 nter(p);. sqlit
185b0 65 33 50 61 67 65 72 53 65 74 46 6c 61 67 73 28 e3PagerSetFlags(
185c0 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 46 pBt->pPager, pgF
185d0 6c 61 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 lags);. sqlite3
185e0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
185f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
18600 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
18610 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 64 65 ** Change the de
18620 66 61 75 6c 74 20 70 61 67 65 73 20 73 69 7a 65 fault pages size
18630 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 and the number
18640 6f 66 20 72 65 73 65 72 76 65 64 20 62 79 74 65 of reserved byte
18650 73 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a 20 4f s per page..** O
18660 72 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 73 r, if the page s
18670 69 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 ize has already
18680 62 65 65 6e 20 66 69 78 65 64 2c 20 72 65 74 75 been fixed, retu
18690 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e rn SQLITE_READON
186a0 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 LY .** without c
186b0 68 61 6e 67 69 6e 67 20 61 6e 79 74 68 69 6e 67 hanging anything
186c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ..**.** The page
186d0 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 size must be a
186e0 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 power of 2 betwe
186f0 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 en 512 and 65536
18700 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a . If the page.*
18710 2a 20 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 * size supplied
18720 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 does not meet th
18730 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 is constraint th
18740 65 6e 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 en the page size
18750 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 is not.** chang
18760 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 ed..**.** Page s
18770 69 7a 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 izes are constra
18780 69 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 ined to be a pow
18790 65 72 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 er of two so tha
187a0 74 20 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 t the region.**
187b0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
187c0 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f file used for lo
187d0 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 cking (beginning
187e0 20 61 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 at PENDING_BYTE
187f0 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 ,.** the first b
18800 79 74 65 20 70 61 73 74 20 74 68 65 20 31 47 42 yte past the 1GB
18810 20 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 boundary, 0x400
18820 30 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 00000) needs to
18830 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 occur.** at the
18840 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 beginning of a p
18850 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 age..**.** If pa
18860 72 61 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 rameter nReserve
18870 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 is less than ze
18880 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d ro, then the num
18890 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a ber of reserved.
188a0 2a 2a 20 62 79 74 65 73 20 70 65 72 20 70 61 67 ** bytes per pag
188b0 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e e is left unchan
188c0 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ged..**.** If th
188d0 65 20 69 46 69 78 21 3d 30 20 74 68 65 6e 20 74 e iFix!=0 then t
188e0 68 65 20 42 54 53 5f 50 41 47 45 53 49 5a 45 5f he BTS_PAGESIZE_
188f0 46 49 58 45 44 20 66 6c 61 67 20 69 73 20 73 65 FIXED flag is se
18900 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 t so that the pa
18910 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 ge size.** and a
18920 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 63 utovacuum mode c
18930 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 an no longer be
18940 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 69 6e 74 20 changed..*/.int
18950 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 sqlite3BtreeSetP
18960 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 ageSize(Btree *p
18970 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 , int pageSize,
18980 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e int nReserve, in
18990 74 20 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 t iFix){. int r
189a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
189b0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
189c0 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
189d0 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 t( nReserve>=-1
189e0 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 && nReserve<=255
189f0 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
18a00 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 20 eeEnter(p);.#if
18a10 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
18a20 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3e . if( nReserve>
18a30 70 42 74 2d 3e 6f 70 74 69 6d 61 6c 52 65 73 65 pBt->optimalRese
18a40 72 76 65 20 29 20 70 42 74 2d 3e 6f 70 74 69 6d rve ) pBt->optim
18a50 61 6c 52 65 73 65 72 76 65 20 3d 20 28 75 38 29 alReserve = (u8)
18a60 6e 52 65 73 65 72 76 65 3b 0a 23 65 6e 64 69 66 nReserve;.#endif
18a70 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 . if( pBt->btsF
18a80 6c 61 67 73 20 26 20 42 54 53 5f 50 41 47 45 53 lags & BTS_PAGES
18a90 49 5a 45 5f 46 49 58 45 44 20 29 7b 0a 20 20 20 IZE_FIXED ){.
18aa0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18ab0 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
18ac0 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
18ad0 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 Y;. }. if( nRe
18ae0 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e serve<0 ){. n
18af0 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 Reserve = pBt->p
18b00 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 ageSize - pBt->u
18b10 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 sableSize;. }.
18b20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 assert( nReserv
18b30 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 e>=0 && nReserve
18b40 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 <=255 );. if( p
18b50 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
18b60 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 pageSize<=SQLITE
18b70 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 _MAX_PAGE_SIZE &
18b80 26 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65 &. ((page
18b90 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 Size-1)&pageSize
18ba0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 )==0 ){. asse
18bb0 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 rt( (pageSize &
18bc0 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 7)==0 );. ass
18bd0 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 ert( !pBt->pCurs
18be0 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 or );. pBt->p
18bf0 61 67 65 53 69 7a 65 20 3d 20 28 75 33 32 29 70 ageSize = (u32)p
18c00 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 ageSize;. fre
18c10 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
18c20 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
18c30 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 te3PagerSetPages
18c40 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ize(pBt->pPager,
18c50 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c &pBt->pageSize,
18c60 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 nReserve);. pB
18c70 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 t->usableSize =
18c80 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
18c90 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 (u16)nReserve;.
18ca0 20 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d if( iFix ) pBt-
18cb0 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
18cc0 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b _PAGESIZE_FIXED;
18cd0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
18ce0 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
18cf0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
18d00 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e eturn the curren
18d10 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 tly defined page
18d20 20 73 69 7a 65 0a 2a 2f 0a 69 6e 74 20 73 71 6c size.*/.int sql
18d30 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
18d40 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a Size(Btree *p){.
18d50 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d return p->pBt-
18d60 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a >pageSize;.}../*
18d70 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
18d80 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 n is similar to
18d90 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
18da0 65 73 65 72 76 65 28 29 2c 20 65 78 63 65 70 74 eserve(), except
18db0 20 74 68 61 74 20 69 74 0a 2a 2a 20 6d 61 79 20 that it.** may
18dc0 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 69 only be called i
18dd0 66 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 f it is guarante
18de0 65 64 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 ed that the b-tr
18df0 65 65 20 6d 75 74 65 78 20 69 73 20 61 6c 72 65 ee mutex is alre
18e00 61 64 79 0a 2a 2a 20 68 65 6c 64 2e 0a 2a 2a 0a ady.** held..**.
18e10 2a 2a 20 54 68 69 73 20 69 73 20 75 73 65 66 75 ** This is usefu
18e20 6c 20 69 6e 20 6f 6e 65 20 73 70 65 63 69 61 6c l in one special
18e30 20 63 61 73 65 20 69 6e 20 74 68 65 20 62 61 63 case in the bac
18e40 6b 75 70 20 41 50 49 20 63 6f 64 65 20 77 68 65 kup API code whe
18e50 72 65 20 69 74 20 69 73 0a 2a 2a 20 6b 6e 6f 77 re it is.** know
18e60 6e 20 74 68 61 74 20 74 68 65 20 73 68 61 72 65 n that the share
18e70 64 20 62 2d 74 72 65 65 20 6d 75 74 65 78 20 69 d b-tree mutex i
18e80 73 20 68 65 6c 64 2c 20 62 75 74 20 74 68 65 20 s held, but the
18e90 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 0a 2a 2a mutex on the .**
18ea0 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
18eb0 20 74 68 61 74 20 6f 77 6e 73 20 2a 70 20 69 73 that owns *p is
18ec0 20 6e 6f 74 2e 20 49 6e 20 74 68 69 73 20 63 61 not. In this ca
18ed0 73 65 20 69 66 20 73 71 6c 69 74 65 33 42 74 72 se if sqlite3Btr
18ee0 65 65 45 6e 74 65 72 28 29 0a 2a 2a 20 77 65 72 eeEnter().** wer
18ef0 65 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2c 20 e to be called,
18f00 69 74 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65 it might collide
18f10 20 77 69 74 68 20 73 6f 6d 65 20 6f 74 68 65 72 with some other
18f20 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 operation on th
18f30 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 e.** database ha
18f40 6e 64 6c 65 20 74 68 61 74 20 6f 77 6e 73 20 2a ndle that owns *
18f50 70 2c 20 63 61 75 73 69 6e 67 20 75 6e 64 65 66 p, causing undef
18f60 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a ined behavior..*
18f70 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
18f80 65 65 47 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 eeGetReserveNoMu
18f90 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 tex(Btree *p){.
18fa0 20 69 6e 74 20 6e 3b 0a 20 20 61 73 73 65 72 74 int n;. assert
18fb0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
18fc0 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 held(p->pBt->mut
18fd0 65 78 29 20 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e ex) );. n = p->
18fe0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
18ff0 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 p->pBt->usableSi
19000 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a ze;. return n;.
19010 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
19020 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
19030 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 74 20 tes of space at
19040 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 the end of every
19050 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 61 72 page that.** ar
19060 65 20 69 6e 74 65 6e 74 75 61 6c 6c 79 20 6c 65 e intentually le
19070 66 74 20 75 6e 75 73 65 64 2e 20 20 54 68 69 73 ft unused. This
19080 20 69 73 20 74 68 65 20 22 72 65 73 65 72 76 65 is the "reserve
19090 64 22 20 73 70 61 63 65 20 74 68 61 74 20 69 73 d" space that is
190a0 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 75 73 .** sometimes us
190b0 65 64 20 62 79 20 65 78 74 65 6e 73 69 6f 6e 73 ed by extensions
190c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ..**.** If SQLIT
190d0 45 5f 48 41 53 5f 4d 55 54 45 58 20 69 73 20 64 E_HAS_MUTEX is d
190e0 65 66 69 6e 65 64 20 74 68 65 6e 20 74 68 65 20 efined then the
190f0 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 number returned
19100 69 73 20 74 68 65 0a 2a 2a 20 67 72 65 61 74 65 is the.** greate
19110 72 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 r of the current
19120 20 72 65 73 65 72 76 65 64 20 73 70 61 63 65 20 reserved space
19130 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 and the maximum
19140 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 72 65 73 requested.** res
19150 65 72 76 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 69 erve space..*/.i
19160 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
19170 65 74 4f 70 74 69 6d 61 6c 52 65 73 65 72 76 65 etOptimalReserve
19180 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
19190 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 t n;. sqlite3Bt
191a0 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e reeEnter(p);. n
191b0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
191c0 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 74 65 78 etReserveNoMutex
191d0 28 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 (p);.#ifdef SQLI
191e0 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 TE_HAS_CODEC. i
191f0 66 28 20 6e 3c 70 2d 3e 70 42 74 2d 3e 6f 70 74 f( n<p->pBt->opt
19200 69 6d 61 6c 52 65 73 65 72 76 65 20 29 20 6e 20 imalReserve ) n
19210 3d 20 70 2d 3e 70 42 74 2d 3e 6f 70 74 69 6d 61 = p->pBt->optima
19220 6c 52 65 73 65 72 76 65 3b 0a 23 65 6e 64 69 66 lReserve;.#endif
19230 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
19240 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
19250 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 n n;.}.../*.** S
19260 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 et the maximum p
19270 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 age count for a
19280 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 database if mxPa
19290 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a ge is positive..
192a0 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 ** No changes ar
192b0 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 e made if mxPage
192c0 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 is 0 or negativ
192d0 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 e..** Regardless
192e0 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 of the value of
192f0 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 mxPage, return
19300 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 the maximum page
19310 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 73 count..*/.int s
19320 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 qlite3BtreeMaxPa
19330 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 geCount(Btree *p
19340 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
19350 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 int n;. sqlite
19360 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
19370 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 n = sqlite3Pag
19380 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 erMaxPageCount(p
19390 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d ->pBt->pPager, m
193a0 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 xPage);. sqlite
193b0 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
193c0 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f return n;.}../
193d0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
193e0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 42 values for the B
193f0 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TS_SECURE_DELETE
19400 20 61 6e 64 20 42 54 53 5f 4f 56 45 52 57 52 49 and BTS_OVERWRI
19410 54 45 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 TE flags:.**.**
19420 20 20 20 6e 65 77 46 6c 61 67 3d 3d 30 20 20 20 newFlag==0
19430 20 20 20 20 42 6f 74 68 20 42 54 53 5f 53 45 43 Both BTS_SEC
19440 55 52 45 5f 44 45 4c 45 54 45 20 61 6e 64 20 42 URE_DELETE and B
19450 54 53 5f 4f 56 45 52 57 52 49 54 45 20 61 72 65 TS_OVERWRITE are
19460 20 63 6c 65 61 72 65 64 0a 2a 2a 20 20 20 20 6e cleared.** n
19470 65 77 46 6c 61 67 3d 3d 31 20 20 20 20 20 20 20 ewFlag==1
19480 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 BTS_SECURE_DELET
19490 45 20 73 65 74 20 61 6e 64 20 42 54 53 5f 4f 56 E set and BTS_OV
194a0 45 52 57 52 49 54 45 20 69 73 20 63 6c 65 61 72 ERWRITE is clear
194b0 65 64 0a 2a 2a 20 20 20 20 6e 65 77 46 6c 61 67 ed.** newFlag
194c0 3d 3d 32 20 20 20 20 20 20 20 42 54 53 5f 53 45 ==2 BTS_SE
194d0 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6c 65 61 CURE_DELETE clea
194e0 72 65 64 20 61 6e 64 20 42 54 53 5f 4f 56 45 52 red and BTS_OVER
194f0 57 52 49 54 45 20 69 73 20 73 65 74 0a 2a 2a 20 WRITE is set.**
19500 20 20 20 6e 65 77 46 6c 61 67 3d 3d 28 2d 31 29 newFlag==(-1)
19510 20 20 20 20 4e 6f 20 63 68 61 6e 67 65 73 0a 2a No changes.*
19520 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
19530 65 20 61 63 74 73 20 61 73 20 61 20 71 75 65 72 e acts as a quer
19540 79 20 69 66 20 6e 65 77 46 6c 61 67 20 69 73 20 y if newFlag is
19550 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 0a 2a less than zero.*
19560 2a 0a 2a 2a 20 57 69 74 68 20 42 54 53 5f 4f 56 *.** With BTS_OV
19570 45 52 57 52 49 54 45 20 73 65 74 2c 20 64 65 6c ERWRITE set, del
19580 65 74 65 64 20 63 6f 6e 74 65 6e 74 20 69 73 20 eted content is
19590 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 7a overwritten by z
195a0 65 72 6f 73 2c 20 62 75 74 0a 2a 2a 20 66 72 65 eros, but.** fre
195b0 65 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 elist leaf pages
195c0 20 61 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e are not written
195d0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 61 74 back to the dat
195e0 61 62 61 73 65 2e 20 20 54 68 75 73 20 69 6e 2d abase. Thus in-
195f0 70 61 67 65 0a 2a 2a 20 64 65 6c 65 74 65 64 20 page.** deleted
19600 63 6f 6e 74 65 6e 74 20 69 73 20 63 6c 65 61 72 content is clear
19610 65 64 2c 20 62 75 74 20 66 72 65 65 6c 69 73 74 ed, but freelist
19620 20 64 65 6c 65 74 65 64 20 63 6f 6e 74 65 6e 74 deleted content
19630 20 69 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 57 is not..**.** W
19640 69 74 68 20 42 54 53 5f 53 45 43 55 52 45 5f 44 ith BTS_SECURE_D
19650 45 4c 45 54 45 2c 20 6f 70 65 72 61 74 69 6f 6e ELETE, operation
19660 20 69 73 20 6c 69 6b 65 20 42 54 53 5f 4f 56 45 is like BTS_OVE
19670 52 57 52 49 54 45 20 77 69 74 68 20 74 68 65 20 RWRITE with the
19680 61 64 64 69 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 addition.** that
19690 20 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 70 freelist leaf p
196a0 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65 6e ages are written
196b0 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 back into the d
196c0 61 74 61 62 61 73 65 2c 20 69 6e 63 72 65 61 73 atabase, increas
196d0 69 6e 67 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e ing.** the amoun
196e0 74 20 6f 66 20 64 69 73 6b 20 49 2f 4f 2e 0a 2a t of disk I/O..*
196f0 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
19700 65 65 53 65 63 75 72 65 44 65 6c 65 74 65 28 42 eeSecureDelete(B
19710 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 65 77 tree *p, int new
19720 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 62 3b 0a Flag){. int b;.
19730 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
19740 75 72 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 urn 0;. sqlite3
19750 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
19760 20 61 73 73 65 72 74 28 20 42 54 53 5f 4f 56 45 assert( BTS_OVE
19770 52 57 52 49 54 45 3d 3d 42 54 53 5f 53 45 43 55 RWRITE==BTS_SECU
19780 52 45 5f 44 45 4c 45 54 45 2a 32 20 29 3b 0a 20 RE_DELETE*2 );.
19790 20 61 73 73 65 72 74 28 20 42 54 53 5f 46 41 53 assert( BTS_FAS
197a0 54 5f 53 45 43 55 52 45 3d 3d 28 42 54 53 5f 4f T_SECURE==(BTS_O
197b0 56 45 52 57 52 49 54 45 7c 42 54 53 5f 53 45 43 VERWRITE|BTS_SEC
197c0 55 52 45 5f 44 45 4c 45 54 45 29 20 29 3b 0a 20 URE_DELETE) );.
197d0 20 69 66 28 20 6e 65 77 46 6c 61 67 3e 3d 30 20 if( newFlag>=0
197e0 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d 3e 62 ){. p->pBt->b
197f0 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f tsFlags &= ~BTS_
19800 46 41 53 54 5f 53 45 43 55 52 45 3b 0a 20 20 20 FAST_SECURE;.
19810 20 70 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 p->pBt->btsFlag
19820 73 20 7c 3d 20 42 54 53 5f 53 45 43 55 52 45 5f s |= BTS_SECURE_
19830 44 45 4c 45 54 45 2a 6e 65 77 46 6c 61 67 3b 0a DELETE*newFlag;.
19840 20 20 7d 0a 20 20 62 20 3d 20 28 70 2d 3e 70 42 }. b = (p->pB
19850 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
19860 53 5f 46 41 53 54 5f 53 45 43 55 52 45 29 2f 42 S_FAST_SECURE)/B
19870 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TS_SECURE_DELETE
19880 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
19890 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
198a0 72 6e 20 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rn b;.}../*.** C
198b0 68 61 6e 67 65 20 74 68 65 20 27 61 75 74 6f 2d hange the 'auto-
198c0 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 vacuum' property
198d0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
198e0 2e 20 49 66 20 74 68 65 20 27 61 75 74 6f 56 61 . If the 'autoVa
198f0 63 75 75 6d 27 0a 2a 2a 20 70 61 72 61 6d 65 74 cuum'.** paramet
19900 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 er is non-zero,
19910 74 68 65 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d then auto-vacuum
19920 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 64 mode is enabled
19930 2e 20 49 66 20 7a 65 72 6f 2c 20 69 74 0a 2a 2a . If zero, it.**
19940 20 69 73 20 64 69 73 61 62 6c 65 64 2e 20 54 68 is disabled. Th
19950 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 e default value
19960 66 6f 72 20 74 68 65 20 61 75 74 6f 2d 76 61 63 for the auto-vac
19970 75 75 6d 20 70 72 6f 70 65 72 74 79 20 69 73 20 uum property is
19980 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 62 .** determined b
19990 79 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 46 y the SQLITE_DEF
199a0 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 AULT_AUTOVACUUM
199b0 6d 61 63 72 6f 2e 0a 2a 2f 0a 69 6e 74 20 73 71 macro..*/.int sq
199c0 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74 lite3BtreeSetAut
199d0 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 oVacuum(Btree *p
199e0 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d , int autoVacuum
199f0 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
19a00 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
19a10 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
19a20 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 6c 73 65 _READONLY;.#else
19a30 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
19a40 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 = p->pBt;. int
19a50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
19a60 0a 20 20 75 38 20 61 76 20 3d 20 28 75 38 29 61 . u8 av = (u8)a
19a70 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 20 73 71 utoVacuum;.. sq
19a80 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
19a90 70 29 3b 0a 20 20 69 66 28 20 28 70 42 74 2d 3e p);. if( (pBt->
19aa0 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50 btsFlags & BTS_P
19ab0 41 47 45 53 49 5a 45 5f 46 49 58 45 44 29 21 3d AGESIZE_FIXED)!=
19ac0 30 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d 0 && (av ?1:0)!=
19ad0 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
19ae0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
19af0 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d TE_READONLY;. }
19b00 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 else{. pBt->a
19b10 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f utoVacuum = av ?
19b20 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 1:0;. pBt->in
19b30 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 crVacuum = av==2
19b40 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c ?1:0;. }. sql
19b50 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
19b60 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
19b70 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
19b80 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 Return the value
19b90 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61 of the 'auto-va
19ba0 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20 cuum' property.
19bb0 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 If auto-vacuum i
19bc0 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20 s .** enabled 1
19bd0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 is returned. Oth
19be0 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 69 6e 74 erwise 0..*/.int
19bf0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
19c00 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 AutoVacuum(Btree
19c10 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c *p){.#ifdef SQL
19c20 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
19c30 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52 UUM. return BTR
19c40 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f EE_AUTOVACUUM_NO
19c50 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 NE;.#else. int
19c60 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 rc;. sqlite3Btr
19c70 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 eeEnter(p);. rc
19c80 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 = (. (!p->pB
19c90 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 t->autoVacuum)?B
19ca0 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
19cb0 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 NONE:. (!p->p
19cc0 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f Bt->incrVacuum)?
19cd0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
19ce0 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 _FULL:. BTREE
19cf0 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 _AUTOVACUUM_INCR
19d00 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 . );. sqlite3B
19d10 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
19d20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
19d30 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 f.}../*.** If th
19d40 65 20 75 73 65 72 20 68 61 73 20 6e 6f 74 20 73 e user has not s
19d50 65 74 20 74 68 65 20 73 61 66 65 74 79 2d 6c 65 et the safety-le
19d60 76 65 6c 20 66 6f 72 20 74 68 69 73 20 64 61 74 vel for this dat
19d70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
19d80 0a 2a 2a 20 75 73 69 6e 67 20 22 50 52 41 47 4d .** using "PRAGM
19d90 41 20 73 79 6e 63 68 72 6f 6e 6f 75 73 22 2c 20 A synchronous",
19da0 61 6e 64 20 69 66 20 74 68 65 20 73 61 66 65 74 and if the safet
19db0 79 2d 6c 65 76 65 6c 20 69 73 20 6e 6f 74 20 61 y-level is not a
19dc0 6c 72 65 61 64 79 0a 2a 2a 20 73 65 74 20 74 6f lready.** set to
19dd0 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 the value passe
19de0 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 d to this functi
19df0 6f 6e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 on as the second
19e00 20 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20 73 parameter,.** s
19e10 65 74 20 69 74 20 73 6f 2e 0a 2a 2f 0a 23 69 66 et it so..*/.#if
19e20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
19e30 53 59 4e 43 48 52 4f 4e 4f 55 53 21 3d 53 51 4c SYNCHRONOUS!=SQL
19e40 49 54 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f ITE_DEFAULT_WAL_
19e50 53 59 4e 43 48 52 4f 4e 4f 55 53 0a 73 74 61 74 SYNCHRONOUS.stat
19e60 69 63 20 76 6f 69 64 20 73 65 74 44 65 66 61 75 ic void setDefau
19e70 6c 74 53 79 6e 63 46 6c 61 67 28 42 74 53 68 61 ltSyncFlag(BtSha
19e80 72 65 64 20 2a 70 42 74 2c 20 75 38 20 73 61 66 red *pBt, u8 saf
19e90 65 74 79 5f 6c 65 76 65 6c 29 7b 0a 20 20 73 71 ety_level){. sq
19ea0 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 44 62 20 lite3 *db;. Db
19eb0 2a 70 44 62 3b 0a 20 20 69 66 28 20 28 64 62 3d *pDb;. if( (db=
19ec0 70 42 74 2d 3e 64 62 29 21 3d 30 20 26 26 20 28 pBt->db)!=0 && (
19ed0 70 44 62 3d 64 62 2d 3e 61 44 62 29 21 3d 30 20 pDb=db->aDb)!=0
19ee0 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 44 ){. while( pD
19ef0 62 2d 3e 70 42 74 3d 3d 30 20 7c 7c 20 70 44 62 b->pBt==0 || pDb
19f00 2d 3e 70 42 74 2d 3e 70 42 74 21 3d 70 42 74 20 ->pBt->pBt!=pBt
19f10 29 7b 20 70 44 62 2b 2b 3b 20 7d 0a 20 20 20 20 ){ pDb++; }.
19f20 69 66 28 20 70 44 62 2d 3e 62 53 79 6e 63 53 65 if( pDb->bSyncSe
19f30 74 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 70 44 t==0 . && pD
19f40 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 21 b->safety_level!
19f50 3d 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 0a 20 =safety_level .
19f60 20 20 20 20 26 26 20 70 44 62 21 3d 26 64 62 2d && pDb!=&db-
19f70 3e 61 44 62 5b 31 5d 20 0a 20 20 20 20 29 7b 0a >aDb[1] . ){.
19f80 20 20 20 20 20 20 70 44 62 2d 3e 73 61 66 65 74 pDb->safet
19f90 79 5f 6c 65 76 65 6c 20 3d 20 73 61 66 65 74 79 y_level = safety
19fa0 5f 6c 65 76 65 6c 3b 0a 20 20 20 20 20 20 73 71 _level;. sq
19fb0 6c 69 74 65 33 50 61 67 65 72 53 65 74 46 6c 61 lite3PagerSetFla
19fc0 67 73 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 0a gs(pBt->pPager,.
19fd0 20 20 20 20 20 20 20 20 20 20 70 44 62 2d 3e 73 pDb->s
19fe0 61 66 65 74 79 5f 6c 65 76 65 6c 20 7c 20 28 64 afety_level | (d
19ff0 62 2d 3e 66 6c 61 67 73 20 26 20 50 41 47 45 52 b->flags & PAGER
1a000 5f 46 4c 41 47 53 5f 4d 41 53 4b 29 29 3b 0a 20 _FLAGS_MASK));.
1a010 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 }. }.}.#else
1a020 0a 23 20 64 65 66 69 6e 65 20 73 65 74 44 65 66 .# define setDef
1a030 61 75 6c 74 53 79 6e 63 46 6c 61 67 28 70 42 74 aultSyncFlag(pBt
1a040 2c 73 61 66 65 74 79 5f 6c 65 76 65 6c 29 0a 23 ,safety_level).#
1a050 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 endif../*.** Get
1a060 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
1a070 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 pPage1 of the da
1a080 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
1a090 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 is will.** also
1a0a0 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f acquire a readlo
1a0b0 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e ck on that file.
1a0c0 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
1a0d0 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
1a0e0 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 success. If the
1a0f0 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a file is not a.*
1a100 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 * well-formed da
1a110 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
1a120 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1a130 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
1a140 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
1a150 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
1a160 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b database is lock
1a170 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ed. SQLITE_NOME
1a180 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 M.** is returned
1a190 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f if we run out o
1a1a0 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 f memory. .*/.st
1a1b0 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 atic int lockBtr
1a1c0 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ee(BtShared *pBt
1a1d0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
1a1e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1a1f0 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 ult code from su
1a200 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 bfunctions */.
1a210 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
1a220 20 20 20 20 20 2f 2a 20 50 61 67 65 20 31 20 6f /* Page 1 o
1a230 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
1a240 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 ile */. int nPa
1a250 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ge; /*
1a260 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
1a270 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1a280 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 46 */. int nPageF
1a290 69 6c 65 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 ile = 0; /* Nu
1a2a0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1a2b0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1a2c0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 le */. int nPag
1a2d0 65 48 65 61 64 65 72 3b 20 20 20 20 20 2f 2a 20 eHeader; /*
1a2e0 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
1a2f0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1a300 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 68 64 72 according to hdr
1a310 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
1a320 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1a330 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1a340 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
1a350 70 50 61 67 65 31 3d 3d 30 20 29 3b 0a 20 20 72 pPage1==0 );. r
1a360 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1a370 53 68 61 72 65 64 4c 6f 63 6b 28 70 42 74 2d 3e SharedLock(pBt->
1a380 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72 pPager);. if( r
1a390 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
1a3a0 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
1a3b0 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
1a3c0 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 t, 1, &pPage1, 0
1a3d0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1a3e0 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
1a3f0 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d rc;.. /* Do som
1a400 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 e checking to he
1a410 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 lp insure the fi
1a420 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 le we opened rea
1a430 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 lly is. ** a va
1a440 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 69 6c lid database fil
1a450 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 50 61 67 65 e. . */. nPage
1a460 20 3d 20 6e 50 61 67 65 48 65 61 64 65 72 20 3d = nPageHeader =
1a470 20 67 65 74 34 62 79 74 65 28 32 38 2b 28 75 38 get4byte(28+(u8
1a480 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 74 61 29 *)pPage1->aData)
1a490 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 ;. sqlite3Pager
1a4a0 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
1a4b0 50 61 67 65 72 2c 20 26 6e 50 61 67 65 46 69 6c Pager, &nPageFil
1a4c0 65 29 3b 0a 20 20 69 66 28 20 6e 50 61 67 65 3d e);. if( nPage=
1a4d0 3d 30 20 7c 7c 20 6d 65 6d 63 6d 70 28 32 34 2b =0 || memcmp(24+
1a4e0 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 (u8*)pPage1->aDa
1a4f0 74 61 2c 20 39 32 2b 28 75 38 2a 29 70 50 61 67 ta, 92+(u8*)pPag
1a500 65 31 2d 3e 61 44 61 74 61 2c 34 29 21 3d 30 20 e1->aData,4)!=0
1a510 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 6e ){. nPage = n
1a520 50 61 67 65 46 69 6c 65 3b 0a 20 20 7d 0a 20 20 PageFile;. }.
1a530 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 if( nPage>0 ){.
1a540 20 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 3b u32 pageSize;
1a550 0a 20 20 20 20 75 33 32 20 75 73 61 62 6c 65 53 . u32 usableS
1a560 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 ize;. u8 *pag
1a570 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 e1 = pPage1->aDa
1a580 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ta;. rc = SQL
1a590 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 ITE_NOTADB;.
1a5a0 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 /* EVIDENCE-OF:
1a5b0 52 2d 34 33 37 33 37 2d 33 39 39 39 39 20 45 76 R-43737-39999 Ev
1a5c0 65 72 79 20 76 61 6c 69 64 20 53 51 4c 69 74 65 ery valid SQLite
1a5d0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 database file b
1a5e0 65 67 69 6e 73 0a 20 20 20 20 2a 2a 20 77 69 74 egins. ** wit
1a5f0 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 h the following
1a600 31 36 20 62 79 74 65 73 20 28 69 6e 20 68 65 78 16 bytes (in hex
1a610 29 3a 20 35 33 20 35 31 20 34 63 20 36 39 20 37 ): 53 51 4c 69 7
1a620 34 20 36 35 20 32 30 20 36 36 20 36 66 20 37 32 4 65 20 66 6f 72
1a630 20 36 64 0a 20 20 20 20 2a 2a 20 36 31 20 37 34 6d. ** 61 74
1a640 20 32 30 20 33 33 20 30 30 2e 20 2a 2f 0a 20 20 20 33 00. */.
1a650 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 if( memcmp(pag
1a660 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 e1, zMagicHeader
1a670 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 , 16)!=0 ){.
1a680 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
1a690 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
1a6a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
1a6b0 4d 49 54 5f 57 41 4c 0a 20 20 20 20 69 66 28 20 MIT_WAL. if(
1a6c0 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 page1[18]>1 ){.
1a6d0 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 pBt->btsFla
1a6e0 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f gs |= BTS_READ_O
1a6f0 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 NLY;. }. i
1a700 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 f( page1[19]>1 )
1a710 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 {. goto pag
1a720 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
1a730 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
1a740 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 32 20 if( page1[18]>2
1a750 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 ){. pBt->bt
1a760 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 sFlags |= BTS_RE
1a770 41 44 5f 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 AD_ONLY;. }.
1a780 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d if( page1[19]
1a790 3e 32 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f >2 ){. goto
1a7a0 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
1a7b0 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ed;. }.. /
1a7c0 2a 20 49 66 20 74 68 65 20 77 72 69 74 65 20 76 * If the write v
1a7d0 65 72 73 69 6f 6e 20 69 73 20 73 65 74 20 74 6f ersion is set to
1a7e0 20 32 2c 20 74 68 69 73 20 64 61 74 61 62 61 73 2, this databas
1a7f0 65 20 73 68 6f 75 6c 64 20 62 65 20 61 63 63 65 e should be acce
1a800 73 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 57 ssed. ** in W
1a810 41 4c 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 20 AL mode. If the
1a820 6c 6f 67 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 log is not alrea
1a830 64 79 20 6f 70 65 6e 2c 20 6f 70 65 6e 20 69 74 dy open, open it
1a840 20 6e 6f 77 2e 20 54 68 65 6e 20 0a 20 20 20 20 now. Then .
1a850 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ** return SQLITE
1a860 5f 4f 4b 20 61 6e 64 20 72 65 74 75 72 6e 20 77 _OK and return w
1a870 69 74 68 6f 75 74 20 70 6f 70 75 6c 61 74 69 6e ithout populatin
1a880 67 20 42 74 53 68 61 72 65 64 2e 70 50 61 67 65 g BtShared.pPage
1a890 31 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 61 1.. ** The ca
1a8a0 6c 6c 65 72 20 64 65 74 65 63 74 73 20 74 68 69 ller detects thi
1a8b0 73 20 61 6e 64 20 63 61 6c 6c 73 20 74 68 69 73 s and calls this
1a8c0 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 2e function again.
1a8d0 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 This is. **
1a8e0 72 65 71 75 69 72 65 64 20 61 73 20 74 68 65 20 required as the
1a8f0 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 version of page
1a900 31 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 1 currently in t
1a910 68 65 20 70 61 67 65 31 20 62 75 66 66 65 72 0a he page1 buffer.
1a920 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 ** may not b
1a930 65 20 74 68 65 20 6c 61 74 65 73 74 20 76 65 72 e the latest ver
1a940 73 69 6f 6e 20 2d 20 74 68 65 72 65 20 6d 61 79 sion - there may
1a950 20 62 65 20 61 20 6e 65 77 65 72 20 6f 6e 65 20 be a newer one
1a960 69 6e 20 74 68 65 20 6c 6f 67 0a 20 20 20 20 2a in the log. *
1a970 2a 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 * file.. */.
1a980 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d if( page1[19]
1a990 3d 3d 32 20 26 26 20 28 70 42 74 2d 3e 62 74 73 ==2 && (pBt->bts
1a9a0 46 6c 61 67 73 20 26 20 42 54 53 5f 4e 4f 5f 57 Flags & BTS_NO_W
1a9b0 41 4c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 AL)==0 ){.
1a9c0 69 6e 74 20 69 73 4f 70 65 6e 20 3d 20 30 3b 0a int isOpen = 0;.
1a9d0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1a9e0 65 33 50 61 67 65 72 4f 70 65 6e 57 61 6c 28 70 e3PagerOpenWal(p
1a9f0 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 69 73 4f Bt->pPager, &isO
1aa00 70 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 pen);. if(
1aa10 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1aa20 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 . goto pa
1aa30 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
1aa40 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1aa50 20 20 20 20 20 20 73 65 74 44 65 66 61 75 6c 74 setDefault
1aa60 53 79 6e 63 46 6c 61 67 28 70 42 74 2c 20 53 51 SyncFlag(pBt, SQ
1aa70 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c LITE_DEFAULT_WAL
1aa80 5f 53 59 4e 43 48 52 4f 4e 4f 55 53 2b 31 29 3b _SYNCHRONOUS+1);
1aa90 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 4f . if( isO
1aaa0 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pen==0 ){.
1aab0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1aac0 70 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 20 pPage1);.
1aad0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1aae0 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 _OK;. }.
1aaf0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 }. rc
1ab00 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b = SQLITE_NOTADB;
1ab10 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1ab20 20 20 73 65 74 44 65 66 61 75 6c 74 53 79 6e 63 setDefaultSync
1ab30 46 6c 61 67 28 70 42 74 2c 20 53 51 4c 49 54 45 Flag(pBt, SQLITE
1ab40 5f 44 45 46 41 55 4c 54 5f 53 59 4e 43 48 52 4f _DEFAULT_SYNCHRO
1ab50 4e 4f 55 53 2b 31 29 3b 0a 20 20 20 20 7d 0a 23 NOUS+1);. }.#
1ab60 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 45 56 endif.. /* EV
1ab70 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 35 34 IDENCE-OF: R-154
1ab80 36 35 2d 32 30 38 31 33 20 54 68 65 20 6d 61 78 65-20813 The max
1ab90 69 6d 75 6d 20 61 6e 64 20 6d 69 6e 69 6d 75 6d imum and minimum
1aba0 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 embedded payloa
1abb0 64 0a 20 20 20 20 2a 2a 20 66 72 61 63 74 69 6f d. ** fractio
1abc0 6e 73 20 61 6e 64 20 74 68 65 20 6c 65 61 66 20 ns and the leaf
1abd0 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e payload fraction
1abe0 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 values must be
1abf0 36 34 2c 20 33 32 2c 20 61 6e 64 20 33 32 2e 0a 64, 32, and 32..
1ac00 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
1ac10 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 67 e original desig
1ac20 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 n allowed these
1ac30 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 2c amounts to vary,
1ac40 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20 2a but as of. *
1ac50 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c * version 3.6.0,
1ac60 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65 6d we require them
1ac70 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 20 to be fixed..
1ac80 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d */. if( mem
1ac90 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 cmp(&page1[21],
1aca0 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 "\100\040\040",3
1acb0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f )!=0 ){. go
1acc0 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 to page1_init_fa
1acd0 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iled;. }.
1ace0 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 /* EVIDENCE-OF:
1acf0 52 2d 35 31 38 37 33 2d 33 39 36 31 38 20 54 68 R-51873-39618 Th
1ad00 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 e page size for
1ad10 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
1ad20 69 73 0a 20 20 20 20 2a 2a 20 64 65 74 65 72 6d is. ** determ
1ad30 69 6e 65 64 20 62 79 20 74 68 65 20 32 2d 62 79 ined by the 2-by
1ad40 74 65 20 69 6e 74 65 67 65 72 20 6c 6f 63 61 74 te integer locat
1ad50 65 64 20 61 74 20 61 6e 20 6f 66 66 73 65 74 20 ed at an offset
1ad60 6f 66 20 31 36 20 62 79 74 65 73 20 66 72 6f 6d of 16 bytes from
1ad70 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 65 67 69 . ** the begi
1ad80 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 nning of the dat
1ad90 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
1ada0 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 28 70 pageSize = (p
1adb0 61 67 65 31 5b 31 36 5d 3c 3c 38 29 20 7c 20 28 age1[16]<<8) | (
1adc0 70 61 67 65 31 5b 31 37 5d 3c 3c 31 36 29 3b 0a page1[17]<<16);.
1add0 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
1ade0 4f 46 3a 20 52 2d 32 35 30 30 38 2d 32 31 36 38 OF: R-25008-2168
1adf0 38 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 8 The size of a
1ae00 70 61 67 65 20 69 73 20 61 20 70 6f 77 65 72 20 page is a power
1ae10 6f 66 20 74 77 6f 0a 20 20 20 20 2a 2a 20 62 65 of two. ** be
1ae20 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 tween 512 and 65
1ae30 35 33 36 20 69 6e 63 6c 75 73 69 76 65 2e 20 2a 536 inclusive. *
1ae40 2f 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 /. if( ((page
1ae50 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 Size-1)&pageSize
1ae60 29 21 3d 30 0a 20 20 20 20 20 7c 7c 20 70 61 67 )!=0. || pag
1ae70 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 eSize>SQLITE_MAX
1ae80 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 20 20 _PAGE_SIZE .
1ae90 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 3d 32 35 || pageSize<=25
1aea0 36 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6 . ){.
1aeb0 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
1aec0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
1aed0 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 assert( (pageS
1aee0 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 ize & 7)==0 );.
1aef0 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f /* EVIDENCE-O
1af00 46 3a 20 52 2d 35 39 33 31 30 2d 35 31 32 30 35 F: R-59310-51205
1af10 20 54 68 65 20 22 72 65 73 65 72 76 65 64 20 73 The "reserved s
1af20 70 61 63 65 22 20 73 69 7a 65 20 69 6e 20 74 68 pace" size in th
1af30 65 20 31 2d 62 79 74 65 0a 20 20 20 20 2a 2a 20 e 1-byte. **
1af40 69 6e 74 65 67 65 72 20 61 74 20 6f 66 66 73 65 integer at offse
1af50 74 20 32 30 20 69 73 20 74 68 65 20 6e 75 6d 62 t 20 is the numb
1af60 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
1af70 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 pace at the end
1af80 6f 66 0a 20 20 20 20 2a 2a 20 65 61 63 68 20 70 of. ** each p
1af90 61 67 65 20 74 6f 20 72 65 73 65 72 76 65 20 66 age to reserve f
1afa0 6f 72 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 0a or extensions. .
1afb0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 56 **. ** EV
1afc0 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 33 37 34 IDENCE-OF: R-374
1afd0 39 37 2d 34 32 34 31 32 20 54 68 65 20 73 69 7a 97-42412 The siz
1afe0 65 20 6f 66 20 74 68 65 20 72 65 73 65 72 76 65 e of the reserve
1aff0 64 20 72 65 67 69 6f 6e 20 69 73 0a 20 20 20 20 d region is.
1b000 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 ** determined by
1b010 20 74 68 65 20 6f 6e 65 2d 62 79 74 65 20 75 6e the one-byte un
1b020 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 66 signed integer f
1b030 6f 75 6e 64 20 61 74 20 61 6e 20 6f 66 66 73 65 ound at an offse
1b040 74 20 6f 66 20 32 30 0a 20 20 20 20 2a 2a 20 69 t of 20. ** i
1b050 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
1b060 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 2a 2f file header. */
1b070 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 . usableSize
1b080 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 = pageSize - pag
1b090 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 e1[20];. if(
1b0a0 28 75 33 32 29 70 61 67 65 53 69 7a 65 21 3d 70 (u32)pageSize!=p
1b0b0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a Bt->pageSize ){.
1b0c0 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 /* After r
1b0d0 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 eading the first
1b0e0 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
1b0f0 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 abase assuming a
1b100 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 page size.
1b110 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e ** of BtShared.
1b120 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 pageSize, we hav
1b130 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 e discovered tha
1b140 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 t the page-size
1b150 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 is. ** actu
1b160 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 ally pageSize. U
1b170 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
1b180 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 se, leave pBt->p
1b190 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a Page1 at. *
1b1a0 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 * zero and retur
1b1b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 n SQLITE_OK. The
1b1c0 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c caller will cal
1b1d0 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a l this function.
1b1e0 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 ** again w
1b1f0 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 ith the correct
1b200 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 page-size..
1b210 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 */. releas
1b220 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
1b230 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 pBt->usable
1b240 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 69 7a Size = usableSiz
1b250 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 e;. pBt->pa
1b260 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a geSize = pageSiz
1b270 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d e;. freeTem
1b280 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
1b290 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1b2a0 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
1b2b0 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 pBt->pPager, &pB
1b2c0 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 t->pageSize,.
1b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2f0 70 61 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 pageSize-usableS
1b300 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ize);. retu
1b310 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
1b320 20 69 66 28 20 28 70 42 74 2d 3e 64 62 2d 3e 66 if( (pBt->db->f
1b330 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 72 lags & SQLITE_Wr
1b340 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 20 26 26 iteSchema)==0 &&
1b350 20 6e 50 61 67 65 3e 6e 50 61 67 65 46 69 6c 65 nPage>nPageFile
1b360 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1b370 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1b380 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 PT;. goto p
1b390 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
1b3a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 45 ;. }. /* E
1b3b0 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 38 VIDENCE-OF: R-28
1b3c0 33 31 32 2d 36 34 37 30 34 20 48 6f 77 65 76 65 312-64704 Howeve
1b3d0 72 2c 20 74 68 65 20 75 73 61 62 6c 65 20 73 69 r, the usable si
1b3e0 7a 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 ze is not allowe
1b3f0 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 6c d to. ** be l
1b400 65 73 73 20 74 68 61 6e 20 34 38 30 2e 20 49 6e ess than 480. In
1b410 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 other words, if
1b420 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 the page size i
1b430 73 20 35 31 32 2c 20 74 68 65 6e 20 74 68 65 0a s 512, then the.
1b440 20 20 20 20 2a 2a 20 72 65 73 65 72 76 65 64 20 ** reserved
1b450 73 70 61 63 65 20 73 69 7a 65 20 63 61 6e 6e 6f space size canno
1b460 74 20 65 78 63 65 65 64 20 33 32 2e 20 2a 2f 0a t exceed 32. */.
1b470 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69 if( usableSi
1b480 7a 65 3c 34 38 30 20 29 7b 0a 20 20 20 20 20 20 ze<480 ){.
1b490 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
1b4a0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
1b4b0 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
1b4c0 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 = pageSize;.
1b4d0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1b4e0 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 69 = usableSize;.#i
1b4f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1b500 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
1b510 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1b520 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 = (get4byte(&pa
1b530 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 ge1[36 + 4*4])?1
1b540 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e :0);. pBt->in
1b550 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 crVacuum = (get4
1b560 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b byte(&page1[36 +
1b570 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 7*4])?1:0);.#en
1b580 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d 61 dif. }.. /* ma
1b590 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d 61 xLocal is the ma
1b5a0 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 ximum amount of
1b5b0 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 65 payload to store
1b5c0 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 2a locally for. *
1b5d0 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 20 * a cell. Make
1b5e0 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c 6c sure it is small
1b5f0 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 20 enough so that
1b600 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e 6f at least minFano
1b610 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 61 ut. ** cells ca
1b620 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f 6e n will fit on on
1b630 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 75 e page. We assu
1b640 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 67 me a 10-byte pag
1b650 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 42 e header.. ** B
1b660 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c 6f esides the paylo
1b670 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 73 ad, the cell mus
1b680 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 20 t store:. **
1b690 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72 2-byte pointer
1b6a0 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 2a to the cell. *
1b6b0 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 69 * 4-byte chi
1b6c0 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 ld pointer. **
1b6d0 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20 9-byte nKey
1b6e0 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 value. ** 4
1b6f0 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75 -byte nData valu
1b700 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 e. ** 4-byt
1b710 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 e overflow page
1b720 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f 20 pointer. ** So
1b730 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 a cell consists
1b740 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 6e of a 2-byte poin
1b750 74 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 ter, a header wh
1b760 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 ich is as much a
1b770 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 s. ** 17 bytes
1b780 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 long, 0 to N byt
1b790 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 es of payload, a
1b7a0 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 nd an optional 4
1b7b0 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 byte overflow.
1b7c0 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 ** page pointer
1b7d0 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 .. */. pBt->ma
1b7e0 78 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 28 28 xLocal = (u16)((
1b7f0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
1b800 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 29 12)*64/255 - 23)
1b810 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 ;. pBt->minLoca
1b820 6c 20 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e l = (u16)((pBt->
1b830 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 usableSize-12)*3
1b840 32 2f 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 70 2/255 - 23);. p
1b850 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 28 75 Bt->maxLeaf = (u
1b860 31 36 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 16)(pBt->usableS
1b870 69 7a 65 20 2d 20 33 35 29 3b 0a 20 20 70 42 74 ize - 35);. pBt
1b880 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 75 31 36 ->minLeaf = (u16
1b890 29 28 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 )((pBt->usableSi
1b8a0 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 ze-12)*32/255 -
1b8b0 32 33 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 23);. if( pBt->
1b8c0 6d 61 78 4c 6f 63 61 6c 3e 31 32 37 20 29 7b 0a maxLocal>127 ){.
1b8d0 20 20 20 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 pBt->max1byt
1b8e0 65 50 61 79 6c 6f 61 64 20 3d 20 31 32 37 3b 0a ePayload = 127;.
1b8f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 }else{. pBt
1b900 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61 ->max1bytePayloa
1b910 64 20 3d 20 28 75 38 29 70 42 74 2d 3e 6d 61 78 d = (u8)pBt->max
1b920 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 Local;. }. ass
1b930 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 ert( pBt->maxLea
1b940 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c f + 23 <= MX_CEL
1b950 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 L_SIZE(pBt) );.
1b960 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 pBt->pPage1 = p
1b970 50 61 67 65 31 3b 0a 20 20 70 42 74 2d 3e 6e 50 Page1;. pBt->nP
1b980 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 72 age = nPage;. r
1b990 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1b9a0 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 ..page1_init_fai
1b9b0 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 led:. releasePa
1b9c0 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 ge(pPage1);. pB
1b9d0 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 t->pPage1 = 0;.
1b9e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
1b9f0 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
1ba00 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1ba10 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
1ba20 20 6f 70 65 6e 20 6f 6e 20 70 42 74 2e 20 54 68 open on pBt. Th
1ba30 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a is is for use.**
1ba40 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 in assert() exp
1ba50 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 ressions, so it
1ba60 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 is only compiled
1ba70 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f if NDEBUG is no
1ba80 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a t.** defined..**
1ba90 0a 2a 2a 20 4f 6e 6c 79 20 77 72 69 74 65 20 63 .** Only write c
1baa0 75 72 73 6f 72 73 20 61 72 65 20 63 6f 75 6e 74 ursors are count
1bab0 65 64 20 69 66 20 77 72 4f 6e 6c 79 20 69 73 20 ed if wrOnly is
1bac0 74 72 75 65 2e 20 20 49 66 20 77 72 4f 6e 6c 79 true. If wrOnly
1bad0 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 74 68 65 is.** false the
1bae0 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 n all cursors ar
1baf0 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a e counted..**.**
1bb00 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 For the purpose
1bb10 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e s of this routin
1bb20 65 2c 20 61 20 63 75 72 73 6f 72 20 69 73 20 61 e, a cursor is a
1bb30 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a ny cursor that.*
1bb40 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 * is capable of
1bb50 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
1bb60 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
1bb70 73 65 2e 20 20 43 75 72 73 6f 72 73 20 74 68 61 se. Cursors tha
1bb80 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 74 t.** have been t
1bb90 72 69 70 70 65 64 20 69 6e 74 6f 20 74 68 65 20 ripped into the
1bba0 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 73 74 61 CURSOR_FAULT sta
1bbb0 74 65 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 te are not count
1bbc0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
1bbd0 74 20 63 6f 75 6e 74 56 61 6c 69 64 43 75 72 73 t countValidCurs
1bbe0 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 ors(BtShared *pB
1bbf0 74 2c 20 69 6e 74 20 77 72 4f 6e 6c 79 29 7b 0a t, int wrOnly){.
1bc00 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
1bc10 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 ;. int r = 0;.
1bc20 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 for(pCur=pBt->p
1bc30 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 Cursor; pCur; pC
1bc40 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b ur=pCur->pNext){
1bc50 0a 20 20 20 20 69 66 28 20 28 77 72 4f 6e 6c 79 . if( (wrOnly
1bc60 3d 3d 30 20 7c 7c 20 28 70 43 75 72 2d 3e 63 75 ==0 || (pCur->cu
1bc70 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 57 72 rFlags & BTCF_Wr
1bc80 69 74 65 46 6c 61 67 29 21 3d 30 29 0a 20 20 20 iteFlag)!=0).
1bc90 20 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 && pCur->eStat
1bca0 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e!=CURSOR_FAULT
1bcb0 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 ) r++; . }. re
1bcc0 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 turn r;.}.#endif
1bcd0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ../*.** If there
1bce0 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 are no outstand
1bcf0 69 6e 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 ing cursors and
1bd00 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 we are not in th
1bd10 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 e middle.** of a
1bd20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 transaction but
1bd30 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 there is a read
1bd40 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
1bd50 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 abase, then.** t
1bd60 68 69 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 his routine unre
1bd70 66 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 fs the first pag
1bd80 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1bd90 65 20 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a e file which .**
1bda0 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 has the effect
1bdb0 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 of releasing the
1bdc0 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a read lock..**.*
1bdd0 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 * If there is a
1bde0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 transaction in p
1bdf0 72 6f 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f rogress, this ro
1be00 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
1be10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1be20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
1be30 75 73 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 used(BtShared *p
1be40 42 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 Bt){. assert( s
1be50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1be60 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1be70 0a 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 . assert( count
1be80 56 61 6c 69 64 43 75 72 73 6f 72 73 28 70 42 74 ValidCursors(pBt
1be90 2c 30 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 ,0)==0 || pBt->i
1bea0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 nTransaction>TRA
1beb0 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 NS_NONE );. if(
1bec0 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1bed0 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ion==TRANS_NONE
1bee0 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d && pBt->pPage1!=
1bef0 30 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 0 ){. MemPage
1bf00 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e *pPage1 = pBt->
1bf10 70 50 61 67 65 31 3b 0a 20 20 20 20 61 73 73 65 pPage1;. asse
1bf20 72 74 28 20 70 50 61 67 65 31 2d 3e 61 44 61 74 rt( pPage1->aDat
1bf30 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
1bf40 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
1bf50 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
1bf60 72 29 3d 3d 31 20 29 3b 0a 20 20 20 20 70 42 74 r)==1 );. pBt
1bf70 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
1bf80 20 20 72 65 6c 65 61 73 65 50 61 67 65 4e 6f 74 releasePageNot
1bf90 4e 75 6c 6c 28 70 50 61 67 65 31 29 3b 0a 20 20 Null(pPage1);.
1bfa0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 }.}../*.** If pB
1bfb0 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 t points to an e
1bfc0 6d 70 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 mpty file then c
1bfd0 6f 6e 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 onvert that empt
1bfe0 79 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 y file.** into a
1bff0 20 6e 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 new empty datab
1c000 61 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a ase by initializ
1c010 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 ing the first pa
1c020 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 ge of.** the dat
1c030 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 abase..*/.static
1c040 20 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 int newDatabase
1c050 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
1c060 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b . MemPage *pP1;
1c070 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1c080 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 *data;. int rc
1c090 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1c0a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1c0b0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1c0c0 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 3e if( pBt->nPage>
1c0d0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1c0e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1c0f0 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 pP1 = pBt->pPag
1c100 65 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 e1;. assert( pP
1c110 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 1!=0 );. data =
1c120 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 pP1->aData;. r
1c130 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1c140 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 Write(pP1->pDbPa
1c150 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 ge);. if( rc )
1c160 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d return rc;. mem
1c170 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 cpy(data, zMagic
1c180 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a Header, sizeof(z
1c190 4d 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 MagicHeader));.
1c1a0 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
1c1b0 7a 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 zMagicHeader)==1
1c1c0 36 20 29 3b 0a 20 20 64 61 74 61 5b 31 36 5d 20 6 );. data[16]
1c1d0 3d 20 28 75 38 29 28 28 70 42 74 2d 3e 70 61 67 = (u8)((pBt->pag
1c1e0 65 53 69 7a 65 3e 3e 38 29 26 30 78 66 66 29 3b eSize>>8)&0xff);
1c1f0 0a 20 20 64 61 74 61 5b 31 37 5d 20 3d 20 28 75 . data[17] = (u
1c200 38 29 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 8)((pBt->pageSiz
1c210 65 3e 3e 31 36 29 26 30 78 66 66 29 3b 0a 20 20 e>>16)&0xff);.
1c220 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 data[18] = 1;.
1c230 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 data[19] = 1;.
1c240 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 assert( pBt->usa
1c250 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 bleSize<=pBt->pa
1c260 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75 geSize && pBt->u
1c270 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70 sableSize+255>=p
1c280 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
1c290 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29 data[20] = (u8)
1c2a0 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d (pBt->pageSize -
1c2b0 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
1c2c0 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20 );. data[21] =
1c2d0 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d 64;. data[22] =
1c2e0 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20 32;. data[23]
1c2f0 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26 = 32;. memset(&
1c300 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 data[24], 0, 100
1c310 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 -24);. zeroPage
1c320 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 (pP1, PTF_INTKEY
1c330 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 |PTF_LEAF|PTF_LE
1c340 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d AFDATA );. pBt-
1c350 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
1c360 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b _PAGESIZE_FIXED;
1c370 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1c380 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1c390 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 assert( pBt->a
1c3a0 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 utoVacuum==1 ||
1c3b0 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d pBt->autoVacuum=
1c3c0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1c3d0 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d pBt->incrVacuum=
1c3e0 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 =1 || pBt->incrV
1c3f0 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 acuum==0 );. pu
1c400 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 t4byte(&data[36
1c410 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 + 4*4], pBt->aut
1c420 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 oVacuum);. put4
1c430 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 byte(&data[36 +
1c440 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 7*4], pBt->incrV
1c450 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 acuum);.#endif.
1c460 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 31 3b pBt->nPage = 1;
1c470 0a 20 20 64 61 74 61 5b 33 31 5d 20 3d 20 31 3b . data[31] = 1;
1c480 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1c490 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e _OK;.}../*.** In
1c4a0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 66 69 72 itialize the fir
1c4b0 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 st page of the d
1c4c0 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 63 72 atabase file (cr
1c4d0 65 61 74 69 6e 67 20 61 20 64 61 74 61 62 61 73 eating a databas
1c4e0 65 0a 2a 2a 20 63 6f 6e 73 69 73 74 69 6e 67 20 e.** consisting
1c4f0 6f 66 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 of a single page
1c500 20 61 6e 64 20 6e 6f 20 73 63 68 65 6d 61 20 6f and no schema o
1c510 62 6a 65 63 74 73 29 2e 20 52 65 74 75 72 6e 20 bjects). Return
1c520 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 66 20 SQLITE_OK.** if
1c530 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 successful, or a
1c540 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
1c550 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a ode otherwise..*
1c560 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
1c570 65 65 4e 65 77 44 62 28 42 74 72 65 65 20 2a 70 eeNewDb(Btree *p
1c580 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
1c590 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1c5a0 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 6e (p);. p->pBt->n
1c5b0 50 61 67 65 20 3d 20 30 3b 0a 20 20 72 63 20 3d Page = 0;. rc =
1c5c0 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 2d 3e newDatabase(p->
1c5d0 70 42 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 pBt);. sqlite3B
1c5e0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1c5f0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1c600 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 .** Attempt to s
1c610 74 61 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 tart a new trans
1c620 61 63 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d action. A write-
1c630 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 transaction.** i
1c640 73 20 73 74 61 72 74 65 64 20 69 66 20 74 68 65 s started if the
1c650 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1c660 20 69 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 is nonzero, oth
1c670 65 72 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a erwise a read-.*
1c680 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 * transaction.
1c690 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
1c6a0 67 75 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d gument is 2 or m
1c6b0 6f 72 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 ore and exclusiv
1c6c0 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1c6d0 20 69 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 is started, mea
1c6e0 6e 69 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 ning that no oth
1c6f0 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c er process is al
1c700 6c 6f 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 lowed.** to acce
1c710 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ss the database.
1c720 20 20 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 A preexisting
1c730 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 transaction may
1c740 6e 6f 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 not be.** upgrad
1c750 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 ed to exclusive
1c760 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 by calling this
1c770 72 6f 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 routine a second
1c780 20 74 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 time - the.** e
1c790 78 63 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 xclusivity flag
1c7a0 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 only works for a
1c7b0 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e new transaction
1c7c0 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d ..**.** A write-
1c7d0 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
1c7e0 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f be started befo
1c7f0 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e re attempting an
1c800 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f y .** changes to
1c810 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
1c820 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c None of the foll
1c830 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a owing routines .
1c840 2a 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c ** will work unl
1c850 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f ess a transactio
1c860 6e 20 69 73 20 73 74 61 72 74 65 64 20 66 69 72 n is started fir
1c870 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 st:.**.** s
1c880 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
1c890 65 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 eTable().**
1c8a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 sqlite3BtreeCre
1c8b0 61 74 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 ateIndex().**
1c8c0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1c8d0 6c 65 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 learTable().**
1c8e0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1c8f0 44 72 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 DropTable().**
1c900 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1c910 49 6e 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 Insert().**
1c920 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
1c930 65 74 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ete().** sq
1c940 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
1c950 4d 65 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 Meta().**.** If
1c960 61 6e 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d an initial attem
1c970 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 pt to acquire th
1c980 65 20 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 e lock fails bec
1c990 61 75 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e ause of lock con
1c9a0 74 65 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 tention.** and t
1c9b0 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
1c9c0 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 previously unloc
1c9d0 6b 65 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 ked, then invoke
1c9e0 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
1c9f0 72 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 r.** if there is
1ca00 20 6f 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 one. But if th
1ca10 65 72 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 ere was previous
1ca20 6c 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 ly a read-lock,
1ca30 64 6f 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 do not.** invoke
1ca40 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
1ca50 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 r - just return
1ca60 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 SQLITE_BUSY. SQ
1ca70 4c 49 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a LITE_BUSY is .**
1ca80 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 returned when t
1ca90 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
1caa0 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f a read-lock in o
1cab0 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 rder to avoid a
1cac0 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 deadlock..**.**
1cad0 53 75 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 Suppose there ar
1cae0 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 e two processes
1caf0 41 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 A and B. A has
1cb00 61 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 a read lock and
1cb10 42 20 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 B has.** a reser
1cb20 76 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 ved lock. B tri
1cb30 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f es to promote to
1cb40 20 65 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 exclusive but i
1cb50 73 20 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 s blocked becaus
1cb60 65 0a 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 e.** of A's read
1cb70 20 6c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 lock. A tries
1cb80 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 to promote to re
1cb90 73 65 72 76 65 64 20 62 75 74 20 69 73 20 62 6c served but is bl
1cba0 6f 63 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f ocked by B..** O
1cbb0 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 ne or the other
1cbc0 6f 66 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 of the two proce
1cbd0 73 73 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 sses must give w
1cbe0 61 79 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 ay or there can
1cbf0 62 65 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 be.** no progres
1cc00 73 2e 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 s. By returning
1cc10 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 SQLITE_BUSY and
1cc20 20 6e 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 not invoking th
1cc30 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a e busy callback.
1cc40 2a 2a 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 ** when A alread
1cc50 79 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 y has a read loc
1cc60 6b 2c 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 k, we encourage
1cc70 41 20 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 A to give up and
1cc80 20 6c 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 let B.** procee
1cc90 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 d..*/.int sqlite
1cca0 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 3BtreeBeginTrans
1ccb0 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 (Btree *p, int w
1ccc0 72 66 6c 61 67 29 7b 0a 20 20 42 74 53 68 61 72 rflag){. BtShar
1ccd0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1cce0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
1ccf0 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 62 43 ITE_OK;. int bC
1cd00 6f 6e 63 75 72 72 65 6e 74 20 3d 20 28 70 2d 3e oncurrent = (p->
1cd10 64 62 2d 3e 62 43 6f 6e 63 75 72 72 65 6e 74 20 db->bConcurrent
1cd20 26 26 20 21 49 53 41 55 54 4f 56 41 43 55 55 4d && !ISAUTOVACUUM
1cd30 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );.. sqlite3Btr
1cd40 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 eeEnter(p);. bt
1cd50 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b reeIntegrity(p);
1cd60 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 74 .. /* If the bt
1cd70 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 ree is already i
1cd80 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 n a write-transa
1cd90 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20 2a ction, or it. *
1cda0 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 * is already in
1cdb0 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 a read-transacti
1cdc0 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d 74 72 on and a read-tr
1cdd0 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 ansaction. ** i
1cde0 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 69 s requested, thi
1cdf0 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 s is a no-op..
1ce00 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 */. if( p->inTr
1ce10 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1ce20 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d || (p->inTrans=
1ce30 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 20 21 =TRANS_READ && !
1ce40 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 20 67 wrflag) ){. g
1ce50 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
1ce60 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1ce70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1ce80 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c n==TRANS_WRITE |
1ce90 7c 20 49 66 4e 6f 74 4f 6d 69 74 41 56 28 70 42 | IfNotOmitAV(pB
1cea0 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 29 3d t->bDoTruncate)=
1ceb0 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 =0 );.. /* Writ
1cec0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 e transactions a
1ced0 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 re not possible
1cee0 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 on a read-only d
1cef0 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 atabase */. if(
1cf00 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 (pBt->btsFlags
1cf10 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 & BTS_READ_ONLY)
1cf20 21 3d 30 20 26 26 20 77 72 66 6c 61 67 20 29 7b !=0 && wrflag ){
1cf30 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1cf40 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 _READONLY;. g
1cf50 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
1cf60 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 . }..#ifndef SQ
1cf70 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1cf80 5f 43 41 43 48 45 0a 20 20 7b 0a 20 20 20 20 73 _CACHE. {. s
1cf90 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 3d qlite3 *pBlock =
1cfa0 20 30 3b 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 0;. /* If an
1cfb0 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 68 other database h
1cfc0 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64 andle has alread
1cfd0 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65 y opened a write
1cfe0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 transaction .
1cff0 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 ** on this sha
1d000 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 red-btree struct
1d010 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 ure and a second
1d020 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1d030 6f 6e 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71 on is. ** req
1d040 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 uested, return S
1d050 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 QLITE_LOCKED..
1d060 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 77 72 */. if( (wr
1d070 66 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 flag && pBt->inT
1d080 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
1d090 53 5f 57 52 49 54 45 29 0a 20 20 20 20 20 7c 7c S_WRITE). ||
1d0a0 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 (pBt->btsFlags
1d0b0 26 20 42 54 53 5f 50 45 4e 44 49 4e 47 29 21 3d & BTS_PENDING)!=
1d0c0 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 0. ){. p
1d0d0 42 6c 6f 63 6b 20 3d 20 70 42 74 2d 3e 70 57 72 Block = pBt->pWr
1d0e0 69 74 65 72 2d 3e 64 62 3b 0a 20 20 20 20 7d 65 iter->db;. }e
1d0f0 6c 73 65 20 69 66 28 20 77 72 66 6c 61 67 3e 31 lse if( wrflag>1
1d100 20 29 7b 0a 20 20 20 20 20 20 42 74 4c 6f 63 6b ){. BtLock
1d110 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 20 20 66 *pIter;. f
1d120 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c or(pIter=pBt->pL
1d130 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 ock; pIter; pIte
1d140 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b r=pIter->pNext){
1d150 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 . if( pIt
1d160 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b er->pBtree!=p ){
1d170 0a 20 20 20 20 20 20 20 20 20 20 70 42 6c 6f 63 . pBloc
1d180 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 k = pIter->pBtre
1d190 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 20 e->db;.
1d1a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1d1b0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1d1c0 20 20 20 20 69 66 28 20 70 42 6c 6f 63 6b 20 29 if( pBlock )
1d1d0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 {. sqlite3C
1d1e0 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
1d1f0 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b (p->db, pBlock);
1d200 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1d210 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
1d220 43 41 43 48 45 3b 0a 20 20 20 20 20 20 67 6f 74 CACHE;. got
1d230 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 o trans_begun;.
1d240 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1d250 0a 20 20 2f 2a 20 41 6e 79 20 72 65 61 64 2d 6f . /* Any read-o
1d260 6e 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69 74 nly or read-writ
1d270 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6d e transaction im
1d280 70 6c 69 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 plies a read-loc
1d290 6b 20 6f 6e 20 0a 20 20 2a 2a 20 70 61 67 65 20 k on . ** page
1d2a0 31 2e 20 53 6f 20 69 66 20 73 6f 6d 65 20 6f 74 1. So if some ot
1d2b0 68 65 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 her shared-cache
1d2c0 20 63 6c 69 65 6e 74 20 61 6c 72 65 61 64 79 20 client already
1d2d0 68 61 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b has a write-lock
1d2e0 20 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65 20 31 . ** on page 1
1d2f0 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f , the transactio
1d300 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e n cannot be open
1d310 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 ed. */. rc = qu
1d320 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
1d330 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 bleLock(p, MASTE
1d340 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 R_ROOT, READ_LOC
1d350 4b 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 K);. if( SQLITE
1d360 5f 4f 4b 21 3d 72 63 20 29 20 67 6f 74 6f 20 74 _OK!=rc ) goto t
1d370 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 70 rans_begun;.. p
1d380 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 Bt->btsFlags &=
1d390 7e 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 ~BTS_INITIALLY_E
1d3a0 4d 50 54 59 3b 0a 20 20 69 66 28 20 70 42 74 2d MPTY;. if( pBt-
1d3b0 3e 6e 50 61 67 65 3d 3d 30 20 29 20 70 42 74 2d >nPage==0 ) pBt-
1d3c0 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
1d3d0 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 _INITIALLY_EMPTY
1d3e0 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 2f 2a 20 ;. do {. /*
1d3f0 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65 65 28 29 Call lockBtree()
1d400 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 70 42 until either pB
1d410 74 2d 3e 70 50 61 67 65 31 20 69 73 20 70 6f 70 t->pPage1 is pop
1d420 75 6c 61 74 65 64 20 6f 72 0a 20 20 20 20 2a 2a ulated or. **
1d430 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 72 65 74 lockBtree() ret
1d440 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f urns something o
1d450 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 ther than SQLITE
1d460 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65 65 28 29 _OK. lockBtree()
1d470 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72 65 74 75 . ** may retu
1d480 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 62 75 74 rn SQLITE_OK but
1d490 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67 leave pBt->pPag
1d4a0 65 31 20 73 65 74 20 74 6f 20 30 20 69 66 20 61 e1 set to 0 if a
1d4b0 66 74 65 72 0a 20 20 20 20 2a 2a 20 72 65 61 64 fter. ** read
1d4c0 69 6e 67 20 70 61 67 65 20 31 20 69 74 20 64 69 ing page 1 it di
1d4d0 73 63 6f 76 65 72 73 20 74 68 61 74 20 74 68 65 scovers that the
1d4e0 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 68 page-size of th
1d4f0 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20 20 e database .
1d500 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 ** file is not p
1d510 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e 20 49 6e Bt->pageSize. In
1d520 20 74 68 69 73 20 63 61 73 65 20 6c 6f 63 6b 42 this case lockB
1d530 74 72 65 65 28 29 20 77 69 6c 6c 20 75 70 64 61 tree() will upda
1d540 74 65 0a 20 20 20 20 2a 2a 20 70 42 74 2d 3e 70 te. ** pBt->p
1d550 61 67 65 53 69 7a 65 20 74 6f 20 74 68 65 20 70 ageSize to the p
1d560 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 68 65 20 age-size of the
1d570 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 file on disk..
1d580 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 */. while(
1d590 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 26 pBt->pPage1==0 &
1d5a0 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 & SQLITE_OK==(rc
1d5b0 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28 70 42 74 = lockBtree(pBt
1d5c0 29 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 )) );.. if( r
1d5d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1d5e0 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 wrflag ){.
1d5f0 69 66 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 if( (pBt->btsFla
1d600 67 73 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e gs & BTS_READ_ON
1d610 4c 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 LY)!=0 ){.
1d620 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 rc = SQLITE_RE
1d630 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 ADONLY;. }e
1d640 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 lse{. int
1d650 20 65 78 46 6c 61 67 20 3d 20 62 43 6f 6e 63 75 exFlag = bConcu
1d660 72 72 65 6e 74 20 3f 20 2d 31 20 3a 20 28 77 72 rrent ? -1 : (wr
1d670 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 20 20 20 flag>1);.
1d680 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1d690 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 erBegin(pBt->pPa
1d6a0 67 65 72 2c 20 65 78 46 6c 61 67 2c 20 73 71 6c ger, exFlag, sql
1d6b0 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 ite3TempInMemory
1d6c0 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 (p->db));.
1d6d0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1d6e0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1d6f0 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 rc = newDatabas
1d700 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 e(pBt);.
1d710 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1d720 20 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1d730 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1d740 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 unlockBtreeIfU
1d750 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 nused(pBt);.
1d760 7d 0a 20 20 7d 77 68 69 6c 65 28 20 28 72 63 26 }. }while( (rc&
1d770 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 42 55 0xFF)==SQLITE_BU
1d780 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 SY && pBt->inTra
1d790 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
1d7a0 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 20 20 20 NONE &&.
1d7b0 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 btreeInvokeBus
1d7c0 79 48 61 6e 64 6c 65 72 28 70 42 74 29 20 29 3b yHandler(pBt) );
1d7d0 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
1d7e0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1d7f0 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
1d800 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 NS_NONE ){.
1d810 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
1d820 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 51 on++;.#ifndef SQ
1d830 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1d840 5f 43 41 43 48 45 0a 20 20 20 20 20 20 69 66 28 _CACHE. if(
1d850 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
1d860 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1d870 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d 3d p->lock.pBtree==
1d880 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 p && p->lock.iTa
1d890 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 ble==1 );.
1d8a0 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 p->lock.eLock
1d8b0 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 = READ_LOCK;.
1d8c0 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 p->lock.pNe
1d8d0 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b xt = pBt->pLock;
1d8e0 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 4c . pBt->pL
1d8f0 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a ock = &p->lock;.
1d900 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
1d910 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 }. p->inTr
1d920 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 ans = (wrflag?TR
1d930 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f ANS_WRITE:TRANS_
1d940 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 READ);. if( p
1d950 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 ->inTrans>pBt->i
1d960 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a nTransaction ){.
1d970 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 pBt->inTra
1d980 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e nsaction = p->in
1d990 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 20 Trans;. }.
1d9a0 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 if( wrflag ){.
1d9b0 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 MemPage *pP
1d9c0 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 age1 = pBt->pPag
1d9d0 65 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e1;.#ifndef SQLI
1d9e0 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
1d9f0 41 43 48 45 0a 20 20 20 20 20 20 61 73 73 65 72 ACHE. asser
1da00 74 28 20 21 70 42 74 2d 3e 70 57 72 69 74 65 72 t( !pBt->pWriter
1da10 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 );. pBt->p
1da20 57 72 69 74 65 72 20 3d 20 70 3b 0a 20 20 20 20 Writer = p;.
1da30 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 pBt->btsFlags
1da40 26 3d 20 7e 42 54 53 5f 45 58 43 4c 55 53 49 56 &= ~BTS_EXCLUSIV
1da50 45 3b 0a 20 20 20 20 20 20 69 66 28 20 77 72 66 E;. if( wrf
1da60 6c 61 67 3e 31 20 29 20 70 42 74 2d 3e 62 74 73 lag>1 ) pBt->bts
1da70 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 45 58 43 Flags |= BTS_EXC
1da80 4c 55 53 49 56 45 3b 0a 23 65 6e 64 69 66 0a 0a LUSIVE;.#endif..
1da90 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
1daa0 64 62 2d 73 69 7a 65 20 68 65 61 64 65 72 20 66 db-size header f
1dab0 69 65 6c 64 20 69 73 20 69 6e 63 6f 72 72 65 63 ield is incorrec
1dac0 74 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 t (as it may be
1dad0 69 66 20 61 6e 20 6f 6c 64 0a 20 20 20 20 20 20 if an old.
1dae0 2a 2a 20 63 6c 69 65 6e 74 20 68 61 73 20 62 65 ** client has be
1daf0 65 6e 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 en writing the d
1db00 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c 20 75 atabase file), u
1db10 70 64 61 74 65 20 69 74 20 6e 6f 77 2e 20 44 6f pdate it now. Do
1db20 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 ing. ** thi
1db30 73 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 20 s sooner rather
1db40 74 68 61 6e 20 6c 61 74 65 72 20 6d 65 61 6e 73 than later means
1db50 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 the database si
1db60 7a 65 20 63 61 6e 20 73 61 66 65 6c 79 20 0a 20 ze can safely .
1db70 20 20 20 20 20 2a 2a 20 72 65 2d 72 65 61 64 20 ** re-read
1db80 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a the database siz
1db90 65 20 66 72 6f 6d 20 70 61 67 65 20 31 20 69 66 e from page 1 if
1dba0 20 61 20 73 61 76 65 70 6f 69 6e 74 20 6f 72 20 a savepoint or
1dbb0 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 transaction.
1dbc0 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 ** rollback oc
1dbd0 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 curs within the
1dbe0 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 transaction..
1dbf0 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
1dc00 70 42 74 2d 3e 6e 50 61 67 65 21 3d 67 65 74 34 pBt->nPage!=get4
1dc10 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 byte(&pPage1->aD
1dc20 61 74 61 5b 32 38 5d 29 20 29 7b 0a 20 20 20 20 ata[28]) ){.
1dc30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1dc40 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
1dc50 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 1->pDbPage);.
1dc60 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1dc70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1dc80 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
1dc90 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d 2c age1->aData[28],
1dca0 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 pBt->nPage);.
1dcb0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1dcc0 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 74 72 61 6e }. }...tran
1dcd0 73 5f 62 65 67 75 6e 3a 0a 23 69 66 6e 64 65 66 s_begun:.#ifndef
1dce0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e SQLITE_OMIT_CON
1dcf0 43 55 52 52 45 4e 54 0a 20 20 69 66 28 20 62 43 CURRENT. if( bC
1dd00 6f 6e 63 75 72 72 65 6e 74 20 26 26 20 72 63 3d oncurrent && rc=
1dd10 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 71 =SQLITE_OK && sq
1dd20 6c 69 74 65 33 50 61 67 65 72 49 73 57 61 6c 28 lite3PagerIsWal(
1dd30 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29 7b 0a pBt->pPager) ){.
1dd40 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1dd50 50 61 67 65 72 42 65 67 69 6e 43 6f 6e 63 75 72 PagerBeginConcur
1dd60 72 65 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 rent(pBt->pPager
1dd70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1dd80 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c QLITE_OK && wrfl
1dd90 61 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ag ){. rc =
1dda0 20 62 74 72 65 65 50 74 72 6d 61 70 41 6c 6c 6f btreePtrmapAllo
1ddb0 63 61 74 65 28 70 42 74 29 3b 0a 20 20 20 20 7d cate(pBt);. }
1ddc0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 . }.#endif.. i
1ddd0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1dde0 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 && wrflag ){.
1ddf0 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d /* This call m
1de00 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 akes sure that t
1de10 68 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 he pager has the
1de20 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 correct number
1de30 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 of. ** open s
1de40 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 avepoints. If th
1de50 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
1de60 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 er is greater th
1de70 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 an 0 and. **
1de80 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
1de90 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
1dea0 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c pen, then it wil
1deb0 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 l be opened here
1dec0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
1ded0 20 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d nSavepoint = p-
1dee0 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b >db->nSavepoint;
1def0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1df00 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
1df10 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c int(pBt->pPager,
1df20 20 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 nSavepoint);.
1df30 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1df40 5f 4f 4b 20 26 26 20 6e 53 61 76 65 70 6f 69 6e _OK && nSavepoin
1df50 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 t ){. rc =
1df60 62 74 72 65 65 50 74 72 6d 61 70 42 65 67 69 6e btreePtrmapBegin
1df70 28 70 42 74 2c 20 6e 53 61 76 65 70 6f 69 6e 74 (pBt, nSavepoint
1df80 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1df90 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
1dfa0 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1dfb0 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
1dfc0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 urn rc;.}..#ifnd
1dfd0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1dfe0 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a 2a 2a UTOVACUUM../*.**
1dff0 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 Set the pointer
1e000 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 -map entries for
1e010 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 all children of
1e020 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 6c 73 page pPage. Als
1e030 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 20 63 o, if.** pPage c
1e040 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 74 68 ontains cells th
1e050 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 at point to over
1e060 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 74 20 flow pages, set
1e070 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d the pointer.** m
1e080 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 ap entries for t
1e090 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
1e0a0 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 74 s as well..*/.st
1e0b0 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 69 6c atic int setChil
1e0c0 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 67 65 dPtrmaps(MemPage
1e0d0 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 *pPage){. int
1e0e0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1e0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e100 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 61 62 * Counter variab
1e110 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c le */. int nCel
1e120 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
1e130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1e140 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e mber of cells in
1e150 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f 0a 20 page pPage */.
1e160 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
1e170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e180 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
1e190 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 de */. BtShared
1e1a0 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 *pBt = pPage->p
1e1b0 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 Bt;. Pgno pgno
1e1c0 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 0a = pPage->pgno;..
1e1d0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1e1e0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
1e1f0 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
1e200 29 3b 0a 20 20 72 63 20 3d 20 70 50 61 67 65 2d );. rc = pPage-
1e210 3e 69 73 49 6e 69 74 20 3f 20 53 51 4c 49 54 45 >isInit ? SQLITE
1e220 5f 4f 4b 20 3a 20 62 74 72 65 65 49 6e 69 74 50 _OK : btreeInitP
1e230 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66 age(pPage);. if
1e240 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1e250 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6e ) return rc;. n
1e260 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
1e270 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b ell;.. for(i=0;
1e280 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
1e290 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
1e2a0 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
1e2b0 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 70 50 i);.. ptrmapP
1e2c0 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c utOvflPtr(pPage,
1e2d0 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 0a 20 pCell, &rc);..
1e2e0 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
1e2f0 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e eaf ){. Pgn
1e300 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 o childPgno = ge
1e310 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 t4byte(pCell);.
1e320 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
1e330 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 Bt, childPgno, P
1e340 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e TRMAP_BTREE, pgn
1e350 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 o, &rc);. }.
1e360 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
1e370 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 ->leaf ){. Pg
1e380 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 no childPgno = g
1e390 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
1e3a0 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
1e3b0 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
1e3c0 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63 ptrmapPut(pBt, c
1e3d0 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50 hildPgno, PTRMAP
1e3e0 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72 _BTREE, pgno, &r
1e3f0 63 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 c);. }.. retur
1e400 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
1e410 6f 6d 65 77 68 65 72 65 20 6f 6e 20 70 50 61 67 omewhere on pPag
1e420 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 e is a pointer t
1e430 6f 20 70 61 67 65 20 69 46 72 6f 6d 2e 20 20 4d o page iFrom. M
1e440 6f 64 69 66 79 20 74 68 69 73 20 70 6f 69 6e 74 odify this point
1e450 65 72 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 er so.** that it
1e460 20 70 6f 69 6e 74 73 20 74 6f 20 69 54 6f 2e 20 points to iTo.
1e470 50 61 72 61 6d 65 74 65 72 20 65 54 79 70 65 20 Parameter eType
1e480 64 65 73 63 72 69 62 65 73 20 74 68 65 20 74 79 describes the ty
1e490 70 65 20 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f pe of pointer to
1e4a0 0a 2a 2a 20 62 65 20 6d 6f 64 69 66 69 65 64 2c .** be modified,
1e4b0 20 61 73 20 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a as follows:.**
1e4c0 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 .** PTRMAP_BTREE
1e4d0 3a 20 20 20 20 20 70 50 61 67 65 20 69 73 20 61 : pPage is a
1e4e0 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 btree-page. The
1e4f0 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 pointer points
1e500 61 74 20 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20 at a child .**
1e510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e520 20 70 61 67 65 20 6f 66 20 70 50 61 67 65 2e 0a page of pPage..
1e530 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 **.** PTRMAP_OVE
1e540 52 46 4c 4f 57 31 3a 20 70 50 61 67 65 20 69 73 RFLOW1: pPage is
1e550 20 61 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 a btree-page. T
1e560 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 he pointer point
1e570 73 20 61 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 s at an overflow
1e580 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1e590 20 20 20 20 20 20 70 61 67 65 20 70 6f 69 6e 74 page point
1e5a0 65 64 20 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20 ed to by one of
1e5b0 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61 the cells on pPa
1e5c0 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 ge..**.** PTRMAP
1e5d0 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 _OVERFLOW2: pPag
1e5e0 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e is an overflow
1e5f0 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 -page. The point
1e600 65 72 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 er points at the
1e610 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 next.**
1e620 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 overf
1e630 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
1e640 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
1e650 69 6e 74 20 6d 6f 64 69 66 79 50 61 67 65 50 6f int modifyPagePo
1e660 69 6e 74 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 inter(MemPage *p
1e670 50 61 67 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d Page, Pgno iFrom
1e680 2c 20 50 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65 , Pgno iTo, u8 e
1e690 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Type){. assert(
1e6a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1e6b0 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
1e6c0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1e6d0 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
1e6e0 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
1e6f0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
1e700 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
1e710 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b 0a AP_OVERFLOW2 ){.
1e720 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 /* The point
1e730 65 72 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 er is always the
1e740 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6f first 4 bytes o
1e750 66 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 f the page in th
1e760 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 is case. */.
1e770 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70 50 if( get4byte(pP
1e780 61 67 65 2d 3e 61 44 61 74 61 29 21 3d 69 46 72 age->aData)!=iFr
1e790 6f 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 om ){. retu
1e7a0 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1e7b0 54 5f 50 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 T_PGNO(pPage->pg
1e7c0 6e 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 no);. }. p
1e7d0 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 ut4byte(pPage->a
1e7e0 44 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 Data, iTo);. }e
1e7f0 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a lse{. int i;.
1e800 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 int nCell;.
1e810 20 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 20 20 int rc;..
1e820 72 63 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e rc = pPage->isIn
1e830 69 74 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a it ? SQLITE_OK :
1e840 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 btreeInitPage(p
1e850 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
1e860 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
1e870 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 nCell = pPage
1e880 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f ->nCell;.. fo
1e890 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
1e8a0 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a i++){. u8 *
1e8b0 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
1e8c0 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 (pPage, i);.
1e8d0 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
1e8e0 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b MAP_OVERFLOW1 ){
1e8f0 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 . CellInf
1e900 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 o info;.
1e910 70 50 61 67 65 2d 3e 78 50 61 72 73 65 43 65 6c pPage->xParseCel
1e920 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 l(pPage, pCell,
1e930 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 &info);.
1e940 69 66 28 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3c if( info.nLocal<
1e950 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 7b info.nPayload ){
1e960 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
1e970 43 65 6c 6c 2b 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cell+info.nSize
1e980 3e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 2b 70 > pPage->aData+p
1e990 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
1e9a0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 eSize ){.
1e9b0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1e9c0 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 TE_CORRUPT_PGNO(
1e9d0 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pPage->pgno);.
1e9e0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e9f0 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67 if( iFrom==g
1ea00 65 74 34 62 79 74 65 28 70 43 65 6c 6c 2b 69 6e et4byte(pCell+in
1ea10 66 6f 2e 6e 53 69 7a 65 2d 34 29 20 29 7b 0a 20 fo.nSize-4) ){.
1ea20 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 put4b
1ea30 79 74 65 28 70 43 65 6c 6c 2b 69 6e 66 6f 2e 6e yte(pCell+info.n
1ea40 53 69 7a 65 2d 34 2c 20 69 54 6f 29 3b 0a 20 20 Size-4, iTo);.
1ea50 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
1ea60 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1ea70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1ea80 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 se{. if(
1ea90 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d get4byte(pCell)=
1eaa0 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =iFrom ){.
1eab0 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 put4byte(pCe
1eac0 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 ll, iTo);.
1ead0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1eae0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1eaf0 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d }. . if( i=
1eb00 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 =nCell ){.
1eb10 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 if( eType!=PTRMA
1eb20 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 P_BTREE || .
1eb30 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 get4byte(&
1eb40 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
1eb50 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
1eb60 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 )!=iFrom ){.
1eb70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1eb80 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 E_CORRUPT_PGNO(p
1eb90 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 Page->pgno);.
1eba0 20 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 }. put4b
1ebb0 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
1ebc0 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
1ebd0 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 et+8], iTo);.
1ebe0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1ebf0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
1ec00 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 *.** Move the op
1ec10 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 en database page
1ec20 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 pDbPage to loca
1ec30 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 tion iFreePage i
1ec40 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 n the .** databa
1ec50 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 se. The pDbPage
1ec60 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e reference remain
1ec70 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 s valid..**.** T
1ec80 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 he isCommit flag
1ec90 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
1eca0 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
1ecb0 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 to remember tha
1ecc0 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c t.** the journal
1ecd0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
1ece0 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 c()ed before dat
1ecf0 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 abase page pDbPa
1ed00 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e ge->pgno .** can
1ed10 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 be written to.
1ed20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 The caller has a
1ed30 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 lready promised
1ed40 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 not to write to
1ed50 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f that.** page..*/
1ed60 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f .static int relo
1ed70 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 catePage(. BtSh
1ed80 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
1ed90 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f /* Btree */
1eda0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 . MemPage *pDbP
1edb0 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f age, /* O
1edc0 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 pen page to move
1edd0 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 */. u8 eType,
1ede0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1edf0 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 * Pointer map 't
1ee00 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 ype' entry for p
1ee10 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
1ee20 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 iPtrPage,
1ee30 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1ee40 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e map 'page-no' en
1ee50 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 try for pDbPage
1ee60 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 */. Pgno iFreeP
1ee70 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a age, /*
1ee80 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f The location to
1ee90 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f move pDbPage to
1eea0 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d */. int isComm
1eeb0 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f it /
1eec0 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 * isCommit flag
1eed0 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
1eee0 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a 3PagerMovepage *
1eef0 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a /.){. MemPage *
1ef00 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 pPtrPage; /* T
1ef10 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e he page that con
1ef20 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
1ef30 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 to pDbPage */.
1ef40 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 Pgno iDbPage = p
1ef50 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 DbPage->pgno;.
1ef60 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
1ef70 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 pBt->pPager;. i
1ef80 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
1ef90 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
1efa0 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 OVERFLOW2 || eTy
1efb0 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 pe==PTRMAP_OVERF
1efc0 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 LOW1 || . e
1efd0 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 Type==PTRMAP_BTR
1efe0 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 EE || eType==PTR
1eff0 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a MAP_ROOTPAGE );.
1f000 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1f010 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1f020 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1f030 73 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 sert( pDbPage->p
1f040 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a Bt==pBt );.. /*
1f050 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 Move page iDbPa
1f060 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 ge from its curr
1f070 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 ent location to
1f080 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 page number iFre
1f090 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 ePage */. TRACE
1f0a0 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d (("AUTOVACUUM: M
1f0b0 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 oving %d to free
1f0c0 20 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61 page %d (ptr pa
1f0d0 67 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e ge %d type %d)\n
1f0e0 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 ", . iDbPag
1f0f0 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 e, iFreePage, iP
1f100 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b trPage, eType));
1f110 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
1f120 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 agerMovepage(pPa
1f130 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 ger, pDbPage->pD
1f140 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
1f150 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 , isCommit);. i
1f160 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1f170 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
1f180 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 c;. }. pDbPage
1f190 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 ->pgno = iFreePa
1f1a0 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 ge;.. /* If pDb
1f1b0 50 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 Page was a btree
1f1c0 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d -page, then it m
1f1d0 61 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 ay have child pa
1f1e0 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 ges and/or cells
1f1f0 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 . ** that point
1f200 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
1f210 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 es. The pointer
1f220 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 map entries for
1f230 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 all these. ** p
1f240 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 ages need to be
1f250 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 changed.. **.
1f260 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 ** If pDbPage is
1f270 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
1f280 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 e, then the firs
1f290 74 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 t 4 bytes may st
1f2a0 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 ore a. ** point
1f2b0 65 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 er to a subseque
1f2c0 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 nt overflow page
1f2d0 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 . If this is the
1f2e0 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a case, then. **
1f2f0 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
1f300 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 needs to be upd
1f310 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 ated for the sub
1f320 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 sequent overflow
1f330 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 page.. */. if
1f340 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
1f350 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d BTREE || eType==
1f360 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 PTRMAP_ROOTPAGE
1f370 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 ){. rc = setC
1f380 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 hildPtrmaps(pDbP
1f390 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
1f3a0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1f3b0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1f3c0 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1f3d0 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c Pgno nextOvfl
1f3e0 20 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 = get4byte(pDbP
1f3f0 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 age->aData);.
1f400 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 if( nextOvfl!=0
1f410 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ){. ptrmap
1f420 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 Put(pBt, nextOvf
1f430 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c l, PTRMAP_OVERFL
1f440 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20 OW2, iFreePage,
1f450 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 &rc);. if(
1f460 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1f470 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1f480 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1f490 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 }. }.. /* Fix
1f4a0 74 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 the database poi
1f4b0 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 nter on page iPt
1f4c0 72 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 rPage that point
1f4d0 65 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f ed at iDbPage so
1f4e0 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f . ** that it po
1f4f0 69 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 ints at iFreePag
1f500 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 e. Also fix the
1f510 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
1f520 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 y for. ** iPtrP
1f530 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 age.. */. if(
1f540 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f eType!=PTRMAP_RO
1f550 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 OTPAGE ){. rc
1f560 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
1f570 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 pBt, iPtrPage, &
1f580 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 pPtrPage, 0);.
1f590 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f5a0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
1f5b0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1f5c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1f5d0 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 gerWrite(pPtrPag
1f5e0 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
1f5f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1f600 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 OK ){. rele
1f610 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 asePage(pPtrPage
1f620 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1f630 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 rc;. }. rc
1f640 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 = modifyPagePoi
1f650 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 nter(pPtrPage, i
1f660 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 DbPage, iFreePag
1f670 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 e, eType);. r
1f680 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 eleasePage(pPtrP
1f690 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
1f6a0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1f6b0 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
1f6c0 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 Bt, iFreePage, e
1f6d0 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Type, iPtrPage,
1f6e0 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a &rc);. }. }.
1f6f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f700 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 /* Forward decla
1f710 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 ration required
1f720 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 by incrVacuumSte
1f730 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 p(). */.static i
1f740 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 nt allocateBtree
1f750 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c Page(BtShared *,
1f760 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e MemPage **, Pgn
1f770 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a o *, Pgno, u8);.
1f780 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 ./*.** Perform a
1f790 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 single step of
1f7a0 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 an incremental-v
1f7b0 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 acuum. If succes
1f7c0 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 sful, return.**
1f7d0 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 SQLITE_OK. If th
1f7e0 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 ere is no work t
1f7f0 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 o do (and theref
1f800 6f 72 65 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 ore no point in
1f810 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 .** calling this
1f820 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 function again)
1f830 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
1f840 44 4f 4e 45 2e 20 4f 72 2c 20 69 66 20 61 6e 20 DONE. Or, if an
1f850 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 error .** occurs
1f860 2c 20 72 65 74 75 72 6e 20 73 6f 6d 65 20 6f 74 , return some ot
1f870 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a her error code..
1f880 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 **.** More speci
1f890 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 66 75 fically, this fu
1f8a0 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 nction attempts
1f8b0 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65 20 74 to re-organize t
1f8c0 68 65 20 64 61 74 61 62 61 73 65 20 73 6f 20 0a he database so .
1f8d0 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 61 73 74 ** that the last
1f8e0 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c page of the fil
1f8f0 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
1f900 73 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 se is no longer
1f910 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 in use..**.** Pa
1f920 72 61 6d 65 74 65 72 20 6e 46 69 6e 20 69 73 20 rameter nFin is
1f930 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
1f940 67 65 73 20 74 68 61 74 20 74 68 69 73 20 64 61 ges that this da
1f950 74 61 62 61 73 65 20 77 6f 75 6c 64 20 63 6f 6e tabase would con
1f960 74 61 69 6e 0a 2a 2a 20 77 65 72 65 20 74 68 69 tain.** were thi
1f970 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 s function calle
1f980 64 20 75 6e 74 69 6c 20 69 74 20 72 65 74 75 72 d until it retur
1f990 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a ns SQLITE_DONE..
1f9a0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 43 6f **.** If the bCo
1f9b0 6d 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 69 mmit parameter i
1f9c0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 s non-zero, this
1f9d0 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
1f9e0 73 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 63 s that the .** c
1f9f0 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 aller will keep
1fa00 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 calling incrVacu
1fa10 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 20 69 umStep() until i
1fa20 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 t returns SQLITE
1fa30 5f 44 4f 4e 45 20 0a 2a 2a 20 6f 72 20 61 6e 20 _DONE .** or an
1fa40 65 72 72 6f 72 2e 20 62 43 6f 6d 6d 69 74 20 69 error. bCommit i
1fa50 73 20 70 61 73 73 65 64 20 74 72 75 65 20 66 6f s passed true fo
1fa60 72 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d r an auto-vacuum
1fa70 2d 6f 6e 2d 63 6f 6d 6d 69 74 20 0a 2a 2a 20 6f -on-commit .** o
1fa80 70 65 72 61 74 69 6f 6e 2c 20 6f 72 20 66 61 6c peration, or fal
1fa90 73 65 20 66 6f 72 20 61 6e 20 69 6e 63 72 65 6d se for an increm
1faa0 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2f ental vacuum..*/
1fab0 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 72 .static int incr
1fac0 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 61 VacuumStep(BtSha
1fad0 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e red *pBt, Pgno n
1fae0 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50 Fin, Pgno iLastP
1faf0 67 2c 20 69 6e 74 20 62 43 6f 6d 6d 69 74 29 7b g, int bCommit){
1fb00 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 . Pgno nFreeLis
1fb10 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
1fb20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
1fb30 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 still on the fre
1fb40 65 2d 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e-list */. int
1fb50 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 rc;.. assert( s
1fb60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1fb70 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1fb80 0a 20 20 61 73 73 65 72 74 28 20 69 4c 61 73 74 . assert( iLast
1fb90 50 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 Pg>nFin );.. if
1fba0 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 ( !PTRMAP_ISPAGE
1fbb0 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 (pBt, iLastPg) &
1fbc0 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 & iLastPg!=PENDI
1fbd0 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1fbe0 29 20 29 7b 0a 20 20 20 20 75 38 20 65 54 79 70 ) ){. u8 eTyp
1fbf0 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 72 e;. Pgno iPtr
1fc00 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 Page;.. nFree
1fc10 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 28 List = get4byte(
1fc20 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 &pBt->pPage1->aD
1fc30 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 ata[36]);. if
1fc40 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 ( nFreeList==0 )
1fc50 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1fc60 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
1fc70 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d }.. rc = ptrm
1fc80 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73 74 apGet(pBt, iLast
1fc90 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 Pg, &eType, &iPt
1fca0 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 rPage);. if(
1fcb0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1fcc0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1fcd0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1fce0 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f eType==PTRMAP_RO
1fcf0 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 OTPAGE ){.
1fd00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1fd10 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
1fd20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 }.. if( eType
1fd30 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 ==PTRMAP_FREEPAG
1fd40 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 62 E ){. if( b
1fd50 43 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 Commit==0 ){.
1fd60 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 /* Remove t
1fd70 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 he page from the
1fd80 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 files free-list
1fd90 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 . This is not re
1fda0 71 75 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a quired. *
1fdb0 2a 20 69 66 20 62 43 6f 6d 6d 69 74 20 69 73 20 * if bCommit is
1fdc0 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 non-zero. In tha
1fdd0 74 20 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 t case, the free
1fde0 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 -list will be.
1fdf0 20 20 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 ** truncat
1fe00 65 64 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 ed to zero after
1fe10 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
1fe20 65 74 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f eturns, so it do
1fe30 65 73 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a esn't . *
1fe40 2a 20 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 * matter if it s
1fe50 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f till contains so
1fe60 6d 65 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 me garbage entri
1fe70 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 es.. */.
1fe80 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 Pgno iFre
1fe90 65 50 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d ePg;. Mem
1fea0 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 Page *pFreePg;.
1feb0 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f rc = allo
1fec0 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
1fed0 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 t, &pFreePg, &iF
1fee0 72 65 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 reePg, iLastPg,
1fef0 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 29 3b 0a BTALLOC_EXACT);.
1ff00 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1ff10 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ff20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1ff30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1ff40 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 assert( iFre
1ff50 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a ePg==iLastPg );.
1ff60 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1ff70 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 age(pFreePg);.
1ff80 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 }. } else
1ff90 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 {. Pgno iF
1ffa0 72 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 reePg;
1ffb0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 /* Index of f
1ffc0 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 ree page to move
1ffd0 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 pLastPg to */.
1ffe0 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c MemPage *pL
1fff0 61 73 74 50 67 3b 0a 20 20 20 20 20 20 75 38 20 astPg;. u8
20000 65 4d 6f 64 65 20 3d 20 42 54 41 4c 4c 4f 43 5f eMode = BTALLOC_
20010 41 4e 59 3b 20 20 20 2f 2a 20 4d 6f 64 65 20 70 ANY; /* Mode p
20020 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6c 6c arameter for all
20030 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 29 ocateBtreePage()
20040 20 2a 2f 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 */. Pgno i
20050 4e 65 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 Near = 0;
20060 20 20 20 20 2f 2a 20 6e 65 61 72 62 79 20 70 61 /* nearby pa
20070 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6c 6c 6f rameter for allo
20080 63 61 74 65 42 74 72 65 65 50 61 67 65 28 29 20 cateBtreePage()
20090 2a 2f 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 */.. rc = b
200a0 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
200b0 20 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 iLastPg, &pLast
200c0 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 Pg, 0);. if
200d0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
200e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
200f0 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 n rc;. }..
20100 20 20 20 20 20 2f 2a 20 49 66 20 62 43 6f 6d 6d /* If bComm
20110 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 it is zero, this
20120 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 loop runs exact
20130 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 ly once and page
20140 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a pLastPg. *
20150 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74 * is swapped wit
20160 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 h the first free
20170 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 page pulled off
20180 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a the free list..
20190 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
201a0 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 * On the other h
201b0 61 6e 64 2c 20 69 66 20 62 43 6f 6d 6d 69 74 20 and, if bCommit
201c0 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
201d0 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a zero, then keep.
201e0 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 ** looping
201f0 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 until a free-pa
20200 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 ge located withi
20210 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e n the first nFin
20220 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 pages. **
20230 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 of the file is f
20240 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ound.. */.
20250 20 20 20 20 20 69 66 28 20 62 43 6f 6d 6d 69 74 if( bCommit
20260 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65 ==0 ){. e
20270 4d 6f 64 65 20 3d 20 42 54 41 4c 4c 4f 43 5f 4c Mode = BTALLOC_L
20280 45 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 61 72 E;. iNear
20290 20 3d 20 6e 46 69 6e 3b 0a 20 20 20 20 20 20 7d = nFin;. }
202a0 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 . do {.
202b0 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 MemPage *pFr
202c0 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 eePg;. rc
202d0 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
202e0 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
202f0 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4e Pg, &iFreePg, iN
20300 65 61 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 ear, eMode);.
20310 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
20320 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
20330 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
20340 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 pLastPg);.
20350 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
20360 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
20370 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 releasePage(pFr
20380 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 eePg);. }wh
20390 69 6c 65 28 20 62 43 6f 6d 6d 69 74 20 26 26 20 ile( bCommit &&
203a0 69 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a iFreePg>nFin );.
203b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 assert( iF
203c0 72 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b reePg<iLastPg );
203d0 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 . . rc
203e0 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
203f0 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 pBt, pLastPg, eT
20400 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 ype, iPtrPage, i
20410 46 72 65 65 50 67 2c 20 62 43 6f 6d 6d 69 74 29 FreePg, bCommit)
20420 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 ;. releaseP
20430 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 age(pLastPg);.
20440 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
20450 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
20460 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
20470 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
20480 20 69 66 28 20 62 43 6f 6d 6d 69 74 3d 3d 30 20 if( bCommit==0
20490 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 ){. do {.
204a0 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 iLastPg--;.
204b0 20 7d 77 68 69 6c 65 28 20 69 4c 61 73 74 50 67 }while( iLastPg
204c0 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
204d0 41 47 45 28 70 42 74 29 20 7c 7c 20 50 54 52 4d AGE(pBt) || PTRM
204e0 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 AP_ISPAGE(pBt, i
204f0 4c 61 73 74 50 67 29 20 29 3b 0a 20 20 20 20 70 LastPg) );. p
20500 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 20 Bt->bDoTruncate
20510 3d 20 31 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 50 = 1;. pBt->nP
20520 61 67 65 20 3d 20 69 4c 61 73 74 50 67 3b 0a 20 age = iLastPg;.
20530 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
20540 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
20550 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 The database ope
20560 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ned by the first
20570 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 argument is an
20580 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
20590 62 61 73 65 0a 2a 2a 20 6e 4f 72 69 67 20 70 61 base.** nOrig pa
205a0 67 65 73 20 69 6e 20 73 69 7a 65 20 63 6f 6e 74 ges in size cont
205b0 61 69 6e 69 6e 67 20 6e 46 72 65 65 20 66 72 65 aining nFree fre
205c0 65 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 e pages. Return
205d0 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a the expected .**
205e0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
205f0 61 62 61 73 65 20 69 6e 20 70 61 67 65 73 20 66 abase in pages f
20600 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 61 75 74 6f ollowing an auto
20610 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74 69 6f -vacuum operatio
20620 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e n..*/.static Pgn
20630 6f 20 66 69 6e 61 6c 44 62 53 69 7a 65 28 42 74 o finalDbSize(Bt
20640 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
20650 6f 20 6e 4f 72 69 67 2c 20 50 67 6e 6f 20 6e 46 o nOrig, Pgno nF
20660 72 65 65 29 7b 0a 20 20 69 6e 74 20 6e 45 6e 74 ree){. int nEnt
20670 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ry;
20680 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
20690 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f 6e 20 r of entries on
206a0 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67 65 20 one ptrmap page
206b0 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 */. Pgno nPtrma
206c0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
206d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
206e0 66 20 50 74 72 4d 61 70 20 70 61 67 65 73 20 74 f PtrMap pages t
206f0 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 o be freed */.
20700 50 67 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 Pgno nFin;
20710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20720 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 /* Return value
20730 2a 2f 0a 0a 20 20 6e 45 6e 74 72 79 20 3d 20 70 */.. nEntry = p
20740 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 Bt->usableSize/5
20750 3b 0a 20 20 6e 50 74 72 6d 61 70 20 3d 20 28 6e ;. nPtrmap = (n
20760 46 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 Free-nOrig+PTRMA
20770 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e 4f P_PAGENO(pBt, nO
20780 72 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e rig)+nEntry)/nEn
20790 74 72 79 3b 0a 20 20 6e 46 69 6e 20 3d 20 6e 4f try;. nFin = nO
207a0 72 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 rig - nFree - nP
207b0 74 72 6d 61 70 3b 0a 20 20 69 66 28 20 6e 4f 72 trmap;. if( nOr
207c0 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ig>PENDING_BYTE_
207d0 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 PAGE(pBt) && nFi
207e0 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 n<PENDING_BYTE_P
207f0 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
20800 6e 46 69 6e 2d 2d 3b 0a 20 20 7d 0a 20 20 77 68 nFin--;. }. wh
20810 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 ile( PTRMAP_ISPA
20820 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c GE(pBt, nFin) ||
20830 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 nFin==PENDING_B
20840 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
20850 0a 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 7d . nFin--;. }
20860 0a 0a 20 20 72 65 74 75 72 6e 20 6e 46 69 6e 3b .. return nFin;
20870 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 .}../*.** A writ
20880 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 e-transaction mu
20890 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 st be opened bef
208a0 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 ore calling this
208b0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 function..** It
208c0 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 performs a sing
208d0 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 le unit of work
208e0 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 towards an incre
208f0 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a mental vacuum..*
20900 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 *.** If the incr
20910 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 emental vacuum i
20920 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 s finished after
20930 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 this function h
20940 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 as run,.** SQLIT
20950 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e E_DONE is return
20960 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 ed. If it is not
20970 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e finished, but n
20980 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 o error occurred
20990 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ,.** SQLITE_OK i
209a0 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
209b0 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 rwise an SQLite
209c0 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a error code. .*/.
209d0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
209e0 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 IncrVacuum(Btree
209f0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
20a00 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
20a10 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c = p->pBt;.. sql
20a20 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
20a30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
20a40 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
20a50 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 20 =TRANS_WRITE &&
20a60 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
20a70 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 S_WRITE );. if(
20a80 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 !pBt->autoVacuu
20a90 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 m ){. rc = SQ
20aa0 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c LITE_DONE;. }el
20ab0 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 se{. Pgno nOr
20ac0 69 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f ig = btreePageco
20ad0 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 50 67 unt(pBt);. Pg
20ae0 6e 6f 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 no nFree = get4b
20af0 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
20b00 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 ->aData[36]);.
20b10 20 20 50 67 6e 6f 20 6e 46 69 6e 20 3d 20 66 69 Pgno nFin = fi
20b20 6e 61 6c 44 62 53 69 7a 65 28 70 42 74 2c 20 6e nalDbSize(pBt, n
20b30 4f 72 69 67 2c 20 6e 46 72 65 65 29 3b 0a 0a 20 Orig, nFree);..
20b40 20 20 20 69 66 28 20 6e 4f 72 69 67 3c 6e 46 69 if( nOrig<nFi
20b50 6e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 n ){. rc =
20b60 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
20b70 4b 50 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 KPT;. }else i
20b80 66 28 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 20 f( nFree>0 ){.
20b90 20 20 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c rc = saveAll
20ba0 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 Cursors(pBt, 0,
20bb0 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
20bc0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
20bd0 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 invalidat
20be0 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 eAllOverflowCach
20bf0 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 e(pBt);.
20c00 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 rc = incrVacuumS
20c10 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 6e tep(pBt, nFin, n
20c20 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 Orig, 0);.
20c30 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
20c40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
20c50 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
20c60 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3PagerWrite(pBt-
20c70 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 >pPage1->pDbPage
20c80 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 );. put4b
20c90 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
20ca0 2d 3e 61 44 61 74 61 5b 32 38 5d 2c 20 70 42 74 ->aData[28], pBt
20cb0 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 ->nPage);.
20cc0 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
20cd0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
20ce0 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ONE;. }. }.
20cf0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
20d00 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
20d10 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
20d20 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
20d30 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c led prior to sql
20d40 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 ite3PagerCommit
20d50 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 when a transacti
20d60 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 on.** is committ
20d70 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 ed for an auto-v
20d80 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a acuum database..
20d90 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f **.** If SQLITE_
20da0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 OK is returned,
20db0 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 then *pnTrunc is
20dc0 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 set to the numb
20dd0 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 er of pages.** t
20de0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
20df0 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 should be trunc
20e00 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 ated to during t
20e10 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 he commit proces
20e20 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 s. .** i.e. the
20e30 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 database has bee
20e40 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f n reorganized so
20e50 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 that only the f
20e60 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a irst *pnTrunc.**
20e70 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 pages are in us
20e80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
20e90 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 autoVacuumCommi
20ea0 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
20eb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
20ec0 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 ITE_OK;. Pager
20ed0 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 *pPager = pBt->p
20ee0 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c Pager;. VVA_ONL
20ef0 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 Y( int nRef = sq
20f00 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
20f10 6e 74 28 70 50 61 67 65 72 29 3b 20 29 0a 0a 20 nt(pPager); )..
20f20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
20f30 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
20f40 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 >mutex) );. inv
20f50 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
20f60 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 owCache(pBt);.
20f70 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f assert(pBt->auto
20f80 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 Vacuum);. if( !
20f90 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
20fa0 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e ){. Pgno nFin
20fb0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
20fc0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
20fd0 64 61 74 61 62 61 73 65 20 61 66 74 65 72 20 61 database after a
20fe0 75 74 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a utovacuuming */.
20ff0 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 Pgno nFree;
21000 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
21010 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 of pages on the
21020 20 66 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 freelist initia
21030 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 lly */. Pgno
21040 69 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a iFree; /*
21050 20 54 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 The next page t
21060 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 o be freed */.
21070 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 Pgno nOrig;
21080 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
21090 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65 size before fre
210a0 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e 4f 72 eing */.. nOr
210b0 69 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f ig = btreePageco
210c0 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 69 66 unt(pBt);. if
210d0 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 ( PTRMAP_ISPAGE(
210e0 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e pBt, nOrig) || n
210f0 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 Orig==PENDING_BY
21100 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a TE_PAGE(pBt) ){.
21110 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e /* It is n
21120 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 ot possible to c
21130 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65 reate a database
21140 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 for which the f
21150 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 inal page.
21160 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70 ** is either a p
21170 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 ointer-map page
21180 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 or the pending-b
21190 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 yte page. If one
211a0 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 . ** is enc
211b0 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 ountered, this i
211c0 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 ndicates corrupt
211d0 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
211e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
211f0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
21200 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 }.. nFree
21210 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 = get4byte(&pBt
21220 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
21230 33 36 5d 29 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 36]);. nFin =
21240 20 66 69 6e 61 6c 44 62 53 69 7a 65 28 70 42 74 finalDbSize(pBt
21250 2c 20 6e 4f 72 69 67 2c 20 6e 46 72 65 65 29 3b , nOrig, nFree);
21260 0a 20 20 20 20 69 66 28 20 6e 46 69 6e 3e 6e 4f . if( nFin>nO
21270 72 69 67 20 29 20 72 65 74 75 72 6e 20 53 51 4c rig ) return SQL
21280 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
21290 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 6e 3c 6e ;. if( nFin<n
212a0 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 63 Orig ){. rc
212b0 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 = saveAllCursor
212c0 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20 20 s(pBt, 0, 0);.
212d0 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 46 72 65 }. for(iFre
212e0 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65 65 3e 6e e=nOrig; iFree>n
212f0 46 69 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 Fin && rc==SQLIT
21300 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d 29 7b 0a E_OK; iFree--){.
21310 20 20 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 rc = incrV
21320 61 63 75 75 6d 53 74 65 70 28 70 42 74 2c 20 6e acuumStep(pBt, n
21330 46 69 6e 2c 20 69 46 72 65 65 2c 20 31 29 3b 0a Fin, iFree, 1);.
21340 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72 }. if( (r
21350 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c c==SQLITE_DONE |
21360 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 | rc==SQLITE_OK)
21370 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 && nFree>0 ){.
21380 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
21390 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3PagerWrite(pBt-
213a0 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 >pPage1->pDbPage
213b0 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 );. put4byt
213c0 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
213d0 61 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 aData[32], 0);.
213e0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
213f0 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
21400 61 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 a[36], 0);.
21410 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
21420 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 pPage1->aData[28
21430 5d 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 20 20 ], nFin);.
21440 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 pBt->bDoTruncate
21450 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 42 74 2d = 1;. pBt-
21460 3e 6e 50 61 67 65 20 3d 20 6e 46 69 6e 3b 0a 20 >nPage = nFin;.
21470 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
21480 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
21490 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
214a0 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 Rollback(pPager)
214b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 ;. }. }.. a
214c0 73 73 65 72 74 28 20 6e 52 65 66 3e 3d 73 71 6c ssert( nRef>=sql
214d0 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
214e0 74 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 t(pPager) );. r
214f0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c eturn rc;.}..#el
21500 73 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c se /* ifndef SQL
21510 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
21520 55 55 4d 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 UUM */.# define
21530 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
21540 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e x) SQLITE_OK.#en
21550 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
21560 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 55 52 52 ITE_OMIT_CONCURR
21570 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 ENT./*.** This f
21580 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
21590 64 20 61 73 20 70 61 72 74 20 6f 66 20 6d 65 72 d as part of mer
215a0 67 69 6e 67 20 61 6e 20 43 4f 4e 43 55 52 52 45 ging an CONCURRE
215b0 4e 54 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 NT transaction w
215c0 69 74 68 0a 2a 2a 20 74 68 65 20 73 6e 61 70 73 ith.** the snaps
215d0 68 6f 74 20 61 74 20 74 68 65 20 68 65 61 64 20 hot at the head
215e0 6f 66 20 74 68 65 20 77 61 6c 20 66 69 6c 65 2e of the wal file.
215f0 20 49 74 20 72 65 6c 6f 63 61 74 65 73 20 61 6c It relocates al
21600 6c 20 70 61 67 65 73 20 69 6e 20 74 68 65 0a 2a l pages in the.*
21610 2a 20 72 61 6e 67 65 20 69 46 69 72 73 74 2e 2e * range iFirst..
21620 69 4c 61 73 74 2c 20 69 6e 63 6c 75 73 69 76 65 iLast, inclusive
21630 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 . It is assumed
21640 74 68 61 74 20 74 68 65 20 42 74 72 65 65 50 74 that the BtreePt
21650 72 6d 61 70 20 0a 2a 2a 20 73 74 72 75 63 74 75 rmap .** structu
21660 72 65 20 61 74 20 42 74 53 68 61 72 65 64 2e 70 re at BtShared.p
21670 4d 61 70 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 Map contains the
21680 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 location of the
21690 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 65 61 63 pointers to eac
216a0 68 0a 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 h.** page in the
216b0 20 72 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 range..**.** If
216c0 20 70 6e 43 75 72 72 65 6e 74 20 69 73 20 4e 55 pnCurrent is NU
216d0 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 LL, then all pag
216e0 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 es in the range
216f0 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 63 75 72 are moved to cur
21700 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 65 65 20 6c rently.** free l
21710 6f 63 61 74 69 6f 6e 73 20 28 69 2e 65 2e 20 66 ocations (i.e. f
21720 72 65 65 2d 6c 69 73 74 20 65 6e 74 72 69 65 73 ree-list entries
21730 29 20 77 69 74 68 69 6e 20 74 68 65 20 64 61 74 ) within the dat
21740 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
21750 65 20 70 61 67 65 0a 2a 2a 20 69 46 69 72 73 74 e page.** iFirst
21760 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 20 70 ..**.** Or, if p
21770 6e 43 75 72 72 65 6e 74 20 69 73 20 6e 6f 74 20 nCurrent is not
21780 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 70 6f NULL, then it po
21790 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 75 65 20 ints to a value
217a0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a containing the.*
217b0 2a 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f * current size o
217c0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
217d0 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 6e ile in pages. In
217e0 20 74 68 69 73 20 63 61 73 65 2c 20 61 6c 6c 20 this case, all
217f0 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 72 65 6c pages are.** rel
21800 6f 63 61 74 65 64 20 74 6f 20 74 68 65 20 65 6e ocated to the en
21810 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 d of the databas
21820 65 20 66 69 6c 65 20 2d 20 70 61 67 65 20 69 46 e file - page iF
21830 69 72 73 74 20 69 73 20 72 65 6c 6f 63 61 74 65 irst is relocate
21840 64 20 74 6f 0a 2a 2a 20 70 61 67 65 20 28 2a 70 d to.** page (*p
21850 6e 43 75 72 72 65 6e 74 2b 31 29 2c 20 70 61 67 nCurrent+1), pag
21860 65 20 69 46 69 72 73 74 2b 31 20 74 6f 20 70 61 e iFirst+1 to pa
21870 67 65 20 28 2a 70 6e 43 75 72 72 65 6e 74 2b 32 ge (*pnCurrent+2
21880 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a ), and so on..**
21890 20 56 61 6c 75 65 20 2a 70 6e 43 75 72 72 65 6e Value *pnCurren
218a0 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 t is set to the
218b0 6e 65 77 20 73 69 7a 65 20 6f 66 20 74 68 65 20 new size of the
218c0 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 database before
218d0 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f this .** functio
218e0 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a n returns..**.**
218f0 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 If no error occ
21900 75 72 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 urs, SQLITE_OK i
21910 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
21920 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 rwise, an SQLite
21930 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a error code..*/.
21940 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
21950 52 65 6c 6f 63 61 74 65 52 61 6e 67 65 28 0a 20 RelocateRange(.
21960 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
21970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21980 20 2f 2a 20 42 2d 74 72 65 65 20 68 61 6e 64 6c /* B-tree handl
21990 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 69 72 e */. Pgno iFir
219a0 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 st,
219b0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
219c0 70 61 67 65 20 74 6f 20 72 65 6c 6f 63 61 74 65 page to relocate
219d0 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 4c 61 73 74 */. Pgno iLast
219e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
219f0 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 61 /* Last pa
21a00 67 65 20 74 6f 20 72 65 6c 6f 63 61 74 65 20 2a ge to relocate *
21a10 2f 0a 20 20 50 67 6e 6f 20 2a 70 6e 43 75 72 72 /. Pgno *pnCurr
21a20 65 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ent
21a30 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 /* If not NU
21a40 4c 4c 2c 20 49 4e 2f 4f 55 54 3a 20 44 61 74 61 LL, IN/OUT: Data
21a50 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 29 7b 0a base size */.){.
21a60 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
21a70 45 5f 4f 4b 3b 0a 20 20 42 74 72 65 65 50 74 72 E_OK;. BtreePtr
21a80 6d 61 70 20 2a 70 4d 61 70 20 3d 20 70 42 74 2d map *pMap = pBt-
21a90 3e 70 4d 61 70 3b 0a 20 20 50 67 6e 6f 20 69 50 >pMap;. Pgno iP
21aa0 67 3b 0a 0a 20 20 66 6f 72 28 69 50 67 3d 69 46 g;.. for(iPg=iF
21ab0 69 72 73 74 3b 20 69 50 67 3c 3d 69 4c 61 73 74 irst; iPg<=iLast
21ac0 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f && rc==SQLITE_O
21ad0 4b 3b 20 69 50 67 2b 2b 29 7b 0a 20 20 20 20 4d K; iPg++){. M
21ae0 65 6d 50 61 67 65 20 2a 70 46 72 65 65 20 3d 20 emPage *pFree =
21af0 30 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 61 0; /* Page a
21b00 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20 66 72 llocated from fr
21b10 65 65 2d 6c 69 73 74 20 2a 2f 0a 20 20 20 20 4d ee-list */. M
21b20 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b emPage *pPg = 0;
21b30 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 3b 20 . Pgno iNew;
21b40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21b50 4e 65 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 New page number
21b60 66 6f 72 20 70 50 67 20 2a 2f 0a 20 20 20 20 50 for pPg */. P
21b70 74 72 6d 61 70 45 6e 74 72 79 20 2a 70 45 6e 74 trmapEntry *pEnt
21b80 72 79 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 ry; /* Pointe
21b90 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 r map entry for
21ba0 70 61 67 65 20 69 50 67 20 2a 2f 0a 0a 20 20 20 page iPg */..
21bb0 20 69 66 28 20 69 50 67 3d 3d 50 45 4e 44 49 4e if( iPg==PENDIN
21bc0 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
21bd0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
21be0 20 70 45 6e 74 72 79 20 3d 20 26 70 4d 61 70 2d pEntry = &pMap-
21bf0 3e 61 50 74 72 5b 69 50 67 20 2d 20 70 4d 61 70 >aPtr[iPg - pMap
21c00 2d 3e 69 46 69 72 73 74 5d 3b 0a 0a 20 20 20 20 ->iFirst];..
21c10 69 66 28 20 70 45 6e 74 72 79 2d 3e 65 54 79 70 if( pEntry->eTyp
21c20 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e==PTRMAP_FREEPA
21c30 47 45 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f GE ){. Pgno
21c40 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 72 63 dummy;. rc
21c50 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
21c60 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
21c70 2c 20 26 64 75 6d 6d 79 2c 20 69 50 67 2c 20 42 , &dummy, iPg, B
21c80 54 41 4c 4c 4f 43 5f 45 58 41 43 54 29 3b 0a 20 TALLOC_EXACT);.
21c90 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
21ca0 28 70 46 72 65 65 29 3b 0a 20 20 20 20 20 20 61 (pFree);. a
21cb0 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
21cc0 45 5f 4f 4b 20 7c 7c 20 64 75 6d 6d 79 3d 3d 69 E_OK || dummy==i
21cd0 50 67 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 Pg );. }else
21ce0 69 66 28 20 70 6e 43 75 72 72 65 6e 74 20 29 7b if( pnCurrent ){
21cf0 0a 20 20 20 20 20 20 62 74 72 65 65 47 65 74 50 . btreeGetP
21d00 61 67 65 28 70 42 74 2c 20 69 50 67 2c 20 26 70 age(pBt, iPg, &p
21d10 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 Pg, 0);. as
21d20 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
21d30 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
21d40 67 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 g->pDbPage) );.
21d50 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
21d60 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
21d70 63 6f 75 6e 74 28 70 50 67 2d 3e 70 44 62 50 61 count(pPg->pDbPa
21d80 67 65 29 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 ge)==1 );.
21d90 69 4e 65 77 20 3d 20 2b 2b 28 2a 70 6e 43 75 72 iNew = ++(*pnCur
21da0 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 rent);. if(
21db0 20 69 4e 65 77 3d 3d 50 45 4e 44 49 4e 47 5f 42 iNew==PENDING_B
21dc0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 20 YTE_PAGE(pBt) )
21dd0 69 4e 65 77 20 3d 20 2b 2b 28 2a 70 6e 43 75 72 iNew = ++(*pnCur
21de0 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20 rent);. rc
21df0 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 = relocatePage(p
21e00 42 74 2c 20 70 50 67 2c 20 70 45 6e 74 72 79 2d Bt, pPg, pEntry-
21e10 3e 65 54 79 70 65 2c 20 70 45 6e 74 72 79 2d 3e >eType, pEntry->
21e20 70 61 72 65 6e 74 2c 20 69 4e 65 77 2c 20 31 29 parent, iNew, 1)
21e30 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 ;. releaseP
21e40 61 67 65 4e 6f 74 4e 75 6c 6c 28 70 50 67 29 3b ageNotNull(pPg);
21e50 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
21e60 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
21e70 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
21e80 46 72 65 65 2c 20 26 69 4e 65 77 2c 20 69 46 69 Free, &iNew, iFi
21e90 72 73 74 2d 31 2c 20 42 54 41 4c 4c 4f 43 5f 4c rst-1, BTALLOC_L
21ea0 45 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 E);. assert
21eb0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
21ec0 7c 7c 20 69 4e 65 77 3c 69 46 69 72 73 74 20 29 || iNew<iFirst )
21ed0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
21ee0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
21ef0 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
21f00 28 70 46 72 65 65 29 3b 0a 20 20 20 20 20 20 20 (pFree);.
21f10 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
21f20 74 2c 20 69 50 67 2c 20 26 70 50 67 2c 20 30 29 t, iPg, &pPg, 0)
21f30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 ;. rc = r
21f40 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c elocatePage(pBt,
21f50 20 70 50 67 2c 20 70 45 6e 74 72 79 2d 3e 65 54 pPg, pEntry->eT
21f60 79 70 65 2c 20 70 45 6e 74 72 79 2d 3e 70 61 72 ype, pEntry->par
21f70 65 6e 74 2c 69 4e 65 77 2c 31 29 3b 0a 20 20 20 ent,iNew,1);.
21f80 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
21f90 28 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 (pPg);. }.
21fa0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
21fb0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 21 64 65 66 n rc;.}../* !def
21fc0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
21fd0 5f 43 4f 4e 43 55 52 52 45 4e 54 29 0a 2a 2a 0a _CONCURRENT).**.
21fe0 2a 2a 20 54 68 65 20 62 2d 74 72 65 65 20 68 61 ** The b-tree ha
21ff0 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 20 74 ndle passed as t
22000 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 he only argument
22010 20 69 73 20 61 62 6f 75 74 20 74 6f 20 63 6f 6d is about to com
22020 6d 69 74 20 61 6e 0a 2a 2a 20 43 4f 4e 43 55 52 mit an.** CONCUR
22030 52 45 4e 54 20 74 72 61 6e 73 61 63 74 69 6f 6e RENT transaction
22040 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 . At this point
22050 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 it is guaranteed
22060 20 74 68 61 74 20 74 68 69 73 20 69 73 20 0a 2a that this is .*
22070 2a 20 70 6f 73 73 69 62 6c 65 20 2d 20 74 68 65 * possible - the
22080 20 77 61 6c 20 57 52 49 54 45 52 20 6c 6f 63 6b wal WRITER lock
22090 20 69 73 20 68 65 6c 64 20 61 6e 64 20 69 74 20 is held and it
220a0 69 73 20 6b 6e 6f 77 6e 20 74 68 61 74 20 74 68 is known that th
220b0 65 72 65 20 61 72 65 20 0a 2a 2a 20 6e 6f 20 63 ere are .** no c
220c0 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 63 6f onflicts with co
220d0 6d 6d 69 74 74 65 64 20 74 72 61 6e 73 61 63 74 mmitted transact
220e0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
220f0 69 6e 74 20 62 74 72 65 65 46 69 78 55 6e 6c 6f int btreeFixUnlo
22100 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a cked(Btree *p){.
22110 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
22120 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 = p->pBt;. MemP
22130 61 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42 age *pPage1 = pB
22140 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 75 38 20 t->pPage1;. u8
22150 2a 70 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 *p1 = pPage1->aD
22160 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 ata;. Pager *pP
22170 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 ager = pBt->pPag
22180 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 er;. int rc = S
22190 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
221a0 49 66 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 If page 1 of the
221b0 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
221c0 20 77 72 69 74 61 62 6c 65 2c 20 74 68 65 6e 20 writable, then
221d0 6e 6f 20 70 61 67 65 73 20 77 65 72 65 20 61 6c no pages were al
221e0 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6f 72 20 located. ** or
221f0 66 72 65 65 64 20 62 79 20 74 68 69 73 20 74 72 freed by this tr
22200 61 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20 74 68 ansaction. In th
22210 69 73 20 63 61 73 65 20 6e 6f 20 73 70 65 63 69 is case no speci
22220 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69 73 20 0a al handling is .
22230 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2e 20 4f ** required. O
22240 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 61 67 therwise, if pag
22250 65 20 31 20 69 73 20 64 69 72 74 79 2c 20 70 72 e 1 is dirty, pr
22260 6f 63 65 65 64 2e 20 20 2a 2f 0a 20 20 42 74 72 oceed. */. Btr
22270 65 65 50 74 72 6d 61 70 20 2a 70 4d 61 70 20 3d eePtrmap *pMap =
22280 20 70 42 74 2d 3e 70 4d 61 70 3b 0a 20 20 50 67 pBt->pMap;. Pg
22290 6e 6f 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 no iTrunk = get4
222a0 62 79 74 65 28 26 70 31 5b 33 32 5d 29 3b 0a 20 byte(&p1[32]);.
222b0 20 50 67 6e 6f 20 6e 50 61 67 65 20 3d 20 62 74 Pgno nPage = bt
222c0 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 reePagecount(pBt
222d0 29 3b 0a 20 20 75 33 32 20 6e 46 72 65 65 20 3d );. u32 nFree =
222e0 20 67 65 74 34 62 79 74 65 28 26 70 31 5b 33 36 get4byte(&p1[36
222f0 5d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 ]);.. assert( p
22300 42 74 2d 3e 70 4d 61 70 20 29 3b 0a 20 20 72 63 Bt->pMap );. rc
22310 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 = sqlite3PagerU
22320 70 67 72 61 64 65 53 6e 61 70 73 68 6f 74 28 70 pgradeSnapshot(p
22330 50 61 67 65 72 2c 20 70 50 61 67 65 31 2d 3e 70 Pager, pPage1->p
22340 44 62 50 61 67 65 29 3b 0a 20 20 61 73 73 65 72 DbPage);. asser
22350 74 28 20 70 31 3d 3d 70 50 61 67 65 31 2d 3e 61 t( p1==pPage1->a
22360 44 61 74 61 20 29 3b 0a 0a 20 20 69 66 28 20 72 Data );.. if( r
22370 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
22380 20 20 20 20 50 67 6e 6f 20 6e 48 50 61 67 65 20 Pgno nHPage
22390 3d 20 67 65 74 34 62 79 74 65 28 26 70 31 5b 32 = get4byte(&p1[2
223a0 38 5d 29 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 8]);. Pgno nF
223b0 69 6e 20 3d 20 6e 48 50 61 67 65 3b 20 20 20 20 in = nHPage;
223c0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
223d0 64 62 20 61 66 74 65 72 20 74 72 61 6e 73 61 63 db after transac
223e0 74 69 6f 6e 20 6d 65 72 67 65 20 2a 2f 0a 0a 20 tion merge */..
223f0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 if( sqlite3Pa
22400 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
22410 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 Page1->pDbPage)
22420 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 48 ){. Pgno iH
22430 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 Trunk = get4byte
22440 28 26 70 31 5b 33 32 5d 29 3b 0a 20 20 20 20 20 (&p1[32]);.
22450 20 75 33 32 20 6e 48 46 72 65 65 20 3d 20 67 65 u32 nHFree = ge
22460 74 34 62 79 74 65 28 26 70 31 5b 33 36 5d 29 3b t4byte(&p1[36]);
22470 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 .. /* Attac
22480 68 20 74 68 65 20 68 65 61 64 20 64 61 74 61 62 h the head datab
22490 61 73 65 20 66 72 65 65 20 6c 69 73 74 20 74 6f ase free list to
224a0 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
224b0 63 75 72 72 65 6e 74 0a 20 20 20 20 20 20 2a 2a current. **
224c0 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 72 transactions fr
224d0 65 65 2d 6c 69 73 74 20 28 69 66 20 61 6e 79 29 ee-list (if any)
224e0 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 . */. if(
224f0 69 54 72 75 6e 6b 21 3d 30 20 29 7b 0a 20 20 20 iTrunk!=0 ){.
22500 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
22510 31 5b 33 36 5d 2c 20 6e 48 46 72 65 65 20 2b 20 1[36], nHFree +
22520 6e 46 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 nFree);.
22530 70 75 74 34 62 79 74 65 28 26 70 31 5b 33 32 5d put4byte(&p1[32]
22540 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 , iTrunk);.
22550 20 20 20 77 68 69 6c 65 28 20 69 54 72 75 6e 6b while( iTrunk
22560 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 44 62 ){. Db
22570 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 73 Page *pTrunk = s
22580 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
22590 70 28 70 50 61 67 65 72 2c 20 69 54 72 75 6e 6b p(pPager, iTrunk
225a0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 54 72 );. iTr
225b0 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 28 unk = get4byte((
225c0 75 38 2a 29 70 54 72 75 6e 6b 2d 3e 70 44 61 74 u8*)pTrunk->pDat
225d0 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 a);. if
225e0 28 20 69 54 72 75 6e 6b 3d 3d 30 20 29 7b 0a 20 ( iTrunk==0 ){.
225f0 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 put4b
22600 79 74 65 28 28 75 38 2a 29 70 54 72 75 6e 6b 2d yte((u8*)pTrunk-
22610 3e 70 44 61 74 61 2c 20 69 48 54 72 75 6e 6b 29 >pData, iHTrunk)
22620 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
22630 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
22640 61 67 65 72 55 6e 72 65 66 28 70 54 72 75 6e 6b agerUnref(pTrunk
22650 29 3b 0a 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 );. };.
22660 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
22670 20 6e 48 50 61 67 65 3c 28 70 4d 61 70 2d 3e 69 nHPage<(pMap->i
22680 46 69 72 73 74 2d 31 29 20 29 7b 0a 20 20 20 20 First-1) ){.
22690 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
226a0 61 73 65 20 63 6f 6e 73 69 73 74 65 64 20 6f 66 ase consisted of
226b0 20 28 70 4d 61 70 2d 3e 69 46 69 72 73 74 2d 31 (pMap->iFirst-1
226c0 29 20 70 61 67 65 73 20 77 68 65 6e 20 74 68 65 ) pages when the
226d0 20 63 75 72 72 65 6e 74 0a 20 20 20 20 20 20 20 current.
226e0 20 2a 2a 20 63 6f 6e 63 75 72 72 65 6e 74 20 74 ** concurrent t
226f0 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6f ransaction was o
22700 70 65 6e 65 64 2e 20 41 6e 64 20 61 6e 20 63 6f pened. And an co
22710 6e 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 ncurrent transac
22720 74 69 6f 6e 20 6d 61 79 0a 20 20 20 20 20 20 20 tion may.
22730 20 2a 2a 20 6e 6f 74 20 62 65 20 65 78 65 63 75 ** not be execu
22740 74 65 64 20 6f 6e 20 61 6e 20 61 75 74 6f 2d 76 ted on an auto-v
22750 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 20 2d acuum database -
22760 20 73 6f 20 74 68 65 20 64 62 20 73 68 6f 75 6c so the db shoul
22770 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f d . ** no
22780 74 20 68 61 76 65 20 73 68 72 75 6e 6b 20 73 69 t have shrunk si
22790 6e 63 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 nce the transact
227a0 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 ion was opened.
227b0 54 68 65 72 65 66 6f 72 65 20 6e 48 50 61 67 65 Therefore nHPage
227c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 68 6f 75 . ** shou
227d0 6c 64 20 62 65 20 73 65 74 20 74 6f 20 28 70 4d ld be set to (pM
227e0 61 70 2d 3e 69 46 69 72 73 74 2d 31 29 20 6f 72 ap->iFirst-1) or
227f0 20 67 72 65 61 74 65 72 2e 20 2a 2f 0a 20 20 20 greater. */.
22800 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
22810 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
22820 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
22830 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65 /* The curre
22840 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 nt transaction a
22850 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 70 llocated pages p
22860 4d 61 70 2d 3e 69 46 69 72 73 74 20 74 68 72 6f Map->iFirst thro
22870 75 67 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e ugh. ** n
22880 50 61 67 65 20 28 69 6e 63 6c 75 73 69 76 65 29 Page (inclusive)
22890 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
228a0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
228b0 2e 20 4d 65 61 6e 77 68 69 6c 65 2c 0a 20 20 20 . Meanwhile,.
228c0 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 72 ** other tr
228d0 61 6e 73 61 63 74 69 6f 6e 73 20 68 61 76 65 20 ansactions have
228e0 61 6c 6c 6f 63 61 74 65 64 20 28 69 46 69 72 73 allocated (iFirs
228f0 74 2e 2e 6e 48 50 61 67 65 29 2e 20 53 6f 20 6d t..nHPage). So m
22900 6f 76 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 ove. ** p
22910 61 67 65 73 20 28 69 46 69 72 73 74 2e 2e 4d 49 ages (iFirst..MI
22920 4e 28 6e 50 61 67 65 2c 6e 48 50 61 67 65 29 29 N(nPage,nHPage))
22930 20 74 6f 20 28 4d 41 58 28 6e 50 61 67 65 2c 6e to (MAX(nPage,n
22940 48 50 61 67 65 29 2b 31 29 2e 20 20 2a 2f 0a 20 HPage)+1). */.
22950 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4c 61 73 Pgno iLas
22960 74 20 3d 20 4d 49 4e 28 6e 50 61 67 65 2c 20 6e t = MIN(nPage, n
22970 48 50 61 67 65 29 3b 20 20 20 20 2f 2a 20 4c 61 HPage); /* La
22980 73 74 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 st page to move
22990 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 */. Pgno
229a0 6e 43 75 72 72 65 6e 74 3b 20 20 20 20 20 20 20 nCurrent;
229b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
229c0 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65 20 6f * Current size o
229d0 66 20 64 62 20 2a 2f 0a 20 20 20 20 20 20 20 20 f db */.
229e0 6e 43 75 72 72 65 6e 74 20 3d 20 4d 41 58 28 6e nCurrent = MAX(n
229f0 50 61 67 65 2c 20 6e 48 50 61 67 65 29 3b 0a 20 Page, nHPage);.
22a00 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 rc = btre
22a10 65 52 65 6c 6f 63 61 74 65 52 61 6e 67 65 28 70 eRelocateRange(p
22a20 42 74 2c 20 70 4d 61 70 2d 3e 69 46 69 72 73 74 Bt, pMap->iFirst
22a30 2c 20 69 4c 61 73 74 2c 20 26 6e 43 75 72 72 65 , iLast, &nCurre
22a40 6e 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a nt);.. /*
22a50 20 54 68 65 72 65 20 61 72 65 20 6e 6f 77 20 6e There are now n
22a60 6f 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 77 69 74 o collisions wit
22a70 68 20 74 68 65 20 73 6e 61 70 73 68 6f 74 20 61 h the snapshot a
22a80 74 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 t the head of th
22a90 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 e. ** dat
22aa0 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f 20 61 abase file. So a
22ab0 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 t this point it
22ac0 77 6f 75 6c 64 20 62 65 20 70 6f 73 73 69 62 6c would be possibl
22ad0 65 20 74 6f 20 77 72 69 74 65 0a 20 20 20 20 20 e to write.
22ae0 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 ** the transa
22af0 63 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 ction out to dis
22b00 6b 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 k. Before doing
22b10 73 6f 20 74 68 6f 75 67 68 2c 20 61 74 74 65 6d so though, attem
22b20 70 74 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a pt to. **
22b30 20 72 65 6c 6f 63 61 74 65 20 73 6f 6d 65 20 6f relocate some o
22b40 66 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 f the new pages
22b50 74 6f 20 66 72 65 65 20 6c 6f 63 61 74 69 6f 6e to free location
22b60 73 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 s within the bod
22b70 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 y. ** of
22b80 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
22b90 65 20 28 69 2e 65 2e 20 66 72 65 65 2d 6c 69 73 e (i.e. free-lis
22ba0 74 20 65 6e 74 72 69 65 73 29 2e 20 2a 2f 0a 20 t entries). */.
22bb0 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
22bc0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
22bd0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 assert( nC
22be0 75 72 72 65 6e 74 21 3d 50 45 4e 44 49 4e 47 5f urrent!=PENDING_
22bf0 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
22c00 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
22c10 74 65 33 50 61 67 65 72 53 65 74 44 62 73 69 7a te3PagerSetDbsiz
22c20 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e e(pBt->pPager, n
22c30 43 75 72 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 Current);.
22c40 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 nFree = get4
22c50 62 79 74 65 28 26 70 31 5b 33 36 5d 29 3b 0a 20 byte(&p1[36]);.
22c60 20 20 20 20 20 20 20 20 20 6e 46 69 6e 20 3d 20 nFin =
22c70 6e 43 75 72 72 65 6e 74 2d 6e 46 72 65 65 3b 0a nCurrent-nFree;.
22c80 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 43 if( nC
22c90 75 72 72 65 6e 74 3e 50 45 4e 44 49 4e 47 5f 42 urrent>PENDING_B
22ca0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 26 26 YTE_PAGE(pBt) &&
22cb0 20 6e 46 69 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 nFin<=PENDING_B
22cc0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
22cd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 46 69 . nFi
22ce0 6e 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d n--;. }
22cf0 0a 20 20 20 20 20 20 20 20 20 20 6e 46 69 6e 20 . nFin
22d00 3d 20 4d 41 58 28 6e 46 69 6e 2c 20 6e 48 50 61 = MAX(nFin, nHPa
22d10 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 ge);. r
22d20 63 20 3d 20 62 74 72 65 65 52 65 6c 6f 63 61 74 c = btreeRelocat
22d30 65 52 61 6e 67 65 28 70 42 74 2c 20 6e 46 69 6e eRange(pBt, nFin
22d40 2b 31 2c 20 6e 43 75 72 72 65 6e 74 2c 20 30 29 +1, nCurrent, 0)
22d50 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
22d60 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
22d70 31 5b 32 38 5d 2c 20 6e 46 69 6e 29 3b 0a 20 20 1[28], nFin);.
22d80 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
22d90 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 44 sqlite3PagerSetD
22da0 62 73 69 7a 65 28 70 50 61 67 65 72 2c 20 6e 46 bsize(pPager, nF
22db0 69 6e 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 in);. }.. retu
22dc0 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 0a 23 rn rc;.}.#else.#
22dd0 20 64 65 66 69 6e 65 20 62 74 72 65 65 46 69 78 define btreeFix
22de0 55 6e 6c 6f 63 6b 65 64 28 58 29 20 20 53 51 4c Unlocked(X) SQL
22df0 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a ITE_OK.#endif /*
22e00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e SQLITE_OMIT_CON
22e10 43 55 52 52 45 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a CURRENT */../*.*
22e20 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
22e30 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68 oes the first ph
22e40 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 ase of a two-pha
22e50 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 se commit. This
22e60 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73 routine.** caus
22e70 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f es a rollback jo
22e80 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61 urnal to be crea
22e90 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 20 ted (if it does
22ea0 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
22eb0 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61 t).** and popula
22ec0 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20 ted with enough
22ed0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74 information so t
22ee0 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c hat if a power l
22ef0 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 oss occurs.** th
22f00 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 e database can b
22f10 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 e restored to it
22f20 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 s original state
22f30 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b by playing back
22f40 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e .** the journal.
22f50 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 Then the conte
22f60 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e nts of the journ
22f70 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f al are flushed o
22f80 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73 ut to.** the dis
22f90 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f k. After the jo
22fa0 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20 urnal is safely
22fb0 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68 on oxide, the ch
22fc0 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 anges to the.**
22fd0 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 69 database are wri
22fe0 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 tten into the da
22ff0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
23000 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65 flushed to oxide
23010 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 ..** At the end
23020 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68 of this call, th
23030 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
23040 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 al still exists
23050 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 on the.** disk a
23060 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20 nd we are still
23070 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b holding all lock
23080 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61 s, so the transa
23090 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a ction has not.**
230a0 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65 committed. See
230b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
230c0 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 6f mitPhaseTwo() fo
230d0 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 r the second pha
230e0 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d se of the.** com
230f0 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a mit process..**.
23100 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 ** This call is
23110 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 72 a no-op if no wr
23120 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
23130 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 is currently act
23140 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a 2a ive on pBt..**.*
23150 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 6e * Otherwise, syn
23160 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 c the database f
23170 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 65 ile for the btre
23180 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 70 e pBt. zMaster p
23190 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 oints to.** the
231a0 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 name of a master
231b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
231c0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 at should be wri
231d0 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a tten into the.**
231e0 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 individual jour
231f0 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 20 nal file, or is
23200 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e 67 NULL, indicating
23210 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e no master journ
23220 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 6e al file .** (sin
23230 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 gle database tra
23240 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a nsaction)..**.**
23250 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 When this is ca
23260 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 72 lled, the master
23270 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 20 journal should
23280 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 65 already have bee
23290 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 6f n.** created, po
232a0 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 69 pulated with thi
232b0 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 s journal pointe
232c0 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 r and synced to
232d0 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 disk..**.** Once
232e0 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e 65 this is routine
232f0 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 74 has returned, t
23300 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 65 he only thing re
23310 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 74 quired to commit
23320 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 72 .** the write-tr
23330 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 68 ansaction for th
23340 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 is database file
23350 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 is to delete th
23360 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 69 6e e journal..*/.in
23370 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
23380 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 mmitPhaseOne(Btr
23390 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 ee *p, const cha
233a0 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 r *zMaster){. i
233b0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
233c0 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 K;. if( p->inTr
233d0 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
233e0 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
233f0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
23400 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
23410 45 6e 74 65 72 28 70 29 3b 0a 0a 23 69 66 6e 64 Enter(p);..#ifnd
23420 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
23430 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
23440 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
23450 6d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 m ){. asser
23460 74 28 20 49 53 43 4f 4e 43 55 52 52 45 4e 54 3d t( ISCONCURRENT=
23470 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d =0 );. rc =
23480 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 autoVacuumCommi
23490 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 t(pBt);. if
234a0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
234b0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
234c0 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
234d0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
234e0 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
234f0 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 62 }. if( pBt->b
23500 44 6f 54 72 75 6e 63 61 74 65 20 29 7b 0a 20 20 DoTruncate ){.
23510 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
23520 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 TruncateImage(pB
23530 74 2d 3e 70 50 61 67 65 72 2c 20 70 42 74 2d 3e t->pPager, pBt->
23540 6e 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 nPage);. }.#e
23550 6e 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 3d ndif. if( rc=
23560 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 49 53 =SQLITE_OK && IS
23570 43 4f 4e 43 55 52 52 45 4e 54 20 29 7b 0a 20 20 CONCURRENT ){.
23580 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 46 69 rc = btreeFi
23590 78 55 6e 6c 6f 63 6b 65 64 28 70 29 3b 0a 20 20 xUnlocked(p);.
235a0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
235b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
235c0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
235d0 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f agerCommitPhaseO
235e0 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ne(pBt->pPager,
235f0 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 zMaster, 0);.
23600 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 }. sqlite3Bt
23610 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d reeLeave(p);. }
23620 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
23630 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
23640 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 66 tion is called f
23650 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 43 6f rom both BtreeCo
23660 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 mmitPhaseTwo() a
23670 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b nd BtreeRollback
23680 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e ().** at the con
23690 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 72 61 clusion of a tra
236a0 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 nsaction..*/.sta
236b0 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 45 6e tic void btreeEn
236c0 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 74 72 dTransaction(Btr
236d0 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
236e0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
236f0 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
23700 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 = p->db;. asser
23710 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
23720 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
23730 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
23740 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
23750 20 20 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 pBt->bDoTrunca
23760 74 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 te = 0;.#endif.
23770 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e if( p->inTrans>
23780 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 64 62 TRANS_NONE && db
23790 2d 3e 6e 56 64 62 65 52 65 61 64 3e 31 20 29 7b ->nVdbeRead>1 ){
237a0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
237b0 20 61 72 65 20 6f 74 68 65 72 20 61 63 74 69 76 are other activ
237c0 65 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 e statements tha
237d0 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 69 73 t belong to this
237e0 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a database. **
237f0 20 68 61 6e 64 6c 65 2c 20 64 6f 77 6e 67 72 61 handle, downgra
23800 64 65 20 74 6f 20 61 20 72 65 61 64 2d 6f 6e 6c de to a read-onl
23810 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 y transaction. T
23820 68 65 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 he other stateme
23830 6e 74 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 73 nts. ** may s
23840 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20 till be reading
23850 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
23860 65 2e 20 20 2a 2f 0a 20 20 20 20 64 6f 77 6e 67 e. */. downg
23870 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 radeAllSharedCac
23880 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b heTableLocks(p);
23890 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 . p->inTrans
238a0 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 = TRANS_READ;.
238b0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 }else{. /* If
238c0 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 64 20 the handle had
238d0 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 6e any kind of tran
238e0 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 saction open, de
238f0 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 20 crement the .
23900 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
23910 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61 count of the sha
23920 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68 red btree. If th
23930 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
23940 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 61 63 unt . ** reac
23950 68 65 73 20 30 2c 20 73 65 74 20 74 68 65 20 73 hes 0, set the s
23960 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 hared state to T
23970 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 RANS_NONE. The u
23980 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
23990 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c ed(). ** call
239a0 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f below will unlo
239b0 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20 2a ck the pager. *
239c0 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 /. if( p->inT
239d0 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 rans!=TRANS_NONE
239e0 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 72 41 ){. clearA
239f0 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
23a00 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 leLocks(p);.
23a10 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 pBt->nTransact
23a20 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 ion--;. if(
23a30 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 0==pBt->nTransa
23a40 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 ction ){.
23a50 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
23a60 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 ion = TRANS_NONE
23a70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
23a80 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 . /* Set the
23a90 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
23aa0 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 ion state to TRA
23ab0 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f NS_NONE and unlo
23ac0 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 ck the . ** p
23ad0 61 67 65 72 20 69 66 20 74 68 69 73 20 63 61 6c ager if this cal
23ae0 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c l closed the onl
23af0 79 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 y read or write
23b00 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a 2f transaction. */
23b10 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 . p->inTrans
23b20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 = TRANS_NONE;.
23b30 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 unlockBtreeIfU
23b40 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 7d 0a nused(pBt);. }.
23b50 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 . /* If this wa
23b60 73 20 61 6e 20 43 4f 4e 43 55 52 52 45 4e 54 20 s an CONCURRENT
23b70 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 64 65 6c transaction, del
23b80 65 74 65 20 74 68 65 20 70 42 74 2d 3e 70 4d 61 ete the pBt->pMa
23b90 70 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 41 p object.. ** A
23ba0 6c 73 6f 20 63 61 6c 6c 20 50 61 67 65 72 45 6e lso call PagerEn
23bb0 64 43 6f 6e 63 75 72 72 65 6e 74 28 29 20 74 6f dConcurrent() to
23bc0 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
23bd0 20 70 61 67 65 72 20 68 61 73 20 64 69 73 63 61 pager has disca
23be0 72 64 65 64 0a 20 20 2a 2a 20 74 68 65 20 72 65 rded. ** the re
23bf0 63 6f 72 64 20 6f 66 20 61 6c 6c 20 70 61 67 65 cord of all page
23c00 73 20 72 65 61 64 20 77 69 74 68 69 6e 20 74 68 s read within th
23c10 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 e transaction.
23c20 2a 2f 0a 20 20 62 74 72 65 65 50 74 72 6d 61 70 */. btreePtrmap
23c30 44 65 6c 65 74 65 28 70 42 74 29 3b 0a 20 20 73 Delete(pBt);. s
23c40 71 6c 69 74 65 33 50 61 67 65 72 45 6e 64 43 6f qlite3PagerEndCo
23c50 6e 63 75 72 72 65 6e 74 28 70 42 74 2d 3e 70 50 ncurrent(pBt->pP
23c60 61 67 65 72 29 3b 0a 20 20 62 74 72 65 65 49 6e ager);. btreeIn
23c70 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f tegrity(p);.}../
23c80 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 *.** Commit the
23c90 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 transaction curr
23ca0 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 ently in progres
23cb0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
23cc0 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 utine implements
23cd0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 the second phas
23ce0 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 e of a 2-phase c
23cf0 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73 ommit. The.** s
23d00 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
23d10 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 tPhaseOne() rout
23d20 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
23d30 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f st phase and sho
23d40 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 uld.** be invoke
23d50 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 d prior to calli
23d60 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
23d70 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 The sqlite3Btr
23d80 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
23d90 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69 ().** routine di
23da0 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f d all the work o
23db0 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d f writing inform
23dc0 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 ation out to dis
23dd0 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74 k and flushing t
23de0 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73 he.** contents s
23df0 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 o that they are
23e00 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65 written onto the
23e10 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20 disk platter.
23e20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 All this.** rout
23e30 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73 ine has to do is
23e40 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63 delete or trunc
23e50 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 ate or zero the
23e60 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a header in the.**
23e70 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
23e80 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75 urnal (which cau
23e90 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 ses the transact
23ea0 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61 ion to commit) a
23eb0 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 nd.** drop locks
23ec0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 ..**.** Normally
23ed0 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 , if an error oc
23ee0 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 20 70 curs while the p
23ef0 61 67 65 72 20 6c 61 79 65 72 20 69 73 20 61 74 ager layer is at
23f00 74 65 6d 70 74 69 6e 67 20 74 6f 20 0a 2a 2a 20 tempting to .**
23f10 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 75 6e 64 finalize the und
23f20 65 72 6c 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 erlying journal
23f30 66 69 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 file, this funct
23f40 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 ion returns an e
23f50 72 72 6f 72 20 61 6e 64 0a 2a 2a 20 74 68 65 20 rror and.** the
23f60 75 70 70 65 72 20 6c 61 79 65 72 20 77 69 6c 6c upper layer will
23f70 20 61 74 74 65 6d 70 74 20 61 20 72 6f 6c 6c 62 attempt a rollb
23f80 61 63 6b 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 ack. However, if
23f90 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
23fa0 6d 65 6e 74 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 7a ment.** is non-z
23fb0 65 72 6f 20 74 68 65 6e 20 74 68 69 73 20 62 2d ero then this b-
23fc0 74 72 65 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tree transaction
23fd0 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6d 75 is part of a mu
23fe0 6c 74 69 2d 66 69 6c 65 20 0a 2a 2a 20 74 72 61 lti-file .** tra
23ff0 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 nsaction. In thi
24000 73 20 63 61 73 65 2c 20 74 68 65 20 74 72 61 6e s case, the tran
24010 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 saction has alre
24020 61 64 79 20 62 65 65 6e 20 63 6f 6d 6d 69 74 74 ady been committ
24030 65 64 20 0a 2a 2a 20 28 62 79 20 64 65 6c 65 74 ed .** (by delet
24040 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ing a master jou
24050 72 6e 61 6c 20 66 69 6c 65 29 20 61 6e 64 20 74 rnal file) and t
24060 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 69 he caller will i
24070 67 6e 6f 72 65 20 74 68 69 73 20 0a 2a 2a 20 66 gnore this .** f
24080 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
24090 63 6f 64 65 2e 20 53 6f 2c 20 65 76 65 6e 20 69 code. So, even i
240a0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
240b0 73 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 6c s in the pager l
240c0 61 79 65 72 2c 0a 2a 2a 20 72 65 73 65 74 20 74 ayer,.** reset t
240d0 68 65 20 62 2d 74 72 65 65 20 6f 62 6a 65 63 74 he b-tree object
240e0 73 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 s internal state
240f0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 to indicate tha
24100 74 20 74 68 65 20 77 72 69 74 65 0a 2a 2a 20 74 t the write.** t
24110 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62 ransaction has b
24120 65 65 6e 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 een closed. This
24130 20 69 73 20 71 75 69 74 65 20 73 61 66 65 2c 20 is quite safe,
24140 61 73 20 74 68 65 20 70 61 67 65 72 20 77 69 6c as the pager wil
24150 6c 20 68 61 76 65 0a 2a 2a 20 74 72 61 6e 73 69 l have.** transi
24160 74 69 6f 6e 65 64 20 74 6f 20 74 68 65 20 65 72 tioned to the er
24170 72 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ror state..**.**
24180 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 This will relea
24190 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 se the write loc
241a0 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
241b0 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 e file. If ther
241c0 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 e.** are no acti
241d0 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 ve cursors, it a
241e0 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 lso releases the
241f0 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 69 read lock..*/.i
24200 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
24210 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 ommitPhaseTwo(Bt
24220 72 65 65 20 2a 70 2c 20 69 6e 74 20 62 43 6c 65 ree *p, int bCle
24230 61 6e 75 70 29 7b 0a 0a 20 20 69 66 28 20 70 2d anup){.. if( p-
24240 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
24250 4e 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51 NONE ) return SQ
24260 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 LITE_OK;. sqlit
24270 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
24280 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
24290 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 y(p);.. /* If t
242a0 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 20 he handle has a
242b0 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
242c0 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 n open, commit t
242d0 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 73 he shared-btrees
242e0 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 . ** transacti
242f0 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 73 on and set the s
24300 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 hared state to T
24310 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a RANS_READ.. */.
24320 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
24330 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b ==TRANS_WRITE ){
24340 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 . int rc;.
24350 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
24360 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 p->pBt;. ass
24370 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
24380 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
24390 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 RITE );. asse
243a0 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 rt( pBt->nTransa
243b0 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 ction>0 );. r
243c0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
243d0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
243e0 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
243f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
24400 4f 4b 20 26 26 20 62 43 6c 65 61 6e 75 70 3d 3d OK && bCleanup==
24410 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
24420 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
24430 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
24440 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 ;. }. p->i
24450 44 61 74 61 56 65 72 73 69 6f 6e 2d 2d 3b 20 20 DataVersion--;
24460 2f 2a 20 43 6f 6d 70 65 6e 73 61 74 65 20 66 6f /* Compensate fo
24470 72 20 70 50 61 67 65 72 2d 3e 69 44 61 74 61 56 r pPager->iDataV
24480 65 72 73 69 6f 6e 2b 2b 3b 20 2a 2f 0a 20 20 20 ersion++; */.
24490 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
244a0 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 ion = TRANS_READ
244b0 3b 0a 20 20 20 20 62 74 72 65 65 43 6c 65 61 72 ;. btreeClear
244c0 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b HasContent(pBt);
244d0 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 . }.. btreeEnd
244e0 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a Transaction(p);.
244f0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
24500 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
24510 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
24520 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 *.** Do both pha
24530 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e ses of a commit.
24540 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
24550 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 treeCommit(Btree
24560 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
24570 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
24580 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 ter(p);. rc = s
24590 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
245a0 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b tPhaseOne(p, 0);
245b0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
245c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
245d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
245e0 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2c 20 30 mitPhaseTwo(p, 0
245f0 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
24600 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
24610 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
24620 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
24630 65 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 e sets the state
24640 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 to CURSOR_FAULT
24650 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a and the error.*
24660 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 * code to errCod
24670 65 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 e for every curs
24680 6f 72 20 6f 6e 20 61 6e 79 20 42 74 53 68 61 72 or on any BtShar
24690 65 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a ed that pBtree.*
246a0 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 20 4f * references. O
246b0 72 20 69 66 20 74 68 65 20 77 72 69 74 65 4f 6e r if the writeOn
246c0 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 ly flag is set t
246d0 6f 20 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 0a 2a o 1, then only.*
246e0 2a 20 74 72 69 70 20 77 72 69 74 65 20 63 75 72 * trip write cur
246f0 73 6f 72 73 20 61 6e 64 20 6c 65 61 76 65 20 72 sors and leave r
24700 65 61 64 20 63 75 72 73 6f 72 73 20 75 6e 63 68 ead cursors unch
24710 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 anged..**.** Eve
24720 72 79 20 63 75 72 73 6f 72 20 69 73 20 61 20 63 ry cursor is a c
24730 61 6e 64 69 64 61 74 65 20 74 6f 20 62 65 20 74 andidate to be t
24740 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e ripped, includin
24750 67 20 63 75 72 73 6f 72 73 0a 2a 2a 20 74 68 61 g cursors.** tha
24760 74 20 62 65 6c 6f 6e 67 20 74 6f 20 6f 74 68 65 t belong to othe
24770 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
24780 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 ctions that happ
24790 65 6e 20 74 6f 20 62 65 0a 2a 2a 20 73 68 61 72 en to be.** shar
247a0 69 6e 67 20 74 68 65 20 63 61 63 68 65 20 77 69 ing the cache wi
247b0 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a th pBtree..**.**
247c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 This routine ge
247d0 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 ts called when a
247e0 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
247f0 2e 20 49 66 20 74 68 65 20 77 72 69 74 65 4f 6e . If the writeOn
24800 6c 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72 ly.** flag is tr
24810 75 65 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 ue, then only wr
24820 69 74 65 2d 63 75 72 73 6f 72 73 20 6e 65 65 64 ite-cursors need
24830 20 62 65 20 74 72 69 70 70 65 64 20 2d 20 72 65 be tripped - re
24840 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f ad-only.** curso
24850 72 73 20 73 61 76 65 20 74 68 65 69 72 20 63 75 rs save their cu
24860 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 73 20 rrent positions
24870 73 6f 20 74 68 61 74 20 74 68 65 79 20 6d 61 79 so that they may
24880 20 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 66 6f continue .** fo
24890 6c 6c 6f 77 69 6e 67 20 74 68 65 20 72 6f 6c 6c llowing the roll
248a0 62 61 63 6b 2e 20 4f 72 2c 20 69 66 20 77 72 69 back. Or, if wri
248b0 74 65 4f 6e 6c 79 20 69 73 20 66 61 6c 73 65 2c teOnly is false,
248c0 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 all cursors are
248d0 20 0a 2a 2a 20 74 72 69 70 70 65 64 2e 20 49 6e .** tripped. In
248e0 20 67 65 6e 65 72 61 6c 2c 20 77 72 69 74 65 4f general, writeO
248f0 6e 6c 79 20 69 73 20 66 61 6c 73 65 20 69 66 20 nly is false if
24900 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
24910 62 65 69 6e 67 0a 2a 2a 20 72 6f 6c 6c 65 64 20 being.** rolled
24920 62 61 63 6b 20 6d 6f 64 69 66 69 65 64 20 74 68 back modified th
24930 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
24940 61 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 a. In this case
24950 62 2d 74 72 65 65 20 72 6f 6f 74 0a 2a 2a 20 70 b-tree root.** p
24960 61 67 65 73 20 6d 61 79 20 62 65 20 6d 6f 76 65 ages may be move
24970 64 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f d or deleted fro
24980 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 m the database a
24990 6c 74 6f 67 65 74 68 65 72 2c 20 6d 61 6b 69 6e ltogether, makin
249a0 67 0a 2a 2a 20 69 74 20 75 6e 73 61 66 65 20 66 g.** it unsafe f
249b0 6f 72 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 or read cursors
249c0 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2a 0a to continue..**.
249d0 2a 2a 20 49 66 20 74 68 65 20 77 72 69 74 65 4f ** If the writeO
249e0 6e 6c 79 20 66 6c 61 67 20 69 73 20 74 72 75 65 nly flag is true
249f0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 69 73 and an error is
24a00 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 encountered whi
24a10 6c 65 20 0a 2a 2a 20 73 61 76 69 6e 67 20 74 68 le .** saving th
24a20 65 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 e current positi
24a30 6f 6e 20 6f 66 20 61 20 72 65 61 64 2d 6f 6e 6c on of a read-onl
24a40 79 20 63 75 72 73 6f 72 2c 20 61 6c 6c 20 63 75 y cursor, all cu
24a50 72 73 6f 72 73 2c 20 0a 2a 2a 20 69 6e 63 6c 75 rsors, .** inclu
24a60 64 69 6e 67 20 61 6c 6c 20 72 65 61 64 2d 63 75 ding all read-cu
24a70 72 73 6f 72 73 20 61 72 65 20 74 72 69 70 70 65 rsors are trippe
24a80 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f d..**.** SQLITE_
24a90 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
24aa0 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 f successful, or
24ab0 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
24ac0 75 72 73 20 77 68 69 6c 65 0a 2a 2a 20 73 61 76 urs while.** sav
24ad0 69 6e 67 20 61 20 63 75 72 73 6f 72 20 70 6f 73 ing a cursor pos
24ae0 69 74 69 6f 6e 2c 20 61 6e 20 53 51 4c 69 74 65 ition, an SQLite
24af0 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a error code..*/.
24b00 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
24b10 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 TripAllCursors(B
24b20 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e tree *pBtree, in
24b30 74 20 65 72 72 43 6f 64 65 2c 20 69 6e 74 20 77 t errCode, int w
24b40 72 69 74 65 4f 6e 6c 79 29 7b 0a 20 20 42 74 43 riteOnly){. BtC
24b50 75 72 73 6f 72 20 2a 70 3b 0a 20 20 69 6e 74 20 ursor *p;. int
24b60 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
24b70 0a 20 20 61 73 73 65 72 74 28 20 28 77 72 69 74 . assert( (writ
24b80 65 4f 6e 6c 79 3d 3d 30 20 7c 7c 20 77 72 69 74 eOnly==0 || writ
24b90 65 4f 6e 6c 79 3d 3d 31 29 20 26 26 20 42 54 43 eOnly==1) && BTC
24ba0 46 5f 57 72 69 74 65 46 6c 61 67 3d 3d 31 20 29 F_WriteFlag==1 )
24bb0 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29 ;. if( pBtree )
24bc0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
24bd0 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b eeEnter(pBtree);
24be0 0a 20 20 20 20 66 6f 72 28 70 3d 70 42 74 72 65 . for(p=pBtre
24bf0 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b e->pBt->pCursor;
24c00 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
24c10 0a 20 20 20 20 20 20 69 66 28 20 77 72 69 74 65 . if( write
24c20 4f 6e 6c 79 20 26 26 20 28 70 2d 3e 63 75 72 46 Only && (p->curF
24c30 6c 61 67 73 20 26 20 42 54 43 46 5f 57 72 69 74 lags & BTCF_Writ
24c40 65 46 6c 61 67 29 3d 3d 30 20 29 7b 0a 20 20 20 eFlag)==0 ){.
24c50 20 20 20 20 20 69 66 28 20 70 2d 3e 65 53 74 61 if( p->eSta
24c60 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
24c70 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 || p->eState==C
24c80 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 URSOR_SKIPNEXT )
24c90 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
24ca0 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 saveCursorPosit
24cb0 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ion(p);.
24cc0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
24cd0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
24ce0 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
24cf0 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 BtreeTripAllCurs
24d00 6f 72 73 28 70 42 74 72 65 65 2c 20 72 63 2c 20 ors(pBtree, rc,
24d10 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0);.
24d20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
24d30 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
24d40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
24d50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
24d60 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 earCursor(p);.
24d70 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 p->eState
24d80 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a = CURSOR_FAULT;.
24d90 20 20 20 20 20 20 20 20 70 2d 3e 73 6b 69 70 4e p->skipN
24da0 65 78 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 ext = errCode;.
24db0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 74 72 }. btr
24dc0 65 65 52 65 6c 65 61 73 65 41 6c 6c 43 75 72 73 eeReleaseAllCurs
24dd0 6f 72 50 61 67 65 73 28 70 29 3b 0a 20 20 20 20 orPages(p);.
24de0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
24df0 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b eeLeave(pBtree);
24e00 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
24e10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 ;.}../*.** Rollb
24e20 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 ack the transact
24e30 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e ion in progress.
24e40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 69 70 43 6f .**.** If tripCo
24e50 64 65 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 de is not SQLITE
24e60 5f 4f 4b 20 74 68 65 6e 20 63 75 72 73 6f 72 73 _OK then cursors
24e70 20 77 69 6c 6c 20 62 65 20 69 6e 76 61 6c 69 64 will be invalid
24e80 61 74 65 64 20 28 74 72 69 70 70 65 64 29 2e 0a ated (tripped)..
24e90 2a 2a 20 4f 6e 6c 79 20 77 72 69 74 65 20 63 75 ** Only write cu
24ea0 72 73 6f 72 73 20 61 72 65 20 74 72 69 70 70 65 rsors are trippe
24eb0 64 20 69 66 20 77 72 69 74 65 4f 6e 6c 79 20 69 d if writeOnly i
24ec0 73 20 74 72 75 65 20 62 75 74 20 61 6c 6c 20 63 s true but all c
24ed0 75 72 73 6f 72 73 20 61 72 65 0a 2a 2a 20 74 72 ursors are.** tr
24ee0 69 70 70 65 64 20 69 66 20 77 72 69 74 65 4f 6e ipped if writeOn
24ef0 6c 79 20 69 73 20 66 61 6c 73 65 2e 20 20 41 6e ly is false. An
24f00 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 y attempt to use
24f10 0a 2a 2a 20 61 20 74 72 69 70 70 65 64 20 63 75 .** a tripped cu
24f20 72 73 6f 72 20 77 69 6c 6c 20 72 65 73 75 6c 74 rsor will result
24f30 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a in an error..**
24f40 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 .** This will re
24f50 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 lease the write
24f60 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
24f70 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 base file. If t
24f80 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 here.** are no a
24f90 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 ctive cursors, i
24fa0 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 t also releases
24fb0 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
24fc0 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
24fd0 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 eeRollback(Btree
24fe0 20 2a 70 2c 20 69 6e 74 20 74 72 69 70 43 6f 64 *p, int tripCod
24ff0 65 2c 20 69 6e 74 20 77 72 69 74 65 4f 6e 6c 79 e, int writeOnly
25000 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
25010 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
25020 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 ->pBt;. MemPage
25030 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 61 73 73 *pPage1;.. ass
25040 65 72 74 28 20 77 72 69 74 65 4f 6e 6c 79 3d 3d ert( writeOnly==
25050 31 20 7c 7c 20 77 72 69 74 65 4f 6e 6c 79 3d 3d 1 || writeOnly==
25060 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 0 );. assert( t
25070 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f ripCode==SQLITE_
25080 41 42 4f 52 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c ABORT_ROLLBACK |
25090 7c 20 74 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 | tripCode==SQLI
250a0 54 45 5f 4f 4b 20 29 3b 0a 20 20 73 71 6c 69 74 TE_OK );. sqlit
250b0 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
250c0 0a 20 20 69 66 28 20 74 72 69 70 43 6f 64 65 3d . if( tripCode=
250d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
250e0 20 20 72 63 20 3d 20 74 72 69 70 43 6f 64 65 20 rc = tripCode
250f0 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 = saveAllCursors
25100 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 (pBt, 0, 0);.
25110 20 69 66 28 20 72 63 20 29 20 77 72 69 74 65 4f if( rc ) writeO
25120 6e 6c 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 nly = 0;. }else
25130 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
25140 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
25150 74 72 69 70 43 6f 64 65 20 29 7b 0a 20 20 20 20 tripCode ){.
25160 69 6e 74 20 72 63 32 20 3d 20 73 71 6c 69 74 65 int rc2 = sqlite
25170 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 3BtreeTripAllCur
25180 73 6f 72 73 28 70 2c 20 74 72 69 70 43 6f 64 65 sors(p, tripCode
25190 2c 20 77 72 69 74 65 4f 6e 6c 79 29 3b 0a 20 20 , writeOnly);.
251a0 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
251b0 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 77 72 69 74 LITE_OK || (writ
251c0 65 4f 6e 6c 79 3d 3d 30 20 26 26 20 72 63 32 3d eOnly==0 && rc2=
251d0 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 =SQLITE_OK) );.
251e0 20 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 if( rc2!=SQLI
251f0 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 TE_OK ) rc = rc2
25200 3b 0a 20 20 7d 0a 20 20 62 74 72 65 65 49 6e 74 ;. }. btreeInt
25210 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 egrity(p);.. if
25220 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
25230 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
25240 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 int rc2;.. a
25250 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49 ssert( TRANS_WRI
25260 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 TE==pBt->inTrans
25270 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 action );. rc
25280 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 2 = sqlite3Pager
25290 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 Rollback(pBt->pP
252a0 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ager);. if( r
252b0 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b c2!=SQLITE_OK ){
252c0 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b . rc = rc2;
252d0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
252e0 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 he rollback may
252f0 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20 74 have destroyed t
25300 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 he pPage1->aData
25310 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 value. So.
25320 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 47 65 74 ** call btreeGet
25330 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31 Page() on page 1
25340 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 again to make.
25350 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65 ** sure pPage
25360 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20 1->aData is set
25370 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
25380 20 20 69 66 28 20 62 74 72 65 65 47 65 74 50 61 if( btreeGetPa
25390 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
253a0 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f e1, 0)==SQLITE_O
253b0 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e K ){. int n
253c0 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
253d0 32 38 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 28+(u8*)pPage1->
253e0 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 74 65 aData);. te
253f0 73 74 63 61 73 65 28 20 6e 50 61 67 65 3d 3d 30 stcase( nPage==0
25400 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 50 );. if( nP
25410 61 67 65 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 age==0 ) sqlite3
25420 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
25430 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 Bt->pPager, &nPa
25440 67 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 ge);. testc
25450 61 73 65 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 ase( pBt->nPage!
25460 3d 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 =nPage );.
25470 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e 50 61 pBt->nPage = nPa
25480 67 65 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 ge;. releas
25490 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
254a0 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
254b0 20 63 6f 75 6e 74 56 61 6c 69 64 43 75 72 73 6f countValidCurso
254c0 72 73 28 70 42 74 2c 20 31 29 3d 3d 30 20 29 3b rs(pBt, 1)==0 );
254d0 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e . pBt->inTran
254e0 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f saction = TRANS_
254f0 52 45 41 44 3b 0a 20 20 20 20 62 74 72 65 65 43 READ;. btreeC
25500 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 70 learHasContent(p
25510 42 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 Bt);. }.. btre
25520 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 eEndTransaction(
25530 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 p);. sqlite3Btr
25540 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
25550 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
25560 2a 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d * Start a statem
25570 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 ent subtransacti
25580 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 on. The subtrans
25590 61 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 72 6f action can be ro
255a0 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 64 lled.** back ind
255b0 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 74 68 ependently of th
255c0 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 e main transacti
255d0 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20 73 74 61 on. You must sta
255e0 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e rt a transaction
255f0 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73 74 61 72 .** before star
25600 74 69 6e 67 20 61 20 73 75 62 74 72 61 6e 73 61 ting a subtransa
25610 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 ction. The subtr
25620 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e 64 ansaction is end
25630 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
25640 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d 61 69 6e .** if the main
25650 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d transaction com
25660 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 mits or rolls ba
25670 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d ck..**.** Statem
25680 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 ent subtransacti
25690 6f 6e 73 20 61 72 65 20 75 73 65 64 20 61 72 6f ons are used aro
256a0 75 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20 53 und individual S
256b0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a QL statements.**
256c0 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61 69 that are contai
256d0 6e 65 64 20 77 69 74 68 69 6e 20 61 20 42 45 47 ned within a BEG
256e0 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f 63 IN...COMMIT bloc
256f0 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 k. If a constra
25700 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 int.** error occ
25710 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 urs within the s
25720 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65 66 tatement, the ef
25730 66 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 fect of that one
25740 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 statement.** ca
25750 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b n be rolled back
25760 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
25770 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
25780 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 entire transacti
25790 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 74 61 74 on..**.** A stat
257a0 65 6d 65 6e 74 20 73 75 62 2d 74 72 61 6e 73 61 ement sub-transa
257b0 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 ction is impleme
257c0 6e 74 65 64 20 61 73 20 61 6e 20 61 6e 6f 6e 79 nted as an anony
257d0 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2e 20 mous savepoint.
257e0 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 70 61 73 The.** value pas
257f0 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
25800 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 d parameter is t
25810 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
25820 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 2c 0a 2a of savepoints,.*
25830 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 * including the
25840 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 new anonymous sa
25850 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e 20 6f 6e vepoint, open on
25860 20 74 68 65 20 42 2d 54 72 65 65 2e 20 69 2e 65 the B-Tree. i.e
25870 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 . if there.** ar
25880 65 20 6e 6f 20 61 63 74 69 76 65 20 73 61 76 65 e no active save
25890 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f 20 6f 74 points and no ot
258a0 68 65 72 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 her statement-tr
258b0 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 2c ansactions open,
258c0 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e 74 20 69 .** iStatement i
258d0 73 20 31 2e 20 54 68 69 73 20 61 6e 6f 6e 79 6d s 1. This anonym
258e0 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 20 63 61 ous savepoint ca
258f0 6e 20 62 65 20 72 65 6c 65 61 73 65 64 20 6f 72 n be released or
25900 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a 2a 2a 20 rolled back.**
25910 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 using the sqlite
25920 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 3BtreeSavepoint(
25930 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 69 ) function..*/.i
25940 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 nt sqlite3BtreeB
25950 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65 20 2a eginStmt(Btree *
25960 70 2c 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e p, int iStatemen
25970 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
25980 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
25990 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 p->pBt;. sqlite
259a0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
259b0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 assert( p->inT
259c0 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
259d0 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 E );. assert( (
259e0 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
259f0 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 3d 3d BTS_READ_ONLY)==
25a00 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
25a10 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 Statement>0 );.
25a20 20 61 73 73 65 72 74 28 20 69 53 74 61 74 65 6d assert( iStatem
25a30 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 ent>p->db->nSave
25a40 70 6f 69 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 point );. asser
25a50 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
25a60 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
25a70 54 45 20 29 3b 0a 20 20 2f 2a 20 41 74 20 74 68 TE );. /* At th
25a80 65 20 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 e pager level, a
25a90 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
25aa0 61 63 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65 action is a save
25ab0 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 2a 2a 20 point with. **
25ac0 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 an index greater
25ad0 20 74 68 61 6e 20 61 6c 6c 20 73 61 76 65 70 6f than all savepo
25ae0 69 6e 74 73 20 63 72 65 61 74 65 64 20 65 78 70 ints created exp
25af0 6c 69 63 69 74 6c 79 20 75 73 69 6e 67 0a 20 20 licitly using.
25b00 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ** SQL statement
25b10 73 2e 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c s. It is illegal
25b20 20 74 6f 20 6f 70 65 6e 2c 20 72 65 6c 65 61 73 to open, releas
25b30 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e e or rollback an
25b40 79 0a 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 y. ** such save
25b50 70 6f 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 points while the
25b60 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
25b70 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
25b80 20 69 73 20 61 63 74 69 76 65 2e 0a 20 20 2a 2f is active.. */
25b90 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
25ba0 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e agerOpenSavepoin
25bb0 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
25bc0 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 69 66 Statement);. if
25bd0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
25be0 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
25bf0 65 50 74 72 6d 61 70 42 65 67 69 6e 28 70 42 74 ePtrmapBegin(pBt
25c00 2c 20 69 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 , iStatement);.
25c10 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 }. sqlite3Btre
25c20 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
25c30 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
25c40 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
25c50 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
25c60 63 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c ction, op, is al
25c70 77 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 ways SAVEPOINT_R
25c80 4f 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 OLLBACK.** or SA
25c90 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e VEPOINT_RELEASE.
25ca0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 This function e
25cb0 69 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f ither releases o
25cc0 72 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 r rolls back the
25cd0 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 64 .** savepoint id
25ce0 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 entified by para
25cf0 6d 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e 74 meter iSavepoint
25d00 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 , depending on t
25d10 68 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 he value .** of
25d20 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c op..**.** Normal
25d30 6c 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 69 ly, iSavepoint i
25d40 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
25d50 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e r equal to zero.
25d60 20 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 20 However, if op
25d70 69 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f is.** SAVEPOINT_
25d80 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 ROLLBACK, then i
25d90 53 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c Savepoint may al
25da0 73 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 so be -1. In thi
25db0 73 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 s case the .** c
25dc0 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 65 ontents of the e
25dd0 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
25de0 6e 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 n are rolled bac
25df0 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 k. This is diffe
25e00 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e rent.** from a n
25e10 6f 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f ormal transactio
25e20 6e 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e n rollback, as n
25e30 6f 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 o locks are rele
25e40 61 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 ased and the.**
25e50 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 transaction rema
25e60 69 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 69 6e 74 ins open..*/.int
25e70 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 sqlite3BtreeSav
25e80 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 2c epoint(Btree *p,
25e90 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 int op, int iSa
25ea0 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 vepoint){. int
25eb0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
25ec0 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69 6e if( p && p->in
25ed0 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
25ee0 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 TE ){. BtShar
25ef0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
25f00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 ;. assert( op
25f10 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
25f20 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 ASE || op==SAVEP
25f30 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b OINT_ROLLBACK );
25f40 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 61 . assert( iSa
25f50 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 vepoint>=0 || (i
25f60 53 61 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 Savepoint==-1 &&
25f70 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
25f80 4f 4c 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 20 OLLBACK) );.
25f90 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
25fa0 72 28 70 29 3b 0a 20 20 20 20 62 74 72 65 65 50 r(p);. btreeP
25fb0 74 72 6d 61 70 45 6e 64 28 70 42 74 2c 20 6f 70 trmapEnd(pBt, op
25fc0 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 , iSavepoint);.
25fd0 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 if( op==SAVEP
25fe0 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b OINT_ROLLBACK ){
25ff0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 61 76 65 . rc = save
26000 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 AllCursors(pBt,
26010 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 0, 0);. }.
26020 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
26030 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
26040 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 sqlite3PagerSav
26050 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 epoint(pBt->pPag
26060 65 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f 69 er, op, iSavepoi
26070 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 nt);. }. i
26080 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
26090 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 53 ){. if( iS
260a0 61 76 65 70 6f 69 6e 74 3c 30 20 26 26 20 28 70 avepoint<0 && (p
260b0 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
260c0 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 TS_INITIALLY_EMP
260d0 54 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 TY)!=0 ){.
260e0 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 30 pBt->nPage = 0
260f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
26100 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 rc = newDatabase
26110 28 70 42 74 29 3b 0a 20 20 20 20 20 20 70 42 74 (pBt);. pBt
26120 2d 3e 6e 50 61 67 65 20 3d 20 67 65 74 34 62 79 ->nPage = get4by
26130 74 65 28 32 38 20 2b 20 70 42 74 2d 3e 70 50 61 te(28 + pBt->pPa
26140 67 65 31 2d 3e 61 44 61 74 61 29 3b 0a 0a 20 20 ge1->aData);..
26150 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
26160 61 73 65 20 73 69 7a 65 20 77 61 73 20 77 72 69 ase size was wri
26170 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6f 66 tten into the of
26180 66 73 65 74 20 32 38 20 6f 66 20 74 68 65 20 68 fset 28 of the h
26190 65 61 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 eader. ** w
261a0 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 hen the transact
261b0 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 73 6f 20 ion started, so
261c0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 we know that the
261d0 20 76 61 6c 75 65 20 61 74 20 6f 66 66 73 65 74 value at offset
261e0 0a 20 20 20 20 20 20 2a 2a 20 32 38 20 69 73 20 . ** 28 is
261f0 6e 6f 6e 7a 65 72 6f 2e 20 2a 2f 0a 20 20 20 20 nonzero. */.
26200 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e assert( pBt->n
26210 50 61 67 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a Page>0 );. }.
26220 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
26230 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 Leave(p);. }.
26240 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
26250 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
26260 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 cursor for the
26270 42 54 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 BTree whose root
26280 20 69 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a is on the page.
26290 2a 2a 20 69 54 61 62 6c 65 2e 20 49 66 20 61 20 ** iTable. If a
262a0 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 read-only cursor
262b0 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 is requested, i
262c0 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
262d0 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 t.** the caller
262e0 61 6c 72 65 61 64 79 20 68 61 73 20 61 74 20 6c already has at l
262f0 65 61 73 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 east a read-only
26300 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 transaction ope
26310 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 n.** on the data
26320 62 61 73 65 20 61 6c 72 65 61 64 79 2e 20 49 66 base already. If
26330 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 a write-cursor
26340 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 is requested, th
26350 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 en.** the caller
26360 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 is assumed to h
26370 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 ave an open writ
26380 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
26390 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 54 52 45 *.** If the BTRE
263a0 45 5f 57 52 43 53 52 20 62 69 74 20 6f 66 20 77 E_WRCSR bit of w
263b0 72 46 6c 61 67 20 69 73 20 63 6c 65 61 72 2c 20 rFlag is clear,
263c0 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
263d0 63 61 6e 20 6f 6e 6c 79 0a 2a 2a 20 62 65 20 75 can only.** be u
263e0 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 2e sed for reading.
263f0 20 20 49 66 20 74 68 65 20 42 54 52 45 45 5f 57 If the BTREE_W
26400 52 43 53 52 20 62 69 74 20 69 73 20 73 65 74 2c RCSR bit is set,
26410 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
26420 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 .** can be used
26430 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66 for reading or f
26440 6f 72 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 or writing if ot
26450 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 her conditions f
26460 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 61 72 or writing.** ar
26470 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 e also met. The
26480 73 65 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 se are the condi
26490 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 tions that must
264a0 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 0a be met in order.
264b0 2a 2a 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74 ** for writing t
264c0 6f 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a o be allowed:.**
264d0 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 .** 1: The curs
264e0 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 or must have bee
264f0 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 n opened with wr
26500 46 6c 61 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Flag containing
26510 42 54 52 45 45 5f 57 52 43 53 52 0a 2a 2a 0a 2a BTREE_WRCSR.**.*
26520 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 * 2: Other data
26530 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
26540 20 74 68 61 74 20 73 68 61 72 65 20 74 68 65 20 that share the
26550 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 same pager cache
26560 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 .** but whic
26570 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 h are not in the
26580 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 READ_UNCOMMITTE
26590 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 D state may not
265a0 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 have.** curs
265b0 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 ors open with wr
265c0 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 Flag==0 on the s
265d0 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 ame table. Othe
265e0 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 rwise.** the
265f0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
26600 20 74 68 69 73 20 77 72 69 74 65 20 63 75 72 73 this write curs
26610 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 or would be visi
26620 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 ble to.** th
26630 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 e read cursors i
26640 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 n the other data
26650 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
26660 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 .**.** 3: The d
26670 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 atabase must be
26680 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e writable (not on
26690 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 read-only media
266a0 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 ).**.** 4: Ther
266b0 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 e must be an act
266c0 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ive transaction.
266d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 54 52 45 45 .**.** The BTREE
266e0 5f 46 4f 52 44 45 4c 45 54 45 20 62 69 74 20 6f _FORDELETE bit o
266f0 66 20 77 72 46 6c 61 67 20 6d 61 79 20 6f 70 74 f wrFlag may opt
26700 69 6f 6e 61 6c 6c 79 20 62 65 20 73 65 74 20 69 ionally be set i
26710 66 20 42 54 52 45 45 5f 57 52 43 53 52 0a 2a 2a f BTREE_WRCSR.**
26720 20 69 73 20 73 65 74 2e 20 20 49 66 20 46 4f 52 is set. If FOR
26730 44 45 4c 45 54 45 20 69 73 20 73 65 74 2c 20 74 DELETE is set, t
26740 68 61 74 20 69 73 20 61 20 68 69 6e 74 20 74 6f hat is a hint to
26750 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
26760 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 ion that.** this
26770 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 6f 6e 6c cursor will onl
26780 79 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 65 y be used to see
26790 6b 20 74 6f 20 61 6e 64 20 64 65 6c 65 74 65 20 k to and delete
267a0 65 6e 74 72 69 65 73 20 6f 66 20 61 6e 20 69 6e entries of an in
267b0 64 65 78 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f dex.** as part o
267c0 66 20 61 20 6c 61 72 67 65 72 20 44 45 4c 45 54 f a larger DELET
267d0 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 E statement. Th
267e0 65 20 46 4f 52 44 45 4c 45 54 45 20 68 69 6e 74 e FORDELETE hint
267f0 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 0a is not used by.
26800 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e ** this implemen
26810 74 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 6e 20 tation. But in
26820 61 20 68 79 70 6f 74 68 65 74 69 63 61 6c 20 61 a hypothetical a
26830 6c 74 65 72 6e 61 74 69 76 65 20 73 74 6f 72 61 lternative stora
26840 67 65 20 65 6e 67 69 6e 65 20 0a 2a 2a 20 69 6e ge engine .** in
26850 20 77 68 69 63 68 20 69 6e 64 65 78 20 65 6e 74 which index ent
26860 72 69 65 73 20 61 72 65 20 61 75 74 6f 6d 61 74 ries are automat
26870 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 ically deleted w
26880 68 65 6e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e hen correspondin
26890 67 20 74 61 62 6c 65 0a 2a 2a 20 72 6f 77 73 20 g table.** rows
268a0 61 72 65 20 64 65 6c 65 74 65 64 2c 20 74 68 65 are deleted, the
268b0 20 46 4f 52 44 45 4c 45 54 45 20 66 6c 61 67 20 FORDELETE flag
268c0 69 73 20 61 20 68 69 6e 74 20 74 68 61 74 20 61 is a hint that a
268d0 6c 6c 20 53 45 45 4b 20 61 6e 64 20 44 45 4c 45 ll SEEK and DELE
268e0 54 45 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 TE.** operations
268f0 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 on this cursor
26900 63 61 6e 20 62 65 20 6e 6f 2d 6f 70 73 20 61 6e can be no-ops an
26910 64 20 61 6c 6c 20 52 45 41 44 20 6f 70 65 72 61 d all READ opera
26920 74 69 6f 6e 73 20 63 61 6e 20 0a 2a 2a 20 72 65 tions can .** re
26930 74 75 72 6e 20 61 20 6e 75 6c 6c 20 72 6f 77 20 turn a null row
26940 28 32 2d 62 79 74 65 73 3a 20 30 78 30 31 20 30 (2-bytes: 0x01 0
26950 78 30 30 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 x00)..**.** No c
26960 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 hecking is done
26970 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
26980 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 65 t page iTable re
26990 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 ally is the.** r
269a0 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d oot page of a b-
269b0 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 20 tree. If it is
269c0 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 not, then the cu
269d0 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a rsor acquired.**
269e0 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 will not work c
269f0 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 orrectly..**.**
26a00 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 It is assumed th
26a10 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 at the sqlite3Bt
26a20 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 20 reeCursorZero()
26a30 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 0a has been called.
26a40 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 6e ** on pCur to in
26a50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
26a60 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 20 ory space prior
26a70 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
26a80 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 routine..*/.sta
26a90 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 75 72 tic int btreeCur
26aa0 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c sor(. Btree *p,
26ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
26ad0 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 The btree */.
26ae0 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 int iTable,
26af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26b00 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 /* Root p
26b10 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 age of table to
26b20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 open */. int wr
26b30 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 Flag,
26b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26b50 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 /* 1 to write.
26b60 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 0 read-only */.
26b70 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
26b80 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 *pKeyInfo,
26b90 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
26ba0 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 73 arg to comparis
26bb0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 on function */.
26bc0 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 BtCursor *pCur
26bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26be0 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
26bf0 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 20 for new cursor
26c00 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 */.){. BtShared
26c10 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 *pBt = p->pBt;
26c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
26c30 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 65 20 * Shared b-tree
26c40 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 43 75 handle */. BtCu
26c50 72 73 6f 72 20 2a 70 58 3b 20 20 20 20 20 20 20 rsor *pX;
26c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26c70 20 20 20 2f 2a 20 4c 6f 6f 70 69 6e 67 20 6f 76 /* Looping ov
26c80 65 72 20 6f 74 68 65 72 20 61 6c 6c 20 63 75 72 er other all cur
26c90 73 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 sors */.. asser
26ca0 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
26cb0 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
26cc0 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 assert( wrFlag
26cd0 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 77 ==0 . || w
26ce0 72 46 6c 61 67 3d 3d 42 54 52 45 45 5f 57 52 43 rFlag==BTREE_WRC
26cf0 53 52 20 0a 20 20 20 20 20 20 20 7c 7c 20 77 72 SR . || wr
26d00 46 6c 61 67 3d 3d 28 42 54 52 45 45 5f 57 52 43 Flag==(BTREE_WRC
26d10 53 52 7c 42 54 52 45 45 5f 46 4f 52 44 45 4c 45 SR|BTREE_FORDELE
26d20 54 45 29 20 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 TE) . );.. /*
26d30 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 The following as
26d40 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 73 20 sert statements
26d50 76 65 72 69 66 79 20 74 68 61 74 20 69 66 20 74 verify that if t
26d60 68 69 73 20 69 73 20 61 20 73 68 61 72 61 62 6c his is a sharabl
26d70 65 20 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 e . ** b-tree d
26d80 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 6f 6e atabase, the con
26d90 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 nection is holdi
26da0 6e 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20 ng the required
26db0 74 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 table locks, .
26dc0 2a 2a 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f ** and that no o
26dd0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ther connection
26de0 68 61 73 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 has any open cur
26df0 73 6f 72 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 sor that conflic
26e00 74 73 20 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 ts with . ** th
26e10 69 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 is lock. */. a
26e20 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 ssert( hasShared
26e30 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
26e40 2c 20 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e , iTable, pKeyIn
26e50 66 6f 21 3d 30 2c 20 28 77 72 46 6c 61 67 3f 32 fo!=0, (wrFlag?2
26e60 3a 31 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 :1)) );. assert
26e70 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 21 ( wrFlag==0 || !
26e80 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 hasReadConflicts
26e90 28 70 2c 20 69 54 61 62 6c 65 29 20 29 3b 0a 0a (p, iTable) );..
26ea0 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
26eb0 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 the caller has
26ec0 6f 70 65 6e 65 64 20 74 68 65 20 72 65 71 75 69 opened the requi
26ed0 72 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e red transaction.
26ee0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
26ef0 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e >inTrans>TRANS_N
26f00 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ONE );. assert(
26f10 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 70 2d wrFlag==0 || p-
26f20 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
26f30 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 WRITE );. asser
26f40 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 26 t( pBt->pPage1 &
26f50 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 & pBt->pPage1->a
26f60 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 Data );. assert
26f70 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 28 ( wrFlag==0 || (
26f80 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
26f90 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 3d 3d BTS_READ_ONLY)==
26fa0 30 20 29 3b 0a 0a 20 20 69 66 28 20 77 72 46 6c 0 );.. if( wrFl
26fb0 61 67 20 29 7b 0a 20 20 20 20 61 6c 6c 6f 63 61 ag ){. alloca
26fc0 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 teTempSpace(pBt)
26fd0 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 ;. if( pBt->p
26fe0 54 6d 70 53 70 61 63 65 3d 3d 30 20 29 20 72 65 TmpSpace==0 ) re
26ff0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
27000 4d 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 M_BKPT;. }. if
27010 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 62 ( iTable==1 && b
27020 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 treePagecount(pB
27030 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 t)==0 ){. ass
27040 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 29 ert( wrFlag==0 )
27050 3b 0a 20 20 20 20 69 54 61 62 6c 65 20 3d 20 30 ;. iTable = 0
27060 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 ;. }.. /* Now
27070 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 that no other er
27080 72 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 rors can occur,
27090 66 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 finish filling i
270a0 6e 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 n the BtCursor.
270b0 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e ** variables an
270c0 64 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f d link the curso
270d0 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61 r into the BtSha
270e0 72 65 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 red list. */.
270f0 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d pCur->pgnoRoot =
27100 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 (Pgno)iTable;.
27110 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d pCur->iPage = -
27120 31 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 1;. pCur->pKeyI
27130 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a nfo = pKeyInfo;.
27140 20 20 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d pCur->pBtree =
27150 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 p;. pCur->pBt
27160 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 63 = pBt;. pCur->c
27170 75 72 46 6c 61 67 73 20 3d 20 77 72 46 6c 61 67 urFlags = wrFlag
27180 20 3f 20 42 54 43 46 5f 57 72 69 74 65 46 6c 61 ? BTCF_WriteFla
27190 67 20 3a 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 g : 0;. pCur->c
271a0 75 72 50 61 67 65 72 46 6c 61 67 73 20 3d 20 77 urPagerFlags = w
271b0 72 46 6c 61 67 20 3f 20 30 20 3a 20 50 41 47 45 rFlag ? 0 : PAGE
271c0 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c 59 3b 0a R_GET_READONLY;.
271d0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
271e0 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 75 e two or more cu
271f0 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 73 61 6d rsors on the sam
27200 65 20 62 74 72 65 65 2c 20 74 68 65 6e 20 61 6c e btree, then al
27210 6c 20 73 75 63 68 0a 20 20 2a 2a 20 63 75 72 73 l such. ** curs
27220 6f 72 73 20 2a 6d 75 73 74 2a 20 68 61 76 65 20 ors *must* have
27230 74 68 65 20 42 54 43 46 5f 4d 75 6c 74 69 70 6c the BTCF_Multipl
27240 65 20 66 6c 61 67 20 73 65 74 2e 20 2a 2f 0a 20 e flag set. */.
27250 20 66 6f 72 28 70 58 3d 70 42 74 2d 3e 70 43 75 for(pX=pBt->pCu
27260 72 73 6f 72 3b 20 70 58 3b 20 70 58 3d 70 58 2d rsor; pX; pX=pX-
27270 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
27280 20 70 58 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 28 pX->pgnoRoot==(
27290 50 67 6e 6f 29 69 54 61 62 6c 65 20 29 7b 0a 20 Pgno)iTable ){.
272a0 20 20 20 20 20 70 58 2d 3e 63 75 72 46 6c 61 67 pX->curFlag
272b0 73 20 7c 3d 20 42 54 43 46 5f 4d 75 6c 74 69 70 s |= BTCF_Multip
272c0 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e le;. pCur->
272d0 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 46 curFlags |= BTCF
272e0 5f 4d 75 6c 74 69 70 6c 65 3b 0a 20 20 20 20 7d _Multiple;. }
272f0 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 70 4e 65 . }. pCur->pNe
27300 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f xt = pBt->pCurso
27310 72 3b 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f r;. pBt->pCurso
27320 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 r = pCur;. pCur
27330 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
27340 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 65 74 R_INVALID;. ret
27350 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
27360 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
27370 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 eCursor(. Btree
27380 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
27390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
273a0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
273b0 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 ree */. int iTa
273c0 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ble,
273d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
273e0 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
273f0 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 e of table to op
27400 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c en */. int wrFl
27410 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ag,
27420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
27430 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 /* 1 to writ
27440 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e. 0 read-only *
27450 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
27460 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
27470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
27480 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 /* First arg to
27490 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 xCompare() */.
274a0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 BtCursor *pCur
274b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
274c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
274d0 72 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 rite new cursor
274e0 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
274f0 20 72 63 3b 0a 20 20 69 66 28 20 69 54 61 62 6c rc;. if( iTabl
27500 65 3c 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 e<1 ){. rc =
27510 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
27520 4b 50 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 KPT;. }else{.
27530 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
27540 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20 3d ter(p);. rc =
27550 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 btreeCursor(p,
27560 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 iTable, wrFlag,
27570 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b pKeyInfo, pCur);
27580 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
27590 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 eLeave(p);. }.
275a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
275b0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
275c0 73 69 7a 65 20 6f 66 20 61 20 42 74 43 75 72 73 size of a BtCurs
275d0 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 62 79 74 or object in byt
275e0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 es..**.** This i
275f0 6e 74 65 72 66 61 63 65 73 20 69 73 20 6e 65 65 nterfaces is nee
27600 64 65 64 20 73 6f 20 74 68 61 74 20 75 73 65 72 ded so that user
27610 73 20 6f 66 20 63 75 72 73 6f 72 73 20 63 61 6e s of cursors can
27620 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 preallocate.**
27630 73 75 66 66 69 63 69 65 6e 74 20 73 74 6f 72 61 sufficient stora
27640 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 63 75 72 ge to hold a cur
27650 73 6f 72 2e 20 20 54 68 65 20 42 74 43 75 72 73 sor. The BtCurs
27660 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 61 or object is opa
27670 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 72 73 20 que.** to users
27680 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 64 so they cannot d
27690 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 29 20 74 o the sizeof() t
276a0 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 68 65 79 hemselves - they
276b0 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 74 68 must call.** th
276c0 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 69 is routine..*/.i
276d0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
276e0 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 7b ursorSize(void){
276f0 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 . return ROUND8
27700 28 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72 (sizeof(BtCursor
27710 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ));.}../*.** Ini
27720 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 74 tialize memory t
27730 68 61 74 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 hat will be conv
27740 65 72 74 65 64 20 69 6e 74 6f 20 61 20 42 74 43 erted into a BtC
27750 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2a ursor object..**
27760 0a 2a 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 61 .** The simple a
27770 70 70 72 6f 61 63 68 20 68 65 72 65 20 77 6f 75 pproach here wou
27780 6c 64 20 62 65 20 74 6f 20 6d 65 6d 73 65 74 28 ld be to memset(
27790 29 20 74 68 65 20 65 6e 74 69 72 65 20 6f 62 6a ) the entire obj
277a0 65 63 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2e 20 ect.** to zero.
277b0 20 42 75 74 20 69 74 20 74 75 72 6e 73 20 6f 75 But it turns ou
277c0 74 20 74 68 61 74 20 74 68 65 20 61 70 50 61 67 t that the apPag
277d0 65 5b 5d 20 61 6e 64 20 61 69 49 64 78 5b 5d 20 e[] and aiIdx[]
277e0 61 72 72 61 79 73 0a 2a 2a 20 64 6f 20 6e 6f 74 arrays.** do not
277f0 20 6e 65 65 64 20 74 6f 20 62 65 20 7a 65 72 6f need to be zero
27800 65 64 20 61 6e 64 20 74 68 65 79 20 61 72 65 20 ed and they are
27810 6c 61 72 67 65 2c 20 73 6f 20 77 65 20 63 61 6e large, so we can
27820 20 73 61 76 65 20 61 20 6c 6f 74 0a 2a 2a 20 6f save a lot.** o
27830 66 20 72 75 6e 2d 74 69 6d 65 20 62 79 20 73 6b f run-time by sk
27840 69 70 70 69 6e 67 20 74 68 65 20 69 6e 69 74 69 ipping the initi
27850 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 6f alization of tho
27860 73 65 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a se elements..*/.
27870 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
27880 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 74 43 75 eCursorZero(BtCu
27890 72 73 6f 72 20 2a 70 29 7b 0a 20 20 6d 65 6d 73 rsor *p){. mems
278a0 65 74 28 70 2c 20 30 2c 20 6f 66 66 73 65 74 6f et(p, 0, offseto
278b0 66 28 42 74 43 75 72 73 6f 72 2c 20 69 50 61 67 f(BtCursor, iPag
278c0 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c e));.}../*.** Cl
278d0 6f 73 65 20 61 20 63 75 72 73 6f 72 2e 20 20 54 ose a cursor. T
278e0 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 he read lock on
278f0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
27900 65 20 69 73 20 72 65 6c 65 61 73 65 64 0a 2a 2a e is released.**
27910 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 63 when the last c
27920 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e ursor is closed.
27930 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
27940 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 treeCloseCursor(
27950 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
27960 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
27970 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b = pCur->pBtree;
27980 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29 7b . if( pBtree ){
27990 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
279a0 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a Bt = pCur->pBt;.
279b0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
279c0 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 Enter(pBtree);.
279d0 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
279e0 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 pCursor!=0 );.
279f0 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75 72 73 if( pBt->pCurs
27a00 6f 72 3d 3d 70 43 75 72 20 29 7b 0a 20 20 20 20 or==pCur ){.
27a10 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d pBt->pCursor =
27a20 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 pCur->pNext;.
27a30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 42 }else{. B
27a40 74 43 75 72 73 6f 72 20 2a 70 50 72 65 76 20 3d tCursor *pPrev =
27a50 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 pBt->pCursor;.
27a60 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 do{.
27a70 20 69 66 28 20 70 50 72 65 76 2d 3e 70 4e 65 78 if( pPrev->pNex
27a80 74 3d 3d 70 43 75 72 20 29 7b 0a 20 20 20 20 20 t==pCur ){.
27a90 20 20 20 20 20 70 50 72 65 76 2d 3e 70 4e 65 78 pPrev->pNex
27aa0 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b t = pCur->pNext;
27ab0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
27ac0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
27ad0 20 20 20 20 70 50 72 65 76 20 3d 20 70 50 72 65 pPrev = pPre
27ae0 76 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 v->pNext;.
27af0 7d 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 }while( ALWAYS(p
27b00 50 72 65 76 29 20 29 3b 0a 20 20 20 20 7d 0a 20 Prev) );. }.
27b10 20 20 20 62 74 72 65 65 52 65 6c 65 61 73 65 41 btreeReleaseA
27b20 6c 6c 43 75 72 73 6f 72 50 61 67 65 73 28 70 43 llCursorPages(pC
27b30 75 72 29 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 ur);. unlockB
27b40 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
27b50 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
27b60 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65 72 66 ree(pCur->aOverf
27b70 6c 6f 77 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 low);. sqlite
27b80 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 3_free(pCur->pKe
27b90 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 y);. sqlite3B
27ba0 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 treeLeave(pBtree
27bb0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
27bc0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
27bd0 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
27be0 65 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 e BtCursor* give
27bf0 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e n in the argumen
27c00 74 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a t has a valid.**
27c10 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 BtCursor.info s
27c20 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 tructure. If it
27c30 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
27c40 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 valid, call.** b
27c50 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 treeParseCell()
27c60 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a to fill it in..*
27c70 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e *.** BtCursor.in
27c80 66 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 fo is a cache of
27c90 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e the information
27ca0 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
27cb0 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 cell..** Using t
27cc0 68 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 his cache reduce
27cd0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
27ce0 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 calls to btreePa
27cf0 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2f 0a 23 69 rseCell()..*/.#i
27d00 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 fndef NDEBUG. s
27d10 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 tatic void asser
27d20 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 tCellInfo(BtCurs
27d30 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 or *pCur){. C
27d40 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
27d50 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 memset(&info,
27d60 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 0, sizeof(info))
27d70 3b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 ;. btreeParse
27d80 43 65 6c 6c 28 70 43 75 72 2d 3e 70 50 61 67 65 Cell(pCur->pPage
27d90 2c 20 70 43 75 72 2d 3e 69 78 2c 20 26 69 6e 66 , pCur->ix, &inf
27da0 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 o);. assert(
27db0 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 6d 65 CORRUPT_DB || me
27dc0 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 mcmp(&info, &pCu
27dd0 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 r->info, sizeof(
27de0 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d info))==0 );. }
27df0 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
27e00 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
27e10 78 29 0a 23 65 6e 64 69 66 0a 73 74 61 74 69 63 x).#endif.static
27e20 20 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e 45 SQLITE_NOINLINE
27e30 20 76 6f 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 void getCellInf
27e40 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 o(BtCursor *pCur
27e50 29 7b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 ){. if( pCur->i
27e60 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a nfo.nSize==0 ){.
27e70 20 20 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 pCur->curFla
27e80 67 73 20 7c 3d 20 42 54 43 46 5f 56 61 6c 69 64 gs |= BTCF_Valid
27e90 4e 4b 65 79 3b 0a 20 20 20 20 62 74 72 65 65 50 NKey;. btreeP
27ea0 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 70 arseCell(pCur->p
27eb0 50 61 67 65 2c 70 43 75 72 2d 3e 69 78 2c 26 70 Page,pCur->ix,&p
27ec0 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 7d 65 Cur->info);. }e
27ed0 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 43 lse{. assertC
27ee0 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
27ef0 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
27f00 45 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 EBUG /* The nex
27f10 74 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f t routine used o
27f20 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 nly within asser
27f30 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a t() statements *
27f40 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 /./*.** Return t
27f50 72 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e rue if the given
27f60 20 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c BtCursor is val
27f70 69 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 id. A valid cur
27f80 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 sor is one.** th
27f90 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 at is currently
27fa0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f pointing to a ro
27fb0 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 w in a (non-empt
27fc0 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 y) table..** Thi
27fd0 73 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74 s is a verificat
27fe0 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 ion routine is u
27ff0 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 sed only within
28000 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
28010 6e 74 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 nts..*/.int sqli
28020 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 te3BtreeCursorIs
28030 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a Valid(BtCursor *
28040 70 43 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 pCur){. return
28050 70 43 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 pCur && pCur->eS
28060 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
28070 49 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ID;.}.#endif /*
28080 4e 44 45 42 55 47 20 2a 2f 0a 69 6e 74 20 73 71 NDEBUG */.int sq
28090 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
280a0 49 73 56 61 6c 69 64 4e 4e 28 42 74 43 75 72 73 IsValidNN(BtCurs
280b0 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
280c0 65 72 74 28 20 70 43 75 72 21 3d 30 20 29 3b 0a ert( pCur!=0 );.
280d0 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 65 return pCur->e
280e0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
280f0 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 LID;.}../*.** Re
28100 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f turn the value o
28110 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 f the integer ke
28120 79 20 6f 72 20 22 72 6f 77 69 64 22 20 66 6f 72 y or "rowid" for
28130 20 61 20 74 61 62 6c 65 20 62 74 72 65 65 2e 0a a table btree..
28140 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
28150 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f is only valid fo
28160 72 20 61 20 63 75 72 73 6f 72 20 74 68 61 74 20 r a cursor that
28170 69 73 20 70 6f 69 6e 74 69 6e 67 20 69 6e 74 6f is pointing into
28180 20 61 0a 2a 2a 20 6f 72 64 69 6e 61 72 79 20 74 a.** ordinary t
28190 61 62 6c 65 20 62 74 72 65 65 2e 20 20 49 66 20 able btree. If
281a0 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 the cursor point
281b0 73 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 62 74 s to an index bt
281c0 72 65 65 20 6f 72 0a 2a 2a 20 69 73 20 69 6e 76 ree or.** is inv
281d0 61 6c 69 64 2c 20 74 68 65 20 72 65 73 75 6c 74 alid, the result
281e0 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
281f0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
28200 2f 0a 69 36 34 20 73 71 6c 69 74 65 33 42 74 72 /.i64 sqlite3Btr
28210 65 65 49 6e 74 65 67 65 72 4b 65 79 28 42 74 43 eeIntegerKey(BtC
28220 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
28230 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
28240 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
28250 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
28260 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
28270 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
28280 72 74 28 20 70 43 75 72 2d 3e 63 75 72 49 6e 74 rt( pCur->curInt
28290 4b 65 79 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c Key );. getCell
282a0 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 72 65 Info(pCur);. re
282b0 74 75 72 6e 20 70 43 75 72 2d 3e 69 6e 66 6f 2e turn pCur->info.
282c0 6e 4b 65 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 nKey;.}../*.** R
282d0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
282e0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 79 of bytes of pay
282f0 6c 6f 61 64 20 66 6f 72 20 74 68 65 20 65 6e 74 load for the ent
28300 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73 0a ry that pCur is.
28310 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 ** currently poi
28320 6e 74 69 6e 67 20 74 6f 2e 20 20 46 6f 72 20 74 nting to. For t
28330 61 62 6c 65 20 62 74 72 65 65 73 2c 20 74 68 69 able btrees, thi
28340 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 61 6d s will be the am
28350 6f 75 6e 74 0a 2a 2a 20 6f 66 20 64 61 74 61 2e ount.** of data.
28360 20 20 46 6f 72 20 69 6e 64 65 78 20 62 74 72 65 For index btre
28370 65 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 es, this will be
28380 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
28390 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 key..**.** The
283a0 63 61 6c 6c 65 72 20 6d 75 73 74 20 67 75 61 72 caller must guar
283b0 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 antee that the c
283c0 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
283d0 67 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a g to a non-NULL.
283e0 2a 2a 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 ** valid entry.
283f0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
28400 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f the calling pro
28410 63 65 64 75 72 65 20 6d 75 73 74 20 67 75 61 72 cedure must guar
28420 61 6e 74 65 65 0a 2a 2a 20 74 68 61 74 20 74 68 antee.** that th
28430 65 20 63 75 72 73 6f 72 20 68 61 73 20 43 75 72 e cursor has Cur
28440 73 6f 72 2e 65 53 74 61 74 65 3d 3d 43 55 52 53 sor.eState==CURS
28450 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2f 0a 75 33 32 OR_VALID..*/.u32
28460 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 79 sqlite3BtreePay
28470 6c 6f 61 64 53 69 7a 65 28 42 74 43 75 72 73 6f loadSize(BtCurso
28480 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 r *pCur){. asse
28490 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
284a0 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
284b0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
284c0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
284d0 49 44 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 ID );. getCellI
284e0 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 72 65 74 nfo(pCur);. ret
284f0 75 72 6e 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e urn pCur->info.n
28500 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 2f 2a 0a 2a Payload;.}../*.*
28510 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 * Given the page
28520 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 number of an ov
28530 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 erflow page in t
28540 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72 he database (par
28550 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c ameter.** ovfl),
28560 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 this function f
28570 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 inds the page nu
28580 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
28590 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a page in the .**
285a0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
285b0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
285c0 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 If possible, it
285d0 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 uses the auto-va
285e0 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d cuum.** pointer-
285f0 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 map data instead
28600 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 of reading the
28610 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 content of page
28620 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a ovfl to do so. .
28630 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
28640 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 r occurs an SQLi
28650 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
28660 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
28670 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wise:.**.** The
28680 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
28690 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 he next overflow
286a0 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e page in the lin
286b0 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 ked list is .**
286c0 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e written to *pPgn
286d0 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f oNext. If page o
286e0 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 vfl is the last
286f0 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b page in its link
28700 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 ed .** list, *pP
28710 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 gnoNext is set t
28720 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 o zero. .**.** I
28730 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 f ppPage is not
28740 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 NULL, and a refe
28750 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d rence to the Mem
28760 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 Page object corr
28770 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 esponding.** to
28780 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 page number pOvf
28790 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 l was obtained,
287a0 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 then *ppPage is
287b0 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 set to point to
287c0 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 that.** referenc
287d0 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73 e. It is the res
287e0 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 ponsibility of t
287f0 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c he caller to cal
28800 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a l releasePage().
28810 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f ** on *ppPage to
28820 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65 free the refere
28830 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 nce. In no refer
28840 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 ence was obtaine
28850 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 d (because.** th
28860 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 e pointer-map wa
28870 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e s used to obtain
28880 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a the value for *
28890 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e pPgnoNext), then
288a0 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 .** *ppPage is s
288b0 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 et to zero..*/.s
288c0 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 tatic int getOve
288d0 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 rflowPage(. BtS
288e0 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
288f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
28900 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
28910 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 /. Pgno ovfl,
28920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28930 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 /* Current over
28940 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 flow page number
28950 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
28960 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 ppPage,
28970 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 /* OUT: MemPa
28980 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 ge handle (may b
28990 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e e NULL) */. Pgn
289a0 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 o *pPgnoNext
289b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
289c0 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 : Next overflow
289d0 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 page number */.)
289e0 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 {. Pgno next =
289f0 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 0;. MemPage *pP
28a00 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 age = 0;. int r
28a10 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
28a20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
28a30 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
28a40 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
28a50 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b sert(pPgnoNext);
28a60 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
28a70 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
28a80 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e . /* Try to fin
28a90 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 d the next page
28aa0 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 in the overflow
28ab0 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 list using the.
28ac0 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 ** autovacuum p
28ad0 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 ointer-map pages
28ae0 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 . Guess that the
28af0 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 next page in .
28b00 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ** the overflow
28b10 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 list is page nu
28b20 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 mber (ovfl+1). I
28b30 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 f that guess tur
28b40 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 ns . ** out to
28b50 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 be wrong, fall b
28b60 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 ack to loading t
28b70 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 he data of page
28b80 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 . ** number ovf
28b90 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 l to determine t
28ba0 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d he next page num
28bb0 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ber.. */. if(
28bc0 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
28bd0 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f ){. Pgno pgno
28be0 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 ;. Pgno iGues
28bf0 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 s = ovfl+1;.
28c00 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 u8 eType;.. w
28c10 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 hile( PTRMAP_ISP
28c20 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 AGE(pBt, iGuess)
28c30 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 || iGuess==PEND
28c40 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
28c50 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 t) ){. iGue
28c60 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ss++;. }..
28c70 20 69 66 28 20 69 47 75 65 73 73 3c 3d 62 74 72 if( iGuess<=btr
28c80 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 eePagecount(pBt)
28c90 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
28ca0 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 trmapGet(pBt, iG
28cb0 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 uess, &eType, &p
28cc0 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 gno);. if(
28cd0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
28ce0 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
28cf0 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f VERFLOW2 && pgno
28d00 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 ==ovfl ){.
28d10 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b next = iGuess;
28d20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
28d30 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
28d40 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e }. }. }.#en
28d50 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e dif.. assert( n
28d60 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 ext==0 || rc==SQ
28d70 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 LITE_DONE );. i
28d80 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
28d90 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 ){. rc = btr
28da0 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f eeGetPage(pBt, o
28db0 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 28 70 70 vfl, &pPage, (pp
28dc0 50 61 67 65 3d 3d 30 29 20 3f 20 50 41 47 45 52 Page==0) ? PAGER
28dd0 5f 47 45 54 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 _GET_READONLY :
28de0 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0);. assert(
28df0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
28e00 20 70 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 pPage==0 );.
28e10 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
28e20 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 OK ){. next
28e30 20 3d 20 67 65 74 34 62 79 74 65 28 70 50 61 67 = get4byte(pPag
28e40 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 7d e->aData);. }
28e50 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 . }.. *pPgnoNe
28e60 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 xt = next;. if(
28e70 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2a ppPage ){. *
28e80 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a ppPage = pPage;.
28e90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c }else{. rel
28ea0 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
28eb0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 . }. return (r
28ec0 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f c==SQLITE_DONE ?
28ed0 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 SQLITE_OK : rc)
28ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 ;.}../*.** Copy
28ef0 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 data from a buff
28f00 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 er to a page, or
28f10 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 from a page to
28f20 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 a buffer..**.**
28f30 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20 70 6f pPayload is a po
28f40 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 73 74 inter to data st
28f50 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61 73 65 ored on database
28f60 20 70 61 67 65 20 70 44 62 50 61 67 65 2e 0a 2a page pDbPage..*
28f70 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 65 4f * If argument eO
28f80 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e p is false, then
28f90 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 nByte bytes of
28fa0 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 0a data are copied.
28fb0 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 ** from pPayload
28fc0 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 to the buffer p
28fd0 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70 42 75 ointed at by pBu
28fe0 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74 72 75 f. If eOp is tru
28ff0 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 e,.** then sqlit
29000 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 69 e3PagerWrite() i
29010 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 s called on pDbP
29020 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79 age and nByte by
29030 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 tes.** of data a
29040 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 re copied from t
29050 68 65 20 62 75 66 66 65 72 20 70 42 75 66 20 74 he buffer pBuf t
29060 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a o pPayload..**.*
29070 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
29080 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 eturned on succe
29090 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e ss, otherwise an
290a0 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a error code..*/.
290b0 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70 79 50 static int copyP
290c0 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a ayload(. void *
290d0 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 pPayload,
290e0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
290f0 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 o page data */.
29100 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 void *pBuf,
29110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
29120 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 inter to buffer
29130 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 */. int nByte,
29140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
29150 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
29160 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 s to copy */. i
29170 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20 20 20 nt eOp,
29180 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e /* 0 ->
29190 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 2c copy from page,
291a0 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 1 -> copy to pa
291b0 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a ge */. DbPage *
291c0 70 44 62 50 61 67 65 20 20 20 20 20 20 20 20 20 pDbPage
291d0 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
291e0 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f ning pPayload */
291f0 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 7b .){. if( eOp ){
29200 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 . /* Copy dat
29210 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20 74 6f a from buffer to
29220 20 70 61 67 65 20 28 61 20 77 72 69 74 65 20 6f page (a write o
29230 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 peration) */.
29240 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
29250 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 3PagerWrite(pDbP
29260 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
29270 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
29280 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
29290 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
292a0 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c (pPayload, pBuf,
292b0 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 nByte);. }else
292c0 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 {. /* Copy da
292d0 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 ta from page to
292e0 62 75 66 66 65 72 20 28 61 20 72 65 61 64 20 6f buffer (a read o
292f0 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 peration) */.
29300 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 70 50 memcpy(pBuf, pP
29310 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a ayload, nByte);.
29320 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
29330 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
29340 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
29350 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f s used to read o
29360 72 20 6f 76 65 72 77 72 69 74 65 20 70 61 79 6c r overwrite payl
29370 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a oad information.
29380 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 ** for the entry
29390 20 74 68 61 74 20 74 68 65 20 70 43 75 72 20 63 that the pCur c
293a0 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
293b0 67 20 74 6f 2e 20 54 68 65 20 65 4f 70 0a 2a 2a g to. The eOp.**
293c0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 74 argument is int
293d0 65 72 70 72 65 74 65 64 20 61 73 20 66 6f 6c 6c erpreted as foll
293e0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 30 3a 20 ows:.**.** 0:
293f0 54 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 The operation is
29400 20 61 20 72 65 61 64 2e 20 50 6f 70 75 6c 61 74 a read. Populat
29410 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 e the overflow c
29420 61 63 68 65 2e 0a 2a 2a 20 20 20 31 3a 20 54 68 ache..** 1: Th
29430 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 e operation is a
29440 20 77 72 69 74 65 2e 20 50 6f 70 75 6c 61 74 65 write. Populate
29450 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 61 the overflow ca
29460 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 che..**.** A tot
29470 61 6c 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 al of "amt" byte
29480 73 20 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 s are read or wr
29490 69 74 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 itten beginning
294a0 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 at "offset"..**
294b0 44 61 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 Data is read to
294c0 6f 72 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 or from the buff
294d0 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 er pBuf..**.** T
294e0 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 he content being
294f0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
29500 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 6f 6e might appear on
29510 20 74 68 65 20 6d 61 69 6e 20 70 61 67 65 0a 2a the main page.*
29520 2a 20 6f 72 20 62 65 20 73 63 61 74 74 65 72 65 * or be scattere
29530 64 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c d out on multipl
29540 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 e overflow pages
29550 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
29560 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 65 6e urrent cursor en
29570 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 try uses one or
29580 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
29590 67 65 73 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 ges.** this func
295a0 74 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 63 61 74 tion may allocat
295b0 65 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 e space for and
295c0 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 0a lazily populate.
295d0 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 ** the overflow
295e0 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 page-list cache
295f0 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e array (BtCursor.
29600 61 4f 76 65 72 66 6c 6f 77 29 2e 20 0a 2a 2a 20 aOverflow). .**
29610 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 Subsequent calls
29620 20 75 73 65 20 74 68 69 73 20 63 61 63 68 65 20 use this cache
29630 74 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 to make seeking
29640 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 to the supplied
29650 6f 66 66 73 65 74 20 0a 2a 2a 20 6d 6f 72 65 20 offset .** more
29660 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a efficient..**.**
29670 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f Once an overflo
29680 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
29690 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 e has been alloc
296a0 61 74 65 64 2c 20 69 74 20 6d 75 73 74 20 62 65 ated, it must be
296b0 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 20 .** invalidated
296c0 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 75 if some other cu
296d0 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 74 rsor writes to t
296e0 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f he same table, o
296f0 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 73 r if.** the curs
29700 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 61 or is moved to a
29710 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e 20 different row.
29720 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e Additionally, in
29730 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 auto-vacuum.**
29740 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 mode, the follow
29750 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 69 ing events may i
29760 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 65 nvalidate an ove
29770 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
29780 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a cache..**.** *
29790 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 An incremental
297a0 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 vacuum,.** * A
297b0 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f commit in auto_
297c0 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f vacuum="full" mo
297d0 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74 de,.** * Creat
297e0 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 79 ing a table (may
297f0 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 require moving
29800 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
29810 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
29820 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 0a accessPayload(.
29830 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
29840 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 , /* Cursor
29850 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 pointing to ent
29860 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 ry to read from
29870 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 2c */. u32 offset,
29880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 /* Beg
29890 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 in reading this
298a0 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64 far into payload
298b0 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20 20 */. u32 amt,
298c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
298d0 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 ad this many byt
298e0 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 es */. unsigned
298f0 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20 char *pBuf, /*
29900 57 72 69 74 65 20 74 68 65 20 62 79 74 65 73 20 Write the bytes
29910 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 into this buffer
29920 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20 20 */ . int eOp
29930 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
29940 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e ero to read. non
29950 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 -zero to write.
29960 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 */.){. unsigned
29970 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b char *aPayload;
29980 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
29990 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 49 64 TE_OK;. int iId
299a0 78 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 x = 0;. MemPage
299b0 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
299c0 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 pPage;
299d0 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 70 61 /* Btree pa
299e0 67 65 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e ge of current en
299f0 74 72 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 try */. BtShare
29a00 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 d *pBt = pCur->p
29a10 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Bt;
29a20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 /* Btree th
29a30 69 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 is cursor belong
29a40 73 20 74 6f 20 2a 2f 0a 23 69 66 64 65 66 20 53 s to */.#ifdef S
29a50 51 4c 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 QLITE_DIRECT_OVE
29a60 52 46 4c 4f 57 5f 52 45 41 44 0a 20 20 75 6e 73 RFLOW_READ. uns
29a70 69 67 6e 65 64 20 63 68 61 72 20 2a 20 63 6f 6e igned char * con
29a80 73 74 20 70 42 75 66 53 74 61 72 74 20 3d 20 70 st pBufStart = p
29a90 42 75 66 3b 20 20 20 20 20 2f 2a 20 53 74 61 72 Buf; /* Star
29aa0 74 20 6f 66 20 6f 72 69 67 69 6e 61 6c 20 6f 75 t of original ou
29ab0 74 20 62 75 66 66 65 72 20 2a 2f 0a 23 65 6e 64 t buffer */.#end
29ac0 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 if.. assert( pP
29ad0 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 age );. assert(
29ae0 20 65 4f 70 3d 3d 30 20 7c 7c 20 65 4f 70 3d 3d eOp==0 || eOp==
29af0 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 1 );. assert( p
29b00 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
29b10 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
29b20 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 78 3c ssert( pCur->ix<
29b30 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
29b40 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
29b50 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
29b60 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e );.. getCellIn
29b70 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 fo(pCur);. aPay
29b80 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 load = pCur->inf
29b90 6f 2e 70 50 61 79 6c 6f 61 64 3b 0a 20 20 61 73 o.pPayload;. as
29ba0 73 65 72 74 28 20 6f 66 66 73 65 74 2b 61 6d 74 sert( offset+amt
29bb0 20 3c 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e <= pCur->info.n
29bc0 50 61 79 6c 6f 61 64 20 29 3b 0a 0a 20 20 61 73 Payload );.. as
29bd0 73 65 72 74 28 20 61 50 61 79 6c 6f 61 64 20 3e sert( aPayload >
29be0 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b pPage->aData );
29bf0 0a 20 20 69 66 28 20 28 75 70 74 72 29 28 61 50 . if( (uptr)(aP
29c00 61 79 6c 6f 61 64 20 2d 20 70 50 61 67 65 2d 3e ayload - pPage->
29c10 61 44 61 74 61 29 20 3e 20 28 70 42 74 2d 3e 75 aData) > (pBt->u
29c20 73 61 62 6c 65 53 69 7a 65 20 2d 20 70 43 75 72 sableSize - pCur
29c30 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 29 20 29 ->info.nLocal) )
29c40 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 {. /* Trying
29c50 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 to read or write
29c60 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
29c70 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 the data is an
29c80 65 72 72 6f 72 2e 20 20 54 68 65 0a 20 20 20 20 error. The.
29c90 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 ** conditional a
29ca0 62 6f 76 65 20 69 73 20 72 65 61 6c 6c 79 3a 0a bove is really:.
29cb0 20 20 20 20 2a 2a 20 20 20 20 26 61 50 61 79 6c ** &aPayl
29cc0 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e oad[pCur->info.n
29cd0 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d Local] > &pPage-
29ce0 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 >aData[pBt->usab
29cf0 6c 65 53 69 7a 65 5d 0a 20 20 20 20 2a 2a 20 62 leSize]. ** b
29d00 75 74 20 69 73 20 72 65 63 61 73 74 20 69 6e 74 ut is recast int
29d10 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20 66 6f o its current fo
29d20 72 6d 20 74 6f 20 61 76 6f 69 64 20 69 6e 74 65 rm to avoid inte
29d30 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 70 72 6f ger overflow pro
29d40 62 6c 65 6d 73 0a 20 20 20 20 2a 2f 0a 20 20 20 blems. */.
29d50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
29d60 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 50 61 67 ORRUPT_PGNO(pPag
29d70 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 20 e->pgno);. }..
29d80 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 /* Check if dat
29d90 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 a must be read/w
29da0 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 ritten to/from t
29db0 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 he btree page it
29dc0 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f self. */. if( o
29dd0 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f ffset<pCur->info
29de0 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 .nLocal ){. i
29df0 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 nt a = amt;.
29e00 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 if( a+offset>pCu
29e10 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 r->info.nLocal )
29e20 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 {. a = pCur
29e30 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 ->info.nLocal -
29e40 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 offset;. }.
29e50 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f rc = copyPaylo
29e60 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 ad(&aPayload[off
29e70 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 set], pBuf, a, e
29e80 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 Op, pPage->pDbPa
29e90 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 ge);. offset
29ea0 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d = 0;. pBuf +=
29eb0 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 a;. amt -= a
29ec0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f ;. }else{. o
29ed0 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 ffset -= pCur->i
29ee0 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a nfo.nLocal;. }.
29ef0 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
29f00 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 TE_OK && amt>0 )
29f10 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 {. const u32
29f20 6f 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e ovflSize = pBt->
29f30 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 usableSize - 4;
29f40 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e /* Bytes conten
29f50 74 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 t per ovfl page
29f60 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 */. Pgno next
29f70 50 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 Page;.. nextP
29f80 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
29f90 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 aPayload[pCur->i
29fa0 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 20 nfo.nLocal]);..
29fb0 20 20 20 2f 2a 20 49 66 20 74 68 65 20 42 74 43 /* If the BtC
29fc0 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b ursor.aOverflow[
29fd0 5d 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 ] has not been a
29fe0 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61 llocated, alloca
29ff0 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 20 20 2a te it now.. *
2a000 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 61 4f 76 *. ** The aOv
2a010 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 69 erflow[] array i
2a020 73 20 73 69 7a 65 64 20 61 74 20 6f 6e 65 20 65 s sized at one e
2a030 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 ntry for each ov
2a040 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 erflow page.
2a050 2a 2a 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c ** in the overfl
2a060 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 ow chain. The pa
2a070 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
2a080 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 first overflow
2a090 70 61 67 65 20 69 73 0a 20 20 20 20 2a 2a 20 73 page is. ** s
2a0a0 74 6f 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c tored in aOverfl
2a0b0 6f 77 5b 30 5d 2c 20 65 74 63 2e 20 41 20 76 61 ow[0], etc. A va
2a0c0 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 lue of 0 in the
2a0d0 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 aOverflow[] arra
2a0e0 79 0a 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20 22 y. ** means "
2a0f0 6e 6f 74 20 79 65 74 20 6b 6e 6f 77 6e 22 20 28 not yet known" (
2a100 74 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a the cache is laz
2a110 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a ily populated)..
2a120 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
2a130 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
2a140 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 BTCF_ValidOvfl)
2a150 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ==0 ){. int
2a160 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75 72 2d 3e nOvfl = (pCur->
2a170 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 info.nPayload-pC
2a180 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b ur->info.nLocal+
2a190 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f 76 66 6c ovflSize-1)/ovfl
2a1a0 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Size;. if(
2a1b0 6e 4f 76 66 6c 3e 70 43 75 72 2d 3e 6e 4f 76 66 nOvfl>pCur->nOvf
2a1c0 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 lAlloc ){.
2a1d0 20 20 50 67 6e 6f 20 2a 61 4e 65 77 20 3d 20 28 Pgno *aNew = (
2a1e0 50 67 6e 6f 2a 29 73 71 6c 69 74 65 33 52 65 61 Pgno*)sqlite3Rea
2a1f0 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 20 20 20 lloc(.
2a200 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f pCur->aOverflo
2a210 77 2c 20 6e 4f 76 66 6c 2a 32 2a 73 69 7a 65 6f w, nOvfl*2*sizeo
2a220 66 28 50 67 6e 6f 29 0a 20 20 20 20 20 20 20 20 f(Pgno).
2a230 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 );. if( a
2a240 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 New==0 ){.
2a250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
2a260 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 E_NOMEM_BKPT;.
2a270 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2a280 20 20 20 20 20 20 20 70 43 75 72 2d 3e 6e 4f 76 pCur->nOv
2a290 66 6c 41 6c 6c 6f 63 20 3d 20 6e 4f 76 66 6c 2a flAlloc = nOvfl*
2a2a0 32 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 2;. pCu
2a2b0 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 61 r->aOverflow = a
2a2c0 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 New;. }.
2a2d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d }. mem
2a2e0 73 65 74 28 70 43 75 72 2d 3e 61 4f 76 65 72 66 set(pCur->aOverf
2a2f0 6c 6f 77 2c 20 30 2c 20 6e 4f 76 66 6c 2a 73 69 low, 0, nOvfl*si
2a300 7a 65 6f 66 28 50 67 6e 6f 29 29 3b 0a 20 20 20 zeof(Pgno));.
2a310 20 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 pCur->curFlag
2a320 73 20 7c 3d 20 42 54 43 46 5f 56 61 6c 69 64 4f s |= BTCF_ValidO
2a330 76 66 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a vfl;. }else{.
2a340 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
2a350 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
2a360 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 st cache has bee
2a370 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 n allocated and
2a380 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 65 6e 74 the. ** ent
2a390 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 ry for the first
2a3a0 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c required overfl
2a3b0 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 64 ow page is valid
2a3c0 2c 20 73 6b 69 70 0a 20 20 20 20 20 20 2a 2a 20 , skip. **
2a3d0 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a directly to it..
2a3e0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
2a3f0 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c f( pCur->aOverfl
2a400 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 ow[offset/ovflSi
2a410 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ze] ){. i
2a420 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f 76 Idx = (offset/ov
2a430 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 flSize);.
2a440 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 nextPage = pCur
2a450 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
2a460 5d 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 ];. offse
2a470 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c t = (offset%ovfl
2a480 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Size);. }.
2a490 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 }.. assert
2a4a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
2a4b0 26 26 20 61 6d 74 3e 30 20 29 3b 0a 20 20 20 20 && amt>0 );.
2a4c0 77 68 69 6c 65 28 20 6e 65 78 74 50 61 67 65 20 while( nextPage
2a4d0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 ){. /* If r
2a4e0 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 equired, populat
2a4f0 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 e the overflow p
2a500 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 age-list cache.
2a510 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
2a520 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
2a530 5b 69 49 64 78 5d 3d 3d 30 0a 20 20 20 20 20 20 [iIdx]==0.
2a540 20 20 20 20 20 20 20 20 7c 7c 20 70 43 75 72 2d || pCur-
2a550 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d >aOverflow[iIdx]
2a560 3d 3d 6e 65 78 74 50 61 67 65 0a 20 20 20 20 20 ==nextPage.
2a570 20 20 20 20 20 20 20 20 20 7c 7c 20 43 4f 52 52 || CORR
2a580 55 50 54 5f 44 42 20 29 3b 0a 20 20 20 20 20 20 UPT_DB );.
2a590 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
2a5a0 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 iIdx] = nextPage
2a5b0 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 ;.. if( off
2a5c0 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b set>=ovflSize ){
2a5d0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
2a5e0 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 only reason to r
2a5f0 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69 73 ead this page is
2a600 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 to obtain the p
2a610 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e age. ** n
2a620 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 umber for the ne
2a630 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f xt page in the o
2a640 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 verflow chain. T
2a650 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 he page.
2a660 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 ** data is not r
2a670 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 equired. So firs
2a680 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 t try to lookup
2a690 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 the overflow.
2a6a0 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 ** page-lis
2a6b0 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c t cache, if any,
2a6c0 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 then fall back
2a6d0 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c to the getOverfl
2a6e0 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 owPage().
2a6f0 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 ** function..
2a700 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
2a710 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 assert( pCur->c
2a720 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 urFlags & BTCF_V
2a730 61 6c 69 64 4f 76 66 6c 20 29 3b 0a 20 20 20 20 alidOvfl );.
2a740 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
2a750 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3d 3d 70 42 ->pBtree->db==pB
2a760 74 2d 3e 64 62 20 29 3b 0a 20 20 20 20 20 20 20 t->db );.
2a770 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 if( pCur->aOver
2a780 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a flow[iIdx+1] ){.
2a790 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 nextPa
2a7a0 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 ge = pCur->aOver
2a7b0 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 flow[iIdx+1];.
2a7c0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2a7d0 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f rc = getO
2a7e0 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c verflowPage(pBt,
2a7f0 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 6e nextPage, 0, &n
2a800 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 extPage);.
2a810 20 20 7d 0a 20 20 20 20 20 20 20 20 6f 66 66 73 }. offs
2a820 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a et -= ovflSize;.
2a830 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2a840 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 /* Need to
2a850 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 70 read this page p
2a860 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 roperly. It cont
2a870 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 ains some of the
2a880 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 . ** rang
2a890 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 e of data that i
2a8a0 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f s being read (eO
2a8b0 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e p==0) or written
2a8c0 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 (eOp!=0)..
2a8d0 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
2a8e0 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 52 46 ITE_DIRECT_OVERF
2a8f0 4c 4f 57 5f 52 45 41 44 0a 20 20 20 20 20 20 20 LOW_READ.
2a900 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 sqlite3_file *f
2a910 64 3b 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 d; /* File
2a920 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 64 6f from which to do
2a930 20 64 69 72 65 63 74 20 6f 76 65 72 66 6c 6f 77 direct overflow
2a940 20 72 65 61 64 20 2a 2f 0a 23 65 6e 64 69 66 0a read */.#endif.
2a950 20 20 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 int a =
2a960 61 6d 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 amt;. if(
2a970 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 a + offset > ov
2a980 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 flSize ){.
2a990 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65 a = ovflSize
2a9a0 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 - offset;.
2a9b0 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c }..#ifdef SQL
2a9c0 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 52 46 ITE_DIRECT_OVERF
2a9d0 4c 4f 57 5f 52 45 41 44 0a 20 20 20 20 20 20 20 LOW_READ.
2a9e0 20 2f 2a 20 49 66 20 61 6c 6c 20 74 68 65 20 66 /* If all the f
2a9f0 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 ollowing are tru
2aa00 65 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 e:. **.
2aa10 20 20 20 20 20 20 2a 2a 20 20 20 31 29 20 74 68 ** 1) th
2aa20 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70 65 is is a read ope
2aa30 72 61 74 69 6f 6e 2c 20 61 6e 64 20 0a 20 20 20 ration, and .
2aa40 20 20 20 20 20 2a 2a 20 20 20 32 29 20 64 61 74 ** 2) dat
2aa50 61 20 69 73 20 72 65 71 75 69 72 65 64 20 66 72 a is required fr
2aa60 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 om the start of
2aa70 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 this overflow pa
2aa80 67 65 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 ge, and.
2aa90 2a 2a 20 20 20 33 29 20 74 68 65 72 65 20 69 73 ** 3) there is
2aaa0 20 6e 6f 20 6f 70 65 6e 20 77 72 69 74 65 2d 74 no open write-t
2aab0 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a ransaction, and.
2aac0 20 20 20 20 20 20 20 20 2a 2a 20 20 20 34 29 20 ** 4)
2aad0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
2aae0 66 69 6c 65 2d 62 61 63 6b 65 64 2c 20 61 6e 64 file-backed, and
2aaf0 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 35 29 . ** 5)
2ab00 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
2ab10 20 69 6e 20 74 68 65 20 57 41 4c 20 66 69 6c 65 in the WAL file
2ab20 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 36 29 . ** 6)
2ab30 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 at least 4 byte
2ab40 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 s have already b
2ab50 65 65 6e 20 72 65 61 64 20 69 6e 74 6f 20 74 68 een read into th
2ab60 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 e output buffer
2ab70 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
2ab80 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 61 74 61 ** then data
2ab90 20 63 61 6e 20 62 65 20 72 65 61 64 20 64 69 72 can be read dir
2aba0 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 ectly from the d
2abb0 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 74 atabase file int
2abc0 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a o the. **
2abd0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 output buffer,
2abe0 62 79 70 61 73 73 69 6e 67 20 74 68 65 20 70 61 bypassing the pa
2abf0 67 65 2d 63 61 63 68 65 20 61 6c 74 6f 67 65 74 ge-cache altoget
2ac00 68 65 72 2e 20 54 68 69 73 20 73 70 65 65 64 73 her. This speeds
2ac10 0a 20 20 20 20 20 20 20 20 2a 2a 20 75 70 20 6c . ** up l
2ac20 6f 61 64 69 6e 67 20 6c 61 72 67 65 20 72 65 63 oading large rec
2ac30 6f 72 64 73 20 74 68 61 74 20 73 70 61 6e 20 6d ords that span m
2ac40 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 any overflow pag
2ac50 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 es.. */.
2ac60 20 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d if( eOp==
2ac70 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
2ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ac90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2aca0 20 28 31 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 (1) */.
2acb0 20 26 26 20 6f 66 66 73 65 74 3d 3d 30 20 20 20 && offset==0
2acc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2acd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ace0 20 20 20 20 20 20 20 2f 2a 20 28 32 29 20 2a 2f /* (2) */
2acf0 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 42 74 . && pBt
2ad00 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
2ad10 3d 54 52 41 4e 53 5f 52 45 41 44 20 20 20 20 20 =TRANS_READ
2ad20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ad30 2f 2a 20 28 33 29 20 2a 2f 0a 20 20 20 20 20 20 /* (3) */.
2ad40 20 20 20 26 26 20 28 66 64 20 3d 20 73 71 6c 69 && (fd = sqli
2ad50 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 74 te3PagerFile(pBt
2ad60 2d 3e 70 50 61 67 65 72 29 29 2d 3e 70 4d 65 74 ->pPager))->pMet
2ad70 68 6f 64 73 20 20 20 20 20 2f 2a 20 28 34 29 20 hods /* (4)
2ad80 2a 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 30 */. && 0
2ad90 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 55 73 ==sqlite3PagerUs
2ada0 65 57 61 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 eWal(pBt->pPager
2adb0 2c 20 6e 65 78 74 50 61 67 65 29 20 20 20 20 20 , nextPage)
2adc0 20 20 2f 2a 20 28 35 29 20 2a 2f 0a 20 20 20 20 /* (5) */.
2add0 20 20 20 20 20 26 26 20 26 70 42 75 66 5b 2d 34 && &pBuf[-4
2ade0 5d 3e 3d 70 42 75 66 53 74 61 72 74 20 20 20 20 ]>=pBufStart
2adf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ae00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 28 36 /* (6
2ae10 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 29 7b 0a ) */. ){.
2ae20 20 20 20 20 20 20 20 20 20 20 75 38 20 61 53 61 u8 aSa
2ae30 76 65 5b 34 5d 3b 0a 20 20 20 20 20 20 20 20 20 ve[4];.
2ae40 20 75 38 20 2a 61 57 72 69 74 65 20 3d 20 26 70 u8 *aWrite = &p
2ae50 42 75 66 5b 2d 34 5d 3b 0a 20 20 20 20 20 20 20 Buf[-4];.
2ae60 20 20 20 61 73 73 65 72 74 28 20 61 57 72 69 74 assert( aWrit
2ae70 65 3e 3d 70 42 75 66 53 74 61 72 74 20 29 3b 20 e>=pBufStart );
2ae80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ae90 20 20 20 20 20 20 20 20 2f 2a 20 64 75 65 20 74 /* due t
2aea0 6f 20 28 36 29 20 2a 2f 0a 20 20 20 20 20 20 20 o (6) */.
2aeb0 20 20 20 6d 65 6d 63 70 79 28 61 53 61 76 65 2c memcpy(aSave,
2aec0 20 61 57 72 69 74 65 2c 20 34 29 3b 0a 20 20 20 aWrite, 4);.
2aed0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
2aee0 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 57 te3OsRead(fd, aW
2aef0 72 69 74 65 2c 20 61 2b 34 2c 20 28 69 36 34 29 rite, a+4, (i64)
2af00 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2a 28 6e pBt->pageSize*(n
2af10 65 78 74 50 61 67 65 2d 31 29 29 3b 0a 20 20 20 extPage-1));.
2af20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 nextPage
2af30 3d 20 67 65 74 34 62 79 74 65 28 61 57 72 69 74 = get4byte(aWrit
2af40 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 e);. me
2af50 6d 63 70 79 28 61 57 72 69 74 65 2c 20 61 53 61 mcpy(aWrite, aSa
2af60 76 65 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 ve, 4);.
2af70 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 }else.#endif..
2af80 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 {.
2af90 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
2afa0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 e;. rc
2afb0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
2afc0 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e t(pBt->pPager, n
2afd0 65 78 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 extPage, &pDbPag
2afe0 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
2aff0 20 28 65 4f 70 3d 3d 30 20 3f 20 50 41 47 45 52 (eOp==0 ? PAGER
2b000 5f 47 45 54 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 _GET_READONLY :
2b010 30 29 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 0). );.
2b020 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
2b030 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
2b040 20 20 20 20 20 20 20 20 20 20 20 61 50 61 79 6c aPayl
2b050 6f 61 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 oad = sqlite3Pag
2b060 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 erGetData(pDbPag
2b070 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
2b080 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 34 62 nextPage = get4b
2b090 79 74 65 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20 yte(aPayload);.
2b0a0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
2b0b0 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 copyPayload(&aPa
2b0c0 79 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c yload[offset+4],
2b0d0 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 pBuf, a, eOp, p
2b0e0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
2b0f0 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
2b100 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b rUnref(pDbPage);
2b110 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 66 . off
2b120 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 set = 0;.
2b130 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
2b140 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b amt -= a;
2b150 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 6d 74 . if( amt
2b160 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b ==0 ) return rc;
2b170 0a 20 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d . pBuf +=
2b180 20 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 a;. }.
2b190 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b if( rc ) break
2b1a0 3b 0a 20 20 20 20 20 20 69 49 64 78 2b 2b 3b 0a ;. iIdx++;.
2b1b0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
2b1c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
2b1d0 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 2f & amt>0 ){. /
2b1e0 2a 20 4f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e * Overflow chain
2b1f0 20 65 6e 64 73 20 70 72 65 6d 61 74 75 72 65 6c ends prematurel
2b200 79 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 y */. return
2b210 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
2b220 47 4e 4f 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 GNO(pPage->pgno)
2b230 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
2b240 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 c;.}../*.** Read
2b250 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 61 79 part of the pay
2b260 6c 6f 61 64 20 66 6f 72 20 74 68 65 20 72 6f 77 load for the row
2b270 20 61 74 20 77 68 69 63 68 20 74 68 61 74 20 63 at which that c
2b280 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 63 75 ursor pCur is cu
2b290 72 72 65 6e 74 6c 79 0a 2a 2a 20 70 6f 69 6e 74 rrently.** point
2b2a0 69 6e 67 2e 20 20 22 61 6d 74 22 20 62 79 74 65 ing. "amt" byte
2b2b0 73 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 s will be transf
2b2c0 65 72 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b erred into pBuf[
2b2d0 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 ]. The transfer
2b2e0 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f .** begins at "o
2b2f0 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 70 43 ffset"..**.** pC
2b300 75 72 20 63 61 6e 20 62 65 20 70 6f 69 6e 74 69 ur can be pointi
2b310 6e 67 20 74 6f 20 65 69 74 68 65 72 20 61 20 74 ng to either a t
2b320 61 62 6c 65 20 6f 72 20 61 6e 20 69 6e 64 65 78 able or an index
2b330 20 62 2d 74 72 65 65 2e 0a 2a 2a 20 49 66 20 70 b-tree..** If p
2b340 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 74 61 62 ointing to a tab
2b350 6c 65 20 62 74 72 65 65 2c 20 74 68 65 6e 20 74 le btree, then t
2b360 68 65 20 63 6f 6e 74 65 6e 74 20 73 65 63 74 69 he content secti
2b370 6f 6e 20 69 73 20 72 65 61 64 2e 20 20 49 66 0a on is read. If.
2b380 2a 2a 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 ** pCur is point
2b390 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 ing to an index
2b3a0 62 2d 74 72 65 65 20 74 68 65 6e 20 74 68 65 20 b-tree then the
2b3b0 6b 65 79 20 73 65 63 74 69 6f 6e 20 69 73 20 72 key section is r
2b3c0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 ead..**.** For s
2b3d0 71 6c 69 74 65 33 42 74 72 65 65 50 61 79 6c 6f qlite3BtreePaylo
2b3e0 61 64 28 29 2c 20 74 68 65 20 63 61 6c 6c 65 72 ad(), the caller
2b3f0 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 must ensure tha
2b400 74 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 t pCur is pointi
2b410 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 ng.** to a valid
2b420 20 72 6f 77 20 69 6e 20 74 68 65 20 74 61 62 6c row in the tabl
2b430 65 2e 20 20 46 6f 72 20 73 71 6c 69 74 65 33 42 e. For sqlite3B
2b440 74 72 65 65 50 61 79 6c 6f 61 64 43 68 65 63 6b treePayloadCheck
2b450 65 64 28 29 2c 20 74 68 65 0a 2a 2a 20 63 75 72 ed(), the.** cur
2b460 73 6f 72 20 6d 69 67 68 74 20 62 65 20 69 6e 76 sor might be inv
2b470 61 6c 69 64 20 6f 72 20 6d 69 67 68 74 20 6e 65 alid or might ne
2b480 65 64 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 ed to be restore
2b490 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72 d before being r
2b4a0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ead..**.** Retur
2b4b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
2b4c0 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 uccess or an err
2b4d0 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 or code if anyth
2b4e0 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e ing goes.** wron
2b4f0 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 g. An error is
2b500 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 returned if "off
2b510 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 set+amt" is larg
2b520 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 er than.** the a
2b530 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 vailable payload
2b540 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
2b550 42 74 72 65 65 50 61 79 6c 6f 61 64 28 42 74 43 BtreePayload(BtC
2b560 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 ursor *pCur, u32
2b570 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 offset, u32 amt
2b580 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 , void *pBuf){.
2b590 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
2b5a0 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
2b5b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
2b5c0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
2b5d0 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
2b5e0 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 ert( pCur->iPage
2b5f0 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 70 50 61 >=0 && pCur->pPa
2b600 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ge );. assert(
2b610 70 43 75 72 2d 3e 69 78 3c 70 43 75 72 2d 3e 70 pCur->ix<pCur->p
2b620 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
2b630 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 return accessPa
2b640 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 yload(pCur, offs
2b650 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e et, amt, (unsign
2b660 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 ed char*)pBuf, 0
2b670 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
2b680 20 76 61 72 69 61 6e 74 20 6f 66 20 73 71 6c 69 variant of sqli
2b690 74 65 33 42 74 72 65 65 50 61 79 6c 6f 61 64 28 te3BtreePayload(
2b6a0 29 20 77 6f 72 6b 73 20 65 76 65 6e 20 69 66 20 ) works even if
2b6b0 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 6e the cursor has n
2b6c0 6f 74 0a 2a 2a 20 69 6e 20 74 68 65 20 43 55 52 ot.** in the CUR
2b6d0 53 4f 52 5f 56 41 4c 49 44 20 73 74 61 74 65 2e SOR_VALID state.
2b6e0 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 It is only use
2b6f0 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 d by the sqlite3
2b700 5f 62 6c 6f 62 5f 72 65 61 64 28 29 0a 2a 2a 20 _blob_read().**
2b710 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 69 interface..*/.#i
2b720 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
2b730 54 5f 49 4e 43 52 42 4c 4f 42 0a 73 74 61 74 69 T_INCRBLOB.stati
2b740 63 20 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e c SQLITE_NOINLIN
2b750 45 20 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c E int accessPayl
2b760 6f 61 64 43 68 65 63 6b 65 64 28 0a 20 20 42 74 oadChecked(. Bt
2b770 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 0a 20 20 Cursor *pCur,.
2b780 75 33 32 20 6f 66 66 73 65 74 2c 0a 20 20 75 33 u32 offset,. u3
2b790 32 20 61 6d 74 2c 0a 20 20 76 6f 69 64 20 2a 70 2 amt,. void *p
2b7a0 42 75 66 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b Buf.){. int rc;
2b7b0 0a 20 20 69 66 20 28 20 70 43 75 72 2d 3e 65 53 . if ( pCur->eS
2b7c0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 tate==CURSOR_INV
2b7d0 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 ALID ){. retu
2b7e0 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b rn SQLITE_ABORT;
2b7f0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 . }. assert( c
2b800 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 ursorOwnsBtShare
2b810 64 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 d(pCur) );. rc
2b820 3d 20 62 74 72 65 65 52 65 73 74 6f 72 65 43 75 = btreeRestoreCu
2b830 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 rsorPosition(pCu
2b840 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 r);. return rc
2b850 3f 20 72 63 20 3a 20 61 63 63 65 73 73 50 61 79 ? rc : accessPay
2b860 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 load(pCur, offse
2b870 74 2c 20 61 6d 74 2c 20 70 42 75 66 2c 20 30 29 t, amt, pBuf, 0)
2b880 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 ;.}.int sqlite3B
2b890 74 72 65 65 50 61 79 6c 6f 61 64 43 68 65 63 6b treePayloadCheck
2b8a0 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ed(BtCursor *pCu
2b8b0 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 r, u32 offset, u
2b8c0 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 32 amt, void *pB
2b8d0 75 66 29 7b 0a 20 20 69 66 28 20 70 43 75 72 2d uf){. if( pCur-
2b8e0 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
2b8f0 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 61 73 73 VALID ){. ass
2b900 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 ert( cursorOwnsB
2b910 74 53 68 61 72 65 64 28 70 43 75 72 29 20 29 3b tShared(pCur) );
2b920 0a 20 20 20 20 72 65 74 75 72 6e 20 61 63 63 65 . return acce
2b930 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 ssPayload(pCur,
2b940 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 offset, amt, pBu
2b950 66 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a f, 0);. }else{.
2b960 20 20 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 return acces
2b970 73 50 61 79 6c 6f 61 64 43 68 65 63 6b 65 64 28 sPayloadChecked(
2b980 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
2b990 74 2c 20 70 42 75 66 29 3b 0a 20 20 7d 0a 7d 0a t, pBuf);. }.}.
2b9a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
2b9b0 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a _OMIT_INCRBLOB *
2b9c0 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
2b9d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 a pointer to pay
2b9e0 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e load information
2b9f0 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 from the entry
2ba00 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 that the .** pCu
2ba10 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e r cursor is poin
2ba20 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f ting to. The po
2ba30 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 inter is to the
2ba40 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 beginning of.**
2ba50 74 68 65 20 6b 65 79 20 69 66 20 69 6e 64 65 78 the key if index
2ba60 20 62 74 72 65 65 73 20 28 70 50 61 67 65 2d 3e btrees (pPage->
2ba70 69 6e 74 4b 65 79 3d 3d 30 29 20 61 6e 64 20 69 intKey==0) and i
2ba80 73 20 74 68 65 20 64 61 74 61 20 66 6f 72 0a 2a s the data for.*
2ba90 2a 20 74 61 62 6c 65 20 62 74 72 65 65 73 20 28 * table btrees (
2baa0 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d 3d 31 pPage->intKey==1
2bab0 29 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ). The number of
2bac0 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 bytes of availa
2bad0 62 6c 65 0a 2a 2a 20 6b 65 79 2f 64 61 74 61 20 ble.** key/data
2bae0 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
2baf0 2a 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 *pAmt. If *pAmt
2bb00 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 76 61 ==0, then the va
2bb10 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 lue.** returned
2bb20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 76 61 will not be a va
2bb30 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a lid pointer..**.
2bb40 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
2bb50 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 is an optimizati
2bb60 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f on. It is commo
2bb70 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 n for the entire
2bb80 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61 key.** and data
2bb90 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 6c to fit on the l
2bba0 6f 63 61 6c 20 70 61 67 65 20 61 6e 64 20 66 6f ocal page and fo
2bbb0 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 6e 6f r there to be no
2bbc0 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 overflow.** pag
2bbd0 65 73 2e 20 20 57 68 65 6e 20 74 68 61 74 20 69 es. When that i
2bbe0 73 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 74 69 s so, this routi
2bbf0 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 ne can be used t
2bc00 6f 20 61 63 63 65 73 73 20 74 68 65 0a 2a 2a 20 o access the.**
2bc10 6b 65 79 20 61 6e 64 20 64 61 74 61 20 77 69 74 key and data wit
2bc20 68 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f hout making a co
2bc30 70 79 2e 20 20 49 66 20 74 68 65 20 6b 65 79 20 py. If the key
2bc40 61 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 69 6c and/or data spil
2bc50 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 ls.** onto overf
2bc60 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 65 6e 20 low pages, then
2bc70 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 accessPayload()
2bc80 6d 75 73 74 20 62 65 20 75 73 65 64 20 74 6f 20 must be used to
2bc90 72 65 61 73 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 reassemble.** th
2bca0 65 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 20 63 e key/data and c
2bcb0 6f 70 79 20 69 74 20 69 6e 74 6f 20 61 20 70 72 opy it into a pr
2bcc0 65 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 eallocated buffe
2bcd0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 r..**.** The poi
2bce0 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 62 79 nter returned by
2bcf0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f this routine lo
2bd00 6f 6b 73 20 64 69 72 65 63 74 6c 79 20 69 6e 74 oks directly int
2bd10 6f 20 74 68 65 20 63 61 63 68 65 64 0a 2a 2a 20 o the cached.**
2bd20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
2bd30 62 61 73 65 2e 20 20 54 68 65 20 64 61 74 61 20 base. The data
2bd40 6d 69 67 68 74 20 63 68 61 6e 67 65 20 6f 72 20 might change or
2bd50 6d 6f 76 65 20 74 68 65 20 6e 65 78 74 20 74 69 move the next ti
2bd60 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 65 20 me.** any btree
2bd70 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
2bd80 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e d..*/.static con
2bd90 73 74 20 76 6f 69 64 20 2a 66 65 74 63 68 50 61 st void *fetchPa
2bda0 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f yload(. BtCurso
2bdb0 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a r *pCur, /*
2bdc0 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
2bdd0 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 to entry to rea
2bde0 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 d from */. u32
2bdf0 2a 70 41 6d 74 20 20 20 20 20 20 20 20 20 20 20 *pAmt
2be00 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 /* Write the nu
2be10 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
2be20 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a e bytes here */.
2be30 29 7b 0a 20 20 75 33 32 20 61 6d 74 3b 0a 20 20 ){. u32 amt;.
2be40 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 assert( pCur!=0
2be50 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d && pCur->iPage>=
2be60 30 20 26 26 20 70 43 75 72 2d 3e 70 50 61 67 65 0 && pCur->pPage
2be70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
2be80 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
2be90 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
2bea0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
2beb0 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
2bec0 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
2bed0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 );. assert( cu
2bee0 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 64 rsorOwnsBtShared
2bef0 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
2bf00 72 74 28 20 70 43 75 72 2d 3e 69 78 3c 70 43 75 rt( pCur->ix<pCu
2bf10 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 r->pPage->nCell
2bf20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
2bf30 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3e 30 20 r->info.nSize>0
2bf40 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
2bf50 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 64 r->info.pPayload
2bf60 3e 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 61 44 >pCur->pPage->aD
2bf70 61 74 61 20 7c 7c 20 43 4f 52 52 55 50 54 5f 44 ata || CORRUPT_D
2bf80 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 B );. assert( p
2bf90 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f Cur->info.pPaylo
2bfa0 61 64 3c 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e ad<pCur->pPage->
2bfb0 61 44 61 74 61 45 6e 64 20 7c 7c 43 4f 52 52 55 aDataEnd ||CORRU
2bfc0 50 54 5f 44 42 29 3b 0a 20 20 61 6d 74 20 3d 20 PT_DB);. amt =
2bfd0 28 69 6e 74 29 28 70 43 75 72 2d 3e 70 50 61 67 (int)(pCur->pPag
2bfe0 65 2d 3e 61 44 61 74 61 45 6e 64 20 2d 20 70 43 e->aDataEnd - pC
2bff0 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 ur->info.pPayloa
2c000 64 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e d);. if( pCur->
2c010 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3c 61 6d 74 20 info.nLocal<amt
2c020 29 20 61 6d 74 20 3d 20 70 43 75 72 2d 3e 69 6e ) amt = pCur->in
2c030 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 2a 70 41 fo.nLocal;. *pA
2c040 6d 74 20 3d 20 61 6d 74 3b 0a 20 20 72 65 74 75 mt = amt;. retu
2c050 72 6e 20 28 76 6f 69 64 2a 29 70 43 75 72 2d 3e rn (void*)pCur->
2c060 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 64 3b 0a 7d info.pPayload;.}
2c070 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 .../*.** For the
2c080 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 entry that curs
2c090 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 or pCur is point
2c0a0 20 74 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a to, return as.*
2c0b0 2a 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20 * many bytes of
2c0c0 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 the key or data
2c0d0 61 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 as are available
2c0e0 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a on the local.**
2c0f0 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 57 b-tree page. W
2c100 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 rite the number
2c110 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74 of available byt
2c120 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a es into *pAmt..*
2c130 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 *.** The pointer
2c140 20 72 65 74 75 72 6e 65 64 20 69 73 20 65 70 68 returned is eph
2c150 65 6d 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 79 emeral. The key
2c160 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a /data may move.*
2c170 2a 20 6f 72 20 62 65 20 64 65 73 74 72 6f 79 65 * or be destroye
2c180 64 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 d on the next ca
2c190 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72 65 65 20 ll to any Btree
2c1a0 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c routine,.** incl
2c1b0 75 64 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d uding calls from
2c1c0 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 other threads a
2c1d0 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20 gainst the same
2c1e0 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c cache..** Hence,
2c1f0 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 a mutex on the
2c200 42 74 53 68 61 72 65 64 20 73 68 6f 75 6c 64 20 BtShared should
2c210 62 65 20 68 65 6c 64 20 70 72 69 6f 72 20 74 6f be held prior to
2c220 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 calling.** this
2c230 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
2c240 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 These routines i
2c250 73 20 75 73 65 64 20 74 6f 20 67 65 74 20 71 75 s used to get qu
2c260 69 63 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65 ick access to ke
2c270 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e y and data.** in
2c280 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 the common case
2c290 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c where no overfl
2c2a0 6f 77 20 70 61 67 65 73 20 61 72 65 20 75 73 65 ow pages are use
2c2b0 64 2e 0a 2a 2f 0a 63 6f 6e 73 74 20 76 6f 69 64 d..*/.const void
2c2c0 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 *sqlite3BtreePa
2c2d0 79 6c 6f 61 64 46 65 74 63 68 28 42 74 43 75 72 yloadFetch(BtCur
2c2e0 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a sor *pCur, u32 *
2c2f0 70 41 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 pAmt){. return
2c300 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 fetchPayload(pCu
2c310 72 2c 20 70 41 6d 74 29 3b 0a 7d 0a 0a 0a 2f 2a r, pAmt);.}.../*
2c320 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
2c330 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 sor down to a ne
2c340 77 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 w child page. T
2c350 68 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d he newPgno argum
2c360 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 ent is the.** pa
2c370 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
2c380 20 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d child page to m
2c390 6f 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 ove to..**.** Th
2c3a0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
2c3b0 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 rns SQLITE_CORRU
2c3c0 50 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 PT if the page-h
2c3d0 65 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c eader flags fiel
2c3e0 64 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 d of.** the new
2c3f0 63 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 child page does
2c400 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c not match the fl
2c410 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ags field of the
2c420 20 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a parent (i.e..**
2c430 20 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 if an intkey pa
2c440 67 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 ge appears to be
2c450 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 the parent of a
2c460 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 non-intkey page
2c470 2c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 , or.** vice-ver
2c480 73 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 sa)..*/.static i
2c490 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 nt moveToChild(B
2c4a0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
2c4b0 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 42 32 newPgno){. B
2c4c0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
2c4d0 43 75 72 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 Cur->pBt;. int
2c4e0 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 rc;.. assert( c
2c4f0 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 ursorOwnsBtShare
2c500 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 d(pCur) );. ass
2c510 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
2c520 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
2c530 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
2c540 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 4f r->iPage<BTCURSO
2c550 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 R_MAX_DEPTH );.
2c560 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
2c570 50 61 67 65 3e 3d 30 20 29 3b 0a 20 20 69 66 28 Page>=0 );. if(
2c580 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 pCur->iPage>=(B
2c590 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 TCURSOR_MAX_DEPT
2c5a0 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 H-1) ){. retu
2c5b0 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
2c5c0 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 70 43 T_BKPT;. }. pC
2c5d0 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
2c5e0 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 72 46 0;. pCur->curF
2c5f0 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 5f 56 lags &= ~(BTCF_V
2c600 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 alidNKey|BTCF_Va
2c610 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 70 43 75 72 lidOvfl);. pCur
2c620 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
2c630 61 67 65 5d 20 3d 20 70 43 75 72 2d 3e 69 78 3b age] = pCur->ix;
2c640 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b . pCur->apPage[
2c650 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 pCur->iPage] = p
2c660 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 70 43 Cur->pPage;. pC
2c670 75 72 2d 3e 69 78 20 3d 20 30 3b 0a 20 20 70 43 ur->ix = 0;. pC
2c680 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 20 20 72 ur->iPage++;. r
2c690 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 c = getAndInitPa
2c6a0 67 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c ge(pBt, newPgno,
2c6b0 20 26 70 43 75 72 2d 3e 70 50 61 67 65 2c 0a 20 &pCur->pPage,.
2c6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c6d0 20 20 20 20 20 20 20 70 43 75 72 2c 20 70 43 75 pCur, pCu
2c6e0 72 2d 3e 63 75 72 50 61 67 65 72 46 6c 61 67 73 r->curPagerFlags
2c6f0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
2c700 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 65 ITE_OK ){. se
2c710 74 4d 65 6d 70 61 67 65 52 6f 6f 74 28 70 43 75 tMempageRoot(pCu
2c720 72 2d 3e 70 50 61 67 65 2c 20 70 43 75 72 2d 3e r->pPage, pCur->
2c730 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 7d 0a 20 pgnoRoot);. }.
2c740 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
2c750 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
2c760 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50 UG./*.** Page pP
2c770 61 72 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 arent is an inte
2c780 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 rnal (non-leaf)
2c790 74 72 65 65 20 70 61 67 65 2e 20 54 68 69 73 20 tree page. This
2c7a0 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 function .** ass
2c7b0 65 72 74 73 20 74 68 61 74 20 70 61 67 65 20 6e erts that page n
2c7c0 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 umber iChild is
2c7d0 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64 20 69 the left-child i
2c7e0 66 20 74 68 65 20 69 49 64 78 27 74 68 0a 2a 2a f the iIdx'th.**
2c7f0 20 63 65 6c 6c 20 69 6e 20 70 61 67 65 20 70 50 cell in page pP
2c800 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49 arent. Or, if iI
2c810 64 78 20 69 73 20 65 71 75 61 6c 20 74 6f 20 74 dx is equal to t
2c820 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
2c830 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70 of.** cells in p
2c840 50 61 72 65 6e 74 2c 20 74 68 61 74 20 70 61 67 Parent, that pag
2c850 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 e number iChild
2c860 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 is the right-chi
2c870 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67 ld of.** the pag
2c880 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
2c890 64 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e d assertParentIn
2c8a0 64 65 78 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 dex(MemPage *pPa
2c8b0 72 65 6e 74 2c 20 69 6e 74 20 69 49 64 78 2c 20 rent, int iIdx,
2c8c0 50 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20 Pgno iChild){.
2c8d0 69 66 28 20 43 4f 52 52 55 50 54 5f 44 42 20 29 if( CORRUPT_DB )
2c8e0 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 65 return; /* The
2c8f0 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 65 73 74 conditions test
2c900 65 64 20 62 65 6c 6f 77 20 6d 69 67 68 74 20 6e ed below might n
2c910 6f 74 20 62 65 20 74 72 75 65 0a 20 20 20 20 20 ot be true.
2c920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c930 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 61 20 63 ** in a c
2c940 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 orrupt database
2c950 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 */. assert( iId
2c960 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c x<=pParent->nCel
2c970 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d l );. if( iIdx=
2c980 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 =pParent->nCell
2c990 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 ){. assert( g
2c9a0 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 et4byte(&pParent
2c9b0 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
2c9c0 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d >hdrOffset+8])==
2c9d0 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 iChild );. }els
2c9e0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 e{. assert( g
2c9f0 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c et4byte(findCell
2ca00 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 (pParent, iIdx))
2ca10 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a ==iChild );. }.
2ca20 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e }.#else.# defin
2ca30 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e e assertParentIn
2ca40 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 dex(x,y,z) .#end
2ca50 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 if../*.** Move t
2ca60 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 he cursor up to
2ca70 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
2ca80 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 .**.** pCur->idx
2ca90 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 is set to the c
2caa0 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 ell index that c
2cab0 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e ontains the poin
2cac0 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 ter.** to the pa
2cad0 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 ge we are coming
2cae0 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 from. If we ar
2caf0 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 e coming from th
2cb00 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 e.** right-most
2cb10 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 child page then
2cb20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 pCur->idx is set
2cb30 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 to one more tha
2cb40 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 n.** the largest
2cb50 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a cell index..*/.
2cb60 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 65 static void move
2cb70 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f ToParent(BtCurso
2cb80 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 r *pCur){. MemP
2cb90 61 67 65 20 2a 70 4c 65 61 66 3b 0a 20 20 61 73 age *pLeaf;. as
2cba0 73 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 sert( cursorOwns
2cbb0 42 74 53 68 61 72 65 64 28 70 43 75 72 29 20 29 BtShared(pCur) )
2cbc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
2cbd0 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
2cbe0 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
2cbf0 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e rt( pCur->iPage>
2cc00 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
2cc10 43 75 72 2d 3e 70 50 61 67 65 20 29 3b 0a 20 20 Cur->pPage );.
2cc20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 assertParentInde
2cc30 78 28 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 x(. pCur->apP
2cc40 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d age[pCur->iPage-
2cc50 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 1], . pCur->a
2cc60 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
2cc70 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e -1], . pCur->
2cc80 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29 3b pPage->pgno. );
2cc90 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 43 75 . testcase( pCu
2cca0 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
2ccb0 50 61 67 65 2d 31 5d 20 3e 20 70 43 75 72 2d 3e Page-1] > pCur->
2ccc0 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
2ccd0 67 65 2d 31 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a ge-1]->nCell );.
2cce0 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
2ccf0 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
2cd00 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 curFlags &= ~(BT
2cd10 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 CF_ValidNKey|BTC
2cd20 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 F_ValidOvfl);.
2cd30 70 43 75 72 2d 3e 69 78 20 3d 20 70 43 75 72 2d pCur->ix = pCur-
2cd40 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
2cd50 67 65 2d 31 5d 3b 0a 20 20 70 4c 65 61 66 20 3d ge-1];. pLeaf =
2cd60 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 pCur->pPage;.
2cd70 70 43 75 72 2d 3e 70 50 61 67 65 20 3d 20 70 43 pCur->pPage = pC
2cd80 75 72 2d 3e 61 70 50 61 67 65 5b 2d 2d 70 43 75 ur->apPage[--pCu
2cd90 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 72 65 6c r->iPage];. rel
2cda0 65 61 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 easePageNotNull(
2cdb0 70 4c 65 61 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pLeaf);.}../*.**
2cdc0 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
2cdd0 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
2cde0 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 74 root page of it
2cdf0 73 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 s b-tree structu
2ce00 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 re..**.** If the
2ce10 20 74 61 62 6c 65 20 68 61 73 20 61 20 76 69 72 table has a vir
2ce20 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 tual root page,
2ce30 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
2ce40 69 73 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e is moved to poin
2ce50 74 0a 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 74 t.** to the virt
2ce60 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e ual root page in
2ce70 73 74 65 61 64 20 6f 66 20 74 68 65 20 61 63 74 stead of the act
2ce80 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 ual root page. A
2ce90 20 74 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 table has a.**
2cea0 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 virtual root pag
2ceb0 65 20 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 e when the actua
2cec0 6c 20 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 l root page cont
2ced0 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e ains no cells an
2cee0 64 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 d a .** single c
2cef0 68 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 hild page. This
2cf00 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 can only happen
2cf10 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 with the table r
2cf20 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 2e ooted at page 1.
2cf30 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d .**.** If the b-
2cf40 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 69 tree structure i
2cf50 73 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75 72 s empty, the cur
2cf60 73 6f 72 20 73 74 61 74 65 20 69 73 20 73 65 74 sor state is set
2cf70 20 74 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 to .** CURSOR_I
2cf80 4e 56 41 4c 49 44 20 61 6e 64 20 74 68 69 73 20 NVALID and this
2cf90 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
2cfa0 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 4f 74 SQLITE_EMPTY. Ot
2cfb0 68 65 72 77 69 73 65 2c 0a 2a 2a 20 74 68 65 20 herwise,.** the
2cfc0 63 75 72 73 6f 72 20 69 73 20 73 65 74 20 74 6f cursor is set to
2cfd0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 point to the fi
2cfe0 72 73 74 20 63 65 6c 6c 20 6c 6f 63 61 74 65 64 rst cell located
2cff0 20 6f 6e 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 on the root.**
2d000 28 6f 72 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 (or virtual root
2d010 29 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 63 ) page and the c
2d020 75 72 73 6f 72 20 73 74 61 74 65 20 69 73 20 73 ursor state is s
2d030 65 74 20 74 6f 20 43 55 52 53 4f 52 5f 56 41 4c et to CURSOR_VAL
2d040 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 ID..**.** If thi
2d050 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
2d060 6e 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ns successfully,
2d070 20 69 74 20 6d 61 79 20 62 65 20 61 73 73 75 6d it may be assum
2d080 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 ed that the.** p
2d090 61 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 age-header flags
2d0a0 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 indicate that t
2d0b0 68 65 20 5b 76 69 72 74 75 61 6c 5d 20 72 6f 6f he [virtual] roo
2d0c0 74 2d 70 61 67 65 20 69 73 20 74 68 65 20 65 78 t-page is the ex
2d0d0 70 65 63 74 65 64 20 0a 2a 2a 20 6b 69 6e 64 20 pected .** kind
2d0e0 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 20 28 of b-tree page (
2d0f0 69 2e 65 2e 20 69 66 20 77 68 65 6e 20 6f 70 65 i.e. if when ope
2d100 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 ning the cursor
2d110 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20 6e the caller did n
2d120 6f 74 0a 2a 2a 20 73 70 65 63 69 66 79 20 61 20 ot.** specify a
2d130 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
2d140 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 e the flags byte
2d150 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 35 20 is set to 0x05
2d160 6f 72 20 30 78 30 44 2c 0a 2a 2a 20 69 6e 64 69 or 0x0D,.** indi
2d170 63 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 62 cating a table b
2d180 2d 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 -tree, or if the
2d190 20 63 61 6c 6c 65 72 20 64 69 64 20 73 70 65 63 caller did spec
2d1a0 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a ify a KeyInfo .*
2d1b0 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 * structure the
2d1c0 66 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 flags byte is se
2d1d0 74 20 74 6f 20 30 78 30 32 20 6f 72 20 30 78 30 t to 0x02 or 0x0
2d1e0 41 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e A, indicating an
2d1f0 20 69 6e 64 65 78 0a 2a 2a 20 62 2d 74 72 65 65 index.** b-tree
2d200 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
2d210 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 moveToRoot(BtCu
2d220 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d rsor *pCur){. M
2d230 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 emPage *pRoot;.
2d240 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
2d250 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _OK;.. assert(
2d260 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 cursorOwnsBtShar
2d270 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ed(pCur) );. as
2d280 73 65 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 sert( CURSOR_INV
2d290 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 ALID < CURSOR_RE
2d2a0 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 QUIRESEEK );. a
2d2b0 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 ssert( CURSOR_VA
2d2c0 4c 49 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 LID < CURSOR_R
2d2d0 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 EQUIRESEEK );.
2d2e0 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 assert( CURSOR_F
2d2f0 41 55 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f AULT > CURSOR_
2d300 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 REQUIRESEEK );.
2d310 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
2d320 53 74 61 74 65 20 3c 20 43 55 52 53 4f 52 5f 52 State < CURSOR_R
2d330 45 51 55 49 52 45 53 45 45 4b 20 7c 7c 20 70 43 EQUIRESEEK || pC
2d340 75 72 2d 3e 69 50 61 67 65 3c 30 20 29 3b 0a 20 ur->iPage<0 );.
2d350 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 assert( pCur->p
2d360 67 6e 6f 52 6f 6f 74 3e 30 20 7c 7c 20 70 43 75 gnoRoot>0 || pCu
2d370 72 2d 3e 69 50 61 67 65 3c 30 20 29 3b 0a 0a 20 r->iPage<0 );..
2d380 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
2d390 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 >=0 ){. if( p
2d3a0 43 75 72 2d 3e 69 50 61 67 65 20 29 7b 0a 20 20 Cur->iPage ){.
2d3b0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 4e releasePageN
2d3c0 6f 74 4e 75 6c 6c 28 70 43 75 72 2d 3e 70 50 61 otNull(pCur->pPa
2d3d0 67 65 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 ge);. while
2d3e0 28 20 2d 2d 70 43 75 72 2d 3e 69 50 61 67 65 20 ( --pCur->iPage
2d3f0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 ){. relea
2d400 73 65 50 61 67 65 4e 6f 74 4e 75 6c 6c 28 70 43 sePageNotNull(pC
2d410 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
2d420 3e 69 50 61 67 65 5d 29 3b 0a 20 20 20 20 20 20 >iPage]);.
2d430 7d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 }. pCur->pP
2d440 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
2d450 67 65 5b 30 5d 3b 0a 20 20 20 20 20 20 67 6f 74 ge[0];. got
2d460 6f 20 73 6b 69 70 5f 69 6e 69 74 3b 0a 20 20 20 o skip_init;.
2d470 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }. }else if( p
2d480 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 Cur->pgnoRoot==0
2d490 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 ){. pCur->eS
2d4a0 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
2d4b0 56 41 4c 49 44 3b 0a 20 20 20 20 72 65 74 75 72 VALID;. retur
2d4c0 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a n SQLITE_EMPTY;.
2d4d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
2d4e0 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 ert( pCur->iPage
2d4f0 3d 3d 28 2d 31 29 20 29 3b 0a 20 20 20 20 69 66 ==(-1) );. if
2d500 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d ( pCur->eState>=
2d510 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
2d520 45 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 EK ){. if(
2d530 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
2d540 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 RSOR_FAULT ){.
2d550 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
2d560 75 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 ur->skipNext!=SQ
2d570 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
2d580 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e return pCur->
2d590 73 6b 69 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 skipNext;.
2d5a0 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
2d5b0 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
2d5c0 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pCur);. }.
2d5d0 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
2d5e0 50 61 67 65 28 70 43 75 72 2d 3e 70 42 74 72 65 Page(pCur->pBtre
2d5f0 65 2d 3e 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 e->pBt, pCur->pg
2d600 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 70 noRoot, &pCur->p
2d610 50 61 67 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Page,.
2d620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0,
2d630 20 70 43 75 72 2d 3e 63 75 72 50 61 67 65 72 46 pCur->curPagerF
2d640 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 lags);. if( r
2d650 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
2d660 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 pCur->eSta
2d670 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
2d680 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 LID;. retur
2d690 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
2d6a0 73 65 74 4d 65 6d 70 61 67 65 52 6f 6f 74 28 70 setMempageRoot(p
2d6b0 43 75 72 2d 3e 70 50 61 67 65 2c 20 70 43 75 72 Cur->pPage, pCur
2d6c0 2d 3e 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 ->pgnoRoot);.
2d6d0 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 pCur->iPage = 0
2d6e0 3b 0a 20 20 20 20 70 43 75 72 2d 3e 63 75 72 49 ;. pCur->curI
2d6f0 6e 74 4b 65 79 20 3d 20 70 43 75 72 2d 3e 70 50 ntKey = pCur->pP
2d700 61 67 65 2d 3e 69 6e 74 4b 65 79 3b 0a 20 20 7d age->intKey;. }
2d710 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d . pRoot = pCur-
2d720 3e 70 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 >pPage;. assert
2d730 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 ( pRoot->pgno==p
2d740 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b Cur->pgnoRoot );
2d750 0a 0a 20 20 2f 2a 20 49 66 20 70 43 75 72 2d 3e .. /* If pCur->
2d760 70 4b 65 79 49 6e 66 6f 20 69 73 20 6e 6f 74 20 pKeyInfo is not
2d770 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 NULL, then the c
2d780 61 6c 6c 65 72 20 74 68 61 74 20 6f 70 65 6e 65 aller that opene
2d790 64 20 74 68 69 73 20 63 75 72 73 6f 72 0a 20 20 d this cursor.
2d7a0 2a 2a 20 65 78 70 65 63 74 65 64 20 74 6f 20 6f ** expected to o
2d7b0 70 65 6e 20 69 74 20 6f 6e 20 61 6e 20 69 6e 64 pen it on an ind
2d7c0 65 78 20 62 2d 74 72 65 65 2e 20 4f 74 68 65 72 ex b-tree. Other
2d7d0 77 69 73 65 2c 20 69 66 20 70 4b 65 79 49 6e 66 wise, if pKeyInf
2d7e0 6f 20 69 73 0a 20 20 2a 2a 20 4e 55 4c 4c 2c 20 o is. ** NULL,
2d7f0 74 68 65 20 63 61 6c 6c 65 72 20 65 78 70 65 63 the caller expec
2d800 74 73 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 ts a table b-tre
2d810 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f e. If this is no
2d820 74 20 74 68 65 20 63 61 73 65 2c 0a 20 20 2a 2a t the case,. **
2d830 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 return an SQLIT
2d840 45 5f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e E_CORRUPT error.
2d850 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 72 6c . **. ** Earl
2d860 69 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ier versions of
2d870 53 51 4c 69 74 65 20 61 73 73 75 6d 65 64 20 74 SQLite assumed t
2d880 68 61 74 20 74 68 69 73 20 74 65 73 74 20 63 6f hat this test co
2d890 75 6c 64 20 6e 6f 74 20 66 61 69 6c 0a 20 20 2a uld not fail. *
2d8a0 2a 20 69 66 20 74 68 65 20 72 6f 6f 74 20 70 61 * if the root pa
2d8b0 67 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 6c ge was already l
2d8c0 6f 61 64 65 64 20 77 68 65 6e 20 74 68 69 73 20 oaded when this
2d8d0 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c function was cal
2d8e0 6c 65 64 20 28 69 2e 65 2e 0a 20 20 2a 2a 20 69 led (i.e.. ** i
2d8f0 66 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 f pCur->iPage>=0
2d900 29 2e 20 42 75 74 20 74 68 69 73 20 69 73 20 6e ). But this is n
2d910 6f 74 20 73 6f 20 69 66 20 74 68 65 20 64 61 74 ot so if the dat
2d920 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 abase is corrupt
2d930 65 64 20 0a 20 20 2a 2a 20 69 6e 20 73 75 63 68 ed . ** in such
2d940 20 61 20 77 61 79 20 74 68 61 74 20 70 61 67 65 a way that page
2d950 20 70 52 6f 6f 74 20 69 73 20 6c 69 6e 6b 65 64 pRoot is linked
2d960 20 69 6e 74 6f 20 61 20 73 65 63 6f 6e 64 20 62 into a second b
2d970 2d 74 72 65 65 20 74 61 62 6c 65 20 0a 20 20 2a -tree table . *
2d980 2a 20 28 6f 72 20 74 68 65 20 66 72 65 65 6c 69 * (or the freeli
2d990 73 74 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 st). */. asser
2d9a0 74 28 20 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 t( pRoot->intKey
2d9b0 3d 3d 31 20 7c 7c 20 70 52 6f 6f 74 2d 3e 69 6e ==1 || pRoot->in
2d9c0 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 tKey==0 );. if(
2d9d0 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 3d 3d pRoot->isInit==
2d9e0 30 20 7c 7c 20 28 70 43 75 72 2d 3e 70 4b 65 79 0 || (pCur->pKey
2d9f0 49 6e 66 6f 3d 3d 30 29 21 3d 70 52 6f 6f 74 2d Info==0)!=pRoot-
2da00 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 >intKey ){. r
2da10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
2da20 52 55 50 54 5f 50 47 4e 4f 28 70 43 75 72 2d 3e RUPT_PGNO(pCur->
2da30 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pPage->pgno);.
2da40 7d 0a 0a 73 6b 69 70 5f 69 6e 69 74 3a 20 20 0a }..skip_init: .
2da50 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 30 3b 0a pCur->ix = 0;.
2da60 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
2da70 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
2da80 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 curFlags &= ~(BT
2da90 43 46 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f 56 CF_AtLast|BTCF_V
2daa0 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 alidNKey|BTCF_Va
2dab0 6c 69 64 4f 76 66 6c 29 3b 0a 0a 20 20 70 52 6f lidOvfl);.. pRo
2dac0 6f 74 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 ot = pCur->pPage
2dad0 3b 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e ;. if( pRoot->n
2dae0 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 70 43 Cell>0 ){. pC
2daf0 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
2db00 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 7d 65 6c SOR_VALID;. }el
2db10 73 65 20 69 66 28 20 21 70 52 6f 6f 74 2d 3e 6c se if( !pRoot->l
2db20 65 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 eaf ){. Pgno
2db30 73 75 62 70 61 67 65 3b 0a 20 20 20 20 69 66 28 subpage;. if(
2db40 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20 pRoot->pgno!=1
2db50 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
2db60 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
2db70 20 20 73 75 62 70 61 67 65 20 3d 20 67 65 74 34 subpage = get4
2db80 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 byte(&pRoot->aDa
2db90 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 ta[pRoot->hdrOff
2dba0 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 set+8]);. pCu
2dbb0 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
2dbc0 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 OR_VALID;. rc
2dbd0 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 = moveToChild(p
2dbe0 43 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 Cur, subpage);.
2dbf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 }else{. pCur
2dc00 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
2dc10 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 72 R_INVALID;. r
2dc20 63 20 3d 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 c = SQLITE_EMPTY
2dc30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
2dc40 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 c;.}../*.** Move
2dc50 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e the cursor down
2dc60 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 to the left-mos
2dc70 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e t leaf entry ben
2dc80 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 eath the.** entr
2dc90 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 y to which it is
2dca0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
2dcb0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c ing..**.** The l
2dcc0 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 eft-most leaf is
2dcd0 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 the one with th
2dce0 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d e smallest key -
2dcf0 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e the first.** in
2dd00 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
2dd10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
2dd20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 moveToLeftmost(B
2dd30 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
2dd40 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 Pgno pgno;. i
2dd50 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
2dd60 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 K;. MemPage *pP
2dd70 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 age;.. assert(
2dd80 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 cursorOwnsBtShar
2dd90 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ed(pCur) );. as
2dda0 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
2ddb0 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
2ddc0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d );. while( rc=
2ddd0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 =SQLITE_OK && !(
2dde0 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 pPage = pCur->pP
2ddf0 61 67 65 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 age)->leaf ){.
2de00 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
2de10 69 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 ix<pPage->nCell
2de20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 );. pgno = ge
2de30 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
2de40 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 78 29 pPage, pCur->ix)
2de50 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 );. rc = move
2de60 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 ToChild(pCur, pg
2de70 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 no);. }. retur
2de80 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
2de90 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 ove the cursor d
2dea0 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74 own to the right
2deb0 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 -most leaf entry
2dec0 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 beneath the.**
2ded0 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 74 page to which it
2dee0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
2def0 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 inting. Notice
2df00 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a the difference.*
2df10 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f * between moveTo
2df20 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d Leftmost() and m
2df30 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 oveToRightmost()
2df40 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 . moveToLeftmos
2df50 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 t().** finds the
2df60 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 left-most entry
2df70 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e beneath the *en
2df80 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 try* whereas mov
2df90 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a eToRightmost().*
2dfa0 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 68 * finds the righ
2dfb0 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e t-most entry ben
2dfc0 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e eath the *page*.
2dfd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 .**.** The right
2dfe0 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74 -most entry is t
2dff0 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 he one with the
2e000 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68 largest key - th
2e010 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e e last.** key in
2e020 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
2e030 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
2e040 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 moveToRightmost(
2e050 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
2e060 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 . Pgno pgno;.
2e070 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
2e080 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 OK;. MemPage *p
2e090 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 Page = 0;.. ass
2e0a0 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 ert( cursorOwnsB
2e0b0 74 53 68 61 72 65 64 28 70 43 75 72 29 20 29 3b tShared(pCur) );
2e0c0 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
2e0d0 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
2e0e0 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 VALID );. while
2e0f0 28 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 ( !(pPage = pCur
2e100 2d 3e 70 50 61 67 65 29 2d 3e 6c 65 61 66 20 29 ->pPage)->leaf )
2e110 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 {. pgno = get
2e120 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
2e130 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
2e140 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 fset+8]);. pC
2e150 75 72 2d 3e 69 78 20 3d 20 70 50 61 67 65 2d 3e ur->ix = pPage->
2e160 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 nCell;. rc =
2e170 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
2e180 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 , pgno);. if(
2e190 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
2e1a0 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 69 78 20 . }. pCur->ix
2e1b0 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 = pPage->nCell-1
2e1c0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
2e1d0 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 ->info.nSize==0
2e1e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 43 );. assert( (pC
2e1f0 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 ur->curFlags & B
2e200 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 29 3d 3d TCF_ValidNKey)==
2e210 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0 );. return SQ
2e220 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d LITE_OK;.}../* M
2e230 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 ove the cursor t
2e240 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 o the first entr
2e250 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 y in the table.
2e260 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
2e270 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e K.** on success.
2e280 20 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 Set *pRes to 0
2e290 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 if the cursor a
2e2a0 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 ctually points t
2e2b0 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f o something.** o
2e2c0 72 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 r set *pRes to 1
2e2d0 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 if the table is
2e2e0 20 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 empty..*/.int s
2e2f0 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 qlite3BtreeFirst
2e300 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
2e310 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 int *pRes){. i
2e320 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
2e330 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 ( cursorOwnsBtSh
2e340 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 ared(pCur) );.
2e350 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
2e360 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
2e370 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
2e380 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f ex) );. rc = mo
2e390 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a veToRoot(pCur);.
2e3a0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
2e3b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
2e3c0 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e t( pCur->pPage->
2e3d0 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 2a nCell>0 );. *
2e3e0 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 63 pRes = 0;. rc
2e3f0 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 = moveToLeftmos
2e400 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 t(pCur);. }else
2e410 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2e420 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 61 73 73 EMPTY ){. ass
2e430 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 ert( pCur->pgnoR
2e440 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e oot==0 || pCur->
2e450 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 pPage->nCell==0
2e460 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 );. *pRes = 1
2e470 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
2e480 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 E_OK;. }. retu
2e490 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 rn rc;.}../* Mov
2e4a0 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
2e4b0 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
2e4c0 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 n the table. Re
2e4d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a turn SQLITE_OK.*
2e4e0 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 * on success. S
2e4f0 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 et *pRes to 0 if
2e500 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 the cursor actu
2e510 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 ally points to s
2e520 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 omething.** or s
2e530 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 et *pRes to 1 if
2e540 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
2e550 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 pty..*/.int sqli
2e560 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 te3BtreeLast(BtC
2e570 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
2e580 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 *pRes){. int r
2e590 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 c;. . assert( c
2e5a0 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 ursorOwnsBtShare
2e5b0 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 d(pCur) );. ass
2e5c0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
2e5d0 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
2e5e0 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
2e5f0 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
2e600 20 63 75 72 73 6f 72 20 61 6c 72 65 61 64 79 20 cursor already
2e610 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 points to the la
2e620 73 74 20 65 6e 74 72 79 2c 20 74 68 69 73 20 69 st entry, this i
2e630 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 s a no-op. */.
2e640 69 66 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 if( CURSOR_VALID
2e650 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 26 ==pCur->eState &
2e660 26 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 & (pCur->curFlag
2e670 73 20 26 20 42 54 43 46 5f 41 74 4c 61 73 74 29 s & BTCF_AtLast)
2e680 21 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 !=0 ){.#ifdef SQ
2e690 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f LITE_DEBUG. /
2e6a0 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 72 * This block ser
2e6b0 76 65 73 20 74 6f 20 61 73 73 65 72 74 28 29 20 ves to assert()
2e6c0 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
2e6d0 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f 69 6e really does poin
2e6e0 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 t . ** to the
2e6f0 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 last entry in t
2e700 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a 20 20 he b-tree. */.
2e710 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f int ii;. fo
2e720 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72 2d r(ii=0; ii<pCur-
2e730 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 >iPage; ii++){.
2e740 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
2e750 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d 70 43 r->aiIdx[ii]==pC
2e760 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d 2d 3e ur->apPage[ii]->
2e770 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 nCell );. }.
2e780 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
2e790 3e 69 78 3d 3d 70 43 75 72 2d 3e 70 50 61 67 65 >ix==pCur->pPage
2e7a0 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 20 ->nCell-1 );.
2e7b0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 assert( pCur->p
2e7c0 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 Page->leaf );.#e
2e7d0 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 ndif. return
2e7e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
2e7f0 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
2e800 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
2e810 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
2e820 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
2e830 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
2e840 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 2a 70 _VALID );. *p
2e850 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 Res = 0;. rc
2e860 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 = moveToRightmos
2e870 74 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 t(pCur);. if(
2e880 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
2e890 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 63 75 {. pCur->cu
2e8a0 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 46 5f 41 rFlags |= BTCF_A
2e8b0 74 4c 61 73 74 3b 0a 20 20 20 20 7d 65 6c 73 65 tLast;. }else
2e8c0 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 63 75 {. pCur->cu
2e8d0 72 46 6c 61 67 73 20 26 3d 20 7e 42 54 43 46 5f rFlags &= ~BTCF_
2e8e0 41 74 4c 61 73 74 3b 0a 20 20 20 20 7d 0a 20 20 AtLast;. }.
2e8f0 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 }else if( rc==SQ
2e900 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 LITE_EMPTY ){.
2e910 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
2e920 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 pgnoRoot==0 || p
2e930 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c Cur->pPage->nCel
2e940 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 2a 70 52 65 l==0 );. *pRe
2e950 73 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 s = 1;. rc =
2e960 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
2e970 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
2e980 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
2e990 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 r so that it poi
2e9a0 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 nts to an entry
2e9b0 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a near the key .**
2e9c0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 specified by pI
2e9d0 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e dxKey or intKey.
2e9e0 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 Return a succ
2e9f0 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ess code..**.**
2ea00 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 For INTKEY table
2ea10 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 s, the intKey pa
2ea20 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e rameter is used.
2ea30 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 pIdxKey .** mu
2ea40 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 st be NULL. For
2ea50 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 index tables, p
2ea60 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 IdxKey is used a
2ea70 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 nd intKey.** is
2ea80 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 ignored..**.** I
2ea90 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 f an exact match
2eaa0 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 is not found, t
2eab0 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
2eac0 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 s always.** left
2ead0 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c pointing at a l
2eae0 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 eaf page which w
2eaf0 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e ould hold the en
2eb00 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 try if it.** wer
2eb10 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 e present. The
2eb20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 cursor might poi
2eb30 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 nt to an entry t
2eb40 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 hat comes.** bef
2eb50 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 ore or after the
2eb60 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 key..**.** An i
2eb70 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65 nteger is writte
2eb80 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 n into *pRes whi
2eb90 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 ch is the result
2eba0 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 of.** comparing
2ebb0 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 the key with th
2ebc0 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 e entry to which
2ebd0 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a the cursor is .
2ebe0 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 ** pointing. Th
2ebf0 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 e meaning of the
2ec00 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e integer written
2ec10 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 into.** *pRes i
2ec20 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
2ec30 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 .** *pRes<0
2ec40 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 The cursor
2ec50 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
2ec60 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 at an entry tha
2ec70 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
2ec80 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 is smaller
2ec90 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 than intKey/pId
2eca0 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 xKey or if the t
2ecb0 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a able is empty.**
2ecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ecd0 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 and the cursor
2ece0 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 is therefore le
2ecf0 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 ft point to noth
2ed00 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a ing..**.** *
2ed10 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 pRes==0 The
2ed20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
2ed30 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
2ed40 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 try that.**
2ed50 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 exa
2ed60 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 ctly matches int
2ed70 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a Key/pIdxKey..**.
2ed80 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 ** *pRes>0
2ed90 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
2eda0 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
2edb0 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
2edc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
2edd0 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 is larger t
2ede0 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b han intKey/pIdxK
2edf0 65 79 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 69 6e ey..**.** For in
2ee00 64 65 78 20 74 61 62 6c 65 73 2c 20 74 68 65 20 dex tables, the
2ee10 70 49 64 78 4b 65 79 2d 3e 65 71 53 65 65 6e 20 pIdxKey->eqSeen
2ee20 66 69 65 6c 64 20 69 73 20 73 65 74 20 74 6f 20 field is set to
2ee30 31 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 65 78 1 if there.** ex
2ee40 69 73 74 73 20 61 6e 20 65 6e 74 72 79 20 69 6e ists an entry in
2ee50 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
2ee60 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 exactly matches
2ee70 70 49 64 78 4b 65 79 2e 20 20 0a 2a 2f 0a 69 6e pIdxKey. .*/.in
2ee80 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f t sqlite3BtreeMo
2ee90 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 vetoUnpacked(.
2eea0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
2eeb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2eec0 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f 76 cursor to be mov
2eed0 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 ed */. Unpacked
2eee0 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 2c Record *pIdxKey,
2eef0 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 /* Unpacked ind
2ef00 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 20 ex key */. i64
2ef10 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 20 intKey,
2ef20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c /* The tabl
2ef30 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 62 e key */. int b
2ef40 69 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 20 iasRight,
2ef50 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 /* If true,
2ef60 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20 bias the search
2ef70 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 to the high end
2ef80 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 */. int *pRes
2ef90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2efa0 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65 Write search re
2efb0 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b sults here */.){
2efc0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 65 63 . int rc;. Rec
2efd0 6f 72 64 43 6f 6d 70 61 72 65 20 78 52 65 63 6f ordCompare xReco
2efe0 72 64 43 6f 6d 70 61 72 65 3b 0a 0a 20 20 61 73 rdCompare;.. as
2eff0 73 65 72 74 28 20 63 75 72 73 6f 72 4f 77 6e 73 sert( cursorOwns
2f000 42 74 53 68 61 72 65 64 28 70 43 75 72 29 20 29 BtShared(pCur) )
2f010 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
2f020 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
2f030 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
2f040 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
2f050 65 72 74 28 20 70 52 65 73 20 29 3b 0a 20 20 61 ert( pRes );. a
2f060 73 73 65 72 74 28 20 28 70 49 64 78 4b 65 79 3d ssert( (pIdxKey=
2f070 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 =0)==(pCur->pKey
2f080 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 20 20 61 73 Info==0) );. as
2f090 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
2f0a0 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te!=CURSOR_VALID
2f0b0 20 7c 7c 20 28 70 49 64 78 4b 65 79 3d 3d 30 29 || (pIdxKey==0)
2f0c0 3d 3d 28 70 43 75 72 2d 3e 63 75 72 49 6e 74 4b ==(pCur->curIntK
2f0d0 65 79 21 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 ey!=0) );.. /*
2f0e0 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
2f0f0 20 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f already positio
2f100 6e 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 ned at the point
2f110 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20 we are trying.
2f120 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 ** to move to,
2f130 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e then just return
2f140 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 without doing a
2f150 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 ny work */. if(
2f160 20 70 49 64 78 4b 65 79 3d 3d 30 0a 20 20 20 26 pIdxKey==0. &
2f170 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d & pCur->eState==
2f180 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 CURSOR_VALID &&
2f190 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 (pCur->curFlags
2f1a0 26 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 & BTCF_ValidNKey
2f1b0 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 69 66 )!=0. ){. if
2f1c0 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 ( pCur->info.nKe
2f1d0 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 y==intKey ){.
2f1e0 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
2f1f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
2f200 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E_OK;. }.
2f210 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e if( pCur->info.n
2f220 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 Key<intKey ){.
2f230 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 63 if( (pCur->c
2f240 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 41 urFlags & BTCF_A
2f250 74 4c 61 73 74 29 21 3d 30 20 29 7b 0a 20 20 20 tLast)!=0 ){.
2f260 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b *pRes = -1;
2f270 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
2f280 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
2f290 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 }. /* If t
2f2a0 68 65 20 72 65 71 75 65 73 74 65 64 20 6b 65 79 he requested key
2f2b0 20 69 73 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 is one more tha
2f2c0 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b n the previous k
2f2d0 65 79 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a ey, then. *
2f2e0 2a 20 74 72 79 20 74 6f 20 67 65 74 20 74 68 65 * try to get the
2f2f0 72 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 re using sqlite3
2f300 42 74 72 65 65 4e 65 78 74 28 29 20 72 61 74 68 BtreeNext() rath
2f310 65 72 20 74 68 61 6e 20 61 20 66 75 6c 6c 0a 20 er than a full.
2f320 20 20 20 20 20 2a 2a 20 62 69 6e 61 72 79 20 73 ** binary s
2f330 65 61 72 63 68 2e 20 20 54 68 69 73 20 69 73 20 earch. This is
2f340 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 an optimization
2f350 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 72 72 65 only. The corre
2f360 63 74 20 61 6e 73 77 65 72 0a 20 20 20 20 20 20 ct answer.
2f370 2a 2a 20 69 73 20 73 74 69 6c 6c 20 6f 62 74 61 ** is still obta
2f380 69 6e 65 64 20 77 69 74 68 6f 75 74 20 74 68 69 ined without thi
2f390 73 20 63 61 73 65 2c 20 6f 6e 6c 79 20 61 20 6c s case, only a l
2f3a0 69 74 74 6c 65 20 6d 6f 72 65 20 73 6c 6f 77 65 ittle more slowe
2f3b0 6c 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 ly */. if(
2f3c0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 2b pCur->info.nKey+
2f3d0 31 3d 3d 69 6e 74 4b 65 79 20 26 26 20 21 70 43 1==intKey && !pC
2f3e0 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 29 7b 0a ur->skipNext ){.
2f3f0 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
2f400 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 0;. rc =
2f410 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
2f420 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 (pCur, 0);.
2f430 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
2f440 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
2f450 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 getCellInfo(pC
2f460 75 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ur);. i
2f470 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b f( pCur->info.nK
2f480 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ey==intKey ){.
2f490 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
2f4a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
2f4b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2f4c0 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 }else if( rc==SQ
2f4d0 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
2f4e0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
2f4f0 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d TE_OK;. }
2f500 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
2f510 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
2f520 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2f530 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 49 }. }.. if( pI
2f540 64 78 4b 65 79 20 29 7b 0a 20 20 20 20 78 52 65 dxKey ){. xRe
2f550 63 6f 72 64 43 6f 6d 70 61 72 65 20 3d 20 73 71 cordCompare = sq
2f560 6c 69 74 65 33 56 64 62 65 46 69 6e 64 43 6f 6d lite3VdbeFindCom
2f570 70 61 72 65 28 70 49 64 78 4b 65 79 29 3b 0a 20 pare(pIdxKey);.
2f580 20 20 20 70 49 64 78 4b 65 79 2d 3e 65 72 72 43 pIdxKey->errC
2f590 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 ode = 0;. ass
2f5a0 65 72 74 28 20 70 49 64 78 4b 65 79 2d 3e 64 65 ert( pIdxKey->de
2f5b0 66 61 75 6c 74 5f 72 63 3d 3d 31 20 0a 20 20 20 fault_rc==1 .
2f5c0 20 20 20 20 20 20 7c 7c 20 70 49 64 78 4b 65 79 || pIdxKey
2f5d0 2d 3e 64 65 66 61 75 6c 74 5f 72 63 3d 3d 30 20 ->default_rc==0
2f5e0 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 49 64 . || pId
2f5f0 78 4b 65 79 2d 3e 64 65 66 61 75 6c 74 5f 72 63 xKey->default_rc
2f600 3d 3d 2d 31 0a 20 20 20 20 29 3b 0a 20 20 7d 65 ==-1. );. }e
2f610 6c 73 65 7b 0a 20 20 20 20 78 52 65 63 6f 72 64 lse{. xRecord
2f620 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 2f 2a 20 Compare = 0; /*
2f630 41 6c 6c 20 6b 65 79 73 20 61 72 65 20 69 6e 74 All keys are int
2f640 65 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 0a 20 20 egers */. }..
2f650 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 rc = moveToRoot(
2f660 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 pCur);. if( rc
2f670 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 ){. if( rc==S
2f680 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 QLITE_EMPTY ){.
2f690 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
2f6a0 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c r->pgnoRoot==0 |
2f6b0 7c 20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e | pCur->pPage->n
2f6c0 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Cell==0 );.
2f6d0 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 *pRes = -1;.
2f6e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
2f6f0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 _OK;. }. r
2f700 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
2f710 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 assert( pCur->pP
2f720 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 age );. assert(
2f730 20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 69 73 pCur->pPage->is
2f740 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 Init );. assert
2f750 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
2f760 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
2f770 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
2f780 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3e 20 30 pPage->nCell > 0
2f790 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
2f7a0 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 7c 7c 20 ur->iPage==0 ||
2f7b0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
2f7c0 3e 69 6e 74 4b 65 79 3d 3d 70 43 75 72 2d 3e 63 >intKey==pCur->c
2f7d0 75 72 49 6e 74 4b 65 79 20 29 3b 0a 20 20 61 73 urIntKey );. as
2f7e0 73 65 72 74 28 20 70 43 75 72 2d 3e 63 75 72 49 sert( pCur->curI
2f7f0 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 ntKey || pIdxKey
2f800 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 );. for(;;){.
2f810 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 2c int lwr, upr,
2f820 20 69 64 78 2c 20 63 3b 0a 20 20 20 20 50 67 6e idx, c;. Pgn
2f830 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 o chldPg;. Me
2f840 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 mPage *pPage = p
2f850 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 20 20 Cur->pPage;.
2f860 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 u8 *pCell;
2f870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f880 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
2f890 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 o current cell i
2f8a0 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 n pPage */..
2f8b0 2f 2a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 /* pPage->nCell
2f8c0 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 must be greater
2f8d0 74 68 61 6e 20 7a 65 72 6f 2e 20 49 66 20 74 68 than zero. If th
2f8e0 69 73 20 69 73 20 74 68 65 20 72 6f 6f 74 2d 70 is is the root-p
2f8f0 61 67 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 age. ** the c
2f900 75 72 73 6f 72 20 77 6f 75 6c 64 20 68 61 76 65 ursor would have
2f910 20 62 65 65 6e 20 49 4e 56 41 4c 49 44 20 61 62 been INVALID ab
2f920 6f 76 65 20 61 6e 64 20 74 68 69 73 20 66 6f 72 ove and this for
2f930 28 3b 3b 29 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a (;;) loop. **
2f940 20 6e 6f 74 20 72 75 6e 2e 20 49 66 20 74 68 69 not run. If thi
2f950 73 20 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f s is not the roo
2f960 74 2d 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 t-page, then the
2f970 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 72 moveToChild() r
2f980 6f 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 77 6f outine. ** wo
2f990 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 uld have already
2f9a0 20 64 65 74 65 63 74 65 64 20 64 62 20 63 6f 72 detected db cor
2f9b0 72 75 70 74 69 6f 6e 2e 20 53 69 6d 69 6c 61 72 ruption. Similar
2f9c0 6c 79 2c 20 70 50 61 67 65 20 6d 75 73 74 0a 20 ly, pPage must.
2f9d0 20 20 20 2a 2a 20 62 65 20 74 68 65 20 72 69 67 ** be the rig
2f9e0 68 74 20 6b 69 6e 64 20 28 69 6e 64 65 78 20 6f ht kind (index o
2f9f0 72 20 74 61 62 6c 65 29 20 6f 66 20 62 2d 74 72 r table) of b-tr
2fa00 65 65 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 ee page. Otherwi
2fa10 73 65 0a 20 20 20 20 2a 2a 20 61 20 6d 6f 76 65 se. ** a move
2fa20 54 6f 43 68 69 6c 64 28 29 20 6f 72 20 6d 6f 76 ToChild() or mov
2fa30 65 54 6f 52 6f 6f 74 28 29 20 63 61 6c 6c 20 77 eToRoot() call w
2fa40 6f 75 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 ould have detect
2fa50 65 64 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 ed corruption.
2fa60 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
2fa70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b Page->nCell>0 );
2fa80 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
2fa90 67 65 2d 3e 69 6e 74 4b 65 79 3d 3d 28 70 49 64 ge->intKey==(pId
2faa0 78 4b 65 79 3d 3d 30 29 20 29 3b 0a 20 20 20 20 xKey==0) );.
2fab0 6c 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 lwr = 0;. upr
2fac0 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d = pPage->nCell-
2fad0 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 62 1;. assert( b
2fae0 69 61 73 52 69 67 68 74 3d 3d 30 20 7c 7c 20 62 iasRight==0 || b
2faf0 69 61 73 52 69 67 68 74 3d 3d 31 20 29 3b 0a 20 iasRight==1 );.
2fb00 20 20 20 69 64 78 20 3d 20 75 70 72 3e 3e 28 31 idx = upr>>(1
2fb10 2d 62 69 61 73 52 69 67 68 74 29 3b 20 2f 2a 20 -biasRight); /*
2fb20 69 64 78 20 3d 20 62 69 61 73 52 69 67 68 74 20 idx = biasRight
2fb30 3f 20 75 70 72 20 3a 20 28 6c 77 72 2b 75 70 72 ? upr : (lwr+upr
2fb40 29 2f 32 3b 20 2a 2f 0a 20 20 20 20 70 43 75 72 )/2; */. pCur
2fb50 2d 3e 69 78 20 3d 20 28 75 31 36 29 69 64 78 3b ->ix = (u16)idx;
2fb60 0a 20 20 20 20 69 66 28 20 78 52 65 63 6f 72 64 . if( xRecord
2fb70 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 Compare==0 ){.
2fb80 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 for(;;){.
2fb90 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 i64 nCellKe
2fba0 79 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c y;. pCell
2fbb0 20 3d 20 66 69 6e 64 43 65 6c 6c 50 61 73 74 50 = findCellPastP
2fbc0 74 72 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a tr(pPage, idx);.
2fbd0 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 if( pPag
2fbe0 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 29 7b e->intKeyLeaf ){
2fbf0 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
2fc00 28 20 30 78 38 30 20 3c 3d 20 2a 28 70 43 65 6c ( 0x80 <= *(pCel
2fc10 6c 2b 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 l++) ){.
2fc20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 3e 3d 70 if( pCell>=p
2fc30 50 61 67 65 2d 3e 61 44 61 74 61 45 6e 64 20 29 Page->aDataEnd )
2fc40 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
2fc50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
2fc60 52 52 55 50 54 5f 50 47 4e 4f 28 70 50 61 67 65 RRUPT_PGNO(pPage
2fc70 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
2fc80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
2fc90 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2fca0 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 28 70 getVarint(p
2fcb0 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43 65 Cell, (u64*)&nCe
2fcc0 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 llKey);.
2fcd0 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 if( nCellKey<int
2fce0 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Key ){.
2fcf0 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20 lwr = idx+1;.
2fd00 20 20 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e if( lwr>
2fd10 75 70 72 20 29 7b 20 63 20 3d 20 2d 31 3b 20 62 upr ){ c = -1; b
2fd20 72 65 61 6b 3b 20 7d 0a 20 20 20 20 20 20 20 20 reak; }.
2fd30 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c 6c 4b }else if( nCellK
2fd40 65 79 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 ey>intKey ){.
2fd50 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 upr = idx
2fd60 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 -1;. if
2fd70 28 20 6c 77 72 3e 75 70 72 20 29 7b 20 63 20 3d ( lwr>upr ){ c =
2fd80 20 2b 31 3b 20 62 72 65 61 6b 3b 20 7d 0a 20 20 +1; break; }.
2fd90 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2fda0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
2fdb0 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 CellKey==intKey
2fdc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 );. pCu
2fdd0 72 2d 3e 69 78 20 3d 20 28 75 31 36 29 69 64 78 r->ix = (u16)idx
2fde0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
2fdf0 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
2fe00 20 20 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 lwr
2fe10 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 = idx;.
2fe20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 6e goto moveto_n
2fe30 65 78 74 5f 6c 61 79 65 72 3b 0a 20 20 20 20 20 ext_layer;.
2fe40 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
2fe50 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 63 75 pCur->cu
2fe60 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 46 5f 56 rFlags |= BTCF_V
2fe70 61 6c 69 64 4e 4b 65 79 3b 0a 20 20 20 20 20 20 alidNKey;.
2fe80 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f pCur->info
2fe90 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 .nKey = nCellKey
2fea0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 ;. pC
2feb0 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
2fec0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
2fed0 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
2fee0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
2fef0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
2ff00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
2ff10 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c assert( l
2ff20 77 72 2b 75 70 72 3e 3d 30 20 29 3b 0a 20 20 20 wr+upr>=0 );.
2ff30 20 20 20 20 20 69 64 78 20 3d 20 28 6c 77 72 2b idx = (lwr+
2ff40 75 70 72 29 3e 3e 31 3b 20 20 2f 2a 20 69 64 78 upr)>>1; /* idx
2ff50 20 3d 20 28 6c 77 72 2b 75 70 72 29 2f 32 3b 20 = (lwr+upr)/2;
2ff60 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d */. }. }
2ff70 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72 28 else{. for(
2ff80 3b 3b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ;;){. int
2ff90 20 6e 43 65 6c 6c 3b 20 20 2f 2a 20 53 69 7a 65 nCell; /* Size
2ffa0 20 6f 66 20 74 68 65 20 70 43 65 6c 6c 20 63 65 of the pCell ce
2ffb0 6c 6c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 ll in bytes */.
2ffc0 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 pCell = f
2ffd0 69 6e 64 43 65 6c 6c 50 61 73 74 50 74 72 28 70 indCellPastPtr(p
2ffe0 50 61 67 65 2c 20 69 64 78 29 3b 0a 0a 20 20 20 Page, idx);..
2fff0 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 /* The maxi
30000 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 mum supported pa
30010 67 65 2d 73 69 7a 65 20 69 73 20 36 35 35 33 36 ge-size is 65536
30020 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d 65 61 bytes. This mea
30030 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 ns that.
30040 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** the maximum n
30050 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 20 umber of record
30060 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f 6e 20 bytes stored on
30070 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65 65 0a an index B-Tree.
30080 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 ** page
30090 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31 36 33 is less than 163
300a0 38 34 20 62 79 74 65 73 20 61 6e 64 20 6d 61 79 84 bytes and may
300b0 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61 20 be stored as a
300c0 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20 20 2a 2-byte. *
300d0 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73 20 69 * varint. This i
300e0 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 nformation is us
300f0 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f ed to attempt to
30100 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67 20 0a avoid parsing .
30110 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 ** the e
30120 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20 63 68 ntire cell by ch
30130 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 20 63 ecking for the c
30140 61 73 65 73 20 77 68 65 72 65 20 74 68 65 20 72 ases where the r
30150 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20 20 20 ecord is .
30160 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e 74 69 ** stored enti
30170 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 rely within the
30180 62 2d 74 72 65 65 20 70 61 67 65 20 62 79 20 69 b-tree page by i
30190 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 69 nspecting the fi
301a0 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 rst . **
301b0 32 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 2 bytes of the c
301c0 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ell.. */.
301d0 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 nCell =
301e0 70 43 65 6c 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 pCell[0];.
301f0 20 20 69 66 28 20 6e 43 65 6c 6c 3c 3d 70 50 61 if( nCell<=pPa
30200 67 65 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c ge->max1bytePayl
30210 6f 61 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 oad ){.
30220 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 /* This branch
30230 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 63 6f runs if the reco
30240 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 rd-size field of
30250 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 0a 20 the cell is a.
30260 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e 67 ** sing
30270 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 le byte varint a
30280 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 66 69 nd the record fi
30290 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 ts entirely on t
302a0 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 20 20 he main.
302b0 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 65 ** b-tree page
302c0 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 . */.
302d0 74 65 73 74 63 61 73 65 28 20 70 43 65 6c 6c 2b testcase( pCell+
302e0 6e 43 65 6c 6c 2b 31 3d 3d 70 50 61 67 65 2d 3e nCell+1==pPage->
302f0 61 44 61 74 61 45 6e 64 20 29 3b 0a 20 20 20 20 aDataEnd );.
30300 20 20 20 20 20 20 63 20 3d 20 78 52 65 63 6f 72 c = xRecor
30310 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 dCompare(nCell,
30320 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 31 5d (void*)&pCell[1]
30330 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 , pIdxKey);.
30340 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 28 }else if( !(
30350 70 43 65 6c 6c 5b 31 5d 20 26 20 30 78 38 30 29 pCell[1] & 0x80)
30360 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 . && (
30370 6e 43 65 6c 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 nCell = ((nCell&
30380 30 78 37 66 29 3c 3c 37 29 20 2b 20 70 43 65 6c 0x7f)<<7) + pCel
30390 6c 5b 31 5d 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 l[1])<=pPage->ma
303a0 78 4c 6f 63 61 6c 0a 20 20 20 20 20 20 20 20 29 xLocal. )
303b0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
303c0 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 he record-size f
303d0 69 65 6c 64 20 69 73 20 61 20 32 20 62 79 74 65 ield is a 2 byte
303e0 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 varint and the
303f0 72 65 63 6f 72 64 20 0a 20 20 20 20 20 20 20 20 record .
30400 20 20 2a 2a 20 66 69 74 73 20 65 6e 74 69 72 65 ** fits entire
30410 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 ly on the main b
30420 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a -tree page. */.
30430 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 testca
30440 73 65 28 20 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b se( pCell+nCell+
30450 32 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 45 2==pPage->aDataE
30460 6e 64 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 nd );.
30470 63 20 3d 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 c = xRecordCompa
30480 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a re(nCell, (void*
30490 29 26 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 )&pCell[2], pIdx
304a0 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 Key);. }e
304b0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
304c0 2a 20 54 68 65 20 72 65 63 6f 72 64 20 66 6c 6f * The record flo
304d0 77 73 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 ws over onto one
304e0 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f or more overflo
304f0 77 20 70 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 w pages. In.
30500 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 61 ** this ca
30510 73 65 20 74 68 65 20 77 68 6f 6c 65 20 63 65 6c se the whole cel
30520 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 61 l needs to be pa
30530 72 73 65 64 2c 20 61 20 62 75 66 66 65 72 20 61 rsed, a buffer a
30540 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 20 20 llocated.
30550 20 20 20 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 ** and access
30560 50 61 79 6c 6f 61 64 28 29 20 75 73 65 64 20 74 Payload() used t
30570 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72 o retrieve the r
30580 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 ecord into the.
30590 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 ** buff
305a0 65 72 20 62 65 66 6f 72 65 20 56 64 62 65 52 65 er before VdbeRe
305b0 63 6f 72 64 43 6f 6d 70 61 72 65 28 29 20 63 61 cordCompare() ca
305c0 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 0a 20 20 n be called. .
305d0 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
305e0 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72 ** If the r
305f0 65 63 6f 72 64 20 69 73 20 63 6f 72 72 75 70 74 ecord is corrupt
30600 2c 20 74 68 65 20 78 52 65 63 6f 72 64 43 6f 6d , the xRecordCom
30610 70 61 72 65 20 72 6f 75 74 69 6e 65 20 6d 61 79 pare routine may
30620 20 72 65 61 64 0a 20 20 20 20 20 20 20 20 20 20 read.
30630 2a 2a 20 75 70 20 74 6f 20 74 77 6f 20 76 61 72 ** up to two var
30640 69 6e 74 73 20 70 61 73 74 20 74 68 65 20 65 6e ints past the en
30650 64 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 2e d of the buffer.
30660 20 41 6e 20 65 78 74 72 61 20 31 38 20 0a 20 20 An extra 18 .
30670 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 ** bytes
30680 20 6f 66 20 70 61 64 64 69 6e 67 20 69 73 20 61 of padding is a
30690 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20 llocated at the
306a0 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66 65 end of the buffe
306b0 72 20 69 6e 0a 20 20 20 20 20 20 20 20 20 20 2a r in. *
306c0 2a 20 63 61 73 65 20 74 68 69 73 20 68 61 70 70 * case this happ
306d0 65 6e 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 ens. */.
306e0 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b 65 void *pCellKe
306f0 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 y;. u8
30700 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f 64 * const pCellBod
30710 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61 67 y = pCell - pPag
30720 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b e->childPtrSize;
30730 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
30740 2d 3e 78 50 61 72 73 65 43 65 6c 6c 28 70 50 61 ->xParseCell(pPa
30750 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 26 ge, pCellBody, &
30760 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 pCur->info);.
30770 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 28 nCell = (
30780 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e int)pCur->info.n
30790 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 74 Key;. t
307a0 65 73 74 63 61 73 65 28 20 6e 43 65 6c 6c 3c 30 estcase( nCell<0
307b0 20 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ); /* True if
307c0 20 6b 65 79 20 73 69 7a 65 20 69 73 20 32 5e 33 key size is 2^3
307d0 32 20 6f 72 20 6d 6f 72 65 20 2a 2f 0a 20 20 20 2 or more */.
307e0 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
307f0 20 6e 43 65 6c 6c 3d 3d 30 20 29 3b 20 20 2f 2a nCell==0 ); /*
30800 20 49 6e 76 61 6c 69 64 20 6b 65 79 20 73 69 7a Invalid key siz
30810 65 3a 20 20 30 78 38 30 20 30 78 38 30 20 30 78 e: 0x80 0x80 0x
30820 30 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 00 */.
30830 74 65 73 74 63 61 73 65 28 20 6e 43 65 6c 6c 3d testcase( nCell=
30840 3d 31 20 29 3b 20 20 2f 2a 20 49 6e 76 61 6c 69 =1 ); /* Invali
30850 64 20 6b 65 79 20 73 69 7a 65 3a 20 20 30 78 38 d key size: 0x8
30860 30 20 30 78 38 30 20 30 78 30 31 20 2a 2f 0a 20 0 0x80 0x01 */.
30870 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 testcas
30880 65 28 20 6e 43 65 6c 6c 3d 3d 32 20 29 3b 20 20 e( nCell==2 );
30890 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 65 67 61 6c /* Minimum legal
308a0 20 69 6e 64 65 78 20 6b 65 79 20 73 69 7a 65 20 index key size
308b0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
308c0 20 6e 43 65 6c 6c 3c 32 20 29 7b 0a 20 20 20 20 nCell<2 ){.
308d0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
308e0 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f ITE_CORRUPT_PGNO
308f0 28 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 (pPage->pgno);.
30900 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
30910 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
30920 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
30930 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 pCellKey =
30940 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e sqlite3Malloc( n
30950 43 65 6c 6c 2b 31 38 20 29 3b 0a 20 20 20 20 20 Cell+18 );.
30960 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 4b 65 if( pCellKe
30970 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 y==0 ){.
30980 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
30990 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 NOMEM_BKPT;.
309a0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 goto mov
309b0 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
309c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
309d0 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 28 75 31 pCur->ix = (u1
309e0 36 29 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 6)idx;.
309f0 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c rc = accessPayl
30a00 6f 61 64 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 oad(pCur, 0, nCe
30a10 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 ll, (unsigned ch
30a20 61 72 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 30 29 ar*)pCellKey, 0)
30a30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 ;. pCur
30a40 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 42 ->curFlags &= ~B
30a50 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 3b 0a 20 TCF_ValidOvfl;.
30a60 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 if( rc
30a70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
30a80 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c qlite3_free(pCel
30a90 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 lKey);.
30aa0 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
30ab0 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20 inish;.
30ac0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d }. c =
30ad0 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 xRecordCompare(
30ae0 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79 2c nCell, pCellKey,
30af0 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 pIdxKey);.
30b00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
30b10 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 e(pCellKey);.
30b20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
30b30 73 73 65 72 74 28 20 0a 20 20 20 20 20 20 20 20 ssert( .
30b40 20 20 20 20 28 70 49 64 78 4b 65 79 2d 3e 65 72 (pIdxKey->er
30b50 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 43 4f rCode!=SQLITE_CO
30b60 52 52 55 50 54 20 7c 7c 20 63 3d 3d 30 29 0a 20 RRUPT || c==0).
30b70 20 20 20 20 20 20 20 20 26 26 20 28 70 49 64 78 && (pIdx
30b80 4b 65 79 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 Key->errCode!=SQ
30b90 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 43 LITE_NOMEM || pC
30ba0 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e ur->pBtree->db->
30bb0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 0a 20 20 mallocFailed).
30bc0 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
30bd0 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 if( c<0 ){.
30be0 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b lwr = idx+
30bf0 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
30c00 20 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 if( c>0 ){.
30c10 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d upr = idx-
30c20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
30c30 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
30c40 72 74 28 20 63 3d 3d 30 20 29 3b 0a 20 20 20 20 rt( c==0 );.
30c50 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b *pRes = 0;
30c60 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
30c70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
30c80 20 20 20 20 20 70 43 75 72 2d 3e 69 78 20 3d 20 pCur->ix =
30c90 28 75 31 36 29 69 64 78 3b 0a 20 20 20 20 20 20 (u16)idx;.
30ca0 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 79 2d if( pIdxKey-
30cb0 3e 65 72 72 43 6f 64 65 20 29 20 72 63 20 3d 20 >errCode ) rc =
30cc0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a SQLITE_CORRUPT;.
30cd0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d goto m
30ce0 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
30cf0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
30d00 69 66 28 20 6c 77 72 3e 75 70 72 20 29 20 62 72 if( lwr>upr ) br
30d10 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 eak;. ass
30d20 65 72 74 28 20 6c 77 72 2b 75 70 72 3e 3d 30 20 ert( lwr+upr>=0
30d30 29 3b 0a 20 20 20 20 20 20 20 20 69 64 78 20 3d );. idx =
30d40 20 28 6c 77 72 2b 75 70 72 29 3e 3e 31 3b 20 20 (lwr+upr)>>1;
30d50 2f 2a 20 69 64 78 20 3d 20 28 6c 77 72 2b 75 70 /* idx = (lwr+up
30d60 72 29 2f 32 20 2a 2f 0a 20 20 20 20 20 20 7d 0a r)/2 */. }.
30d70 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
30d80 28 20 6c 77 72 3d 3d 75 70 72 2b 31 20 7c 7c 20 ( lwr==upr+1 ||
30d90 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 (pPage->intKey &
30da0 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 29 20 & !pPage->leaf)
30db0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
30dc0 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a Page->isInit );.
30dd0 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c if( pPage->l
30de0 65 61 66 20 29 7b 0a 20 20 20 20 20 20 61 73 73 eaf ){. ass
30df0 65 72 74 28 20 70 43 75 72 2d 3e 69 78 3c 70 43 ert( pCur->ix<pC
30e00 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ur->pPage->nCell
30e10 20 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e );. pCur->
30e20 69 78 20 3d 20 28 75 31 36 29 69 64 78 3b 0a 20 ix = (u16)idx;.
30e30 20 20 20 20 20 2a 70 52 65 73 20 3d 20 63 3b 0a *pRes = c;.
30e40 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
30e50 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f E_OK;. goto
30e60 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a moveto_finish;.
30e70 20 20 20 20 7d 0a 6d 6f 76 65 74 6f 5f 6e 65 78 }.moveto_nex
30e80 74 5f 6c 61 79 65 72 3a 0a 20 20 20 20 69 66 28 t_layer:. if(
30e90 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 lwr>=pPage->nCe
30ea0 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 ll ){. chld
30eb0 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 Pg = get4byte(&p
30ec0 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
30ed0 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
30ee0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
30ef0 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 chldPg = get4
30f00 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
30f10 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 age, lwr));.
30f20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 78 20 3d }. pCur->ix =
30f30 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20 20 72 (u16)lwr;. r
30f40 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
30f50 70 43 75 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20 pCur, chldPg);.
30f60 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61 if( rc ) brea
30f70 6b 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66 69 k;. }.moveto_fi
30f80 6e 69 73 68 3a 0a 20 20 70 43 75 72 2d 3e 69 6e nish:. pCur->in
30f90 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
30fa0 61 73 73 65 72 74 28 20 28 70 43 75 72 2d 3e 63 assert( (pCur->c
30fb0 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 urFlags & BTCF_V
30fc0 61 6c 69 64 4f 76 66 6c 29 3d 3d 30 20 29 3b 0a alidOvfl)==0 );.
30fd0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
30fe0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
30ff0 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 UE if the cursor
31000 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
31010 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 at an entry of
31020 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
31030 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 TRUE will be re
31040 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 turned after a c
31050 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
31060 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a reeNext() moves.
31070 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 ** past the last
31080 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
31090 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 ble or sqlite3Bt
310a0 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 reePrev() moves
310b0 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 past.** the firs
310c0 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 t entry. TRUE i
310d0 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 s also returned
310e0 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
310f0 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 empty..*/.int sq
31100 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 lite3BtreeEof(Bt
31110 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
31120 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 /* TODO: What i
31130 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
31140 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 in CURSOR_REQUIR
31150 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 ESEEK but all ta
31160 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a ble entries. **
31170 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 have been delet
31180 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c ed? This API wil
31190 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 l need to change
311a0 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 to return an er
311b0 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 ror code. ** as
311c0 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f well as the boo
311d0 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 lean result valu
311e0 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e e.. */. return
311f0 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d (CURSOR_VALID!=
31200 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d pCur->eState);.}
31210 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
31220 6e 20 65 73 74 69 6d 61 74 65 20 66 6f 72 20 74 n estimate for t
31230 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
31240 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 s in the table t
31250 68 61 74 20 70 43 75 72 20 69 73 0a 2a 2a 20 70 hat pCur is.** p
31260 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 52 65 74 ointing to. Ret
31270 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e urn a negative n
31280 75 6d 62 65 72 20 69 66 20 6e 6f 20 65 73 74 69 umber if no esti
31290 6d 61 74 65 20 69 73 20 63 75 72 72 65 6e 74 6c mate is currentl
312a0 79 20 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e y .** available.
312b0 0a 2a 2f 0a 69 36 34 20 73 71 6c 69 74 65 33 42 .*/.i64 sqlite3B
312c0 74 72 65 65 52 6f 77 43 6f 75 6e 74 45 73 74 28 treeRowCountEst(
312d0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
312e0 0a 20 20 69 36 34 20 6e 3b 0a 20 20 75 38 20 69 . i64 n;. u8 i
312f0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
31300 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 64 28 sorOwnsBtShared(
31310 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
31320 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
31330 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
31340 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
31350 3b 0a 0a 20 20 2f 2a 20 43 75 72 72 65 6e 74 6c ;.. /* Currentl
31360 79 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 y this interface
31370 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 is only called
31380 62 79 20 74 68 65 20 4f 50 5f 49 66 53 6d 61 6c by the OP_IfSmal
31390 6c 65 72 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 2c ler. ** opcode,
313a0 20 61 6e 64 20 69 74 20 74 68 61 74 20 63 61 73 and it that cas
313b0 65 20 74 68 65 20 63 75 72 73 6f 72 20 77 69 6c e the cursor wil
313c0 6c 20 61 6c 77 61 79 73 20 62 65 20 76 61 6c 69 l always be vali
313d0 64 20 61 6e 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 d and. ** will
313e0 61 6c 77 61 79 73 20 70 6f 69 6e 74 20 74 6f 20 always point to
313f0 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a a leaf node. */.
31400 20 20 69 66 28 20 4e 45 56 45 52 28 70 43 75 72 if( NEVER(pCur
31410 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
31420 5f 56 41 4c 49 44 29 20 29 20 72 65 74 75 72 6e _VALID) ) return
31430 20 2d 31 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 -1;. if( NEVER
31440 28 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6c 65 (pCur->pPage->le
31450 61 66 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 af==0) ) return
31460 2d 31 3b 0a 0a 20 20 6e 20 3d 20 70 43 75 72 2d -1;.. n = pCur-
31470 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 >pPage->nCell;.
31480 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 75 72 for(i=0; i<pCur
31490 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
314a0 20 20 20 6e 20 2a 3d 20 70 43 75 72 2d 3e 61 70 n *= pCur->ap
314b0 50 61 67 65 5b 69 5d 2d 3e 6e 43 65 6c 6c 3b 0a Page[i]->nCell;.
314c0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a }. return n;.
314d0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 }../*.** Advance
314e0 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
314f0 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e he next entry in
31500 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 0a the database. .
31510 2a 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 3a ** Return value:
31520 0a 2a 2a 0a 2a 2a 20 20 20 20 53 51 4c 49 54 45 .**.** SQLITE
31530 5f 4f 4b 20 20 20 20 20 20 20 20 73 75 63 63 65 _OK succe
31540 73 73 0a 2a 2a 20 20 20 20 53 51 4c 49 54 45 5f ss.** SQLITE_
31550 44 4f 4e 45 20 20 20 20 20 20 63 75 72 73 6f 72 DONE cursor
31560 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e is already poin
31570 74 69 6e 67 20 61 74 20 74 68 65 20 6c 61 73 74 ting at the last
31580 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 6f element.** o
31590 74 68 65 72 77 69 73 65 20 20 20 20 20 20 20 20 therwise
315a0 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 65 72 72 some kind of err
315b0 6f 72 20 6f 63 63 75 72 72 65 64 0a 2a 2a 0a 2a or occurred.**.*
315c0 2a 20 54 68 65 20 6d 61 69 6e 20 65 6e 74 72 79 * The main entry
315d0 20 70 6f 69 6e 74 20 69 73 20 73 71 6c 69 74 65 point is sqlite
315e0 33 42 74 72 65 65 4e 65 78 74 28 29 2e 20 20 54 3BtreeNext(). T
315f0 68 61 74 20 72 6f 75 74 69 6e 65 20 69 73 20 6f hat routine is o
31600 70 74 69 6d 69 7a 65 64 0a 2a 2a 20 66 6f 72 20 ptimized.** for
31610 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 the common case
31620 6f 66 20 6d 65 72 65 6c 79 20 69 6e 63 72 65 6d of merely increm
31630 65 6e 74 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 enting the cell
31640 63 6f 75 6e 74 65 72 20 42 74 43 75 72 73 6f 72 counter BtCursor
31650 2e 61 69 49 64 78 0a 2a 2a 20 74 6f 20 74 68 65 .aiIdx.** to the
31660 20 6e 65 78 74 20 63 65 6c 6c 20 6f 6e 20 74 68 next cell on th
31670 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 2e 20 e current page.
31680 20 54 68 65 20 28 73 6c 6f 77 65 72 29 20 62 74 The (slower) bt
31690 72 65 65 4e 65 78 74 28 29 20 68 65 6c 70 65 72 reeNext() helper
316a0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 .** routine is c
316b0 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 20 69 73 alled when it is
316c0 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 6d 6f necessary to mo
316d0 76 65 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e ve to a differen
316e0 74 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 6f 20 t page or.** to
316f0 72 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 73 restore the curs
31700 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 69 74 or..**.** If bit
31710 20 30 78 30 31 20 6f 66 20 74 68 65 20 46 20 61 0x01 of the F a
31720 72 67 75 6d 65 6e 74 20 69 6e 20 73 71 6c 69 74 rgument in sqlit
31730 65 33 42 74 72 65 65 4e 65 78 74 28 43 2c 46 29 e3BtreeNext(C,F)
31740 20 69 73 20 31 2c 20 74 68 65 6e 20 74 68 65 0a is 1, then the.
31750 2a 2a 20 63 75 72 73 6f 72 20 63 6f 72 72 65 73 ** cursor corres
31760 70 6f 6e 64 73 20 74 6f 20 61 6e 20 53 51 4c 20 ponds to an SQL
31770 69 6e 64 65 78 20 61 6e 64 20 74 68 69 73 20 72 index and this r
31780 6f 75 74 69 6e 65 20 63 6f 75 6c 64 20 68 61 76 outine could hav
31790 65 20 62 65 65 6e 0a 2a 2a 20 73 6b 69 70 70 65 e been.** skippe
317a0 64 20 69 66 20 74 68 65 20 53 51 4c 20 69 6e 64 d if the SQL ind
317b0 65 78 20 68 61 64 20 62 65 65 6e 20 61 20 75 6e ex had been a un
317c0 69 71 75 65 20 69 6e 64 65 78 2e 20 20 54 68 65 ique index. The
317d0 20 46 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 F argument.** i
317e0 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20 s a hint to the
317f0 69 6d 70 6c 65 6d 65 6e 74 2e 20 20 53 51 4c 69 implement. SQLi
31800 74 65 20 62 74 72 65 65 20 69 6d 70 6c 65 6d 65 te btree impleme
31810 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 ntation does not
31820 20 75 73 65 0a 2a 2a 20 74 68 69 73 20 68 69 6e use.** this hin
31830 74 2c 20 62 75 74 20 43 4f 4d 44 42 32 20 64 6f t, but COMDB2 do
31840 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 es..*/.static SQ
31850 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 69 6e LITE_NOINLINE in
31860 74 20 62 74 72 65 65 4e 65 78 74 28 42 74 43 75 t btreeNext(BtCu
31870 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 rsor *pCur){. i
31880 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 nt rc;. int idx
31890 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
318a0 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 ge;.. assert( c
318b0 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 ursorOwnsBtShare
318c0 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 d(pCur) );. ass
318d0 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e ert( pCur->skipN
318e0 65 78 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e ext==0 || pCur->
318f0 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
31900 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 ALID );. if( pC
31910 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 ur->eState!=CURS
31920 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 OR_VALID ){.
31930 61 73 73 65 72 74 28 20 28 70 43 75 72 2d 3e 63 assert( (pCur->c
31940 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 urFlags & BTCF_V
31950 61 6c 69 64 4f 76 66 6c 29 3d 3d 30 20 29 3b 0a alidOvfl)==0 );.
31960 20 20 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 rc = restore
31970 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
31980 43 75 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 Cur);. if( rc
31990 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
319a0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
319b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 43 55 }. if( CU
319c0 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 RSOR_INVALID==pC
319d0 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 ur->eState ){.
319e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
319f0 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 E_DONE;. }.
31a00 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 if( pCur->skip
31a10 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 61 73 Next ){. as
31a20 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
31a30 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
31a40 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 || pCur->eState
31a50 3d 3d 43 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 ==CURSOR_SKIPNEX
31a60 54 20 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d T );. pCur-
31a70 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
31a80 5f 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 69 66 _VALID;. if
31a90 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 ( pCur->skipNext
31aa0 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 >0 ){. pC
31ab0 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 ur->skipNext = 0
31ac0 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
31ad0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
31ae0 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e }. pCur->
31af0 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 skipNext = 0;.
31b00 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 }. }.. pPage
31b10 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a = pCur->pPage;.
31b20 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e idx = ++pCur->
31b30 69 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 ix;. assert( pP
31b40 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a age->isInit );..
31b50 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 /* If the data
31b60 62 61 73 65 20 66 69 6c 65 20 69 73 20 63 6f 72 base file is cor
31b70 72 75 70 74 2c 20 69 74 20 69 73 20 70 6f 73 73 rupt, it is poss
31b80 69 62 6c 65 20 66 6f 72 20 74 68 65 20 76 61 6c ible for the val
31b90 75 65 20 6f 66 20 69 64 78 20 0a 20 20 2a 2a 20 ue of idx . **
31ba0 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 20 68 65 to be invalid he
31bb0 72 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c re. This can onl
31bc0 79 20 6f 63 63 75 72 20 69 66 20 61 20 73 65 63 y occur if a sec
31bd0 6f 6e 64 20 63 75 72 73 6f 72 20 6d 6f 64 69 66 ond cursor modif
31be0 69 65 73 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 ies. ** the pag
31bf0 65 20 77 68 69 6c 65 20 63 75 72 73 6f 72 20 70 e while cursor p
31c00 43 75 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 Cur is holding a
31c10 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 reference to it
31c20 2e 20 57 68 69 63 68 20 63 61 6e 0a 20 20 2a 2a . Which can. **
31c30 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 only happen if
31c40 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
31c50 63 6f 72 72 75 70 74 20 69 6e 20 73 75 63 68 20 corrupt in such
31c60 61 20 77 61 79 20 61 73 20 74 6f 20 6c 69 6e 6b a way as to link
31c70 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 69 the. ** page i
31c80 6e 74 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e nto more than on
31c90 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 e b-tree structu
31ca0 72 65 2e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 re. */. testcas
31cb0 65 28 20 69 64 78 3e 70 50 61 67 65 2d 3e 6e 43 e( idx>pPage->nC
31cc0 65 6c 6c 20 29 3b 0a 0a 20 20 69 66 28 20 69 64 ell );.. if( id
31cd0 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x>=pPage->nCell
31ce0 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 ){. if( !pPag
31cf0 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
31d00 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
31d10 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 d(pCur, get4byte
31d20 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
31d30 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
31d40 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 8]));. if(
31d50 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
31d60 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 6f 76 return mov
31d70 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 eToLeftmost(pCur
31d80 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b );. }. do{
31d90 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d . if( pCur-
31da0 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 >iPage==0 ){.
31db0 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 pCur->eStat
31dc0 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
31dd0 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ID;. retu
31de0 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a rn SQLITE_DONE;.
31df0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f }. mo
31e00 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 veToParent(pCur)
31e10 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 ;. pPage =
31e20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 20 pCur->pPage;.
31e30 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 69 }while( pCur->i
31e40 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x>=pPage->nCell
31e50 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
31e60 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
31e70 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
31e80 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 BtreeNext(pCur,
31e90 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0);. }else{.
31ea0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
31eb0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
31ec0 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 . if( pPage->le
31ed0 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e af ){. return
31ee0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
31ef0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
31f00 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
31f10 43 75 72 29 3b 0a 20 20 7d 0a 7d 0a 69 6e 74 20 Cur);. }.}.int
31f20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
31f30 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
31f40 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 4d int flags){. M
31f50 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 emPage *pPage;.
31f60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
31f70 52 28 20 66 6c 61 67 73 20 29 3b 20 20 2f 2a 20 R( flags ); /*
31f80 55 73 65 64 20 69 6e 20 43 4f 4d 44 42 32 20 62 Used in COMDB2 b
31f90 75 74 20 6e 6f 74 20 6e 61 74 69 76 65 20 53 51 ut not native SQ
31fa0 4c 69 74 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 Lite */. assert
31fb0 28 20 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 ( cursorOwnsBtSh
31fc0 61 72 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 ared(pCur) );.
31fd0 61 73 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 30 assert( flags==0
31fe0 20 7c 7c 20 66 6c 61 67 73 3d 3d 31 20 29 3b 0a || flags==1 );.
31ff0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
32000 73 6b 69 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 skipNext==0 || p
32010 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Cur->eState!=CUR
32020 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 70 SOR_VALID );. p
32030 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
32040 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 72 = 0;. pCur->cur
32050 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 5f Flags &= ~(BTCF_
32060 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 ValidNKey|BTCF_V
32070 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 69 66 28 alidOvfl);. if(
32080 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCur->eState!=C
32090 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 20 72 65 URSOR_VALID ) re
320a0 74 75 72 6e 20 62 74 72 65 65 4e 65 78 74 28 70 turn btreeNext(p
320b0 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 Cur);. pPage =
320c0 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 69 pCur->pPage;. i
320d0 66 28 20 28 2b 2b 70 43 75 72 2d 3e 69 78 29 3e f( (++pCur->ix)>
320e0 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b =pPage->nCell ){
320f0 0a 20 20 20 20 70 43 75 72 2d 3e 69 78 2d 2d 3b . pCur->ix--;
32100 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 72 65 . return btre
32110 65 4e 65 78 74 28 70 43 75 72 29 3b 0a 20 20 7d eNext(pCur);. }
32120 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 . if( pPage->le
32130 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e af ){. return
32140 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
32150 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
32160 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
32170 43 75 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a Cur);. }.}../*.
32180 2a 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 73 ** Step the curs
32190 6f 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 or to the back t
321a0 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 o the previous e
321b0 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 ntry in the data
321c0 62 61 73 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 base..** Return
321d0 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 values:.**.**
321e0 20 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 SQLITE_OK
321f0 73 75 63 63 65 73 73 0a 2a 2a 20 20 20 20 20 53 success.** S
32200 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 74 68 65 QLITE_DONE the
32210 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 cursor is alrea
32220 64 79 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 dy on the first
32230 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 74 element of the t
32240 61 62 6c 65 0a 2a 2a 20 20 20 20 20 6f 74 68 65 able.** othe
32250 72 77 69 73 65 20 20 20 20 20 73 6f 6d 65 20 6b rwise some k
32260 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 ind of error occ
32270 75 72 72 65 64 0a 2a 2a 0a 2a 2a 20 54 68 65 20 urred.**.** The
32280 6d 61 69 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 main entry point
32290 20 69 73 20 73 71 6c 69 74 65 33 42 74 72 65 65 is sqlite3Btree
322a0 50 72 65 76 69 6f 75 73 28 29 2e 20 20 54 68 61 Previous(). Tha
322b0 74 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 70 74 t routine is opt
322c0 69 6d 69 7a 65 64 0a 2a 2a 20 66 6f 72 20 74 68 imized.** for th
322d0 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 e common case of
322e0 20 6d 65 72 65 6c 79 20 64 65 63 72 65 6d 65 6e merely decremen
322f0 74 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 63 6f ting the cell co
32300 75 6e 74 65 72 20 42 74 43 75 72 73 6f 72 2e 61 unter BtCursor.a
32310 69 49 64 78 0a 2a 2a 20 74 6f 20 74 68 65 20 70 iIdx.** to the p
32320 72 65 76 69 6f 75 73 20 63 65 6c 6c 20 6f 6e 20 revious cell on
32330 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 the current page
32340 2e 20 20 54 68 65 20 28 73 6c 6f 77 65 72 29 20 . The (slower)
32350 62 74 72 65 65 50 72 65 76 69 6f 75 73 28 29 0a btreePrevious().
32360 2a 2a 20 68 65 6c 70 65 72 20 72 6f 75 74 69 6e ** helper routin
32370 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e e is called when
32380 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 it is necessary
32390 20 74 6f 20 6d 6f 76 65 20 74 6f 20 61 20 64 69 to move to a di
323a0 66 66 65 72 65 6e 74 20 70 61 67 65 0a 2a 2a 20 fferent page.**
323b0 6f 72 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 or to restore th
323c0 65 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 e cursor..**.**
323d0 49 66 20 62 69 74 20 30 78 30 31 20 6f 66 20 74 If bit 0x01 of t
323e0 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74 6f he F argument to
323f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 sqlite3BtreePre
32400 76 69 6f 75 73 28 43 2c 46 29 20 69 73 20 31 2c vious(C,F) is 1,
32410 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63 75 72 then.** the cur
32420 73 6f 72 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 sor corresponds
32430 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 20 to an SQL index
32440 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 and this routine
32450 20 63 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e could have been
32460 0a 2a 2a 20 73 6b 69 70 70 65 64 20 69 66 20 74 .** skipped if t
32470 68 65 20 53 51 4c 20 69 6e 64 65 78 20 68 61 64 he SQL index had
32480 20 62 65 65 6e 20 61 20 75 6e 69 71 75 65 20 69 been a unique i
32490 6e 64 65 78 2e 20 20 54 68 65 20 46 20 61 72 67 ndex. The F arg
324a0 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 68 69 ument is a.** hi
324b0 6e 74 20 74 6f 20 74 68 65 20 69 6d 70 6c 65 6d nt to the implem
324c0 65 6e 74 2e 20 20 54 68 65 20 6e 61 74 69 76 65 ent. The native
324d0 20 53 51 4c 69 74 65 20 62 74 72 65 65 20 69 6d SQLite btree im
324e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 plementation doe
324f0 73 20 6e 6f 74 0a 2a 2a 20 75 73 65 20 74 68 69 s not.** use thi
32500 73 20 68 69 6e 74 2c 20 62 75 74 20 43 4f 4d 44 s hint, but COMD
32510 42 32 20 64 6f 65 73 2e 0a 2a 2f 0a 73 74 61 74 B2 does..*/.stat
32520 69 63 20 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 ic SQLITE_NOINLI
32530 4e 45 20 69 6e 74 20 62 74 72 65 65 50 72 65 76 NE int btreePrev
32540 69 6f 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 ious(BtCursor *p
32550 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
32560 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
32570 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
32580 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 65 64 28 sorOwnsBtShared(
32590 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
325a0 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 t( pCur->skipNex
325b0 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 t==0 || pCur->eS
325c0 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
325d0 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ID );. assert(
325e0 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 (pCur->curFlags
325f0 26 20 28 42 54 43 46 5f 41 74 4c 61 73 74 7c 42 & (BTCF_AtLast|B
32600 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 7c 42 54 TCF_ValidOvfl|BT
32610 43 46 5f 56 61 6c 69 64 4e 4b 65 79 29 29 3d 3d CF_ValidNKey))==
32620 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
32630 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
32640 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 =0 );. if( pCur
32650 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
32660 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 63 _VALID ){. rc
32670 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
32680 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
32690 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
326a0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
326b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
326c0 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
326d0 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
326e0 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 72 65 tate ){. re
326f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
32700 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
32710 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 29 pCur->skipNext )
32720 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
32730 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
32740 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 RSOR_VALID || pC
32750 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
32760 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 3b 0a 20 OR_SKIPNEXT );.
32770 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 pCur->eStat
32780 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 e = CURSOR_VALID
32790 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 ;. if( pCur
327a0 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b 0a ->skipNext<0 ){.
327b0 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 73 6b pCur->sk
327c0 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 ipNext = 0;.
327d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
327e0 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
327f0 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 pCur->skipNe
32800 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 xt = 0;. }.
32810 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 }.. pPage = pCu
32820 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73 73 65 r->pPage;. asse
32830 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
32840 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 t );. if( !pPag
32850 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 e->leaf ){. i
32860 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 69 nt idx = pCur->i
32870 78 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 x;. rc = move
32880 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 ToChild(pCur, ge
32890 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
328a0 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 pPage, idx)));.
328b0 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
328c0 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 rn rc;. rc =
328d0 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 moveToRightmost(
328e0 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pCur);. }else{.
328f0 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d while( pCur-
32900 3e 69 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 >ix==0 ){.
32910 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
32920 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 =0 ){. pC
32930 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
32940 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 SOR_INVALID;.
32950 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
32960 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d TE_DONE;. }
32970 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 72 . moveToPar
32980 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d ent(pCur);. }
32990 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
329a0 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 r->info.nSize==0
329b0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
329c0 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 (pCur->curFlags
329d0 26 20 28 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 & (BTCF_ValidOvf
329e0 6c 29 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 70 l))==0 );.. p
329f0 43 75 72 2d 3e 69 78 2d 2d 3b 0a 20 20 20 20 70 Cur->ix--;. p
32a00 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 Page = pCur->pPa
32a10 67 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ge;. if( pPag
32a20 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 e->intKey && !pP
32a30 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
32a40 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
32a50 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75 treePrevious(pCu
32a60 72 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 r, 0);. }else
32a70 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
32a80 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
32a90 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
32aa0 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
32ab0 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 ePrevious(BtCurs
32ac0 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 66 6c or *pCur, int fl
32ad0 61 67 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ags){. assert(
32ae0 63 75 72 73 6f 72 4f 77 6e 73 42 74 53 68 61 72 cursorOwnsBtShar
32af0 65 64 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ed(pCur) );. as
32b00 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 30 20 7c sert( flags==0 |
32b10 7c 20 66 6c 61 67 73 3d 3d 31 20 29 3b 0a 20 20 | flags==1 );.
32b20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b assert( pCur->sk
32b30 69 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 43 75 ipNext==0 || pCu
32b40 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f r->eState!=CURSO
32b50 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 55 4e 55 R_VALID );. UNU
32b60 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 20 66 SED_PARAMETER( f
32b70 6c 61 67 73 20 29 3b 20 20 2f 2a 20 55 73 65 64 lags ); /* Used
32b80 20 69 6e 20 43 4f 4d 44 42 32 20 62 75 74 20 6e in COMDB2 but n
32b90 6f 74 20 6e 61 74 69 76 65 20 53 51 4c 69 74 65 ot native SQLite
32ba0 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 63 75 72 46 */. pCur->curF
32bb0 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 5f 41 lags &= ~(BTCF_A
32bc0 74 4c 61 73 74 7c 42 54 43 46 5f 56 61 6c 69 64 tLast|BTCF_Valid
32bd0 4f 76 66 6c 7c 42 54 43 46 5f 56 61 6c 69 64 4e Ovfl|BTCF_ValidN
32be0 4b 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e 69 6e Key);. pCur->in
32bf0 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
32c00 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
32c10 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 0a 20 !=CURSOR_VALID.
32c20 20 20 7c 7c 20 70 43 75 72 2d 3e 69 78 3d 3d 30 || pCur->ix==0
32c30 0a 20 20 20 7c 7c 20 70 43 75 72 2d 3e 70 50 61 . || pCur->pPa
32c40 67 65 2d 3e 6c 65 61 66 3d 3d 30 0a 20 20 29 7b ge->leaf==0. ){
32c50 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 72 65 . return btre
32c60 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 29 3b ePrevious(pCur);
32c70 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 69 78 2d . }. pCur->ix-
32c80 2d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 -;. return SQLI
32c90 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
32ca0 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
32cb0 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 age from the dat
32cc0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
32cd0 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 69 * The new page i
32ce0 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 s marked as dirt
32cf0 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f y. (In other wo
32d00 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65 rds, sqlite3Page
32d10 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 20 rWrite().** has
32d20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c already been cal
32d30 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 led on the new p
32d40 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70 age.) The new p
32d50 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 age has also.**
32d60 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20 been referenced
32d70 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 and the calling
32d80 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f routine is respo
32d90 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 nsible for calli
32da0 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 ng.** sqlite3Pag
32db0 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 erUnref() on the
32dc0 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e 20 69 new page when i
32dd0 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a t is done..**.**
32de0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
32df0 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 turned on succes
32e00 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 72 65 s. Any other re
32e10 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 turn value indic
32e20 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 ates.** an error
32e30 2e 20 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 . *ppPage is se
32e40 74 20 74 6f 20 4e 55 4c 4c 20 69 6e 20 74 68 65 t to NULL in the
32e50 20 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 event of an err
32e60 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 or..**.** If the
32e70 20 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 "nearby" parame
32e80 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 ter is not 0, th
32e90 65 6e 20 61 6e 20 65 66 66 6f 72 74 20 69 73 20 en an effort is
32ea0 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 made to .** loca
32eb0 74 65 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 te a page close
32ec0 74 6f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 to the page numb
32ed0 65 72 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 er "nearby". Th
32ee0 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 is can be used i
32ef0 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 n an.** attempt
32f00 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 to keep related
32f10 70 61 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 pages close to e
32f20 61 63 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 ach other in the
32f30 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a database file,.
32f40 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e ** which in turn
32f50 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 can make databa
32f60 73 65 20 61 63 63 65 73 73 20 66 61 73 74 65 72 se access faster
32f70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 ..**.** If the e
32f80 4d 6f 64 65 20 70 61 72 61 6d 65 74 65 72 20 69 Mode parameter i
32f90 73 20 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 s BTALLOC_EXACT
32fa0 61 6e 64 20 74 68 65 20 6e 65 61 72 62 79 20 70 and the nearby p
32fb0 61 67 65 20 65 78 69 73 74 73 0a 2a 2a 20 61 6e age exists.** an
32fc0 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 ywhere on the fr
32fd0 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74 ee-list, then it
32fe0 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
32ff0 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 o be returned.
33000 49 66 0a 2a 2a 20 65 4d 6f 64 65 20 69 73 20 42 If.** eMode is B
33010 54 41 4c 4c 4f 43 5f 4c 54 20 74 68 65 6e 20 74 TALLOC_LT then t
33020 68 65 20 70 61 67 65 20 72 65 74 75 72 6e 65 64 he page returned
33030 20 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74 68 will be less th
33040 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 an or equal.** t
33050 6f 20 6e 65 61 72 62 79 20 69 66 20 61 6e 79 20 o nearby if any
33060 73 75 63 68 20 70 61 67 65 20 65 78 69 73 74 73 such page exists
33070 2e 20 20 49 66 20 65 4d 6f 64 65 20 69 73 20 42 . If eMode is B
33080 54 41 4c 4c 4f 43 5f 41 4e 59 20 74 68 65 6e 20 TALLOC_ANY then
33090 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
330a0 72 65 73 74 72 69 63 74 69 6f 6e 73 20 6f 6e 20 restrictions on
330b0 77 68 69 63 68 20 70 61 67 65 20 69 73 20 72 65 which page is re
330c0 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
330d0 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 c int allocateBt
330e0 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 reePage(. BtSha
330f0 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 red *pBt,
33100 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a /* The btree *
33110 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
33120 50 61 67 65 2c 20 20 20 20 20 20 2f 2a 20 53 74 Page, /* St
33130 6f 72 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 ore pointer to t
33140 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 he allocated pag
33150 65 20 68 65 72 65 20 2a 2f 0a 20 20 50 67 6e 6f e here */. Pgno
33160 20 2a 70 50 67 6e 6f 2c 20 20 20 20 20 20 20 20 *pPgno,
33170 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 /* Store the
33180 70 61 67 65 20 6e 75 6d 62 65 72 20 68 65 72 65 page number here
33190 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 */. Pgno nearb
331a0 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 y, /*
331b0 53 65 61 72 63 68 20 66 6f 72 20 61 20 70 61 67 Search for a pag
331c0 65 20 6e 65 61 72 20 74 68 69 73 20 6f 6e 65 20 e near this one
331d0 2a 2f 0a 20 20 75 38 20 65 4d 6f 64 65 20 20 20 */. u8 eMode
331e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
331f0 54 41 4c 4c 4f 43 5f 45 58 41 43 54 2c 20 42 54 TALLOC_EXACT, BT
33200 41 4c 4c 4f 43 5f 4c 54 2c 20 6f 72 20 42 54 41 ALLOC_LT, or BTA
33210 4c 4c 4f 43 5f 41 4e 59 20 2a 2f 0a 29 7b 0a 20 LLOC_ANY */.){.
33220 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 MemPage *pPage1
33230 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 ;. int rc;. u3
33240 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 2 n; /* Numb
33250 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 er of pages on t
33260 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 he freelist */.
33270 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e u32 k; /* N
33280 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20 umber of leaves
33290 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20 on the trunk of
332a0 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a the freelist */.
332b0 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e MemPage *pTrun
332c0 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 k = 0;. MemPage
332d0 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 *pPrevTrunk = 0
332e0 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b ;. Pgno mxPage;
332f0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 /* Total si
33300 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
33310 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 se file */.. as
33320 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
33330 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
33340 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
33350 28 20 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 ( eMode==BTALLOC
33360 5f 41 4e 59 20 7c 7c 20 28 6e 65 61 72 62 79 3e _ANY || (nearby>
33370 30 20 26 26 20 52 45 51 55 49 52 45 5f 50 54 52 0 && REQUIRE_PTR
33380 4d 41 50 20 29 20 29 3b 0a 20 20 70 50 61 67 65 MAP ) );. pPage
33390 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
333a0 0a 20 20 6d 78 50 61 67 65 20 3d 20 62 74 72 65 . mxPage = btre
333b0 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b ePagecount(pBt);
333c0 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f . /* EVIDENCE-O
333d0 46 3a 20 52 2d 30 35 31 31 39 2d 30 32 36 33 37 F: R-05119-02637
333e0 20 54 68 65 20 34 2d 62 79 74 65 20 62 69 67 2d The 4-byte big-
333f0 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 61 endian integer a
33400 74 20 6f 66 66 73 65 74 20 33 36 0a 20 20 2a 2a t offset 36. **
33410 20 73 74 6f 72 65 73 20 73 74 6f 72 65 73 20 74 stores stores t
33420 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
33430 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 of pages on the
33440 66 72 65 65 6c 69 73 74 2e 20 2a 2f 0a 20 20 6e freelist. */. n
33450 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
33460 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b ge1->aData[36]);
33470 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d . testcase( n==
33480 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20 69 66 mxPage-1 );. if
33490 28 20 49 53 43 4f 4e 43 55 52 52 45 4e 54 3d 3d ( ISCONCURRENT==
334a0 30 20 26 26 20 6e 3e 3d 6d 78 50 61 67 65 20 29 0 && n>=mxPage )
334b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
334c0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
334d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 ;. }.. /* Ensu
334e0 72 65 20 70 61 67 65 20 31 20 69 73 20 77 72 69 re page 1 is wri
334f0 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 table. This func
33500 74 69 6f 6e 20 77 69 6c 6c 20 65 69 74 68 65 72 tion will either
33510 20 63 68 61 6e 67 65 20 74 68 65 20 6e 75 6d 62 change the numb
33520 65 72 0a 20 20 2a 2a 20 6f 66 20 70 61 67 65 73 er. ** of pages
33530 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
33540 74 20 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 t or the size of
33550 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
33560 6c 65 2e 20 53 69 6e 63 65 20 62 6f 74 68 0a 20 le. Since both.
33570 20 2a 2a 20 6f 66 20 74 68 65 73 65 20 6f 70 65 ** of these ope
33580 72 61 74 69 6f 6e 73 20 69 6e 76 6f 6c 76 65 20 rations involve
33590 6d 6f 64 69 66 79 69 6e 67 20 70 61 67 65 20 31 modifying page 1
335a0 20 68 65 61 64 65 72 20 66 69 65 6c 64 73 2c 20 header fields,
335b0 70 61 67 65 20 31 0a 20 20 2a 2a 20 77 69 6c 6c page 1. ** will
335c0 20 64 65 66 69 6e 69 74 65 6c 79 20 62 65 20 77 definitely be w
335d0 72 69 74 74 65 6e 20 62 79 20 74 68 69 73 20 74 ritten by this t
335e0 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 ransaction. If t
335f0 68 69 73 20 69 73 20 61 6e 20 43 4f 4e 43 55 52 his is an CONCUR
33600 52 45 4e 54 0a 20 20 2a 2a 20 74 72 61 6e 73 61 RENT. ** transa
33610 63 74 69 6f 6e 2c 20 65 6e 73 75 72 65 20 74 68 ction, ensure th
33620 65 20 42 74 72 65 65 50 74 72 6d 61 70 20 73 74 e BtreePtrmap st
33630 72 75 63 74 75 72 65 20 68 61 73 20 62 65 65 6e ructure has been
33640 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a allocated. */.
33650 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
33660 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d gerWrite(pPage1-
33670 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
33680 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
33690 0a 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20 .. if( n>0 ){.
336a0 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 /* There are
336b0 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 pages on the fre
336c0 65 6c 69 73 74 2e 20 20 52 65 75 73 65 20 6f 6e elist. Reuse on
336d0 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65 73 e of those pages
336e0 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 54 . */. Pgno iT
336f0 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73 65 61 runk;. u8 sea
33700 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f 2a 20 rchList = 0; /*
33710 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 If the free-list
33720 20 6d 75 73 74 20 62 65 20 73 65 61 72 63 68 65 must be searche
33730 64 20 66 6f 72 20 27 6e 65 61 72 62 79 27 20 2a d for 'nearby' *
33740 2f 0a 20 20 20 20 75 33 32 20 6e 53 65 61 72 63 /. u32 nSearc
33750 68 20 3d 20 30 3b 20 20 20 2f 2a 20 43 6f 75 6e h = 0; /* Coun
33760 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 t of the number
33770 6f 66 20 73 65 61 72 63 68 20 61 74 74 65 6d 70 of search attemp
33780 74 73 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f ts */. . /
33790 2a 20 49 66 20 65 4d 6f 64 65 3d 3d 42 54 41 4c * If eMode==BTAL
337a0 4c 4f 43 5f 45 58 41 43 54 20 61 6e 64 20 61 20 LOC_EXACT and a
337b0 71 75 65 72 79 20 6f 66 20 74 68 65 20 70 6f 69 query of the poi
337c0 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 nter-map. **
337d0 73 68 6f 77 73 20 74 68 61 74 20 74 68 65 20 70 shows that the p
337e0 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 age 'nearby' is
337f0 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 68 65 somewhere on the
33800 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e free-list, then
33810 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 . ** the enti
33820 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 20 re-list will be
33830 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 61 searched for tha
33840 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 t page.. */.
33850 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 42 54 if( eMode==BT
33860 41 4c 4c 4f 43 5f 45 58 41 43 54 20 29 7b 0a 20 ALLOC_EXACT ){.
33870 20 20 20 20 20 61 73 73 65 72 74 28 20 49 53 41 assert( ISA
33880 55 54 4f 56 41 43 55 55 4d 21 3d 49 53 43 4f 4e UTOVACUUM!=ISCON
33890 43 55 52 52 45 4e 54 20 29 3b 0a 20 20 20 20 20 CURRENT );.
338a0 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
338b0 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 M ){. if(
338c0 20 6e 65 61 72 62 79 3c 3d 6d 78 50 61 67 65 20 nearby<=mxPage
338d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 ){. u8
338e0 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 eType;.
338f0 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e assert( nearby>
33900 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 0 );. a
33910 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
33920 56 61 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 Vacuum );.
33930 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
33940 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 et(pBt, nearby,
33950 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 &eType, 0);.
33960 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
33970 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
33980 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
33990 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
339a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 65 {. se
339b0 61 72 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 archList = 1;.
339c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
339d0 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
339e0 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c . searchL
339f0 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d ist = 1;. }
33a00 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 . }else if( e
33a10 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 45 Mode==BTALLOC_LE
33a20 20 29 7b 0a 20 20 20 20 20 20 73 65 61 72 63 68 ){. search
33a30 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a List = 1;. }.
33a40 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e . /* Decremen
33a50 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 t the free-list
33a60 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 count by 1. Set
33a70 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e iTrunk to the in
33a80 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a dex of the. *
33a90 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 * first free-lis
33aa0 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 t trunk page. iP
33ab0 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 revTrunk is init
33ac0 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a ially 1.. */.
33ad0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
33ae0 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c age1->aData[36],
33af0 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 n-1);.. /* T
33b00 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 20 74 he code within t
33b10 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 his loop is run
33b20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 65 only once if the
33b30 20 27 73 65 61 72 63 68 4c 69 73 74 27 20 76 61 'searchList' va
33b40 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 riable. ** is
33b50 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 68 65 72 not true. Other
33b60 77 69 73 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e wise, it runs on
33b70 63 65 20 66 6f 72 20 65 61 63 68 20 74 72 75 6e ce for each trun
33b80 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 k-page on the.
33b90 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 ** free-list u
33ba0 6e 74 69 6c 20 74 68 65 20 70 61 67 65 20 27 6e ntil the page 'n
33bb0 65 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65 earby' is locate
33bc0 64 20 28 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f d (eMode==BTALLO
33bd0 43 5f 45 58 41 43 54 29 0a 20 20 20 20 2a 2a 20 C_EXACT). **
33be0 6f 72 20 75 6e 74 69 6c 20 61 20 70 61 67 65 20 or until a page
33bf0 6c 65 73 73 20 74 68 61 6e 20 27 6e 65 61 72 62 less than 'nearb
33c00 79 27 20 69 73 20 6c 6f 63 61 74 65 64 20 28 65 y' is located (e
33c10 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 54 Mode==BTALLOC_LT
33c20 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 ). */. do
33c30 7b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 {. pPrevTru
33c40 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 nk = pTrunk;.
33c50 20 20 20 69 66 28 20 70 50 72 65 76 54 72 75 6e if( pPrevTrun
33c60 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 k ){. /*
33c70 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 30 EVIDENCE-OF: R-0
33c80 31 35 30 36 2d 31 31 30 35 33 20 54 68 65 20 66 1506-11053 The f
33c90 69 72 73 74 20 69 6e 74 65 67 65 72 20 6f 6e 20 irst integer on
33ca0 61 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b a freelist trunk
33cb0 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
33cc0 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d is the page num
33cd0 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 ber of the next
33ce0 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 freelist trunk p
33cf0 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 20 age in the list
33d00 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 7a 65 or. ** ze
33d10 72 6f 20 69 66 20 74 68 69 73 20 69 73 20 74 68 ro if this is th
33d20 65 20 6c 61 73 74 20 66 72 65 65 6c 69 73 74 20 e last freelist
33d30 74 72 75 6e 6b 20 70 61 67 65 2e 20 2a 2f 0a 20 trunk page. */.
33d40 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 iTrunk =
33d50 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 54 get4byte(&pPrevT
33d60 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b runk->aData[0]);
33d70 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
33d80 20 20 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 /* EVIDENC
33d90 45 2d 4f 46 3a 20 52 2d 35 39 38 34 31 2d 31 33 E-OF: R-59841-13
33da0 37 39 38 20 54 68 65 20 34 2d 62 79 74 65 20 62 798 The 4-byte b
33db0 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
33dc0 72 20 61 74 20 6f 66 66 73 65 74 20 33 32 0a 20 r at offset 32.
33dd0 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 73 ** stores
33de0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
33df0 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 70 61 of the first pa
33e00 67 65 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 ge of the freeli
33e10 73 74 2c 20 6f 72 20 7a 65 72 6f 20 69 66 0a 20 st, or zero if.
33e20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 72 ** the fr
33e30 65 65 6c 69 73 74 20 69 73 20 65 6d 70 74 79 2e eelist is empty.
33e40 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 54 72 75 */. iTru
33e50 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 nk = get4byte(&p
33e60 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
33e70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
33e80 20 74 65 73 74 63 61 73 65 28 20 69 54 72 75 6e testcase( iTrun
33e90 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 k==mxPage );.
33ea0 20 20 20 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 if( iTrunk>mx
33eb0 50 61 67 65 20 7c 7c 20 6e 53 65 61 72 63 68 2b Page || nSearch+
33ec0 2b 20 3e 20 6e 20 29 7b 0a 20 20 20 20 20 20 20 + > n ){.
33ed0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
33ee0 52 55 50 54 5f 50 47 4e 4f 28 70 50 72 65 76 54 RUPT_PGNO(pPrevT
33ef0 72 75 6e 6b 20 3f 20 70 50 72 65 76 54 72 75 6e runk ? pPrevTrun
33f00 6b 2d 3e 70 67 6e 6f 20 3a 20 31 29 3b 0a 20 20 k->pgno : 1);.
33f10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
33f20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
33f30 55 6e 75 73 65 64 50 61 67 65 28 70 42 74 2c 20 UnusedPage(pBt,
33f40 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c iTrunk, &pTrunk,
33f50 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
33f60 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
33f70 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b pTrunk = 0;
33f80 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
33f90 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
33fa0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
33fb0 73 73 65 72 74 28 20 70 54 72 75 6e 6b 21 3d 30 ssert( pTrunk!=0
33fc0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
33fd0 28 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 21 ( pTrunk->aData!
33fe0 3d 30 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 45 =0 );. /* E
33ff0 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 33 VIDENCE-OF: R-13
34000 35 32 33 2d 30 34 33 39 34 20 54 68 65 20 73 65 523-04394 The se
34010 63 6f 6e 64 20 69 6e 74 65 67 65 72 20 6f 6e 20 cond integer on
34020 61 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b a freelist trunk
34030 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 page. ** i
34040 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
34050 6c 65 61 66 20 70 61 67 65 20 70 6f 69 6e 74 65 leaf page pointe
34060 72 73 20 74 6f 20 66 6f 6c 6c 6f 77 2e 20 2a 2f rs to follow. */
34070 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62 . k = get4b
34080 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 yte(&pTrunk->aDa
34090 74 61 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 ta[4]);. if
340a0 28 20 6b 3d 3d 30 20 26 26 20 21 73 65 61 72 63 ( k==0 && !searc
340b0 68 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 hList ){.
340c0 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 68 61 /* The trunk ha
340d0 73 20 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 s no leaves and
340e0 74 68 65 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 the list is not
340f0 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 2e 20 being searched.
34100 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 . ** So e
34110 78 74 72 61 63 74 20 74 68 65 20 74 72 75 6e 6b xtract the trunk
34120 20 70 61 67 65 20 69 74 73 65 6c 66 20 61 6e 64 page itself and
34130 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 6e use it as the n
34140 65 77 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a ewly . **
34150 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 allocated page
34160 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
34170 74 28 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 t( pPrevTrunk==0
34180 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d );. rc =
34190 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
341a0 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
341b0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
341c0 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
341d0 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
341e0 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
341f0 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 50 67 6e }. *pPgn
34200 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 o = iTrunk;.
34210 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
34220 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 e1->aData[32], &
34230 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
34240 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 , 4);. *p
34250 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a pPage = pTrunk;.
34260 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
34270 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 0;. TRAC
34280 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 E(("ALLOCATE: %d
34290 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 trunk - %d free
342a0 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 pages left\n",
342b0 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 *pPgno, n-1));.
342c0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b }else if( k
342d0 3e 28 75 33 32 29 28 70 42 74 2d 3e 75 73 61 62 >(u32)(pBt->usab
342e0 6c 65 53 69 7a 65 2f 34 20 2d 20 32 29 20 29 7b leSize/4 - 2) ){
342f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 . /* Valu
34300 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 e of k is out of
34310 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61 73 range. Databas
34320 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a e corruption */.
34330 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
34340 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 47 4e 4f ITE_CORRUPT_PGNO
34350 28 69 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 (iTrunk);.
34360 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
34370 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 ate_page;.#ifnde
34380 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
34390 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 7d TOVACUUM. }
343a0 65 6c 73 65 20 69 66 28 20 73 65 61 72 63 68 4c else if( searchL
343b0 69 73 74 20 0a 20 20 20 20 20 20 20 20 20 20 20 ist .
343c0 20 26 26 20 28 6e 65 61 72 62 79 3d 3d 69 54 72 && (nearby==iTr
343d0 75 6e 6b 20 7c 7c 20 28 69 54 72 75 6e 6b 3c 6e unk || (iTrunk<n
343e0 65 61 72 62 79 20 26 26 20 65 4d 6f 64 65 3d 3d earby && eMode==
343f0 42 54 41 4c 4c 4f 43 5f 4c 45 29 29 20 0a 20 20 BTALLOC_LE)) .
34400 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ){. /
34410 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 62 65 * The list is be
34420 69 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e 64 ing searched and
34430 20 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65 this trunk page
34440 20 69 73 20 74 68 65 20 70 61 67 65 0a 20 20 20 is the page.
34450 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 ** to alloc
34460 61 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 ate, regardless
34470 6f 66 20 77 68 65 74 68 65 72 20 69 74 20 68 61 of whether it ha
34480 73 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 s leaves..
34490 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 2a 70 50 */. *pP
344a0 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 gno = iTrunk;.
344b0 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
344c0 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
344d0 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a searchList = 0;.
344e0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
344f0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
34500 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
34510 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
34520 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
34530 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
34540 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
34550 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 if( k==0
34560 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
34570 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a !pPrevTrunk ){.
34580 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
34590 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
345a0 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
345b0 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
345c0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
345d0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
345e0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
345f0 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 e(pPrevTrunk->pD
34600 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
34610 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
34620 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
34630 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
34640 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
34650 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
34660 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
34670 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
34680 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e ta[0], &pTrunk->
34690 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
346a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
346b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
346c0 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 /* The trunk
346d0 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 page is required
346e0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 by the caller b
346f0 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a ut it contains .
34700 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 ** poi
34710 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 nters to free-li
34720 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 st leaves. The f
34730 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 irst leaf become
34740 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 s a trunk.
34750 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 ** page in t
34760 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 his case..
34770 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
34780 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 MemPage *pNewTr
34790 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 unk;. P
347a0 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 gno iNewTrunk =
347b0 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b get4byte(&pTrunk
347c0 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 ->aData[8]);.
347d0 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 if( iNewT
347e0 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a runk>mxPage ){ .
347f0 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
34800 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
34810 50 47 4e 4f 28 69 54 72 75 6e 6b 29 3b 0a 20 20 PGNO(iTrunk);.
34820 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
34830 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
34840 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
34850 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
34860 28 20 69 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 50 ( iNewTrunk==mxP
34870 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 age );.
34880 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 55 6e rc = btreeGetUn
34890 75 73 65 64 50 61 67 65 28 70 42 74 2c 20 69 4e usedPage(pBt, iN
348a0 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 ewTrunk, &pNewTr
348b0 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 unk, 0);.
348c0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
348d0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
348e0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
348f0 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
34900 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
34910 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
34920 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 75 gerWrite(pNewTru
34930 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
34940 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
34950 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
34960 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 release
34970 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b Page(pNewTrunk);
34980 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
34990 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
349a0 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d age;. }
349b0 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
349c0 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 y(&pNewTrunk->aD
349d0 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[0], &pTrunk-
349e0 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 >aData[0], 4);.
349f0 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
34a00 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 e(&pNewTrunk->aD
34a10 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 ata[4], k-1);.
34a20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
34a30 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 pNewTrunk->aData
34a40 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 [8], &pTrunk->aD
34a50 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 ata[12], (k-1)*4
34a60 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c );. rel
34a70 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 easePage(pNewTru
34a80 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nk);. i
34a90 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 f( !pPrevTrunk )
34aa0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 {. as
34ab0 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
34ac0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
34ad0 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29 age1->pDbPage) )
34ae0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 ;. pu
34af0 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
34b00 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 aData[32], iNewT
34b10 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 runk);.
34b20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
34b30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
34b40 50 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 PagerWrite(pPrev
34b50 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
34b60 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
34b70 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
34b80 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
34b90 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
34ba0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
34bb0 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
34bc0 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
34bd0 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b ta[0], iNewTrunk
34be0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
34bf0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
34c00 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 pTrunk = 0;.
34c10 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
34c20 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 OCATE: %d trunk
34c30 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 - %d free pages
34c40 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c left\n", *pPgno,
34c50 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 n-1));.#endif.
34c60 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b }else if( k
34c70 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a >0 ){. /*
34c80 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 66 20 Extract a leaf
34c90 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a from the trunk *
34ca0 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6c /. u32 cl
34cb0 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20 50 osest;. P
34cc0 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 20 gno iPage;.
34cd0 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
34ce0 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e 6b *aData = pTrunk
34cf0 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 20 ->aData;.
34d00 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b if( nearby>0 ){
34d10 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 69 . u32 i
34d20 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 ;. clos
34d30 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 est = 0;.
34d40 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 42 54 if( eMode==BT
34d50 41 4c 4c 4f 43 5f 4c 45 20 29 7b 0a 20 20 20 20 ALLOC_LE ){.
34d60 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
34d70 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<k; i++){.
34d80 20 20 20 20 20 20 20 20 20 20 69 50 61 67 65 20 iPage
34d90 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 = get4byte(&aDat
34da0 61 5b 38 2b 69 2a 34 5d 29 3b 0a 20 20 20 20 20 a[8+i*4]);.
34db0 20 20 20 20 20 20 20 20 20 69 66 28 20 69 50 61 if( iPa
34dc0 67 65 3c 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 ge<=nearby ){.
34dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c cl
34de0 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 20 osest = i;.
34df0 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
34e00 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
34e10 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
34e20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
34e30 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
34e40 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 dist;.
34e50 20 20 20 64 69 73 74 20 3d 20 73 71 6c 69 74 65 dist = sqlite
34e60 33 41 62 73 49 6e 74 33 32 28 67 65 74 34 62 79 3AbsInt32(get4by
34e70 74 65 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 te(&aData[8]) -
34e80 6e 65 61 72 62 79 29 3b 0a 20 20 20 20 20 20 20 nearby);.
34e90 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c for(i=1; i<
34ea0 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 k; i++){.
34eb0 20 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 int d2 =
34ec0 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 32 28 sqlite3AbsInt32(
34ed0 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b get4byte(&aData[
34ee0 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 8+i*4]) - nearby
34ef0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
34f00 20 69 66 28 20 64 32 3c 64 69 73 74 20 29 7b 0a if( d2<dist ){.
34f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34f20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 closest = i;.
34f30 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73 dis
34f40 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 t = d2;.
34f50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
34f60 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
34f70 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
34f80 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 . close
34f90 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 st = 0;.
34fa0 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 }.. iPage
34fb0 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 = get4byte(&aDa
34fc0 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 ta[8+closest*4])
34fd0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
34fe0 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 se( iPage==mxPag
34ff0 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
35000 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b iPage>mxPage ){
35010 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
35020 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 50 SQLITE_CORRUPT_P
35030 47 4e 4f 28 69 54 72 75 6e 6b 29 3b 0a 20 20 20 GNO(iTrunk);.
35040 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
35050 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
35060 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
35070 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67 65 testcase( iPage
35080 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 ==mxPage );.
35090 20 20 20 20 69 66 28 20 21 73 65 61 72 63 68 4c if( !searchL
350a0 69 73 74 20 0a 20 20 20 20 20 20 20 20 20 7c 7c ist . ||
350b0 20 28 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 (iPage==nearby
350c0 7c 7c 20 28 69 50 61 67 65 3c 6e 65 61 72 62 79 || (iPage<nearby
350d0 20 26 26 20 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c && eMode==BTALL
350e0 4f 43 5f 4c 45 29 29 20 0a 20 20 20 20 20 20 20 OC_LE)) .
350f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
35100 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 t noContent;.
35110 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 *pPgno =
35120 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 iPage;.
35130 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 TRACE(("ALLOCAT
35140 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 25 E: %d was leaf %
35150 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b d of %d on trunk
35160 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 20 %d".
35170 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 65 ": %d more
35180 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a free pages\n",.
35190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
351a0 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 *pPgno, closest
351b0 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 +1, k, pTrunk->p
351c0 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 gno, n-1));.
351d0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
351e0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
351f0 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
35200 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 if( rc
35210 29 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 ) goto end_alloc
35220 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
35230 20 20 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c if( closest<
35240 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 k-1 ){.
35250 20 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 memcpy(&aData
35260 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 [8+closest*4], &
35270 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 aData[4+k*4], 4)
35280 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
35290 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
352a0 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 (&aData[4], k-1)
352b0 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 6f ;. noCo
352c0 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 65 ntent = !btreeGe
352d0 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c tHasContent(pBt,
352e0 20 2a 70 50 67 6e 6f 29 3f 20 50 41 47 45 52 5f *pPgno)? PAGER_
352f0 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 3a 20 GET_NOCONTENT :
35300 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 0;. rc
35310 3d 20 62 74 72 65 65 47 65 74 55 6e 75 73 65 64 = btreeGetUnused
35320 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f Page(pBt, *pPgno
35330 2c 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 , ppPage, noCont
35340 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ent);.
35350 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
35360 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
35370 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
35380 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 erWrite((*ppPage
35390 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 )->pDbPage);.
353a0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
353b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
353c0 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 rele
353d0 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 asePage(*ppPage)
353e0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
353f0 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 *ppPage = 0;.
35400 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
35410 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
35420 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b searchList = 0;
35430 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
35440 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 }. release
35450 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 Page(pPrevTrunk)
35460 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 ;. pPrevTru
35470 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 nk = 0;. }whi
35480 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 le( searchList )
35490 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
354a0 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 * There are no p
354b0 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ages on the free
354c0 6c 69 73 74 2c 20 73 6f 20 61 70 70 65 6e 64 20 list, so append
354d0 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 a new page to th
354e0 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 e. ** databas
354f0 65 20 69 6d 61 67 65 2e 0a 20 20 20 20 2a 2a 0a e image.. **.
35500 20 20 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c ** Normally,
35510 20 6e 65 77 20 70 61 67 65 73 20 61 6c 6c 6f 63 new pages alloc
35520 61 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f ated by this blo
35530 63 6b 20 63 61 6e 20 62 65 20 72 65 71 75 65 73 ck can be reques
35540 74 65 64 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 ted from the.
35550 20 2a 2a 20 70 61 67 65 72 20 6c 61 79 65 72 20 ** pager layer
35560 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e with the 'no-con
35570 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20 tent' flag set.
35580 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 74 68 This prevents th
35590 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 66 e pager. ** f
355a0 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 72 65 rom trying to re
355b0 61 64 20 74 68 65 20 70 61 67 65 73 20 63 6f 6e ad the pages con
355c0 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 6b 2e 20 tent from disk.
355d0 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 0a However, if the.
355e0 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 74 ** current t
355f0 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 ransaction has a
35600 6c 72 65 61 64 79 20 72 75 6e 20 6f 6e 65 20 6f lready run one o
35610 72 20 6d 6f 72 65 20 69 6e 63 72 65 6d 65 6e 74 r more increment
35620 61 6c 2d 76 61 63 75 75 6d 0a 20 20 20 20 2a 2a al-vacuum. **
35630 20 73 74 65 70 73 2c 20 74 68 65 6e 20 74 68 65 steps, then the
35640 20 70 61 67 65 20 77 65 20 61 72 65 20 61 62 6f page we are abo
35650 75 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d ut to allocate m
35660 61 79 20 63 6f 6e 74 61 69 6e 20 63 6f 6e 74 65 ay contain conte
35670 6e 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69 nt. ** that i
35680 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 s required in th
35690 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 6f 6c e event of a rol
356a0 6c 62 61 63 6b 2e 20 49 6e 20 74 68 69 73 20 63 lback. In this c
356b0 61 73 65 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e ase, do. ** n
356c0 6f 74 20 73 65 74 20 74 68 65 20 6e 6f 2d 63 6f ot set the no-co
356d0 6e 74 65 6e 74 20 66 6c 61 67 2e 20 54 68 69 73 ntent flag. This
356e0 20 63 61 75 73 65 73 20 74 68 65 20 70 61 67 65 causes the page
356f0 72 20 74 6f 20 6c 6f 61 64 20 61 6e 64 20 6a 6f r to load and jo
35700 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 74 68 65 urnal. ** the
35710 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f current page co
35720 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 6f 76 65 ntent before ove
35730 72 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 rwriting it..
35740 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 **. ** Note
35750 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 77 that the pager w
35760 69 6c 6c 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 ill not actually
35770 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 attempt to load
35780 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 20 or journal .
35790 20 2a 2a 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 ** content for
357a0 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 72 65 any page that re
357b0 61 6c 6c 79 20 64 6f 65 73 20 6c 69 65 20 70 61 ally does lie pa
357c0 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 st the end of th
357d0 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a e database. *
357e0 2a 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 * file on disk.
357f0 53 6f 20 74 68 65 20 65 66 66 65 63 74 73 20 6f So the effects o
35800 66 20 64 69 73 61 62 6c 69 6e 67 20 74 68 65 20 f disabling the
35810 6e 6f 2d 63 6f 6e 74 65 6e 74 20 6f 70 74 69 6d no-content optim
35820 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 ization. ** h
35830 65 72 65 20 61 72 65 20 63 6f 6e 66 69 6e 65 64 ere are confined
35840 20 74 6f 20 74 68 6f 73 65 20 70 61 67 65 73 20 to those pages
35850 74 68 61 74 20 6c 69 65 20 62 65 74 77 65 65 6e that lie between
35860 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a the end of the.
35870 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
35880 69 6d 61 67 65 20 61 6e 64 20 74 68 65 20 65 6e image and the en
35890 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 d of the databas
358a0 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 e file.. */.
358b0 20 20 20 69 6e 74 20 62 4e 6f 43 6f 6e 74 65 6e int bNoConten
358c0 74 20 3d 20 28 30 3d 3d 49 66 4e 6f 74 4f 6d 69 t = (0==IfNotOmi
358d0 74 41 56 28 70 42 74 2d 3e 62 44 6f 54 72 75 6e tAV(pBt->bDoTrun
358e0 63 61 74 65 29 29 3f 20 50 41 47 45 52 5f 47 45 cate))? PAGER_GE
358f0 54 5f 4e 4f 43 4f 4e 54 45 4e 54 3a 30 3b 0a 0a T_NOCONTENT:0;..
35900 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
35910 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
35920 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
35930 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
35940 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 42 eturn rc;. pB
35950 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 t->nPage++;.
35960 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d if( pBt->nPage==
35970 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
35980 45 28 70 42 74 29 20 29 20 70 42 74 2d 3e 6e 50 E(pBt) ) pBt->nP
35990 61 67 65 2b 2b 3b 0a 0a 23 69 66 6e 64 65 66 20 age++;..#ifndef
359a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
359b0 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
359c0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 Bt->autoVacuum &
359d0 26 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 & PTRMAP_ISPAGE(
359e0 70 42 74 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 pBt, pBt->nPage)
359f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
35a00 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f *pPgno refers to
35a10 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 a pointer-map p
35a20 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 age, allocate tw
35a30 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 o new pages.
35a40 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 ** at the end
35a50 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 of the file inst
35a60 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 ead of one. The
35a70 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 first allocated
35a80 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 page. ** be
35a90 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e comes a new poin
35aa0 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 ter-map page, th
35ab0 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 e second is used
35ac0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a by the caller..
35ad0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d */. M
35ae0 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b emPage *pPg = 0;
35af0 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 . TRACE(("A
35b00 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d LLOCATE: %d from
35b10 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f end of file (po
35b20 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c inter-map page)\
35b30 6e 22 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 29 n", pBt->nPage))
35b40 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
35b50 70 42 74 2d 3e 6e 50 61 67 65 21 3d 50 45 4e 44 pBt->nPage!=PEND
35b60 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
35b70 74 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d t) );. rc =
35b80 20 62 74 72 65 65 47 65 74 55 6e 75 73 65 64 50 btreeGetUnusedP
35b90 61 67 65 28 70 42 74 2c 20 70 42 74 2d 3e 6e 50 age(pBt, pBt->nP
35ba0 61 67 65 2c 20 26 70 50 67 2c 20 62 4e 6f 43 6f age, &pPg, bNoCo
35bb0 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 ntent);. if
35bc0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
35bd0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
35be0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
35bf0 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b e(pPg->pDbPage);
35c00 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
35c10 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
35c20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 }. if( rc
35c30 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
35c40 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b pBt->nPage++;
35c50 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
35c60 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e 47 5f 42 nPage==PENDING_B
35c70 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
35c80 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b 20 7d pBt->nPage++; }
35c90 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
35ca0 20 20 70 75 74 34 62 79 74 65 28 32 38 20 2b 20 put4byte(28 +
35cb0 28 75 38 2a 29 70 42 74 2d 3e 70 50 61 67 65 31 (u8*)pBt->pPage1
35cc0 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 6e 50 ->aData, pBt->nP
35cd0 61 67 65 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f age);. *pPgno
35ce0 20 3d 20 70 42 74 2d 3e 6e 50 61 67 65 3b 0a 0a = pBt->nPage;..
35cf0 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 assert( *pPg
35d00 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 no!=PENDING_BYTE
35d10 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 _PAGE(pBt) );.
35d20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 55 rc = btreeGetU
35d30 6e 75 73 65 64 50 61 67 65 28 70 42 74 2c 20 2a nusedPage(pBt, *
35d40 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 62 pPgno, ppPage, b
35d50 4e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 NoContent);.
35d60 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
35d70 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c rc;. rc = sql
35d80 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28 ite3PagerWrite((
35d90 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 *ppPage)->pDbPag
35da0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
35db0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
35dc0 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
35dd0 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 2a ppPage);. *
35de0 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 ppPage = 0;.
35df0 7d 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 4c }. TRACE(("AL
35e00 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 LOCATE: %d from
35e10 65 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 end of file\n",
35e20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 *pPgno));. }..
35e30 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 assert( *pPgno!
35e40 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
35e50 47 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f GE(pBt) );..end_
35e60 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 allocate_page:.
35e70 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 releasePage(pTr
35e80 75 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 unk);. releaseP
35e90 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b age(pPrevTrunk);
35ea0 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
35eb0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 QLITE_OK || sqli
35ec0 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
35ed0 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e ount((*ppPage)->
35ee0 70 44 62 50 61 67 65 29 3c 3d 31 20 29 3b 0a 20 pDbPage)<=1 );.
35ef0 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
35f00 49 54 45 5f 4f 4b 20 7c 7c 20 28 2a 70 70 50 61 ITE_OK || (*ppPa
35f10 67 65 29 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 29 ge)->isInit==0 )
35f20 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
35f30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
35f40 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
35f50 20 61 64 64 20 70 61 67 65 20 69 50 61 67 65 20 add page iPage
35f60 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
35f70 66 69 6c 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 file free-list.
35f80 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 .** It is assume
35f90 64 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 d that the page
35fa0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 is not already a
35fb0 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 72 65 part of the fre
35fc0 65 2d 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 e-list..**.** Th
35fd0 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 e value passed a
35fe0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
35ff0 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
36000 6e 63 74 69 6f 6e 20 69 73 20 6f 70 74 69 6f 6e nction is option
36010 61 6c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 al..** If the ca
36020 6c 6c 65 72 20 68 61 70 70 65 6e 73 20 74 6f 20 ller happens to
36030 68 61 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 have a pointer t
36040 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 o the MemPage ob
36050 6a 65 63 74 20 0a 2a 2a 20 63 6f 72 72 65 73 70 ject .** corresp
36060 6f 6e 64 69 6e 67 20 74 6f 20 70 61 67 65 20 69 onding to page i
36070 50 61 67 65 20 68 61 6e 64 79 2c 20 69 74 20 6d Page handy, it m
36080 61 79 20 70 61 73 73 20 69 74 20 61 73 20 74 68 ay pass it as th
36090 65 20 73 65 63 6f 6e 64 20 76 61 6c 75 65 2e 20 e second value.
360a0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 .** Otherwise, i
360b0 74 20 6d 61 79 20 70 61 73 73 20 4e 55 4c 4c 2e t may pass NULL.
360c0 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 70 6f 69 6e .**.** If a poin
360d0 74 65 72 20 74 6f 20 61 20 4d 65 6d 50 61 67 65 ter to a MemPage
360e0 20 6f 62 6a 65 63 74 20 69 73 20 70 61 73 73 65 object is passe
360f0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
36100 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 69 74 73 argument,.** its
36110 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
36120 20 69 73 20 6e 6f 74 20 61 6c 74 65 72 65 64 20 is not altered
36130 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
36140 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
36150 66 72 65 65 50 61 67 65 32 28 42 74 53 68 61 72 freePage2(BtShar
36160 65 64 20 2a 70 42 74 2c 20 4d 65 6d 50 61 67 65 ed *pBt, MemPage
36170 20 2a 70 4d 65 6d 50 61 67 65 2c 20 50 67 6e 6f *pMemPage, Pgno
36180 20 69 50 61 67 65 29 7b 0a 20 20 4d 65 6d 50 61 iPage){. MemPa
36190 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 20 ge *pTrunk = 0;
361a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
361b0 2a 20 46 72 65 65 2d 6c 69 73 74 20 74 72 75 6e * Free-list trun
361c0 6b 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f k page */. Pgno
361d0 20 69 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 iTrunk = 0;
361e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
361f0 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f /* Page number o
36200 66 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e f free-list trun
36210 6b 20 70 61 67 65 20 2a 2f 20 0a 20 20 4d 65 6d k page */ . Mem
36220 50 61 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 Page *pPage1 = p
36230 42 74 2d 3e 70 50 61 67 65 31 3b 20 20 20 20 20 Bt->pPage1;
36240 20 2f 2a 20 4c 6f 63 61 6c 20 72 65 66 65 72 65 /* Local refere
36250 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f nce to page 1 */
36260 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
36270 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
36280 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 62 /* Page b
36290 65 69 6e 67 20 66 72 65 65 64 2e 20 4d 61 79 20 eing freed. May
362a0 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e be NULL. */. in
362b0 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
362c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
362d0 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 /* Return Code
362e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 65 65 3b */. int nFree;
362f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 /* Ini
36310 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 tial number of p
36320 61 67 65 73 20 6f 6e 20 66 72 65 65 2d 6c 69 73 ages on free-lis
36330 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
36340 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
36350 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
36360 3b 0a 20 20 61 73 73 65 72 74 28 20 43 4f 52 52 ;. assert( CORR
36370 55 50 54 5f 44 42 20 7c 7c 20 69 50 61 67 65 3e UPT_DB || iPage>
36380 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 1 );. assert( !
36390 70 4d 65 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d pMemPage || pMem
363a0 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 Page->pgno==iPag
363b0 65 20 29 3b 0a 0a 20 20 69 66 28 20 69 50 61 67 e );.. if( iPag
363c0 65 3c 32 20 29 20 72 65 74 75 72 6e 20 53 51 4c e<2 ) return SQL
363d0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
363e0 3b 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 ;. if( pMemPage
363f0 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
36400 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 pMemPage;. sq
36410 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 lite3PagerRef(pP
36420 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
36430 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
36440 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f e = btreePageLoo
36450 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b kup(pBt, iPage);
36460 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 . }.. /* Incre
36470 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 ment the free pa
36480 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 ge count on pPag
36490 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c e1 */. rc = sql
364a0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
364b0 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
364c0 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
364d0 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
364e0 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
364f0 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
36500 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 [36]);. put4byt
36510 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
36520 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a [36], nFree+1);.
36530 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 . if( pBt->btsF
36540 6c 61 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 lags & BTS_SECUR
36550 45 5f 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 E_DELETE ){.
36560 2f 2a 20 49 66 20 74 68 65 20 73 65 63 75 72 65 /* If the secure
36570 5f 64 65 6c 65 74 65 20 6f 70 74 69 6f 6e 20 69 _delete option i
36580 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 0a s enabled, then.
36590 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 ** always fu
365a0 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 lly overwrite de
365b0 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f leted informatio
365c0 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 n with zeros..
365d0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 70 */. if( (!p
365e0 50 61 67 65 20 26 26 20 28 28 72 63 20 3d 20 62 Page && ((rc = b
365f0 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
36600 20 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 iPage, &pPage,
36610 30 29 29 21 3d 30 29 20 29 0a 20 20 20 20 20 7c 0))!=0) ). |
36620 7c 20 20 20 20 20 20 20 20 20 20 20 20 28 28 72 | ((r
36630 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
36640 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
36650 50 61 67 65 29 29 21 3d 30 29 0a 20 20 20 20 29 Page))!=0). )
36660 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 {. goto fre
36670 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d epage_out;. }
36680 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 . memset(pPag
36690 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 e->aData, 0, pPa
366a0 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a ge->pBt->pageSiz
366b0 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 e);. }.. /* If
366c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 the database su
366d0 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 pports auto-vacu
366e0 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65 6e 74 um, write an ent
366f0 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 ry in the pointe
36700 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e r-map. ** to in
36710 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 dicate that the
36720 70 61 67 65 20 69 73 20 66 72 65 65 2e 0a 20 20 page is free..
36730 2a 2f 0a 20 20 69 66 28 20 52 45 51 55 49 52 45 */. if( REQUIRE
36740 5f 50 54 52 4d 41 50 20 29 7b 0a 20 20 20 20 70 _PTRMAP ){. p
36750 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69 50 trmapPut(pBt, iP
36760 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 age, PTRMAP_FREE
36770 50 41 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 PAGE, 0, &rc);.
36780 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
36790 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
367a0 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e }.. /* Now man
367b0 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74 75 ipulate the actu
367c0 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65 65 al database free
367d0 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65 2e -list structure.
367e0 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a 20 There are two.
367f0 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69 65 ** possibilitie
36800 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c s. If the free-l
36810 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ist is currently
36820 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74 68 empty, or if th
36830 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72 75 e first. ** tru
36840 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 nk page in the f
36850 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c ree-list is full
36860 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 , then this page
36870 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 will become a.
36880 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 ** new free-lis
36890 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74 t trunk page. Ot
368a0 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c 6c herwise, it will
368b0 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f become a leaf o
368c0 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 f the. ** first
368d0 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 trunk page in t
368e0 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65 2d he current free-
368f0 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b list. This block
36900 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20 2a tests if it. *
36910 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f * is possible to
36920 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61 73 add the page as
36930 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 a new free-list
36940 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 leaf.. */. if
36950 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20 20 ( nFree!=0 ){.
36960 20 20 75 33 32 20 6e 4c 65 61 66 3b 20 20 20 20 u32 nLeaf;
36970 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
36980 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 nitial number of
36990 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74 leaf cells on t
369a0 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20 runk page */..
369b0 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 iTrunk = get4b
369c0 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
369d0 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20 ta[32]);. rc
369e0 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
369f0 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 Bt, iTrunk, &pTr
36a00 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 unk, 0);. if(
36a10 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
36a20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 {. goto fre
36a30 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d epage_out;. }
36a40 0a 0a 20 20 20 20 6e 4c 65 61 66 20 3d 20 67 65 .. nLeaf = ge
36a50 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
36a60 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20 61 aData[4]);. a
36a70 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 ssert( pBt->usab
36a80 6c 65 53 69 7a 65 3e 33 32 20 29 3b 0a 20 20 20 leSize>32 );.
36a90 20 69 66 28 20 6e 4c 65 61 66 20 3e 20 28 75 33 if( nLeaf > (u3
36aa0 32 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 2)pBt->usableSiz
36ab0 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 e/4 - 2 ){.
36ac0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
36ad0 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
36ae0 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
36af0 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ut;. }. if
36b00 28 20 6e 4c 65 61 66 20 3c 20 28 75 33 32 29 70 ( nLeaf < (u32)p
36b10 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 Bt->usableSize/4
36b20 20 2d 20 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a - 8 ){. /*
36b30 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
36b40 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 ere is room on t
36b50 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 74 6f he trunk page to
36b60 20 69 6e 73 65 72 74 20 74 68 65 20 70 61 67 65 insert the page
36b70 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 . ** being
36b80 66 72 65 65 64 20 61 73 20 61 20 6e 65 77 20 6c freed as a new l
36b90 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 eaf.. **.
36ba0 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 ** Note that
36bb0 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 the trunk page
36bc0 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 66 75 is not really fu
36bd0 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 6f 6e 74 ll until it cont
36be0 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a 20 75 73 ains. ** us
36bf0 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 65 ableSize/4 - 2 e
36c00 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75 73 61 62 ntries, not usab
36c10 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 leSize/4 - 8 ent
36c20 72 69 65 73 20 61 73 20 77 65 20 68 61 76 65 0a ries as we have.
36c30 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 64 2e 20 ** coded.
36c40 20 42 75 74 20 64 75 65 20 74 6f 20 61 20 63 6f But due to a co
36c50 64 69 6e 67 20 65 72 72 6f 72 20 69 6e 20 76 65 ding error in ve
36c60 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
36c70 20 70 72 69 6f 72 20 74 6f 0a 20 20 20 20 20 20 prior to.
36c80 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74 61 62 61 ** 3.6.0, databa
36c90 73 65 73 20 77 69 74 68 20 66 72 65 65 6c 69 73 ses with freelis
36ca0 74 20 74 72 75 6e 6b 20 70 61 67 65 73 20 68 6f t trunk pages ho
36cb0 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 0a lding more than.
36cc0 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 ** usableS
36cd0 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 ize/4 - 8 entrie
36ce0 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 s will be report
36cf0 65 64 20 61 73 20 63 6f 72 72 75 70 74 2e 20 20 ed as corrupt.
36d00 49 6e 20 6f 72 64 65 72 0a 20 20 20 20 20 20 2a In order. *
36d10 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62 61 * to maintain ba
36d20 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
36d30 69 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 ility with older
36d40 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
36d50 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 ite,. ** we
36d60 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 will continue t
36d70 6f 20 72 65 73 74 72 69 63 74 20 74 68 65 20 6e o restrict the n
36d80 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
36d90 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 to usableSize/4
36da0 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20 66 6f - 8. ** fo
36db0 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d 65 20 r now. At some
36dc0 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 point in the fut
36dd0 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72 79 6f ure (once everyo
36de0 6e 65 20 68 61 73 20 75 70 67 72 61 64 65 64 0a ne has upgraded.
36df0 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e 36 2e ** to 3.6.
36e00 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65 20 73 0 or later) we s
36e10 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72 20 66 hould consider f
36e20 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64 69 74 ixing the condit
36e30 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20 20 20 ional above.
36e40 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22 75 73 ** to read "us
36e50 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20 69 6e ableSize/4-2" in
36e60 73 74 65 61 64 20 6f 66 20 22 75 73 61 62 6c 65 stead of "usable
36e70 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20 20 20 Size/4-8"..
36e80 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 45 56 49 **. ** EVI
36e90 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 39 39 32 DENCE-OF: R-1992
36ea0 30 2d 31 31 35 37 36 20 48 6f 77 65 76 65 72 2c 0-11576 However,
36eb0 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 newer versions
36ec0 6f 66 20 53 51 4c 69 74 65 20 73 74 69 6c 6c 0a of SQLite still.
36ed0 20 20 20 20 20 20 2a 2a 20 61 76 6f 69 64 20 75 ** avoid u
36ee0 73 69 6e 67 20 74 68 65 20 6c 61 73 74 20 73 69 sing the last si
36ef0 78 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 x entries in the
36f00 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 freelist trunk
36f10 70 61 67 65 20 61 72 72 61 79 20 69 6e 0a 20 20 page array in.
36f20 20 20 20 20 2a 2a 20 6f 72 64 65 72 20 74 68 61 ** order tha
36f30 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 t database files
36f40 20 63 72 65 61 74 65 64 20 62 79 20 6e 65 77 65 created by newe
36f50 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 r versions of SQ
36f60 4c 69 74 65 20 63 61 6e 20 62 65 0a 20 20 20 20 Lite can be.
36f70 20 20 2a 2a 20 72 65 61 64 20 62 79 20 6f 6c 64 ** read by old
36f80 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 er versions of S
36f90 51 4c 69 74 65 2e 0a 20 20 20 20 20 20 2a 2f 0a QLite.. */.
36fa0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
36fb0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
36fc0 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
36fd0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
36fe0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
36ff0 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72 75 put4byte(&pTru
37000 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c nk->aData[4], nL
37010 65 61 66 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 eaf+1);.
37020 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b put4byte(&pTrunk
37030 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a ->aData[8+nLeaf*
37040 34 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 4], iPage);.
37050 20 20 20 20 69 66 28 20 70 50 61 67 65 20 26 26 if( pPage &&
37060 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 (pBt->btsFlags
37070 26 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c & BTS_SECURE_DEL
37080 45 54 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ETE)==0 ){.
37090 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
370a0 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 rDontWrite(pPage
370b0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
370c0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
370d0 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f = btreeSetHasCo
370e0 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65 ntent(pBt, iPage
370f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
37100 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 TRACE(("FREE-PA
37110 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 GE: %d leaf on t
37120 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c runk page %d\n",
37130 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 pPage->pgno,pTru
37140 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 nk->pgno));.
37150 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
37160 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a out;. }. }..
37170 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
37180 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f flows to this po
37190 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 int, then it was
371a0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
371b0 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 add the. ** th
371c0 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 e page being fre
371d0 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67 ed as a leaf pag
371e0 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 e of the first t
371f0 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 runk in the free
37200 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 -list.. ** Poss
37210 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 ibly because the
37220 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d free-list is em
37230 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 pty, or possibly
37240 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20 because the .
37250 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 ** first trunk i
37260 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
37270 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 is full. Either
37280 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65 way, the page be
37290 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 ing freed. ** w
372a0 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e ill become the n
372b0 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ew first trunk p
372c0 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d age in the free-
372d0 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
372e0 20 70 50 61 67 65 3d 3d 30 20 26 26 20 53 51 4c pPage==0 && SQL
372f0 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 62 74 ITE_OK!=(rc = bt
37300 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
37310 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 iPage, &pPage, 0
37320 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 )) ){. goto f
37330 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d reepage_out;. }
37340 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
37350 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
37360 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
37370 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
37380 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 {. goto freep
37390 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 age_out;. }. p
373a0 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 ut4byte(pPage->a
373b0 44 61 74 61 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 Data, iTrunk);.
373c0 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
373d0 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a ->aData[4], 0);.
373e0 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 put4byte(&pPag
373f0 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 e1->aData[32], i
37400 50 61 67 65 29 3b 0a 20 20 54 52 41 43 45 28 28 Page);. TRACE((
37410 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e "FREE-PAGE: %d n
37420 65 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65 ew trunk page re
37430 70 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70 placing %d\n", p
37440 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 Page->pgno, iTru
37450 6e 6b 29 29 3b 0a 0a 66 72 65 65 70 61 67 65 5f nk));..freepage_
37460 6f 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65 out:. if( pPage
37470 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
37480 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 sInit = 0;. }.
37490 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
374a0 67 65 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 ge);. releasePa
374b0 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 ge(pTrunk);. re
374c0 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 turn rc;.}.stati
374d0 63 20 76 6f 69 64 20 66 72 65 65 50 61 67 65 28 c void freePage(
374e0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
374f0 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 int *pRC){. if(
37500 20 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f (*pRC)==SQLITE_
37510 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d OK ){. *pRC =
37520 20 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65 freePage2(pPage
37530 2d 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50 ->pBt, pPage, pP
37540 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a age->pgno);. }.
37550 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e }../*.** Free an
37560 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 y overflow pages
37570 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
37580 20 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e the given Cell.
37590 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 6c Write the.** l
375a0 6f 63 61 6c 20 43 65 6c 6c 20 73 69 7a 65 20 28 ocal Cell size (
375b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
375c0 74 65 73 20 6f 6e 20 74 68 65 20 6f 72 69 67 69 tes on the origi
375d0 6e 61 6c 20 70 61 67 65 2c 20 6f 6d 69 74 74 69 nal page, omitti
375e0 6e 67 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77 29 20 ng.** overflow)
375f0 69 6e 74 6f 20 2a 70 6e 53 69 7a 65 2e 0a 2a 2f into *pnSize..*/
37600 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 .static int clea
37610 72 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 rCell(. MemPage
37620 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
37630 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 /* The page th
37640 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
37650 43 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e Cell */. unsign
37660 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 ed char *pCell,
37670 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
37680 20 6f 66 20 74 68 65 20 43 65 6c 6c 20 2a 2f 0a of the Cell */.
37690 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 CellInfo *pInf
376a0 6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 o /* Si
376b0 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ze information a
376c0 62 6f 75 74 20 74 68 65 20 63 65 6c 6c 20 2a 2f bout the cell */
376d0 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a .){. BtShared *
376e0 70 42 74 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c pBt;. Pgno ovfl
376f0 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Pgno;. int rc;.
37700 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75 int nOvfl;. u
37710 33 32 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 32 ovflPageSize;
37720 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
37730 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
37740 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
37750 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 78 50 ) );. pPage->xP
37760 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 arseCell(pPage,
37770 70 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 20 pCell, pInfo);.
37780 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 if( pInfo->nLoc
37790 61 6c 3d 3d 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c al==pInfo->nPayl
377a0 6f 61 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 oad ){. retur
377b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a n SQLITE_OK; /*
377c0 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 No overflow pag
377d0 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68 6f es. Return witho
377e0 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e ut doing anythin
377f0 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 g */. }. if( p
37800 43 65 6c 6c 2b 70 49 6e 66 6f 2d 3e 6e 53 69 7a Cell+pInfo->nSiz
37810 65 2d 31 20 3e 20 70 50 61 67 65 2d 3e 61 44 61 e-1 > pPage->aDa
37820 74 61 2b 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 ta+pPage->maskPa
37830 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20 43 65 6c ge ){. /* Cel
37840 6c 20 65 78 74 65 6e 64 73 20 70 61 73 74 20 65 l extends past e
37850 6e 64 20 6f 66 20 70 61 67 65 20 2a 2f 0a 20 20 nd of page */.
37860 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
37870 43 4f 52 52 55 50 54 5f 50 47 4e 4f 28 70 50 61 CORRUPT_PGNO(pPa
37880 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 ge->pgno);. }.
37890 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 ovflPgno = get4
378a0 62 79 74 65 28 70 43 65 6c 6c 20 2b 20 70 49 6e byte(pCell + pIn
378b0 66 6f 2d 3e 6e 53 69 7a 65 20 2d 20 34 29 3b 0a fo->nSize - 4);.
378c0 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 pBt = pPage->p
378d0 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 Bt;. assert( pB
378e0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e 20 t->usableSize >
378f0 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 53 4 );. ovflPageS
37900 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c ize = pBt->usabl
37910 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 eSize - 4;. nOv
37920 66 6c 20 3d 20 28 70 49 6e 66 6f 2d 3e 6e 50 61 fl = (pInfo->nPa
37930 79 6c 6f 61 64 20 2d 20 70 49 6e 66 6f 2d 3e 6e yload - pInfo->n
37940 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 Local + ovflPage
37950 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 Size - 1)/ovflPa
37960 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 geSize;. assert
37970 28 20 6e 4f 76 66 6c 3e 30 20 7c 7c 20 0a 20 20 ( nOvfl>0 || .
37980 20 20 28 43 4f 52 52 55 50 54 5f 44 42 20 26 26 (CORRUPT_DB &&
37990 20 28 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 (pInfo->nPayloa
379a0 64 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 d + ovflPageSize
379b0 29 3c 6f 76 66 6c 50 61 67 65 53 69 7a 65 29 0a )<ovflPageSize).
379c0 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f );. while( nO
379d0 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 50 67 6e vfl-- ){. Pgn
379e0 6f 20 69 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 o iNext = 0;.
379f0 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 MemPage *pOvfl
37a00 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 76 66 = 0;. if( ovf
37a10 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 lPgno<2 || ovflP
37a20 67 6e 6f 3e 62 74 72 65 65 50 61 67 65 63 6f 75 gno>btreePagecou
37a30 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 nt(pBt) ){.
37a40 20 2f 2a 20 30 20 69 73 20 6e 6f 74 20 61 20 6c /* 0 is not a l
37a50 65 67 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 egal page number
37a60 20 61 6e 64 20 70 61 67 65 20 31 20 63 61 6e 6e and page 1 cann
37a70 6f 74 20 62 65 20 61 6e 20 0a 20 20 20 20 20 20 ot be an .
37a80 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ** overflow page
37a90 2e 20 54 68 65 72 65 66 6f 72 65 20 69 66 20 6f . Therefore if o
37aa0 76 66 6c 50 67 6e 6f 3c 32 20 6f 72 20 70 61 73 vflPgno<2 or pas
37ab0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
37ac0 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 . ** file
37ad0 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 the database mus
37ae0 74 20 62 65 20 63 6f 72 72 75 70 74 2e 20 2a 2f t be corrupt. */
37af0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
37b00 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
37b10 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 T;. }. if(
37b20 20 6e 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 nOvfl ){.
37b30 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 rc = getOverflow
37b40 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67 Page(pBt, ovflPg
37b50 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 26 69 4e 65 no, &pOvfl, &iNe
37b60 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 xt);. if( r
37b70 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
37b80 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 28 20 }.. if( (
37b90 70 4f 76 66 6c 20 7c 7c 20 28 28 70 4f 76 66 6c pOvfl || ((pOvfl
37ba0 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b = btreePageLook
37bb0 75 70 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f up(pBt, ovflPgno
37bc0 29 29 21 3d 30 29 20 29 0a 20 20 20 20 20 26 26 ))!=0) ). &&
37bd0 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
37be0 65 52 65 66 63 6f 75 6e 74 28 70 4f 76 66 6c 2d eRefcount(pOvfl-
37bf0 3e 70 44 62 50 61 67 65 29 21 3d 31 0a 20 20 20 >pDbPage)!=1.
37c00 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 ){. /* The
37c10 72 65 20 69 73 20 6e 6f 20 72 65 61 73 6f 6e 20 re is no reason
37c20 61 6e 79 20 63 75 72 73 6f 72 20 73 68 6f 75 6c any cursor shoul
37c30 64 20 68 61 76 65 20 61 6e 20 6f 75 74 73 74 61 d have an outsta
37c40 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 20 nding reference
37c50 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 6e 20 . ** to an
37c60 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 62 65 overflow page be
37c70 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 20 63 65 6c longing to a cel
37c80 6c 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 l that is being
37c90 64 65 6c 65 74 65 64 2f 75 70 64 61 74 65 64 2e deleted/updated.
37ca0 0a 20 20 20 20 20 20 2a 2a 20 53 6f 20 69 66 20 . ** So if
37cb0 74 68 65 72 65 20 65 78 69 73 74 73 20 6d 6f 72 there exists mor
37cc0 65 20 74 68 61 6e 20 6f 6e 65 20 72 65 66 65 72 e than one refer
37cd0 65 6e 63 65 20 74 6f 20 74 68 69 73 20 70 61 67 ence to this pag
37ce0 65 2c 20 74 68 65 6e 20 69 74 20 0a 20 20 20 20 e, then it .
37cf0 20 20 2a 2a 20 6d 75 73 74 20 6e 6f 74 20 72 65 ** must not re
37d00 61 6c 6c 79 20 62 65 20 61 6e 20 6f 76 65 72 66 ally be an overf
37d10 6c 6f 77 20 70 61 67 65 20 61 6e 64 20 74 68 65 low page and the
37d20 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 database must b
37d30 65 20 63 6f 72 72 75 70 74 2e 20 0a 20 20 20 20 e corrupt. .
37d40 20 20 2a 2a 20 49 74 20 69 73 20 68 65 6c 70 66 ** It is helpf
37d50 75 6c 20 74 6f 20 64 65 74 65 63 74 20 74 68 69 ul to detect thi
37d60 73 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 s before calling
37d70 20 66 72 65 65 50 61 67 65 32 28 29 2c 20 61 73 freePage2(), as
37d80 20 0a 20 20 20 20 20 20 2a 2a 20 66 72 65 65 50 . ** freeP
37d90 61 67 65 32 28 29 20 6d 61 79 20 7a 65 72 6f 20 age2() may zero
37da0 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 the page content
37db0 73 20 69 66 20 73 65 63 75 72 65 2d 64 65 6c 65 s if secure-dele
37dc0 74 65 20 6d 6f 64 65 20 69 73 0a 20 20 20 20 20 te mode is.
37dd0 20 2a 2a 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 ** enabled. If
37de0 74 68 69 73 20 27 6f 76 65 72 66 6c 6f 77 27 20 this 'overflow'
37df0 70 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f 20 page happens to
37e00 62 65 20 61 20 70 61 67 65 20 74 68 61 74 20 74 be a page that t
37e10 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c he. ** call
37e20 65 72 20 69 73 20 69 74 65 72 61 74 69 6e 67 20 er is iterating
37e30 74 68 72 6f 75 67 68 20 6f 72 20 75 73 69 6e 67 through or using
37e40 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 77 in some other w
37e50 61 79 2c 20 74 68 69 73 0a 20 20 20 20 20 20 2a ay, this. *
37e60 2a 20 63 61 6e 20 62 65 20 70 72 6f 62 6c 65 6d * can be problem
37e70 61 74 69 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 atic.. */.
37e80 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
37e90 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
37ea0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
37eb0 72 63 20 3d 20 66 72 65 65 50 61 67 65 32 28 70 rc = freePage2(p
37ec0 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 Bt, pOvfl, ovflP
37ed0 67 6e 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 gno);. }..
37ee0 20 69 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 if( pOvfl ){.
37ef0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
37f00 55 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 Unref(pOvfl->pDb
37f10 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Page);. }.
37f20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
37f30 20 72 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e rc;. ovflPgn
37f40 6f 20 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 o = iNext;. }.
37f50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
37f60 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 K;.}../*.** Crea
37f70 74 65 20 74 68 65 20 62 79 74 65 20 73 65 71 75 te the byte sequ
37f80 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65 70 ence used to rep
37f90 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e resent a cell on
37fa0 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 page pPage.** a
37fb0 6e 64 20 77 72 69 74 65 20 74 68 61 74 20 62 79 nd write that by
37fc0 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74 6f te sequence into
37fd0 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 pCell[]. Overf
37fe0 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a 2a low pages are.**
37ff0 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 allocated and f
38000 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 65 illed in as nece
38010 73 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c 6c ssary. The call
38020 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a ing procedure.**
38030 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
38040 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 for making sure
38050 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 sufficient space
38060 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 has been alloca
38070 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c ted.** for pCell
38080 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 []..**.** Note t
38090 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e hat pCell does n
380a0 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65 65 ot necessary nee
380b0 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 d to point to th
380c0 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a e pPage->aData.*
380d0 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d * area. pCell m
380e0 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f ight point to so
380f0 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f me temporary sto
38100 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 rage. The cell
38110 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 will.** be const
38120 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20 74 ructed in this t
38130 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74 68 emporary area th
38140 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 en copied into p
38150 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c Page->aData.** l
38160 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ater..*/.static
38170 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a int fillInCell(.
38180 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
38190 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
381a0 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 /* The page tha
381b0 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 t contains the c
381c0 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ell */. unsigne
381d0 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 d char *pCell,
381e0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c /* Compl
381f0 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 ete text of the
38200 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 cell */. const
38210 42 74 72 65 65 50 61 79 6c 6f 61 64 20 2a 70 58 BtreePayload *pX
38220 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 79 6c , /* Payl
38230 6f 61 64 20 77 69 74 68 20 77 68 69 63 68 20 74 oad with which t
38240 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 o construct the
38250 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 cell */. int *p
38260 6e 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 nSize
38270 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
38280 65 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 e cell size here
38290 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 */.){. int nPa
382a0 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 yload;. const u
382b0 38 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 8 *pSrc;. int n
382c0 53 72 63 2c 20 6e 2c 20 72 63 2c 20 6d 6e 3b 0a Src, n, rc, mn;.
382d0 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74 3b int spaceLeft;
382e0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 . MemPage *pToR
382f0 65 6c 65 61 73 65 3b 0a 20 20 75 6e 73 69 67 6e elease;. unsign
38300 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72 3b ed char *pPrior;
38310 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
38320 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 *pPayload;. Bt
38330 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 50 Shared *pBt;. P
38340 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 3b 0a 20 20 gno pgnoOvfl;.
38350 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 0a 20 20 int nHeader;..
38360 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
38370 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
38380 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
38390 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 .. /* pPage is
383a0 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 not necessarily
383b0 77 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 writeable since
383c0 70 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 pCell might be a
383d0 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 uxiliary. ** bu
383e0 66 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20 ffer space that
383f0 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d is separate from
38400 20 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65 the pPage buffe
38410 72 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 r area */. asse
38420 72 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d rt( pCell<pPage-
38430 3e 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e >aData || pCell>
38440 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 =&pPage->aData[p
38450 50 61 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 Page->pBt->pageS
38460 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 ize].
38470 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 || sqlite3Pager
38480 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
38490