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 74 65 64 29 29 0a 20 20 29 7b 0a 20 20 20 itted)). ){.
1520: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a return 1;. }..
1530: 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6c 69 65 /* If the clie
1540: 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 20 6f nt is reading o
1550: 72 20 77 72 69 74 69 6e 67 20 61 6e 20 69 6e 64 r writing an ind
1560: 65 78 20 61 6e 64 20 74 68 65 20 73 63 68 65 6d ex and the schem
1570: 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f a is. ** not lo
1580: 61 64 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 aded, then it is
1590: 20 74 6f 6f 20 64 69 66 66 69 63 75 6c 74 20 74 too difficult t
15a0: 6f 20 61 63 74 75 61 6c 6c 79 20 63 68 65 63 6b o actually check
15b0: 20 74 6f 20 73 65 65 20 69 66 0a 20 20 2a 2a 20 to see if. **
15c0: 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b the correct lock
15d0: 73 20 61 72 65 20 68 65 6c 64 2e 20 20 53 6f 20 s are held. So
15e0: 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20 do not bother -
15f0: 6a 75 73 74 20 72 65 74 75 72 6e 20 74 72 75 65 just return true
1600: 2e 0a 20 20 2a 2a 20 54 68 69 73 20 63 61 73 65 .. ** This case
1610: 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75 does not come u
1620: 70 20 76 65 72 79 20 6f 66 74 65 6e 20 61 6e 79 p very often any
1630: 68 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 how.. */. if(
1640: 69 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 isIndex && (!pSc
1650: 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 hema || (pSchema
1660: 2d 3e 73 63 68 65 6d 61 46 6c 61 67 73 26 44 42 ->schemaFlags&DB
1670: 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d _SchemaLoaded)==
1680: 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 0) ){. return
1690: 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 1;. }.. /* Fi
16a0: 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f 6f gure out the roo
16b0: 74 2d 70 61 67 65 20 74 68 61 74 20 74 68 65 20 t-page that the
16c0: 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 lock should be h
16d0: 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c eld on. For tabl
16e0: 65 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 e. ** b-trees,
16f0: 74 68 69 73 20 69 73 20 6a 75 73 74 20 74 68 65 this is just the
1700: 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 root page of th
1710: 65 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20 72 e b-tree being r
1720: 65 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 ead or. ** writ
1730: 74 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 ten. For index b
1740: 2d 74 72 65 65 73 2c 20 69 74 20 69 73 20 74 68 -trees, it is th
1750: 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
1760: 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 20 he associated.
1770: 2a 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 ** table. */.
1780: 69 66 28 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 if( isIndex ){.
1790: 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a HashElem *p;.
17a0: 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 for(p=sqlite
17b0: 48 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 HashFirst(&pSche
17c0: 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 70 3b ma->idxHash); p;
17d0: 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 p=sqliteHashNex
17e0: 74 28 70 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 t(p)){. Ind
17f0: 65 78 20 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 ex *pIdx = (Inde
1800: 78 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 x *)sqliteHashDa
1810: 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 ta(p);. if(
1820: 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e pIdx->tnum==(in
1830: 74 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 t)iRoot ){.
1840: 20 20 20 69 66 28 20 69 54 61 62 20 29 7b 0a 20 if( iTab ){.
1850: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 /* Two
1860: 6f 72 20 6d 6f 72 65 20 69 6e 64 65 78 65 73 20 or more indexes
1870: 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 20 72 share the same r
1880: 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 65 72 65 oot page. There
1890: 20 6d 75 73 74 0a 20 20 20 20 20 20 20 20 20 20 must.
18a0: 2a 2a 20 62 65 20 69 6d 70 6f 73 74 65 72 20 74 ** be imposter t
18b0: 61 62 6c 65 73 2e 20 20 53 6f 20 6a 75 73 74 20 ables. So just
18c0: 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 54 68 return true. Th
18d0: 65 20 61 73 73 65 72 74 20 69 73 20 6e 6f 74 0a e assert is not.
18e0: 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 ** use
18f0: 66 75 6c 20 69 6e 20 74 68 61 74 20 63 61 73 65 ful in that case
1900: 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 . */. r
1910: 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 eturn 1;.
1920: 20 7d 0a 20 20 20 20 20 20 20 20 69 54 61 62 20 }. iTab
1930: 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e = pIdx->pTable->
1940: 74 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 tnum;. }.
1950: 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1960: 20 69 54 61 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 iTab = iRoot;.
1970: 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 }.. /* Search
1980: 66 6f 72 20 74 68 65 20 72 65 71 75 69 72 65 64 for the required
1990: 20 6c 6f 63 6b 2e 20 45 69 74 68 65 72 20 61 20 lock. Either a
19a0: 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f write-lock on ro
19b0: 6f 74 2d 70 61 67 65 20 69 54 61 62 2c 20 61 20 ot-page iTab, a
19c0: 0a 20 20 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b . ** write-lock
19d0: 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 on the schema t
19e0: 61 62 6c 65 2c 20 6f 72 20 28 69 66 20 74 68 65 able, or (if the
19f0: 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 client is readi
1a00: 6e 67 29 20 61 0a 20 20 2a 2a 20 72 65 61 64 2d ng) a. ** read-
1a10: 6c 6f 63 6b 20 6f 6e 20 69 54 61 62 20 77 69 6c lock on iTab wil
1a20: 6c 20 73 75 66 66 69 63 65 2e 20 52 65 74 75 72 l suffice. Retur
1a30: 6e 20 31 20 69 66 20 61 6e 79 20 6f 66 20 74 68 n 1 if any of th
1a40: 65 73 65 20 61 72 65 20 66 6f 75 6e 64 2e 20 20 ese are found.
1a50: 2a 2f 0a 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 */. for(pLock=p
1a60: 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 Btree->pBt->pLoc
1a70: 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d k; pLock; pLock=
1a80: 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 pLock->pNext){.
1a90: 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 if( pLock->pB
1aa0: 74 72 65 65 3d 3d 70 42 74 72 65 65 20 0a 20 20 tree==pBtree .
1ab0: 20 20 20 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 && (pLock->iT
1ac0: 61 62 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70 able==iTab || (p
1ad0: 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 Lock->eLock==WRI
1ae0: 54 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b TE_LOCK && pLock
1af0: 2d 3e 69 54 61 62 6c 65 3d 3d 31 29 29 0a 20 20 ->iTable==1)).
1b00: 20 20 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f && pLock->eLo
1b10: 63 6b 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 0a 20 ck>=eLockType .
1b20: 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ){. retu
1b30: 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
1b40: 0a 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 . /* Failed to
1b50: 66 69 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 find the require
1b60: 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 d lock. */. ret
1b70: 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 urn 0;.}.#endif
1b80: 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
1b90: 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
1ba0: 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 E_DEBUG./*.****
1bb0: 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
1bc0: 79 20 62 65 20 75 73 65 64 20 61 73 20 70 61 72 y be used as par
1bd0: 74 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 t of assert() st
1be0: 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a atements only. *
1bf0: 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ***.**.** Return
1c00: 20 74 72 75 65 20 69 66 20 69 74 20 77 6f 75 6c true if it woul
1c10: 64 20 62 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72 d be illegal for
1c20: 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 pBtree to write
1c30: 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 61 62 into the.** tab
1c40: 6c 65 20 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74 le or index root
1c50: 65 64 20 61 74 20 69 52 6f 6f 74 20 62 65 63 61 ed at iRoot beca
1c60: 75 73 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 use other shared
1c70: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 connections are
1c80: 0a 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 .** simultaneous
1c90: 6c 79 20 72 65 61 64 69 6e 67 20 74 68 61 74 20 ly reading that
1ca0: 73 61 6d 65 20 74 61 62 6c 65 20 6f 72 20 69 6e same table or in
1cb0: 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 dex..**.** It is
1cc0: 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 illegal for pBt
1cd0: 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 66 20 ree to write if
1ce0: 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72 65 65 some other Btree
1cf0: 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 object that.**
1d00: 73 68 61 72 65 73 20 74 68 65 20 73 61 6d 65 20 shares the same
1d10: 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
1d20: 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 is currently rea
1d30: 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 0a ding or writing.
1d40: 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20 74 61 62 ** the iRoot tab
1d50: 6c 65 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 le. Except, if
1d60: 74 68 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 the other Btree
1d70: 6f 62 6a 65 63 74 20 68 61 73 20 74 68 65 0a 2a object has the.*
1d80: 2a 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 * read-uncommitt
1d90: 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 ed flag set, the
1da0: 6e 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 n it is OK for t
1db0: 68 65 20 6f 74 68 65 72 20 6f 62 6a 65 63 74 20 he other object
1dc0: 74 6f 0a 2a 2a 20 68 61 76 65 20 61 20 72 65 61 to.** have a rea
1dd0: 64 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 d cursor..**.**
1de0: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 66 For example, bef
1df0: 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61 ore writing to a
1e00: 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 ny part of the t
1e10: 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 0a 2a 2a able or index.**
1e20: 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 rooted at page
1e30: 69 52 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f 75 6c iRoot, one shoul
1e40: 64 20 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 d call:.**.**
1e50: 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 assert( !hasRea
1e60: 64 43 6f 6e 66 6c 69 63 74 73 28 70 42 74 72 65 dConflicts(pBtre
1e70: 65 2c 20 69 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a e, iRoot) );.*/.
1e80: 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 52 65 static int hasRe
1e90: 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74 72 65 adConflicts(Btre
1ea0: 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20 e *pBtree, Pgno
1eb0: 69 52 6f 6f 74 29 7b 0a 20 20 42 74 43 75 72 73 iRoot){. BtCurs
1ec0: 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 or *p;. for(p=p
1ed0: 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 Btree->pBt->pCur
1ee0: 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 sor; p; p=p->pNe
1ef0: 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e xt){. if( p->
1f00: 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 pgnoRoot==iRoot
1f10: 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42 74 72 . && p->pBtr
1f20: 65 65 21 3d 70 42 74 72 65 65 0a 20 20 20 20 20 ee!=pBtree.
1f30: 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72 65 65 && 0==(p->pBtree
1f40: 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 ->db->flags & SQ
1f50: 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 LITE_ReadUncommi
1f60: 74 74 65 64 29 0a 20 20 20 20 29 7b 0a 20 20 20 tted). ){.
1f70: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1f80: 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1f90: 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 0;.}.#endif /
1fa0: 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f * #ifdef SQLITE_
1fb0: 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 DEBUG */../*.**
1fc0: 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 20 Query to see if
1fd0: 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d Btree handle p m
1fe0: 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b ay obtain a lock
1ff0: 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a of type eLock .
2000: 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 ** (READ_LOCK or
2010: 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 WRITE_LOCK) on
2020: 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 the table with r
2030: 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52 oot-page iTab. R
2040: 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
2050: 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d OK if the lock m
2060: 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28 ay be obtained (
2070: 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 by calling.** se
2080: 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
2090: 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c eLock()), or SQL
20a0: 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f ITE_LOCKED if no
20b0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
20c0: 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
20d0: 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 eTableLock(Btree
20e0: 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20 *p, Pgno iTab,
20f0: 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 u8 eLock){. BtS
2100: 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
2110: 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 pBt;. BtLock *p
2120: 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 Iter;.. assert(
2130: 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
2140: 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
2150: 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
2160: 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 EAD_LOCK || eLoc
2170: 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b k==WRITE_LOCK );
2180: 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
2190: 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
21a0: 20 21 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 !(p->db->flags&
21b0: 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d SQLITE_ReadUncom
21c0: 6d 69 74 74 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d mitted)||eLock==
21d0: 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 WRITE_LOCK||iTab
21e0: 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 ==1 );. . /* I
21f0: 66 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 77 f requesting a w
2200: 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 rite-lock, then
2210: 74 68 65 20 42 74 72 65 65 20 6d 75 73 74 20 68 the Btree must h
2220: 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 ave an open writ
2230: 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 e. ** transacti
2240: 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 2e on on this file.
2250: 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79 2c And, obviously,
2260: 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62 65 20 for this to be
2270: 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a 20 6d so there . ** m
2280: 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 77 ust be an open w
2290: 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
22a0: 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69 74 73 on the file its
22b0: 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 elf.. */. asse
22c0: 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f rt( eLock==READ_
22d0: 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 2d LOCK || (p==pBt-
22e0: 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d 3e 69 >pWriter && p->i
22f0: 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
2300: 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ITE) );. assert
2310: 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ( eLock==READ_LO
2320: 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 CK || pBt->inTra
2330: 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
2340: 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f 2a WRITE );. . /*
2350: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
2360: 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 a no-op if the
2370: 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 shared-cache is
2380: 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 not enabled */.
2390: 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
23a0: 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
23b0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
23c0: 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 /* If some oth
23d0: 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 er connection is
23e0: 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c holding an excl
23f0: 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a usive lock, the.
2400: 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c ** requested l
2410: 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f ock may not be o
2420: 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 btained.. */.
2430: 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 if( pBt->pWriter
2440: 21 3d 70 20 26 26 20 28 70 42 74 2d 3e 62 74 73 !=p && (pBt->bts
2450: 46 6c 61 67 73 20 26 20 42 54 53 5f 45 58 43 4c Flags & BTS_EXCL
2460: 55 53 49 56 45 29 21 3d 30 20 29 7b 0a 20 20 20 USIVE)!=0 ){.
2470: 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
2480: 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c onBlocked(p->db,
2490: 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 pBt->pWriter->d
24a0: 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 b);. return S
24b0: 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
24c0: 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 REDCACHE;. }..
24d0: 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
24e0: 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
24f0: 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
2500: 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f ){. /* The co
2510: 6e 64 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e ndition (pIter->
2520: 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e eLock!=eLock) in
2530: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
2540: 66 28 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73 f(...) . ** s
2550: 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 tatement is a si
2560: 6d 70 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a mplification of:
2570: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
2580: 20 28 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c (eLock==WRITE_L
2590: 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c OCK || pIter->eL
25a0: 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 ock==WRITE_LOCK)
25b0: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 . **. ** s
25c0: 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 ince we know tha
25d0: 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 t if eLock==WRIT
25e0: 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 E_LOCK, then no
25f0: 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
2600: 0a 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 . ** may hold
2610: 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e a WRITE_LOCK on
2620: 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 any table in th
2630: 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 is file (since t
2640: 68 65 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20 here can. **
2650: 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 only be a single
2660: 20 77 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f writer).. */
2670: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 . assert( pIt
2680: 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f er->eLock==READ_
2690: 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 LOCK || pIter->e
26a0: 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
26b0: 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
26c0: 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
26d0: 20 7c 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 || pIter->pBtre
26e0: 65 3d 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65 e==p || pIter->e
26f0: 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 Lock==READ_LOCK)
2700: 3b 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d ;. if( pIter-
2710: 3e 70 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 >pBtree!=p && pI
2720: 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ter->iTable==iTa
2730: 62 20 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63 b && pIter->eLoc
2740: 6b 21 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 k!=eLock ){.
2750: 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 sqlite3Connect
2760: 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 ionBlocked(p->db
2770: 2c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d , pIter->pBtree-
2780: 3e 64 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 >db);. if(
2790: 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
27a0: 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 K ){. ass
27b0: 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 ert( p==pBt->pWr
27c0: 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 iter );.
27d0: 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d pBt->btsFlags |=
27e0: 20 42 54 53 5f 50 45 4e 44 49 4e 47 3b 0a 20 20 BTS_PENDING;.
27f0: 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
2800: 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
2810: 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
2820: 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
2830: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
2840: 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
2850: 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
2860: 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
2870: 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
2880: 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64 _CACHE./*.** Add
2890: 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 a lock on the t
28a0: 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
28b0: 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68 age iTable to th
28c0: 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75 e shared-btree u
28d0: 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20 sed.** by Btree
28e0: 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65 handle p. Parame
28f0: 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62 ter eLock must b
2900: 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f e either READ_LO
2910: 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f CK or .** WRITE_
2920: 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 LOCK..**.** This
2930: 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
2940: 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
2950: 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65 .**.** (a) The
2960: 20 73 70 65 63 69 66 69 65 64 20 42 74 72 65 65 specified Btree
2970: 20 6f 62 6a 65 63 74 20 70 20 69 73 20 63 6f 6e object p is con
2980: 6e 65 63 74 65 64 20 74 6f 20 61 20 73 68 61 72 nected to a shar
2990: 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 able.** da
29a0: 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74 68 tabase (one with
29b0: 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73 68 the BtShared.sh
29c0: 61 72 61 62 6c 65 20 66 6c 61 67 20 73 65 74 29 arable flag set)
29d0: 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28 62 , and.**.** (b
29e0: 29 20 4e 6f 20 6f 74 68 65 72 20 42 74 72 65 65 ) No other Btree
29f0: 20 6f 62 6a 65 63 74 73 20 68 6f 6c 64 20 61 20 objects hold a
2a00: 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69 lock that confli
2a10: 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 cts.** wit
2a20: 68 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 h the requested
2a30: 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65 72 79 lock (i.e. query
2a40: 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
2a50: 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20 Lock() has.**
2a60: 20 20 20 20 61 6c 72 65 61 64 79 20 62 65 65 6e already been
2a70: 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65 74 75 called and retu
2a80: 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e rned SQLITE_OK).
2a90: 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
2aa0: 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
2ab0: 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 the lock is adde
2ac0: 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 d successfully.
2ad0: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a SQLITE_NOMEM .**
2ae0: 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
2af0: 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 a malloc attempt
2b00: 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 fails..*/.stati
2b10: 63 20 69 6e 74 20 73 65 74 53 68 61 72 65 64 43 c int setSharedC
2b20: 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 acheTableLock(Bt
2b30: 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 ree *p, Pgno iTa
2b40: 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a ble, u8 eLock){.
2b50: 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
2b60: 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f = p->pBt;. BtLo
2b70: 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 ck *pLock = 0;.
2b80: 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a BtLock *pIter;.
2b90: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
2ba0: 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
2bb0: 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
2bc0: 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ( eLock==READ_LO
2bd0: 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 CK || eLock==WRI
2be0: 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 TE_LOCK );. ass
2bf0: 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b ert( p->db!=0 );
2c00: 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 .. /* A connect
2c10: 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 61 ion with the rea
2c20: 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c d-uncommitted fl
2c30: 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 76 65 ag set will neve
2c40: 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 r try to. ** ob
2c50: 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b tain a read-lock
2c60: 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63 using this func
2c70: 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72 tion. The only r
2c80: 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 ead-lock obtaine
2c90: 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e d. ** by a conn
2ca0: 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75 ection in read-u
2cb0: 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20 ncommitted mode
2cc0: 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 is on the sqlite
2cd0: 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20 74 61 _master . ** ta
2ce0: 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f ble, and that lo
2cf0: 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 ck is obtained i
2d00: 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e n BtreeBeginTran
2d10: 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 s(). */. asser
2d20: 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c t( 0==(p->db->fl
2d30: 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
2d40: 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65 ncommitted) || e
2d50: 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
2d60: 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 );.. /* This f
2d70: 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f unction should o
2d80: 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e nly be called on
2d90: 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d 74 72 a sharable b-tr
2da0: 65 65 20 61 66 74 65 72 20 69 74 20 0a 20 20 2a ee after it . *
2db0: 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74 65 72 * has been deter
2dc0: 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6f 74 mined that no ot
2dd0: 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c 64 73 her b-tree holds
2de0: 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c a conflicting l
2df0: 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 ock. */. asser
2e00: 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 t( p->sharable )
2e10: 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 ;. assert( SQLI
2e20: 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 TE_OK==queryShar
2e30: 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
2e40: 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 (p, iTable, eLoc
2e50: 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 k) );.. /* Firs
2e60: 74 20 73 65 61 72 63 68 20 74 68 65 20 6c 69 73 t search the lis
2e70: 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e t for an existin
2e80: 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 g lock on this t
2e90: 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 able. */. for(p
2ea0: 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Iter=pBt->pLock;
2eb0: 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 pIter; pIter=pI
2ec0: 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
2ed0: 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54 61 62 if( pIter->iTab
2ee0: 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 49 le==iTable && pI
2ef0: 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 ter->pBtree==p )
2f00: 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 {. pLock =
2f10: 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62 72 65 pIter;. bre
2f20: 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ak;. }. }..
2f30: 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f 76 65 /* If the above
2f40: 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 20 search did not
2f50: 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 find a BtLock st
2f60: 72 75 63 74 20 61 73 73 6f 63 69 61 74 69 6e 67 ruct associating
2f70: 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 Btree p. ** wi
2f80: 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c 65 2c th table iTable,
2f90: 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e allocate one an
2fa0: 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 d link it into t
2fb0: 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 he list.. */.
2fc0: 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 if( !pLock ){.
2fd0: 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 pLock = (BtLoc
2fe0: 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f k *)sqlite3Mallo
2ff0: 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c cZero(sizeof(BtL
3000: 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28 20 21 ock));. if( !
3010: 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 pLock ){. r
3020: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
3030: 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c EM;. }. pL
3040: 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 ock->iTable = iT
3050: 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d able;. pLock-
3060: 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 >pBtree = p;.
3070: 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 pLock->pNext =
3080: 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 pBt->pLock;.
3090: 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f pBt->pLock = pLo
30a0: 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ck;. }.. /* Se
30b0: 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f t the BtLock.eLo
30c0: 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74 ck variable to t
30d0: 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 he maximum of th
30e0: 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 e current lock.
30f0: 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 71 75 ** and the requ
3100: 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 ested lock. This
3110: 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72 69 74 means if a writ
3120: 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 e-lock was alrea
3130: 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 dy held. ** and
3140: 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 a read-lock req
3150: 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 uested, we don't
3160: 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 incorrectly dow
3170: 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e ngrade the lock.
3180: 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
3190: 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f WRITE_LOCK>READ_
31a0: 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c LOCK );. if( eL
31b0: 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b ock>pLock->eLock
31c0: 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 ){. pLock->e
31d0: 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 Lock = eLock;.
31e0: 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
31f0: 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 TE_OK;.}.#endif
3200: 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
3210: 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a SHARED_CACHE */.
3220: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
3230: 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
3240: 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 E./*.** Release
3250: 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 6c 6f all the table lo
3260: 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 cks (locks obtai
3270: 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 74 6f ned via calls to
3280: 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61 72 65 .** the setShare
3290: 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
32a0: 29 20 70 72 6f 63 65 64 75 72 65 29 20 68 65 6c ) procedure) hel
32b0: 64 20 62 79 20 42 74 72 65 65 20 6f 62 6a 65 63 d by Btree objec
32c0: 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 t p..**.** This
32d0: 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 function assumes
32e0: 20 74 68 61 74 20 42 74 72 65 65 20 70 20 68 61 that Btree p ha
32f0: 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f s an open read o
3300: 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e r write .** tran
3310: 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 saction. If it d
3320: 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 oes not, then th
3330: 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 66 6c e BTS_PENDING fl
3340: 61 67 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 ag.** may be inc
3350: 6f 72 72 65 63 74 6c 79 20 63 6c 65 61 72 65 64 orrectly cleared
3360: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
3370: 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 clearAllSharedC
3380: 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 acheTableLocks(B
3390: 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 tree *p){. BtSh
33a0: 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
33b0: 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 Bt;. BtLock **p
33c0: 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c pIter = &pBt->pL
33d0: 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ock;.. assert(
33e0: 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
33f0: 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 sMutex(p) );. a
3400: 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
3410: 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 le || 0==*ppIter
3420: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
3430: 3e 69 6e 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 >inTrans>0 );..
3440: 20 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20 while( *ppIter
3450: 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 ){. BtLock *p
3460: 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a Lock = *ppIter;.
3470: 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74 assert( (pBt
3480: 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
3490: 5f 45 58 43 4c 55 53 49 56 45 29 3d 3d 30 20 7c _EXCLUSIVE)==0 |
34a0: 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d | pBt->pWriter==
34b0: 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b pLock->pBtree );
34c0: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f . assert( pLo
34d0: 63 6b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 ck->pBtree->inTr
34e0: 61 6e 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 ans>=pLock->eLoc
34f0: 6b 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f k );. if( pLo
3500: 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b ck->pBtree==p ){
3510: 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d . *ppIter =
3520: 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pLock->pNext;.
3530: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f assert( pLo
3540: 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c ck->iTable!=1 ||
3550: 20 70 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b pLock==&p->lock
3560: 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c );. if( pL
3570: 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 ock->iTable!=1 )
3580: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
3590: 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 3_free(pLock);.
35a0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
35b0: 7b 0a 20 20 20 20 20 20 70 70 49 74 65 72 20 3d {. ppIter =
35c0: 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a &pLock->pNext;.
35d0: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 }. }.. ass
35e0: 65 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c ert( (pBt->btsFl
35f0: 61 67 73 20 26 20 42 54 53 5f 50 45 4e 44 49 4e ags & BTS_PENDIN
3600: 47 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 G)==0 || pBt->pW
3610: 72 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 riter );. if( p
3620: 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 Bt->pWriter==p )
3630: 7b 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 {. pBt->pWrit
3640: 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d er = 0;. pBt-
3650: 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e 28 42 >btsFlags &= ~(B
3660: 54 53 5f 45 58 43 4c 55 53 49 56 45 7c 42 54 53 TS_EXCLUSIVE|BTS
3670: 5f 50 45 4e 44 49 4e 47 29 3b 0a 20 20 7d 65 6c _PENDING);. }el
3680: 73 65 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 se if( pBt->nTra
3690: 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 nsaction==2 ){.
36a0: 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 /* This funct
36b0: 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 ion is called wh
36c0: 65 6e 20 42 74 72 65 65 20 70 20 69 73 20 63 6f en Btree p is co
36d0: 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 ncluding its .
36e0: 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
36f0: 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 . If there curre
3700: 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 ntly exists a wr
3710: 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e iter, and p is n
3720: 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 ot. ** that w
3730: 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 riter, then the
3740: 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 number of locks
3750: 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 held by connecti
3760: 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a ons other. **
3770: 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 than the writer
3780: 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 must be about t
3790: 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 o drop to zero.
37a0: 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 In this case.
37b0: 20 2a 2a 20 73 65 74 20 74 68 65 20 42 54 53 5f ** set the BTS_
37c0: 50 45 4e 44 49 4e 47 20 66 6c 61 67 20 74 6f 20 PENDING flag to
37d0: 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 0.. **. **
37e0: 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 If there is not
37f0: 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 currently a wri
3800: 74 65 72 2c 20 74 68 65 6e 20 42 54 53 5f 50 45 ter, then BTS_PE
3810: 4e 44 49 4e 47 20 6d 75 73 74 0a 20 20 20 20 2a NDING must. *
3820: 2a 20 62 65 20 7a 65 72 6f 20 61 6c 72 65 61 64 * be zero alread
3830: 79 2e 20 53 6f 20 74 68 69 73 20 6e 65 78 74 20 y. So this next
3840: 6c 69 6e 65 20 69 73 20 68 61 72 6d 6c 65 73 73 line is harmless
3850: 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e 0a 20 in that case..
3860: 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 62 */. pBt->b
3870: 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f tsFlags &= ~BTS_
3880: 50 45 4e 44 49 4e 47 3b 0a 20 20 7d 0a 7d 0a 0a PENDING;. }.}..
3890: 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
38a0: 69 6f 6e 20 63 68 61 6e 67 65 73 20 61 6c 6c 20 ion changes all
38b0: 77 72 69 74 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 write-locks held
38c0: 20 62 79 20 42 74 72 65 65 20 70 20 69 6e 74 6f by Btree p into
38d0: 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a read-locks..*/.
38e0: 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 77 6e static void down
38f0: 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 gradeAllSharedCa
3900: 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 cheTableLocks(Bt
3910: 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
3920: 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
3930: 74 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 t;. if( pBt->pW
3940: 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 riter==p ){.
3950: 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 BtLock *pLock;.
3960: 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 pBt->pWriter
3970: 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 62 74 = 0;. pBt->bt
3980: 73 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 53 5f sFlags &= ~(BTS_
3990: 45 58 43 4c 55 53 49 56 45 7c 42 54 53 5f 50 45 EXCLUSIVE|BTS_PE
39a0: 4e 44 49 4e 47 29 3b 0a 20 20 20 20 66 6f 72 28 NDING);. for(
39b0: 70 4c 6f 63 6b 3d 70 42 74 2d 3e 70 4c 6f 63 6b pLock=pBt->pLock
39c0: 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 ; pLock; pLock=p
39d0: 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 Lock->pNext){.
39e0: 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
39f0: 6b 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c k->eLock==READ_L
3a00: 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 70 42 OCK || pLock->pB
3a10: 74 72 65 65 3d 3d 70 20 29 3b 0a 20 20 20 20 20 tree==p );.
3a20: 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 pLock->eLock =
3a30: 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d READ_LOCK;. }
3a40: 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f . }.}..#endif /
3a50: 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 * SQLITE_OMIT_SH
3a60: 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 ARED_CACHE */..s
3a70: 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
3a80: 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a sePage(MemPage *
3a90: 70 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77 pPage); /* Forw
3aa0: 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f ard reference */
3ab0: 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 20 54 68 69 73 20 ../*.***** This
3ac0: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
3ad0: 69 6e 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 inside of assert
3ae0: 28 29 20 6f 6e 6c 79 20 2a 2a 2a 2a 0a 2a 2a 0a () only ****.**.
3af0: 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 ** Verify that t
3b00: 68 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 he cursor holds
3b10: 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 69 74 73 the mutex on its
3b20: 20 42 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 BtShared.*/.#if
3b30: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
3b40: 0a 73 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 .static int curs
3b50: 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 orHoldsMutex(BtC
3b60: 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 ursor *p){. ret
3b70: 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 urn sqlite3_mute
3b80: 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d x_held(p->pBt->m
3b90: 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a utex);.}.#endif.
3ba0: 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 ./*.** Invalidat
3bb0: 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 e the overflow c
3bc0: 61 63 68 65 20 6f 66 20 74 68 65 20 63 75 72 73 ache of the curs
3bd0: 6f 72 20 70 61 73 73 65 64 20 61 73 20 74 68 65 or passed as the
3be0: 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
3bf0: 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 .** on the share
3c00: 64 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 d btree structur
3c10: 65 20 70 42 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e e pBt..*/.#defin
3c20: 65 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 e invalidateOver
3c30: 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 20 flowCache(pCur)
3c40: 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 (pCur->curFlags
3c50: 26 3d 20 7e 42 54 43 46 5f 56 61 6c 69 64 4f 76 &= ~BTCF_ValidOv
3c60: 66 6c 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c fl)../*.** Inval
3c70: 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c idate the overfl
3c80: 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 ow page-list cac
3c90: 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f he for all curso
3ca0: 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 rs opened.** on
3cb0: 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 the shared btree
3cc0: 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a structure pBt..
3cd0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
3ce0: 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
3cf0: 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 flowCache(BtShar
3d00: 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 ed *pBt){. BtCu
3d10: 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 rsor *p;. asser
3d20: 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
3d30: 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
3d40: 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 ) );. for(p=pBt
3d50: 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d ->pCursor; p; p=
3d60: 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
3d70: 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
3d80: 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d wCache(p);. }.}
3d90: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
3da0: 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f _OMIT_INCRBLOB./
3db0: 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
3dc0: 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 65 66 on is called bef
3dd0: 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 ore modifying th
3de0: 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 e contents of a
3df0: 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e 76 61 table.** to inva
3e00: 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 lidate any incrb
3e10: 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68 61 74 lob cursors that
3e20: 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 are open on the
3e30: 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 20 6f .** row or one o
3e40: 66 20 74 68 65 20 72 6f 77 73 20 62 65 69 6e 67 f the rows being
3e50: 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a modified..**.**
3e60: 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 If argument isC
3e70: 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74 72 75 learTable is tru
3e80: 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 e, then the enti
3e90: 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 re contents of t
3ea0: 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 61 he.** table is a
3eb0: 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74 bout to be delet
3ec0: 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
3ed0: 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 invalidate all
3ee0: 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 72 73 incrblob.** curs
3ef0: 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 79 20 ors open on any
3f00: 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65 20 74 row within the t
3f10: 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
3f20: 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a age pgnoRoot..**
3f30: 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 .** Otherwise, i
3f40: 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 f argument isCle
3f50: 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c 73 65 arTable is false
3f60: 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 77 , then the row w
3f70: 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f ith.** rowid iRo
3f80: 77 20 69 73 20 62 65 69 6e 67 20 72 65 70 6c 61 w is being repla
3f90: 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 ced or deleted.
3fa0: 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 In this case inv
3fb0: 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c 79 20 alidate.** only
3fc0: 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 20 63 those incrblob c
3fd0: 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 ursors open on t
3fe0: 68 61 74 20 73 70 65 63 69 66 69 63 20 72 6f 77 hat specific row
3ff0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
4000: 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 invalidateIncrb
4010: 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 42 74 lobCursors(. Bt
4020: 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 ree *pBtree,
4030: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
4040: 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 abase file to ch
4050: 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 52 6f eck */. i64 iRo
4060: 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w,
4070: 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74 68 /* The rowid th
4080: 61 74 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e at might be chan
4090: 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 73 ging */. int is
40a0: 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20 20 20 ClearTable
40b0: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c /* True if all
40c0: 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e 67 20 rows are being
40d0: 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 deleted */.){.
40e0: 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 42 BtCursor *p;. B
40f0: 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
4100: 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 61 73 Btree->pBt;. as
4110: 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
4120: 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 eeHoldsMutex(pBt
4130: 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d ree) );. for(p=
4140: 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
4150: 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
4160: 20 20 69 66 28 20 28 70 2d 3e 63 75 72 46 6c 61 if( (p->curFla
4170: 67 73 20 26 20 42 54 43 46 5f 49 6e 63 72 62 6c gs & BTCF_Incrbl
4180: 6f 62 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28 ob)!=0. && (
4190: 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 isClearTable ||
41a0: 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 p->info.nKey==iR
41b0: 6f 77 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ow). ){.
41c0: 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 p->eState = CUR
41d0: 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 SOR_INVALID;.
41e0: 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a }. }.}..#else.
41f0: 20 20 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69 /* Stub functi
4200: 6f 6e 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 on when INCRBLOB
4210: 20 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 is omitted */.
4220: 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 #define invalid
4230: 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f ateIncrblobCurso
4240: 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 rs(x,y,z).#endif
4250: 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
4260: 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 0a INCRBLOB */../*.
4270: 2a 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 ** Set bit pgno
4280: 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e of the BtShared.
4290: 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
42a0: 65 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c ec. This is call
42b0: 65 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 ed .** when a pa
42c0: 67 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 ge that previous
42d0: 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 ly contained dat
42e0: 61 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 a becomes a free
42f0: 2d 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 70 -list leaf .** p
4300: 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 age..**.** The B
4310: 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
4320: 65 6e 74 20 62 69 74 76 65 63 20 65 78 69 73 74 ent bitvec exist
4330: 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 s to work around
4340: 20 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 62 an obscure.** b
4350: 75 67 20 63 61 75 73 65 64 20 62 79 20 74 68 65 ug caused by the
4360: 20 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 20 interaction of
4370: 74 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f 70 two useful IO op
4380: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 72 timizations surr
4390: 6f 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 2d ounding.** free-
43a0: 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 3a list leaf pages:
43b0: 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e .**.** 1) When
43c0: 20 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c all data is del
43d0: 65 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67 65 eted from a page
43e0: 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 62 65 and the page be
43f0: 63 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 20 comes.** a
4400: 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
4410: 61 67 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 age, the page is
4420: 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 not written to
4430: 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
4440: 20 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c 69 (as free-li
4450: 73 74 20 6c 65 61 66 20 70 61 67 65 73 20 63 6f st leaf pages co
4460: 6e 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 ntain no meaning
4470: 66 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 74 ful data). Somet
4480: 69 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 63 imes.** suc
4490: 68 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74 20 h a page is not
44a0: 65 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 even journalled
44b0: 28 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 (as it will not
44c0: 62 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 be modified,.**
44d0: 20 20 20 20 20 77 68 79 20 62 6f 74 68 65 72 20 why bother
44e0: 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 journalling it?)
44f0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 ..**.** 2) Whe
4500: 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 n a free-list le
4510: 61 66 20 70 61 67 65 20 69 73 20 72 65 75 73 65 af page is reuse
4520: 64 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 69 d, its content i
4530: 73 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 20 s not read.**
4540: 20 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 from the data
4550: 62 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e 20 base or written
4560: 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
4570: 69 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 20 ile (why should
4580: 69 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 it.** be, i
4590: 66 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 f it is not at a
45a0: 6c 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e ll meaningful?).
45b0: 0a 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65 .**.** By themse
45c0: 6c 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69 lves, these opti
45d0: 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66 mizations work f
45e0: 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20 ine and provide
45f0: 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 6f a handy.** perfo
4600: 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f 20 rmance boost to
4610: 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 69 bulk delete or i
4620: 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 nsert operations
4630: 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a 2a . However, if.**
4640: 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 a page is moved
4650: 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 to the free-lis
4660: 74 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 t and then reuse
4670: 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d d within the sam
4680: 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
4690: 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 , a problem come
46a0: 73 20 75 70 2e 20 49 66 20 74 68 65 20 70 61 67 s up. If the pag
46b0: 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c e is not journal
46c0: 6c 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 69 led when.** it i
46d0: 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 s moved to the f
46e0: 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 20 ree-list and it
46f0: 69 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 is also not jour
4700: 6e 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a 2a nalled when it.*
4710: 2a 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 * is extracted f
4720: 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 rom the free-lis
4730: 74 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74 68 t and reused, th
4740: 65 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 en the original
4750: 64 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 6c data.** may be l
4760: 6f 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65 6e ost. In the even
4770: 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2c t of a rollback,
4780: 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 it may not be p
4790: 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 ossible.** to re
47a0: 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 store the databa
47b0: 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e se to its origin
47c0: 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e al configuration
47d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 ..**.** The solu
47e0: 74 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53 68 tion is the BtSh
47f0: 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 ared.pHasContent
4800: 20 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 bitvec. Wheneve
4810: 72 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 r a page is .**
4820: 6d 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 moved to become
4830: 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 a free-list leaf
4840: 20 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72 65 page, the corre
4850: 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 0a sponding bit is.
4860: 2a 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62 69 ** set in the bi
4870: 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 tvec. Whenever a
4880: 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 65 78 leaf page is ex
4890: 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 tracted from the
48a0: 20 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f free-list,.** o
48b0: 70 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 62 ptimization 2 ab
48c0: 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 64 20 69 ove is omitted i
48d0: 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 f the correspond
48e0: 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65 61 ing bit is alrea
48f0: 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 dy.** set in BtS
4900: 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
4910: 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 t. The contents
4920: 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61 72 of the bitvec ar
4930: 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 e cleared.** at
4940: 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 the end of every
4950: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
4960: 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
4970: 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 eSetHasContent(B
4980: 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 tShared *pBt, Pg
4990: 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 no pgno){. int
49a0: 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
49b0: 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 if( !pBt->pHas
49c0: 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 61 Content ){. a
49d0: 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 70 42 74 ssert( pgno<=pBt
49e0: 2d 3e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 70 ->nPage );. p
49f0: 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
4a00: 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 = sqlite3BitvecC
4a10: 72 65 61 74 65 28 70 42 74 2d 3e 6e 50 61 67 65 reate(pBt->nPage
4a20: 29 3b 0a 20 20 20 20 69 66 28 20 21 70 42 74 2d );. if( !pBt-
4a30: 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b 0a >pHasContent ){.
4a40: 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
4a50: 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
4a60: 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
4a70: 49 54 45 5f 4f 4b 20 26 26 20 70 67 6e 6f 3c 3d ITE_OK && pgno<=
4a80: 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a sqlite3BitvecSiz
4a90: 65 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 e(pBt->pHasConte
4aa0: 6e 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 nt) ){. rc =
4ab0: 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
4ac0: 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e (pBt->pHasConten
4ad0: 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 t, pgno);. }.
4ae0: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
4af0: 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 42 74 .** Query the Bt
4b00: 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 Shared.pHasConte
4b10: 6e 74 20 76 65 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a nt vector..**.**
4b20: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
4b30: 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 s called when a
4b40: 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
4b50: 61 67 65 20 69 73 20 72 65 6d 6f 76 65 64 20 66 age is removed f
4b60: 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 72 65 65 2d rom the.** free-
4b70: 6c 69 73 74 20 66 6f 72 20 72 65 75 73 65 2e 20 list for reuse.
4b80: 49 74 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 It returns false
4b90: 20 69 66 20 69 74 20 69 73 20 73 61 66 65 20 74 if it is safe t
4ba0: 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a o retrieve the.*
4bb0: 2a 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 * page from the
4bc0: 70 61 67 65 72 20 6c 61 79 65 72 20 77 69 74 68 pager layer with
4bd0: 20 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 the 'no-content
4be0: 27 20 66 6c 61 67 20 73 65 74 2e 20 54 72 75 65 ' flag set. True
4bf0: 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 otherwise..*/.s
4c00: 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 tatic int btreeG
4c10: 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 etHasContent(BtS
4c20: 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f hared *pBt, Pgno
4c30: 20 70 67 6e 6f 29 7b 0a 20 20 42 69 74 76 65 63 pgno){. Bitvec
4c40: 20 2a 70 20 3d 20 70 42 74 2d 3e 70 48 61 73 43 *p = pBt->pHasC
4c50: 6f 6e 74 65 6e 74 3b 0a 20 20 72 65 74 75 72 6e ontent;. return
4c60: 20 28 70 20 26 26 20 28 70 67 6e 6f 3e 73 71 6c (p && (pgno>sql
4c70: 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 ite3BitvecSize(p
4c80: 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 ) || sqlite3Bitv
4c90: 65 63 54 65 73 74 28 70 2c 20 70 67 6e 6f 29 29 ecTest(p, pgno))
4ca0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 );.}../*.** Clea
4cb0: 72 20 28 64 65 73 74 72 6f 79 29 20 74 68 65 20 r (destroy) the
4cc0: 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e BtShared.pHasCon
4cd0: 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 tent bitvec. Thi
4ce0: 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 s should be.** i
4cf0: 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f nvoked at the co
4d00: 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61 63 68 nclusion of each
4d10: 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
4d20: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
4d30: 69 64 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 id btreeClearHas
4d40: 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
4d50: 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 *pBt){. sqlite
4d60: 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
4d70: 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 Bt->pHasContent)
4d80: 3b 0a 20 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e ;. pBt->pHasCon
4d90: 74 65 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a tent = 0;.}../*.
4da0: 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 6f ** Release all o
4db0: 66 20 74 68 65 20 61 70 50 61 67 65 5b 5d 20 70 f the apPage[] p
4dc0: 61 67 65 73 20 66 6f 72 20 61 20 63 75 72 73 6f ages for a curso
4dd0: 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
4de0: 64 20 62 74 72 65 65 52 65 6c 65 61 73 65 41 6c d btreeReleaseAl
4df0: 6c 43 75 72 73 6f 72 50 61 67 65 73 28 42 74 43 lCursorPages(BtC
4e00: 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
4e10: 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
4e20: 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 ; i<=pCur->iPage
4e30: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 ; i++){. rele
4e40: 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
4e50: 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 70 43 Page[i]);. pC
4e60: 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 ur->apPage[i] =
4e70: 30 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 69 0;. }. pCur->i
4e80: 50 61 67 65 20 3d 20 2d 31 3b 0a 7d 0a 0a 0a 2f Page = -1;.}.../
4e90: 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 *.** Save the cu
4ea0: 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 rrent cursor pos
4eb0: 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 61 72 ition in the var
4ec0: 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e iables BtCursor.
4ed0: 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 74 43 nKey .** and BtC
4ee0: 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 65 20 ursor.pKey. The
4ef0: 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 20 69 cursor's state i
4f00: 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52 5f s set to CURSOR_
4f10: 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a 2a 0a REQUIRESEEK..**.
4f20: 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 ** The caller mu
4f30: 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 st ensure that t
4f40: 68 65 20 63 75 72 73 6f 72 20 69 73 20 76 61 6c he cursor is val
4f50: 69 64 20 28 68 61 73 20 65 53 74 61 74 65 3d 3d id (has eState==
4f60: 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 0a 2a 2a CURSOR_VALID).**
4f70: 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
4f80: 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 g this routine.
4f90: 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
4fa0: 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 saveCursorPositi
4fb0: 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 on(BtCursor *pCu
4fc0: 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 r){. int rc;..
4fd0: 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f assert( CURSOR_
4fe0: 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 VALID==pCur->eSt
4ff0: 61 74 65 20 7c 7c 20 43 55 52 53 4f 52 5f 53 4b ate || CURSOR_SK
5000: 49 50 4e 45 58 54 3d 3d 70 43 75 72 2d 3e 65 53 IPNEXT==pCur->eS
5010: 74 61 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 tate );. assert
5020: 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b 65 79 20 ( 0==pCur->pKey
5030: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 );. assert( cur
5040: 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
5050: 75 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 43 ur) );.. if( pC
5060: 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
5070: 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 7b 0a 20 OR_SKIPNEXT ){.
5080: 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
5090: 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a = CURSOR_VALID;.
50a0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 }else{. pCu
50b0: 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b r->skipNext = 0;
50c0: 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
50d0: 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
50e0: 70 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 pCur, &pCur->nKe
50f0: 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 y);. assert( rc
5100: 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 ==SQLITE_OK );
5110: 2f 2a 20 4b 65 79 53 69 7a 65 28 29 20 63 61 6e /* KeySize() can
5120: 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 0a 20 20 2f not fail */.. /
5130: 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
5140: 69 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 intKey table, th
5150: 65 6e 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c en the above cal
5160: 6c 20 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a l to BtreeKeySiz
5170: 65 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 e(). ** stores
5180: 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 the integer key
5190: 69 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 in pCur->nKey. I
51a0: 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 n this case this
51b0: 20 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 value is. ** a
51c0: 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71 75 69 ll that is requi
51d0: 72 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 red. Otherwise,
51e0: 69 66 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f if pCur is not o
51f0: 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 pen on an intKey
5200: 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 . ** table, the
5210: 6e 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 n malloc space f
5220: 6f 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 or and store the
5230: 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 pCur->nKey byte
5240: 73 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 s of key . ** d
5250: 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ata.. */. if(
5260: 30 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 0==pCur->apPage[
5270: 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 0]->intKey ){.
5280: 20 20 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 void *pKey = s
5290: 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 qlite3Malloc( pC
52a0: 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 ur->nKey );.
52b0: 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 if( pKey ){.
52c0: 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
52d0: 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 reeKey(pCur, 0,
52e0: 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c (int)pCur->nKey,
52f0: 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 pKey);. if
5300: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
5310: 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d ){. pCur-
5320: 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 >pKey = pKey;.
5330: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
5340: 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
5350: 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pKey);. }.
5360: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
5370: 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
5380: 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 M;. }. }. a
5390: 73 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70 ssert( !pCur->ap
53a0: 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 Page[0]->intKey
53b0: 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 || !pCur->pKey )
53c0: 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
53d0: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 62 74 ITE_OK ){. bt
53e0: 72 65 65 52 65 6c 65 61 73 65 41 6c 6c 43 75 72 reeReleaseAllCur
53f0: 73 6f 72 50 61 67 65 73 28 70 43 75 72 29 3b 0a sorPages(pCur);.
5400: 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
5410: 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 = CURSOR_REQUIR
5420: 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e ESEEK;. }.. in
5430: 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
5440: 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 Cache(pCur);. r
5450: 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
5460: 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 Forward referenc
5470: 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 e */.static int
5480: 53 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 SQLITE_NOINLINE
5490: 73 61 76 65 43 75 72 73 6f 72 73 4f 6e 4c 69 73 saveCursorsOnLis
54a0: 74 28 42 74 43 75 72 73 6f 72 2a 2c 50 67 6e 6f t(BtCursor*,Pgno
54b0: 2c 42 74 43 75 72 73 6f 72 2a 29 3b 0a 0a 2f 2a ,BtCursor*);../*
54c0: 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 .** Save the pos
54d0: 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 itions of all cu
54e0: 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 70 45 rsors (except pE
54f0: 78 63 65 70 74 29 20 74 68 61 74 20 61 72 65 20 xcept) that are
5500: 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 open on.** the t
5510: 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
5520: 61 67 65 20 69 52 6f 6f 74 2e 20 20 22 53 61 76 age iRoot. "Sav
5530: 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 70 ing the cursor p
5540: 6f 73 69 74 69 6f 6e 22 20 6d 65 61 6e 73 20 74 osition" means t
5550: 68 61 74 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 74 hat.** the locat
5560: 69 6f 6e 20 69 6e 20 74 68 65 20 62 74 72 65 65 ion in the btree
5570: 20 69 73 20 72 65 6d 65 6d 62 65 72 65 64 20 69 is remembered i
5580: 6e 20 73 75 63 68 20 61 20 77 61 79 20 74 68 61 n such a way tha
5590: 74 20 69 74 20 63 61 6e 20 62 65 0a 2a 2a 20 6d t it can be.** m
55a0: 6f 76 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 oved back to the
55b0: 20 73 61 6d 65 20 73 70 6f 74 20 61 66 74 65 72 same spot after
55c0: 20 74 68 65 20 62 74 72 65 65 20 68 61 73 20 62 the btree has b
55d0: 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 54 een modified. T
55e0: 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 his.** routine i
55f0: 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 s called just be
5600: 66 6f 72 65 20 63 75 72 73 6f 72 20 70 45 78 63 fore cursor pExc
5610: 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d ept is used to m
5620: 6f 64 69 66 79 20 74 68 65 0a 2a 2a 20 74 61 62 odify the.** tab
5630: 6c 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 le, for example
5640: 69 6e 20 42 74 72 65 65 44 65 6c 65 74 65 28 29 in BtreeDelete()
5650: 20 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 or BtreeInsert(
5660: 29 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 )..**.** Impleme
5670: 6e 74 61 74 69 6f 6e 20 6e 6f 74 65 3a 20 20 54 ntation note: T
5680: 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 65 72 65 his routine mere
5690: 6c 79 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 ly checks to see
56a0: 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 0a if any cursors.
56b0: 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 73 61 ** need to be sa
56c0: 76 65 64 2e 20 20 49 74 20 63 61 6c 6c 73 20 6f ved. It calls o
56d0: 75 74 20 74 6f 20 73 61 76 65 43 75 72 73 6f 72 ut to saveCursor
56e0: 73 4f 6e 4c 69 73 74 28 29 20 69 6e 20 74 68 65 sOnList() in the
56f0: 20 28 75 6e 75 73 75 61 6c 29 0a 2a 2a 20 65 76 (unusual).** ev
5700: 65 6e 74 20 74 68 61 74 20 63 75 72 73 6f 72 73 ent that cursors
5710: 20 61 72 65 20 69 6e 20 6e 65 65 64 20 74 6f 20 are in need to
5720: 62 65 69 6e 67 20 73 61 76 65 64 2e 0a 2a 2f 0a being saved..*/.
5730: 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 static int saveA
5740: 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 llCursors(BtShar
5750: 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 ed *pBt, Pgno iR
5760: 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 oot, BtCursor *p
5770: 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 Except){. BtCur
5780: 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 sor *p;. assert
5790: 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
57a0: 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
57b0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
57c0: 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 xcept==0 || pExc
57d0: 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b ept->pBt==pBt );
57e0: 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 . for(p=pBt->pC
57f0: 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
5800: 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
5810: 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d !=pExcept && (0=
5820: 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e =iRoot || p->pgn
5830: 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 29 20 oRoot==iRoot) )
5840: 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 break;. }. ret
5850: 75 72 6e 20 70 20 3f 20 73 61 76 65 43 75 72 73 urn p ? saveCurs
5860: 6f 72 73 4f 6e 4c 69 73 74 28 70 2c 20 69 52 6f orsOnList(p, iRo
5870: 6f 74 2c 20 70 45 78 63 65 70 74 29 20 3a 20 53 ot, pExcept) : S
5880: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 QLITE_OK;.}../*
5890: 54 68 69 73 20 68 65 6c 70 65 72 20 72 6f 75 74 This helper rout
58a0: 69 6e 65 20 74 6f 20 73 61 76 65 41 6c 6c 43 75 ine to saveAllCu
58b0: 72 73 6f 72 73 20 64 6f 65 73 20 74 68 65 20 61 rsors does the a
58c0: 63 74 75 61 6c 20 77 6f 72 6b 20 6f 66 20 73 61 ctual work of sa
58d0: 76 69 6e 67 0a 2a 2a 20 74 68 65 20 63 75 72 73 ving.** the curs
58e0: 6f 72 73 20 69 66 20 61 6e 64 20 77 68 65 6e 20 ors if and when
58f0: 61 20 63 75 72 73 6f 72 20 69 73 20 66 6f 75 6e a cursor is foun
5900: 64 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 d that actually
5910: 72 65 71 75 69 72 65 73 20 73 61 76 69 6e 67 2e requires saving.
5920: 0a 2a 2a 20 54 68 65 20 63 6f 6d 6d 6f 6e 20 63 .** The common c
5930: 61 73 65 20 69 73 20 74 68 61 74 20 6e 6f 20 63 ase is that no c
5940: 75 72 73 6f 72 73 20 6e 65 65 64 20 74 6f 20 62 ursors need to b
5950: 65 20 73 61 76 65 64 2c 20 73 6f 20 74 68 69 73 e saved, so this
5960: 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 62 routine is.** b
5970: 72 6f 6b 65 6e 20 6f 75 74 20 66 72 6f 6d 20 69 roken out from i
5980: 74 73 20 63 61 6c 6c 65 72 20 74 6f 20 61 76 6f ts caller to avo
5990: 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 20 73 id unnecessary s
59a0: 74 61 63 6b 20 70 6f 69 6e 74 65 72 20 6d 6f 76 tack pointer mov
59b0: 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 ement..*/.static
59c0: 20 69 6e 74 20 53 51 4c 49 54 45 5f 4e 4f 49 4e int SQLITE_NOIN
59d0: 4c 49 4e 45 20 73 61 76 65 43 75 72 73 6f 72 73 LINE saveCursors
59e0: 4f 6e 4c 69 73 74 28 0a 20 20 42 74 43 75 72 73 OnList(. BtCurs
59f0: 6f 72 20 2a 70 2c 20 20 20 20 20 20 20 20 20 2f or *p, /
5a00: 2a 20 54 68 65 20 66 69 72 73 74 20 63 75 72 73 * The first curs
5a10: 6f 72 20 74 68 61 74 20 6e 65 65 64 73 20 73 61 or that needs sa
5a20: 76 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 ving */. Pgno i
5a30: 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 2f Root, /
5a40: 2a 20 4f 6e 6c 79 20 73 61 76 65 20 63 75 72 73 * Only save curs
5a50: 6f 72 20 77 69 74 68 20 74 68 69 73 20 69 52 6f or with this iRo
5a60: 6f 74 2e 20 53 61 76 65 20 61 6c 6c 20 69 66 20 ot. Save all if
5a70: 7a 65 72 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 zero */. BtCurs
5a80: 6f 72 20 2a 70 45 78 63 65 70 74 20 20 20 20 2f or *pExcept /
5a90: 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 * Do not save th
5aa0: 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a is cursor */.){.
5ab0: 20 20 64 6f 7b 0a 20 20 20 20 69 66 28 20 70 21 do{. if( p!
5ac0: 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d =pExcept && (0==
5ad0: 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f iRoot || p->pgno
5ae0: 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 29 7b 0a Root==iRoot) ){.
5af0: 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 53 74 if( p->eSt
5b00: 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
5b10: 44 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d D || p->eState==
5b20: 43 55 52 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 CURSOR_SKIPNEXT
5b30: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 ){. int r
5b40: 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f c = saveCursorPo
5b50: 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 sition(p);.
5b60: 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
5b70: 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 !=rc ){.
5b80: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
5b90: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
5ba0: 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 se{. test
5bb0: 63 61 73 65 28 20 70 2d 3e 69 50 61 67 65 3e 30 case( p->iPage>0
5bc0: 20 29 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 );. btre
5bd0: 65 52 65 6c 65 61 73 65 41 6c 6c 43 75 72 73 6f eReleaseAllCurso
5be0: 72 50 61 67 65 73 28 70 29 3b 0a 20 20 20 20 20 rPages(p);.
5bf0: 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d }. }. p =
5c00: 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 77 68 p->pNext;. }wh
5c10: 69 6c 65 28 20 70 20 29 3b 0a 20 20 72 65 74 75 ile( p );. retu
5c20: 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
5c30: 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
5c40: 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 current cursor
5c50: 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 position..*/.voi
5c60: 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c d sqlite3BtreeCl
5c70: 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 earCursor(BtCurs
5c80: 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
5c90: 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
5ca0: 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
5cb0: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
5cc0: 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 ur->pKey);. pCu
5cd0: 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 r->pKey = 0;. p
5ce0: 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
5cf0: 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a RSOR_INVALID;.}.
5d00: 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76 ./*.** In this v
5d10: 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d ersion of BtreeM
5d20: 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61 oveto, pKey is a
5d30: 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65 packed index re
5d40: 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20 cord.** such as
5d50: 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 is generated by
5d60: 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 the OP_MakeRecor
5d70: 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63 d opcode. Unpac
5d80: 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 k the.** record
5d90: 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74 and then call Bt
5da0: 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
5db0: 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f d() to do the wo
5dc0: 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e rk..*/.static in
5dd0: 74 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 t btreeMoveto(.
5de0: 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
5df0: 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f /* Cursor o
5e00: 70 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 65 pen on the btree
5e10: 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 to be searched
5e20: 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
5e30: 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b *pKey, /* Pack
5e40: 65 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 74 ed key if the bt
5e50: 72 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20 ree is an index
5e60: 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 */. i64 nKey,
5e70: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 /* Inte
5e80: 67 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 6c ger key for tabl
5e90: 65 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 es. Size of pKe
5ea0: 79 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f y for indices */
5eb0: 0a 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 20 . int bias,
5ec0: 20 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 73 /* Bias s
5ed0: 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 earch to the hig
5ee0: 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a h end */. int *
5ef0: 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 2f pRes /
5f00: 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 * Write search r
5f10: 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 esults here */.)
5f20: 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
5f30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5f40: 2a 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a 2f * Status code */
5f50: 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
5f60: 64 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a d *pIdxKey; /*
5f70: 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 Unpacked index
5f80: 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20 61 53 key */. char aS
5f90: 70 61 63 65 5b 32 30 30 5d 3b 20 20 20 20 20 20 pace[200];
5fa0: 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 /* Temp spac
5fb0: 65 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 e for pIdxKey -
5fc0: 74 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f to avoid a mallo
5fd0: 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 46 72 c */. char *pFr
5fe0: 65 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 ee = 0;.. if( p
5ff0: 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 72 Key ){. asser
6000: 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 29 28 69 t( nKey==(i64)(i
6010: 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 20 20 70 nt)nKey );. p
6020: 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 IdxKey = sqlite3
6030: 56 64 62 65 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 VdbeAllocUnpacke
6040: 64 52 65 63 6f 72 64 28 0a 20 20 20 20 20 20 20 dRecord(.
6050: 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c pCur->pKeyInfo,
6060: 20 61 53 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 aSpace, sizeof(
6070: 61 53 70 61 63 65 29 2c 20 26 70 46 72 65 65 0a aSpace), &pFree.
6080: 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 );. if( p
6090: 49 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 IdxKey==0 ) retu
60a0: 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
60b0: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
60c0: 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 75 RecordUnpack(pCu
60d0: 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69 6e r->pKeyInfo, (in
60e0: 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 20 70 49 t)nKey, pKey, pI
60f0: 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 dxKey);. if(
6100: 70 49 64 78 4b 65 79 2d 3e 6e 46 69 65 6c 64 3d pIdxKey->nField=
6110: 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
6120: 74 65 33 44 62 46 72 65 65 28 70 43 75 72 2d 3e te3DbFree(pCur->
6130: 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 46 pKeyInfo->db, pF
6140: 72 65 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ree);. retu
6150: 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
6160: 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
6170: 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
6180: 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
6190: 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
61a0: 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
61b0: 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
61c0: 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
61d0: 20 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 if( pFree ){.
61e0: 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
61f0: 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d (pCur->pKeyInfo-
6200: 3e 64 62 2c 20 70 46 72 65 65 29 3b 0a 20 20 7d >db, pFree);. }
6210: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
6220: 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 ./*.** Restore t
6230: 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
6240: 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 position it was
6250: 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 in (or as close
6260: 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 to as possible)
6270: 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 .** when saveCur
6280: 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 sorPosition() wa
6290: 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 s called. Note t
62a0: 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 hat this call de
62b0: 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 letes the .** sa
62c0: 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 ved position inf
62d0: 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 o stored by save
62e0: 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
62f0: 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 , so there can b
6300: 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 e.** at most one
6310: 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f effective resto
6320: 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
6330: 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 () call after ea
6340: 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f ch .** saveCurso
6350: 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a rPosition()..*/.
6360: 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
6370: 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 RestoreCursorPos
6380: 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a ition(BtCursor *
6390: 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b pCur){. int rc;
63a0: 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b . int skipNext;
63b0: 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
63c0: 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
63d0: 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
63e0: 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 Cur->eState>=CUR
63f0: 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
6400: 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 );. if( pCur->e
6410: 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 State==CURSOR_FA
6420: 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ULT ){. retur
6430: 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 n pCur->skipNext
6440: 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 ;. }. pCur->eS
6450: 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
6460: 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 VALID;. rc = bt
6470: 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 reeMoveto(pCur,
6480: 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 pCur->pKey, pCur
6490: 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 73 6b 69 70 ->nKey, 0, &skip
64a0: 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d Next);. if( rc=
64b0: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
64c0: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
64d0: 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 Cur->pKey);.
64e0: 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a pCur->pKey = 0;.
64f0: 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
6500: 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
6510: 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e _VALID || pCur->
6520: 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
6530: 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 70 43 NVALID );. pC
6540: 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 7c 3d 20 ur->skipNext |=
6550: 73 6b 69 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 skipNext;. if
6560: 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 ( pCur->skipNext
6570: 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 && pCur->eState
6580: 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
6590: 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 {. pCur->eS
65a0: 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 53 4b tate = CURSOR_SK
65b0: 49 50 4e 45 58 54 3b 0a 20 20 20 20 7d 0a 20 20 IPNEXT;. }.
65c0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
65d0: 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 ..#define restor
65e0: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
65f0: 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 p) \. (p->eStat
6600: 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 e>=CURSOR_REQUIR
6610: 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 ESEEK ? \.
6620: 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 43 btreeRestoreC
6630: 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 ursorPosition(p)
6640: 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 51 : \. SQ
6650: 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 LITE_OK)../*.**
6660: 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 Determine whethe
6670: 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 6f r or not a curso
6680: 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d r has moved from
6690: 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 77 68 the position wh
66a0: 65 72 65 0a 2a 2a 20 69 74 20 77 61 73 20 6c 61 ere.** it was la
66b0: 73 74 20 70 6c 61 63 65 64 2c 20 6f 72 20 68 61 st placed, or ha
66c0: 73 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 s been invalidat
66d0: 65 64 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 ed for any other
66e0: 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 43 75 72 73 reason..** Curs
66f0: 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 ors can move whe
6700: 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 n the row they a
6710: 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 69 re pointing at i
6720: 73 20 64 65 6c 65 74 65 64 20 6f 75 74 0a 2a 2a s deleted out.**
6730: 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d from under them
6740: 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 20 20 , for example.
6750: 43 75 72 73 6f 72 20 6d 69 67 68 74 20 61 6c 73 Cursor might als
6760: 6f 20 6d 6f 76 65 20 69 66 20 61 20 62 74 72 65 o move if a btre
6770: 65 0a 2a 2a 20 69 73 20 72 65 62 61 6c 61 6e 63 e.** is rebalanc
6780: 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e ed..**.** Callin
6790: 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 g this routine w
67a0: 69 74 68 20 61 20 4e 55 4c 4c 20 63 75 72 73 6f ith a NULL curso
67b0: 72 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e r pointer return
67c0: 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 55 s false..**.** U
67d0: 73 65 20 74 68 65 20 73 65 70 61 72 61 74 65 20 se the separate
67e0: 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
67f0: 6f 72 52 65 73 74 6f 72 65 28 29 20 72 6f 75 74 orRestore() rout
6800: 69 6e 65 20 74 6f 20 72 65 73 74 6f 72 65 20 61 ine to restore a
6810: 20 63 75 72 73 6f 72 0a 2a 2a 20 62 61 63 6b 20 cursor.** back
6820: 74 6f 20 77 68 65 72 65 20 69 74 20 6f 75 67 68 to where it ough
6830: 74 20 74 6f 20 62 65 20 69 66 20 74 68 69 73 20 t to be if this
6840: 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
6850: 74 72 75 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c true..*/.int sql
6860: 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 ite3BtreeCursorH
6870: 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 asMoved(BtCursor
6880: 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75 72 *pCur){. retur
6890: 6e 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d n pCur->eState!=
68a0: 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a CURSOR_VALID;.}.
68b0: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
68c0: 69 6e 65 20 72 65 73 74 6f 72 65 73 20 61 20 63 ine restores a c
68d0: 75 72 73 6f 72 20 62 61 63 6b 20 74 6f 20 69 74 ursor back to it
68e0: 73 20 6f 72 69 67 69 6e 61 6c 20 70 6f 73 69 74 s original posit
68f0: 69 6f 6e 20 61 66 74 65 72 20 69 74 0a 2a 2a 20 ion after it.**
6900: 68 61 73 20 62 65 65 6e 20 6d 6f 76 65 64 20 62 has been moved b
6910: 79 20 73 6f 6d 65 20 6f 75 74 73 69 64 65 20 61 y some outside a
6920: 63 74 69 76 69 74 79 20 28 73 75 63 68 20 61 73 ctivity (such as
6930: 20 61 20 62 74 72 65 65 20 72 65 62 61 6c 61 6e a btree rebalan
6940: 63 65 20 6f 72 0a 2a 2a 20 61 20 72 6f 77 20 68 ce or.** a row h
6950: 61 76 69 6e 67 20 62 65 65 6e 20 64 65 6c 65 74 aving been delet
6960: 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 ed out from unde
6970: 72 20 74 68 65 20 63 75 72 73 6f 72 29 2e 20 20 r the cursor).
6980: 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 .**.** On succes
6990: 73 2c 20 74 68 65 20 2a 70 44 69 66 66 65 72 65 s, the *pDiffere
69a0: 6e 74 52 6f 77 20 70 61 72 61 6d 65 74 65 72 20 ntRow parameter
69b0: 69 73 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 is false if the
69c0: 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a cursor is left.*
69d0: 2a 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 78 * pointing at ex
69e0: 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 72 actly the same r
69f0: 6f 77 2e 20 20 2a 70 44 69 66 66 65 72 6e 74 52 ow. *pDifferntR
6a00: 6f 77 20 69 73 20 74 68 65 20 72 6f 77 20 74 68 ow is the row th
6a10: 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 e cursor.** was
6a20: 70 6f 69 6e 74 69 6e 67 20 74 6f 20 68 61 73 20 pointing to has
6a30: 62 65 65 6e 20 64 65 6c 65 74 65 64 2c 20 66 6f been deleted, fo
6a40: 72 63 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 rcing the cursor
6a50: 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d to point to som
6a60: 65 0a 2a 2a 20 6e 65 61 72 62 79 20 72 6f 77 2e e.** nearby row.
6a70: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
6a80: 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 ine should only
6a90: 62 65 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 be called for a
6aa0: 63 75 72 73 6f 72 20 74 68 61 74 20 6a 75 73 74 cursor that just
6ab0: 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 54 52 55 returned.** TRU
6ac0: 45 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 42 74 E from sqlite3Bt
6ad0: 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
6ae0: 64 28 29 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 d()..*/.int sqli
6af0: 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 52 65 te3BtreeCursorRe
6b00: 73 74 6f 72 65 28 42 74 43 75 72 73 6f 72 20 2a store(BtCursor *
6b10: 70 43 75 72 2c 20 69 6e 74 20 2a 70 44 69 66 66 pCur, int *pDiff
6b20: 65 72 65 6e 74 52 6f 77 29 7b 0a 20 20 69 6e 74 erentRow){. int
6b30: 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
6b40: 70 43 75 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 pCur!=0 );. ass
6b50: 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
6b60: 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
6b70: 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 );. rc = restor
6b80: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
6b90: 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 pCur);. if( rc
6ba0: 29 7b 0a 20 20 20 20 2a 70 44 69 66 66 65 72 65 ){. *pDiffere
6bb0: 6e 74 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 72 ntRow = 1;. r
6bc0: 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
6bd0: 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
6be0: 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 !=CURSOR_VALID )
6bf0: 7b 0a 20 20 20 20 2a 70 44 69 66 66 65 72 65 6e {. *pDifferen
6c00: 74 52 6f 77 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 tRow = 1;. }els
6c10: 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
6c20: 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3d 3d 30 Cur->skipNext==0
6c30: 20 29 3b 0a 20 20 20 20 2a 70 44 69 66 66 65 72 );. *pDiffer
6c40: 65 6e 74 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 0a entRow = 0;. }.
6c50: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
6c60: 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 OK;.}..#ifndef S
6c70: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
6c80: 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 ACUUM./*.** Give
6c90: 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 n a page number
6ca0: 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64 61 74 of a regular dat
6cb0: 61 62 61 73 65 20 70 61 67 65 2c 20 72 65 74 75 abase page, retu
6cc0: 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e rn the page.** n
6cd0: 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 6f umber for the po
6ce0: 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 74 inter-map page t
6cf0: 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
6d00: 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 0a 2a entry for the.*
6d10: 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e 75 6d * input page num
6d20: 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ber..**.** Retur
6d30: 6e 20 30 20 28 6e 6f 74 20 61 20 76 61 6c 69 64 n 0 (not a valid
6d40: 20 70 61 67 65 29 20 66 6f 72 20 70 67 6e 6f 3d page) for pgno=
6d50: 3d 31 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 =1 since there i
6d60: 73 0a 2a 2a 20 6e 6f 20 70 6f 69 6e 74 65 72 20 s.** no pointer
6d70: 6d 61 70 20 61 73 73 6f 63 69 61 74 65 64 20 77 map associated w
6d80: 69 74 68 20 70 61 67 65 20 31 2e 20 20 54 68 65 ith page 1. The
6d90: 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b integrity_check
6da0: 20 6c 6f 67 69 63 0a 2a 2a 20 72 65 71 75 69 72 logic.** requir
6db0: 65 73 20 74 68 61 74 20 70 74 72 6d 61 70 50 61 es that ptrmapPa
6dc0: 67 65 6e 6f 28 2a 2c 31 29 21 3d 31 2e 0a 2a 2f geno(*,1)!=1..*/
6dd0: 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72 .static Pgno ptr
6de0: 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72 mapPageno(BtShar
6df0: 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 ed *pBt, Pgno pg
6e00: 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 no){. int nPage
6e10: 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 sPerMapPage;. P
6e20: 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72 65 74 gno iPtrMap, ret
6e30: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
6e40: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
6e50: 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
6e60: 69 66 28 20 70 67 6e 6f 3c 32 20 29 20 72 65 74 if( pgno<2 ) ret
6e70: 75 72 6e 20 30 3b 0a 20 20 6e 50 61 67 65 73 50 urn 0;. nPagesP
6e80: 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 74 erMapPage = (pBt
6e90: 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b ->usableSize/5)+
6ea0: 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 28 1;. iPtrMap = (
6eb0: 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 pgno-2)/nPagesPe
6ec0: 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 20 rMapPage;. ret
6ed0: 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 = (iPtrMap*nPage
6ee0: 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 32 sPerMapPage) + 2
6ef0: 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 45 ; . if( ret==PE
6f00: 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
6f10: 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 2b pBt) ){. ret+
6f20: 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 +;. }. return
6f30: 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 ret;.}../*.** Wr
6f40: 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 ite an entry int
6f50: 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 o the pointer ma
6f60: 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f p..**.** This ro
6f70: 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 68 utine updates th
6f80: 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
6f90: 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 6d try for page num
6fa0: 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 ber 'key'.** so
6fb0: 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f 20 that it maps to
6fc0: 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e 64 type 'eType' and
6fd0: 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d parent page num
6fe0: 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a ber 'pgno'..**.*
6ff0: 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e 69 * If *pRC is ini
7000: 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 tially non-zero
7010: 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 (non-SQLITE_OK)
7020: 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
7030: 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e e is.** a no-op.
7040: 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 If an error oc
7050: 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f 70 curs, the approp
7060: 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 riate error code
7070: 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 is written.** i
7080: 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 nto *pRC..*/.sta
7090: 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 tic void ptrmapP
70a0: 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ut(BtShared *pBt
70b0: 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 , Pgno key, u8 e
70c0: 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e Type, Pgno paren
70d0: 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 t, int *pRC){.
70e0: 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
70f0: 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
7100: 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 map page */. u
7110: 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 8 *pPtrmap;
7120: 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 /* The pointer
7130: 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 map data */. Pg
7140: 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 no iPtrmap;
7150: 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d /* The pointer m
7160: 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a ap page number *
7170: 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 /. int offset;
7180: 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
7190: 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 in pointer map p
71a0: 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b age */. int rc;
71b0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
71c0: 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 turn code from s
71d0: 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a ubfunctions */..
71e0: 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 if( *pRC ) ret
71f0: 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 urn;.. assert(
7200: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
7210: 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
7220: 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 ;. /* The maste
7230: 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e r-journal page n
7240: 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 umber must never
7250: 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70 6f be used as a po
7260: 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a inter map page *
7270: 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 /. assert( 0==P
7280: 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
7290: 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 , PENDING_BYTE_P
72a0: 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 AGE(pBt)) );..
72b0: 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 assert( pBt->aut
72c0: 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 oVacuum );. if(
72d0: 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a key==0 ){. *
72e0: 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 pRC = SQLITE_COR
72f0: 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 RUPT_BKPT;. r
7300: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 eturn;. }. iPt
7310: 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 rmap = PTRMAP_PA
7320: 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a GENO(pBt, key);.
7330: 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
7340: 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 gerGet(pBt->pPag
7350: 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 er, iPtrmap, &pD
7360: 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
7370: 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
7380: 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 *pRC = rc;.
7390: 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
73a0: 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f offset = PTRMAP_
73b0: 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 PTROFFSET(iPtrma
73c0: 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f p, key);. if( o
73d0: 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 2a ffset<0 ){. *
73e0: 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 pRC = SQLITE_COR
73f0: 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 RUPT_BKPT;. g
7400: 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74 3b oto ptrmap_exit;
7410: 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f . }. assert( o
7420: 66 66 73 65 74 20 3c 3d 20 28 69 6e 74 29 70 42 ffset <= (int)pB
7430: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 35 20 t->usableSize-5
7440: 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 );. pPtrmap = (
7450: 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 u8 *)sqlite3Page
7460: 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
7470: 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21 );.. if( eType!
7480: 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d =pPtrmap[offset]
7490: 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70 50 || get4byte(&pP
74a0: 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 trmap[offset+1])
74b0: 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 !=parent ){.
74c0: 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55 TRACE(("PTRMAP_U
74d0: 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 PDATE: %d->(%d,%
74e0: 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 d)\n", key, eTyp
74f0: 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 e, parent));.
7500: 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 6c 69 *pRC= rc = sqli
7510: 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 te3PagerWrite(pD
7520: 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
7530: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
7540: 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b 6f . pPtrmap[o
7550: 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a ffset] = eType;.
7560: 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
7570: 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 pPtrmap[offset+1
7580: 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 ], parent);.
7590: 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 78 }. }..ptrmap_ex
75a0: 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 it:. sqlite3Pag
75b0: 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 erUnref(pDbPage)
75c0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
75d0: 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 an entry from th
75e0: 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a e pointer map..*
75f0: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
7600: 65 20 72 65 74 72 69 65 76 65 73 20 74 68 65 20 e retrieves the
7610: 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
7620: 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79 27 y for page 'key'
7630: 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 , writing.** the
7640: 20 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e 74 type and parent
7650: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 page number to
7660: 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50 67 *pEType and *pPg
7670: 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e no respectively.
7680: 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 .** An error cod
7690: 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 e is returned if
76a0: 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 something goes
76b0: 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 wrong, otherwise
76c0: 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 SQLITE_OK..*/.s
76d0: 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 tatic int ptrmap
76e0: 47 65 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 Get(BtShared *pB
76f0: 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 t, Pgno key, u8
7700: 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 *pEType, Pgno *p
7710: 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 Pgno){. DbPage
7720: 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 *pDbPage; /* T
7730: 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 he pointer map p
7740: 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 age */. int iPt
7750: 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 rmap; /* P
7760: 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 ointer map page
7770: 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 index */. u8 *p
7780: 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a Ptrmap; /*
7790: 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 Pointer map pag
77a0: 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 e data */. int
77b0: 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f offset; /
77c0: 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72 * Offset of entr
77d0: 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 y in pointer map
77e0: 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 */. int rc;..
77f0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
7800: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
7810: 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 >mutex) );.. iP
7820: 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 trmap = PTRMAP_P
7830: 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b AGENO(pBt, key);
7840: 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
7850: 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 agerGet(pBt->pPa
7860: 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 ger, iPtrmap, &p
7870: 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
7880: 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 c!=0 ){. retu
7890: 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 rn rc;. }. pPt
78a0: 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c rmap = (u8 *)sql
78b0: 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
78c0: 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 (pDbPage);.. of
78d0: 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 fset = PTRMAP_PT
78e0: 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c ROFFSET(iPtrmap,
78f0: 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 key);. if( off
7900: 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c set<0 ){. sql
7910: 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
7920: 44 62 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 DbPage);. ret
7930: 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
7940: 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 PT_BKPT;. }. a
7950: 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 3c 3d ssert( offset <=
7960: 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61 62 6c (int)pBt->usabl
7970: 65 53 69 7a 65 2d 35 20 29 3b 0a 20 20 61 73 73 eSize-5 );. ass
7980: 65 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 ert( pEType!=0 )
7990: 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 ;. *pEType = pP
79a0: 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 trmap[offset];.
79b0: 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 if( pPgno ) *pP
79c0: 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 gno = get4byte(&
79d0: 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 pPtrmap[offset+1
79e0: 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 ]);.. sqlite3Pa
79f0: 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 gerUnref(pDbPage
7a00: 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 );. if( *pEType
7a10: 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 <1 || *pEType>5
7a20: 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
7a30: 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
7a40: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
7a50: 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 ;.}..#else /* if
7a60: 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f defined SQLITE_
7a70: 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 OMIT_AUTOVACUUM
7a80: 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 */. #define ptr
7a90: 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 mapPut(w,x,y,z,r
7aa0: 63 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 c). #define ptr
7ab0: 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 mapGet(w,x,y,z)
7ac0: 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 SQLITE_OK. #def
7ad0: 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 ine ptrmapPutOvf
7ae0: 6c 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 lPtr(x, y, rc).#
7af0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 endif../*.** Giv
7b00: 65 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 en a btree page
7b10: 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 and a cell index
7b20: 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 (0 means the fi
7b30: 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 rst cell on.** t
7b40: 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 he page, 1 means
7b50: 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c the second cell
7b60: 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 , and so forth)
7b70: 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
7b80: 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 .** to the cell
7b90: 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 content..**.** T
7ba0: 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b his routine work
7bb0: 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 s only for pages
7bc0: 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e that do not con
7bd0: 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 tain overflow ce
7be0: 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lls..*/.#define
7bf0: 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a findCell(P,I) \.
7c00: 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 ((P)->aData +
7c10: 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 ((P)->maskPage &
7c20: 20 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e get2byte(&(P)->
7c30: 61 43 65 6c 6c 49 64 78 5b 32 2a 28 49 29 5d 29 aCellIdx[2*(I)])
7c40: 29 29 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 )).#define findC
7c50: 65 6c 6c 76 32 28 44 2c 4d 2c 4f 2c 49 29 20 28 ellv2(D,M,O,I) (
7c60: 44 2b 28 4d 26 67 65 74 32 62 79 74 65 28 44 2b D+(M&get2byte(D+
7c70: 28 4f 2b 32 2a 28 49 29 29 29 29 29 0a 0a 0a 2f (O+2*(I))))).../
7c80: 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 *.** This a more
7c90: 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e complex version
7ca0: 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 of findCell() t
7cb0: 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a hat works for.**
7cc0: 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 63 pages that do c
7cd0: 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 ontain overflow
7ce0: 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 cells..*/.static
7cf0: 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f u8 *findOverflo
7d00: 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 wCell(MemPage *p
7d10: 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 Page, int iCell)
7d20: 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 {. int i;. ass
7d30: 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
7d40: 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
7d50: 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
7d60: 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 for(i=pPage->nOv
7d70: 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 erflow-1; i>=0;
7d80: 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b i--){. int k;
7d90: 0a 20 20 20 20 6b 20 3d 20 70 50 61 67 65 2d 3e . k = pPage->
7da0: 61 69 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 69 aiOvfl[i];. i
7db0: 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20 f( k<=iCell ){.
7dc0: 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c if( k==iCel
7dd0: 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 l ){. ret
7de0: 75 72 6e 20 70 50 61 67 65 2d 3e 61 70 4f 76 66 urn pPage->apOvf
7df0: 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 l[i];. }.
7e00: 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 iCell--;.
7e10: 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
7e20: 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
7e30: 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a iCell);.}../*.**
7e40: 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f Parse a cell co
7e50: 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 ntent block and
7e60: 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c fill in the Cell
7e70: 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 Info structure.
7e80: 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 There.** are tw
7e90: 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 o versions of th
7ea0: 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 62 74 is function. bt
7eb0: 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 reeParseCell() t
7ec0: 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 akes a .** cell
7ed0: 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 65 63 index as the sec
7ee0: 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 ond argument and
7ef0: 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
7f00: 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 tr() .** takes a
7f10: 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
7f20: 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c body of the cell
7f30: 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 as its second a
7f40: 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 rgument..*/.stat
7f50: 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 ic void btreePar
7f60: 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d seCellPtr(. Mem
7f70: 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
7f80: 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e /* Page con
7f90: 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c taining the cell
7fa0: 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
7fb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7fc0: 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
7fd0: 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 cell text. */.
7fe0: 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 CellInfo *pInfo
7ff0: 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 /* Fill
8000: 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
8010: 65 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 49 e */.){. u8 *pI
8020: 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ter;
8030: 20 20 2f 2a 20 46 6f 72 20 73 63 61 6e 6e 69 6e /* For scannin
8040: 67 20 74 68 72 6f 75 67 68 20 70 43 65 6c 6c 20 g through pCell
8050: 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 */. u32 nPayloa
8060: 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d; /*
8070: 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
8080: 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 of cell payload
8090: 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
80a0: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
80b0: 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
80c0: 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
80d0: 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 pPage->leaf==0
80e0: 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d || pPage->leaf==
80f0: 31 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 1 );. if( pPage
8100: 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 29 7b 0a ->intKeyLeaf ){.
8110: 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
8120: 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3d e->childPtrSize=
8130: 3d 30 20 29 3b 0a 20 20 20 20 70 49 74 65 72 20 =0 );. pIter
8140: 3d 20 70 43 65 6c 6c 20 2b 20 67 65 74 56 61 72 = pCell + getVar
8150: 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 6e 50 61 int32(pCell, nPa
8160: 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 74 65 yload);. pIte
8170: 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28 70 r += getVarint(p
8180: 49 74 65 72 2c 20 28 75 36 34 2a 29 26 70 49 6e Iter, (u64*)&pIn
8190: 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c fo->nKey);. }el
81a0: 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 6f se if( pPage->no
81b0: 50 61 79 6c 6f 61 64 20 29 7b 0a 20 20 20 20 61 Payload ){. a
81c0: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 63 68 ssert( pPage->ch
81d0: 69 6c 64 50 74 72 53 69 7a 65 3d 3d 34 20 29 3b ildPtrSize==4 );
81e0: 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a . pInfo->nSiz
81f0: 65 20 3d 20 34 20 2b 20 67 65 74 56 61 72 69 6e e = 4 + getVarin
8200: 74 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 28 75 36 t(&pCell[4], (u6
8210: 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 4*)&pInfo->nKey)
8220: 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 ;. pInfo->nPa
8230: 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 70 yload = 0;. p
8240: 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 30 Info->nLocal = 0
8250: 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 ;. pInfo->iOv
8260: 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 erflow = 0;.
8270: 70 49 6e 66 6f 2d 3e 70 50 61 79 6c 6f 61 64 20 pInfo->pPayload
8280: 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b = 0;. return;
8290: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
82a0: 74 65 72 20 3d 20 70 43 65 6c 6c 20 2b 20 70 50 ter = pCell + pP
82b0: 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
82c0: 65 3b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 e;. pIter +=
82d0: 67 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 getVarint32(pIte
82e0: 72 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 r, nPayload);.
82f0: 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 pInfo->nKey =
8300: 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 nPayload;. }.
8310: 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 pInfo->nPayload
8320: 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 = nPayload;. pI
8330: 6e 66 6f 2d 3e 70 50 61 79 6c 6f 61 64 20 3d 20 nfo->pPayload =
8340: 70 49 74 65 72 3b 0a 20 20 74 65 73 74 63 61 73 pIter;. testcas
8350: 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 e( nPayload==pPa
8360: 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a ge->maxLocal );.
8370: 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 testcase( nPay
8380: 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 load==pPage->max
8390: 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 Local+1 );. if(
83a0: 20 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 nPayload<=pPage
83b0: 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 ->maxLocal ){.
83c0: 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 /* This is the
83d0: 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 (easy) common c
83e0: 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e ase where the en
83f0: 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 tire payload fit
8400: 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 s. ** on the
8410: 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 local page. No
8420: 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 overflow is requ
8430: 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
8440: 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 pInfo->nSize =
8450: 6e 50 61 79 6c 6f 61 64 20 2b 20 28 75 31 36 29 nPayload + (u16)
8460: 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b (pIter - pCell);
8470: 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e . if( pInfo->
8480: 6e 53 69 7a 65 3c 34 20 29 20 70 49 6e 66 6f 2d nSize<4 ) pInfo-
8490: 3e 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 20 20 >nSize = 4;.
84a0: 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 pInfo->nLocal =
84b0: 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 (u16)nPayload;.
84c0: 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 pInfo->iOverf
84d0: 6c 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 low = 0;. }else
84e0: 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 {. /* If the
84f0: 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 payload will not
8500: 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 fit completely
8510: 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 on the local pag
8520: 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a e, we have. *
8530: 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f 77 20 * to decide how
8540: 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 6c 6f much to store lo
8550: 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d 75 cally and how mu
8560: 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f ch to spill onto
8570: 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 . ** overflow
8580: 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 74 72 pages. The str
8590: 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 6e 69 ategy is to mini
85a0: 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 mize the amount
85b0: 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 2a 2a of unused. **
85c0: 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c space on overfl
85d0: 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 20 6b ow pages while k
85e0: 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e eeping the amoun
85f0: 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61 t of local stora
8600: 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65 74 ge. ** in bet
8610: 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e ween minLocal an
8620: 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 d maxLocal..
8630: 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69 6e **. ** Warnin
8640: 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 68 65 g: changing the
8650: 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 way overflow pa
8660: 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 69 62 yload is distrib
8670: 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 20 20 uted in any.
8680: 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 73 75 ** way will resu
8690: 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 lt in an incompa
86a0: 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d 61 tible file forma
86b0: 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e t.. */. in
86c0: 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 t minLocal; /*
86d0: 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f Minimum amount o
86e0: 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c f payload held l
86f0: 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e ocally */. in
8700: 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 t maxLocal; /*
8710: 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f Maximum amount o
8720: 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c f payload held l
8730: 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e ocally */. in
8740: 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 t surplus; /*
8750: 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 Overflow payload
8760: 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c available for l
8770: 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a ocal storage */.
8780: 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 . minLocal =
8790: 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b pPage->minLocal;
87a0: 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 . maxLocal =
87b0: 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b pPage->maxLocal;
87c0: 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d 20 6d . surplus = m
87d0: 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79 6c inLocal + (nPayl
87e0: 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 oad - minLocal)%
87f0: 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 (pPage->pBt->usa
8800: 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 bleSize - 4);.
8810: 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70 testcase( surp
8820: 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b lus==maxLocal );
8830: 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 . testcase( s
8840: 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c urplus==maxLocal
8850: 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 73 75 +1 );. if( su
8860: 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 rplus <= maxLoca
8870: 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f l ){. pInfo
8880: 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 ->nLocal = (u16)
8890: 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c surplus;. }el
88a0: 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d se{. pInfo-
88b0: 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6d >nLocal = (u16)m
88c0: 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 inLocal;. }.
88d0: 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 pInfo->iOverf
88e0: 6c 6f 77 20 3d 20 28 75 31 36 29 28 26 70 49 6e low = (u16)(&pIn
88f0: 66 6f 2d 3e 70 50 61 79 6c 6f 61 64 5b 70 49 6e fo->pPayload[pIn
8900: 66 6f 2d 3e 6e 4c 6f 63 61 6c 5d 20 2d 20 70 43 fo->nLocal] - pC
8910: 65 6c 6c 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d ell);. pInfo-
8920: 3e 6e 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e >nSize = pInfo->
8930: 69 4f 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 iOverflow + 4;.
8940: 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
8950: 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
8960: 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
8970: 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 e, /* Pa
8980: 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ge containing th
8990: 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 e cell */. int
89a0: 69 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 iCell,
89b0: 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 /* The cell
89c0: 69 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 index. First ce
89d0: 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c ll is 0 */. Cel
89e0: 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 lInfo *pInfo
89f0: 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 /* Fill in
8a00: 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
8a10: 2f 0a 29 7b 0a 20 20 62 74 72 65 65 50 61 72 73 /.){. btreePars
8a20: 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
8a30: 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
8a40: 69 43 65 6c 6c 29 2c 20 70 49 6e 66 6f 29 3b 0a iCell), pInfo);.
8a50: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
8a60: 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
8a70: 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 r of bytes that
8a80: 61 20 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 a Cell needs in
8a90: 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 the cell.** data
8aa0: 20 61 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 area of the btr
8ab0: 65 65 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 ee-page. The re
8ac0: 74 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c turn number incl
8ad0: 75 64 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a udes the cell.**
8ae0: 20 64 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 data header and
8af0: 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f the local paylo
8b00: 61 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 ad, but not any
8b10: 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 overflow page or
8b20: 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 .** the space us
8b30: 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 ed by the cell p
8b40: 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 ointer..*/.stati
8b50: 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 c u16 cellSizePt
8b60: 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 r(MemPage *pPage
8b70: 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 , u8 *pCell){.
8b80: 75 38 20 2a 70 49 74 65 72 20 3d 20 70 43 65 6c u8 *pIter = pCel
8b90: 6c 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 l + pPage->child
8ba0: 50 74 72 53 69 7a 65 3b 20 2f 2a 20 46 6f 72 20 PtrSize; /* For
8bb0: 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 62 79 74 looping over byt
8bc0: 65 73 20 6f 66 20 70 43 65 6c 6c 20 2a 2f 0a 20 es of pCell */.
8bd0: 20 75 38 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 u8 *pEnd;
8be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8bf0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 /* End
8c00: 20 6d 61 72 6b 20 66 6f 72 20 61 20 76 61 72 69 mark for a vari
8c10: 6e 74 20 2a 2f 0a 20 20 75 33 32 20 6e 53 69 7a nt */. u32 nSiz
8c20: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
8c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8c40: 20 2f 2a 20 53 69 7a 65 20 76 61 6c 75 65 20 74 /* Size value t
8c50: 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 23 69 66 o return */..#if
8c60: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
8c70: 0a 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 . /* The value
8c80: 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
8c90: 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
8ca0: 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 73 always be the s
8cb0: 61 6d 65 20 61 73 0a 20 20 2a 2a 20 74 68 65 20 ame as. ** the
8cc0: 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 69 7a 65 29 (CellInfo.nSize)
8cd0: 20 76 61 6c 75 65 20 66 6f 75 6e 64 20 62 79 20 value found by
8ce0: 64 6f 69 6e 67 20 61 20 66 75 6c 6c 20 70 61 72 doing a full par
8cf0: 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 se of the. ** c
8d00: 65 6c 6c 2e 20 49 66 20 53 51 4c 49 54 45 5f 44 ell. If SQLITE_D
8d10: 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2c EBUG is defined,
8d20: 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 74 20 an assert() at
8d30: 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 0a 20 20 the bottom of.
8d40: 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
8d50: 20 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74 verifies that t
8d60: 68 69 73 20 69 6e 76 61 72 69 61 6e 74 20 69 73 his invariant is
8d70: 20 6e 6f 74 20 76 69 6f 6c 61 74 65 64 2e 20 2a not violated. *
8d80: 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 64 65 62 /. CellInfo deb
8d90: 75 67 69 6e 66 6f 3b 0a 20 20 62 74 72 65 65 50 uginfo;. btreeP
8da0: 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
8db0: 65 2c 20 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 e, pCell, &debug
8dc0: 69 6e 66 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 info);.#endif..
8dd0: 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 6f 50 61 if( pPage->noPa
8de0: 79 6c 6f 61 64 20 29 7b 0a 20 20 20 20 70 45 6e yload ){. pEn
8df0: 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 d = &pIter[9];.
8e00: 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65 while( (*pIte
8e10: 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 49 74 r++)&0x80 && pIt
8e20: 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 20 20 61 er<pEnd );. a
8e30: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 63 68 ssert( pPage->ch
8e40: 69 6c 64 50 74 72 53 69 7a 65 3d 3d 34 20 29 3b ildPtrSize==4 );
8e50: 0a 20 20 20 20 72 65 74 75 72 6e 20 28 75 31 36 . return (u16
8e60: 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 )(pIter - pCell)
8e70: 3b 0a 20 20 7d 0a 20 20 6e 53 69 7a 65 20 3d 20 ;. }. nSize =
8e80: 2a 70 49 74 65 72 3b 0a 20 20 69 66 28 20 6e 53 *pIter;. if( nS
8e90: 69 7a 65 3e 3d 30 78 38 30 20 29 7b 0a 20 20 20 ize>=0x80 ){.
8ea0: 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 39 pEnd = &pIter[9
8eb0: 5d 3b 0a 20 20 20 20 6e 53 69 7a 65 20 26 3d 20 ];. nSize &=
8ec0: 30 78 37 66 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 0x7f;. do{.
8ed0: 20 20 20 20 6e 53 69 7a 65 20 3d 20 28 6e 53 69 nSize = (nSi
8ee0: 7a 65 3c 3c 37 29 20 7c 20 28 2a 2b 2b 70 49 74 ze<<7) | (*++pIt
8ef0: 65 72 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 er & 0x7f);.
8f00: 7d 77 68 69 6c 65 28 20 2a 28 70 49 74 65 72 29 }while( *(pIter)
8f10: 3e 3d 30 78 38 30 20 26 26 20 70 49 74 65 72 3c >=0x80 && pIter<
8f20: 70 45 6e 64 20 29 3b 0a 20 20 7d 0a 20 20 70 49 pEnd );. }. pI
8f30: 74 65 72 2b 2b 3b 0a 20 20 69 66 28 20 70 50 61 ter++;. if( pPa
8f40: 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
8f50: 20 20 2f 2a 20 70 49 74 65 72 20 6e 6f 77 20 70 /* pIter now p
8f60: 6f 69 6e 74 73 20 61 74 20 74 68 65 20 36 34 2d oints at the 64-
8f70: 62 69 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 bit integer key
8f80: 76 61 6c 75 65 2c 20 61 20 76 61 72 69 61 62 6c value, a variabl
8f90: 65 20 6c 65 6e 67 74 68 20 0a 20 20 20 20 2a 2a e length . **
8fa0: 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 66 6f integer. The fo
8fb0: 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 6f llowing block mo
8fc0: 76 65 73 20 70 49 74 65 72 20 74 6f 20 70 6f 69 ves pIter to poi
8fd0: 6e 74 20 61 74 20 74 68 65 20 66 69 72 73 74 20 nt at the first
8fe0: 62 79 74 65 0a 20 20 20 20 2a 2a 20 70 61 73 74 byte. ** past
8ff0: 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
9000: 6b 65 79 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 key value. */.
9010: 20 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72 5b pEnd = &pIter[
9020: 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 9];. while( (
9030: 2a 70 49 74 65 72 2b 2b 29 26 30 78 38 30 20 26 *pIter++)&0x80 &
9040: 26 20 70 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a & pIter<pEnd );.
9050: 20 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 }. testcase(
9060: 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 nSize==pPage->ma
9070: 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 xLocal );. test
9080: 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 case( nSize==pPa
9090: 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 ge->maxLocal+1 )
90a0: 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 3c 3d 70 ;. if( nSize<=p
90b0: 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 Page->maxLocal )
90c0: 7b 0a 20 20 20 20 6e 53 69 7a 65 20 2b 3d 20 28 {. nSize += (
90d0: 75 33 32 29 28 70 49 74 65 72 20 2d 20 70 43 65 u32)(pIter - pCe
90e0: 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 ll);. if( nSi
90f0: 7a 65 3c 34 20 29 20 6e 53 69 7a 65 20 3d 20 34 ze<4 ) nSize = 4
9100: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
9110: 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 nt minLocal = pP
9120: 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 age->minLocal;.
9130: 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f nSize = minLo
9140: 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d cal + (nSize - m
9150: 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 50 61 67 inLocal) % (pPag
9160: 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
9170: 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 ze - 4);. tes
9180: 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
9190: 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b age->maxLocal );
91a0: 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e . testcase( n
91b0: 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 Size==pPage->max
91c0: 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 Local+1 );. i
91d0: 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e f( nSize>pPage->
91e0: 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 maxLocal ){.
91f0: 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 nSize = minLoc
9200: 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 al;. }. nS
9210: 69 7a 65 20 2b 3d 20 34 20 2b 20 28 75 31 36 29 ize += 4 + (u16)
9220: 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b (pIter - pCell);
9230: 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
9240: 53 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e Size==debuginfo.
9250: 6e 53 69 7a 65 20 7c 7c 20 43 4f 52 52 55 50 54 nSize || CORRUPT
9260: 5f 44 42 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 _DB );. return
9270: 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 (u16)nSize;.}..#
9280: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
9290: 55 47 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61 UG./* This varia
92a0: 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 tion on cellSize
92b0: 50 74 72 28 29 20 69 73 20 75 73 65 64 20 69 6e Ptr() is used in
92c0: 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 side of assert()
92d0: 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f statements.** o
92e0: 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75 nly. */.static u
92f0: 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 16 cellSize(MemP
9300: 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
9310: 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e iCell){. return
9320: 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
9330: 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 ge, findCell(pPa
9340: 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 ge, iCell));.}.#
9350: 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
9360: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
9370: 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ACUUM./*.** If t
9380: 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 he cell pCell, p
9390: 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 67 art of page pPag
93a0: 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 e contains a poi
93b0: 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 nter.** to an ov
93c0: 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 erflow page, ins
93d0: 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 ert an entry int
93e0: 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
93f0: 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 p.** for the ove
9400: 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 rflow page..*/.s
9410: 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 tatic void ptrma
9420: 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 pPutOvflPtr(MemP
9430: 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
9440: 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 pCell, int *pRC)
9450: 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 {. CellInfo inf
9460: 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 o;. if( *pRC )
9470: 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
9480: 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 ( pCell!=0 );.
9490: 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
94a0: 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
94b0: 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e &info);. if( in
94c0: 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a fo.iOverflow ){.
94d0: 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 Pgno ovfl =
94e0: 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b get4byte(&pCell[
94f0: 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 info.iOverflow])
9500: 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ;. ptrmapPut(
9510: 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c pPage->pBt, ovfl
9520: 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
9530: 57 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c W1, pPage->pgno,
9540: 20 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e pRC);. }.}.#en
9550: 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 dif.../*.** Defr
9560: 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 20 agment the page
9570: 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c given. All Cell
9580: 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 s are moved to t
9590: 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 he.** end of the
95a0: 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 page and all fr
95b0: 65 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c 6c ee space is coll
95c0: 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a ected into one.*
95d0: 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74 68 * big FreeBlk th
95e0: 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 at occurs in bet
95f0: 77 65 65 6e 20 74 68 65 20 68 65 61 64 65 72 20 ween the header
9600: 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e and cell.** poin
9610: 74 65 72 20 61 72 72 61 79 20 61 6e 64 20 74 68 ter array and th
9620: 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
9630: 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 45 56 49 44 45 rea..**.** EVIDE
9640: 4e 43 45 2d 4f 46 3a 20 52 2d 34 34 35 38 32 2d NCE-OF: R-44582-
9650: 36 30 31 33 38 20 53 51 4c 69 74 65 20 6d 61 79 60138 SQLite may
9660: 20 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 from time to ti
9670: 6d 65 20 72 65 6f 72 67 61 6e 69 7a 65 20 61 0a me reorganize a.
9680: 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 65 20 73 ** b-tree page s
9690: 6f 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 o that there are
96a0: 20 6e 6f 20 66 72 65 65 62 6c 6f 63 6b 73 20 6f no freeblocks o
96b0: 72 20 66 72 61 67 6d 65 6e 74 20 62 79 74 65 73 r fragment bytes
96c0: 2c 20 61 6c 6c 0a 2a 2a 20 75 6e 75 73 65 64 20 , all.** unused
96d0: 62 79 74 65 73 20 61 72 65 20 63 6f 6e 74 61 69 bytes are contai
96e0: 6e 65 64 20 69 6e 20 74 68 65 20 75 6e 61 6c 6c ned in the unall
96f0: 6f 63 61 74 65 64 20 73 70 61 63 65 20 72 65 67 ocated space reg
9700: 69 6f 6e 2c 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20 ion, and all.**
9710: 63 65 6c 6c 73 20 61 72 65 20 70 61 63 6b 65 64 cells are packed
9720: 20 74 69 67 68 74 6c 79 20 61 74 20 74 68 65 20 tightly at the
9730: 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e end of the page.
9740: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
9750: 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 efragmentPage(Me
9760: 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 mPage *pPage){.
9770: 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
9780: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
9790: 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
97a0: 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 int pc;
97b0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
97c0: 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 69 2d ddress of the i-
97d0: 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 th cell */. int
97e0: 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 hdr;
97f0: 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
9800: 74 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 t to the page he
9810: 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 69 ader */. int si
9820: 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
9830: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
9840: 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 a cell */. int
9850: 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 usableSize;
9860: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
9870: 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 of usable bytes
9880: 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 on a page */.
9890: 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 int cellOffset;
98a0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
98b0: 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c fset to the cell
98c0: 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a pointer array *
98d0: 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 /. int cbrk;
98e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
98f0: 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * Offset to the
9900: 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
9910: 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c a */. int nCell
9920: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
9930: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
9940: 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65 ells on the page
9950: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
9960: 68 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20 20 har *data;
9970: 20 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61 74 /* The page dat
9980: 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 a */. unsigned
9990: 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20 20 char *temp;
99a0: 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20 66 /* Temp area f
99b0: 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 or cell content
99c0: 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
99d0: 61 72 20 2a 73 72 63 3b 20 20 20 20 20 20 20 20 ar *src;
99e0: 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 63 6f 6e /* Source of con
99f0: 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 tent */. int iC
9a00: 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 20 20 ellFirst;
9a10: 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c /* First al
9a20: 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e 64 lowable cell ind
9a30: 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c ex */. int iCel
9a40: 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 lLast;
9a50: 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69 /* Last possi
9a60: 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a ble cell index *
9a70: 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 /... assert( sq
9a80: 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
9a90: 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
9aa0: 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
9ab0: 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 t( pPage->pBt!=0
9ac0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
9ad0: 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
9ae0: 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d Size <= SQLITE_M
9af0: 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a AX_PAGE_SIZE );.
9b00: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
9b10: 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b >nOverflow==0 );
9b20: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
9b30: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
9b40: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
9b50: 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 30 3b 0a );. temp = 0;.
9b60: 20 20 73 72 63 20 3d 20 64 61 74 61 20 3d 20 70 src = data = p
9b70: 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 Page->aData;. h
9b80: 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
9b90: 66 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 ffset;. cellOff
9ba0: 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c set = pPage->cel
9bb0: 6c 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c lOffset;. nCell
9bc0: 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
9bd0: 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c . assert( nCell
9be0: 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 ==get2byte(&data
9bf0: 5b 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 [hdr+3]) );. us
9c00: 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 ableSize = pPage
9c10: 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
9c20: 65 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 e;. cbrk = usab
9c30: 6c 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 leSize;. iCellF
9c40: 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 irst = cellOffse
9c50: 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 t + 2*nCell;. i
9c60: 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c CellLast = usabl
9c70: 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f 72 eSize - 4;. for
9c80: 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 (i=0; i<nCell; i
9c90: 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 ++){. u8 *pAd
9ca0: 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 dr; /* The i
9cb0: 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 -th cell pointer
9cc0: 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20 */. pAddr =
9cd0: 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 &data[cellOffset
9ce0: 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 + i*2];. pc
9cf0: 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 72 = get2byte(pAddr
9d00: 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
9d10: 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 pc==iCellFirst
9d20: 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
9d30: 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 pc==iCellLast )
9d40: 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ;.#if !defined(S
9d50: 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 QLITE_ENABLE_OVE
9d60: 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b RSIZE_CELL_CHECK
9d70: 29 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 63 ). /* These c
9d80: 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 61 onditions have a
9d90: 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 69 lready been veri
9da0: 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e 69 fied in btreeIni
9db0: 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 69 tPage(). ** i
9dc0: 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
9dd0: 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 OVERSIZE_CELL_CH
9de0: 45 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20 0a ECK is defined .
9df0: 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
9e00: 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 c<iCellFirst ||
9e10: 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a pc>iCellLast ){.
9e20: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
9e30: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
9e40: 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
9e50: 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 69 assert( pc>=i
9e60: 43 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63 3c CellFirst && pc<
9e70: 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 =iCellLast );.
9e80: 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a size = cellSiz
9e90: 65 50 74 72 28 70 50 61 67 65 2c 20 26 73 72 63 ePtr(pPage, &src
9ea0: 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20 [pc]);. cbrk
9eb0: 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64 65 66 -= size;.#if def
9ec0: 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
9ed0: 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c LE_OVERSIZE_CELL
9ee0: 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66 28 20 _CHECK). if(
9ef0: 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 cbrk<iCellFirst
9f00: 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
9f10: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
9f20: 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 KPT;. }.#else
9f30: 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43 . if( cbrk<iC
9f40: 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 2b 73 ellFirst || pc+s
9f50: 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 ize>usableSize )
9f60: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
9f70: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
9f80: 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 PT;. }.#endif
9f90: 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 62 72 . assert( cbr
9fa0: 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 k+size<=usableSi
9fb0: 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c ze && cbrk>=iCel
9fc0: 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65 lFirst );. te
9fd0: 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 69 7a stcase( cbrk+siz
9fe0: 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b e==usableSize );
9ff0: 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 . testcase( p
a000: 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 c+size==usableSi
a010: 7a 65 20 29 3b 0a 20 20 20 20 70 75 74 32 62 79 ze );. put2by
a020: 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 3b te(pAddr, cbrk);
a030: 0a 20 20 20 20 69 66 28 20 74 65 6d 70 3d 3d 30 . if( temp==0
a040: 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 78 3b ){. int x;
a050: 0a 20 20 20 20 20 20 69 66 28 20 63 62 72 6b 3d . if( cbrk=
a060: 3d 70 63 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a =pc ) continue;.
a070: 20 20 20 20 20 20 74 65 6d 70 20 3d 20 73 71 6c temp = sql
a080: 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 ite3PagerTempSpa
a090: 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 ce(pPage->pBt->p
a0a0: 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 78 20 Pager);. x
a0b0: 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
a0c0: 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 20 20 [hdr+5]);.
a0d0: 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 78 5d 2c memcpy(&temp[x],
a0e0: 20 26 64 61 74 61 5b 78 5d 2c 20 28 63 62 72 6b &data[x], (cbrk
a0f0: 2b 73 69 7a 65 29 20 2d 20 78 29 3b 0a 20 20 20 +size) - x);.
a100: 20 20 20 73 72 63 20 3d 20 74 65 6d 70 3b 0a 20 src = temp;.
a110: 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 }. memcpy(
a120: 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 73 72 &data[cbrk], &sr
a130: 63 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 c[pc], size);.
a140: 7d 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 6b }. assert( cbrk
a150: 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a >=iCellFirst );.
a160: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
a170: 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a [hdr+5], cbrk);.
a180: 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 data[hdr+1] =
a190: 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 0;. data[hdr+2]
a1a0: 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 = 0;. data[hdr
a1b0: 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 +7] = 0;. memse
a1c0: 74 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 t(&data[iCellFir
a1d0: 73 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 st], 0, cbrk-iCe
a1e0: 6c 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 65 llFirst);. asse
a1f0: 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
a200: 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
a210: 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
a220: 20 69 66 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 if( cbrk-iCellF
a230: 69 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 irst!=pPage->nFr
a240: 65 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ee ){. return
a250: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
a260: 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 BKPT;. }. retu
a270: 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
a280: 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 74 68 ./*.** Search th
a290: 65 20 66 72 65 65 2d 6c 69 73 74 20 6f 6e 20 70 e free-list on p
a2a0: 61 67 65 20 70 50 67 20 66 6f 72 20 73 70 61 63 age pPg for spac
a2b0: 65 20 74 6f 20 73 74 6f 72 65 20 61 20 63 65 6c e to store a cel
a2c0: 6c 20 6e 42 79 74 65 20 62 79 74 65 73 20 69 6e l nByte bytes in
a2d0: 0a 2a 2a 20 73 69 7a 65 2e 20 49 66 20 6f 6e 65 .** size. If one
a2e0: 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 72 can be found, r
a2f0: 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
a300: 74 6f 20 74 68 65 20 73 70 61 63 65 20 61 6e 64 to the space and
a310: 20 72 65 6d 6f 76 65 20 69 74 0a 2a 2a 20 66 72 remove it.** fr
a320: 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 om the free-list
a330: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 73 75 ..**.** If no su
a340: 69 74 61 62 6c 65 20 73 70 61 63 65 20 63 61 6e itable space can
a350: 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 be found on the
a360: 20 66 72 65 65 2d 6c 69 73 74 2c 20 72 65 74 75 free-list, retu
a370: 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 rn NULL..**.** T
a380: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
a390: 20 64 65 74 65 63 74 20 63 6f 72 72 75 70 74 69 detect corrupti
a3a0: 6f 6e 20 77 69 74 68 69 6e 20 70 50 67 2e 20 20 on within pPg.
a3b0: 49 66 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 If corruption is
a3c0: 0a 2a 2a 20 64 65 74 65 63 74 65 64 20 74 68 65 .** detected the
a3d0: 6e 20 2a 70 52 63 20 69 73 20 73 65 74 20 74 6f n *pRc is set to
a3e0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
a3f0: 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 and NULL is retu
a400: 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rned..**.** If a
a410: 20 73 6c 6f 74 20 6f 66 20 61 74 20 6c 65 61 73 slot of at leas
a420: 74 20 6e 42 79 74 65 20 62 79 74 65 73 20 69 73 t nByte bytes is
a430: 20 66 6f 75 6e 64 20 62 75 74 20 63 61 6e 6e 6f found but canno
a440: 74 20 62 65 20 75 73 65 64 20 62 65 63 61 75 73 t be used becaus
a450: 65 20 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 e .** there are
a460: 61 6c 72 65 61 64 79 20 61 74 20 6c 65 61 73 74 already at least
a470: 20 36 30 20 66 72 61 67 6d 65 6e 74 65 64 20 62 60 fragmented b
a480: 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 ytes on the page
a490: 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a , return NULL..*
a4a0: 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 * In this case,
a4b0: 69 66 20 70 62 44 65 66 72 61 67 20 70 61 72 61 if pbDefrag para
a4c0: 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c meter is not NUL
a4d0: 4c 2c 20 73 65 74 20 2a 70 62 44 65 66 72 61 67 L, set *pbDefrag
a4e0: 20 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 to true..*/.sta
a4f0: 74 69 63 20 75 38 20 2a 70 61 67 65 46 69 6e 64 tic u8 *pageFind
a500: 53 6c 6f 74 28 4d 65 6d 50 61 67 65 20 2a 70 50 Slot(MemPage *pP
a510: 67 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e g, int nByte, in
a520: 74 20 2a 70 52 63 2c 20 69 6e 74 20 2a 70 62 44 t *pRc, int *pbD
a530: 65 66 72 61 67 29 7b 0a 20 20 63 6f 6e 73 74 20 efrag){. const
a540: 69 6e 74 20 68 64 72 20 3d 20 70 50 67 2d 3e 68 int hdr = pPg->h
a550: 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 38 20 2a drOffset;. u8 *
a560: 20 63 6f 6e 73 74 20 61 44 61 74 61 20 3d 20 70 const aData = p
a570: 50 67 2d 3e 61 44 61 74 61 3b 0a 20 20 69 6e 74 Pg->aData;. int
a580: 20 69 41 64 64 72 3b 0a 20 20 69 6e 74 20 70 63 iAddr;. int pc
a590: 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 ;. int usableSi
a5a0: 7a 65 20 3d 20 70 50 67 2d 3e 70 42 74 2d 3e 75 ze = pPg->pBt->u
a5b0: 73 61 62 6c 65 53 69 7a 65 3b 0a 0a 20 20 66 6f sableSize;.. fo
a5c0: 72 28 69 41 64 64 72 3d 68 64 72 2b 31 3b 20 28 r(iAddr=hdr+1; (
a5d0: 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 61 pc = get2byte(&a
a5e0: 44 61 74 61 5b 69 41 64 64 72 5d 29 29 3e 30 3b Data[iAddr]))>0;
a5f0: 20 69 41 64 64 72 3d 70 63 29 7b 0a 20 20 20 20 iAddr=pc){.
a600: 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 int size;
a610: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
a620: 74 68 65 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f the free slot */
a630: 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 . /* EVIDENCE
a640: 2d 4f 46 3a 20 52 2d 30 36 38 36 36 2d 33 39 31 -OF: R-06866-391
a650: 32 35 20 46 72 65 65 62 6c 6f 63 6b 73 20 61 72 25 Freeblocks ar
a660: 65 20 61 6c 77 61 79 73 20 63 6f 6e 6e 65 63 74 e always connect
a670: 65 64 20 69 6e 20 6f 72 64 65 72 20 6f 66 0a 20 ed in order of.
a680: 20 20 20 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 ** increasing
a690: 20 6f 66 66 73 65 74 2e 20 2a 2f 0a 20 20 20 20 offset. */.
a6a0: 69 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69 7a if( pc>usableSiz
a6b0: 65 2d 34 20 7c 7c 20 70 63 3c 69 41 64 64 72 2b e-4 || pc<iAddr+
a6c0: 34 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 63 20 4 ){. *pRc
a6d0: 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
a6e0: 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 72 65 74 _BKPT;. ret
a6f0: 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 0;. }.
a700: 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a /* EVIDENCE-OF:
a710: 20 52 2d 32 32 37 31 30 2d 35 33 33 32 38 20 54 R-22710-53328 T
a720: 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 he third and fou
a730: 72 74 68 20 62 79 74 65 73 20 6f 66 20 65 61 63 rth bytes of eac
a740: 68 0a 20 20 20 20 2a 2a 20 66 72 65 65 62 6c 6f h. ** freeblo
a750: 63 6b 20 66 6f 72 6d 20 61 20 62 69 67 2d 65 6e ck form a big-en
a760: 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
a770: 63 68 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ch is the size o
a780: 66 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 0a f the freeblock.
a790: 20 20 20 20 2a 2a 20 69 6e 20 62 79 74 65 73 2c ** in bytes,
a7a0: 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 34 including the 4
a7b0: 2d 62 79 74 65 20 68 65 61 64 65 72 2e 20 2a 2f -byte header. */
a7c0: 0a 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 . size = get2
a7d0: 62 79 74 65 28 26 61 44 61 74 61 5b 70 63 2b 32 byte(&aData[pc+2
a7e0: 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 ]);. if( size
a7f0: 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 >=nByte ){.
a800: 20 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 int x = size -
a810: 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 74 65 73 nByte;. tes
a820: 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a 20 tcase( x==4 );.
a830: 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 78 testcase( x
a840: 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ==3 );. if(
a850: 20 78 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 x<4 ){.
a860: 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 /* EVIDENCE-OF:
a870: 52 2d 31 31 34 39 38 2d 35 38 30 32 32 20 49 6e R-11498-58022 In
a880: 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 62 a well-formed b
a890: 2d 74 72 65 65 20 70 61 67 65 2c 20 74 68 65 20 -tree page, the
a8a0: 74 6f 74 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a total. **
a8b0: 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
a8c0: 20 69 6e 20 66 72 61 67 6d 65 6e 74 73 20 6d 61 in fragments ma
a8d0: 79 20 6e 6f 74 20 65 78 63 65 65 64 20 36 30 2e y not exceed 60.
a8e0: 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a8f0: 61 44 61 74 61 5b 68 64 72 2b 37 5d 3e 3d 36 30 aData[hdr+7]>=60
a900: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
a910: 28 20 70 62 44 65 66 72 61 67 20 29 20 2a 70 62 ( pbDefrag ) *pb
a920: 44 65 66 72 61 67 20 3d 20 31 3b 0a 20 20 20 20 Defrag = 1;.
a930: 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
a940: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a950: 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 /* Remove the
a960: 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 slot from the fr
a970: 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 ee-list. Update
a980: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 the number of.
a990: 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e ** fragmen
a9a0: 74 65 64 20 62 79 74 65 73 20 77 69 74 68 69 6e ted bytes within
a9b0: 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 the page. */.
a9c0: 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44 memcpy(&aD
a9d0: 61 74 61 5b 69 41 64 64 72 5d 2c 20 26 61 44 61 ata[iAddr], &aDa
a9e0: 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 ta[pc], 2);.
a9f0: 20 20 20 20 61 44 61 74 61 5b 68 64 72 2b 37 5d aData[hdr+7]
aa00: 20 2b 3d 20 28 75 38 29 78 3b 0a 20 20 20 20 20 += (u8)x;.
aa10: 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 2b }else if( size+
aa20: 70 63 20 3e 20 75 73 61 62 6c 65 53 69 7a 65 20 pc > usableSize
aa30: 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 63 20 ){. *pRc
aa40: 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
aa50: 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 72 _BKPT;. r
aa60: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d eturn 0;. }
aa70: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
aa80: 20 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e The slot remain
aa90: 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 s on the free-li
aaa0: 73 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 73 st. Reduce its s
aab0: 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 ize to account.
aac0: 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 ** for t
aad0: 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 he portion used
aae0: 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 by the new alloc
aaf0: 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 ation. */.
ab00: 20 20 70 75 74 32 62 79 74 65 28 26 61 44 61 74 put2byte(&aDat
ab10: 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 a[pc+2], x);.
ab20: 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
ab30: 6e 20 26 61 44 61 74 61 5b 70 63 20 2b 20 78 5d n &aData[pc + x]
ab40: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
ab50: 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
ab60: 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 * Allocate nByte
ab70: 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
ab80: 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 from within the
ab90: 42 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 B-Tree page pass
aba0: 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 ed.** as the fir
abb0: 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 st argument. Wri
abc0: 74 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 te into *pIdx th
abd0: 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 e index into pPa
abe0: 67 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f ge->aData[].** o
abf0: 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 f the first byte
ac00: 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 of allocated sp
ac10: 61 63 65 2e 20 52 65 74 75 72 6e 20 65 69 74 68 ace. Return eith
ac20: 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a er SQLITE_OK or.
ac30: 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 ** an error code
ac40: 20 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 (usually SQLITE
ac50: 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a _CORRUPT)..**.**
ac60: 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 The caller guar
ac70: 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 antees that ther
ac80: 65 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 e is sufficient
ac90: 73 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 space to make th
aca0: 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e e.** allocation.
acb0: 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d This routine m
acc0: 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 65 66 ight need to def
acd0: 72 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 ragment in order
ace0: 20 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c to bring.** all
acf0: 20 74 68 65 20 73 70 61 63 65 20 74 6f 67 65 74 the space toget
ad00: 68 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 her, however. T
ad10: 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
ad20: 20 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 avoid using.**
ad30: 74 68 65 20 66 69 72 73 74 20 74 77 6f 20 62 79 the first two by
ad40: 74 65 73 20 70 61 73 74 20 74 68 65 20 63 65 6c tes past the cel
ad50: 6c 20 70 6f 69 6e 74 65 72 20 61 72 65 61 20 73 l pointer area s
ad60: 69 6e 63 65 20 70 72 65 73 75 6d 61 62 6c 79 20 ince presumably
ad70: 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 this.** allocati
ad80: 6f 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 64 65 on is being made
ad90: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 in order to ins
ada0: 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 ert a new cell,
adb0: 73 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c so we will.** al
adc0: 73 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 69 6e so end up needin
add0: 67 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 g a new cell poi
ade0: 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nter..*/.static
adf0: 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 int allocateSpac
ae00: 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
ae10: 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 , int nByte, int
ae20: 20 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 *pIdx){. const
ae30: 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 int hdr = pPage
ae40: 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 ->hdrOffset;
ae50: 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f /* Local cache o
ae60: 66 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 f pPage->hdrOffs
ae70: 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e et */. u8 * con
ae80: 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d st data = pPage-
ae90: 3e 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 >aData; /*
aea0: 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 Local cache of p
aeb0: 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 Page->aData */.
aec0: 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 int top;
aed0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aee0: 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
aef0: 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 yte of cell cont
af00: 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
af10: 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
af20: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
af30: 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 65 /* Integer re
af40: 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
af50: 6e 74 20 67 61 70 3b 20 20 20 20 20 20 20 20 2f nt gap; /
af60: 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
af70: 67 61 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c gap between cell
af80: 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 63 65 pointers and ce
af90: 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 ll content */.
afa0: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
afb0: 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
afc0: 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
afd0: 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
afe0: 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 pPage->pBt );.
aff0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
b000: 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
b010: 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
b020: 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
b030: 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d >=0 ); /* Minim
b040: 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
b050: 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4 */. assert( p
b060: 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 Page->nFree>=nBy
b070: 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 te );. assert(
b080: 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
b090: 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
b0a0: 20 6e 42 79 74 65 20 3c 20 28 69 6e 74 29 28 70 nByte < (int)(p
b0b0: 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
b0c0: 65 53 69 7a 65 2d 38 29 20 29 3b 0a 0a 20 20 61 eSize-8) );.. a
b0d0: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 ssert( pPage->ce
b0e0: 6c 6c 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 llOffset == hdr
b0f0: 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e + 12 - 4*pPage->
b100: 6c 65 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 leaf );. gap =
b110: 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
b120: 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 t + 2*pPage->nCe
b130: 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 67 61 ll;. assert( ga
b140: 70 3c 3d 36 35 35 33 36 20 29 3b 0a 20 20 2f 2a p<=65536 );. /*
b150: 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d EVIDENCE-OF: R-
b160: 32 39 33 35 36 2d 30 32 33 39 31 20 49 66 20 74 29356-02391 If t
b170: 68 65 20 64 61 74 61 62 61 73 65 20 75 73 65 73 he database uses
b180: 20 61 20 36 35 35 33 36 2d 62 79 74 65 20 70 61 a 65536-byte pa
b190: 67 65 20 73 69 7a 65 0a 20 20 2a 2a 20 61 6e 64 ge size. ** and
b1a0: 20 74 68 65 20 72 65 73 65 72 76 65 64 20 73 70 the reserved sp
b1b0: 61 63 65 20 69 73 20 7a 65 72 6f 20 28 74 68 65 ace is zero (the
b1c0: 20 75 73 75 61 6c 20 76 61 6c 75 65 20 66 6f 72 usual value for
b1d0: 20 72 65 73 65 72 76 65 64 20 73 70 61 63 65 29 reserved space)
b1e0: 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 . ** then the c
b1f0: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 66 73 ell content offs
b200: 65 74 20 6f 66 20 61 6e 20 65 6d 70 74 79 20 70 et of an empty p
b210: 61 67 65 20 77 61 6e 74 73 20 74 6f 20 62 65 20 age wants to be
b220: 36 35 35 33 36 2e 0a 20 20 2a 2a 20 48 6f 77 65 65536.. ** Howe
b230: 76 65 72 2c 20 74 68 61 74 20 69 6e 74 65 67 65 ver, that intege
b240: 72 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 r is too large t
b250: 6f 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 o be stored in a
b260: 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 2-byte unsigned
b270: 0a 20 20 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 . ** integer, s
b280: 6f 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20 69 o a value of 0 i
b290: 73 20 75 73 65 64 20 69 6e 20 69 74 73 20 70 6c s used in its pl
b2a0: 61 63 65 2e 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 ace. */. top =
b2b0: 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 get2byteNotZero(
b2c0: 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 &data[hdr+5]);.
b2d0: 20 69 66 28 20 67 61 70 3e 74 6f 70 20 29 20 72 if( gap>top ) r
b2e0: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
b2f0: 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a RUPT_BKPT;.. /*
b300: 20 49 66 20 74 68 65 72 65 20 69 73 20 65 6e 6f If there is eno
b310: 75 67 68 20 73 70 61 63 65 20 62 65 74 77 65 65 ugh space betwee
b320: 6e 20 67 61 70 20 61 6e 64 20 74 6f 70 20 66 6f n gap and top fo
b330: 72 20 6f 6e 65 20 6d 6f 72 65 20 63 65 6c 6c 20 r one more cell
b340: 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 61 72 72 pointer. ** arr
b350: 61 79 20 65 6e 74 72 79 20 6f 66 66 73 65 74 2c ay entry offset,
b360: 20 61 6e 64 20 69 66 20 74 68 65 20 66 72 65 65 and if the free
b370: 6c 69 73 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 list is not empt
b380: 79 2c 20 74 68 65 6e 20 73 65 61 72 63 68 20 74 y, then search t
b390: 68 65 0a 20 20 2a 2a 20 66 72 65 65 6c 69 73 74 he. ** freelist
b3a0: 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 looking for a f
b3b0: 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f ree slot big eno
b3c0: 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 74 ugh to satisfy t
b3d0: 68 65 20 72 65 71 75 65 73 74 2e 0a 20 20 2a 2f he request.. */
b3e0: 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 . testcase( gap
b3f0: 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 +2==top );. tes
b400: 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d 74 6f tcase( gap+1==to
b410: 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 p );. testcase(
b420: 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 gap==top );. i
b430: 66 28 20 67 61 70 2b 32 3c 3d 74 6f 70 20 26 26 f( gap+2<=top &&
b440: 20 28 64 61 74 61 5b 68 64 72 2b 31 5d 20 7c 7c (data[hdr+1] ||
b450: 20 64 61 74 61 5b 68 64 72 2b 32 5d 29 20 29 7b data[hdr+2]) ){
b460: 0a 20 20 20 20 69 6e 74 20 62 44 65 66 72 61 67 . int bDefrag
b470: 20 3d 20 30 3b 0a 20 20 20 20 75 38 20 2a 70 53 = 0;. u8 *pS
b480: 70 61 63 65 20 3d 20 70 61 67 65 46 69 6e 64 53 pace = pageFindS
b490: 6c 6f 74 28 70 50 61 67 65 2c 20 6e 42 79 74 65 lot(pPage, nByte
b4a0: 2c 20 26 72 63 2c 20 26 62 44 65 66 72 61 67 29 , &rc, &bDefrag)
b4b0: 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
b4c0: 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 eturn rc;. if
b4d0: 28 20 62 44 65 66 72 61 67 20 29 20 67 6f 74 6f ( bDefrag ) goto
b4e0: 20 64 65 66 72 61 67 6d 65 6e 74 5f 70 61 67 65 defragment_page
b4f0: 3b 0a 20 20 20 20 69 66 28 20 70 53 70 61 63 65 ;. if( pSpace
b500: 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
b510: 28 20 70 53 70 61 63 65 3e 3d 64 61 74 61 20 26 ( pSpace>=data &
b520: 26 20 28 70 53 70 61 63 65 20 2d 20 64 61 74 61 & (pSpace - data
b530: 29 3c 36 35 35 33 36 20 29 3b 0a 20 20 20 20 20 )<65536 );.
b540: 20 2a 70 49 64 78 20 3d 20 28 69 6e 74 29 28 70 *pIdx = (int)(p
b550: 53 70 61 63 65 20 2d 20 64 61 74 61 29 3b 0a 20 Space - data);.
b560: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
b570: 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
b580: 0a 0a 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 .. /* The reque
b590: 73 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 st could not be
b5a0: 66 75 6c 66 69 6c 6c 65 64 20 75 73 69 6e 67 20 fulfilled using
b5b0: 61 20 66 72 65 65 6c 69 73 74 20 73 6c 6f 74 2e a freelist slot.
b5c0: 20 20 43 68 65 63 6b 0a 20 20 2a 2a 20 74 6f 20 Check. ** to
b5d0: 73 65 65 20 69 66 20 64 65 66 72 61 67 6d 65 6e see if defragmen
b5e0: 74 61 74 69 6f 6e 20 69 73 20 6e 65 63 65 73 73 tation is necess
b5f0: 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 ary.. */. test
b600: 63 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 case( gap+2+nByt
b610: 65 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 e==top );. if(
b620: 67 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 gap+2+nByte>top
b630: 29 7b 0a 20 64 65 66 72 61 67 6d 65 6e 74 5f 70 ){. defragment_p
b640: 61 67 65 3a 0a 20 20 20 20 61 73 73 65 72 74 28 age:. assert(
b650: 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 pPage->nCell>0
b660: 7c 7c 20 43 4f 52 52 55 50 54 5f 44 42 20 29 3b || CORRUPT_DB );
b670: 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 . rc = defrag
b680: 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b mentPage(pPage);
b690: 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
b6a0: 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 turn rc;. top
b6b0: 20 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 = get2byteNotZe
b6c0: 72 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 ro(&data[hdr+5])
b6d0: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 61 ;. assert( ga
b6e0: 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20 29 3b 0a p+nByte<=top );.
b6f0: 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 }... /* Alloc
b700: 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 ate memory from
b710: 74 68 65 20 67 61 70 20 69 6e 20 62 65 74 77 65 the gap in betwe
b720: 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e en the cell poin
b730: 74 65 72 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 ter array. ** a
b740: 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 nd the cell cont
b750: 65 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 62 ent area. The b
b760: 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 63 treeInitPage() c
b770: 61 6c 6c 20 68 61 73 20 61 6c 72 65 61 64 79 0a all has already.
b780: 20 20 2a 2a 20 76 61 6c 69 64 61 74 65 64 20 74 ** validated t
b790: 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 47 69 he freelist. Gi
b7a0: 76 65 6e 20 74 68 61 74 20 74 68 65 20 66 72 65 ven that the fre
b7b0: 65 6c 69 73 74 20 69 73 20 76 61 6c 69 64 2c 20 elist is valid,
b7c0: 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 6e 6f there. ** is no
b7d0: 20 77 61 79 20 74 68 61 74 20 74 68 65 20 61 6c way that the al
b7e0: 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20 65 78 74 location can ext
b7f0: 65 6e 64 20 6f 66 66 20 74 68 65 20 65 6e 64 20 end off the end
b800: 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 20 20 2a of the page.. *
b810: 2a 20 54 68 65 20 61 73 73 65 72 74 28 29 20 62 * The assert() b
b820: 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 68 elow verifies th
b830: 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 e previous sente
b840: 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 nce.. */. top
b850: 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70 75 74 32 -= nByte;. put2
b860: 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
b870: 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73 73 65 72 ], top);. asser
b880: 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 3c 3d 20 t( top+nByte <=
b890: 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74 2d (int)pPage->pBt-
b8a0: 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 >usableSize );.
b8b0: 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 *pIdx = top;.
b8c0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
b8d0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
b8e0: 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 n a section of t
b8f0: 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 he pPage->aData
b900: 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e to the freelist.
b910: 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 .** The first by
b920: 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 te of the new fr
b930: 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 ee block is pPag
b940: 65 2d 3e 61 44 61 74 61 5b 69 53 74 61 72 74 5d e->aData[iStart]
b950: 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a 65 .** and the size
b960: 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 of the block is
b970: 20 69 53 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a iSize bytes..**
b980: 0a 2a 2a 20 41 64 6a 61 63 65 6e 74 20 66 72 65 .** Adjacent fre
b990: 65 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 61 6c eblocks are coal
b9a0: 65 73 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 esced..**.** Not
b9b0: 65 20 74 68 61 74 20 65 76 65 6e 20 74 68 6f 75 e that even thou
b9c0: 67 68 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b gh the freeblock
b9d0: 20 6c 69 73 74 20 77 61 73 20 63 68 65 63 6b 65 list was checke
b9e0: 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 d by btreeInitPa
b9f0: 67 65 28 29 2c 0a 2a 2a 20 74 68 61 74 20 72 6f ge(),.** that ro
ba00: 75 74 69 6e 65 20 77 69 6c 6c 20 6e 6f 74 20 64 utine will not d
ba10: 65 74 65 63 74 20 6f 76 65 72 6c 61 70 20 62 65 etect overlap be
ba20: 74 77 65 65 6e 20 63 65 6c 6c 73 20 6f 72 20 66 tween cells or f
ba30: 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 72 0a reeblocks. Nor.
ba40: 2a 2a 20 64 6f 65 73 20 69 74 20 64 65 74 65 63 ** does it detec
ba50: 74 20 63 65 6c 6c 73 20 6f 72 20 66 72 65 65 62 t cells or freeb
ba60: 6c 6f 63 6b 73 20 74 68 61 74 20 65 6e 63 72 6f locks that encro
ba70: 75 63 68 20 69 6e 74 6f 20 74 68 65 20 72 65 73 uch into the res
ba80: 65 72 76 65 64 20 62 79 74 65 73 0a 2a 2a 20 61 erved bytes.** a
ba90: 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
baa0: 20 70 61 67 65 2e 20 20 53 6f 20 64 6f 20 61 64 page. So do ad
bab0: 64 69 74 69 6f 6e 61 6c 20 63 6f 72 72 75 70 74 ditional corrupt
bac0: 69 6f 6e 20 63 68 65 63 6b 73 20 69 6e 73 69 64 ion checks insid
bad0: 65 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e e this.** routin
bae0: 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c e and return SQL
baf0: 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61 ITE_CORRUPT if a
bb00: 6e 79 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 20 ny problems are
bb10: 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 found..*/.static
bb20: 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 4d int freeSpace(M
bb30: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
bb40: 31 36 20 69 53 74 61 72 74 2c 20 75 31 36 20 69 16 iStart, u16 i
bb50: 53 69 7a 65 29 7b 0a 20 20 75 31 36 20 69 50 74 Size){. u16 iPt
bb60: 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
bb70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
bb80: 2a 20 41 64 64 72 65 73 73 20 6f 66 20 70 74 72 * Address of ptr
bb90: 20 74 6f 20 6e 65 78 74 20 66 72 65 65 62 6c 6f to next freeblo
bba0: 63 6b 20 2a 2f 0a 20 20 75 31 36 20 69 46 72 65 ck */. u16 iFre
bbb0: 65 42 6c 6b 3b 20 20 20 20 20 20 20 20 20 20 20 eBlk;
bbc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bbd0: 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 Address of the
bbe0: 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b 20 2a next freeblock *
bbf0: 2f 0a 20 20 75 38 20 68 64 72 3b 20 20 20 20 20 /. u8 hdr;
bc00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc10: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
bc20: 65 20 68 65 61 64 65 72 20 73 69 7a 65 2e 20 20 e header size.
bc30: 30 20 6f 72 20 31 30 30 20 2a 2f 0a 20 20 75 38 0 or 100 */. u8
bc40: 20 6e 46 72 61 67 20 3d 20 30 3b 20 20 20 20 20 nFrag = 0;
bc50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc60: 20 20 20 20 2f 2a 20 52 65 64 75 63 74 69 6f 6e /* Reduction
bc70: 20 69 6e 20 66 72 61 67 6d 65 6e 74 61 74 69 6f in fragmentatio
bc80: 6e 20 2a 2f 0a 20 20 75 31 36 20 69 4f 72 69 67 n */. u16 iOrig
bc90: 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 20 20 20 Size = iSize;
bca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bcb0: 4f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f Original value o
bcc0: 66 20 69 53 69 7a 65 20 2a 2f 0a 20 20 75 33 32 f iSize */. u32
bcd0: 20 69 4c 61 73 74 20 3d 20 70 50 61 67 65 2d 3e iLast = pPage->
bce0: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
bcf0: 34 3b 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 6f 4; /* Largest po
bd00: 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 6b ssible freeblock
bd10: 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 33 32 offset */. u32
bd20: 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b iEnd = iStart +
bd30: 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 iSize;
bd40: 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
bd50: 20 70 61 73 74 20 74 68 65 20 69 53 74 61 72 74 past the iStart
bd60: 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 buffer */. uns
bd70: 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
bd80: 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
bd90: 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 65 /* Page conte
bda0: 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 nt */.. assert(
bdb0: 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 pPage->pBt!=0 )
bdc0: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
bdd0: 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
bde0: 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
bdf0: 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
be00: 20 69 53 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e iStart>=pPage->
be10: 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67 hdrOffset+6+pPag
be20: 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 e->childPtrSize
be30: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 4f 52 );. assert( COR
be40: 52 55 50 54 5f 44 42 20 7c 7c 20 69 45 6e 64 20 RUPT_DB || iEnd
be50: 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 <= pPage->pBt->u
be60: 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 61 sableSize );. a
be70: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
be80: 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
be90: 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
bea0: 20 20 61 73 73 65 72 74 28 20 69 53 69 7a 65 3e assert( iSize>
beb0: 3d 34 20 29 3b 20 20 20 2f 2a 20 4d 69 6e 69 6d =4 ); /* Minim
bec0: 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
bed0: 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 4 */. assert( i
bee0: 53 74 61 72 74 3c 3d 69 4c 61 73 74 20 29 3b 0a Start<=iLast );.
bef0: 0a 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 . /* Overwrite
bf00: 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 deleted informat
bf10: 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 20 77 ion with zeros w
bf20: 68 65 6e 20 74 68 65 20 73 65 63 75 72 65 5f 64 hen the secure_d
bf30: 65 6c 65 74 65 0a 20 20 2a 2a 20 6f 70 74 69 6f elete. ** optio
bf40: 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a n is enabled */.
bf50: 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74 if( pPage->pBt
bf60: 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
bf70: 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 29 _SECURE_DELETE )
bf80: 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 64 61 {. memset(&da
bf90: 74 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20 69 ta[iStart], 0, i
bfa0: 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a Size);. }.. /*
bfb0: 20 54 68 65 20 6c 69 73 74 20 6f 66 20 66 72 65 The list of fre
bfc0: 65 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 eblocks must be
bfd0: 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 in ascending ord
bfe0: 65 72 2e 20 20 46 69 6e 64 20 74 68 65 20 0a 20 er. Find the .
bff0: 20 2a 2a 20 73 70 6f 74 20 6f 6e 20 74 68 65 20 ** spot on the
c000: 6c 69 73 74 20 77 68 65 72 65 20 69 53 74 61 72 list where iStar
c010: 74 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 t should be inse
c020: 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 rted.. */. hdr
c030: 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
c040: 73 65 74 3b 0a 20 20 69 50 74 72 20 3d 20 68 64 set;. iPtr = hd
c050: 72 20 2b 20 31 3b 0a 20 20 69 66 28 20 64 61 74 r + 1;. if( dat
c060: 61 5b 69 50 74 72 2b 31 5d 3d 3d 30 20 26 26 20 a[iPtr+1]==0 &&
c070: 64 61 74 61 5b 69 50 74 72 5d 3d 3d 30 20 29 7b data[iPtr]==0 ){
c080: 0a 20 20 20 20 69 46 72 65 65 42 6c 6b 20 3d 20 . iFreeBlk =
c090: 30 3b 20 20 2f 2a 20 53 68 6f 72 74 63 75 74 20 0; /* Shortcut
c0a0: 66 6f 72 20 74 68 65 20 63 61 73 65 20 77 68 65 for the case whe
c0b0: 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 n the freelist i
c0c0: 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 7d 65 6c s empty */. }el
c0d0: 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 se{. while( (
c0e0: 69 46 72 65 65 42 6c 6b 20 3d 20 67 65 74 32 62 iFreeBlk = get2b
c0f0: 79 74 65 28 26 64 61 74 61 5b 69 50 74 72 5d 29 yte(&data[iPtr])
c100: 29 3e 30 20 26 26 20 69 46 72 65 65 42 6c 6b 3c )>0 && iFreeBlk<
c110: 69 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 iStart ){.
c120: 69 66 28 20 69 46 72 65 65 42 6c 6b 3c 69 50 74 if( iFreeBlk<iPt
c130: 72 2b 34 20 29 20 72 65 74 75 72 6e 20 53 51 4c r+4 ) return SQL
c140: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
c150: 3b 0a 20 20 20 20 20 20 69 50 74 72 20 3d 20 69 ;. iPtr = i
c160: 46 72 65 65 42 6c 6b 3b 0a 20 20 20 20 7d 0a 20 FreeBlk;. }.
c170: 20 20 20 69 66 28 20 69 46 72 65 65 42 6c 6b 3e if( iFreeBlk>
c180: 69 4c 61 73 74 20 29 20 72 65 74 75 72 6e 20 53 iLast ) return S
c190: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
c1a0: 50 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 PT;. assert(
c1b0: 69 46 72 65 65 42 6c 6b 3e 69 50 74 72 20 7c 7c iFreeBlk>iPtr ||
c1c0: 20 69 46 72 65 65 42 6c 6b 3d 3d 30 20 29 3b 0a iFreeBlk==0 );.
c1d0: 20 20 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69 . /* At thi
c1e0: 73 20 70 6f 69 6e 74 3a 0a 20 20 20 20 2a 2a 20 s point:. **
c1f0: 20 20 20 69 46 72 65 65 42 6c 6b 3a 20 20 20 46 iFreeBlk: F
c200: 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 20 61 irst freeblock a
c210: 66 74 65 72 20 69 53 74 61 72 74 2c 20 6f 72 20 fter iStart, or
c220: 7a 65 72 6f 20 69 66 20 6e 6f 6e 65 0a 20 20 20 zero if none.
c230: 20 2a 2a 20 20 20 20 69 50 74 72 3a 20 20 20 20 ** iPtr:
c240: 20 20 20 54 68 65 20 61 64 64 72 65 73 73 20 6f The address o
c250: 66 20 61 20 70 6f 69 6e 74 65 72 20 69 46 72 65 f a pointer iFre
c260: 65 42 6c 6b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 eBlk. **.
c270: 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 ** Check to see
c280: 69 66 20 69 46 72 65 65 42 6c 6b 20 73 68 6f 75 if iFreeBlk shou
c290: 6c 64 20 62 65 20 63 6f 61 6c 65 73 63 65 64 20 ld be coalesced
c2a0: 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 onto the end of
c2b0: 69 53 74 61 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 iStart.. */.
c2c0: 20 20 20 69 66 28 20 69 46 72 65 65 42 6c 6b 20 if( iFreeBlk
c2d0: 26 26 20 69 45 6e 64 2b 33 3e 3d 69 46 72 65 65 && iEnd+3>=iFree
c2e0: 42 6c 6b 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 Blk ){. nFr
c2f0: 61 67 20 3d 20 69 46 72 65 65 42 6c 6b 20 2d 20 ag = iFreeBlk -
c300: 69 45 6e 64 3b 0a 20 20 20 20 20 20 69 66 28 20 iEnd;. if(
c310: 69 45 6e 64 3e 69 46 72 65 65 42 6c 6b 20 29 20 iEnd>iFreeBlk )
c320: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
c330: 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
c340: 20 20 69 45 6e 64 20 3d 20 69 46 72 65 65 42 6c iEnd = iFreeBl
c350: 6b 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 k + get2byte(&da
c360: 74 61 5b 69 46 72 65 65 42 6c 6b 2b 32 5d 29 3b ta[iFreeBlk+2]);
c370: 0a 20 20 20 20 20 20 69 53 69 7a 65 20 3d 20 69 . iSize = i
c380: 45 6e 64 20 2d 20 69 53 74 61 72 74 3b 0a 20 20 End - iStart;.
c390: 20 20 20 20 69 46 72 65 65 42 6c 6b 20 3d 20 67 iFreeBlk = g
c3a0: 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 46 et2byte(&data[iF
c3b0: 72 65 65 42 6c 6b 5d 29 3b 0a 20 20 20 20 7d 0a reeBlk]);. }.
c3c0: 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 69 50 74 . /* If iPt
c3d0: 72 20 69 73 20 61 6e 6f 74 68 65 72 20 66 72 65 r is another fre
c3e0: 65 62 6c 6f 63 6b 20 28 74 68 61 74 20 69 73 2c eblock (that is,
c3f0: 20 69 66 20 69 50 74 72 20 69 73 20 6e 6f 74 20 if iPtr is not
c400: 74 68 65 20 66 72 65 65 6c 69 73 74 0a 20 20 20 the freelist.
c410: 20 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 20 74 ** pointer in t
c420: 68 65 20 70 61 67 65 20 68 65 61 64 65 72 29 20 he page header)
c430: 74 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 then check to se
c440: 65 20 69 66 20 69 53 74 61 72 74 20 73 68 6f 75 e if iStart shou
c450: 6c 64 20 62 65 0a 20 20 20 20 2a 2a 20 63 6f 61 ld be. ** coa
c460: 6c 65 73 63 65 64 20 6f 6e 74 6f 20 74 68 65 20 lesced onto the
c470: 65 6e 64 20 6f 66 20 69 50 74 72 2e 0a 20 20 20 end of iPtr..
c480: 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 74 72 */. if( iPtr
c490: 3e 68 64 72 2b 31 20 29 7b 0a 20 20 20 20 20 20 >hdr+1 ){.
c4a0: 69 6e 74 20 69 50 74 72 45 6e 64 20 3d 20 69 50 int iPtrEnd = iP
c4b0: 74 72 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 tr + get2byte(&d
c4c0: 61 74 61 5b 69 50 74 72 2b 32 5d 29 3b 0a 20 20 ata[iPtr+2]);.
c4d0: 20 20 20 20 69 66 28 20 69 50 74 72 45 6e 64 2b if( iPtrEnd+
c4e0: 33 3e 3d 69 53 74 61 72 74 20 29 7b 0a 20 20 20 3>=iStart ){.
c4f0: 20 20 20 20 20 69 66 28 20 69 50 74 72 45 6e 64 if( iPtrEnd
c500: 3e 69 53 74 61 72 74 20 29 20 72 65 74 75 72 6e >iStart ) return
c510: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
c520: 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 6e 46 BKPT;. nF
c530: 72 61 67 20 2b 3d 20 69 53 74 61 72 74 20 2d 20 rag += iStart -
c540: 69 50 74 72 45 6e 64 3b 0a 20 20 20 20 20 20 20 iPtrEnd;.
c550: 20 69 53 69 7a 65 20 3d 20 69 45 6e 64 20 2d 20 iSize = iEnd -
c560: 69 50 74 72 3b 0a 20 20 20 20 20 20 20 20 69 53 iPtr;. iS
c570: 74 61 72 74 20 3d 20 69 50 74 72 3b 0a 20 20 20 tart = iPtr;.
c580: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
c590: 66 28 20 6e 46 72 61 67 3e 64 61 74 61 5b 68 64 f( nFrag>data[hd
c5a0: 72 2b 37 5d 20 29 20 72 65 74 75 72 6e 20 53 51 r+7] ) return SQ
c5b0: 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
c5c0: 54 3b 0a 20 20 20 20 64 61 74 61 5b 68 64 72 2b T;. data[hdr+
c5d0: 37 5d 20 2d 3d 20 6e 46 72 61 67 3b 0a 20 20 7d 7] -= nFrag;. }
c5e0: 0a 20 20 69 66 28 20 69 53 74 61 72 74 3d 3d 67 . if( iStart==g
c5f0: 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
c600: 72 2b 35 5d 29 20 29 7b 0a 20 20 20 20 2f 2a 20 r+5]) ){. /*
c610: 54 68 65 20 6e 65 77 20 66 72 65 65 62 6c 6f 63 The new freebloc
c620: 6b 20 69 73 20 61 74 20 74 68 65 20 62 65 67 69 k is at the begi
c630: 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 65 6c nning of the cel
c640: 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2c 0a l content area,.
c650: 20 20 20 20 2a 2a 20 73 6f 20 6a 75 73 74 20 65 ** so just e
c660: 78 74 65 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 xtend the cell c
c670: 6f 6e 74 65 6e 74 20 61 72 65 61 20 72 61 74 68 ontent area rath
c680: 65 72 20 74 68 61 6e 20 63 72 65 61 74 65 20 61 er than create a
c690: 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 66 72 nother. ** fr
c6a0: 65 65 6c 69 73 74 20 65 6e 74 72 79 20 2a 2f 0a eelist entry */.
c6b0: 20 20 20 20 69 66 28 20 69 50 74 72 21 3d 68 64 if( iPtr!=hd
c6c0: 72 2b 31 20 29 20 72 65 74 75 72 6e 20 53 51 4c r+1 ) return SQL
c6d0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
c6e0: 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 ;. put2byte(&
c6f0: 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 69 46 72 data[hdr+1], iFr
c700: 65 65 42 6c 6b 29 3b 0a 20 20 20 20 70 75 74 32 eeBlk);. put2
c710: 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
c720: 5d 2c 20 69 45 6e 64 29 3b 0a 20 20 7d 65 6c 73 ], iEnd);. }els
c730: 65 7b 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 e{. /* Insert
c740: 20 74 68 65 20 6e 65 77 20 66 72 65 65 62 6c 6f the new freeblo
c750: 63 6b 20 69 6e 74 6f 20 74 68 65 20 66 72 65 65 ck into the free
c760: 6c 69 73 74 20 2a 2f 0a 20 20 20 20 70 75 74 32 list */. put2
c770: 62 79 74 65 28 26 64 61 74 61 5b 69 50 74 72 5d byte(&data[iPtr]
c780: 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 70 , iStart);. p
c790: 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 53 ut2byte(&data[iS
c7a0: 74 61 72 74 5d 2c 20 69 46 72 65 65 42 6c 6b 29 tart], iFreeBlk)
c7b0: 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 ;. put2byte(&
c7c0: 64 61 74 61 5b 69 53 74 61 72 74 2b 32 5d 2c 20 data[iStart+2],
c7d0: 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 50 iSize);. }. pP
c7e0: 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 69 4f age->nFree += iO
c7f0: 72 69 67 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 rigSize;. retur
c800: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
c810: 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 /*.** Decode the
c820: 20 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 flags byte (the
c830: 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 first byte of t
c840: 68 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 he header) for a
c850: 20 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 page.** and ini
c860: 74 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f tialize fields o
c870: 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 f the MemPage st
c880: 72 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e ructure accordin
c890: 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 gly..**.** Only
c8a0: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
c8b0: 6d 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 mbinations are s
c8c0: 75 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 upported. Anyth
c8d0: 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a ing different.**
c8e0: 20 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 indicates a cor
c8f0: 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
c900: 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 les:.**.**
c910: 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a PTF_ZERODATA.
c920: 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a ** PTF_Z
c930: 45 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 ERODATA | PTF_LE
c940: 41 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 AF.** PT
c950: 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 F_LEAFDATA | PTF
c960: 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 _INTKEY.**
c970: 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 PTF_LEAFDATA
c980: 7c 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 | PTF_INTKEY | P
c990: 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 TF_LEAF.*/.stati
c9a0: 63 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 c int decodeFlag
c9b0: 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 s(MemPage *pPage
c9c0: 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b , int flagByte){
c9d0: 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
c9e0: 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 ; /* A copy
c9f0: 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f of pPage->pBt */
ca00: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
ca10: 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 e->hdrOffset==(p
ca20: 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 Page->pgno==1 ?
ca30: 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 100 : 0) );. as
ca40: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
ca50: 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
ca60: 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
ca70: 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 pPage->leaf = (
ca80: 75 38 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 u8)(flagByte>>3)
ca90: 3b 20 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c ; assert( PTF_L
caa0: 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 EAF == 1<<3 );.
cab0: 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 flagByte &= ~PT
cac0: 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d F_LEAF;. pPage-
cad0: 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 >childPtrSize =
cae0: 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 4-4*pPage->leaf;
caf0: 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e . pBt = pPage->
cb00: 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 pBt;. if( flagB
cb10: 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 yte==(PTF_LEAFDA
cb20: 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 TA | PTF_INTKEY)
cb30: 20 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 ){. /* EVIDE
cb40: 4e 43 45 2d 4f 46 3a 20 52 2d 30 33 36 34 30 2d NCE-OF: R-03640-
cb50: 31 33 34 31 35 20 41 20 76 61 6c 75 65 20 6f 66 13415 A value of
cb60: 20 35 20 6d 65 61 6e 73 20 74 68 65 20 70 61 67 5 means the pag
cb70: 65 20 69 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 e is an interior
cb80: 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 62 2d . ** table b-
cb90: 74 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 tree page. */.
cba0: 20 20 61 73 73 65 72 74 28 20 28 50 54 46 5f 4c assert( (PTF_L
cbb0: 45 41 46 44 41 54 41 7c 50 54 46 5f 49 4e 54 4b EAFDATA|PTF_INTK
cbc0: 45 59 29 3d 3d 35 20 29 3b 0a 20 20 20 20 2f 2a EY)==5 );. /*
cbd0: 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d EVIDENCE-OF: R-
cbe0: 32 30 35 30 31 2d 36 31 37 39 36 20 41 20 76 61 20501-61796 A va
cbf0: 6c 75 65 20 6f 66 20 31 33 20 6d 65 61 6e 73 20 lue of 13 means
cc00: 74 68 65 20 70 61 67 65 20 69 73 20 61 20 6c 65 the page is a le
cc10: 61 66 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 af. ** table
cc20: 62 2d 74 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a b-tree page. */.
cc30: 20 20 20 20 61 73 73 65 72 74 28 20 28 50 54 46 assert( (PTF
cc40: 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 49 4e _LEAFDATA|PTF_IN
cc50: 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 29 3d 3d TKEY|PTF_LEAF)==
cc60: 31 33 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 13 );. pPage-
cc70: 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 >intKey = 1;.
cc80: 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 4c 65 pPage->intKeyLe
cc90: 61 66 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 af = pPage->leaf
cca0: 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 6f 50 ;. pPage->noP
ccb0: 61 79 6c 6f 61 64 20 3d 20 21 70 50 61 67 65 2d ayload = !pPage-
ccc0: 3e 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 >leaf;. pPage
ccd0: 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 ->maxLocal = pBt
cce0: 2d 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 ->maxLeaf;. p
ccf0: 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d Page->minLocal =
cd00: 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 pBt->minLeaf;.
cd10: 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 }else if( flagB
cd20: 79 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 yte==PTF_ZERODAT
cd30: 41 20 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44 A ){. /* EVID
cd40: 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 37 32 32 35 ENCE-OF: R-27225
cd50: 2d 35 33 39 33 36 20 41 20 76 61 6c 75 65 20 6f -53936 A value o
cd60: 66 20 32 20 6d 65 61 6e 73 20 74 68 65 20 70 61 f 2 means the pa
cd70: 67 65 20 69 73 20 61 6e 20 69 6e 74 65 72 69 6f ge is an interio
cd80: 72 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 62 r. ** index b
cd90: 2d 74 72 65 65 20 70 61 67 65 2e 20 2a 2f 0a 20 -tree page. */.
cda0: 20 20 20 61 73 73 65 72 74 28 20 28 50 54 46 5f assert( (PTF_
cdb0: 5a 45 52 4f 44 41 54 41 29 3d 3d 32 20 29 3b 0a ZERODATA)==2 );.
cdc0: 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
cdd0: 4f 46 3a 20 52 2d 31 36 35 37 31 2d 31 31 36 31 OF: R-16571-1161
cde0: 35 20 41 20 76 61 6c 75 65 20 6f 66 20 31 30 20 5 A value of 10
cdf0: 6d 65 61 6e 73 20 74 68 65 20 70 61 67 65 20 69 means the page i
ce00: 73 20 61 20 6c 65 61 66 0a 20 20 20 20 2a 2a 20 s a leaf. **
ce10: 69 6e 64 65 78 20 62 2d 74 72 65 65 20 70 61 67 index b-tree pag
ce20: 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 e. */. assert
ce30: 28 20 28 50 54 46 5f 5a 45 52 4f 44 41 54 41 7c ( (PTF_ZERODATA|
ce40: 50 54 46 5f 4c 45 41 46 29 3d 3d 31 30 20 29 3b PTF_LEAF)==10 );
ce50: 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b . pPage->intK
ce60: 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ey = 0;. pPag
ce70: 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 3d 20 e->intKeyLeaf =
ce80: 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 6f 0;. pPage->no
ce90: 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 Payload = 0;.
cea0: 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c pPage->maxLocal
ceb0: 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c = pBt->maxLocal
cec0: 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e ;. pPage->min
ced0: 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e Local = pBt->min
cee0: 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a Local;. }else{.
cef0: 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
cf00: 4f 46 3a 20 52 2d 34 37 36 30 38 2d 35 36 34 36 OF: R-47608-5646
cf10: 39 20 41 6e 79 20 6f 74 68 65 72 20 76 61 6c 75 9 Any other valu
cf20: 65 20 66 6f 72 20 74 68 65 20 62 2d 74 72 65 65 e for the b-tree
cf30: 20 70 61 67 65 20 74 79 70 65 20 69 73 0a 20 20 page type is.
cf40: 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 2a ** an error. *
cf50: 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
cf60: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
cf70: 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6d ;. }. pPage->m
cf80: 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d ax1bytePayload =
cf90: 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 65 50 61 pBt->max1bytePa
cfa0: 79 6c 6f 61 64 3b 0a 20 20 72 65 74 75 72 6e 20 yload;. return
cfb0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
cfc0: 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
cfd0: 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 he auxiliary inf
cfe0: 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 ormation for a d
cff0: 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a isk block..**.**
d000: 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
d010: 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 K on success. I
d020: 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 f we see that th
d030: 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e e page does.** n
d040: 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c ot contain a wel
d050: 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 l-formed databas
d060: 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 e page, then ret
d070: 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 urn .** SQLITE_C
d080: 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 ORRUPT. Note th
d090: 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 at a return of S
d0a0: 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f QLITE_OK does no
d0b0: 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 t.** guarantee t
d0c0: 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
d0d0: 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 well-formed. It
d0e0: 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 only shows that
d0f0: 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f .** we failed to
d100: 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 detect any corr
d110: 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 uption..*/.stati
d120: 63 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 c int btreeInitP
d130: 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
d140: 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 ge){.. assert(
d150: 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b pPage->pBt!=0 );
d160: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
d170: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
d180: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
d190: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
d1a0: 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 age->pgno==sqlit
d1b0: 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
d1c0: 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 r(pPage->pDbPage
d1d0: 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
d1e0: 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 Page == sqlite3P
d1f0: 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 agerGetExtra(pPa
d200: 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
d210: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
d220: 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 >aData == sqlite
d230: 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 3PagerGetData(pP
d240: 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
d250: 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e .. if( !pPage->
d260: 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 isInit ){. u1
d270: 36 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 6 pc;
d280: 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 /* Address of a
d290: 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 freeblock withi
d2a0: 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d n pPage->aData[]
d2b0: 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 */. u8 hdr;
d2c0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
d2d0: 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e fset to beginnin
d2e0: 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 g of page header
d2f0: 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 */. u8 *data
d300: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 ; /* Eq
d310: 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 ual to pPage->aD
d320: 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 ata */. BtSha
d330: 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 red *pBt;
d340: 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 /* The main btr
d350: 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a ee structure */.
d360: 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 int usableSi
d370: 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 ze; /* Amount
d380: 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 of usable space
d390: 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f on each page */
d3a0: 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 . u16 cellOff
d3b0: 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 set; /* Offse
d3c0: 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 t from start of
d3d0: 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 page to first ce
d3e0: 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 ll pointer */.
d3f0: 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 int nFree;
d400: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
d410: 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f f unused bytes o
d420: 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 n the page */.
d430: 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 int top;
d440: 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
d450: 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 te of the cell c
d460: 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
d470: 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 int iCellFirs
d480: 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 t; /* First a
d490: 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 llowable cell or
d4a0: 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 freeblock offse
d4b0: 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 t */. int iCe
d4c0: 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c llLast; /* L
d4d0: 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c ast possible cel
d4e0: 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f l or freeblock o
d4f0: 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 ffset */.. pB
d500: 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
d510: 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 . hdr = pPage
d520: 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 ->hdrOffset;.
d530: 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
d540: 44 61 74 61 3b 0a 20 20 20 20 2f 2a 20 45 56 49 Data;. /* EVI
d550: 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 32 38 35 39 DENCE-OF: R-2859
d560: 34 2d 30 32 38 39 30 20 54 68 65 20 6f 6e 65 2d 4-02890 The one-
d570: 62 79 74 65 20 66 6c 61 67 20 61 74 20 6f 66 66 byte flag at off
d580: 73 65 74 20 30 20 69 6e 64 69 63 61 74 69 6e 67 set 0 indicating
d590: 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 2d 74 72 . ** the b-tr
d5a0: 65 65 20 70 61 67 65 20 74 79 70 65 2e 20 2a 2f ee page type. */
d5b0: 0a 20 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 . if( decodeF
d5c0: 6c 61 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 lags(pPage, data
d5d0: 5b 68 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 [hdr]) ) return
d5e0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
d5f0: 4b 50 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 KPT;. assert(
d600: 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d pBt->pageSize>=
d610: 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 512 && pBt->page
d620: 53 69 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 Size<=65536 );.
d630: 20 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 pPage->maskPa
d640: 67 65 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e ge = (u16)(pBt->
d650: 70 61 67 65 53 69 7a 65 20 2d 20 31 29 3b 0a 20 pageSize - 1);.
d660: 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 pPage->nOverf
d670: 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 low = 0;. usa
d680: 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 bleSize = pBt->u
d690: 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 sableSize;. p
d6a0: 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
d6b0: 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 = cellOffset =
d6c0: 68 64 72 20 2b 20 38 20 2b 20 70 50 61 67 65 2d hdr + 8 + pPage-
d6d0: 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 >childPtrSize;.
d6e0: 20 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 pPage->aDataE
d6f0: 6e 64 20 3d 20 26 64 61 74 61 5b 75 73 61 62 6c nd = &data[usabl
d700: 65 53 69 7a 65 5d 3b 0a 20 20 20 20 70 50 61 67 eSize];. pPag
d710: 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64 e->aCellIdx = &d
d720: 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b ata[cellOffset];
d730: 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 . /* EVIDENCE
d740: 2d 4f 46 3a 20 52 2d 35 38 30 31 35 2d 34 38 31 -OF: R-58015-481
d750: 37 35 20 54 68 65 20 74 77 6f 2d 62 79 74 65 20 75 The two-byte
d760: 69 6e 74 65 67 65 72 20 61 74 20 6f 66 66 73 65 integer at offse
d770: 74 20 35 20 64 65 73 69 67 6e 61 74 65 73 0a 20 t 5 designates.
d780: 20 20 20 2a 2a 20 74 68 65 20 73 74 61 72 74 20 ** the start
d790: 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
d7a0: 65 6e 74 20 61 72 65 61 2e 20 41 20 7a 65 72 6f ent area. A zero
d7b0: 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 value for this
d7c0: 69 6e 74 65 67 65 72 20 69 73 0a 20 20 20 20 2a integer is. *
d7d0: 2a 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 * interpreted as
d7e0: 20 36 35 35 33 36 2e 20 2a 2f 0a 20 20 20 20 74 65536. */. t
d7f0: 6f 70 20 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 op = get2byteNot
d800: 5a 65 72 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 Zero(&data[hdr+5
d810: 5d 29 3b 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 ]);. /* EVIDE
d820: 4e 43 45 2d 4f 46 3a 20 52 2d 33 37 30 30 32 2d NCE-OF: R-37002-
d830: 33 32 37 37 34 20 54 68 65 20 74 77 6f 2d 62 79 32774 The two-by
d840: 74 65 20 69 6e 74 65 67 65 72 20 61 74 20 6f 66 te integer at of
d850: 66 73 65 74 20 33 20 67 69 76 65 73 20 74 68 65 fset 3 gives the
d860: 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f . ** number o
d870: 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 f cells on the p
d880: 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 age. */. pPag
d890: 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 e->nCell = get2b
d8a0: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
d8b0: 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
d8c0: 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 ->nCell>MX_CELL(
d8d0: 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a pBt) ){. /*
d8e0: 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20 66 To many cells f
d8f0: 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 or a single page
d900: 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73 74 . The page must
d910: 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a 20 be corrupt */.
d920: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
d930: 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
d940: 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63 . }. testc
d950: 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ase( pPage->nCel
d960: 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 l==MX_CELL(pBt)
d970: 29 3b 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e );. /* EVIDEN
d980: 43 45 2d 4f 46 3a 20 52 2d 32 34 30 38 39 2d 35 CE-OF: R-24089-5
d990: 37 39 37 39 20 49 66 20 61 20 70 61 67 65 20 63 7979 If a page c
d9a0: 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 ontains no cells
d9b0: 20 28 77 68 69 63 68 20 69 73 20 6f 6e 6c 79 0a (which is only.
d9c0: 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 65 20 ** possible
d9d0: 66 6f 72 20 61 20 72 6f 6f 74 20 70 61 67 65 20 for a root page
d9e0: 6f 66 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 of a table that
d9f0: 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 72 6f 77 73 contains no rows
da00: 29 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a ) then the. *
da10: 2a 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * offset to the
da20: 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
da30: 61 20 77 69 6c 6c 20 65 71 75 61 6c 20 74 68 65 a will equal the
da40: 20 70 61 67 65 20 73 69 7a 65 20 6d 69 6e 75 73 page size minus
da50: 20 74 68 65 0a 20 20 20 20 2a 2a 20 62 79 74 65 the. ** byte
da60: 73 20 6f 66 20 72 65 73 65 72 76 65 64 20 73 70 s of reserved sp
da70: 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 ace. */. asse
da80: 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c rt( pPage->nCell
da90: 3e 30 20 7c 7c 20 74 6f 70 3d 3d 75 73 61 62 6c >0 || top==usabl
daa0: 65 53 69 7a 65 20 7c 7c 20 43 4f 52 52 55 50 54 eSize || CORRUPT
dab0: 5f 44 42 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 _DB );.. /* A
dac0: 20 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 malformed datab
dad0: 61 73 65 20 70 61 67 65 20 6d 69 67 68 74 20 63 ase page might c
dae0: 61 75 73 65 20 75 73 20 74 6f 20 72 65 61 64 20 ause us to read
daf0: 70 61 73 74 20 74 68 65 20 65 6e 64 0a 20 20 20 past the end.
db00: 20 2a 2a 20 6f 66 20 70 61 67 65 20 77 68 65 6e ** of page when
db10: 20 70 61 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e parsing a cell.
db20: 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a . **. **
db30: 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 The following b
db40: 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 lock of code che
db50: 63 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 65 65 cks early to see
db60: 20 69 66 20 61 20 63 65 6c 6c 20 65 78 74 65 6e if a cell exten
db70: 64 73 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 ds. ** past t
db80: 68 65 20 65 6e 64 20 6f 66 20 61 20 70 61 67 65 he end of a page
db90: 20 62 6f 75 6e 64 61 72 79 20 61 6e 64 20 63 61 boundary and ca
dba0: 75 73 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 uses SQLITE_CORR
dbb0: 55 50 54 20 74 6f 20 62 65 20 0a 20 20 20 20 2a UPT to be . *
dbc0: 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 69 74 * returned if it
dbd0: 20 64 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 does.. */.
dbe0: 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 iCellFirst = c
dbf0: 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 ellOffset + 2*pP
dc00: 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 age->nCell;.
dc10: 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 iCellLast = usab
dc20: 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 leSize - 4;.#if
dc30: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
dc40: 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 NABLE_OVERSIZE_C
dc50: 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b ELL_CHECK). {
dc60: 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
dc70: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
dc80: 78 20 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 x into the cell
dc90: 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f pointer array */
dca0: 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 . int sz;
dcb0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
dcc0: 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 of a cell */..
dcd0: 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d if( !pPage-
dce0: 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 >leaf ) iCellLas
dcf0: 74 2d 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 t--;. for(i
dd00: 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 =0; i<pPage->nCe
dd10: 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ll; i++){.
dd20: 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 pc = get2byte(
dd30: 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 &data[cellOffset
dd40: 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 +i*2]);.
dd50: 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 testcase( pc==iC
dd60: 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ellFirst );.
dd70: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 testcase( pc
dd80: 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 ==iCellLast );.
dd90: 20 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 if( pc<iC
dda0: 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 ellFirst || pc>i
ddb0: 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 CellLast ){.
ddc0: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
ddd0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
dde0: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
ddf0: 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a sz = cellSiz
de00: 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 ePtr(pPage, &dat
de10: 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 a[pc]);.
de20: 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d testcase( pc+sz=
de30: 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 =usableSize );.
de40: 20 20 20 20 20 20 20 69 66 28 20 70 63 2b 73 7a if( pc+sz
de50: 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 >usableSize ){.
de60: 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
de70: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
de80: 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 KPT;. }.
de90: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
dea0: 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 !pPage->leaf )
deb0: 69 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 iCellLast++;.
dec0: 20 7d 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 } .#endif..
ded0: 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 /* Compute the
dee0: 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 total free space
def0: 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 on the page.
df00: 20 2a 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a ** EVIDENCE-OF:
df10: 20 52 2d 32 33 35 38 38 2d 33 34 34 35 30 20 54 R-23588-34450 T
df20: 68 65 20 74 77 6f 2d 62 79 74 65 20 69 6e 74 65 he two-byte inte
df30: 67 65 72 20 61 74 20 6f 66 66 73 65 74 20 31 20 ger at offset 1
df40: 67 69 76 65 73 20 74 68 65 0a 20 20 20 20 2a 2a gives the. **
df50: 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 66 69 start of the fi
df60: 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 20 6f 6e rst freeblock on
df70: 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 69 73 the page, or is
df80: 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 zero if there a
df90: 72 65 20 6e 6f 0a 20 20 20 20 2a 2a 20 66 72 65 re no. ** fre
dfa0: 65 62 6c 6f 63 6b 73 2e 20 2a 2f 0a 20 20 20 20 eblocks. */.
dfb0: 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
dfc0: 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 ata[hdr+1]);.
dfd0: 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68 64 nFree = data[hd
dfe0: 72 2b 37 5d 20 2b 20 74 6f 70 3b 20 20 2f 2a 20 r+7] + top; /*
dff0: 49 6e 69 74 20 6e 46 72 65 65 20 74 6f 20 6e 6f Init nFree to no
e000: 6e 2d 66 72 65 65 62 6c 6f 63 6b 20 66 72 65 65 n-freeblock free
e010: 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 77 68 space */. wh
e020: 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 ile( pc>0 ){.
e030: 20 20 20 75 31 36 20 6e 65 78 74 2c 20 73 69 7a u16 next, siz
e040: 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c e;. if( pc<
e050: 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 iCellFirst || pc
e060: 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 >iCellLast ){.
e070: 20 20 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 /* EVIDENC
e080: 45 2d 4f 46 3a 20 52 2d 35 35 35 33 30 2d 35 32 E-OF: R-55530-52
e090: 39 33 30 20 49 6e 20 61 20 77 65 6c 6c 2d 66 6f 930 In a well-fo
e0a0: 72 6d 65 64 20 62 2d 74 72 65 65 20 70 61 67 65 rmed b-tree page
e0b0: 2c 20 74 68 65 72 65 20 77 69 6c 6c 0a 20 20 20 , there will.
e0c0: 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 62 ** always b
e0d0: 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 63 e at least one c
e0e0: 65 6c 6c 20 62 65 66 6f 72 65 20 74 68 65 20 66 ell before the f
e0f0: 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 2e 0a irst freeblock..
e100: 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
e110: 20 20 20 2a 2a 20 4f 72 2c 20 74 68 65 20 66 72 ** Or, the fr
e120: 65 65 62 6c 6f 63 6b 20 69 73 20 6f 66 66 20 74 eeblock is off t
e130: 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 he end of the pa
e140: 67 65 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 ge. */.
e150: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
e160: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
e170: 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ; . }.
e180: 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 next = get2byte
e190: 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 (&data[pc]);.
e1a0: 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 size = get2by
e1b0: 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b te(&data[pc+2]);
e1c0: 0a 20 20 20 20 20 20 69 66 28 20 28 6e 65 78 74 . if( (next
e1d0: 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 >0 && next<=pc+s
e1e0: 69 7a 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a ize+3) || pc+siz
e1f0: 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a e>usableSize ){.
e200: 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
e210: 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 blocks must be i
e220: 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
e230: 72 2e 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20 r. And the last
e240: 62 79 74 65 20 6f 66 0a 20 20 20 20 20 20 20 20 byte of.
e250: 2a 2a 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 ** the free-bloc
e260: 6b 20 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 k must lie on th
e270: 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e e database page.
e280: 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 */. ret
e290: 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
e2a0: 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 PT_BKPT; .
e2b0: 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 }. nFree =
e2c0: 6e 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 nFree + size;.
e2d0: 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 pc = next;.
e2e0: 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 }.. /* At
e2f0: 74 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 this point, nFre
e300: 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 e contains the s
e310: 75 6d 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 um of the offset
e320: 20 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 to the start.
e330: 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c ** of the cell
e340: 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c -content area pl
e350: 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 us the number of
e360: 20 66 72 65 65 20 62 79 74 65 73 20 77 69 74 68 free bytes with
e370: 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 in. ** the ce
e380: 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e ll-content area.
e390: 20 49 66 20 74 68 69 73 20 69 73 20 67 72 65 61 If this is grea
e3a0: 74 65 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 ter than the usa
e3b0: 62 6c 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 ble-size. **
e3c0: 6f 66 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 of the page, the
e3d0: 6e 20 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 n the page must
e3e0: 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 be corrupted. Th
e3f0: 69 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 is check also.
e400: 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 ** serves to v
e410: 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6f erify that the o
e420: 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 ffset to the sta
e430: 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 rt of the cell-c
e440: 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 ontent. ** ar
e450: 65 61 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f ea, according to
e460: 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
e470: 2c 20 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 , lies within th
e480: 65 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 e page.. */.
e490: 20 20 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 if( nFree>usa
e4a0: 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 bleSize ){.
e4b0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
e4c0: 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 ORRUPT_BKPT; .
e4d0: 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e }. pPage->n
e4e0: 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 Free = (u16)(nFr
e4f0: 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 ee - iCellFirst)
e500: 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 ;. pPage->isI
e510: 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 nit = 1;. }. r
e520: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e530: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 .}../*.** Set up
e540: 20 61 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 a raw page so t
e550: 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b hat it looks lik
e560: 65 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 e a database pag
e570: 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 e holding.** no
e580: 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 entries..*/.stat
e590: 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 ic void zeroPage
e5a0: 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
e5b0: 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 int flags){. u
e5c0: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 nsigned char *da
e5d0: 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
e5e0: 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 a;. BtShared *p
e5f0: 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
e600: 0a 20 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 . u8 hdr = pPag
e610: 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
e620: 75 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 u16 first;.. as
e630: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
e640: 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 erPagenumber(pPa
e650: 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 ge->pDbPage)==pP
e660: 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 age->pgno );. a
e670: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
e680: 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
e690: 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 e->pDbPage) == (
e6a0: 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 void*)pPage );.
e6b0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
e6c0: 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
e6d0: 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 ge->pDbPage) ==
e6e0: 64 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 data );. assert
e6f0: 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
e700: 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
e710: 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
e720: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
e730: 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
e740: 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
e750: 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
e760: 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TS_SECURE_DELETE
e770: 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 ){. memset(&
e780: 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 data[hdr], 0, pB
e790: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
e7a0: 68 64 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 hdr);. }. data
e7b0: 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c [hdr] = (char)fl
e7c0: 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 ags;. first = h
e7d0: 64 72 20 2b 20 28 28 66 6c 61 67 73 26 50 54 46 dr + ((flags&PTF
e7e0: 5f 4c 45 41 46 29 3d 3d 30 20 3f 20 31 32 20 3a _LEAF)==0 ? 12 :
e7f0: 20 38 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 8);. memset(&d
e800: 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 ata[hdr+1], 0, 4
e810: 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d );. data[hdr+7]
e820: 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 = 0;. put2byte
e830: 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 (&data[hdr+5], p
e840: 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b Bt->usableSize);
e850: 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 . pPage->nFree
e860: 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 75 73 61 = (u16)(pBt->usa
e870: 62 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 29 bleSize - first)
e880: 3b 0a 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 ;. decodeFlags(
e890: 70 50 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 pPage, flags);.
e8a0: 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 pPage->cellOffs
e8b0: 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 50 et = first;. pP
e8c0: 61 67 65 2d 3e 61 44 61 74 61 45 6e 64 20 3d 20 age->aDataEnd =
e8d0: 26 64 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c &data[pBt->usabl
e8e0: 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 2d eSize];. pPage-
e8f0: 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64 61 74 >aCellIdx = &dat
e900: 61 5b 66 69 72 73 74 5d 3b 0a 20 20 70 50 61 67 a[first];. pPag
e910: 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 e->nOverflow = 0
e920: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
e930: 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 >pageSize>=512 &
e940: 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c & pBt->pageSize<
e950: 3d 36 35 35 33 36 20 29 3b 0a 20 20 70 50 61 67 =65536 );. pPag
e960: 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 28 75 e->maskPage = (u
e970: 31 36 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 16)(pBt->pageSiz
e980: 65 20 2d 20 31 29 3b 0a 20 20 70 50 61 67 65 2d e - 1);. pPage-
e990: 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 >nCell = 0;. pP
e9a0: 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b age->isInit = 1;
e9b0: 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 .}.../*.** Conve
e9c0: 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 rt a DbPage obta
e9d0: 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 ined from the pa
e9e0: 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 ger into a MemPa
e9f0: 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 ge used by.** th
ea00: 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a e btree layer..*
ea10: 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 /.static MemPage
ea20: 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 *btreePageFromD
ea30: 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 bPage(DbPage *pD
ea40: 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f bPage, Pgno pgno
ea50: 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 , BtShared *pBt)
ea60: 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
ea70: 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 ge = (MemPage*)s
ea80: 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
ea90: 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 tra(pDbPage);.
eaa0: 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 pPage->aData = s
eab0: 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
eac0: 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ta(pDbPage);. p
ead0: 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 Page->pDbPage =
eae0: 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 pDbPage;. pPage
eaf0: 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 ->pBt = pBt;. p
eb00: 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e Page->pgno = pgn
eb10: 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f o;. pPage->hdrO
eb20: 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 ffset = pPage->p
eb30: 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 gno==1 ? 100 : 0
eb40: 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 ;. return pPage
eb50: 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 ; .}../*.** Get
eb60: 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
eb70: 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 pager. Initiali
eb80: 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 ze the MemPage.p
eb90: 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 Bt and.** MemPag
eba0: 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 e.aData elements
ebb0: 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a if needed..**.*
ebc0: 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 * If the noConte
ebd0: 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 nt flag is set,
ebe0: 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 it means that we
ebf0: 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
ec00: 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ut.** the conten
ec10: 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 t of the page at
ec20: 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 this time. So
ec30: 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 do not go to the
ec40: 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 disk.** to fetc
ec50: 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 h the content.
ec60: 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 Just fill in the
ec70: 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 content with ze
ec80: 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 ros for now..**
ec90: 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 If in the future
eca0: 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 we call sqlite3
ecb0: 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 PagerWrite() on
ecc0: 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a this page, that.
ecd0: 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 ** means we have
ece0: 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 started to be c
ecf0: 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 oncerned about c
ed00: 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 ontent and the d
ed10: 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 isk.** read shou
ed20: 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 ld occur at that
ed30: 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 point..*/.stati
ed40: 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61 c int btreeGetPa
ed50: 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
ed60: 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 pBt, /* Th
ed70: 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e e btree */. Pgn
ed80: 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
ed90: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
eda0: 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 he page to fetch
edb0: 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
edc0: 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 ppPage, /* Re
edd0: 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e turn the page in
ede0: 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 this parameter
edf0: 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 */. int flags
ee00: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 /* PAG
ee10: 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 ER_GET_NOCONTENT
ee20: 20 6f 72 20 50 41 47 45 52 5f 47 45 54 5f 52 45 or PAGER_GET_RE
ee30: 41 44 4f 4e 4c 59 20 2a 2f 0a 29 7b 0a 20 20 69 ADONLY */.){. i
ee40: 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 67 65 20 nt rc;. DbPage
ee50: 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 73 73 *pDbPage;.. ass
ee60: 65 72 74 28 20 66 6c 61 67 73 3d 3d 30 20 7c 7c ert( flags==0 ||
ee70: 20 66 6c 61 67 73 3d 3d 50 41 47 45 52 5f 47 45 flags==PAGER_GE
ee80: 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 7c 7c 20 66 T_NOCONTENT || f
ee90: 6c 61 67 73 3d 3d 50 41 47 45 52 5f 47 45 54 5f lags==PAGER_GET_
eea0: 52 45 41 44 4f 4e 4c 59 20 29 3b 0a 20 20 61 73 READONLY );. as
eeb0: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
eec0: 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
eed0: 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 tex) );. rc = s
eee0: 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
eef0: 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 re(pBt->pPager,
ef00: 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a 29 pgno, (DbPage**)
ef10: 26 70 44 62 50 61 67 65 2c 20 66 6c 61 67 73 29 &pDbPage, flags)
ef20: 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
ef30: 75 72 6e 20 72 63 3b 0a 20 20 2a 70 70 50 61 67 urn rc;. *ppPag
ef40: 65 20 3d 20 62 74 72 65 65 50 61 67 65 46 72 6f e = btreePageFro
ef50: 6d 44 62 50 61 67 65 28 70 44 62 50 61 67 65 2c mDbPage(pDbPage,
ef60: 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 72 pgno, pBt);. r
ef70: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
ef80: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 .}../*.** Retrie
ef90: 76 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 ve a page from t
efa0: 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 he pager cache.
efb0: 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
efc0: 20 70 61 67 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 page is not.**
efd0: 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 already in the p
efe0: 61 67 65 72 20 63 61 63 68 65 20 72 65 74 75 72 ager cache retur
eff0: 6e 20 4e 55 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 n NULL. Initiali
f000: 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 ze the MemPage.p
f010: 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 Bt and.** MemPag
f020: 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 e.aData elements
f030: 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 if needed..*/.s
f040: 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 tatic MemPage *b
f050: 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 42 treePageLookup(B
f060: 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 tShared *pBt, Pg
f070: 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 44 62 50 61 no pgno){. DbPa
f080: 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 61 ge *pDbPage;. a
f090: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
f0a0: 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
f0b0: 75 74 65 78 29 20 29 3b 0a 20 20 70 44 62 50 61 utex) );. pDbPa
f0c0: 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ge = sqlite3Page
f0d0: 72 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50 61 rLookup(pBt->pPa
f0e0: 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 ger, pgno);. if
f0f0: 28 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20 20 ( pDbPage ){.
f100: 20 72 65 74 75 72 6e 20 62 74 72 65 65 50 61 67 return btreePag
f110: 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 eFromDbPage(pDbP
f120: 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b age, pgno, pBt);
f130: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
f140: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
f150: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
f160: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
f170: 6e 20 70 61 67 65 73 2e 20 49 66 20 74 68 65 72 n pages. If ther
f180: 65 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 e is any kind of
f190: 0a 2a 2a 20 65 72 72 6f 72 2c 20 72 65 74 75 72 .** error, retur
f1a0: 6e 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 n ((unsigned int
f1b0: 29 2d 31 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 )-1)..*/.static
f1c0: 50 67 6e 6f 20 62 74 72 65 65 50 61 67 65 63 6f Pgno btreePageco
f1d0: 75 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 unt(BtShared *pB
f1e0: 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 t){. return pBt
f1f0: 2d 3e 6e 50 61 67 65 3b 0a 7d 0a 75 33 32 20 73 ->nPage;.}.u32 s
f200: 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 50 qlite3BtreeLastP
f210: 61 67 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 age(Btree *p){.
f220: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
f230: 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
f240: 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
f250: 28 28 70 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 29 ((p->pBt->nPage)
f260: 26 30 78 38 30 30 30 30 30 30 29 3d 3d 30 20 29 &0x8000000)==0 )
f270: 3b 0a 20 20 72 65 74 75 72 6e 20 62 74 72 65 65 ;. return btree
f280: 50 61 67 65 63 6f 75 6e 74 28 70 2d 3e 70 42 74 Pagecount(p->pBt
f290: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 );.}../*.** Get
f2a0: 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
f2b0: 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 pager and initia
f2c0: 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 lize it. This r
f2d0: 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20 61 outine is just a
f2e0: 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 .** convenience
f2f0: 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 wrapper around s
f300: 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f eparate calls to
f310: 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20 btreeGetPage()
f320: 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69 and .** btreeIni
f330: 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 tPage()..**.** I
f340: 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
f350: 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 s, then the valu
f360: 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 e *ppPage is set
f370: 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65 64 to is undefined
f380: 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61 . It.** may rema
f390: 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72 in unchanged, or
f3a0: 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20 74 it may be set t
f3b0: 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c o an invalid val
f3c0: 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ue..*/.static in
f3d0: 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 t getAndInitPage
f3e0: 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
f3f0: 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
f400: 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
f410: 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 ase file */. Pg
f420: 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 no pgno,
f430: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f440: 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 Number of the p
f450: 61 67 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 age to get */.
f460: 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 MemPage **ppPage
f470: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
f480: 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 /* Write the pag
f490: 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a e pointer here *
f4a0: 2f 0a 20 20 69 6e 74 20 62 52 65 61 64 6f 6e 6c /. int bReadonl
f4b0: 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
f4c0: 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 47 45 54 /* PAGER_GET
f4d0: 5f 52 45 41 44 4f 4e 4c 59 20 6f 72 20 30 20 2a _READONLY or 0 *
f4e0: 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
f4f0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
f500: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
f510: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
f520: 65 72 74 28 20 62 52 65 61 64 6f 6e 6c 79 3d 3d ert( bReadonly==
f530: 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e PAGER_GET_READON
f540: 4c 59 20 7c 7c 20 62 52 65 61 64 6f 6e 6c 79 3d LY || bReadonly=
f550: 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 67 6e =0 );.. if( pgn
f560: 6f 3e 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 o>btreePagecount
f570: 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 63 20 (pBt) ){. rc
f580: 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
f590: 5f 42 4b 50 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a _BKPT;. }else{.
f5a0: 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
f5b0: 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c tPage(pBt, pgno,
f5c0: 20 70 70 50 61 67 65 2c 20 62 52 65 61 64 6f 6e ppPage, bReadon
f5d0: 6c 79 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ly);. if( rc=
f5e0: 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 2a =SQLITE_OK && (*
f5f0: 70 70 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 3d ppPage)->isInit=
f600: 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d =0 ){. rc =
f610: 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a btreeInitPage(*
f620: 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 ppPage);. i
f630: 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
f640: 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ){. rele
f650: 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 asePage(*ppPage)
f660: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
f670: 20 20 7d 0a 0a 20 20 74 65 73 74 63 61 73 65 28 }.. testcase(
f680: 20 70 67 6e 6f 3d 3d 30 20 29 3b 0a 20 20 61 73 pgno==0 );. as
f690: 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 7c 7c sert( pgno!=0 ||
f6a0: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 rc==SQLITE_CORR
f6b0: 55 50 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 UPT );. return
f6c0: 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c rc;.}../*.** Rel
f6d0: 65 61 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 ease a MemPage.
f6e0: 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 This should be
f6f0: 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 called once for
f700: 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 each prior.** ca
f710: 6c 6c 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 ll to btreeGetPa
f720: 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ge..*/.static vo
f730: 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d id releasePage(M
f740: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a emPage *pPage){.
f750: 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
f760: 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
f770: 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 ->aData );. a
f780: 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 ssert( pPage->pB
f790: 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
f7a0: 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 21 pPage->pDbPage!
f7b0: 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
f7c0: 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 ( sqlite3PagerGe
f7d0: 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 tExtra(pPage->pD
f7e0: 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a bPage) == (void*
f7f0: 29 70 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 )pPage );. as
f800: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
f810: 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d erGetData(pPage-
f820: 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 >pDbPage)==pPage
f830: 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 ->aData );. a
f840: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
f850: 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
f860: 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
f870: 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
f880: 55 6e 72 65 66 4e 6f 74 4e 75 6c 6c 28 70 50 61 UnrefNotNull(pPa
f890: 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
f8a0: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e }.}../*.** Durin
f8b0: 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 g a rollback, wh
f8c0: 65 6e 20 74 68 65 20 70 61 67 65 72 20 72 65 6c en the pager rel
f8d0: 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e oads information
f8e0: 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 0a into the cache.
f8f0: 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 ** so that the c
f900: 61 63 68 65 20 69 73 20 72 65 73 74 6f 72 65 64 ache is restored
f910: 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c to its original
f920: 20 73 74 61 74 65 20 61 74 20 74 68 65 20 73 74 state at the st
f930: 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74 72 art of.** the tr
f940: 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20 65 ansaction, for e
f950: 61 63 68 20 70 61 67 65 20 72 65 73 74 6f 72 65 ach page restore
f960: 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 d this routine i
f970: 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 s called..**.**
f980: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 This routine nee
f990: 64 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 ds to reset the
f9a0: 65 78 74 72 61 20 64 61 74 61 20 73 65 63 74 69 extra data secti
f9b0: 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 on at the end of
f9c0: 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 the.** page to
f9d0: 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20 72 agree with the r
f9e0: 65 73 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a 2f estored data..*/
f9f0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
fa00: 65 52 65 69 6e 69 74 28 44 62 50 61 67 65 20 2a eReinit(DbPage *
fa10: 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61 67 pData){. MemPag
fa20: 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 67 e *pPage;. pPag
fa30: 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 e = (MemPage *)s
fa40: 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
fa50: 74 72 61 28 70 44 61 74 61 29 3b 0a 20 20 61 73 tra(pData);. as
fa60: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
fa70: 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 erPageRefcount(p
fa80: 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 69 66 28 Data)>0 );. if(
fa90: 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
faa0: 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 {. assert( sq
fab0: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
fac0: 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
fad0: 65 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 ex) );. pPage
fae0: 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
faf0: 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 if( sqlite3Pag
fb00: 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 erPageRefcount(p
fb10: 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 20 20 20 Data)>1 ){.
fb20: 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 74 20 /* pPage might
fb30: 6e 6f 74 20 62 65 20 61 20 62 74 72 65 65 20 70 not be a btree p
fb40: 61 67 65 3b 20 20 69 74 20 6d 69 67 68 74 20 62 age; it might b
fb50: 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 e an overflow pa
fb60: 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 70 ge. ** or p
fb70: 74 72 6d 61 70 20 70 61 67 65 20 6f 72 20 61 20 trmap page or a
fb80: 66 72 65 65 20 70 61 67 65 2e 20 20 49 6e 20 74 free page. In t
fb90: 68 6f 73 65 20 63 61 73 65 73 2c 20 74 68 65 20 hose cases, the
fba0: 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 20 20 following.
fbb0: 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 ** call to btree
fbc0: 49 6e 69 74 50 61 67 65 28 29 20 77 69 6c 6c 20 InitPage() will
fbd0: 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e 20 53 51 likely return SQ
fbe0: 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 0a 20 20 LITE_CORRUPT..
fbf0: 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f 20 68 61 ** But no ha
fc00: 72 6d 20 69 73 20 64 6f 6e 65 20 62 79 20 74 68 rm is done by th
fc10: 69 73 2e 20 20 41 6e 64 20 69 74 20 69 73 20 76 is. And it is v
fc20: 65 72 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 ery important th
fc30: 61 74 0a 20 20 20 20 20 20 2a 2a 20 62 74 72 65 at. ** btre
fc40: 65 49 6e 69 74 50 61 67 65 28 29 20 62 65 20 63 eInitPage() be c
fc50: 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20 62 alled on every b
fc60: 74 72 65 65 20 70 61 67 65 20 73 6f 20 77 65 20 tree page so we
fc70: 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 make. ** th
fc80: 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72 79 e call for every
fc90: 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65 73 page that comes
fca0: 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74 69 in for re-initi
fcb0: 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 62 74 72 ng. */. btr
fcc0: 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
fcd0: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
fce0: 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 /*.** Invoke the
fcf0: 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f busy handler fo
fd00: 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 r a btree..*/.st
fd10: 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e atic int btreeIn
fd20: 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 vokeBusyHandler(
fd30: 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 42 void *pArg){. B
fd40: 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 28 tShared *pBt = (
fd50: 42 74 53 68 61 72 65 64 2a 29 70 41 72 67 3b 0a BtShared*)pArg;.
fd60: 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 64 assert( pBt->d
fd70: 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 b );. assert( s
fd80: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
fd90: 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 74 65 78 d(pBt->db->mutex
fda0: 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 ) );. return sq
fdb0: 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 lite3InvokeBusyH
fdc0: 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e 64 62 2d andler(&pBt->db-
fdd0: 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 3b 0a 7d >busyHandler);.}
fde0: 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 64 ../*.** Open a d
fdf0: 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
fe00: 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 .** zFilename i
fe10: 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
fe20: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
fe30: 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 If zFilename i
fe40: 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e 20 61 s NULL.** then a
fe50: 6e 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 n ephemeral data
fe60: 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 2e base is created.
fe70: 20 20 54 68 65 20 65 70 68 65 6d 65 72 61 6c 20 The ephemeral
fe80: 64 61 74 61 62 61 73 65 20 6d 69 67 68 74 0a 2a database might.*
fe90: 2a 20 62 65 20 65 78 63 6c 75 73 69 76 65 6c 79 * be exclusively
fea0: 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 6f 72 20 69 in memory, or i
feb0: 74 20 6d 69 67 68 74 20 75 73 65 20 61 20 64 69 t might use a di
fec0: 73 6b 2d 62 61 73 65 64 20 6d 65 6d 6f 72 79 20 sk-based memory
fed0: 63 61 63 68 65 2e 0a 2a 2a 20 45 69 74 68 65 72 cache..** Either
fee0: 20 77 61 79 2c 20 74 68 65 20 65 70 68 65 6d 65 way, the epheme
fef0: 72 61 6c 20 64 61 74 61 62 61 73 65 20 77 69 6c ral database wil
ff00: 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c l be automatical
ff10: 6c 79 20 64 65 6c 65 74 65 64 20 0a 2a 2a 20 77 ly deleted .** w
ff20: 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 hen sqlite3Btree
ff30: 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c 6c 65 Close() is calle
ff40: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c d..**.** If zFil
ff50: 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 ename is ":memor
ff60: 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d y:" then an in-m
ff70: 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 emory database i
ff80: 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 s created.** tha
ff90: 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c t is automatical
ffa0: 6c 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 ly destroyed whe
ffb0: 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a n it is closed..
ffc0: 2a 2a 0a 2a 2a 20 54 68 65 20 22 66 6c 61 67 73 **.** The "flags
ffd0: 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 " parameter is a
ffe0: 20 62 69 74 6d 61 73 6b 20 74 68 61 74 20 6d 69 bitmask that mi
fff0: 67 68 74 20 63 6f 6e 74 61 69 6e 20 62 69 74 73 ght contain bits
10000 20 6c 69 6b 65 0a 2a 2a 20 42 54 52 45 45 5f 4f like.** BTREE_O
10010 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 2f MIT_JOURNAL and/
10020 6f 72 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 2e or BTREE_MEMORY.
10030 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 .**.** If the da
10040 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 tabase is alread
10050 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 y opened in the
10060 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
10070 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 nnection.** and
10080 77 65 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 we are in shared
10090 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 cache mode, the
100a0 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 n the open will
100b0 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 fail with an.**
100c0 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
100d0 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e T error. We can
100e0 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 not allow two or
100f0 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a more BtShared.*
10100 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 * objects in the
10110 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
10120 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 onnection since
10130 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 doing so will le
10140 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d ad.** to problem
10150 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a s with locking..
10160 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 */.int sqlite3Bt
10170 72 65 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 reeOpen(. sqlit
10180 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
10190 20 20 20 2f 2a 20 56 46 53 20 74 6f 20 75 73 65 /* VFS to use
101a0 20 66 6f 72 20 74 68 69 73 20 62 2d 74 72 65 65 for this b-tree
101b0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
101c0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a *zFilename, /*
101d0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
101e0 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
101f0 20 42 54 72 65 65 20 64 61 74 61 62 61 73 65 20 BTree database
10200 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
10210 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
10220 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 Associated datab
10230 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
10240 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c Btree **ppBtree,
10250 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
10260 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20 er to new Btree
10270 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68 object written h
10280 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ere */. int fla
10290 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
102a0 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 20 /* Options */.
102b0 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 int vfsFlags
102c0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
102d0 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 s passed through
102e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e to sqlite3_vfs.
102f0 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 xOpen() */.){.
10300 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
10310 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
10320 2a 20 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 * Shared part of
10330 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 btree structure
10340 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 */. Btree *p;
10350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10360 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 /* Handle t
10370 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 o return */. sq
10380 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
10390 65 78 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 exOpen = 0; /*
103a0 50 72 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 Prevents a race
103b0 63 6f 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 condition. Ticke
103c0 74 20 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 t #3537 */. int
103d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
103e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
103f0 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 esult code from
10400 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f this function */
10410 0a 20 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 . u8 nReserve;
10420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10430 20 20 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 /* Byte of unu
10440 73 65 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 sed space on eac
10450 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 h page */. unsi
10460 67 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 gned char zDbHea
10470 64 65 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 der[100]; /* Da
10480 74 61 62 61 73 65 20 68 65 61 64 65 72 20 63 6f tabase header co
10490 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 54 ntent */.. /* T
104a0 72 75 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 61 rue if opening a
104b0 6e 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 65 6d n ephemeral, tem
104c0 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20 porary database
104d0 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 */. const int i
104e0 73 54 65 6d 70 44 62 20 3d 20 7a 46 69 6c 65 6e sTempDb = zFilen
104f0 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
10500 61 6d 65 5b 30 5d 3d 3d 30 3b 0a 0a 20 20 2f 2a ame[0]==0;.. /*
10510 20 53 65 74 20 74 68 65 20 76 61 72 69 61 62 6c Set the variabl
10520 65 20 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 e isMemdb to tru
10530 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f e for an in-memo
10540 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 ry database, or
10550 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 . ** false for
10560 61 20 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 a file-based dat
10570 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 abase.. */.#ifd
10580 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ef SQLITE_OMIT_M
10590 45 4d 4f 52 59 44 42 0a 20 20 63 6f 6e 73 74 20 EMORYDB. const
105a0 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b int isMemdb = 0;
105b0 0a 23 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 69 .#else. const i
105c0 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 28 7a 46 nt isMemdb = (zF
105d0 69 6c 65 6e 61 6d 65 20 26 26 20 73 74 72 63 6d ilename && strcm
105e0 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d p(zFilename, ":m
105f0 65 6d 6f 72 79 3a 22 29 3d 3d 30 29 0a 20 20 20 emory:")==0).
10600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10610 20 20 20 20 7c 7c 20 28 69 73 54 65 6d 70 44 62 || (isTempDb
10620 20 26 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49 && sqlite3TempI
10630 6e 4d 65 6d 6f 72 79 28 64 62 29 29 0a 20 20 20 nMemory(db)).
10640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10650 20 20 20 20 7c 7c 20 28 76 66 73 46 6c 61 67 73 || (vfsFlags
10660 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & SQLITE_OPEN_M
10670 45 4d 4f 52 59 29 21 3d 30 3b 0a 23 65 6e 64 69 EMORY)!=0;.#endi
10680 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 f.. assert( db!
10690 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
106a0 70 56 66 73 21 3d 30 20 29 3b 0a 20 20 61 73 73 pVfs!=0 );. ass
106b0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
106c0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
106d0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
106e0 28 66 6c 61 67 73 26 30 78 66 66 29 3d 3d 66 6c (flags&0xff)==fl
106f0 61 67 73 20 29 3b 20 20 20 2f 2a 20 66 6c 61 67 ags ); /* flag
10700 73 20 66 69 74 20 69 6e 20 38 20 62 69 74 73 20 s fit in 8 bits
10710 2a 2f 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20 61 20 */.. /* Only a
10720 42 54 52 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 BTREE_SINGLE dat
10730 61 62 61 73 65 20 63 61 6e 20 62 65 20 42 54 52 abase can be BTR
10740 45 45 5f 55 4e 4f 52 44 45 52 45 44 20 2a 2f 0a EE_UNORDERED */.
10750 20 20 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 assert( (flags
10760 20 26 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 & BTREE_UNORDER
10770 45 44 29 3d 3d 30 20 7c 7c 20 28 66 6c 61 67 73 ED)==0 || (flags
10780 20 26 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 & BTREE_SINGLE)
10790 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 42 !=0 );.. /* A B
107a0 54 52 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 TREE_SINGLE data
107b0 62 61 73 65 20 69 73 20 61 6c 77 61 79 73 20 61 base is always a
107c0 20 74 65 6d 70 6f 72 61 72 79 20 61 6e 64 2f 6f temporary and/o
107d0 72 20 65 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 20 r ephemeral */.
107e0 20 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 assert( (flags
107f0 26 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 3d & BTREE_SINGLE)=
10800 3d 30 20 7c 7c 20 69 73 54 65 6d 70 44 62 20 29 =0 || isTempDb )
10810 3b 0a 0a 20 20 69 66 28 20 69 73 4d 65 6d 64 62 ;.. if( isMemdb
10820 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d ){. flags |=
10830 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 3b 0a 20 BTREE_MEMORY;.
10840 20 7d 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61 }. if( (vfsFla
10850 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
10860 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20 _MAIN_DB)!=0 &&
10870 28 69 73 4d 65 6d 64 62 20 7c 7c 20 69 73 54 65 (isMemdb || isTe
10880 6d 70 44 62 29 20 29 7b 0a 20 20 20 20 76 66 73 mpDb) ){. vfs
10890 46 6c 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 Flags = (vfsFlag
108a0 73 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e s & ~SQLITE_OPEN
108b0 5f 4d 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 _MAIN_DB) | SQLI
108c0 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b TE_OPEN_TEMP_DB;
108d0 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 . }. p = sqlit
108e0 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a e3MallocZero(siz
108f0 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 eof(Btree));. i
10900 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 f( !p ){. ret
10910 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
10920 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 ;. }. p->inTra
10930 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b ns = TRANS_NONE;
10940 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 . p->db = db;.#
10950 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
10960 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
10970 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 p->lock.pBtree
10980 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e = p;. p->lock.
10990 69 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 iTable = 1;.#end
109a0 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 if..#if !defined
109b0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 (SQLITE_OMIT_SHA
109c0 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 RED_CACHE) && !d
109d0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
109e0 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a IT_DISKIO). /*.
109f0 20 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 ** If this Btr
10a00 65 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 ee is a candidat
10a10 65 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 e for shared cac
10a20 68 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 he, try to find
10a30 61 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 an. ** existing
10a40 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
10a50 20 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 that we can sha
10a60 72 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 re with. */. i
10a70 66 28 20 69 73 54 65 6d 70 44 62 3d 3d 30 20 26 f( isTempDb==0 &
10a80 26 20 28 69 73 4d 65 6d 64 62 3d 3d 30 20 7c 7c & (isMemdb==0 ||
10a90 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 (vfsFlags&SQLIT
10aa0 45 5f 4f 50 45 4e 5f 55 52 49 29 21 3d 30 29 20 E_OPEN_URI)!=0)
10ab0 29 7b 0a 20 20 20 20 69 66 28 20 76 66 73 46 6c ){. if( vfsFl
10ac0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
10ad0 4e 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 7b N_SHAREDCACHE ){
10ae0 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c . int nFull
10af0 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d Pathname = pVfs-
10b00 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 >mxPathname+1;.
10b10 20 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c char *zFull
10b20 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 Pathname = sqlit
10b30 65 33 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 e3Malloc(nFullPa
10b40 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 4d thname);. M
10b50 55 54 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 UTEX_LOGIC( sqli
10b60 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
10b70 53 68 61 72 65 64 3b 20 29 0a 20 20 20 20 20 20 Shared; ).
10b80 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b p->sharable = 1;
10b90 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c . if( !zFul
10ba0 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 lPathname ){.
10bb0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
10bc0 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 e(p);. re
10bd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
10be0 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 M;. }.
10bf0 20 69 66 28 20 69 73 4d 65 6d 64 62 20 29 7b 0a if( isMemdb ){.
10c00 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a memcpy(z
10c10 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 FullPathname, zF
10c20 69 6c 65 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 ilename, sqlite3
10c30 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 6e 61 Strlen30(zFilena
10c40 6d 65 29 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 me)+1);. }e
10c50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
10c60 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 = sqlite3OsFullP
10c70 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 athname(pVfs, zF
10c80 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 ilename,.
10c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ca0 20 20 20 20 20 20 20 20 20 20 20 20 6e 46 75 6c nFul
10cb0 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c lPathname, zFull
10cc0 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Pathname);.
10cd0 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
10ce0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
10cf0 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d ree(zFullPathnam
10d00 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 e);. sq
10d10 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
10d20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
10d30 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
10d40 20 20 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 }.#if SQLITE
10d50 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 _THREADSAFE.
10d60 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 mutexOpen = sq
10d70 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
10d80 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
10d90 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 TIC_OPEN);.
10da0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
10db0 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b nter(mutexOpen);
10dc0 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 . mutexShar
10dd0 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ed = sqlite3Mute
10de0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
10df0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
10e00 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 R);. sqlite
10e10 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
10e20 74 65 78 53 68 61 72 65 64 29 3b 0a 23 65 6e 64 texShared);.#end
10e30 69 66 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 if. for(pBt
10e40 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 =GLOBAL(BtShared
10e50 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 *,sqlite3SharedC
10e60 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 acheList); pBt;
10e70 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b pBt=pBt->pNext){
10e80 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
10e90 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pBt->nRef>0 );.
10ea0 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 if( 0==s
10eb0 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e trcmp(zFullPathn
10ec0 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 ame, sqlite3Page
10ed0 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 rFilename(pBt->p
10ee0 50 61 67 65 72 2c 20 30 29 29 0a 20 20 20 20 20 Pager, 0)).
10ef0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 && s
10f00 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 70 qlite3PagerVfs(p
10f10 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 Bt->pPager)==pVf
10f20 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 s ){. i
10f30 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 nt iDb;.
10f40 20 20 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e 44 for(iDb=db->nD
10f50 62 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44 62 b-1; iDb>=0; iDb
10f60 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 --){.
10f70 20 42 74 72 65 65 20 2a 70 45 78 69 73 74 69 6e Btree *pExistin
10f80 67 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d g = db->aDb[iDb]
10f90 2e 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 .pBt;.
10fa0 20 20 69 66 28 20 70 45 78 69 73 74 69 6e 67 20 if( pExisting
10fb0 26 26 20 70 45 78 69 73 74 69 6e 67 2d 3e 70 42 && pExisting->pB
10fc0 74 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 t==pBt ){.
10fd0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
10fe0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
10ff0 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 xShared);.
11000 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
11010 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
11020 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 xOpen);.
11030 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
11040 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 ee(zFullPathname
11050 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
11060 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
11070 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
11080 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
11090 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 NSTRAINT;.
110a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
110b0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d }. p-
110c0 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 >pBt = pBt;.
110d0 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b pBt->nRef+
110e0 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 +;. bre
110f0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
11100 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
11110 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
11120 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
11130 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
11140 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b (zFullPathname);
11150 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
11160 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 LITE_DEBUG. e
11170 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e lse{. /* In
11180 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 debug mode, we
11190 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 mark all persist
111a0 65 6e 74 20 64 61 74 61 62 61 73 65 73 20 61 73 ent databases as
111b0 20 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 sharable.
111c0 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 ** even when the
111d0 79 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 y are not. This
111e0 20 65 78 65 72 63 69 73 65 73 20 74 68 65 20 6c exercises the l
111f0 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a ocking code and.
11200 20 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d ** gives m
11210 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 ore opportunity
11220 66 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 for asserts(sqli
11230 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
11240 29 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 ). ** state
11250 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f ments to find lo
11260 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a cking problems..
11270 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 */. p
11280 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a ->sharable = 1;.
11290 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
112a0 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 .#endif. if( pB
112b0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 t==0 ){. /*.
112c0 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 ** The follow
112d0 69 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 ing asserts make
112e0 20 73 75 72 65 20 74 68 61 74 20 73 74 72 75 63 sure that struc
112f0 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 tures used by th
11300 65 20 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 e btree are.
11310 2a 2a 20 74 68 65 20 72 69 67 68 74 20 73 69 7a ** the right siz
11320 65 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 e. This is to g
11330 75 61 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a uard against siz
11340 65 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 e changes that r
11350 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 esult. ** whe
11360 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 n compiling on a
11370 20 64 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 different archi
11380 74 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a tecture.. */.
11390 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
113a0 6f 66 28 69 36 34 29 3d 3d 38 20 29 3b 0a 20 20 of(i64)==8 );.
113b0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
113c0 28 75 36 34 29 3d 3d 38 20 29 3b 0a 20 20 20 20 (u64)==8 );.
113d0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
113e0 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 32)==4 );. as
113f0 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36 sert( sizeof(u16
11400 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65 )==2 );. asse
11410 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 rt( sizeof(Pgno)
11420 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 ==4 );. . pB
11430 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f t = sqlite3Mallo
11440 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 cZero( sizeof(*p
11450 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 Bt) );. if( p
11460 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 Bt==0 ){. r
11470 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
11480 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 ;. goto btr
11490 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ee_open_out;.
114a0 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
114b0 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 te3PagerOpen(pVf
114c0 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c s, &pBt->pPager,
114d0 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 zFilename,.
114e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
114f0 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45 EXTRA_SIZE
11500 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 , flags, vfsFlag
11510 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a s, pageReinit);.
11520 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
11530 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
11540 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 4d 6d qlite3PagerSetMm
11550 61 70 4c 69 6d 69 74 28 70 42 74 2d 3e 70 50 61 apLimit(pBt->pPa
11560 67 65 72 2c 20 64 62 2d 3e 73 7a 4d 6d 61 70 29 ger, db->szMmap)
11570 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
11580 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c ite3PagerReadFil
11590 65 68 65 61 64 65 72 28 70 42 74 2d 3e 70 50 61 eheader(pBt->pPa
115a0 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 ger,sizeof(zDbHe
115b0 61 64 65 72 29 2c 7a 44 62 48 65 61 64 65 72 29 ader),zDbHeader)
115c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
115d0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
115e0 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 . goto btre
115f0 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 e_open_out;.
11600 7d 0a 20 20 20 20 70 42 74 2d 3e 6f 70 65 6e 46 }. pBt->openF
11610 6c 61 67 73 20 3d 20 28 75 38 29 66 6c 61 67 73 lags = (u8)flags
11620 3b 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d 20 ;. pBt->db =
11630 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 db;. sqlite3P
11640 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
11650 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 er(pBt->pPager,
11660 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 btreeInvokeBusyH
11670 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b 0a 20 20 andler, pBt);.
11680 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a p->pBt = pBt;.
11690 20 20 0a 20 20 20 20 70 42 74 2d 3e 70 43 75 72 . pBt->pCur
116a0 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 sor = 0;. pBt
116b0 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
116c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 if( sqlite3Pag
116d0 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 erIsreadonly(pBt
116e0 2d 3e 70 50 61 67 65 72 29 20 29 20 70 42 74 2d ->pPager) ) pBt-
116f0 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
11700 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0a 23 69 66 64 _READ_ONLY;.#ifd
11710 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 ef SQLITE_SECURE
11720 5f 44 45 4c 45 54 45 0a 20 20 20 20 70 42 74 2d _DELETE. pBt-
11730 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
11740 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 3b 0a _SECURE_DELETE;.
11750 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 45 56 #endif. /* EV
11760 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 35 31 38 IDENCE-OF: R-518
11770 37 33 2d 33 39 36 31 38 20 54 68 65 20 70 61 67 73-39618 The pag
11780 65 20 73 69 7a 65 20 66 6f 72 20 61 20 64 61 74 e size for a dat
11790 61 62 61 73 65 20 66 69 6c 65 20 69 73 0a 20 20 abase file is.
117a0 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 ** determined
117b0 62 79 20 74 68 65 20 32 2d 62 79 74 65 20 69 6e by the 2-byte in
117c0 74 65 67 65 72 20 6c 6f 63 61 74 65 64 20 61 74 teger located at
117d0 20 61 6e 20 6f 66 66 73 65 74 20 6f 66 20 31 36 an offset of 16
117e0 20 62 79 74 65 73 20 66 72 6f 6d 0a 20 20 20 20 bytes from.
117f0 2a 2a 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 ** the beginning
11800 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
11810 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 70 42 file. */. pB
11820 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 7a t->pageSize = (z
11830 44 62 48 65 61 64 65 72 5b 31 36 5d 3c 3c 38 29 DbHeader[16]<<8)
11840 20 7c 20 28 7a 44 62 48 65 61 64 65 72 5b 31 37 | (zDbHeader[17
11850 5d 3c 3c 31 36 29 3b 0a 20 20 20 20 69 66 28 20 ]<<16);. if(
11860 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 pBt->pageSize<51
11870 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 2 || pBt->pageSi
11880 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ze>SQLITE_MAX_PA
11890 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 GE_SIZE.
118a0 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 || ((pBt->pageS
118b0 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 ize-1)&pBt->page
118c0 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Size)!=0 ){.
118d0 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
118e0 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c = 0;.#ifndef SQL
118f0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
11900 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 UUM. /* If
11910 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 the magic name "
11920 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 :memory:" will c
11930 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f reate an in-memo
11940 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 ry database, the
11950 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65 n. ** leave
11960 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20 the autoVacuum
11970 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f mode at 0 (do no
11980 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20 t auto-vacuum),
11990 65 76 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a even if. **
119a0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
119b0 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72 AUTOVACUUM is tr
119c0 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 ue. On the other
119d0 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20 hand, if.
119e0 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ** SQLITE_OMIT_M
119f0 45 4d 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e EMORYDB has been
11a00 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22 defined, then "
11a10 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73 :memory:" is jus
11a20 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 t a. ** reg
11a30 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 ular file-name.
11a40 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
11a50 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70 auto-vacuum app
11a60 6c 69 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d lies as per norm
11a70 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 al.. */.
11a80 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 if( zFilename
11a90 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a && !isMemdb ){.
11aa0 20 20 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 pBt->aut
11ab0 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 oVacuum = (SQLIT
11ac0 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 E_DEFAULT_AUTOVA
11ad0 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 CUUM ? 1 : 0);.
11ae0 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 pBt->incr
11af0 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 Vacuum = (SQLITE
11b00 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
11b10 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b UUM==2 ? 1 : 0);
11b20 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
11b30 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d nReserve =
11b40 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
11b50 20 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 /* EVIDENCE
11b60 2d 4f 46 3a 20 52 2d 33 37 34 39 37 2d 34 32 34 -OF: R-37497-424
11b70 31 32 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 12 The size of t
11b80 68 65 20 72 65 73 65 72 76 65 64 20 72 65 67 69 he reserved regi
11b90 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 on is. ** d
11ba0 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
11bb0 20 6f 6e 65 2d 62 79 74 65 20 75 6e 73 69 67 6e one-byte unsign
11bc0 65 64 20 69 6e 74 65 67 65 72 20 66 6f 75 6e 64 ed integer found
11bd0 20 61 74 20 61 6e 20 6f 66 66 73 65 74 20 6f 66 at an offset of
11be0 20 32 30 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 20. ** int
11bf0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
11c00 69 6c 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 ile header. */.
11c10 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 nReserve =
11c20 7a 44 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20 zDbHeader[20];.
11c30 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 pBt->btsFla
11c40 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 49 gs |= BTS_PAGESI
11c50 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e 64 65 ZE_FIXED;.#ifnde
11c60 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
11c70 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 70 TOVACUUM. p
11c80 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d Bt->autoVacuum =
11c90 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 (get4byte(&zDbH
11ca0 65 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 eader[36 + 4*4])
11cb0 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42 74 ?1:0);. pBt
11cc0 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
11cd0 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 get4byte(&zDbHea
11ce0 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 der[36 + 7*4])?1
11cf0 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 :0);.#endif.
11d00 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
11d10 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
11d20 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
11d30 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20 &pBt->pageSize,
11d40 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20 69 nReserve);. i
11d50 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 74 72 f( rc ) goto btr
11d60 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ee_open_out;.
11d70 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
11d80 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 = pBt->pageSize
11d90 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 - nReserve;.
11da0 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 70 assert( (pBt->p
11db0 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 ageSize & 7)==0
11dc0 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c ); /* 8-byte al
11dd0 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 53 ignment of pageS
11de0 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20 21 ize */. .#if !
11df0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
11e00 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
11e10 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
11e20 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f LITE_OMIT_DISKIO
11e30 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 ). /* Add the
11e40 20 6e 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 new BtShared ob
11e50 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b ject to the link
11e60 65 64 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 ed list sharable
11e70 20 42 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 BtShareds..
11e80 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 */. if( p->sh
11e90 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 arable ){.
11ea0 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c MUTEX_LOGIC( sql
11eb0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
11ec0 78 53 68 61 72 65 64 3b 20 29 0a 20 20 20 20 20 xShared; ).
11ed0 20 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a pBt->nRef = 1;.
11ee0 20 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 MUTEX_LOGI
11ef0 43 28 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d C( mutexShared =
11f00 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
11f10 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
11f20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 29 STATIC_MASTER);)
11f30 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
11f40 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
11f50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
11f60 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
11f70 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6d {. pBt->m
11f80 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
11f90 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
11fa0 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 MUTEX_FAST);.
11fb0 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 if( pBt->mu
11fc0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 tex==0 ){.
11fd0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
11fe0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 NOMEM;.
11ff0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
12000 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 d = 0;.
12010 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e goto btree_open
12020 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a _out;. }.
12030 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
12040 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
12050 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a r(mutexShared);.
12060 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 pBt->pNext
12070 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 = GLOBAL(BtShar
12080 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 ed*,sqlite3Share
12090 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 dCacheList);.
120a0 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 GLOBAL(BtShar
120b0 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 ed*,sqlite3Share
120c0 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 dCacheList) = pB
120d0 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 t;. sqlite3
120e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
120f0 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 7d exShared);. }
12100 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 .#endif. }..#if
12110 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
12120 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
12130 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 HE) && !defined(
12140 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
12150 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 IO). /* If the
12160 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20 61 new Btree uses a
12170 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 61 sharable pBtSha
12180 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 red, then link t
12190 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 he new. ** Btre
121a0 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 e into the list
121b0 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 of all sharable
121c0 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 73 Btrees for the s
121d0 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a ame connection..
121e0 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 73 ** The list is
121f0 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69 kept in ascendi
12200 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 20 ng order by pBt
12210 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 address.. */.
12220 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
12230 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
12240 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 Btree *pSib;.
12250 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 for(i=0; i<db
12260 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
12270 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 64 if( (pSib = d
12280 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d b->aDb[i].pBt)!=
12290 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 61 0 && pSib->shara
122a0 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 77 ble ){. w
122b0 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 65 hile( pSib->pPre
122c0 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 62 v ){ pSib = pSib
122d0 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 ->pPrev; }.
122e0 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 if( p->pBt<pS
122f0 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 ib->pBt ){.
12300 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 p->pNext =
12310 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pSib;.
12320 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 p->pPrev = 0;.
12330 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 50 pSib->pP
12340 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 rev = p;.
12350 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
12360 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 while( pSib->p
12370 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 4e Next && pSib->pN
12380 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 ext->pBt<p->pBt
12390 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
123a0 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 Sib = pSib->pNex
123b0 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 t;. }.
123c0 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 p->pNex
123d0 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b t = pSib->pNext;
123e0 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 . p->pP
123f0 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 rev = pSib;.
12400 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 if( p->pNe
12410 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 xt ){.
12420 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 p->pNext->pPre
12430 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 v = p;.
12440 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 }. pSi
12450 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 b->pNext = p;.
12460 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
12470 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
12480 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
12490 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b 0a *ppBtree = p;.
124a0 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a .btree_open_out:
124b0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
124c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
124d0 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 pBt && pBt->pPag
124e0 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 er ){. sqli
124f0 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 te3PagerClose(pB
12500 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 t->pPager);.
12510 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
12520 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c ee(pBt);. sql
12530 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
12540 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b 0a *ppBtree = 0;.
12550 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
12560 49 66 20 74 68 65 20 42 2d 54 72 65 65 20 77 61 If the B-Tree wa
12570 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f s successfully o
12580 70 65 6e 65 64 2c 20 73 65 74 20 74 68 65 20 70 pened, set the p
12590 61 67 65 72 2d 63 61 63 68 65 20 73 69 7a 65 20 ager-cache size
125a0 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 to the. ** de
125b0 66 61 75 6c 74 20 76 61 6c 75 65 2e 20 45 78 63 fault value. Exc
125c0 65 70 74 2c 20 77 68 65 6e 20 6f 70 65 6e 69 6e ept, when openin
125d0 67 20 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e 67 g on an existing
125e0 20 73 68 61 72 65 64 20 70 61 67 65 72 2d 63 61 shared pager-ca
125f0 63 68 65 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e che,. ** do n
12600 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 ot change the pa
12610 67 65 72 2d 63 61 63 68 65 20 73 69 7a 65 2e 0a ger-cache size..
12620 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 */. if( s
12630 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d qlite3BtreeSchem
12640 61 28 70 2c 20 30 2c 20 30 29 3d 3d 30 20 29 7b a(p, 0, 0)==0 ){
12650 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 . sqlite3Pa
12660 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 gerSetCachesize(
12670 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 p->pBt->pPager,
12680 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 SQLITE_DEFAULT_C
12690 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 ACHE_SIZE);.
126a0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 }. }. if( mute
126b0 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 xOpen ){. ass
126c0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
126d0 65 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 ex_held(mutexOpe
126e0 6e 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 n) );. sqlite
126f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
12700 74 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 texOpen);. }.
12710 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12720 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 .** Decrement th
12730 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 e BtShared.nRef
12740 63 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 counter. When i
12750 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a t reaches zero,.
12760 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 ** remove the Bt
12770 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
12780 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e from the sharin
12790 67 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a g list. Return.
127a0 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 ** true if the B
127b0 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 tShared.nRef cou
127c0 6e 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 nter reaches zer
127d0 6f 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 o and return.**
127e0 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 false if it is s
127f0 74 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a till positive..*
12800 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d /.static int rem
12810 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 oveFromSharingLi
12820 73 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 st(BtShared *pBt
12830 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
12840 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
12850 43 48 45 0a 20 20 4d 55 54 45 58 5f 4c 4f 47 49 CHE. MUTEX_LOGI
12860 43 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 C( sqlite3_mutex
12870 20 2a 70 4d 61 73 74 65 72 3b 20 29 0a 20 20 42 *pMaster; ). B
12880 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a tShared *pList;.
12890 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20 int removed =
128a0 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 0;.. assert( sq
128b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
128c0 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
128d0 29 3b 0a 20 20 4d 55 54 45 58 5f 4c 4f 47 49 43 );. MUTEX_LOGIC
128e0 28 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 ( pMaster = sqli
128f0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
12900 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
12910 43 5f 4d 41 53 54 45 52 29 3b 20 29 0a 20 20 73 C_MASTER); ). s
12920 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
12930 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 70 er(pMaster);. p
12940 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 Bt->nRef--;. if
12950 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 29 ( pBt->nRef<=0 )
12960 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41 4c {. if( GLOBAL
12970 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
12980 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
12990 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 t)==pBt ){.
129a0 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 GLOBAL(BtShared
129b0 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 *,sqlite3SharedC
129c0 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 2d acheList) = pBt-
129d0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 >pNext;. }els
129e0 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 3d e{. pList =
129f0 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 GLOBAL(BtShared
12a00 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 *,sqlite3SharedC
12a10 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 acheList);.
12a20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 while( ALWAYS(p
12a30 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d 3e List) && pList->
12a40 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20 20 pNext!=pBt ){.
12a50 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 73 pList=pLis
12a60 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 t->pNext;.
12a70 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 }. if( ALWA
12a80 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 YS(pList) ){.
12a90 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 78 pList->pNex
12aa0 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a t = pBt->pNext;.
12ab0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
12ac0 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 if( SQLITE_THR
12ad0 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20 20 EADSAFE ){.
12ae0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 sqlite3_mutex_f
12af0 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 3b ree(pBt->mutex);
12b00 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f 76 . }. remov
12b10 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 ed = 1;. }. sq
12b20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
12b30 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 e(pMaster);. re
12b40 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 65 turn removed;.#e
12b50 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a lse. return 1;.
12b60 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
12b70 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e 70 Make sure pBt->p
12b80 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 20 TmpSpace points
12b90 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e to an allocation
12ba0 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c 5f of .** MX_CELL_
12bb0 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 20 SIZE(pBt) bytes
12bc0 77 69 74 68 20 61 20 34 2d 62 79 74 65 20 70 72 with a 4-byte pr
12bd0 65 66 69 78 20 66 6f 72 20 61 20 6c 65 66 74 2d efix for a left-
12be0 63 68 69 6c 64 0a 2a 2a 20 70 6f 69 6e 74 65 72 child.** pointer
12bf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
12c00 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 allocateTempSpa
12c10 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ce(BtShared *pBt
12c20 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 ){. if( !pBt->p
12c30 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 TmpSpace ){.
12c40 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d pBt->pTmpSpace =
12c50 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
12c60 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a oc( pBt->pageSiz
12c70 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 e );.. /* One
12c80 20 6f 66 20 74 68 65 20 75 73 65 73 20 6f 66 20 of the uses of
12c90 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 69 pBt->pTmpSpace i
12ca0 73 20 74 6f 20 66 6f 72 6d 61 74 20 63 65 6c 6c s to format cell
12cb0 73 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 s before. **
12cc0 69 6e 73 65 72 74 69 6e 67 20 74 68 65 6d 20 69 inserting them i
12cd0 6e 74 6f 20 61 20 6c 65 61 66 20 70 61 67 65 20 nto a leaf page
12ce0 28 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 49 6e (function fillIn
12cf0 43 65 6c 6c 28 29 29 2e 20 49 66 0a 20 20 20 20 Cell()). If.
12d00 2a 2a 20 61 20 63 65 6c 6c 20 69 73 20 6c 65 73 ** a cell is les
12d10 73 20 74 68 61 6e 20 34 20 62 79 74 65 73 20 69 s than 4 bytes i
12d20 6e 20 73 69 7a 65 2c 20 69 74 20 69 73 20 72 6f n size, it is ro
12d30 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20 62 79 unded up to 4 by
12d40 74 65 73 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 tes. ** by th
12d50 65 20 76 61 72 69 6f 75 73 20 72 6f 75 74 69 6e e various routin
12d60 65 73 20 74 68 61 74 20 6d 61 6e 69 70 75 6c 61 es that manipula
12d70 74 65 20 62 69 6e 61 72 79 20 63 65 6c 6c 73 2e te binary cells.
12d80 20 57 68 69 63 68 0a 20 20 20 20 2a 2a 20 63 61 Which. ** ca
12d90 6e 20 6d 65 61 6e 20 74 68 61 74 20 66 69 6c 6c n mean that fill
12da0 49 6e 43 65 6c 6c 28 29 20 6f 6e 6c 79 20 69 6e InCell() only in
12db0 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 66 69 itializes the fi
12dc0 72 73 74 20 32 20 6f 72 20 33 0a 20 20 20 20 2a rst 2 or 3. *
12dd0 2a 20 62 79 74 65 73 20 6f 66 20 70 54 6d 70 53 * bytes of pTmpS
12de0 70 61 63 65 2c 20 62 75 74 20 74 68 61 74 20 74 pace, but that t
12df0 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 he first 4 bytes
12e00 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d are copied from
12e10 0a 20 20 20 20 2a 2a 20 69 74 20 69 6e 74 6f 20 . ** it into
12e20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e a database page.
12e30 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 63 74 This is not act
12e40 75 61 6c 6c 79 20 61 20 70 72 6f 62 6c 65 6d 2c ually a problem,
12e50 20 62 75 74 20 69 74 0a 20 20 20 20 2a 2a 20 64 but it. ** d
12e60 6f 65 73 20 63 61 75 73 65 20 61 20 76 61 6c 67 oes cause a valg
12e70 72 69 6e 64 20 65 72 72 6f 72 20 77 68 65 6e 20 rind error when
12e80 74 68 65 20 31 20 6f 72 20 32 20 62 79 74 65 73 the 1 or 2 bytes
12e90 20 6f 66 20 75 6e 69 74 69 61 6c 69 7a 65 64 20 of unitialized
12ea0 0a 20 20 20 20 2a 2a 20 64 61 74 61 20 69 73 20 . ** data is
12eb0 70 61 73 73 65 64 20 74 6f 20 73 79 73 74 65 6d passed to system
12ec0 20 63 61 6c 6c 20 77 72 69 74 65 28 29 2e 20 53 call write(). S
12ed0 6f 20 74 6f 20 61 76 6f 69 64 20 74 68 69 73 20 o to avoid this
12ee0 65 72 72 6f 72 2c 0a 20 20 20 20 2a 2a 20 7a 65 error,. ** ze
12ef0 72 6f 20 74 68 65 20 66 69 72 73 74 20 34 20 62 ro the first 4 b
12f00 79 74 65 73 20 6f 66 20 74 65 6d 70 20 73 70 61 ytes of temp spa
12f10 63 65 20 68 65 72 65 2e 0a 20 20 20 20 2a 2a 0a ce here.. **.
12f20 20 20 20 20 2a 2a 20 41 6c 73 6f 3a 20 20 50 72 ** Also: Pr
12f30 6f 76 69 64 65 20 66 6f 75 72 20 62 79 74 65 73 ovide four bytes
12f40 20 6f 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 of initialized
12f50 73 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 space before the
12f60 0a 20 20 20 20 2a 2a 20 62 65 67 69 6e 6e 69 6e . ** beginnin
12f70 67 20 6f 66 20 70 54 6d 70 53 70 61 63 65 20 61 g of pTmpSpace a
12f80 73 20 61 6e 20 61 72 65 61 20 61 76 61 69 6c 61 s an area availa
12f90 62 6c 65 20 74 6f 20 70 72 65 70 65 6e 64 20 74 ble to prepend t
12fa0 68 65 0a 20 20 20 20 2a 2a 20 6c 65 66 74 2d 63 he. ** left-c
12fb0 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 hild pointer to
12fc0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
12fd0 20 61 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a a cell.. */.
12fe0 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 54 6d if( pBt->pTm
12ff0 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 pSpace ){.
13000 6d 65 6d 73 65 74 28 70 42 74 2d 3e 70 54 6d 70 memset(pBt->pTmp
13010 53 70 61 63 65 2c 20 30 2c 20 38 29 3b 0a 20 20 Space, 0, 8);.
13020 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 pBt->pTmpSpa
13030 63 65 20 2b 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 ce += 4;. }.
13040 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 }.}../*.** Free
13050 20 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 the pBt->pTmpSp
13060 61 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a ace allocation.*
13070 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 /.static void fr
13080 65 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 eeTempSpace(BtSh
13090 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 ared *pBt){. if
130a0 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 ( pBt->pTmpSpace
130b0 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d ){. pBt->pTm
130c0 70 53 70 61 63 65 20 2d 3d 20 34 3b 0a 20 20 20 pSpace -= 4;.
130d0 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 sqlite3PageFree
130e0 28 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 29 (pBt->pTmpSpace)
130f0 3b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 ;. pBt->pTmpS
13100 70 61 63 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a pace = 0;. }.}.
13110 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 ./*.** Close an
13120 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e open database an
13130 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c d invalidate all
13140 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 69 6e 74 cursors..*/.int
13150 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
13160 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 se(Btree *p){.
13170 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
13180 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73 p->pBt;. BtCurs
13190 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 or *pCur;.. /*
131a0 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 Close all cursor
131b0 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 69 s opened via thi
131c0 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 s handle. */.
131d0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
131e0 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
131f0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
13200 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
13210 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74 p);. pCur = pBt
13220 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 ->pCursor;. whi
13230 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 le( pCur ){.
13240 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d BtCursor *pTmp =
13250 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20 pCur;. pCur
13260 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 = pCur->pNext;.
13270 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 if( pTmp->pBt
13280 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
13290 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
132a0 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 eCursor(pTmp);.
132b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
132c0 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69 ollback any acti
132d0 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ve transaction a
132e0 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64 nd free the hand
132f0 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 le structure..
13300 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 ** The call to s
13310 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
13320 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20 ack() drops any
13330 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 table-locks held
13340 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 by. ** this ha
13350 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c ndle.. */. sql
13360 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 ite3BtreeRollbac
13370 6b 28 70 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 k(p, SQLITE_OK,
13380 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 0);. sqlite3Btr
13390 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f eeLeave(p);.. /
133a0 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 73 * If there are s
133b0 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 till other outst
133c0 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 anding reference
133d0 73 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d s to the shared-
133e0 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 btree. ** struc
133f0 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 ture, return now
13400 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 . The remainder
13410 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 of this procedur
13420 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 e cleans . ** u
13430 70 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 p the shared-btr
13440 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ee.. */. asser
13450 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b t( p->wantToLock
13460 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 ==0 && p->locked
13470 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 2d ==0 );. if( !p-
13480 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d >sharable || rem
13490 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 oveFromSharingLi
134a0 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f st(pBt) ){. /
134b0 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f 20 * The pBt is no
134c0 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 68 longer on the sh
134d0 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 aring list, so w
134e0 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20 20 e can access.
134f0 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20 68 ** it without h
13500 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 aving to hold th
13510 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a e mutex.. **.
13520 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 ** Clean out
13530 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 and delete the
13540 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e BtShared object.
13550 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
13560 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 6f rt( !pBt->pCurso
13570 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 r );. sqlite3
13580 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e PagerClose(pBt->
13590 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 pPager);. if(
135a0 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d pBt->xFreeSchem
135b0 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d a && pBt->pSchem
135c0 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e a ){. pBt->
135d0 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d xFreeSchema(pBt-
135e0 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d >pSchema);. }
135f0 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
13600 65 65 28 30 2c 20 70 42 74 2d 3e 70 53 63 68 65 ee(0, pBt->pSche
13610 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d ma);. freeTem
13620 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
13630 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 sqlite3_free(pB
13640 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 t);. }..#ifndef
13650 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
13660 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 RED_CACHE. asse
13670 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
13680 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 k==0 );. assert
13690 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 ( p->locked==0 )
136a0 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 ;. if( p->pPrev
136b0 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 ) p->pPrev->pNe
136c0 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 xt = p->pNext;.
136d0 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 if( p->pNext )
136e0 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 p->pNext->pPrev
136f0 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 = p->pPrev;.#end
13700 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 if.. sqlite3_fr
13710 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ee(p);. return
13720 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
13730 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c .** Change the l
13740 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 imit on the numb
13750 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f er of pages allo
13760 77 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 wed in the cache
13770 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ..**.** The maxi
13780 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 mum number of ca
13790 63 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74 che pages is set
137a0 20 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 to the absolute
137b0 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 .** value of mxP
137c0 61 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 age. If mxPage
137d0 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
137e0 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f pager will.** o
137f0 70 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e perate asynchron
13800 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 ously - it will
13810 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 not stop to do f
13820 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e sync()s.** to in
13830 73 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69 sure data is wri
13840 74 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b tten to the disk
13850 20 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a surface before.
13860 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 ** continuing.
13870 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 Transactions sti
13880 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 ll work if synch
13890 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a ronous is off,.*
138a0 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 * and the databa
138b0 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 se cannot be cor
138c0 72 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70 rupted if this p
138d0 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 rogram.** crashe
138e0 73 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f s. But if the o
138f0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
13900 63 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 crashes or there
13910 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 is.** an abrupt
13920 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 power failure w
13930 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 hen synchronous
13940 69 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 is off, the data
13950 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 base.** could be
13960 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
13970 6e 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 nsistent and unr
13980 65 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 ecoverable state
13990 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 ..** Synchronous
139a0 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c is on by defaul
139b0 74 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f t so database co
139c0 72 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a rruption is not.
139d0 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f ** normally a wo
139e0 72 72 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 rry..*/.int sqli
139f0 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 te3BtreeSetCache
13a00 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 Size(Btree *p, i
13a10 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 nt mxPage){. Bt
13a20 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
13a30 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 >pBt;. assert(
13a40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
13a50 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
13a60 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
13a70 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 eeEnter(p);. sq
13a80 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 lite3PagerSetCac
13a90 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 hesize(pBt->pPag
13aa0 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 er, mxPage);. s
13ab0 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
13ac0 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
13ad0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 LITE_OK;.}..#if
13ae0 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f SQLITE_MAX_MMAP_
13af0 53 49 5a 45 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 61 SIZE>0./*.** Cha
13b00 6e 67 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e nge the limit on
13b10 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 the amount of t
13b20 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13b30 20 74 68 61 74 20 6d 61 79 20 62 65 0a 2a 2a 20 that may be.**
13b40 6d 65 6d 6f 72 79 20 6d 61 70 70 65 64 2e 0a 2a memory mapped..*
13b50 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
13b60 65 65 53 65 74 4d 6d 61 70 4c 69 6d 69 74 28 42 eeSetMmapLimit(B
13b70 74 72 65 65 20 2a 70 2c 20 73 71 6c 69 74 65 33 tree *p, sqlite3
13b80 5f 69 6e 74 36 34 20 73 7a 4d 6d 61 70 29 7b 0a _int64 szMmap){.
13b90 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
13ba0 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 = p->pBt;. asse
13bb0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
13bc0 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
13bd0 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
13be0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
13bf0 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
13c00 74 4d 6d 61 70 4c 69 6d 69 74 28 70 42 74 2d 3e tMmapLimit(pBt->
13c10 70 50 61 67 65 72 2c 20 73 7a 4d 6d 61 70 29 3b pPager, szMmap);
13c20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
13c30 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
13c40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
13c50 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
13c60 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 20 MAX_MMAP_SIZE>0
13c70 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 */../*.** Change
13c80 20 74 68 65 20 77 61 79 20 64 61 74 61 20 69 73 the way data is
13c90 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 synced to disk
13ca0 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 in order to incr
13cb0 65 61 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 ease or decrease
13cc0 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 .** how well the
13cd0 20 64 61 74 61 62 61 73 65 20 72 65 73 69 73 74 database resist
13ce0 73 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 s damage due to
13cf0 4f 53 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 OS crashes and p
13d00 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 ower.** failures
13d10 2e 20 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 . Level 1 is th
13d20 65 20 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 e same as asynch
13d30 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 ronous (no syncs
13d40 28 29 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 () occur and.**
13d50 74 68 65 72 65 20 69 73 20 61 20 68 69 67 68 20 there is a high
13d60 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 probability of d
13d70 61 6d 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 amage) Level 2
13d80 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 is the default.
13d90 20 54 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 There.** is a v
13da0 65 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d ery low but non-
13db0 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 zero probability
13dc0 20 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 of damage. Lev
13dd0 65 6c 20 33 20 72 65 64 75 63 65 73 20 74 68 65 el 3 reduces the
13de0 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 .** probability
13df0 6f 66 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 of damage to nea
13e00 72 20 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 r zero but with
13e10 61 20 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 a write performa
13e20 6e 63 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a nce reduction..*
13e30 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
13e40 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 _OMIT_PAGER_PRAG
13e50 4d 41 53 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 MAS.int sqlite3B
13e60 74 72 65 65 53 65 74 50 61 67 65 72 46 6c 61 67 treeSetPagerFlag
13e70 73 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 s(. Btree *p,
13e80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
13e90 68 65 20 62 74 72 65 65 20 74 6f 20 73 65 74 20 he btree to set
13ea0 74 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c the safety level
13eb0 20 6f 6e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 on */. unsigne
13ec0 64 20 70 67 46 6c 61 67 73 20 20 20 20 20 20 20 d pgFlags
13ed0 2f 2a 20 56 61 72 69 6f 75 73 20 50 41 47 45 52 /* Various PAGER
13ee0 5f 2a 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 _* flags */.){.
13ef0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
13f00 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
13f10 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
13f20 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
13f30 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
13f40 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
13f50 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
13f60 46 6c 61 67 73 28 70 42 74 2d 3e 70 50 61 67 65 Flags(pBt->pPage
13f70 72 2c 20 70 67 46 6c 61 67 73 29 3b 0a 20 20 73 r, pgFlags);. s
13f80 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
13f90 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
13fa0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
13fb0 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
13fc0 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 TRUE if the give
13fd0 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74 n btree is set t
13fe0 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 o safety level 1
13ff0 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 . In other.** w
14000 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 ords, return TRU
14010 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f E if no sync() o
14020 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 ccurs on the dis
14030 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 69 6e 74 20 k files..*/.int
14040 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 sqlite3BtreeSync
14050 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a Disabled(Btree *
14060 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a p){. BtShared *
14070 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
14080 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
14090 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
140a0 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
140b0 78 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 x) ); . sqlite
140c0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
140d0 20 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 assert( pBt &&
140e0 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a pBt->pPager );.
140f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
14100 67 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 gerNosync(pBt->p
14110 50 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 Pager);. sqlite
14120 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
14130 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
14140 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
14150 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 73 default pages s
14160 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 ize and the numb
14170 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 er of reserved b
14180 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a ytes per page..*
14190 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 70 61 67 * Or, if the pag
141a0 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 e size has alrea
141b0 64 79 20 62 65 65 6e 20 66 69 78 65 64 2c 20 72 dy been fixed, r
141c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
141d0 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75 DONLY .** withou
141e0 74 20 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 68 t changing anyth
141f0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ing..**.** The p
14200 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 age size must be
14210 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 a power of 2 be
14220 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 tween 512 and 65
14230 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61 67 536. If the pag
14240 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c 69 e.** size suppli
14250 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74 ed does not meet
14260 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 this constraint
14270 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 73 then the page s
14280 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 ize is not.** ch
14290 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 anged..**.** Pag
142a0 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e 73 e sizes are cons
142b0 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61 20 trained to be a
142c0 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f 20 power of two so
142d0 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e 0a that the region.
142e0 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ** of the databa
142f0 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 se file used for
14300 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e locking (beginn
14310 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f 42 ing at PENDING_B
14320 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73 YTE,.** the firs
14330 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 t byte past the
14340 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 78 1GB boundary, 0x
14350 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73 20 40000000) needs
14360 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 to occur.** at t
14370 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
14380 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 a page..**.** If
14390 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65 parameter nRese
143a0 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e rve is less than
143b0 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
143c0 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 number of reserv
143d0 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20 ed.** bytes per
143e0 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 page is left unc
143f0 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 hanged..**.** If
14400 20 74 68 65 20 69 46 69 78 21 3d 30 20 74 68 65 the iFix!=0 the
14410 6e 20 74 68 65 20 42 54 53 5f 50 41 47 45 53 49 n the BTS_PAGESI
14420 5a 45 5f 46 49 58 45 44 20 66 6c 61 67 20 69 73 ZE_FIXED flag is
14430 20 73 65 74 20 73 6f 20 74 68 61 74 20 74 68 65 set so that the
14440 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e page size.** an
14450 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 d autovacuum mod
14460 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 e can no longer
14470 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 69 be changed..*/.i
14480 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
14490 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
144a0 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69 7a *p, int pageSiz
144b0 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c e, int nReserve,
144c0 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 6e int iFix){. in
144d0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
144e0 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
144f0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 t = p->pBt;. as
14500 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d sert( nReserve>=
14510 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d -1 && nReserve<=
14520 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 255 );. sqlite3
14530 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 BtreeEnter(p);.#
14540 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f if SQLITE_HAS_CO
14550 44 45 43 0a 20 20 69 66 28 20 6e 52 65 73 65 72 DEC. if( nReser
14560 76 65 3e 70 42 74 2d 3e 6f 70 74 69 6d 61 6c 52 ve>pBt->optimalR
14570 65 73 65 72 76 65 20 29 20 70 42 74 2d 3e 6f 70 eserve ) pBt->op
14580 74 69 6d 61 6c 52 65 73 65 72 76 65 20 3d 20 28 timalReserve = (
14590 75 38 29 6e 52 65 73 65 72 76 65 3b 0a 23 65 6e u8)nReserve;.#en
145a0 64 69 66 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 dif. if( pBt->b
145b0 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50 41 tsFlags & BTS_PA
145c0 47 45 53 49 5a 45 5f 46 49 58 45 44 20 29 7b 0a GESIZE_FIXED ){.
145d0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
145e0 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 Leave(p);. re
145f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 turn SQLITE_READ
14600 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ONLY;. }. if(
14610 6e 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 nReserve<0 ){.
14620 20 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 74 nReserve = pBt
14630 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 ->pageSize - pBt
14640 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 ->usableSize;.
14650 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 }. assert( nRes
14660 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 erve>=0 && nRese
14670 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 rve<=255 );. if
14680 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 ( pageSize>=512
14690 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c && pageSize<=SQL
146a0 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
146b0 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 70 E &&. ((p
146c0 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 ageSize-1)&pageS
146d0 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 ize)==0 ){. a
146e0 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 ssert( (pageSize
146f0 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 & 7)==0 );.
14700 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50 assert( !pBt->pP
14710 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43 age1 && !pBt->pC
14720 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 ursor );. pBt
14730 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 33 ->pageSize = (u3
14740 32 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 2)pageSize;.
14750 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 freeTempSpace(pB
14760 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 t);. }. rc = s
14770 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
14780 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 gesize(pBt->pPag
14790 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 er, &pBt->pageSi
147a0 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 ze, nReserve);.
147b0 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
147c0 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 = pBt->pageSize
147d0 20 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 - (u16)nReserve
147e0 3b 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 70 ;. if( iFix ) p
147f0 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 Bt->btsFlags |=
14800 42 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 BTS_PAGESIZE_FIX
14810 45 44 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 ED;. sqlite3Btr
14820 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
14830 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
14840 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 * Return the cur
14850 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 rently defined p
14860 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 69 6e 74 20 age size.*/.int
14870 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
14880 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 ageSize(Btree *p
14890 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 ){. return p->p
148a0 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a Bt->pageSize;.}.
148b0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
148c0 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 tion is similar
148d0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 to sqlite3BtreeG
148e0 65 74 52 65 73 65 72 76 65 28 29 2c 20 65 78 63 etReserve(), exc
148f0 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 6d ept that it.** m
14900 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ay only be calle
14910 64 20 69 66 20 69 74 20 69 73 20 67 75 61 72 61 d if it is guara
14920 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 62 nteed that the b
14930 2d 74 72 65 65 20 6d 75 74 65 78 20 69 73 20 61 -tree mutex is a
14940 6c 72 65 61 64 79 0a 2a 2a 20 68 65 6c 64 2e 0a lready.** held..
14950 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 73 **.** This is us
14960 65 66 75 6c 20 69 6e 20 6f 6e 65 20 73 70 65 63 eful in one spec
14970 69 61 6c 20 63 61 73 65 20 69 6e 20 74 68 65 20 ial case in the
14980 62 61 63 6b 75 70 20 41 50 49 20 63 6f 64 65 20 backup API code
14990 77 68 65 72 65 20 69 74 20 69 73 0a 2a 2a 20 6b where it is.** k
149a0 6e 6f 77 6e 20 74 68 61 74 20 74 68 65 20 73 68 nown that the sh
149b0 61 72 65 64 20 62 2d 74 72 65 65 20 6d 75 74 65 ared b-tree mute
149c0 78 20 69 73 20 68 65 6c 64 2c 20 62 75 74 20 74 x is held, but t
149d0 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 he mutex on the
149e0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e .** database han
149f0 64 6c 65 20 74 68 61 74 20 6f 77 6e 73 20 2a 70 dle that owns *p
14a00 20 69 73 20 6e 6f 74 2e 20 49 6e 20 74 68 69 73 is not. In this
14a10 20 63 61 73 65 20 69 66 20 73 71 6c 69 74 65 33 case if sqlite3
14a20 42 74 72 65 65 45 6e 74 65 72 28 29 0a 2a 2a 20 BtreeEnter().**
14a30 77 65 72 65 20 74 6f 20 62 65 20 63 61 6c 6c 65 were to be calle
14a40 64 2c 20 69 74 20 6d 69 67 68 74 20 63 6f 6c 6c d, it might coll
14a50 69 64 65 20 77 69 74 68 20 73 6f 6d 65 20 6f 74 ide with some ot
14a60 68 65 72 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e her operation on
14a70 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
14a80 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6f 77 6e handle that own
14a90 73 20 2a 70 2c 20 63 61 75 73 69 6e 67 20 75 6e s *p, causing un
14aa0 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 defined behavior
14ab0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
14ac0 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 4e BtreeGetReserveN
14ad0 6f 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 oMutex(Btree *p)
14ae0 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 61 73 73 {. int n;. ass
14af0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14b00 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e ex_held(p->pBt->
14b10 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 20 3d 20 mutex) );. n =
14b20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 p->pBt->pageSize
14b30 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c - p->pBt->usabl
14b40 65 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 eSize;. return
14b50 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 n;.}../*.** Retu
14b60 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
14b70 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
14b80 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 at the end of ev
14b90 65 72 79 20 70 61 67 65 20 74 68 61 74 0a 2a 2a ery page that.**
14ba0 20 61 72 65 20 69 6e 74 65 6e 74 75 61 6c 6c 79 are intentually
14bb0 20 6c 65 66 74 20 75 6e 75 73 65 64 2e 20 20 54 left unused. T
14bc0 68 69 73 20 69 73 20 74 68 65 20 22 72 65 73 65 his is the "rese
14bd0 72 76 65 64 22 20 73 70 61 63 65 20 74 68 61 74 rved" space that
14be0 20 69 73 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 is.** sometimes
14bf0 20 75 73 65 64 20 62 79 20 65 78 74 65 6e 73 69 used by extensi
14c00 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 ons..**.** If SQ
14c10 4c 49 54 45 5f 48 41 53 5f 4d 55 54 45 58 20 69 LITE_HAS_MUTEX i
14c20 73 20 64 65 66 69 6e 65 64 20 74 68 65 6e 20 74 s defined then t
14c30 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e he number return
14c40 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 67 72 65 ed is the.** gre
14c50 61 74 65 72 20 6f 66 20 74 68 65 20 63 75 72 72 ater of the curr
14c60 65 6e 74 20 72 65 73 65 72 76 65 64 20 73 70 61 ent reserved spa
14c70 63 65 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d ce and the maxim
14c80 75 6d 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 um requested.**
14c90 72 65 73 65 72 76 65 20 73 70 61 63 65 2e 0a 2a reserve space..*
14ca0 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
14cb0 65 65 47 65 74 4f 70 74 69 6d 61 6c 52 65 73 65 eeGetOptimalRese
14cc0 72 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 rve(Btree *p){.
14cd0 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 int n;. sqlite
14ce0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
14cf0 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 42 74 72 n = sqlite3Btr
14d00 65 65 47 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 eeGetReserveNoMu
14d10 74 65 78 28 70 29 3b 0a 23 69 66 64 65 66 20 53 tex(p);.#ifdef S
14d20 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
14d30 20 20 69 66 28 20 6e 3c 70 2d 3e 70 42 74 2d 3e if( n<p->pBt->
14d40 6f 70 74 69 6d 61 6c 52 65 73 65 72 76 65 20 29 optimalReserve )
14d50 20 6e 20 3d 20 70 2d 3e 70 42 74 2d 3e 6f 70 74 n = p->pBt->opt
14d60 69 6d 61 6c 52 65 73 65 72 76 65 3b 0a 23 65 6e imalReserve;.#en
14d70 64 69 66 0a 20 20 73 71 6c 69 74 65 33 42 74 72 dif. sqlite3Btr
14d80 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
14d90 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn n;.}.../*.*
14da0 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 * Set the maximu
14db0 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 m page count for
14dc0 20 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d a database if m
14dd0 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 xPage is positiv
14de0 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 e..** No changes
14df0 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 are made if mxP
14e00 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 age is 0 or nega
14e10 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c tive..** Regardl
14e20 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ess of the value
14e30 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 of mxPage, retu
14e40 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 rn the maximum p
14e50 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 69 6e age count..*/.in
14e60 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 t sqlite3BtreeMa
14e70 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 xPageCount(Btree
14e80 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 *p, int mxPage)
14e90 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c {. int n;. sql
14ea0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
14eb0 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 );. n = sqlite3
14ec0 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e PagerMaxPageCoun
14ed0 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 t(p->pBt->pPager
14ee0 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c , mxPage);. sql
14ef0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
14f00 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d );. return n;.}
14f10 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
14f20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 BTS_SECURE_DELET
14f30 45 20 66 6c 61 67 20 69 66 20 6e 65 77 46 6c 61 E flag if newFla
14f40 67 20 69 73 20 30 20 6f 72 20 31 2e 20 20 49 66 g is 0 or 1. If
14f50 20 6e 65 77 46 6c 61 67 20 69 73 20 2d 31 2c 0a newFlag is -1,.
14f60 2a 2a 20 74 68 65 6e 20 6d 61 6b 65 20 6e 6f 20 ** then make no
14f70 63 68 61 6e 67 65 73 2e 20 20 41 6c 77 61 79 73 changes. Always
14f80 20 72 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 return the valu
14f90 65 20 6f 66 20 74 68 65 20 42 54 53 5f 53 45 43 e of the BTS_SEC
14fa0 55 52 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 73 65 URE_DELETE.** se
14fb0 74 74 69 6e 67 20 61 66 74 65 72 20 74 68 65 20 tting after the
14fc0 63 68 61 6e 67 65 2e 0a 2a 2f 0a 69 6e 74 20 73 change..*/.int s
14fd0 71 6c 69 74 65 33 42 74 72 65 65 53 65 63 75 72 qlite3BtreeSecur
14fe0 65 44 65 6c 65 74 65 28 42 74 72 65 65 20 2a 70 eDelete(Btree *p
14ff0 2c 20 69 6e 74 20 6e 65 77 46 6c 61 67 29 7b 0a , int newFlag){.
15000 20 20 69 6e 74 20 62 3b 0a 20 20 69 66 28 20 70 int b;. if( p
15010 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
15020 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15030 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 6e 65 ter(p);. if( ne
15040 77 46 6c 61 67 3e 3d 30 20 29 7b 0a 20 20 20 20 wFlag>=0 ){.
15050 70 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 p->pBt->btsFlags
15060 20 26 3d 20 7e 42 54 53 5f 53 45 43 55 52 45 5f &= ~BTS_SECURE_
15070 44 45 4c 45 54 45 3b 0a 20 20 20 20 69 66 28 20 DELETE;. if(
15080 6e 65 77 46 6c 61 67 20 29 20 70 2d 3e 70 42 74 newFlag ) p->pBt
15090 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 ->btsFlags |= BT
150a0 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 3b S_SECURE_DELETE;
150b0 0a 20 20 7d 20 0a 20 20 62 20 3d 20 28 70 2d 3e . } . b = (p->
150c0 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
150d0 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 BTS_SECURE_DELET
150e0 45 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 E)!=0;. sqlite3
150f0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
15100 20 72 65 74 75 72 6e 20 62 3b 0a 7d 0a 0a 2f 2a return b;.}../*
15110 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 .** Change the '
15120 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f auto-vacuum' pro
15130 70 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 perty of the dat
15140 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 abase. If the 'a
15150 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 utoVacuum'.** pa
15160 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a rameter is non-z
15170 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 ero, then auto-v
15180 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e acuum mode is en
15190 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 abled. If zero,
151a0 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 it.** is disable
151b0 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 d. The default v
151c0 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 alue for the aut
151d0 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 o-vacuum propert
151e0 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 y is .** determi
151f0 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 ned by the SQLIT
15200 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 E_DEFAULT_AUTOVA
15210 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 69 CUUM macro..*/.i
15220 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
15230 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 etAutoVacuum(Btr
15240 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 ee *p, int autoV
15250 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 acuum){.#ifdef S
15260 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
15270 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 ACUUM. return S
15280 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
15290 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 #else. BtShared
152a0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
152b0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
152c0 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 E_OK;. u8 av =
152d0 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a (u8)autoVacuum;.
152e0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
152f0 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 28 nter(p);. if( (
15300 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
15310 42 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 BTS_PAGESIZE_FIX
15320 45 44 29 21 3d 30 20 26 26 20 28 61 76 20 3f 31 ED)!=0 && (av ?1
15330 3a 30 29 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 :0)!=pBt->autoVa
15340 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d cuum ){. rc =
15350 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
15360 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
15370 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d Bt->autoVacuum =
15380 20 61 76 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 av ?1:0;. pB
15390 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
153a0 61 76 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a av==2 ?1:0;. }.
153b0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
153c0 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
153d0 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f rc;.#endif.}../
153e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
153f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 value of the 'au
15400 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 to-vacuum' prope
15410 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 rty. If auto-vac
15420 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c uum is .** enabl
15430 65 64 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 ed 1 is returned
15440 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a . Otherwise 0..*
15450 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
15460 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeGetAutoVacuum(
15470 42 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 Btree *p){.#ifde
15480 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
15490 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 TOVACUUM. retur
154a0 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 n BTREE_AUTOVACU
154b0 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 UM_NONE;.#else.
154c0 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
154d0 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
154e0 0a 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 21 . rc = (. (!
154f0 70 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 p->pBt->autoVacu
15500 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 um)?BTREE_AUTOVA
15510 43 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 CUUM_NONE:. (
15520 21 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 !p->pBt->incrVac
15530 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 uum)?BTREE_AUTOV
15540 41 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 ACUUM_FULL:.
15550 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
15560 5f 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c _INCR. );. sql
15570 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
15580 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
15590 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a #endif.}.../*.**
155a0 20 47 65 74 20 61 20 72 65 66 65 72 65 6e 63 65 Get a reference
155b0 20 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74 68 to pPage1 of th
155c0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
155d0 20 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 This will.** a
155e0 6c 73 6f 20 61 63 71 75 69 72 65 20 61 20 72 65 lso acquire a re
155f0 61 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 adlock on that f
15600 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 ile..**.** SQLIT
15610 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
15620 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 on success. If
15630 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 the file is not
15640 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 a.** well-forme
15650 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c d database file,
15660 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 then SQLITE_COR
15670 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 RUPT is returned
15680 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 ..** SQLITE_BUSY
15690 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
156a0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
156b0 6c 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f locked. SQLITE_
156c0 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 NOMEM.** is retu
156d0 72 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20 6f rned if we run o
156e0 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a ut of memory. .*
156f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 /.static int loc
15700 6b 42 74 72 65 65 28 42 74 53 68 61 72 65 64 20 kBtree(BtShared
15710 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b *pBt){. int rc;
15720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15730 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f Result code fro
15740 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a m subfunctions *
15750 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 /. MemPage *pPa
15760 67 65 31 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 ge1; /* Page
15770 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 1 of the databa
15780 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 se file */. int
15790 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
157a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
157b0 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
157c0 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 base */. int nP
157d0 61 67 65 46 69 6c 65 20 3d 20 30 3b 20 20 20 2f ageFile = 0; /
157e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
157f0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
15800 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 e file */. int
15810 6e 50 61 67 65 48 65 61 64 65 72 3b 20 20 20 20 nPageHeader;
15820 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
15830 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
15840 61 73 65 20 61 63 63 6f 72 64 69 6e 67 20 74 6f ase according to
15850 20 68 64 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 hdr */.. asser
15860 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15870 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
15880 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
15890 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 3b Bt->pPage1==0 );
158a0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
158b0 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 agerSharedLock(p
158c0 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 Bt->pPager);. i
158d0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
158e0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
158f0 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
15900 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 e(pBt, 1, &pPage
15910 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 1, 0);. if( rc!
15920 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
15930 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f urn rc;.. /* Do
15940 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 some checking t
15950 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68 o help insure th
15960 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 e file we opened
15970 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 really is. **
15980 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 65 a valid database
15990 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e file. . */. n
159a0 50 61 67 65 20 3d 20 6e 50 61 67 65 48 65 61 64 Page = nPageHead
159b0 65 72 20 3d 20 67 65 74 34 62 79 74 65 28 32 38 er = get4byte(28
159c0 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 +(u8*)pPage1->aD
159d0 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 ata);. sqlite3P
159e0 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
159f0 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 t->pPager, &nPag
15a00 65 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 6e 50 eFile);. if( nP
15a10 61 67 65 3d 3d 30 20 7c 7c 20 6d 65 6d 63 6d 70 age==0 || memcmp
15a20 28 32 34 2b 28 75 38 2a 29 70 50 61 67 65 31 2d (24+(u8*)pPage1-
15a30 3e 61 44 61 74 61 2c 20 39 32 2b 28 75 38 2a 29 >aData, 92+(u8*)
15a40 70 50 61 67 65 31 2d 3e 61 44 61 74 61 2c 34 29 pPage1->aData,4)
15a50 21 3d 30 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 !=0 ){. nPage
15a60 20 3d 20 6e 50 61 67 65 46 69 6c 65 3b 0a 20 20 = nPageFile;.
15a70 7d 0a 20 20 69 66 28 20 6e 50 61 67 65 3e 30 20 }. if( nPage>0
15a80 29 7b 0a 20 20 20 20 75 33 32 20 70 61 67 65 53 ){. u32 pageS
15a90 69 7a 65 3b 0a 20 20 20 20 75 33 32 20 75 73 61 ize;. u32 usa
15aa0 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 20 bleSize;. u8
15ab0 2a 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d *page1 = pPage1-
15ac0 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d >aData;. rc =
15ad0 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a SQLITE_NOTADB;.
15ae0 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
15af0 4f 46 3a 20 52 2d 34 33 37 33 37 2d 33 39 39 39 OF: R-43737-3999
15b00 39 20 45 76 65 72 79 20 76 61 6c 69 64 20 53 51 9 Every valid SQ
15b10 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 66 69 Lite database fi
15b20 6c 65 20 62 65 67 69 6e 73 0a 20 20 20 20 2a 2a le begins. **
15b30 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 with the follow
15b40 69 6e 67 20 31 36 20 62 79 74 65 73 20 28 69 6e ing 16 bytes (in
15b50 20 68 65 78 29 3a 20 35 33 20 35 31 20 34 63 20 hex): 53 51 4c
15b60 36 39 20 37 34 20 36 35 20 32 30 20 36 36 20 36 69 74 65 20 66 6
15b70 66 20 37 32 20 36 64 0a 20 20 20 20 2a 2a 20 36 f 72 6d. ** 6
15b80 31 20 37 34 20 32 30 20 33 33 20 30 30 2e 20 2a 1 74 20 33 00. *
15b90 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 /. if( memcmp
15ba0 28 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 (page1, zMagicHe
15bb0 61 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a ader, 16)!=0 ){.
15bc0 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
15bd0 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
15be0 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 }..#ifdef SQLI
15bf0 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 20 20 TE_OMIT_WAL.
15c00 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 if( page1[18]>1
15c10 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 ){. pBt->bt
15c20 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 sFlags |= BTS_RE
15c30 41 44 5f 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 AD_ONLY;. }.
15c40 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d if( page1[19]
15c50 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f >1 ){. goto
15c60 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
15c70 65 64 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a ed;. }.#else.
15c80 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 if( page1[18
15c90 5d 3e 32 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ]>2 ){. pBt
15ca0 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 ->btsFlags |= BT
15cb0 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0a 20 20 20 S_READ_ONLY;.
15cc0 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 }. if( page1
15cd0 5b 31 39 5d 3e 32 20 29 7b 0a 20 20 20 20 20 20 [19]>2 ){.
15ce0 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
15cf0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 failed;. }..
15d00 20 20 20 2f 2a 20 49 66 20 74 68 65 20 77 72 69 /* If the wri
15d10 74 65 20 76 65 72 73 69 6f 6e 20 69 73 20 73 65 te version is se
15d20 74 20 74 6f 20 32 2c 20 74 68 69 73 20 64 61 74 t to 2, this dat
15d30 61 62 61 73 65 20 73 68 6f 75 6c 64 20 62 65 20 abase should be
15d40 61 63 63 65 73 73 65 64 0a 20 20 20 20 2a 2a 20 accessed. **
15d50 69 6e 20 57 41 4c 20 6d 6f 64 65 2e 20 49 66 20 in WAL mode. If
15d60 74 68 65 20 6c 6f 67 20 69 73 20 6e 6f 74 20 61 the log is not a
15d70 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 6f 70 65 lready open, ope
15d80 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 6e 20 0a n it now. Then .
15d90 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 ** return SQ
15da0 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 74 75 LITE_OK and retu
15db0 72 6e 20 77 69 74 68 6f 75 74 20 70 6f 70 75 6c rn without popul
15dc0 61 74 69 6e 67 20 42 74 53 68 61 72 65 64 2e 70 ating BtShared.p
15dd0 50 61 67 65 31 2e 0a 20 20 20 20 2a 2a 20 54 68 Page1.. ** Th
15de0 65 20 63 61 6c 6c 65 72 20 64 65 74 65 63 74 73 e caller detects
15df0 20 74 68 69 73 20 61 6e 64 20 63 61 6c 6c 73 20 this and calls
15e00 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 this function ag
15e10 61 69 6e 2e 20 54 68 69 73 20 69 73 0a 20 20 20 ain. This is.
15e20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 61 73 20 ** required as
15e30 74 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 the version of p
15e40 61 67 65 20 31 20 63 75 72 72 65 6e 74 6c 79 20 age 1 currently
15e50 69 6e 20 74 68 65 20 70 61 67 65 31 20 62 75 66 in the page1 buf
15e60 66 65 72 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e fer. ** may n
15e70 6f 74 20 62 65 20 74 68 65 20 6c 61 74 65 73 74 ot be the latest
15e80 20 76 65 72 73 69 6f 6e 20 2d 20 74 68 65 72 65 version - there
15e90 20 6d 61 79 20 62 65 20 61 20 6e 65 77 65 72 20 may be a newer
15ea0 6f 6e 65 20 69 6e 20 74 68 65 20 6c 6f 67 0a 20 one in the log.
15eb0 20 20 20 2a 2a 20 66 69 6c 65 2e 0a 20 20 20 20 ** file..
15ec0 2a 2f 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 */. if( page1
15ed0 5b 31 39 5d 3d 3d 32 20 26 26 20 28 70 42 74 2d [19]==2 && (pBt-
15ee0 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f >btsFlags & BTS_
15ef0 4e 4f 5f 57 41 4c 29 3d 3d 30 20 29 7b 0a 20 20 NO_WAL)==0 ){.
15f00 20 20 20 20 69 6e 74 20 69 73 4f 70 65 6e 20 3d int isOpen =
15f10 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
15f20 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 57 qlite3PagerOpenW
15f30 61 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 al(pBt->pPager,
15f40 26 69 73 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 &isOpen);.
15f50 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15f60 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
15f70 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
15f80 6c 65 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 led;. }else
15f90 20 69 66 28 20 69 73 4f 70 65 6e 3d 3d 30 20 29 if( isOpen==0 )
15fa0 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 {. releas
15fb0 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
15fc0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
15fd0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
15fe0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
15ff0 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 7d TE_NOTADB;. }
16000 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
16010 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 EVIDENCE-OF: R-1
16020 35 34 36 35 2d 32 30 38 31 33 20 54 68 65 20 6d 5465-20813 The m
16030 61 78 69 6d 75 6d 20 61 6e 64 20 6d 69 6e 69 6d aximum and minim
16040 75 6d 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c um embedded payl
16050 6f 61 64 0a 20 20 20 20 2a 2a 20 66 72 61 63 74 oad. ** fract
16060 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 6c 65 61 ions and the lea
16070 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 f payload fracti
16080 6f 6e 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 on values must b
16090 65 20 36 34 2c 20 33 32 2c 20 61 6e 64 20 33 32 e 64, 32, and 32
160a0 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
160b0 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 The original des
160c0 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 ign allowed thes
160d0 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 e amounts to var
160e0 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 y, but as of.
160f0 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e ** version 3.6.
16100 30 2c 20 77 65 20 72 65 71 75 69 72 65 20 74 68 0, we require th
16110 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a em to be fixed..
16120 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d */. if( m
16130 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d emcmp(&page1[21]
16140 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 , "\100\040\040"
16150 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ,3)!=0 ){.
16160 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
16170 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
16180 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 /* EVIDENCE-OF
16190 3a 20 52 2d 35 31 38 37 33 2d 33 39 36 31 38 20 : R-51873-39618
161a0 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f The page size fo
161b0 72 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c r a database fil
161c0 65 20 69 73 0a 20 20 20 20 2a 2a 20 64 65 74 65 e is. ** dete
161d0 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 32 2d rmined by the 2-
161e0 62 79 74 65 20 69 6e 74 65 67 65 72 20 6c 6f 63 byte integer loc
161f0 61 74 65 64 20 61 74 20 61 6e 20 6f 66 66 73 65 ated at an offse
16200 74 20 6f 66 20 31 36 20 62 79 74 65 73 20 66 72 t of 16 bytes fr
16210 6f 6d 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 65 om. ** the be
16220 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 ginning of the d
16230 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f atabase file. */
16240 0a 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 . pageSize =
16250 28 70 61 67 65 31 5b 31 36 5d 3c 3c 38 29 20 7c (page1[16]<<8) |
16260 20 28 70 61 67 65 31 5b 31 37 5d 3c 3c 31 36 29 (page1[17]<<16)
16270 3b 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 ;. /* EVIDENC
16280 45 2d 4f 46 3a 20 52 2d 32 35 30 30 38 2d 32 31 E-OF: R-25008-21
16290 36 38 38 20 54 68 65 20 73 69 7a 65 20 6f 66 20 688 The size of
162a0 61 20 70 61 67 65 20 69 73 20 61 20 70 6f 77 65 a page is a powe
162b0 72 20 6f 66 20 74 77 6f 0a 20 20 20 20 2a 2a 20 r of two. **
162c0 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 between 512 and
162d0 36 35 35 33 36 20 69 6e 63 6c 75 73 69 76 65 2e 65536 inclusive.
162e0 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 28 70 61 */. if( ((pa
162f0 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 geSize-1)&pageSi
16300 7a 65 29 21 3d 30 0a 20 20 20 20 20 7c 7c 20 70 ze)!=0. || p
16310 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
16320 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 AX_PAGE_SIZE .
16330 20 20 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 3d || pageSize<=
16340 32 35 36 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 256 . ){.
16350 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
16360 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
16370 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 67 assert( (pag
16380 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b eSize & 7)==0 );
16390 0a 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 . /* EVIDENCE
163a0 2d 4f 46 3a 20 52 2d 35 39 33 31 30 2d 35 31 32 -OF: R-59310-512
163b0 30 35 20 54 68 65 20 22 72 65 73 65 72 76 65 64 05 The "reserved
163c0 20 73 70 61 63 65 22 20 73 69 7a 65 20 69 6e 20 space" size in
163d0 74 68 65 20 31 2d 62 79 74 65 0a 20 20 20 20 2a the 1-byte. *
163e0 2a 20 69 6e 74 65 67 65 72 20 61 74 20 6f 66 66 * integer at off
163f0 73 65 74 20 32 30 20 69 73 20 74 68 65 20 6e 75 set 20 is the nu
16400 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
16410 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 6e space at the en
16420 64 20 6f 66 0a 20 20 20 20 2a 2a 20 65 61 63 68 d of. ** each
16430 20 70 61 67 65 20 74 6f 20 72 65 73 65 72 76 65 page to reserve
16440 20 66 6f 72 20 65 78 74 65 6e 73 69 6f 6e 73 2e for extensions.
16450 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 . **. **
16460 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 33 EVIDENCE-OF: R-3
16470 37 34 39 37 2d 34 32 34 31 32 20 54 68 65 20 73 7497-42412 The s
16480 69 7a 65 20 6f 66 20 74 68 65 20 72 65 73 65 72 ize of the reser
16490 76 65 64 20 72 65 67 69 6f 6e 20 69 73 0a 20 20 ved region is.
164a0 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 ** determined
164b0 62 79 20 74 68 65 20 6f 6e 65 2d 62 79 74 65 20 by the one-byte
164c0 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
164d0 20 66 6f 75 6e 64 20 61 74 20 61 6e 20 6f 66 66 found at an off
164e0 73 65 74 20 6f 66 20 32 30 0a 20 20 20 20 2a 2a set of 20. **
164f0 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
16500 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 se file header.
16510 2a 2f 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a */. usableSiz
16520 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 e = pageSize - p
16530 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 age1[20];. if
16540 28 20 28 75 33 32 29 70 61 67 65 53 69 7a 65 21 ( (u32)pageSize!
16550 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 =pBt->pageSize )
16560 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 {. /* After
16570 20 72 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 reading the fir
16580 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 st page of the d
16590 61 74 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 atabase assuming
165a0 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 a page size.
165b0 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 ** of BtShare
165c0 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 d.pageSize, we h
165d0 61 76 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 ave discovered t
165e0 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a hat the page-siz
165f0 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 e is. ** ac
16600 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e tually pageSize.
16610 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 Unlock the data
16620 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d base, leave pBt-
16630 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 >pPage1 at.
16640 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 ** zero and ret
16650 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 urn SQLITE_OK. T
16660 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 he caller will c
16670 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
16680 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e n. ** again
16690 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 with the correc
166a0 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 t page-size..
166b0 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 */. rele
166c0 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b asePage(pPage1);
166d0 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 . pBt->usab
166e0 6c 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 leSize = usableS
166f0 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e ize;. pBt->
16700 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 pageSize = pageS
16710 69 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 ize;. freeT
16720 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 empSpace(pBt);.
16730 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
16740 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
16750 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 e(pBt->pPager, &
16760 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 pBt->pageSize,.
16770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16790 20 20 70 61 67 65 53 69 7a 65 2d 75 73 61 62 6c pageSize-usabl
167a0 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 65 eSize);. re
167b0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
167c0 20 20 20 69 66 28 20 28 70 42 74 2d 3e 64 62 2d if( (pBt->db-
167d0 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
167e0 52 65 63 6f 76 65 72 79 4d 6f 64 65 29 3d 3d 30 RecoveryMode)==0
167f0 20 26 26 20 6e 50 61 67 65 3e 6e 50 61 67 65 46 && nPage>nPageF
16800 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ile ){. rc
16810 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
16820 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 _BKPT;. got
16830 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
16840 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f led;. }. /
16850 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 * EVIDENCE-OF: R
16860 2d 32 38 33 31 32 2d 36 34 37 30 34 20 48 6f 77 -28312-64704 How
16870 65 76 65 72 2c 20 74 68 65 20 75 73 61 62 6c 65 ever, the usable
16880 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 61 6c 6c size is not all
16890 6f 77 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 owed to. ** b
168a0 65 20 6c 65 73 73 20 74 68 61 6e 20 34 38 30 2e e less than 480.
168b0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
168c0 20 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a if the page siz
168d0 65 20 69 73 20 35 31 32 2c 20 74 68 65 6e 20 74 e is 512, then t
168e0 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 65 72 76 he. ** reserv
168f0 65 64 20 73 70 61 63 65 20 73 69 7a 65 20 63 61 ed space size ca
16900 6e 6e 6f 74 20 65 78 63 65 65 64 20 33 32 2e 20 nnot exceed 32.
16910 2a 2f 0a 20 20 20 20 69 66 28 20 75 73 61 62 6c */. if( usabl
16920 65 53 69 7a 65 3c 34 38 30 20 29 7b 0a 20 20 20 eSize<480 ){.
16930 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e goto page1_in
16940 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d it_failed;. }
16950 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 . pBt->pageSi
16960 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 ze = pageSize;.
16970 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 pBt->usableSi
16980 7a 65 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b ze = usableSize;
16990 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
169a0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
169b0 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 pBt->autoVac
169c0 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 uum = (get4byte(
169d0 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d &page1[36 + 4*4]
169e0 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d )?1:0);. pBt-
169f0 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 >incrVacuum = (g
16a00 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 et4byte(&page1[3
16a10 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 6 + 7*4])?1:0);.
16a20 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a #endif. }.. /*
16a30 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 maxLocal is the
16a40 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 maximum amount
16a50 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 of payload to st
16a60 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a ore locally for.
16a70 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 ** a cell. Ma
16a80 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 73 6d ke sure it is sm
16a90 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 all enough so th
16aa0 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 at at least minF
16ab0 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 anout. ** cells
16ac0 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e can will fit on
16ad0 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 one page. We a
16ae0 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 ssume a 10-byte
16af0 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a page header.. *
16b00 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 70 61 * Besides the pa
16b10 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 yload, the cell
16b20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a must store:. **
16b30 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 2-byte poin
16b40 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a ter to the cell.
16b50 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 ** 4-byte
16b60 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 child pointer.
16b70 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b ** 9-byte nK
16b80 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 ey value. **
16b90 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 4-byte nData v
16ba0 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d alue. ** 4-
16bb0 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 byte overflow pa
16bc0 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 ge pointer. **
16bd0 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 So a cell consis
16be0 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 ts of a 2-byte p
16bf0 6f 69 6e 74 65 72 2c 20 61 20 68 65 61 64 65 72 ointer, a header
16c00 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 63 which is as muc
16c10 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 h as. ** 17 byt
16c20 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 es long, 0 to N
16c30 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 bytes of payload
16c40 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 , and an optiona
16c50 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f l 4 byte overflo
16c60 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e w. ** page poin
16c70 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d ter.. */. pBt-
16c80 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 75 31 36 >maxLocal = (u16
16c90 29 28 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 )((pBt->usableSi
16ca0 7a 65 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 ze-12)*64/255 -
16cb0 32 33 29 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 23);. pBt->minL
16cc0 6f 63 61 6c 20 3d 20 28 75 31 36 29 28 28 70 42 ocal = (u16)((pB
16cd0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
16ce0 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 29 3b 0a )*32/255 - 23);.
16cf0 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d pBt->maxLeaf =
16d00 20 28 75 31 36 29 28 70 42 74 2d 3e 75 73 61 62 (u16)(pBt->usab
16d10 6c 65 53 69 7a 65 20 2d 20 33 35 29 3b 0a 20 20 leSize - 35);.
16d20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 pBt->minLeaf = (
16d30 75 31 36 29 28 28 70 42 74 2d 3e 75 73 61 62 6c u16)((pBt->usabl
16d40 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 eSize-12)*32/255
16d50 20 2d 20 32 33 29 3b 0a 20 20 69 66 28 20 70 42 - 23);. if( pB
16d60 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3e 31 32 37 20 t->maxLocal>127
16d70 29 7b 0a 20 20 20 20 70 42 74 2d 3e 6d 61 78 31 ){. pBt->max1
16d80 62 79 74 65 50 61 79 6c 6f 61 64 20 3d 20 31 32 bytePayload = 12
16d90 37 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7;. }else{.
16da0 70 42 74 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 pBt->max1bytePay
16db0 6c 6f 61 64 20 3d 20 28 75 38 29 70 42 74 2d 3e load = (u8)pBt->
16dc0 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 20 20 maxLocal;. }.
16dd0 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 assert( pBt->max
16de0 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f Leaf + 23 <= MX_
16df0 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 CELL_SIZE(pBt) )
16e00 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 ;. pBt->pPage1
16e10 3d 20 70 50 61 67 65 31 3b 0a 20 20 70 42 74 2d = pPage1;. pBt-
16e20 3e 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a >nPage = nPage;.
16e30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16e40 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f OK;..page1_init_
16e50 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73 failed:. releas
16e60 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
16e70 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 pBt->pPage1 = 0
16e80 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
16e90 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
16ea0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
16eb0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 e number of curs
16ec0 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 70 42 74 2e ors open on pBt.
16ed0 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 65 This is for use
16ee0 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 20 .** in assert()
16ef0 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 expressions, so
16f00 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 it is only compi
16f10 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 led if NDEBUG is
16f20 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e not.** defined.
16f30 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 77 72 69 74 .**.** Only writ
16f40 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 63 6f e cursors are co
16f50 75 6e 74 65 64 20 69 66 20 77 72 4f 6e 6c 79 20 unted if wrOnly
16f60 69 73 20 74 72 75 65 2e 20 20 49 66 20 77 72 4f is true. If wrO
16f70 6e 6c 79 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 nly is.** false
16f80 74 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 then all cursors
16f90 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a are counted..**
16fa0 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 .** For the purp
16fb0 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 oses of this rou
16fc0 74 69 6e 65 2c 20 61 20 63 75 72 73 6f 72 20 69 tine, a cursor i
16fd0 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74 68 61 s any cursor tha
16fe0 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c 65 20 t.** is capable
16ff0 6f 66 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 of reading or wr
17000 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 iting to the dat
17010 61 62 61 73 65 2e 20 20 43 75 72 73 6f 72 73 20 abase. Cursors
17020 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 that.** have bee
17030 6e 20 74 72 69 70 70 65 64 20 69 6e 74 6f 20 74 n tripped into t
17040 68 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 he CURSOR_FAULT
17050 73 74 61 74 65 20 61 72 65 20 6e 6f 74 20 63 6f state are not co
17060 75 6e 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 unted..*/.static
17070 20 69 6e 74 20 63 6f 75 6e 74 56 61 6c 69 64 43 int countValidC
17080 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 ursors(BtShared
17090 2a 70 42 74 2c 20 69 6e 74 20 77 72 4f 6e 6c 79 *pBt, int wrOnly
170a0 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
170b0 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 Cur;. int r = 0
170c0 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74 ;. for(pCur=pBt
170d0 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b ->pCursor; pCur;
170e0 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 pCur=pCur->pNex
170f0 74 29 7b 0a 20 20 20 20 69 66 28 20 28 77 72 4f t){. if( (wrO
17100 6e 6c 79 3d 3d 30 20 7c 7c 20 28 70 43 75 72 2d nly==0 || (pCur-
17110 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 >curFlags & BTCF
17120 5f 57 72 69 74 65 46 6c 61 67 29 21 3d 30 29 0a _WriteFlag)!=0).
17130 20 20 20 20 20 26 26 20 70 43 75 72 2d 3e 65 53 && pCur->eS
17140 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 tate!=CURSOR_FAU
17150 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 LT ) r++; . }.
17160 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e return r;.}.#en
17170 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 dif../*.** If th
17180 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 ere are no outst
17190 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20 61 anding cursors a
171a0 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e nd we are not in
171b0 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f the middle.** o
171c0 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
171d0 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20 72 but there is a r
171e0 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ead lock on the
171f0 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a database, then.*
17200 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75 * this routine u
17210 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74 20 nrefs the first
17220 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
17230 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68 20 base file which
17240 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66 65 .** has the effe
17250 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 ct of releasing
17260 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
17270 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 *.** If there is
17280 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
17290 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73 n progress, this
172a0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
172b0 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 -op..*/.static v
172c0 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 oid unlockBtreeI
172d0 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64 fUnused(BtShared
172e0 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74 *pBt){. assert
172f0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17300 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
17310 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 6f );. assert( co
17320 75 6e 74 56 61 6c 69 64 43 75 72 73 6f 72 73 28 untValidCursors(
17330 70 42 74 2c 30 29 3d 3d 30 20 7c 7c 20 70 42 74 pBt,0)==0 || pBt
17340 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e ->inTransaction>
17350 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 TRANS_NONE );.
17360 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 if( pBt->inTrans
17370 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f action==TRANS_NO
17380 4e 45 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 NE && pBt->pPage
17390 31 21 3d 30 20 29 7b 0a 20 20 20 20 4d 65 6d 50 1!=0 ){. MemP
173a0 61 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42 age *pPage1 = pB
173b0 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 20 20 61 t->pPage1;. a
173c0 73 73 65 72 74 28 20 70 50 61 67 65 31 2d 3e 61 ssert( pPage1->a
173d0 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 Data );. asse
173e0 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
173f0 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 Refcount(pBt->pP
17400 61 67 65 72 29 3d 3d 31 20 29 3b 0a 20 20 20 20 ager)==1 );.
17410 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b pBt->pPage1 = 0;
17420 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
17430 28 70 50 61 67 65 31 29 3b 0a 20 20 7d 0a 7d 0a (pPage1);. }.}.
17440 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f ./*.** If pBt po
17450 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 ints to an empty
17460 20 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 file then conve
17470 72 74 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 rt that empty fi
17480 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 le.** into a new
17490 20 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 empty database
174a0 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 by initializing
174b0 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f the first page o
174c0 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 f.** the databas
174d0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
174e0 20 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 newDatabase(BtS
174f0 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d hared *pBt){. M
17500 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 emPage *pP1;. u
17510 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 nsigned char *da
17520 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 ta;. int rc;..
17530 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17540 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
17550 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
17560 20 70 42 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b pBt->nPage>0 ){
17570 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
17580 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 31 TE_OK;. }. pP1
17590 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a = pBt->pPage1;.
175a0 20 20 61 73 73 65 72 74 28 20 70 50 31 21 3d 30 assert( pP1!=0
175b0 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 31 );. data = pP1
175c0 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 ->aData;. rc =
175d0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
175e0 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65 29 3b e(pP1->pDbPage);
175f0 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
17600 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 rn rc;. memcpy(
17610 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 data, zMagicHead
17620 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 er, sizeof(zMagi
17630 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61 73 73 cHeader));. ass
17640 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 ert( sizeof(zMag
17650 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20 29 3b icHeader)==16 );
17660 0a 20 20 64 61 74 61 5b 31 36 5d 20 3d 20 28 75 . data[16] = (u
17670 38 29 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 8)((pBt->pageSiz
17680 65 3e 3e 38 29 26 30 78 66 66 29 3b 0a 20 20 64 e>>8)&0xff);. d
17690 61 74 61 5b 31 37 5d 20 3d 20 28 75 38 29 28 28 ata[17] = (u8)((
176a0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 31 pBt->pageSize>>1
176b0 36 29 26 30 78 66 66 29 3b 0a 20 20 64 61 74 61 6)&0xff);. data
176c0 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 [18] = 1;. data
176d0 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 [19] = 1;. asse
176e0 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 rt( pBt->usableS
176f0 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 ize<=pBt->pageSi
17700 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c ze && pBt->usabl
17710 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e eSize+255>=pBt->
17720 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 pageSize);. dat
17730 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 a[20] = (u8)(pBt
17740 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 ->pageSize - pBt
17750 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 ->usableSize);.
17760 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a data[21] = 64;.
17770 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b data[22] = 32;
17780 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 . data[23] = 32
17790 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 ;. memset(&data
177a0 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 [24], 0, 100-24)
177b0 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 ;. zeroPage(pP1
177c0 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 , PTF_INTKEY|PTF
177d0 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 _LEAF|PTF_LEAFDA
177e0 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 62 74 73 TA );. pBt->bts
177f0 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 Flags |= BTS_PAG
17800 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 ESIZE_FIXED;.#if
17810 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17820 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 61 73 _AUTOVACUUM. as
17830 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 sert( pBt->autoV
17840 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d acuum==1 || pBt-
17850 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20 29 >autoVacuum==0 )
17860 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
17870 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20 7c >incrVacuum==1 |
17880 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 | pBt->incrVacuu
17890 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34 62 79 m==0 );. put4by
178a0 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 34 2a te(&data[36 + 4*
178b0 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 4], pBt->autoVac
178c0 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 uum);. put4byte
178d0 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a 34 5d (&data[36 + 7*4]
178e0 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 , pBt->incrVacuu
178f0 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 70 42 74 m);.#endif. pBt
17900 2d 3e 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 64 ->nPage = 1;. d
17910 61 74 61 5b 33 31 5d 20 3d 20 31 3b 0a 20 20 72 ata[31] = 1;. r
17920 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17930 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
17940 6c 69 7a 65 20 74 68 65 20 66 69 72 73 74 20 70 lize the first p
17950 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
17960 61 73 65 20 66 69 6c 65 20 28 63 72 65 61 74 69 ase file (creati
17970 6e 67 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a ng a database.**
17980 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 consisting of a
17990 20 73 69 6e 67 6c 65 20 70 61 67 65 20 61 6e 64 single page and
179a0 20 6e 6f 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 no schema objec
179b0 74 73 29 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 ts). Return SQLI
179c0 54 45 5f 4f 4b 0a 2a 2a 20 69 66 20 73 75 63 63 TE_OK.** if succ
179d0 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 essful, or an SQ
179e0 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 Lite error code
179f0 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 69 6e otherwise..*/.in
17a00 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 t sqlite3BtreeNe
17a10 77 44 62 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 wDb(Btree *p){.
17a20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
17a30 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
17a40 0a 20 20 70 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 . p->pBt->nPage
17a50 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 6e 65 77 = 0;. rc = new
17a60 44 61 74 61 62 61 73 65 28 70 2d 3e 70 42 74 29 Database(p->pBt)
17a70 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
17a80 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
17a90 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
17aa0 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 Attempt to start
17ab0 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 a new transacti
17ac0 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e on. A write-tran
17ad0 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 saction.** is st
17ae0 61 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 arted if the sec
17af0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
17b00 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 nonzero, otherwi
17b10 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 se a read-.** tr
17b20 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 ansaction. If t
17b30 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
17b40 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 nt is 2 or more
17b50 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a and exclusive.**
17b60 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
17b70 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 started, meaning
17b80 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 that no other p
17b90 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 rocess is allowe
17ba0 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 d.** to access t
17bb0 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 he database. A
17bc0 70 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e preexisting tran
17bd0 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 saction may not
17be0 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 be.** upgraded t
17bf0 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 o exclusive by c
17c00 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
17c10 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d ine a second tim
17c20 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 e - the.** exclu
17c30 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 sivity flag only
17c40 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 works for a new
17c50 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
17c60 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e .** A write-tran
17c70 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 saction must be
17c80 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 started before a
17c90 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a ttempting any .*
17ca0 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 * changes to the
17cb0 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 database. None
17cc0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
17cd0 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 g routines .** w
17ce0 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 ill work unless
17cf0 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
17d00 20 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a started first:.
17d10 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 **.** sqlit
17d20 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 e3BtreeCreateTab
17d30 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c le().** sql
17d40 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 ite3BtreeCreateI
17d50 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 ndex().** s
17d60 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
17d70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 Table().**
17d80 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 sqlite3BtreeDrop
17d90 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 Table().**
17da0 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 sqlite3BtreeInse
17db0 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c rt().** sql
17dc0 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 ite3BtreeDelete(
17dd0 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
17de0 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
17df0 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 ().**.** If an i
17e00 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 nitial attempt t
17e10 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f o acquire the lo
17e20 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 ck fails because
17e30 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 of lock content
17e40 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 ion.** and the d
17e50 61 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 atabase was prev
17e60 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c iously unlocked,
17e70 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 then invoke the
17e80 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a busy handler.**
17e90 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 if there is one
17ea0 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 . But if there
17eb0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 was previously a
17ec0 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e read-lock, do n
17ed0 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 ot.** invoke the
17ee0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 busy handler -
17ef0 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 just return SQLI
17f00 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 TE_BUSY. SQLITE
17f10 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 _BUSY is .** ret
17f20 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 urned when there
17f30 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 is already a re
17f40 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 ad-lock in order
17f50 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 to avoid a dead
17f60 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 lock..**.** Supp
17f70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 ose there are tw
17f80 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e o processes A an
17f90 64 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 d B. A has a re
17fa0 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 ad lock and B ha
17fb0 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 s.** a reserved
17fc0 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 lock. B tries t
17fd0 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 o promote to exc
17fe0 6c 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c lusive but is bl
17ff0 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a ocked because.**
18000 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 of A's read loc
18010 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 k. A tries to p
18020 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 romote to reserv
18030 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 ed but is blocke
18040 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f d by B..** One o
18050 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 r the other of t
18060 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 he two processes
18070 20 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f must give way o
18080 72 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a r there can be.*
18090 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 * no progress.
180a0 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c By returning SQL
180b0 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 ITE_BUSY and not
180c0 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 invoking the bu
180d0 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 sy callback.** w
180e0 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 hen A already ha
180f0 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 s a read lock, w
18100 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f e encourage A to
18110 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 give up and let
18120 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a B.** proceed..*
18130 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 /.int sqlite3Btr
18140 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 eeBeginTrans(Btr
18150 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 ee *p, int wrfla
18160 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 g){. sqlite3 *p
18170 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 Block = 0;. BtS
18180 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
18190 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 pBt;. int rc =
181a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 SQLITE_OK;.. sq
181b0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
181c0 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 p);. btreeInteg
181d0 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 rity(p);.. /* I
181e0 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 61 f the btree is a
181f0 6c 72 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 lready in a writ
18200 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f e-transaction, o
18210 72 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 r it. ** is alr
18220 65 61 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 eady in a read-t
18230 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 ransaction and a
18240 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f read-transactio
18250 6e 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 n. ** is reques
18260 74 65 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e ted, this is a n
18270 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 o-op.. */. if(
18280 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
18290 4e 53 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e NS_WRITE || (p->
182a0 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 inTrans==TRANS_R
182b0 45 41 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 EAD && !wrflag)
182c0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e ){. goto tran
182d0 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 20 20 61 s_begun;. }. a
182e0 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
182f0 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
18300 5f 57 52 49 54 45 20 7c 7c 20 49 66 4e 6f 74 4f _WRITE || IfNotO
18310 6d 69 74 41 56 28 70 42 74 2d 3e 62 44 6f 54 72 mitAV(pBt->bDoTr
18320 75 6e 63 61 74 65 29 3d 3d 30 20 29 3b 0a 0a 20 uncate)==0 );..
18330 20 2f 2a 20 57 72 69 74 65 20 74 72 61 6e 73 61 /* Write transa
18340 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 ctions are not p
18350 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 ossible on a rea
18360 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 d-only database
18370 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 2d 3e 62 */. if( (pBt->b
18380 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 tsFlags & BTS_RE
18390 41 44 5f 4f 4e 4c 59 29 21 3d 30 20 26 26 20 77 AD_ONLY)!=0 && w
183a0 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20 rflag ){. rc
183b0 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c = SQLITE_READONL
183c0 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e Y;. goto tran
183d0 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 s_begun;. }..#i
183e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
183f0 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
18400 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 64 /* If another d
18410 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 68 atabase handle h
18420 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 as already opene
18430 64 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 d a write transa
18440 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 ction . ** on t
18450 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 his shared-btree
18460 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 structure and a
18470 20 73 65 63 6f 6e 64 20 77 72 69 74 65 20 74 72 second write tr
18480 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a ansaction is. *
18490 2a 20 72 65 71 75 65 73 74 65 64 2c 20 72 65 74 * requested, ret
184a0 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
184b0 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 77 D.. */. if( (w
184c0 72 66 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e rflag && pBt->in
184d0 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
184e0 4e 53 5f 57 52 49 54 45 29 0a 20 20 20 7c 7c 20 NS_WRITE). ||
184f0 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 (pBt->btsFlags &
18500 20 42 54 53 5f 50 45 4e 44 49 4e 47 29 21 3d 30 BTS_PENDING)!=0
18510 0a 20 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b . ){. pBlock
18520 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d = pBt->pWriter-
18530 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 >db;. }else if(
18540 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 wrflag>1 ){.
18550 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a BtLock *pIter;.
18560 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 for(pIter=pB
18570 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b t->pLock; pIter;
18580 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e pIter=pIter->pN
18590 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ext){. if(
185a0 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 pIter->pBtree!=p
185b0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 6c 6f ){. pBlo
185c0 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 ck = pIter->pBtr
185d0 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 ee->db;.
185e0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
185f0 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 }. }. if( p
18600 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c Block ){. sql
18610 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c ite3ConnectionBl
18620 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c ocked(p->db, pBl
18630 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 ock);. rc = S
18640 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
18650 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 67 6f REDCACHE;. go
18660 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a to trans_begun;.
18670 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
18680 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f Any read-only o
18690 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 61 r read-write tra
186a0 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 nsaction implies
186b0 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 a read-lock on
186c0 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f . ** page 1. So
186d0 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 if some other s
186e0 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c 69 65 hared-cache clie
186f0 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 nt already has a
18700 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a write-lock . *
18710 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 * on page 1, the
18720 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e transaction can
18730 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a not be opened. *
18740 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 /. rc = querySh
18750 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18760 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f ck(p, MASTER_ROO
18770 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 T, READ_LOCK);.
18780 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
18790 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f rc ) goto trans_
187a0 62 65 67 75 6e 3b 0a 0a 20 20 70 42 74 2d 3e 62 begun;.. pBt->b
187b0 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f tsFlags &= ~BTS_
187c0 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 3b INITIALLY_EMPTY;
187d0 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 . if( pBt->nPag
187e0 65 3d 3d 30 20 29 20 70 42 74 2d 3e 62 74 73 46 e==0 ) pBt->btsF
187f0 6c 61 67 73 20 7c 3d 20 42 54 53 5f 49 4e 49 54 lags |= BTS_INIT
18800 49 41 4c 4c 59 5f 45 4d 50 54 59 3b 0a 20 20 64 IALLY_EMPTY;. d
18810 6f 20 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 o {. /* Call
18820 6c 6f 63 6b 42 74 72 65 65 28 29 20 75 6e 74 69 lockBtree() unti
18830 6c 20 65 69 74 68 65 72 20 70 42 74 2d 3e 70 50 l either pBt->pP
18840 61 67 65 31 20 69 73 20 70 6f 70 75 6c 61 74 65 age1 is populate
18850 64 20 6f 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b d or. ** lock
18860 42 74 72 65 65 28 29 20 72 65 74 75 72 6e 73 20 Btree() returns
18870 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 something other
18880 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 than SQLITE_OK.
18890 6c 6f 63 6b 42 74 72 65 65 28 29 0a 20 20 20 20 lockBtree().
188a0 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 ** may return SQ
188b0 4c 49 54 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 LITE_OK but leav
188c0 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 73 65 e pBt->pPage1 se
188d0 74 20 74 6f 20 30 20 69 66 20 61 66 74 65 72 0a t to 0 if after.
188e0 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 ** reading p
188f0 61 67 65 20 31 20 69 74 20 64 69 73 63 6f 76 65 age 1 it discove
18900 72 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 rs that the page
18910 2d 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 -size of the dat
18920 61 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 abase . ** fi
18930 6c 65 20 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 le is not pBt->p
18940 61 67 65 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 ageSize. In this
18950 20 63 61 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 case lockBtree(
18960 29 20 77 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 ) will update.
18970 20 20 2a 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 ** pBt->pageSi
18980 7a 65 20 74 6f 20 74 68 65 20 70 61 67 65 2d 73 ze to the page-s
18990 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
189a0 6f 6e 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a on disk.. */.
189b0 20 20 20 20 77 68 69 6c 65 28 20 70 42 74 2d 3e while( pBt->
189c0 70 50 61 67 65 31 3d 3d 30 20 26 26 20 53 51 4c pPage1==0 && SQL
189d0 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f ITE_OK==(rc = lo
189e0 63 6b 42 74 72 65 65 28 70 42 74 29 29 20 29 3b ckBtree(pBt)) );
189f0 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 .. if( rc==SQ
18a00 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 LITE_OK && wrfla
18a10 67 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 g ){. if( (
18a20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
18a30 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d BTS_READ_ONLY)!=
18a40 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
18a50 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c = SQLITE_READONL
18a60 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a Y;. }else{.
18a70 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
18a80 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 ite3PagerBegin(p
18a90 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 66 6c 61 Bt->pPager,wrfla
18aa0 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 6d 70 49 g>1,sqlite3TempI
18ab0 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 29 29 3b nMemory(p->db));
18ac0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
18ad0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
18ae0 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 rc = new
18af0 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 Database(pBt);.
18b00 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
18b10 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 . }. . if
18b20 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
18b30 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 ){. unlockB
18b40 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
18b50 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c );. }. }whil
18b60 65 28 20 28 72 63 26 30 78 46 46 29 3d 3d 53 51 e( (rc&0xFF)==SQ
18b70 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 LITE_BUSY && pBt
18b80 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
18b90 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 =TRANS_NONE &&.
18ba0 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e btreeIn
18bb0 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 vokeBusyHandler(
18bc0 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 pBt) );.. if( r
18bd0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
18be0 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 if( p->inTra
18bf0 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ns==TRANS_NONE )
18c00 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 {. pBt->nTr
18c10 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 ansaction++;.#if
18c20 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18c30 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
18c40 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 if( p->shara
18c50 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ble ){. a
18c60 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 ssert( p->lock.p
18c70 42 74 72 65 65 3d 3d 70 20 26 26 20 70 2d 3e 6c Btree==p && p->l
18c80 6f 63 6b 2e 69 54 61 62 6c 65 3d 3d 31 20 29 3b ock.iTable==1 );
18c90 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b . p->lock
18ca0 2e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f .eLock = READ_LO
18cb0 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c CK;. p->l
18cc0 6f 63 6b 2e 70 4e 65 78 74 20 3d 20 70 42 74 2d ock.pNext = pBt-
18cd0 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 >pLock;.
18ce0 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d pBt->pLock = &p-
18cf0 3e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 >lock;. }.#
18d00 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
18d10 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 p->inTrans = (wr
18d20 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 flag?TRANS_WRITE
18d30 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 :TRANS_READ);.
18d40 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
18d50 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
18d60 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ion ){. pBt
18d70 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
18d80 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 = p->inTrans;.
18d90 20 20 7d 0a 20 20 20 20 69 66 28 20 77 72 66 6c }. if( wrfl
18da0 61 67 20 29 7b 0a 20 20 20 20 20 20 4d 65 6d 50 ag ){. MemP
18db0 61 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42 age *pPage1 = pB
18dc0 74 2d 3e 70 50 61 67 65 31 3b 0a 23 69 66 6e 64 t->pPage1;.#ifnd
18dd0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
18de0 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 HARED_CACHE.
18df0 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
18e00 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 pWriter );.
18e10 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 pBt->pWriter =
18e20 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 p;. pBt->bt
18e30 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 45 sFlags &= ~BTS_E
18e40 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 XCLUSIVE;.
18e50 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 20 70 if( wrflag>1 ) p
18e60 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 Bt->btsFlags |=
18e70 42 54 53 5f 45 58 43 4c 55 53 49 56 45 3b 0a 23 BTS_EXCLUSIVE;.#
18e80 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 endif.. /*
18e90 49 66 20 74 68 65 20 64 62 2d 73 69 7a 65 20 68 If the db-size h
18ea0 65 61 64 65 72 20 66 69 65 6c 64 20 69 73 20 69 eader field is i
18eb0 6e 63 6f 72 72 65 63 74 20 28 61 73 20 69 74 20 ncorrect (as it
18ec0 6d 61 79 20 62 65 20 69 66 20 61 6e 20 6f 6c 64 may be if an old
18ed0 0a 20 20 20 20 20 20 2a 2a 20 63 6c 69 65 6e 74 . ** client
18ee0 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 69 6e has been writin
18ef0 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 g the database f
18f00 69 6c 65 29 2c 20 75 70 64 61 74 65 20 69 74 20 ile), update it
18f10 6e 6f 77 2e 20 44 6f 69 6e 67 0a 20 20 20 20 20 now. Doing.
18f20 20 2a 2a 20 74 68 69 73 20 73 6f 6f 6e 65 72 20 ** this sooner
18f30 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 rather than late
18f40 72 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 r means the data
18f50 62 61 73 65 20 73 69 7a 65 20 63 61 6e 20 73 61 base size can sa
18f60 66 65 6c 79 20 0a 20 20 20 20 20 20 2a 2a 20 72 fely . ** r
18f70 65 2d 72 65 61 64 20 74 68 65 20 64 61 74 61 62 e-read the datab
18f80 61 73 65 20 73 69 7a 65 20 66 72 6f 6d 20 70 61 ase size from pa
18f90 67 65 20 31 20 69 66 20 61 20 73 61 76 65 70 6f ge 1 if a savepo
18fa0 69 6e 74 20 6f 72 20 74 72 61 6e 73 61 63 74 69 int or transacti
18fb0 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72 6f 6c 6c on. ** roll
18fc0 62 61 63 6b 20 6f 63 63 75 72 73 20 77 69 74 68 back occurs with
18fd0 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 in the transacti
18fe0 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 on.. */.
18ff0 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 if( pBt->nPag
19000 65 21 3d 67 65 74 34 62 79 74 65 28 26 70 50 61 e!=get4byte(&pPa
19010 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d 29 20 ge1->aData[28])
19020 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
19030 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
19040 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 e(pPage1->pDbPag
19050 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
19060 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
19070 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
19080 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
19090 74 61 5b 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 ta[28], pBt->nPa
190a0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ge);. }.
190b0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
190c0 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a ...trans_begun:.
190d0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
190e0 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b _OK && wrflag ){
190f0 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c . /* This cal
19100 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 l makes sure tha
19110 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 t the pager has
19120 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 the correct numb
19130 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 er of. ** ope
19140 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 n savepoints. If
19150 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
19160 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72 meter is greater
19170 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 than 0 and.
19180 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e ** the sub-journ
19190 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 al is not alread
191a0 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 y open, then it
191b0 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 will be opened h
191c0 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ere.. */.
191d0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
191e0 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 rOpenSavepoint(p
191f0 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 Bt->pPager, p->d
19200 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a b->nSavepoint);.
19210 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 }.. btreeInte
19220 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 grity(p);. sqli
19230 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
19240 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
19250 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
19260 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
19270 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
19280 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
19290 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c ies for all chil
192a0 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 dren of page pPa
192b0 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 ge. Also, if.**
192c0 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 pPage contains c
192d0 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 ells that point
192e0 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
192f0 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 s, set the point
19300 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 er.** map entrie
19310 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c s for the overfl
19320 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c ow pages as well
19330 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
19340 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
19350 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
19360 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
19370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19380 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
19390 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 variable */. i
193a0 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 nt nCell;
193b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
193d0 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 ells in page pPa
193e0 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ge */. int rc;
193f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
19410 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 turn code */. B
19420 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
19430 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 Page->pBt;. u8
19440 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 isInitOrig = pPa
19450 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 ge->isInit;. Pg
19460 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d no pgno = pPage-
19470 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 >pgno;.. assert
19480 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
19490 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
194a0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
194b0 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 = btreeInitPage(
194c0 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 pPage);. if( rc
194d0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
194e0 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c goto set_chil
194f0 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 d_ptrmaps_out;.
19500 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 }. nCell = pPa
19510 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f ge->nCell;.. fo
19520 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
19530 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 i++){. u8 *pC
19540 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
19550 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70 Page, i);.. p
19560 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
19570 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72 pPage, pCell, &r
19580 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 50 c);.. if( !pP
19590 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
195a0 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e Pgno childPgn
195b0 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 o = get4byte(pCe
195c0 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 ll);. ptrma
195d0 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 pPut(pBt, childP
195e0 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
195f0 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 E, pgno, &rc);.
19600 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
19610 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
19620 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 Pgno childPg
19630 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
19640 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
19650 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
19660 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ;. ptrmapPut(
19670 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 pBt, childPgno,
19680 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 PTRMAP_BTREE, pg
19690 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 no, &rc);. }..s
196a0 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 et_child_ptrmaps
196b0 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 _out:. pPage->i
196c0 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 sInit = isInitOr
196d0 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ig;. return rc;
196e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 .}../*.** Somewh
196f0 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73 20 ere on pPage is
19700 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 a pointer to pag
19710 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 e iFrom. Modify
19720 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f this pointer so
19730 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e .** that it poin
19740 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d ts to iTo. Param
19750 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72 eter eType descr
19760 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 ibes the type of
19770 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 pointer to.** b
19780 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 20 e modified, as
19790 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 follows:.**.** P
197a0 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 TRMAP_BTREE:
197b0 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 pPage is a btre
197c0 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e e-page. The poin
197d0 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 ter points at a
197e0 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 child .**
197f0 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 page
19800 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a of pPage..**.**
19810 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
19820 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74 1: pPage is a bt
19830 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f ree-page. The po
19840 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 inter points at
19850 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 an overflow.**
19860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19870 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
19880 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 by one of the c
19890 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a ells on pPage..*
198a0 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 *.** PTRMAP_OVER
198b0 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 FLOW2: pPage is
198c0 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 an overflow-page
198d0 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
198e0 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 ints at the next
198f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
19900 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 overflow p
19910 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e age in the list.
19920 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
19930 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 odifyPagePointer
19940 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
19950 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e Pgno iFrom, Pgn
19960 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 o iTo, u8 eType)
19970 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
19980 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
19990 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
199a0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
199b0 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
199c0 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
199d0 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 bPage) );. if(
199e0 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
199f0 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f ERFLOW2 ){. /
19a00 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 * The pointer is
19a10 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 always the firs
19a20 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 65 t 4 bytes of the
19a30 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 page in this ca
19a40 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 se. */. if(
19a50 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e get4byte(pPage->
19a60 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b aData)!=iFrom ){
19a70 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
19a80 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
19a90 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 T;. }. put
19aa0 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
19ab0 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 ta, iTo);. }els
19ac0 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74 e{. u8 isInit
19ad0 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 Orig = pPage->is
19ae0 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b Init;. int i;
19af0 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a . int nCell;.
19b00 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 . btreeInitPa
19b10 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 6e ge(pPage);. n
19b20 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
19b30 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d ell;.. for(i=
19b40 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
19b50 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c {. u8 *pCel
19b60 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
19b70 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 ge, i);. if
19b80 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
19b90 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20 OVERFLOW1 ){.
19ba0 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e CellInfo in
19bb0 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 fo;. btre
19bc0 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
19bd0 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
19be0 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o);. if(
19bf0 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 0a 20 info.iOverflow.
19c00 20 20 20 20 20 20 20 20 26 26 20 70 43 65 6c 6c && pCell
19c10 2b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 2b +info.iOverflow+
19c20 33 3c 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 2b 3<=pPage->aData+
19c30 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 0a pPage->maskPage.
19c40 20 20 20 20 20 20 20 20 20 26 26 20 69 46 72 6f && iFro
19c50 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 m==get4byte(&pCe
19c60 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
19c70 77 5d 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 w]). ){.
19c80 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
19c90 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f e(&pCell[info.iO
19ca0 76 65 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a verflow], iTo);.
19cb0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
19cc0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
19cd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
19ce0 69 66 28 20 67 65 74 34 62 79 74 65 28 70 43 65 if( get4byte(pCe
19cf0 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 ll)==iFrom ){.
19d00 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
19d10 28 70 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 (pCell, iTo);.
19d20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
19d30 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
19d40 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 . }. . if
19d50 28 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 ( i==nCell ){.
19d60 20 20 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 if( eType!=P
19d70 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a TRMAP_BTREE || .
19d80 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62 79 get4by
19d90 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
19da0 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
19db0 74 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a t+8])!=iFrom ){.
19dc0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
19dd0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
19de0 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 PT;. }.
19df0 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 put4byte(&pPag
19e00 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
19e10 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 hdrOffset+8], iT
19e20 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 o);. }.. p
19e30 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 Page->isInit = i
19e40 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 sInitOrig;. }.
19e50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
19e60 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 K;.}.../*.** Mov
19e70 65 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 e the open datab
19e80 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65 ase page pDbPage
19e90 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 to location iFr
19ea0 65 65 50 61 67 65 20 69 6e 20 74 68 65 20 0a 2a eePage in the .*
19eb0 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 * database. The
19ec0 70 44 62 50 61 67 65 20 72 65 66 65 72 65 6e 63 pDbPage referenc
19ed0 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e e remains valid.
19ee0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d .**.** The isCom
19ef0 6d 69 74 20 66 6c 61 67 20 69 6e 64 69 63 61 74 mit flag indicat
19f00 65 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 es that there is
19f10 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 no need to reme
19f20 6d 62 65 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 mber that.** the
19f30 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 journal needs t
19f40 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 o be sync()ed be
19f50 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 fore database pa
19f60 67 65 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f ge pDbPage->pgno
19f70 20 0a 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 .** can be writ
19f80 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c ten to. The call
19f90 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 er has already p
19fa0 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 romised not to w
19fb0 72 69 74 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 rite to that.**
19fc0 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 page..*/.static
19fd0 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 int relocatePage
19fe0 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
19ff0 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t, /*
1a000 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 Btree */. MemPa
1a010 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 ge *pDbPage,
1a020 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 /* Open page
1a030 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 to move */. u8
1a040 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 eType,
1a050 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1a060 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 r map 'type' ent
1a070 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a ry for pDbPage *
1a080 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 /. Pgno iPtrPag
1a090 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e, /*
1a0a0 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 Pointer map 'pag
1a0b0 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 e-no' entry for
1a0c0 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e pDbPage */. Pgn
1a0d0 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 o iFreePage,
1a0e0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 /* The loc
1a0f0 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 ation to move pD
1a100 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e bPage to */. in
1a110 74 20 69 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 t isCommit
1a120 20 20 20 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d /* isComm
1a130 69 74 20 66 6c 61 67 20 70 61 73 73 65 64 20 74 it flag passed t
1a140 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f o sqlite3PagerMo
1a150 76 65 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d vepage */.){. M
1a160 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 65 emPage *pPtrPage
1a170 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 ; /* The page
1a180 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 that contains a
1a190 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61 pointer to pDbPa
1a1a0 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 ge */. Pgno iDb
1a1b0 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e Page = pDbPage->
1a1c0 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 pgno;. Pager *p
1a1d0 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
1a1e0 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ger;. int rc;..
1a1f0 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d assert( eType=
1a200 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
1a210 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 2 || eType==PTRM
1a220 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 AP_OVERFLOW1 ||
1a230 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54 . eType==PT
1a240 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 RMAP_BTREE || eT
1a250 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype==PTRMAP_ROOT
1a260 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 PAGE );. assert
1a270 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1a280 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1a290 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 );. assert( pD
1a2a0 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 bPage->pBt==pBt
1a2b0 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 );.. /* Move pa
1a2c0 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20 ge iDbPage from
1a2d0 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 its current loca
1a2e0 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d tion to page num
1a2f0 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f ber iFreePage */
1a300 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f 56 . TRACE(("AUTOV
1a310 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 ACUUM: Moving %d
1a320 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 64 to free page %d
1a330 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74 79 (ptr page %d ty
1a340 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 pe %d)\n", .
1a350 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 iDbPage, iFree
1a360 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Page, iPtrPage,
1a370 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 eType));. rc =
1a380 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 sqlite3PagerMove
1a390 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 62 page(pPager, pDb
1a3a0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 Page->pDbPage, i
1a3b0 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d FreePage, isComm
1a3c0 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 it);. if( rc!=S
1a3d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1a3e0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
1a3f0 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d pDbPage->pgno =
1a400 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f iFreePage;.. /
1a410 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61 73 * If pDbPage was
1a420 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74 a btree-page, t
1a430 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20 hen it may have
1a440 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f child pages and/
1a450 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 or cells. ** th
1a460 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 at point to over
1a470 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 flow pages. The
1a480 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
1a490 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 ies for all thes
1a4a0 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 e. ** pages nee
1a4b0 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e d to be changed.
1a4c0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 . **. ** If pD
1a4d0 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 bPage is an over
1a4e0 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 flow page, then
1a4f0 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 the first 4 byte
1a500 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20 s may store a.
1a510 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 ** pointer to a
1a520 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 subsequent overf
1a530 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 69 low page. If thi
1a540 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 s is the case, t
1a550 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 hen. ** the poi
1a560 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74 nter map needs t
1a570 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 72 o be updated for
1a580 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 the subsequent
1a590 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 overflow page..
1a5a0 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d */. if( eType=
1a5b0 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c =PTRMAP_BTREE ||
1a5c0 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 eType==PTRMAP_R
1a5d0 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 OOTPAGE ){. r
1a5e0 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d c = setChildPtrm
1a5f0 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20 aps(pDbPage);.
1a600 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1a610 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
1a620 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1a630 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 }else{. Pgno
1a640 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 nextOvfl = get4b
1a650 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61 yte(pDbPage->aDa
1a660 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 ta);. if( nex
1a670 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 tOvfl!=0 ){.
1a680 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
1a690 20 6e 65 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 nextOvfl, PTRMA
1a6a0 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 P_OVERFLOW2, iFr
1a6b0 65 65 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 eePage, &rc);.
1a6c0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1a6d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1a6e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1a6f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
1a700 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 61 /* Fix the data
1a710 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 base pointer on
1a720 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74 68 page iPtrPage th
1a730 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44 at pointed at iD
1a740 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 bPage so. ** th
1a750 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 at it points at
1a760 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 iFreePage. Also
1a770 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20 fix the pointer
1a780 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 map entry for.
1a790 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a ** iPtrPage.. *
1a7a0 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 /. if( eType!=P
1a7b0 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
1a7c0 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 {. rc = btree
1a7d0 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 GetPage(pBt, iPt
1a7e0 72 50 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 rPage, &pPtrPage
1a7f0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
1a800 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1a810 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1a820 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
1a830 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1a840 28 70 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 (pPtrPage->pDbPa
1a850 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
1a860 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1a870 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1a880 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 pPtrPage);.
1a890 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1a8a0 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 }. rc = modif
1a8b0 79 50 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 yPagePointer(pPt
1a8c0 72 50 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 rPage, iDbPage,
1a8d0 69 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 iFreePage, eType
1a8e0 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 );. releasePa
1a8f0 67 65 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 ge(pPtrPage);.
1a900 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1a910 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 74 72 _OK ){. ptr
1a920 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 mapPut(pBt, iFre
1a930 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 ePage, eType, iP
1a940 74 72 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 trPage, &rc);.
1a950 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1a960 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 rc;.}../* Forwa
1a970 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 rd declaration r
1a980 65 71 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 equired by incrV
1a990 61 63 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a acuumStep(). */.
1a9a0 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 static int alloc
1a9b0 61 74 65 42 74 72 65 65 50 61 67 65 28 42 74 53 ateBtreePage(BtS
1a9c0 68 61 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 hared *, MemPage
1a9d0 20 2a 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e **, Pgno *, Pgn
1a9e0 6f 2c 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 o, u8);../*.** P
1a9f0 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 erform a single
1aa00 73 74 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 step of an incre
1aa10 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 mental-vacuum. I
1aa20 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 f successful, re
1aa30 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f turn.** SQLITE_O
1aa40 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e K. If there is n
1aa50 6f 20 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e o work to do (an
1aa60 64 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 20 70 d therefore no p
1aa70 6f 69 6e 74 20 69 6e 20 0a 2a 2a 20 63 61 6c 6c oint in .** call
1aa80 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
1aa90 6e 20 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e n again), return
1aaa0 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 4f 72 SQLITE_DONE. Or
1aab0 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a , if an error .*
1aac0 2a 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e * occurs, return
1aad0 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
1aae0 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4d 6f r code..**.** Mo
1aaf0 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c 79 2c re specifically,
1ab00 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
1ab10 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 ttempts to re-or
1ab20 67 61 6e 69 7a 65 20 74 68 65 20 64 61 74 61 62 ganize the datab
1ab30 61 73 65 20 73 6f 20 0a 2a 2a 20 74 68 61 74 20 ase so .** that
1ab40 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 the last page of
1ab50 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e the file curren
1ab60 74 6c 79 20 69 6e 20 75 73 65 20 69 73 20 6e 6f tly in use is no
1ab70 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a longer in use..
1ab80 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 **.** Parameter
1ab90 6e 46 69 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 nFin is the numb
1aba0 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 er of pages that
1abb0 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 77 this database w
1abc0 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 ould contain.**
1abd0 77 65 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 were this functi
1abe0 6f 6e 20 63 61 6c 6c 65 64 20 75 6e 74 69 6c 20 on called until
1abf0 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 it returns SQLIT
1ac00 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 E_DONE..**.** If
1ac10 20 74 68 65 20 62 43 6f 6d 6d 69 74 20 70 61 72 the bCommit par
1ac20 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
1ac30 72 6f 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f ro, this functio
1ac40 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 n assumes that t
1ac50 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 77 69 he .** caller wi
1ac60 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20 ll keep calling
1ac70 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 29 incrVacuumStep()
1ac80 20 75 6e 74 69 6c 20 69 74 20 72 65 74 75 72 6e until it return
1ac90 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 2a s SQLITE_DONE .*
1aca0 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 2e 20 62 * or an error. b
1acb0 43 6f 6d 6d 69 74 20 69 73 20 70 61 73 73 65 64 Commit is passed
1acc0 20 74 72 75 65 20 66 6f 72 20 61 6e 20 61 75 74 true for an aut
1acd0 6f 2d 76 61 63 75 75 6d 2d 6f 6e 2d 63 6f 6d 6d o-vacuum-on-comm
1ace0 69 74 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e it .** operation
1acf0 2c 20 6f 72 20 66 61 6c 73 65 20 66 6f 72 20 61 , or false for a
1ad00 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 n incremental va
1ad10 63 75 75 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cuum..*/.static
1ad20 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 int incrVacuumSt
1ad30 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ep(BtShared *pBt
1ad40 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e , Pgno nFin, Pgn
1ad50 6f 20 69 4c 61 73 74 50 67 2c 20 69 6e 74 20 62 o iLastPg, int b
1ad60 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 6e 6f 20 Commit){. Pgno
1ad70 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20 nFreeList;
1ad80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1ad90 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e f pages still on
1ada0 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a the free-list *
1adb0 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 /. int rc;.. a
1adc0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1add0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1ade0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1adf0 74 28 20 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 t( iLastPg>nFin
1ae00 29 3b 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 );.. if( !PTRMA
1ae10 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c P_ISPAGE(pBt, iL
1ae20 61 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 astPg) && iLastP
1ae30 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f g!=PENDING_BYTE_
1ae40 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
1ae50 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 50 u8 eType;. P
1ae60 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a 0a 20 gno iPtrPage;..
1ae70 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d 20 67 nFreeList = g
1ae80 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
1ae90 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 age1->aData[36])
1aea0 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 4c ;. if( nFreeL
1aeb0 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ist==0 ){.
1aec0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
1aed0 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 NE;. }.. r
1aee0 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 c = ptrmapGet(pB
1aef0 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 65 54 79 t, iLastPg, &eTy
1af00 70 65 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a pe, &iPtrPage);.
1af10 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1af20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1af30 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1af40 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
1af50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
1af60 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1af70 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1af80 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 PT;. }.. i
1af90 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
1afa0 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 _FREEPAGE ){.
1afb0 20 20 20 69 66 28 20 62 43 6f 6d 6d 69 74 3d 3d if( bCommit==
1afc0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
1afd0 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 Remove the page
1afe0 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66 from the files f
1aff0 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 ree-list. This i
1b000 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20 s not required.
1b010 20 20 20 20 20 20 20 2a 2a 20 69 66 20 62 43 6f ** if bCo
1b020 6d 6d 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f mmit is non-zero
1b030 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 . In that case,
1b040 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 the free-list wi
1b050 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a ll be. **
1b060 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 truncated to ze
1b070 72 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 ro after this fu
1b080 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 nction returns,
1b090 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 so it doesn't .
1b0a0 20 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 ** matter
1b0b0 20 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e if it still con
1b0c0 74 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 tains some garba
1b0d0 67 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 ge entries..
1b0e0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 */. P
1b0f0 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 gno iFreePg;.
1b100 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 MemPage *pF
1b110 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 reePg;. r
1b120 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
1b130 65 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 ePage(pBt, &pFre
1b140 65 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 ePg, &iFreePg, i
1b150 4c 61 73 74 50 67 2c 20 42 54 41 4c 4c 4f 43 5f LastPg, BTALLOC_
1b160 45 58 41 43 54 29 3b 0a 20 20 20 20 20 20 20 20 EXACT);.
1b170 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1b180 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
1b190 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
1b1a0 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 }. asse
1b1b0 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 rt( iFreePg==iLa
1b1c0 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20 20 stPg );.
1b1d0 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 releasePage(pFre
1b1e0 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ePg);. }.
1b1f0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
1b200 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 20 Pgno iFreePg;
1b210 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1b220 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67 65 dex of free page
1b230 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 67 to move pLastPg
1b240 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d to */. Mem
1b250 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a 20 Page *pLastPg;.
1b260 20 20 20 20 20 75 38 20 65 4d 6f 64 65 20 3d 20 u8 eMode =
1b270 42 54 41 4c 4c 4f 43 5f 41 4e 59 3b 20 20 20 2f BTALLOC_ANY; /
1b280 2a 20 4d 6f 64 65 20 70 61 72 61 6d 65 74 65 72 * Mode parameter
1b290 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 42 74 72 for allocateBtr
1b2a0 65 65 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 eePage() */.
1b2b0 20 20 50 67 6e 6f 20 69 4e 65 61 72 20 3d 20 30 Pgno iNear = 0
1b2c0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e ; /* n
1b2d0 65 61 72 62 79 20 70 61 72 61 6d 65 74 65 72 20 earby parameter
1b2e0 66 6f 72 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 for allocateBtre
1b2f0 65 50 61 67 65 28 29 20 2a 2f 0a 0a 20 20 20 20 ePage() */..
1b300 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
1b310 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 age(pBt, iLastPg
1b320 2c 20 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a , &pLastPg, 0);.
1b330 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1b340 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1b350 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1b360 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
1b370 49 66 20 62 43 6f 6d 6d 69 74 20 69 73 20 7a 65 If bCommit is ze
1b380 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 ro, this loop ru
1b390 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 ns exactly once
1b3a0 61 6e 64 20 70 61 67 65 20 70 4c 61 73 74 50 67 and page pLastPg
1b3b0 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 77 61 . ** is swa
1b3c0 70 70 65 64 20 77 69 74 68 20 74 68 65 20 66 69 pped with the fi
1b3d0 72 73 74 20 66 72 65 65 20 70 61 67 65 20 70 75 rst free page pu
1b3e0 6c 6c 65 64 20 6f 66 66 20 74 68 65 20 66 72 65 lled off the fre
1b3f0 65 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a e list.. **
1b400 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 . ** On the
1b410 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 20 other hand, if
1b420 62 43 6f 6d 6d 69 74 20 69 73 20 67 72 65 61 74 bCommit is great
1b430 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 er than zero, th
1b440 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a en keep. **
1b450 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 looping until a
1b460 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 free-page locat
1b470 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 ed within the fi
1b480 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 rst nFin pages.
1b490 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 ** of the f
1b4a0 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 ile is found..
1b4b0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
1b4c0 20 62 43 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 bCommit==0 ){.
1b4d0 20 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 42 eMode = B
1b4e0 54 41 4c 4c 4f 43 5f 4c 45 3b 0a 20 20 20 20 20 TALLOC_LE;.
1b4f0 20 20 20 69 4e 65 61 72 20 3d 20 6e 46 69 6e 3b iNear = nFin;
1b500 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
1b510 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 o {. MemP
1b520 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 age *pFreePg;.
1b530 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 rc = alloc
1b540 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
1b550 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 , &pFreePg, &iFr
1b560 65 65 50 67 2c 20 69 4e 65 61 72 2c 20 65 4d 6f eePg, iNear, eMo
1b570 64 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 de);. if(
1b580 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1b590 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 {. rele
1b5a0 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 asePage(pLastPg)
1b5b0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
1b5c0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
1b5d0 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
1b5e0 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 Page(pFreePg);.
1b5f0 20 20 20 20 20 7d 77 68 69 6c 65 28 20 62 43 6f }while( bCo
1b600 6d 6d 69 74 20 26 26 20 69 46 72 65 65 50 67 3e mmit && iFreePg>
1b610 6e 46 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 nFin );. as
1b620 73 65 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c sert( iFreePg<iL
1b630 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a astPg );. .
1b640 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 rc = reloc
1b650 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 4c 61 atePage(pBt, pLa
1b660 73 74 50 67 2c 20 65 54 79 70 65 2c 20 69 50 74 stPg, eType, iPt
1b670 72 50 61 67 65 2c 20 69 46 72 65 65 50 67 2c 20 rPage, iFreePg,
1b680 62 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 bCommit);.
1b690 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 releasePage(pLas
1b6a0 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 tPg);. if(
1b6b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1b6c0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1b6d0 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1b6e0 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 62 43 6f }. }.. if( bCo
1b6f0 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 mmit==0 ){. d
1b700 6f 20 7b 0a 20 20 20 20 20 20 69 4c 61 73 74 50 o {. iLastP
1b710 67 2d 2d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 g--;. }while(
1b720 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e iLastPg==PENDIN
1b730 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
1b740 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47 || PTRMAP_ISPAG
1b750 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 E(pBt, iLastPg)
1b760 29 3b 0a 20 20 20 20 70 42 74 2d 3e 62 44 6f 54 );. pBt->bDoT
1b770 72 75 6e 63 61 74 65 20 3d 20 31 3b 0a 20 20 20 runcate = 1;.
1b780 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 69 4c pBt->nPage = iL
1b790 61 73 74 50 67 3b 0a 20 20 7d 0a 20 20 72 65 74 astPg;. }. ret
1b7a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1b7b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 61 74 61 ../*.** The data
1b7c0 62 61 73 65 20 6f 70 65 6e 65 64 20 62 79 20 74 base opened by t
1b7d0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1b7e0 74 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 t is an auto-vac
1b7f0 75 75 6d 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 uum database.**
1b800 6e 4f 72 69 67 20 70 61 67 65 73 20 69 6e 20 73 nOrig pages in s
1b810 69 7a 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e ize containing n
1b820 46 72 65 65 20 66 72 65 65 20 70 61 67 65 73 2e Free free pages.
1b830 20 52 65 74 75 72 6e 20 74 68 65 20 65 78 70 65 Return the expe
1b840 63 74 65 64 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 cted .** size of
1b850 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e the database in
1b860 20 70 61 67 65 73 20 66 6f 6c 6c 6f 77 69 6e 67 pages following
1b870 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 an auto-vacuum
1b880 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 operation..*/.st
1b890 61 74 69 63 20 50 67 6e 6f 20 66 69 6e 61 6c 44 atic Pgno finalD
1b8a0 62 53 69 7a 65 28 42 74 53 68 61 72 65 64 20 2a bSize(BtShared *
1b8b0 70 42 74 2c 20 50 67 6e 6f 20 6e 4f 72 69 67 2c pBt, Pgno nOrig,
1b8c0 20 50 67 6e 6f 20 6e 46 72 65 65 29 7b 0a 20 20 Pgno nFree){.
1b8d0 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 int nEntry;
1b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b8f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
1b900 72 69 65 73 20 6f 6e 20 6f 6e 65 20 70 74 72 6d ries on one ptrm
1b910 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e ap page */. Pgn
1b920 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20 20 20 20 o nPtrmap;
1b930 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b940 4e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 70 Number of PtrMap
1b950 20 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 65 pages to be fre
1b960 65 64 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46 69 ed */. Pgno nFi
1b970 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
1b980 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
1b990 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 6e 45 n value */.. nE
1b9a0 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 ntry = pBt->usab
1b9b0 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 6e 50 74 72 leSize/5;. nPtr
1b9c0 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 map = (nFree-nOr
1b9d0 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ig+PTRMAP_PAGENO
1b9e0 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e (pBt, nOrig)+nEn
1b9f0 74 72 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 6e try)/nEntry;. n
1ba00 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 Fin = nOrig - nF
1ba10 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20 ree - nPtrmap;.
1ba20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e 44 49 if( nOrig>PENDI
1ba30 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1ba40 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44 49 4e ) && nFin<PENDIN
1ba50 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
1ba60 20 29 7b 0a 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a ){. nFin--;.
1ba70 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 50 54 52 }. while( PTR
1ba80 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
1ba90 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 nFin) || nFin==P
1baa0 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
1bab0 28 70 42 74 29 20 29 7b 0a 20 20 20 20 6e 46 69 (pBt) ){. nFi
1bac0 6e 2d 2d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 n--;. }.. retu
1bad0 72 6e 20 6e 46 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a rn nFin;.}../*.*
1bae0 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 * A write-transa
1baf0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 ction must be op
1bb00 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c ened before call
1bb10 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
1bb20 6e 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d n..** It perform
1bb30 73 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 s a single unit
1bb40 6f 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 of work towards
1bb50 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 an incremental v
1bb60 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 acuum..**.** If
1bb70 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 the incremental
1bb80 76 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73 68 vacuum is finish
1bb90 65 64 20 61 66 74 65 72 20 74 68 69 73 20 66 75 ed after this fu
1bba0 6e 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a nction has run,.
1bbb0 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 ** SQLITE_DONE i
1bbc0 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 s returned. If i
1bbd0 74 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 t is not finishe
1bbe0 64 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 d, but no error
1bbf0 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c occurred,.** SQL
1bc00 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
1bc10 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e ed. Otherwise an
1bc20 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f SQLite error co
1bc30 64 65 2e 20 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 de. .*/.int sqli
1bc40 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 te3BtreeIncrVacu
1bc50 75 6d 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 um(Btree *p){.
1bc60 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
1bc70 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1bc80 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
1bc90 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 eEnter(p);. ass
1bca0 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
1bcb0 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
1bcc0 52 49 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 RITE && p->inTra
1bcd0 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
1bce0 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 );. if( !pBt->a
1bcf0 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
1bd00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e rc = SQLITE_DON
1bd10 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 E;. }else{.
1bd20 50 67 6e 6f 20 6e 4f 72 69 67 20 3d 20 62 74 72 Pgno nOrig = btr
1bd30 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 eePagecount(pBt)
1bd40 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 ;. Pgno nFree
1bd50 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 = get4byte(&pBt
1bd60 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
1bd70 33 36 5d 29 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 36]);. Pgno n
1bd80 46 69 6e 20 3d 20 66 69 6e 61 6c 44 62 53 69 7a Fin = finalDbSiz
1bd90 65 28 70 42 74 2c 20 6e 4f 72 69 67 2c 20 6e 46 e(pBt, nOrig, nF
1bda0 72 65 65 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e ree);.. if( n
1bdb0 4f 72 69 67 3c 6e 46 69 6e 20 29 7b 0a 20 20 20 Orig<nFin ){.
1bdc0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1bdd0 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1bde0 20 7d 65 6c 73 65 20 69 66 28 20 6e 46 72 65 65 }else if( nFree
1bdf0 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d >0 ){. rc =
1be00 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
1be10 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 pBt, 0, 0);.
1be20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1be30 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 _OK ){. i
1be40 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
1be50 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a flowCache(pBt);.
1be60 20 20 20 20 20 20 20 20 72 63 20 3d 20 69 6e 63 rc = inc
1be70 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 2c rVacuumStep(pBt,
1be80 20 6e 46 69 6e 2c 20 6e 4f 72 69 67 2c 20 30 29 nFin, nOrig, 0)
1be90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1bea0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1beb0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
1bec0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1bed0 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
1bee0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
1bef0 20 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 put4byte(&pBt
1bf00 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
1bf10 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 28], pBt->nPage)
1bf20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
1bf30 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1bf40 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
1bf50 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
1bf60 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1bf70 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1bf80 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1bf90 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72 69 6f e is called prio
1bfa0 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 r to sqlite3Page
1bfb0 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 rCommit when a t
1bfc0 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 ransaction.** is
1bfd0 20 63 6f 6d 6d 69 74 74 65 64 20 66 6f 72 20 61 committed for a
1bfe0 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 n auto-vacuum da
1bff0 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 tabase..**.** If
1c000 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1c010 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e turned, then *pn
1c020 54 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f 20 Trunc is set to
1c030 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
1c040 67 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ges.** the datab
1c050 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 ase file should
1c060 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 be truncated to
1c070 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 during the commi
1c080 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 t process. .** i
1c090 2e 65 2e 20 74 68 65 20 64 61 74 61 62 61 73 65 .e. the database
1c0a0 20 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67 61 has been reorga
1c0b0 6e 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f 6e nized so that on
1c0c0 6c 79 20 74 68 65 20 66 69 72 73 74 20 2a 70 6e ly the first *pn
1c0d0 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 Trunc.** pages a
1c0e0 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 re in use..*/.st
1c0f0 61 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61 63 atic int autoVac
1c100 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 uumCommit(BtShar
1c110 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 ed *pBt){. int
1c120 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1c130 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
1c140 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 = pBt->pPager;.
1c150 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e VVA_ONLY( int n
1c160 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 67 Ref = sqlite3Pag
1c170 65 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 erRefcount(pPage
1c180 72 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 r) );.. assert(
1c190 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1c1a0 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
1c1b0 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 41 );. invalidateA
1c1c0 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 llOverflowCache(
1c1d0 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 70 pBt);. assert(p
1c1e0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b Bt->autoVacuum);
1c1f0 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 . if( !pBt->inc
1c200 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 rVacuum ){. P
1c210 67 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 gno nFin;
1c220 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
1c230 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 ages in database
1c240 20 61 66 74 65 72 20 61 75 74 6f 76 61 63 75 75 after autovacuu
1c250 6d 69 6e 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f ming */. Pgno
1c260 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f nFree; /
1c270 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
1c280 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 s on the freelis
1c290 74 20 69 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 t initially */.
1c2a0 20 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 Pgno iFree;
1c2b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 /* The nex
1c2c0 74 20 70 61 67 65 20 74 6f 20 62 65 20 66 72 65 t page to be fre
1c2d0 65 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e ed */. Pgno n
1c2e0 4f 72 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 Orig; /*
1c2f0 44 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 Database size be
1c300 66 6f 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a fore freeing */.
1c310 0a 20 20 20 20 6e 4f 72 69 67 20 3d 20 62 74 72 . nOrig = btr
1c320 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 eePagecount(pBt)
1c330 3b 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 ;. if( PTRMAP
1c340 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 _ISPAGE(pBt, nOr
1c350 69 67 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 ig) || nOrig==PE
1c360 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1c370 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a pBt) ){. /*
1c380 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
1c390 62 6c 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 ble to create a
1c3a0 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 database for whi
1c3b0 63 68 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 ch the final pag
1c3c0 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 e. ** is ei
1c3d0 74 68 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d ther a pointer-m
1c3e0 61 70 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 ap page or the p
1c3f0 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 ending-byte page
1c400 2e 20 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a . If one. *
1c410 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 * is encountered
1c420 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 , this indicates
1c430 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 corruption..
1c440 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 */. retu
1c450 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1c460 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 T_BKPT;. }..
1c470 20 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 nFree = get4b
1c480 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
1c490 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 ->aData[36]);.
1c4a0 20 20 6e 46 69 6e 20 3d 20 66 69 6e 61 6c 44 62 nFin = finalDb
1c4b0 53 69 7a 65 28 70 42 74 2c 20 6e 4f 72 69 67 2c Size(pBt, nOrig,
1c4c0 20 6e 46 72 65 65 29 3b 0a 20 20 20 20 69 66 28 nFree);. if(
1c4d0 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 nFin>nOrig ) re
1c4e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1c4f0 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 69 66 UPT_BKPT;. if
1c500 28 20 6e 46 69 6e 3c 6e 4f 72 69 67 20 29 7b 0a ( nFin<nOrig ){.
1c510 20 20 20 20 20 20 72 63 20 3d 20 73 61 76 65 41 rc = saveA
1c520 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 llCursors(pBt, 0
1c530 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 0);. }.
1c540 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b for(iFree=nOrig;
1c550 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 iFree>nFin && r
1c560 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 c==SQLITE_OK; iF
1c570 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 ree--){. rc
1c580 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 = incrVacuumSte
1c590 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 p(pBt, nFin, iFr
1c5a0 65 65 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 ee, 1);. }.
1c5b0 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 if( (rc==SQLIT
1c5c0 45 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 E_DONE || rc==SQ
1c5d0 4c 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 LITE_OK) && nFre
1c5e0 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 e>0 ){. rc
1c5f0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1c600 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
1c610 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
1c620 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
1c630 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
1c640 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 ], 0);. put
1c650 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 4byte(&pBt->pPag
1c660 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 e1->aData[36], 0
1c670 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 );. put4byt
1c680 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
1c690 61 44 61 74 61 5b 32 38 5d 2c 20 6e 46 69 6e 29 aData[28], nFin)
1c6a0 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 44 6f ;. pBt->bDo
1c6b0 54 72 75 6e 63 61 74 65 20 3d 20 31 3b 0a 20 20 Truncate = 1;.
1c6c0 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d pBt->nPage =
1c6d0 20 6e 46 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 nFin;. }.
1c6e0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1c6f0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
1c700 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
1c710 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
1c720 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e }.. assert( n
1c730 52 65 66 3e 3d 73 71 6c 69 74 65 33 50 61 67 65 Ref>=sqlite3Page
1c740 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 rRefcount(pPager
1c750 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ) );. return rc
1c760 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 ;.}..#else /* if
1c770 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1c780 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 _AUTOVACUUM */.#
1c790 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64 define setChild
1c7a0 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54 Ptrmaps(x) SQLIT
1c7b0 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a E_OK.#endif../*.
1c7c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1c7d0 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 does the first p
1c7e0 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 hase of a two-ph
1c7f0 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 ase commit. Thi
1c800 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 s routine.** cau
1c810 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a ses a rollback j
1c820 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 ournal to be cre
1c830 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 ated (if it does
1c840 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
1c850 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c st).** and popul
1c860 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 ated with enough
1c870 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 information so
1c880 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 that if a power
1c890 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 loss occurs.** t
1c8a0 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 he database can
1c8b0 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 be restored to i
1c8c0 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 ts original stat
1c8d0 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 e by playing bac
1c8e0 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c k.** the journal
1c8f0 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 . Then the cont
1c900 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ents of the jour
1c910 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 nal are flushed
1c920 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 out to.** the di
1c930 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a sk. After the j
1c940 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 ournal is safely
1c950 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 on oxide, the c
1c960 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
1c970 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 database are wr
1c980 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 itten into the d
1c990 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1c9a0 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 flushed to oxid
1c9b0 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 e..** At the end
1c9c0 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 of this call, t
1c9d0 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
1c9e0 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 nal still exists
1c9f0 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 on the.** disk
1ca00 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c and we are still
1ca10 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 holding all loc
1ca20 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 ks, so the trans
1ca30 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a action has not.*
1ca40 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 * committed. Se
1ca50 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f e sqlite3BtreeCo
1ca60 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 mmitPhaseTwo() f
1ca70 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 or the second ph
1ca80 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ase of the.** co
1ca90 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a mmit process..**
1caa0 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 .** This call is
1cab0 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 a no-op if no w
1cac0 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1cad0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 is currently ac
1cae0 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a tive on pBt..**.
1caf0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 ** Otherwise, sy
1cb00 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
1cb10 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 file for the btr
1cb20 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 ee pBt. zMaster
1cb30 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 points to.** the
1cb40 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 name of a maste
1cb50 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 r journal file t
1cb60 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 hat should be wr
1cb70 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a itten into the.*
1cb80 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 * individual jou
1cb90 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 rnal file, or is
1cba0 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e NULL, indicatin
1cbb0 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 g no master jour
1cbc0 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 nal file .** (si
1cbd0 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
1cbe0 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
1cbf0 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
1cc00 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 alled, the maste
1cc10 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 r journal should
1cc20 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 already have be
1cc30 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 en.** created, p
1cc40 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 opulated with th
1cc50 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 is journal point
1cc60 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f er and synced to
1cc70 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 disk..**.** Onc
1cc80 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e e this is routin
1cc90 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 e has returned,
1cca0 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 the only thing r
1ccb0 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 equired to commi
1ccc0 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 t.** the write-t
1ccd0 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 ransaction for t
1cce0 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
1ccf0 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 e is to delete t
1cd00 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 69 he journal..*/.i
1cd10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
1cd20 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 ommitPhaseOne(Bt
1cd30 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 ree *p, const ch
1cd40 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
1cd50 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1cd60 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 OK;. if( p->inT
1cd70 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
1cd80 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 E ){. BtShare
1cd90 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1cda0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1cdb0 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 eEnter(p);.#ifnd
1cdc0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1cdd0 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
1cde0 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1cdf0 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 m ){. rc =
1ce00 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 autoVacuumCommit
1ce10 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 (pBt);. if(
1ce20 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1ce30 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1ce40 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1ce50 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
1ce60 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
1ce70 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 62 44 . if( pBt->bD
1ce80 6f 54 72 75 6e 63 61 74 65 20 29 7b 0a 20 20 20 oTruncate ){.
1ce90 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 sqlite3PagerT
1cea0 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 runcateImage(pBt
1ceb0 2d 3e 70 50 61 67 65 72 2c 20 70 42 74 2d 3e 6e ->pPager, pBt->n
1cec0 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e Page);. }.#en
1ced0 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c dif. rc = sql
1cee0 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
1cef0 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61 haseOne(pBt->pPa
1cf00 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 ger, zMaster, 0)
1cf10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1cf20 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a eeLeave(p);. }.
1cf30 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1cf40 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1cf50 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 66 72 ion is called fr
1cf60 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 43 6f 6d om both BtreeCom
1cf70 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 6e mitPhaseTwo() an
1cf80 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 d BtreeRollback(
1cf90 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 ).** at the conc
1cfa0 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 72 61 6e lusion of a tran
1cfb0 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 saction..*/.stat
1cfc0 69 63 20 76 6f 69 64 20 62 74 72 65 65 45 6e 64 ic void btreeEnd
1cfd0 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 74 72 65 Transaction(Btre
1cfe0 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
1cff0 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1d000 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1d010 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 p->db;. assert
1d020 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
1d030 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a ldsMutex(p) );..
1d040 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d050 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1d060 20 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 pBt->bDoTruncat
1d070 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 e = 0;.#endif.
1d080 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 if( p->inTrans>T
1d090 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 64 62 2d RANS_NONE && db-
1d0a0 3e 6e 56 64 62 65 52 65 61 64 3e 31 20 29 7b 0a >nVdbeRead>1 ){.
1d0b0 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
1d0c0 61 72 65 20 6f 74 68 65 72 20 61 63 74 69 76 65 are other active
1d0d0 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 statements that
1d0e0 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 69 73 20 belong to this
1d0f0 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 database. **
1d100 68 61 6e 64 6c 65 2c 20 64 6f 77 6e 67 72 61 64 handle, downgrad
1d110 65 20 74 6f 20 61 20 72 65 61 64 2d 6f 6e 6c 79 e to a read-only
1d120 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 transaction. Th
1d130 65 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e e other statemen
1d140 74 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 73 74 ts. ** may st
1d150 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20 66 ill be reading f
1d160 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
1d170 2e 20 20 2a 2f 0a 20 20 20 20 64 6f 77 6e 67 72 . */. downgr
1d180 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 adeAllSharedCach
1d190 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a eTableLocks(p);.
1d1a0 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d p->inTrans =
1d1b0 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d TRANS_READ;. }
1d1c0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 else{. /* If
1d1d0 74 68 65 20 68 61 6e 64 6c 65 20 68 61 64 20 61 the handle had a
1d1e0 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 ny kind of trans
1d1f0 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 action open, dec
1d200 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 20 20 rement the .
1d210 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 ** transaction c
1d220 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61 72 ount of the shar
1d230 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68 65 ed btree. If the
1d240 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 transaction cou
1d250 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 61 63 68 nt . ** reach
1d260 65 73 20 30 2c 20 73 65 74 20 74 68 65 20 73 68 es 0, set the sh
1d270 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 ared state to TR
1d280 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e ANS_NONE. The un
1d290 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 lockBtreeIfUnuse
1d2a0 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 d(). ** call
1d2b0 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f 63 below will unloc
1d2c0 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20 2a 2f k the pager. */
1d2d0 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 . if( p->inTr
1d2e0 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ans!=TRANS_NONE
1d2f0 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 72 41 6c ){. clearAl
1d300 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
1d310 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 20 eLocks(p);.
1d320 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
1d330 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 on--;. if(
1d340 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 0==pBt->nTransac
1d350 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 tion ){.
1d360 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
1d370 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b on = TRANS_NONE;
1d380 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1d390 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63 /* Set the c
1d3a0 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
1d3b0 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e on state to TRAN
1d3c0 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 S_NONE and unloc
1d3d0 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 61 k the . ** pa
1d3e0 67 65 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c ger if this call
1d3f0 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 closed the only
1d400 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
1d410 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a ransaction. */.
1d420 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d p->inTrans =
1d430 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 TRANS_NONE;.
1d440 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
1d450 75 73 65 64 28 70 42 74 29 3b 0a 20 20 7d 0a 0a used(pBt);. }..
1d460 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 btreeIntegrity
1d470 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f (p);.}../*.** Co
1d480 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 mmit the transac
1d490 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 tion currently i
1d4a0 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a n progress..**.*
1d4b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1d4c0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 mplements the se
1d4d0 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 cond phase of a
1d4e0 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 2-phase commit.
1d4f0 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 The.** sqlite3B
1d500 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
1d510 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 ne() routine doe
1d520 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61 73 s the first phas
1d530 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 e and should.**
1d540 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 be invoked prior
1d550 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 to calling this
1d560 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 routine. The s
1d570 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1d580 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 tPhaseOne().** r
1d590 6f 75 74 69 6e 65 20 64 69 64 20 61 6c 6c 20 74 outine did all t
1d5a0 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 he work of writi
1d5b0 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f ng information o
1d5c0 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 ut to disk and f
1d5d0 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 lushing the.** c
1d5e0 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 61 74 20 ontents so that
1d5f0 74 68 65 79 20 61 72 65 20 77 72 69 74 74 65 6e they are written
1d600 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 6b 20 70 onto the disk p
1d610 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 latter. All thi
1d620 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 s.** routine has
1d630 20 74 6f 20 64 6f 20 69 73 20 64 65 6c 65 74 65 to do is delete
1d640 20 6f 72 20 74 72 75 6e 63 61 74 65 20 6f 72 20 or truncate or
1d650 7a 65 72 6f 20 74 68 65 20 68 65 61 64 65 72 20 zero the header
1d660 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f in the.** the ro
1d670 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 llback journal (
1d680 77 68 69 63 68 20 63 61 75 73 65 73 20 74 68 65 which causes the
1d690 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
1d6a0 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64 commit) and.** d
1d6b0 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a rop locks..**.**
1d6c0 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 20 61 6e Normally, if an
1d6d0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
1d6e0 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 6c 61 ile the pager la
1d6f0 79 65 72 20 69 73 20 61 74 74 65 6d 70 74 69 6e yer is attemptin
1d700 67 20 74 6f 20 0a 2a 2a 20 66 69 6e 61 6c 69 7a g to .** finaliz
1d710 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 e the underlying
1d720 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 journal file, t
1d730 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
1d740 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 61 6e urns an error an
1d750 64 0a 2a 2a 20 74 68 65 20 75 70 70 65 72 20 6c d.** the upper l
1d760 61 79 65 72 20 77 69 6c 6c 20 61 74 74 65 6d 70 ayer will attemp
1d770 74 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 48 6f t a rollback. Ho
1d780 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 73 65 wever, if the se
1d790 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a cond argument.**
1d7a0 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 is non-zero the
1d7b0 6e 20 74 68 69 73 20 62 2d 74 72 65 65 20 74 72 n this b-tree tr
1d7c0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 70 61 72 ansaction is par
1d7d0 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c t of a multi-fil
1d7e0 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f e .** transactio
1d7f0 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c n. In this case,
1d800 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1d810 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1d820 6e 20 63 6f 6d 6d 69 74 74 65 64 20 0a 2a 2a 20 n committed .**
1d830 28 62 79 20 64 65 6c 65 74 69 6e 67 20 61 20 6d (by deleting a m
1d840 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1d850 6c 65 29 20 61 6e 64 20 74 68 65 20 63 61 6c 6c le) and the call
1d860 65 72 20 77 69 6c 6c 20 69 67 6e 6f 72 65 20 74 er will ignore t
1d870 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e his .** function
1d880 73 20 72 65 74 75 72 6e 20 63 6f 64 65 2e 20 53 s return code. S
1d890 6f 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 o, even if an er
1d8a0 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 ror occurs in th
1d8b0 65 20 70 61 67 65 72 20 6c 61 79 65 72 2c 0a 2a e pager layer,.*
1d8c0 2a 20 72 65 73 65 74 20 74 68 65 20 62 2d 74 72 * reset the b-tr
1d8d0 65 65 20 6f 62 6a 65 63 74 73 20 69 6e 74 65 72 ee objects inter
1d8e0 6e 61 6c 20 73 74 61 74 65 20 74 6f 20 69 6e 64 nal state to ind
1d8f0 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 77 icate that the w
1d900 72 69 74 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rite.** transact
1d910 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63 6c 6f ion has been clo
1d920 73 65 64 2e 20 54 68 69 73 20 69 73 20 71 75 69 sed. This is qui
1d930 74 65 20 73 61 66 65 2c 20 61 73 20 74 68 65 20 te safe, as the
1d940 70 61 67 65 72 20 77 69 6c 6c 20 68 61 76 65 0a pager will have.
1d950 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 65 64 20 ** transitioned
1d960 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 to the error sta
1d970 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 te..**.** This w
1d980 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 ill release the
1d990 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 write lock on th
1d9a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1d9b0 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 If there.** ar
1d9c0 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 e no active curs
1d9d0 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c ors, it also rel
1d9e0 65 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c eases the read l
1d9f0 6f 63 6b 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ock..*/.int sqli
1da00 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1da10 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70 2c aseTwo(Btree *p,
1da20 20 69 6e 74 20 62 43 6c 65 61 6e 75 70 29 7b 0a int bCleanup){.
1da30 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
1da40 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 20 s==TRANS_NONE )
1da50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1da60 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1da70 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72 65 Enter(p);. btre
1da80 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a eIntegrity(p);..
1da90 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 /* If the hand
1daa0 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d 74 le has a write-t
1dab0 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c ransaction open,
1dac0 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 72 commit the shar
1dad0 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 ed-btrees . **
1dae0 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 transaction and
1daf0 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20 73 set the shared s
1db00 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 tate to TRANS_RE
1db10 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 AD.. */. if( p
1db20 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
1db30 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e _WRITE ){. in
1db40 74 20 72 63 3b 0a 20 20 20 20 42 74 53 68 61 72 t rc;. BtShar
1db50 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1db60 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
1db70 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1db80 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
1db90 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 . assert( pBt
1dba0 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 ->nTransaction>0
1dbb0 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
1dbc0 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
1dbd0 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 61 haseTwo(pBt->pPa
1dbe0 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ger);. if( rc
1dbf0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 62 !=SQLITE_OK && b
1dc00 43 6c 65 61 6e 75 70 3d 3d 30 20 29 7b 0a 20 20 Cleanup==0 ){.
1dc10 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1dc20 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 Leave(p);.
1dc30 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1dc40 0a 20 20 20 20 70 2d 3e 69 44 61 74 61 56 65 72 . p->iDataVer
1dc50 73 69 6f 6e 2d 2d 3b 20 20 2f 2a 20 43 6f 6d 70 sion--; /* Comp
1dc60 65 6e 73 61 74 65 20 66 6f 72 20 70 50 61 67 65 ensate for pPage
1dc70 72 2d 3e 69 44 61 74 61 56 65 72 73 69 6f 6e 2b r->iDataVersion+
1dc80 2b 3b 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 69 +; */. pBt->i
1dc90 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 nTransaction = T
1dca0 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20 62 RANS_READ;. b
1dcb0 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 treeClearHasCont
1dcc0 65 6e 74 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 ent(pBt);. }..
1dcd0 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 btreeEndTransac
1dce0 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 tion(p);. sqlit
1dcf0 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1dd00 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1dd10 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f _OK;.}../*.** Do
1dd20 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f 66 20 both phases of
1dd30 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 69 6e 74 a commit..*/.int
1dd40 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1dd50 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 mit(Btree *p){.
1dd60 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
1dd70 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1dd80 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1dd90 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
1dda0 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 ne(p, 0);. if(
1ddb0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1ddc0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1ddd0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
1dde0 65 54 77 6f 28 70 2c 20 30 29 3b 0a 20 20 7d 0a eTwo(p, 0);. }.
1ddf0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1de00 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1de10 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
1de20 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
1de30 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 55 52 the state to CUR
1de40 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68 SOR_FAULT and th
1de50 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
1de60 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65 to errCode for e
1de70 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 61 very cursor on a
1de80 6e 79 20 42 74 53 68 61 72 65 64 20 74 68 61 74 ny BtShared that
1de90 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 pBtree.** refer
1dea0 65 6e 63 65 73 2e 20 20 4f 72 20 69 66 20 74 68 ences. Or if th
1deb0 65 20 77 72 69 74 65 4f 6e 6c 79 20 66 6c 61 67 e writeOnly flag
1dec0 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74 68 is set to 1, th
1ded0 65 6e 20 6f 6e 6c 79 0a 2a 2a 20 74 72 69 70 20 en only.** trip
1dee0 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 6e write cursors an
1def0 64 20 6c 65 61 76 65 20 72 65 61 64 20 63 75 72 d leave read cur
1df00 73 6f 72 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a sors unchanged..
1df10 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 **.** Every curs
1df20 6f 72 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 or is a candidat
1df30 65 20 74 6f 20 62 65 20 74 72 69 70 70 65 64 2c e to be tripped,
1df40 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 6f including curso
1df50 72 73 0a 2a 2a 20 74 68 61 74 20 62 65 6c 6f 6e rs.** that belon
1df60 67 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 62 g to other datab
1df70 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
1df80 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 62 that happen to b
1df90 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74 68 65 e.** sharing the
1dfa0 20 63 61 63 68 65 20 77 69 74 68 20 70 42 74 72 cache with pBtr
1dfb0 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ee..**.** This r
1dfc0 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c 6c outine gets call
1dfd0 65 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 ed when a rollba
1dfe0 63 6b 20 6f 63 63 75 72 73 2e 20 49 66 20 74 68 ck occurs. If th
1dff0 65 20 77 72 69 74 65 4f 6e 6c 79 0a 2a 2a 20 66 e writeOnly.** f
1e000 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 lag is true, the
1e010 6e 20 6f 6e 6c 79 20 77 72 69 74 65 2d 63 75 72 n only write-cur
1e020 73 6f 72 73 20 6e 65 65 64 20 62 65 20 74 72 69 sors need be tri
1e030 70 70 65 64 20 2d 20 72 65 61 64 2d 6f 6e 6c 79 pped - read-only
1e040 0a 2a 2a 20 63 75 72 73 6f 72 73 20 73 61 76 65 .** cursors save
1e050 20 74 68 65 69 72 20 63 75 72 72 65 6e 74 20 70 their current p
1e060 6f 73 69 74 69 6f 6e 73 20 73 6f 20 74 68 61 74 ositions so that
1e070 20 74 68 65 79 20 6d 61 79 20 63 6f 6e 74 69 6e they may contin
1e080 75 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 ue .** following
1e090 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f the rollback. O
1e0a0 72 2c 20 69 66 20 77 72 69 74 65 4f 6e 6c 79 20 r, if writeOnly
1e0b0 69 73 20 66 61 6c 73 65 2c 20 61 6c 6c 20 63 75 is false, all cu
1e0c0 72 73 6f 72 73 20 61 72 65 20 0a 2a 2a 20 74 72 rsors are .** tr
1e0d0 69 70 70 65 64 2e 20 49 6e 20 67 65 6e 65 72 61 ipped. In genera
1e0e0 6c 2c 20 77 72 69 74 65 4f 6e 6c 79 20 69 73 20 l, writeOnly is
1e0f0 66 61 6c 73 65 20 69 66 20 74 68 65 20 74 72 61 false if the tra
1e100 6e 73 61 63 74 69 6f 6e 20 62 65 69 6e 67 0a 2a nsaction being.*
1e110 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 6d 6f * rolled back mo
1e120 64 69 66 69 65 64 20 74 68 65 20 64 61 74 61 62 dified the datab
1e130 61 73 65 20 73 63 68 65 6d 61 2e 20 49 6e 20 74 ase schema. In t
1e140 68 69 73 20 63 61 73 65 20 62 2d 74 72 65 65 20 his case b-tree
1e150 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 73 20 6d 61 root.** pages ma
1e160 79 20 62 65 20 6d 6f 76 65 64 20 6f 72 20 64 65 y be moved or de
1e170 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 64 leted from the d
1e180 61 74 61 62 61 73 65 20 61 6c 74 6f 67 65 74 68 atabase altogeth
1e190 65 72 2c 20 6d 61 6b 69 6e 67 0a 2a 2a 20 69 74 er, making.** it
1e1a0 20 75 6e 73 61 66 65 20 66 6f 72 20 72 65 61 64 unsafe for read
1e1b0 20 63 75 72 73 6f 72 73 20 74 6f 20 63 6f 6e 74 cursors to cont
1e1c0 69 6e 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 inue..**.** If t
1e1d0 68 65 20 77 72 69 74 65 4f 6e 6c 79 20 66 6c 61 he writeOnly fla
1e1e0 67 20 69 73 20 74 72 75 65 20 61 6e 64 20 61 6e g is true and an
1e1f0 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e error is encoun
1e200 74 65 72 65 64 20 77 68 69 6c 65 20 0a 2a 2a 20 tered while .**
1e210 73 61 76 69 6e 67 20 74 68 65 20 63 75 72 72 65 saving the curre
1e220 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 61 nt position of a
1e230 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f read-only curso
1e240 72 2c 20 61 6c 6c 20 63 75 72 73 6f 72 73 2c 20 r, all cursors,
1e250 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c .** including al
1e260 6c 20 72 65 61 64 2d 63 75 72 73 6f 72 73 20 61 l read-cursors a
1e270 72 65 20 74 72 69 70 70 65 64 2e 0a 2a 2a 0a 2a re tripped..**.*
1e280 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
1e290 65 74 75 72 6e 65 64 20 69 66 20 73 75 63 63 65 eturned if succe
1e2a0 73 73 66 75 6c 2c 20 6f 72 20 69 66 20 61 6e 20 ssful, or if an
1e2b0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
1e2c0 6c 65 0a 2a 2a 20 73 61 76 69 6e 67 20 61 20 63 le.** saving a c
1e2d0 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2c 20 ursor position,
1e2e0 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
1e2f0 63 6f 64 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c code..*/.int sql
1e300 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c ite3BtreeTripAll
1e310 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a 70 Cursors(Btree *p
1e320 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 6f Btree, int errCo
1e330 64 65 2c 20 69 6e 74 20 77 72 69 74 65 4f 6e 6c de, int writeOnl
1e340 79 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a y){. BtCursor *
1e350 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 p;. int rc = SQ
1e360 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
1e370 72 74 28 20 28 77 72 69 74 65 4f 6e 6c 79 3d 3d rt( (writeOnly==
1e380 30 20 7c 7c 20 77 72 69 74 65 4f 6e 6c 79 3d 3d 0 || writeOnly==
1e390 31 29 20 26 26 20 42 54 43 46 5f 57 72 69 74 65 1) && BTCF_Write
1e3a0 46 6c 61 67 3d 3d 31 20 29 3b 0a 20 20 69 66 28 Flag==1 );. if(
1e3b0 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20 73 pBtree ){. s
1e3c0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1e3d0 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 66 6f (pBtree);. fo
1e3e0 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d r(p=pBtree->pBt-
1e3f0 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 >pCursor; p; p=p
1e400 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
1e410 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 66 28 int i;. if(
1e420 20 77 72 69 74 65 4f 6e 6c 79 20 26 26 20 28 70 writeOnly && (p
1e430 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 ->curFlags & BTC
1e440 46 5f 57 72 69 74 65 46 6c 61 67 29 3d 3d 30 20 F_WriteFlag)==0
1e450 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1e460 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1e470 5f 56 41 4c 49 44 20 7c 7c 20 70 2d 3e 65 53 74 _VALID || p->eSt
1e480 61 74 65 3d 3d 43 55 52 53 4f 52 5f 53 4b 49 50 ate==CURSOR_SKIP
1e490 4e 45 58 54 20 29 7b 0a 20 20 20 20 20 20 20 20 NEXT ){.
1e4a0 20 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f rc = saveCurso
1e4b0 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 rPosition(p);.
1e4c0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1e4d0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1e4e0 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 (void)s
1e4f0 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
1e500 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 72 65 65 llCursors(pBtree
1e510 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 , rc, 0);.
1e520 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e530 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1e540 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1e550 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1e560 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
1e570 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 p);. p->e
1e580 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 State = CURSOR_F
1e590 41 55 4c 54 3b 0a 20 20 20 20 20 20 20 20 70 2d AULT;. p-
1e5a0 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 72 72 43 >skipNext = errC
1e5b0 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ode;. }.
1e5c0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 for(i=0; i<=p
1e5d0 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
1e5e0 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1e5f0 67 65 28 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 ge(p->apPage[i])
1e600 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 50 ;. p->apP
1e610 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 age[i] = 0;.
1e620 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
1e630 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1e640 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 pBtree);. }. r
1e650 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1e660 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 ** Rollback the
1e670 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 transaction in p
1e680 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 rogress..**.** I
1e690 66 20 74 72 69 70 43 6f 64 65 20 69 73 20 6e 6f f tripCode is no
1e6a0 74 20 53 51 4c 49 54 45 5f 4f 4b 20 74 68 65 6e t SQLITE_OK then
1e6b0 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 cursors will be
1e6c0 20 69 6e 76 61 6c 69 64 61 74 65 64 20 28 74 72 invalidated (tr
1e6d0 69 70 70 65 64 29 2e 0a 2a 2a 20 4f 6e 6c 79 20 ipped)..** Only
1e6e0 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 write cursors ar
1e6f0 65 20 74 72 69 70 70 65 64 20 69 66 20 77 72 69 e tripped if wri
1e700 74 65 4f 6e 6c 79 20 69 73 20 74 72 75 65 20 62 teOnly is true b
1e710 75 74 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 ut all cursors a
1e720 72 65 0a 2a 2a 20 74 72 69 70 70 65 64 20 69 66 re.** tripped if
1e730 20 77 72 69 74 65 4f 6e 6c 79 20 69 73 20 66 61 writeOnly is fa
1e740 6c 73 65 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 lse. Any attemp
1e750 74 20 74 6f 20 75 73 65 0a 2a 2a 20 61 20 74 72 t to use.** a tr
1e760 69 70 70 65 64 20 63 75 72 73 6f 72 20 77 69 6c ipped cursor wil
1e770 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 65 l result in an e
1e780 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rror..**.** This
1e790 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 will release th
1e7a0 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 e write lock on
1e7b0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1e7c0 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 e. If there.**
1e7d0 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 are no active cu
1e7e0 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 rsors, it also r
1e7f0 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64 eleases the read
1e800 20 6c 6f 63 6b 2e 0a 2a 2f 0a 69 6e 74 20 73 71 lock..*/.int sq
1e810 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
1e820 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ck(Btree *p, int
1e830 20 74 72 69 70 43 6f 64 65 2c 20 69 6e 74 20 77 tripCode, int w
1e840 72 69 74 65 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 riteOnly){. int
1e850 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 rc;. BtShared
1e860 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
1e870 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 MemPage *pPage1
1e880 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 77 72 69 ;.. assert( wri
1e890 74 65 4f 6e 6c 79 3d 3d 31 20 7c 7c 20 77 72 69 teOnly==1 || wri
1e8a0 74 65 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 teOnly==0 );. a
1e8b0 73 73 65 72 74 28 20 74 72 69 70 43 6f 64 65 3d ssert( tripCode=
1e8c0 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 5f 52 4f =SQLITE_ABORT_RO
1e8d0 4c 4c 42 41 43 4b 20 7c 7c 20 74 72 69 70 43 6f LLBACK || tripCo
1e8e0 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b de==SQLITE_OK );
1e8f0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1e900 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 74 nter(p);. if( t
1e910 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f ripCode==SQLITE_
1e920 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 74 OK ){. rc = t
1e930 72 69 70 43 6f 64 65 20 3d 20 73 61 76 65 41 6c ripCode = saveAl
1e940 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c lCursors(pBt, 0,
1e950 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 0);. if( rc
1e960 29 20 77 72 69 74 65 4f 6e 6c 79 20 3d 20 30 3b ) writeOnly = 0;
1e970 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
1e980 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1e990 7d 0a 20 20 69 66 28 20 74 72 69 70 43 6f 64 65 }. if( tripCode
1e9a0 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 20 ){. int rc2
1e9b0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 = sqlite3BtreeTr
1e9c0 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 ipAllCursors(p,
1e9d0 74 72 69 70 43 6f 64 65 2c 20 77 72 69 74 65 4f tripCode, writeO
1e9e0 6e 6c 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 nly);. assert
1e9f0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1ea00 7c 7c 20 28 77 72 69 74 65 4f 6e 6c 79 3d 3d 30 || (writeOnly==0
1ea10 20 26 26 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f && rc2==SQLITE_
1ea20 4f 4b 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 OK) );. if( r
1ea30 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 c2!=SQLITE_OK )
1ea40 72 63 20 3d 20 72 63 32 3b 0a 20 20 7d 0a 20 20 rc = rc2;. }.
1ea50 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
1ea60 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 );.. if( p->inT
1ea70 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
1ea80 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 E ){. int rc2
1ea90 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 ;.. assert( T
1eaa0 52 41 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d RANS_WRITE==pBt-
1eab0 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 >inTransaction )
1eac0 3b 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 ;. rc2 = sqli
1ead0 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
1eae0 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 (pBt->pPager);.
1eaf0 20 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 if( rc2!=SQLI
1eb00 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1eb10 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a c = rc2;. }..
1eb20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 /* The rollb
1eb30 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 ack may have des
1eb40 74 72 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 troyed the pPage
1eb50 31 2d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 1->aData value.
1eb60 20 53 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 So. ** call
1eb70 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20 6f btreeGetPage() o
1eb80 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e 20 74 n page 1 again t
1eb90 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 o make. ** su
1eba0 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 re pPage1->aData
1ebb0 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c is set correctl
1ebc0 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74 y. */. if( bt
1ebd0 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1ebe0 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 1, &pPage1, 0)==
1ebf0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ec00 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 67 int nPage = g
1ec10 65 74 34 62 79 74 65 28 32 38 2b 28 75 38 2a 29 et4byte(28+(u8*)
1ec20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 29 3b 0a pPage1->aData);.
1ec30 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ec40 6e 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 nPage==0 );.
1ec50 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 if( nPage==0 )
1ec60 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1ec70 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 ecount(pBt->pPag
1ec80 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 er, &nPage);.
1ec90 20 20 20 74 65 73 74 63 61 73 65 28 20 70 42 74 testcase( pBt
1eca0 2d 3e 6e 50 61 67 65 21 3d 6e 50 61 67 65 20 29 ->nPage!=nPage )
1ecb0 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 ;. pBt->nPa
1ecc0 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 ge = nPage;.
1ecd0 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
1ece0 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 age1);. }.
1ecf0 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 56 61 assert( countVa
1ed00 6c 69 64 43 75 72 73 6f 72 73 28 70 42 74 2c 20 lidCursors(pBt,
1ed10 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 1)==0 );. pBt
1ed20 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
1ed30 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 = TRANS_READ;.
1ed40 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 btreeClearHasC
1ed50 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 7d ontent(pBt);. }
1ed60 0a 0a 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e .. btreeEndTran
1ed70 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 saction(p);. sq
1ed80 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1ed90 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
1eda0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 .}../*.** Start
1edb0 61 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 a statement subt
1edc0 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 ransaction. The
1edd0 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 subtransaction c
1ede0 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 an be rolled.**
1edf0 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 back independent
1ee00 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 ly of the main t
1ee10 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 ransaction. You
1ee20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 must start a tra
1ee30 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 nsaction .** bef
1ee40 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 ore starting a s
1ee50 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 ubtransaction. T
1ee60 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f he subtransactio
1ee70 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d n is ended autom
1ee80 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 atically .** if
1ee90 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 the main transac
1eea0 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 tion commits or
1eeb0 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a rolls back..**.*
1eec0 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 * Statement subt
1eed0 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 ransactions are
1eee0 75 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 used around indi
1eef0 76 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 vidual SQL state
1ef00 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 ments.** that ar
1ef10 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 e contained with
1ef20 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d in a BEGIN...COM
1ef30 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 MIT block. If a
1ef40 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 constraint.** e
1ef50 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 rror occurs with
1ef60 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 in the statement
1ef70 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 , the effect of
1ef80 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 that one stateme
1ef90 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c nt.** can be rol
1efa0 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 led back without
1efb0 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 having to rollb
1efc0 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 ack the entire t
1efd0 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
1efe0 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 * A statement su
1eff0 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 b-transaction is
1f000 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
1f010 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 an anonymous sav
1f020 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 epoint. The.** v
1f030 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 alue passed as t
1f040 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
1f050 74 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c ter is the total
1f060 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 number of savep
1f070 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 oints,.** includ
1f080 69 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e ing the new anon
1f090 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c ymous savepoint,
1f0a0 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 open on the B-T
1f0b0 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 ree. i.e. if the
1f0c0 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 re.** are no act
1f0d0 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ive savepoints a
1f0e0 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 nd no other stat
1f0f0 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f ement-transactio
1f100 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 ns open,.** iSta
1f110 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 tement is 1. Thi
1f120 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 s anonymous save
1f130 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c point can be rel
1f140 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 eased or rolled
1f150 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 back.** using th
1f160 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 e sqlite3BtreeSa
1f170 76 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 vepoint() functi
1f180 6f 6e 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 on..*/.int sqlit
1f190 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 e3BtreeBeginStmt
1f1a0 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 (Btree *p, int i
1f1b0 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e Statement){. in
1f1c0 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
1f1d0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1f1e0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
1f1f0 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
1f200 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
1f210 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
1f220 73 73 65 72 74 28 20 28 70 42 74 2d 3e 62 74 73 ssert( (pBt->bts
1f230 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 Flags & BTS_READ
1f240 5f 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0a 20 20 61 _ONLY)==0 );. a
1f250 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e ssert( iStatemen
1f260 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 t>0 );. assert(
1f270 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 iStatement>p->d
1f280 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b b->nSavepoint );
1f290 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
1f2a0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
1f2b0 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
1f2c0 2f 2a 20 41 74 20 74 68 65 20 70 61 67 65 72 20 /* At the pager
1f2d0 6c 65 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 level, a stateme
1f2e0 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
1f2f0 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 s a savepoint wi
1f300 74 68 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 th. ** an index
1f310 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c greater than al
1f320 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 l savepoints cre
1f330 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 ated explicitly
1f340 75 73 69 6e 67 0a 20 20 2a 2a 20 53 51 4c 20 73 using. ** SQL s
1f350 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 tatements. It is
1f360 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e illegal to open
1f370 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c , release or rol
1f380 6c 62 61 63 6b 20 61 6e 79 0a 20 20 2a 2a 20 73 lback any. ** s
1f390 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 20 77 uch savepoints w
1f3a0 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 6d 65 hile the stateme
1f3b0 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 nt transaction s
1f3c0 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 avepoint is acti
1f3d0 76 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 ve.. */. rc =
1f3e0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
1f3f0 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 Savepoint(pBt->p
1f400 50 61 67 65 72 2c 20 69 53 74 61 74 65 6d 65 6e Pager, iStatemen
1f410 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 t);. sqlite3Btr
1f420 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1f430 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1f440 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 * The second arg
1f450 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
1f460 6e 63 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 nction, op, is a
1f470 6c 77 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f lways SAVEPOINT_
1f480 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 ROLLBACK.** or S
1f490 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1f4a0 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
1f4b0 65 69 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 either releases
1f4c0 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 or rolls back th
1f4d0 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 e.** savepoint i
1f4e0 64 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 dentified by par
1f4f0 61 6d 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e ameter iSavepoin
1f500 74 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 t, depending on
1f510 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 the value .** of
1f520 20 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 op..**.** Norma
1f530 6c 6c 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 lly, iSavepoint
1f540 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1f550 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f or equal to zero
1f560 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 . However, if op
1f570 20 69 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 is.** SAVEPOINT
1f580 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 _ROLLBACK, then
1f590 69 53 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 iSavepoint may a
1f5a0 6c 73 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 lso be -1. In th
1f5b0 69 73 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 is case the .**
1f5c0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
1f5d0 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 entire transacti
1f5e0 6f 6e 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 on are rolled ba
1f5f0 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 ck. This is diff
1f600 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 erent.** from a
1f610 6e 6f 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 normal transacti
1f620 6f 6e 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 on rollback, as
1f630 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c no locks are rel
1f640 65 61 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a eased and the.**
1f650 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d transaction rem
1f660 61 69 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 69 6e ains open..*/.in
1f670 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 t sqlite3BtreeSa
1f680 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 vepoint(Btree *p
1f690 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 , int op, int iS
1f6a0 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 avepoint){. int
1f6b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1f6c0 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69 . if( p && p->i
1f6d0 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1f6e0 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 ITE ){. BtSha
1f6f0 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1f700 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f t;. assert( o
1f710 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c p==SAVEPOINT_REL
1f720 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 EASE || op==SAVE
1f730 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
1f740 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 ;. assert( iS
1f750 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 avepoint>=0 || (
1f760 69 53 61 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 iSavepoint==-1 &
1f770 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f & op==SAVEPOINT_
1f780 52 4f 4c 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 ROLLBACK) );.
1f790 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1f7a0 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 er(p);. rc =
1f7b0 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 sqlite3PagerSave
1f7c0 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 point(pBt->pPage
1f7d0 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e r, op, iSavepoin
1f7e0 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d t);. if( rc==
1f7f0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1f800 20 20 20 69 66 28 20 69 53 61 76 65 70 6f 69 6e if( iSavepoin
1f810 74 3c 30 20 26 26 20 28 70 42 74 2d 3e 62 74 73 t<0 && (pBt->bts
1f820 46 6c 61 67 73 20 26 20 42 54 53 5f 49 4e 49 54 Flags & BTS_INIT
1f830 49 41 4c 4c 59 5f 45 4d 50 54 59 29 21 3d 30 20 IALLY_EMPTY)!=0
1f840 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e ){. pBt->
1f850 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 nPage = 0;.
1f860 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65 }. rc = ne
1f870 77 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a wDatabase(pBt);.
1f880 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 pBt->nPage
1f890 20 3d 20 67 65 74 34 62 79 74 65 28 32 38 20 2b = get4byte(28 +
1f8a0 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 pBt->pPage1->aD
1f8b0 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ata);.. /*
1f8c0 54 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a The database siz
1f8d0 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e e was written in
1f8e0 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 32 38 to the offset 28
1f8f0 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 0a 20 of the header.
1f900 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 ** when the
1f910 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 transaction sta
1f920 72 74 65 64 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 rted, so we know
1f930 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 that the value
1f940 61 74 20 6f 66 66 73 65 74 0a 20 20 20 20 20 20 at offset.
1f950 2a 2a 20 32 38 20 69 73 20 6e 6f 6e 7a 65 72 6f ** 28 is nonzero
1f960 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 . */. asser
1f970 74 28 20 70 42 74 2d 3e 6e 50 61 67 65 3e 30 20 t( pBt->nPage>0
1f980 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
1f990 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1f9a0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1f9b0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 rc;.}../*.** Cre
1f9c0 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 ate a new cursor
1f9d0 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 for the BTree w
1f9e0 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 hose root is on
1f9f0 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 the page.** iTab
1fa00 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e le. If a read-on
1fa10 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 ly cursor is req
1fa20 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73 uested, it is as
1fa30 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 sumed that.** th
1fa40 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79 e caller already
1fa50 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20 has at least a
1fa60 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 read-only transa
1fa70 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e ction open.** on
1fa80 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c the database al
1fa90 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74 ready. If a writ
1faa0 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 e-cursor is requ
1fab0 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 ested, then.** t
1fac0 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73 he caller is ass
1fad0 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20 umed to have an
1fae0 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
1faf0 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 action..**.** If
1fb00 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e wrFlag==0, then
1fb10 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 the cursor can
1fb20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 only be used for
1fb30 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 reading..** If
1fb40 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 wrFlag==1, then
1fb50 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 the cursor can b
1fb60 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 e used for readi
1fb70 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 ng or for.** wri
1fb80 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f ting if other co
1fb90 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 nditions for wri
1fba0 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 ting are also me
1fbb0 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 t. These.** are
1fbc0 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 the conditions
1fbd0 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 that must be met
1fbe0 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 in order for wr
1fbf0 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 iting to.** be a
1fc00 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a llowed:.**.** 1:
1fc10 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 The cursor mus
1fc20 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e t have been open
1fc30 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d ed with wrFlag==
1fc40 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 1.**.** 2: Othe
1fc50 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
1fc60 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 ctions that shar
1fc70 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 e the same pager
1fc80 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 cache.** bu
1fc90 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 t which are not
1fca0 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f in the READ_UNCO
1fcb0 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 MMITTED state ma
1fcc0 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 y not have.**
1fcd0 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 cursors open w
1fce0 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e ith wrFlag==0 on
1fcf0 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e the same table.
1fd00 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 Otherwise.**
1fd10 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d the changes m
1fd20 61 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74 ade by this writ
1fd30 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 e cursor would b
1fd40 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 e visible to.**
1fd50 20 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72 the read cur
1fd60 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 sors in the othe
1fd70 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
1fd80 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 ction..**.** 3:
1fd90 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 The database mu
1fda0 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 st be writable (
1fdb0 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 not on read-only
1fdc0 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a media).**.** 4:
1fdd0 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 There must be
1fde0 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 an active transa
1fdf0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 ction..**.** No
1fe00 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 checking is done
1fe10 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
1fe20 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 at page iTable r
1fe30 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 eally is the.**
1fe40 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 root page of a b
1fe50 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 -tree. If it is
1fe60 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 not, then the c
1fe70 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a ursor acquired.*
1fe80 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 * will not work
1fe90 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a correctly..**.**
1fea0 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
1feb0 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 hat the sqlite3B
1fec0 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 treeCursorZero()
1fed0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
1fee0 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 .** on pCur to i
1fef0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
1ff00 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 mory space prior
1ff10 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 to invoking thi
1ff20 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 s routine..*/.st
1ff30 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 75 atic int btreeCu
1ff40 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 rsor(. Btree *p
1ff50 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ff70 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 * The btree */.
1ff80 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 int iTable,
1ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffa0 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
1ffb0 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f page of table to
1ffc0 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 open */. int w
1ffd0 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 rFlag,
1ffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fff0 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e /* 1 to write.
20000 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 0 read-only */.
20010 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f struct KeyInfo
20020 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 *pKeyInfo,
20030 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
20040 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 t arg to compari
20050 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a son function */.
20060 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
20070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20080 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 /* Spac
20090 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 e for new cursor
200a0 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 */.){. BtShare
200b0 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
200c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
200d0 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 65 /* Shared b-tree
200e0 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 handle */.. as
200f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
20100 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 eeHoldsMutex(p)
20110 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 );. assert( wrF
20120 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 lag==0 || wrFlag
20130 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 ==1 );.. /* The
20140 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 following asser
20150 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65 72 t statements ver
20160 69 66 79 20 74 68 61 74 20 69 66 20 74 68 69 73 ify that if this
20170 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 20 0a is a sharable .
20180 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61 ** b-tree data
20190 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 base, the connec
201a0 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 tion is holding
201b0 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62 the required tab
201c0 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 le locks, . **
201d0 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 and that no othe
201e0 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 r connection has
201f0 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 any open cursor
20200 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 that conflicts
20210 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 with . ** this
20220 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 lock. */. asse
20230 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 rt( hasSharedCac
20240 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 heTableLock(p, i
20250 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 Table, pKeyInfo!
20260 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 3b =0, wrFlag+1) );
20270 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 . assert( wrFla
20280 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 64 g==0 || !hasRead
20290 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 61 Conflicts(p, iTa
202a0 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 ble) );.. /* As
202b0 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61 sert that the ca
202c0 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64 20 ller has opened
202d0 74 68 65 20 72 65 71 75 69 72 65 64 20 74 72 61 the required tra
202e0 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 nsaction. */. a
202f0 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
20300 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a s>TRANS_NONE );.
20310 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 assert( wrFlag
20320 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e ==0 || p->inTran
20330 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
20340 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
20350 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 2d 3e >pPage1 && pBt->
20360 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b pPage1->aData );
20370 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77 72 .. if( NEVER(wr
20380 46 6c 61 67 20 26 26 20 28 70 42 74 2d 3e 62 74 Flag && (pBt->bt
20390 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 sFlags & BTS_REA
203a0 44 5f 4f 4e 4c 59 29 21 3d 30 29 20 29 7b 0a 20 D_ONLY)!=0) ){.
203b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
203c0 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 _READONLY;. }.
203d0 20 69 66 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 if( wrFlag ){.
203e0 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 allocateTempS
203f0 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 69 pace(pBt);. i
20400 66 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 f( pBt->pTmpSpac
20410 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 e==0 ) return SQ
20420 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
20430 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 if( iTable==1
20440 26 26 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e && btreePagecoun
20450 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a 20 20 20 t(pBt)==0 ){.
20460 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d assert( wrFlag=
20470 3d 30 20 29 3b 0a 20 20 20 20 69 54 61 62 6c 65 =0 );. iTable
20480 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
20490 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 Now that no othe
204a0 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63 r errors can occ
204b0 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69 ur, finish filli
204c0 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73 ng in the BtCurs
204d0 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 or. ** variable
204e0 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20 63 s and link the c
204f0 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 ursor into the B
20500 74 53 68 61 72 65 64 20 6c 69 73 74 2e 20 20 2a tShared list. *
20510 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f /. pCur->pgnoRo
20520 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c ot = (Pgno)iTabl
20530 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 e;. pCur->iPage
20540 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72 2d 3e 70 = -1;. pCur->p
20550 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e KeyInfo = pKeyIn
20560 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 fo;. pCur->pBtr
20570 65 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e ee = p;. pCur->
20580 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 61 73 73 pBt = pBt;. ass
20590 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c ert( wrFlag==0 |
205a0 7c 20 77 72 46 6c 61 67 3d 3d 42 54 43 46 5f 57 | wrFlag==BTCF_W
205b0 72 69 74 65 46 6c 61 67 20 29 3b 0a 20 20 70 43 riteFlag );. pC
205c0 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 3d 20 77 ur->curFlags = w
205d0 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e 70 rFlag;. pCur->p
205e0 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 72 Next = pBt->pCur
205f0 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72 2d sor;. if( pCur-
20600 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 43 >pNext ){. pC
20610 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 ur->pNext->pPrev
20620 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20 70 = pCur;. }. p
20630 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 Bt->pCursor = pC
20640 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 ur;. pCur->eSta
20650 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
20660 4c 49 44 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 LID;. return SQ
20670 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 69 6e 74 20 73 LITE_OK;.}.int s
20680 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
20690 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 r(. Btree *p,
206a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
206b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
206c0 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f /* The btree */
206d0 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
206e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
206f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20700 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 * Root page of t
20710 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a able to open */.
20720 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 int wrFlag,
20730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20750 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 1 to write. 0 r
20760 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 ead-only */. st
20770 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b ruct KeyInfo *pK
20780 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 eyInfo,
20790 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
207a0 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 st arg to xCompa
207b0 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 re() */. BtCurs
207c0 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 or *pCur
207d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
207e0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e /* Write n
207f0 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a ew cursor here *
20800 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
20810 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
20820 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 er(p);. rc = bt
20830 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 54 61 reeCursor(p, iTa
20840 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 ble, wrFlag, pKe
20850 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20 20 yInfo, pCur);.
20860 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
20870 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
20880 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
20890 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 rn the size of a
208a0 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 BtCursor object
208b0 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a in bytes..**.**
208c0 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73 This interfaces
208d0 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 74 68 is needed so th
208e0 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 72 73 at users of curs
208f0 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f 63 ors can prealloc
20900 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e ate.** sufficien
20910 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 6f 6c t storage to hol
20920 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 d a cursor. The
20930 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 BtCursor object
20940 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74 6f is opaque.** to
20950 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 20 63 users so they c
20960 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 69 7a annot do the siz
20970 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 65 73 eof() themselves
20980 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 61 6c - they must cal
20990 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e l.** this routin
209a0 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 e..*/.int sqlite
209b0 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
209c0 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e (void){. return
209d0 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 42 ROUND8(sizeof(B
209e0 74 43 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a 2f 2a tCursor));.}../*
209f0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d .** Initialize m
20a00 65 6d 6f 72 79 20 74 68 61 74 20 77 69 6c 6c 20 emory that will
20a10 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 be converted int
20a20 6f 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a o a BtCursor obj
20a30 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ect..**.** The s
20a40 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 20 68 imple approach h
20a50 65 72 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 ere would be to
20a60 6d 65 6d 73 65 74 28 29 20 74 68 65 20 65 6e 74 memset() the ent
20a70 69 72 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 6f ire object.** to
20a80 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 74 20 74 zero. But it t
20a90 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 74 68 urns out that th
20aa0 65 20 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 e apPage[] and a
20ab0 69 49 64 78 5b 5d 20 61 72 72 61 79 73 0a 2a 2a iIdx[] arrays.**
20ac0 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 do not need to
20ad0 62 65 20 7a 65 72 6f 65 64 20 61 6e 64 20 74 68 be zeroed and th
20ae0 65 79 20 61 72 65 20 6c 61 72 67 65 2c 20 73 6f ey are large, so
20af0 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c we can save a l
20b00 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74 69 6d ot.** of run-tim
20b10 65 20 62 79 20 73 6b 69 70 70 69 6e 67 20 74 68 e by skipping th
20b20 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
20b30 20 6f 66 20 74 68 6f 73 65 20 65 6c 65 6d 65 6e of those elemen
20b40 74 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 ts..*/.void sqli
20b50 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 te3BtreeCursorZe
20b60 72 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b ro(BtCursor *p){
20b70 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
20b80 6f 66 66 73 65 74 6f 66 28 42 74 43 75 72 73 6f offsetof(BtCurso
20b90 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a 2f r, iPage));.}../
20ba0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 *.** Close a cur
20bb0 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c sor. The read l
20bc0 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
20bd0 61 73 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 ase file is rele
20be0 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 ased.** when the
20bf0 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 last cursor is
20c00 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 closed..*/.int s
20c10 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
20c20 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 Cursor(BtCursor
20c30 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 *pCur){. Btree
20c40 2a 70 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e *pBtree = pCur->
20c50 70 42 74 72 65 65 3b 0a 20 20 69 66 28 20 70 42 pBtree;. if( pB
20c60 74 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 tree ){. int
20c70 69 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 i;. BtShared
20c80 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 *pBt = pCur->pBt
20c90 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
20ca0 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b eeEnter(pBtree);
20cb0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
20cc0 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 eClearCursor(pCu
20cd0 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 r);. if( pCur
20ce0 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 ->pPrev ){.
20cf0 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e pCur->pPrev->pN
20d00 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 ext = pCur->pNex
20d10 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
20d20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 pBt->pCursor
20d30 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a = pCur->pNext;.
20d40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 }. if( pC
20d50 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 ur->pNext ){.
20d60 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e pCur->pNext->
20d70 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 50 pPrev = pCur->pP
20d80 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 rev;. }. f
20d90 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d or(i=0; i<=pCur-
20da0 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
20db0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
20dc0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 pCur->apPage[i])
20dd0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c 6f ;. }. unlo
20de0 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
20df0 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pBt);. sqlite
20e00 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 3_free(pCur->aOv
20e10 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 2f 2a 20 erflow);. /*
20e20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 sqlite3_free(pCu
20e30 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 r); */. sqlit
20e40 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 e3BtreeLeave(pBt
20e50 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ree);. }. retu
20e60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
20e70 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
20e80 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 the BtCursor* g
20e90 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 iven in the argu
20ea0 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 ment has a valid
20eb0 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 .** BtCursor.inf
20ec0 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 o structure. If
20ed0 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
20ee0 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a dy valid, call.*
20ef0 2a 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c * btreeParseCell
20f00 28 29 20 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e () to fill it in
20f10 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 ..**.** BtCursor
20f20 2e 69 6e 66 6f 20 69 73 20 61 20 63 61 63 68 65 .info is a cache
20f30 20 6f 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 of the informat
20f40 69 6f 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 ion in the curre
20f50 6e 74 20 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e nt cell..** Usin
20f60 67 20 74 68 69 73 20 63 61 63 68 65 20 72 65 64 g this cache red
20f70 75 63 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 uces the number
20f80 6f 66 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 of calls to btre
20f90 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a eParseCell()..**
20fa0 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 .** 2007-06-25:
20fb0 20 54 68 65 72 65 20 69 73 20 61 20 62 75 67 20 There is a bug
20fc0 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 in some versions
20fd0 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 of MSVC that ca
20fe0 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 use the.** compi
20ff0 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 ler to crash whe
21000 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 n getCellInfo()
21010 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 is implemented a
21020 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 s a macro..** Bu
21030 74 20 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 t there is a mea
21040 73 75 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 sureable speed a
21050 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e dvantage to usin
21060 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 g the macro on g
21070 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 cc.** (when less
21080 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 compiler optimi
21090 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 zations like -Os
210a0 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 or -O0 are used
210b0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 and the.** comp
210c0 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e iler is not doin
210d0 67 20 61 67 67 72 65 73 73 69 76 65 20 69 6e 6c g aggressive inl
210e0 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 ining.) So we u
210f0 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 se a real functi
21100 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 on.** for MSVC a
21110 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 nd a macro for e
21120 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 verything else.
21130 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a Ticket #2457..*
21140 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
21150 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 61 . static void a
21160 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 ssertCellInfo(Bt
21170 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
21180 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f CellInfo info
21190 3b 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 ;. int iPage
211a0 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 = pCur->iPage;.
211b0 20 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c memset(&info,
211c0 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 0, sizeof(info)
211d0 29 3b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 );. btreePars
211e0 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 eCell(pCur->apPa
211f0 67 65 5b 69 50 61 67 65 5d 2c 20 70 43 75 72 2d ge[iPage], pCur-
21200 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 20 26 >aiIdx[iPage], &
21210 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 info);. asser
21220 74 28 20 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c t( CORRUPT_DB ||
21230 20 6d 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 memcmp(&info, &
21240 70 43 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 pCur->info, size
21250 6f 66 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a of(info))==0 );.
21260 20 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 }.#else. #def
21270 69 6e 65 20 61 73 73 65 72 74 43 65 6c 6c 49 6e ine assertCellIn
21280 66 6f 28 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 fo(x).#endif.#if
21290 64 65 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f def _MSC_VER. /
212a0 2a 20 55 73 65 20 61 20 72 65 61 6c 20 66 75 6e * Use a real fun
212b0 63 74 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f ction in MSVC to
212c0 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 work around bug
212d0 73 20 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c s in that compil
212e0 65 72 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 er. */. static
212f0 76 6f 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f void getCellInfo
21300 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
21310 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e {. if( pCur->
21320 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b info.nSize==0 ){
21330 0a 20 20 20 20 20 20 69 6e 74 20 69 50 61 67 65 . int iPage
21340 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a = pCur->iPage;.
21350 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65 btreeParse
21360 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 Cell(pCur->apPag
21370 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 e[iPage],pCur->a
21380 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 iIdx[iPage],&pCu
21390 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 r->info);.
213a0 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c pCur->curFlags |
213b0 3d 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 = BTCF_ValidNKey
213c0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
213d0 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 assertCellInf
213e0 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 o(pCur);. }.
213f0 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e }.#else /* if n
21400 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 ot _MSC_VER */.
21410 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 /* Use a macro
21420 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d in all other com
21430 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 pilers so that t
21440 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 he function is i
21450 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e nlined */.#defin
21460 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 e getCellInfo(pC
21470 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 ur)
21480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
214b0 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
214c0 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 e==0 ){
214d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
21500 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
21510 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 ->iPage;
21520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21540 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
21550 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
21560 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
21570 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b ge],pCur->aiIdx[
21580 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e iPage],&pCur->in
21590 66 6f 29 3b 20 20 20 20 20 20 20 20 5c 0a 20 20 fo); \.
215a0 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 pCur->curFlags
215b0 20 7c 3d 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b |= BTCF_ValidNK
215c0 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ey;
215d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
215e0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
215f0 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 }else{
21600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
21640 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e assertCellIn
21650 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 fo(pCur);
21660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
21690 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f . }.#endif /* _
216a0 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 23 69 66 6e MSC_VER */..#ifn
216b0 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a 20 54 def NDEBUG /* T
216c0 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e 65 20 he next routine
216d0 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e used only within
216e0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
216f0 65 6e 74 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52 65 ents */./*.** Re
21700 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
21710 20 67 69 76 65 6e 20 42 74 43 75 72 73 6f 72 20 given BtCursor
21720 69 73 20 76 61 6c 69 64 2e 20 20 41 20 76 61 6c is valid. A val
21730 69 64 20 63 75 72 73 6f 72 20 69 73 20 6f 6e 65 id cursor is one
21740 0a 2a 2a 20 74 68 61 74 20 69 73 20 63 75 72 72 .** that is curr
21750 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
21760 6f 20 61 20 72 6f 77 20 69 6e 20 61 20 28 6e 6f o a row in a (no
21770 6e 2d 65 6d 70 74 79 29 20 74 61 62 6c 65 2e 0a n-empty) table..
21780 2a 2a 20 54 68 69 73 20 69 73 20 61 20 76 65 72 ** This is a ver
21790 69 66 69 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e ification routin
217a0 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 e is used only w
217b0 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20 73 ithin assert() s
217c0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 69 6e tatements..*/.in
217d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
217e0 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75 rsorIsValid(BtCu
217f0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 rsor *pCur){. r
21800 65 74 75 72 6e 20 70 43 75 72 20 26 26 20 70 43 eturn pCur && pC
21810 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
21820 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64 OR_VALID;.}.#end
21830 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
21840 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a ./*.** Set *pSiz
21850 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 e to the size of
21860 20 74 68 65 20 62 75 66 66 65 72 20 6e 65 65 64 the buffer need
21870 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 ed to hold the v
21880 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6b alue of.** the k
21890 65 79 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 ey for the curre
218a0 6e 74 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 nt entry. If th
218b0 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 e cursor is not
218c0 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 pointing.** to a
218d0 20 76 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70 valid entry, *p
218e0 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 Size is set to 0
218f0 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 . .**.** For a t
21900 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 49 4e able with the IN
21910 54 4b 45 59 20 66 6c 61 67 20 73 65 74 2c 20 74 TKEY flag set, t
21920 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
21930 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20 69 rns the key.** i
21940 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20 6e tself, not the n
21950 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
21960 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a n the key..**.**
21970 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
21980 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 63 75 position the cu
21990 72 73 6f 72 20 70 72 69 6f 72 20 74 6f 20 69 6e rsor prior to in
219a0 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 voking this rout
219b0 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 ine..** .** This
219c0 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 routine cannot
219d0 66 61 69 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 fail. It always
219e0 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
219f0 4f 4b 2e 20 20 0a 2a 2f 0a 69 6e 74 20 73 71 6c OK. .*/.int sql
21a00 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
21a10 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
21a20 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 i64 *pSize){.
21a30 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
21a40 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
21a50 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
21a60 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
21a70 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65 74 43 _VALID );. getC
21a80 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
21a90 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e *pSize = pCur->
21aa0 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 72 65 74 info.nKey;. ret
21ab0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
21ac0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 ../*.** Set *pSi
21ad0 7a 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 ze to the number
21ae0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 of bytes of dat
21af0 61 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74 a in the entry t
21b00 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 he.** cursor cur
21b10 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f rently points to
21b20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
21b30 65 72 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 er must guarante
21b40 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
21b50 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
21b60 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 a non-NULL.** v
21b70 61 6c 69 64 20 65 6e 74 72 79 2e 20 20 49 6e 20 alid entry. In
21b80 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
21b90 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 calling procedu
21ba0 72 65 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 re must guarante
21bb0 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 e.** that the cu
21bc0 72 73 6f 72 20 68 61 73 20 43 75 72 73 6f 72 2e rsor has Cursor.
21bd0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
21be0 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c ALID..**.** Fail
21bf0 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 ure is not possi
21c00 62 6c 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 ble. This funct
21c10 69 6f 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 ion always retur
21c20 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a ns SQLITE_OK..**
21c30 20 49 74 20 6d 69 67 68 74 20 6a 75 73 74 20 61 It might just a
21c40 73 20 77 65 6c 6c 20 62 65 20 61 20 70 72 6f 63 s well be a proc
21c50 65 64 75 72 65 20 28 72 65 74 75 72 6e 69 6e 67 edure (returning
21c60 20 76 6f 69 64 29 20 62 75 74 20 77 65 20 63 6f void) but we co
21c70 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f 20 72 65 74 ntinue.** to ret
21c80 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 urn an integer r
21c90 65 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 20 68 esult code for h
21ca0 69 73 74 6f 72 69 63 61 6c 20 72 65 61 73 6f 6e istorical reason
21cb0 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 s..*/.int sqlite
21cc0 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 3BtreeDataSize(B
21cd0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
21ce0 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 32 *pSize){. as
21cf0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
21d00 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
21d10 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
21d20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
21d30 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
21d40 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 ( pCur->iPage>=0
21d50 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
21d60 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 ur->iPage<BTCURS
21d70 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a OR_MAX_DEPTH );.
21d80 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
21d90 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
21da0 67 65 5d 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 3d ge]->intKeyLeaf=
21db0 3d 31 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 =1 );. getCellI
21dc0 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 nfo(pCur);. *pS
21dd0 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f ize = pCur->info
21de0 2e 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 72 65 74 .nPayload;. ret
21df0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
21e00 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 ../*.** Given th
21e10 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
21e20 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
21e30 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
21e40 65 20 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 e (parameter.**
21e50 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 ovfl), this func
21e60 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 tion finds the p
21e70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
21e80 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 e next page in t
21e90 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 he .** linked li
21ea0 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 st of overflow p
21eb0 61 67 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c ages. If possibl
21ec0 65 2c 20 69 74 20 75 73 65 73 20 74 68 65 20 61 e, it uses the a
21ed0 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f uto-vacuum.** po
21ee0 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 inter-map data i
21ef0 6e 73 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e nstead of readin
21f00 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 g the content of
21f10 20 70 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f page ovfl to do
21f20 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 so. .**.** If a
21f30 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 n error occurs a
21f40 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
21f50 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
21f60 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a Otherwise:.**.*
21f70 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * The page numbe
21f80 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 r of the next ov
21f90 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 erflow page in t
21fa0 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 he linked list i
21fb0 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f s .** written to
21fc0 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 *pPgnoNext. If
21fd0 70 61 67 65 20 6f 76 66 6c 20 69 73 20 74 68 65 page ovfl is the
21fe0 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 69 74 last page in it
21ff0 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 s linked .** lis
22000 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 t, *pPgnoNext is
22010 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a set to zero. .*
22020 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 *.** If ppPage i
22030 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 s not NULL, and
22040 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
22050 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 he MemPage objec
22060 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a t corresponding.
22070 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 ** to page numbe
22080 72 20 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 r pOvfl was obta
22090 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 ined, then *ppPa
220a0 67 65 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 ge is set to poi
220b0 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 nt to that.** re
220c0 66 65 72 65 6e 63 65 2e 20 49 74 20 69 73 20 74 ference. It is t
220d0 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 he responsibilit
220e0 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 y of the caller
220f0 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 to call releaseP
22100 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 age().** on *ppP
22110 61 67 65 20 74 6f 20 66 72 65 65 20 74 68 65 20 age to free the
22120 72 65 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f reference. In no
22130 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20 6f reference was o
22140 62 74 61 69 6e 65 64 20 28 62 65 63 61 75 73 65 btained (because
22150 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d .** the pointer-
22160 6d 61 70 20 77 61 73 20 75 73 65 64 20 74 6f 20 map was used to
22170 6f 62 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 obtain the value
22180 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 for *pPgnoNext)
22190 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 , then.** *ppPag
221a0 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f e is set to zero
221b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
221c0 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 getOverflowPage(
221d0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
221e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
221f0 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
22200 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f file */. Pgno o
22210 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 vfl,
22220 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
22230 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 t overflow page
22240 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 4d 65 6d 50 number */. MemP
22250 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 age **ppPage,
22260 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
22270 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 20 MemPage handle
22280 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f (may be NULL) */
22290 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 . Pgno *pPgnoNe
222a0 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xt
222b0 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 6f 76 65 /* OUT: Next ove
222c0 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 rflow page numbe
222d0 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e r */.){. Pgno n
222e0 65 78 74 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 ext = 0;. MemPa
222f0 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 ge *pPage = 0;.
22300 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
22310 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _OK;.. assert(
22320 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
22330 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
22340 3b 0a 20 20 61 73 73 65 72 74 28 70 50 67 6e 6f ;. assert(pPgno
22350 4e 65 78 74 29 3b 0a 0a 23 69 66 6e 64 65 66 20 Next);..#ifndef
22360 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
22370 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79 20 VACUUM. /* Try
22380 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 65 78 74 to find the next
22390 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 page in the ove
223a0 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e 67 rflow list using
223b0 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76 61 the. ** autova
223c0 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61 70 cuum pointer-map
223d0 20 70 61 67 65 73 2e 20 47 75 65 73 73 20 74 68 pages. Guess th
223e0 61 74 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 at the next page
223f0 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f 76 in . ** the ov
22400 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 73 20 70 erflow list is p
22410 61 67 65 20 6e 75 6d 62 65 72 20 28 6f 76 66 6c age number (ovfl
22420 2b 31 29 2e 20 49 66 20 74 68 61 74 20 67 75 65 +1). If that gue
22430 73 73 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20 6f ss turns . ** o
22440 75 74 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c 20 ut to be wrong,
22450 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f 61 fall back to loa
22460 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 6f 66 ding the data of
22470 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 page . ** numb
22480 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 72 er ovfl to deter
22490 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 20 70 61 mine the next pa
224a0 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a ge number.. */.
224b0 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
224c0 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e acuum ){. Pgn
224d0 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f o pgno;. Pgno
224e0 20 69 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b 31 iGuess = ovfl+1
224f0 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a ;. u8 eType;.
22500 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d . while( PTRM
22510 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 AP_ISPAGE(pBt, i
22520 47 75 65 73 73 29 20 7c 7c 20 69 47 75 65 73 73 Guess) || iGuess
22530 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
22540 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
22550 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20 20 iGuess++;.
22560 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 75 65 73 }.. if( iGues
22570 73 3c 3d 62 74 72 65 65 50 61 67 65 63 6f 75 6e s<=btreePagecoun
22580 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 t(pBt) ){.
22590 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 rc = ptrmapGet(p
225a0 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 54 79 Bt, iGuess, &eTy
225b0 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 20 pe, &pgno);.
225c0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
225d0 5f 4f 4b 20 26 26 20 65 54 79 70 65 3d 3d 50 54 _OK && eType==PT
225e0 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 26 RMAP_OVERFLOW2 &
225f0 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a & pgno==ovfl ){.
22600 20 20 20 20 20 20 20 20 6e 65 78 74 20 3d 20 69 next = i
22610 47 75 65 73 73 3b 0a 20 20 20 20 20 20 20 20 72 Guess;. r
22620 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
22630 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
22640 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 }.#endif.. ass
22650 65 72 74 28 20 6e 65 78 74 3d 3d 30 20 7c 7c 20 ert( next==0 ||
22660 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
22670 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
22680 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 ITE_OK ){. rc
22690 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
226a0 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 67 pBt, ovfl, &pPag
226b0 65 2c 20 28 70 70 50 61 67 65 3d 3d 30 29 20 3f e, (ppPage==0) ?
226c0 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f PAGER_GET_READO
226d0 4e 4c 59 20 3a 20 30 29 3b 0a 20 20 20 20 61 73 NLY : 0);. as
226e0 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
226f0 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 20 _OK || pPage==0
22700 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
22710 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
22720 20 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 next = get4byt
22730 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b e(pPage->aData);
22740 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 . }. }.. *p
22750 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b PgnoNext = next;
22760 0a 20 20 69 66 28 20 70 70 50 61 67 65 20 29 7b . if( ppPage ){
22770 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 . *ppPage = p
22780 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Page;. }else{.
22790 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
227a0 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Page);. }. ret
227b0 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f urn (rc==SQLITE_
227c0 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b DONE ? SQLITE_OK
227d0 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a : rc);.}../*.**
227e0 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 Copy data from
227f0 61 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 a buffer to a pa
22800 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 ge, or from a pa
22810 67 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a ge to a buffer..
22820 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 **.** pPayload i
22830 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 s a pointer to d
22840 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 ata stored on da
22850 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 tabase page pDbP
22860 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d age..** If argum
22870 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 ent eOp is false
22880 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 , then nByte byt
22890 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 es of data are c
228a0 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 opied.** from pP
228b0 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 ayload to the bu
228c0 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 ffer pointed at
228d0 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 by pBuf. If eOp
228e0 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e is true,.** then
228f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
22900 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f te() is called o
22910 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 n pDbPage and nB
22920 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 yte bytes.** of
22930 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 data are copied
22940 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
22950 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 pBuf to pPayload
22960 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
22970 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e K is returned on
22980 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 success, otherw
22990 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 ise an error cod
229a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
229b0 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 copyPayload(.
229c0 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 void *pPayload,
229d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
229e0 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 nter to page dat
229f0 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 a */. void *pBu
22a00 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
22a10 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 /* Pointer to b
22a20 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e uffer */. int n
22a30 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 Byte,
22a40 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
22a50 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 f bytes to copy
22a60 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 */. int eOp,
22a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
22a80 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d * 0 -> copy from
22a90 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 page, 1 -> copy
22aa0 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 to page */. Db
22ab0 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 Page *pDbPage
22ac0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
22ad0 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c containing pPayl
22ae0 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 oad */.){. if(
22af0 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f eOp ){. /* Co
22b00 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 py data from buf
22b10 66 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 fer to page (a w
22b20 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 rite operation)
22b30 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 */. int rc =
22b40 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
22b50 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 e(pDbPage);.
22b60 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
22b70 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
22b80 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
22b90 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c memcpy(pPayload,
22ba0 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 pBuf, nByte);.
22bb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 }else{. /* C
22bc0 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 opy data from pa
22bd0 67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 ge to buffer (a
22be0 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 read operation)
22bf0 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 */. memcpy(pB
22c00 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 uf, pPayload, nB
22c10 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 yte);. }. retu
22c20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
22c30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
22c40 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
22c50 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 read or overwrit
22c60 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d e payload inform
22c70 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 ation.** for the
22c80 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
22c90 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 pCur cursor is p
22ca0 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 54 68 65 20 ointing to. The
22cb0 65 4f 70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 eOp.** argument
22cc0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
22cd0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
22ce0 20 20 20 30 3a 20 54 68 65 20 6f 70 65 72 61 74 0: The operat
22cf0 69 6f 6e 20 69 73 20 61 20 72 65 61 64 2e 20 50 ion is a read. P
22d00 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 opulate the over
22d10 66 6c 6f 77 20 63 61 63 68 65 2e 0a 2a 2a 20 20 flow cache..**
22d20 20 31 3a 20 54 68 65 20 6f 70 65 72 61 74 69 6f 1: The operatio
22d30 6e 20 69 73 20 61 20 77 72 69 74 65 2e 20 50 6f n is a write. Po
22d40 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 pulate the overf
22d50 6c 6f 77 20 63 61 63 68 65 2e 0a 2a 2a 20 20 20 low cache..**
22d60 32 3a 20 54 68 65 20 6f 70 65 72 61 74 69 6f 6e 2: The operation
22d70 20 69 73 20 61 20 72 65 61 64 2e 20 44 6f 20 6e is a read. Do n
22d80 6f 74 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 ot populate the
22d90 6f 76 65 72 66 6c 6f 77 20 63 61 63 68 65 2e 0a overflow cache..
22da0 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 **.** A total of
22db0 20 22 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 "amt" bytes are
22dc0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
22dd0 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f beginning at "o
22de0 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20 ffset"..** Data
22df0 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 is read to or fr
22e00 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 om the buffer pB
22e10 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f uf..**.** The co
22e20 6e 74 65 6e 74 20 62 65 69 6e 67 20 72 65 61 64 ntent being read
22e30 20 6f 72 20 77 72 69 74 74 65 6e 20 6d 69 67 68 or written migh
22e40 74 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 t appear on the
22e50 6d 61 69 6e 20 70 61 67 65 0a 2a 2a 20 6f 72 20 main page.** or
22e60 62 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 be scattered out
22e70 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 on multiple ove
22e80 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 0a rflow pages..**.
22e90 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e ** If the curren
22ea0 74 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20 75 t cursor entry u
22eb0 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ses one or more
22ec0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 overflow pages a
22ed0 6e 64 20 74 68 65 0a 2a 2a 20 65 4f 70 20 61 72 nd the.** eOp ar
22ee0 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 32 2c gument is not 2,
22ef0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d this function m
22f00 61 79 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 ay allocate spac
22f10 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79 e for and lazily
22f20 20 0a 2a 2a 20 70 6f 70 75 6c 61 74 65 73 20 74 .** populates t
22f30 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
22f40 2d 6c 69 73 74 20 63 61 63 68 65 20 61 72 72 61 -list cache arra
22f50 79 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 y (BtCursor.aOve
22f60 72 66 6c 6f 77 29 2e 20 0a 2a 2a 20 53 75 62 73 rflow). .** Subs
22f70 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 equent calls use
22f80 20 74 68 69 73 20 63 61 63 68 65 20 74 6f 20 6d this cache to m
22f90 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74 ake seeking to t
22fa0 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73 he supplied offs
22fb0 65 74 20 0a 2a 2a 20 6d 6f 72 65 20 65 66 66 69 et .** more effi
22fc0 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 cient..**.** Onc
22fd0 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 e an overflow pa
22fe0 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 ge-list cache ha
22ff0 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 s been allocated
23000 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 , it may be.** i
23010 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f nvalidated if so
23020 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 me other cursor
23030 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 writes to the sa
23040 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a me table, or if.
23050 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 ** the cursor is
23060 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 moved to a diff
23070 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 erent row. Addit
23080 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f ionally, in auto
23090 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c -vacuum.** mode,
230a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 the following e
230b0 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 vents may invali
230c0 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 date an overflow
230d0 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
230e0 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 ..**.** * An i
230f0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
23100 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d m,.** * A comm
23110 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 it in auto_vacuu
23120 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a m="full" mode,.*
23130 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 * * Creating a
23140 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 table (may requ
23150 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 ire moving an ov
23160 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f erflow page)..*/
23170 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 .static int acce
23180 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 ssPayload(. BtC
23190 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 ursor *pCur,
231a0 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
231b0 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f ting to entry to
231c0 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 read from */.
231d0 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 u32 offset,
231e0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 /* Begin re
231f0 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 ading this far i
23200 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 nto payload */.
23210 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 u32 amt,
23220 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 /* Read th
23230 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f is many bytes */
23240 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
23250 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 *pBuf, /* Write
23260 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 the bytes into
23270 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a this buffer */ .
23280 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20 int eOp
23290 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 /* zero t
232a0 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f o read. non-zero
232b0 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b to write. */.){
232c0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
232d0 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e *aPayload;. in
232e0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
232f0 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30 ;. int iIdx = 0
23300 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
23310 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
23320 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 e[pCur->iPage];
23330 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f 66 /* Btree page of
23340 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a current entry *
23350 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
23360 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 t = pCur->pBt;
23370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23380 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63 75 /* Btree this cu
23390 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 rsor belongs to
233a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
233b0 5f 44 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 _DIRECT_OVERFLOW
233c0 5f 52 45 41 44 0a 20 20 75 6e 73 69 67 6e 65 64 _READ. unsigned
233d0 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 70 42 char * const pB
233e0 75 66 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a ufStart = pBuf;.
233f0 20 20 69 6e 74 20 62 45 6e 64 3b 20 20 20 20 20 int bEnd;
23400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23410 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
23420 72 75 65 20 69 66 20 72 65 61 64 69 6e 67 20 74 rue if reading t
23430 6f 20 65 6e 64 20 6f 66 20 64 61 74 61 20 2a 2f o end of data */
23440 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
23450 74 28 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 t( pPage );. as
23460 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
23470 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
23480 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
23490 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
234a0 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
234b0 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ell );. assert(
234c0 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
234d0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
234e0 65 72 74 28 20 65 4f 70 21 3d 32 20 7c 7c 20 6f ert( eOp!=2 || o
234f0 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 2f ffset==0 ); /
23500 2a 20 41 6c 77 61 79 73 20 73 74 61 72 74 20 66 * Always start f
23510 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 66 6f rom beginning fo
23520 72 20 65 4f 70 3d 3d 32 20 2a 2f 0a 0a 20 20 67 r eOp==2 */.. g
23530 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
23540 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 ;. aPayload = p
23550 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f Cur->info.pPaylo
23560 61 64 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ad;.#ifdef SQLIT
23570 45 5f 44 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f E_DIRECT_OVERFLO
23580 57 5f 52 45 41 44 0a 20 20 62 45 6e 64 20 3d 20 W_READ. bEnd =
23590 6f 66 66 73 65 74 2b 61 6d 74 3d 3d 70 43 75 72 offset+amt==pCur
235a0 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 3b ->info.nPayload;
235b0 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 .#endif. assert
235c0 28 20 6f 66 66 73 65 74 2b 61 6d 74 20 3c 3d 20 ( offset+amt <=
235d0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c pCur->info.nPayl
235e0 6f 61 64 20 29 3b 0a 0a 20 20 69 66 28 20 26 61 oad );.. if( &a
235f0 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e Payload[pCur->in
23600 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 fo.nLocal] > &pP
23610 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
23620 75 73 61 62 6c 65 53 69 7a 65 5d 20 29 7b 0a 20 usableSize] ){.
23630 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 /* Trying to
23640 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 70 61 read or write pa
23650 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 st the end of th
23660 65 20 64 61 74 61 20 69 73 20 61 6e 20 65 72 72 e data is an err
23670 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e or */. return
23680 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
23690 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 BKPT;. }.. /*
236a0 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75 Check if data mu
236b0 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 74 74 st be read/writt
236c0 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 en to/from the b
236d0 74 72 65 65 20 70 61 67 65 20 69 74 73 65 6c 66 tree page itself
236e0 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 . */. if( offse
236f0 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f t<pCur->info.nLo
23700 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 cal ){. int a
23710 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 = amt;. if(
23720 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 a+offset>pCur->i
23730 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 nfo.nLocal ){.
23740 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e a = pCur->in
23750 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 fo.nLocal - offs
23760 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 et;. }. rc
23770 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 = copyPayload(&
23780 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d aPayload[offset]
23790 2c 20 70 42 75 66 2c 20 61 2c 20 28 65 4f 70 20 , pBuf, a, (eOp
237a0 26 20 30 78 30 31 29 2c 20 70 50 61 67 65 2d 3e & 0x01), pPage->
237b0 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 6f 66 pDbPage);. of
237c0 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 70 42 fset = 0;. pB
237d0 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 61 6d 74 uf += a;. amt
237e0 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a -= a;. }else{.
237f0 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 70 43 offset -= pC
23800 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b ur->info.nLocal;
23810 0a 20 20 7d 0a 0a 0a 20 20 69 66 28 20 72 63 3d . }... if( rc=
23820 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d =SQLITE_OK && am
23830 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 t>0 ){. const
23840 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20 3d 20 u32 ovflSize =
23850 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
23860 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73 20 63 - 4; /* Bytes c
23870 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66 6c 20 ontent per ovfl
23880 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f page */. Pgno
23890 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20 20 20 nextPage;..
238a0 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 34 62 nextPage = get4b
238b0 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b 70 43 yte(&aPayload[pC
238c0 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d ur->info.nLocal]
238d0 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
238e0 65 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 e BtCursor.aOver
238f0 66 6c 6f 77 5b 5d 20 68 61 73 20 6e 6f 74 20 62 flow[] has not b
23900 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 een allocated, a
23910 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a llocate it now..
23920 20 20 20 20 2a 2a 20 45 78 63 65 70 74 2c 20 64 ** Except, d
23930 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 61 o not allocate a
23940 4f 76 65 72 66 6c 6f 77 5b 5d 20 66 6f 72 20 65 Overflow[] for e
23950 4f 70 3d 3d 32 2e 0a 20 20 20 20 2a 2a 0a 20 20 Op==2.. **.
23960 20 20 2a 2a 20 54 68 65 20 61 4f 76 65 72 66 6c ** The aOverfl
23970 6f 77 5b 5d 20 61 72 72 61 79 20 69 73 20 73 69 ow[] array is si
23980 7a 65 64 20 61 74 20 6f 6e 65 20 65 6e 74 72 79 zed at one entry
23990 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66 6c for each overfl
239a0 6f 77 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 69 ow page. ** i
239b0 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 n the overflow c
239c0 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 20 6e hain. The page n
239d0 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69 72 umber of the fir
239e0 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 st overflow page
239f0 20 69 73 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 is. ** store
23a00 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 d in aOverflow[0
23a10 5d 2c 20 65 74 63 2e 20 41 20 76 61 6c 75 65 20 ], etc. A value
23a20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76 65 of 0 in the aOve
23a30 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 0a 20 20 rflow[] array.
23a40 20 20 2a 2a 20 6d 65 61 6e 73 20 22 6e 6f 74 20 ** means "not
23a50 79 65 74 20 6b 6e 6f 77 6e 22 20 28 74 68 65 20 yet known" (the
23a60 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79 20 cache is lazily
23a70 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20 20 populated)..
23a80 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f 70 21 3d */. if( eOp!=
23a90 32 20 26 26 20 28 70 43 75 72 2d 3e 63 75 72 46 2 && (pCur->curF
23aa0 6c 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c 69 lags & BTCF_Vali
23ab0 64 4f 76 66 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 dOvfl)==0 ){.
23ac0 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 int nOvfl = (
23ad0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c pCur->info.nPayl
23ae0 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e oad-pCur->info.n
23af0 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 Local+ovflSize-1
23b00 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 )/ovflSize;.
23b10 20 20 69 66 28 20 6e 4f 76 66 6c 3e 70 43 75 72 if( nOvfl>pCur
23b20 2d 3e 6e 4f 76 66 6c 41 6c 6c 6f 63 20 29 7b 0a ->nOvflAlloc ){.
23b30 20 20 20 20 20 20 20 20 50 67 6e 6f 20 2a 61 4e Pgno *aN
23b40 65 77 20 3d 20 28 50 67 6e 6f 2a 29 73 71 6c 69 ew = (Pgno*)sqli
23b50 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 te3Realloc(.
23b60 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f pCur->aO
23b70 76 65 72 66 6c 6f 77 2c 20 6e 4f 76 66 6c 2a 32 verflow, nOvfl*2
23b80 2a 73 69 7a 65 6f 66 28 50 67 6e 6f 29 0a 20 20 *sizeof(Pgno).
23b90 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
23ba0 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a if( aNew==0 ){.
23bb0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
23bc0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
23bd0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
23be0 20 20 20 20 20 20 70 43 75 72 2d 3e 6e 4f 76 66 pCur->nOvf
23bf0 6c 41 6c 6c 6f 63 20 3d 20 6e 4f 76 66 6c 2a 32 lAlloc = nOvfl*2
23c00 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 ;. pCur
23c10 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 61 4e ->aOverflow = aN
23c20 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ew;. }.
23c30 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
23c40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
23c50 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
23c60 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 2c pCur->aOverflow,
23c70 20 30 2c 20 6e 4f 76 66 6c 2a 73 69 7a 65 6f 66 0, nOvfl*sizeof
23c80 28 50 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 20 (Pgno));.
23c90 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 pCur->curFlags
23ca0 7c 3d 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 |= BTCF_ValidOvf
23cb0 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d l;. }. }
23cc0 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 .. /* If the
23cd0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
23ce0 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 st cache has bee
23cf0 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 n allocated and
23d00 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 the. ** entry
23d10 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 for the first r
23d20 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 equired overflow
23d30 20 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 page is valid,
23d40 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 skip. ** dire
23d50 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 ctly to it..
23d60 2a 2f 0a 20 20 20 20 69 66 28 20 28 70 43 75 72 */. if( (pCur
23d70 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 ->curFlags & BTC
23d80 46 5f 56 61 6c 69 64 4f 76 66 6c 29 21 3d 30 0a F_ValidOvfl)!=0.
23d90 20 20 20 20 20 26 26 20 70 43 75 72 2d 3e 61 4f && pCur->aO
23da0 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f verflow[offset/o
23db0 76 66 6c 53 69 7a 65 5d 0a 20 20 20 20 29 7b 0a vflSize]. ){.
23dc0 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f 66 iIdx = (of
23dd0 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a fset/ovflSize);.
23de0 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d nextPage =
23df0 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
23e00 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 [iIdx];. of
23e10 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f fset = (offset%o
23e20 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a vflSize);. }.
23e30 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d . for( ; rc==
23e40 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 SQLITE_OK && amt
23e50 3e 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b 20 >0 && nextPage;
23e60 69 49 64 78 2b 2b 29 7b 0a 0a 20 20 20 20 20 20 iIdx++){..
23e70 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 /* If required,
23e80 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 populate the ove
23e90 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
23ea0 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 cache. */.
23eb0 69 66 28 20 28 70 43 75 72 2d 3e 63 75 72 46 6c if( (pCur->curFl
23ec0 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c 69 64 ags & BTCF_Valid
23ed0 4f 76 66 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 Ovfl)!=0 ){.
23ee0 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72 assert(!pCur
23ef0 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
23f00 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 ] || pCur->aOver
23f10 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 flow[iIdx]==next
23f20 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 Page);. p
23f30 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
23f40 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b Idx] = nextPage;
23f50 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
23f60 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c if( offset>=ovfl
23f70 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
23f80 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 /* The only reas
23f90 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 on to read this
23fa0 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69 page is to obtai
23fb0 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 n the page.
23fc0 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 ** number for
23fd0 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
23fe0 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 n the overflow c
23ff0 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20 hain. The page.
24000 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69 ** data i
24010 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20 s not required.
24020 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 6f 20 So first try to
24030 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66 lookup the overf
24040 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 low. ** p
24050 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20 age-list cache,
24060 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c if any, then fal
24070 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65 l back to the ge
24080 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a tOverflowPage().
24090 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 ** funct
240a0 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ion.. **.
240b0 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 ** Note
240c0 74 68 61 74 20 74 68 65 20 61 4f 76 65 72 66 6c that the aOverfl
240d0 6f 77 5b 5d 20 61 72 72 61 79 20 6d 75 73 74 20 ow[] array must
240e0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 be allocated bec
240f0 61 75 73 65 20 65 4f 70 21 3d 32 0a 20 20 20 20 ause eOp!=2.
24100 20 20 20 20 2a 2a 20 68 65 72 65 2e 20 20 49 66 ** here. If
24110 20 65 4f 70 3d 3d 32 2c 20 74 68 65 6e 20 6f 66 eOp==2, then of
24120 66 73 65 74 3d 3d 30 20 61 6e 64 20 74 68 69 73 fset==0 and this
24130 20 62 72 61 6e 63 68 20 69 73 20 6e 65 76 65 72 branch is never
24140 20 74 61 6b 65 6e 2e 0a 20 20 20 20 20 20 20 20 taken..
24150 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
24160 74 28 20 65 4f 70 21 3d 32 20 29 3b 0a 20 20 20 t( eOp!=2 );.
24170 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
24180 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 r->curFlags & BT
24190 43 46 5f 56 61 6c 69 64 4f 76 66 6c 20 29 3b 0a CF_ValidOvfl );.
241a0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
241b0 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
241c0 3d 3d 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 20 ==pBt->db );.
241d0 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 if( pCur->a
241e0 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d Overflow[iIdx+1]
241f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 ){. ne
24200 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 xtPage = pCur->a
24210 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d Overflow[iIdx+1]
24220 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
24230 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
24240 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 getOverflowPage(
24250 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 pBt, nextPage, 0
24260 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 , &nextPage);.
24270 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
24280 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 offset -= ovflSi
24290 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ze;. }else{
242a0 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 . /* Need
242b0 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 to read this pa
242c0 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 ge properly. It
242d0 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 contains some of
242e0 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
242f0 72 61 6e 67 65 20 6f 66 20 64 61 74 61 20 74 68 range of data th
24300 61 74 20 69 73 20 62 65 69 6e 67 20 72 65 61 64 at is being read
24310 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 (eOp==0) or wri
24320 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 tten (eOp!=0)..
24330 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 */.#ifdef
24340 20 53 51 4c 49 54 45 5f 44 49 52 45 43 54 5f 4f SQLITE_DIRECT_O
24350 56 45 52 46 4c 4f 57 5f 52 45 41 44 0a 20 20 20 VERFLOW_READ.
24360 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c sqlite3_fil
24370 65 20 2a 66 64 3b 0a 23 65 6e 64 69 66 0a 20 20 e *fd;.#endif.
24380 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d int a = am
24390 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 t;. if( a
243a0 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c + offset > ovfl
243b0 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
243c0 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d a = ovflSize -
243d0 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 offset;.
243e0 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }..#ifdef SQLIT
243f0 45 5f 44 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f E_DIRECT_OVERFLO
24400 57 5f 52 45 41 44 0a 20 20 20 20 20 20 20 20 2f W_READ. /
24410 2a 20 49 66 20 61 6c 6c 20 74 68 65 20 66 6f 6c * If all the fol
24420 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a lowing are true:
24430 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
24440 20 20 20 20 2a 2a 20 20 20 31 29 20 74 68 69 73 ** 1) this
24450 20 69 73 20 61 20 72 65 61 64 20 6f 70 65 72 61 is a read opera
24460 74 69 6f 6e 2c 20 61 6e 64 20 0a 20 20 20 20 20 tion, and .
24470 20 20 20 2a 2a 20 20 20 32 29 20 64 61 74 61 20 ** 2) data
24480 69 73 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d is required from
24490 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
244a0 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 is overflow page
244b0 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a , and. **
244c0 20 20 20 33 29 20 74 68 65 20 64 61 74 61 62 61 3) the databa
244d0 73 65 20 69 73 20 66 69 6c 65 2d 62 61 63 6b 65 se is file-backe
244e0 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a d, and. *
244f0 2a 20 20 20 34 29 20 74 68 65 72 65 20 69 73 20 * 4) there is
24500 6e 6f 20 6f 70 65 6e 20 77 72 69 74 65 2d 74 72 no open write-tr
24510 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 ansaction, and.
24520 20 20 20 20 20 20 20 2a 2a 20 20 20 35 29 20 74 ** 5) t
24530 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e he database is n
24540 6f 74 20 61 20 57 41 4c 20 64 61 74 61 62 61 73 ot a WAL databas
24550 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 e,. **
24560 36 29 20 61 6c 6c 20 64 61 74 61 20 66 72 6f 6d 6) all data from
24570 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65 69 the page is bei
24580 6e 67 20 72 65 61 64 2e 0a 20 20 20 20 20 20 20 ng read..
24590 20 2a 2a 20 20 20 37 29 20 61 74 20 6c 65 61 73 ** 7) at leas
245a0 74 20 34 20 62 79 74 65 73 20 68 61 76 65 20 61 t 4 bytes have a
245b0 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65 61 64 lready been read
245c0 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 into the output
245d0 20 62 75 66 66 65 72 20 0a 20 20 20 20 20 20 20 buffer .
245e0 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 **. ** t
245f0 68 65 6e 20 64 61 74 61 20 63 61 6e 20 62 65 20 hen data can be
24600 72 65 61 64 20 64 69 72 65 63 74 6c 79 20 66 72 read directly fr
24610 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
24620 66 69 6c 65 20 69 6e 74 6f 20 74 68 65 0a 20 20 file into the.
24630 20 20 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 ** output
24640 62 75 66 66 65 72 2c 20 62 79 70 61 73 73 69 6e buffer, bypassin
24650 67 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 g the page-cache
24660 20 61 6c 74 6f 67 65 74 68 65 72 2e 20 54 68 69 altogether. Thi
24670 73 20 73 70 65 65 64 73 0a 20 20 20 20 20 20 20 s speeds.
24680 20 2a 2a 20 75 70 20 6c 6f 61 64 69 6e 67 20 6c ** up loading l
24690 61 72 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 arge records tha
246a0 74 20 73 70 61 6e 20 6d 61 6e 79 20 6f 76 65 72 t span many over
246b0 66 6c 6f 77 20 70 61 67 65 73 2e 0a 20 20 20 20 flow pages..
246c0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 */. i
246d0 66 28 20 28 65 4f 70 26 30 78 30 31 29 3d 3d 30 f( (eOp&0x01)==0
246e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
246f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24700 20 20 20 20 20 20 2f 2a 20 28 31 29 20 2a 2f 0a /* (1) */.
24710 20 20 20 20 20 20 20 20 20 26 26 20 6f 66 66 73 && offs
24720 65 74 3d 3d 30 20 20 20 20 20 20 20 20 20 20 20 et==0
24730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
24750 2a 20 28 32 29 20 2a 2f 0a 20 20 20 20 20 20 20 * (2) */.
24760 20 20 26 26 20 28 62 45 6e 64 20 7c 7c 20 61 3d && (bEnd || a=
24770 3d 6f 76 66 6c 53 69 7a 65 29 20 20 20 20 20 20 =ovflSize)
24780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24790 20 20 20 20 20 20 20 20 2f 2a 20 28 36 29 20 2a /* (6) *
247a0 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 42 /. && pB
247b0 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
247c0 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 20 20 20 ==TRANS_READ
247d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
247e0 20 2f 2a 20 28 34 29 20 2a 2f 0a 20 20 20 20 20 /* (4) */.
247f0 20 20 20 20 26 26 20 28 66 64 20 3d 20 73 71 6c && (fd = sql
24800 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 ite3PagerFile(pB
24810 74 2d 3e 70 50 61 67 65 72 29 29 2d 3e 70 4d 65 t->pPager))->pMe
24820 74 68 6f 64 73 20 20 20 20 20 2f 2a 20 28 33 29 thods /* (3)
24830 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 */. &&
24840 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
24850 74 61 5b 31 39 5d 3d 3d 30 78 30 31 20 20 20 20 ta[19]==0x01
24860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24870 20 20 20 2f 2a 20 28 35 29 20 2a 2f 0a 20 20 20 /* (5) */.
24880 20 20 20 20 20 20 26 26 20 26 70 42 75 66 5b 2d && &pBuf[-
24890 34 5d 3e 3d 70 42 75 66 53 74 61 72 74 20 20 20 4]>=pBufStart
248a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
248b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 28 /* (
248c0 37 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 29 7b 7) */. ){
248d0 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 61 53 . u8 aS
248e0 61 76 65 5b 34 5d 3b 0a 20 20 20 20 20 20 20 20 ave[4];.
248f0 20 20 75 38 20 2a 61 57 72 69 74 65 20 3d 20 26 u8 *aWrite = &
24900 70 42 75 66 5b 2d 34 5d 3b 0a 20 20 20 20 20 20 pBuf[-4];.
24910 20 20 20 20 61 73 73 65 72 74 28 20 61 57 72 69 assert( aWri
24920 74 65 3e 3d 70 42 75 66 53 74 61 72 74 20 29 3b te>=pBufStart );
24930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24940 20 20 20 20 20 20 20 20 20 2f 2a 20 68 65 6e 63 /* henc
24950 65 20 28 37 29 20 2a 2f 0a 20 20 20 20 20 20 20 e (7) */.
24960 20 20 20 6d 65 6d 63 70 79 28 61 53 61 76 65 2c memcpy(aSave,
24970 20 61 57 72 69 74 65 2c 20 34 29 3b 0a 20 20 20 aWrite, 4);.
24980 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
24990 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 57 te3OsRead(fd, aW
249a0 72 69 74 65 2c 20 61 2b 34 2c 20 28 69 36 34 29 rite, a+4, (i64)
249b0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2a 28 6e pBt->pageSize*(n
249c0 65 78 74 50 61 67 65 2d 31 29 29 3b 0a 20 20 20 extPage-1));.
249d0 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 nextPage
249e0 3d 20 67 65 74 34 62 79 74 65 28 61 57 72 69 74 = get4byte(aWrit
249f0 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 e);. me
24a00 6d 63 70 79 28 61 57 72 69 74 65 2c 20 61 53 61 mcpy(aWrite, aSa
24a10 76 65 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 ve, 4);.
24a20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 }else.#endif..
24a30 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 {.
24a40 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
24a50 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 e;. rc
24a60 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 = sqlite3PagerAc
24a70 71 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 quire(pBt->pPage
24a80 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44 r, nextPage, &pD
24a90 62 50 61 67 65 2c 0a 20 20 20 20 20 20 20 20 20 bPage,.
24aa0 20 20 20 20 20 28 28 65 4f 70 26 30 78 30 31 29 ((eOp&0x01)
24ab0 3d 3d 30 20 3f 20 50 41 47 45 52 5f 47 45 54 5f ==0 ? PAGER_GET_
24ac0 52 45 41 44 4f 4e 4c 59 20 3a 20 30 29 0a 20 20 READONLY : 0).
24ad0 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
24ae0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
24af0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
24b00 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d aPayload =
24b10 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
24b20 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
24b30 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 nextP
24b40 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 age = get4byte(a
24b50 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 Payload);.
24b60 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 rc = copyP
24b70 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 ayload(&aPayload
24b80 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 [offset+4], pBuf
24b90 2c 20 61 2c 20 28 65 4f 70 26 30 78 30 31 29 2c , a, (eOp&0x01),
24ba0 20 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 pDbPage);.
24bb0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
24bc0 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 gerUnref(pDbPage
24bd0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f );. o
24be0 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 ffset = 0;.
24bf0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
24c00 0a 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 . amt -=
24c10 61 3b 0a 20 20 20 20 20 20 20 20 70 42 75 66 20 a;. pBuf
24c20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 += a;. }.
24c30 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 }. }.. if( r
24c40 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
24c50 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 amt>0 ){. ret
24c60 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
24c70 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 PT_BKPT;. }. r
24c80 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
24c90 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 ** Read part of
24ca0 74 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 the key associat
24cb0 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 ed with cursor p
24cc0 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a Cur. Exactly.**
24cd0 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c "amt" bytes wil
24ce0 6c 20 62 65 20 74 72 61 6e 73 66 65 72 72 65 64 l be transferred
24cf0 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 into pBuf[]. T
24d00 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 he transfer.** b
24d10 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 egins at "offset
24d20 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c "..**.** The cal
24d30 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 ler must ensure
24d40 74 68 61 74 20 70 43 75 72 20 69 73 20 70 6f 69 that pCur is poi
24d50 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 nting to a valid
24d60 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 74 row.** in the t
24d70 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 able..**.** Retu
24d80 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
24d90 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 success or an er
24da0 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 ror code if anyt
24db0 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f hing goes.** wro
24dc0 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 ng. An error is
24dd0 20 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 returned if "of
24de0 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 fset+amt" is lar
24df0 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 ger than.** the
24e00 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 available payloa
24e10 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 d..*/.int sqlite
24e20 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 3BtreeKey(BtCurs
24e30 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 or *pCur, u32 of
24e40 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 fset, u32 amt, v
24e50 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 61 73 oid *pBuf){. as
24e60 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
24e70 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
24e80 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
24e90 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
24ea0 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
24eb0 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 ( pCur->iPage>=0
24ec0 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 && pCur->apPage
24ed0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b [pCur->iPage] );
24ee0 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
24ef0 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
24f00 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 ge]<pCur->apPage
24f10 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
24f20 43 65 6c 6c 20 29 3b 0a 20 20 72 65 74 75 72 6e Cell );. return
24f30 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 accessPayload(p
24f40 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 Cur, offset, amt
24f50 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 , (unsigned char
24f60 2a 29 70 42 75 66 2c 20 30 29 3b 0a 7d 0a 0a 2f *)pBuf, 0);.}../
24f70 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f *.** Read part o
24f80 66 20 74 68 65 20 64 61 74 61 20 61 73 73 6f 63 f the data assoc
24f90 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f iated with curso
24fa0 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 r pCur. Exactly
24fb0 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 .** "amt" bytes
24fc0 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 will be transfer
24fd0 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 ed into pBuf[].
24fe0 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a The transfer.**
24ff0 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 begins at "offs
25000 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 et"..**.** Retur
25010 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
25020 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 uccess or an err
25030 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 or code if anyth
25040 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e ing goes.** wron
25050 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 g. An error is
25060 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 returned if "off
25070 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 set+amt" is larg
25080 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 er than.** the a
25090 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 vailable payload
250a0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
250b0 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 73 BtreeData(BtCurs
250c0 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 or *pCur, u32 of
250d0 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 fset, u32 amt, v
250e0 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e oid *pBuf){. in
250f0 74 20 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53 t rc;..#ifndef S
25100 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
25110 4c 4f 42 0a 20 20 69 66 20 28 20 70 43 75 72 2d LOB. if ( pCur-
25120 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
25130 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 INVALID ){. r
25140 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f eturn SQLITE_ABO
25150 52 54 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a RT;. }.#endif..
25160 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
25170 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
25180 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
25190 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
251a0 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
251b0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
251c0 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
251d0 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
251e0 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 73 VALID );. ass
251f0 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 ert( pCur->iPage
25200 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 >=0 && pCur->apP
25210 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
25220 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
25230 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
25240 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 ->iPage]<pCur->a
25250 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
25260 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 e]->nCell );.
25270 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c rc = accessPayl
25280 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 oad(pCur, offset
25290 2c 20 61 6d 74 2c 20 70 42 75 66 2c 20 30 29 3b , amt, pBuf, 0);
252a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
252b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
252c0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 n a pointer to p
252d0 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 ayload informati
252e0 6f 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 on from the entr
252f0 79 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 y that the .** p
25300 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f Cur cursor is po
25310 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 inting to. The
25320 70 6f 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 pointer is to th
25330 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a e beginning of.*
25340 2a 20 74 68 65 20 6b 65 79 20 69 66 20 69 6e 64 * the key if ind
25350 65 78 20 62 74 72 65 65 73 20 28 70 50 61 67 65 ex btrees (pPage
25360 2d 3e 69 6e 74 4b 65 79 3d 3d 30 29 20 61 6e 64 ->intKey==0) and
25370 20 69 73 20 74 68 65 20 64 61 74 61 20 66 6f 72 is the data for
25380 0a 2a 2a 20 74 61 62 6c 65 20 62 74 72 65 65 73 .** table btrees
25390 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d (pPage->intKey=
253a0 3d 31 29 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 =1). The number
253b0 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 of bytes of avai
253c0 6c 61 62 6c 65 0a 2a 2a 20 6b 65 79 2f 64 61 74 lable.** key/dat
253d0 61 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 a is written int
253e0 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 o *pAmt. If *pA
253f0 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 mt==0, then the
25400 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 value.** returne
25410 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 d will not be a
25420 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a valid pointer..*
25430 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
25440 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 e is an optimiza
25450 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d tion. It is com
25460 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69 mon for the enti
25470 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 re key.** and da
25480 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 ta to fit on the
25490 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e 64 20 local page and
254a0 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 for there to be
254b0 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 no overflow.** p
254c0 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68 61 74 ages. When that
254d0 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 is so, this rou
254e0 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 tine can be used
254f0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 0a 2a to access the.*
25500 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 77 * key and data w
25510 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 ithout making a
25520 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20 6b 65 copy. If the ke
25530 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 y and/or data sp
25540 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 ills.** onto ove
25550 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 65 rflow pages, the
25560 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 n accessPayload(
25570 29 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 74 ) must be used t
25580 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a 2a 2a 20 o reassemble.**
25590 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 the key/data and
255a0 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20 61 20 copy it into a
255b0 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 preallocated buf
255c0 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 fer..**.** The p
255d0 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 ointer returned
255e0 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 by this routine
255f0 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79 20 69 looks directly i
25600 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64 0a 2a nto the cached.*
25610 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 * page of the da
25620 74 61 62 61 73 65 2e 20 20 54 68 65 20 64 61 74 tabase. The dat
25630 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 6f a might change o
25640 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78 74 20 r move the next
25650 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 time.** any btre
25660 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c e routine is cal
25670 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 led..*/.static c
25680 6f 6e 73 74 20 76 6f 69 64 20 2a 66 65 74 63 68 onst void *fetch
25690 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 Payload(. BtCur
256a0 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
256b0 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 /* Cursor pointi
256c0 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 ng to entry to r
256d0 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 ead from */. u3
256e0 32 20 2a 70 41 6d 74 20 20 20 20 20 20 20 20 20 2 *pAmt
256f0 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
25700 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 number of availa
25710 62 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a ble bytes here *
25720 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 /.){. assert( p
25730 43 75 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e Cur!=0 && pCur->
25740 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 iPage>=0 && pCur
25750 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
25760 50 61 67 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 Page]);. assert
25770 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
25780 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
25790 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
257a0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
257b0 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
257c0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
257d0 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
257e0 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
257f0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
25800 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
25810 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
25820 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
25830 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
25840 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3e 30 ur->info.nSize>0
25850 20 29 3b 0a 20 20 2a 70 41 6d 74 20 3d 20 70 43 );. *pAmt = pC
25860 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b ur->info.nLocal;
25870 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
25880 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 )pCur->info.pPay
25890 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 load;.}.../*.**
258a0 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 For the entry th
258b0 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 at cursor pCur i
258c0 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 s point to, retu
258d0 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 rn as.** many by
258e0 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f tes of the key o
258f0 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61 76 r data as are av
25900 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c ailable on the l
25910 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 ocal.** b-tree p
25920 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 age. Write the
25930 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 number of availa
25940 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a ble bytes into *
25950 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 pAmt..**.** The
25960 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 pointer returned
25970 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 is ephemeral.
25980 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 The key/data may
25990 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 move.** or be d
259a0 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 estroyed on the
259b0 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 next call to any
259c0 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a Btree routine,.
259d0 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c ** including cal
259e0 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 ls from other th
259f0 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 reads against th
25a00 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a e same cache..**
25a10 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 Hence, a mutex
25a20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 on the BtShared
25a30 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 should be held p
25a40 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a rior to calling.
25a50 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ** this routine.
25a60 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
25a70 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f tines is used to
25a80 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 get quick acces
25a90 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 s to key and dat
25aa0 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d a.** in the comm
25ab0 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f on case where no
25ac0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
25ad0 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 63 6f 6e are used..*/.con
25ae0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
25af0 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 BtreeKeyFetch(Bt
25b00 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 Cursor *pCur, u3
25b10 32 20 2a 70 41 6d 74 29 7b 0a 20 20 72 65 74 75 2 *pAmt){. retu
25b20 72 6e 20 66 65 74 63 68 50 61 79 6c 6f 61 64 28 rn fetchPayload(
25b30 70 43 75 72 2c 20 70 41 6d 74 29 3b 0a 7d 0a 63 pCur, pAmt);.}.c
25b40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
25b50 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
25b60 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
25b70 20 75 33 32 20 2a 70 41 6d 74 29 7b 0a 20 20 72 u32 *pAmt){. r
25b80 65 74 75 72 6e 20 66 65 74 63 68 50 61 79 6c 6f eturn fetchPaylo
25b90 61 64 28 70 43 75 72 2c 20 70 41 6d 74 29 3b 0a ad(pCur, pAmt);.
25ba0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 }.../*.** Move t
25bb0 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 he cursor down t
25bc0 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 o a new child pa
25bd0 67 65 2e 20 20 54 68 65 20 6e 65 77 50 67 6e 6f ge. The newPgno
25be0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
25bf0 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 .** page number
25c00 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 of the child pag
25c10 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2a e to move to..**
25c20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
25c30 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 n returns SQLITE
25c40 5f 43 4f 52 52 55 50 54 20 69 66 20 74 68 65 20 _CORRUPT if the
25c50 70 61 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67 page-header flag
25c60 73 20 66 69 65 6c 64 20 6f 66 0a 2a 2a 20 74 68 s field of.** th
25c70 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 e new child page
25c80 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
25c90 74 68 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 the flags field
25ca0 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 28 69 of the parent (i
25cb0 2e 65 2e 0a 2a 2a 20 69 66 20 61 6e 20 69 6e 74 .e..** if an int
25cc0 6b 65 79 20 70 61 67 65 20 61 70 70 65 61 72 73 key page appears
25cd0 20 74 6f 20 62 65 20 74 68 65 20 70 61 72 65 6e to be the paren
25ce0 74 20 6f 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b 65 t of a non-intke
25cf0 79 20 70 61 67 65 2c 20 6f 72 0a 2a 2a 20 76 69 y page, or.** vi
25d00 63 65 2d 76 65 72 73 61 29 2e 0a 2a 2f 0a 73 74 ce-versa)..*/.st
25d10 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43 atic int moveToC
25d20 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a 70 hild(BtCursor *p
25d30 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f Cur, u32 newPgno
25d40 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
25d50 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50 61 nt i = pCur->iPa
25d60 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ge;. MemPage *p
25d70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68 61 NewPage;. BtSha
25d80 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d red *pBt = pCur-
25d90 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 >pBt;.. assert(
25da0 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
25db0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
25dc0 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
25dd0 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
25de0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
25df0 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 4f r->iPage<BTCURSO
25e00 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 R_MAX_DEPTH );.
25e10 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
25e20 50 61 67 65 3e 3d 30 20 29 3b 0a 20 20 69 66 28 Page>=0 );. if(
25e30 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 pCur->iPage>=(B
25e40 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 TCURSOR_MAX_DEPT
25e50 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 H-1) ){. retu
25e60 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
25e70 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 T_BKPT;. }. rc
25e80 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
25e90 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 e(pBt, newPgno,
25ea0 26 70 4e 65 77 50 61 67 65 2c 0a 20 20 20 20 20 &pNewPage,.
25eb0 20 20 20 20 20 20 20 20 20 20 28 70 43 75 72 2d (pCur-
25ec0 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 >curFlags & BTCF
25ed0 5f 57 72 69 74 65 46 6c 61 67 29 3d 3d 30 20 3f _WriteFlag)==0 ?
25ee0 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f PAGER_GET_READO
25ef0 4e 4c 59 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 NLY : 0);. if(
25f00 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
25f10 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 pCur->apPage[i
25f20 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a +1] = pNewPage;.
25f30 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b pCur->aiIdx[i+
25f40 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 1] = 0;. pCur->
25f50 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 iPage++;.. pCur
25f60 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
25f70 3b 0a 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 ;. pCur->curFla
25f80 67 73 20 26 3d 20 7e 28 42 54 43 46 5f 56 61 6c gs &= ~(BTCF_Val
25f90 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 6c 69 idNKey|BTCF_Vali
25fa0 64 4f 76 66 6c 29 3b 0a 20 20 69 66 28 20 70 4e dOvfl);. if( pN
25fb0 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 ewPage->nCell<1
25fc0 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 || pNewPage->int
25fd0 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 Key!=pCur->apPag
25fe0 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a e[i]->intKey ){.
25ff0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
26000 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
26010 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
26020 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 ITE_OK;.}..#if S
26030 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a QLITE_DEBUG./*.*
26040 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 * Page pParent i
26050 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e s an internal (n
26060 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 on-leaf) tree pa
26070 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ge. This functio
26080 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 n .** asserts th
26090 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 at page number i
260a0 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 Child is the lef
260b0 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 t-child if the i
260c0 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 Idx'th.** cell i
260d0 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 n page pParent.
260e0 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 Or, if iIdx is e
260f0 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 qual to the tota
26100 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 l number of.** c
26110 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c ells in pParent,
26120 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 that page numbe
26130 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 r iChild is the
26140 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a right-child of.*
26150 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 * the page..*/.s
26160 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 tatic void asser
26170 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d tParentIndex(Mem
26180 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 Page *pParent, i
26190 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 nt iIdx, Pgno iC
261a0 68 69 6c 64 29 7b 0a 20 20 69 66 28 20 43 4f 52 hild){. if( COR
261b0 52 55 50 54 5f 44 42 20 29 20 72 65 74 75 72 6e RUPT_DB ) return
261c0 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 ; /* The condit
261d0 69 6f 6e 73 20 74 65 73 74 65 64 20 62 65 6c 6f ions tested belo
261e0 77 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 74 w might not be t
261f0 72 75 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 rue.
26200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26210 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 70 74 20 ** in a corrupt
26220 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 61 73 database */. as
26230 73 65 72 74 28 20 69 49 64 78 3c 3d 70 50 61 72 sert( iIdx<=pPar
26240 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 ent->nCell );.
26250 69 66 28 20 69 49 64 78 3d 3d 70 50 61 72 65 6e if( iIdx==pParen
26260 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 t->nCell ){.
26270 61 73 73 65 72 74 28 20 67 65 74 34 62 79 74 65 assert( get4byte
26280 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 (&pParent->aData
26290 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 [pParent->hdrOff
262a0 73 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 set+8])==iChild
262b0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
262c0 61 73 73 65 72 74 28 20 67 65 74 34 62 79 74 65 assert( get4byte
262d0 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e (findCell(pParen
262e0 74 2c 20 69 49 64 78 29 29 3d 3d 69 43 68 69 6c t, iIdx))==iChil
262f0 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 d );. }.}.#else
26300 0a 23 20 20 64 65 66 69 6e 65 20 61 73 73 65 72 .# define asser
26310 74 50 61 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 tParentIndex(x,y
26320 2c 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a ,z) .#endif../*.
26330 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
26340 6f 72 20 75 70 20 74 6f 20 74 68 65 20 70 61 72 or up to the par
26350 65 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 ent page..**.**
26360 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 pCur->idx is set
26370 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 to the cell ind
26380 65 78 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ex that contains
26390 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 the pointer.**
263a0 74 6f 20 74 68 65 20 70 61 67 65 20 77 65 20 61 to the page we a
263b0 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 re coming from.
263c0 20 49 66 20 77 65 20 61 72 65 20 63 6f 6d 69 6e If we are comin
263d0 67 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 g from the.** ri
263e0 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 ght-most child p
263f0 61 67 65 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 age then pCur->i
26400 64 78 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 dx is set to one
26410 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 more than.** th
26420 65 20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 69 e largest cell i
26430 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ndex..*/.static
26440 76 6f 69 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e void moveToParen
26450 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
26460 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
26470 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
26480 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
26490 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
264a0 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
264b0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
264c0 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 Page>0 );. asse
264d0 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 rt( pCur->apPage
264e0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b [pCur->iPage] );
264f0 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 . assertParentI
26500 6e 64 65 78 28 0a 20 20 20 20 70 43 75 72 2d 3e ndex(. pCur->
26510 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
26520 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 ge-1], . pCur
26530 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
26540 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 age-1], . pCu
26550 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
26560 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 29 iPage]->pgno. )
26570 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 43 ;. testcase( pC
26580 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
26590 69 50 61 67 65 2d 31 5d 20 3e 20 70 43 75 72 2d iPage-1] > pCur-
265a0 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
265b0 61 67 65 2d 31 5d 2d 3e 6e 43 65 6c 6c 20 29 3b age-1]->nCell );
265c0 0a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 .. releasePage(
265d0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
265e0 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 20 70 43 r->iPage]);. pC
265f0 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 70 ur->iPage--;. p
26600 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
26610 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 72 = 0;. pCur->cur
26620 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 5f Flags &= ~(BTCF_
26630 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 ValidNKey|BTCF_V
26640 61 6c 69 64 4f 76 66 6c 29 3b 0a 7d 0a 0a 2f 2a alidOvfl);.}../*
26650 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
26660 73 6f 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 sor to point to
26670 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 the root page of
26680 20 69 74 73 20 62 2d 74 72 65 65 20 73 74 72 75 its b-tree stru
26690 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 cture..**.** If
266a0 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 the table has a
266b0 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 virtual root pag
266c0 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 e, then the curs
266d0 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 70 or is moved to p
266e0 6f 69 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 76 oint.** to the v
266f0 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 irtual root page
26700 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 instead of the
26710 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 actual root page
26720 2e 20 41 20 74 61 62 6c 65 20 68 61 73 20 61 0a . A table has a.
26730 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 ** virtual root
26740 70 61 67 65 20 77 68 65 6e 20 74 68 65 20 61 63 page when the ac
26750 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 63 tual root page c
26760 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 ontains no cells
26770 20 61 6e 64 20 61 20 0a 2a 2a 20 73 69 6e 67 6c and a .** singl
26780 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20 54 68 e child page. Th
26790 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 is can only happ
267a0 65 6e 20 77 69 74 68 20 74 68 65 20 74 61 62 6c en with the tabl
267b0 65 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 e rooted at page
267c0 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 1..**.** If the
267d0 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
267e0 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68 65 20 e is empty, the
267f0 63 75 72 73 6f 72 20 73 74 61 74 65 20 69 73 20 cursor state is
26800 73 65 74 20 74 6f 20 0a 2a 2a 20 43 55 52 53 4f set to .** CURSO
26810 52 5f 49 4e 56 41 4c 49 44 2e 20 4f 74 68 65 72 R_INVALID. Other
26820 77 69 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 wise, the cursor
26830 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 is set to point
26840 20 74 6f 20 74 68 65 20 66 69 72 73 74 0a 2a 2a to the first.**
26850 20 63 65 6c 6c 20 6c 6f 63 61 74 65 64 20 6f 6e cell located on
26860 20 74 68 65 20 72 6f 6f 74 20 28 6f 72 20 76 69 the root (or vi
26870 72 74 75 61 6c 20 72 6f 6f 74 29 20 70 61 67 65 rtual root) page
26880 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 and the cursor
26890 73 74 61 74 65 0a 2a 2a 20 69 73 20 73 65 74 20 state.** is set
268a0 74 6f 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e to CURSOR_VALID.
268b0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 66 .**.** If this f
268c0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
268d0 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 69 74 successfully, it
268e0 20 6d 61 79 20 62 65 20 61 73 73 75 6d 65 64 20 may be assumed
268f0 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 61 67 65 that the.** page
26900 2d 68 65 61 64 65 72 20 66 6c 61 67 73 20 69 6e -header flags in
26910 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 dicate that the
26920 5b 76 69 72 74 75 61 6c 5d 20 72 6f 6f 74 2d 70 [virtual] root-p
26930 61 67 65 20 69 73 20 74 68 65 20 65 78 70 65 63 age is the expec
26940 74 65 64 20 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 ted .** kind of
26950 62 2d 74 72 65 65 20 70 61 67 65 20 28 69 2e 65 b-tree page (i.e
26960 2e 20 69 66 20 77 68 65 6e 20 6f 70 65 6e 69 6e . if when openin
26970 67 20 74 68 65 20 63 75 72 73 6f 72 20 74 68 65 g the cursor the
26980 20 63 61 6c 6c 65 72 20 64 69 64 20 6e 6f 74 0a caller did not.
26990 2a 2a 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 ** specify a Key
269a0 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 Info structure t
269b0 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 69 73 he flags byte is
269c0 20 73 65 74 20 74 6f 20 30 78 30 35 20 6f 72 20 set to 0x05 or
269d0 30 78 30 44 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 0x0D,.** indicat
269e0 69 6e 67 20 61 20 74 61 62 6c 65 20 62 2d 74 72 ing a table b-tr
269f0 65 65 2c 20 6f 72 20 69 66 20 74 68 65 20 63 61 ee, or if the ca
26a00 6c 6c 65 72 20 64 69 64 20 73 70 65 63 69 66 79 ller did specify
26a10 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 a KeyInfo .** s
26a20 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 tructure the fla
26a30 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74 gs byte is set t
26a40 6f 20 30 78 30 32 20 6f 72 20 30 78 30 41 2c 20 o 0x02 or 0x0A,
26a50 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e indicating an in
26a60 64 65 78 0a 2a 2a 20 62 2d 74 72 65 65 29 2e 0a dex.** b-tree)..
26a70 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
26a80 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f veToRoot(BtCurso
26a90 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 r *pCur){. MemP
26aa0 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e age *pRoot;. in
26ab0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
26ac0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
26ad0 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
26ae0 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
26af0 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 CURSOR_INVALID
26b00 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 < CURSOR_REQUIRE
26b10 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 SEEK );. assert
26b20 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 ( CURSOR_VALID
26b30 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 < CURSOR_REQUIR
26b40 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 ESEEK );. asser
26b50 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 t( CURSOR_FAULT
26b60 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 > CURSOR_REQUI
26b70 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 RESEEK );. if(
26b80 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 pCur->eState>=CU
26b90 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
26ba0 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 ){. if( pCur
26bb0 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
26bc0 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 _FAULT ){.
26bd0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b assert( pCur->sk
26be0 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f ipNext!=SQLITE_O
26bf0 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 K );. retur
26c00 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 n pCur->skipNext
26c10 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
26c20 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
26c30 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a sor(pCur);. }..
26c40 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
26c50 65 3e 3d 30 20 29 7b 0a 20 20 20 20 77 68 69 6c e>=0 ){. whil
26c60 65 28 20 70 43 75 72 2d 3e 69 50 61 67 65 20 29 e( pCur->iPage )
26c70 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 releasePage(pCu
26c80 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
26c90 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 7d 65 6c iPage--]);. }el
26ca0 73 65 20 69 66 28 20 70 43 75 72 2d 3e 70 67 6e se if( pCur->pgn
26cb0 6f 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 20 20 20 oRoot==0 ){.
26cc0 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
26cd0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
26ce0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
26cf0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
26d00 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 rc = getAndIni
26d10 74 50 61 67 65 28 70 43 75 72 2d 3e 70 42 74 72 tPage(pCur->pBtr
26d20 65 65 2d 3e 70 42 74 2c 20 70 43 75 72 2d 3e 70 ee->pBt, pCur->p
26d30 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e gnoRoot, &pCur->
26d40 61 70 50 61 67 65 5b 30 5d 2c 0a 20 20 20 20 20 apPage[0],.
26d50 20 20 20 20 20 20 20 20 20 20 20 20 28 70 43 75 (pCu
26d60 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 r->curFlags & BT
26d70 43 46 5f 57 72 69 74 65 46 6c 61 67 29 3d 3d 30 CF_WriteFlag)==0
26d80 20 3f 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 ? PAGER_GET_REA
26d90 44 4f 4e 4c 59 20 3a 20 30 29 3b 0a 20 20 20 20 DONLY : 0);.
26da0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
26db0 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d K ){. pCur-
26dc0 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
26dd0 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 _INVALID;.
26de0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
26df0 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 . pCur->iPage
26e00 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 52 6f 6f = 0;. }. pRoo
26e10 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 t = pCur->apPage
26e20 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 [0];. assert( p
26e30 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 Root->pgno==pCur
26e40 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a 0a 20 ->pgnoRoot );..
26e50 20 2f 2a 20 49 66 20 70 43 75 72 2d 3e 70 4b 65 /* If pCur->pKe
26e60 79 49 6e 66 6f 20 69 73 20 6e 6f 74 20 4e 55 4c yInfo is not NUL
26e70 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c L, then the call
26e80 65 72 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 er that opened t
26e90 68 69 73 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 his cursor. **
26ea0 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70 65 6e expected to open
26eb0 20 69 74 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 it on an index
26ec0 62 2d 74 72 65 65 2e 20 4f 74 68 65 72 77 69 73 b-tree. Otherwis
26ed0 65 2c 20 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 e, if pKeyInfo i
26ee0 73 0a 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 s. ** NULL, the
26ef0 20 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 caller expects
26f00 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 a table b-tree.
26f10 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 If this is not t
26f20 68 65 20 63 61 73 65 2c 0a 20 20 2a 2a 20 72 65 he case,. ** re
26f30 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 turn an SQLITE_C
26f40 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 0a 20 ORRUPT error. .
26f50 20 2a 2a 0a 20 20 2a 2a 20 45 61 72 6c 69 65 72 **. ** Earlier
26f60 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
26f70 69 74 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 ite assumed that
26f80 20 74 68 69 73 20 74 65 73 74 20 63 6f 75 6c 64 this test could
26f90 20 6e 6f 74 20 66 61 69 6c 0a 20 20 2a 2a 20 69 not fail. ** i
26fa0 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 f the root page
26fb0 77 61 73 20 61 6c 72 65 61 64 79 20 6c 6f 61 64 was already load
26fc0 65 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ed when this fun
26fd0 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 ction was called
26fe0 20 28 69 2e 65 2e 0a 20 20 2a 2a 20 69 66 20 70 (i.e.. ** if p
26ff0 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 29 2e 20 Cur->iPage>=0).
27000 42 75 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 But this is not
27010 73 6f 20 69 66 20 74 68 65 20 64 61 74 61 62 61 so if the databa
27020 73 65 20 69 73 20 63 6f 72 72 75 70 74 65 64 20 se is corrupted
27030 0a 20 20 2a 2a 20 69 6e 20 73 75 63 68 20 61 20 . ** in such a
27040 77 61 79 20 74 68 61 74 20 70 61 67 65 20 70 52 way that page pR
27050 6f 6f 74 20 69 73 20 6c 69 6e 6b 65 64 20 69 6e oot is linked in
27060 74 6f 20 61 20 73 65 63 6f 6e 64 20 62 2d 74 72 to a second b-tr
27070 65 65 20 74 61 62 6c 65 20 0a 20 20 2a 2a 20 28 ee table . ** (
27080 6f 72 20 74 68 65 20 66 72 65 65 6c 69 73 74 29 or the freelist)
27090 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
270a0 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 3d 3d 31 pRoot->intKey==1
270b0 20 7c 7c 20 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 || pRoot->intKe
270c0 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 52 y==0 );. if( pR
270d0 6f 6f 74 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 7c oot->isInit==0 |
270e0 7c 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 | (pCur->pKeyInf
270f0 6f 3d 3d 30 29 21 3d 70 52 6f 6f 74 2d 3e 69 6e o==0)!=pRoot->in
27100 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 75 tKey ){. retu
27110 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
27120 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 70 T_BKPT;. }.. p
27130 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 Cur->aiIdx[0] =
27140 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 0;. pCur->info.
27150 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 nSize = 0;. pCu
27160 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e r->curFlags &= ~
27170 28 42 54 43 46 5f 41 74 4c 61 73 74 7c 42 54 43 (BTCF_AtLast|BTC
27180 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 F_ValidNKey|BTCF
27190 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 0a 20 20 _ValidOvfl);..
271a0 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c if( pRoot->nCell
271b0 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e >0 ){. pCur->
271c0 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
271d0 56 41 4c 49 44 3b 0a 20 20 7d 65 6c 73 65 20 69 VALID;. }else i
271e0 66 28 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 f( !pRoot->leaf
271f0 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70 ){. Pgno subp
27200 61 67 65 3b 0a 20 20 20 20 69 66 28 20 70 52 6f age;. if( pRo
27210 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 20 72 65 ot->pgno!=1 ) re
27220 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
27230 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 73 75 UPT_BKPT;. su
27240 62 70 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 bpage = get4byte
27250 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 (&pRoot->aData[p
27260 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b Root->hdrOffset+
27270 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 8]);. pCur->e
27280 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 State = CURSOR_V
27290 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d ALID;. rc = m
272a0 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
272b0 20 73 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c subpage);. }el
272c0 73 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 se{. pCur->eS
272d0 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
272e0 56 41 4c 49 44 3b 0a 20 20 7d 0a 20 20 72 65 74 VALID;. }. ret
272f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
27300 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
27310 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 down to the lef
27320 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 t-most leaf entr
27330 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a y beneath the.**
27340 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 entry to which
27350 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 it is currently
27360 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 pointing..**.**
27370 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 The left-most le
27380 61 66 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 af is the one wi
27390 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 th the smallest
273a0 6b 65 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a key - the first.
273b0 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 ** in ascending
273c0 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 order..*/.static
273d0 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d int moveToLeftm
273e0 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ost(BtCursor *pC
273f0 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f ur){. Pgno pgno
27400 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
27410 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 ITE_OK;. MemPag
27420 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 e *pPage;.. ass
27430 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
27440 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
27450 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
27460 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
27470 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 LID );. while(
27480 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
27490 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d !(pPage = pCur-
274a0 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
274b0 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 age])->leaf ){.
274c0 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
274d0 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
274e0 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ge]<pPage->nCell
274f0 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 );. pgno = g
27500 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c et4byte(findCell
27510 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 (pPage, pCur->ai
27520 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
27530 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 ));. rc = mov
27540 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 eToChild(pCur, p
27550 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 gno);. }. retu
27560 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
27570 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
27580 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 down to the righ
27590 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 t-most leaf entr
275a0 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a y beneath the.**
275b0 20 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 page to which i
275c0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 t is currently p
275d0 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 ointing. Notice
275e0 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a the difference.
275f0 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 ** between moveT
27600 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 oLeftmost() and
27610 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 moveToRightmost(
27620 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f ). moveToLeftmo
27630 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 st().** finds th
27640 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 e left-most entr
27650 79 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 y beneath the *e
27660 6e 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f ntry* whereas mo
27670 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a veToRightmost().
27680 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 ** finds the rig
27690 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 ht-most entry be
276a0 6e 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a neath the *page*
276b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 ..**.** The righ
276c0 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 t-most entry is
276d0 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 the one with the
276e0 20 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 largest key - t
276f0 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 he last.** key i
27700 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
27710 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
27720 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
27730 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
27740 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 {. Pgno pgno;.
27750 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
27760 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a _OK;. MemPage *
27770 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 pPage = 0;.. as
27780 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
27790 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
277a0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
277b0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
277c0 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 ALID );. while(
277d0 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d !(pPage = pCur-
277e0 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
277f0 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 age])->leaf ){.
27800 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
27810 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
27820 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
27830 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d t+8]);. pCur-
27840 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
27850 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 ge] = pPage->nCe
27860 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 ll;. rc = mov
27870 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 eToChild(pCur, p
27880 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 gno);. if( rc
27890 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
278a0 7d 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b }. pCur->aiIdx[
278b0 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 pCur->iPage] = p
278c0 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 Page->nCell-1;.
278d0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
278e0 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 3b 0a nfo.nSize==0 );.
278f0 20 20 61 73 73 65 72 74 28 20 28 70 43 75 72 2d assert( (pCur-
27900 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 >curFlags & BTCF
27910 5f 56 61 6c 69 64 4e 4b 65 79 29 3d 3d 30 20 29 _ValidNKey)==0 )
27920 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
27930 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 E_OK;.}../* Move
27940 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
27950 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 he first entry i
27960 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 n the table. Re
27970 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a turn SQLITE_OK.*
27980 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 * on success. S
27990 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 et *pRes to 0 if
279a0 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 the cursor actu
279b0 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 ally points to s
279c0 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 omething.** or s
279d0 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 et *pRes to 1 if
279e0 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
279f0 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 pty..*/.int sqli
27a00 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42 74 te3BtreeFirst(Bt
27a10 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
27a20 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
27a30 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 rc;.. assert( c
27a40 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
27a50 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
27a60 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
27a70 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
27a80 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
27a90 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 ;. rc = moveToR
27aa0 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 oot(pCur);. if(
27ab0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
27ac0 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e {. if( pCur->
27ad0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
27ae0 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 NVALID ){.
27af0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 assert( pCur->pg
27b00 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 noRoot==0 || pCu
27b10 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
27b20 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 iPage]->nCell==0
27b30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
27b40 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 1;. }else{.
27b50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
27b60 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
27b70 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 >iPage]->nCell>0
27b80 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
27b90 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
27ba0 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
27bb0 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Cur);. }. }.
27bc0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
27bd0 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 /* Move the curs
27be0 6f 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 or to the last e
27bf0 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c ntry in the tabl
27c00 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 e. Return SQLIT
27c10 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 E_OK.** on succe
27c20 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 ss. Set *pRes t
27c30 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f o 0 if the curso
27c40 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 r actually point
27c50 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a s to something.*
27c60 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 * or set *pRes t
27c70 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 o 1 if the table
27c80 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e is empty..*/.in
27c90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 t sqlite3BtreeLa
27ca0 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 st(BtCursor *pCu
27cb0 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
27cc0 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 61 73 73 int rc;. . ass
27cd0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
27ce0 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
27cf0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
27d00 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
27d10 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
27d20 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 tex) );.. /* If
27d30 20 74 68 65 20 63 75 72 73 6f 72 20 61 6c 72 65 the cursor alre
27d40 61 64 79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ady points to th
27d50 65 20 6c 61 73 74 20 65 6e 74 72 79 2c 20 74 68 e last entry, th
27d60 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a is is a no-op. *
27d70 2f 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 56 /. if( CURSOR_V
27d80 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
27d90 74 65 20 26 26 20 28 70 43 75 72 2d 3e 63 75 72 te && (pCur->cur
27da0 46 6c 61 67 73 20 26 20 42 54 43 46 5f 41 74 4c Flags & BTCF_AtL
27db0 61 73 74 29 21 3d 30 20 29 7b 0a 23 69 66 64 65 ast)!=0 ){.#ifde
27dc0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
27dd0 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b /* This block
27de0 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 serves to asser
27df0 74 28 29 20 74 68 61 74 20 74 68 65 20 63 75 72 t() that the cur
27e00 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 sor really does
27e10 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f point . ** to
27e20 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
27e30 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a in the b-tree. *
27e40 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 /. int ii;.
27e50 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 for(ii=0; ii<p
27e60 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b Cur->iPage; ii++
27e70 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
27e80 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d pCur->aiIdx[ii]
27e90 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 ==pCur->apPage[i
27ea0 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 i]->nCell );.
27eb0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
27ec0 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
27ed0 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 >iPage]==pCur->a
27ee0 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
27ef0 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 e]->nCell-1 );.
27f00 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
27f10 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
27f20 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 age]->leaf );.#e
27f30 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 ndif. return
27f40 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
27f50 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
27f60 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
27f70 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
27f80 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
27f90 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
27fa0 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 tate ){. as
27fb0 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f sert( pCur->pgno
27fc0 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d Root==0 || pCur-
27fd0 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
27fe0 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 age]->nCell==0 )
27ff0 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
28000 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
28010 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
28020 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
28030 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 _VALID );.
28040 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
28050 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 rc = moveToRigh
28060 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 tmost(pCur);.
28070 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
28080 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
28090 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c pCur->curFlags |
280a0 3d 20 42 54 43 46 5f 41 74 4c 61 73 74 3b 0a 20 = BTCF_AtLast;.
280b0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
280c0 20 20 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 pCur->curFla
280d0 67 73 20 26 3d 20 7e 42 54 43 46 5f 41 74 4c 61 gs &= ~BTCF_AtLa
280e0 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0a st;. }. .
280f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
28100 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 rn rc;.}../* Mov
28110 65 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 e the cursor so
28120 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
28130 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 o an entry near
28140 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 65 63 the key .** spec
28150 69 66 69 65 64 20 62 79 20 70 49 64 78 4b 65 79 ified by pIdxKey
28160 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52 65 or intKey. Re
28170 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 turn a success c
28180 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 ode..**.** For I
28190 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 74 68 NTKEY tables, th
281a0 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d 65 74 e intKey paramet
281b0 65 72 20 69 73 20 75 73 65 64 2e 20 20 70 49 64 er is used. pId
281c0 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 62 65 xKey .** must be
281d0 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 65 NULL. For inde
281e0 78 20 74 61 62 6c 65 73 2c 20 70 49 64 78 4b 65 x tables, pIdxKe
281f0 79 20 69 73 20 75 73 65 64 20 61 6e 64 20 69 6e y is used and in
28200 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 tKey.** is ignor
28210 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
28220 65 78 61 63 74 20 6d 61 74 63 68 20 69 73 20 6e exact match is n
28230 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 ot found, then t
28240 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 he cursor is alw
28250 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e ays.** left poin
28260 74 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20 70 ting at a leaf p
28270 61 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 age which would
28280 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20 69 hold the entry i
28290 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 f it.** were pre
282a0 73 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73 6f sent. The curso
282b0 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f r might point to
282c0 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 63 an entry that c
282d0 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f omes.** before o
282e0 72 20 61 66 74 65 72 20 74 68 65 20 6b 65 79 2e r after the key.
282f0 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 .**.** An intege
28300 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 r is written int
28310 6f 20 2a 70 52 65 73 20 77 68 69 63 68 20 69 73 o *pRes which is
28320 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a the result of.*
28330 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 * comparing the
28340 6b 65 79 20 77 69 74 68 20 74 68 65 20 65 6e 74 key with the ent
28350 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 ry to which the
28360 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 70 6f cursor is .** po
28370 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 inting. The mea
28380 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 ning of the inte
28390 67 65 72 20 77 72 69 74 74 65 6e 20 69 6e 74 6f ger written into
283a0 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 73 20 .** *pRes is as
283b0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
283c0 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20 20 *pRes<0
283d0 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
283e0 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 ft pointing at a
283f0 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 n entry that.**
28400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28410 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
28420 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 20 intKey/pIdxKey
28430 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 or if the table
28440 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 is empty.**
28450 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 and
28460 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 74 the cursor is t
28470 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 6f herefore left po
28480 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a int to nothing..
28490 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d **.** *pRes=
284a0 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73 6f =0 The curso
284b0 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 r is left pointi
284c0 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 ng at an entry t
284d0 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 hat.**
284e0 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 exactly
284f0 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 79 2f 70 matches intKey/p
28500 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 IdxKey..**.**
28510 20 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20 54 *pRes>0 T
28520 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 he cursor is lef
28530 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e t pointing at an
28540 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 entry that.**
28550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28560 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 is larger than i
28570 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a ntKey/pIdxKey..*
28580 2a 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 *.*/.int sqlite3
28590 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
285a0 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 ked(. BtCursor
285b0 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 *pCur,
285c0 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f /* The cursor to
285d0 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 be moved */. U
285e0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
285f0 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 63 IdxKey, /* Unpac
28600 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f ked index key */
28610 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 20 . i64 intKey,
28620 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
28630 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0a he table key */.
28640 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c int biasRight,
28650 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
28660 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65 20 true, bias the
28670 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 search to the hi
28680 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 gh end */. int
28690 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 *pRes
286a0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
286b0 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
286c0 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
286d0 3b 0a 20 20 52 65 63 6f 72 64 43 6f 6d 70 61 72 ;. RecordCompar
286e0 65 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 e xRecordCompare
286f0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
28700 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
28710 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
28720 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
28730 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
28740 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
28750 20 20 61 73 73 65 72 74 28 20 70 52 65 73 20 29 assert( pRes )
28760 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 64 ;. assert( (pId
28770 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 72 2d xKey==0)==(pCur-
28780 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 29 3b >pKeyInfo==0) );
28790 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 .. /* If the cu
287a0 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 rsor is already
287b0 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74 68 positioned at th
287c0 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20 74 e point we are t
287d0 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f rying. ** to mo
287e0 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73 74 ve to, then just
287f0 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 return without
28800 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20 2a doing any work *
28810 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 /. if( pCur->eS
28820 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
28830 49 44 20 26 26 20 28 70 43 75 72 2d 3e 63 75 72 ID && (pCur->cur
28840 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c Flags & BTCF_Val
28850 69 64 4e 4b 65 79 29 21 3d 30 0a 20 20 20 26 26 idNKey)!=0. &&
28860 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
28870 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 ->intKey . ){.
28880 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
28890 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 o.nKey==intKey )
288a0 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 {. *pRes =
288b0 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
288c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
288d0 0a 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e . if( (pCur->
288e0 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f curFlags & BTCF_
288f0 41 74 4c 61 73 74 29 21 3d 30 20 26 26 20 70 43 AtLast)!=0 && pC
28900 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e ur->info.nKey<in
28910 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 tKey ){. *p
28920 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 Res = -1;.
28930 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
28940 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
28950 66 28 20 70 49 64 78 4b 65 79 20 29 7b 0a 20 20 f( pIdxKey ){.
28960 20 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 xRecordCompare
28970 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 = sqlite3VdbeFi
28980 6e 64 43 6f 6d 70 61 72 65 28 70 49 64 78 4b 65 ndCompare(pIdxKe
28990 79 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 2d y);. pIdxKey-
289a0 3e 65 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 >errCode = 0;.
289b0 20 20 61 73 73 65 72 74 28 20 70 49 64 78 4b 65 assert( pIdxKe
289c0 79 2d 3e 64 65 66 61 75 6c 74 5f 72 63 3d 3d 31 y->default_rc==1
289d0 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 49 . || pI
289e0 64 78 4b 65 79 2d 3e 64 65 66 61 75 6c 74 5f 72 dxKey->default_r
289f0 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 7c c==0 . |
28a00 7c 20 70 49 64 78 4b 65 79 2d 3e 64 65 66 61 75 | pIdxKey->defau
28a10 6c 74 5f 72 63 3d 3d 2d 31 0a 20 20 20 20 29 3b lt_rc==-1. );
28a20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 78 52 . }else{. xR
28a30 65 63 6f 72 64 43 6f 6d 70 61 72 65 20 3d 20 30 ecordCompare = 0
28a40 3b 20 2f 2a 20 41 6c 6c 20 6b 65 79 73 20 61 72 ; /* All keys ar
28a50 65 20 69 6e 74 65 67 65 72 73 20 2a 2f 0a 20 20 e integers */.
28a60 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f }.. rc = moveTo
28a70 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 Root(pCur);. if
28a80 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
28a90 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
28aa0 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 ert( pCur->pgnoR
28ab0 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e oot==0 || pCur->
28ac0 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
28ad0 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge] );. assert(
28ae0 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d pCur->pgnoRoot=
28af0 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 =0 || pCur->apPa
28b00 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
28b10 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 >isInit );. ass
28b20 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
28b30 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 e==CURSOR_INVALI
28b40 44 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 D || pCur->apPag
28b50 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
28b60 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 69 66 28 nCell>0 );. if(
28b70 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
28b80 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
28b90 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b . *pRes = -1;
28ba0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
28bb0 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c r->pgnoRoot==0 |
28bc0 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 | pCur->apPage[p
28bd0 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
28be0 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 ll==0 );. ret
28bf0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
28c00 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 }. assert( pCu
28c10 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
28c20 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 tKey || pIdxKey
28c30 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 );. for(;;){.
28c40 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 2c 20 int lwr, upr,
28c50 69 64 78 2c 20 63 3b 0a 20 20 20 20 50 67 6e 6f idx, c;. Pgno
28c60 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d chldPg;. Mem
28c70 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 Page *pPage = pC
28c80 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
28c90 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 75 38 20 >iPage];. u8
28ca0 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 *pCell;
28cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28cc0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 /* Pointer to c
28cd0 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 urrent cell in p
28ce0 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 Page */.. /*
28cf0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d 75 73 pPage->nCell mus
28d00 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 t be greater tha
28d10 6e 20 7a 65 72 6f 2e 20 49 66 20 74 68 69 73 20 n zero. If this
28d20 69 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 is the root-page
28d30 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 73 . ** the curs
28d40 6f 72 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 or would have be
28d50 65 6e 20 49 4e 56 41 4c 49 44 20 61 62 6f 76 65 en INVALID above
28d60 20 61 6e 64 20 74 68 69 73 20 66 6f 72 28 3b 3b and this for(;;
28d70 29 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 6e 6f ) loop. ** no
28d80 74 20 72 75 6e 2e 20 49 66 20 74 68 69 73 20 69 t run. If this i
28d90 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f 74 2d 70 s not the root-p
28da0 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 6f age, then the mo
28db0 76 65 54 6f 43 68 69 6c 64 28 29 20 72 6f 75 74 veToChild() rout
28dc0 69 6e 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 ine. ** would
28dd0 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 64 65 have already de
28de0 74 65 63 74 65 64 20 64 62 20 63 6f 72 72 75 70 tected db corrup
28df0 74 69 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c 79 2c tion. Similarly,
28e00 20 70 50 61 67 65 20 6d 75 73 74 0a 20 20 20 20 pPage must.
28e10 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74 20 ** be the right
28e20 6b 69 6e 64 20 28 69 6e 64 65 78 20 6f 72 20 74 kind (index or t
28e30 61 62 6c 65 29 20 6f 66 20 62 2d 74 72 65 65 20 able) of b-tree
28e40 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 0a page. Otherwise.
28e50 20 20 20 20 2a 2a 20 61 20 6d 6f 76 65 54 6f 43 ** a moveToC
28e60 68 69 6c 64 28 29 20 6f 72 20 6d 6f 76 65 54 6f hild() or moveTo
28e70 52 6f 6f 74 28 29 20 63 61 6c 6c 20 77 6f 75 6c Root() call woul
28e80 64 20 68 61 76 65 20 64 65 74 65 63 74 65 64 20 d have detected
28e90 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 2a 2f 0a corruption. */.
28ea0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
28eb0 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 e->nCell>0 );.
28ec0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
28ed0 3e 69 6e 74 4b 65 79 3d 3d 28 70 49 64 78 4b 65 >intKey==(pIdxKe
28ee0 79 3d 3d 30 29 20 29 3b 0a 20 20 20 20 6c 77 72 y==0) );. lwr
28ef0 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 = 0;. upr =
28f00 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a pPage->nCell-1;.
28f10 20 20 20 20 61 73 73 65 72 74 28 20 62 69 61 73 assert( bias
28f20 52 69 67 68 74 3d 3d 30 20 7c 7c 20 62 69 61 73 Right==0 || bias
28f30 52 69 67 68 74 3d 3d 31 20 29 3b 0a 20 20 20 20 Right==1 );.
28f40 69 64 78 20 3d 20 75 70 72 3e 3e 28 31 2d 62 69 idx = upr>>(1-bi
28f50 61 73 52 69 67 68 74 29 3b 20 2f 2a 20 69 64 78 asRight); /* idx
28f60 20 3d 20 62 69 61 73 52 69 67 68 74 20 3f 20 75 = biasRight ? u
28f70 70 72 20 3a 20 28 6c 77 72 2b 75 70 72 29 2f 32 pr : (lwr+upr)/2
28f80 3b 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61 ; */. pCur->a
28f90 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
28fa0 5d 20 3d 20 28 75 31 36 29 69 64 78 3b 0a 20 20 ] = (u16)idx;.
28fb0 20 20 69 66 28 20 78 52 65 63 6f 72 64 43 6f 6d if( xRecordCom
28fc0 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pare==0 ){.
28fd0 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 for(;;){.
28fe0 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a i64 nCellKey;.
28ff0 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 pCell =
29000 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
29010 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 68 idx) + pPage->ch
29020 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20 ildPtrSize;.
29030 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 if( pPage->i
29040 6e 74 4b 65 79 4c 65 61 66 20 29 7b 0a 20 20 20 ntKeyLeaf ){.
29050 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 30 78 while( 0x
29060 38 30 20 3c 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 80 <= *(pCell++)
29070 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
29080 69 66 28 20 70 43 65 6c 6c 3e 3d 70 50 61 67 65 if( pCell>=pPage
29090 2d 3e 61 44 61 74 61 45 6e 64 20 29 20 72 65 74 ->aDataEnd ) ret
290a0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
290b0 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
290c0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
290d0 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 getVarint
290e0 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e (pCell, (u64*)&n
290f0 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 CellKey);.
29100 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 if( nCellKey<i
29110 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 ntKey ){.
29120 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a lwr = idx+1;.
29130 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 77 if( lw
29140 72 3e 75 70 72 20 29 7b 20 63 20 3d 20 2d 31 3b r>upr ){ c = -1;
29150 20 62 72 65 61 6b 3b 20 7d 0a 20 20 20 20 20 20 break; }.
29160 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c }else if( nCel
29170 6c 4b 65 79 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 lKey>intKey ){.
29180 20 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 upr = i
29190 64 78 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 dx-1;.
291a0 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 20 63 if( lwr>upr ){ c
291b0 20 3d 20 2b 31 3b 20 62 72 65 61 6b 3b 20 7d 0a = +1; break; }.
291c0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
291d0 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
291e0 20 6e 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 nCellKey==intKe
291f0 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 y );. p
29200 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d Cur->curFlags |=
29210 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 3b BTCF_ValidNKey;
29220 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d . pCur-
29230 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 >info.nKey = nCe
29240 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 llKey;.
29250 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
29260 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 r->iPage] = (u16
29270 29 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 )idx;.
29280 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
29290 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
292a0 6c 77 72 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 lwr = idx;.
292b0 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 goto move
292c0 74 6f 5f 6e 65 78 74 5f 6c 61 79 65 72 3b 0a 20 to_next_layer;.
292d0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
292e0 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 52 65 *pRe
292f0 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 s = 0;.
29300 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
29310 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 K;. g
29320 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 oto moveto_finis
29330 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 h;. }.
29340 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
29350 20 61 73 73 65 72 74 28 20 6c 77 72 2b 75 70 72 assert( lwr+upr
29360 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 >=0 );. i
29370 64 78 20 3d 20 28 6c 77 72 2b 75 70 72 29 3e 3e dx = (lwr+upr)>>
29380 31 3b 20 20 2f 2a 20 69 64 78 20 3d 20 28 6c 77 1; /* idx = (lw
29390 72 2b 75 70 72 29 2f 32 3b 20 2a 2f 0a 20 20 20 r+upr)/2; */.
293a0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
293b0 20 20 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 for(;;){.
293c0 20 20 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c int nCell
293d0 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 ;. pCell
293e0 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
293f0 2c 20 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e , idx) + pPage->
29400 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 0a 20 childPtrSize;..
29410 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 /* The ma
29420 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 ximum supported
29430 70 61 67 65 2d 73 69 7a 65 20 69 73 20 36 35 35 page-size is 655
29440 33 36 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d 36 bytes. This m
29450 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20 eans that.
29460 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d ** the maximum
29470 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 number of recor
29480 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f d bytes stored o
29490 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65 n an index B-Tre
294a0 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 e. ** pag
294b0 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31 e is less than 1
294c0 36 33 38 34 20 62 79 74 65 73 20 61 6e 64 20 6d 6384 bytes and m
294d0 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 ay be stored as
294e0 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20 a 2-byte.
294f0 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73 ** varint. This
29500 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
29510 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 used to attempt
29520 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67 to avoid parsing
29530 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 . ** the
29540 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20 entire cell by
29550 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 checking for the
29560 20 63 61 73 65 73 20 77 68 65 72 65 20 74 68 65 cases where the
29570 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20 record is .
29580 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e ** stored en
29590 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68 tirely within th
295a0 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 79 e b-tree page by
295b0 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 inspecting the
295c0 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a first . *
295d0 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 65 * 2 bytes of the
295e0 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a cell.. *
295f0 2f 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 /. nCell
29600 3d 20 70 43 65 6c 6c 5b 30 5d 3b 0a 20 20 20 20 = pCell[0];.
29610 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 3c 3d 70 if( nCell<=p
29620 50 61 67 65 2d 3e 6d 61 78 31 62 79 74 65 50 61 Page->max1bytePa
29630 79 6c 6f 61 64 20 29 7b 0a 20 20 20 20 20 20 20 yload ){.
29640 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
29650 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 h runs if the re
29660 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 cord-size field
29670 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 of the cell is a
29680 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 . ** si
29690 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 ngle byte varint
296a0 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 and the record
296b0 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e fits entirely on
296c0 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 the main.
296d0 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 ** b-tree pa
296e0 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
296f0 20 20 74 65 73 74 63 61 73 65 28 20 70 43 65 6c testcase( pCel
29700 6c 2b 6e 43 65 6c 6c 2b 31 3d 3d 70 50 61 67 65 l+nCell+1==pPage
29710 2d 3e 61 44 61 74 61 45 6e 64 20 29 3b 0a 20 20 ->aDataEnd );.
29720 20 20 20 20 20 20 20 20 63 20 3d 20 78 52 65 63 c = xRec
29730 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c ordCompare(nCell
29740 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c 5b , (void*)&pCell[
29750 31 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 1], pIdxKey);.
29760 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
29770 21 28 70 43 65 6c 6c 5b 31 5d 20 26 20 30 78 38 !(pCell[1] & 0x8
29780 30 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 0) . &&
29790 20 28 6e 43 65 6c 6c 20 3d 20 28 28 6e 43 65 6c (nCell = ((nCel
297a0 6c 26 30 78 37 66 29 3c 3c 37 29 20 2b 20 70 43 l&0x7f)<<7) + pC
297b0 65 6c 6c 5b 31 5d 29 3c 3d 70 50 61 67 65 2d 3e ell[1])<=pPage->
297c0 6d 61 78 4c 6f 63 61 6c 0a 20 20 20 20 20 20 20 maxLocal.
297d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
297e0 20 54 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 The record-size
297f0 20 66 69 65 6c 64 20 69 73 20 61 20 32 20 62 79 field is a 2 by
29800 74 65 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68 te varint and th
29810 65 20 72 65 63 6f 72 64 20 0a 20 20 20 20 20 20 e record .
29820 20 20 20 20 2a 2a 20 66 69 74 73 20 65 6e 74 69 ** fits enti
29830 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e rely on the main
29840 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a b-tree page. *
29850 2f 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 /. test
29860 63 61 73 65 28 20 70 43 65 6c 6c 2b 6e 43 65 6c case( pCell+nCel
29870 6c 2b 32 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 l+2==pPage->aDat
29880 61 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 20 20 aEnd );.
29890 20 20 63 20 3d 20 78 52 65 63 6f 72 64 43 6f 6d c = xRecordCom
298a0 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 pare(nCell, (voi
298b0 64 2a 29 26 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 d*)&pCell[2], pI
298c0 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 dxKey);.
298d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
298e0 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 66 /* The record f
298f0 6c 6f 77 73 20 6f 76 65 72 20 6f 6e 74 6f 20 6f lows over onto o
29900 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 ne or more overf
29910 6c 6f 77 20 70 61 67 65 73 2e 20 49 6e 0a 20 20 low pages. In.
29920 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 ** this
29930 63 61 73 65 20 74 68 65 20 77 68 6f 6c 65 20 63 case the whole c
29940 65 6c 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ell needs to be
29950 70 61 72 73 65 64 2c 20 61 20 62 75 66 66 65 72 parsed, a buffer
29960 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 allocated.
29970 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 63 63 65 ** and acce
29980 73 73 50 61 79 6c 6f 61 64 28 29 20 75 73 65 64 ssPayload() used
29990 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 to retrieve the
299a0 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 record into the
299b0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 . ** bu
299c0 66 66 65 72 20 62 65 66 6f 72 65 20 56 64 62 65 ffer before Vdbe
299d0 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 29 20 RecordCompare()
299e0 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 2a can be called. *
299f0 2f 0a 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 /. void
29a00 20 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 *pCellKey;.
29a10 20 20 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 u8 * const
29a20 20 70 43 65 6c 6c 42 6f 64 79 20 3d 20 70 43 65 pCellBody = pCe
29a30 6c 6c 20 2d 20 70 50 61 67 65 2d 3e 63 68 69 6c ll - pPage->chil
29a40 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 dPtrSize;.
29a50 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 btreeParseCe
29a60 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 llPtr(pPage, pCe
29a70 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72 2d 3e 69 llBody, &pCur->i
29a80 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 nfo);.
29a90 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 70 43 75 nCell = (int)pCu
29aa0 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 r->info.nKey;.
29ab0 20 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 pCellKey
29ac0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
29ad0 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 ( nCell );.
29ae0 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 4b 65 if( pCellKe
29af0 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 y==0 ){.
29b00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
29b10 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 NOMEM;.
29b20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
29b30 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20 inish;.
29b40 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 }. pCu
29b50 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
29b60 50 61 67 65 5d 20 3d 20 28 75 31 36 29 69 64 78 Page] = (u16)idx
29b70 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
29b80 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 accessPayload(p
29b90 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 Cur, 0, nCell, (
29ba0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 unsigned char*)p
29bb0 43 65 6c 6c 4b 65 79 2c 20 32 29 3b 0a 20 20 20 CellKey, 2);.
29bc0 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
29bd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
29be0 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b ite3_free(pCellK
29bf0 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ey);.
29c00 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
29c10 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ish;. }
29c20 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 78 . c = x
29c30 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 RecordCompare(nC
29c40 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 ell, pCellKey, p
29c50 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 IdxKey);.
29c60 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
29c70 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 pCellKey);.
29c80 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 }. ass
29c90 65 72 74 28 20 0a 20 20 20 20 20 20 20 20 20 20 ert( .
29ca0 20 20 28 70 49 64 78 4b 65 79 2d 3e 65 72 72 43 (pIdxKey->errC
29cb0 6f 64 65 21 3d 53 51 4c 49 54 45 5f 43 4f 52 52 ode!=SQLITE_CORR
29cc0 55 50 54 20 7c 7c 20 63 3d 3d 30 29 0a 20 20 20 UPT || c==0).
29cd0 20 20 20 20 20 20 26 26 20 28 70 49 64 78 4b 65 && (pIdxKe
29ce0 79 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 y->errCode!=SQLI
29cf0 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 43 75 72 TE_NOMEM || pCur
29d00 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 61 ->pBtree->db->ma
29d10 6c 6c 6f 63 46 61 69 6c 65 64 29 0a 20 20 20 20 llocFailed).
29d20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 );. i
29d30 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 f( c<0 ){.
29d40 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b lwr = idx+1;
29d50 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 . }else i
29d60 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 f( c>0 ){.
29d70 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d 31 3b upr = idx-1;
29d80 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
29d90 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
29da0 28 20 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ( c==0 );.
29db0 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
29dc0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
29dd0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
29de0 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
29df0 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 Cur->iPage] = (u
29e00 31 36 29 69 64 78 3b 0a 20 20 20 20 20 20 20 20 16)idx;.
29e10 20 20 69 66 28 20 70 49 64 78 4b 65 79 2d 3e 65 if( pIdxKey->e
29e20 72 72 43 6f 64 65 20 29 20 72 63 20 3d 20 53 51 rrCode ) rc = SQ
29e30 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 LITE_CORRUPT;.
29e40 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 goto mov
29e50 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
29e60 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
29e70 28 20 6c 77 72 3e 75 70 72 20 29 20 62 72 65 61 ( lwr>upr ) brea
29e80 6b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 k;. asser
29e90 74 28 20 6c 77 72 2b 75 70 72 3e 3d 30 20 29 3b t( lwr+upr>=0 );
29ea0 0a 20 20 20 20 20 20 20 20 69 64 78 20 3d 20 28 . idx = (
29eb0 6c 77 72 2b 75 70 72 29 3e 3e 31 3b 20 20 2f 2a lwr+upr)>>1; /*
29ec0 20 69 64 78 20 3d 20 28 6c 77 72 2b 75 70 72 29 idx = (lwr+upr)
29ed0 2f 32 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 /2 */. }.
29ee0 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
29ef0 6c 77 72 3d 3d 75 70 72 2b 31 20 7c 7c 20 28 70 lwr==upr+1 || (p
29f00 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 Page->intKey &&
29f10 21 70 50 61 67 65 2d 3e 6c 65 61 66 29 20 29 3b !pPage->leaf) );
29f20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
29f30 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 ge->isInit );.
29f40 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
29f50 66 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 f ){. asser
29f60 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
29f70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 Cur->iPage]<pCur
29f80 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
29f90 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a Page]->nCell );.
29fa0 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
29fb0 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
29fc0 20 28 75 31 36 29 69 64 78 3b 0a 20 20 20 20 20 (u16)idx;.
29fd0 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20 20 20 *pRes = c;.
29fe0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
29ff0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 ;. goto mov
2a000 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
2a010 7d 0a 6d 6f 76 65 74 6f 5f 6e 65 78 74 5f 6c 61 }.moveto_next_la
2a020 79 65 72 3a 0a 20 20 20 20 69 66 28 20 6c 77 72 yer:. if( lwr
2a030 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 >=pPage->nCell )
2a040 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d {. chldPg =
2a050 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
2a060 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
2a070 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
2a080 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 }else{. c
2a090 68 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 hldPg = get4byte
2a0a0 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c (findCell(pPage,
2a0b0 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 lwr));. }.
2a0c0 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
2a0d0 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 ur->iPage] = (u1
2a0e0 36 29 6c 77 72 3b 0a 20 20 20 20 72 63 20 3d 20 6)lwr;. rc =
2a0f0 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
2a100 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 , chldPg);. i
2a110 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20 f( rc ) break;.
2a120 20 7d 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 }.moveto_finish
2a130 3a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e :. pCur->info.n
2a140 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
2a150 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 ->curFlags &= ~(
2a160 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 BTCF_ValidNKey|B
2a170 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a TCF_ValidOvfl);.
2a180 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
2a190 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
2a1a0 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 UE if the cursor
2a1b0 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
2a1c0 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 at an entry of
2a1d0 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
2a1e0 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 TRUE will be re
2a1f0 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 turned after a c
2a200 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
2a210 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a reeNext() moves.
2a220 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 ** past the last
2a230 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
2a240 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 ble or sqlite3Bt
2a250 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 reePrev() moves
2a260 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 past.** the firs
2a270 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 t entry. TRUE i
2a280 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 s also returned
2a290 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
2a2a0 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 empty..*/.int sq
2a2b0 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 lite3BtreeEof(Bt
2a2c0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
2a2d0 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 /* TODO: What i
2a2e0 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
2a2f0 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 in CURSOR_REQUIR
2a300 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 ESEEK but all ta
2a310 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a ble entries. **
2a320 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 have been delet
2a330 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c ed? This API wil
2a340 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 l need to change
2a350 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 to return an er
2a360 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 ror code. ** as
2a370 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f well as the boo
2a380 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 lean result valu
2a390 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e e.. */. return
2a3a0 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d (CURSOR_VALID!=
2a3b0 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d pCur->eState);.}
2a3c0 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 ../*.** Advance
2a3d0 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
2a3e0 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 e next entry in
2a3f0 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 the database. I
2a400 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 f.** successful
2a410 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 then set *pRes=0
2a420 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
2a430 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 .** was already
2a440 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
2a450 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 last entry in th
2a460 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 e database befor
2a470 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e e.** this routin
2a480 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 e was called, th
2a490 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a en set *pRes=1..
2a4a0 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 69 6e 20 65 **.** The main e
2a4b0 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 73 71 ntry point is sq
2a4c0 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 29 lite3BtreeNext()
2a4d0 2e 20 20 54 68 61 74 20 72 6f 75 74 69 6e 65 20 . That routine
2a4e0 69 73 20 6f 70 74 69 6d 69 7a 65 64 0a 2a 2a 20 is optimized.**
2a4f0 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 for the common c
2a500 61 73 65 20 6f 66 20 6d 65 72 65 6c 79 20 69 6e ase of merely in
2a510 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 crementing the c
2a520 65 6c 6c 20 63 6f 75 6e 74 65 72 20 42 74 43 75 ell counter BtCu
2a530 72 73 6f 72 2e 61 69 49 64 78 0a 2a 2a 20 74 6f rsor.aiIdx.** to
2a540 20 74 68 65 20 6e 65 78 74 20 63 65 6c 6c 20 6f the next cell o
2a550 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 n the current pa
2a560 67 65 2e 20 20 54 68 65 20 28 73 6c 6f 77 65 72 ge. The (slower
2a570 29 20 62 74 72 65 65 4e 65 78 74 28 29 20 68 65 ) btreeNext() he
2a580 6c 70 65 72 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 lper.** routine
2a590 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 is called when i
2a5a0 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 t is necessary t
2a5b0 6f 20 6d 6f 76 65 20 74 6f 20 61 20 64 69 66 66 o move to a diff
2a5c0 65 72 65 6e 74 20 70 61 67 65 20 6f 72 0a 2a 2a erent page or.**
2a5d0 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
2a5e0 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 cursor..**.** Th
2a5f0 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
2a600 6f 6e 20 77 69 6c 6c 20 73 65 74 20 2a 70 52 65 on will set *pRe
2a610 73 20 74 6f 20 30 20 6f 72 20 31 2e 20 20 54 68 s to 0 or 1. Th
2a620 65 20 69 6e 69 74 69 61 6c 20 2a 70 52 65 73 20 e initial *pRes
2a630 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 value.** will be
2a640 20 31 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 1 if the cursor
2a650 20 62 65 69 6e 67 20 73 74 65 70 70 65 64 20 63 being stepped c
2a660 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 6e orresponds to an
2a670 20 53 51 4c 20 69 6e 64 65 78 20 61 6e 64 0a 2a SQL index and.*
2a680 2a 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e * if this routin
2a690 65 20 63 6f 75 6c 64 20 68 61 76 65 20 62 65 65 e could have bee
2a6a0 6e 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 61 n skipped if tha
2a6b0 74 20 53 51 4c 20 69 6e 64 65 78 20 68 61 64 20 t SQL index had
2a6c0 62 65 65 6e 0a 2a 2a 20 61 20 75 6e 69 71 75 65 been.** a unique
2a6d0 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 69 index. Otherwi
2a6e0 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 se the caller wi
2a6f0 6c 6c 20 68 61 76 65 20 73 65 74 20 2a 70 52 65 ll have set *pRe
2a700 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 5a 65 s to zero..** Ze
2a710 72 6f 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e ro is the common
2a720 20 63 61 73 65 2e 20 54 68 65 20 62 74 72 65 65 case. The btree
2a730 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
2a740 69 73 20 66 72 65 65 20 74 6f 20 75 73 65 20 74 is free to use t
2a750 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 20 2a 70 he.** initial *p
2a760 52 65 73 20 76 61 6c 75 65 20 61 73 20 61 20 68 Res value as a h
2a770 69 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 70 int to improve p
2a780 65 72 66 6f 72 6d 61 6e 63 65 2c 20 62 75 74 20 erformance, but
2a790 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 53 the current.** S
2a7a0 51 4c 69 74 65 20 62 74 72 65 65 20 69 6d 70 6c QLite btree impl
2a7b0 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 ementation does
2a7c0 6e 6f 74 2e 20 28 4e 6f 74 65 20 74 68 61 74 20 not. (Note that
2a7d0 74 68 65 20 63 6f 6d 64 62 32 20 62 74 72 65 65 the comdb2 btree
2a7e0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
2a7f0 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 69 73 on does use this
2a800 20 68 69 6e 74 2c 20 68 6f 77 65 76 65 72 2e 29 hint, however.)
2a810 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
2a820 45 5f 4e 4f 49 4e 4c 49 4e 45 20 69 6e 74 20 62 E_NOINLINE int b
2a830 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f treeNext(BtCurso
2a840 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 r *pCur, int *pR
2a850 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 es){. int rc;.
2a860 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 int idx;. MemP
2a870 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 age *pPage;.. a
2a880 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
2a890 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
2a8a0 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
2a8b0 3e 73 6b 69 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 >skipNext==0 ||
2a8c0 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCur->eState!=CU
2a8d0 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
2a8e0 61 73 73 65 72 74 28 20 2a 70 52 65 73 3d 3d 30 assert( *pRes==0
2a8f0 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e );. if( pCur->
2a900 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
2a910 41 4c 49 44 20 29 7b 0a 20 20 20 20 61 73 73 65 ALID ){. asse
2a920 72 74 28 20 28 70 43 75 72 2d 3e 63 75 72 46 6c rt( (pCur->curFl
2a930 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c 69 64 ags & BTCF_Valid
2a940 4f 76 66 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 Ovfl)==0 );.
2a950 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
2a960 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
2a970 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
2a980 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
2a990 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
2a9a0 7d 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 }. if( CURSOR
2a9b0 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e _INVALID==pCur->
2a9c0 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 eState ){.
2a9d0 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 *pRes = 1;.
2a9e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
2a9f0 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 K;. }. if(
2aa00 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 pCur->skipNext
2aa10 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
2aa20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
2aa30 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
2aa40 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
2aa50 53 4f 52 5f 53 4b 49 50 4e 45 58 54 20 29 3b 0a SOR_SKIPNEXT );.
2aa60 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 pCur->eSta
2aa70 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 te = CURSOR_VALI
2aa80 44 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 D;. if( pCu
2aa90 72 2d 3e 73 6b 69 70 4e 65 78 74 3e 30 20 29 7b r->skipNext>0 ){
2aaa0 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 73 . pCur->s
2aab0 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 kipNext = 0;.
2aac0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
2aad0 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 TE_OK;. }.
2aae0 20 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e pCur->skipN
2aaf0 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ext = 0;. }.
2ab00 20 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 }.. pPage = pC
2ab10 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
2ab20 3e 69 50 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d >iPage];. idx =
2ab30 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 ++pCur->aiIdx[p
2ab40 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 Cur->iPage];. a
2ab50 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 ssert( pPage->is
2ab60 49 6e 69 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 Init );.. /* If
2ab70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
2ab80 6c 65 20 69 73 20 63 6f 72 72 75 70 74 2c 20 69 le is corrupt, i
2ab90 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f t is possible fo
2aba0 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 69 r the value of i
2abb0 64 78 20 0a 20 20 2a 2a 20 74 6f 20 62 65 20 69 dx . ** to be i
2abc0 6e 76 61 6c 69 64 20 68 65 72 65 2e 20 54 68 69 nvalid here. Thi
2abd0 73 20 63 61 6e 20 6f 6e 6c 79 20 6f 63 63 75 72 s can only occur
2abe0 20 69 66 20 61 20 73 65 63 6f 6e 64 20 63 75 72 if a second cur
2abf0 73 6f 72 20 6d 6f 64 69 66 69 65 73 0a 20 20 2a sor modifies. *
2ac00 2a 20 74 68 65 20 70 61 67 65 20 77 68 69 6c 65 * the page while
2ac10 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 cursor pCur is
2ac20 68 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 holding a refere
2ac30 6e 63 65 20 74 6f 20 69 74 2e 20 57 68 69 63 68 nce to it. Which
2ac40 20 63 61 6e 0a 20 20 2a 2a 20 6f 6e 6c 79 20 68 can. ** only h
2ac50 61 70 70 65 6e 20 69 66 20 74 68 65 20 64 61 74 appen if the dat
2ac60 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 abase is corrupt
2ac70 20 69 6e 20 73 75 63 68 20 61 20 77 61 79 20 61 in such a way a
2ac80 73 20 74 6f 20 6c 69 6e 6b 20 74 68 65 0a 20 20 s to link the.
2ac90 2a 2a 20 70 61 67 65 20 69 6e 74 6f 20 6d 6f 72 ** page into mor
2aca0 65 20 74 68 61 6e 20 6f 6e 65 20 62 2d 74 72 65 e than one b-tre
2acb0 65 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a e structure. */.
2acc0 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 3e testcase( idx>
2acd0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
2ace0 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67 . if( idx>=pPag
2acf0 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 e->nCell ){.
2ad00 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
2ad10 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d ){. rc = m
2ad20 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
2ad30 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
2ad40 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
2ad50 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 drOffset+8]));.
2ad60 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
2ad70 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 turn rc;. r
2ad80 65 74 75 72 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 eturn moveToLeft
2ad90 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
2ada0 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 }. do{.
2adb0 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
2adc0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 =0 ){. *p
2add0 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Res = 1;.
2ade0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
2adf0 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
2ae00 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
2ae10 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
2ae20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 }. moveToPa
2ae30 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 rent(pCur);.
2ae40 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
2ae50 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
2ae60 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 ge];. }while(
2ae70 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
2ae80 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 r->iPage]>=pPage
2ae90 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 69 ->nCell );. i
2aea0 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
2aeb0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
2aec0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
2aed0 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 t(pCur, pRes);.
2aee0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2aef0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
2af00 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
2af10 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
2af20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
2af30 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
2af40 20 20 20 20 72 65 74 75 72 6e 20 6d 6f 76 65 54 return moveT
2af50 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b oLeftmost(pCur);
2af60 0a 20 20 7d 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 . }.}.int sqlit
2af70 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 e3BtreeNext(BtCu
2af80 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
2af90 2a 70 52 65 73 29 7b 0a 20 20 4d 65 6d 50 61 67 *pRes){. MemPag
2afa0 65 20 2a 70 50 61 67 65 3b 0a 20 20 61 73 73 65 e *pPage;. asse
2afb0 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
2afc0 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
2afd0 61 73 73 65 72 74 28 20 70 52 65 73 21 3d 30 20 assert( pRes!=0
2afe0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 52 );. assert( *pR
2aff0 65 73 3d 3d 30 20 7c 7c 20 2a 70 52 65 73 3d 3d es==0 || *pRes==
2b000 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 1 );. assert( p
2b010 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3d 3d 30 Cur->skipNext==0
2b020 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 || pCur->eState
2b030 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 !=CURSOR_VALID )
2b040 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e ;. pCur->info.n
2b050 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
2b060 2d 3e 63 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 ->curFlags &= ~(
2b070 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 BTCF_ValidNKey|B
2b080 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3b 0a TCF_ValidOvfl);.
2b090 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 *pRes = 0;. i
2b0a0 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 f( pCur->eState!
2b0b0 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 20 =CURSOR_VALID )
2b0c0 72 65 74 75 72 6e 20 62 74 72 65 65 4e 65 78 74 return btreeNext
2b0d0 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 (pCur, pRes);.
2b0e0 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
2b0f0 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
2b100 5d 3b 0a 20 20 69 66 28 20 28 2b 2b 70 43 75 72 ];. if( (++pCur
2b110 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
2b120 61 67 65 5d 29 3e 3d 70 50 61 67 65 2d 3e 6e 43 age])>=pPage->nC
2b130 65 6c 6c 20 29 7b 0a 20 20 20 20 70 43 75 72 2d ell ){. pCur-
2b140 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
2b150 67 65 5d 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 ge]--;. retur
2b160 6e 20 62 74 72 65 65 4e 65 78 74 28 70 43 75 72 n btreeNext(pCur
2b170 2c 20 70 52 65 73 29 3b 0a 20 20 7d 0a 20 20 69 , pRes);. }. i
2b180 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
2b190 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
2b1a0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
2b1b0 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 6f 76 65 . return move
2b1c0 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 ToLeftmost(pCur)
2b1d0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
2b1e0 74 65 70 20 74 68 65 20 63 75 72 73 6f 72 20 74 tep the cursor t
2b1f0 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74 68 o the back to th
2b200 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 e previous entry
2b210 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
2b220 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 . If.** success
2b230 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 ful then set *pR
2b240 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 es=0. If the cu
2b250 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 rsor.** was alre
2b260 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 ady pointing to
2b270 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
2b280 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
2b290 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 before.** this r
2b2a0 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 outine was calle
2b2b0 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 d, then set *pRe
2b2c0 73 3d 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d s=1..**.** The m
2b2d0 61 69 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 ain entry point
2b2e0 69 73 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 is sqlite3BtreeP
2b2f0 72 65 76 69 6f 75 73 28 29 2e 20 20 54 68 61 74 revious(). That
2b300 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 70 74 69 routine is opti
2b310 6d 69 7a 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 mized.** for the
2b320 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 common case of
2b330 6d 65 72 65 6c 79 20 64 65 63 72 65 6d 65 6e 74 merely decrement
2b340 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 63 6f 75 ing the cell cou
2b350 6e 74 65 72 20 42 74 43 75 72 73 6f 72 2e 61 69 nter BtCursor.ai
2b360 49 64 78 0a 2a 2a 20 74 6f 20 74 68 65 20 70 72 Idx.** to the pr
2b370 65 76 69 6f 75 73 20 63 65 6c 6c 20 6f 6e 20 74 evious cell on t
2b380 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 2e he current page.
2b390 20 20 54 68 65 20 28 73 6c 6f 77 65 72 29 20 62 The (slower) b
2b3a0 74 72 65 65 50 72 65 76 69 6f 75 73 28 29 0a 2a treePrevious().*
2b3b0 2a 20 68 65 6c 70 65 72 20 72 6f 75 74 69 6e 65 * helper routine
2b3c0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
2b3d0 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 it is necessary
2b3e0 74 6f 20 6d 6f 76 65 20 74 6f 20 61 20 64 69 66 to move to a dif
2b3f0 66 65 72 65 6e 74 20 70 61 67 65 0a 2a 2a 20 6f ferent page.** o
2b400 72 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 r to restore the
2b410 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 cursor..**.** T
2b420 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
2b430 69 6f 6e 20 77 69 6c 6c 20 73 65 74 20 2a 70 52 ion will set *pR
2b440 65 73 20 74 6f 20 30 20 6f 72 20 31 2e 20 20 54 es to 0 or 1. T
2b450 68 65 20 69 6e 69 74 69 61 6c 20 2a 70 52 65 73 he initial *pRes
2b460 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 value.** will b
2b470 65 20 31 20 69 66 20 74 68 65 20 63 75 72 73 6f e 1 if the curso
2b480 72 20 62 65 69 6e 67 20 73 74 65 70 70 65 64 20 r being stepped
2b490 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 corresponds to a
2b4a0 6e 20 53 51 4c 20 69 6e 64 65 78 20 61 6e 64 0a n SQL index and.
2b4b0 2a 2a 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 ** if this routi
2b4c0 6e 65 20 63 6f 75 6c 64 20 68 61 76 65 20 62 65 ne could have be
2b4d0 65 6e 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 en skipped if th
2b4e0 61 74 20 53 51 4c 20 69 6e 64 65 78 20 68 61 64 at SQL index had
2b4f0 20 62 65 65 6e 0a 2a 2a 20 61 20 75 6e 69 71 75 been.** a uniqu
2b500 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 e index. Otherw
2b510 69 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 77 ise the caller w
2b520 69 6c 6c 20 68 61 76 65 20 73 65 74 20 2a 70 52 ill have set *pR
2b530 65 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 5a es to zero..** Z
2b540 65 72 6f 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f ero is the commo
2b550 6e 20 63 61 73 65 2e 20 54 68 65 20 62 74 72 65 n case. The btre
2b560 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
2b570 20 69 73 20 66 72 65 65 20 74 6f 20 75 73 65 20 is free to use
2b580 74 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 20 2a the.** initial *
2b590 70 52 65 73 20 76 61 6c 75 65 20 61 73 20 61 20 pRes value as a
2b5a0 68 69 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 hint to improve
2b5b0 70 65 72 66 6f 72 6d 61 6e 63 65 2c 20 62 75 74 performance, but
2b5c0 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
2b5d0 53 51 4c 69 74 65 20 62 74 72 65 65 20 69 6d 70 SQLite btree imp
2b5e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
2b5f0 20 6e 6f 74 2e 20 28 4e 6f 74 65 20 74 68 61 74 not. (Note that
2b600 20 74 68 65 20 63 6f 6d 64 62 32 20 62 74 72 65 the comdb2 btre
2b610 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
2b620 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 69 ion does use thi
2b630 73 20 68 69 6e 74 2c 20 68 6f 77 65 76 65 72 2e s hint, however.
2b640 29 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 ).*/.static SQLI
2b650 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 69 6e 74 20 TE_NOINLINE int
2b660 62 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 btreePrevious(Bt
2b670 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
2b680 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
2b690 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 rc;. MemPage *p
2b6a0 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
2b6b0 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
2b6c0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
2b6d0 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a ert( pRes!=0 );.
2b6e0 20 20 61 73 73 65 72 74 28 20 2a 70 52 65 73 3d assert( *pRes=
2b6f0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
2b700 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3d 3d pCur->skipNext==
2b710 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 0 || pCur->eStat
2b720 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
2b730 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 43 );. assert( (pC
2b740 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 28 ur->curFlags & (
2b750 42 54 43 46 5f 41 74 4c 61 73 74 7c 42 54 43 46 BTCF_AtLast|BTCF
2b760 5f 56 61 6c 69 64 4f 76 66 6c 7c 42 54 43 46 5f _ValidOvfl|BTCF_
2b770 56 61 6c 69 64 4e 4b 65 79 29 29 3d 3d 30 20 29 ValidNKey))==0 )
2b780 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
2b790 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 ->info.nSize==0
2b7a0 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 );. if( pCur->e
2b7b0 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 State!=CURSOR_VA
2b7c0 4c 49 44 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 LID ){. rc =
2b7d0 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
2b7e0 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 20 ition(pCur);.
2b7f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
2b800 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
2b810 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
2b820 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 if( CURSOR_INVA
2b830 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 LID==pCur->eStat
2b840 65 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 e ){. *pRes
2b850 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 = 1;. retu
2b860 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
2b870 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 }. if( pCur
2b880 2d 3e 73 6b 69 70 4e 65 78 74 20 29 7b 0a 20 20 ->skipNext ){.
2b890 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
2b8a0 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
2b8b0 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e _VALID || pCur->
2b8c0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 53 eState==CURSOR_S
2b8d0 4b 49 50 4e 45 58 54 20 29 3b 0a 20 20 20 20 20 KIPNEXT );.
2b8e0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
2b8f0 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 CURSOR_VALID;.
2b900 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b if( pCur->sk
2b910 69 70 4e 65 78 74 3c 30 20 29 7b 0a 20 20 20 20 ipNext<0 ){.
2b920 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 pCur->skipNe
2b930 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 xt = 0;.
2b940 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
2b950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
2b960 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d pCur->skipNext =
2b970 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0;. }. }..
2b980 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
2b990 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
2b9a0 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 e];. assert( pP
2b9b0 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 age->isInit );.
2b9c0 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
2b9d0 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 f ){. int idx
2b9e0 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 = pCur->aiIdx[p
2b9f0 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 Cur->iPage];.
2ba00 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
2ba10 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 d(pCur, get4byte
2ba20 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c (findCell(pPage,
2ba30 20 69 64 78 29 29 29 3b 0a 20 20 20 20 69 66 28 idx)));. if(
2ba40 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
2ba50 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
2ba60 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b Rightmost(pCur);
2ba70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 . }else{. wh
2ba80 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ile( pCur->aiIdx
2ba90 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 [pCur->iPage]==0
2baa0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 ){. if( pC
2bab0 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a ur->iPage==0 ){.
2bac0 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 pCur->eS
2bad0 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
2bae0 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a VALID;. *
2baf0 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 pRes = 1;.
2bb00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
2bb10 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OK;. }.
2bb20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 moveToParent(p
2bb30 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Cur);. }.
2bb40 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 6e assert( pCur->in
2bb50 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 fo.nSize==0 );.
2bb60 20 20 20 61 73 73 65 72 74 28 20 28 70 43 75 72 assert( (pCur
2bb70 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 28 42 54 ->curFlags & (BT
2bb80 43 46 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 CF_ValidNKey|BTC
2bb90 46 5f 56 61 6c 69 64 4f 76 66 6c 29 29 3d 3d 30 F_ValidOvfl))==0
2bba0 20 29 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e 61 );.. pCur->a
2bbb0 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
2bbc0 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 3d ]--;. pPage =
2bbd0 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
2bbe0 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
2bbf0 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
2bc00 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 y && !pPage->lea
2bc10 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 f ){. rc =
2bc20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
2bc30 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73 29 ious(pCur, pRes)
2bc40 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
2bc50 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
2bc60 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 K;. }. }. r
2bc70 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20 eturn rc;.}.int
2bc80 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
2bc90 69 6f 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 ious(BtCursor *p
2bca0 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b Cur, int *pRes){
2bcb0 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
2bcc0 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
2bcd0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
2bce0 52 65 73 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 Res!=0 );. asse
2bcf0 72 74 28 20 2a 70 52 65 73 3d 3d 30 20 7c 7c 20 rt( *pRes==0 ||
2bd00 2a 70 52 65 73 3d 3d 31 20 29 3b 0a 20 20 61 73 *pRes==1 );. as
2bd10 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 sert( pCur->skip
2bd20 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d Next==0 || pCur-
2bd30 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
2bd40 56 41 4c 49 44 20 29 3b 0a 20 20 2a 70 52 65 73 VALID );. *pRes
2bd50 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 = 0;. pCur->cu
2bd60 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 rFlags &= ~(BTCF
2bd70 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f 56 61 6c _AtLast|BTCF_Val
2bd80 69 64 4f 76 66 6c 7c 42 54 43 46 5f 56 61 6c 69 idOvfl|BTCF_Vali
2bd90 64 4e 4b 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e dNKey);. pCur->
2bda0 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
2bdb0 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
2bdc0 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te!=CURSOR_VALID
2bdd0 0a 20 20 20 7c 7c 20 70 43 75 72 2d 3e 61 69 49 . || pCur->aiI
2bde0 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d dx[pCur->iPage]=
2bdf0 3d 30 0a 20 20 20 7c 7c 20 70 43 75 72 2d 3e 61 =0. || pCur->a
2be00 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
2be10 65 5d 2d 3e 6c 65 61 66 3d 3d 30 0a 20 20 29 7b e]->leaf==0. ){
2be20 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 72 65 . return btre
2be30 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 ePrevious(pCur,
2be40 70 52 65 73 29 3b 0a 20 20 7d 0a 20 20 70 43 75 pRes);. }. pCu
2be50 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
2be60 50 61 67 65 5d 2d 2d 3b 0a 20 20 72 65 74 75 72 Page]--;. retur
2be70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
2be80 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
2be90 20 6e 65 77 20 70 61 67 65 20 66 72 6f 6d 20 74 new page from t
2bea0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
2beb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 ..**.** The new
2bec0 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 page is marked a
2bed0 73 20 64 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 s dirty. (In ot
2bee0 68 65 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 her words, sqlit
2bef0 65 33 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a e3PagerWrite().*
2bf00 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 * has already be
2bf10 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 en called on the
2bf20 20 6e 65 77 20 70 61 67 65 2e 29 20 20 54 68 65 new page.) The
2bf30 20 6e 65 77 20 70 61 67 65 20 68 61 73 20 61 6c new page has al
2bf40 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 so.** been refer
2bf50 65 6e 63 65 64 20 61 6e 64 20 74 68 65 20 63 61 enced and the ca
2bf60 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 lling routine is
2bf70 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
2bf80 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 calling.** sqli
2bf90 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 te3PagerUnref()
2bfa0 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 on the new page
2bfb0 77 68 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e when it is done.
2bfc0 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
2bfd0 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
2bfe0 73 75 63 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 success. Any ot
2bff0 68 65 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 her return value
2c000 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e indicates.** an
2c010 20 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 error. *ppPage
2c020 20 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 and *pPgno are
2c030 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 undefined in the
2c040 20 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 event of an err
2c050 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e or..** Do not in
2c060 76 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 67 65 voke sqlite3Page
2c070 72 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 rUnref() on *ppP
2c080 61 67 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 age if an error
2c090 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
2c0a0 2a 2a 20 49 66 20 74 68 65 20 22 6e 65 61 72 62 ** If the "nearb
2c0b0 79 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 y" parameter is
2c0c0 6e 6f 74 20 30 2c 20 74 68 65 6e 20 61 6e 20 65 not 0, then an e
2c0d0 66 66 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f ffort is made to
2c0e0 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 .** locate a pa
2c0f0 67 65 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 ge close to the
2c100 70 61 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 page number "nea
2c110 72 62 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20 rby". This can
2c120 62 65 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a be used in an.**
2c130 20 61 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 attempt to keep
2c140 20 72 65 6c 61 74 65 64 20 70 61 67 65 73 20 63 related pages c
2c150 6c 6f 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68 lose to each oth
2c160 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 er in the databa
2c170 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 se file,.** whic
2c180 68 20 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 h in turn can ma
2c190 6b 65 20 64 61 74 61 62 61 73 65 20 61 63 63 65 ke database acce
2c1a0 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a ss faster..**.**
2c1b0 20 49 66 20 74 68 65 20 65 4d 6f 64 65 20 70 61 If the eMode pa
2c1c0 72 61 6d 65 74 65 72 20 69 73 20 42 54 41 4c 4c rameter is BTALL
2c1d0 4f 43 5f 45 58 41 43 54 20 61 6e 64 20 74 68 65 OC_EXACT and the
2c1e0 20 6e 65 61 72 62 79 20 70 61 67 65 20 65 78 69 nearby page exi
2c1f0 73 74 73 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 sts.** anywhere
2c200 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 on the free-list
2c210 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 , then it is gua
2c220 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65 ranteed to be re
2c230 74 75 72 6e 65 64 2e 20 20 49 66 0a 2a 2a 20 65 turned. If.** e
2c240 4d 6f 64 65 20 69 73 20 42 54 41 4c 4c 4f 43 5f Mode is BTALLOC_
2c250 4c 54 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 LT then the page
2c260 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 returned will b
2c270 65 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 e less than or e
2c280 71 75 61 6c 0a 2a 2a 20 74 6f 20 6e 65 61 72 62 qual.** to nearb
2c290 79 20 69 66 20 61 6e 79 20 73 75 63 68 20 70 61 y if any such pa
2c2a0 67 65 20 65 78 69 73 74 73 2e 20 20 49 66 20 65 ge exists. If e
2c2b0 4d 6f 64 65 20 69 73 20 42 54 41 4c 4c 4f 43 5f Mode is BTALLOC_
2c2c0 41 4e 59 20 74 68 65 6e 20 74 68 65 72 65 0a 2a ANY then there.*
2c2d0 2a 20 61 72 65 20 6e 6f 20 72 65 73 74 72 69 63 * are no restric
2c2e0 74 69 6f 6e 73 20 6f 6e 20 77 68 69 63 68 20 70 tions on which p
2c2f0 61 67 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e age is returned.
2c300 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
2c310 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
2c320 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
2c330 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 t, /* Th
2c340 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d e btree */. Mem
2c350 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 Page **ppPage,
2c360 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 70 6f 69 /* Store poi
2c370 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f nter to the allo
2c380 63 61 74 65 64 20 70 61 67 65 20 68 65 72 65 20 cated page here
2c390 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f */. Pgno *pPgno
2c3a0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 , /* S
2c3b0 74 6f 72 65 20 74 68 65 20 70 61 67 65 20 6e 75 tore the page nu
2c3c0 6d 62 65 72 20 68 65 72 65 20 2a 2f 0a 20 20 50 mber here */. P
2c3d0 67 6e 6f 20 6e 65 61 72 62 79 2c 20 20 20 20 20 gno nearby,
2c3e0 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 /* Search
2c3f0 66 6f 72 20 61 20 70 61 67 65 20 6e 65 61 72 20 for a page near
2c400 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 75 38 this one */. u8
2c410 20 65 4d 6f 64 65 20 20 20 20 20 20 20 20 20 20 eMode
2c420 20 20 20 20 20 2f 2a 20 42 54 41 4c 4c 4f 43 5f /* BTALLOC_
2c430 45 58 41 43 54 2c 20 42 54 41 4c 4c 4f 43 5f 4c EXACT, BTALLOC_L
2c440 54 2c 20 6f 72 20 42 54 41 4c 4c 4f 43 5f 41 4e T, or BTALLOC_AN
2c450 59 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 Y */.){. MemPag
2c460 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 e *pPage1;. int
2c470 20 72 63 3b 0a 20 20 75 33 32 20 6e 3b 20 20 20 rc;. u32 n;
2c480 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
2c490 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ages on the free
2c4a0 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32 20 6b 3b list */. u32 k;
2c4b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
2c4c0 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 f leaves on the
2c4d0 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 trunk of the fre
2c4e0 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 elist */. MemPa
2c4f0 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a ge *pTrunk = 0;.
2c500 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76 MemPage *pPrev
2c510 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 50 67 6e Trunk = 0;. Pgn
2c520 6f 20 6d 78 50 61 67 65 3b 20 20 20 20 20 2f 2a o mxPage; /*
2c530 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 Total size of t
2c540 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
2c550 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
2c560 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
2c570 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
2c580 0a 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 . assert( eMode
2c590 3d 3d 42 54 41 4c 4c 4f 43 5f 41 4e 59 20 7c 7c ==BTALLOC_ANY ||
2c5a0 20 28 6e 65 61 72 62 79 3e 30 20 26 26 20 49 66 (nearby>0 && If
2c5b0 4e 6f 74 4f 6d 69 74 41 56 28 70 42 74 2d 3e 61 NotOmitAV(pBt->a
2c5c0 75 74 6f 56 61 63 75 75 6d 29 29 20 29 3b 0a 20 utoVacuum)) );.
2c5d0 20 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 pPage1 = pBt->p
2c5e0 50 61 67 65 31 3b 0a 20 20 6d 78 50 61 67 65 20 Page1;. mxPage
2c5f0 3d 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 = btreePagecount
2c600 28 70 42 74 29 3b 0a 20 20 2f 2a 20 45 56 49 44 (pBt);. /* EVID
2c610 45 4e 43 45 2d 4f 46 3a 20 52 2d 30 35 31 31 39 ENCE-OF: R-05119
2c620 2d 30 32 36 33 37 20 54 68 65 20 34 2d 62 79 74 -02637 The 4-byt
2c630 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
2c640 65 67 65 72 20 61 74 20 6f 66 66 73 65 74 20 33 eger at offset 3
2c650 36 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 73 74 6. ** stores st
2c660 6f 72 65 73 20 74 68 65 20 74 6f 74 61 6c 20 6e ores the total n
2c670 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
2c680 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 n the freelist.
2c690 2a 2f 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 */. n = get4byt
2c6a0 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
2c6b0 5b 33 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 [36]);. testcas
2c6c0 65 28 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 e( n==mxPage-1 )
2c6d0 3b 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 ;. if( n>=mxPag
2c6e0 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
2c6f0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
2c700 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e KPT;. }. if( n
2c710 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 >0 ){. /* The
2c720 72 65 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 re are pages on
2c730 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 the freelist. R
2c740 65 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 euse one of thos
2c750 65 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 e pages. */.
2c760 50 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 Pgno iTrunk;.
2c770 20 75 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d u8 searchList =
2c780 20 30 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 0; /* If the fr
2c790 65 65 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 ee-list must be
2c7a0 73 65 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 searched for 'ne
2c7b0 61 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 arby' */. .
2c7c0 20 20 2f 2a 20 49 66 20 65 4d 6f 64 65 3d 3d 42 /* If eMode==B
2c7d0 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 61 6e 64 TALLOC_EXACT and
2c7e0 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 a query of the
2c7f0 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 pointer-map.
2c800 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 ** shows that th
2c810 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 e page 'nearby'
2c820 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 is somewhere on
2c830 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 the free-list, t
2c840 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 hen. ** the e
2c850 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 ntire-list will
2c860 62 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 be searched for
2c870 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a that page.. *
2c880 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
2c890 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
2c8a0 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d . if( eMode==
2c8b0 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 29 7b BTALLOC_EXACT ){
2c8c0 0a 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 . if( nearb
2c8d0 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 y<=mxPage ){.
2c8e0 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 u8 eType;.
2c8f0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
2c900 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 earby>0 );.
2c910 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
2c920 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 autoVacuum );.
2c930 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
2c940 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 pGet(pBt, nearby
2c950 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 , &eType, 0);.
2c960 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
2c970 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
2c980 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
2c990 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
2c9a0 20 20 20 20 20 20 20 20 20 20 73 65 61 72 63 68 search
2c9b0 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 List = 1;.
2c9c0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
2c9d0 7d 65 6c 73 65 20 69 66 28 20 65 4d 6f 64 65 3d }else if( eMode=
2c9e0 3d 42 54 41 4c 4c 4f 43 5f 4c 45 20 29 7b 0a 20 =BTALLOC_LE ){.
2c9f0 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 searchList
2ca00 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 = 1;. }.#endi
2ca10 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d f.. /* Decrem
2ca20 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 ent the free-lis
2ca30 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 t count by 1. Se
2ca40 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 t iTrunk to the
2ca50 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 index of the.
2ca60 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c ** first free-l
2ca70 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 ist trunk page.
2ca80 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e iPrevTrunk is in
2ca90 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a itially 1.. *
2caa0 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
2cab0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
2cac0 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
2cad0 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
2cae0 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62 rn rc;. put4b
2caf0 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
2cb00 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 ta[36], n-1);..
2cb10 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 /* The code w
2cb20 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 ithin this loop
2cb30 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 is run only once
2cb40 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c if the 'searchL
2cb50 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20 ist' variable.
2cb60 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 ** is not true
2cb70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
2cb80 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 runs once for ea
2cb90 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e ch trunk-page on
2cba0 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 the. ** free
2cbb0 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20 -list until the
2cbc0 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 page 'nearby' is
2cbd0 20 6c 6f 63 61 74 65 64 20 28 65 4d 6f 64 65 3d located (eMode=
2cbe0 3d 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 29 0a =BTALLOC_EXACT).
2cbf0 20 20 20 20 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 ** or until
2cc00 61 20 70 61 67 65 20 6c 65 73 73 20 74 68 61 6e a page less than
2cc10 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63 'nearby' is loc
2cc20 61 74 65 64 20 28 65 4d 6f 64 65 3d 3d 42 54 41 ated (eMode==BTA
2cc30 4c 4c 4f 43 5f 4c 54 29 0a 20 20 20 20 2a 2f 0a LLOC_LT). */.
2cc40 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 do {. p
2cc50 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 PrevTrunk = pTru
2cc60 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 nk;. if( pP
2cc70 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 revTrunk ){.
2cc80 20 20 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d /* EVIDENCE-
2cc90 4f 46 3a 20 52 2d 30 31 35 30 36 2d 31 31 30 35 OF: R-01506-1105
2cca0 33 20 54 68 65 20 66 69 72 73 74 20 69 6e 74 65 3 The first inte
2ccb0 67 65 72 20 6f 6e 20 61 20 66 72 65 65 6c 69 73 ger on a freelis
2ccc0 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 20 20 20 t trunk page.
2ccd0 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 70 ** is the p
2cce0 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
2ccf0 65 20 6e 65 78 74 20 66 72 65 65 6c 69 73 74 20 e next freelist
2cd00 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 trunk page in th
2cd10 65 20 6c 69 73 74 20 6f 72 0a 20 20 20 20 20 20 e list or.
2cd20 20 20 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 69 ** zero if thi
2cd30 73 20 69 73 20 74 68 65 20 6c 61 73 74 20 66 72 s is the last fr
2cd40 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 eelist trunk pag
2cd50 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 54 e. */. iT
2cd60 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 runk = get4byte(
2cd70 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
2cd80 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 ta[0]);. }e
2cd90 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
2cda0 45 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 35 EVIDENCE-OF: R-5
2cdb0 39 38 34 31 2d 31 33 37 39 38 20 54 68 65 20 34 9841-13798 The 4
2cdc0 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e -byte big-endian
2cdd0 20 69 6e 74 65 67 65 72 20 61 74 20 6f 66 66 73 integer at offs
2cde0 65 74 20 33 32 0a 20 20 20 20 20 20 20 20 2a 2a et 32. **
2cdf0 20 73 74 6f 72 65 73 20 74 68 65 20 70 61 67 65 stores the page
2ce00 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 number of the f
2ce10 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
2ce20 20 66 72 65 65 6c 69 73 74 2c 20 6f 72 20 7a 65 freelist, or ze
2ce30 72 6f 20 69 66 0a 20 20 20 20 20 20 20 20 2a 2a ro if. **
2ce40 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 the freelist is
2ce50 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20 20 20 empty. */.
2ce60 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 iTrunk = get4
2ce70 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 byte(&pPage1->aD
2ce80 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 ata[32]);.
2ce90 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 }. testcase
2cea0 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 ( iTrunk==mxPage
2ceb0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 54 );. if( iT
2cec0 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 0a 20 runk>mxPage ){.
2ced0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
2cee0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
2cef0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
2cf00 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
2cf10 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 GetPage(pBt, iTr
2cf20 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 unk, &pTrunk, 0)
2cf30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
2cf40 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
2cf50 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 pTrunk = 0;.
2cf60 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
2cf70 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
2cf80 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
2cf90 72 74 28 20 70 54 72 75 6e 6b 21 3d 30 20 29 3b rt( pTrunk!=0 );
2cfa0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
2cfb0 54 72 75 6e 6b 2d 3e 61 44 61 74 61 21 3d 30 20 Trunk->aData!=0
2cfc0 29 3b 0a 20 20 20 20 20 20 2f 2a 20 45 56 49 44 );. /* EVID
2cfd0 45 4e 43 45 2d 4f 46 3a 20 52 2d 31 33 35 32 33 ENCE-OF: R-13523
2cfe0 2d 30 34 33 39 34 20 54 68 65 20 73 65 63 6f 6e -04394 The secon
2cff0 64 20 69 6e 74 65 67 65 72 20 6f 6e 20 61 20 66 d integer on a f
2d000 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 reelist trunk pa
2d010 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 ge. ** is t
2d020 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 he number of lea
2d030 66 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73 20 f page pointers
2d040 74 6f 20 66 6f 6c 6c 6f 77 2e 20 2a 2f 0a 20 20 to follow. */.
2d050 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 k = get4byte
2d060 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
2d070 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 4]);. if( k
2d080 3d 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 ==0 && !searchLi
2d090 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a st ){. /*
2d0a0 20 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e The trunk has n
2d0b0 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 o leaves and the
2d0c0 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 list is not bei
2d0d0 6e 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 ng searched. .
2d0e0 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 ** So extr
2d0f0 61 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 act the trunk pa
2d100 67 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 ge itself and us
2d110 65 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c e it as the newl
2d120 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c y . ** al
2d130 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a located page */.
2d140 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
2d150 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b pPrevTrunk==0 );
2d160 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
2d170 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
2d180 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 pTrunk->pDbPage)
2d190 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
2d1a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
2d1b0 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
2d1c0 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a page;. }.
2d1d0 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d *pPgno =
2d1e0 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 iTrunk;.
2d1f0 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d memcpy(&pPage1-
2d200 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 >aData[32], &pTr
2d210 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 unk->aData[0], 4
2d220 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 );. *ppPa
2d230 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 ge = pTrunk;.
2d240 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b pTrunk = 0;
2d250 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 . TRACE((
2d260 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 "ALLOCATE: %d tr
2d270 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 unk - %d free pa
2d280 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 ges left\n", *pP
2d290 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 gno, n-1));.
2d2a0 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 }else if( k>(u
2d2b0 33 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 32)(pBt->usableS
2d2c0 69 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 ize/4 - 2) ){.
2d2d0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f /* Value o
2d2e0 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 f k is out of ra
2d2f0 6e 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 nge. Database c
2d300 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 orruption */.
2d310 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
2d320 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
2d330 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
2d340 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 allocate_page;.#
2d350 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
2d360 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
2d370 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 }else if( se
2d380 61 72 63 68 4c 69 73 74 20 0a 20 20 20 20 20 20 archList .
2d390 20 20 20 20 20 20 26 26 20 28 6e 65 61 72 62 79 && (nearby
2d3a0 3d 3d 69 54 72 75 6e 6b 20 7c 7c 20 28 69 54 72 ==iTrunk || (iTr
2d3b0 75 6e 6b 3c 6e 65 61 72 62 79 20 26 26 20 65 4d unk<nearby && eM
2d3c0 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 45 29 ode==BTALLOC_LE)
2d3d0 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 ) . ){.
2d3e0 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 /* The list
2d3f0 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 is being searche
2d400 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b d and this trunk
2d410 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 page is the pag
2d420 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 e. ** to
2d430 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 allocate, regard
2d440 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 less of whether
2d450 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 it has leaves..
2d460 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
2d470 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e *pPgno = iTrun
2d480 6b 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 k;. *ppPa
2d490 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 ge = pTrunk;.
2d4a0 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 searchList
2d4b0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 = 0;. rc
2d4c0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
2d4d0 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 ite(pTrunk->pDbP
2d4e0 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 age);. if
2d4f0 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
2d500 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
2d510 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
2d520 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
2d530 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 k==0 ){.
2d540 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e if( !pPrevTrun
2d550 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 k ){.
2d560 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d memcpy(&pPage1-
2d570 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 >aData[32], &pTr
2d580 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 unk->aData[0], 4
2d590 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
2d5a0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
2d5b0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
2d5c0 72 57 72 69 74 65 28 70 50 72 65 76 54 72 75 6e rWrite(pPrevTrun
2d5d0 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
2d5e0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
2d5f0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
2d600 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
2d610 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
2d620 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge;.
2d630 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 }. me
2d640 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e 6b mcpy(&pPrevTrunk
2d650 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 ->aData[0], &pTr
2d660 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 unk->aData[0], 4
2d670 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
2d680 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
2d690 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
2d6a0 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 71 runk page is req
2d6b0 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61 6c uired by the cal
2d6c0 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 61 ler but it conta
2d6d0 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a ins . *
2d6e0 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 * pointers to fr
2d6f0 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 ee-list leaves.
2d700 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20 62 The first leaf b
2d710 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 ecomes a trunk.
2d720 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 ** page
2d730 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 in this case..
2d740 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
2d750 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
2d760 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 NewTrunk;.
2d770 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 Pgno iNewTru
2d780 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 nk = get4byte(&p
2d790 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 Trunk->aData[8])
2d7a0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
2d7b0 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 50 61 67 65 iNewTrunk>mxPage
2d7c0 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20 ){ .
2d7d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
2d7e0 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
2d7f0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
2d800 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
2d810 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
2d820 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 testcase( i
2d830 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 NewTrunk==mxPage
2d840 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 );. rc
2d850 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
2d860 70 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 pBt, iNewTrunk,
2d870 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a &pNewTrunk, 0);.
2d880 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
2d890 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
2d8a0 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
2d8b0 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
2d8c0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 e;. }.
2d8d0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
2d8e0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
2d8f0 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 pNewTrunk->pDbPa
2d900 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
2d910 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
2d920 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2d930 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
2d940 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
2d950 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
2d960 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
2d970 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2d980 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 memcpy(&pNewTr
2d990 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 unk->aData[0], &
2d9a0 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
2d9b0 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 4);.
2d9c0 70 75 74 34 62 79 74 65 28 26 70 4e 65 77 54 72 put4byte(&pNewTr
2d9d0 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b unk->aData[4], k
2d9e0 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d -1);. m
2d9f0 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b emcpy(&pNewTrunk
2da00 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 ->aData[8], &pTr
2da10 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 unk->aData[12],
2da20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 (k-1)*4);.
2da30 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
2da40 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 pNewTrunk);.
2da50 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 if( !pPrev
2da60 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 Trunk ){.
2da70 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
2da80 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
2da90 61 62 6c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 able(pPage1->pDb
2daa0 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 Page) );.
2dab0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
2dac0 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
2dad0 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 , iNewTrunk);.
2dae0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
2daf0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
2db00 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
2db10 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 e(pPrevTrunk->pD
2db20 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
2db30 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
2db40 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
2db50 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
2db60 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge;.
2db70 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 }. pu
2db80 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 t4byte(&pPrevTru
2db90 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e nk->aData[0], iN
2dba0 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
2dbb0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
2dbc0 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
2dbd0 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 0;. TRAC
2dbe0 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 E(("ALLOCATE: %d
2dbf0 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 trunk - %d free
2dc00 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 pages left\n",
2dc10 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 *pPgno, n-1));.#
2dc20 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 endif. }els
2dc30 65 20 69 66 28 20 6b 3e 30 20 29 7b 0a 20 20 20 e if( k>0 ){.
2dc40 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 /* Extract
2dc50 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20 a leaf from the
2dc60 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 trunk */.
2dc70 20 75 33 32 20 63 6c 6f 73 65 73 74 3b 0a 20 20 u32 closest;.
2dc80 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65 Pgno iPage
2dc90 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e ;. unsign
2dca0 65 64 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d ed char *aData =
2dcb0 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a pTrunk->aData;.
2dcc0 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 if( near
2dcd0 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 by>0 ){.
2dce0 20 20 75 33 32 20 69 3b 0a 20 20 20 20 20 20 20 u32 i;.
2dcf0 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a closest = 0;.
2dd00 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 4d if( eM
2dd10 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 45 20 ode==BTALLOC_LE
2dd20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 ){. f
2dd30 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b or(i=0; i<k; i++
2dd40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2dd50 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 iPage = get4byt
2dd60 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 e(&aData[8+i*4])
2dd70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
2dd80 69 66 28 20 69 50 61 67 65 3c 3d 6e 65 61 72 62 if( iPage<=nearb
2dd90 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 y ){.
2dda0 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 closest = i
2ddb0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
2ddc0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
2ddd0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2dde0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
2ddf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
2de00 20 20 20 20 69 6e 74 20 64 69 73 74 3b 0a 20 20 int dist;.
2de10 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d dist =
2de20 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 32 sqlite3AbsInt32
2de30 28 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 (get4byte(&aData
2de40 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 29 3b 0a [8]) - nearby);.
2de50 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 for(
2de60 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a i=1; i<k; i++){.
2de70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
2de80 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 41 62 t d2 = sqlite3Ab
2de90 73 49 6e 74 33 32 28 67 65 74 34 62 79 74 65 28 sInt32(get4byte(
2dea0 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d &aData[8+i*4]) -
2deb0 20 6e 65 61 72 62 79 29 3b 0a 20 20 20 20 20 20 nearby);.
2dec0 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 if( d2<d
2ded0 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ist ){.
2dee0 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d closest =
2def0 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 i;.
2df00 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 dist = d2;.
2df10 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
2df20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
2df30 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2df40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
2df50 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 closest = 0;.
2df60 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
2df70 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 79 iPage = get4by
2df80 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 te(&aData[8+clos
2df90 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 est*4]);.
2dfa0 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67 65 testcase( iPage
2dfb0 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 ==mxPage );.
2dfc0 20 20 20 20 69 66 28 20 69 50 61 67 65 3e 6d 78 if( iPage>mx
2dfd0 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
2dfe0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
2dff0 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
2e000 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
2e010 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
2e020 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2e030 74 65 73 74 63 61 73 65 28 20 69 50 61 67 65 3d testcase( iPage=
2e040 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 =mxPage );.
2e050 20 20 20 69 66 28 20 21 73 65 61 72 63 68 4c 69 if( !searchLi
2e060 73 74 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 st . ||
2e070 28 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 7c (iPage==nearby |
2e080 7c 20 28 69 50 61 67 65 3c 6e 65 61 72 62 79 20 | (iPage<nearby
2e090 26 26 20 65 4d 6f 64 65 3d 3d 42 54 41 4c 4c 4f && eMode==BTALLO
2e0a0 43 5f 4c 45 29 29 20 0a 20 20 20 20 20 20 20 20 C_LE)) .
2e0b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
2e0c0 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 noContent;.
2e0d0 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 *pPgno = i
2e0e0 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 Page;.
2e0f0 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
2e100 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 25 64 : %d was leaf %d
2e110 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 of %d on trunk
2e120 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 %d".
2e130 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 ": %d more
2e140 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 free pages\n",.
2e150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e160 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b *pPgno, closest+
2e170 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 1, k, pTrunk->pg
2e180 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 no, n-1));.
2e190 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
2e1a0 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 3PagerWrite(pTru
2e1b0 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
2e1c0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
2e1d0 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
2e1e0 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
2e1f0 20 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b if( closest<k
2e200 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 -1 ){.
2e210 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b memcpy(&aData[
2e220 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 8+closest*4], &a
2e230 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b Data[4+k*4], 4);
2e240 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2e250 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
2e260 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b &aData[4], k-1);
2e270 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e . noCon
2e280 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 65 74 tent = !btreeGet
2e290 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 HasContent(pBt,
2e2a0 2a 70 50 67 6e 6f 29 3f 20 50 41 47 45 52 5f 47 *pPgno)? PAGER_G
2e2b0 45 54 5f 4e 4f 43 4f 4e 54 45 4e 54 20 3a 20 30 ET_NOCONTENT : 0
2e2c0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
2e2d0 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
2e2e0 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 t, *pPgno, ppPag
2e2f0 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 e, noContent);.
2e300 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
2e310 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
2e320 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
2e330 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
2e340 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 ((*ppPage)->pDbP
2e350 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
2e360 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
2e370 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
2e380 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
2e390 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 (*ppPage);.
2e3a0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2e3b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
2e3c0 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 earchList = 0;.
2e3d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
2e3e0 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
2e3f0 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a ge(pPrevTrunk);.
2e400 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b pPrevTrunk
2e410 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65 = 0;. }while
2e420 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a ( searchList );.
2e430 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
2e440 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 There are no pag
2e450 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 es on the freeli
2e460 73 74 2c 20 73 6f 20 61 70 70 65 6e 64 20 61 20 st, so append a
2e470 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 65 0a new page to the.
2e480 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
2e490 69 6d 61 67 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 image.. **.
2e4a0 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 6e ** Normally, n
2e4b0 65 77 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 ew pages allocat
2e4c0 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b ed by this block
2e4d0 20 63 61 6e 20 62 65 20 72 65 71 75 65 73 74 65 can be requeste
2e4e0 64 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a d from the. *
2e4f0 2a 20 70 61 67 65 72 20 6c 61 79 65 72 20 77 69 * pager layer wi
2e500 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65 th the 'no-conte
2e510 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20 54 68 nt' flag set. Th
2e520 69 73 20 70 72 65 76 65 6e 74 73 20 74 68 65 20 is prevents the
2e530 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 66 72 6f pager. ** fro
2e540 6d 20 74 72 79 69 6e 67 20 74 6f 20 72 65 61 64 m trying to read
2e550 20 74 68 65 20 70 61 67 65 73 20 63 6f 6e 74 65 the pages conte
2e560 6e 74 20 66 72 6f 6d 20 64 69 73 6b 2e 20 48 6f nt from disk. Ho
2e570 77 65 76 65 72 2c 20 69 66 20 74 68 65 0a 20 20 wever, if the.
2e580 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 74 72 61 ** current tra
2e590 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 nsaction has alr
2e5a0 65 61 64 79 20 72 75 6e 20 6f 6e 65 20 6f 72 20 eady run one or
2e5b0 6d 6f 72 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c more incremental
2e5c0 2d 76 61 63 75 75 6d 0a 20 20 20 20 2a 2a 20 73 -vacuum. ** s
2e5d0 74 65 70 73 2c 20 74 68 65 6e 20 74 68 65 20 70 teps, then the p
2e5e0 61 67 65 20 77 65 20 61 72 65 20 61 62 6f 75 74 age we are about
2e5f0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 61 79 to allocate may
2e600 20 63 6f 6e 74 61 69 6e 20 63 6f 6e 74 65 6e 74 contain content
2e610 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 . ** that is
2e620 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 required in the
2e630 65 76 65 6e 74 20 6f 66 20 61 20 72 6f 6c 6c 62 event of a rollb
2e640 61 63 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ack. In this cas
2e650 65 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 e, do. ** not
2e660 20 73 65 74 20 74 68 65 20 6e 6f 2d 63 6f 6e 74 set the no-cont
2e670 65 6e 74 20 66 6c 61 67 2e 20 54 68 69 73 20 63 ent flag. This c
2e680 61 75 73 65 73 20 74 68 65 20 70 61 67 65 72 20 auses the pager
2e690 74 6f 20 6c 6f 61 64 20 61 6e 64 20 6a 6f 75 72 to load and jour
2e6a0 6e 61 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 nal. ** the c
2e6b0 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f 6e 74 urrent page cont
2e6c0 65 6e 74 20 62 65 66 6f 72 65 20 6f 76 65 72 77 ent before overw
2e6d0 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a riting it.. *
2e6e0 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 *. ** Note th
2e6f0 61 74 20 74 68 65 20 70 61 67 65 72 20 77 69 6c at the pager wil
2e700 6c 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 61 l not actually a
2e710 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 6f ttempt to load o
2e720 72 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 20 20 2a r journal . *
2e730 2a 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61 6e * content for an
2e740 79 20 70 61 67 65 20 74 68 61 74 20 72 65 61 6c y page that real
2e750 6c 79 20 64 6f 65 73 20 6c 69 65 20 70 61 73 74 ly does lie past
2e760 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
2e770 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 database. **
2e780 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 53 6f file on disk. So
2e790 20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 the effects of
2e7a0 64 69 73 61 62 6c 69 6e 67 20 74 68 65 20 6e 6f disabling the no
2e7b0 2d 63 6f 6e 74 65 6e 74 20 6f 70 74 69 6d 69 7a -content optimiz
2e7c0 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 65 72 ation. ** her
2e7d0 65 20 61 72 65 20 63 6f 6e 66 69 6e 65 64 20 74 e are confined t
2e7e0 6f 20 74 68 6f 73 65 20 70 61 67 65 73 20 74 68 o those pages th
2e7f0 61 74 20 6c 69 65 20 62 65 74 77 65 65 6e 20 74 at lie between t
2e800 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 he end of the.
2e810 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 6d ** database im
2e820 61 67 65 20 61 6e 64 20 74 68 65 20 65 6e 64 20 age and the end
2e830 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
2e840 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 file.. */.
2e850 20 69 6e 74 20 62 4e 6f 43 6f 6e 74 65 6e 74 20 int bNoContent
2e860 3d 20 28 30 3d 3d 49 66 4e 6f 74 4f 6d 69 74 41 = (0==IfNotOmitA
2e870 56 28 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 61 V(pBt->bDoTrunca
2e880 74 65 29 29 3f 20 50 41 47 45 52 5f 47 45 54 5f te))? PAGER_GET_
2e890 4e 4f 43 4f 4e 54 45 4e 54 3a 30 3b 0a 0a 20 20 NOCONTENT:0;..
2e8a0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2e8b0 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 gerWrite(pBt->pP
2e8c0 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a age1->pDbPage);.
2e8d0 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
2e8e0 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 42 74 2d urn rc;. pBt-
2e8f0 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 69 66 >nPage++;. if
2e900 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 ( pBt->nPage==PE
2e910 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
2e920 70 42 74 29 20 29 20 70 42 74 2d 3e 6e 50 61 67 pBt) ) pBt->nPag
2e930 65 2b 2b 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 e++;..#ifndef SQ
2e940 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
2e950 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 CUUM. if( pBt
2e960 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 ->autoVacuum &&
2e970 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
2e980 74 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 20 29 t, pBt->nPage) )
2e990 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 2a 70 {. /* If *p
2e9a0 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f 20 61 Pgno refers to a
2e9b0 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
2e9c0 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 6f 20 e, allocate two
2e9d0 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 20 20 new pages.
2e9e0 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 ** at the end of
2e9f0 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 65 61 the file instea
2ea00 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 66 69 d of one. The fi
2ea10 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 rst allocated pa
2ea20 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 63 6f ge. ** beco
2ea30 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e 74 65 mes a new pointe
2ea40 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 65 20 r-map page, the
2ea50 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 20 62 second is used b
2ea60 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 y the caller..
2ea70 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d */. Mem
2ea80 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b 0a 20 Page *pPg = 0;.
2ea90 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
2eaa0 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 OCATE: %d from e
2eab0 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69 6e nd of file (poin
2eac0 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e 22 ter-map page)\n"
2ead0 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 29 3b 0a , pBt->nPage));.
2eae0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 42 assert( pB
2eaf0 74 2d 3e 6e 50 61 67 65 21 3d 50 45 4e 44 49 4e t->nPage!=PENDIN
2eb00 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
2eb10 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 );. rc = b
2eb20 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
2eb30 20 70 42 74 2d 3e 6e 50 61 67 65 2c 20 26 70 50 pBt->nPage, &pP
2eb40 67 2c 20 62 4e 6f 43 6f 6e 74 65 6e 74 29 3b 0a g, bNoContent);.
2eb50 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
2eb60 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
2eb70 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
2eb80 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 agerWrite(pPg->p
2eb90 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
2eba0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 releasePage(pPg
2ebb0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
2ebc0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
2ebd0 20 72 63 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e rc;. pBt->
2ebe0 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 20 20 69 nPage++;. i
2ebf0 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d 50 f( pBt->nPage==P
2ec00 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
2ec10 28 70 42 74 29 20 29 7b 20 70 42 74 2d 3e 6e 50 (pBt) ){ pBt->nP
2ec20 61 67 65 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 age++; }. }.#
2ec30 65 6e 64 69 66 0a 20 20 20 20 70 75 74 34 62 79 endif. put4by
2ec40 74 65 28 32 38 20 2b 20 28 75 38 2a 29 70 42 74 te(28 + (u8*)pBt
2ec50 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 2c ->pPage1->aData,
2ec60 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 pBt->nPage);.
2ec70 20 20 2a 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e *pPgno = pBt->
2ec80 6e 50 61 67 65 3b 0a 0a 20 20 20 20 61 73 73 65 nPage;.. asse
2ec90 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 rt( *pPgno!=PEND
2eca0 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
2ecb0 74 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 t) );. rc = b
2ecc0 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
2ecd0 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c *pPgno, ppPage,
2ece0 20 62 4e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 bNoContent);.
2ecf0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
2ed00 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 n rc;. rc = s
2ed10 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
2ed20 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 ((*ppPage)->pDbP
2ed30 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
2ed40 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
2ed50 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
2ed60 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d (*ppPage);. }
2ed70 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c . TRACE(("ALL
2ed80 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 OCATE: %d from e
2ed90 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a nd of file\n", *
2eda0 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 pPgno));. }..
2edb0 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d assert( *pPgno!=
2edc0 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
2edd0 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 E(pBt) );..end_a
2ede0 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 llocate_page:.
2edf0 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 releasePage(pTru
2ee00 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 nk);. releasePa
2ee10 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a ge(pPrevTrunk);.
2ee20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
2ee30 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 _OK ){. if( s
2ee40 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 qlite3PagerPageR
2ee50 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 efcount((*ppPage
2ee60 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b )->pDbPage)>1 ){
2ee70 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
2ee80 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
2ee90 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a *ppPage = 0;.
2eea0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
2eeb0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
2eec0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 ;. }. (*pp
2eed0 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 Page)->isInit =
2eee0 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
2eef0 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d *ppPage = 0;. }
2ef00 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
2ef10 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 QLITE_OK || sqli
2ef20 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
2ef30 62 6c 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 ble((*ppPage)->p
2ef40 44 62 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 DbPage) );. ret
2ef50 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
2ef60 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
2ef70 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 s used to add pa
2ef80 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 ge iPage to the
2ef90 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 database file fr
2efa0 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 ee-list. .** It
2efb0 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
2efc0 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
2efd0 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f already a part o
2efe0 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e f the free-list.
2eff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
2f000 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
2f010 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
2f020 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
2f030 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 is optional..**
2f040 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 If the caller ha
2f050 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 ppens to have a
2f060 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d pointer to the M
2f070 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a emPage object .*
2f080 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
2f090 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61 to page iPage ha
2f0a0 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 ndy, it may pass
2f0b0 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e it as the secon
2f0c0 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 d value. .** Oth
2f0d0 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 erwise, it may p
2f0e0 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 ass NULL..**.**
2f0f0 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 If a pointer to
2f100 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 a MemPage object
2f110 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
2f120 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
2f130 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 t,.** its refere
2f140 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 nce count is not
2f150 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 altered by this
2f160 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
2f170 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 atic int freePag
2f180 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 e2(BtShared *pBt
2f190 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 , MemPage *pMemP
2f1a0 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 age, Pgno iPage)
2f1b0 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 {. MemPage *pTr
2f1c0 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 unk = 0;
2f1d0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d /* Free-
2f1e0 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 list trunk page
2f1f0 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b */. Pgno iTrunk
2f200 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
2f210 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
2f220 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d number of free-
2f230 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 list trunk page
2f240 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 */ . MemPage *p
2f250 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 Page1 = pBt->pPa
2f260 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 ge1; /* Loc
2f270 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 al reference to
2f280 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 page 1 */. MemP
2f290 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 age *pPage;
2f2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f2b0 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 /* Page being fr
2f2c0 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c eed. May be NULL
2f2d0 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 . */. int rc;
2f2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f2f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
2f300 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 turn Code */. i
2f310 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 nt nFree;
2f320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f330 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 /* Initial nu
2f340 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e mber of pages on
2f350 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 free-list */..
2f360 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
2f370 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
2f380 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
2f390 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a ert( iPage>1 );.
2f3a0 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 assert( !pMemP
2f3b0 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d age || pMemPage-
2f3c0 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a >pgno==iPage );.
2f3d0 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 . if( pMemPage
2f3e0 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 ){. pPage = p
2f3f0 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c MemPage;. sql
2f400 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61 ite3PagerRef(pPa
2f410 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
2f420 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 }else{. pPage
2f430 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b = btreePageLook
2f440 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a up(pBt, iPage);.
2f450 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d }.. /* Increm
2f460 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67 ent the free pag
2f470 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 e count on pPage
2f480 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 1 */. rc = sqli
2f490 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
2f4a0 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a age1->pDbPage);.
2f4b0 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
2f4c0 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
2f4d0 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 nFree = get4byte
2f4e0 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
2f4f0 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 36]);. put4byte
2f500 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
2f510 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 36], nFree+1);..
2f520 20 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c if( pBt->btsFl
2f530 61 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 ags & BTS_SECURE
2f540 5f 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 2f _DELETE ){. /
2f550 2a 20 49 66 20 74 68 65 20 73 65 63 75 72 65 5f * If the secure_
2f560 64 65 6c 65 74 65 20 6f 70 74 69 6f 6e 20 69 73 delete option is
2f570 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 0a 20 enabled, then.
2f580 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c ** always ful
2f590 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c ly overwrite del
2f5a0 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e eted information
2f5b0 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 with zeros..
2f5c0 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 70 50 */. if( (!pP
2f5d0 61 67 65 20 26 26 20 28 28 72 63 20 3d 20 62 74 age && ((rc = bt
2f5e0 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
2f5f0 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 iPage, &pPage, 0
2f600 29 29 21 3d 30 29 20 29 0a 20 20 20 20 20 7c 7c ))!=0) ). ||
2f610 20 20 20 20 20 20 20 20 20 20 20 20 28 28 72 63 ((rc
2f620 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
2f630 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 rite(pPage->pDbP
2f640 61 67 65 29 29 21 3d 30 29 0a 20 20 20 20 29 7b age))!=0). ){
2f650 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 . goto free
2f660 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a page_out;. }.
2f670 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 memset(pPage
2f680 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->aData, 0, pPag
2f690 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e->pBt->pageSize
2f6a0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
2f6b0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 the database sup
2f6c0 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 ports auto-vacuu
2f6d0 6d 2c 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72 m, write an entr
2f6e0 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 y in the pointer
2f6f0 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 -map. ** to ind
2f700 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 70 icate that the p
2f710 61 67 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a age is free.. *
2f720 2f 0a 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 /. if( ISAUTOVA
2f730 43 55 55 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d CUUM ){. ptrm
2f740 61 70 50 75 74 28 70 42 74 2c 20 69 50 61 67 65 apPut(pBt, iPage
2f750 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 , PTRMAP_FREEPAG
2f760 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 E, 0, &rc);.
2f770 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72 if( rc ) goto fr
2f780 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a eepage_out;. }.
2f790 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 . /* Now manipu
2f7a0 6c 61 74 65 20 74 68 65 20 61 63 74 75 61 6c 20 late the actual
2f7b0 64 61 74 61 62 61 73 65 20 66 72 65 65 2d 6c 69 database free-li
2f7c0 73 74 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 st structure. Th
2f7d0 65 72 65 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a ere are two. **
2f7e0 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 2e 20 possibilities.
2f7f0 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 If the free-list
2f800 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65 6d is currently em
2f810 70 74 79 2c 20 6f 72 20 69 66 20 74 68 65 20 66 pty, or if the f
2f820 69 72 73 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 irst. ** trunk
2f830 70 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 page in the free
2f840 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 -list is full, t
2f850 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 77 69 hen this page wi
2f860 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a ll become a. **
2f870 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 74 new free-list t
2f880 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 runk page. Other
2f890 77 69 73 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 wise, it will be
2f8a0 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f 66 20 74 come a leaf of t
2f8b0 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 he. ** first tr
2f8c0 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 unk page in the
2f8d0 63 75 72 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 current free-lis
2f8e0 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 t. This block te
2f8f0 73 74 73 20 69 66 20 69 74 0a 20 20 2a 2a 20 69 sts if it. ** i
2f900 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 s possible to ad
2f910 64 20 74 68 65 20 70 61 67 65 20 61 73 20 61 20 d the page as a
2f920 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 new free-list le
2f930 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e af.. */. if( n
2f940 46 72 65 65 21 3d 30 20 29 7b 0a 20 20 20 20 75 Free!=0 ){. u
2f950 33 32 20 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 32 nLeaf;
2f960 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 /* Init
2f970 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 ial number of le
2f980 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e af cells on trun
2f990 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69 k page */.. i
2f9a0 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 Trunk = get4byte
2f9b0 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
2f9c0 33 32 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 32]);. rc = b
2f9d0 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
2f9e0 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b iTrunk, &pTrunk
2f9f0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
2fa00 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
2fa10 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 goto freepa
2fa20 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 ge_out;. }..
2fa30 20 20 20 6e 4c 65 61 66 20 3d 20 67 65 74 34 62 nLeaf = get4b
2fa40 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 yte(&pTrunk->aDa
2fa50 74 61 5b 34 5d 29 3b 0a 20 20 20 20 61 73 73 65 ta[4]);. asse
2fa60 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 rt( pBt->usableS
2fa70 69 7a 65 3e 33 32 20 29 3b 0a 20 20 20 20 69 66 ize>32 );. if
2fa80 28 20 6e 4c 65 61 66 20 3e 20 28 75 33 32 29 70 ( nLeaf > (u32)p
2fa90 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 Bt->usableSize/4
2faa0 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 72 63 - 2 ){. rc
2fab0 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
2fac0 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f T_BKPT;. go
2fad0 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
2fae0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
2faf0 4c 65 61 66 20 3c 20 28 75 33 32 29 70 42 74 2d Leaf < (u32)pBt-
2fb00 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 >usableSize/4 -
2fb10 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 8 ){. /* In
2fb20 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
2fb30 20 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 is room on the
2fb40 74 72 75 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e trunk page to in
2fb50 73 65 72 74 20 74 68 65 20 70 61 67 65 0a 20 20 sert the page.
2fb60 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 ** being fre
2fb70 65 64 20 61 73 20 61 20 6e 65 77 20 6c 65 61 66 ed as a new leaf
2fb80 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
2fb90 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 ** Note that th
2fba0 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 e trunk page is
2fbb0 6e 6f 74 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20 not really full
2fbc0 75 6e 74 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e until it contain
2fbd0 73 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c s. ** usabl
2fbe0 65 53 69 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72 eSize/4 - 2 entr
2fbf0 69 65 73 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53 ies, not usableS
2fc00 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 ize/4 - 8 entrie
2fc10 73 20 61 73 20 77 65 20 68 61 76 65 0a 20 20 20 s as we have.
2fc20 20 20 20 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75 ** coded. Bu
2fc30 74 20 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e t due to a codin
2fc40 67 20 65 72 72 6f 72 20 69 6e 20 76 65 72 73 69 g error in versi
2fc50 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 72 ons of SQLite pr
2fc60 69 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ior to. **
2fc70 33 2e 36 2e 30 2c 20 64 61 74 61 62 61 73 65 73 3.6.0, databases
2fc80 20 77 69 74 68 20 66 72 65 65 6c 69 73 74 20 74 with freelist t
2fc90 72 75 6e 6b 20 70 61 67 65 73 20 68 6f 6c 64 69 runk pages holdi
2fca0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 ng more than.
2fcb0 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 ** usableSize
2fcc0 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 77 /4 - 8 entries w
2fcd0 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 ill be reported
2fce0 61 73 20 63 6f 72 72 75 70 74 2e 20 20 49 6e 20 as corrupt. In
2fcf0 6f 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 order. ** t
2fd00 6f 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 o maintain backw
2fd10 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
2fd20 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 76 65 ty with older ve
2fd30 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
2fd40 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 77 69 ,. ** we wi
2fd50 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 ll continue to r
2fd60 65 73 74 72 69 63 74 20 74 68 65 20 6e 75 6d 62 estrict the numb
2fd70 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 74 6f er of entries to
2fd80 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
2fd90 38 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e 8. ** for n
2fda0 6f 77 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 ow. At some poi
2fdb0 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 nt in the future
2fdc0 20 28 6f 6e 63 65 20 65 76 65 72 79 6f 6e 65 20 (once everyone
2fdd0 68 61 73 20 75 70 67 72 61 64 65 64 0a 20 20 20 has upgraded.
2fde0 20 20 20 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f ** to 3.6.0 o
2fdf0 72 20 6c 61 74 65 72 29 20 77 65 20 73 68 6f 75 r later) we shou
2fe00 6c 64 20 63 6f 6e 73 69 64 65 72 20 66 69 78 69 ld consider fixi
2fe10 6e 67 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e ng the condition
2fe20 61 6c 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a al above. *
2fe30 2a 20 74 6f 20 72 65 61 64 20 22 75 73 61 62 6c * to read "usabl
2fe40 65 53 69 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 eSize/4-2" inste
2fe50 61 64 20 6f 66 20 22 75 73 61 62 6c 65 53 69 7a ad of "usableSiz
2fe60 65 2f 34 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2a e/4-8".. **
2fe70 0a 20 20 20 20 20 20 2a 2a 20 45 56 49 44 45 4e . ** EVIDEN
2fe80 43 45 2d 4f 46 3a 20 52 2d 31 39 39 32 30 2d 31 CE-OF: R-19920-1
2fe90 31 35 37 36 20 48 6f 77 65 76 65 72 2c 20 6e 65 1576 However, ne
2fea0 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 wer versions of
2feb0 53 51 4c 69 74 65 20 73 74 69 6c 6c 0a 20 20 20 SQLite still.
2fec0 20 20 20 2a 2a 20 61 76 6f 69 64 20 75 73 69 6e ** avoid usin
2fed0 67 20 74 68 65 20 6c 61 73 74 20 73 69 78 20 65 g the last six e
2fee0 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 66 72 ntries in the fr
2fef0 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 eelist trunk pag
2ff00 65 20 61 72 72 61 79 20 69 6e 0a 20 20 20 20 20 e array in.
2ff10 20 2a 2a 20 6f 72 64 65 72 20 74 68 61 74 20 64 ** order that d
2ff20 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 63 72 atabase files cr
2ff30 65 61 74 65 64 20 62 79 20 6e 65 77 65 72 20 76 eated by newer v
2ff40 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
2ff50 65 20 63 61 6e 20 62 65 0a 20 20 20 20 20 20 2a e can be. *
2ff60 2a 20 72 65 61 64 20 62 79 20 6f 6c 64 65 72 20 * read by older
2ff70 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
2ff80 74 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 te.. */.
2ff90 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
2ffa0 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b agerWrite(pTrunk
2ffb0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
2ffc0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
2ffd0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 _OK ){. p
2ffe0 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d ut4byte(&pTrunk-
2fff0 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 >aData[4], nLeaf
30000 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 +1);. put
30010 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 4byte(&pTrunk->a
30020 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c Data[8+nLeaf*4],
30030 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 iPage);.
30040 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 if( pPage && (p
30050 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
30060 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TS_SECURE_DELETE
30070 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
30080 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f sqlite3PagerDo
30090 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 ntWrite(pPage->p
300a0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
300b0 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
300c0 62 74 72 65 65 53 65 74 48 61 73 43 6f 6e 74 65 btreeSetHasConte
300d0 6e 74 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a nt(pBt, iPage);.
300e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 }. TR
300f0 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a ACE(("FREE-PAGE:
30100 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e %d leaf on trun
30110 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 k page %d\n",pPa
30120 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d ge->pgno,pTrunk-
30130 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 67 >pgno));. g
30140 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 oto freepage_out
30150 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
30160 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f * If control flo
30170 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 ws to this point
30180 2c 20 74 68 65 6e 20 69 74 20 77 61 73 20 6e 6f , then it was no
30190 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 t possible to ad
301a0 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 70 d the. ** the p
301b0 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 age being freed
301c0 61 73 20 61 20 6c 65 61 66 20 70 61 67 65 20 6f as a leaf page o
301d0 66 20 74 68 65 20 66 69 72 73 74 20 74 72 75 6e f the first trun
301e0 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 k in the free-li
301f0 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 69 62 6c st.. ** Possibl
30200 79 20 62 65 63 61 75 73 65 20 74 68 65 20 66 72 y because the fr
30210 65 65 2d 6c 69 73 74 20 69 73 20 65 6d 70 74 79 ee-list is empty
30220 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 , or possibly be
30230 63 61 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 cause the . **
30240 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 first trunk in t
30250 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 he free-list is
30260 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 77 61 79 full. Either way
30270 2c 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 , the page being
30280 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 69 6c 6c freed. ** will
30290 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 become the new
302a0 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 first trunk page
302b0 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
302c0 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 t.. */. if( pP
302d0 61 67 65 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 age==0 && SQLITE
302e0 5f 4f 4b 21 3d 28 72 63 20 3d 20 62 74 72 65 65 _OK!=(rc = btree
302f0 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 GetPage(pBt, iPa
30300 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 20 ge, &pPage, 0))
30310 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 ){. goto free
30320 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 page_out;. }.
30330 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
30340 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 rWrite(pPage->pD
30350 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
30360 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
30370 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 goto freepage
30380 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 75 74 34 _out;. }. put4
30390 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
303a0 61 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 20 70 75 a, iTrunk);. pu
303b0 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
303c0 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 20 70 Data[4], 0);. p
303d0 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d ut4byte(&pPage1-
303e0 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 50 61 67 >aData[32], iPag
303f0 65 29 3b 0a 20 20 54 52 41 43 45 28 28 22 46 52 e);. TRACE(("FR
30400 45 45 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 EE-PAGE: %d new
30410 74 72 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 trunk page repla
30420 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70 50 61 67 cing %d\n", pPag
30430 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 6e 6b 29 e->pgno, iTrunk)
30440 29 3b 0a 0a 66 72 65 65 70 61 67 65 5f 6f 75 74 );..freepage_out
30450 3a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b :. if( pPage ){
30460 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
30470 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 it = 0;. }. re
30480 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
30490 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 ;. releasePage(
304a0 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 74 75 72 pTrunk);. retur
304b0 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 76 n rc;.}.static v
304c0 6f 69 64 20 66 72 65 65 50 61 67 65 28 4d 65 6d oid freePage(Mem
304d0 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
304e0 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a *pRC){. if( (*
304f0 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 pRC)==SQLITE_OK
30500 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 66 72 ){. *pRC = fr
30510 65 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 eePage2(pPage->p
30520 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 Bt, pPage, pPage
30530 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a ->pgno);. }.}..
30540 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f /*.** Free any o
30550 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 verflow pages as
30560 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
30570 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e 20 20 57 e given Cell. W
30580 72 69 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 61 rite the.** loca
30590 6c 20 43 65 6c 6c 20 73 69 7a 65 20 28 74 68 65 l Cell size (the
305a0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
305b0 20 6f 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c on the original
305c0 20 70 61 67 65 2c 20 6f 6d 69 74 74 69 6e 67 0a page, omitting.
305d0 2a 2a 20 6f 76 65 72 66 6c 6f 77 29 20 69 6e 74 ** overflow) int
305e0 6f 20 2a 70 6e 53 69 7a 65 2e 0a 2a 2f 0a 73 74 o *pnSize..*/.st
305f0 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 atic int clearCe
30600 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ll(. MemPage *p
30610 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f Page, /
30620 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 * The page that
30630 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 65 6c contains the Cel
30640 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 l */. unsigned
30650 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 char *pCell,
30660 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
30670 20 74 68 65 20 43 65 6c 6c 20 2a 2f 0a 20 20 75 the Cell */. u
30680 31 36 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 16 *pnSize
30690 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
306a0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
306b0 20 43 65 6c 6c 20 68 65 72 65 20 2a 2f 0a 29 7b Cell here */.){
306c0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
306d0 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 = pPage->pBt;.
306e0 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
306f0 20 20 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b Pgno ovflPgno;
30700 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
30710 20 6e 4f 76 66 6c 3b 0a 20 20 75 33 32 20 6f 76 nOvfl;. u32 ov
30720 66 6c 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 flPageSize;.. a
30730 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
30740 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
30750 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
30760 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
30770 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
30780 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 2a 70 6e 53 , &info);. *pnS
30790 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 ize = info.nSize
307a0 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 ;. if( info.iOv
307b0 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 erflow==0 ){.
307c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
307d0 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c K; /* No overfl
307e0 6f 77 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e ow pages. Return
307f0 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 without doing a
30800 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 nything */. }.
30810 20 69 66 28 20 70 43 65 6c 6c 2b 69 6e 66 6f 2e if( pCell+info.
30820 69 4f 76 65 72 66 6c 6f 77 2b 33 20 3e 20 70 50 iOverflow+3 > pP
30830 61 67 65 2d 3e 61 44 61 74 61 2b 70 50 61 67 65 age->aData+pPage
30840 2d 3e 6d 61 73 6b 50 61 67 65 20 29 7b 0a 20 20 ->maskPage ){.
30850 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
30860 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 20 2f CORRUPT_BKPT; /
30870 2a 20 43 65 6c 6c 20 65 78 74 65 6e 64 73 20 70 * Cell extends p
30880 61 73 74 20 65 6e 64 20 6f 66 20 70 61 67 65 20 ast end of page
30890 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e */. }. ovflPgn
308a0 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 o = get4byte(&pC
308b0 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c ell[info.iOverfl
308c0 6f 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ow]);. assert(
308d0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
308e0 3e 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 > 4 );. ovflPag
308f0 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 eSize = pBt->usa
30900 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e bleSize - 4;. n
30910 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 Ovfl = (info.nPa
30920 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f yload - info.nLo
30930 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 cal + ovflPageSi
30940 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 ze - 1)/ovflPage
30950 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Size;. assert(
30960 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e ovflPgno==0 || n
30970 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c Ovfl>0 );. whil
30980 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 e( nOvfl-- ){.
30990 20 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 Pgno iNext = 0
309a0 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 ;. MemPage *p
309b0 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 Ovfl = 0;. if
309c0 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 ( ovflPgno<2 ||
309d0 6f 76 66 6c 50 67 6e 6f 3e 62 74 72 65 65 50 61 ovflPgno>btreePa
309e0 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
309f0 20 20 20 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f /* 0 is no
30a00 74 20 61 20 6c 65 67 61 6c 20 70 61 67 65 20 6e t a legal page n
30a10 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 31 umber and page 1
30a20 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 20 cannot be an .
30a30 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 ** overflow
30a40 20 70 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 page. Therefore
30a50 20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f if ovflPgno<2 o
30a60 72 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f r past the end o
30a70 66 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 f the . **
30a80 66 69 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 file the databas
30a90 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 e must be corrup
30aa0 74 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 t. */. retu
30ab0 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
30ac0 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
30ad0 20 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a 20 if( nOvfl ){.
30ae0 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 rc = getOve
30af0 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f rflowPage(pBt, o
30b00 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c vflPgno, &pOvfl,
30b10 20 26 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 &iNext);.
30b20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
30b30 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 rc;. }.. i
30b40 66 28 20 28 20 70 4f 76 66 6c 20 7c 7c 20 28 28 f( ( pOvfl || ((
30b50 70 4f 76 66 6c 20 3d 20 62 74 72 65 65 50 61 67 pOvfl = btreePag
30b60 65 4c 6f 6f 6b 75 70 28 70 42 74 2c 20 6f 76 66 eLookup(pBt, ovf
30b70 6c 50 67 6e 6f 29 29 21 3d 30 29 20 29 0a 20 20 lPgno))!=0) ).
30b80 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 && sqlite3Pag
30b90 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 erPageRefcount(p
30ba0 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 21 3d Ovfl->pDbPage)!=
30bb0 31 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 1. ){. /
30bc0 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 72 65 * There is no re
30bd0 61 73 6f 6e 20 61 6e 79 20 63 75 72 73 6f 72 20 ason any cursor
30be0 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6e 20 6f should have an o
30bf0 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
30c00 65 6e 63 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 ence . ** t
30c10 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 o an overflow pa
30c20 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 ge belonging to
30c30 61 20 63 65 6c 6c 20 74 68 61 74 20 69 73 20 62 a cell that is b
30c40 65 69 6e 67 20 64 65 6c 65 74 65 64 2f 75 70 64 eing deleted/upd
30c50 61 74 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 53 ated.. ** S
30c60 6f 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74 o if there exist
30c70 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 s more than one
30c80 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 69 reference to thi
30c90 73 20 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 s page, then it
30ca0 0a 20 20 20 20 20 20 2a 2a 20 6d 75 73 74 20 6e . ** must n
30cb0 6f 74 20 72 65 61 6c 6c 79 20 62 65 20 61 6e 20 ot really be an
30cc0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 61 6e overflow page an
30cd0 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d d the database m
30ce0 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 2e 20 ust be corrupt.
30cf0 0a 20 20 20 20 20 20 2a 2a 20 49 74 20 69 73 20 . ** It is
30d00 68 65 6c 70 66 75 6c 20 74 6f 20 64 65 74 65 63 helpful to detec
30d10 74 20 74 68 69 73 20 62 65 66 6f 72 65 20 63 61 t this before ca
30d20 6c 6c 69 6e 67 20 66 72 65 65 50 61 67 65 32 28 lling freePage2(
30d30 29 2c 20 61 73 20 0a 20 20 20 20 20 20 2a 2a 20 ), as . **
30d40 66 72 65 65 50 61 67 65 32 28 29 20 6d 61 79 20 freePage2() may
30d50 7a 65 72 6f 20 74 68 65 20 70 61 67 65 20 63 6f zero the page co
30d60 6e 74 65 6e 74 73 20 69 66 20 73 65 63 75 72 65 ntents if secure
30d70 2d 64 65 6c 65 74 65 20 6d 6f 64 65 20 69 73 0a -delete mode is.
30d80 20 20 20 20 20 20 2a 2a 20 65 6e 61 62 6c 65 64 ** enabled
30d90 2e 20 49 66 20 74 68 69 73 20 27 6f 76 65 72 66 . If this 'overf
30da0 6c 6f 77 27 20 70 61 67 65 20 68 61 70 70 65 6e low' page happen
30db0 73 20 74 6f 20 62 65 20 61 20 70 61 67 65 20 74 s to be a page t
30dc0 68 61 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a hat the. **
30dd0 20 63 61 6c 6c 65 72 20 69 73 20 69 74 65 72 61 caller is itera
30de0 74 69 6e 67 20 74 68 72 6f 75 67 68 20 6f 72 20 ting through or
30df0 75 73 69 6e 67 20 69 6e 20 73 6f 6d 65 20 6f 74 using in some ot
30e00 68 65 72 20 77 61 79 2c 20 74 68 69 73 0a 20 20 her way, this.
30e10 20 20 20 20 2a 2a 20 63 61 6e 20 62 65 20 70 72 ** can be pr
30e20 6f 62 6c 65 6d 61 74 69 63 2e 0a 20 20 20 20 20 oblematic..
30e30 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 */. rc = S
30e40 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
30e50 50 54 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 PT;. }else{.
30e60 20 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 rc = freePa
30e70 67 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 ge2(pBt, pOvfl,
30e80 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 7d ovflPgno);. }
30e90 0a 0a 20 20 20 20 69 66 28 20 70 4f 76 66 6c 20 .. if( pOvfl
30ea0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
30eb0 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c PagerUnref(pOvfl
30ec0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
30ed0 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 }. if( rc ) r
30ee0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6f 76 eturn rc;. ov
30ef0 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a flPgno = iNext;.
30f00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
30f10 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
30f20 20 43 72 65 61 74 65 20 74 68 65 20 62 79 74 65 Create the byte
30f30 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 74 sequence used t
30f40 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 63 65 o represent a ce
30f50 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 ll on page pPage
30f60 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 .** and write th
30f70 61 74 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 at byte sequence
30f80 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 into pCell[].
30f90 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 Overflow pages a
30fa0 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 re.** allocated
30fb0 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 73 and filled in as
30fc0 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 necessary. The
30fd0 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 calling procedu
30fe0 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 re.** is respons
30ff0 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 ible for making
31000 73 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 20 sure sufficient
31010 73 70 61 63 65 20 68 61 73 20 62 65 65 6e 20 61 space has been a
31020 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 llocated.** for
31030 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e pCell[]..**.** N
31040 6f 74 65 20 74 68 61 74 20 70 43 65 6c 6c 20 64 ote that pCell d
31050 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 oes not necessar
31060 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 20 y need to point
31070 74 6f 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 to the pPage->aD
31080 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43 ata.** area. pC
31090 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 ell might point
310a0 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72 to some temporar
310b0 79 20 73 74 6f 72 61 67 65 2e 20 20 54 68 65 20 y storage. The
310c0 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 cell will.** be
310d0 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20 74 constructed in t
310e0 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 61 72 his temporary ar
310f0 65 61 20 74 68 65 6e 20 63 6f 70 69 65 64 20 69 ea then copied i
31100 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 nto pPage->aData
31110 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 .** later..*/.st
31120 61 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 atic int fillInC
31130 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a ell(. MemPage *
31140 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 pPage,
31150 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
31160 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
31170 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e the cell */. un
31180 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
31190 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ll, /*
311a0 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 Complete text of
311b0 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 the cell */. c
311c0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
311d0 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a i64 nKey, /*
311e0 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f The key */. co
311f0 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
31200 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20 int nData, /*
31210 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e The data */. in
31220 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 t nZero,
31230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
31240 45 78 74 72 61 20 7a 65 72 6f 20 62 79 74 65 73 Extra zero bytes
31250 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 44 to append to pD
31260 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e ata */. int *pn
31270 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 Size
31280 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
31290 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 20 cell size here
312a0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79 */.){. int nPay
312b0 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38 load;. const u8
312c0 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53 *pSrc;. int nS
312d0 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 rc, n, rc;. int
312e0 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 spaceLeft;. Me
312f0 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 mPage *pOvfl = 0
31300 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f ;. MemPage *pTo
31310 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 75 Release = 0;. u
31320 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 nsigned char *pP
31330 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 rior;. unsigned
31340 20 63 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b char *pPayload;
31350 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
31360 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 = pPage->pBt;.
31370 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d Pgno pgnoOvfl =
31380 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 65 0;. int nHeade
31390 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 r;.. assert( sq
313a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
313b0 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
313c0 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 ex) );.. /* pPa
313d0 67 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 ge is not necess
313e0 61 72 69 6c 79 20 77 72 69 74 65 61 62 6c 65 20 arily writeable
313f0 73 69 6e 63 65 20 70 43 65 6c 6c 20 6d 69 67 68 since pCell migh
31400 74 20 62 65 20 61 75 78 69 6c 69 61 72 79 0a 20 t be auxiliary.
31410 20 2a 2a 20 62 75 66 66 65 72 20 73 70 61 63 65 ** buffer space
31420 20 74 68 61 74 20 69 73 20 73 65 70 61 72 61 74 that is separat
31430 65 20 66 72 6f 6d 20 74 68 65 20 70 50 61 67 65 e from the pPage
31440 20 62 75 66 66 65 72 20 61 72 65 61 20 2a 2f 0a buffer area */.
31450 20 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c 3c assert( pCell<
31460 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 pPage->aData ||
31470 70 43 65 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e 61 pCell>=&pPage->a
31480 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
31490 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
314a0 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
314b0 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
314c0 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
314d0 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 /* Fill in the
314e0 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 65 header. */. nHe
314f0 61 64 65 72 20 3d 20 70 50 61 67 65 2d 3e 63 68 ader = pPage->ch
31500 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 6e 50 ildPtrSize;. nP
31510 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b ayload = nData +
31520 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50 nZero;. if( pP
31530 61 67 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 age->intKeyLeaf
31540 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b ){. nHeader +
31550 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 70 = putVarint32(&p
31560 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e Cell[nHeader], n
31570 50 61 79 6c 6f 61 64 29 3b 0a 20 20 7d 65 6c 73 Payload);. }els
31580 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e e{. assert( n
31590 44 61 74 61 3d 3d 30 20 29 3b 0a 20 20 20 20 61 Data==0 );. a
315a0 73 73 65 72 74 28 20 6e 5a 65 72 6f 3d 3d 30 20 ssert( nZero==0
315b0 29 3b 0a 20 20 7d 0a 20 20 6e 48 65 61 64 65 72 );. }. nHeader
315c0 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 += putVarint(&p
315d0 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a Cell[nHeader], *
315e0 28 75 36 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 (u64*)&nKey);.
315f0 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 . /* Fill in th
31600 65 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 20 2a e payload size *
31610 2f 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 /. if( pPage->i
31620 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 70 53 72 ntKey ){. pSr
31630 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 6e c = pData;. n
31640 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 Src = nData;.
31650 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 65 nData = 0;. }e
31660 6c 73 65 7b 20 0a 20 20 20 20 69 66 28 20 4e 45 lse{ . if( NE
31670 56 45 52 28 6e 4b 65 79 3e 30 78 37 66 66 66 66 VER(nKey>0x7ffff
31680 66 66 66 20 7c 7c 20 70 4b 65 79 3d 3d 30 29 20 fff || pKey==0)
31690 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
316a0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
316b0 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e KPT;. }. n
316c0 50 61 79 6c 6f 61 64 20 3d 20 28 69 6e 74 29 6e Payload = (int)n
316d0 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 Key;. pSrc =
316e0 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d pKey;. nSrc =
316f0 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a (int)nKey;. }.
31700 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 64 3c 3d if( nPayload<=
31710 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pPage->maxLocal
31720 29 7b 0a 20 20 20 20 6e 20 3d 20 6e 48 65 61 64 ){. n = nHead
31730 65 72 20 2b 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 er + nPayload;.
31740 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d testcase( n==
31750 33 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 3 );. testcas
31760 65 28 20 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 69 e( n==4 );. i
31770 66 28 20 6e 3c 34 20 29 20 6e 20 3d 20 34 3b 0a f( n<4 ) n = 4;.
31780 20 20 20 20 2a 70 6e 53 69 7a 65 20 3d 20 6e 3b *pnSize = n;
31790 0a 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d . spaceLeft =
317a0 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 nPayload;. p
317b0 50 72 69 6f 72 20 3d 20 70 43 65 6c 6c 3b 0a 20 Prior = pCell;.
317c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
317d0 6d 6e 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c mn = pPage->minL
317e0 6f 63 61 6c 3b 0a 20 20 20 20 6e 20 3d 20 6d 6e ocal;. n = mn
317f0 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 2d 20 6d + (nPayload - m
31800 6e 29 20 25 20 28 70 50 61 67 65 2d 3e 70 42 74 n) % (pPage->pBt
31810 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 ->usableSize - 4
31820 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
31830 20 6e 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f n==pPage->maxLo
31840 63 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 cal );. testc
31850 61 73 65 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 6d ase( n==pPage->m
31860 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 axLocal+1 );.
31870 20 69 66 28 20 6e 20 3e 20 70 50 61 67 65 2d 3e if( n > pPage->
31880 6d 61 78 4c 6f 63 61 6c 20 29 20 6e 20 3d 20 6d maxLocal ) n = m
31890 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c 65 66 74 n;. spaceLeft
318a0 20 3d 20 6e 3b 0a 20 20 20 20 2a 70 6e 53 69 7a = n;. *pnSiz
318b0 65 20 3d 20 6e 20 2b 20 6e 48 65 61 64 65 72 20 e = n + nHeader
318c0 2b 20 34 3b 0a 20 20 20 20 70 50 72 69 6f 72 20 + 4;. pPrior
318d0 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 = &pCell[nHeader
318e0 2b 6e 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 79 6c +n];. }. pPayl
318f0 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 oad = &pCell[nHe
31900 61 64 65 72 5d 3b 0a 0a 20 20 2f 2a 20 41 74 20 ader];.. /* At
31910 74 68 69 73 20 70 6f 69 6e 74 20 76 61 72 69 61 this point varia
31920 62 6c 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 bles should be s
31930 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 et as follows:.
31940 20 2a 2a 0a 20 20 2a 2a 20 20 20 6e 50 61 79 6c **. ** nPayl
31950 6f 61 64 20 20 20 20 20 20 20 20 20 20 20 54 6f oad To
31960 74 61 6c 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 tal payload size
31970 20 69 6e 20 62 79 74 65 73 0a 20 20 2a 2a 20 20 in bytes. **
31980 20 70 50 61 79 6c 6f 61 64 20 20 20 20 20 20 20 pPayload
31990 20 20 20 20 42 65 67 69 6e 20 77 72 69 74 69 6e Begin writin
319a0 67 20 70 61 79 6c 6f 61 64 20 68 65 72 65 0a 20 g payload here.
319b0 20 2a 2a 20 20 20 73 70 61 63 65 4c 65 66 74 20 ** spaceLeft
319c0 20 20 20 20 20 20 20 20 20 53 70 61 63 65 20 61 Space a
319d0 76 61 69 6c 61 62 6c 65 20 61 74 20 70 50 61 79 vailable at pPay
319e0 6c 6f 61 64 2e 20 20 49 66 20 6e 50 61 79 6c 6f load. If nPaylo
319f0 61 64 3e 73 70 61 63 65 4c 65 66 74 2c 0a 20 20 ad>spaceLeft,.
31a00 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
31a10 20 20 20 20 20 20 20 20 74 68 61 74 20 6d 65 61 that mea
31a20 6e 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 ns content must
31a30 73 70 69 6c 6c 20 69 6e 74 6f 20 6f 76 65 72 66 spill into overf
31a40 6c 6f 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 low pages.. **
31a50 20 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 *pnSize
31a60 20 20 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 Size of the
31a70 20 6c 6f 63 61 6c 20 63 65 6c 6c 20 28 6e 6f 74 local cell (not
31a80 20 63 6f 75 6e 74 69 6e 67 20 6f 76 65 72 66 6c counting overfl
31a90 6f 77 20 70 61 67 65 73 29 0a 20 20 2a 2a 20 20 ow pages). **
31aa0 20 70 50 72 69 6f 72 20 20 20 20 20 20 20 20 20 pPrior
31ab0 20 20 20 20 57 68 65 72 65 20 74 6f 20 77 72 69 Where to wri
31ac0 74 65 20 74 68 65 20 70 67 6e 6f 20 6f 66 20 74 te the pgno of t
31ad0 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f he first overflo
31ae0 77 20 70 61 67 65 0a 20 20 2a 2a 0a 20 20 2a 2a w page. **. **
31af0 20 55 73 65 20 61 20 63 61 6c 6c 20 74 6f 20 62 Use a call to b
31b00 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
31b10 28 29 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 () to verify tha
31b20 74 20 74 68 65 20 76 61 6c 75 65 73 20 61 62 6f t the values abo
31b30 76 65 0a 20 20 2a 2a 20 77 65 72 65 20 63 6f 6d ve. ** were com
31b40 70 75 74 65 64 20 63 6f 72 72 65 63 74 6c 79 2e puted correctly.
31b50 0a 20 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 . */.#if SQLITE
31b60 5f 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20 43 _DEBUG. {. C
31b70 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
31b80 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
31b90 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
31ba0 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 , &info);. as
31bb0 73 65 72 74 28 20 6e 48 65 61 64 65 72 3d 28 69 sert( nHeader=(i
31bc0 6e 74 29 28 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 nt)(info.pPayloa
31bd0 64 20 2d 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20 d - pCell) );.
31be0 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e assert( info.n
31bf0 4b 65 79 3d 3d 6e 4b 65 79 20 29 3b 0a 20 20 20 Key==nKey );.
31c00 20 61 73 73 65 72 74 28 20 2a 70 6e 53 69 7a 65 assert( *pnSize
31c10 20 3d 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 20 29 == info.nSize )
31c20 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 70 ;. assert( sp
31c30 61 63 65 4c 65 66 74 20 3d 3d 20 69 6e 66 6f 2e aceLeft == info.
31c40 6e 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20 61 73 nLocal );. as
31c50 73 65 72 74 28 20 70 50 72 69 6f 72 20 3d 3d 20 sert( pPrior ==
31c60 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
31c70 72 66 6c 6f 77 5d 20 29 3b 0a 20 20 7d 0a 23 65 rflow] );. }.#e
31c80 6e 64 69 66 0a 0a 20 20 2f 2a 20 57 72 69 74 65 ndif.. /* Write
31c90 20 74 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 74 the payload int
31ca0 6f 20 74 68 65 20 6c 6f 63 61 6c 20 43 65 6c 6c o the local Cell
31cb0 20 61 6e 64 20 61 6e 79 20 65 78 74 72 61 20 69 and any extra i
31cc0 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 nto overflow pag
31cd0 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e es */. while( n
31ce0 50 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 Payload>0 ){.
31cf0 20 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d 3d if( spaceLeft==
31d00 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0 ){.#ifndef SQL
31d10 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
31d20 55 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 UUM. Pgno p
31d30 67 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f gnoPtrmap = pgno
31d40 4f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f Ovfl; /* Overflo
31d50 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d w page pointer-m
31d60 61 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f ap entry page */
31d70 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
31d80 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 autoVacuum ){.
31d90 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 do{.
31da0 20 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a pgnoOvfl++;.
31db0 20 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 } while(
31dc0 20 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 4d . PTRM
31dd0 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 AP_ISPAGE(pBt, p
31de0 67 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f gnoOvfl) || pgno
31df0 4f 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 Ovfl==PENDING_BY
31e00 54 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 TE_PAGE(pBt) .
31e10 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d );. }
31e20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 .#endif. rc
31e30 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
31e40 50 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c Page(pBt, &pOvfl
31e50 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e , &pgnoOvfl, pgn
31e60 6f 4f 76 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 oOvfl, 0);.#ifnd
31e70 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
31e80 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
31e90 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
31ea0 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f se supports auto
31eb0 2d 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 -vacuum, and the
31ec0 20 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 second or subse
31ed0 71 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f quent. ** o
31ee0 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 verflow page is
31ef0 62 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c being allocated,
31f00 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f add an entry to
31f10 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
31f20 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 . ** for th
31f30 61 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 at page now. .
31f40 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
31f50 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 If this is the f
31f60 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 irst overflow pa
31f70 67 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 ge, then write a
31f80 20 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0a partial entry .
31f90 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 ** to the
31fa0 70 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 pointer-map. If
31fb0 77 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 we write nothing
31fc0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 to this pointer
31fd0 2d 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 -map slot,.
31fe0 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 74 ** then the opt
31ff0 69 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 imistic overflow
32000 20 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 6e chain processin
32010 67 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 g in clearCell()
32020 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 . ** may mi
32030 73 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 75 sinterpret the u
32040 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 76 61 6c ninitialized val
32050 75 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 74 ues and delete t
32060 68 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e he. ** wron
32070 67 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 g pages from the
32080 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 database..
32090 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 */. if( pB
320a0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 t->autoVacuum &&
320b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
320c0 7b 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 79 {. u8 eTy
320d0 70 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 pe = (pgnoPtrmap
320e0 3f 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 ?PTRMAP_OVERFLOW
320f0 32 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 2:PTRMAP_OVERFLO
32100 57 31 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 W1);. ptr
32110 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f mapPut(pBt, pgno
32120 4f 76 66 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e Ovfl, eType, pgn
32130 6f 50 74 72 6d 61 70 2c 20 26 72 63 29 3b 0a 20 oPtrmap, &rc);.
32140 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
32150 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 . relea
32160 73 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 sePage(pOvfl);.
32170 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
32180 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 .#endif. if
32190 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
321a0 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
321b0 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 20 elease);.
321c0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
321d0 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 }.. /* If
321e0 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e pToRelease is n
321f0 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 ot zero than pPr
32200 69 6f 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 ior points into
32210 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 the data area.
32220 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c ** of pToRel
32230 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 ease. Make sure
32240 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 pToRelease is s
32250 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 till writeable.
32260 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
32270 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c pToRelease==0 |
32280 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 | sqlite3PagerIs
32290 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 6c writeable(pToRel
322a0 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 29 ease->pDbPage) )
322b0 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 ;.. /* If p
322c0 50 72 69 6f 72 20 69 73 20 70 61 72 74 20 6f 66 Prior is part of
322d0 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20 6f the data area o
322e0 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 f pPage, then ma
322f0 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 20 ke sure pPage.
32300 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 ** is still
32310 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 writeable */.
32320 20 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f assert( pPrio
32330 72 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c r<pPage->aData |
32340 7c 20 70 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 | pPrior>=&pPage
32350 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 ->aData[pBt->pag
32360 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 eSize].
32370 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 || sqlite3Pag
32380 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
32390 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
323a0 0a 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 .. put4byte
323b0 28 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 (pPrior, pgnoOvf
323c0 6c 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 l);. releas
323d0 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 ePage(pToRelease
323e0 29 3b 0a 20 20 20 20 20 20 70 54 6f 52 65 6c 65 );. pToRele
323f0 61 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 ase = pOvfl;.
32400 20 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 pPrior = pOvf
32410 6c 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 l->aData;.
32420 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c put4byte(pPrior,
32430 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 0);. pPayl
32440 6f 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 oad = &pOvfl->aD
32450 61 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 ata[4];. sp
32460 61 63 65 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 aceLeft = pBt->u
32470 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 sableSize - 4;.
32480 20 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 }. n = nPa
32490 79 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28 20 6e yload;. if( n
324a0 3e 73 70 61 63 65 4c 65 66 74 20 29 20 6e 20 3d >spaceLeft ) n =
324b0 20 73 70 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 spaceLeft;..
324c0 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 /* If pToReleas
324d0 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 e is not zero th
324e0 61 6e 20 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e an pPayload poin
324f0 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ts into the data
32500 20 61 72 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 area. ** of
32510 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b pToRelease. Mak
32520 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 e sure pToReleas
32530 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 e is still write
32540 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 able. */. ass
32550 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65 3d ert( pToRelease=
32560 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 =0 || sqlite3Pag
32570 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 54 erIswriteable(pT
32580 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 oRelease->pDbPag
32590 65 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 e) );.. /* If
325a0 20 70 50 61 79 6c 6f 61 64 20 69 73 20 70 61 72 pPayload is par
325b0 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 61 72 t of the data ar
325c0 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74 68 65 ea of pPage, the
325d0 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50 61 67 n make sure pPag
325e0 65 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c e. ** is stil
325f0 6c 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 l writeable */.
32600 20 20 20 61 73 73 65 72 74 28 20 70 50 61 79 6c assert( pPayl
32610 6f 61 64 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 oad<pPage->aData
32620 20 7c 7c 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 || pPayload>=&p
32630 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d Page->aData[pBt-
32640 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 >pageSize].
32650 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
32660 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
32670 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
32680 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 ) );.. if( nS
32690 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 rc>0 ){. if
326a0 28 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e ( n>nSrc ) n = n
326b0 53 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65 72 Src;. asser
326c0 74 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20 20 t( pSrc );.
326d0 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 memcpy(pPayload
326e0 2c 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 , pSrc, n);.
326f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
32700 73 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c set(pPayload, 0,
32710 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e n);. }. n
32720 50 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 Payload -= n;.
32730 20 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b pPayload += n;
32740 0a 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a . pSrc += n;.
32750 20 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 nSrc -= n;.
32760 20 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 spaceLeft -=
32770 6e 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3d n;. if( nSrc=
32780 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 =0 ){. nSrc
32790 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 = nData;.
327a0 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 pSrc = pData;.
327b0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 }. }. releas
327c0 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 ePage(pToRelease
327d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
327e0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
327f0 52 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 Remove the i-th
32800 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e cell from pPage.
32810 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 This routine e
32820 66 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c ffects pPage onl
32830 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 y..** The cell c
32840 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 ontent is not fr
32850 65 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 eed or deallocat
32860 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d ed. It is assum
32870 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 ed that.** the c
32880 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 ell content has
32890 62 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 been copied some
328a0 70 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 place else. Thi
328b0 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a s routine just.*
328c0 2a 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 * removes the re
328d0 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 ference to the c
328e0 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a ell from pPage..
328f0 2a 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 **.** "sz" must
32900 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 be the number of
32910 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 bytes in the ce
32920 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ll..*/.static vo
32930 69 64 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 id dropCell(MemP
32940 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
32950 69 64 78 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 idx, int sz, int
32960 20 2a 70 52 43 29 7b 0a 20 20 75 33 32 20 70 63 *pRC){. u32 pc
32970 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 ; /* Off
32980 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 set to cell cont
32990 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e ent of cell bein
329a0 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 g deleted */. u
329b0 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 8 *data; /
329c0 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a * pPage->aData *
329d0 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 /. u8 *ptr;
329e0 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d /* Used to m
329f0 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 ove bytes around
32a00 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a within data[] *
32a10 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
32a20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 /* The retur
32a30 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
32a40 68 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 hdr; /* B
32a50 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
32a60 68 65 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 header. 0 most
32a70 70 61 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 pages. 100 page
32a80 20 31 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 1 */.. if( *pR
32a90 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 C ) return;.. a
32aa0 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 ssert( idx>=0 &&
32ab0 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c idx<pPage->nCel
32ac0 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 l );. assert( s
32ad0 7a 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 z==cellSize(pPag
32ae0 65 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 e, idx) );. ass
32af0 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
32b00 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
32b10 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
32b20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
32b30 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
32b40 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
32b50 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 );. data = pPag
32b60 65 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 e->aData;. ptr
32b70 3d 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 = &pPage->aCellI
32b80 64 78 5b 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 dx[2*idx];. pc
32b90 3d 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b = get2byte(ptr);
32ba0 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
32bb0 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 74 65 73 hdrOffset;. tes
32bc0 74 63 61 73 65 28 20 70 63 3d 3d 67 65 74 32 62 tcase( pc==get2b
32bd0 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
32be0 29 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 ) );. testcase(
32bf0 20 70 63 2b 73 7a 3d 3d 70 50 61 67 65 2d 3e 70 pc+sz==pPage->p
32c00 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
32c10 3b 0a 20 20 69 66 28 20 70 63 20 3c 20 28 75 33 ;. if( pc < (u3
32c20 32 29 67 65 74 32 62 79 74 65 28 26 64 61 74 61 2)get2byte(&data
32c30 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 [hdr+5]) || pc+s
32c40 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e z > pPage->pBt->
32c50 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
32c60 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f *pRC = SQLITE_
32c70 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
32c80 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
32c90 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 rc = freeSpace(p
32ca0 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 Page, pc, sz);.
32cb0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a if( rc ){. *
32cc0 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 pRC = rc;. re
32cd0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 50 61 67 turn;. }. pPag
32ce0 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 69 66 e->nCell--;. if
32cf0 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d ( pPage->nCell==
32d00 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 0 ){. memset(
32d10 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c &data[hdr+1], 0,
32d20 20 34 29 3b 0a 20 20 20 20 64 61 74 61 5b 68 64 4);. data[hd
32d30 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 20 20 70 75 r+7] = 0;. pu
32d40 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
32d50 2b 35 5d 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d +5], pPage->pBt-
32d60 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 >usableSize);.
32d70 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d pPage->nFree =
32d80 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
32d90 62 6c 65 53 69 7a 65 20 2d 20 70 50 61 67 65 2d bleSize - pPage-
32da0 3e 68 64 72 4f 66 66 73 65 74 0a 20 20 20 20 20 >hdrOffset.
32db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32dc0 20 20 2d 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 - pPage->child
32dd0 50 74 72 53 69 7a 65 20 2d 20 38 3b 0a 20 20 7d PtrSize - 8;. }
32de0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 6d 6f 76 else{. memmov
32df0 65 28 70 74 72 2c 20 70 74 72 2b 32 2c 20 32 2a e(ptr, ptr+2, 2*
32e00 28 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2d 20 (pPage->nCell -
32e10 69 64 78 29 29 3b 0a 20 20 20 20 70 75 74 32 62 idx));. put2b
32e20 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
32e30 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b , pPage->nCell);
32e40 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 . pPage->nFre
32e50 65 20 2b 3d 20 32 3b 0a 20 20 7d 0a 7d 0a 0a 2f e += 2;. }.}../
32e60 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 *.** Insert a ne
32e70 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 w cell on pPage
32e80 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 22 69 at cell index "i
32e90 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e 74 73 ". pCell points
32ea0 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 to the.** conte
32eb0 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a nt of the cell..
32ec0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c **.** If the cel
32ed0 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 66 l content will f
32ee0 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2c 20 it on the page,
32ef0 74 68 65 6e 20 70 75 74 20 69 74 20 74 68 65 72 then put it ther
32f00 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77 69 6c e. If it.** wil
32f10 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65 6e 20 l not fit, then
32f20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
32f30 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
32f40 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a 2a 2a into pTemp if.**
32f50 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20 6e 75 pTemp is not nu
32f60 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73 73 20 ll. Regardless
32f70 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f 63 61 of pTemp, alloca
32f80 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 0a 2a te a new entry.*
32f90 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 70 4f 76 * in pPage->apOv
32fa0 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69 74 fl[] and make it
32fb0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63 65 point to the ce
32fc0 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74 68 ll content (eith
32fd0 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20 6f er.** in pTemp o
32fe0 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 r the original p
32ff0 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20 72 Cell) and also r
33000 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78 2e ecord its index.
33010 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 .** Allocating
33020 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 70 a new entry in p
33030 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 6d Page->aCell[] im
33040 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20 70 plies that .** p
33050 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
33060 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a is incremented..
33070 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
33080 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d nsertCell(. Mem
33090 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f Page *pPage, /
330a0 2a 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 * Page into whic
330b0 68 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 h we are copying
330c0 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 */. int i,
330d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 /* New c
330e0 65 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 ell becomes the
330f0 69 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 i-th cell of the
33100 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 page */. u8 *p
33110 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 Cell, /*
33120 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e Content of the n
33130 65 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 ew cell */. int
33140 20 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f sz, /
33150 2a 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 * Bytes of conte
33160 6e 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 nt in pCell */.
33170 20 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 u8 *pTemp,
33180 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 /* Temp stora
33190 67 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 ge space for pCe
331a0 6c 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f ll, if needed */
331b0 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 . Pgno iChild,
331c0 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a /* If non-z
331d0 65 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 ero, replace fir
331e0 73 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20 st 4 bytes with
331f0 74 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 this value */.
33200 69 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 int *pRC
33210 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 /* Read and wr
33220 69 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ite return code
33230 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a from here */.){.
33240 20 20 69 6e 74 20 69 64 78 20 3d 20 30 3b 20 20 int idx = 0;
33250 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 /* Where to
33260 77 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 write new cell c
33270 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d ontent in data[]
33280 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 */. int j;
33290 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
332a0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
332b0 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f end; /
332c0 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 * First byte pas
332d0 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 t the last cell
332e0 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b pointer in data[
332f0 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 ] */. int ins;
33300 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
33310 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 x in data[] wher
33320 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 e new cell point
33330 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a er is inserted *
33340 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 /. int cellOffs
33350 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 et; /* Address
33360 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 of first cell p
33370 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d ointer in data[]
33380 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 */. u8 *data;
33390 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
333a0 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 ontent of the wh
333b0 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 69 ole page */.. i
333c0 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e f( *pRC ) return
333d0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d ;.. assert( i>=
333e0 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 0 && i<=pPage->n
333f0 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 Cell+pPage->nOve
33400 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 rflow );. asser
33410 74 28 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 t( MX_CELL(pPage
33420 2d 3e 70 42 74 29 3c 3d 31 30 39 32 31 20 29 3b ->pBt)<=10921 );
33430 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
33440 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c ->nCell<=MX_CELL
33450 28 70 50 61 67 65 2d 3e 70 42 74 29 20 7c 7c 20 (pPage->pBt) ||
33460 43 4f 52 52 55 50 54 5f 44 42 20 29 3b 0a 20 20 CORRUPT_DB );.
33470 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
33480 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 Overflow<=ArrayS
33490 69 7a 65 28 70 50 61 67 65 2d 3e 61 70 4f 76 66 ize(pPage->apOvf
334a0 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 l) );. assert(
334b0 41 72 72 61 79 53 69 7a 65 28 70 50 61 67 65 2d ArraySize(pPage-
334c0 3e 61 70 4f 76 66 6c 29 3d 3d 41 72 72 61 79 53 >apOvfl)==ArrayS
334d0 69 7a 65 28 70 50 61 67 65 2d 3e 61 69 4f 76 66 ize(pPage->aiOvf
334e0 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 l) );. assert(
334f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
33500 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
33510 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 utex) );. /* Th
33520 65 20 63 65 6c 6c 20 73 68 6f 75 6c 64 20 6e 6f e cell should no
33530 72 6d 61 6c 6c 79 20 62 65 20 73 69 7a 65 64 20 rmally be sized
33540 63 6f 72 72 65 63 74 6c 79 2e 20 20 48 6f 77 65 correctly. Howe
33550 76 65 72 2c 20 77 68 65 6e 20 6d 6f 76 69 6e 67 ver, when moving
33560 20 61 0a 20 20 2a 2a 20 6d 61 6c 66 6f 72 6d 65 a. ** malforme
33570 64 20 63 65 6c 6c 20 66 72 6f 6d 20 61 20 6c 65 d cell from a le
33580 61 66 20 70 61 67 65 20 74 6f 20 61 6e 20 69 6e af page to an in
33590 74 65 72 69 6f 72 20 70 61 67 65 2c 20 69 66 20 terior page, if
335a0 74 68 65 20 63 65 6c 6c 20 73 69 7a 65 0a 20 20 the cell size.
335b0 2a 2a 20 77 61 6e 74 65 64 20 74 6f 20 62 65 20 ** wanted to be
335c0 6c 65 73 73 20 74 68 61 6e 20 34 20 62 75 74 20 less than 4 but
335d0 67 6f 74 20 72 6f 75 6e 64 65 64 20 75 70 20 74 got rounded up t
335e0 6f 20 34 20 6f 6e 20 74 68 65 20 6c 65 61 66 2c o 4 on the leaf,
335f0 20 74 68 65 6e 20 73 69 7a 65 0a 20 20 2a 2a 20 then size. **
33600 6d 69 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 might be less th
33610 61 6e 20 38 20 28 6c 65 61 66 2d 73 69 7a 65 20 an 8 (leaf-size
33620 2b 20 70 6f 69 6e 74 65 72 29 20 6f 6e 20 74 68 + pointer) on th
33630 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e e interior node.
33640 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 Hence. ** the
33650 20 74 65 72 6d 20 61 66 74 65 72 20 74 68 65 20 term after the
33660 7c 7c 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 || in the follow
33670 69 6e 67 20 61 73 73 65 72 74 28 29 2e 20 2a 2f ing assert(). */
33680 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 . assert( sz==c
33690 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
336a0 2c 20 70 43 65 6c 6c 29 20 7c 7c 20 28 73 7a 3d , pCell) || (sz=
336b0 3d 38 20 26 26 20 69 43 68 69 6c 64 3e 30 29 20 =8 && iChild>0)
336c0 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e );. if( pPage->
336d0 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b nOverflow || sz+
336e0 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 2>pPage->nFree )
336f0 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 {. if( pTemp
33700 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
33710 70 54 65 6d 70 2c 20 70 43 65 6c 6c 2c 20 73 7a pTemp, pCell, sz
33720 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
33730 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 pTemp;. }.
33740 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a if( iChild ){.
33750 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
33760 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 Cell, iChild);.
33770 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 }. j = pPa
33780 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b ge->nOverflow++;
33790 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28 . assert( j<(
337a0 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67 int)(sizeof(pPag
337b0 65 2d 3e 61 70 4f 76 66 6c 29 2f 73 69 7a 65 6f e->apOvfl)/sizeo
337c0 66 28 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b f(pPage->apOvfl[
337d0 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 0])) );. pPag
337e0 65 2d 3e 61 70 4f 76 66 6c 5b 6a 5d 20 3d 20 70 e->apOvfl[j] = p
337f0 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d Cell;. pPage-
33800 3e 61 69 4f 76 66 6c 5b 6a 5d 20 3d 20 28 75 31 >aiOvfl[j] = (u1
33810 36 29 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 6)i;. }else{.
33820 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
33830 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
33840 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
33850 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
33860 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 _OK ){. *pR
33870 43 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65 C = rc;. re
33880 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 turn;. }.
33890 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
338a0 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
338b0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
338c0 29 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 );. data = pP
338d0 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 age->aData;.
338e0 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 cellOffset = pPa
338f0 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a ge->cellOffset;.
33900 20 20 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 end = cellOf
33910 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e fset + 2*pPage->
33920 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 6e 73 20 3d nCell;. ins =
33930 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
33940 69 3b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f i;. rc = allo
33950 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c cateSpace(pPage,
33960 20 73 7a 2c 20 26 69 64 78 29 3b 0a 20 20 20 20 sz, &idx);.
33970 69 66 28 20 72 63 20 29 7b 20 2a 70 52 43 20 3d if( rc ){ *pRC =
33980 20 72 63 3b 20 72 65 74 75 72 6e 3b 20 7d 0a 20 rc; return; }.
33990 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c 6f 63 61 /* The alloca
339a0 74 65 53 70 61 63 65 28 29 20 72 6f 75 74 69 6e teSpace() routin
339b0 65 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 65 e guarantees the
339c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 70 following two p
339d0 72 6f 70 65 72 74 69 65 73 0a 20 20 20 20 2a 2a roperties. **
339e0 20 69 66 20 69 74 20 72 65 74 75 72 6e 73 20 73 if it returns s
339f0 75 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 61 73 uccess */. as
33a00 73 65 72 74 28 20 69 64 78 20 3e 3d 20 65 6e 64 sert( idx >= end
33a10 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +2 );. assert
33a20 28 20 69 64 78 2b 73 7a 20 3c 3d 20 28 69 6e 74 ( idx+sz <= (int
33a30 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 )pPage->pBt->usa
33a40 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 bleSize );. p
33a50 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 Page->nCell++;.
33a60 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 pPage->nFree
33a70 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 -= (u16)(2 + sz)
33a80 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 ;. memcpy(&da
33a90 74 61 5b 69 64 78 5d 2c 20 70 43 65 6c 6c 2c 20 ta[idx], pCell,
33aa0 73 7a 29 3b 0a 20 20 20 20 69 66 28 20 69 43 68 sz);. if( iCh
33ab0 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74 ild ){. put
33ac0 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78 5d 4byte(&data[idx]
33ad0 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 7d , iChild);. }
33ae0 0a 20 20 20 20 6d 65 6d 6d 6f 76 65 28 26 64 61 . memmove(&da
33af0 74 61 5b 69 6e 73 2b 32 5d 2c 20 26 64 61 74 61 ta[ins+2], &data
33b00 5b 69 6e 73 5d 2c 20 65 6e 64 2d 69 6e 73 29 3b [ins], end-ins);
33b10 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 . put2byte(&d
33b20 61 74 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a ata[ins], idx);.
33b30 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
33b40 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
33b50 73 65 74 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e set+3], pPage->n
33b60 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 Cell);.#ifndef S
33b70 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
33b80 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50 ACUUM. if( pP
33b90 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 age->pBt->autoVa
33ba0 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f 2a cuum ){. /*
33bb0 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63 6f The cell may co
33bc0 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 ntain a pointer
33bd0 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 to an overflow p
33be0 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69 74 age. If so, writ
33bf0 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 e. ** the e
33c00 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65 ntry for the ove
33c10 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f 20 rflow page into
33c20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
33c30 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
33c40 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 ptrmapPutOvflPtr
33c50 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70 (pPage, pCell, p
33c60 52 43 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 RC);. }.#endi
33c70 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 f. }.}../*.** A
33c80 72 72 61 79 20 61 70 43 65 6c 6c 5b 5d 20 63 6f rray apCell[] co
33c90 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 ntains pointers
33ca0 74 6f 20 6e 43 65 6c 6c 20 62 2d 74 72 65 65 20 to nCell b-tree
33cb0 70 61 67 65 20 63 65 6c 6c 73 2e 20 54 68 65 20 page cells. The
33cc0 0a 2a 2a 20 73 7a 43 65 6c 6c 5b 5d 20 61 72 72 .** szCell[] arr
33cd0 61 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 ay contains the
33ce0 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 size in bytes of
33cf0 20 65 61 63 68 20 63 65 6c 6c 2e 20 54 68 69 73 each cell. This
33d00 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 70 function.** rep
33d10 6c 61 63 65 73 20 74 68 65 20 63 75 72 72 65 6e laces the curren
33d20 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 t contents of pa
33d30 67 65 20 70 50 67 20 77 69 74 68 20 74 68 65 20 ge pPg with the
33d40 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
33d50 63 65 6c 6c 0a 2a 2a 20 61 72 72 61 79 2e 0a 2a cell.** array..*
33d60 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 *.** Some of the
33d70 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c cells in apCell
33d80 5b 5d 20 6d 61 79 20 63 75 72 72 65 6e 74 6c 79 [] may currently
33d90 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 70 50 be stored in pP
33da0 67 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 g. This.** funct
33db0 69 6f 6e 20 77 6f 72 6b 73 20 61 72 6f 75 6e 64 ion works around
33dc0 20 70 72 6f 62 6c 65 6d 73 20 63 61 75 73 65 64 problems caused
33dd0 20 62 79 20 74 68 69 73 20 62 79 20 6d 61 6b 69 by this by maki
33de0 6e 67 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 79 ng a copy of any
33df0 20 0a 2a 2a 20 73 75 63 68 20 63 65 6c 6c 73 20 .** such cells
33e00 62 65 66 6f 72 65 20 6f 76 65 72 77 72 69 74 69 before overwriti
33e10 6e 67 20 74 68 65 20 70 61 67 65 20 64 61 74 61 ng the page data
33e20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4d 65 6d 50 ..**.** The MemP
33e30 61 67 65 2e 6e 46 72 65 65 20 66 69 65 6c 64 20 age.nFree field
33e40 69 73 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 is invalidated b
33e50 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
33e60 20 49 74 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 It is the .** r
33e70 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 esponsibility of
33e80 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 73 the caller to s
33e90 65 74 20 69 74 20 63 6f 72 72 65 63 74 6c 79 2e et it correctly.
33ea0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
33eb0 72 65 62 75 69 6c 64 50 61 67 65 28 0a 20 20 4d rebuildPage(. M
33ec0 65 6d 50 61 67 65 20 2a 70 50 67 2c 20 20 20 20 emPage *pPg,
33ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
33ee0 2a 20 45 64 69 74 20 74 68 69 73 20 70 61 67 65 * Edit this page
33ef0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c */. int nCell,
33f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33f10 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20 6e /* Final n
33f20 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
33f30 6e 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a n page */. u8 *
33f40 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 *apCell,
33f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
33f60 72 72 61 79 20 6f 66 20 63 65 6c 6c 73 20 2a 2f rray of cells */
33f70 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 20 20 . u16 *szCell
33f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33f90 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 /* Array of c
33fa0 65 6c 6c 20 73 69 7a 65 73 20 2a 2f 0a 29 7b 0a ell sizes */.){.
33fb0 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 const int hdr
33fc0 3d 20 70 50 67 2d 3e 68 64 72 4f 66 66 73 65 74 = pPg->hdrOffset
33fd0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 ; /* Of
33fe0 66 73 65 74 20 6f 66 20 68 65 61 64 65 72 20 6f fset of header o
33ff0 6e 20 70 50 67 20 2a 2f 0a 20 20 75 38 20 2a 20 n pPg */. u8 *
34000 63 6f 6e 73 74 20 61 44 61 74 61 20 3d 20 70 50 const aData = pP
34010 67 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 g->aData;
34020 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
34030 6f 20 64 61 74 61 20 66 6f 72 20 70 50 67 20 2a o data for pPg *
34040 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 75 73 /. const int us
34050 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 67 2d 3e ableSize = pPg->
34060 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
34070 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 45 . u8 * const pE
34080 6e 64 20 3d 20 26 61 44 61 74 61 5b 75 73 61 62 nd = &aData[usab
34090 6c 65 53 69 7a 65 5d 3b 0a 20 20 69 6e 74 20 69 leSize];. int i
340a0 3b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 70 74 72 ;. u8 *pCellptr
340b0 20 3d 20 70 50 67 2d 3e 61 43 65 6c 6c 49 64 78 = pPg->aCellIdx
340c0 3b 0a 20 20 75 38 20 2a 70 54 6d 70 20 3d 20 73 ;. u8 *pTmp = s
340d0 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 qlite3PagerTempS
340e0 70 61 63 65 28 70 50 67 2d 3e 70 42 74 2d 3e 70 pace(pPg->pBt->p
340f0 50 61 67 65 72 29 3b 0a 20 20 75 38 20 2a 70 44 Pager);. u8 *pD
34100 61 74 61 3b 0a 0a 20 20 69 20 3d 20 67 65 74 32 ata;.. i = get2
34110 62 79 74 65 28 26 61 44 61 74 61 5b 68 64 72 2b byte(&aData[hdr+
34120 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 5]);. memcpy(&p
34130 54 6d 70 5b 69 5d 2c 20 26 61 44 61 74 61 5b 69 Tmp[i], &aData[i
34140 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 ], usableSize -
34150 69 29 3b 0a 0a 20 20 70 44 61 74 61 20 3d 20 70 i);.. pData = p
34160 45 6e 64 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 End;. for(i=0;
34170 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
34180 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 61 u8 *pCell = a
34190 70 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 69 66 pCell[i];. if
341a0 28 20 70 43 65 6c 6c 3e 61 44 61 74 61 20 26 26 ( pCell>aData &&
341b0 20 70 43 65 6c 6c 3c 70 45 6e 64 20 29 7b 0a 20 pCell<pEnd ){.
341c0 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 26 70 54 pCell = &pT
341d0 6d 70 5b 70 43 65 6c 6c 20 2d 20 61 44 61 74 61 mp[pCell - aData
341e0 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 61 ];. }. pDa
341f0 74 61 20 2d 3d 20 73 7a 43 65 6c 6c 5b 69 5d 3b ta -= szCell[i];
34200 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44 61 74 . memcpy(pDat
34210 61 2c 20 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c a, pCell, szCell
34220 5b 69 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 [i]);. put2by
34230 74 65 28 70 43 65 6c 6c 70 74 72 2c 20 28 70 44 te(pCellptr, (pD
34240 61 74 61 20 2d 20 61 44 61 74 61 29 29 3b 0a 20 ata - aData));.
34250 20 20 20 70 43 65 6c 6c 70 74 72 20 2b 3d 20 32 pCellptr += 2
34260 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a ;. assert( sz
34270 43 65 6c 6c 5b 69 5d 3d 3d 63 65 6c 6c 53 69 7a Cell[i]==cellSiz
34280 65 50 74 72 28 70 50 67 2c 20 70 43 65 6c 6c 29 ePtr(pPg, pCell)
34290 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 );. }.. /* Th
342a0 65 20 70 50 67 2d 3e 6e 46 72 65 65 20 66 69 65 e pPg->nFree fie
342b0 6c 64 20 69 73 20 6e 6f 77 20 73 65 74 20 69 6e ld is now set in
342c0 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 65 20 63 correctly. The c
342d0 61 6c 6c 65 72 20 77 69 6c 6c 20 66 69 78 20 69 aller will fix i
342e0 74 2e 20 2a 2f 0a 20 20 70 50 67 2d 3e 6e 43 65 t. */. pPg->nCe
342f0 6c 6c 20 3d 20 6e 43 65 6c 6c 3b 0a 20 20 70 50 ll = nCell;. pP
34300 67 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 g->nOverflow = 0
34310 3b 0a 0a 20 20 70 75 74 32 62 79 74 65 28 26 61 ;.. put2byte(&a
34320 44 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 29 3b Data[hdr+1], 0);
34330 0a 20 20 70 75 74 32 62 79 74 65 28 26 61 44 61 . put2byte(&aDa
34340 74 61 5b 68 64 72 2b 33 5d 2c 20 70 50 67 2d 3e ta[hdr+3], pPg->
34350 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 nCell);. put2by
34360 74 65 28 26 61 44 61 74 61 5b 68 64 72 2b 35 5d te(&aData[hdr+5]
34370 2c 20 70 44 61 74 61 20 2d 20 61 44 61 74 61 29 , pData - aData)
34380 3b 0a 20 20 61 44 61 74 61 5b 68 64 72 2b 37 5d ;. aData[hdr+7]
34390 20 3d 20 30 78 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a = 0x00;.}../*.*
343a0 2a 20 41 72 72 61 79 20 61 70 43 65 6c 6c 5b 5d * Array apCell[]
343b0 20 63 6f 6e 74 61 69 6e 73 20 6e 43 65 6c 6c 20 contains nCell
343c0 70 6f 69 6e 74 65 72 73 20 74 6f 20 62 2d 74 72 pointers to b-tr
343d0 65 65 20 63 65 6c 6c 73 2e 20 41 72 72 61 79 20 ee cells. Array
343e0 73 7a 43 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 61 69 szCell.** contai
343f0 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 ns the size in b
34400 79 74 65 73 20 6f 66 20 65 61 63 68 20 73 75 63 ytes of each suc
34410 68 20 63 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e h cell. This fun
34420 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 ction attempts t
34430 6f 20 0a 2a 2a 20 61 64 64 20 74 68 65 20 63 65 o .** add the ce
34440 6c 6c 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 lls stored in th
34450 65 20 61 72 72 61 79 20 74 6f 20 70 61 67 65 20 e array to page
34460 70 50 67 2e 20 49 66 20 69 74 20 63 61 6e 6e 6f pPg. If it canno
34470 74 20 28 62 65 63 61 75 73 65 20 0a 2a 2a 20 74 t (because .** t
34480 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f he page needs to
34490 20 62 65 20 64 65 66 72 61 67 6d 65 6e 74 65 64 be defragmented
344a0 20 62 65 66 6f 72 65 20 74 68 65 20 63 65 6c 6c before the cell
344b0 73 20 77 69 6c 6c 20 66 69 74 29 2c 20 6e 6f 6e s will fit), non
344c0 2d 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 75 -zero.** is retu
344d0 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rned. Otherwise,
344e0 20 69 66 20 74 68 65 20 63 65 6c 6c 73 20 61 72 if the cells ar
344f0 65 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66 e added successf
34500 75 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 0a 2a 2a ully, zero is.**
34510 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
34520 20 41 72 67 75 6d 65 6e 74 20 70 43 65 6c 6c 70 Argument pCellp
34530 74 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 tr points to the
34540 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 first entry in
34550 74 68 65 20 63 65 6c 6c 2d 70 6f 69 6e 74 65 72 the cell-pointer
34560 20 61 72 72 61 79 0a 2a 2a 20 28 70 61 72 74 20 array.** (part
34570 6f 66 20 70 61 67 65 20 70 50 67 29 20 74 6f 20 of page pPg) to
34580 70 6f 70 75 6c 61 74 65 2e 20 41 66 74 65 72 20 populate. After
34590 63 65 6c 6c 20 61 70 43 65 6c 6c 5b 30 5d 20 69 cell apCell[0] i
345a0 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 s written to the
345b0 0a 2a 2a 20 70 61 67 65 20 62 6f 64 79 2c 20 61 .** page body, a
345c0 20 31 36 2d 62 69 74 20 6f 66 66 73 65 74 20 69 16-bit offset i
345d0 73 20 77 72 69 74 74 65 6e 20 74 6f 20 70 43 65 s written to pCe
345e0 6c 6c 70 74 72 2e 20 41 6e 64 20 73 6f 20 6f 6e llptr. And so on
345f0 2c 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63 65 , for each.** ce
34600 6c 6c 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e ll in the array.
34610 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f It is the respo
34620 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 nsibility of the
34630 20 63 61 6c 6c 65 72 20 74 6f 20 65 6e 73 75 72 caller to ensur
34640 65 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 e.** that it is
34650 73 61 66 65 20 74 6f 20 6f 76 65 72 77 72 69 74 safe to overwrit
34660 65 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 e this part of t
34670 68 65 20 63 65 6c 6c 2d 70 6f 69 6e 74 65 72 20 he cell-pointer
34680 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 array..**.** Whe
34690 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
346a0 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 70 44 61 is called, *ppDa
346b0 74 61 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 ta points to the
346c0 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 0a 2a start of the .*
346d0 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 6f * content area o
346e0 6e 20 70 61 67 65 20 70 50 67 2e 20 49 66 20 74 n page pPg. If t
346f0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 63 he size of the c
34700 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 65 ontent area is e
34710 78 74 65 6e 64 65 64 2c 0a 2a 2a 20 2a 70 70 44 xtended,.** *ppD
34720 61 74 61 20 69 73 20 75 70 64 61 74 65 64 20 74 ata is updated t
34730 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e o point to the n
34740 65 77 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 ew start of the
34750 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 content area.**
34760 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
34770 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 61 6c 6c 79 2c ..**.** Finally,
34780 20 61 72 67 75 6d 65 6e 74 20 70 42 65 67 69 6e argument pBegin
34790 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 62 points to the b
347a0 79 74 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 yte immediately
347b0 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 0a 2a 2a following the.**
347c0 20 65 6e 64 20 6f 66 20 74 68 65 20 73 70 61 63 end of the spac
347d0 65 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 e required by th
347e0 69 73 20 70 61 67 65 20 66 6f 72 20 74 68 65 20 is page for the
347f0 63 65 6c 6c 2d 70 6f 69 6e 74 65 72 20 61 72 65 cell-pointer are
34800 61 20 28 66 6f 72 0a 2a 2a 20 61 6c 6c 20 63 65 a (for.** all ce
34810 6c 6c 73 20 2d 20 6e 6f 74 20 6a 75 73 74 20 74 lls - not just t
34820 68 6f 73 65 20 69 6e 73 65 72 74 65 64 20 62 79 hose inserted by
34830 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 61 6c the current cal
34840 6c 29 2e 20 49 66 20 74 68 65 20 63 6f 6e 74 65 l). If the conte
34850 6e 74 0a 2a 2a 20 61 72 65 61 20 6d 75 73 74 20 nt.** area must
34860 62 65 20 65 78 74 65 6e 64 65 64 20 74 6f 20 62 be extended to b
34870 65 66 6f 72 65 20 74 68 69 73 20 70 6f 69 6e 74 efore this point
34880 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 63 63 in order to acc
34890 6f 6d 6f 64 61 74 65 20 61 6c 6c 0a 2a 2a 20 63 omodate all.** c
348a0 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d ells in apCell[]
348b0 2c 20 74 68 65 6e 20 74 68 65 20 63 65 6c 6c 73 , then the cells
348c0 20 64 6f 20 6e 6f 74 20 66 69 74 20 61 6e 64 20 do not fit and
348d0 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 non-zero is retu
348e0 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
348f0 69 6e 74 20 70 61 67 65 49 6e 73 65 72 74 41 72 int pageInsertAr
34900 72 61 79 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a ray(. MemPage *
34910 70 50 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 pPg,
34920 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 /* Page t
34930 6f 20 61 64 64 20 63 65 6c 6c 73 20 74 6f 20 2a o add cells to *
34940 2f 0a 20 20 75 38 20 2a 70 42 65 67 69 6e 2c 20 /. u8 *pBegin,
34950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34960 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 63 65 /* End of ce
34970 6c 6c 2d 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll-pointer array
34980 20 2a 2f 0a 20 20 75 38 20 2a 2a 70 70 44 61 74 */. u8 **ppDat
34990 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a,
349a0 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a /* IN/OUT:
349b0 20 50 61 67 65 20 63 6f 6e 74 65 6e 74 20 2d 61 Page content -a
349c0 72 65 61 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 rea pointer */.
349d0 20 75 38 20 2a 70 43 65 6c 6c 70 74 72 2c 20 20 u8 *pCellptr,
349e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
349f0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 /* Pointer to c
34a00 65 6c 6c 2d 70 6f 69 6e 74 65 72 20 61 72 65 61 ell-pointer area
34a10 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c */. int nCell,
34a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34a30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
34a40 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 of cells to add
34a50 74 6f 20 70 50 67 20 2a 2f 0a 20 20 75 38 20 2a to pPg */. u8 *
34a60 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 *apCell,
34a70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
34a80 72 72 61 79 20 6f 66 20 63 65 6c 6c 73 20 2a 2f rray of cells */
34a90 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 20 20 . u16 *szCell
34aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34ab0 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 /* Array of c
34ac0 65 6c 6c 20 73 69 7a 65 73 20 2a 2f 0a 29 7b 0a ell sizes */.){.
34ad0 20 20 69 6e 74 20 69 3b 0a 20 20 75 38 20 2a 61 int i;. u8 *a
34ae0 44 61 74 61 20 3d 20 70 50 67 2d 3e 61 44 61 74 Data = pPg->aDat
34af0 61 3b 0a 20 20 75 38 20 2a 70 44 61 74 61 20 3d a;. u8 *pData =
34b00 20 2a 70 70 44 61 74 61 3b 0a 20 20 63 6f 6e 73 *ppData;. cons
34b10 74 20 69 6e 74 20 62 46 72 65 65 6c 69 73 74 20 t int bFreelist
34b20 3d 20 61 44 61 74 61 5b 31 5d 20 7c 7c 20 61 44 = aData[1] || aD
34b30 61 74 61 5b 32 5d 3b 0a 20 20 61 73 73 65 72 74 ata[2];. assert
34b40 28 20 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 ( CORRUPT_DB ||
34b50 70 50 67 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d pPg->hdrOffset==
34b60 30 20 29 3b 20 20 20 20 2f 2a 20 4e 65 76 65 72 0 ); /* Never
34b70 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 called on page
34b80 31 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 1 */. for(i=0;
34b90 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
34ba0 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 7a 43 65 int sz = szCe
34bb0 6c 6c 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 72 ll[i];. int r
34bc0 63 3b 0a 20 20 20 20 75 38 20 2a 70 53 6c 6f 74 c;. u8 *pSlot
34bd0 3b 0a 20 20 20 20 69 66 28 20 62 46 72 65 65 6c ;. if( bFreel
34be0 69 73 74 3d 3d 30 20 7c 7c 20 28 70 53 6c 6f 74 ist==0 || (pSlot
34bf0 20 3d 20 70 61 67 65 46 69 6e 64 53 6c 6f 74 28 = pageFindSlot(
34c00 70 50 67 2c 20 73 7a 2c 20 26 72 63 2c 20 30 29 pPg, sz, &rc, 0)
34c10 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 44 )==0 ){. pD
34c20 61 74 61 20 2d 3d 20 73 7a 3b 0a 20 20 20 20 20 ata -= sz;.
34c30 20 69 66 28 20 70 44 61 74 61 3c 70 42 65 67 69 if( pData<pBegi
34c40 6e 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 n ) return 1;.
34c50 20 20 20 20 70 53 6c 6f 74 20 3d 20 70 44 61 74 pSlot = pDat
34c60 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d a;. }. mem
34c70 63 70 79 28 70 53 6c 6f 74 2c 20 61 70 43 65 6c cpy(pSlot, apCel
34c80 6c 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 70 l[i], sz);. p
34c90 75 74 32 62 79 74 65 28 70 43 65 6c 6c 70 74 72 ut2byte(pCellptr
34ca0 2c 20 28 70 53 6c 6f 74 20 2d 20 61 44 61 74 61 , (pSlot - aData
34cb0 29 29 3b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 ));. pCellptr
34cc0 20 2b 3d 20 32 3b 0a 20 20 7d 0a 20 20 2a 70 70 += 2;. }. *pp
34cd0 44 61 74 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 Data = pData;.
34ce0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
34cf0 2a 2a 20 41 72 72 61 79 20 61 70 43 65 6c 6c 5b ** Array apCell[
34d00 5d 20 63 6f 6e 74 61 69 6e 73 20 6e 43 65 6c 6c ] contains nCell
34d10 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 62 2d 74 pointers to b-t
34d20 72 65 65 20 63 65 6c 6c 73 2e 20 41 72 72 61 79 ree cells. Array
34d30 20 73 7a 43 65 6c 6c 20 0a 2a 2a 20 63 6f 6e 74 szCell .** cont
34d40 61 69 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e ains the size in
34d50 20 62 79 74 65 73 20 6f 66 20 65 61 63 68 20 73 bytes of each s
34d60 75 63 68 20 63 65 6c 6c 2e 20 54 68 69 73 20 66 uch cell. This f
34d70 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 74 68 65 unction adds the
34d80 0a 2a 2a 20 73 70 61 63 65 20 61 73 73 6f 63 69 .** space associ
34d90 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20 63 ated with each c
34da0 65 6c 6c 20 69 6e 20 74 68 65 20 61 72 72 61 79 ell in the array
34db0 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e 74 that is current
34dc0 6c 79 20 73 74 6f 72 65 64 20 0a 2a 2a 20 77 69 ly stored .** wi
34dd0 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 thin the body of
34de0 20 70 50 67 20 74 6f 20 74 68 65 20 70 50 67 20 pPg to the pPg
34df0 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 65 20 63 free-list. The c
34e00 65 6c 6c 2d 70 6f 69 6e 74 65 72 73 20 61 6e 64 ell-pointers and
34e10 20 6f 74 68 65 72 0a 2a 2a 20 66 69 65 6c 64 73 other.** fields
34e20 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 72 65 of the page are
34e30 20 6e 6f 74 20 75 70 64 61 74 65 64 2e 0a 2a 2a not updated..**
34e40 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
34e50 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 74 6f n returns the to
34e60 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 tal number of ce
34e70 6c 6c 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 lls added to the
34e80 20 66 72 65 65 2d 6c 69 73 74 2e 0a 2a 2f 0a 73 free-list..*/.s
34e90 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 46 72 tatic int pageFr
34ea0 65 65 41 72 72 61 79 28 0a 20 20 4d 65 6d 50 61 eeArray(. MemPa
34eb0 67 65 20 2a 70 50 67 2c 20 20 20 20 20 20 20 20 ge *pPg,
34ec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
34ed0 67 65 20 74 6f 20 65 64 69 74 20 2a 2f 0a 20 20 ge to edit */.
34ee0 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 int nCell,
34ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34f00 2f 2a 20 43 65 6c 6c 73 20 74 6f 20 64 65 6c 65 /* Cells to dele
34f10 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 te */. u8 **apC
34f20 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ell,
34f30 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 /* Array
34f40 20 6f 66 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 75 of cells */. u
34f50 31 36 20 2a 73 7a 43 65 6c 6c 20 20 20 20 20 20 16 *szCell
34f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
34f70 2a 20 41 72 72 61 79 20 6f 66 20 63 65 6c 6c 20 * Array of cell
34f80 73 69 7a 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 sizes */.){. u8
34f90 20 2a 20 63 6f 6e 73 74 20 61 44 61 74 61 20 3d * const aData =
34fa0 20 70 50 67 2d 3e 61 44 61 74 61 3b 0a 20 20 75 pPg->aData;. u
34fb0 38 20 2a 20 63 6f 6e 73 74 20 70 45 6e 64 20 3d 8 * const pEnd =
34fc0 20 26 61 44 61 74 61 5b 70 50 67 2d 3e 70 42 74 &aData[pPg->pBt
34fd0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 3b 0a 20 ->usableSize];.
34fe0 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 53 74 61 u8 * const pSta
34ff0 72 74 20 3d 20 26 61 44 61 74 61 5b 70 50 67 2d rt = &aData[pPg-
35000 3e 68 64 72 4f 66 66 73 65 74 20 2b 20 38 20 2b >hdrOffset + 8 +
35010 20 70 50 67 2d 3e 63 68 69 6c 64 50 74 72 53 69 pPg->childPtrSi
35020 7a 65 5d 3b 0a 20 20 69 6e 74 20 6e 52 65 74 20 ze];. int nRet
35030 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 = 0;. int i;.
35040 75 38 20 2a 70 46 72 65 65 20 3d 20 30 3b 0a 20 u8 *pFree = 0;.
35050 20 69 6e 74 20 73 7a 46 72 65 65 20 3d 20 30 3b int szFree = 0;
35060 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e .. for(i=0; i<n
35070 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
35080 75 38 20 2a 70 43 65 6c 6c 20 3d 20 61 70 43 65 u8 *pCell = apCe
35090 6c 6c 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 ll[i];. if( p
350a0 43 65 6c 6c 3e 3d 70 53 74 61 72 74 20 26 26 20 Cell>=pStart &&
350b0 70 43 65 6c 6c 3c 70 45 6e 64 20 29 7b 0a 20 20 pCell<pEnd ){.
350c0 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 7a 43 int sz = szC
350d0 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 ell[i];. if
350e0 28 20 70 46 72 65 65 21 3d 28 70 43 65 6c 6c 20 ( pFree!=(pCell
350f0 2b 20 73 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 + sz) ){.
35100 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20 if( pFree ){.
35110 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
35120 70 46 72 65 65 3e 61 44 61 74 61 20 26 26 20 28 pFree>aData && (
35130 70 46 72 65 65 20 2d 20 61 44 61 74 61 29 3c 36 pFree - aData)<6
35140 35 35 33 36 20 29 3b 0a 20 20 20 20 20 20 20 20 5536 );.
35150 20 20 66 72 65 65 53 70 61 63 65 28 70 50 67 2c freeSpace(pPg,
35160 20 28 75 31 36 29 28 70 46 72 65 65 20 2d 20 61 (u16)(pFree - a
35170 44 61 74 61 29 2c 20 73 7a 46 72 65 65 29 3b 0a Data), szFree);.
35180 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
35190 20 20 70 46 72 65 65 20 3d 20 70 43 65 6c 6c 3b pFree = pCell;
351a0 0a 20 20 20 20 20 20 20 20 73 7a 46 72 65 65 20 . szFree
351b0 3d 20 73 7a 3b 0a 20 20 20 20 20 20 20 20 69 66 = sz;. if
351c0 28 20 70 46 72 65 65 2b 73 7a 3e 70 45 6e 64 20 ( pFree+sz>pEnd
351d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 ) return 0;.
351e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
351f0 20 70 46 72 65 65 20 3d 20 70 43 65 6c 6c 3b 0a pFree = pCell;.
35200 20 20 20 20 20 20 20 20 73 7a 46 72 65 65 20 2b szFree +
35210 3d 20 73 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = sz;. }.
35220 20 20 20 20 6e 52 65 74 2b 2b 3b 0a 20 20 20 20 nRet++;.
35230 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72 65 }. }. if( pFre
35240 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 e ){. assert(
35250 20 70 46 72 65 65 3e 61 44 61 74 61 20 26 26 20 pFree>aData &&
35260 28 70 46 72 65 65 20 2d 20 61 44 61 74 61 29 3c (pFree - aData)<
35270 36 35 35 33 36 20 29 3b 0a 20 20 20 20 66 72 65 65536 );. fre
35280 65 53 70 61 63 65 28 70 50 67 2c 20 28 75 31 36 eSpace(pPg, (u16
35290 29 28 70 46 72 65 65 20 2d 20 61 44 61 74 61 29 )(pFree - aData)
352a0 2c 20 73 7a 46 72 65 65 29 3b 0a 20 20 7d 0a 20 , szFree);. }.
352b0 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a 7d 0a return nRet;.}.
352c0 0a 2f 2a 0a 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 ./*.** apCell[]
352d0 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 63 6f 6e and szCell[] con
352e0 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 tains pointers t
352f0 6f 20 61 6e 64 20 73 69 7a 65 73 20 6f 66 20 61 o and sizes of a
35300 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 0a ll cells in the.
35310 2a 2a 20 70 61 67 65 73 20 62 65 69 6e 67 20 62 ** pages being b
35320 61 6c 61 6e 63 65 64 2e 20 20 54 68 65 20 63 75 alanced. The cu
35330 72 72 65 6e 74 20 70 61 67 65 2c 20 70 50 67 2c rrent page, pPg,
35340 20 68 61 73 20 70 50 67 2d 3e 6e 43 65 6c 6c 20 has pPg->nCell
35350 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 0a 2a cells starting.*
35360 2a 20 77 69 74 68 20 61 70 43 65 6c 6c 5b 69 4f * with apCell[iO
35370 6c 64 5d 2e 20 20 41 66 74 65 72 20 62 61 6c 61 ld]. After bala
35380 6e 63 69 6e 67 2c 20 74 68 69 73 20 70 61 67 65 ncing, this page
35390 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 6e 4e 65 should hold nNe
353a0 77 20 63 65 6c 6c 73 0a 2a 2a 20 73 74 61 72 74 w cells.** start
353b0 69 6e 67 20 61 74 20 61 70 43 65 6c 6c 5b 69 4e ing at apCell[iN
353c0 65 77 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ew]..**.** This
353d0 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20 74 68 routine makes th
353e0 65 20 6e 65 63 65 73 73 61 72 79 20 61 64 6a 75 e necessary adju
353f0 73 74 6d 65 6e 74 73 20 74 6f 20 70 50 67 20 73 stments to pPg s
35400 6f 20 74 68 61 74 20 69 74 20 63 6f 6e 74 61 69 o that it contai
35410 6e 73 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 ns.** the correc
35420 74 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62 65 t cells after be
35430 69 6e 67 20 62 61 6c 61 6e 63 65 64 2e 0a 2a 2a ing balanced..**
35440 0a 2a 2a 20 54 68 65 20 70 50 67 2d 3e 6e 46 72 .** The pPg->nFr
35450 65 65 20 66 69 65 6c 64 20 69 73 20 69 6e 76 61 ee field is inva
35460 6c 69 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 lid when this fu
35470 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 nction returns.
35480 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 72 65 73 It is the.** res
35490 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 ponsibility of t
354a0 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 73 65 74 he caller to set
354b0 20 69 74 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a it correctly..*
354c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 64 /.static void ed
354d0 69 74 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 itPage(. MemPag
354e0 65 20 2a 70 50 67 2c 20 20 20 20 20 20 20 20 20 e *pPg,
354f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 64 69 /* Edi
35500 74 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 t this page */.
35510 20 69 6e 74 20 69 4f 6c 64 2c 20 20 20 20 20 20 int iOld,
35520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
35530 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 /* Index of fir
35540 73 74 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c st cell currentl
35550 79 20 6f 6e 20 70 61 67 65 20 2a 2f 0a 20 20 69 y on page */. i
35560 6e 74 20 69 4e 65 77 2c 20 20 20 20 20 20 20 20 nt iNew,
35570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
35580 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 77 20 66 * Index of new f
35590 69 72 73 74 20 63 65 6c 6c 20 6f 6e 20 70 61 67 irst cell on pag
355a0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 2c e */. int nNew,
355b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
355c0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20 /* Final
355d0 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 number of cells
355e0 6f 6e 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 on page */. u8
355f0 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 **apCell,
35600 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
35610 41 72 72 61 79 20 6f 66 20 63 65 6c 6c 73 20 2a Array of cells *
35620 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 20 /. u16 *szCell
35630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
35640 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 /* Array of
35650 63 65 6c 6c 20 73 69 7a 65 73 20 2a 2f 0a 29 7b cell sizes */.){
35660 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 44 . u8 * const aD
35670 61 74 61 20 3d 20 70 50 67 2d 3e 61 44 61 74 61 ata = pPg->aData
35680 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 ;. const int hd
35690 72 20 3d 20 70 50 67 2d 3e 68 64 72 4f 66 66 73 r = pPg->hdrOffs
356a0 65 74 3b 0a 20 20 75 38 20 2a 70 42 65 67 69 6e et;. u8 *pBegin
356b0 20 3d 20 26 70 50 67 2d 3e 61 43 65 6c 6c 49 64 = &pPg->aCellId
356c0 78 5b 6e 4e 65 77 20 2a 20 32 5d 3b 0a 20 20 69 x[nNew * 2];. i
356d0 6e 74 20 6e 43 65 6c 6c 20 3d 20 70 50 67 2d 3e nt nCell = pPg->
356e0 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 nCell; /*
356f0 43 65 6c 6c 73 20 73 74 6f 72 65 64 20 6f 6e 20 Cells stored on
35700 70 50 67 20 2a 2f 0a 20 20 75 38 20 2a 70 44 61 pPg */. u8 *pDa
35710 74 61 3b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 70 ta;. u8 *pCellp
35720 74 72 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 tr;. int i;. i
35730 6e 74 20 69 4f 6c 64 45 6e 64 20 3d 20 69 4f 6c nt iOldEnd = iOl
35740 64 20 2b 20 70 50 67 2d 3e 6e 43 65 6c 6c 20 2b d + pPg->nCell +
35750 20 70 50 67 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b pPg->nOverflow;
35760 0a 20 20 69 6e 74 20 69 4e 65 77 45 6e 64 20 3d . int iNewEnd =
35770 20 69 4e 65 77 20 2b 20 6e 4e 65 77 3b 0a 0a 23 iNew + nNew;..#
35780 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
35790 55 47 0a 20 20 75 38 20 2a 70 54 6d 70 20 3d 20 UG. u8 *pTmp =
357a0 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 sqlite3PagerTemp
357b0 53 70 61 63 65 28 70 50 67 2d 3e 70 42 74 2d 3e Space(pPg->pBt->
357c0 70 50 61 67 65 72 29 3b 0a 20 20 6d 65 6d 63 70 pPager);. memcp
357d0 79 28 70 54 6d 70 2c 20 61 44 61 74 61 2c 20 70 y(pTmp, aData, p
357e0 50 67 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 Pg->pBt->usableS
357f0 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 ize);.#endif..
35800 2f 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c 73 20 /* Remove cells
35810 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20 61 from the start a
35820 6e 64 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 nd end of the pa
35830 67 65 20 2a 2f 0a 20 20 69 66 28 20 69 4f 6c 64 ge */. if( iOld
35840 3c 69 4e 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 <iNew ){. int
35850 20 6e 53 68 69 66 74 20 3d 20 70 61 67 65 46 72 nShift = pageFr
35860 65 65 41 72 72 61 79 28 0a 20 20 20 20 20 20 20 eeArray(.
35870 20 70 50 67 2c 20 69 4e 65 77 2d 69 4f 6c 64 2c pPg, iNew-iOld,
35880 20 26 61 70 43 65 6c 6c 5b 69 4f 6c 64 5d 2c 20 &apCell[iOld],
35890 26 73 7a 43 65 6c 6c 5b 69 4f 6c 64 5d 0a 20 20 &szCell[iOld].
358a0 20 20 29 3b 0a 20 20 20 20 6d 65 6d 6d 6f 76 65 );. memmove
358b0 28 70 50 67 2d 3e 61 43 65 6c 6c 49 64 78 2c 20 (pPg->aCellIdx,
358c0 26 70 50 67 2d 3e 61 43 65 6c 6c 49 64 78 5b 6e &pPg->aCellIdx[n
358d0 53 68 69 66 74 2a 32 5d 2c 20 6e 43 65 6c 6c 2a Shift*2], nCell*
358e0 32 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 2d 3d 2);. nCell -=
358f0 20 6e 53 68 69 66 74 3b 0a 20 20 7d 0a 20 20 69 nShift;. }. i
35900 66 28 20 69 4e 65 77 45 6e 64 20 3c 20 69 4f 6c f( iNewEnd < iOl
35910 64 45 6e 64 20 29 7b 0a 20 20 20 20 6e 43 65 6c dEnd ){. nCel
35920 6c 20 2d 3d 20 70 61 67 65 46 72 65 65 41 72 72 l -= pageFreeArr
35930 61 79 28 0a 20 20 20 20 20 20 20 20 70 50 67 2c ay(. pPg,
35940 20 69 4f 6c 64 45 6e 64 2d 69 4e 65 77 45 6e 64 iOldEnd-iNewEnd
35950 2c 20 26 61 70 43 65 6c 6c 5b 69 4e 65 77 45 6e , &apCell[iNewEn
35960 64 5d 2c 20 26 73 7a 43 65 6c 6c 5b 69 4e 65 77 d], &szCell[iNew
35970 45 6e 64 5d 0a 20 20 20 20 29 3b 0a 20 20 7d 0a End]. );. }.
35980 0a 20 20 70 44 61 74 61 20 3d 20 26 61 44 61 74 . pData = &aDat
35990 61 5b 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 a[get2byteNotZer
359a0 6f 28 26 61 44 61 74 61 5b 68 64 72 2b 35 5d 29 o(&aData[hdr+5])
359b0 5d 3b 0a 20 20 69 66 28 20 70 44 61 74 61 3c 70 ];. if( pData<p
359c0 42 65 67 69 6e 20 29 20 67 6f 74 6f 20 65 64 69 Begin ) goto edi
359d0 74 70 61 67 65 5f 66 61 69 6c 3b 0a 0a 20 20 2f tpage_fail;.. /
359e0 2a 20 41 64 64 20 63 65 6c 6c 73 20 74 6f 20 74 * Add cells to t
359f0 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
35a00 70 61 67 65 20 2a 2f 0a 20 20 69 66 28 20 69 4e page */. if( iN
35a10 65 77 3c 69 4f 6c 64 20 29 7b 0a 20 20 20 20 69 ew<iOld ){. i
35a20 6e 74 20 6e 41 64 64 20 3d 20 4d 49 4e 28 6e 4e nt nAdd = MIN(nN
35a30 65 77 2c 69 4f 6c 64 2d 69 4e 65 77 29 3b 0a 20 ew,iOld-iNew);.
35a40 20 20 20 61 73 73 65 72 74 28 20 28 69 4f 6c 64 assert( (iOld
35a50 2d 69 4e 65 77 29 3c 6e 4e 65 77 20 7c 7c 20 6e -iNew)<nNew || n
35a60 43 65 6c 6c 3d 3d 30 20 7c 7c 20 43 4f 52 52 55 Cell==0 || CORRU
35a70 50 54 5f 44 42 20 29 3b 0a 20 20 20 20 70 43 65 PT_DB );. pCe
35a80 6c 6c 70 74 72 20 3d 20 70 50 67 2d 3e 61 43 65 llptr = pPg->aCe
35a90 6c 6c 49 64 78 3b 0a 20 20 20 20 6d 65 6d 6d 6f llIdx;. memmo
35aa0 76 65 28 26 70 43 65 6c 6c 70 74 72 5b 6e 41 64 ve(&pCellptr[nAd
35ab0 64 2a 32 5d 2c 20 70 43 65 6c 6c 70 74 72 2c 20 d*2], pCellptr,
35ac0 6e 43 65 6c 6c 2a 32 29 3b 0a 20 20 20 20 69 66 nCell*2);. if
35ad0 28 20 70 61 67 65 49 6e 73 65 72 74 41 72 72 61 ( pageInsertArra
35ae0 79 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 y(. pPg
35af0 2c 20 70 42 65 67 69 6e 2c 20 26 70 44 61 74 61 , pBegin, &pData
35b00 2c 20 70 43 65 6c 6c 70 74 72 2c 0a 20 20 20 20 , pCellptr,.
35b10 20 20 20 20 20 20 6e 41 64 64 2c 20 26 61 70 43 nAdd, &apC
35b20 65 6c 6c 5b 69 4e 65 77 5d 2c 20 26 73 7a 43 65 ell[iNew], &szCe
35b30 6c 6c 5b 69 4e 65 77 5d 0a 20 20 20 20 29 20 29 ll[iNew]. ) )
35b40 20 67 6f 74 6f 20 65 64 69 74 70 61 67 65 5f 66 goto editpage_f
35b50 61 69 6c 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 2b ail;. nCell +
35b60 3d 20 6e 41 64 64 3b 0a 20 20 7d 0a 0a 20 20 2f = nAdd;. }.. /
35b70 2a 20 41 64 64 20 61 6e 79 20 6f 76 65 72 66 6c * Add any overfl
35b80 6f 77 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 66 6f ow cells */. fo
35b90 72 28 69 3d 30 3b 20 69 3c 70 50 67 2d 3e 6e 4f r(i=0; i<pPg->nO
35ba0 76 65 72 66 6c 6f 77 3b 20 69 2b 2b 29 7b 0a 20 verflow; i++){.
35bb0 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d 20 28 int iCell = (
35bc0 69 4f 6c 64 20 2b 20 70 50 67 2d 3e 61 69 4f 76 iOld + pPg->aiOv
35bd0 66 6c 5b 69 5d 29 20 2d 20 69 4e 65 77 3b 0a 20 fl[i]) - iNew;.
35be0 20 20 20 69 66 28 20 69 43 65 6c 6c 3e 3d 30 20 if( iCell>=0
35bf0 26 26 20 69 43 65 6c 6c 3c 6e 4e 65 77 20 29 7b && iCell<nNew ){
35c00 0a 20 20 20 20 20 20 70 43 65 6c 6c 70 74 72 20 . pCellptr
35c10 3d 20 26 70 50 67 2d 3e 61 43 65 6c 6c 49 64 78 = &pPg->aCellIdx
35c20 5b 69 43 65 6c 6c 20 2a 20 32 5d 3b 0a 20 20 20 [iCell * 2];.
35c30 20 20 20 6d 65 6d 6d 6f 76 65 28 26 70 43 65 6c memmove(&pCel
35c40 6c 70 74 72 5b 32 5d 2c 20 70 43 65 6c 6c 70 74 lptr[2], pCellpt
35c50 72 2c 20 28 6e 43 65 6c 6c 20 2d 20 69 43 65 6c r, (nCell - iCel
35c60 6c 29 20 2a 20 32 29 3b 0a 20 20 20 20 20 20 6e l) * 2);. n
35c70 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 69 66 Cell++;. if
35c80 28 20 70 61 67 65 49 6e 73 65 72 74 41 72 72 61 ( pageInsertArra
35c90 79 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 y(. p
35ca0 50 67 2c 20 70 42 65 67 69 6e 2c 20 26 70 44 61 Pg, pBegin, &pDa
35cb0 74 61 2c 20 70 43 65 6c 6c 70 74 72 2c 0a 20 20 ta, pCellptr,.
35cc0 20 20 20 20 20 20 20 20 20 20 31 2c 20 26 61 70 1, &ap
35cd0 43 65 6c 6c 5b 69 43 65 6c 6c 20 2b 20 69 4e 65 Cell[iCell + iNe
35ce0 77 5d 2c 20 26 73 7a 43 65 6c 6c 5b 69 43 65 6c w], &szCell[iCel
35cf0 6c 20 2b 20 69 4e 65 77 5d 0a 20 20 20 20 20 20 l + iNew].
35d00 29 20 29 20 67 6f 74 6f 20 65 64 69 74 70 61 67 ) ) goto editpag
35d10 65 5f 66 61 69 6c 3b 0a 20 20 20 20 7d 0a 20 20 e_fail;. }.
35d20 7d 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 63 }.. /* Append c
35d30 65 6c 6c 73 20 74 6f 20 74 68 65 20 65 6e 64 20 ells to the end
35d40 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 of the page */.
35d50 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 70 50 67 pCellptr = &pPg
35d60 2d 3e 61 43 65 6c 6c 49 64 78 5b 6e 43 65 6c 6c ->aCellIdx[nCell
35d70 2a 32 5d 3b 0a 20 20 69 66 28 20 70 61 67 65 49 *2];. if( pageI
35d80 6e 73 65 72 74 41 72 72 61 79 28 0a 20 20 20 20 nsertArray(.
35d90 20 20 20 20 70 50 67 2c 20 70 42 65 67 69 6e 2c pPg, pBegin,
35da0 20 26 70 44 61 74 61 2c 20 70 43 65 6c 6c 70 74 &pData, pCellpt
35db0 72 2c 0a 20 20 20 20 20 20 20 20 6e 4e 65 77 2d r,. nNew-
35dc0 6e 43 65 6c 6c 2c 20 26 61 70 43 65 6c 6c 5b 69 nCell, &apCell[i
35dd0 4e 65 77 2b 6e 43 65 6c 6c 5d 2c 20 26 73 7a 43 New+nCell], &szC
35de0 65 6c 6c 5b 69 4e 65 77 2b 6e 43 65 6c 6c 5d 0a ell[iNew+nCell].
35df0 20 20 29 20 29 20 67 6f 74 6f 20 65 64 69 74 70 ) ) goto editp
35e00 61 67 65 5f 66 61 69 6c 3b 0a 0a 20 20 70 50 67 age_fail;.. pPg
35e10 2d 3e 6e 43 65 6c 6c 20 3d 20 6e 4e 65 77 3b 0a ->nCell = nNew;.
35e20 20 20 70 50 67 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPg->nOverflow
35e30 20 3d 20 30 3b 0a 0a 20 20 70 75 74 32 62 79 74 = 0;.. put2byt
35e40 65 28 26 61 44 61 74 61 5b 68 64 72 2b 33 5d 2c e(&aData[hdr+3],
35e50 20 70 50 67 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 pPg->nCell);.
35e60 70 75 74 32 62 79 74 65 28 26 61 44 61 74 61 5b put2byte(&aData[
35e70 68 64 72 2b 35 5d 2c 20 70 44 61 74 61 20 2d 20 hdr+5], pData -
35e80 61 44 61 74 61 29 3b 0a 0a 23 69 66 64 65 66 20 aData);..#ifdef
35e90 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 SQLITE_DEBUG. f
35ea0 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 20 26 or(i=0; i<nNew &
35eb0 26 20 21 43 4f 52 52 55 50 54 5f 44 42 3b 20 69 & !CORRUPT_DB; i
35ec0 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 ++){. u8 *pCe
35ed0 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b 69 2b 69 4e ll = apCell[i+iN
35ee0 65 77 5d 3b 0a 20 20 20 20 69 6e 74 20 69 4f 66 ew];. int iOf
35ef0 66 20 3d 20 67 65 74 32 62 79 74 65 28 26 70 50 f = get2byte(&pP
35f00 67 2d 3e 61 43 65 6c 6c 49 64 78 5b 69 2a 32 5d g->aCellIdx[i*2]
35f10 29 3b 0a 20 20 20 20 69 66 28 20 70 43 65 6c 6c );. if( pCell
35f20 3e 3d 61 44 61 74 61 20 26 26 20 70 43 65 6c 6c >=aData && pCell
35f30 3c 26 61 44 61 74 61 5b 70 50 67 2d 3e 70 42 74 <&aData[pPg->pBt
35f40 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 20 29 7b ->usableSize] ){
35f50 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 26 . pCell = &
35f60 70 54 6d 70 5b 70 43 65 6c 6c 20 2d 20 61 44 61 pTmp[pCell - aDa
35f70 74 61 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 ta];. }. a
35f80 73 73 65 72 74 28 20 30 3d 3d 6d 65 6d 63 6d 70 ssert( 0==memcmp
35f90 28 70 43 65 6c 6c 2c 20 26 61 44 61 74 61 5b 69 (pCell, &aData[i
35fa0 4f 66 66 5d 2c 20 73 7a 43 65 6c 6c 5b 69 2b 69 Off], szCell[i+i
35fb0 4e 65 77 5d 29 20 29 3b 0a 20 20 7d 0a 23 65 6e New]) );. }.#en
35fc0 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 3b 0a 20 dif.. return;.
35fd0 65 64 69 74 70 61 67 65 5f 66 61 69 6c 3a 0a 20 editpage_fail:.
35fe0 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 65 64 /* Unable to ed
35ff0 69 74 20 74 68 69 73 20 70 61 67 65 2e 20 52 65 it this page. Re
36000 62 75 69 6c 64 20 69 74 20 66 72 6f 6d 20 73 63 build it from sc
36010 72 61 74 63 68 20 69 6e 73 74 65 61 64 2e 20 2a ratch instead. *
36020 2f 0a 20 20 72 65 62 75 69 6c 64 50 61 67 65 28 /. rebuildPage(
36030 70 50 67 2c 20 6e 4e 65 77 2c 20 26 61 70 43 65 pPg, nNew, &apCe
36040 6c 6c 5b 69 4e 65 77 5d 2c 20 26 73 7a 43 65 6c ll[iNew], &szCel
36050 6c 5b 69 4e 65 77 5d 29 3b 0a 7d 0a 0a 2f 2a 0a l[iNew]);.}../*.
36060 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
36070 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 parameters dete
36080 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 rmine how many a
36090 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 djacent pages ge
360a0 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e t involved.** in
360b0 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 a balancing ope
360c0 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 ration. NN is t
360d0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 he number of nei
360e0 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
360f0 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 side.** of the
36100 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63 page that partic
36110 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
36120 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ancing operation
36130 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 . NB is the.**
36140 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
36150 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69 pages that parti
36160 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e cipate, includin
36170 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67 g the target pag
36180 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 e and.** NN neig
36190 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
361a0 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 side..**.** The
361b0 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 minimum value of
361c0 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 NN is 1 (of cou
361d0 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e rse). Increasin
361e0 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 g NN above 1.**
361f0 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 (to 2 or 3) give
36200 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f s a modest impro
36210 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 vement in SELECT
36220 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 and DELETE perf
36230 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 ormance.** in ex
36240 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 change for a lar
36250 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 ger degradation
36260 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 in INSERT and UP
36270 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 DATE performance
36280 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
36290 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 f NN appears to
362a0 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65 give the best re
362b0 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a sults overall..*
362c0 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 /.#define NN 1
362d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
362e0 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 mber of neighbor
362f0 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 s on either side
36300 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 of pPage */.#de
36310 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 fine NB (NN*2+1)
36320 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 /* Total p
36330 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e ages involved in
36340 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a the balance */.
36350 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
36360 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e _OMIT_QUICKBALAN
36370 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 CE./*.** This ve
36380 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 rsion of balance
36390 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 () handles the c
363a0 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 ommon special ca
363b0 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 se where.** a ne
363c0 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 w entry is being
363d0 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 inserted on the
363e0 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 extreme right-e
363f0 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 nd of the.** tre
36400 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 e, in other word
36410 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 s, when the new
36420 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d entry will becom
36430 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a e the largest.**
36440 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 entry in the tr
36450 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 ee..**.** Instea
36460 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62 d of trying to b
36470 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 alance the 3 rig
36480 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 ht-most leaf pag
36490 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 es, just add.**
364a0 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 a new page to th
364b0 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
364c0 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e e and put the on
364d0 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a e new entry in.*
364e0 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 * that page. Th
364f0 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 is leaves the ri
36500 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 ght side of the
36510 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a tree somewhat.**
36520 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 unbalanced. Bu
36530 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 t odds are that
36540 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 we will be inser
36550 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 ting new entries
36560 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 .** at the end s
36570 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 oon afterwards s
36580 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 o the nearly emp
36590 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 ty page will qui
365a0 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e ckly.** fill up.
365b0 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a On average..**
365c0 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 .** pPage is the
365d0 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 leaf page which
365e0 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f is the right-mo
365f0 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 st page in the t
36600 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 ree..** pParent
36610 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 is its parent.
36620 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 pPage must have
36630 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f a single overflo
36640 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 w entry.** which
36650 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 is also the rig
36660 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e ht-most entry on
36670 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
36680 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 The pSpace buff
36690 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 er is used to st
366a0 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ore a temporary
366b0 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69 copy of the divi
366c0 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74 der.** cell that
366d0 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 will be inserte
366e0 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 d into pParent.
366f0 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73 Such a cell cons
36700 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62 ists of a 4.** b
36710 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 yte page number
36720 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61 followed by a va
36730 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
36740 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a teger. In other.
36750 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73 ** words, at mos
36760 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63 t 13 bytes. Henc
36770 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 e the pSpace buf
36780 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a fer must be at.*
36790 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73 * least 13 bytes
367a0 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 in size..*/.sta
367b0 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
367c0 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70 quick(MemPage *p
367d0 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20 Parent, MemPage
367e0 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61 *pPage, u8 *pSpa
367f0 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ce){. BtShared
36800 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61 *const pBt = pPa
36810 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42 ge->pBt; /* B
36820 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a -Tree Database *
36830 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 /. MemPage *pNe
36840 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
36850 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c /* Newl
36860 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 y allocated page
36870 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
36880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
368a0 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50 turn Code */. P
368b0 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20 gno pgnoNew;
368c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
368d0 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
368e0 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20 er of pNew */..
368f0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
36900 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
36910 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
36920 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
36930 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
36940 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 ble(pParent->pDb
36950 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
36960 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
36970 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 low==1 );.. /*
36980 54 68 69 73 20 65 72 72 6f 72 20 63 6f 6e 64 69 This error condi
36990 74 69 6f 6e 20 69 73 20 6e 6f 77 20 63 61 75 67 tion is now caug
369a0 68 74 20 70 72 69 6f 72 20 74 6f 20 72 65 61 63 ht prior to reac
369b0 68 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 hing this functi
369c0 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 on */. if( NEVE
369d0 52 28 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d R(pPage->nCell==
369e0 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 0) ) return SQLI
369f0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
36a00 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
36a10 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 a new page. This
36a20 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d page will becom
36a30 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c e the right-sibl
36a40 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 ing of . ** pPa
36a50 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 ge. Make the par
36a60 65 6e 74 20 70 61 67 65 20 77 72 69 74 61 62 6c ent page writabl
36a70 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e e, so that the n
36a80 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a ew divider cell.
36a90 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 ** may be inse
36aa0 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 rted. If both th
36ab0 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 ese operations a
36ac0 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 re successful, p
36ad0 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 roceed.. */. r
36ae0 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
36af0 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 ePage(pBt, &pNew
36b00 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 , &pgnoNew, 0, 0
36b10 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 );.. if( rc==SQ
36b20 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 LITE_OK ){..
36b30 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 u8 *pOut = &pSpa
36b40 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 ce[4];. u8 *p
36b50 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 70 Cell = pPage->ap
36b60 4f 76 66 6c 5b 30 5d 3b 0a 20 20 20 20 75 31 36 Ovfl[0];. u16
36b70 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 szCell = cellSi
36b80 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 zePtr(pPage, pCe
36b90 6c 6c 29 3b 0a 20 20 20 20 75 38 20 2a 70 53 74 ll);. u8 *pSt
36ba0 6f 70 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 op;.. assert(
36bb0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
36bc0 72 69 74 65 61 62 6c 65 28 70 4e 65 77 2d 3e 70 riteable(pNew->p
36bd0 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 61 DbPage) );. a
36be0 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 ssert( pPage->aD
36bf0 61 74 61 5b 30 5d 3d 3d 28 50 54 46 5f 49 4e 54 ata[0]==(PTF_INT
36c00 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 KEY|PTF_LEAFDATA
36c10 7c 50 54 46 5f 4c 45 41 46 29 20 29 3b 0a 20 20 |PTF_LEAF) );.
36c20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c zeroPage(pNew,
36c30 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f PTF_INTKEY|PTF_
36c40 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 LEAFDATA|PTF_LEA
36c50 46 29 3b 0a 20 20 20 20 72 65 62 75 69 6c 64 50 F);. rebuildP
36c60 61 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 age(pNew, 1, &pC
36c70 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 ell, &szCell);.
36c80 20 20 20 70 4e 65 77 2d 3e 6e 46 72 65 65 20 3d pNew->nFree =
36c90 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
36ca0 20 2d 20 70 4e 65 77 2d 3e 63 65 6c 6c 4f 66 66 - pNew->cellOff
36cb0 73 65 74 20 2d 20 32 20 2d 20 73 7a 43 65 6c 6c set - 2 - szCell
36cc0 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ;.. /* If thi
36cd0 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 s is an auto-vac
36ce0 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 70 uum database, up
36cf0 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 date the pointer
36d00 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 74 68 map. ** with
36d10 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 entries for the
36d20 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20 61 new page, and a
36d30 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d 20 ny pointer from
36d40 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c the . ** cell
36d50 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f 20 on the page to
36d60 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
36d70 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 74 . If either of t
36d80 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 65 72 hese. ** oper
36d90 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 74 68 ations fails, th
36da0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 e return code is
36db0 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 63 6f set, but the co
36dc0 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66 ntents. ** of
36dd0 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
36de0 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e 69 70 are still manip
36df0 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 63 6f ulated by thh co
36e00 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2a de below.. **
36e10 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 74 20 That is Ok, at
36e20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 70 this point the p
36e30 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 67 75 arent page is gu
36e40 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 20 20 aranteed to.
36e50 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 ** be marked as
36e60 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 6e 67 dirty. Returning
36e70 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 77 an error code w
36e80 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 20 20 ill cause a.
36e90 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e 64 ** rollback, und
36ea0 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 73 oing any changes
36eb0 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 61 72 made to the par
36ec0 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f ent page.. */
36ed0 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 . if( ISAUTOV
36ee0 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 ACUUM ){. p
36ef0 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 trmapPut(pBt, pg
36f00 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 54 noNew, PTRMAP_BT
36f10 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 REE, pParent->pg
36f20 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 no, &rc);.
36f30 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77 2d if( szCell>pNew-
36f40 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 >minLocal ){.
36f50 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 ptrmapPutOv
36f60 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 65 6c flPtr(pNew, pCel
36f70 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 7d l, &rc);. }
36f80 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
36f90 20 43 72 65 61 74 65 20 61 20 64 69 76 69 64 65 Create a divide
36fa0 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 72 74 r cell to insert
36fb0 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 54 into pParent. T
36fc0 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a he divider cell.
36fd0 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 73 20 ** consists
36fe0 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 67 65 of a 4-byte page
36ff0 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 61 67 number (the pag
37000 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 61 67 e number of pPag
37010 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 20 e) and. ** a
37020 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 variable length
37030 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 63 68 key value (which
37040 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d must be the sam
37050 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a 20 e value as the.
37060 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b 65 ** largest ke
37070 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 20 20 y on pPage)..
37080 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 66 69 **. ** To fi
37090 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 6b nd the largest k
370a0 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 61 67 ey value on pPag
370b0 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 74 68 e, first find th
370c0 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a 20 20 e right-most .
370d0 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 ** cell on pPa
370e0 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 74 77 ge. The first tw
370f0 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 o fields of this
37100 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 0a 20 cell are the .
37110 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c 65 6e ** record-len
37120 67 74 68 20 28 61 20 76 61 72 69 61 62 6c 65 20 gth (a variable
37130 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 61 length integer a
37140 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 20 69 t most 32-bits i
37150 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a 20 61 n size). ** a
37160 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 nd the key value
37170 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e (a variable len
37180 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d 61 79 gth integer, may
37190 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 65 29 have any value)
371a0 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72 .. ** The fir
371b0 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65 28 st of the while(
371c0 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f 77 ...) loops below
371d0 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65 20 skips over the
371e0 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a 20 20 record-length.
371f0 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65 20 ** field. The
37200 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e 2e second while(...
37210 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74 68 ) loop copies th
37220 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f 6d e key value from
37230 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 6c 6c the. ** cell
37240 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20 74 on pPage into t
37250 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72 he pSpace buffer
37260 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 65 .. */. pCe
37270 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
37280 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c age, pPage->nCel
37290 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f 70 20 l-1);. pStop
372a0 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 = &pCell[9];.
372b0 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 6c 6c while( (*(pCell
372c0 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 43 65 ++)&0x80) && pCe
372d0 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 20 20 ll<pStop );.
372e0 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 pStop = &pCell[9
372f0 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 28 ];. while( ((
37300 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 70 43 *(pOut++) = *(pC
37310 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 26 26 ell++))&0x80) &&
37320 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a pCell<pStop );.
37330 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 . /* Insert t
37340 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 he new divider c
37350 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 ell into pParent
37360 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 74 43 . */. insertC
37370 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 50 61 ell(pParent, pPa
37380 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 53 70 rent->nCell, pSp
37390 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 74 2d ace, (int)(pOut-
373a0 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 20 20 pSpace),.
373b0 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 67 65 0, pPage
373c0 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 0a 20 ->pgno, &rc);..
373d0 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 72 69 /* Set the ri
373e0 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74 65 ght-child pointe
373f0 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 6f 20 r of pParent to
37400 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 point to the new
37410 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70 75 page. */. pu
37420 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d t4byte(&pParent-
37430 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e >aData[pParent->
37440 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 hdrOffset+8], pg
37450 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f noNew);. . /
37460 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72 65 * Release the re
37470 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 6e ference to the n
37480 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 ew page. */.
37490 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
374a0 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
374b0 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a rc;.}.#endif /*
374c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 SQLITE_OMIT_QUI
374d0 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 23 69 CKBALANCE */..#i
374e0 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 f 0./*.** This f
374f0 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 unction does not
37500 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e 79 74 contribute anyt
37510 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 65 72 hing to the oper
37520 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e ation of SQLite.
37530 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 74 69 .** it is someti
37540 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 74 65 mes activated te
37550 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c 65 20 mporarily while
37560 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 20 72 debugging code r
37570 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a 20 66 esponsible .** f
37580 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 6e 74 or setting point
37590 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 2e 0a er-map entries..
375a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 */.static int pt
375b0 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 4d rmapCheckPages(M
375c0 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 65 2c emPage **apPage,
375d0 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 int nPage){. i
375e0 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28 69 nt i, j;. for(i
375f0 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b 2b =0; i<nPage; i++
37600 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0a 20 ){. Pgno n;.
37610 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d 65 6d u8 e;. Mem
37620 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 61 70 Page *pPage = ap
37630 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 74 53 Page[i];. BtS
37640 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 hared *pBt = pPa
37650 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 ge->pBt;. ass
37660 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e ert( pPage->isIn
37670 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 6a it );.. for(j
37680 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e 43 65 =0; j<pPage->nCe
37690 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ll; j++){.
376a0 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
376b0 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 20 20 u8 *z;.
376c0 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 6e 64 . z = find
376d0 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 3b 0a Cell(pPage, j);.
376e0 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65 btreeParse
376f0 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 7a CellPtr(pPage, z
37700 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 , &info);.
37710 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c if( info.iOverfl
37720 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 50 67 ow ){. Pg
37730 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 no ovfl = get4by
37740 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65 72 te(&z[info.iOver
37750 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 20 20 flow]);.
37760 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6f ptrmapGet(pBt, o
37770 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 20 vfl, &e, &n);.
37780 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3d assert( n=
37790 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20 =pPage->pgno &&
377a0 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
377b0 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 OW1 );. }.
377c0 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d if( !pPage-
377d0 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 >leaf ){.
377e0 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67 65 Pgno child = ge
377f0 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 20 20 t4byte(z);.
37800 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 ptrmapGet(pBt
37810 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e 29 , child, &e, &n)
37820 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
37830 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f ( n==pPage->pgno
37840 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 && e==PTRMAP_BT
37850 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 REE );. }.
37860 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 50 }. if( !pP
37870 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
37880 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 Pgno child =
37890 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
378a0 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
378b0 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
378c0 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 ptrmapGet(pBt
378d0 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e 29 , child, &e, &n)
378e0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
378f0 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 n==pPage->pgno &
37900 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 & e==PTRMAP_BTRE
37910 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 E );. }. }.
37920 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
37930 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
37940 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
37950 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f 6e to copy the con
37960 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 2d 74 tents of the b-t
37970 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 64 20 ree node stored
37980 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 72 6f .** on page pFro
37990 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 49 m to page pTo. I
379a0 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 61 73 f page pFrom was
379b0 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 65 not a leaf page
379c0 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 70 6f , then.** the po
379d0 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 inter-map entrie
379e0 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 6c 64 s for each child
379f0 20 70 61 67 65 20 61 72 65 20 75 70 64 61 74 65 page are update
37a00 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a d so that the.**
37a10 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 74 6f parent page sto
37a20 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 red in the point
37a30 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 20 70 er map is page p
37a40 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e To. If pFrom con
37a50 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 63 65 tained.** any ce
37a60 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 6c 6f lls with overflo
37a70 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73 2c w page pointers,
37a80 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 then the corres
37a90 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 72 0a ponding pointer.
37aa0 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 61 ** map entries a
37ab0 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 64 20 re also updated
37ac0 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 72 65 so that the pare
37ad0 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 65 20 nt page is page
37ae0 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 46 pTo..**.** If pF
37af0 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 6c 79 rom is currently
37b00 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 6f 76 carrying any ov
37b10 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 65 6e erflow cells (en
37b20 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 tries in the.**
37b30 4d 65 6d 50 61 67 65 2e 61 70 4f 76 66 6c 5b 5d MemPage.apOvfl[]
37b40 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 72 array), they ar
37b50 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f 20 e not copied to
37b60 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 6f pTo. .**.** Befo
37b70 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 61 re returning, pa
37b80 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 74 ge pTo is reinit
37b90 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 74 ialized using bt
37ba0 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a reeInitPage()..*
37bb0 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 6d *.** The perform
37bc0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e ance of this fun
37bd0 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 69 ction is not cri
37be0 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e 6c tical. It is onl
37bf0 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 68 y used by .** th
37c00 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f e balance_shallo
37c10 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e 63 wer() and balanc
37c20 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 65 e_deeper() proce
37c30 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 6f dures, neither o
37c40 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 63 f.** which are c
37c50 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 65 alled often unde
37c60 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d 73 r normal circums
37c70 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 tances..*/.stati
37c80 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 43 c void copyNodeC
37c90 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 2a ontent(MemPage *
37ca0 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 2a pFrom, MemPage *
37cb0 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a pTo, int *pRC){.
37cc0 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 if( (*pRC)==SQ
37cd0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 42 LITE_OK ){. B
37ce0 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 20 tShared * const
37cf0 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 pBt = pFrom->pBt
37d00 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 ;. u8 * const
37d10 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d 3e aFrom = pFrom->
37d20 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a 20 aData;. u8 *
37d30 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f 2d const aTo = pTo-
37d40 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 20 >aData;. int
37d50 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 3d const iFromHdr =
37d60 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 65 pFrom->hdrOffse
37d70 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 t;. int const
37d80 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f 2d iToHdr = ((pTo-
37d90 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 20 >pgno==1) ? 100
37da0 3a 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 72 63 : 0);. int rc
37db0 3b 0a 20 20 20 20 69 6e 74 20 69 44 61 74 61 3b ;. int iData;
37dc0 0a 20 20 0a 20 20 0a 20 20 20 20 61 73 73 65 72 . . . asser
37dd0 74 28 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 t( pFrom->isInit
37de0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
37df0 70 46 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 pFrom->nFree>=iT
37e00 6f 48 64 72 20 29 3b 0a 20 20 20 20 61 73 73 65 oHdr );. asse
37e10 72 74 28 20 67 65 74 32 62 79 74 65 28 26 61 46 rt( get2byte(&aF
37e20 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 rom[iFromHdr+5])
37e30 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 <= (int)pBt->us
37e40 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 0a 20 ableSize );. .
37e50 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 62 /* Copy the b
37e60 2d 74 72 65 65 20 6e 6f 64 65 20 63 6f 6e 74 65 -tree node conte
37e70 6e 74 20 66 72 6f 6d 20 70 61 67 65 20 70 46 72 nt from page pFr
37e80 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 om to page pTo.
37e90 2a 2f 0a 20 20 20 20 69 44 61 74 61 20 3d 20 67 */. iData = g
37ea0 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 et2byte(&aFrom[i
37eb0 46 72 6f 6d 48 64 72 2b 35 5d 29 3b 0a 20 20 20 FromHdr+5]);.
37ec0 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b 69 44 61 memcpy(&aTo[iDa
37ed0 74 61 5d 2c 20 26 61 46 72 6f 6d 5b 69 44 61 74 ta], &aFrom[iDat
37ee0 61 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 a], pBt->usableS
37ef0 69 7a 65 2d 69 44 61 74 61 29 3b 0a 20 20 20 20 ize-iData);.
37f00 6d 65 6d 63 70 79 28 26 61 54 6f 5b 69 54 6f 48 memcpy(&aTo[iToH
37f10 64 72 5d 2c 20 26 61 46 72 6f 6d 5b 69 46 72 6f dr], &aFrom[iFro
37f20 6d 48 64 72 5d 2c 20 70 46 72 6f 6d 2d 3e 63 65 mHdr], pFrom->ce
37f30 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 46 72 llOffset + 2*pFr
37f40 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 0a 20 om->nCell);. .
37f50 20 20 20 2f 2a 20 52 65 69 6e 69 74 69 61 6c 69 /* Reinitiali
37f60 7a 65 20 70 61 67 65 20 70 54 6f 20 73 6f 20 74 ze page pTo so t
37f70 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 hat the contents
37f80 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 of the MemPage
37f90 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 2a 2a structure. **
37fa0 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 64 match the new d
37fb0 61 74 61 2e 20 54 68 65 20 69 6e 69 74 69 61 6c ata. The initial
37fc0 69 7a 61 74 69 6f 6e 20 6f 66 20 70 54 6f 20 63 ization of pTo c
37fd0 61 6e 20 61 63 74 75 61 6c 6c 79 20 66 61 69 6c an actually fail
37fe0 20 75 6e 64 65 72 0a 20 20 20 20 2a 2a 20 66 61 under. ** fa
37ff0 69 72 6c 79 20 6f 62 73 63 75 72 65 20 63 69 72 irly obscure cir
38000 63 75 6d 73 74 61 6e 63 65 73 2c 20 65 76 65 6e cumstances, even
38010 20 74 68 6f 75 67 68 20 69 74 20 69 73 20 61 20 though it is a
38020 63 6f 70 79 20 6f 66 20 69 6e 69 74 69 61 6c 69 copy of initiali
38030 7a 65 64 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 zed . ** page
38040 20 70 46 72 6f 6d 2e 0a 20 20 20 20 2a 2f 0a 20 pFrom.. */.
38050 20 20 20 70 54 6f 2d 3e 69 73 49 6e 69 74 20 3d pTo->isInit =
38060 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 0;. rc = btr
38070 65 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b eeInitPage(pTo);
38080 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
38090 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
380a0 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 20 *pRC = rc;.
380b0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
380c0 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 . /* If this
380d0 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 is an auto-vacu
380e0 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 um database, upd
380f0 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d ate the pointer-
38100 6d 61 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 map entries.
38110 2a 2a 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 ** for any b-tre
38120 65 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 e or overflow pa
38130 67 65 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 ges that pTo now
38140 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f contains the po
38150 69 6e 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a inters to.. *
38160 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f /. if( ISAUTO
38170 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
38180 2a 70 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 *pRC = setChildP
38190 74 72 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 trmaps(pTo);.
381a0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
381b0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 This routine red
381c0 69 73 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 istributes cells
381d0 20 6f 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 on the iParentI
381e0 64 78 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 dx'th child of p
381f0 50 61 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 Parent.** (herea
38200 66 74 65 72 20 22 74 68 65 20 70 61 67 65 22 29 fter "the page")
38210 20 61 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 and up to 2 sib
38220 6c 69 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c lings so that al
38230 6c 20 70 61 67 65 73 20 68 61 76 65 20 61 62 6f l pages have abo
38240 75 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 ut the.** same a
38250 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 mount of free sp
38260 61 63 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 ace. Usually a s
38270 69 6e 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e ingle sibling on
38280 20 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 either side of
38290 74 68 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 the.** page are
382a0 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 used in the bala
382b0 6e 63 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f ncing, though bo
382c0 74 68 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 th siblings migh
382d0 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a t come from one.
382e0 2a 2a 20 73 69 64 65 20 69 66 20 74 68 65 20 70 ** side if the p
382f0 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 age is the first
38300 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f or last child o
38310 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 f its parent. If
38320 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 the page .** ha
38330 73 20 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 s fewer than 2 s
38340 69 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 iblings (somethi
38350 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c ng which can onl
38360 79 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 y happen if the
38370 70 61 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f page.** is a roo
38380 74 20 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c t page or a chil
38390 64 20 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 d of a root page
383a0 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c ) then all avail
383b0 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a able siblings.**
383c0 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 participate in
383d0 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a the balancing..*
383e0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
383f0 6f 66 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 of siblings of t
38400 68 65 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 he page might be
38410 20 69 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 increased or de
38420 63 72 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f creased by .** o
38430 6e 65 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 ne or two in an
38440 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 effort to keep p
38450 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c ages nearly full
38460 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 but not over fu
38470 6c 6c 2e 20 0a 2a 2a 0a 2