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 54 61 62 20 3d 20 70 49 64 78 2d 3e iTab = pIdx->
1850: 70 54 61 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 pTable->tnum;.
1860: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 }. }. }e
1870: 6c 73 65 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 lse{. iTab =
1880: 69 52 6f 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a iRoot;. }.. /*
1890: 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 Search for the
18a0: 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 required lock. E
18b0: 69 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c 6f ither a write-lo
18c0: 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 ck on root-page
18d0: 69 54 61 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 iTab, a . ** wr
18e0: 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite-lock on the
18f0: 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 schema table, or
1900: 20 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74 20 (if the client
1910: 69 73 20 72 65 61 64 69 6e 67 29 20 61 0a 20 20 is reading) a.
1920: 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 ** read-lock on
1930: 69 54 61 62 20 77 69 6c 6c 20 73 75 66 66 69 63 iTab will suffic
1940: 65 2e 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 e. Return 1 if a
1950: 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72 65 20 ny of these are
1960: 66 6f 75 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 found. */. for
1970: 28 70 4c 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 (pLock=pBtree->p
1980: 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b Bt->pLock; pLock
1990: 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 ; pLock=pLock->p
19a0: 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
19b0: 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 Lock->pBtree==pB
19c0: 74 72 65 65 20 0a 20 20 20 20 20 26 26 20 28 70 tree . && (p
19d0: 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 Lock->iTable==iT
19e0: 61 62 20 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c ab || (pLock->eL
19f0: 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
1a00: 26 26 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 && pLock->iTable
1a10: 3d 3d 31 29 29 0a 20 20 20 20 20 26 26 20 70 4c ==1)). && pL
1a20: 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 ock->eLock>=eLoc
1a30: 6b 54 79 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 kType . ){.
1a40: 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1a50: 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 }. }.. /* Fa
1a60: 69 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 iled to find the
1a70: 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 required lock.
1a80: 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
1a90: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1aa0: 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 64 E_DEBUG */..#ifd
1ab0: 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1ac0: 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e /*.**** This fun
1ad0: 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 ction may be use
1ae0: 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 d as part of ass
1af0: 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
1b00: 20 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0a 2a 2a 0a 2a only. ****.**.*
1b10: 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
1b20: 20 69 74 20 77 6f 75 6c 64 20 62 65 20 69 6c 6c it would be ill
1b30: 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 20 egal for pBtree
1b40: 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 68 to write into th
1b50: 65 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e e.** table or in
1b60: 64 65 78 20 72 6f 6f 74 65 64 20 61 74 20 69 52 dex rooted at iR
1b70: 6f 6f 74 20 62 65 63 61 75 73 65 20 6f 74 68 65 oot because othe
1b80: 72 20 73 68 61 72 65 64 20 63 6f 6e 6e 65 63 74 r shared connect
1b90: 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 69 6d 75 ions are.** simu
1ba0: 6c 74 61 6e 65 6f 75 73 6c 79 20 72 65 61 64 69 ltaneously readi
1bb0: 6e 67 20 74 68 61 74 20 73 61 6d 65 20 74 61 62 ng that same tab
1bc0: 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a le or index..**.
1bd0: 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c ** It is illegal
1be0: 20 66 6f 72 20 70 42 74 72 65 65 20 74 6f 20 77 for pBtree to w
1bf0: 72 69 74 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 rite if some oth
1c00: 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 er Btree object
1c10: 74 68 61 74 0a 2a 2a 20 73 68 61 72 65 73 20 74 that.** shares t
1c20: 68 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 he same BtShared
1c30: 20 6f 62 6a 65 63 74 20 69 73 20 63 75 72 72 65 object is curre
1c40: 6e 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 ntly reading or
1c50: 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 69 writing.** the i
1c60: 52 6f 6f 74 20 74 61 62 6c 65 2e 20 20 45 78 63 Root table. Exc
1c70: 65 70 74 2c 20 69 66 20 74 68 65 20 6f 74 68 65 ept, if the othe
1c80: 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 68 r Btree object h
1c90: 61 73 20 74 68 65 0a 2a 2a 20 72 65 61 64 2d 75 as the.** read-u
1ca0: 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
1cb0: 73 65 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 set, then it is
1cc0: 4f 4b 20 66 6f 72 20 74 68 65 20 6f 74 68 65 72 OK for the other
1cd0: 20 6f 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 68 61 object to.** ha
1ce0: 76 65 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 ve a read cursor
1cf0: 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ..**.** For exam
1d00: 70 6c 65 2c 20 62 65 66 6f 72 65 20 77 72 69 74 ple, before writ
1d10: 69 6e 67 20 74 6f 20 61 6e 79 20 70 61 72 74 20 ing to any part
1d20: 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 of the table or
1d30: 69 6e 64 65 78 0a 2a 2a 20 72 6f 6f 74 65 64 20 index.** rooted
1d40: 61 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 6f at page iRoot, o
1d50: 6e 65 20 73 68 6f 75 6c 64 20 63 61 6c 6c 3a 0a ne should call:.
1d60: 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 **.** assert(
1d70: 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 !hasReadConflic
1d80: 74 73 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 ts(pBtree, iRoot
1d90: 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ) );.*/.static i
1da0: 6e 74 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 nt hasReadConfli
1db0: 63 74 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 cts(Btree *pBtre
1dc0: 65 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a e, Pgno iRoot){.
1dd0: 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
1de0: 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 for(p=pBtree->p
1df0: 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
1e00: 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
1e10: 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 if( p->pgnoRoot
1e20: 3d 3d 69 52 6f 6f 74 20 0a 20 20 20 20 20 26 26 ==iRoot . &&
1e30: 20 70 2d 3e 70 42 74 72 65 65 21 3d 70 42 74 72 p->pBtree!=pBtr
1e40: 65 65 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 70 ee. && 0==(p
1e50: 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c ->pBtree->db->fl
1e60: 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 ags & SQLITE_Rea
1e70: 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20 dUncommitted).
1e80: 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1e90: 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
1ea0: 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e return 0;.}.#en
1eb0: 64 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 66 dif /* #ifdef
1ec0: 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f SQLITE_DEBUG */
1ed0: 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f ../*.** Query to
1ee0: 20 73 65 65 20 69 66 20 42 74 72 65 65 20 68 61 see if Btree ha
1ef0: 6e 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69 ndle p may obtai
1f00: 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 n a lock of type
1f10: 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 eLock .** (READ
1f20: 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c _LOCK or WRITE_L
1f30: 4f 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c OCK) on the tabl
1f40: 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 e with root-page
1f50: 20 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a iTab. Return.**
1f60: 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 SQLITE_OK if th
1f70: 65 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 e lock may be ob
1f80: 74 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 tained (by calli
1f90: 6e 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43 ng.** setSharedC
1fa0: 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 acheTableLock())
1fb0: 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b , or SQLITE_LOCK
1fc0: 45 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 ED if not..*/.st
1fd0: 61 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68 atic int querySh
1fe0: 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
1ff0: 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e ck(Btree *p, Pgn
2000: 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b o iTab, u8 eLock
2010: 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
2020: 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
2030: 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 tLock *pIter;..
2040: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
2050: 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
2060: 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
2070: 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
2080: 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 || eLock==WRITE
2090: 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
20a0: 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 t( p->db!=0 );.
20b0: 20 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 62 assert( !(p->db
20c0: 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 ->flags&SQLITE_R
20d0: 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c eadUncommitted)|
20e0: 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f |eLock==WRITE_LO
20f0: 43 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 CK||iTab==1 );.
2100: 20 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 73 . /* If reques
2110: 74 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 ting a write-loc
2120: 6b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 65 k, then the Btre
2130: 65 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 6f e must have an o
2140: 70 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 pen write. ** t
2150: 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
2160: 69 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 is file. And, ob
2170: 76 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 viously, for thi
2180: 73 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 65 s to be so there
2190: 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 . ** must be a
21a0: 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 n open write tra
21b0: 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 nsaction on the
21c0: 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a file itself.. *
21d0: 2f 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 /. assert( eLoc
21e0: 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
21f0: 28 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 (p==pBt->pWriter
2200: 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d && p->inTrans==
2210: 54 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a TRANS_WRITE) );.
2220: 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
2230: 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 =READ_LOCK || pB
2240: 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
2250: 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
2260: 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f . . /* This ro
2270: 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
2280: 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 if the shared-c
2290: 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 ache is not enab
22a0: 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d led */. if( !p-
22b0: 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
22c0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
22d0: 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 K;. }.. /* If
22e0: 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 some other conne
22f0: 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 ction is holding
2300: 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
2310: 63 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 ck, the. ** req
2320: 75 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 uested lock may
2330: 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e not be obtained.
2340: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d . */. if( pBt-
2350: 3e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 28 >pWriter!=p && (
2360: 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
2370: 42 54 53 5f 45 58 43 4c 55 53 49 56 45 29 21 3d BTS_EXCLUSIVE)!=
2380: 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
2390: 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 ConnectionBlocke
23a0: 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 57 d(p->db, pBt->pW
23b0: 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 20 riter->db);.
23c0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
23d0: 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 CKED_SHAREDCACHE
23e0: 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 74 ;. }.. for(pIt
23f0: 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 er=pBt->pLock; p
2400: 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 Iter; pIter=pIte
2410: 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f r->pNext){. /
2420: 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 * The condition
2430: 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 (pIter->eLock!=e
2440: 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f 6c Lock) in the fol
2450: 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 0a lowing if(...) .
2460: 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
2470: 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 is a simplifica
2480: 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a tion of:. **.
2490: 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 6b 3d ** (eLock=
24a0: 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 20 70 =WRITE_LOCK || p
24b0: 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 Iter->eLock==WRI
24c0: 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a 2a 0a TE_LOCK). **.
24d0: 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 20 ** since we
24e0: 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65 4c 6f know that if eLo
24f0: 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 2c 20 ck==WRITE_LOCK,
2500: 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 63 6f then no other co
2510: 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 nnection. **
2520: 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49 54 45 may hold a WRITE
2530: 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 61 62 _LOCK on any tab
2540: 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 le in this file
2550: 28 73 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e (since there can
2560: 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 . ** only be
2570: 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 29 a single writer)
2580: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
2590: 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 ert( pIter->eLoc
25a0: 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
25b0: 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 pIter->eLock==WR
25c0: 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 ITE_LOCK );.
25d0: 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
25e0: 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 EAD_LOCK || pIte
25f0: 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20 r->pBtree==p ||
2600: 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 pIter->eLock==RE
2610: 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 AD_LOCK);. if
2620: 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 ( pIter->pBtree!
2630: 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54 61 =p && pIter->iTa
2640: 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 49 74 ble==iTab && pIt
2650: 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b er->eLock!=eLock
2660: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
2670: 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
2680: 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 72 2d ed(p->db, pIter-
2690: 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 >pBtree->db);.
26a0: 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d 3d 57 if( eLock==W
26b0: 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 RITE_LOCK ){.
26c0: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d assert( p==
26d0: 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a pBt->pWriter );.
26e0: 20 20 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 pBt->bts
26f0: 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 45 4e Flags |= BTS_PEN
2700: 44 49 4e 47 3b 0a 20 20 20 20 20 20 7d 0a 20 20 DING;. }.
2710: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
2720: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
2730: 41 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ACHE;. }. }.
2740: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
2750: 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 OK;.}.#endif /*
2760: 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 !SQLITE_OMIT_SHA
2770: 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
2780: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
2790: 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
27a0: 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 *.** Add a lock
27b0: 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 on the table wit
27c0: 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 h root-page iTab
27d0: 6c 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 le to the shared
27e0: 2d 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 -btree used.** b
27f0: 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 y Btree handle p
2800: 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 . Parameter eLoc
2810: 6b 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 k must be either
2820: 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a READ_LOCK or .*
2830: 2a 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a * WRITE_LOCK..**
2840: 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
2850: 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 66 6f n assumes the fo
2860: 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
2870: 20 28 61 29 20 54 68 65 20 73 70 65 63 69 66 69 (a) The specifi
2880: 65 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 ed Btree object
2890: 70 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 p is connected t
28a0: 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a 20 o a sharable.**
28b0: 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 database (
28c0: 6f 6e 65 20 77 69 74 68 20 74 68 65 20 42 74 53 one with the BtS
28d0: 68 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20 66 hared.sharable f
28e0: 6c 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a 2a lag set), and.**
28f0: 0a 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 .** (b) No oth
2900: 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 er Btree objects
2910: 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 61 hold a lock tha
2920: 74 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 t conflicts.**
2930: 20 20 20 20 20 77 69 74 68 20 74 68 65 20 72 65 with the re
2940: 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 2e quested lock (i.
2950: 65 2e 20 71 75 65 72 79 53 68 61 72 65 64 43 61 e. querySharedCa
2960: 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 68 cheTableLock() h
2970: 61 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 65 as.** alre
2980: 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 ady been called
2990: 61 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51 4c and returned SQL
29a0: 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 ITE_OK)..**.** S
29b0: 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
29c0: 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b rned if the lock
29d0: 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65 73 is added succes
29e0: 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e sfully. SQLITE_N
29f0: 4f 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 75 OMEM .** is retu
2a00: 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 rned if a malloc
2a10: 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a attempt fails..
2a20: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 */.static int se
2a30: 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
2a40: 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 eLock(Btree *p,
2a50: 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 Pgno iTable, u8
2a60: 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 eLock){. BtShar
2a70: 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
2a80: 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 ;. BtLock *pLoc
2a90: 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 k = 0;. BtLock
2aa0: 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 *pIter;.. asser
2ab0: 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
2ac0: 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
2ad0: 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
2ae0: 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c =READ_LOCK || eL
2af0: 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
2b00: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
2b10: 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 db!=0 );.. /* A
2b20: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 connection with
2b30: 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d the read-uncomm
2b40: 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 20 77 itted flag set w
2b50: 69 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74 6f ill never try to
2b60: 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 72 . ** obtain a r
2b70: 65 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 74 ead-lock using t
2b80: 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 his function. Th
2b90: 65 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 6b e only read-lock
2ba0: 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 62 obtained. ** b
2bb0: 79 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 y a connection i
2bc0: 6e 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 n read-uncommitt
2bd0: 65 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 68 ed mode is on th
2be0: 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
2bf0: 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e 64 . ** table, and
2c00: 20 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f 62 that lock is ob
2c10: 74 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65 42 tained in BtreeB
2c20: 65 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a 2f eginTrans(). */
2c30: 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 . assert( 0==(p
2c40: 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 ->db->flags&SQLI
2c50: 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
2c60: 65 64 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 ed) || eLock==WR
2c70: 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f ITE_LOCK );.. /
2c80: 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
2c90: 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 should only be c
2ca0: 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 61 alled on a shara
2cb0: 62 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 72 ble b-tree after
2cc0: 20 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 65 it . ** has be
2cd0: 65 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 en determined th
2ce0: 61 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 72 at no other b-tr
2cf0: 65 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 6c ee holds a confl
2d00: 69 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f icting lock. */
2d10: 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 . assert( p->sh
2d20: 61 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 arable );. asse
2d30: 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 rt( SQLITE_OK==q
2d40: 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
2d50: 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 ableLock(p, iTab
2d60: 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 le, eLock) );..
2d70: 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 /* First search
2d80: 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e the list for an
2d90: 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f existing lock o
2da0: 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f n this table. */
2db0: 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 . for(pIter=pBt
2dc0: 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 ->pLock; pIter;
2dd0: 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 pIter=pIter->pNe
2de0: 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 xt){. if( pIt
2df0: 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 er->iTable==iTab
2e00: 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74 le && pIter->pBt
2e10: 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
2e20: 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20 pLock = pIter;.
2e30: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2e40: 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
2e50: 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 20 he above search
2e60: 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42 did not find a B
2e70: 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73 tLock struct ass
2e80: 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 70 ociating Btree p
2e90: 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 . ** with table
2ea0: 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 iTable, allocat
2eb0: 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 e one and link i
2ec0: 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e t into the list.
2ed0: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f . */. if( !pLo
2ee0: 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 ck ){. pLock
2ef0: 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 = (BtLock *)sqli
2f00: 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 te3MallocZero(si
2f10: 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 zeof(BtLock));.
2f20: 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b if( !pLock ){
2f30: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
2f40: 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
2f50: 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 }. pLock->iTa
2f60: 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 ble = iTable;.
2f70: 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 pLock->pBtree
2f80: 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e = p;. pLock->
2f90: 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f pNext = pBt->pLo
2fa0: 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f ck;. pBt->pLo
2fb0: 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a ck = pLock;. }.
2fc0: 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74 . /* Set the Bt
2fd0: 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61 Lock.eLock varia
2fe0: 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d ble to the maxim
2ff0: 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e um of the curren
3000: 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 t lock. ** and
3010: 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
3020: 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 ck. This means i
3030: 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 f a write-lock w
3040: 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a as already held.
3050: 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d ** and a read-
3060: 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20 lock requested,
3070: 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65 we don't incorre
3080: 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74 ctly downgrade t
3090: 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 he lock.. */.
30a0: 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f assert( WRITE_LO
30b0: 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a CK>READ_LOCK );.
30c0: 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 if( eLock>pLoc
30d0: 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 k->eLock ){.
30e0: 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 pLock->eLock = e
30f0: 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 Lock;. }.. ret
3100: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
3110: 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 .#endif /* !SQLI
3120: 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
3130: 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ACHE */..#ifndef
3140: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
3150: 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 RED_CACHE./*.**
3160: 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 Release all the
3170: 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 table locks (loc
3180: 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20 ks obtained via
3190: 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 20 calls to.** the
31a0: 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
31b0: 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 64 bleLock() proced
31c0: 75 72 65 29 20 68 65 6c 64 20 62 79 20 42 74 72 ure) held by Btr
31d0: 65 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a ee object p..**.
31e0: 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
31f0: 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 42 74 assumes that Bt
3200: 72 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70 65 ree p has an ope
3210: 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 n read or write
3220: 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e .** transaction.
3230: 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2c If it does not,
3240: 20 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50 45 then the BTS_PE
3250: 4e 44 49 4e 47 20 66 6c 61 67 0a 2a 2a 20 6d 61 NDING flag.** ma
3260: 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 y be incorrectly
3270: 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 cleared..*/.sta
3280: 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 6c tic void clearAl
3290: 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
32a0: 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 eLocks(Btree *p)
32b0: 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
32c0: 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 t = p->pBt;. Bt
32d0: 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d 20 Lock **ppIter =
32e0: 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 &pBt->pLock;..
32f0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
3300: 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
3310: 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
3320: 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 3d ->sharable || 0=
3330: 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 73 =*ppIter );. as
3340: 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 sert( p->inTrans
3350: 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 >0 );.. while(
3360: 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 *ppIter ){. B
3370: 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a tLock *pLock = *
3380: 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 ppIter;. asse
3390: 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 rt( (pBt->btsFla
33a0: 67 73 20 26 20 42 54 53 5f 45 58 43 4c 55 53 49 gs & BTS_EXCLUSI
33b0: 56 45 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 VE)==0 || pBt->p
33c0: 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 Writer==pLock->p
33d0: 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 73 Btree );. ass
33e0: 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 ert( pLock->pBtr
33f0: 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f ee->inTrans>=pLo
3400: 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 ck->eLock );.
3410: 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 if( pLock->pBtr
3420: 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a ee==p ){. *
3430: 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e ppIter = pLock->
3440: 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 pNext;. ass
3450: 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 ert( pLock->iTab
3460: 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d 3d le!=1 || pLock==
3470: 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 &p->lock );.
3480: 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 if( pLock->iTa
3490: 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 ble!=1 ){.
34a0: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
34b0: 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Lock);. }.
34c0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
34d0: 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d ppIter = &pLock-
34e0: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
34f0: 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 }.. assert( (pB
3500: 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
3510: 53 5f 50 45 4e 44 49 4e 47 29 3d 3d 30 20 7c 7c S_PENDING)==0 ||
3520: 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b pBt->pWriter );
3530: 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 . if( pBt->pWri
3540: 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 ter==p ){. pB
3550: 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a t->pWriter = 0;.
3560: 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 pBt->btsFlag
3570: 73 20 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 s &= ~(BTS_EXCLU
3580: 53 49 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 SIVE|BTS_PENDING
3590: 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
35a0: 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e Bt->nTransaction
35b0: 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ==2 ){. /* Th
35c0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
35d0: 61 6c 6c 65 64 20 77 68 65 6e 20 42 74 72 65 65 alled when Btree
35e0: 20 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e 67 p is concluding
35f0: 20 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72 61 its . ** tra
3600: 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 nsaction. If the
3610: 72 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 69 re currently exi
3620: 73 74 73 20 61 20 77 72 69 74 65 72 2c 20 61 6e sts a writer, an
3630: 64 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a d p is not. *
3640: 2a 20 74 68 61 74 20 77 72 69 74 65 72 2c 20 74 * that writer, t
3650: 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hen the number o
3660: 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 f locks held by
3670: 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 65 connections othe
3680: 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 r. ** than th
3690: 65 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 65 e writer must be
36a0: 20 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 74 about to drop t
36b0: 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 o zero. In this
36c0: 63 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 case. ** set
36d0: 74 68 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 the BTS_PENDING
36e0: 66 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 2a flag to 0.. *
36f0: 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 *. ** If ther
3700: 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 e is not current
3710: 6c 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 65 ly a writer, the
3720: 6e 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 6d 75 n BTS_PENDING mu
3730: 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 st. ** be zer
3740: 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 o already. So th
3750: 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 is next line is
3760: 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 harmless in that
3770: 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 case.. */.
3780: 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 pBt->btsFlags
3790: 26 3d 20 7e 42 54 53 5f 50 45 4e 44 49 4e 47 3b &= ~BTS_PENDING;
37a0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
37b0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e is function chan
37c0: 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f ges all write-lo
37d0: 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 65 cks held by Btre
37e0: 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f e p into read-lo
37f0: 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 cks..*/.static v
3800: 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c oid downgradeAll
3810: 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
3820: 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b Locks(Btree *p){
3830: 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
3840: 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 = p->pBt;. if(
3850: 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 pBt->pWriter==p
3860: 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a ){. BtLock *
3870: 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e pLock;. pBt->
3880: 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 pWriter = 0;.
3890: 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 pBt->btsFlags &
38a0: 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 49 56 = ~(BTS_EXCLUSIV
38b0: 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 3b 0a E|BTS_PENDING);.
38c0: 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 for(pLock=pB
38d0: 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b t->pLock; pLock;
38e0: 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e pLock=pLock->pN
38f0: 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ext){. asse
3900: 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b rt( pLock->eLock
3910: 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 ==READ_LOCK || p
3920: 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 Lock->pBtree==p
3930: 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e );. pLock->
3940: 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 eLock = READ_LOC
3950: 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a K;. }. }.}..
3960: 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
3970: 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
3980: 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f HE */..static vo
3990: 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d id releasePage(M
39a0: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 emPage *pPage);
39b0: 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 /* Forward refe
39c0: 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a rence */../*.***
39d0: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
39e0: 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f is used inside o
39f0: 66 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 f assert() only
3a00: 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 ****.**.** Verif
3a10: 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f y that the curso
3a20: 72 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 r holds the mute
3a30: 78 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65 x on its BtShare
3a40: 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 d.*/.#ifdef SQLI
3a50: 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 TE_DEBUG.static
3a60: 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d int cursorHoldsM
3a70: 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 utex(BtCursor *p
3a80: 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
3a90: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
3aa0: 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d ->pBt->mutex);.}
3ab0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
3ac0: 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 nvalidate the ov
3ad0: 65 72 66 6c 6f 77 20 63 61 63 68 65 20 6f 66 20 erflow cache of
3ae0: 74 68 65 20 63 75 72 73 6f 72 20 70 61 73 73 65 the cursor passe
3af0: 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
3b00: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 6f 6e 20 74 rgument..** on t
3b10: 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20 he shared btree
3b20: 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a structure pBt..*
3b30: 2f 0a 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 /.#define invali
3b40: 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 dateOverflowCach
3b50: 65 28 70 43 75 72 29 20 28 70 43 75 72 2d 3e 63 e(pCur) (pCur->c
3b60: 75 72 46 6c 61 67 73 20 26 3d 20 7e 42 54 43 46 urFlags &= ~BTCF
3b70: 5f 56 61 6c 69 64 4f 76 66 6c 29 0a 0a 2f 2a 0a _ValidOvfl)../*.
3b80: 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 ** Invalidate th
3b90: 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
3ba0: 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 list cache for a
3bb0: 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 ll cursors opene
3bc0: 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 d.** on the shar
3bd0: 65 64 20 62 74 72 65 65 20 73 74 72 75 63 74 75 ed btree structu
3be0: 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 re pBt..*/.stati
3bf0: 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 c void invalidat
3c00: 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 eAllOverflowCach
3c10: 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 e(BtShared *pBt)
3c20: 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
3c30: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
3c40: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
3c50: 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 t->mutex) );. f
3c60: 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f or(p=pBt->pCurso
3c70: 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 r; p; p=p->pNext
3c80: 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 ){. invalidat
3c90: 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 eOverflowCache(p
3ca0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 );. }.}..#ifnde
3cb0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
3cc0: 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 54 68 69 CRBLOB./*.** Thi
3cd0: 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
3ce0: 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 lled before modi
3cf0: 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e fying the conten
3d00: 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2a ts of a table.**
3d10: 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 to invalidate a
3d20: 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 ny incrblob curs
3d30: 6f 72 73 20 74 68 61 74 20 61 72 65 20 6f 70 65 ors that are ope
3d40: 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 20 n on the.** row
3d50: 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f or one of the ro
3d60: 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 ws being modifie
3d70: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 d..**.** If argu
3d80: 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c ment isClearTabl
3d90: 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 e is true, then
3da0: 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 the entire conte
3db0: 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 61 nts of the.** ta
3dc0: 62 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f 20 ble is about to
3dd0: 62 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 be deleted. In t
3de0: 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 his case invalid
3df0: 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 ate all incrblob
3e00: 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e .** cursors open
3e10: 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 68 on any row with
3e20: 69 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 in the table wit
3e30: 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e 6f h root-page pgno
3e40: 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 Root..**.** Othe
3e50: 72 77 69 73 65 2c 20 69 66 20 61 72 67 75 6d 65 rwise, if argume
3e60: 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 nt isClearTable
3e70: 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 is false, then t
3e80: 68 65 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 72 he row with.** r
3e90: 6f 77 69 64 20 69 52 6f 77 20 69 73 20 62 65 69 owid iRow is bei
3ea0: 6e 67 20 72 65 70 6c 61 63 65 64 20 6f 72 20 64 ng replaced or d
3eb0: 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 eleted. In this
3ec0: 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 0a case invalidate.
3ed0: 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 6e ** only those in
3ee0: 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f crblob cursors o
3ef0: 70 65 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 63 pen on that spec
3f00: 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 ific row..*/.sta
3f10: 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 tic void invalid
3f20: 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f ateIncrblobCurso
3f30: 72 73 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 rs(. Btree *pBt
3f40: 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ree, /*
3f50: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
3f60: 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 le to check */.
3f70: 20 69 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 i64 iRow,
3f80: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
3f90: 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 rowid that might
3fa0: 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a be changing */.
3fb0: 20 20 69 6e 74 20 69 73 43 6c 65 61 72 54 61 62 int isClearTab
3fc0: 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 le /* Tru
3fd0: 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 61 72 e if all rows ar
3fe0: 65 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 e being deleted
3ff0: 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 */.){. BtCursor
4000: 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64 20 *p;. BtShared
4010: 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 *pBt = pBtree->p
4020: 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 Bt;. assert( sq
4030: 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
4040: 75 74 65 78 28 70 42 74 72 65 65 29 20 29 3b 0a utex(pBtree) );.
4050: 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 for(p=pBt->pCu
4060: 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e rsor; p; p=p->pN
4070: 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 28 70 ext){. if( (p
4080: 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 43 ->curFlags & BTC
4090: 46 5f 49 6e 63 72 62 6c 6f 62 29 21 3d 30 0a 20 F_Incrblob)!=0.
40a0: 20 20 20 20 26 26 20 28 69 73 43 6c 65 61 72 54 && (isClearT
40b0: 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e able || p->info.
40c0: 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 20 20 nKey==iRow).
40d0: 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 74 61 ){. p->eSta
40e0: 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
40f0: 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d LID;. }. }.}
4100: 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 53 74 75 ..#else. /* Stu
4110: 62 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 6e 20 b function when
4120: 49 4e 43 52 42 4c 4f 42 20 69 73 20 6f 6d 69 74 INCRBLOB is omit
4130: 74 65 64 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 ted */. #define
4140: 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 invalidateIncrb
4150: 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 2c 7a lobCursors(x,y,z
4160: 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ).#endif /* SQLI
4170: 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
4180: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 */../*.** Set b
4190: 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 it pgno of the B
41a0: 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
41b0: 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 ent bitvec. This
41c0: 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 is called .** w
41d0: 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 hen a page that
41e0: 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 previously conta
41f0: 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 ined data become
4200: 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 s a free-list le
4210: 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a af .** page..**.
4220: 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e ** The BtShared.
4230: 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
4240: 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 ec exists to wor
4250: 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 k around an obsc
4260: 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 ure.** bug cause
4270: 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 d by the interac
4280: 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 tion of two usef
4290: 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 ul IO optimizati
42a0: 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a ons surrounding.
42b0: 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 ** free-list lea
42c0: 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 f pages:.**.**
42d0: 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 1) When all dat
42e0: 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f a is deleted fro
42f0: 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 m a page and the
4300: 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a page becomes.**
4310: 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 a free-lis
4320: 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 t leaf page, the
4330: 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 page is not wri
4340: 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
4350: 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 base.** (as
4360: 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
4370: 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f pages contain no
4380: 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 meaningful data
4390: 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ). Sometimes.**
43a0: 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65 such a page
43b0: 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 is not even jou
43c0: 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 rnalled (as it w
43d0: 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 ill not be modif
43e0: 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 ied,.** why
43f0: 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c bother journall
4400: 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 ing it?)..**.**
4410: 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2) When a free
4420: 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
4430: 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 is reused, its c
4440: 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 ontent is not re
4450: 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 ad.** from
4460: 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
4470: 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
4480: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 ournal file (why
4490: 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 should it.**
44a0: 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 be, if it is
44b0: 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 not at all meani
44c0: 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 ngful?)..**.** B
44d0: 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 y themselves, th
44e0: 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ese optimization
44f0: 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 s work fine and
4500: 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a provide a handy.
4510: 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 ** performance b
4520: 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c oost to bulk del
4530: 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 ete or insert op
4540: 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 erations. Howeve
4550: 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 r, if.** a page
4560: 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
4570: 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 free-list and th
4580: 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e en reused within
4590: 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 the same.** tra
45a0: 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 nsaction, a prob
45b0: 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 lem comes up. If
45c0: 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
45d0: 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e journalled when
45e0: 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 .** it is moved
45f0: 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 to the free-list
4600: 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 and it is also
4610: 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 not journalled w
4620: 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 hen it.** is ext
4630: 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 racted from the
4640: 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 free-list and re
4650: 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f used, then the o
4660: 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 riginal data.**
4670: 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 may be lost. In
4680: 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 the event of a r
4690: 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 ollback, it may
46a0: 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a not be possible.
46b0: 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ** to restore th
46c0: 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
46d0: 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 s original confi
46e0: 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 guration..**.**
46f0: 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
4700: 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
4710: 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e sContent bitvec.
4720: 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 Whenever a page
4730: 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f is .** moved to
4740: 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c become a free-l
4750: 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
4760: 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
4770: 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 bit is.** set i
4780: 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 n the bitvec. Wh
4790: 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 enever a leaf pa
47a0: 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ge is extracted
47b0: 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
47c0: 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 st,.** optimizat
47d0: 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f ion 2 above is o
47e0: 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63 6f mitted if the co
47f0: 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 rresponding bit
4800: 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 65 is already.** se
4810: 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 48 t in BtShared.pH
4820: 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 63 asContent. The c
4830: 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 ontents of the b
4840: 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 65 itvec are cleare
4850: 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 d.** at the end
4860: 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 63 of every transac
4870: 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
4880: 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 43 int btreeSetHasC
4890: 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 ontent(BtShared
48a0: 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
48b0: 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
48c0: 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 ITE_OK;. if( !p
48d0: 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
48e0: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
48f0: 67 6e 6f 3c 3d 70 42 74 2d 3e 6e 50 61 67 65 20 gno<=pBt->nPage
4900: 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 );. pBt->pHas
4910: 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 Content = sqlite
4920: 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 42 3BitvecCreate(pB
4930: 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 t->nPage);. i
4940: 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e f( !pBt->pHasCon
4950: 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 tent ){. rc
4960: 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
4970: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
4980: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
4990: 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 & pgno<=sqlite3B
49a0: 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 itvecSize(pBt->p
49b0: 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 HasContent) ){.
49c0: 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
49d0: 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 itvecSet(pBt->pH
49e0: 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 asContent, pgno)
49f0: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
4a00: 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 c;.}../*.** Quer
4a10: 79 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 y the BtShared.p
4a20: 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f HasContent vecto
4a30: 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 r..**.** This fu
4a40: 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
4a50: 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 when a free-lis
4a60: 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 t leaf page is r
4a70: 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a emoved from the.
4a80: 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 ** free-list for
4a90: 20 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 reuse. It retur
4aa0: 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 ns false if it i
4ab0: 73 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 s safe to retrie
4ac0: 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 ve the.** page f
4ad0: 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 rom the pager la
4ae0: 79 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f yer with the 'no
4af0: 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 -content' flag s
4b00: 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 et. True otherwi
4b10: 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e se..*/.static in
4b20: 74 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e t btreeGetHasCon
4b30: 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 tent(BtShared *p
4b40: 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
4b50: 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 Bitvec *p = pB
4b60: 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a t->pHasContent;.
4b70: 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 return (p && (
4b80: 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 pgno>sqlite3Bitv
4b90: 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c ecSize(p) || sql
4ba0: 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
4bb0: 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a , pgno)));.}../*
4bc0: 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 .** Clear (destr
4bd0: 6f 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 oy) the BtShared
4be0: 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
4bf0: 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 vec. This should
4c00: 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 be.** invoked a
4c10: 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e t the conclusion
4c20: 20 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 of each write-t
4c30: 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
4c40: 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
4c50: 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 ClearHasContent(
4c60: 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
4c70: 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
4c80: 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 estroy(pBt->pHas
4c90: 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d Content);. pBt-
4ca0: 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 >pHasContent = 0
4cb0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
4cc0: 73 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 70 se all of the ap
4cd0: 50 61 67 65 5b 5d 20 70 61 67 65 73 20 66 6f 72 Page[] pages for
4ce0: 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 a cursor..*/.st
4cf0: 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 52 atic void btreeR
4d00: 65 6c 65 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 eleaseAllCursorP
4d10: 61 67 65 73 28 42 74 43 75 72 73 6f 72 20 2a 70 ages(BtCursor *p
4d20: 43 75 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 Cur){. int i;.
4d30: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 for(i=0; i<=pCu
4d40: 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a r->iPage; i++){.
4d50: 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
4d60: 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 pCur->apPage[i])
4d70: 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 ;. pCur->apPa
4d80: 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 ge[i] = 0;. }.
4d90: 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d pCur->iPage = -
4da0: 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 1;.}.../*.** Sav
4db0: 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 e the current cu
4dc0: 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e rsor position in
4dd0: 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42 the variables B
4de0: 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a tCursor.nKey .**
4df0: 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b and BtCursor.pK
4e00: 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73 ey. The cursor's
4e10: 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f state is set to
4e20: 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
4e30: 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 EEK..**.** The c
4e40: 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 aller must ensur
4e50: 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
4e60: 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73 20 r is valid (has
4e70: 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
4e80: 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 ALID).** prior t
4e90: 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 o calling this r
4ea0: 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 outine. .*/.sta
4eb0: 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 tic int saveCurs
4ec0: 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 orPosition(BtCur
4ed0: 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e sor *pCur){. in
4ee0: 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
4ef0: 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 CURSOR_VALID==p
4f00: 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 Cur->eState );.
4f10: 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 assert( 0==pCur
4f20: 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 ->pKey );. asse
4f30: 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
4f40: 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 utex(pCur) );..
4f50: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
4f60: 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 eeKeySize(pCur,
4f70: 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 &pCur->nKey);.
4f80: 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
4f90: 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 TE_OK ); /* Key
4fa0: 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 Size() cannot fa
4fb0: 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 il */.. /* If t
4fc0: 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 his is an intKey
4fd0: 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 table, then the
4fe0: 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 above call to B
4ff0: 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 treeKeySize().
5000: 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e ** stores the in
5010: 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 teger key in pCu
5020: 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 r->nKey. In this
5030: 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 case this value
5040: 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 is. ** all tha
5050: 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f t is required. O
5060: 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 therwise, if pCu
5070: 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e r is not open on
5080: 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 an intKey. **
5090: 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c table, then mall
50a0: 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 oc space for and
50b0: 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d store the pCur-
50c0: 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b >nKey bytes of k
50d0: 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 ey . ** data..
50e0: 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 */. if( 0==pCu
50f0: 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
5100: 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 tKey ){. void
5110: 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 *pKey = sqlite3
5120: 4d 61 6c 6c 6f 63 28 20 70 43 75 72 2d 3e 6e 4b Malloc( pCur->nK
5130: 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4b ey );. if( pK
5140: 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ey ){. rc =
5150: 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
5160: 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 70 (pCur, 0, (int)p
5170: 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 Cur->nKey, pKey)
5180: 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
5190: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
51a0: 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 pCur->pKey
51b0: 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 = pKey;. }e
51c0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
51d0: 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29 3b ite3_free(pKey);
51e0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
51f0: 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
5200: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
5210: 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
5220: 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 !pCur->apPage[0
5230: 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43 ]->intKey || !pC
5240: 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69 ur->pKey );.. i
5250: 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
5260: 20 29 7b 0a 20 20 20 20 62 74 72 65 65 52 65 6c ){. btreeRel
5270: 65 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 easeAllCursorPag
5280: 65 73 28 70 43 75 72 29 3b 0a 20 20 20 20 70 43 es(pCur);. pC
5290: 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
52a0: 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b SOR_REQUIRESEEK;
52b0: 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 . }.. invalida
52c0: 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
52d0: 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 pCur);. return
52e0: 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 rc;.}../* Forwar
52f0: 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 d reference */.s
5300: 74 61 74 69 63 20 69 6e 74 20 53 51 4c 49 54 45 tatic int SQLITE
5310: 5f 4e 4f 49 4e 4c 49 4e 45 20 73 61 76 65 43 75 _NOINLINE saveCu
5320: 72 73 6f 72 73 4f 6e 4c 69 73 74 28 42 74 43 75 rsorsOnList(BtCu
5330: 72 73 6f 72 2a 2c 50 67 6e 6f 2c 42 74 43 75 72 rsor*,Pgno,BtCur
5340: 73 6f 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 61 sor*);../*.** Sa
5350: 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 ve the positions
5360: 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 of all cursors
5370: 28 65 78 63 65 70 74 20 70 45 78 63 65 70 74 29 (except pExcept)
5380: 20 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20 6f that are open o
5390: 6e 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 77 n.** the table w
53a0: 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 ith root-page iR
53b0: 6f 6f 74 2e 20 20 22 53 61 76 69 6e 67 20 74 68 oot. "Saving th
53c0: 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f e cursor positio
53d0: 6e 22 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a n" means that.**
53e0: 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 69 6e the location in
53f0: 20 74 68 65 20 62 74 72 65 65 20 69 73 20 72 65 the btree is re
5400: 6d 65 6d 62 65 72 65 64 20 69 6e 20 73 75 63 68 membered in such
5410: 20 61 20 77 61 79 20 74 68 61 74 20 69 74 20 63 a way that it c
5420: 61 6e 20 62 65 0a 2a 2a 20 6d 6f 76 65 64 20 62 an be.** moved b
5430: 61 63 6b 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ack to the same
5440: 73 70 6f 74 20 61 66 74 65 72 20 74 68 65 20 62 spot after the b
5450: 74 72 65 65 20 68 61 73 20 62 65 65 6e 20 6d 6f tree has been mo
5460: 64 69 66 69 65 64 2e 20 20 54 68 69 73 0a 2a 2a dified. This.**
5470: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
5480: 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 ed just before c
5490: 75 72 73 6f 72 20 70 45 78 63 65 70 74 20 69 73 ursor pExcept is
54a0: 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20 used to modify
54b0: 74 68 65 0a 2a 2a 20 74 61 62 6c 65 2c 20 66 6f the.** table, fo
54c0: 72 20 65 78 61 6d 70 6c 65 20 69 6e 20 42 74 72 r example in Btr
54d0: 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 eeDelete() or Bt
54e0: 72 65 65 49 6e 73 65 72 74 28 29 2e 0a 2a 2a 0a reeInsert()..**.
54f0: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
5500: 6e 20 6e 6f 74 65 3a 20 20 54 68 69 73 20 72 6f n note: This ro
5510: 75 74 69 6e 65 20 6d 65 72 65 6c 79 20 63 68 65 utine merely che
5520: 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e cks to see if an
5530: 79 20 63 75 72 73 6f 72 73 0a 2a 2a 20 6e 65 65 y cursors.** nee
5540: 64 20 74 6f 20 62 65 20 73 61 76 65 64 2e 20 20 d to be saved.
5550: 49 74 20 63 61 6c 6c 73 20 6f 75 74 20 74 6f 20 It calls out to
5560: 73 61 76 65 43 75 72 73 6f 72 73 4f 6e 4c 69 73 saveCursorsOnLis
5570: 74 28 29 20 69 6e 20 74 68 65 20 28 75 6e 75 73 t() in the (unus
5580: 75 61 6c 29 0a 2a 2a 20 65 76 65 6e 74 20 74 68 ual).** event th
5590: 61 74 20 63 75 72 73 6f 72 73 20 61 72 65 20 69 at cursors are i
55a0: 6e 20 6e 65 65 64 20 74 6f 20 62 65 69 6e 67 20 n need to being
55b0: 73 61 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 saved..*/.static
55c0: 20 69 6e 74 20 73 61 76 65 41 6c 6c 43 75 72 73 int saveAllCurs
55d0: 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 ors(BtShared *pB
55e0: 74 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 t, Pgno iRoot, B
55f0: 74 43 75 72 73 6f 72 20 2a 70 45 78 63 65 70 74 tCursor *pExcept
5600: 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
5610: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
5620: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
5630: 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
5640: 61 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d assert( pExcept=
5650: 3d 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70 =0 || pExcept->p
5660: 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72 Bt==pBt );. for
5670: 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b (p=pBt->pCursor;
5680: 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
5690: 0a 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63 . if( p!=pExc
56a0: 65 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 ept && (0==iRoot
56b0: 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d || p->pgnoRoot=
56c0: 3d 69 52 6f 6f 74 29 20 29 20 62 72 65 61 6b 3b =iRoot) ) break;
56d0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 20 . }. return p
56e0: 3f 20 73 61 76 65 43 75 72 73 6f 72 73 4f 6e 4c ? saveCursorsOnL
56f0: 69 73 74 28 70 2c 20 69 52 6f 6f 74 2c 20 70 45 ist(p, iRoot, pE
5700: 78 63 65 70 74 29 20 3a 20 53 51 4c 49 54 45 5f xcept) : SQLITE_
5710: 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 68 OK;.}../* This h
5720: 65 6c 70 65 72 20 72 6f 75 74 69 6e 65 20 74 6f elper routine to
5730: 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 20 saveAllCursors
5740: 64 6f 65 73 20 74 68 65 20 61 63 74 75 61 6c 20 does the actual
5750: 77 6f 72 6b 20 6f 66 20 73 61 76 69 6e 67 0a 2a work of saving.*
5760: 2a 20 74 68 65 20 63 75 72 73 6f 72 73 20 69 66 * the cursors if
5770: 20 61 6e 64 20 77 68 65 6e 20 61 20 63 75 72 73 and when a curs
5780: 6f 72 20 69 73 20 66 6f 75 6e 64 20 74 68 61 74 or is found that
5790: 20 61 63 74 75 61 6c 6c 79 20 72 65 71 75 69 72 actually requir
57a0: 65 73 20 73 61 76 69 6e 67 2e 0a 2a 2a 20 54 68 es saving..** Th
57b0: 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 69 73 e common case is
57c0: 20 74 68 61 74 20 6e 6f 20 63 75 72 73 6f 72 73 that no cursors
57d0: 20 6e 65 65 64 20 74 6f 20 62 65 20 73 61 76 65 need to be save
57e0: 64 2c 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 d, so this routi
57f0: 6e 65 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e 20 ne is.** broken
5800: 6f 75 74 20 66 72 6f 6d 20 69 74 73 20 63 61 6c out from its cal
5810: 6c 65 72 20 74 6f 20 61 76 6f 69 64 20 75 6e 6e ler to avoid unn
5820: 65 63 65 73 73 61 72 79 20 73 74 61 63 6b 20 70 ecessary stack p
5830: 6f 69 6e 74 65 72 20 6d 6f 76 65 6d 65 6e 74 2e ointer movement.
5840: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 53 .*/.static int S
5850: 51 4c 49 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 73 QLITE_NOINLINE s
5860: 61 76 65 43 75 72 73 6f 72 73 4f 6e 4c 69 73 74 aveCursorsOnList
5870: 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 2c (. BtCursor *p,
5880: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
5890: 66 69 72 73 74 20 63 75 72 73 6f 72 20 74 68 61 first cursor tha
58a0: 74 20 6e 65 65 64 73 20 73 61 76 69 6e 67 20 2a t needs saving *
58b0: 2f 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 /. Pgno iRoot,
58c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 /* Only
58d0: 20 73 61 76 65 20 63 75 72 73 6f 72 20 77 69 74 save cursor wit
58e0: 68 20 74 68 69 73 20 69 52 6f 6f 74 2e 20 53 61 h this iRoot. Sa
58f0: 76 65 20 61 6c 6c 20 69 66 20 7a 65 72 6f 20 2a ve all if zero *
5900: 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 45 /. BtCursor *pE
5910: 78 63 65 70 74 20 20 20 20 2f 2a 20 44 6f 20 6e xcept /* Do n
5920: 6f 74 20 73 61 76 65 20 74 68 69 73 20 63 75 72 ot save this cur
5930: 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 64 6f 7b 0a sor */.){. do{.
5940: 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63 65 if( p!=pExce
5950: 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 pt && (0==iRoot
5960: 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d || p->pgnoRoot==
5970: 69 52 6f 6f 74 29 20 29 7b 0a 20 20 20 20 20 20 iRoot) ){.
5980: 69 66 28 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 if( p->eState==C
5990: 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 URSOR_VALID ){.
59a0: 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 int rc =
59b0: 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 saveCursorPositi
59c0: 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 on(p);. i
59d0: 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 f( SQLITE_OK!=rc
59e0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
59f0: 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
5a00: 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
5a10: 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
5a20: 28 20 70 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a ( p->iPage>0 );.
5a30: 20 20 20 20 20 20 20 20 62 74 72 65 65 52 65 6c btreeRel
5a40: 65 61 73 65 41 6c 6c 43 75 72 73 6f 72 50 61 67 easeAllCursorPag
5a50: 65 73 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 es(p);. }.
5a60: 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e }. p = p->
5a70: 70 4e 65 78 74 3b 0a 20 20 7d 77 68 69 6c 65 28 pNext;. }while(
5a80: 20 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 p );. return S
5a90: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
5aa0: 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 75 72 ** Clear the cur
5ab0: 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 rent cursor posi
5ac0: 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tion..*/.void sq
5ad0: 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 lite3BtreeClearC
5ae0: 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a ursor(BtCursor *
5af0: 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 pCur){. assert(
5b00: 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
5b10: 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c x(pCur) );. sql
5b20: 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e ite3_free(pCur->
5b30: 70 4b 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 pKey);. pCur->p
5b40: 4b 65 79 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d Key = 0;. pCur-
5b50: 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
5b60: 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a _INVALID;.}../*.
5b70: 2a 2a 20 49 6e 20 74 68 69 73 20 76 65 72 73 69 ** In this versi
5b80: 6f 6e 20 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 on of BtreeMovet
5b90: 6f 2c 20 70 4b 65 79 20 69 73 20 61 20 70 61 63 o, pKey is a pac
5ba0: 6b 65 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 ked index record
5bb0: 0a 2a 2a 20 73 75 63 68 20 61 73 20 69 73 20 67 .** such as is g
5bc0: 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 enerated by the
5bd0: 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 OP_MakeRecord op
5be0: 63 6f 64 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 code. Unpack th
5bf0: 65 0a 2a 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 e.** record and
5c00: 74 68 65 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d then call BtreeM
5c10: 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 ovetoUnpacked()
5c20: 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a to do the work..
5c30: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
5c40: 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 reeMoveto(. BtC
5c50: 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 ursor *pCur,
5c60: 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 /* Cursor open
5c70: 6f 6e 20 74 68 65 20 62 74 72 65 65 20 74 6f 20 on the btree to
5c80: 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 be searched */.
5c90: 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
5ca0: 79 2c 20 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b y, /* Packed k
5cb0: 65 79 20 69 66 20 74 68 65 20 62 74 72 65 65 20 ey if the btree
5cc0: 69 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 is an index */.
5cd0: 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 i64 nKey,
5ce0: 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
5cf0: 6b 65 79 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 key for tables.
5d00: 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f Size of pKey fo
5d10: 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 r indices */. i
5d20: 6e 74 20 62 69 61 73 2c 20 20 20 20 20 20 20 20 nt bias,
5d30: 20 20 20 2f 2a 20 42 69 61 73 20 73 65 61 72 63 /* Bias searc
5d40: 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e h to the high en
5d50: 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 d */. int *pRes
5d60: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
5d70: 69 74 65 20 73 65 61 72 63 68 20 72 65 73 75 6c ite search resul
5d80: 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ts here */.){.
5d90: 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
5da0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
5db0: 61 74 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 atus code */. U
5dc0: 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
5dd0: 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 IdxKey; /* Unp
5de0: 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 acked index key
5df0: 2a 2f 0a 20 20 63 68 61 72 20 61 53 70 61 63 65 */. char aSpace
5e00: 5b 32 30 30 5d 3b 20 20 20 20 20 20 20 20 20 20 [200];
5e10: 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 20 66 6f /* Temp space fo
5e20: 72 20 70 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 r pIdxKey - to a
5e30: 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f void a malloc */
5e40: 0a 20 20 63 68 61 72 20 2a 70 46 72 65 65 20 3d . char *pFree =
5e50: 20 30 3b 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 0;.. if( pKey
5e60: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e ){. assert( n
5e70: 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e 74 29 6e Key==(i64)(int)n
5e80: 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 64 78 4b Key );. pIdxK
5e90: 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 ey = sqlite3Vdbe
5ea0: 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 52 65 63 AllocUnpackedRec
5eb0: 6f 72 64 28 0a 20 20 20 20 20 20 20 20 70 43 75 ord(. pCu
5ec0: 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 61 53 70 r->pKeyInfo, aSp
5ed0: 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 ace, sizeof(aSpa
5ee0: 63 65 29 2c 20 26 70 46 72 65 65 0a 20 20 20 20 ce), &pFree.
5ef0: 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b );. if( pIdxK
5f00: 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 ey==0 ) return S
5f10: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
5f20: 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f sqlite3VdbeReco
5f30: 72 64 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 rdUnpack(pCur->p
5f40: 4b 65 79 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b KeyInfo, (int)nK
5f50: 65 79 2c 20 70 4b 65 79 2c 20 70 49 64 78 4b 65 ey, pKey, pIdxKe
5f60: 79 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 y);. if( pIdx
5f70: 4b 65 79 2d 3e 6e 46 69 65 6c 64 3d 3d 30 20 29 Key->nField==0 )
5f80: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
5f90: 62 46 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 bFree(pCur->pKey
5fa0: 49 6e 66 6f 2d 3e 64 62 2c 20 70 46 72 65 65 29 Info->db, pFree)
5fb0: 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
5fc0: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
5fd0: 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 PT;. }. }els
5fe0: 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d e{. pIdxKey =
5ff0: 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 0;. }. rc = s
6000: 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
6010: 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20 oUnpacked(pCur,
6020: 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 pIdxKey, nKey, b
6030: 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 ias, pRes);. if
6040: 28 20 70 46 72 65 65 20 29 7b 0a 20 20 20 20 73 ( pFree ){. s
6050: 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 43 75 qlite3DbFree(pCu
6060: 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c r->pKeyInfo->db,
6070: 20 70 46 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 pFree);. }. r
6080: 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
6090: 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 63 ** Restore the c
60a0: 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f 73 ursor to the pos
60b0: 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e 20 ition it was in
60c0: 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f 20 (or as close to
60d0: 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a 20 as possible).**
60e0: 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72 50 when saveCursorP
60f0: 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63 61 osition() was ca
6100: 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 lled. Note that
6110: 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74 65 this call delete
6120: 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64 20 s the .** saved
6130: 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73 74 position info st
6140: 6f 72 65 64 20 62 79 20 73 61 76 65 43 75 72 73 ored by saveCurs
6150: 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73 6f orPosition(), so
6160: 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a there can be.**
6170: 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66 66 at most one eff
6180: 65 63 74 69 76 65 20 72 65 73 74 6f 72 65 43 75 ective restoreCu
6190: 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 rsorPosition() c
61a0: 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20 0a all after each .
61b0: 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 ** saveCursorPos
61c0: 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 ition()..*/.stat
61d0: 69 63 20 69 6e 74 20 62 74 72 65 65 52 65 73 74 ic int btreeRest
61e0: 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
61f0: 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 n(BtCursor *pCur
6200: 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
6210: 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
6220: 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
6230: 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
6240: 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f >eState>=CURSOR_
6250: 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 REQUIRESEEK );.
6260: 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
6270: 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e==CURSOR_FAULT
6280: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 ){. return pC
6290: 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 ur->skipNext;.
62a0: 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 }. pCur->eState
62b0: 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
62c0: 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 4d D;. rc = btreeM
62d0: 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 72 oveto(pCur, pCur
62e0: 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b ->pKey, pCur->nK
62f0: 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b ey, 0, &pCur->sk
6300: 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 72 ipNext);. if( r
6310: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
6320: 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
6330: 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 (pCur->pKey);.
6340: 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 pCur->pKey = 0
6350: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
6360: 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
6370: 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 OR_VALID || pCur
6380: 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
6390: 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 _INVALID );.
63a0: 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 if( pCur->skipNe
63b0: 78 74 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 xt && pCur->eSta
63c0: 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
63d0: 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e ){. pCur->
63e0: 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
63f0: 53 4b 49 50 4e 45 58 54 3b 0a 20 20 20 20 7d 0a SKIPNEXT;. }.
6400: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
6410: 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 .}..#define rest
6420: 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
6430: 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 n(p) \. (p->eSt
6440: 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 ate>=CURSOR_REQU
6450: 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 IRESEEK ? \.
6460: 20 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 btreeRestor
6470: 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
6480: 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 p) : \.
6490: 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a SQLITE_OK)../*.*
64a0: 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 * Determine whet
64b0: 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 her or not a cur
64c0: 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 sor has moved fr
64d0: 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 om the position
64e0: 77 68 65 72 65 0a 2a 2a 20 69 74 20 77 61 73 20 where.** it was
64f0: 6c 61 73 74 20 70 6c 61 63 65 64 2c 20 6f 72 20 last placed, or
6500: 68 61 73 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 has been invalid
6510: 61 74 65 64 20 66 6f 72 20 61 6e 79 20 6f 74 68 ated for any oth
6520: 65 72 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 43 75 er reason..** Cu
6530: 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 rsors can move w
6540: 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 hen the row they
6550: 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 are pointing at
6560: 20 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 0a is deleted out.
6570: 2a 2a 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 ** from under th
6580: 65 6d 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e em, for example.
6590: 20 20 43 75 72 73 6f 72 20 6d 69 67 68 74 20 61 Cursor might a
65a0: 6c 73 6f 20 6d 6f 76 65 20 69 66 20 61 20 62 74 lso move if a bt
65b0: 72 65 65 0a 2a 2a 20 69 73 20 72 65 62 61 6c 61 ree.** is rebala
65c0: 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c nced..**.** Call
65d0: 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
65e0: 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63 75 72 with a NULL cur
65f0: 73 6f 72 20 70 6f 69 6e 74 65 72 20 72 65 74 75 sor pointer retu
6600: 72 6e 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a rns false..**.**
6610: 20 55 73 65 20 74 68 65 20 73 65 70 61 72 61 74 Use the separat
6620: 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 e sqlite3BtreeCu
6630: 72 73 6f 72 52 65 73 74 6f 72 65 28 29 20 72 6f rsorRestore() ro
6640: 75 74 69 6e 65 20 74 6f 20 72 65 73 74 6f 72 65 utine to restore
6650: 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 62 61 63 a cursor.** bac
6660: 6b 20 74 6f 20 77 68 65 72 65 20 69 74 20 6f 75 k to where it ou
6670: 67 68 74 20 74 6f 20 62 65 20 69 66 20 74 68 69 ght to be if thi
6680: 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
6690: 73 20 74 72 75 65 2e 0a 2a 2f 0a 69 6e 74 20 73 s true..*/.int s
66a0: 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
66b0: 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 rHasMoved(BtCurs
66c0: 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 or *pCur){. ret
66d0: 75 72 6e 20 70 43 75 72 2d 3e 65 53 74 61 74 65 urn pCur->eState
66e0: 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a !=CURSOR_VALID;.
66f0: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
6700: 75 74 69 6e 65 20 72 65 73 74 6f 72 65 73 20 61 utine restores a
6710: 20 63 75 72 73 6f 72 20 62 61 63 6b 20 74 6f 20 cursor back to
6720: 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 70 6f 73 its original pos
6730: 69 74 69 6f 6e 20 61 66 74 65 72 20 69 74 0a 2a ition after it.*
6740: 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 76 65 64 * has been moved
6750: 20 62 79 20 73 6f 6d 65 20 6f 75 74 73 69 64 65 by some outside
6760: 20 61 63 74 69 76 69 74 79 20 28 73 75 63 68 20 activity (such
6770: 61 73 20 61 20 62 74 72 65 65 20 72 65 62 61 6c as a btree rebal
6780: 61 6e 63 65 20 6f 72 0a 2a 2a 20 61 20 72 6f 77 ance or.** a row
6790: 20 68 61 76 69 6e 67 20 62 65 65 6e 20 64 65 6c having been del
67a0: 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e eted out from un
67b0: 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 29 2e der the cursor).
67c0: 20 20 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 .**.** On succ
67d0: 65 73 73 2c 20 74 68 65 20 2a 70 44 69 66 66 65 ess, the *pDiffe
67e0: 72 65 6e 74 52 6f 77 20 70 61 72 61 6d 65 74 65 rentRow paramete
67f0: 72 20 69 73 20 66 61 6c 73 65 20 69 66 20 74 68 r is false if th
6800: 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 e cursor is left
6810: 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 .** pointing at
6820: 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 exactly the same
6830: 20 72 6f 77 2e 20 20 2a 70 44 69 66 66 65 72 6e row. *pDiffern
6840: 74 52 6f 77 20 69 73 20 74 68 65 20 72 6f 77 20 tRow is the row
6850: 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 the cursor.** wa
6860: 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 68 61 s pointing to ha
6870: 73 20 62 65 65 6e 20 64 65 6c 65 74 65 64 2c 20 s been deleted,
6880: 66 6f 72 63 69 6e 67 20 74 68 65 20 63 75 72 73 forcing the curs
6890: 6f 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 73 or to point to s
68a0: 6f 6d 65 0a 2a 2a 20 6e 65 61 72 62 79 20 72 6f ome.** nearby ro
68b0: 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f w..**.** This ro
68c0: 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c utine should onl
68d0: 79 20 62 65 20 63 61 6c 6c 65 64 20 66 6f 72 20 y be called for
68e0: 61 20 63 75 72 73 6f 72 20 74 68 61 74 20 6a 75 a cursor that ju
68f0: 73 74 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 54 st returned.** T
6900: 52 55 45 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 RUE from sqlite3
6910: 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f BtreeCursorHasMo
6920: 76 65 64 28 29 2e 0a 2a 2f 0a 69 6e 74 20 73 71 ved()..*/.int sq
6930: 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
6940: 52 65 73 74 6f 72 65 28 42 74 43 75 72 73 6f 72 Restore(BtCursor
6950: 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 44 69 *pCur, int *pDi
6960: 66 66 65 72 65 6e 74 52 6f 77 29 7b 0a 20 20 69 fferentRow){. i
6970: 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
6980: 28 20 70 43 75 72 21 3d 30 20 29 3b 0a 20 20 61 ( pCur!=0 );. a
6990: 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
69a0: 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
69b0: 44 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 D );. rc = rest
69c0: 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
69d0: 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
69e0: 63 20 29 7b 0a 20 20 20 20 2a 70 44 69 66 66 65 c ){. *pDiffe
69f0: 72 65 6e 74 52 6f 77 20 3d 20 31 3b 0a 20 20 20 rentRow = 1;.
6a00: 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
6a10: 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
6a20: 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te!=CURSOR_VALID
6a30: 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e || NEVER(pCur->
6a40: 73 6b 69 70 4e 65 78 74 21 3d 30 29 20 29 7b 0a skipNext!=0) ){.
6a50: 20 20 20 20 2a 70 44 69 66 66 65 72 65 6e 74 52 *pDifferentR
6a60: 6f 77 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ow = 1;. }else{
6a70: 0a 20 20 20 20 2a 70 44 69 66 66 65 72 65 6e 74 . *pDifferent
6a80: 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 Row = 0;. }. r
6a90: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
6aa0: 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
6ab0: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
6ac0: 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 UM./*.** Given a
6ad0: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
6ae0: 61 20 72 65 67 75 6c 61 72 20 64 61 74 61 62 61 a regular databa
6af0: 73 65 20 70 61 67 65 2c 20 72 65 74 75 72 6e 20 se page, return
6b00: 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 the page.** numb
6b10: 65 72 20 66 6f 72 20 74 68 65 20 70 6f 69 6e 74 er for the point
6b20: 65 72 2d 6d 61 70 20 70 61 67 65 20 74 68 61 74 er-map page that
6b30: 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e contains the en
6b40: 74 72 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 69 try for the.** i
6b50: 6e 70 75 74 20 70 61 67 65 20 6e 75 6d 62 65 72 nput page number
6b60: 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 ..**.** Return 0
6b70: 20 28 6e 6f 74 20 61 20 76 61 6c 69 64 20 70 61 (not a valid pa
6b80: 67 65 29 20 66 6f 72 20 70 67 6e 6f 3d 3d 31 20 ge) for pgno==1
6b90: 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 0a 2a since there is.*
6ba0: 2a 20 6e 6f 20 70 6f 69 6e 74 65 72 20 6d 61 70 * no pointer map
6bb0: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
6bc0: 20 70 61 67 65 20 31 2e 20 20 54 68 65 20 69 6e page 1. The in
6bd0: 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 6c 6f tegrity_check lo
6be0: 67 69 63 0a 2a 2a 20 72 65 71 75 69 72 65 73 20 gic.** requires
6bf0: 74 68 61 74 20 70 74 72 6d 61 70 50 61 67 65 6e that ptrmapPagen
6c00: 6f 28 2a 2c 31 29 21 3d 31 2e 0a 2a 2f 0a 73 74 o(*,1)!=1..*/.st
6c10: 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 atic Pgno ptrmap
6c20: 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 Pageno(BtShared
6c30: 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
6c40: 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 {. int nPagesPe
6c50: 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f rMapPage;. Pgno
6c60: 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 iPtrMap, ret;.
6c70: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
6c80: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
6c90: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
6ca0: 20 70 67 6e 6f 3c 32 20 29 20 72 65 74 75 72 6e pgno<2 ) return
6cb0: 20 30 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 0;. nPagesPerM
6cc0: 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 apPage = (pBt->u
6cd0: 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a sableSize/5)+1;.
6ce0: 20 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e iPtrMap = (pgn
6cf0: 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 o-2)/nPagesPerMa
6d00: 70 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 pPage;. ret = (
6d10: 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 iPtrMap*nPagesPe
6d20: 72 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a rMapPage) + 2; .
6d30: 20 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 if( ret==PENDI
6d40: 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
6d50: 29 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a ) ){. ret++;.
6d60: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 }. return ret
6d70: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
6d80: 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 an entry into t
6d90: 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a he pointer map..
6da0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
6db0: 6e 65 20 75 70 64 61 74 65 73 20 74 68 65 20 70 ne updates the p
6dc0: 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
6dd0: 20 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 for page number
6de0: 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 'key'.** so tha
6df0: 74 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 t it maps to typ
6e00: 65 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 e 'eType' and pa
6e10: 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 rent page number
6e20: 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49 'pgno'..**.** I
6e30: 66 20 2a 70 52 43 20 69 73 20 69 6e 69 74 69 61 f *pRC is initia
6e40: 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f lly non-zero (no
6e50: 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 65 n-SQLITE_OK) the
6e60: 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
6e70: 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 s.** a no-op. I
6e80: 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
6e90: 73 2c 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 s, the appropria
6ea0: 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
6eb0: 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f written.** into
6ec0: 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 *pRC..*/.static
6ed0: 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 28 void ptrmapPut(
6ee0: 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
6ef0: 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 gno key, u8 eTyp
6f00: 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c 20 e, Pgno parent,
6f10: 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 50 int *pRC){. DbP
6f20: 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f age *pDbPage; /
6f30: 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
6f40: 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a p page */. u8 *
6f50: 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a pPtrmap; /*
6f60: 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
6f70: 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 data */. Pgno
6f80: 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 iPtrmap; /*
6f90: 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
6fa0: 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 page number */.
6fb0: 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 int offset;
6fc0: 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 /* Offset in
6fd0: 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
6fe0: 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
6ff0: 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
7000: 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66 n code from subf
7010: 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69 unctions */.. i
7020: 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e f( *pRC ) return
7030: 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
7040: 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
7050: 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
7060: 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a /* The master-j
7070: 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 ournal page numb
7080: 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 62 65 er must never be
7090: 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 6e 74 used as a point
70a0: 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 er map page */.
70b0: 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 52 4d assert( 0==PTRM
70c0: 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 50 AP_ISPAGE(pBt, P
70d0: 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
70e0: 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 (pBt)) );.. ass
70f0: 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 ert( pBt->autoVa
7100: 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 cuum );. if( ke
7110: 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 y==0 ){. *pRC
7120: 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
7130: 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75 T_BKPT;. retu
7140: 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 rn;. }. iPtrma
7150: 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e p = PTRMAP_PAGEN
7160: 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 O(pBt, key);. r
7170: 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
7180: 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c Get(pBt->pPager,
7190: 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 iPtrmap, &pDbPa
71a0: 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 ge);. if( rc!=S
71b0: 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
71c0: 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 *pRC = rc;. r
71d0: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66 eturn;. }. off
71e0: 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 set = PTRMAP_PTR
71f0: 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 OFFSET(iPtrmap,
7200: 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 key);. if( offs
7210: 65 74 3c 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 et<0 ){. *pRC
7220: 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
7230: 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f T_BKPT;. goto
7240: 20 70 74 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20 ptrmap_exit;.
7250: 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 }. assert( offs
7260: 65 74 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e et <= (int)pBt->
7270: 75 73 61 62 6c 65 53 69 7a 65 2d 35 20 29 3b 0a usableSize-5 );.
7280: 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 pPtrmap = (u8
7290: 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
72a0: 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a tData(pDbPage);.
72b0: 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 70 50 . if( eType!=pP
72c0: 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c 7c trmap[offset] ||
72d0: 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d get4byte(&pPtrm
72e0: 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d 70 ap[offset+1])!=p
72f0: 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52 41 arent ){. TRA
7300: 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44 41 CE(("PTRMAP_UPDA
7310: 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29 5c TE: %d->(%d,%d)\
7320: 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c 20 n", key, eType,
7330: 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 2a 70 parent));. *p
7340: 52 43 3d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 RC= rc = sqlite3
7350: 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 PagerWrite(pDbPa
7360: 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ge);. if( rc=
7370: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
7380: 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 pPtrmap[offs
7390: 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 et] = eType;.
73a0: 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 74 put4byte(&pPt
73b0: 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 rmap[offset+1],
73c0: 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 parent);. }.
73d0: 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 78 69 74 3a }..ptrmap_exit:
73e0: 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 . sqlite3PagerU
73f0: 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 7d nref(pDbPage);.}
7400: 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 ../*.** Read an
7410: 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70 entry from the p
7420: 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a ointer map..**.*
7430: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
7440: 65 74 72 69 65 76 65 73 20 74 68 65 20 70 6f 69 etrieves the poi
7450: 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 nter map entry f
7460: 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77 or page 'key', w
7470: 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 riting.** the ty
7480: 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 pe and parent pa
7490: 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 ge number to *pE
74a0: 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 Type and *pPgno
74b0: 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a respectively..**
74c0: 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 An error code i
74d0: 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f s returned if so
74e0: 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
74f0: 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 ng, otherwise SQ
7500: 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 LITE_OK..*/.stat
7510: 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74 ic int ptrmapGet
7520: 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
7530: 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 Pgno key, u8 *pE
7540: 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e Type, Pgno *pPgn
7550: 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
7560: 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 bPage; /* The
7570: 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
7580: 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 */. int iPtrma
7590: 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e p; /* Poin
75a0: 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64 ter map page ind
75b0: 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 ex */. u8 *pPtr
75c0: 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f map; /* Po
75d0: 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 64 inter map page d
75e0: 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 ata */. int off
75f0: 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f set; /* O
7600: 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69 ffset of entry i
7610: 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f n pointer map */
7620: 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
7630: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
7640: 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
7650: 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d tex) );.. iPtrm
7660: 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 ap = PTRMAP_PAGE
7670: 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 NO(pBt, key);.
7680: 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
7690: 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 rGet(pBt->pPager
76a0: 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 , iPtrmap, &pDbP
76b0: 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
76c0: 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
76d0: 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 rc;. }. pPtrma
76e0: 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 p = (u8 *)sqlite
76f0: 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
7700: 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 bPage);.. offse
7710: 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 t = PTRMAP_PTROF
7720: 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 FSET(iPtrmap, ke
7730: 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 65 74 y);. if( offset
7740: 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 <0 ){. sqlite
7750: 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 3PagerUnref(pDbP
7760: 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e age);. return
7770: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
7780: 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 BKPT;. }. asse
7790: 72 74 28 20 6f 66 66 73 65 74 20 3c 3d 20 28 69 rt( offset <= (i
77a0: 6e 74 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 nt)pBt->usableSi
77b0: 7a 65 2d 35 20 29 3b 0a 20 20 61 73 73 65 72 74 ze-5 );. assert
77c0: 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 ( pEType!=0 );.
77d0: 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d *pEType = pPtrm
77e0: 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 ap[offset];. if
77f0: 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f ( pPgno ) *pPgno
7800: 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 74 = get4byte(&pPt
7810: 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b rmap[offset+1]);
7820: 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 .. sqlite3Pager
7830: 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a Unref(pDbPage);.
7840: 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 if( *pEType<1
7850: 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 || *pEType>5 ) r
7860: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
7870: 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74 RUPT_BKPT;. ret
7880: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
7890: 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 ..#else /* if de
78a0: 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 fined SQLITE_OMI
78b0: 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a T_AUTOVACUUM */.
78c0: 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 #define ptrmap
78d0: 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a Put(w,x,y,z,rc).
78e0: 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 #define ptrmap
78f0: 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c Get(w,x,y,z) SQL
7900: 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
7910: 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
7920: 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65 6e 64 r(x, y, rc).#end
7930: 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 if../*.** Given
7940: 61 20 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 a btree page and
7950: 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 a cell index (0
7960: 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 means the first
7970: 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 cell on.** the
7980: 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 page, 1 means th
7990: 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 e second cell, a
79a0: 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 nd so forth) ret
79b0: 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a urn a pointer.**
79c0: 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e to the cell con
79d0: 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tent..**.** This
79e0: 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f routine works o
79f0: 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 nly for pages th
7a00: 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 at do not contai
7a10: 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 n overflow cells
7a20: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e ..*/.#define fin
7a30: 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 dCell(P,I) \. (
7a40: 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 (P)->aData + ((P
7a50: 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 )->maskPage & ge
7a60: 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 43 65 t2byte(&(P)->aCe
7a70: 6c 6c 49 64 78 5b 32 2a 28 49 29 5d 29 29 29 0a llIdx[2*(I)]))).
7a80: 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c 6c #define findCell
7a90: 76 32 28 44 2c 4d 2c 4f 2c 49 29 20 28 44 2b 28 v2(D,M,O,I) (D+(
7aa0: 4d 26 67 65 74 32 62 79 74 65 28 44 2b 28 4f 2b M&get2byte(D+(O+
7ab0: 32 2a 28 49 29 29 29 29 29 0a 0a 0a 2f 2a 0a 2a 2*(I))))).../*.*
7ac0: 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f * This a more co
7ad0: 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66 mplex version of
7ae0: 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74 findCell() that
7af0: 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 works for.** pa
7b00: 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74 ges that do cont
7b10: 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
7b20: 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 ls..*/.static u8
7b30: 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 *findOverflowCe
7b40: 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ll(MemPage *pPag
7b50: 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 e, int iCell){.
7b60: 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 int i;. assert
7b70: 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
7b80: 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
7b90: 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
7ba0: 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 (i=pPage->nOverf
7bb0: 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d low-1; i>=0; i--
7bc0: 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 ){. int k;.
7bd0: 20 20 6b 20 3d 20 70 50 61 67 65 2d 3e 61 69 4f k = pPage->aiO
7be0: 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 vfl[i];. if(
7bf0: 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 k<=iCell ){.
7c00: 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 if( k==iCell )
7c10: 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
7c20: 20 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 69 pPage->apOvfl[i
7c30: 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ];. }.
7c40: 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a iCell--;. }.
7c50: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 6e }. return fin
7c60: 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 dCell(pPage, iCe
7c70: 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 ll);.}../*.** Pa
7c80: 72 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 rse a cell conte
7c90: 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c nt block and fil
7ca0: 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 l in the CellInf
7cb0: 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 o structure. Th
7cc0: 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 ere.** are two v
7cd0: 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 ersions of this
7ce0: 66 75 6e 63 74 69 6f 6e 2e 20 20 62 74 72 65 65 function. btree
7cf0: 50 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 ParseCell() take
7d00: 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 s a .** cell ind
7d10: 65 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ex as the second
7d20: 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 62 74 argument and bt
7d30: 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
7d40: 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f ) .** takes a po
7d50: 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f 64 inter to the bod
7d60: 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 73 y of the cell as
7d70: 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 its second argu
7d80: 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ment..*/.static
7d90: 76 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 void btreeParseC
7da0: 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 ellPtr(. MemPag
7db0: 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
7dc0: 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
7dd0: 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f ning the cell */
7de0: 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 . u8 *pCell,
7df0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
7e00: 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c inter to the cel
7e10: 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c l text. */. Cel
7e20: 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 lInfo *pInfo
7e30: 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 /* Fill in
7e40: 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
7e50: 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 49 74 65 72 /.){. u8 *pIter
7e60: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
7e70: 2a 20 46 6f 72 20 73 63 61 6e 6e 69 6e 67 20 74 * For scanning t
7e80: 68 72 6f 75 67 68 20 70 43 65 6c 6c 20 2a 2f 0a hrough pCell */.
7e90: 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 u32 nPayload;
7ea0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
7eb0: 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
7ec0: 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a cell payload */.
7ed0: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
7ee0: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
7ef0: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
7f00: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
7f10: 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 age->leaf==0 ||
7f20: 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 pPage->leaf==1 )
7f30: 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 ;. if( pPage->i
7f40: 6e 74 4b 65 79 4c 65 61 66 20 29 7b 0a 20 20 20 ntKeyLeaf ){.
7f50: 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
7f60: 63 68 69 6c 64 50 74 72 53 69 7a 65 3d 3d 30 20 childPtrSize==0
7f70: 29 3b 0a 20 20 20 20 70 49 74 65 72 20 3d 20 70 );. pIter = p
7f80: 43 65 6c 6c 20 2b 20 67 65 74 56 61 72 69 6e 74 Cell + getVarint
7f90: 33 32 28 70 43 65 6c 6c 2c 20 6e 50 61 79 6c 6f 32(pCell, nPaylo
7fa0: 61 64 29 3b 0a 20 20 20 20 70 49 74 65 72 20 2b ad);. pIter +
7fb0: 3d 20 67 65 74 56 61 72 69 6e 74 28 70 49 74 65 = getVarint(pIte
7fc0: 72 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d r, (u64*)&pInfo-
7fd0: 3e 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 20 >nKey);. }else
7fe0: 69 66 28 20 70 50 61 67 65 2d 3e 6e 6f 50 61 79 if( pPage->noPay
7ff0: 6c 6f 61 64 20 29 7b 0a 20 20 20 20 61 73 73 65 load ){. asse
8000: 72 74 28 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 rt( pPage->child
8010: 50 74 72 53 69 7a 65 3d 3d 34 20 29 3b 0a 20 20 PtrSize==4 );.
8020: 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d pInfo->nSize =
8030: 20 34 20 2b 20 67 65 74 56 61 72 69 6e 74 28 26 4 + getVarint(&
8040: 70 43 65 6c 6c 5b 34 5d 2c 20 28 75 36 34 2a 29 pCell[4], (u64*)
8050: 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 &pInfo->nKey);.
8060: 20 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f pInfo->nPaylo
8070: 61 64 20 3d 20 30 3b 0a 20 20 20 20 70 49 6e 66 ad = 0;. pInf
8080: 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 30 3b 0a 20 o->nLocal = 0;.
8090: 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 pInfo->iOverf
80a0: 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 70 49 6e low = 0;. pIn
80b0: 66 6f 2d 3e 70 50 61 79 6c 6f 61 64 20 3d 20 30 fo->pPayload = 0
80c0: 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
80d0: 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 74 65 72 }else{. pIter
80e0: 20 3d 20 70 43 65 6c 6c 20 2b 20 70 50 61 67 65 = pCell + pPage
80f0: 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a ->childPtrSize;.
8100: 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 pIter += get
8110: 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 Varint32(pIter,
8120: 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 nPayload);. p
8130: 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
8140: 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e yload;. }. pIn
8150: 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e fo->nPayload = n
8160: 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f Payload;. pInfo
8170: 2d 3e 70 50 61 79 6c 6f 61 64 20 3d 20 70 49 74 ->pPayload = pIt
8180: 65 72 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 er;. testcase(
8190: 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d nPayload==pPage-
81a0: 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 >maxLocal );. t
81b0: 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 estcase( nPayloa
81c0: 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 d==pPage->maxLoc
81d0: 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 50 al+1 );. if( nP
81e0: 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d ayload<=pPage->m
81f0: 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 2f axLocal ){. /
8200: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 28 65 * This is the (e
8210: 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 asy) common case
8220: 20 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 where the entir
8230: 65 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 e payload fits.
8240: 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 ** on the loc
8250: 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 al page. No ove
8260: 72 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 rflow is require
8270: 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 d.. */. pI
8280: 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 6e 50 61 nfo->nSize = nPa
8290: 79 6c 6f 61 64 20 2b 20 28 75 31 36 29 28 70 49 yload + (u16)(pI
82a0: 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 20 20 ter - pCell);.
82b0: 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 53 69 if( pInfo->nSi
82c0: 7a 65 3c 34 20 29 20 70 49 6e 66 6f 2d 3e 6e 53 ze<4 ) pInfo->nS
82d0: 69 7a 65 20 3d 20 34 3b 0a 20 20 20 20 70 49 6e ize = 4;. pIn
82e0: 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
82f0: 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 6)nPayload;.
8300: 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 pInfo->iOverflow
8310: 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
8320: 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 /* If the pay
8330: 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 load will not fi
8340: 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 t completely on
8350: 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 the local page,
8360: 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 we have. ** t
8370: 6f 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 o decide how muc
8380: 68 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c h to store local
8390: 6c 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 ly and how much
83a0: 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 to spill onto.
83b0: 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 ** overflow pa
83c0: 67 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 ges. The strate
83d0: 67 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a gy is to minimiz
83e0: 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 e the amount of
83f0: 75 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 unused. ** sp
8400: 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 ace on overflow
8410: 70 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 pages while keep
8420: 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f ing the amount o
8430: 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a f local storage.
8440: 20 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 ** in betwee
8450: 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d n minLocal and m
8460: 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a axLocal.. **.
8470: 20 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 ** Warning:
8480: 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 changing the wa
8490: 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f y overflow paylo
84a0: 61 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 ad is distribute
84b0: 64 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 d in any. **
84c0: 77 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 way will result
84d0: 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 in an incompatib
84e0: 6c 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a le file format..
84f0: 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d */. int m
8500: 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e inLocal; /* Min
8510: 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 imum amount of p
8520: 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 ayload held loca
8530: 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d lly */. int m
8540: 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 axLocal; /* Max
8550: 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 imum amount of p
8560: 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 ayload held loca
8570: 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 lly */. int s
8580: 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 urplus; /* Ove
8590: 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 rflow payload av
85a0: 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 ailable for loca
85b0: 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 l storage */..
85c0: 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 minLocal = pPa
85d0: 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 ge->minLocal;.
85e0: 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 maxLocal = pPa
85f0: 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 ge->maxLocal;.
8600: 20 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c surplus = minL
8610: 6f 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 ocal + (nPayload
8620: 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 - minLocal)%(pP
8630: 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
8640: 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 Size - 4);. t
8650: 65 73 74 63 61 73 65 28 20 73 75 72 70 6c 75 73 estcase( surplus
8660: 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ==maxLocal );.
8670: 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70 testcase( surp
8680: 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31 20 lus==maxLocal+1
8690: 29 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c );. if( surpl
86a0: 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 us <= maxLocal )
86b0: 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e {. pInfo->n
86c0: 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 73 75 72 Local = (u16)sur
86d0: 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b plus;. }else{
86e0: 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c . pInfo->nL
86f0: 6f 63 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e 4c ocal = (u16)minL
8700: 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ocal;. }.
8710: 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 pInfo->iOverflow
8720: 20 3d 20 28 75 31 36 29 28 26 70 49 6e 66 6f 2d = (u16)(&pInfo-
8730: 3e 70 50 61 79 6c 6f 61 64 5b 70 49 6e 66 6f 2d >pPayload[pInfo-
8740: 3e 6e 4c 6f 63 61 6c 5d 20 2d 20 70 43 65 6c 6c >nLocal] - pCell
8750: 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 );. pInfo->nS
8760: 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 ize = pInfo->iOv
8770: 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a erflow + 4;. }.
8780: 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 }.static void bt
8790: 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a 20 20 reeParseCell(.
87a0: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
87b0: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
87c0: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 containing the c
87d0: 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 ell */. int iCe
87e0: 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
87f0: 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e 64 /* The cell ind
8800: 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c 6c 20 ex. First cell
8810: 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e is 0 */. CellIn
8820: 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 fo *pInfo
8830: 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 /* Fill in thi
8840: 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 s structure */.)
8850: 7b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65 {. btreeParseCe
8860: 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 66 69 6e llPtr(pPage, fin
8870: 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 dCell(pPage, iCe
8880: 6c 6c 29 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a ll), pInfo);.}..
8890: 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
88a0: 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
88b0: 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43 f bytes that a C
88c0: 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 ell needs in the
88d0: 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 cell.** data ar
88e0: 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d ea of the btree-
88f0: 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 page. The retur
8900: 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 n number include
8910: 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 s the cell.** da
8920: 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 ta header and th
8930: 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c e local payload,
8940: 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 but not any ove
8950: 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a rflow page or.**
8960: 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 the space used
8970: 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e by the cell poin
8980: 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 ter..*/.static u
8990: 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 16 cellSizePtr(M
89a0: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
89b0: 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 8 *pCell){. u8
89c0: 2a 70 49 74 65 72 20 3d 20 70 43 65 6c 6c 20 2b *pIter = pCell +
89d0: 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
89e0: 53 69 7a 65 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f Size; /* For loo
89f0: 70 69 6e 67 20 6f 76 65 72 20 62 79 74 65 73 20 ping over bytes
8a00: 6f 66 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 of pCell */. u8
8a10: 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 *pEnd;
8a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8a30: 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6d 61 /* End ma
8a40: 72 6b 20 66 6f 72 20 61 20 76 61 72 69 6e 74 20 rk for a varint
8a50: 2a 2f 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b 20 */. u32 nSize;
8a60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8a70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8a80: 20 53 69 7a 65 20 76 61 6c 75 65 20 74 6f 20 72 Size value to r
8a90: 65 74 75 72 6e 20 2a 2f 0a 0a 23 69 66 64 65 66 eturn */..#ifdef
8aa0: 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
8ab0: 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 /* The value ret
8ac0: 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 urned by this fu
8ad0: 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c nction should al
8ae0: 77 61 79 73 20 62 65 20 74 68 65 20 73 61 6d 65 ways be the same
8af0: 20 61 73 0a 20 20 2a 2a 20 74 68 65 20 28 43 65 as. ** the (Ce
8b00: 6c 6c 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 76 61 llInfo.nSize) va
8b10: 6c 75 65 20 66 6f 75 6e 64 20 62 79 20 64 6f 69 lue found by doi
8b20: 6e 67 20 61 20 66 75 6c 6c 20 70 61 72 73 65 20 ng a full parse
8b30: 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c of the. ** cell
8b40: 2e 20 49 66 20 53 51 4c 49 54 45 5f 44 45 42 55 . If SQLITE_DEBU
8b50: 47 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e G is defined, an
8b60: 20 61 73 73 65 72 74 28 29 20 61 74 20 74 68 65 assert() at the
8b70: 20 62 6f 74 74 6f 6d 20 6f 66 0a 20 20 2a 2a 20 bottom of. **
8b80: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 76 65 this function ve
8b90: 72 69 66 69 65 73 20 74 68 61 74 20 74 68 69 73 rifies that this
8ba0: 20 69 6e 76 61 72 69 61 6e 74 20 69 73 20 6e 6f invariant is no
8bb0: 74 20 76 69 6f 6c 61 74 65 64 2e 20 2a 2f 0a 20 t violated. */.
8bc0: 20 43 65 6c 6c 49 6e 66 6f 20 64 65 62 75 67 69 CellInfo debugi
8bd0: 6e 66 6f 3b 0a 20 20 62 74 72 65 65 50 61 72 73 nfo;. btreePars
8be0: 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
8bf0: 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 pCell, &debuginf
8c00: 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 o);.#endif.. if
8c10: 28 20 70 50 61 67 65 2d 3e 6e 6f 50 61 79 6c 6f ( pPage->noPaylo
8c20: 61 64 20 29 7b 0a 20 20 20 20 70 45 6e 64 20 3d ad ){. pEnd =
8c30: 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 &pIter[9];.
8c40: 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b while( (*pIter++
8c50: 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72 3c )&0x80 && pIter<
8c60: 70 45 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65 pEnd );. asse
8c70: 72 74 28 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 rt( pPage->child
8c80: 50 74 72 53 69 7a 65 3d 3d 34 20 29 3b 0a 20 20 PtrSize==4 );.
8c90: 20 20 72 65 74 75 72 6e 20 28 75 31 36 29 28 70 return (u16)(p
8ca0: 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 20 Iter - pCell);.
8cb0: 20 7d 0a 20 20 6e 53 69 7a 65 20 3d 20 2a 70 49 }. nSize = *pI
8cc0: 74 65 72 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 ter;. if( nSize
8cd0: 3e 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 70 45 >=0x80 ){. pE
8ce0: 6e 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a nd = &pIter[9];.
8cf0: 20 20 20 20 6e 53 69 7a 65 20 26 3d 20 30 78 37 nSize &= 0x7
8d00: 66 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 f;. do{.
8d10: 20 6e 53 69 7a 65 20 3d 20 28 6e 53 69 7a 65 3c nSize = (nSize<
8d20: 3c 37 29 20 7c 20 28 2a 2b 2b 70 49 74 65 72 20 <7) | (*++pIter
8d30: 26 20 30 78 37 66 29 3b 0a 20 20 20 20 7d 77 68 & 0x7f);. }wh
8d40: 69 6c 65 28 20 2a 28 70 49 74 65 72 29 3e 3d 30 ile( *(pIter)>=0
8d50: 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e x80 && pIter<pEn
8d60: 64 20 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 72 d );. }. pIter
8d70: 2b 2b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d ++;. if( pPage-
8d80: 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 2f >intKey ){. /
8d90: 2a 20 70 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e * pIter now poin
8da0: 74 73 20 61 74 20 74 68 65 20 36 34 2d 62 69 74 ts at the 64-bit
8db0: 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c integer key val
8dc0: 75 65 2c 20 61 20 76 61 72 69 61 62 6c 65 20 6c ue, a variable l
8dd0: 65 6e 67 74 68 20 0a 20 20 20 20 2a 2a 20 69 6e ength . ** in
8de0: 74 65 67 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f teger. The follo
8df0: 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 wing block moves
8e00: 20 70 49 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 pIter to point
8e10: 61 74 20 74 68 65 20 66 69 72 73 74 20 62 79 74 at the first byt
8e20: 65 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 e. ** past th
8e30: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6b 65 79 e end of the key
8e40: 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 value. */. p
8e50: 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b End = &pIter[9];
8e60: 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 . while( (*pI
8e70: 74 65 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 ter++)&0x80 && p
8e80: 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d Iter<pEnd );. }
8e90: 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 . testcase( nSi
8ea0: 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ze==pPage->maxLo
8eb0: 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 cal );. testcas
8ec0: 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d e( nSize==pPage-
8ed0: 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 >maxLocal+1 );.
8ee0: 20 69 66 28 20 6e 53 69 7a 65 3c 3d 70 50 61 67 if( nSize<=pPag
8ef0: 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 e->maxLocal ){.
8f00: 20 20 20 6e 53 69 7a 65 20 2b 3d 20 28 75 33 32 nSize += (u32
8f10: 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 )(pIter - pCell)
8f20: 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 7a 65 3c ;. if( nSize<
8f30: 34 20 29 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 4 ) nSize = 4;.
8f40: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
8f50: 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 minLocal = pPage
8f60: 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 ->minLocal;.
8f70: 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c nSize = minLocal
8f80: 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c + (nSize - minL
8f90: 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e ocal) % (pPage->
8fa0: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
8fb0: 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 - 4);. testca
8fc0: 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
8fd0: 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
8fe0: 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
8ff0: 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
9000: 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
9010: 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 nSize>pPage->max
9020: 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e Local ){. n
9030: 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b Size = minLocal;
9040: 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 . }. nSize
9050: 20 2b 3d 20 34 20 2b 20 28 75 31 36 29 28 70 49 += 4 + (u16)(pI
9060: 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 20 20 ter - pCell);.
9070: 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a }. assert( nSiz
9080: 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 e==debuginfo.nSi
9090: 7a 65 20 7c 7c 20 43 4f 52 52 55 50 54 5f 44 42 ze || CORRUPT_DB
90a0: 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 31 );. return (u1
90b0: 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 64 6)nSize;.}..#ifd
90c0: 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
90d0: 2f 2a 20 54 68 69 73 20 76 61 72 69 61 74 69 6f /* This variatio
90e0: 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 n on cellSizePtr
90f0: 28 29 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 () is used insid
9100: 65 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 e of assert() st
9110: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 atements.** only
9120: 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 . */.static u16
9130: 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 67 65 cellSize(MemPage
9140: 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 *pPage, int iCe
9150: 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 65 ll){. return ce
9160: 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
9170: 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
9180: 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 iCell));.}.#end
9190: 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
91a0: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
91b0: 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 UM./*.** If the
91c0: 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 cell pCell, part
91d0: 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 63 of page pPage c
91e0: 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
91f0: 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 r.** to an overf
9200: 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 low page, insert
9210: 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 an entry into t
9220: 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a he pointer-map.*
9230: 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c * for the overfl
9240: 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 ow page..*/.stat
9250: 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 ic void ptrmapPu
9260: 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 tOvflPtr(MemPage
9270: 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 65 *pPage, u8 *pCe
9280: 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 ll, int *pRC){.
9290: 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
92a0: 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 if( *pRC ) ret
92b0: 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
92c0: 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 62 74 72 Cell!=0 );. btr
92d0: 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
92e0: 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
92f0: 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e fo);. if( info.
9300: 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 iOverflow ){.
9310: 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 Pgno ovfl = get
9320: 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 4byte(&pCell[inf
9330: 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 o.iOverflow]);.
9340: 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 ptrmapPut(pPa
9350: 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 ge->pBt, ovfl, P
9360: 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c TRMAP_OVERFLOW1,
9370: 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 pPage->pgno, pR
9380: 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 C);. }.}.#endif
9390: 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d .../*.** Defragm
93a0: 65 6e 74 20 74 68 65 20 70 61 67 65 20 67 69 76 ent the page giv
93b0: 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 en. All Cells a
93c0: 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a re moved to the.
93d0: 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 ** end of the pa
93e0: 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 ge and all free
93f0: 73 70 61 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 space is collect
9400: 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 ed into one.** b
9410: 69 67 20 46 72 65 65 42 6c 6b 20 74 68 61 74 20 ig FreeBlk that
9420: 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 occurs in betwee
9430: 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 n the header and
9440: 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 cell.** pointer
9450: 20 61 72 72 61 79 20 61 6e 64 20 74 68 65 20 63 array and the c
9460: 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
9470: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
9480: 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 4d defragmentPage(M
9490: 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a emPage *pPage){.
94a0: 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
94b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
94c0: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
94d0: 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 int pc;
94e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
94f0: 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 69 Address of the i
9500: 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e -th cell */. in
9510: 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 t hdr;
9520: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
9530: 65 74 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 et to the page h
9540: 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 eader */. int s
9550: 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
9560: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
9570: 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 a cell */. int
9580: 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 usableSize;
9590: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
95a0: 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 r of usable byte
95b0: 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 s on a page */.
95c0: 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b int cellOffset;
95d0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
95e0: 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c ffset to the cel
95f0: 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 l pointer array
9600: 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 */. int cbrk;
9610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9620: 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 /* Offset to the
9630: 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
9640: 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c ea */. int nCel
9650: 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
9660: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
9670: 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 cells on the pag
9680: 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
9690: 63 68 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20 char *data;
96a0: 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61 /* The page da
96b0: 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ta */. unsigned
96c0: 20 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20 char *temp;
96d0: 20 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20 /* Temp area
96e0: 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 for cell content
96f0: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
9700: 68 61 72 20 2a 73 72 63 3b 20 20 20 20 20 20 20 har *src;
9710: 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 63 6f /* Source of co
9720: 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntent */. int i
9730: 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 20 CellFirst;
9740: 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
9750: 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e llowable cell in
9760: 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 dex */. int iCe
9770: 6c 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 llLast;
9780: 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 /* Last poss
9790: 69 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 ible cell index
97a0: 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */... assert( s
97b0: 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
97c0: 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
97d0: 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
97e0: 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d rt( pPage->pBt!=
97f0: 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
9800: 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
9810: 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45 5f eSize <= SQLITE_
9820: 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29 3b MAX_PAGE_SIZE );
9830: 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
9840: 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 ->nOverflow==0 )
9850: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
9860: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
9870: 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
9880: 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 30 3b ) );. temp = 0;
9890: 0a 20 20 73 72 63 20 3d 20 64 61 74 61 20 3d 20 . src = data =
98a0: 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
98b0: 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
98c0: 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 Offset;. cellOf
98d0: 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 fset = pPage->ce
98e0: 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c llOffset;. nCel
98f0: 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c l = pPage->nCell
9900: 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c ;. assert( nCel
9910: 6c 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 l==get2byte(&dat
9920: 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 a[hdr+3]) );. u
9930: 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 sableSize = pPag
9940: 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
9950: 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 ze;. cbrk = usa
9960: 62 6c 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c bleSize;. iCell
9970: 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 First = cellOffs
9980: 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 et + 2*nCell;.
9990: 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 iCellLast = usab
99a0: 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f leSize - 4;. fo
99b0: 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
99c0: 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 i++){. u8 *pA
99d0: 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ddr; /* The
99e0: 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 i-th cell pointe
99f0: 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d r */. pAddr =
9a00: 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 &data[cellOffse
9a10: 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 t + i*2];. pc
9a20: 20 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 = get2byte(pAdd
9a30: 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 r);. testcase
9a40: 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 ( pc==iCellFirst
9a50: 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
9a60: 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 ( pc==iCellLast
9a70: 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 );.#if !defined(
9a80: 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 SQLITE_ENABLE_OV
9a90: 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 ERSIZE_CELL_CHEC
9aa0: 4b 29 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 K). /* These
9ab0: 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 conditions have
9ac0: 61 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 already been ver
9ad0: 69 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e ified in btreeIn
9ae0: 69 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 itPage(). **
9af0: 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
9b00: 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 _OVERSIZE_CELL_C
9b10: 48 45 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20 HECK is defined
9b20: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
9b30: 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c pc<iCellFirst ||
9b40: 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b pc>iCellLast ){
9b50: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
9b60: 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
9b70: 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a T;. }.#endif.
9b80: 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d assert( pc>=
9b90: 69 43 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63 iCellFirst && pc
9ba0: 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 <=iCellLast );.
9bb0: 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 size = cellSi
9bc0: 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 73 72 zePtr(pPage, &sr
9bd0: 63 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72 6b c[pc]);. cbrk
9be0: 20 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64 65 -= size;.#if de
9bf0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
9c00: 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c BLE_OVERSIZE_CEL
9c10: 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66 28 L_CHECK). if(
9c20: 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 cbrk<iCellFirst
9c30: 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
9c40: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
9c50: 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 BKPT;. }.#els
9c60: 65 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 e. if( cbrk<i
9c70: 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 2b CellFirst || pc+
9c80: 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 size>usableSize
9c90: 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
9ca0: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
9cb0: 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 KPT;. }.#endi
9cc0: 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 62 f. assert( cb
9cd0: 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 rk+size<=usableS
9ce0: 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 65 ize && cbrk>=iCe
9cf0: 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 llFirst );. t
9d00: 65 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 69 estcase( cbrk+si
9d10: 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 ze==usableSize )
9d20: 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
9d30: 70 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53 pc+size==usableS
9d40: 69 7a 65 20 29 3b 0a 20 20 20 20 70 75 74 32 62 ize );. put2b
9d50: 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 yte(pAddr, cbrk)
9d60: 3b 0a 20 20 20 20 69 66 28 20 74 65 6d 70 3d 3d ;. if( temp==
9d70: 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 78 0 ){. int x
9d80: 3b 0a 20 20 20 20 20 20 69 66 28 20 63 62 72 6b ;. if( cbrk
9d90: 3d 3d 70 63 20 29 20 63 6f 6e 74 69 6e 75 65 3b ==pc ) continue;
9da0: 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 73 71 . temp = sq
9db0: 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 lite3PagerTempSp
9dc0: 61 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e ace(pPage->pBt->
9dd0: 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 78 pPager);. x
9de0: 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
9df0: 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 20 a[hdr+5]);.
9e00: 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 78 5d memcpy(&temp[x]
9e10: 2c 20 26 64 61 74 61 5b 78 5d 2c 20 28 63 62 72 , &data[x], (cbr
9e20: 6b 2b 73 69 7a 65 29 20 2d 20 78 29 3b 0a 20 20 k+size) - x);.
9e30: 20 20 20 20 73 72 63 20 3d 20 74 65 6d 70 3b 0a src = temp;.
9e40: 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
9e50: 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 73 (&data[cbrk], &s
9e60: 72 63 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 rc[pc], size);.
9e70: 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 }. assert( cbr
9e80: 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b k>=iCellFirst );
9e90: 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
9ea0: 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b a[hdr+5], cbrk);
9eb0: 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d . data[hdr+1] =
9ec0: 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 0;. data[hdr+2
9ed0: 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 ] = 0;. data[hd
9ee0: 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 r+7] = 0;. mems
9ef0: 65 74 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 et(&data[iCellFi
9f00: 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 rst], 0, cbrk-iC
9f10: 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 ellFirst);. ass
9f20: 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
9f30: 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
9f40: 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
9f50: 20 20 69 66 28 20 63 62 72 6b 2d 69 43 65 6c 6c if( cbrk-iCell
9f60: 46 69 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e 46 First!=pPage->nF
9f70: 72 65 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ree ){. retur
9f80: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
9f90: 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 _BKPT;. }. ret
9fa0: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
9fb0: 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 74 ../*.** Search t
9fc0: 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6f 6e 20 he free-list on
9fd0: 70 61 67 65 20 70 50 67 20 66 6f 72 20 73 70 61 page pPg for spa
9fe0: 63 65 20 74 6f 20 73 74 6f 72 65 20 61 20 63 65 ce to store a ce
9ff0: 6c 6c 20 6e 42 79 74 65 20 62 79 74 65 73 20 69 ll nByte bytes i
a000: 6e 0a 2a 2a 20 73 69 7a 65 2e 20 49 66 20 6f 6e n.** size. If on
a010: 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 e can be found,
a020: 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
a030: 20 74 6f 20 74 68 65 20 73 70 61 63 65 20 61 6e to the space an
a040: 64 20 72 65 6d 6f 76 65 20 69 74 0a 2a 2a 20 66 d remove it.** f
a050: 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 rom the free-lis
a060: 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 73 t..**.** If no s
a070: 75 69 74 61 62 6c 65 20 73 70 61 63 65 20 63 61 uitable space ca
a080: 6e 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 74 68 n be found on th
a090: 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 72 65 74 e free-list, ret
a0a0: 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 urn NULL..**.**
a0b0: 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
a0c0: 79 20 64 65 74 65 63 74 20 63 6f 72 72 75 70 74 y detect corrupt
a0d0: 69 6f 6e 20 77 69 74 68 69 6e 20 70 50 67 2e 20 ion within pPg.
a0e0: 49 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 61 If it does and a
a0f0: 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 52 63 20 rgument .** pRc
a100: 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 is non-NULL, the
a110: 6e 20 2a 70 52 63 20 69 73 20 73 65 74 20 74 6f n *pRc is set to
a120: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
a130: 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 and NULL is retu
a140: 72 6e 65 64 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 rned..** Or, if
a150: 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 corruption is de
a160: 74 65 63 74 65 64 20 61 6e 64 20 70 52 63 20 69 tected and pRc i
a170: 73 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 20 69 73 20 s NULL, NULL is
a180: 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
a190: 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 67 .** corruption g
a1a0: 6f 65 73 20 75 6e 72 65 70 6f 72 74 65 64 2e 0a oes unreported..
a1b0: 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 6c 6f 74 20 **.** If a slot
a1c0: 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74 of at least nByt
a1d0: 65 20 62 79 74 65 73 20 69 73 20 66 6f 75 6e 64 e bytes is found
a1e0: 20 62 75 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 but cannot be u
a1f0: 73 65 64 20 62 65 63 61 75 73 65 20 0a 2a 2a 20 sed because .**
a200: 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61 64 there are alread
a210: 79 20 61 74 20 6c 65 61 73 74 20 36 30 20 66 72 y at least 60 fr
a220: 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 6f agmented bytes o
a230: 6e 20 74 68 65 20 70 61 67 65 2c 20 72 65 74 75 n the page, retu
a240: 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 6e 20 74 rn NULL..** In t
a250: 68 69 73 20 63 61 73 65 2c 20 69 66 20 70 62 44 his case, if pbD
a260: 65 66 72 61 67 20 70 61 72 61 6d 65 74 65 72 20 efrag parameter
a270: 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 65 74 is not NULL, set
a280: 20 2a 70 62 44 65 66 72 61 67 20 74 6f 20 74 72 *pbDefrag to tr
a290: 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 ue..*/.static u8
a2a0: 20 2a 70 61 67 65 46 69 6e 64 53 6c 6f 74 28 4d *pageFindSlot(M
a2b0: 65 6d 50 61 67 65 20 2a 70 50 67 2c 20 69 6e 74 emPage *pPg, int
a2c0: 20 6e 42 79 74 65 2c 20 69 6e 74 20 2a 70 52 63 nByte, int *pRc
a2d0: 2c 20 69 6e 74 20 2a 70 62 44 65 66 72 61 67 29 , int *pbDefrag)
a2e0: 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 {. const int hd
a2f0: 72 20 3d 20 70 50 67 2d 3e 68 64 72 4f 66 66 73 r = pPg->hdrOffs
a300: 65 74 3b 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 et;. u8 * const
a310: 20 61 44 61 74 61 20 3d 20 70 50 67 2d 3e 61 44 aData = pPg->aD
a320: 61 74 61 3b 0a 20 20 69 6e 74 20 69 41 64 64 72 ata;. int iAddr
a330: 3b 0a 20 20 69 6e 74 20 70 63 3b 0a 20 20 69 6e ;. int pc;. in
a340: 74 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 t usableSize = p
a350: 50 67 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 Pg->pBt->usableS
a360: 69 7a 65 3b 0a 0a 20 20 66 6f 72 28 69 41 64 64 ize;.. for(iAdd
a370: 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 3d 20 67 r=hdr+1; (pc = g
a380: 65 74 32 62 79 74 65 28 26 61 44 61 74 61 5b 69 et2byte(&aData[i
a390: 41 64 64 72 5d 29 29 3e 30 3b 20 69 41 64 64 72 Addr]))>0; iAddr
a3a0: 3d 70 63 29 7b 0a 20 20 20 20 69 6e 74 20 73 69 =pc){. int si
a3b0: 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ze; /
a3c0: 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 66 72 * Size of the fr
a3d0: 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 69 ee slot */. i
a3e0: 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69 7a 65 f( pc>usableSize
a3f0: 2d 34 20 7c 7c 20 70 63 3c 69 41 64 64 72 2b 34 -4 || pc<iAddr+4
a400: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 ){. if( pR
a410: 63 20 29 20 2a 70 52 63 20 3d 20 53 51 4c 49 54 c ) *pRc = SQLIT
a420: 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
a430: 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
a440: 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 20 3d }. size =
a450: 20 67 65 74 32 62 79 74 65 28 26 61 44 61 74 61 get2byte(&aData
a460: 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 [pc+2]);. if(
a470: 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a size>=nByte ){.
a480: 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 73 69 int x = si
a490: 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 ze - nByte;.
a4a0: 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d 34 testcase( x==4
a4b0: 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
a4c0: 73 65 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 se( x==3 );.
a4d0: 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 if( x<4 ){.
a4e0: 20 20 20 20 20 69 66 28 20 61 44 61 74 61 5b 68 if( aData[h
a4f0: 64 72 2b 37 5d 3e 3d 36 30 20 29 7b 0a 20 20 20 dr+7]>=60 ){.
a500: 20 20 20 20 20 20 20 69 66 28 20 70 62 44 65 66 if( pbDef
a510: 72 61 67 20 29 20 2a 70 62 44 65 66 72 61 67 20 rag ) *pbDefrag
a520: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 72 = 1;. r
a530: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 eturn 0;.
a540: 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 }. /* Re
a550: 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74 20 66 72 move the slot fr
a560: 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 om the free-list
a570: 2e 20 55 70 64 61 74 65 20 74 68 65 20 6e 75 6d . Update the num
a580: 62 65 72 20 6f 66 0a 20 20 20 20 20 20 20 20 2a ber of. *
a590: 2a 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 * fragmented byt
a5a0: 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 es within the pa
a5b0: 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d ge. */. m
a5c0: 65 6d 63 70 79 28 26 61 44 61 74 61 5b 69 41 64 emcpy(&aData[iAd
a5d0: 64 72 5d 2c 20 26 61 44 61 74 61 5b 70 63 5d 2c dr], &aData[pc],
a5e0: 20 32 29 3b 0a 20 20 20 20 20 20 20 20 61 44 61 2);. aDa
a5f0: 74 61 5b 68 64 72 2b 37 5d 20 2b 3d 20 28 75 38 ta[hdr+7] += (u8
a600: 29 78 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 )x;. }else
a610: 69 66 28 20 73 69 7a 65 2b 70 63 20 3e 20 75 73 if( size+pc > us
a620: 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 ableSize ){.
a630: 20 20 20 20 69 66 28 20 70 52 63 20 29 20 2a 70 if( pRc ) *p
a640: 52 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 Rc = SQLITE_CORR
a650: 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
a660: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
a670: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a680: 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 6d /* The slot rem
a690: 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ains on the free
a6a0: 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 20 69 74 -list. Reduce it
a6b0: 73 20 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e s size to accoun
a6c0: 74 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f t. ** fo
a6d0: 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 r the portion us
a6e0: 65 64 20 62 79 20 74 68 65 20 6e 65 77 20 61 6c ed by the new al
a6f0: 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 location. */.
a700: 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 61 put2byte(&a
a710: 44 61 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a Data[pc+2], x);.
a720: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
a730: 74 75 72 6e 20 26 61 44 61 74 61 5b 70 63 20 2b turn &aData[pc +
a740: 20 78 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a x];. }. }..
a750: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
a760: 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
a770: 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 yte bytes of spa
a780: 63 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 ce from within t
a790: 68 65 20 42 2d 54 72 65 65 20 70 61 67 65 20 70 he B-Tree page p
a7a0: 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 assed.** as the
a7b0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 first argument.
a7c0: 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64 78 Write into *pIdx
a7d0: 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 the index into
a7e0: 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a pPage->aData[].*
a7f0: 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 * of the first b
a800: 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 yte of allocated
a810: 20 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20 65 space. Return e
a820: 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 ither SQLITE_OK
a830: 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 or.** an error c
a840: 6f 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51 4c ode (usually SQL
a850: 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a ITE_CORRUPT)..**
a860: 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 .** The caller g
a870: 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 uarantees that t
a880: 68 65 72 65 20 69 73 20 73 75 66 66 69 63 69 65 here is sufficie
a890: 6e 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b 65 nt space to make
a8a0: 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 the.** allocati
a8b0: 6f 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e on. This routin
a8c0: 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 e might need to
a8d0: 64 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f 72 defragment in or
a8e0: 64 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 der to bring.**
a8f0: 61 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74 6f all the space to
a900: 67 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72 2e gether, however.
a910: 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 This routine w
a920: 69 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67 0a ill avoid using.
a930: 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 6f ** the first two
a940: 20 62 79 74 65 73 20 70 61 73 74 20 74 68 65 20 bytes past the
a950: 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 65 cell pointer are
a960: 61 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61 62 a since presumab
a970: 6c 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 ly this.** alloc
a980: 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6d ation is being m
a990: 61 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ade in order to
a9a0: 69 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c insert a new cel
a9b0: 6c 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a l, so we will.**
a9c0: 20 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65 65 also end up nee
a9d0: 64 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c 20 ding a new cell
a9e0: 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 pointer..*/.stat
a9f0: 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53 ic int allocateS
aa00: 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 pace(MemPage *pP
aa10: 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 age, int nByte,
aa20: 69 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63 6f int *pIdx){. co
aa30: 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50 nst int hdr = pP
aa40: 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 age->hdrOffset;
aa50: 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 /* Local cach
aa60: 65 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72 4f e of pPage->hdrO
aa70: 66 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 ffset */. u8 *
aa80: 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50 61 const data = pPa
aa90: 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 ge->aData;
aaa0: 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f /* Local cache o
aab0: 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a f pPage->aData *
aac0: 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 /. int top;
aad0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aae0: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
aaf0: 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 t byte of cell c
ab00: 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
ab10: 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20 20 20 int gap;
ab20: 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f /* First byte o
ab30: 66 20 67 61 70 20 62 65 74 77 65 65 6e 20 63 65 f gap between ce
ab40: 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 ll pointers and
ab50: 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a cell content */.
ab60: 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
ab70: 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 65 74 /* Integer ret
ab80: 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 0a 20 urn code */. .
ab90: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
aba0: 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
abb0: 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
abc0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
abd0: 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 age->pBt );. as
abe0: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
abf0: 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
ac00: 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
ac10: 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d assert( nByte>=
ac20: 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 0 ); /* Minimum
ac30: 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 cell size is 4
ac40: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
ac50: 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 ge->nFree>=nByte
ac60: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
ac70: 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
ac80: 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
ac90: 42 79 74 65 20 3c 20 70 50 61 67 65 2d 3e 70 42 Byte < pPage->pB
aca0: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20 t->usableSize-8
acb0: 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 );.. assert( pP
acc0: 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
acd0: 3d 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a == hdr + 12 - 4*
ace0: 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 pPage->leaf );.
acf0: 20 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 gap = pPage->ce
ad00: 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 llOffset + 2*pPa
ad10: 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 ge->nCell;. ass
ad20: 65 72 74 28 20 67 61 70 3c 3d 36 35 35 33 36 20 ert( gap<=65536
ad30: 29 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 );. top = get2b
ad40: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
ad50: 29 3b 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 );. if( gap>top
ad60: 20 29 7b 0a 20 20 20 20 69 66 28 20 74 6f 70 3d ){. if( top=
ad70: 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 6f 70 20 =0 ){. top
ad80: 3d 20 36 35 35 33 36 3b 0a 20 20 20 20 7d 65 6c = 65536;. }el
ad90: 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
ada0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
adb0: 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a BKPT;. }. }.
adc0: 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
add0: 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 62 s enough space b
ade0: 65 74 77 65 65 6e 20 67 61 70 20 61 6e 64 20 74 etween gap and t
adf0: 6f 70 20 66 6f 72 20 6f 6e 65 20 6d 6f 72 65 20 op for one more
ae00: 63 65 6c 6c 20 70 6f 69 6e 74 65 72 0a 20 20 2a cell pointer. *
ae10: 2a 20 61 72 72 61 79 20 65 6e 74 72 79 20 6f 66 * array entry of
ae20: 66 73 65 74 2c 20 61 6e 64 20 69 66 20 74 68 65 fset, and if the
ae30: 20 66 72 65 65 6c 69 73 74 20 69 73 20 6e 6f 74 freelist is not
ae40: 20 65 6d 70 74 79 2c 20 74 68 65 6e 20 73 65 61 empty, then sea
ae50: 72 63 68 20 74 68 65 0a 20 20 2a 2a 20 66 72 65 rch the. ** fre
ae60: 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f elist looking fo
ae70: 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 69 r a free slot bi
ae80: 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69 g enough to sati
ae90: 73 66 79 20 74 68 65 20 72 65 71 75 65 73 74 2e sfy the request.
aea0: 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 . */. testcase
aeb0: 28 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a ( gap+2==top );.
aec0: 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b testcase( gap+
aed0: 31 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 1==top );. test
aee0: 63 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 case( gap==top )
aef0: 3b 0a 20 20 69 66 28 20 67 61 70 2b 32 3c 3d 74 ;. if( gap+2<=t
af00: 6f 70 20 26 26 20 28 64 61 74 61 5b 68 64 72 2b op && (data[hdr+
af10: 31 5d 20 7c 7c 20 64 61 74 61 5b 68 64 72 2b 32 1] || data[hdr+2
af20: 5d 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 ]) ){. int rc
af30: 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
af40: 20 20 69 6e 74 20 62 44 65 66 72 61 67 20 3d 20 int bDefrag =
af50: 30 3b 0a 20 20 20 20 75 38 20 2a 70 53 70 61 63 0;. u8 *pSpac
af60: 65 20 3d 20 70 61 67 65 46 69 6e 64 53 6c 6f 74 e = pageFindSlot
af70: 28 70 50 61 67 65 2c 20 6e 42 79 74 65 2c 20 26 (pPage, nByte, &
af80: 72 63 2c 20 26 62 44 65 66 72 61 67 29 3b 0a 20 rc, &bDefrag);.
af90: 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
afa0: 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 62 rn rc;. if( b
afb0: 44 65 66 72 61 67 20 29 20 67 6f 74 6f 20 64 65 Defrag ) goto de
afc0: 66 72 61 67 6d 65 6e 74 5f 70 61 67 65 3b 0a 20 fragment_page;.
afd0: 20 20 20 69 66 28 20 70 53 70 61 63 65 20 29 7b if( pSpace ){
afe0: 0a 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 . *pIdx = p
aff0: 53 70 61 63 65 20 2d 20 64 61 74 61 3b 0a 20 20 Space - data;.
b000: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
b010: 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
b020: 0a 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 . /* The reques
b030: 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 t could not be f
b040: 75 6c 66 69 6c 6c 65 64 20 75 73 69 6e 67 20 61 ulfilled using a
b050: 20 66 72 65 65 6c 69 73 74 20 73 6c 6f 74 2e 20 freelist slot.
b060: 20 43 68 65 63 6b 0a 20 20 2a 2a 20 74 6f 20 73 Check. ** to s
b070: 65 65 20 69 66 20 64 65 66 72 61 67 6d 65 6e 74 ee if defragment
b080: 61 74 69 6f 6e 20 69 73 20 6e 65 63 65 73 73 61 ation is necessa
b090: 72 79 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 ry.. */. testc
b0a0: 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 ase( gap+2+nByte
b0b0: 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 ==top );. if( g
b0c0: 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 ap+2+nByte>top )
b0d0: 7b 0a 20 64 65 66 72 61 67 6d 65 6e 74 5f 70 61 {. defragment_pa
b0e0: 67 65 3a 0a 20 20 20 20 74 65 73 74 63 61 73 65 ge:. testcase
b0f0: 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d ( pPage->nCell==
b100: 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 65 0 );. rc = de
b110: 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 fragmentPage(pPa
b120: 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ge);. if( rc
b130: 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
b140: 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 4e top = get2byteN
b150: 6f 74 5a 65 72 6f 28 26 64 61 74 61 5b 68 64 72 otZero(&data[hdr
b160: 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +5]);. assert
b170: 28 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 ( gap+nByte<=top
b180: 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 );. }... /* A
b190: 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 llocate memory f
b1a0: 72 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 rom the gap in b
b1b0: 65 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 etween the cell
b1c0: 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 pointer array.
b1d0: 2a 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 ** and the cell
b1e0: 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 content area. T
b1f0: 68 65 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 he btreeInitPage
b200: 28 29 20 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 () call has alre
b210: 61 64 79 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 ady. ** validat
b220: 65 64 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e ed the freelist.
b230: 20 20 47 69 76 65 6e 20 74 68 61 74 20 74 68 65 Given that the
b240: 20 66 72 65 65 6c 69 73 74 20 69 73 20 76 61 6c freelist is val
b250: 69 64 2c 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 id, there. ** i
b260: 73 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74 68 s no way that th
b270: 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e e allocation can
b280: 20 65 78 74 65 6e 64 20 6f 66 66 20 74 68 65 20 extend off the
b290: 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e end of the page.
b2a0: 0a 20 20 2a 2a 20 54 68 65 20 61 73 73 65 72 74 . ** The assert
b2b0: 28 29 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65 () below verifie
b2c0: 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 s the previous s
b2d0: 65 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 entence.. */.
b2e0: 74 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 top -= nByte;.
b2f0: 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
b300: 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 dr+5], top);. a
b310: 73 73 65 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 ssert( top+nByte
b320: 20 3c 3d 20 28 69 6e 74 29 70 50 61 67 65 2d 3e <= (int)pPage->
b330: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
b340: 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 );. *pIdx = top
b350: 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
b360: 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
b370: 65 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 eturn a section
b380: 6f 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 of the pPage->aD
b390: 61 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c ata to the freel
b3a0: 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 ist..** The firs
b3b0: 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 t byte of the ne
b3c0: 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 w free block is
b3d0: 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 69 53 74 pPage->aData[iSt
b3e0: 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 art].** and the
b3f0: 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 size of the bloc
b400: 6b 20 69 73 20 69 53 69 7a 65 20 62 79 74 65 73 k is iSize bytes
b410: 2e 0a 2a 2a 0a 2a 2a 20 41 64 6a 61 63 65 6e 74 ..**.** Adjacent
b420: 20 66 72 65 65 62 6c 6f 63 6b 73 20 61 72 65 20 freeblocks are
b430: 63 6f 61 6c 65 73 63 65 64 2e 0a 2a 2a 0a 2a 2a coalesced..**.**
b440: 20 4e 6f 74 65 20 74 68 61 74 20 65 76 65 6e 20 Note that even
b450: 74 68 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 though the freeb
b460: 6c 6f 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 lock list was ch
b470: 65 63 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e ecked by btreeIn
b480: 69 74 50 61 67 65 28 29 2c 0a 2a 2a 20 74 68 61 itPage(),.** tha
b490: 74 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6e t routine will n
b4a0: 6f 74 20 64 65 74 65 63 74 20 6f 76 65 72 6c 61 ot detect overla
b4b0: 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 73 20 p between cells
b4c0: 6f 72 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 or freeblocks.
b4d0: 4e 6f 72 0a 2a 2a 20 64 6f 65 73 20 69 74 20 64 Nor.** does it d
b4e0: 65 74 65 63 74 20 63 65 6c 6c 73 20 6f 72 20 66 etect cells or f
b4f0: 72 65 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 65 reeblocks that e
b500: 6e 63 72 6f 75 63 68 20 69 6e 74 6f 20 74 68 65 ncrouch into the
b510: 20 72 65 73 65 72 76 65 64 20 62 79 74 65 73 0a reserved bytes.
b520: 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 ** at the end of
b530: 20 74 68 65 20 70 61 67 65 2e 20 20 53 6f 20 64 the page. So d
b540: 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 72 o additional cor
b550: 72 75 70 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 ruption checks i
b560: 6e 73 69 64 65 20 74 68 69 73 0a 2a 2a 20 72 6f nside this.** ro
b570: 75 74 69 6e 65 20 61 6e 64 20 72 65 74 75 72 6e utine and return
b580: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
b590: 69 66 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 20 if any problems
b5a0: 61 72 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 are found..*/.st
b5b0: 61 74 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 atic int freeSpa
b5c0: 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ce(MemPage *pPag
b5d0: 65 2c 20 75 31 36 20 69 53 74 61 72 74 2c 20 75 e, u16 iStart, u
b5e0: 31 36 20 69 53 69 7a 65 29 7b 0a 20 20 75 31 36 16 iSize){. u16
b5f0: 20 69 50 74 72 3b 20 20 20 20 20 20 20 20 20 20 iPtr;
b600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b610: 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
b620: 20 70 74 72 20 74 6f 20 6e 65 78 74 20 66 72 65 ptr to next fre
b630: 65 62 6c 6f 63 6b 20 2a 2f 0a 20 20 75 31 36 20 eblock */. u16
b640: 69 46 72 65 65 42 6c 6b 3b 20 20 20 20 20 20 20 iFreeBlk;
b650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b660: 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
b670: 74 68 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f the next freeblo
b680: 63 6b 20 2a 2f 0a 20 20 75 38 20 68 64 72 3b 20 ck */. u8 hdr;
b690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b6a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
b6b0: 20 50 61 67 65 20 68 65 61 64 65 72 20 73 69 7a Page header siz
b6c0: 65 2e 20 20 30 20 6f 72 20 31 30 30 20 2a 2f 0a e. 0 or 100 */.
b6d0: 20 20 75 38 20 6e 46 72 61 67 20 3d 20 30 3b 20 u8 nFrag = 0;
b6e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b6f0: 20 20 20 20 20 20 20 20 2f 2a 20 52 65 64 75 63 /* Reduc
b700: 74 69 6f 6e 20 69 6e 20 66 72 61 67 6d 65 6e 74 tion in fragment
b710: 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 31 36 20 69 ation */. u16 i
b720: 4f 72 69 67 53 69 7a 65 20 3d 20 69 53 69 7a 65 OrigSize = iSize
b730: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b740: 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 76 61 6c /* Original val
b750: 75 65 20 6f 66 20 69 53 69 7a 65 20 2a 2f 0a 20 ue of iSize */.
b760: 20 75 33 32 20 69 4c 61 73 74 20 3d 20 70 50 61 u32 iLast = pPa
b770: 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
b780: 69 7a 65 2d 34 3b 20 2f 2a 20 4c 61 72 67 65 73 ize-4; /* Larges
b790: 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65 65 62 t possible freeb
b7a0: 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 lock offset */.
b7b0: 20 75 33 32 20 69 45 6e 64 20 3d 20 69 53 74 61 u32 iEnd = iSta
b7c0: 72 74 20 2b 20 69 53 69 7a 65 3b 20 20 20 20 20 rt + iSize;
b7d0: 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
b7e0: 62 79 74 65 20 70 61 73 74 20 74 68 65 20 69 53 byte past the iS
b7f0: 74 61 72 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 tart buffer */.
b800: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b810: 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
b820: 61 74 61 3b 20 20 20 2f 2a 20 50 61 67 65 20 63 ata; /* Page c
b830: 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 ontent */.. ass
b840: 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 ert( pPage->pBt!
b850: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
b860: 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
b870: 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
b880: 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
b890: 65 72 74 28 20 69 53 74 61 72 74 3e 3d 70 50 61 ert( iStart>=pPa
b8a0: 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b ge->hdrOffset+6+
b8b0: 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
b8c0: 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ize );. assert(
b8d0: 20 43 4f 52 52 55 50 54 5f 44 42 20 7c 7c 20 69 CORRUPT_DB || i
b8e0: 45 6e 64 20 3c 3d 20 70 50 61 67 65 2d 3e 70 42 End <= pPage->pB
b8f0: 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b t->usableSize );
b900: 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
b910: 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
b920: 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
b930: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 );. assert( iS
b940: 69 7a 65 3e 3d 34 20 29 3b 20 20 20 2f 2a 20 4d ize>=4 ); /* M
b950: 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
b960: 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 72 is 4 */. asser
b970: 74 28 20 69 53 74 61 72 74 3c 3d 69 4c 61 73 74 t( iStart<=iLast
b980: 20 29 3b 0a 0a 20 20 2f 2a 20 4f 76 65 72 77 72 );.. /* Overwr
b990: 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f ite deleted info
b9a0: 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 rmation with zer
b9b0: 6f 73 20 77 68 65 6e 20 74 68 65 20 73 65 63 75 os when the secu
b9c0: 72 65 5f 64 65 6c 65 74 65 0a 20 20 2a 2a 20 6f re_delete. ** o
b9d0: 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 ption is enabled
b9e0: 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 2d */. if( pPage-
b9f0: 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 >pBt->btsFlags &
ba00: 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 BTS_SECURE_DELE
ba10: 54 45 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 TE ){. memset
ba20: 28 26 64 61 74 61 5b 69 53 74 61 72 74 5d 2c 20 (&data[iStart],
ba30: 30 2c 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 0, iSize);. }..
ba40: 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 /* The list of
ba50: 20 66 72 65 65 62 6c 6f 63 6b 73 20 6d 75 73 74 freeblocks must
ba60: 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 be in ascending
ba70: 20 6f 72 64 65 72 2e 20 20 46 69 6e 64 20 74 68 order. Find th
ba80: 65 20 0a 20 20 2a 2a 20 73 70 6f 74 20 6f 6e 20 e . ** spot on
ba90: 74 68 65 20 6c 69 73 74 20 77 68 65 72 65 20 69 the list where i
baa0: 53 74 61 72 74 20 73 68 6f 75 6c 64 20 62 65 20 Start should be
bab0: 69 6e 73 65 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 inserted.. */.
bac0: 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 hdr = pPage->hd
bad0: 72 4f 66 66 73 65 74 3b 0a 20 20 69 50 74 72 20 rOffset;. iPtr
bae0: 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 69 66 28 = hdr + 1;. if(
baf0: 20 64 61 74 61 5b 69 50 74 72 2b 31 5d 3d 3d 30 data[iPtr+1]==0
bb00: 20 26 26 20 64 61 74 61 5b 69 50 74 72 5d 3d 3d && data[iPtr]==
bb10: 30 20 29 7b 0a 20 20 20 20 69 46 72 65 65 42 6c 0 ){. iFreeBl
bb20: 6b 20 3d 20 30 3b 20 20 2f 2a 20 53 68 6f 72 74 k = 0; /* Short
bb30: 63 75 74 20 66 6f 72 20 74 68 65 20 63 61 73 65 cut for the case
bb40: 20 77 68 65 6e 20 74 68 65 20 66 72 65 65 6c 69 when the freeli
bb50: 73 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 st is empty */.
bb60: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c }else{. whil
bb70: 65 28 20 28 69 46 72 65 65 42 6c 6b 20 3d 20 67 e( (iFreeBlk = g
bb80: 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 50 et2byte(&data[iP
bb90: 74 72 5d 29 29 3e 30 20 26 26 20 69 46 72 65 65 tr]))>0 && iFree
bba0: 42 6c 6b 3c 69 53 74 61 72 74 20 29 7b 0a 20 20 Blk<iStart ){.
bbb0: 20 20 20 20 69 66 28 20 69 46 72 65 65 42 6c 6b if( iFreeBlk
bbc0: 3c 69 50 74 72 2b 34 20 29 20 72 65 74 75 72 6e <iPtr+4 ) return
bbd0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
bbe0: 42 4b 50 54 3b 0a 20 20 20 20 20 20 69 50 74 72 BKPT;. iPtr
bbf0: 20 3d 20 69 46 72 65 65 42 6c 6b 3b 0a 20 20 20 = iFreeBlk;.
bc00: 20 7d 0a 20 20 20 20 69 66 28 20 69 46 72 65 65 }. if( iFree
bc10: 42 6c 6b 3e 69 4c 61 73 74 20 29 20 72 65 74 75 Blk>iLast ) retu
bc20: 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
bc30: 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73 65 T_BKPT;. asse
bc40: 72 74 28 20 69 46 72 65 65 42 6c 6b 3e 69 50 74 rt( iFreeBlk>iPt
bc50: 72 20 7c 7c 20 69 46 72 65 65 42 6c 6b 3d 3d 30 r || iFreeBlk==0
bc60: 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 41 74 );. . /* At
bc70: 20 74 68 69 73 20 70 6f 69 6e 74 3a 0a 20 20 20 this point:.
bc80: 20 2a 2a 20 20 20 20 69 46 72 65 65 42 6c 6b 3a ** iFreeBlk:
bc90: 20 20 20 46 69 72 73 74 20 66 72 65 65 62 6c 6f First freeblo
bca0: 63 6b 20 61 66 74 65 72 20 69 53 74 61 72 74 2c ck after iStart,
bcb0: 20 6f 72 20 7a 65 72 6f 20 69 66 20 6e 6f 6e 65 or zero if none
bcc0: 0a 20 20 20 20 2a 2a 20 20 20 20 69 50 74 72 3a . ** iPtr:
bcd0: 20 20 20 20 20 20 20 54 68 65 20 61 64 64 72 65 The addre
bce0: 73 73 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 ss of a pointer
bcf0: 69 46 72 65 65 42 6c 6b 0a 20 20 20 20 2a 2a 0a iFreeBlk. **.
bd00: 20 20 20 20 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ** Check to
bd10: 73 65 65 20 69 66 20 69 46 72 65 65 42 6c 6b 20 see if iFreeBlk
bd20: 73 68 6f 75 6c 64 20 62 65 20 63 6f 61 6c 65 73 should be coales
bd30: 63 65 64 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 ced onto the end
bd40: 20 6f 66 20 69 53 74 61 72 74 2e 0a 20 20 20 20 of iStart..
bd50: 2a 2f 0a 20 20 20 20 69 66 28 20 69 46 72 65 65 */. if( iFree
bd60: 42 6c 6b 20 26 26 20 69 45 6e 64 2b 33 3e 3d 69 Blk && iEnd+3>=i
bd70: 46 72 65 65 42 6c 6b 20 29 7b 0a 20 20 20 20 20 FreeBlk ){.
bd80: 20 6e 46 72 61 67 20 3d 20 69 46 72 65 65 42 6c nFrag = iFreeBl
bd90: 6b 20 2d 20 69 45 6e 64 3b 0a 20 20 20 20 20 20 k - iEnd;.
bda0: 69 66 28 20 69 45 6e 64 3e 69 46 72 65 65 42 6c if( iEnd>iFreeBl
bdb0: 6b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 k ) return SQLIT
bdc0: 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
bdd0: 20 20 20 20 20 20 69 45 6e 64 20 3d 20 69 46 72 iEnd = iFr
bde0: 65 65 42 6c 6b 20 2b 20 67 65 74 32 62 79 74 65 eeBlk + get2byte
bdf0: 28 26 64 61 74 61 5b 69 46 72 65 65 42 6c 6b 2b (&data[iFreeBlk+
be00: 32 5d 29 3b 0a 20 20 20 20 20 20 69 53 69 7a 65 2]);. iSize
be10: 20 3d 20 69 45 6e 64 20 2d 20 69 53 74 61 72 74 = iEnd - iStart
be20: 3b 0a 20 20 20 20 20 20 69 46 72 65 65 42 6c 6b ;. iFreeBlk
be30: 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
be40: 61 5b 69 46 72 65 65 42 6c 6b 5d 29 3b 0a 20 20 a[iFreeBlk]);.
be50: 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
be60: 20 69 50 74 72 20 69 73 20 61 6e 6f 74 68 65 72 iPtr is another
be70: 20 66 72 65 65 62 6c 6f 63 6b 20 28 74 68 61 74 freeblock (that
be80: 20 69 73 2c 20 69 66 20 69 50 74 72 20 69 73 20 is, if iPtr is
be90: 6e 6f 74 20 74 68 65 20 66 72 65 65 6c 69 73 74 not the freelist
bea0: 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 . ** pointer
beb0: 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 in the page head
bec0: 65 72 29 20 74 68 65 6e 20 63 68 65 63 6b 20 74 er) then check t
bed0: 6f 20 73 65 65 20 69 66 20 69 53 74 61 72 74 20 o see if iStart
bee0: 73 68 6f 75 6c 64 20 62 65 0a 20 20 20 20 2a 2a should be. **
bef0: 20 63 6f 61 6c 65 73 63 65 64 20 6f 6e 74 6f 20 coalesced onto
bf00: 74 68 65 20 65 6e 64 20 6f 66 20 69 50 74 72 2e the end of iPtr.
bf10: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
bf20: 69 50 74 72 3e 68 64 72 2b 31 20 29 7b 0a 20 20 iPtr>hdr+1 ){.
bf30: 20 20 20 20 69 6e 74 20 69 50 74 72 45 6e 64 20 int iPtrEnd
bf40: 3d 20 69 50 74 72 20 2b 20 67 65 74 32 62 79 74 = iPtr + get2byt
bf50: 65 28 26 64 61 74 61 5b 69 50 74 72 2b 32 5d 29 e(&data[iPtr+2])
bf60: 3b 0a 20 20 20 20 20 20 69 66 28 20 69 50 74 72 ;. if( iPtr
bf70: 45 6e 64 2b 33 3e 3d 69 53 74 61 72 74 20 29 7b End+3>=iStart ){
bf80: 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 50 74 . if( iPt
bf90: 72 45 6e 64 3e 69 53 74 61 72 74 20 29 20 72 65 rEnd>iStart ) re
bfa0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
bfb0: 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
bfc0: 20 20 6e 46 72 61 67 20 2b 3d 20 69 53 74 61 72 nFrag += iStar
bfd0: 74 20 2d 20 69 50 74 72 45 6e 64 3b 0a 20 20 20 t - iPtrEnd;.
bfe0: 20 20 20 20 20 69 53 69 7a 65 20 3d 20 69 45 6e iSize = iEn
bff0: 64 20 2d 20 69 50 74 72 3b 0a 20 20 20 20 20 20 d - iPtr;.
c000: 20 20 69 53 74 61 72 74 20 3d 20 69 50 74 72 3b iStart = iPtr;
c010: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
c020: 20 20 20 69 66 28 20 6e 46 72 61 67 3e 64 61 74 if( nFrag>dat
c030: 61 5b 68 64 72 2b 37 5d 20 29 20 72 65 74 75 72 a[hdr+7] ) retur
c040: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
c050: 5f 42 4b 50 54 3b 0a 20 20 20 20 64 61 74 61 5b _BKPT;. data[
c060: 68 64 72 2b 37 5d 20 2d 3d 20 6e 46 72 61 67 3b hdr+7] -= nFrag;
c070: 0a 20 20 7d 0a 20 20 69 66 28 20 69 53 74 61 72 . }. if( iStar
c080: 74 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 t==get2byte(&dat
c090: 61 5b 68 64 72 2b 35 5d 29 20 29 7b 0a 20 20 20 a[hdr+5]) ){.
c0a0: 20 2f 2a 20 54 68 65 20 6e 65 77 20 66 72 65 65 /* The new free
c0b0: 62 6c 6f 63 6b 20 69 73 20 61 74 20 74 68 65 20 block is at the
c0c0: 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
c0d0: 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
c0e0: 65 61 2c 0a 20 20 20 20 2a 2a 20 73 6f 20 6a 75 ea,. ** so ju
c0f0: 73 74 20 65 78 74 65 6e 64 20 74 68 65 20 63 65 st extend the ce
c100: 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
c110: 72 61 74 68 65 72 20 74 68 61 6e 20 63 72 65 61 rather than crea
c120: 74 65 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a te another. *
c130: 2a 20 66 72 65 65 6c 69 73 74 20 65 6e 74 72 79 * freelist entry
c140: 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 74 72 */. if( iPtr
c150: 21 3d 68 64 72 2b 31 20 29 20 72 65 74 75 72 6e !=hdr+1 ) return
c160: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
c170: 42 4b 50 54 3b 0a 20 20 20 20 70 75 74 32 62 79 BKPT;. put2by
c180: 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c te(&data[hdr+1],
c190: 20 69 46 72 65 65 42 6c 6b 29 3b 0a 20 20 20 20 iFreeBlk);.
c1a0: 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
c1b0: 64 72 2b 35 5d 2c 20 69 45 6e 64 29 3b 0a 20 20 dr+5], iEnd);.
c1c0: 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 6e }else{. /* In
c1d0: 73 65 72 74 20 74 68 65 20 6e 65 77 20 66 72 65 sert the new fre
c1e0: 65 62 6c 6f 63 6b 20 69 6e 74 6f 20 74 68 65 20 eblock into the
c1f0: 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 20 20 freelist */.
c200: 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 put2byte(&data[i
c210: 50 74 72 5d 2c 20 69 53 74 61 72 74 29 3b 0a 20 Ptr], iStart);.
c220: 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 put2byte(&dat
c230: 61 5b 69 53 74 61 72 74 5d 2c 20 69 46 72 65 65 a[iStart], iFree
c240: 42 6c 6b 29 3b 0a 20 20 20 20 70 75 74 32 62 79 Blk);. put2by
c250: 74 65 28 26 64 61 74 61 5b 69 53 74 61 72 74 2b te(&data[iStart+
c260: 32 5d 2c 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 2], iSize);. }.
c270: 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b pPage->nFree +
c280: 3d 20 69 4f 72 69 67 53 69 7a 65 3b 0a 20 20 72 = iOrigSize;. r
c290: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c2a0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 .}../*.** Decode
c2b0: 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
c2c0: 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 (the first byte
c2d0: 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 66 of the header) f
c2e0: 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64 or a page.** and
c2f0: 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c initialize fiel
c300: 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67 ds of the MemPag
c310: 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 6f e structure acco
c320: 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f rdingly..**.** O
c330: 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e nly the followin
c340: 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 61 g combinations a
c350: 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 41 re supported. A
c360: 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 6e nything differen
c370: 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 61 t.** indicates a
c380: 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
c390: 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 e files:.**.**
c3a0: 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 PTF_ZEROD
c3b0: 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 ATA.** P
c3c0: 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 54 TF_ZERODATA | PT
c3d0: 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 20 F_LEAF.**
c3e0: 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c PTF_LEAFDATA |
c3f0: 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 PTF_INTKEY.**
c400: 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 PTF_LEAFD
c410: 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 ATA | PTF_INTKEY
c420: 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 | PTF_LEAF.*/.s
c430: 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 65 tatic int decode
c440: 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70 Flags(MemPage *p
c450: 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79 Page, int flagBy
c460: 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 te){. BtShared
c470: 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63 *pBt; /* A c
c480: 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42 opy of pPage->pB
c490: 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
c4a0: 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
c4b0: 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d ==(pPage->pgno==
c4c0: 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b 0a 1 ? 100 : 0) );.
c4d0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
c4e0: 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
c4f0: 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
c500: 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 66 );. pPage->leaf
c510: 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 65 = (u8)(flagByte
c520: 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 50 >>3); assert( P
c530: 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20 TF_LEAF == 1<<3
c540: 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d );. flagByte &=
c550: 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50 ~PTF_LEAF;. pP
c560: 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
c570: 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c e = 4-4*pPage->l
c580: 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 eaf;. pBt = pPa
c590: 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 ge->pBt;. if( f
c5a0: 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45 lagByte==(PTF_LE
c5b0: 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 AFDATA | PTF_INT
c5c0: 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67 KEY) ){. pPag
c5d0: 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 e->intKey = 1;.
c5e0: 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 pPage->intKey
c5f0: 4c 65 61 66 20 3d 20 70 50 61 67 65 2d 3e 6c 65 Leaf = pPage->le
c600: 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e af;. pPage->n
c610: 6f 50 61 79 6c 6f 61 64 20 3d 20 21 70 50 61 67 oPayload = !pPag
c620: 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 e->leaf;. pPa
c630: 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 ge->maxLocal = p
c640: 42 74 2d 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 Bt->maxLeaf;.
c650: 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
c660: 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b = pBt->minLeaf;
c670: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 . }else if( fla
c680: 67 42 79 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 gByte==PTF_ZEROD
c690: 41 54 41 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ATA ){. pPage
c6a0: 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 ->intKey = 0;.
c6b0: 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 4c pPage->intKeyL
c6c0: 65 61 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 eaf = 0;. pPa
c6d0: 67 65 2d 3e 6e 6f 50 61 79 6c 6f 61 64 20 3d 20 ge->noPayload =
c6e0: 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 0;. pPage->ma
c6f0: 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 xLocal = pBt->ma
c700: 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 xLocal;. pPag
c710: 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 e->minLocal = pB
c720: 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d t->minLocal;. }
c730: 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
c740: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
c750: 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 70 50 61 67 BKPT;. }. pPag
c760: 65 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f e->max1bytePaylo
c770: 61 64 20 3d 20 70 42 74 2d 3e 6d 61 78 31 62 79 ad = pBt->max1by
c780: 74 65 50 61 79 6c 6f 61 64 3b 0a 20 20 72 65 74 tePayload;. ret
c790: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
c7a0: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
c7b0: 7a 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 ze the auxiliary
c7c0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 information for
c7d0: 20 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a a disk block..*
c7e0: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
c7f0: 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
c800: 2e 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 . If we see tha
c810: 74 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a t the page does.
c820: 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 ** not contain a
c830: 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 well-formed dat
c840: 61 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e abase page, then
c850: 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 return .** SQLI
c860: 54 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 TE_CORRUPT. Not
c870: 65 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 e that a return
c880: 6f 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 of SQLITE_OK doe
c890: 73 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 s not.** guarant
c8a0: 65 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ee that the page
c8b0: 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e is well-formed.
c8c0: 20 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 It only shows
c8d0: 74 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 that.** we faile
c8e0: 64 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 d to detect any
c8f0: 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 corruption..*/.s
c900: 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 tatic int btreeI
c910: 6e 69 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 nitPage(MemPage
c920: 2a 70 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 *pPage){.. asse
c930: 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d rt( pPage->pBt!=
c940: 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
c950: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
c960: 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
c970: 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
c980: 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 ( pPage->pgno==s
c990: 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e qlite3PagerPagen
c9a0: 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 umber(pPage->pDb
c9b0: 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
c9c0: 74 28 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 t( pPage == sqli
c9d0: 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
c9e0: 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
c9f0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
ca00: 61 67 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 age->aData == sq
ca10: 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
ca20: 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
ca30: 29 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 ) );.. if( !pPa
ca40: 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 ge->isInit ){.
ca50: 20 20 75 31 36 20 70 63 3b 20 20 20 20 20 20 20 u16 pc;
ca60: 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
ca70: 6f 66 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 of a freeblock w
ca80: 69 74 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 ithin pPage->aDa
ca90: 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 ta[] */. u8 h
caa0: 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f dr; /
cab0: 2a 20 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 * Offset to begi
cac0: 6e 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 nning of page he
cad0: 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a ader */. u8 *
cae0: 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f data; /
caf0: 2a 20 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 * Equal to pPage
cb00: 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 ->aData */. B
cb10: 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 tShared *pBt;
cb20: 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e /* The main
cb30: 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 btree structure
cb40: 20 2a 2f 0a 20 20 20 20 69 6e 74 20 75 73 61 62 */. int usab
cb50: 6c 65 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d leSize; /* Am
cb60: 6f 75 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 ount of usable s
cb70: 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 pace on each pag
cb80: 65 20 2a 2f 0a 20 20 20 20 75 31 36 20 63 65 6c e */. u16 cel
cb90: 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f lOffset; /* O
cba0: 66 66 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 ffset from start
cbb0: 20 6f 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 of page to firs
cbc0: 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a t cell pointer *
cbd0: 2f 0a 20 20 20 20 69 6e 74 20 6e 46 72 65 65 3b /. int nFree;
cbe0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
cbf0: 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 er of unused byt
cc00: 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a es on the page *
cc10: 2f 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 20 20 /. int top;
cc20: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
cc30: 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 t byte of the ce
cc40: 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
cc50: 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c */. int iCell
cc60: 46 69 72 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 First; /* Fir
cc70: 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c st allowable cel
cc80: 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f l or freeblock o
cc90: 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74 ffset */. int
cca0: 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 iCellLast;
ccb0: 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 /* Last possible
ccc0: 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f cell or freeblo
ccd0: 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 0a 20 20 ck offset */..
cce0: 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 pBt = pPage->p
ccf0: 42 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 Bt;.. hdr = p
cd00: 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
cd10: 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 . data = pPag
cd20: 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 e->aData;. if
cd30: 28 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 ( decodeFlags(pP
cd40: 61 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 age, data[hdr])
cd50: 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
cd60: 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
cd70: 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 assert( pBt->p
cd80: 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
cd90: 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 36 pBt->pageSize<=6
cda0: 35 35 33 36 20 29 3b 0a 20 20 20 20 70 50 61 67 5536 );. pPag
cdb0: 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 28 75 e->maskPage = (u
cdc0: 31 36 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 16)(pBt->pageSiz
cdd0: 65 20 2d 20 31 29 3b 0a 20 20 20 20 70 50 61 67 e - 1);. pPag
cde0: 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 e->nOverflow = 0
cdf0: 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 ;. usableSize
ce00: 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
ce10: 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 ze;. pPage->c
ce20: 65 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c ellOffset = cell
ce30: 4f 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 31 Offset = hdr + 1
ce40: 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 2 - 4*pPage->lea
ce50: 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 44 f;. pPage->aD
ce60: 61 74 61 45 6e 64 20 3d 20 26 64 61 74 61 5b 75 ataEnd = &data[u
ce70: 73 61 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 20 20 sableSize];.
ce80: 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 20 pPage->aCellIdx
ce90: 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 = &data[cellOffs
cea0: 65 74 5d 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 et];. top = g
ceb0: 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 26 et2byteNotZero(&
cec0: 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 data[hdr+5]);.
ced0: 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d pPage->nCell =
cee0: 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
cef0: 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 hdr+3]);. if(
cf00: 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 pPage->nCell>MX
cf10: 5f 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 _CELL(pBt) ){.
cf20: 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 /* To many c
cf30: 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c ells for a singl
cf40: 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 e page. The pag
cf50: 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 e must be corrup
cf60: 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 t */. retur
cf70: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
cf80: 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
cf90: 20 74 65 73 74 63 61 73 65 28 20 70 50 61 67 65 testcase( pPage
cfa0: 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c ->nCell==MX_CELL
cfb0: 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 2f 2a (pBt) );.. /*
cfc0: 20 41 20 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 A malformed dat
cfd0: 61 62 61 73 65 20 70 61 67 65 20 6d 69 67 68 74 abase page might
cfe0: 20 63 61 75 73 65 20 75 73 20 74 6f 20 72 65 61 cause us to rea
cff0: 64 20 70 61 73 74 20 74 68 65 20 65 6e 64 0a 20 d past the end.
d000: 20 20 20 2a 2a 20 6f 66 20 70 61 67 65 20 77 68 ** of page wh
d010: 65 6e 20 70 61 72 73 69 6e 67 20 61 20 63 65 6c en parsing a cel
d020: 6c 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 l. . **.
d030: 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
d040: 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 block of code c
d050: 68 65 63 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 hecks early to s
d060: 65 65 20 69 66 20 61 20 63 65 6c 6c 20 65 78 74 ee if a cell ext
d070: 65 6e 64 73 0a 20 20 20 20 2a 2a 20 70 61 73 74 ends. ** past
d080: 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 70 61 the end of a pa
d090: 67 65 20 62 6f 75 6e 64 61 72 79 20 61 6e 64 20 ge boundary and
d0a0: 63 61 75 73 65 73 20 53 51 4c 49 54 45 5f 43 4f causes SQLITE_CO
d0b0: 52 52 55 50 54 20 74 6f 20 62 65 20 0a 20 20 20 RRUPT to be .
d0c0: 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 ** returned if
d0d0: 69 74 20 64 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a it does.. */.
d0e0: 20 20 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d iCellFirst =
d0f0: 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
d100: 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 pPage->nCell;.
d110: 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 iCellLast = us
d120: 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 23 69 ableSize - 4;.#i
d130: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
d140: 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 _ENABLE_OVERSIZE
d150: 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 _CELL_CHECK).
d160: 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 {. int i;
d170: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
d180: 64 65 78 20 69 6e 74 6f 20 74 68 65 20 63 65 6c dex into the cel
d190: 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 l pointer array
d1a0: 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b */. int sz;
d1b0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
d1c0: 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a ze of a cell */.
d1d0: 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 . if( !pPag
d1e0: 65 2d 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c e->leaf ) iCellL
d1f0: 61 73 74 2d 2d 3b 0a 20 20 20 20 20 20 66 6f 72 ast--;. for
d200: 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e (i=0; i<pPage->n
d210: 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
d220: 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 pc = get2byt
d230: 65 28 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 e(&data[cellOffs
d240: 65 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 et+i*2]);.
d250: 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d testcase( pc==
d260: 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 iCellFirst );.
d270: 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
d280: 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b pc==iCellLast );
d290: 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 63 3c . if( pc<
d2a0: 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 iCellFirst || pc
d2b0: 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 >iCellLast ){.
d2c0: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
d2d0: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
d2e0: 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 PT;. }.
d2f0: 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 sz = cellS
d300: 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 izePtr(pPage, &d
d310: 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 ata[pc]);.
d320: 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 testcase( pc+s
d330: 7a 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b z==usableSize );
d340: 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 63 2b . if( pc+
d350: 73 7a 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b sz>usableSize ){
d360: 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
d370: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
d380: 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 7d _BKPT;. }
d390: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
d3a0: 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
d3b0: 29 20 69 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 ) iCellLast++;.
d3c0: 20 20 20 7d 20 20 0a 23 65 6e 64 69 66 0a 0a 20 } .#endif..
d3d0: 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 /* Compute th
d3e0: 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 e total free spa
d3f0: 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a ce on the page *
d400: 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 /. pc = get2b
d410: 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
d420: 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64 );. nFree = d
d430: 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 ata[hdr+7] + top
d440: 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e ;. while( pc>
d450: 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36 20 6e 0 ){. u16 n
d460: 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 ext, size;.
d470: 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 if( pc<iCellFir
d480: 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 st || pc>iCellLa
d490: 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a st ){. /*
d4a0: 20 53 74 61 72 74 20 6f 66 20 66 72 65 65 20 62 Start of free b
d4b0: 6c 6f 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 lock is off the
d4c0: 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 page */.
d4d0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
d4e0: 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 RRUPT_BKPT; .
d4f0: 20 20 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 }. next
d500: 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
d510: 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a [pc]);. siz
d520: 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 e = get2byte(&da
d530: 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 ta[pc+2]);.
d540: 20 69 66 28 20 28 6e 65 78 74 3e 30 20 26 26 20 if( (next>0 &&
d550: 6e 65 78 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 next<=pc+size+3)
d560: 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 || pc+size>usab
d570: 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
d580: 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 /* Free blocks
d590: 20 6d 75 73 74 20 62 65 20 69 6e 20 61 73 63 65 must be in asce
d5a0: 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 41 6e 64 nding order. And
d5b0: 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f the last byte o
d5c0: 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 f. ** the
d5d0: 20 66 72 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 free-block must
d5e0: 20 6c 69 65 20 6f 6e 20 74 68 65 20 64 61 74 61 lie on the data
d5f0: 62 61 73 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 base page. */.
d600: 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
d610: 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
d620: 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 T; . }.
d630: 20 20 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 20 nFree = nFree
d640: 2b 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 + size;. pc
d650: 20 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a = next;. }..
d660: 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 /* At this p
d670: 6f 69 6e 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 oint, nFree cont
d680: 61 69 6e 73 20 74 68 65 20 73 75 6d 20 6f 66 20 ains the sum of
d690: 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 the offset to th
d6a0: 65 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f e start. ** o
d6b0: 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 f the cell-conte
d6c0: 6e 74 20 61 72 65 61 20 70 6c 75 73 20 74 68 65 nt area plus the
d6d0: 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 number of free
d6e0: 62 79 74 65 73 20 77 69 74 68 69 6e 0a 20 20 20 bytes within.
d6f0: 20 2a 2a 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e ** the cell-con
d700: 74 65 6e 74 20 61 72 65 61 2e 20 49 66 20 74 68 tent area. If th
d710: 69 73 20 69 73 20 67 72 65 61 74 65 72 20 74 68 is is greater th
d720: 61 6e 20 74 68 65 20 75 73 61 62 6c 65 2d 73 69 an the usable-si
d730: 7a 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 ze. ** of the
d740: 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 page, then the
d750: 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 page must be cor
d760: 72 75 70 74 65 64 2e 20 54 68 69 73 20 63 68 65 rupted. This che
d770: 63 6b 20 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 ck also. ** s
d780: 65 72 76 65 73 20 74 6f 20 76 65 72 69 66 79 20 erves to verify
d790: 74 68 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 that the offset
d7a0: 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
d7b0: 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 the cell-content
d7c0: 0a 20 20 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 . ** area, ac
d7d0: 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 70 cording to the p
d7e0: 61 67 65 20 68 65 61 64 65 72 2c 20 6c 69 65 73 age header, lies
d7f0: 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 within the page
d800: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
d810: 20 6e 46 72 65 65 3e 75 73 61 62 6c 65 53 69 7a nFree>usableSiz
d820: 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 e ){. retur
d830: 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
d840: 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 _BKPT; . }.
d850: 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d pPage->nFree =
d860: 20 28 75 31 36 29 28 6e 46 72 65 65 20 2d 20 69 (u16)(nFree - i
d870: 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 CellFirst);.
d880: 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
d890: 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
d8a0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
d8b0: 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 .** Set up a raw
d8c0: 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 page so that it
d8d0: 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 looks like a da
d8e0: 74 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 tabase page hold
d8f0: 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 ing.** no entrie
d900: 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
d910: 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 d zeroPage(MemPa
d920: 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 ge *pPage, int f
d930: 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 lags){. unsigne
d940: 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 d char *data = p
d950: 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 Page->aData;. B
d960: 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
d970: 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 Page->pBt;. u8
d980: 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
d990: 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 Offset;. u16 fi
d9a0: 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rst;.. assert(
d9b0: 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
d9c0: 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 number(pPage->pD
d9d0: 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 bPage)==pPage->p
d9e0: 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 gno );. assert(
d9f0: 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
da00: 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 Extra(pPage->pDb
da10: 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 Page) == (void*)
da20: 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 pPage );. asser
da30: 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
da40: 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 etData(pPage->pD
da50: 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 bPage) == data )
da60: 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
da70: 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
da80: 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
da90: 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
daa0: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
dab0: 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
dac0: 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 74 );. if( pBt->bt
dad0: 73 46 6c 61 67 73 20 26 20 42 54 53 5f 53 45 43 sFlags & BTS_SEC
dae0: 55 52 45 5f 44 45 4c 45 54 45 20 29 7b 0a 20 20 URE_DELETE ){.
daf0: 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 68 memset(&data[h
db00: 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 dr], 0, pBt->usa
db10: 62 6c 65 53 69 7a 65 20 2d 20 68 64 72 29 3b 0a bleSize - hdr);.
db20: 20 20 7d 0a 20 20 64 61 74 61 5b 68 64 72 5d 20 }. data[hdr]
db30: 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b 0a 20 = (char)flags;.
db40: 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b 20 28 first = hdr + (
db50: 28 66 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 29 (flags&PTF_LEAF)
db60: 3d 3d 30 20 3f 20 31 32 20 3a 20 38 29 3b 0a 20 ==0 ? 12 : 8);.
db70: 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 memset(&data[hd
db80: 72 2b 31 5d 2c 20 30 2c 20 34 29 3b 0a 20 20 64 r+1], 0, 4);. d
db90: 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a ata[hdr+7] = 0;.
dba0: 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
dbb0: 5b 68 64 72 2b 35 5d 2c 20 70 42 74 2d 3e 75 73 [hdr+5], pBt->us
dbc0: 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 ableSize);. pPa
dbd0: 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 ge->nFree = (u16
dbe0: 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a )(pBt->usableSiz
dbf0: 65 20 2d 20 66 69 72 73 74 29 3b 0a 20 20 64 65 e - first);. de
dc00: 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c codeFlags(pPage,
dc10: 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 flags);. pPage
dc20: 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 ->cellOffset = f
dc30: 69 72 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 61 irst;. pPage->a
dc40: 44 61 74 61 45 6e 64 20 3d 20 26 64 61 74 61 5b DataEnd = &data[
dc50: 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d pBt->usableSize]
dc60: 3b 0a 20 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c ;. pPage->aCell
dc70: 49 64 78 20 3d 20 26 64 61 74 61 5b 66 69 72 73 Idx = &data[firs
dc80: 74 5d 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 t];. pPage->nOv
dc90: 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 erflow = 0;. as
dca0: 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 sert( pBt->pageS
dcb0: 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d ize>=512 && pBt-
dcc0: 3e 70 61 67 65 53 69 7a 65 3c 3d 36 35 35 33 36 >pageSize<=65536
dcd0: 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 );. pPage->mas
dce0: 6b 50 61 67 65 20 3d 20 28 75 31 36 29 28 70 42 kPage = (u16)(pB
dcf0: 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 29 t->pageSize - 1)
dd00: 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ;. pPage->nCell
dd10: 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69 = 0;. pPage->i
dd20: 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f sInit = 1;.}.../
dd30: 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 44 *.** Convert a D
dd40: 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64 20 66 bPage obtained f
dd50: 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 69 6e rom the pager in
dd60: 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 73 65 to a MemPage use
dd70: 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 72 65 d by.** the btre
dd80: 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 61 74 e layer..*/.stat
dd90: 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 ic MemPage *btre
dda0: 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 ePageFromDbPage(
ddb0: 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c DbPage *pDbPage,
ddc0: 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 53 68 Pgno pgno, BtSh
ddd0: 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 ared *pBt){. Me
dde0: 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 28 mPage *pPage = (
ddf0: 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 65 33 MemPage*)sqlite3
de00: 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 PagerGetExtra(pD
de10: 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d bPage);. pPage-
de20: 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 >aData = sqlite3
de30: 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 PagerGetData(pDb
de40: 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e Page);. pPage->
de50: 70 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 pDbPage = pDbPag
de60: 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 74 20 e;. pPage->pBt
de70: 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65 2d 3e = pBt;. pPage->
de80: 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 70 pgno = pgno;. p
de90: 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 Page->hdrOffset
dea0: 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 = pPage->pgno==1
deb0: 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 72 65 ? 100 : 0;. re
dec0: 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d 0a 0a turn pPage; .}..
ded0: 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 65 /*.** Get a page
dee0: 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 2e from the pager.
def0: 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 Initialize the
df00: 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
df10: 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
df20: 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
df30: 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 eded..**.** If t
df40: 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 he noContent fla
df50: 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d 65 61 g is set, it mea
df60: 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f ns that we do no
df70: 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a 2a 20 t care about.**
df80: 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
df90: 68 65 20 70 61 67 65 20 61 74 20 74 68 69 73 20 he page at this
dfa0: 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 time. So do not
dfb0: 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 0a go to the disk.
dfc0: 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 ** to fetch the
dfd0: 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 20 66 content. Just f
dfe0: 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 ill in the conte
dff0: 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 66 6f nt with zeros fo
e000: 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 6e 20 r now..** If in
e010: 74 68 65 20 66 75 74 75 72 65 20 77 65 20 63 61 the future we ca
e020: 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 ll sqlite3PagerW
e030: 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 20 70 rite() on this p
e040: 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d 65 61 age, that.** mea
e050: 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 72 74 ns we have start
e060: 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 72 6e ed to be concern
e070: 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 6e 74 ed about content
e080: 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a 2a 2a and the disk.**
e090: 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 read should occ
e0a0: 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 ur at that point
e0b0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e0c0: 62 74 72 65 65 47 65 74 50 61 67 65 28 0a 20 20 btreeGetPage(.
e0d0: 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
e0e0: 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 /* The btre
e0f0: 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
e100: 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
e110: 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
e120: 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 e to fetch */.
e130: 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 MemPage **ppPage
e140: 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 , /* Return t
e150: 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 he page in this
e160: 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 69 parameter */. i
e170: 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 nt flags
e180: 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 47 45 54 /* PAGER_GET
e190: 5f 4e 4f 43 4f 4e 54 45 4e 54 20 6f 72 20 50 41 _NOCONTENT or PA
e1a0: 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c 59 GER_GET_READONLY
e1b0: 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
e1c0: 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
e1d0: 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 66 ge;.. assert( f
e1e0: 6c 61 67 73 3d 3d 30 20 7c 7c 20 66 6c 61 67 73 lags==0 || flags
e1f0: 3d 3d 50 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f ==PAGER_GET_NOCO
e200: 4e 54 45 4e 54 20 7c 7c 20 66 6c 61 67 73 3d 3d NTENT || flags==
e210: 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e PAGER_GET_READON
e220: 4c 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 LY );. assert(
e230: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
e240: 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
e250: 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
e260: 50 61 67 65 72 41 63 71 75 69 72 65 28 70 42 74 PagerAcquire(pBt
e270: 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 ->pPager, pgno,
e280: 28 44 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 (DbPage**)&pDbPa
e290: 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 69 66 ge, flags);. if
e2a0: 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
e2b0: 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 ;. *ppPage = bt
e2c0: 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
e2d0: 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
e2e0: 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 pBt);. return
e2f0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
e300: 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 .** Retrieve a p
e310: 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
e320: 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 er cache. If the
e330: 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
e340: 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 is not.** alread
e350: 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 y in the pager c
e360: 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c ache return NULL
e370: 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 . Initialize the
e380: 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
e390: 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
e3a0: 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
e3b0: 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
e3c0: 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 MemPage *btreePa
e3d0: 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 geLookup(BtShare
e3e0: 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
e3f0: 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
e400: 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 bPage;. assert(
e410: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
e420: 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
e430: 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 );. pDbPage = s
e440: 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
e450: 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 p(pBt->pPager, p
e460: 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 gno);. if( pDbP
e470: 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 age ){. retur
e480: 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 n btreePageFromD
e490: 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 bPage(pDbPage, p
e4a0: 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 gno, pBt);. }.
e4b0: 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
e4c0: 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
e4d0: 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
e4e0: 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 ase file in page
e4f0: 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 s. If there is a
e500: 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 ny kind of.** er
e510: 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e ror, return ((un
e520: 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a signed int)-1)..
e530: 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 62 */.static Pgno b
e540: 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 42 74 treePagecount(Bt
e550: 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
e560: 72 65 74 75 72 6e 20 70 42 74 2d 3e 6e 50 61 67 return pBt->nPag
e570: 65 3b 0a 7d 0a 75 33 32 20 73 71 6c 69 74 65 33 e;.}.u32 sqlite3
e580: 42 74 72 65 65 4c 61 73 74 50 61 67 65 28 42 74 BtreeLastPage(Bt
e590: 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
e5a0: 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
e5b0: 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
e5c0: 20 20 61 73 73 65 72 74 28 20 28 28 70 2d 3e 70 assert( ((p->p
e5d0: 42 74 2d 3e 6e 50 61 67 65 29 26 30 78 38 30 30 Bt->nPage)&0x800
e5e0: 30 30 30 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 0000)==0 );. re
e5f0: 74 75 72 6e 20 62 74 72 65 65 50 61 67 65 63 6f turn btreePageco
e600: 75 6e 74 28 70 2d 3e 70 42 74 29 3b 0a 7d 0a 0a unt(p->pBt);.}..
e610: 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 65 /*.** Get a page
e620: 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
e630: 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 and initialize i
e640: 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 t. This routine
e650: 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a 20 63 6f is just a.** co
e660: 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 nvenience wrappe
e670: 72 20 61 72 6f 75 6e 64 20 73 65 70 61 72 61 74 r around separat
e680: 65 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 e calls to btree
e690: 47 65 74 50 61 67 65 28 29 20 61 6e 64 20 0a 2a GetPage() and .*
e6a0: 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 * btreeInitPage(
e6b0: 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 )..**.** If an e
e6c0: 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 rror occurs, the
e6d0: 6e 20 74 68 65 20 76 61 6c 75 65 20 2a 70 70 50 n the value *ppP
e6e0: 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 69 73 age is set to is
e6f0: 20 75 6e 64 65 66 69 6e 65 64 2e 20 49 74 0a 2a undefined. It.*
e700: 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20 75 6e 63 * may remain unc
e710: 68 61 6e 67 65 64 2c 20 6f 72 20 69 74 20 6d 61 hanged, or it ma
e720: 79 20 62 65 20 73 65 74 20 74 6f 20 61 6e 20 69 y be set to an i
e730: 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e 0a 2a 2f nvalid value..*/
e740: 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 41 .static int getA
e750: 6e 64 49 6e 69 74 50 61 67 65 28 0a 20 20 42 74 ndInitPage(. Bt
e760: 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 Shared *pBt,
e770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e780: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
e790: 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e le */. Pgno pgn
e7a0: 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o,
e7b0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
e7c0: 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f r of the page to
e7d0: 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 get */. MemPag
e7e0: 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 e **ppPage,
e7f0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
e800: 74 65 20 74 68 65 20 70 61 67 65 20 70 6f 69 6e te the page poin
e810: 74 65 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ter here */. in
e820: 74 20 62 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 t bReadonly
e830: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e840: 20 50 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f PAGER_GET_READO
e850: 4e 4c 59 20 6f 72 20 30 20 2a 2f 0a 29 7b 0a 20 NLY or 0 */.){.
e860: 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
e870: 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
e880: 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
e890: 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 ) );. assert( b
e8a0: 52 65 61 64 6f 6e 6c 79 3d 3d 50 41 47 45 52 5f Readonly==PAGER_
e8b0: 47 45 54 5f 52 45 41 44 4f 4e 4c 59 20 7c 7c 20 GET_READONLY ||
e8c0: 62 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 29 3b 0a bReadonly==0 );.
e8d0: 0a 20 20 69 66 28 20 70 67 6e 6f 3e 62 74 72 65 . if( pgno>btre
e8e0: 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 ePagecount(pBt)
e8f0: 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
e900: 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
e910: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
e920: 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
e930: 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 pBt, pgno, ppPag
e940: 65 2c 20 62 52 65 61 64 6f 6e 6c 79 29 3b 0a 20 e, bReadonly);.
e950: 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
e960: 45 5f 4f 4b 20 26 26 20 28 2a 70 70 50 61 67 65 E_OK && (*ppPage
e970: 29 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 29 7b 0a )->isInit==0 ){.
e980: 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
e990: 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67 65 InitPage(*ppPage
e9a0: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
e9b0: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
e9c0: 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
e9d0: 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 e(*ppPage);.
e9e0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
e9f0: 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f 3d testcase( pgno=
ea00: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
ea10: 70 67 6e 6f 21 3d 30 20 7c 7c 20 72 63 3d 3d 53 pgno!=0 || rc==S
ea20: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b QLITE_CORRUPT );
ea30: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
ea40: 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
ea50: 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 MemPage. This
ea60: 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 should be called
ea70: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 once for each p
ea80: 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 rior.** call to
ea90: 62 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f btreeGetPage..*/
eaa0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
eab0: 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 easePage(MemPage
eac0: 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 *pPage){. if(
ead0: 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 pPage ){. ass
eae0: 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 ert( pPage->aDat
eaf0: 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
eb00: 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 pPage->pBt );.
eb10: 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
eb20: 2d 3e 70 44 62 50 61 67 65 21 3d 30 20 29 3b 0a ->pDbPage!=0 );.
eb30: 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
eb40: 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
eb50: 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
eb60: 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 == (void*)pPage
eb70: 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
eb80: 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
eb90: 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ata(pPage->pDbPa
eba0: 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 ge)==pPage->aDat
ebb0: 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
ebc0: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
ebd0: 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
ebe0: 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 mutex) );. sq
ebf0: 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 4e lite3PagerUnrefN
ec00: 6f 74 4e 75 6c 6c 28 70 50 61 67 65 2d 3e 70 44 otNull(pPage->pD
ec10: 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bPage);. }.}../
ec20: 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f *.** During a ro
ec30: 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 llback, when the
ec40: 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69 pager reloads i
ec50: 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
ec60: 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 the cache.** so
ec70: 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69 that the cache i
ec80: 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 s restored to it
ec90: 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 s original state
eca0: 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
ecb0: 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 .** the transact
ecc0: 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61 ion, for each pa
ecd0: 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73 ge restored this
ece0: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
ecf0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
ed00: 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 outine needs to
ed10: 72 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20 reset the extra
ed20: 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20 data section at
ed30: 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
ed40: 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20 * page to agree
ed50: 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65 with the restore
ed60: 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 d data..*/.stati
ed70: 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69 c void pageReini
ed80: 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 29 t(DbPage *pData)
ed90: 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
eda0: 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d ge;. pPage = (M
edb0: 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 emPage *)sqlite3
edc0: 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 PagerGetExtra(pD
edd0: 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ata);. assert(
ede0: 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
edf0: 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e Refcount(pData)>
ee00: 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 0 );. if( pPage
ee10: 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 ->isInit ){.
ee20: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
ee30: 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
ee40: 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
ee50: 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
ee60: 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 it = 0;. if(
ee70: 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
ee80: 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e Refcount(pData)>
ee90: 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 1 ){. /* pP
eea0: 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 age might not be
eeb0: 20 61 20 62 74 72 65 65 20 70 61 67 65 3b 20 20 a btree page;
eec0: 69 74 20 6d 69 67 68 74 20 62 65 20 61 6e 20 6f it might be an o
eed0: 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 verflow page.
eee0: 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 ** or ptrmap
eef0: 70 61 67 65 20 6f 72 20 61 20 66 72 65 65 20 70 page or a free p
ef00: 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 age. In those c
ef10: 61 73 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 ases, the follow
ef20: 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c ing. ** cal
ef30: 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 74 50 61 l to btreeInitPa
ef40: 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 ge() will likely
ef50: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
ef60: 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a ORRUPT.. **
ef70: 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 But no harm is
ef80: 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 41 done by this. A
ef90: 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 69 6d nd it is very im
efa0: 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 20 portant that.
efb0: 20 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 ** btreeInitP
efc0: 61 67 65 28 29 20 62 65 20 63 61 6c 6c 65 64 20 age() be called
efd0: 6f 6e 20 65 76 65 72 79 20 62 74 72 65 65 20 70 on every btree p
efe0: 61 67 65 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 age so we make.
eff0: 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c ** the call
f000: 20 66 6f 72 20 65 76 65 72 79 20 70 61 67 65 20 for every page
f010: 74 68 61 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f that comes in fo
f020: 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f r re-initing. */
f030: 0a 20 20 20 20 20 20 62 74 72 65 65 49 6e 69 74 . btreeInit
f040: 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
f050: 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
f060: 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 Invoke the busy
f070: 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 handler for a bt
f080: 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ree..*/.static i
f090: 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 nt btreeInvokeBu
f0a0: 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a syHandler(void *
f0b0: 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 pArg){. BtShare
f0c0: 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 d *pBt = (BtShar
f0d0: 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 ed*)pArg;. asse
f0e0: 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 rt( pBt->db );.
f0f0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
f100: 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
f110: 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
f120: 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
f130: 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
f140: 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 (&pBt->db->busyH
f150: 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a andler);.}../*.*
f160: 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 * Open a databas
f170: 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a e file..** .** z
f180: 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 Filename is the
f190: 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
f1a0: 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a base file. If z
f1b0: 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c Filename is NULL
f1c0: 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 65 70 68 65 .** then an ephe
f1d0: 6d 65 72 61 6c 20 64 61 74 61 62 61 73 65 20 69 meral database i
f1e0: 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 s created. The
f1f0: 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 62 61 ephemeral databa
f200: 73 65 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 65 se might.** be e
f210: 78 63 6c 75 73 69 76 65 6c 79 20 69 6e 20 6d 65 xclusively in me
f220: 6d 6f 72 79 2c 20 6f 72 20 69 74 20 6d 69 67 68 mory, or it migh
f230: 74 20 75 73 65 20 61 20 64 69 73 6b 2d 62 61 73 t use a disk-bas
f240: 65 64 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e ed memory cache.
f250: 0a 2a 2a 20 45 69 74 68 65 72 20 77 61 79 2c 20 .** Either way,
f260: 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 64 61 the ephemeral da
f270: 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 61 tabase will be a
f280: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c utomatically del
f290: 65 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20 73 71 eted .** when sq
f2a0: 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 lite3BtreeClose(
f2b0: 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a ) is called..**.
f2c0: 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 ** If zFilename
f2d0: 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 is ":memory:" th
f2e0: 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 en an in-memory
f2f0: 64 61 74 61 62 61 73 65 20 69 73 20 63 72 65 61 database is crea
f300: 74 65 64 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 ted.** that is a
f310: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 73 utomatically des
f320: 74 72 6f 79 65 64 20 77 68 65 6e 20 69 74 20 69 troyed when it i
f330: 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 s closed..**.**
f340: 54 68 65 20 22 66 6c 61 67 73 22 20 70 61 72 61 The "flags" para
f350: 6d 65 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 meter is a bitma
f360: 73 6b 20 74 68 61 74 20 6d 69 67 68 74 20 63 6f sk that might co
f370: 6e 74 61 69 6e 20 62 69 74 73 20 6c 69 6b 65 0a ntain bits like.
f380: 2a 2a 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f ** BTREE_OMIT_JO
f390: 55 52 4e 41 4c 20 61 6e 64 2f 6f 72 20 42 54 52 URNAL and/or BTR
f3a0: 45 45 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a EE_MEMORY..**.**
f3b0: 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
f3c0: 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e is already open
f3d0: 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 ed in the same d
f3e0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
f3f0: 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 on.** and we are
f400: 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 in shared cache
f410: 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 mode, then the
f420: 6f 70 65 6e 20 77 69 6c 6c 20 66 61 69 6c 20 77 open will fail w
f430: 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 ith an.** SQLITE
f440: 5f 43 4f 4e 53 54 52 41 49 4e 54 20 65 72 72 6f _CONSTRAINT erro
f450: 72 2e 20 20 57 65 20 63 61 6e 6e 6f 74 20 61 6c r. We cannot al
f460: 6c 6f 77 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 low two or more
f470: 42 74 53 68 61 72 65 64 0a 2a 2a 20 6f 62 6a 65 BtShared.** obje
f480: 63 74 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 cts in the same
f490: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
f4a0: 69 6f 6e 20 73 69 6e 63 65 20 64 6f 69 6e 67 20 ion since doing
f4b0: 73 6f 20 77 69 6c 6c 20 6c 65 61 64 0a 2a 2a 20 so will lead.**
f4c0: 74 6f 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 to problems with
f4d0: 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 69 6e 74 locking..*/.int
f4e0: 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 sqlite3BtreeOpe
f4f0: 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
f500: 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 *pVfs, /*
f510: 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 VFS to use for t
f520: 68 69 73 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 his b-tree */.
f530: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f540: 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 ename, /* Name
f550: 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 of the file cont
f560: 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 aining the BTree
f570: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 database */. s
f580: 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
f590: 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 /* Associ
f5a0: 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61 ated database ha
f5b0: 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ndle */. Btree
f5c0: 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 **ppBtree,
f5d0: 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
f5e0: 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 new Btree object
f5f0: 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
f600: 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
f610: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
f620: 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 tions */. int v
f630: 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
f640: 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 /* Flags pass
f650: 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 ed through to sq
f660: 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
f670: 29 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 ) */.){. BtShar
f680: 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 ed *pBt = 0;
f690: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72 /* Shar
f6a0: 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 65 ed part of btree
f6b0: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
f6c0: 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20 Btree *p;
f6d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f6e0: 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 * Handle to retu
f6f0: 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f rn */. sqlite3_
f700: 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e mutex *mutexOpen
f710: 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e = 0; /* Preven
f720: 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 ts a race condit
f730: 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 33 ion. Ticket #353
f740: 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 7 */. int rc =
f750: 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
f760: 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
f770: 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 66 code from this f
f780: 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 unction */. u8
f790: 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 20 nReserve;
f7a0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
f7b0: 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 70 yte of unused sp
f7c0: 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 ace on each page
f7d0: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
f7e0: 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 30 har zDbHeader[10
f7f0: 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 0]; /* Database
f800: 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 header content
f810: 2a 2f 0a 0a 20 20 2f 2a 20 54 72 75 65 20 69 66 */.. /* True if
f820: 20 6f 70 65 6e 69 6e 67 20 61 6e 20 65 70 68 65 opening an ephe
f830: 6d 65 72 61 6c 2c 20 74 65 6d 70 6f 72 61 72 79 meral, temporary
f840: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 database */. c
f850: 6f 6e 73 74 20 69 6e 74 20 69 73 54 65 6d 70 44 onst int isTempD
f860: 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 b = zFilename==0
f870: 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d || zFilename[0]
f880: 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 ==0;.. /* Set t
f890: 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 he variable isMe
f8a0: 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 mdb to true for
f8b0: 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
f8c0: 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 abase, or . **
f8d0: 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 false for a file
f8e0: 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e -based database.
f8f0: 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
f900: 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 ITE_OMIT_MEMORYD
f910: 42 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 B. const int is
f920: 4d 65 6d 64 62 20 3d 20 30 3b 0a 23 65 6c 73 65 Memdb = 0;.#else
f930: 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d . const int isM
f940: 65 6d 64 62 20 3d 20 28 7a 46 69 6c 65 6e 61 6d emdb = (zFilenam
f950: 65 20 26 26 20 73 74 72 63 6d 70 28 7a 46 69 6c e && strcmp(zFil
f960: 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a ename, ":memory:
f970: 22 29 3d 3d 30 29 0a 20 20 20 20 20 20 20 20 20 ")==0).
f980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
f990: 20 28 69 73 54 65 6d 70 44 62 20 26 26 20 73 71 (isTempDb && sq
f9a0: 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 lite3TempInMemor
f9b0: 79 28 64 62 29 29 0a 20 20 20 20 20 20 20 20 20 y(db)).
f9c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
f9d0: 20 28 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c (vfsFlags & SQL
f9e0: 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d 4f 52 59 29 ITE_OPEN_MEMORY)
f9f0: 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 !=0;.#endif.. a
fa00: 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
fa10: 20 20 61 73 73 65 72 74 28 20 70 56 66 73 21 3d assert( pVfs!=
fa20: 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
fa30: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
fa40: 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
fa50: 20 20 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 assert( (flags
fa60: 26 30 78 66 66 29 3d 3d 66 6c 61 67 73 20 29 3b &0xff)==flags );
fa70: 20 20 20 2f 2a 20 66 6c 61 67 73 20 66 69 74 20 /* flags fit
fa80: 69 6e 20 38 20 62 69 74 73 20 2a 2f 0a 0a 20 20 in 8 bits */..
fa90: 2f 2a 20 4f 6e 6c 79 20 61 20 42 54 52 45 45 5f /* Only a BTREE_
faa0: 53 49 4e 47 4c 45 20 64 61 74 61 62 61 73 65 20 SINGLE database
fab0: 63 61 6e 20 62 65 20 42 54 52 45 45 5f 55 4e 4f can be BTREE_UNO
fac0: 52 44 45 52 45 44 20 2a 2f 0a 20 20 61 73 73 65 RDERED */. asse
fad0: 72 74 28 20 28 66 6c 61 67 73 20 26 20 42 54 52 rt( (flags & BTR
fae0: 45 45 5f 55 4e 4f 52 44 45 52 45 44 29 3d 3d 30 EE_UNORDERED)==0
faf0: 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 42 54 52 || (flags & BTR
fb00: 45 45 5f 53 49 4e 47 4c 45 29 21 3d 30 20 29 3b EE_SINGLE)!=0 );
fb10: 0a 0a 20 20 2f 2a 20 41 20 42 54 52 45 45 5f 53 .. /* A BTREE_S
fb20: 49 4e 47 4c 45 20 64 61 74 61 62 61 73 65 20 69 INGLE database i
fb30: 73 20 61 6c 77 61 79 73 20 61 20 74 65 6d 70 6f s always a tempo
fb40: 72 61 72 79 20 61 6e 64 2f 6f 72 20 65 70 68 65 rary and/or ephe
fb50: 6d 65 72 61 6c 20 2a 2f 0a 20 20 61 73 73 65 72 meral */. asser
fb60: 74 28 20 28 66 6c 61 67 73 20 26 20 42 54 52 45 t( (flags & BTRE
fb70: 45 5f 53 49 4e 47 4c 45 29 3d 3d 30 20 7c 7c 20 E_SINGLE)==0 ||
fb80: 69 73 54 65 6d 70 44 62 20 29 3b 0a 0a 20 20 69 isTempDb );.. i
fb90: 66 28 20 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 f( isMemdb ){.
fba0: 20 20 66 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 flags |= BTREE
fbb0: 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 69 _MEMORY;. }. i
fbc0: 66 28 20 28 76 66 73 46 6c 61 67 73 20 26 20 53 f( (vfsFlags & S
fbd0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
fbe0: 44 42 29 21 3d 30 20 26 26 20 28 69 73 4d 65 6d DB)!=0 && (isMem
fbf0: 64 62 20 7c 7c 20 69 73 54 65 6d 70 44 62 29 20 db || isTempDb)
fc00: 29 7b 0a 20 20 20 20 76 66 73 46 6c 61 67 73 20 ){. vfsFlags
fc10: 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e 53 = (vfsFlags & ~S
fc20: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
fc30: 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 DB) | SQLITE_OPE
fc40: 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a 20 N_TEMP_DB;. }.
fc50: 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
fc60: 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 ocZero(sizeof(Bt
fc70: 72 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70 20 ree));. if( !p
fc80: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
fc90: 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
fca0: 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 p->inTrans = T
fcb0: 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e RANS_NONE;. p->
fcc0: 64 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65 66 db = db;.#ifndef
fcd0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
fce0: 52 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e 6c RED_CACHE. p->l
fcf0: 6f 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b 0a ock.pBtree = p;.
fd00: 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 p->lock.iTable
fd10: 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 = 1;.#endif..#i
fd20: 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
fd30: 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
fd40: 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 CHE) && !defined
fd50: 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 (SQLITE_OMIT_DIS
fd60: 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 KIO). /*. ** I
fd70: 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73 20 f this Btree is
fd80: 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 a candidate for
fd90: 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 72 shared cache, tr
fda0: 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20 2a y to find an. *
fdb0: 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68 61 * existing BtSha
fdc0: 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74 20 red object that
fdd0: 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69 74 we can share wit
fde0: 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 54 h. */. if( isT
fdf0: 65 6d 70 44 62 3d 3d 30 20 26 26 20 28 69 73 4d empDb==0 && (isM
fe00: 65 6d 64 62 3d 3d 30 20 7c 7c 20 28 76 66 73 46 emdb==0 || (vfsF
fe10: 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e lags&SQLITE_OPEN
fe20: 5f 55 52 49 29 21 3d 30 29 20 29 7b 0a 20 20 20 _URI)!=0) ){.
fe30: 20 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20 if( vfsFlags &
fe40: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
fe50: 45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20 EDCACHE ){.
fe60: 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 int nFullPathna
fe70: 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 me = pVfs->mxPat
fe80: 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 hname+1;. c
fe90: 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 har *zFullPathna
fea0: 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c me = sqlite3Mall
feb0: 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 oc(nFullPathname
fec0: 29 3b 0a 20 20 20 20 20 20 4d 55 54 45 58 5f 4c );. MUTEX_L
fed0: 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f 6d 75 OGIC( sqlite3_mu
fee0: 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 tex *mutexShared
fef0: 3b 20 29 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 ; ). p->sha
ff00: 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 rable = 1;.
ff10: 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e if( !zFullPathn
ff20: 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ame ){. s
ff30: 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
ff40: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
ff50: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
ff60: 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 }. if( i
ff70: 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 sMemdb ){.
ff80: 20 20 6d 65 6d 63 70 79 28 7a 46 75 6c 6c 50 61 memcpy(zFullPa
ff90: 74 68 6e 61 6d 65 2c 20 7a 46 69 6c 65 6e 61 6d thname, zFilenam
ffa0: 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e e, sqlite3Strlen
ffb0: 33 30 28 7a 46 69 6c 65 6e 61 6d 65 29 2b 31 29 30(zFilename)+1)
ffc0: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
ffd0: 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
ffe0: 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
fff0: 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d e(pVfs, zFilenam
10000 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
10010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10020 20 20 20 20 20 20 6e 46 75 6c 6c 50 61 74 68 6e nFullPathn
10030 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 ame, zFullPathna
10040 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 me);. if(
10050 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
10060 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 sqlite3_free(zF
10070 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 ullPathname);.
10080 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
10090 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 free(p);.
100a0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
100b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
100c0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
100d0 44 53 41 46 45 0a 20 20 20 20 20 20 6d 75 74 65 DSAFE. mute
100e0 78 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d xOpen = sqlite3M
100f0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
10100 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 _MUTEX_STATIC_OP
10110 45 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 EN);. sqlit
10120 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
10130 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 utexOpen);.
10140 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 mutexShared = s
10150 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
10160 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
10170 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 ATIC_MASTER);.
10180 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
10190 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 x_enter(mutexSha
101a0 72 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 red);.#endif.
101b0 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 for(pBt=GLOBA
101c0 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
101d0 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
101e0 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 st); pBt; pBt=pB
101f0 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 t->pNext){.
10200 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
10210 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 nRef>0 );.
10220 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 if( 0==strcmp(
10230 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 zFullPathname, s
10240 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e qlite3PagerFilen
10250 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ame(pBt->pPager,
10260 20 30 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 0)).
10270 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
10280 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 PagerVfs(pBt->pP
10290 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 ager)==pVfs ){.
102a0 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 int iDb
102b0 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 ;. for(
102c0 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 iDb=db->nDb-1; i
102d0 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 Db>=0; iDb--){.
102e0 20 20 20 20 20 20 20 20 20 20 20 42 74 72 65 65 Btree
102f0 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20 64 62 *pExisting = db
10300 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a ->aDb[iDb].pBt;.
10310 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
10320 70 45 78 69 73 74 69 6e 67 20 26 26 20 70 45 78 pExisting && pEx
10330 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 isting->pBt==pBt
10340 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
10350 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
10360 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 leave(mutexShare
10370 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 d);.
10380 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
10390 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 leave(mutexOpen)
103a0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
103b0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 sqlite3_free(zFu
103c0 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
103d0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
103e0 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 e3_free(p);.
103f0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
10400 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
10410 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 NT;.
10420 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
10430 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d p->pBt =
10440 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 pBt;.
10450 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 pBt->nRef++;.
10460 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
10470 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
10480 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
10490 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 tex_leave(mutexS
104a0 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 hared);. sq
104b0 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c lite3_free(zFull
104c0 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d Pathname);. }
104d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
104e0 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 EBUG. else{.
104f0 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 /* In debug
10500 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 mode, we mark a
10510 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 ll persistent da
10520 74 61 62 61 73 65 73 20 61 73 20 73 68 61 72 61 tabases as shara
10530 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 ble. ** eve
10540 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 n when they are
10550 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 not. This exerc
10560 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ises the locking
10570 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 code and.
10580 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 ** gives more op
10590 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 portunity for as
105a0 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 serts(sqlite3_mu
105b0 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 tex_held()).
105c0 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 ** statements
105d0 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 to find locking
105e0 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 problems..
105f0 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 */. p->shar
10600 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a able = 1;. }.
10610 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 #endif. }.#endi
10620 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 f. if( pBt==0 )
10630 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 {. /*. **
10640 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 The following as
10650 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 serts make sure
10660 74 68 61 74 20 73 74 72 75 63 74 75 72 65 73 20 that structures
10670 75 73 65 64 20 62 79 20 74 68 65 20 62 74 72 65 used by the btre
10680 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 e are. ** the
10690 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 right size. Th
106a0 69 73 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 is is to guard a
106b0 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e gainst size chan
106c0 67 65 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a ges that result.
106d0 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 ** when comp
106e0 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 iling on a diffe
106f0 72 65 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 rent architectur
10700 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 e.. */. as
10710 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 sert( sizeof(i64
10720 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 )==8 || sizeof(i
10730 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 64)==4 );. as
10740 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 sert( sizeof(u64
10750 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 )==8 || sizeof(u
10760 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 64)==4 );. as
10770 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 sert( sizeof(u32
10780 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
10790 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d rt( sizeof(u16)=
107a0 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =2 );. assert
107b0 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d ( sizeof(Pgno)==
107c0 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 4 );. . pBt
107d0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
107e0 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 ero( sizeof(*pBt
107f0 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 ) );. if( pBt
10800 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ==0 ){. rc
10810 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
10820 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 goto btree
10830 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d _open_out;. }
10840 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
10850 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 3PagerOpen(pVfs,
10860 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a &pBt->pPager, z
10870 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 Filename,.
10880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10890 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 EXTRA_SIZE,
108a0 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 2c flags, vfsFlags,
108b0 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 pageReinit);.
108c0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
108d0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
108e0 69 74 65 33 50 61 67 65 72 53 65 74 4d 6d 61 70 ite3PagerSetMmap
108f0 4c 69 6d 69 74 28 70 42 74 2d 3e 70 50 61 67 65 Limit(pBt->pPage
10900 72 2c 20 64 62 2d 3e 73 7a 4d 6d 61 70 29 3b 0a r, db->szMmap);.
10910 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
10920 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 e3PagerReadFileh
10930 65 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 eader(pBt->pPage
10940 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 r,sizeof(zDbHead
10950 65 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a er),zDbHeader);.
10960 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
10970 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
10980 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f goto btree_
10990 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
109a0 20 20 20 20 70 42 74 2d 3e 6f 70 65 6e 46 6c 61 pBt->openFla
109b0 67 73 20 3d 20 28 75 38 29 66 6c 61 67 73 3b 0a gs = (u8)flags;.
109c0 20 20 20 20 70 42 74 2d 3e 64 62 20 3d 20 64 62 pBt->db = db
109d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
109e0 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 erSetBusyhandler
109f0 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 (pBt->pPager, bt
10a00 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e reeInvokeBusyHan
10a10 64 6c 65 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 dler, pBt);.
10a20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 p->pBt = pBt;.
10a30 0a 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f . pBt->pCurso
10a40 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e r = 0;. pBt->
10a50 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 pPage1 = 0;.
10a60 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 if( sqlite3Pager
10a70 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d 3e Isreadonly(pBt->
10a80 70 50 61 67 65 72 29 20 29 20 70 42 74 2d 3e 62 pPager) ) pBt->b
10a90 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 tsFlags |= BTS_R
10aa0 45 41 44 5f 4f 4e 4c 59 3b 0a 23 69 66 64 65 66 EAD_ONLY;.#ifdef
10ab0 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
10ac0 45 4c 45 54 45 0a 20 20 20 20 70 42 74 2d 3e 62 ELETE. pBt->b
10ad0 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53 tsFlags |= BTS_S
10ae0 45 43 55 52 45 5f 44 45 4c 45 54 45 3b 0a 23 65 ECURE_DELETE;.#e
10af0 6e 64 69 66 0a 20 20 20 20 70 42 74 2d 3e 70 61 ndif. pBt->pa
10b00 67 65 53 69 7a 65 20 3d 20 28 7a 44 62 48 65 61 geSize = (zDbHea
10b10 64 65 72 5b 31 36 5d 3c 3c 38 29 20 7c 20 28 7a der[16]<<8) | (z
10b20 44 62 48 65 61 64 65 72 5b 31 37 5d 3c 3c 31 36 DbHeader[17]<<16
10b30 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e );. if( pBt->
10b40 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 20 pageSize<512 ||
10b50 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 53 51 pBt->pageSize>SQ
10b60 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
10b70 5a 45 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 ZE. || (
10b80 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 31 (pBt->pageSize-1
10b90 29 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 )&pBt->pageSize)
10ba0 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 42 74 !=0 ){. pBt
10bb0 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a ->pageSize = 0;.
10bc0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
10bd0 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
10be0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6d /* If the m
10bf0 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f agic name ":memo
10c00 72 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 65 ry:" will create
10c10 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
10c20 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 20 tabase, then.
10c30 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 ** leave the
10c40 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 20 autoVacuum mode
10c50 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 74 at 0 (do not aut
10c60 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e 20 o-vacuum), even
10c70 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 if. ** SQLI
10c80 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 TE_DEFAULT_AUTOV
10c90 41 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20 4f ACUUM is true. O
10ca0 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 n the other hand
10cb0 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 , if. ** SQ
10cc0 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
10cd0 44 42 20 68 61 73 20 62 65 65 6e 20 64 65 66 69 DB has been defi
10ce0 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f ned, then ":memo
10cf0 72 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a 20 ry:" is just a.
10d00 20 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 20 ** regular
10d10 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 file-name. In th
10d20 69 73 20 63 61 73 65 20 74 68 65 20 61 75 74 6f is case the auto
10d30 2d 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73 20 -vacuum applies
10d40 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 as per normal..
10d50 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
10d60 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 ( zFilename && !
10d70 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 isMemdb ){.
10d80 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 pBt->autoVacu
10d90 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 um = (SQLITE_DEF
10da0 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 AULT_AUTOVACUUM
10db0 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 ? 1 : 0);.
10dc0 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 pBt->incrVacuu
10dd0 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 m = (SQLITE_DEFA
10de0 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d ULT_AUTOVACUUM==
10df0 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 2 ? 1 : 0);.
10e00 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
10e10 20 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 nReserve = 0;.
10e20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
10e30 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48 65 nReserve = zDbHe
10e40 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 20 ader[20];.
10e50 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d pBt->btsFlags |=
10e60 20 42 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 BTS_PAGESIZE_FI
10e70 58 45 44 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c XED;.#ifndef SQL
10e80 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
10e90 55 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 UUM. pBt->a
10ea0 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 utoVacuum = (get
10eb0 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 4byte(&zDbHeader
10ec0 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 [36 + 4*4])?1:0)
10ed0 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 ;. pBt->inc
10ee0 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 rVacuum = (get4b
10ef0 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 yte(&zDbHeader[3
10f00 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 6 + 7*4])?1:0);.
10f10 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 #endif. }.
10f20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
10f30 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 erSetPagesize(pB
10f40 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d t->pPager, &pBt-
10f50 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 65 >pageSize, nRese
10f60 72 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 rve);. if( rc
10f70 20 29 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 ) goto btree_op
10f80 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 70 42 74 2d en_out;. pBt-
10f90 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 >usableSize = pB
10fa0 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e 52 t->pageSize - nR
10fb0 65 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 eserve;. asse
10fc0 72 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53 69 rt( (pBt->pageSi
10fd0 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20 2f ze & 7)==0 ); /
10fe0 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 * 8-byte alignme
10ff0 6e 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20 2a nt of pageSize *
11000 2f 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69 6e /. .#if !defin
11010 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
11020 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
11030 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
11040 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 20 OMIT_DISKIO).
11050 20 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 /* Add the new
11060 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
11070 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 to the linked li
11080 73 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 st sharable BtSh
11090 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 areds.. */.
110a0 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c if( p->sharabl
110b0 65 20 29 7b 0a 20 20 20 20 20 20 4d 55 54 45 58 e ){. MUTEX
110c0 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f _LOGIC( sqlite3_
110d0 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 mutex *mutexShar
110e0 65 64 3b 20 29 0a 20 20 20 20 20 20 70 42 74 2d ed; ). pBt-
110f0 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 >nRef = 1;.
11100 20 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 6d 75 MUTEX_LOGIC( mu
11110 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 texShared = sqli
11120 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
11130 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
11140 43 5f 4d 41 53 54 45 52 29 3b 29 0a 20 20 20 20 C_MASTER);).
11150 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 if( SQLITE_THR
11160 45 41 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 EADSAFE && sqlit
11170 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
11180 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 CoreMutex ){.
11190 20 20 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 pBt->mutex
111a0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
111b0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
111c0 5f 46 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 _FAST);.
111d0 69 66 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d if( pBt->mutex==
111e0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
111f0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
11200 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e ;. db->
11210 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 mallocFailed = 0
11220 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
11230 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b btree_open_out;
11240 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
11250 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
11260 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
11270 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 exShared);.
11280 20 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c pBt->pNext = GL
11290 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 OBAL(BtShared*,s
112a0 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
112b0 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c eList);. GL
112c0 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 OBAL(BtShared*,s
112d0 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
112e0 65 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 eList) = pBt;.
112f0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
11300 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 x_leave(mutexSha
11310 72 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 red);. }.#end
11320 69 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 if. }..#if !def
11330 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
11340 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 _SHARED_CACHE) &
11350 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
11360 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 E_OMIT_DISKIO).
11370 20 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 /* If the new B
11380 74 72 65 65 20 75 73 65 73 20 61 20 73 68 61 72 tree uses a shar
11390 61 62 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 able pBtShared,
113a0 74 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 then link the ne
113b0 77 0a 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 w. ** Btree int
113c0 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c o the list of al
113d0 6c 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 l sharable Btree
113e0 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 s for the same c
113f0 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 onnection.. **
11400 54 68 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 The list is kept
11410 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 in ascending or
11420 64 65 72 20 62 79 20 70 42 74 20 61 64 64 72 65 der by pBt addre
11430 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ss.. */. if( p
11440 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
11450 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 int i;. Btr
11460 65 65 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f ee *pSib;. fo
11470 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 r(i=0; i<db->nDb
11480 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
11490 28 20 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 ( (pSib = db->aD
114a0 62 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 b[i].pBt)!=0 &&
114b0 70 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 pSib->sharable )
114c0 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 {. while(
114d0 20 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 pSib->pPrev ){
114e0 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 pSib = pSib->pPr
114f0 65 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 ev; }. if
11500 28 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 ( p->pBt<pSib->p
11510 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Bt ){.
11520 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b p->pNext = pSib;
11530 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 . p->pP
11540 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 rev = 0;.
11550 20 20 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d pSib->pPrev =
11560 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 p;. }els
11570 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 e{. whi
11580 6c 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 le( pSib->pNext
11590 26 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e && pSib->pNext->
115a0 70 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 pBt<p->pBt ){.
115b0 20 20 20 20 20 20 20 20 20 20 70 53 69 62 20 3d pSib =
115c0 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 pSib->pNext;.
115d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
115e0 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
115f0 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Sib->pNext;.
11600 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d p->pPrev =
11610 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 pSib;.
11620 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b if( p->pNext ){
11630 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e . p->
11640 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 pNext->pPrev = p
11650 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
11660 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e pSib->pN
11670 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 ext = p;.
11680 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
11690 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
116a0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 }.#endif. *pp
116b0 42 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 Btree = p;..btre
116c0 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 e_open_out:. if
116d0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
116e0 29 7b 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 ){. if( pBt &
116f0 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b & pBt->pPager ){
11700 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 . sqlite3Pa
11710 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 gerClose(pBt->pP
11720 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ager);. }.
11730 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 sqlite3_free(pB
11740 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f t);. sqlite3_
11750 66 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 free(p);. *pp
11760 42 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 65 6c Btree = 0;. }el
11770 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 se{. /* If th
11780 65 20 42 2d 54 72 65 65 20 77 61 73 20 73 75 63 e B-Tree was suc
11790 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 cessfully opened
117a0 2c 20 73 65 74 20 74 68 65 20 70 61 67 65 72 2d , set the pager-
117b0 63 61 63 68 65 20 73 69 7a 65 20 74 6f 20 74 68 cache size to th
117c0 65 0a 20 20 20 20 2a 2a 20 64 65 66 61 75 6c 74 e. ** default
117d0 20 76 61 6c 75 65 2e 20 45 78 63 65 70 74 2c 20 value. Except,
117e0 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 6f 6e 20 when opening on
117f0 61 6e 20 65 78 69 73 74 69 6e 67 20 73 68 61 72 an existing shar
11800 65 64 20 70 61 67 65 72 2d 63 61 63 68 65 2c 0a ed pager-cache,.
11810 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 63 68 ** do not ch
11820 61 6e 67 65 20 74 68 65 20 70 61 67 65 72 2d 63 ange the pager-c
11830 61 63 68 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a ache size.. *
11840 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 /. if( sqlite
11850 33 42 74 72 65 65 53 63 68 65 6d 61 28 70 2c 20 3BtreeSchema(p,
11860 30 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 0, 0)==0 ){.
11870 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
11880 74 43 61 63 68 65 73 69 7a 65 28 70 2d 3e 70 42 tCachesize(p->pB
11890 74 2d 3e 70 50 61 67 65 72 2c 20 53 51 4c 49 54 t->pPager, SQLIT
118a0 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f E_DEFAULT_CACHE_
118b0 53 49 5a 45 29 3b 0a 20 20 20 20 7d 0a 20 20 7d SIZE);. }. }
118c0 0a 20 20 69 66 28 20 6d 75 74 65 78 4f 70 65 6e . if( mutexOpen
118d0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
118e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
118f0 6c 64 28 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b ld(mutexOpen) );
11900 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
11910 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 ex_leave(mutexOp
11920 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 en);. }. retur
11930 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 n rc;.}../*.** D
11940 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42 74 53 ecrement the BtS
11950 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 hared.nRef count
11960 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72 65 61 er. When it rea
11970 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 ches zero,.** re
11980 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 72 65 move the BtShare
11990 64 20 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d d structure from
119a0 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73 the sharing lis
119b0 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 t. Return.** tr
119c0 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72 ue if the BtShar
119d0 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 ed.nRef counter
119e0 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 reaches zero and
119f0 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 return.** false
11a00 20 69 66 20 69 74 20 69 73 20 73 74 69 6c 6c 20 if it is still
11a10 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 positive..*/.sta
11a20 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 tic int removeFr
11a30 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 42 74 omSharingList(Bt
11a40 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 Shared *pBt){.#i
11a50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
11a60 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
11a70 20 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 73 71 MUTEX_LOGIC( sq
11a80 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 lite3_mutex *pMa
11a90 73 74 65 72 3b 20 29 0a 20 20 42 74 53 68 61 72 ster; ). BtShar
11aa0 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 ed *pList;. int
11ab0 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 removed = 0;..
11ac0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
11ad0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 _mutex_notheld(p
11ae0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
11af0 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 70 4d 61 MUTEX_LOGIC( pMa
11b00 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 ster = sqlite3Mu
11b10 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
11b20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
11b30 54 45 52 29 3b 20 29 0a 20 20 73 71 6c 69 74 65 TER); ). sqlite
11b40 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 3_mutex_enter(pM
11b50 61 73 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e aster);. pBt->n
11b60 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 Ref--;. if( pBt
11b70 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 ->nRef<=0 ){.
11b80 20 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 if( GLOBAL(BtSh
11b90 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 ared*,sqlite3Sha
11ba0 72 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 redCacheList)==p
11bb0 42 74 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 Bt ){. GLOB
11bc0 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
11bd0 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
11be0 69 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 ist) = pBt->pNex
11bf0 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
11c00 20 20 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 pList = GLOB
11c10 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
11c20 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
11c30 69 73 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c ist);. whil
11c40 65 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 e( ALWAYS(pList)
11c50 20 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 && pList->pNext
11c60 21 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 !=pBt ){.
11c70 20 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e pList=pList->pN
11c80 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
11c90 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c if( ALWAYS(pL
11ca0 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ist) ){.
11cb0 70 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 pList->pNext = p
11cc0 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 Bt->pNext;.
11cd0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
11ce0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
11cf0 46 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 FE ){. sqli
11d00 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 te3_mutex_free(p
11d10 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 Bt->mutex);.
11d20 7d 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 }. removed =
11d30 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 1;. }. sqlite3
11d40 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 _mutex_leave(pMa
11d50 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 ster);. return
11d60 72 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 removed;.#else.
11d70 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 return 1;.#endi
11d80 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 f.}../*.** Make
11d90 73 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 sure pBt->pTmpSp
11da0 61 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e ace points to an
11db0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a allocation of .
11dc0 2a 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 ** MX_CELL_SIZE(
11dd0 70 42 74 29 20 62 79 74 65 73 20 77 69 74 68 20 pBt) bytes with
11de0 61 20 34 2d 62 79 74 65 20 70 72 65 66 69 78 20 a 4-byte prefix
11df0 66 6f 72 20 61 20 6c 65 66 74 2d 63 68 69 6c 64 for a left-child
11e00 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a .** pointer..*/.
11e10 73 74 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f static void allo
11e20 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 42 74 cateTempSpace(Bt
11e30 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
11e40 69 66 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 if( !pBt->pTmpSp
11e50 61 63 65 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e ace ){. pBt->
11e60 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 pTmpSpace = sqli
11e70 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 te3PageMalloc( p
11e80 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a Bt->pageSize );.
11e90 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 . /* One of t
11ea0 68 65 20 75 73 65 73 20 6f 66 20 70 42 74 2d 3e he uses of pBt->
11eb0 70 54 6d 70 53 70 61 63 65 20 69 73 20 74 6f 20 pTmpSpace is to
11ec0 66 6f 72 6d 61 74 20 63 65 6c 6c 73 20 62 65 66 format cells bef
11ed0 6f 72 65 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 ore. ** inser
11ee0 74 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 20 61 ting them into a
11ef0 20 6c 65 61 66 20 70 61 67 65 20 28 66 75 6e 63 leaf page (func
11f00 74 69 6f 6e 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 tion fillInCell(
11f10 29 29 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 20 )). If. ** a
11f20 63 65 6c 6c 20 69 73 20 6c 65 73 73 20 74 68 61 cell is less tha
11f30 6e 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a n 4 bytes in siz
11f40 65 2c 20 69 74 20 69 73 20 72 6f 75 6e 64 65 64 e, it is rounded
11f50 20 75 70 20 74 6f 20 34 20 62 79 74 65 73 0a 20 up to 4 bytes.
11f60 20 20 20 2a 2a 20 62 79 20 74 68 65 20 76 61 72 ** by the var
11f70 69 6f 75 73 20 72 6f 75 74 69 6e 65 73 20 74 68 ious routines th
11f80 61 74 20 6d 61 6e 69 70 75 6c 61 74 65 20 62 69 at manipulate bi
11f90 6e 61 72 79 20 63 65 6c 6c 73 2e 20 57 68 69 63 nary cells. Whic
11fa0 68 0a 20 20 20 20 2a 2a 20 63 61 6e 20 6d 65 61 h. ** can mea
11fb0 6e 20 74 68 61 74 20 66 69 6c 6c 49 6e 43 65 6c n that fillInCel
11fc0 6c 28 29 20 6f 6e 6c 79 20 69 6e 69 74 69 61 6c l() only initial
11fd0 69 7a 65 73 20 74 68 65 20 66 69 72 73 74 20 32 izes the first 2
11fe0 20 6f 72 20 33 0a 20 20 20 20 2a 2a 20 62 79 74 or 3. ** byt
11ff0 65 73 20 6f 66 20 70 54 6d 70 53 70 61 63 65 2c es of pTmpSpace,
12000 20 62 75 74 20 74 68 61 74 20 74 68 65 20 66 69 but that the fi
12010 72 73 74 20 34 20 62 79 74 65 73 20 61 72 65 20 rst 4 bytes are
12020 63 6f 70 69 65 64 20 66 72 6f 6d 0a 20 20 20 20 copied from.
12030 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 64 61 74 ** it into a dat
12040 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 69 73 abase page. This
12050 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 is not actually
12060 20 61 20 70 72 6f 62 6c 65 6d 2c 20 62 75 74 20 a problem, but
12070 69 74 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 63 it. ** does c
12080 61 75 73 65 20 61 20 76 61 6c 67 72 69 6e 64 20 ause a valgrind
12090 65 72 72 6f 72 20 77 68 65 6e 20 74 68 65 20 31 error when the 1
120a0 20 6f 72 20 32 20 62 79 74 65 73 20 6f 66 20 75 or 2 bytes of u
120b0 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20 20 20 nitialized .
120c0 2a 2a 20 64 61 74 61 20 69 73 20 70 61 73 73 65 ** data is passe
120d0 64 20 74 6f 20 73 79 73 74 65 6d 20 63 61 6c 6c d to system call
120e0 20 77 72 69 74 65 28 29 2e 20 53 6f 20 74 6f 20 write(). So to
120f0 61 76 6f 69 64 20 74 68 69 73 20 65 72 72 6f 72 avoid this error
12100 2c 0a 20 20 20 20 2a 2a 20 7a 65 72 6f 20 74 68 ,. ** zero th
12110 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 e first 4 bytes
12120 6f 66 20 74 65 6d 70 20 73 70 61 63 65 20 68 65 of temp space he
12130 72 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a re.. **. *
12140 2a 20 41 6c 73 6f 3a 20 20 50 72 6f 76 69 64 65 * Also: Provide
12150 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 69 four bytes of i
12160 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 nitialized space
12170 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20 20 before the.
12180 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 ** beginning of
12190 70 54 6d 70 53 70 61 63 65 20 61 73 20 61 6e 20 pTmpSpace as an
121a0 61 72 65 61 20 61 76 61 69 6c 61 62 6c 65 20 74 area available t
121b0 6f 20 70 72 65 70 65 6e 64 20 74 68 65 0a 20 20 o prepend the.
121c0 20 20 2a 2a 20 6c 65 66 74 2d 63 68 69 6c 64 20 ** left-child
121d0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 pointer to the b
121e0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 63 65 eginning of a ce
121f0 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ll.. */. i
12200 66 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 f( pBt->pTmpSpac
12210 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 e ){. memse
12220 74 28 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 t(pBt->pTmpSpace
12230 2c 20 30 2c 20 38 29 3b 0a 20 20 20 20 20 20 70 , 0, 8);. p
12240 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 2b 3d Bt->pTmpSpace +=
12250 20 34 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 4;. }. }.}.
12260 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
12270 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 pBt->pTmpSpace a
12280 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
12290 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d tic void freeTem
122a0 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 pSpace(BtShared
122b0 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 70 42 74 *pBt){. if( pBt
122c0 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 ->pTmpSpace ){.
122d0 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 pBt->pTmpSpac
122e0 65 20 2d 3d 20 34 3b 0a 20 20 20 20 73 71 6c 69 e -= 4;. sqli
122f0 74 65 33 50 61 67 65 46 72 65 65 28 70 42 74 2d te3PageFree(pBt-
12300 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 >pTmpSpace);.
12310 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
12320 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 0;. }.}../*.*
12330 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e 20 * Close an open
12340 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 6e 76 database and inv
12350 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 75 72 73 alidate all curs
12360 6f 72 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ors..*/.int sqli
12370 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 te3BtreeClose(Bt
12380 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
12390 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
123a0 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 t;. BtCursor *p
123b0 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 Cur;.. /* Close
123c0 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 all cursors ope
123d0 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 61 6e ned via this han
123e0 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 dle. */. asser
123f0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
12400 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
12410 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
12420 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
12430 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 pCur = pBt->pCu
12440 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 rsor;. while( p
12450 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72 Cur ){. BtCur
12460 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 sor *pTmp = pCur
12470 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70 43 75 ;. pCur = pCu
12480 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 r->pNext;. if
12490 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d ( pTmp->pBtree==
124a0 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
124b0 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
124c0 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a or(pTmp);. }.
124d0 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 }.. /* Rollba
124e0 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20 74 72 ck any active tr
124f0 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 ansaction and fr
12500 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74 ee the handle st
12510 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 ructure.. ** Th
12520 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
12530 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 3BtreeRollback()
12540 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 drops any table
12550 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 -locks held by.
12560 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e ** this handle.
12570 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 . */. sqlite3B
12580 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2c 20 treeRollback(p,
12590 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 SQLITE_OK);. sq
125a0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
125b0 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 p);.. /* If the
125c0 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 re are still oth
125d0 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 er outstanding r
125e0 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
125f0 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 shared-btree.
12600 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 ** structure, re
12610 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 turn now. The re
12620 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 mainder of this
12630 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 procedure cleans
12640 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 . ** up the sh
12650 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f ared-btree.. */
12660 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 . assert( p->wa
12670 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 ntToLock==0 && p
12680 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 ->locked==0 );.
12690 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
126a0 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 e || removeFromS
126b0 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 haringList(pBt)
126c0 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 ){. /* The pB
126d0 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f t is no longer o
126e0 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 n the sharing li
126f0 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 st, so we can ac
12700 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 cess. ** it w
12710 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
12720 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e hold the mutex.
12730 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
12740 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c lean out and del
12750 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 ete the BtShared
12760 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a object.. */.
12770 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 assert( !pBt
12780 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 ->pCursor );.
12790 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
127a0 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b se(pBt->pPager);
127b0 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 . if( pBt->xF
127c0 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 reeSchema && pBt
127d0 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 ->pSchema ){.
127e0 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 pBt->xFreeSch
127f0 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ema(pBt->pSchema
12800 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
12810 69 74 65 33 44 62 46 72 65 65 28 30 2c 20 70 42 ite3DbFree(0, pB
12820 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 t->pSchema);.
12830 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 freeTempSpace(p
12840 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Bt);. sqlite3
12850 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 7d 0a _free(pBt);. }.
12860 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
12870 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
12880 45 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 E. assert( p->w
12890 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a antToLock==0 );.
128a0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 assert( p->loc
128b0 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 ked==0 );. if(
128c0 70 2d 3e 70 50 72 65 76 20 29 20 70 2d 3e 70 50 p->pPrev ) p->pP
128d0 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e rev->pNext = p->
128e0 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 70 2d 3e pNext;. if( p->
128f0 70 4e 65 78 74 20 29 20 70 2d 3e 70 4e 65 78 74 pNext ) p->pNext
12900 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 ->pPrev = p->pPr
12910 65 76 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 ev;.#endif.. sq
12920 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
12930 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
12940 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e K;.}../*.** Chan
12950 67 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 ge the limit on
12960 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
12970 67 65 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 74 ges allowed in t
12980 68 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 he cache..**.**
12990 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
129a0 65 72 20 6f 66 20 63 61 63 68 65 20 70 61 67 65 er of cache page
129b0 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 s is set to the
129c0 61 62 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 absolute.** valu
129d0 65 20 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 66 e of mxPage. If
129e0 20 6d 78 50 61 67 65 20 69 73 20 6e 65 67 61 74 mxPage is negat
129f0 69 76 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 ive, the pager w
12a00 69 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 61 ill.** operate a
12a10 73 79 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 synchronously -
12a20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 it will not stop
12a30 20 74 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 0a to do fsync()s.
12a40 2a 2a 20 74 6f 20 69 6e 73 75 72 65 20 64 61 74 ** to insure dat
12a50 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 a is written to
12a60 74 68 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 the disk surface
12a70 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 before.** conti
12a80 6e 75 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 74 nuing. Transact
12a90 69 6f 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b 20 ions still work
12aa0 69 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 if synchronous i
12ab0 73 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 s off,.** and th
12ac0 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f e database canno
12ad0 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 69 t be corrupted i
12ae0 66 20 74 68 69 73 20 70 72 6f 67 72 61 6d 0a 2a f this program.*
12af0 2a 20 63 72 61 73 68 65 73 2e 20 20 42 75 74 20 * crashes. But
12b00 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 if the operating
12b10 20 73 79 73 74 65 6d 20 63 72 61 73 68 65 73 20 system crashes
12b20 6f 72 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 or there is.** a
12b30 6e 20 61 62 72 75 70 74 20 70 6f 77 65 72 20 66 n abrupt power f
12b40 61 69 6c 75 72 65 20 77 68 65 6e 20 73 79 6e 63 ailure when sync
12b50 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 hronous is off,
12b60 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
12b70 63 6f 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e could be left in
12b80 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
12b90 20 61 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 and unrecoverab
12ba0 6c 65 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 6e le state..** Syn
12bb0 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 chronous is on b
12bc0 79 20 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 y default so dat
12bd0 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
12be0 20 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 is not.** norma
12bf0 6c 6c 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a lly a worry..*/.
12c00 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
12c10 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 72 SetCacheSize(Btr
12c20 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 ee *p, int mxPag
12c30 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a e){. BtShared *
12c40 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
12c50 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
12c60 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
12c70 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
12c80 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
12c90 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 p);. sqlite3Pag
12ca0 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 erSetCachesize(p
12cb0 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 Bt->pPager, mxPa
12cc0 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ge);. sqlite3Bt
12cd0 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
12ce0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12cf0 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d .}..#if SQLITE_M
12d00 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a 2f AX_MMAP_SIZE>0./
12d10 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
12d20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 61 6d 6f limit on the amo
12d30 75 6e 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 unt of the datab
12d40 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 6d 61 ase file that ma
12d50 79 20 62 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d y be.** memory m
12d60 61 70 70 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 apped..*/.int sq
12d70 6c 69 74 65 33 42 74 72 65 65 53 65 74 4d 6d 61 lite3BtreeSetMma
12d80 70 4c 69 6d 69 74 28 42 74 72 65 65 20 2a 70 2c pLimit(Btree *p,
12d90 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
12da0 7a 4d 6d 61 70 29 7b 0a 20 20 42 74 53 68 61 72 zMmap){. BtShar
12db0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
12dc0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
12dd0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
12de0 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
12df0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
12e00 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ter(p);. sqlite
12e10 33 50 61 67 65 72 53 65 74 4d 6d 61 70 4c 69 6d 3PagerSetMmapLim
12e20 69 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 it(pBt->pPager,
12e30 73 7a 4d 6d 61 70 29 3b 0a 20 20 73 71 6c 69 74 szMmap);. sqlit
12e40 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
12e50 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
12e60 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a _OK;.}.#endif /*
12e70 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 SQLITE_MAX_MMAP
12e80 5f 53 49 5a 45 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a _SIZE>0 */../*.*
12e90 2a 20 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 * Change the way
12ea0 20 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 data is synced
12eb0 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 to disk in order
12ec0 20 74 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 to increase or
12ed0 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 decrease.** how
12ee0 77 65 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 well the databas
12ef0 65 20 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 e resists damage
12f00 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 due to OS crash
12f10 65 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 es and power.**
12f20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c failures. Level
12f30 20 31 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 1 is the same a
12f40 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 s asynchronous (
12f50 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 no syncs() occur
12f60 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 and.** there is
12f70 20 61 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c a high probabil
12f80 69 74 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 ity of damage)
12f90 4c 65 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 Level 2 is the d
12fa0 65 66 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a efault. There.*
12fb0 2a 20 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 * is a very low
12fc0 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f but non-zero pro
12fd0 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 bability of dama
12fe0 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 ge. Level 3 red
12ff0 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 uces the.** prob
13000 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 ability of damag
13010 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 e to near zero b
13020 75 74 20 77 69 74 68 20 61 20 77 72 69 74 65 20 ut with a write
13030 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 performance redu
13040 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ction..*/.#ifnde
13050 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 f SQLITE_OMIT_PA
13060 47 45 52 5f 50 52 41 47 4d 41 53 0a 69 6e 74 20 GER_PRAGMAS.int
13070 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 sqlite3BtreeSetP
13080 61 67 65 72 46 6c 61 67 73 28 0a 20 20 42 74 72 agerFlags(. Btr
13090 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 ee *p,
130a0 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
130b0 20 74 6f 20 73 65 74 20 74 68 65 20 73 61 66 65 to set the safe
130c0 74 79 20 6c 65 76 65 6c 20 6f 6e 20 2a 2f 0a 20 ty level on */.
130d0 20 75 6e 73 69 67 6e 65 64 20 70 67 46 6c 61 67 unsigned pgFlag
130e0 73 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f s /* Vario
130f0 75 73 20 50 41 47 45 52 5f 2a 20 66 6c 61 67 73 us PAGER_* flags
13100 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 */.){. BtShare
13110 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
13120 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
13130 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
13140 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
13150 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
13160 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 er(p);. sqlite3
13170 50 61 67 65 72 53 65 74 46 6c 61 67 73 28 70 42 PagerSetFlags(pB
13180 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 46 6c 61 t->pPager, pgFla
13190 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 gs);. sqlite3Bt
131a0 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
131b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
131c0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
131d0 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
131e0 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 65 20 the given btree
131f0 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 74 79 is set to safety
13200 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 level 1. In ot
13210 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 her.** words, re
13220 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 turn TRUE if no
13230 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e sync() occurs on
13240 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e the disk files.
13250 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
13260 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 treeSyncDisabled
13270 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 (Btree *p){. Bt
13280 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
13290 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a >pBt;. int rc;.
132a0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
132b0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
132c0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a db->mutex) ); .
132d0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
132e0 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
132f0 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 ( pBt && pBt->pP
13300 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 ager );. rc = s
13310 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e qlite3PagerNosyn
13320 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a c(pBt->pPager);.
13330 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
13340 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
13350 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 rc;.}../*.** Ch
13360 61 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 ange the default
13370 20 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 pages size and
13380 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 the number of re
13390 73 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 served bytes per
133a0 20 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 page..** Or, if
133b0 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 the page size h
133c0 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
133d0 66 69 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 fixed, return SQ
133e0 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a LITE_READONLY .*
133f0 2a 20 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 * without changi
13400 6e 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a ng anything..**.
13410 2a 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 ** The page size
13420 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
13430 20 6f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 of 2 between 51
13440 32 20 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 2 and 65536. If
13450 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a the page.** siz
13460 65 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 e supplied does
13470 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f not meet this co
13480 6e 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 nstraint then th
13490 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e e page size is n
134a0 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a ot.** changed..*
134b0 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 *.** Page sizes
134c0 61 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 are constrained
134d0 74 6f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 to be a power of
134e0 20 74 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 two so that the
134f0 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 region.** of th
13500 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13510 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 used for locking
13520 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 (beginning at P
13530 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 ENDING_BYTE,.**
13540 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 the first byte p
13550 61 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e ast the 1GB boun
13560 64 61 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 dary, 0x40000000
13570 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 ) needs to occur
13580 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e .** at the begin
13590 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a ning of a page..
135a0 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 **.** If paramet
135b0 65 72 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c er nReserve is l
135c0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 ess than zero, t
135d0 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hen the number o
135e0 66 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 f reserved.** by
135f0 74 65 73 20 70 65 72 20 70 61 67 65 20 69 73 20 tes per page is
13600 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a left unchanged..
13610 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 **.** If the iFi
13620 78 21 3d 30 20 74 68 65 6e 20 74 68 65 20 42 54 x!=0 then the BT
13630 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 S_PAGESIZE_FIXED
13640 20 66 6c 61 67 20 69 73 20 73 65 74 20 73 6f 20 flag is set so
13650 74 68 61 74 20 74 68 65 20 70 61 67 65 20 73 69 that the page si
13660 7a 65 0a 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 ze.** and autova
13670 63 75 75 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f cuum mode can no
13680 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 longer be chang
13690 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ed..*/.int sqlit
136a0 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 e3BtreeSetPageSi
136b0 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ze(Btree *p, int
136c0 20 70 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e pageSize, int n
136d0 52 65 73 65 72 76 65 2c 20 69 6e 74 20 69 46 69 Reserve, int iFi
136e0 78 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 x){. int rc = S
136f0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 QLITE_OK;. BtSh
13700 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
13710 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 Bt;. assert( nR
13720 65 73 65 72 76 65 3e 3d 2d 31 20 26 26 20 6e 52 eserve>=-1 && nR
13730 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 eserve<=255 );.
13740 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
13750 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 er(p);. if( pBt
13760 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
13770 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 20 _PAGESIZE_FIXED
13780 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
13790 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
137a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 return SQLITE_R
137b0 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 EADONLY;. }. i
137c0 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 7b f( nReserve<0 ){
137d0 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 . nReserve =
137e0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
137f0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
13800 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
13810 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 Reserve>=0 && nR
13820 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 eserve<=255 );.
13830 20 69 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 if( pageSize>=5
13840 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 12 && pageSize<=
13850 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
13860 53 49 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 SIZE &&.
13870 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 ((pageSize-1)&pa
13880 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 geSize)==0 ){.
13890 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 assert( (pageS
138a0 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 ize & 7)==0 );.
138b0 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d assert( !pBt-
138c0 3e 70 50 61 67 65 31 20 26 26 20 21 70 42 74 2d >pPage1 && !pBt-
138d0 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 >pCursor );.
138e0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
138f0 28 75 33 32 29 70 61 67 65 53 69 7a 65 3b 0a 20 (u32)pageSize;.
13900 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 freeTempSpace
13910 28 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 (pBt);. }. rc
13920 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
13930 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
13940 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
13950 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 eSize, nReserve)
13960 3b 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 ;. pBt->usableS
13970 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 ize = pBt->pageS
13980 69 7a 65 20 2d 20 28 75 31 36 29 6e 52 65 73 65 ize - (u16)nRese
13990 72 76 65 3b 0a 20 20 69 66 28 20 69 46 69 78 20 rve;. if( iFix
139a0 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 ) pBt->btsFlags
139b0 7c 3d 20 42 54 53 5f 50 41 47 45 53 49 5a 45 5f |= BTS_PAGESIZE_
139c0 46 49 58 45 44 3b 0a 20 20 73 71 6c 69 74 65 33 FIXED;. sqlite3
139d0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
139e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
139f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
13a00 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 currently define
13a10 64 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 69 d page size.*/.i
13a20 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
13a30 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
13a40 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
13a50 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b ->pBt->pageSize;
13a60 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .}..#if defined(
13a70 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
13a80 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
13a90 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a ITE_DEBUG)./*.**
13aa0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
13ab0 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c s similar to sql
13ac0 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 ite3BtreeGetRese
13ad0 72 76 65 28 29 2c 20 65 78 63 65 70 74 20 74 68 rve(), except th
13ae0 61 74 20 69 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c at it.** may onl
13af0 79 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 69 y be called if i
13b00 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 t is guaranteed
13b10 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 65 20 that the b-tree
13b20 6d 75 74 65 78 20 69 73 20 61 6c 72 65 61 64 79 mutex is already
13b30 0a 2a 2a 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 .** held..**.**
13b40 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20 69 This is useful i
13b50 6e 20 6f 6e 65 20 73 70 65 63 69 61 6c 20 63 61 n one special ca
13b60 73 65 20 69 6e 20 74 68 65 20 62 61 63 6b 75 70 se in the backup
13b70 20 41 50 49 20 63 6f 64 65 20 77 68 65 72 65 20 API code where
13b80 69 74 20 69 73 0a 2a 2a 20 6b 6e 6f 77 6e 20 74 it is.** known t
13b90 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 62 hat the shared b
13ba0 2d 74 72 65 65 20 6d 75 74 65 78 20 69 73 20 68 -tree mutex is h
13bb0 65 6c 64 2c 20 62 75 74 20 74 68 65 20 6d 75 74 eld, but the mut
13bc0 65 78 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 64 61 ex on the .** da
13bd0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 tabase handle th
13be0 61 74 20 6f 77 6e 73 20 2a 70 20 69 73 20 6e 6f at owns *p is no
13bf0 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 t. In this case
13c00 69 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 if sqlite3BtreeE
13c10 6e 74 65 72 28 29 0a 2a 2a 20 77 65 72 65 20 74 nter().** were t
13c20 6f 20 62 65 20 63 61 6c 6c 65 64 2c 20 69 74 20 o be called, it
13c30 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65 20 77 69 might collide wi
13c40 74 68 20 73 6f 6d 65 20 6f 74 68 65 72 20 6f 70 th some other op
13c50 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65 0a 2a eration on the.*
13c60 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c * database handl
13c70 65 20 74 68 61 74 20 6f 77 6e 73 20 2a 70 2c 20 e that owns *p,
13c80 63 61 75 73 69 6e 67 20 75 6e 64 65 66 69 6e 65 causing undefine
13c90 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 69 d behavior..*/.i
13ca0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
13cb0 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 74 65 78 etReserveNoMutex
13cc0 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 (Btree *p){. as
13cd0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
13ce0 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d tex_held(p->pBt-
13cf0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 >mutex) );. ret
13d00 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 urn p->pBt->page
13d10 53 69 7a 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 Size - p->pBt->u
13d20 73 61 62 6c 65 53 69 7a 65 3b 0a 7d 0a 23 65 6e sableSize;.}.#en
13d30 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 dif /* SQLITE_HA
13d40 53 5f 43 4f 44 45 43 20 7c 7c 20 53 51 4c 49 54 S_CODEC || SQLIT
13d50 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 20 E_DEBUG */..#if
13d60 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
13d70 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d OMIT_PAGER_PRAGM
13d80 41 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 AS) || !defined(
13d90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 SQLITE_OMIT_VACU
13da0 55 4d 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e UM)./*.** Return
13db0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
13dc0 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 74 ytes of space at
13dd0 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 the end of ever
13de0 79 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 61 y page that.** a
13df0 72 65 20 69 6e 74 65 6e 74 75 61 6c 6c 79 20 6c re intentually l
13e00 65 66 74 20 75 6e 75 73 65 64 2e 20 20 54 68 69 eft unused. Thi
13e10 73 20 69 73 20 74 68 65 20 22 72 65 73 65 72 76 s is the "reserv
13e20 65 64 22 20 73 70 61 63 65 20 74 68 61 74 20 69 ed" space that i
13e30 73 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 75 s.** sometimes u
13e40 73 65 64 20 62 79 20 65 78 74 65 6e 73 69 6f 6e sed by extension
13e50 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 s..*/.int sqlite
13e60 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 3BtreeGetReserve
13e70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
13e80 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 t n;. sqlite3Bt
13e90 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e reeEnter(p);. n
13ea0 20 3d 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 = p->pBt->pageS
13eb0 69 7a 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 ize - p->pBt->us
13ec0 61 62 6c 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 ableSize;. sqli
13ed0 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
13ee0 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a ;. return n;.}.
13ef0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d ./*.** Set the m
13f00 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e aximum page coun
13f10 74 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 t for a database
13f20 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f if mxPage is po
13f30 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 sitive..** No ch
13f40 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 anges are made i
13f50 66 20 6d 78 50 61 67 65 20 69 73 20 30 20 6f 72 f mxPage is 0 or
13f60 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 negative..** Re
13f70 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
13f80 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2c value of mxPage,
13f90 20 72 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 return the maxi
13fa0 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a mum page count..
13fb0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 */.int sqlite3Bt
13fc0 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 reeMaxPageCount(
13fd0 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 Btree *p, int mx
13fe0 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a Page){. int n;.
13ff0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
14000 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 73 71 ter(p);. n = sq
14010 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 lite3PagerMaxPag
14020 65 43 6f 75 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 eCount(p->pBt->p
14030 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a Pager, mxPage);.
14040 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
14050 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
14060 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 n;.}../*.** Set
14070 20 74 68 65 20 42 54 53 5f 53 45 43 55 52 45 5f the BTS_SECURE_
14080 44 45 4c 45 54 45 20 66 6c 61 67 20 69 66 20 6e DELETE flag if n
14090 65 77 46 6c 61 67 20 69 73 20 30 20 6f 72 20 31 ewFlag is 0 or 1
140a0 2e 20 20 49 66 20 6e 65 77 46 6c 61 67 20 69 73 . If newFlag is
140b0 20 2d 31 2c 0a 2a 2a 20 74 68 65 6e 20 6d 61 6b -1,.** then mak
140c0 65 20 6e 6f 20 63 68 61 6e 67 65 73 2e 20 20 41 e no changes. A
140d0 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74 68 65 lways return the
140e0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 42 54 value of the BT
140f0 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a S_SECURE_DELETE.
14100 2a 2a 20 73 65 74 74 69 6e 67 20 61 66 74 65 72 ** setting after
14110 20 74 68 65 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a the change..*/.
14120 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14130 53 65 63 75 72 65 44 65 6c 65 74 65 28 42 74 72 SecureDelete(Btr
14140 65 65 20 2a 70 2c 20 69 6e 74 20 6e 65 77 46 6c ee *p, int newFl
14150 61 67 29 7b 0a 20 20 69 6e 74 20 62 3b 0a 20 20 ag){. int b;.
14160 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
14170 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 n 0;. sqlite3Bt
14180 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 reeEnter(p);. i
14190 66 28 20 6e 65 77 46 6c 61 67 3e 3d 30 20 29 7b f( newFlag>=0 ){
141a0 0a 20 20 20 20 70 2d 3e 70 42 74 2d 3e 62 74 73 . p->pBt->bts
141b0 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 53 45 Flags &= ~BTS_SE
141c0 43 55 52 45 5f 44 45 4c 45 54 45 3b 0a 20 20 20 CURE_DELETE;.
141d0 20 69 66 28 20 6e 65 77 46 6c 61 67 20 29 20 70 if( newFlag ) p
141e0 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 ->pBt->btsFlags
141f0 7c 3d 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 |= BTS_SECURE_DE
14200 4c 45 54 45 3b 0a 20 20 7d 20 0a 20 20 62 20 3d LETE;. } . b =
14210 20 28 70 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 (p->pBt->btsFla
14220 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f gs & BTS_SECURE_
14230 44 45 4c 45 54 45 29 21 3d 30 3b 0a 20 20 73 71 DELETE)!=0;. sq
14240 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
14250 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 3b 0a p);. return b;.
14260 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 }.#endif /* !def
14270 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
14280 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 _PAGER_PRAGMAS)
14290 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
142a0 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 TE_OMIT_VACUUM)
142b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 */../*.** Change
142c0 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 the 'auto-vacuu
142d0 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 m' property of t
142e0 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 he database. If
142f0 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27 the 'autoVacuum'
14300 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 .** parameter is
14310 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
14320 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 auto-vacuum mode
14330 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 is enabled. If
14340 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 zero, it.** is d
14350 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66 isabled. The def
14360 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 ault value for t
14370 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 he auto-vacuum p
14380 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 roperty is .** d
14390 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
143a0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
143b0 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f AUTOVACUUM macro
143c0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
143d0 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 BtreeSetAutoVacu
143e0 75 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 um(Btree *p, int
143f0 20 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 autoVacuum){.#i
14400 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
14410 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 _AUTOVACUUM. re
14420 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 turn SQLITE_READ
14430 4f 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 ONLY;.#else. Bt
14440 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
14450 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d >pBt;. int rc =
14460 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 SQLITE_OK;. u8
14470 20 61 76 20 3d 20 28 75 38 29 61 75 74 6f 56 61 av = (u8)autoVa
14480 63 75 75 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 cuum;.. sqlite3
14490 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
144a0 20 69 66 28 20 28 70 42 74 2d 3e 62 74 73 46 6c if( (pBt->btsFl
144b0 61 67 73 20 26 20 42 54 53 5f 50 41 47 45 53 49 ags & BTS_PAGESI
144c0 5a 45 5f 46 49 58 45 44 29 21 3d 30 20 26 26 20 ZE_FIXED)!=0 &&
144d0 28 61 76 20 3f 31 3a 30 29 21 3d 70 42 74 2d 3e (av ?1:0)!=pBt->
144e0 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 autoVacuum ){.
144f0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 rc = SQLITE_RE
14500 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b ADONLY;. }else{
14510 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 . pBt->autoVa
14520 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a 30 3b 0a cuum = av ?1:0;.
14530 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 pBt->incrVac
14540 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f 31 3a 30 uum = av==2 ?1:0
14550 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 ;. }. sqlite3B
14560 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
14570 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
14580 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 f.}../*.** Retur
14590 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 n the value of t
145a0 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 he 'auto-vacuum'
145b0 20 70 72 6f 70 65 72 74 79 2e 20 49 66 20 61 75 property. If au
145c0 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 0a 2a 2a to-vacuum is .**
145d0 20 65 6e 61 62 6c 65 64 20 31 20 69 73 20 72 65 enabled 1 is re
145e0 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
145f0 65 20 30 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 e 0..*/.int sqli
14600 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 te3BtreeGetAutoV
14610 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b acuum(Btree *p){
14620 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
14630 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
14640 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 return BTREE_AU
14650 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 TOVACUUM_NONE;.#
14660 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b 0a 20 else. int rc;.
14670 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
14680 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 0a er(p);. rc = (.
14690 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 61 75 (!p->pBt->au
146a0 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f toVacuum)?BTREE_
146b0 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3a AUTOVACUUM_NONE:
146c0 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 69 . (!p->pBt->i
146d0 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52 45 45 ncrVacuum)?BTREE
146e0 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c _AUTOVACUUM_FULL
146f0 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55 54 4f :. BTREE_AUTO
14700 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 29 3b VACUUM_INCR. );
14710 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
14720 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
14730 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a n rc;.#endif.}..
14740 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 65 66 ./*.** Get a ref
14750 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31 erence to pPage1
14760 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
14770 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c file. This wil
14780 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72 l.** also acquir
14790 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 e a readlock on
147a0 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a that file..**.**
147b0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
147c0 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 turned on succes
147d0 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 s. If the file
147e0 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c is not a.** well
147f0 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 -formed database
14800 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 file, then SQLI
14810 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 TE_CORRUPT is re
14820 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c 49 54 turned..** SQLIT
14830 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e E_BUSY is return
14840 65 64 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ed if the databa
14850 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 53 se is locked. S
14860 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 QLITE_NOMEM.** i
14870 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 77 65 s returned if we
14880 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f run out of memo
14890 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ry. .*/.static i
148a0 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 74 53 nt lockBtree(BtS
148b0 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 hared *pBt){. i
148c0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
148d0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
148e0 64 65 20 66 72 6f 6d 20 73 75 62 66 75 6e 63 74 de from subfunct
148f0 69 6f 6e 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ions */. MemPag
14900 65 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 2f e *pPage1; /
14910 2a 20 50 61 67 65 20 31 20 6f 66 20 74 68 65 20 * Page 1 of the
14920 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
14930 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 . int nPage;
14940 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
14950 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
14960 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
14970 69 6e 74 20 6e 50 61 67 65 46 69 6c 65 20 3d 20 int nPageFile =
14980 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 0; /* Number o
14990 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
149a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
149b0 20 20 69 6e 74 20 6e 50 61 67 65 48 65 61 64 65 int nPageHeade
149c0 72 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 r; /* Number
149d0 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
149e0 20 64 61 74 61 62 61 73 65 20 61 63 63 6f 72 64 database accord
149f0 69 6e 67 20 74 6f 20 68 64 72 20 2a 2f 0a 0a 20 ing to hdr */..
14a00 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14a10 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
14a20 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
14a30 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
14a40 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ==0 );. rc = sq
14a50 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 lite3PagerShared
14a60 4c 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 Lock(pBt->pPager
14a70 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
14a80 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
14a90 72 63 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 rc;. rc = btree
14aa0 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 GetPage(pBt, 1,
14ab0 26 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 &pPage1, 0);. i
14ac0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
14ad0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 ) return rc;..
14ae0 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 /* Do some chec
14af0 6b 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 king to help ins
14b00 75 72 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 ure the file we
14b10 6f 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 opened really is
14b20 0a 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 . ** a valid da
14b30 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 tabase file. .
14b40 2a 2f 0a 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 */. nPage = nPa
14b50 67 65 48 65 61 64 65 72 20 3d 20 67 65 74 34 62 geHeader = get4b
14b60 79 74 65 28 32 38 2b 28 75 38 2a 29 70 50 61 67 yte(28+(u8*)pPag
14b70 65 31 2d 3e 61 44 61 74 61 29 3b 0a 20 20 73 71 e1->aData);. sq
14b80 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
14b90 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c unt(pBt->pPager,
14ba0 20 26 6e 50 61 67 65 46 69 6c 65 29 3b 0a 20 20 &nPageFile);.
14bb0 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 7c 7c 20 if( nPage==0 ||
14bc0 6d 65 6d 63 6d 70 28 32 34 2b 28 75 38 2a 29 70 memcmp(24+(u8*)p
14bd0 50 61 67 65 31 2d 3e 61 44 61 74 61 2c 20 39 32 Page1->aData, 92
14be0 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 +(u8*)pPage1->aD
14bf0 61 74 61 2c 34 29 21 3d 30 20 29 7b 0a 20 20 20 ata,4)!=0 ){.
14c00 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 46 69 nPage = nPageFi
14c10 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 50 le;. }. if( nP
14c20 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 75 33 32 age>0 ){. u32
14c30 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 75 pageSize;. u
14c40 33 32 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 32 usableSize;.
14c50 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 u8 *page1 = p
14c60 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 Page1->aData;.
14c70 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
14c80 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65 TADB;. if( me
14c90 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67 mcmp(page1, zMag
14ca0 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30 icHeader, 16)!=0
14cb0 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
14cc0 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
14cd0 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 ;. }..#ifdef
14ce0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a SQLITE_OMIT_WAL.
14cf0 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 if( page1[18
14d00 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ]>1 ){. pBt
14d10 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 ->btsFlags |= BT
14d20 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0a 20 20 20 S_READ_ONLY;.
14d30 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 }. if( page1
14d40 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 [19]>1 ){.
14d50 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
14d60 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 23 65 failed;. }.#e
14d70 6c 73 65 0a 20 20 20 20 69 66 28 20 70 61 67 65 lse. if( page
14d80 31 5b 31 38 5d 3e 32 20 29 7b 0a 20 20 20 20 20 1[18]>2 ){.
14d90 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c pBt->btsFlags |
14da0 3d 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b = BTS_READ_ONLY;
14db0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
14dc0 61 67 65 31 5b 31 39 5d 3e 32 20 29 7b 0a 20 20 age1[19]>2 ){.
14dd0 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 goto page1_i
14de0 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
14df0 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
14e00 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e 20 69 write version i
14e10 73 20 73 65 74 20 74 6f 20 32 2c 20 74 68 69 73 s set to 2, this
14e20 20 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64 database should
14e30 20 62 65 20 61 63 63 65 73 73 65 64 0a 20 20 20 be accessed.
14e40 20 2a 2a 20 69 6e 20 57 41 4c 20 6d 6f 64 65 2e ** in WAL mode.
14e50 20 49 66 20 74 68 65 20 6c 6f 67 20 69 73 20 6e If the log is n
14e60 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c ot already open,
14e70 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 54 68 open it now. Th
14e80 65 6e 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 en . ** retur
14e90 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 n SQLITE_OK and
14ea0 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 70 return without p
14eb0 6f 70 75 6c 61 74 69 6e 67 20 42 74 53 68 61 72 opulating BtShar
14ec0 65 64 2e 70 50 61 67 65 31 2e 0a 20 20 20 20 2a ed.pPage1.. *
14ed0 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 65 74 * The caller det
14ee0 65 63 74 73 20 74 68 69 73 20 61 6e 64 20 63 61 ects this and ca
14ef0 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
14f00 6e 20 61 67 61 69 6e 2e 20 54 68 69 73 20 69 73 n again. This is
14f10 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64 . ** required
14f20 20 61 73 20 74 68 65 20 76 65 72 73 69 6f 6e 20 as the version
14f30 6f 66 20 70 61 67 65 20 31 20 63 75 72 72 65 6e of page 1 curren
14f40 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65 31 tly in the page1
14f50 20 62 75 66 66 65 72 0a 20 20 20 20 2a 2a 20 6d buffer. ** m
14f60 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 6c 61 ay not be the la
14f70 74 65 73 74 20 76 65 72 73 69 6f 6e 20 2d 20 74 test version - t
14f80 68 65 72 65 20 6d 61 79 20 62 65 20 61 20 6e 65 here may be a ne
14f90 77 65 72 20 6f 6e 65 20 69 6e 20 74 68 65 20 6c wer one in the l
14fa0 6f 67 0a 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a og. ** file..
14fb0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
14fc0 61 67 65 31 5b 31 39 5d 3d 3d 32 20 26 26 20 28 age1[19]==2 && (
14fd0 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 pBt->btsFlags &
14fe0 42 54 53 5f 4e 4f 5f 57 41 4c 29 3d 3d 30 20 29 BTS_NO_WAL)==0 )
14ff0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 4f 70 {. int isOp
15000 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 en = 0;. rc
15010 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f = sqlite3PagerO
15020 70 65 6e 57 61 6c 28 70 42 74 2d 3e 70 50 61 67 penWal(pBt->pPag
15030 65 72 2c 20 26 69 73 4f 70 65 6e 29 3b 0a 20 20 er, &isOpen);.
15040 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15050 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
15060 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 goto page1_init
15070 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d _failed;. }
15080 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65 6e 3d else if( isOpen=
15090 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
150a0 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 leasePage(pPage1
150b0 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
150c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
150d0 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
150e0 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 SQLITE_NOTADB;.
150f0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
15100 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 /* The maximum
15110 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f embedded fractio
15120 6e 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c n must be exactl
15130 79 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 y 25%. And the
15140 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 minimum. ** e
15150 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e mbedded fraction
15160 20 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 must be 12.5% f
15170 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 or both leaf-dat
15180 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 a and non-leaf-d
15190 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 ata.. ** The
151a0 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 original design
151b0 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d allowed these am
151c0 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 ounts to vary, b
151d0 75 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 ut as of. **
151e0 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 version 3.6.0, w
151f0 65 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 e require them t
15200 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 o be fixed..
15210 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d */. if( memcm
15220 70 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c p(&page1[21], "\
15230 31 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 100\040\040",3)!
15240 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f =0 ){. goto
15250 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
15260 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 ed;. }. pa
15270 67 65 53 69 7a 65 20 3d 20 28 70 61 67 65 31 5b geSize = (page1[
15280 31 36 5d 3c 3c 38 29 20 7c 20 28 70 61 67 65 31 16]<<8) | (page1
15290 5b 31 37 5d 3c 3c 31 36 29 3b 0a 20 20 20 20 69 [17]<<16);. i
152a0 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 f( ((pageSize-1)
152b0 26 70 61 67 65 53 69 7a 65 29 21 3d 30 0a 20 20 &pageSize)!=0.
152c0 20 20 20 7c 7c 20 70 61 67 65 53 69 7a 65 3e 53 || pageSize>S
152d0 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
152e0 49 5a 45 20 0a 20 20 20 20 20 7c 7c 20 70 61 67 IZE . || pag
152f0 65 53 69 7a 65 3c 3d 32 35 36 20 0a 20 20 20 20 eSize<=256 .
15300 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
15310 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
15320 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
15330 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 t( (pageSize & 7
15340 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 )==0 );. usab
15350 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a leSize = pageSiz
15360 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 e - page1[20];.
15370 20 20 20 69 66 28 20 28 75 33 32 29 70 61 67 65 if( (u32)page
15380 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 Size!=pBt->pageS
15390 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ize ){. /*
153a0 41 66 74 65 72 20 72 65 61 64 69 6e 67 20 74 68 After reading th
153b0 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 e first page of
153c0 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 the database ass
153d0 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73 69 7a uming a page siz
153e0 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 e. ** of Bt
153f0 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 2c Shared.pageSize,
15400 20 77 65 20 68 61 76 65 20 64 69 73 63 6f 76 65 we have discove
15410 72 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67 red that the pag
15420 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 20 20 e-size is.
15430 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 67 65 ** actually page
15440 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 Size. Unlock the
15450 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61 76 65 database, leave
15460 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61 74 0a pBt->pPage1 at.
15470 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e ** zero an
15480 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f d return SQLITE_
15490 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 OK. The caller w
154a0 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 ill call this fu
154b0 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 nction. **
154c0 61 67 61 69 6e 20 77 69 74 68 20 74 68 65 20 63 again with the c
154d0 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65 orrect page-size
154e0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
154f0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
15500 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42 74 2d ge1);. pBt-
15510 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 75 73 >usableSize = us
15520 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 ableSize;.
15530 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
15540 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 pageSize;.
15550 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 freeTempSpace(pB
15560 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 t);. rc = s
15570 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
15580 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 gesize(pBt->pPag
15590 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 er, &pBt->pageSi
155a0 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze,.
155b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155c0 20 20 20 20 20 20 20 70 61 67 65 53 69 7a 65 2d pageSize-
155d0 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 usableSize);.
155e0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
155f0 20 20 7d 0a 20 20 20 20 69 66 28 20 28 70 42 74 }. if( (pBt
15600 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 ->db->flags & SQ
15610 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 LITE_RecoveryMod
15620 65 29 3d 3d 30 20 26 26 20 6e 50 61 67 65 3e 6e e)==0 && nPage>n
15630 50 61 67 65 46 69 6c 65 20 29 7b 0a 20 20 20 20 PageFile ){.
15640 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
15650 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
15660 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
15670 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
15680 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69 if( usableSi
15690 7a 65 3c 34 38 30 20 29 7b 0a 20 20 20 20 20 20 ze<480 ){.
156a0 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
156b0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
156c0 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
156d0 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 = pageSize;.
156e0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
156f0 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 69 = usableSize;.#i
15700 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15710 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
15720 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
15730 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 = (get4byte(&pa
15740 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 ge1[36 + 4*4])?1
15750 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e :0);. pBt->in
15760 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 crVacuum = (get4
15770 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b byte(&page1[36 +
15780 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 7*4])?1:0);.#en
15790 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d 61 dif. }.. /* ma
157a0 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d 61 xLocal is the ma
157b0 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 ximum amount of
157c0 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 65 payload to store
157d0 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 2a locally for. *
157e0 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 20 * a cell. Make
157f0 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c 6c sure it is small
15800 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 20 enough so that
15810 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e 6f at least minFano
15820 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 61 ut. ** cells ca
15830 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f 6e n will fit on on
15840 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 75 e page. We assu
15850 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 67 me a 10-byte pag
15860 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 42 e header.. ** B
15870 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c 6f esides the paylo
15880 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 73 ad, the cell mus
15890 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 20 t store:. **
158a0 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72 2-byte pointer
158b0 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 2a to the cell. *
158c0 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 69 * 4-byte chi
158d0 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 ld pointer. **
158e0 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20 9-byte nKey
158f0 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 value. ** 4
15900 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75 -byte nData valu
15910 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 e. ** 4-byt
15920 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 e overflow page
15930 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f 20 pointer. ** So
15940 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 a cell consists
15950 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 6e of a 2-byte poin
15960 74 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 ter, a header wh
15970 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 ich is as much a
15980 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 s. ** 17 bytes
15990 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 long, 0 to N byt
159a0 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 es of payload, a
159b0 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 nd an optional 4
159c0 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 byte overflow.
159d0 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 ** page pointer
159e0 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 .. */. pBt->ma
159f0 78 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 28 28 xLocal = (u16)((
15a00 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
15a10 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 29 12)*64/255 - 23)
15a20 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 ;. pBt->minLoca
15a30 6c 20 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e l = (u16)((pBt->
15a40 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 usableSize-12)*3
15a50 32 2f 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 70 2/255 - 23);. p
15a60 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 28 75 Bt->maxLeaf = (u
15a70 31 36 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 16)(pBt->usableS
15a80 69 7a 65 20 2d 20 33 35 29 3b 0a 20 20 70 42 74 ize - 35);. pBt
15a90 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 75 31 36 ->minLeaf = (u16
15aa0 29 28 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 )((pBt->usableSi
15ab0 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 ze-12)*32/255 -
15ac0 32 33 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 23);. if( pBt->
15ad0 6d 61 78 4c 6f 63 61 6c 3e 31 32 37 20 29 7b 0a maxLocal>127 ){.
15ae0 20 20 20 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 pBt->max1byt
15af0 65 50 61 79 6c 6f 61 64 20 3d 20 31 32 37 3b 0a ePayload = 127;.
15b00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 }else{. pBt
15b10 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61 ->max1bytePayloa
15b20 64 20 3d 20 28 75 38 29 70 42 74 2d 3e 6d 61 78 d = (u8)pBt->max
15b30 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 Local;. }. ass
15b40 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 ert( pBt->maxLea
15b50 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c f + 23 <= MX_CEL
15b60 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 L_SIZE(pBt) );.
15b70 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 pBt->pPage1 = p
15b80 50 61 67 65 31 3b 0a 20 20 70 42 74 2d 3e 6e 50 Page1;. pBt->nP
15b90 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 72 age = nPage;. r
15ba0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15bb0 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 ..page1_init_fai
15bc0 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 led:. releasePa
15bd0 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 ge(pPage1);. pB
15be0 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 t->pPage1 = 0;.
15bf0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
15c00 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
15c10 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
15c20 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
15c30 20 6f 70 65 6e 20 6f 6e 20 70 42 74 2e 20 54 68 open on pBt. Th
15c40 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a is is for use.**
15c50 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 in assert() exp
15c60 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 ressions, so it
15c70 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 is only compiled
15c80 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f if NDEBUG is no
15c90 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a t.** defined..**
15ca0 0a 2a 2a 20 4f 6e 6c 79 20 77 72 69 74 65 20 63 .** Only write c
15cb0 75 72 73 6f 72 73 20 61 72 65 20 63 6f 75 6e 74 ursors are count
15cc0 65 64 20 69 66 20 77 72 4f 6e 6c 79 20 69 73 20 ed if wrOnly is
15cd0 74 72 75 65 2e 20 20 49 66 20 77 72 4f 6e 6c 79 true. If wrOnly
15ce0 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 74 68 65 is.** false the
15cf0 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 n all cursors ar
15d00 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a e counted..**.**
15d10 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 For the purpose
15d20 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e s of this routin
15d30 65 2c 20 61 20 63 75 72 73 6f 72 20 69 73 20 61 e, a cursor is a
15d40 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a ny cursor that.*
15d50 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 * is capable of
15d60 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
15d70 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
15d80 73 65 2e 20 20 43 75 72 73 6f 72 73 20 74 68 61 se. Cursors tha
15d90 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 74 t.** have been t
15da0 72 69 70 70 65 64 20 69 6e 74 6f 20 74 68 65 20 ripped into the
15db0 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 73 74 61 CURSOR_FAULT sta
15dc0 74 65 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 te are not count
15dd0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
15de0 74 20 63 6f 75 6e 74 56 61 6c 69 64 43 75 72 73 t countValidCurs
15df0 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 ors(BtShared *pB
15e00 74 2c 20 69 6e 74 20 77 72 4f 6e 6c 79 29 7b 0a t, int wrOnly){.
15e10 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
15e20 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 ;. int r = 0;.
15e30 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 for(pCur=pBt->p
15e40 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 Cursor; pCur; pC
15e50 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b ur=pCur->pNext){
15e60 0a 20 20 20 20 69 66 28 20 28 77 72 4f 6e 6c 79 . if( (wrOnly
15e70 3d 3d 30 20 7c 7c 20 28 70 43 75 72 2d 3e 63 75 ==0 || (pCur->cu
15e80 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 57 72 rFlags & BTCF_Wr
15e90 69 74 65 46 6c 61 67 29 21 3d 30 29 0a 20 20 20 iteFlag)!=0).
15ea0 20 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 && pCur->eStat
15eb0 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e!=CURSOR_FAULT
15ec0 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 ) r++; . }. re
15ed0 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 turn r;.}.#endif
15ee0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ../*.** If there
15ef0 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 are no outstand
15f00 69 6e 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 ing cursors and
15f10 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 we are not in th
15f20 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 e middle.** of a
15f30 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 transaction but
15f40 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 there is a read
15f50 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
15f60 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 abase, then.** t
15f70 68 69 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 his routine unre
15f80 66 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 fs the first pag
15f90 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
15fa0 65 20 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a e file which .**
15fb0 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 has the effect
15fc0 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 of releasing the
15fd0 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a read lock..**.*
15fe0 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 * If there is a
15ff0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 transaction in p
16000 72 6f 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f rogress, this ro
16010 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
16020 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
16030 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
16040 75 73 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 used(BtShared *p
16050 42 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 Bt){. assert( s
16060 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
16070 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
16080 0a 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 . assert( count
16090 56 61 6c 69 64 43 75 72 73 6f 72 73 28 70 42 74 ValidCursors(pBt
160a0 2c 30 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 ,0)==0 || pBt->i
160b0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 nTransaction>TRA
160c0 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 NS_NONE );. if(
160d0 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
160e0 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ion==TRANS_NONE
160f0 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d && pBt->pPage1!=
16100 30 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 0 ){. MemPage
16110 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e *pPage1 = pBt->
16120 70 50 61 67 65 31 3b 0a 20 20 20 20 61 73 73 65 pPage1;. asse
16130 72 74 28 20 70 50 61 67 65 31 2d 3e 61 44 61 74 rt( pPage1->aDat
16140 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
16150 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
16160 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
16170 72 29 3d 3d 31 20 29 3b 0a 20 20 20 20 70 42 74 r)==1 );. pBt
16180 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
16190 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
161a0 61 67 65 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a age1);. }.}../*
161b0 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e 74 .** If pBt point
161c0 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 69 s to an empty fi
161d0 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 le then convert
161e0 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 0a that empty file.
161f0 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 6d ** into a new em
16200 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 20 pty database by
16210 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 initializing the
16220 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a 2a first page of.*
16230 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a * the database..
16240 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 */.static int ne
16250 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72 wDatabase(BtShar
16260 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 ed *pBt){. MemP
16270 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69 age *pP1;. unsi
16280 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
16290 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
162a0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
162b0 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
162c0 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42 tex) );. if( pB
162d0 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 t->nPage>0 ){.
162e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
162f0 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 OK;. }. pP1 =
16300 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 pBt->pPage1;. a
16310 73 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b ssert( pP1!=0 );
16320 0a 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 . data = pP1->a
16330 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c Data;. rc = sql
16340 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
16350 50 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 P1->pDbPage);.
16360 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
16370 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 rc;. memcpy(dat
16380 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c a, zMagicHeader,
16390 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 sizeof(zMagicHe
163a0 61 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 ader));. assert
163b0 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 ( sizeof(zMagicH
163c0 65 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 eader)==16 );.
163d0 64 61 74 61 5b 31 36 5d 20 3d 20 28 75 38 29 28 data[16] = (u8)(
163e0 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3e (pBt->pageSize>>
163f0 38 29 26 30 78 66 66 29 3b 0a 20 20 64 61 74 61 8)&0xff);. data
16400 5b 31 37 5d 20 3d 20 28 75 38 29 28 28 70 42 74 [17] = (u8)((pBt
16410 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 31 36 29 26 ->pageSize>>16)&
16420 30 78 66 66 29 3b 0a 20 20 64 61 74 61 5b 31 38 0xff);. data[18
16430 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 39 ] = 1;. data[19
16440 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 ] = 1;. assert(
16450 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
16460 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 <=pBt->pageSize
16470 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 && pBt->usableSi
16480 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 67 ze+255>=pBt->pag
16490 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32 eSize);. data[2
164a0 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e 70 0] = (u8)(pBt->p
164b0 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 ageSize - pBt->u
164c0 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64 61 sableSize);. da
164d0 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 64 ta[21] = 64;. d
164e0 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 20 ata[22] = 32;.
164f0 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a 20 data[23] = 32;.
16500 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 34 memset(&data[24
16510 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 20 ], 0, 100-24);.
16520 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 50 zeroPage(pP1, P
16530 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 TF_INTKEY|PTF_LE
16540 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 20 AF|PTF_LEAFDATA
16550 29 3b 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 );. pBt->btsFla
16560 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 49 gs |= BTS_PAGESI
16570 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e 64 65 ZE_FIXED;.#ifnde
16580 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16590 54 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 TOVACUUM. asser
165a0 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 t( pBt->autoVacu
165b0 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 um==1 || pBt->au
165c0 74 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 toVacuum==0 );.
165d0 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
165e0 63 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 crVacuum==1 || p
165f0 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d Bt->incrVacuum==
16600 30 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 0 );. put4byte(
16610 26 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c &data[36 + 4*4],
16620 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
16630 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 );. put4byte(&d
16640 61 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 ata[36 + 7*4], p
16650 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b Bt->incrVacuum);
16660 0a 23 65 6e 64 69 66 0a 20 20 70 42 74 2d 3e 6e .#endif. pBt->n
16670 50 61 67 65 20 3d 20 31 3b 0a 20 20 64 61 74 61 Page = 1;. data
16680 5b 33 31 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 [31] = 1;. retu
16690 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
166a0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
166b0 65 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 e the first page
166c0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
166d0 20 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 20 file (creating
166e0 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f a database.** co
166f0 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 nsisting of a si
16700 6e 67 6c 65 20 70 61 67 65 20 61 6e 64 20 6e 6f ngle page and no
16710 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 29 schema objects)
16720 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f . Return SQLITE_
16730 4f 4b 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 73 OK.** if success
16740 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 ful, or an SQLit
16750 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 e error code oth
16760 65 72 77 69 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 erwise..*/.int s
16770 71 6c 69 74 65 33 42 74 72 65 65 4e 65 77 44 62 qlite3BtreeNewDb
16780 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
16790 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 t rc;. sqlite3B
167a0 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
167b0 70 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 p->pBt->nPage =
167c0 30 3b 0a 20 20 72 63 20 3d 20 6e 65 77 44 61 74 0;. rc = newDat
167d0 61 62 61 73 65 28 70 2d 3e 70 42 74 29 3b 0a 20 abase(p->pBt);.
167e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
167f0 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
16800 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 rc;.}../*.** Att
16810 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 empt to start a
16820 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e new transaction.
16830 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 A write-transac
16840 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 tion.** is start
16850 65 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 ed if the second
16860 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e argument is non
16870 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 zero, otherwise
16880 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 a read-.** trans
16890 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 action. If the
168a0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
168b0 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 is 2 or more and
168c0 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 exclusive.** tr
168d0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
168e0 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 rted, meaning th
168f0 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 at no other proc
16900 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a ess is allowed.*
16910 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 * to access the
16920 64 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 database. A pre
16930 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 existing transac
16940 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a tion may not be.
16950 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 ** upgraded to e
16960 78 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c xclusive by call
16970 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
16980 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d a second time -
16990 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 the.** exclusiv
169a0 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f ity flag only wo
169b0 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 rks for a new tr
169c0 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
169d0 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 A write-transac
169e0 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 tion must be sta
169f0 72 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 rted before atte
16a00 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 mpting any .** c
16a10 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 hanges to the da
16a20 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 tabase. None of
16a30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 the following r
16a40 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c outines .** will
16a50 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 work unless a t
16a60 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 ransaction is st
16a70 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a arted first:.**.
16a80 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
16a90 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 treeCreateTable(
16aa0 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
16ab0 33 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 3BtreeCreateInde
16ac0 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 x().** sqli
16ad0 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 te3BtreeClearTab
16ae0 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c le().** sql
16af0 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
16b00 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c le().** sql
16b10 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
16b20 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
16b30 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 3BtreeDelete().*
16b40 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 * sqlite3Bt
16b50 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a reeUpdateMeta().
16b60 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 **.** If an init
16b70 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 ial attempt to a
16b80 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 cquire the lock
16b90 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 fails because of
16ba0 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e lock contention
16bb0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 .** and the data
16bc0 62 61 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 base was previou
16bd0 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 sly unlocked, th
16be0 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 en invoke the bu
16bf0 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 sy handler.** if
16c00 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 there is one.
16c10 42 75 74 20 69 66 20 74 68 65 72 65 20 77 61 73 But if there was
16c20 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 previously a re
16c30 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a ad-lock, do not.
16c40 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 ** invoke the bu
16c50 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 sy handler - jus
16c60 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f t return SQLITE_
16c70 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 BUSY. SQLITE_BU
16c80 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e SY is .** return
16c90 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 ed when there is
16ca0 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d already a read-
16cb0 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f lock in order to
16cc0 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 avoid a deadloc
16cd0 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 k..**.** Suppose
16ce0 20 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 there are two p
16cf0 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 rocesses A and B
16d00 2e 20 20 41 20 68 61 73 20 61 20 72 65 61 64 20 . A has a read
16d10 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a lock and B has.*
16d20 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 * a reserved loc
16d30 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 k. B tries to p
16d40 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 romote to exclus
16d50 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b ive but is block
16d60 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 ed because.** of
16d70 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 A's read lock.
16d80 20 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d A tries to prom
16d90 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 ote to reserved
16da0 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 but is blocked b
16db0 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 y B..** One or t
16dc0 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 he other of the
16dd0 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 two processes mu
16de0 73 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 st give way or t
16df0 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e here can be.** n
16e00 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 o progress. By
16e10 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 returning SQLITE
16e20 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e _BUSY and not in
16e30 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 voking the busy
16e40 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e callback.** when
16e50 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 A already has a
16e60 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 read lock, we e
16e70 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 ncourage A to gi
16e80 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a ve up and let B.
16e90 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 69 ** proceed..*/.i
16ea0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 nt sqlite3BtreeB
16eb0 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 20 eginTrans(Btree
16ec0 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 7b *p, int wrflag){
16ed0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f . sqlite3 *pBlo
16ee0 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 ck = 0;. BtShar
16ef0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
16f00 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
16f10 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 ITE_OK;.. sqlit
16f20 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
16f30 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
16f40 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 y(p);.. /* If t
16f50 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 he btree is alre
16f60 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 ady in a write-t
16f70 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 ransaction, or i
16f80 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 t. ** is alread
16f90 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e y in a read-tran
16fa0 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 saction and a re
16fb0 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 ad-transaction.
16fc0 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 ** is requested
16fd0 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
16fe0 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d p.. */. if( p-
16ff0 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
17000 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 WRITE || (p->inT
17010 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 rans==TRANS_READ
17020 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a && !wrflag) ){.
17030 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 goto trans_b
17040 65 67 75 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 egun;. }. asse
17050 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 rt( pBt->inTrans
17060 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
17070 49 54 45 20 7c 7c 20 49 66 4e 6f 74 4f 6d 69 74 ITE || IfNotOmit
17080 41 56 28 70 42 74 2d 3e 62 44 6f 54 72 75 6e 63 AV(pBt->bDoTrunc
17090 61 74 65 29 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a ate)==0 );.. /*
170a0 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 Write transacti
170b0 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 ons are not poss
170c0 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f ible on a read-o
170d0 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a nly database */.
170e0 20 20 69 66 28 20 28 70 42 74 2d 3e 62 74 73 46 if( (pBt->btsF
170f0 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 5f lags & BTS_READ_
17100 4f 4e 4c 59 29 21 3d 30 20 26 26 20 77 72 66 6c ONLY)!=0 && wrfl
17110 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 ag ){. rc = S
17120 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
17130 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 goto trans_b
17140 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 egun;. }..#ifnd
17150 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
17160 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a HARED_CACHE. /*
17170 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61 If another data
17180 62 61 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20 base handle has
17190 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61 already opened a
171a0 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
171b0 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 on . ** on this
171c0 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 shared-btree st
171d0 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 73 65 ructure and a se
171e0 63 6f 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73 cond write trans
171f0 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 action is. ** r
17200 65 71 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e equested, return
17210 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
17220 20 20 2a 2f 0a 20 20 69 66 28 20 28 77 72 66 6c */. if( (wrfl
17230 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 ag && pBt->inTra
17240 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
17250 57 52 49 54 45 29 0a 20 20 20 7c 7c 20 28 70 42 WRITE). || (pB
17260 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
17270 53 5f 50 45 4e 44 49 4e 47 29 21 3d 30 0a 20 20 S_PENDING)!=0.
17280 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 ){. pBlock =
17290 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 pBt->pWriter->db
172a0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 ;. }else if( wr
172b0 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 flag>1 ){. Bt
172c0 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 Lock *pIter;.
172d0 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
172e0 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
172f0 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
17300 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 ){. if( pIt
17310 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b er->pBtree!=p ){
17320 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 . pBlock
17330 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d = pIter->pBtree-
17340 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 >db;. bre
17350 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
17360 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f }. }. if( pBlo
17370 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ck ){. sqlite
17380 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
17390 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b ed(p->db, pBlock
173a0 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
173b0 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
173c0 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 CACHE;. goto
173d0 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
173e0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e .#endif.. /* An
173f0 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 y read-only or r
17400 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 ead-write transa
17410 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 ction implies a
17420 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 read-lock on .
17430 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66 ** page 1. So if
17440 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72 some other shar
17450 65 64 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20 ed-cache client
17460 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 77 72 already has a wr
17470 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f ite-lock . ** o
17480 6e 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 72 n page 1, the tr
17490 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 ansaction cannot
174a0 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 be opened. */.
174b0 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 rc = queryShare
174c0 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
174d0 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 p, MASTER_ROOT,
174e0 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 READ_LOCK);. if
174f0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 ( SQLITE_OK!=rc
17500 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 ) goto trans_beg
17510 75 6e 3b 0a 0a 20 20 70 42 74 2d 3e 62 74 73 46 un;.. pBt->btsF
17520 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 49 4e 49 lags &= ~BTS_INI
17530 54 49 41 4c 4c 59 5f 45 4d 50 54 59 3b 0a 20 20 TIALLY_EMPTY;.
17540 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d if( pBt->nPage==
17550 30 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 0 ) pBt->btsFlag
17560 73 20 7c 3d 20 42 54 53 5f 49 4e 49 54 49 41 4c s |= BTS_INITIAL
17570 4c 59 5f 45 4d 50 54 59 3b 0a 20 20 64 6f 20 7b LY_EMPTY;. do {
17580 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 . /* Call loc
17590 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 kBtree() until e
175a0 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 ither pBt->pPage
175b0 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 1 is populated o
175c0 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 r. ** lockBtr
175d0 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d ee() returns som
175e0 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ething other tha
175f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 n SQLITE_OK. loc
17600 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 kBtree(). **
17610 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
17620 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 E_OK but leave p
17630 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 Bt->pPage1 set t
17640 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 o 0 if after.
17650 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 ** reading page
17660 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 1 it discovers
17670 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 that the page-si
17680 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
17690 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 se . ** file
176a0 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 is not pBt->page
176b0 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 Size. In this ca
176c0 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 se lockBtree() w
176d0 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a ill update. *
176e0 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 * pBt->pageSize
176f0 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 to the page-size
17700 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 of the file on
17710 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 disk.. */.
17720 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 while( pBt->pPa
17730 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 ge1==0 && SQLITE
17740 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 _OK==(rc = lockB
17750 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 tree(pBt)) );..
17760 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
17770 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 E_OK && wrflag )
17780 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 42 74 {. if( (pBt
17790 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
177a0 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d 30 20 29 _READ_ONLY)!=0 )
177b0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
177c0 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
177d0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
177e0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
177f0 33 50 61 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3PagerBegin(pBt-
17800 3e 70 50 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 >pPager,wrflag>1
17810 2c 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 ,sqlite3TempInMe
17820 6d 6f 72 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 mory(p->db));.
17830 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
17840 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17850 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 rc = newDat
17860 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 abase(pBt);.
17870 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
17880 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 }. . if( r
17890 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
178a0 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 unlockBtre
178b0 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
178c0 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 }. }while(
178d0 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 (rc&0xFF)==SQLIT
178e0 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69 E_BUSY && pBt->i
178f0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
17900 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 ANS_NONE &&.
17910 20 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b btreeInvok
17920 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 eBusyHandler(pBt
17930 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d ) );.. if( rc==
17940 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17950 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d if( p->inTrans=
17960 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 =TRANS_NONE ){.
17970 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 pBt->nTrans
17980 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 action++;.#ifnde
17990 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
179a0 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 ARED_CACHE.
179b0 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
179c0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
179d0 72 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 rt( p->lock.pBtr
179e0 65 65 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b ee==p && p->lock
179f0 2e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 .iTable==1 );.
17a00 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c p->lock.eL
17a10 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b ock = READ_LOCK;
17a20 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b . p->lock
17a30 2e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c .pNext = pBt->pL
17a40 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 70 42 74 ock;. pBt
17a50 2d 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f ->pLock = &p->lo
17a60 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 ck;. }.#end
17a70 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e if. }. p->
17a80 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 inTrans = (wrfla
17a90 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 g?TRANS_WRITE:TR
17aa0 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 ANS_READ);. i
17ab0 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 f( p->inTrans>pB
17ac0 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
17ad0 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 ){. pBt->i
17ae0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 nTransaction = p
17af0 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d ->inTrans;. }
17b00 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 20 . if( wrflag
17b10 29 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 ){. MemPage
17b20 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e *pPage1 = pBt->
17b30 70 50 61 67 65 31 3b 0a 23 69 66 6e 64 65 66 20 pPage1;.#ifndef
17b40 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
17b50 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 61 ED_CACHE. a
17b60 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 57 72 ssert( !pBt->pWr
17b70 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 70 42 iter );. pB
17b80 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 70 3b 0a t->pWriter = p;.
17b90 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c pBt->btsFl
17ba0 61 67 73 20 26 3d 20 7e 42 54 53 5f 45 58 43 4c ags &= ~BTS_EXCL
17bb0 55 53 49 56 45 3b 0a 20 20 20 20 20 20 69 66 28 USIVE;. if(
17bc0 20 77 72 66 6c 61 67 3e 31 20 29 20 70 42 74 2d wrflag>1 ) pBt-
17bd0 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 >btsFlags |= BTS
17be0 5f 45 58 43 4c 55 53 49 56 45 3b 0a 23 65 6e 64 _EXCLUSIVE;.#end
17bf0 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 if.. /* If
17c00 74 68 65 20 64 62 2d 73 69 7a 65 20 68 65 61 64 the db-size head
17c10 65 72 20 66 69 65 6c 64 20 69 73 20 69 6e 63 6f er field is inco
17c20 72 72 65 63 74 20 28 61 73 20 69 74 20 6d 61 79 rrect (as it may
17c30 20 62 65 20 69 66 20 61 6e 20 6f 6c 64 0a 20 20 be if an old.
17c40 20 20 20 20 2a 2a 20 63 6c 69 65 6e 74 20 68 61 ** client ha
17c50 73 20 62 65 65 6e 20 77 72 69 74 69 6e 67 20 74 s been writing t
17c60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
17c70 29 2c 20 75 70 64 61 74 65 20 69 74 20 6e 6f 77 ), update it now
17c80 2e 20 44 6f 69 6e 67 0a 20 20 20 20 20 20 2a 2a . Doing. **
17c90 20 74 68 69 73 20 73 6f 6f 6e 65 72 20 72 61 74 this sooner rat
17ca0 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 20 6d her than later m
17cb0 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 eans the databas
17cc0 65 20 73 69 7a 65 20 63 61 6e 20 73 61 66 65 6c e size can safel
17cd0 79 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 2d 72 y . ** re-r
17ce0 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 ead the database
17cf0 20 73 69 7a 65 20 66 72 6f 6d 20 70 61 67 65 20 size from page
17d00 31 20 69 66 20 61 20 73 61 76 65 70 6f 69 6e 74 1 if a savepoint
17d10 20 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a or transaction.
17d20 20 20 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 ** rollbac
17d30 6b 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 k occurs within
17d40 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
17d50 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
17d60 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d if( pBt->nPage!=
17d70 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 get4byte(&pPage1
17d80 2d 3e 61 44 61 74 61 5b 32 38 5d 29 20 29 7b 0a ->aData[28]) ){.
17d90 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
17da0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17db0 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
17dc0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
17dd0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17de0 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
17df0 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
17e00 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 28], pBt->nPage)
17e10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
17e20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a }. }. }...
17e30 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 trans_begun:. i
17e40 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17e50 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 && wrflag ){.
17e60 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d /* This call m
17e70 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 akes sure that t
17e80 68 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 he pager has the
17e90 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 correct number
17ea0 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 of. ** open s
17eb0 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 avepoints. If th
17ec0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
17ed0 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 er is greater th
17ee0 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 an 0 and. **
17ef0 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
17f00 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
17f10 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c pen, then it wil
17f20 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 l be opened here
17f30 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
17f40 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 = sqlite3PagerOp
17f50 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d enSavepoint(pBt-
17f60 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e >pPager, p->db->
17f70 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d nSavepoint);. }
17f80 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 .. btreeIntegri
17f90 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 ty(p);. sqlite3
17fa0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
17fb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
17fc0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17fd0 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f IT_AUTOVACUUM../
17fe0 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 *.** Set the poi
17ff0 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
18000 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 for all childre
18010 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e n of page pPage.
18020 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 Also, if.** pPa
18030 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c ge contains cell
18040 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 s that point to
18050 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 overflow pages,
18060 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a set the pointer.
18070 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 ** map entries f
18080 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
18090 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a pages as well..*
180a0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 /.static int set
180b0 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d ChildPtrmaps(Mem
180c0 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
180d0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
180e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
180f0 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 /* Counter va
18100 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 riable */. int
18110 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 nCell;
18120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c * Number of cell
18140 73 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 s in page pPage
18150 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
18160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18170 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
18180 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 n code */. BtSh
18190 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 ared *pBt = pPag
181a0 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 e->pBt;. u8 isI
181b0 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d nitOrig = pPage-
181c0 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 >isInit;. Pgno
181d0 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 pgno = pPage->pg
181e0 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 no;.. assert( s
181f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18200 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
18210 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 tex) );. rc = b
18220 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 treeInitPage(pPa
18230 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 ge);. if( rc!=S
18240 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18250 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 goto set_child_p
18260 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a trmaps_out;. }.
18270 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d nCell = pPage-
18280 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 >nCell;.. for(i
18290 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b =0; i<nCell; i++
182a0 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c ){. u8 *pCell
182b0 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
182c0 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d e, i);.. ptrm
182d0 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 apPutOvflPtr(pPa
182e0 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b ge, pCell, &rc);
182f0 0a 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 .. if( !pPage
18300 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
18310 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d Pgno childPgno =
18320 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 get4byte(pCell)
18330 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 ;. ptrmapPu
18340 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f t(pBt, childPgno
18350 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
18360 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 pgno, &rc);.
18370 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 }. }.. if( !pP
18380 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
18390 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 Pgno childPgno
183a0 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
183b0 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
183c0 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
183d0 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
183e0 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 , childPgno, PTR
183f0 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c MAP_BTREE, pgno,
18400 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f &rc);. }..set_
18410 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 child_ptrmaps_ou
18420 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e t:. pPage->isIn
18430 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b it = isInitOrig;
18440 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18450 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 ./*.** Somewhere
18460 20 6f 6e 20 70 50 61 67 65 20 69 73 20 61 20 70 on pPage is a p
18470 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 ointer to page i
18480 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 From. Modify th
18490 69 73 20 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a is pointer so.**
184a0 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
184b0 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 to iTo. Paramete
184c0 72 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 r eType describe
184d0 73 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f s the type of po
184e0 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d inter to.** be m
184f0 6f 64 69 66 69 65 64 2c 20 61 73 20 20 66 6f 6c odified, as fol
18500 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d lows:.**.** PTRM
18510 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 AP_BTREE: pP
18520 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 age is a btree-p
18530 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 age. The pointer
18540 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 points at a chi
18550 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ld .**
18560 20 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 page of
18570 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 pPage..**.** PT
18580 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 RMAP_OVERFLOW1:
18590 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 pPage is a btree
185a0 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 -page. The point
185b0 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 er points at an
185c0 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 overflow.**
185d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
185e0 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ge pointed to by
185f0 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c one of the cell
18600 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a s on pPage..**.*
18610 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f * PTRMAP_OVERFLO
18620 57 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 W2: pPage is an
18630 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 overflow-page. T
18640 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 he pointer point
18650 73 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a s at the next.**
18660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18670 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 overflow page
18680 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f in the list..*/
18690 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 .static int modi
186a0 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 fyPagePointer(Me
186b0 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 mPage *pPage, Pg
186c0 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 no iFrom, Pgno i
186d0 54 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 To, u8 eType){.
186e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
186f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
18700 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
18710 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
18720 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
18730 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
18740 67 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 ge) );. if( eTy
18750 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 pe==PTRMAP_OVERF
18760 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 LOW2 ){. /* T
18770 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c he pointer is al
18780 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 ways the first 4
18790 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 bytes of the pa
187a0 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ge in this case.
187b0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 */. if( get
187c0 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
187d0 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 ta)!=iFrom ){.
187e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
187f0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
18800 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 }. put4by
18810 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c te(pPage->aData,
18820 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a iTo);. }else{.
18830 20 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 u8 isInitOri
18840 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 g = pPage->isIni
18850 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 t;. int i;.
18860 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 int nCell;..
18870 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 btreeInitPage(
18880 70 50 61 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c pPage);. nCel
18890 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c l = pPage->nCell
188a0 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ;.. for(i=0;
188b0 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
188c0 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d u8 *pCell =
188d0 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
188e0 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 i);. if( e
188f0 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 Type==PTRMAP_OVE
18900 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 RFLOW1 ){.
18910 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
18920 0a 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61 . btreePa
18930 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
18940 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
18950 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 . if( inf
18960 6f 2e 69 4f 76 65 72 66 6c 6f 77 0a 20 20 20 20 o.iOverflow.
18970 20 20 20 20 20 26 26 20 70 43 65 6c 6c 2b 69 6e && pCell+in
18980 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 2b 33 3c 3d fo.iOverflow+3<=
18990 70 50 61 67 65 2d 3e 61 44 61 74 61 2b 70 50 61 pPage->aData+pPa
189a0 67 65 2d 3e 6d 61 73 6b 50 61 67 65 0a 20 20 20 ge->maskPage.
189b0 20 20 20 20 20 20 26 26 20 69 46 72 6f 6d 3d 3d && iFrom==
189c0 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b get4byte(&pCell[
189d0 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 info.iOverflow])
189e0 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
189f0 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
18a00 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 pCell[info.iOver
18a10 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 flow], iTo);.
18a20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
18a30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
18a40 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 lse{. if(
18a50 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 get4byte(pCell)
18a60 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 ==iFrom ){.
18a70 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 put4byte(pC
18a80 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 ell, iTo);.
18a90 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
18aa0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
18ab0 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 }. . if( i
18ac0 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 ==nCell ){.
18ad0 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d if( eType!=PTRM
18ae0 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 AP_BTREE || .
18af0 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 get4byte(
18b00 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
18b10 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
18b20 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 ])!=iFrom ){.
18b30 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
18b40 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
18b50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
18b60 75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e ut4byte(&pPage->
18b70 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18b80 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b Offset+8], iTo);
18b90 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 . }.. pPag
18ba0 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e e->isInit = isIn
18bb0 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 itOrig;. }. re
18bc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
18bd0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 }.../*.** Move t
18be0 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 he open database
18bf0 20 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f page pDbPage to
18c00 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 location iFreeP
18c10 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 age in the .** d
18c20 61 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 atabase. The pDb
18c30 50 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 Page reference r
18c40 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a emains valid..**
18c50 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 .** The isCommit
18c60 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20 flag indicates
18c70 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f that there is no
18c80 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 need to remembe
18c90 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f r that.** the jo
18ca0 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 urnal needs to b
18cb0 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 e sync()ed befor
18cc0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
18cd0 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a pDbPage->pgno .*
18ce0 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e * can be written
18cf0 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 to. The caller
18d00 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d has already prom
18d10 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 ised not to writ
18d20 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 e to that.** pag
18d30 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
18d40 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 relocatePage(.
18d50 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
18d60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 /* Btr
18d70 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ee */. MemPage
18d80 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20 *pDbPage,
18d90 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f /* Open page to
18da0 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 move */. u8 eT
18db0 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ype,
18dc0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d /* Pointer m
18dd0 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20 ap 'type' entry
18de0 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 for pDbPage */.
18df0 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 Pgno iPtrPage,
18e00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
18e10 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e nter map 'page-n
18e20 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 o' entry for pDb
18e30 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 Page */. Pgno i
18e40 46 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20 FreePage,
18e50 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 /* The locati
18e60 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 on to move pDbPa
18e70 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 ge to */. int i
18e80 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 sCommit
18e90 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 /* isCommit
18ea0 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73 flag passed to s
18eb0 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
18ec0 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 age */.){. MemP
18ed0 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20 age *pPtrPage;
18ee0 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 /* The page tha
18ef0 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 t contains a poi
18f00 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20 nter to pDbPage
18f10 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 */. Pgno iDbPag
18f20 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e e = pDbPage->pgn
18f30 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 o;. Pager *pPag
18f40 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 er = pBt->pPager
18f50 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
18f60 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54 ssert( eType==PT
18f70 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c RMAP_OVERFLOW2 |
18f80 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
18f90 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 OVERFLOW1 || .
18fa0 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 eType==PTRMA
18fb0 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 P_BTREE || eType
18fc0 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 ==PTRMAP_ROOTPAG
18fd0 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 E );. assert( s
18fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18ff0 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
19000 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61 . assert( pDbPa
19010 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a ge->pBt==pBt );.
19020 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 . /* Move page
19030 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73 iDbPage from its
19040 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f current locatio
19050 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 n to page number
19060 20 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 iFreePage */.
19070 54 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55 TRACE(("AUTOVACU
19080 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f UM: Moving %d to
19090 20 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70 free page %d (p
190a0 74 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20 tr page %d type
190b0 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 %d)\n", . i
190c0 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 DbPage, iFreePag
190d0 65 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79 e, iPtrPage, eTy
190e0 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c pe));. rc = sql
190f0 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 ite3PagerMovepag
19100 65 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67 e(pPager, pDbPag
19110 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65 e->pDbPage, iFre
19120 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 ePage, isCommit)
19130 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
19140 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
19150 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 urn rc;. }. pD
19160 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 bPage->pgno = iF
19170 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 reePage;.. /* I
19180 66 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20 f pDbPage was a
19190 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e btree-page, then
191a0 20 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69 it may have chi
191b0 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 ld pages and/or
191c0 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 cells. ** that
191d0 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f point to overflo
191e0 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69 w pages. The poi
191f0 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 nter map entries
19200 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 for all these.
19210 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74 ** pages need t
19220 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 o be changed..
19230 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61 **. ** If pDbPa
19240 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f ge is an overflo
19250 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 w page, then the
19260 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d first 4 bytes m
19270 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 ay store a. **
19280 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62 pointer to a sub
19290 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 sequent overflow
192a0 20 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69 page. If this i
192b0 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e s the case, then
192c0 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 . ** the pointe
192d0 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62 r map needs to b
192e0 65 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68 e updated for th
192f0 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 e subsequent ove
19300 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f rflow page.. */
19310 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 . if( eType==PT
19320 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 RMAP_BTREE || eT
19330 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype==PTRMAP_ROOT
19340 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d PAGE ){. rc =
19350 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 setChildPtrmaps
19360 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
19370 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
19380 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19390 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c rc;. }. }el
193a0 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 se{. Pgno nex
193b0 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 tOvfl = get4byte
193c0 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 (pDbPage->aData)
193d0 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 ;. if( nextOv
193e0 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 fl!=0 ){. p
193f0 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 trmapPut(pBt, ne
19400 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f xtOvfl, PTRMAP_O
19410 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 VERFLOW2, iFreeP
19420 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 age, &rc);.
19430 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
19440 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
19450 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
19460 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
19470 20 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73 Fix the databas
19480 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 e pointer on pag
19490 65 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20 e iPtrPage that
194a0 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61 pointed at iDbPa
194b0 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 ge so. ** that
194c0 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72 it points at iFr
194d0 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 eePage. Also fix
194e0 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
194f0 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 entry for. **
19500 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 iPtrPage.. */.
19510 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d if( eType!=PTRM
19520 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 AP_ROOTPAGE ){.
19530 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
19540 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 Page(pBt, iPtrPa
19550 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 ge, &pPtrPage, 0
19560 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
19570 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19580 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
19590 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
195a0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
195b0 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 trPage->pDbPage)
195c0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
195d0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
195e0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 releasePage(pPt
195f0 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 rPage);. re
19600 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
19610 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 rc = modifyPa
19620 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 gePointer(pPtrPa
19630 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 ge, iDbPage, iFr
19640 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a eePage, eType);.
19650 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
19660 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 pPtrPage);. i
19670 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
19680 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ){. ptrmap
19690 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 Put(pBt, iFreePa
196a0 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 ge, eType, iPtrP
196b0 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d age, &rc);. }
196c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
196d0 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 ;.}../* Forward
196e0 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 declaration requ
196f0 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 ired by incrVacu
19700 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 umStep(). */.sta
19710 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 tic int allocate
19720 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 BtreePage(BtShar
19730 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a ed *, MemPage **
19740 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 , Pgno *, Pgno,
19750 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 u8);../*.** Perf
19760 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 orm a single ste
19770 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e p of an incremen
19780 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 tal-vacuum. If s
19790 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 uccessful, retur
197a0 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 n.** SQLITE_OK.
197b0 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 If there is no w
197c0 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 ork to do (and t
197d0 68 65 72 65 66 6f 72 65 20 6e 6f 20 70 6f 69 6e herefore no poin
197e0 74 20 69 6e 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 t in .** calling
197f0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
19800 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 gain), return SQ
19810 4c 49 54 45 5f 44 4f 4e 45 2e 20 4f 72 2c 20 69 LITE_DONE. Or, i
19820 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f f an error .** o
19830 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 73 6f ccurs, return so
19840 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 me other error c
19850 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 ode..**.** More
19860 73 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 specifically, th
19870 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 is function atte
19880 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e mpts to re-organ
19890 69 7a 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ize the database
198a0 20 73 6f 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 so .** that the
198b0 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 last page of th
198c0 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 e file currently
198d0 20 69 6e 20 75 73 65 20 69 73 20 6e 6f 20 6c 6f in use is no lo
198e0 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a nger in use..**.
198f0 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 6e 46 69 ** Parameter nFi
19900 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 n is the number
19910 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 74 68 of pages that th
19920 69 73 20 64 61 74 61 62 61 73 65 20 77 6f 75 6c is database woul
19930 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 77 65 72 d contain.** wer
19940 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 e this function
19950 63 61 6c 6c 65 64 20 75 6e 74 69 6c 20 69 74 20 called until it
19960 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 returns SQLITE_D
19970 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ONE..**.** If th
19980 65 20 62 43 6f 6d 6d 69 74 20 70 61 72 61 6d 65 e bCommit parame
19990 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ter is non-zero,
199a0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
199b0 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
199c0 0a 2a 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 .** caller will
199d0 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 keep calling inc
199e0 72 56 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e rVacuumStep() un
199f0 74 69 6c 20 69 74 20 72 65 74 75 72 6e 73 20 53 til it returns S
19a00 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 2a 2a 20 6f QLITE_DONE .** o
19a10 72 20 61 6e 20 65 72 72 6f 72 2e 20 62 43 6f 6d r an error. bCom
19a20 6d 69 74 20 69 73 20 70 61 73 73 65 64 20 74 72 mit is passed tr
19a30 75 65 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 ue for an auto-v
19a40 61 63 75 75 6d 2d 6f 6e 2d 63 6f 6d 6d 69 74 20 acuum-on-commit
19a50 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20 6f .** operation, o
19a60 72 20 66 61 6c 73 65 20 66 6f 72 20 61 6e 20 69 r false for an i
19a70 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
19a80 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
19a90 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
19aa0 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
19ab0 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 gno nFin, Pgno i
19ac0 4c 61 73 74 50 67 2c 20 69 6e 74 20 62 43 6f 6d LastPg, int bCom
19ad0 6d 69 74 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 mit){. Pgno nFr
19ae0 65 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 eeList;
19af0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
19b00 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 ages still on th
19b10 65 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 20 e free-list */.
19b20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 int rc;.. asse
19b30 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
19b40 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
19b50 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
19b60 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b 0a iLastPg>nFin );.
19b70 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 . if( !PTRMAP_I
19b80 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
19b90 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d Pg) && iLastPg!=
19ba0 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
19bb0 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 75 38 E(pBt) ){. u8
19bc0 20 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f eType;. Pgno
19bd0 20 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 iPtrPage;..
19be0 6e 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 nFreeList = get4
19bf0 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 byte(&pBt->pPage
19c00 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 1->aData[36]);.
19c10 20 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 if( nFreeList
19c20 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
19c30 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
19c40 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d . }.. rc =
19c50 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
19c60 69 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c iLastPg, &eType,
19c70 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 &iPtrPage);.
19c80 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
19c90 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
19ca0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
19cb0 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
19cc0 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 AP_ROOTPAGE ){.
19cd0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
19ce0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
19cf0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
19d00 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 eType==PTRMAP_FR
19d10 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 EEPAGE ){.
19d20 69 66 28 20 62 43 6f 6d 6d 69 74 3d 3d 30 20 29 if( bCommit==0 )
19d30 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d {. /* Rem
19d40 6f 76 65 20 74 68 65 20 70 61 67 65 20 66 72 6f ove the page fro
19d50 6d 20 74 68 65 20 66 69 6c 65 73 20 66 72 65 65 m the files free
19d60 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 6e -list. This is n
19d70 6f 74 20 72 65 71 75 69 72 65 64 0a 20 20 20 20 ot required.
19d80 20 20 20 20 2a 2a 20 69 66 20 62 43 6f 6d 6d 69 ** if bCommi
19d90 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 t is non-zero. I
19da0 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 n that case, the
19db0 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 free-list will
19dc0 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 be. ** tr
19dd0 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 uncated to zero
19de0 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
19df0 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 ion returns, so
19e00 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 it doesn't .
19e10 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 ** matter if
19e20 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 it still contai
19e30 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 ns some garbage
19e40 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 entries..
19e50 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f */. Pgno
19e60 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 iFreePg;.
19e70 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 MemPage *pFree
19e80 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d Pg;. rc =
19e90 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
19ea0 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 ge(pBt, &pFreePg
19eb0 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73 , &iFreePg, iLas
19ec0 74 50 67 2c 20 42 54 41 4c 4c 4f 43 5f 45 58 41 tPg, BTALLOC_EXA
19ed0 43 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 CT);. if(
19ee0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
19ef0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
19f00 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
19f10 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
19f20 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 iFreePg==iLastP
19f30 67 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c g );. rel
19f40 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50 67 easePage(pFreePg
19f50 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
19f60 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 50 67 else {. Pg
19f70 6e 6f 20 69 46 72 65 65 50 67 3b 20 20 20 20 20 no iFreePg;
19f80 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
19f90 20 6f 66 20 66 72 65 65 20 70 61 67 65 20 74 6f of free page to
19fa0 20 6d 6f 76 65 20 70 4c 61 73 74 50 67 20 74 6f move pLastPg to
19fb0 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 */. MemPag
19fc0 65 20 2a 70 4c 61 73 74 50 67 3b 0a 20 20 20 20 e *pLastPg;.
19fd0 20 20 75 38 20 65 4d 6f 64 65 20 3d 20 42 54 41 u8 eMode = BTA
19fe0 4c 4c 4f 43 5f 41 4e 59 3b 20 20 20 2f 2a 20 4d LLOC_ANY; /* M
19ff0 6f 64 65 20 70 61 72 61 6d 65 74 65 72 20 66 6f ode parameter fo
1a000 72 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 r allocateBtreeP
1a010 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 50 age() */. P
1a020 67 6e 6f 20 69 4e 65 61 72 20 3d 20 30 3b 20 20 gno iNear = 0;
1a030 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 65 61 72 /* near
1a040 62 79 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 by parameter for
1a050 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
1a060 67 65 28 29 20 2a 2f 0a 0a 20 20 20 20 20 20 72 ge() */.. r
1a070 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
1a080 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 (pBt, iLastPg, &
1a090 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 pLastPg, 0);.
1a0a0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1a0b0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1a0c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1a0d0 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1a0e0 62 43 6f 6d 6d 69 74 20 69 73 20 7a 65 72 6f 2c bCommit is zero,
1a0f0 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 this loop runs
1a100 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 exactly once and
1a110 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 page pLastPg.
1a120 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 ** is swappe
1a130 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 d with the first
1a140 20 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 free page pulle
1a150 64 20 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c d off the free l
1a160 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ist.. **.
1a170 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 ** On the ot
1a180 68 65 72 20 68 61 6e 64 2c 20 69 66 20 62 43 6f her hand, if bCo
1a190 6d 6d 69 74 20 69 73 20 67 72 65 61 74 65 72 20 mmit is greater
1a1a0 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 than zero, then
1a1b0 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f keep. ** lo
1a1c0 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 oping until a fr
1a1d0 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65 64 20 ee-page located
1a1e0 77 69 74 68 69 6e 20 74 68 65 20 66 69 72 73 74 within the first
1a1f0 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20 20 20 nFin pages.
1a200 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 ** of the file
1a210 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 is found..
1a220 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 43 */. if( bC
1a230 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ommit==0 ){.
1a240 20 20 20 20 65 4d 6f 64 65 20 3d 20 42 54 41 4c eMode = BTAL
1a250 4c 4f 43 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20 LOC_LE;.
1a260 69 4e 65 61 72 20 3d 20 6e 46 69 6e 3b 0a 20 20 iNear = nFin;.
1a270 20 20 20 20 7d 0a 20 20 20 20 20 20 64 6f 20 7b }. do {
1a280 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 . MemPage
1a290 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 *pFreePg;.
1a2a0 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
1a2b0 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
1a2c0 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 pFreePg, &iFreeP
1a2d0 67 2c 20 69 4e 65 61 72 2c 20 65 4d 6f 64 65 29 g, iNear, eMode)
1a2e0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1a2f0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1a300 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 release
1a310 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 Page(pLastPg);.
1a320 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1a330 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
1a340 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
1a350 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 e(pFreePg);.
1a360 20 20 7d 77 68 69 6c 65 28 20 62 43 6f 6d 6d 69 }while( bCommi
1a370 74 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 t && iFreePg>nFi
1a380 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 n );. asser
1a390 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 t( iFreePg<iLast
1a3a0 50 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 Pg );. .
1a3b0 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 rc = relocate
1a3c0 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 Page(pBt, pLastP
1a3d0 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 g, eType, iPtrPa
1a3e0 67 65 2c 20 69 46 72 65 65 50 67 2c 20 62 43 6f ge, iFreePg, bCo
1a3f0 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 72 65 6c mmit);. rel
1a400 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 easePage(pLastPg
1a410 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1a420 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1a430 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1a440 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1a450 20 7d 0a 0a 20 20 69 66 28 20 62 43 6f 6d 6d 69 }.. if( bCommi
1a460 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 6f 20 7b t==0 ){. do {
1a470 0a 20 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d . iLastPg--
1a480 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 69 4c ;. }while( iL
1a490 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 astPg==PENDING_B
1a4a0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 7c 7c YTE_PAGE(pBt) ||
1a4b0 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
1a4c0 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 3b 0a Bt, iLastPg) );.
1a4d0 20 20 20 20 70 42 74 2d 3e 62 44 6f 54 72 75 6e pBt->bDoTrun
1a4e0 63 61 74 65 20 3d 20 31 3b 0a 20 20 20 20 70 42 cate = 1;. pB
1a4f0 74 2d 3e 6e 50 61 67 65 20 3d 20 69 4c 61 73 74 t->nPage = iLast
1a500 50 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e Pg;. }. return
1a510 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1a520 2a 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61 73 *.** The databas
1a530 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 e opened by the
1a540 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
1a550 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
1a560 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 6e 4f 72 database.** nOr
1a570 69 67 20 70 61 67 65 73 20 69 6e 20 73 69 7a 65 ig pages in size
1a580 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 46 72 65 containing nFre
1a590 65 20 66 72 65 65 20 70 61 67 65 73 2e 20 52 65 e free pages. Re
1a5a0 74 75 72 6e 20 74 68 65 20 65 78 70 65 63 74 65 turn the expecte
1a5b0 64 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 d .** size of th
1a5c0 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 70 61 e database in pa
1a5d0 67 65 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e ges following an
1a5e0 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 70 65 auto-vacuum ope
1a5f0 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 ration..*/.stati
1a600 63 20 50 67 6e 6f 20 66 69 6e 61 6c 44 62 53 69 c Pgno finalDbSi
1a610 7a 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ze(BtShared *pBt
1a620 2c 20 50 67 6e 6f 20 6e 4f 72 69 67 2c 20 50 67 , Pgno nOrig, Pg
1a630 6e 6f 20 6e 46 72 65 65 29 7b 0a 20 20 69 6e 74 no nFree){. int
1a640 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 nEntry;
1a650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a660 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
1a670 73 20 6f 6e 20 6f 6e 65 20 70 74 72 6d 61 70 20 s on one ptrmap
1a680 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e page */. Pgno n
1a690 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 20 20 Ptrmap;
1a6a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1a6b0 62 65 72 20 6f 66 20 50 74 72 4d 61 70 20 70 61 ber of PtrMap pa
1a6c0 67 65 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 ges to be freed
1a6d0 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 20 */. Pgno nFin;
1a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6f0 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 /* Return v
1a700 61 6c 75 65 20 2a 2f 0a 0a 20 20 6e 45 6e 74 72 alue */.. nEntr
1a710 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 y = pBt->usableS
1a720 69 7a 65 2f 35 3b 0a 20 20 6e 50 74 72 6d 61 70 ize/5;. nPtrmap
1a730 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b = (nFree-nOrig+
1a740 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
1a750 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e 74 72 79 t, nOrig)+nEntry
1a760 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 6e 46 69 6e )/nEntry;. nFin
1a770 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 72 65 65 = nOrig - nFree
1a780 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20 20 69 66 - nPtrmap;. if
1a790 28 20 6e 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f ( nOrig>PENDING_
1a7a0 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 26 BYTE_PAGE(pBt) &
1a7b0 26 20 6e 46 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 & nFin<PENDING_B
1a7c0 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
1a7d0 0a 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 7d . nFin--;. }
1a7e0 0a 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 . while( PTRMAP
1a7f0 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 46 69 _ISPAGE(pBt, nFi
1a800 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 n) || nFin==PEND
1a810 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
1a820 74 29 20 29 7b 0a 20 20 20 20 6e 46 69 6e 2d 2d t) ){. nFin--
1a830 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
1a840 6e 46 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 nFin;.}../*.** A
1a850 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
1a860 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 on must be opene
1a870 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 d before calling
1a880 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
1a890 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 ** It performs a
1a8a0 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 single unit of
1a8b0 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 work towards an
1a8c0 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
1a8d0 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 um..**.** If the
1a8e0 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
1a8f0 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 uum is finished
1a900 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
1a910 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 ion has run,.**
1a920 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 SQLITE_DONE is r
1a930 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 eturned. If it i
1a940 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 s not finished,
1a950 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 but no error occ
1a960 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 urred,.** SQLITE
1a970 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
1a980 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 Otherwise an SQ
1a990 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e Lite error code.
1a9a0 20 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 .*/.int sqlite3
1a9b0 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 BtreeIncrVacuum(
1a9c0 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
1a9d0 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 rc;. BtShared
1a9e0 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a *pBt = p->pBt;..
1a9f0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
1aa00 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
1aa10 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ( pBt->inTransac
1aa20 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
1aa30 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d E && p->inTrans=
1aa40 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
1aa50 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 6f if( !pBt->auto
1aa60 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 Vacuum ){. rc
1aa70 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a = SQLITE_DONE;.
1aa80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e }else{. Pgn
1aa90 6f 20 6e 4f 72 69 67 20 3d 20 62 74 72 65 65 50 o nOrig = btreeP
1aaa0 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
1aab0 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 20 3d 20 Pgno nFree =
1aac0 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 get4byte(&pBt->p
1aad0 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
1aae0 29 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e );. Pgno nFin
1aaf0 20 3d 20 66 69 6e 61 6c 44 62 53 69 7a 65 28 70 = finalDbSize(p
1ab00 42 74 2c 20 6e 4f 72 69 67 2c 20 6e 46 72 65 65 Bt, nOrig, nFree
1ab10 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 );.. if( nOri
1ab20 67 3c 6e 46 69 6e 20 29 7b 0a 20 20 20 20 20 20 g<nFin ){.
1ab30 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
1ab40 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 65 UPT_BKPT;. }e
1ab50 6c 73 65 20 69 66 28 20 6e 46 72 65 65 3e 30 20 lse if( nFree>0
1ab60 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 61 ){. rc = sa
1ab70 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 veAllCursors(pBt
1ab80 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 , 0, 0);. i
1ab90 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1aba0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 ){. inva
1abb0 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f lidateAllOverflo
1abc0 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 wCache(pBt);.
1abd0 20 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 rc = incrVa
1abe0 63 75 75 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 cuumStep(pBt, nF
1abf0 69 6e 2c 20 6e 4f 72 69 67 2c 20 30 29 3b 0a 20 in, nOrig, 0);.
1ac00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1ac10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ac20 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
1ac30 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1ac40 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
1ac50 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
1ac60 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 put4byte(&pBt->p
1ac70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d Page1->aData[28]
1ac80 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 , pBt->nPage);.
1ac90 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
1aca0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1acb0 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a ITE_DONE;. }.
1acc0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1acd0 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1ace0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1acf0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1ad00 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 s called prior t
1ad10 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f o sqlite3PagerCo
1ad20 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e mmit when a tran
1ad30 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f saction.** is co
1ad40 6d 6d 69 74 74 65 64 20 66 6f 72 20 61 6e 20 61 mmitted for an a
1ad50 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
1ad60 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 ase..**.** If SQ
1ad70 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
1ad80 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 ned, then *pnTru
1ad90 6e 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 nc is set to the
1ada0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1adb0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1adc0 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
1add0 74 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 truncated to dur
1ade0 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 ing the commit p
1adf0 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e rocess. .** i.e.
1ae00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1ae10 73 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a s been reorganiz
1ae20 65 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 ed so that only
1ae30 74 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 the first *pnTru
1ae40 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 nc.** pages are
1ae50 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 in use..*/.stati
1ae60 63 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d c int autoVacuum
1ae70 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 Commit(BtShared
1ae80 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 *pBt){. int rc
1ae90 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 = SQLITE_OK;. P
1aea0 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
1aeb0 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 Bt->pPager;. VV
1aec0 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 A_ONLY( int nRef
1aed0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 = sqlite3PagerR
1aee0 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 efcount(pPager)
1aef0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 );.. assert( sq
1af00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1af10 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1af20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f invalidateAllO
1af30 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 verflowCache(pBt
1af40 29 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d );. assert(pBt-
1af50 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 >autoVacuum);.
1af60 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 if( !pBt->incrVa
1af70 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f cuum ){. Pgno
1af80 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f nFin; /
1af90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
1afa0 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 s in database af
1afb0 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e ter autovacuumin
1afc0 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 g */. Pgno nF
1afd0 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ree; /* N
1afe0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
1aff0 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 n the freelist i
1b000 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 nitially */.
1b010 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 Pgno iFree;
1b020 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 /* The next p
1b030 61 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 age to be freed
1b040 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 */. Pgno nOri
1b050 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 g; /* Dat
1b060 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 6f 72 abase size befor
1b070 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 20 e freeing */..
1b080 20 20 6e 4f 72 69 67 20 3d 20 62 74 72 65 65 50 nOrig = btreeP
1b090 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
1b0a0 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53 if( PTRMAP_IS
1b0b0 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 PAGE(pBt, nOrig)
1b0c0 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 || nOrig==PENDI
1b0d0 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1b0e0 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 ) ){. /* It
1b0f0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1b100 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 61 74 to create a dat
1b110 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 68 20 abase for which
1b120 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 0a 20 the final page.
1b130 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 68 65 ** is eithe
1b140 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 r a pointer-map
1b150 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 6e 64 page or the pend
1b160 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e 20 49 ing-byte page. I
1b170 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 f one. ** i
1b180 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 s encountered, t
1b190 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 63 6f his indicates co
1b1a0 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 rruption..
1b1b0 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
1b1c0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1b1d0 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 KPT;. }..
1b1e0 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 nFree = get4byte
1b1f0 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
1b200 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e Data[36]);. n
1b210 46 69 6e 20 3d 20 66 69 6e 61 6c 44 62 53 69 7a Fin = finalDbSiz
1b220 65 28 70 42 74 2c 20 6e 4f 72 69 67 2c 20 6e 46 e(pBt, nOrig, nF
1b230 72 65 65 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 ree);. if( nF
1b240 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 72 in>nOrig ) retur
1b250 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1b260 5f 42 4b 50 54 3b 0a 20 20 20 20 69 66 28 20 6e _BKPT;. if( n
1b270 46 69 6e 3c 6e 4f 72 69 67 20 29 7b 0a 20 20 20 Fin<nOrig ){.
1b280 20 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 rc = saveAllC
1b290 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 ursors(pBt, 0, 0
1b2a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
1b2b0 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 (iFree=nOrig; iF
1b2c0 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d ree>nFin && rc==
1b2d0 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 SQLITE_OK; iFree
1b2e0 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 --){. rc =
1b2f0 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 incrVacuumStep(p
1b300 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 2c Bt, nFin, iFree,
1b310 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 1);. }. i
1b320 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 f( (rc==SQLITE_D
1b330 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 ONE || rc==SQLIT
1b340 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e 30 E_OK) && nFree>0
1b350 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1b360 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1b370 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
1b380 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 75 bPage);. pu
1b390 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
1b3a0 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 ge1->aData[32],
1b3b0 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 0);. put4by
1b3c0 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
1b3d0 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b 0a >aData[36], 0);.
1b3e0 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
1b3f0 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
1b400 74 61 5b 32 38 5d 2c 20 6e 46 69 6e 29 3b 0a 20 ta[28], nFin);.
1b410 20 20 20 20 20 70 42 74 2d 3e 62 44 6f 54 72 75 pBt->bDoTru
1b420 6e 63 61 74 65 20 3d 20 31 3b 0a 20 20 20 20 20 ncate = 1;.
1b430 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e 46 pBt->nPage = nF
1b440 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 in;. }. if
1b450 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1b460 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b470 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 PagerRollback(pP
1b480 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
1b490 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 66 .. assert( nRef
1b4a0 3e 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 >=sqlite3PagerRe
1b4b0 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 fcount(pPager) )
1b4c0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1b4d0 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 6e 64 65 ..#else /* ifnde
1b4e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1b4f0 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 20 64 65 TOVACUUM */.# de
1b500 66 69 6e 65 20 73 65 74 43 68 69 6c 64 50 74 72 fine setChildPtr
1b510 6d 61 70 73 28 78 29 20 53 51 4c 49 54 45 5f 4f maps(x) SQLITE_O
1b520 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 K.#endif../*.**
1b530 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
1b540 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61 73 s the first phas
1b550 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 73 65 e of a two-phase
1b560 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 20 72 commit. This r
1b570 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73 65 73 outine.** causes
1b580 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
1b590 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61 74 65 nal to be create
1b5a0 64 20 28 69 66 20 69 74 20 64 6f 65 73 20 6e 6f d (if it does no
1b5b0 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 29 t already exist)
1b5c0 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 .** and populate
1b5d0 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20 69 6e d with enough in
1b5e0 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74 68 61 formation so tha
1b5f0 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c 6f 73 t if a power los
1b600 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65 20 s occurs.** the
1b610 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 20 database can be
1b620 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 restored to its
1b630 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 62 original state b
1b640 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 0a 2a y playing back.*
1b650 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 * the journal.
1b660 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 Then the content
1b670 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s of the journal
1b680 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f 75 74 are flushed out
1b690 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73 6b 2e to.** the disk.
1b6a0 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 After the jour
1b6b0 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20 6f 6e nal is safely on
1b6c0 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68 61 6e oxide, the chan
1b6d0 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 ges to the.** da
1b6e0 74 61 62 61 73 65 20 61 72 65 20 77 72 69 74 74 tabase are writt
1b6f0 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 en into the data
1b700 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 66 6c base file and fl
1b710 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65 2e 0a ushed to oxide..
1b720 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f 66 ** At the end of
1b730 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68 65 20 this call, the
1b740 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
1b750 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 6f 6e still exists on
1b760 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 the.** disk and
1b770 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20 68 6f we are still ho
1b780 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c lding all locks,
1b790 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 so the transact
1b7a0 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 ion has not.** c
1b7b0 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65 20 73 ommitted. See s
1b7c0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1b7d0 74 50 68 61 73 65 54 77 6f 28 29 20 66 6f 72 20 tPhaseTwo() for
1b7e0 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 65 the second phase
1b7f0 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 of the.** commi
1b800 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a t process..**.**
1b810 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 This call is a
1b820 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 72 69 74 no-op if no writ
1b830 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e-transaction is
1b840 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 currently activ
1b850 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 e on pBt..**.**
1b860 4f 74 68 65 72 77 69 73 65 2c 20 73 79 6e 63 20 Otherwise, sync
1b870 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1b880 65 20 66 6f 72 20 74 68 65 20 62 74 72 65 65 20 e for the btree
1b890 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 pBt. zMaster poi
1b8a0 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 nts to.** the na
1b8b0 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a me of a master j
1b8c0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 ournal file that
1b8d0 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 should be writt
1b8e0 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 en into the.** i
1b8f0 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 ndividual journa
1b900 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 20 4e 55 l file, or is NU
1b910 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 6e LL, indicating n
1b920 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c o master journal
1b930 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 6e 67 6c file .** (singl
1b940 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 e database trans
1b950 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 action)..**.** W
1b960 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c hen this is call
1b970 65 64 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a ed, the master j
1b980 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 20 61 6c ournal should al
1b990 72 65 61 64 79 20 68 61 76 65 20 62 65 65 6e 0a ready have been.
1b9a0 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 6f 70 75 ** created, popu
1b9b0 6c 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 lated with this
1b9c0 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 journal pointer
1b9d0 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 64 69 and synced to di
1b9e0 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 sk..**.** Once t
1b9f0 68 69 73 20 69 73 20 72 6f 75 74 69 6e 65 20 68 his is routine h
1ba00 61 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 as returned, the
1ba10 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 65 71 75 only thing requ
1ba20 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a ired to commit.*
1ba30 2a 20 74 68 65 20 77 72 69 74 65 2d 74 72 61 6e * the write-tran
1ba40 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 68 69 73 saction for this
1ba50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1ba60 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 s to delete the
1ba70 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 69 6e 74 20 journal..*/.int
1ba80 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1ba90 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65 itPhaseOne(Btree
1baa0 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
1bab0 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 *zMaster){. int
1bac0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1bad0 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
1bae0 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
1baf0 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
1bb00 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1bb10 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
1bb20 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 ter(p);.#ifndef
1bb30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1bb40 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
1bb50 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
1bb60 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 75 74 {. rc = aut
1bb70 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 70 42 oVacuumCommit(pB
1bb80 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 t);. if( rc
1bb90 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1bba0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
1bbb0 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
1bbc0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1bbd0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1bbe0 20 20 69 66 28 20 70 42 74 2d 3e 62 44 6f 54 72 if( pBt->bDoTr
1bbf0 75 6e 63 61 74 65 20 29 7b 0a 20 20 20 20 20 20 uncate ){.
1bc00 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
1bc10 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 cateImage(pBt->p
1bc20 50 61 67 65 72 2c 20 70 42 74 2d 3e 6e 50 61 67 Pager, pBt->nPag
1bc30 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
1bc40 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1bc50 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
1bc60 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 72 eOne(pBt->pPager
1bc70 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 , zMaster, 0);.
1bc80 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1bc90 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 eave(p);. }. r
1bca0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1bcb0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1bcc0 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 is called from
1bcd0 62 6f 74 68 20 42 74 72 65 65 43 6f 6d 6d 69 74 both BtreeCommit
1bce0 50 68 61 73 65 54 77 6f 28 29 20 61 6e 64 20 42 PhaseTwo() and B
1bcf0 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a treeRollback().*
1bd00 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 * at the conclus
1bd10 69 6f 6e 20 6f 66 20 61 20 74 72 61 6e 73 61 63 ion of a transac
1bd20 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
1bd30 76 6f 69 64 20 62 74 72 65 65 45 6e 64 54 72 61 void btreeEndTra
1bd40 6e 73 61 63 74 69 6f 6e 28 42 74 72 65 65 20 2a nsaction(Btree *
1bd50 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a p){. BtShared *
1bd60 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1bd70 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1bd80 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 >db;. assert( s
1bd90 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1bda0 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 23 69 66 Mutex(p) );..#if
1bdb0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1bdc0 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 70 42 _AUTOVACUUM. pB
1bdd0 74 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 20 3d t->bDoTruncate =
1bde0 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 0;.#endif. if(
1bdf0 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e p->inTrans>TRAN
1be00 53 5f 4e 4f 4e 45 20 26 26 20 64 62 2d 3e 6e 56 S_NONE && db->nV
1be10 64 62 65 52 65 61 64 3e 31 20 29 7b 0a 20 20 20 dbeRead>1 ){.
1be20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
1be30 20 6f 74 68 65 72 20 61 63 74 69 76 65 20 73 74 other active st
1be40 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 62 65 atements that be
1be50 6c 6f 6e 67 20 74 6f 20 74 68 69 73 20 64 61 74 long to this dat
1be60 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 68 61 6e abase. ** han
1be70 64 6c 65 2c 20 64 6f 77 6e 67 72 61 64 65 20 74 dle, downgrade t
1be80 6f 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 o a read-only tr
1be90 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 6f ansaction. The o
1bea0 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 0a ther statements.
1beb0 20 20 20 20 2a 2a 20 6d 61 79 20 73 74 69 6c 6c ** may still
1bec0 20 62 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d be reading from
1bed0 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
1bee0 2a 2f 0a 20 20 20 20 64 6f 77 6e 67 72 61 64 65 */. downgrade
1bef0 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
1bf00 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 bleLocks(p);.
1bf10 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 p->inTrans = TR
1bf20 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 ANS_READ;. }els
1bf30 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 e{. /* If the
1bf40 20 68 61 6e 64 6c 65 20 68 61 64 20 61 6e 79 20 handle had any
1bf50 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 61 63 74 kind of transact
1bf60 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 72 65 6d ion open, decrem
1bf70 65 6e 74 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 ent the . **
1bf80 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e transaction coun
1bf90 74 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 t of the shared
1bfa0 62 74 72 65 65 2e 20 49 66 20 74 68 65 20 74 72 btree. If the tr
1bfb0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 ansaction count
1bfc0 0a 20 20 20 20 2a 2a 20 72 65 61 63 68 65 73 20 . ** reaches
1bfd0 30 2c 20 73 65 74 20 74 68 65 20 73 68 61 72 65 0, set the share
1bfe0 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 d state to TRANS
1bff0 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f 63 _NONE. The unloc
1c000 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 29 kBtreeIfUnused()
1c010 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 65 6c . ** call bel
1c020 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 ow will unlock t
1c030 68 65 20 70 61 67 65 72 2e 20 20 2a 2f 0a 20 20 he pager. */.
1c040 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
1c050 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a !=TRANS_NONE ){.
1c060 20 20 20 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 clearAllSh
1c070 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
1c080 63 6b 73 28 70 29 3b 0a 20 20 20 20 20 20 70 42 cks(p);. pB
1c090 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d t->nTransaction-
1c0a0 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d -;. if( 0==
1c0b0 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
1c0c0 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 n ){. pBt
1c0d0 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
1c0e0 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 = TRANS_NONE;.
1c0f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1c100 20 2f 2a 20 53 65 74 20 74 68 65 20 63 75 72 72 /* Set the curr
1c110 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1c120 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e state to TRANS_N
1c130 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 ONE and unlock t
1c140 68 65 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 he . ** pager
1c150 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c if this call cl
1c160 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 osed the only re
1c170 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e ad or write tran
1c180 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 saction. */.
1c190 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 p->inTrans = TR
1c1a0 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 75 6e ANS_NONE;. un
1c1b0 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 lockBtreeIfUnuse
1c1c0 64 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 d(pBt);. }.. b
1c1d0 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
1c1e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 ;.}../*.** Commi
1c1f0 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
1c200 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 n currently in p
1c210 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 rogress..**.** T
1c220 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c his routine impl
1c230 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f 6e ements the secon
1c240 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d 70 d phase of a 2-p
1c250 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 hase commit. Th
1c260 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 e.** sqlite3Btre
1c270 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
1c280 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 ) routine does t
1c290 68 65 20 66 69 72 73 74 20 70 68 61 73 65 20 61 he first phase a
1c2a0 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 nd should.** be
1c2b0 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f invoked prior to
1c2c0 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f calling this ro
1c2d0 75 74 69 6e 65 2e 20 20 54 68 65 20 73 71 6c 69 utine. The sqli
1c2e0 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1c2f0 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 aseOne().** rout
1c300 69 6e 65 20 64 69 64 20 61 6c 6c 20 74 68 65 20 ine did all the
1c310 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 work of writing
1c320 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 information out
1c330 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 to disk and flus
1c340 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 hing the.** cont
1c350 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 ents so that the
1c360 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e y are written on
1c370 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 to the disk plat
1c380 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a ter. All this.*
1c390 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f * routine has to
1c3a0 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 do is delete or
1c3b0 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 truncate or zer
1c3c0 6f 20 74 68 65 20 68 65 61 64 65 72 20 69 6e 20 o the header in
1c3d0 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 the.** the rollb
1c3e0 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 ack journal (whi
1c3f0 63 68 20 63 61 75 73 65 73 20 74 68 65 20 74 72 ch causes the tr
1c400 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d ansaction to com
1c410 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 mit) and.** drop
1c420 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f locks..**.** No
1c430 72 6d 61 6c 6c 79 2c 20 69 66 20 61 6e 20 65 72 rmally, if an er
1c440 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
1c450 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
1c460 20 69 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 is attempting t
1c470 6f 20 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 20 74 o .** finalize t
1c480 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6a 6f he underlying jo
1c490 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 69 73 urnal file, this
1c4a0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1c4b0 73 20 61 6e 20 65 72 72 6f 72 20 61 6e 64 0a 2a s an error and.*
1c4c0 2a 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 * the upper laye
1c4d0 72 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 61 r will attempt a
1c4e0 20 72 6f 6c 6c 62 61 63 6b 2e 20 48 6f 77 65 76 rollback. Howev
1c4f0 65 72 2c 20 69 66 20 74 68 65 20 73 65 63 6f 6e er, if the secon
1c500 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 d argument.** is
1c510 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 non-zero then t
1c520 68 69 73 20 62 2d 74 72 65 65 20 74 72 61 6e 73 his b-tree trans
1c530 61 63 74 69 6f 6e 20 69 73 20 70 61 72 74 20 6f action is part o
1c540 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 0a f a multi-file .
1c550 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
1c560 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 In this case, th
1c570 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 e transaction ha
1c580 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 s already been c
1c590 6f 6d 6d 69 74 74 65 64 20 0a 2a 2a 20 28 62 79 ommitted .** (by
1c5a0 20 64 65 6c 65 74 69 6e 67 20 61 20 6d 61 73 74 deleting a mast
1c5b0 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 er journal file)
1c5c0 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 65 72 20 and the caller
1c5d0 77 69 6c 6c 20 69 67 6e 6f 72 65 20 74 68 69 73 will ignore this
1c5e0 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 72 .** functions r
1c5f0 65 74 75 72 6e 20 63 6f 64 65 2e 20 53 6f 2c 20 eturn code. So,
1c600 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 even if an error
1c610 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20 70 occurs in the p
1c620 61 67 65 72 20 6c 61 79 65 72 2c 0a 2a 2a 20 72 ager layer,.** r
1c630 65 73 65 74 20 74 68 65 20 62 2d 74 72 65 65 20 eset the b-tree
1c640 6f 62 6a 65 63 74 73 20 69 6e 74 65 72 6e 61 6c objects internal
1c650 20 73 74 61 74 65 20 74 6f 20 69 6e 64 69 63 61 state to indica
1c660 74 65 20 74 68 61 74 20 74 68 65 20 77 72 69 74 te that the writ
1c670 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1c680 20 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 has been closed
1c690 2e 20 54 68 69 73 20 69 73 20 71 75 69 74 65 20 . This is quite
1c6a0 73 61 66 65 2c 20 61 73 20 74 68 65 20 70 61 67 safe, as the pag
1c6b0 65 72 20 77 69 6c 6c 20 68 61 76 65 0a 2a 2a 20 er will have.**
1c6c0 74 72 61 6e 73 69 74 69 6f 6e 65 64 20 74 6f 20 transitioned to
1c6d0 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e the error state.
1c6e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c .**.** This will
1c6f0 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 release the wri
1c700 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 te lock on the d
1c710 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 atabase file. I
1c720 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e f there.** are n
1c730 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 o active cursors
1c740 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 , it also releas
1c750 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b es the read lock
1c760 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1c770 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1c780 54 77 6f 28 42 74 72 65 65 20 2a 70 2c 20 69 6e Two(Btree *p, in
1c790 74 20 62 43 6c 65 61 6e 75 70 29 7b 0a 0a 20 20 t bCleanup){..
1c7a0 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d if( p->inTrans==
1c7b0 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 20 72 65 74 TRANS_NONE ) ret
1c7c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1c7d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1c7e0 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e er(p);. btreeIn
1c7f0 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f tegrity(p);.. /
1c800 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 * If the handle
1c810 68 61 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e has a write-tran
1c820 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f saction open, co
1c830 6d 6d 69 74 20 74 68 65 20 73 68 61 72 65 64 2d mmit the shared-
1c840 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 btrees . ** tra
1c850 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 nsaction and set
1c860 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 the shared stat
1c870 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e e to TRANS_READ.
1c880 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 . */. if( p->i
1c890 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1c8a0 49 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ITE ){. int r
1c8b0 63 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 c;. BtShared
1c8c0 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
1c8d0 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
1c8e0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
1c8f0 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
1c900 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e assert( pBt->n
1c910 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b Transaction>0 );
1c920 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1c930 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
1c940 65 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 eTwo(pBt->pPager
1c950 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1c960 51 4c 49 54 45 5f 4f 4b 20 26 26 20 62 43 6c 65 QLITE_OK && bCle
1c970 61 6e 75 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 anup==0 ){.
1c980 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1c990 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 ve(p);. ret
1c9a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1c9b0 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
1c9c0 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 tion = TRANS_REA
1c9d0 44 3b 0a 20 20 20 20 62 74 72 65 65 43 6c 65 61 D;. btreeClea
1c9e0 72 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 rHasContent(pBt)
1c9f0 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e ;. }.. btreeEn
1ca00 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b dTransaction(p);
1ca10 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
1ca20 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
1ca30 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1ca40 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 /*.** Do both ph
1ca50 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 ases of a commit
1ca60 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1ca70 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 BtreeCommit(Btre
1ca80 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
1ca90 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1caa0 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
1cab0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1cac0 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 itPhaseOne(p, 0)
1cad0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1cae0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
1caf0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
1cb00 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2c 20 mmitPhaseTwo(p,
1cb10 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0);. }. sqlite
1cb20 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1cb30 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1cb40 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
1cb50 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 61 74 ne sets the stat
1cb60 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c e to CURSOR_FAUL
1cb70 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a T and the error.
1cb80 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f ** code to errCo
1cb90 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 de for every cur
1cba0 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 64 20 sor on BtShared
1cbb0 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 that pBtree.** r
1cbc0 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a eferences..**.**
1cbd0 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 69 73 Every cursor is
1cbe0 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 tripped, includ
1cbf0 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 61 74 ing cursors that
1cc00 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 belong.** to ot
1cc10 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e her database con
1cc20 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 nections that ha
1cc30 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 72 69 ppen to be shari
1cc40 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 ng.** the cache
1cc50 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a with pBtree..**.
1cc60 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1cc70 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e gets called when
1cc80 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 a rollback occu
1cc90 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f rs..** All curso
1cca0 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d rs using the sam
1ccb0 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 e cache must be
1ccc0 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 tripped.** to pr
1ccd0 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 event them from
1cce0 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 74 68 trying to use th
1ccf0 65 20 62 74 72 65 65 20 61 66 74 65 72 0a 2a 2a e btree after.**
1cd00 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 the rollback.
1cd10 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 The rollback may
1cd20 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 74 61 have deleted ta
1cd30 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 bles.** or moved
1cd40 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 root pages, so
1cd50 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 it is not suffic
1cd60 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 ient to.** save
1cd70 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
1cd80 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 cursor. The cu
1cd90 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 rsor must be.**
1cda0 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a invalidated..*/.
1cdb0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
1cdc0 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 eTripAllCursors(
1cdd0 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 Btree *pBtree, i
1cde0 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 nt errCode){. B
1cdf0 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 69 66 tCursor *p;. if
1ce00 28 20 70 42 74 72 65 65 3d 3d 30 20 29 20 72 65 ( pBtree==0 ) re
1ce10 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 turn;. sqlite3B
1ce20 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 treeEnter(pBtree
1ce30 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 );. for(p=pBtre
1ce40 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b e->pBt->pCursor;
1ce50 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
1ce60 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1ce70 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
1ce80 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 20 20 rCursor(p);.
1ce90 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 p->eState = CURS
1cea0 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d OR_FAULT;. p-
1ceb0 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 72 72 43 >skipNext = errC
1cec0 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ode;. for(i=0
1ced0 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 ; i<=p->iPage; i
1cee0 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
1cef0 73 65 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65 sePage(p->apPage
1cf00 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 [i]);. p->a
1cf10 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 pPage[i] = 0;.
1cf20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
1cf30 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 3BtreeLeave(pBtr
1cf40 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f ee);.}../*.** Ro
1cf50 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 llback the trans
1cf60 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 action in progre
1cf70 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 ss. All cursors
1cf80 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 will be.** inva
1cf90 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 lided by this op
1cfa0 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 eration. Any at
1cfb0 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63 tempt to use a c
1cfc0 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 ursor.** that wa
1cfd0 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65 s open at the be
1cfe0 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 ginning of this
1cff0 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 operation will r
1d000 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 esult.** in an e
1d010 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rror..**.** This
1d020 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 will release th
1d030 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 e write lock on
1d040 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1d050 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 e. If there.**
1d060 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 are no active cu
1d070 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 rsors, it also r
1d080 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64 eleases the read
1d090 20 6c 6f 63 6b 2e 0a 2a 2f 0a 69 6e 74 20 73 71 lock..*/.int sq
1d0a0 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
1d0b0 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ck(Btree *p, int
1d0c0 20 74 72 69 70 43 6f 64 65 29 7b 0a 20 20 69 6e tripCode){. in
1d0d0 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
1d0e0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1d0f0 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
1d100 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 1;.. sqlite3Btr
1d110 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
1d120 28 20 74 72 69 70 43 6f 64 65 3d 3d 53 51 4c 49 ( tripCode==SQLI
1d130 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
1d140 3d 20 74 72 69 70 43 6f 64 65 20 3d 20 73 61 76 = tripCode = sav
1d150 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
1d160 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0, 0);. }else{
1d170 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1d180 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 _OK;. }. if( t
1d190 72 69 70 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 ripCode ){. s
1d1a0 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
1d1b0 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 74 72 69 llCursors(p, tri
1d1c0 70 43 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 62 74 pCode);. }. bt
1d1d0 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b reeIntegrity(p);
1d1e0 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 .. if( p->inTra
1d1f0 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
1d200 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a ){. int rc2;.
1d210 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41 . assert( TRA
1d220 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 NS_WRITE==pBt->i
1d230 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a nTransaction );.
1d240 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 rc2 = sqlite
1d250 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
1d260 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
1d270 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 if( rc2!=SQLITE
1d280 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
1d290 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 = rc2;. }..
1d2a0 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 /* The rollbac
1d2b0 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 k may have destr
1d2c0 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 2d oyed the pPage1-
1d2d0 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 >aData value. S
1d2e0 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 o. ** call bt
1d2f0 72 65 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 reeGetPage() on
1d300 70 61 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 page 1 again to
1d310 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 make. ** sure
1d320 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 pPage1->aData i
1d330 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e s set correctly.
1d340 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 */. if( btre
1d350 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c eGetPage(pBt, 1,
1d360 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 &pPage1, 0)==SQ
1d370 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d380 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 67 65 74 int nPage = get
1d390 34 62 79 74 65 28 32 38 2b 28 75 38 2a 29 70 50 4byte(28+(u8*)pP
1d3a0 61 67 65 31 2d 3e 61 44 61 74 61 29 3b 0a 20 20 age1->aData);.
1d3b0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 testcase( nP
1d3c0 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 age==0 );.
1d3d0 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 20 73 if( nPage==0 ) s
1d3e0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
1d3f0 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
1d400 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 , &nPage);.
1d410 20 74 65 73 74 63 61 73 65 28 20 70 42 74 2d 3e testcase( pBt->
1d420 6e 50 61 67 65 21 3d 6e 50 61 67 65 20 29 3b 0a nPage!=nPage );.
1d430 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 pBt->nPage
1d440 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 = nPage;.
1d450 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
1d460 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 e1);. }. a
1d470 73 73 65 72 74 28 20 63 6f 75 6e 74 56 61 6c 69 ssert( countVali
1d480 64 43 75 72 73 6f 72 73 28 70 42 74 2c 20 31 29 dCursors(pBt, 1)
1d490 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e ==0 );. pBt->
1d4a0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
1d4b0 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20 TRANS_READ;.
1d4c0 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e btreeClearHasCon
1d4d0 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 7d 0a 0a tent(pBt);. }..
1d4e0 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 btreeEndTransa
1d4f0 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 ction(p);. sqli
1d500 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
1d510 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1d520 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 ../*.** Start a
1d530 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 statement subtra
1d540 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 nsaction. The su
1d550 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e btransaction can
1d560 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 be rolled.** ba
1d570 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 ck independently
1d580 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 of the main tra
1d590 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 nsaction. You mu
1d5a0 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 st start a trans
1d5b0 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 action .** befor
1d5c0 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 62 e starting a sub
1d5d0 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 transaction. The
1d5e0 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 subtransaction
1d5f0 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 is ended automat
1d600 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 ically .** if th
1d610 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 e main transacti
1d620 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f on commits or ro
1d630 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 lls back..**.**
1d640 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 Statement subtra
1d650 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 nsactions are us
1d660 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 ed around indivi
1d670 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 dual SQL stateme
1d680 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 nts.** that are
1d690 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
1d6a0 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 a BEGIN...COMMI
1d6b0 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 T block. If a c
1d6c0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
1d6d0 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e or occurs within
1d6e0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
1d6f0 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 the effect of th
1d700 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 at one statement
1d710 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 .** can be rolle
1d720 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 d back without h
1d730 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 aving to rollbac
1d740 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 k the entire tra
1d750 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
1d760 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 2d A statement sub-
1d770 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 transaction is i
1d780 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 6e mplemented as an
1d790 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 anonymous savep
1d7a0 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c oint. The.** val
1d7b0 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ue passed as the
1d7c0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
1d7d0 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e r is the total n
1d7e0 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 umber of savepoi
1d7f0 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e nts,.** includin
1d800 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d g the new anonym
1d810 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f ous savepoint, o
1d820 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 pen on the B-Tre
1d830 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 65 e. i.e. if there
1d840 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 .** are no activ
1d850 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 e savepoints and
1d860 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d no other statem
1d870 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 ent-transactions
1d880 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 open,.** iState
1d890 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 20 ment is 1. This
1d8a0 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f anonymous savepo
1d8b0 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 61 int can be relea
1d8c0 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 sed or rolled ba
1d8d0 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 ck.** using the
1d8e0 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 sqlite3BtreeSave
1d8f0 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e point() function
1d900 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1d910 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 BtreeBeginStmt(B
1d920 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 tree *p, int iSt
1d930 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 atement){. int
1d940 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a rc;. BtShared *
1d950 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1d960 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1d970 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
1d980 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
1d990 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 S_WRITE );. ass
1d9a0 65 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c ert( (pBt->btsFl
1d9b0 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 5f 4f ags & BTS_READ_O
1d9c0 4e 4c 59 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 NLY)==0 );. ass
1d9d0 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e ert( iStatement>
1d9e0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
1d9f0 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d Statement>p->db-
1da00 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 >nSavepoint );.
1da10 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
1da20 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
1da30 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 2f 2a NS_WRITE );. /*
1da40 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 At the pager le
1da50 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 vel, a statement
1da60 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1da70 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 a savepoint with
1da80 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 . ** an index g
1da90 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 reater than all
1daa0 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 savepoints creat
1dab0 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 ed explicitly us
1dac0 69 6e 67 0a 20 20 2a 2a 20 53 51 4c 20 73 74 61 ing. ** SQL sta
1dad0 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 69 tements. It is i
1dae0 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c 20 llegal to open,
1daf0 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 release or rollb
1db00 61 63 6b 20 61 6e 79 0a 20 20 2a 2a 20 73 75 63 ack any. ** suc
1db10 68 20 73 61 76 65 70 6f 69 6e 74 73 20 77 68 69 h savepoints whi
1db20 6c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 le the statement
1db30 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 transaction sav
1db40 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 epoint is active
1db50 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 .. */. rc = sq
1db60 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
1db70 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 vepoint(pBt->pPa
1db80 67 65 72 2c 20 69 53 74 61 74 65 6d 65 6e 74 29 ger, iStatement)
1db90 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1dba0 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
1dbb0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1dbc0 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
1dbd0 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
1dbe0 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 tion, op, is alw
1dbf0 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f ays SAVEPOINT_RO
1dc00 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 LLBACK.** or SAV
1dc10 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 EPOINT_RELEASE.
1dc20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 This function ei
1dc30 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 ther releases or
1dc40 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a rolls back the.
1dc50 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 ** savepoint ide
1dc60 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ntified by param
1dc70 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c eter iSavepoint,
1dc80 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
1dc90 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f e value .** of o
1dca0 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c p..**.** Normall
1dcb0 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 y, iSavepoint is
1dcc0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
1dcd0 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 equal to zero.
1dce0 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 However, if op i
1dcf0 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 s.** SAVEPOINT_R
1dd00 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 OLLBACK, then iS
1dd10 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 avepoint may als
1dd20 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 o be -1. In this
1dd30 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f case the .** co
1dd40 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e ntents of the en
1dd50 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tire transaction
1dd60 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b are rolled back
1dd70 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72 . This is differ
1dd80 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f ent.** from a no
1dd90 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e rmal transaction
1dda0 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f rollback, as no
1ddb0 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 locks are relea
1ddc0 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 sed and the.** t
1ddd0 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 ransaction remai
1dde0 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 69 6e 74 20 ns open..*/.int
1ddf0 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 sqlite3BtreeSave
1de00 70 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 point(Btree *p,
1de10 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 int op, int iSav
1de20 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 epoint){. int r
1de30 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1de40 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 if( p && p->inT
1de50 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
1de60 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 E ){. BtShare
1de70 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1de80 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d . assert( op=
1de90 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
1dea0 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f SE || op==SAVEPO
1deb0 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a INT_ROLLBACK );.
1dec0 20 20 20 20 61 73 73 65 72 74 28 20 69 53 61 76 assert( iSav
1ded0 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 epoint>=0 || (iS
1dee0 61 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 avepoint==-1 &&
1def0 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f op==SAVEPOINT_RO
1df00 4c 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 LLBACK) );. s
1df10 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1df20 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 (p);. rc = sq
1df30 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f lite3PagerSavepo
1df40 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c int(pBt->pPager,
1df50 20 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 op, iSavepoint)
1df60 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1df70 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1df80 20 69 66 28 20 69 53 61 76 65 70 6f 69 6e 74 3c if( iSavepoint<
1df90 30 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46 6c 0 && (pBt->btsFl
1dfa0 61 67 73 20 26 20 42 54 53 5f 49 4e 49 54 49 41 ags & BTS_INITIA
1dfb0 4c 4c 59 5f 45 4d 50 54 59 29 21 3d 30 20 29 7b LLY_EMPTY)!=0 ){
1dfc0 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 . pBt->nP
1dfd0 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d age = 0;. }
1dfe0 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 . rc = newD
1dff0 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 atabase(pBt);.
1e000 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d pBt->nPage =
1e010 20 67 65 74 34 62 79 74 65 28 32 38 20 2b 20 70 get4byte(28 + p
1e020 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
1e030 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 a);.. /* Th
1e040 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
1e050 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f was written into
1e060 20 74 68 65 20 6f 66 66 73 65 74 20 32 38 20 6f the offset 28 o
1e070 66 20 74 68 65 20 68 65 61 64 65 72 0a 20 20 20 f the header.
1e080 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 74 ** when the t
1e090 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 72 74 ransaction start
1e0a0 65 64 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 ed, so we know t
1e0b0 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 61 74 hat the value at
1e0c0 20 6f 66 66 73 65 74 0a 20 20 20 20 20 20 2a 2a offset. **
1e0d0 20 32 38 20 69 73 20 6e 6f 6e 7a 65 72 6f 2e 20 28 is nonzero.
1e0e0 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1e0f0 20 70 42 74 2d 3e 6e 50 61 67 65 3e 30 20 29 3b pBt->nPage>0 );
1e100 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1e110 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1e120 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1e130 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ;.}../*.** Creat
1e140 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 66 e a new cursor f
1e150 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 68 6f or the BTree who
1e160 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 68 se root is on th
1e170 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c 65 e page.** iTable
1e180 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c 79 . If a read-only
1e190 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 cursor is reque
1e1a0 73 74 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 sted, it is assu
1e1b0 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 med that.** the
1e1c0 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79 20 68 caller already h
1e1d0 61 73 20 61 74 20 6c 65 61 73 74 20 61 20 72 65 as at least a re
1e1e0 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 ad-only transact
1e1f0 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 74 ion open.** on t
1e200 68 65 20 64 61 74 61 62 61 73 65 20 61 6c 72 65 he database alre
1e210 61 64 79 2e 20 49 66 20 61 20 77 72 69 74 65 2d ady. If a write-
1e220 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 cursor is reques
1e230 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 ted, then.** the
1e240 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73 75 6d caller is assum
1e250 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20 6f 70 ed to have an op
1e260 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
1e270 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 tion..**.** If w
1e280 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 rFlag==0, then t
1e290 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e he cursor can on
1e2a0 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 ly be used for r
1e2b0 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 eading..** If wr
1e2c0 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 Flag==1, then th
1e2d0 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 e cursor can be
1e2e0 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 used for reading
1e2f0 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 or for.** writi
1e300 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 ng if other cond
1e310 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 itions for writi
1e320 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e ng are also met.
1e330 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 These.** are t
1e340 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 he conditions th
1e350 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 at must be met i
1e360 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 n order for writ
1e370 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c ing to.** be all
1e380 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 owed:.**.** 1:
1e390 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 The cursor must
1e3a0 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 have been opened
1e3b0 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a with wrFlag==1.
1e3c0 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 **.** 2: Other
1e3d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1e3e0 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 ions that share
1e3f0 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 the same pager c
1e400 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 ache.** but
1e410 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e which are not in
1e420 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d the READ_UNCOMM
1e430 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 ITTED state may
1e440 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 not have.**
1e450 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 cursors open wit
1e460 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 h wrFlag==0 on t
1e470 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 he same table.
1e480 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 Otherwise.**
1e490 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 the changes mad
1e4a0 65 20 62 79 20 74 68 69 73 20 77 72 69 74 65 20 e by this write
1e4b0 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 cursor would be
1e4c0 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 visible to.**
1e4d0 20 20 74 68 65 20 72 65 61 64 20 63 75 72 73 6f the read curso
1e4e0 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 rs in the other
1e4f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1e500 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 ion..**.** 3: T
1e510 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 he database must
1e520 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f be writable (no
1e530 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d t on read-only m
1e540 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 edia).**.** 4:
1e550 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e There must be an
1e560 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 active transact
1e570 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 ion..**.** No ch
1e580 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 ecking is done t
1e590 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
1e5a0 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 page iTable rea
1e5b0 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f lly is the.** ro
1e5c0 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 ot page of a b-t
1e5d0 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e ree. If it is n
1e5e0 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 ot, then the cur
1e5f0 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 sor acquired.**
1e600 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f will not work co
1e610 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 rrectly..**.** I
1e620 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
1e630 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 t the sqlite3Btr
1e640 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 20 68 eeCursorZero() h
1e650 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 0a 2a as been called.*
1e660 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 6e 69 * on pCur to ini
1e670 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
1e680 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 20 74 ry space prior t
1e690 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 o invoking this
1e6a0 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 routine..*/.stat
1e6b0 69 63 20 69 6e 74 20 62 74 72 65 65 43 75 72 73 ic int btreeCurs
1e6c0 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 or(. Btree *p,
1e6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e6f0 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 The btree */. i
1e700 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 nt iTable,
1e710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e720 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 /* Root pa
1e730 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f ge of table to o
1e740 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 pen */. int wrF
1e750 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 lag,
1e760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e770 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 /* 1 to write. 0
1e780 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 read-only */.
1e790 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a struct KeyInfo *
1e7a0 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 pKeyInfo,
1e7b0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1e7c0 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f arg to compariso
1e7d0 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 n function */.
1e7e0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 BtCursor *pCur
1e7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e800 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
1e810 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 20 2a for new cursor *
1e820 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 /.){. BtShared
1e830 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 20 *pBt = p->pBt;
1e840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e850 20 53 68 61 72 65 64 20 62 2d 74 72 65 65 20 68 Shared b-tree h
1e860 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 andle */.. asse
1e870 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1e880 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
1e890 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 . assert( wrFla
1e8a0 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 3d 3d g==0 || wrFlag==
1e8b0 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 1 );.. /* The f
1e8c0 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 ollowing assert
1e8d0 73 74 61 74 65 6d 65 6e 74 73 20 76 65 72 69 66 statements verif
1e8e0 79 20 74 68 61 74 20 69 66 20 74 68 69 73 20 69 y that if this i
1e8f0 73 20 61 20 73 68 61 72 61 62 6c 65 20 0a 20 20 s a sharable .
1e900 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61 62 61 ** b-tree databa
1e910 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 se, the connecti
1e920 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 on is holding th
1e930 65 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65 e required table
1e940 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 61 6e locks, . ** an
1e950 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 d that no other
1e960 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 connection has a
1e970 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 ny open cursor t
1e980 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 hat conflicts wi
1e990 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 6c 6f th . ** this lo
1e9a0 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ck. */. assert
1e9b0 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 ( hasSharedCache
1e9c0 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 TableLock(p, iTa
1e9d0 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 3d 30 ble, pKeyInfo!=0
1e9e0 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 3b 0a 20 , wrFlag+1) );.
1e9f0 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d assert( wrFlag=
1ea00 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 64 43 6f =0 || !hasReadCo
1ea10 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 61 62 6c nflicts(p, iTabl
1ea20 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 e) );.. /* Asse
1ea30 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c rt that the call
1ea40 65 72 20 68 61 73 20 6f 70 65 6e 65 64 20 74 68 er has opened th
1ea50 65 20 72 65 71 75 69 72 65 64 20 74 72 61 6e 73 e required trans
1ea60 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 73 73 action. */. ass
1ea70 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e ert( p->inTrans>
1ea80 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 TRANS_NONE );.
1ea90 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d assert( wrFlag==
1eaa0 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 0 || p->inTrans=
1eab0 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
1eac0 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 assert( pBt->p
1ead0 50 61 67 65 31 20 26 26 20 70 42 74 2d 3e 70 50 Page1 && pBt->pP
1eae0 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 0a age1->aData );..
1eaf0 20 20 69 66 28 20 4e 45 56 45 52 28 77 72 46 6c if( NEVER(wrFl
1eb00 61 67 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46 ag && (pBt->btsF
1eb10 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 5f lags & BTS_READ_
1eb20 4f 4e 4c 59 29 21 3d 30 29 20 29 7b 0a 20 20 20 ONLY)!=0) ){.
1eb30 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 return SQLITE_R
1eb40 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 EADONLY;. }. i
1eb50 66 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 f( wrFlag ){.
1eb60 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 allocateTempSpa
1eb70 63 65 28 70 42 74 29 3b 0a 20 20 20 20 69 66 28 ce(pBt);. if(
1eb80 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3d pBt->pTmpSpace=
1eb90 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
1eba0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
1ebb0 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 if( iTable==1 &&
1ebc0 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 btreePagecount(
1ebd0 70 42 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 pBt)==0 ){. a
1ebe0 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 ssert( wrFlag==0
1ebf0 20 29 3b 0a 20 20 20 20 69 54 61 62 6c 65 20 3d );. iTable =
1ec00 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 0;. }.. /* No
1ec10 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 w that no other
1ec20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 errors can occur
1ec30 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 , finish filling
1ec40 20 69 6e 20 74 68 65 20 42 74 43 75 72 73 6f 72 in the BtCursor
1ec50 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 . ** variables
1ec60 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 and link the cur
1ec70 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 sor into the BtS
1ec80 68 61 72 65 64 20 6c 69 73 74 2e 20 20 2a 2f 0a hared list. */.
1ec90 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 pCur->pgnoRoot
1eca0 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b = (Pgno)iTable;
1ecb0 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d . pCur->iPage =
1ecc0 20 2d 31 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 -1;. pCur->pKe
1ecd0 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f yInfo = pKeyInfo
1ece0 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65 65 ;. pCur->pBtree
1ecf0 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 = p;. pCur->pB
1ed00 74 20 3d 20 70 42 74 3b 0a 20 20 61 73 73 65 72 t = pBt;. asser
1ed10 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 t( wrFlag==0 ||
1ed20 77 72 46 6c 61 67 3d 3d 42 54 43 46 5f 57 72 69 wrFlag==BTCF_Wri
1ed30 74 65 46 6c 61 67 20 29 3b 0a 20 20 70 43 75 72 teFlag );. pCur
1ed40 2d 3e 63 75 72 46 6c 61 67 73 20 3d 20 77 72 46 ->curFlags = wrF
1ed50 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 lag;. pCur->pNe
1ed60 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f xt = pBt->pCurso
1ed70 72 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 r;. if( pCur->p
1ed80 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 Next ){. pCur
1ed90 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d ->pNext->pPrev =
1eda0 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 pCur;. }. pBt
1edb0 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 ->pCursor = pCur
1edc0 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 ;. pCur->eState
1edd0 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
1ede0 44 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 D;. return SQLI
1edf0 54 45 5f 4f 4b 3b 0a 7d 0a 69 6e 74 20 73 71 6c TE_OK;.}.int sql
1ee00 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 ite3BtreeCursor(
1ee10 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 . Btree *p,
1ee20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ee40 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 * The btree */.
1ee50 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 int iTable,
1ee60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ee80 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 Root page of tab
1ee90 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
1eea0 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 int wrFlag,
1eeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eec0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
1eed0 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 to write. 0 rea
1eee0 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 d-only */. stru
1eef0 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 ct KeyInfo *pKey
1ef00 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 Info,
1ef10 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
1ef20 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 arg to xCompare
1ef30 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 () */. BtCursor
1ef40 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 *pCur
1ef50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ef60 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 /* Write new
1ef70 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a cursor here */.
1ef80 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
1ef90 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1efa0 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 (p);. rc = btre
1efb0 65 43 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c eCursor(p, iTabl
1efc0 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 e, wrFlag, pKeyI
1efd0 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 nfo, pCur);. sq
1efe0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1eff0 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
1f000 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1f010 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 the size of a B
1f020 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 tCursor object i
1f030 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 n bytes..**.** T
1f040 68 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 his interfaces i
1f050 73 20 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 s needed so that
1f060 20 75 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 users of cursor
1f070 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 s can preallocat
1f080 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 e.** sufficient
1f090 73 74 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 storage to hold
1f0a0 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 a cursor. The B
1f0b0 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 tCursor object i
1f0c0 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 s opaque.** to u
1f0d0 73 65 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e sers so they can
1f0e0 6e 6f 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f not do the sizeo
1f0f0 66 28 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d f() themselves -
1f100 20 74 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a they must call.
1f110 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ** this routine.
1f120 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
1f130 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 76 treeCursorSize(v
1f140 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 oid){. return R
1f150 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 42 74 43 OUND8(sizeof(BtC
1f160 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ursor));.}../*.*
1f170 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d * Initialize mem
1f180 6f 72 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ory that will be
1f190 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
1f1a0 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 a BtCursor objec
1f1b0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 6d t..**.** The sim
1f1c0 70 6c 65 20 61 70 70 72 6f 61 63 68 20 68 65 72 ple approach her
1f1d0 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 6d 65 e would be to me
1f1e0 6d 73 65 74 28 29 20 74 68 65 20 65 6e 74 69 72 mset() the entir
1f1f0 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 6f 20 7a e object.** to z
1f200 65 72 6f 2e 20 20 42 75 74 20 69 74 20 74 75 72 ero. But it tur
1f210 6e 73 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 ns out that the
1f220 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49 apPage[] and aiI
1f230 64 78 5b 5d 20 61 72 72 61 79 73 0a 2a 2a 20 64 dx[] arrays.** d
1f240 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 o not need to be
1f250 20 7a 65 72 6f 65 64 20 61 6e 64 20 74 68 65 79 zeroed and they
1f260 20 61 72 65 20 6c 61 72 67 65 2c 20 73 6f 20 77 are large, so w
1f270 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c 6f 74 e can save a lot
1f280 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74 69 6d 65 20 .** of run-time
1f290 62 79 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 by skipping the
1f2a0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f initialization o
1f2b0 66 20 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74 73 f those elements
1f2c0 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
1f2d0 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 3BtreeCursorZero
1f2e0 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 (BtCursor *p){.
1f2f0 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6f 66 memset(p, 0, of
1f300 66 73 65 74 6f 66 28 42 74 43 75 72 73 6f 72 2c fsetof(BtCursor,
1f310 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a iPage));.}../*.
1f320 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f ** Close a curso
1f330 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 r. The read loc
1f340 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
1f350 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 e file is releas
1f360 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c ed.** when the l
1f370 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c ast cursor is cl
1f380 6f 73 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c osed..*/.int sql
1f390 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 ite3BtreeCloseCu
1f3a0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 rsor(BtCursor *p
1f3b0 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 Cur){. Btree *p
1f3c0 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 Btree = pCur->pB
1f3d0 74 72 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 tree;. if( pBtr
1f3e0 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b ee ){. int i;
1f3f0 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
1f400 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a Bt = pCur->pBt;.
1f410 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1f420 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 Enter(pBtree);.
1f430 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1f440 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 learCursor(pCur)
1f450 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e ;. if( pCur->
1f460 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 pPrev ){. p
1f470 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 Cur->pPrev->pNex
1f480 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b t = pCur->pNext;
1f490 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1f4a0 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d pBt->pCursor =
1f4b0 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 pCur->pNext;.
1f4c0 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 }. if( pCur
1f4d0 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 ->pNext ){.
1f4e0 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 pCur->pNext->pP
1f4f0 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 rev = pCur->pPre
1f500 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 v;. }. for
1f510 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 (i=0; i<=pCur->i
1f520 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Page; i++){.
1f530 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
1f540 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a ur->apPage[i]);.
1f550 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b }. unlock
1f560 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 BtreeIfUnused(pB
1f570 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 t);. sqlite3D
1f580 62 46 72 65 65 28 70 42 74 72 65 65 2d 3e 64 62 bFree(pBtree->db
1f590 2c 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f , pCur->aOverflo
1f5a0 77 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 w);. /* sqlit
1f5b0 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a e3_free(pCur); *
1f5c0 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 /. sqlite3Btr
1f5d0 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b eeLeave(pBtree);
1f5e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1f5f0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1f600 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
1f610 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 BtCursor* given
1f620 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 in the argument
1f630 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 has a valid.** B
1f640 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 tCursor.info str
1f650 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 69 ucture. If it i
1f660 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61 s not already va
1f670 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72 lid, call.** btr
1f680 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f eeParseCell() to
1f690 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a fill it in..**.
1f6a0 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f ** BtCursor.info
1f6b0 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 is a cache of t
1f6c0 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 he information i
1f6d0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 n the current ce
1f6e0 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 ll..** Using thi
1f6f0 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 s cache reduces
1f700 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 the number of ca
1f710 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 lls to btreePars
1f720 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 eCell()..**.** 2
1f730 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72 007-06-25: Ther
1f740 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f e is a bug in so
1f750 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d me versions of M
1f760 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20 74 SVC that cause t
1f770 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74 he.** compiler t
1f780 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 74 o crash when get
1f790 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d CellInfo() is im
1f7a0 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d plemented as a m
1f7b0 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65 acro..** But the
1f7c0 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 61 re is a measurea
1f7d0 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e 74 ble speed advant
1f7e0 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65 age to using the
1f7f0 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a macro on gcc.**
1f800 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70 (when less comp
1f810 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f iler optimizatio
1f820 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d ns like -Os or -
1f830 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 20 O0 are used and
1f840 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 the.** compiler
1f850 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 67 is not doing agg
1f860 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 ressive inlining
1f870 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 .) So we use a
1f880 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a real function.**
1f890 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 for MSVC and a
1f8a0 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 macro for everyt
1f8b0 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b hing else. Tick
1f8c0 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 et #2457..*/.#if
1f8d0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 ndef NDEBUG. st
1f8e0 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
1f8f0 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f CellInfo(BtCurso
1f900 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 r *pCur){. Ce
1f910 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 llInfo info;.
1f920 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
1f930 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 r->iPage;. me
1f940 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 mset(&info, 0, s
1f950 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 izeof(info));.
1f960 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
1f970 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 (pCur->apPage[iP
1f980 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 age], pCur->aiId
1f990 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 x[iPage], &info)
1f9a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 43 4f ;. assert( CO
1f9b0 52 52 55 50 54 5f 44 42 20 7c 7c 20 6d 65 6d 63 RRUPT_DB || memc
1f9c0 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d mp(&info, &pCur-
1f9d0 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e >info, sizeof(in
1f9e0 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 fo))==0 );. }.#
1f9f0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61 else. #define a
1fa00 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29 ssertCellInfo(x)
1fa10 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f .#endif.#ifdef _
1fa20 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65 MSC_VER. /* Use
1fa30 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e a real function
1fa40 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b in MSVC to work
1fa50 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20 around bugs in
1fa60 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a that compiler. *
1fa70 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 /. static void
1fa80 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 getCellInfo(BtCu
1fa90 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 rsor *pCur){.
1faa0 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e if( pCur->info.
1fab0 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 nSize==0 ){.
1fac0 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 int iPage = pC
1fad0 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20 ur->iPage;.
1fae0 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
1faf0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
1fb00 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b ge],pCur->aiIdx[
1fb10 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e iPage],&pCur->in
1fb20 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d fo);. pCur-
1fb30 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 >curFlags |= BTC
1fb40 46 5f 56 61 6c 69 64 4e 4b 65 79 3b 0a 20 20 20 F_ValidNKey;.
1fb50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
1fb60 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 sertCellInfo(pCu
1fb70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 r);. }. }.#e
1fb80 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d lse /* if not _M
1fb90 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 SC_VER */. /* U
1fba0 73 65 20 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c se a macro in al
1fbb0 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 l other compiler
1fbc0 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 s so that the fu
1fbd0 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 nction is inline
1fbe0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 d */.#define get
1fbf0 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 CellInfo(pCur)
1fc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc30 20 20 20 20 5c 0a 20 20 69 66 28 20 70 43 75 72 \. if( pCur
1fc40 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 ->info.nSize==0
1fc50 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
1fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc80 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 \. int i
1fc90 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 Page = pCur->iPa
1fca0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
1fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcd0 20 20 20 20 20 20 5c 0a 20 20 20 20 62 74 72 65 \. btre
1fce0 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d eParseCell(pCur-
1fcf0 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 >apPage[iPage],p
1fd00 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 Cur->aiIdx[iPage
1fd10 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 ],&pCur->info);
1fd20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 70 43 75 \. pCu
1fd30 72 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 r->curFlags |= B
1fd40 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 3b 20 20 TCF_ValidNKey;
1fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd70 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 \. }els
1fd80 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e{
1fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdc0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 \. a
1fdd0 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 ssertCellInfo(pC
1fde0 75 72 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ur);
1fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe10 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a \. }.
1fe20 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 #endif /* _MSC_V
1fe30 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e ER */..#ifndef N
1fe40 44 45 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 DEBUG /* The ne
1fe50 78 74 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 xt routine used
1fe60 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 only within asse
1fe70 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
1fe80 2a 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 */./*.** Return
1fe90 74 72 75 65 20 69 66 20 74 68 65 20 67 69 76 65 true if the give
1fea0 6e 20 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 n BtCursor is va
1feb0 6c 69 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 lid. A valid cu
1fec0 72 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 rsor is one.** t
1fed0 68 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 hat is currently
1fee0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 pointing to a r
1fef0 6f 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 ow in a (non-emp
1ff00 74 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 ty) table..** Th
1ff10 69 73 20 69 73 20 61 20 76 65 72 69 66 69 63 61 is is a verifica
1ff20 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 tion routine is
1ff30 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e used only within
1ff40 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
1ff50 65 6e 74 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ents..*/.int sql
1ff60 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1ff70 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 20 sValid(BtCursor
1ff80 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75 72 6e *pCur){. return
1ff90 20 70 43 75 72 20 26 26 20 70 43 75 72 2d 3e 65 pCur && pCur->e
1ffa0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
1ffb0 4c 49 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a LID;.}.#endif /*
1ffc0 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a NDEBUG */../*.*
1ffd0 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 * Set *pSize to
1ffe0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
1fff0 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f buffer needed to
20000 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 hold the value
20010 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f of.** the key fo
20020 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e r the current en
20030 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 try. If the cur
20040 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 sor is not point
20050 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 ing.** to a vali
20060 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 d entry, *pSize
20070 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a is set to 0. .**
20080 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 .** For a table
20090 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 with the INTKEY
200a0 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 flag set, this r
200b0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
200c0 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 he key.** itself
200d0 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 , not the number
200e0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
200f0 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 key..**.** The
20100 63 61 6c 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 caller must posi
20110 74 69 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 tion the cursor
20120 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e prior to invokin
20130 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a g this routine..
20140 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ** .** This rout
20150 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ine cannot fail.
20160 20 20 49 74 20 61 6c 77 61 79 73 20 72 65 74 75 It always retu
20170 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 rns SQLITE_OK.
20180 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
20190 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75 treeKeySize(BtCu
201a0 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 rsor *pCur, i64
201b0 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 *pSize){. asser
201c0 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
201d0 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
201e0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
201f0 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
20200 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 LID || pCur->eSt
20210 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
20220 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d D );. if( pCur-
20230 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
20240 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 53 VALID ){. *pS
20250 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 ize = 0;. }else
20260 7b 0a 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 {. getCellInf
20270 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 2a 70 53 o(pCur);. *pS
20280 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f ize = pCur->info
20290 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 72 65 74 .nKey;. }. ret
202a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
202b0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 ../*.** Set *pSi
202c0 7a 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 ze to the number
202d0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 of bytes of dat
202e0 61 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74 a in the entry t
202f0 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 he.** cursor cur
20300 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f rently points to
20310 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
20320 65 72 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 er must guarante
20330 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
20340 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
20350 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 a non-NULL.** v
20360 61 6c 69 64 20 65 6e 74 72 79 2e 20 20 49 6e 20 alid entry. In
20370 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
20380 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 calling procedu
20390 72 65 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 re must guarante
203a0 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 e.** that the cu
203b0 72 73 6f 72 20 68 61 73 20 43 75 72 73 6f 72 2e rsor has Cursor.
203c0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
203d0 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c ALID..**.** Fail
203e0 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 ure is not possi
203f0 62 6c 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 ble. This funct
20400 69 6f 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 ion always retur
20410 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a ns SQLITE_OK..**
20420 20 49 74 20 6d 69 67 68 74 20 6a 75 73 74 20 61 It might just a
20430 73 20 77 65 6c 6c 20 62 65 20 61 20 70 72 6f 63 s well be a proc
20440 65 64 75 72 65 20 28 72 65 74 75 72 6e 69 6e 67 edure (returning
20450 20 76 6f 69 64 29 20 62 75 74 20 77 65 20 63 6f void) but we co
20460 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f 20 72 65 74 ntinue.** to ret
20470 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 urn an integer r
20480 65 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 20 68 esult code for h
20490 69 73 74 6f 72 69 63 61 6c 20 72 65 61 73 6f 6e istorical reason
204a0 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 s..*/.int sqlite
204b0 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 3BtreeDataSize(B
204c0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
204d0 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 32 *pSize){. as
204e0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
204f0 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
20500 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
20510 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
20520 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
20530 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
20540 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 Cur->iPage]->int
20550 4b 65 79 4c 65 61 66 3d 3d 31 20 29 3b 0a 20 20 KeyLeaf==1 );.
20560 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
20570 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 );. *pSize = pC
20580 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 ur->info.nPayloa
20590 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 d;. return SQLI
205a0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
205b0 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 6e Given the page n
205c0 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 umber of an over
205d0 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 flow page in the
205e0 20 64 61 74 61 62 61 73 65 20 28 70 61 72 61 6d database (param
205f0 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 eter.** ovfl), t
20600 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e his function fin
20610 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 ds the page numb
20620 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 er of the next p
20630 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c age in the .** l
20640 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 inked list of ov
20650 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 erflow pages. If
20660 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 73 possible, it us
20670 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 es the auto-vacu
20680 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 um.** pointer-ma
20690 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 6f p data instead o
206a0 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 6f f reading the co
206b0 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 ntent of page ov
206c0 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a fl to do so. .**
206d0 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
206e0 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 65 occurs an SQLite
206f0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
20700 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 eturned. Otherwi
20710 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 se:.**.** The pa
20720 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
20730 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 next overflow p
20740 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 age in the linke
20750 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77 72 d list is .** wr
20760 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e itten to *pPgnoN
20770 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 ext. If page ovf
20780 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 70 61 l is the last pa
20790 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65 64 ge in its linked
207a0 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67 6e .** list, *pPgn
207b0 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 oNext is set to
207c0 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 zero. .**.** If
207d0 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 ppPage is not NU
207e0 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72 65 LL, and a refere
207f0 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 nce to the MemPa
20800 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 73 ge object corres
20810 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 61 ponding.** to pa
20820 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c 20 ge number pOvfl
20830 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 was obtained, th
20840 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 en *ppPage is se
20850 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
20860 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e at.** reference.
20870 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f It is the respo
20880 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 nsibility of the
20890 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c 20 caller to call
208a0 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a 2a releasePage().**
208b0 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20 66 on *ppPage to f
208c0 72 65 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 ree the referenc
208d0 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65 6e e. In no referen
208e0 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 ce was obtained
208f0 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 (because.** the
20900 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73 20 pointer-map was
20910 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 used to obtain t
20920 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70 50 he value for *pP
20930 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a 2a gnoNext), then.*
20940 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 * *ppPage is set
20950 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 to zero..*/.sta
20960 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 66 tic int getOverf
20970 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 61 lowPage(. BtSha
20980 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 red *pBt,
20990 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
209a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
209b0 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 Pgno ovfl,
209c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
209d0 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66 6c * Current overfl
209e0 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a ow page number *
209f0 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
20a00 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
20a10 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65 /* OUT: MemPage
20a20 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20 handle (may be
20a30 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20 NULL) */. Pgno
20a40 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 *pPgnoNext
20a50 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
20a60 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 Next overflow pa
20a70 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a ge number */.){.
20a80 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b Pgno next = 0;
20a90 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
20aa0 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 e = 0;. int rc
20ab0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
20ac0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
20ad0 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
20ae0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
20af0 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a rt(pPgnoNext);..
20b00 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
20b10 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
20b20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 /* Try to find
20b30 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e the next page in
20b40 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 the overflow li
20b50 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a st using the. *
20b60 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 * autovacuum poi
20b70 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 nter-map pages.
20b80 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e Guess that the n
20b90 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a ext page in . *
20ba0 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c * the overflow l
20bb0 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 ist is page numb
20bc0 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 er (ovfl+1). If
20bd0 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 that guess turns
20be0 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 . ** out to be
20bf0 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 wrong, fall bac
20c00 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 k to loading the
20c10 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 data of page .
20c20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 ** number ovfl
20c30 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
20c40 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 next page numbe
20c50 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 r.. */. if( pB
20c60 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
20c70 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a . Pgno pgno;.
20c80 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 Pgno iGuess
20c90 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 = ovfl+1;. u8
20ca0 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 eType;.. whi
20cb0 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 le( PTRMAP_ISPAG
20cc0 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c E(pBt, iGuess) |
20cd0 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e | iGuess==PENDIN
20ce0 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
20cf0 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 ){. iGuess
20d00 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 ++;. }.. i
20d10 66 28 20 69 47 75 65 73 73 3c 3d 62 74 72 65 65 f( iGuess<=btree
20d20 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 Pagecount(pBt) )
20d30 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 {. rc = ptr
20d40 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 mapGet(pBt, iGue
20d50 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e ss, &eType, &pgn
20d60 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 o);. if( rc
20d70 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 ==SQLITE_OK && e
20d80 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 Type==PTRMAP_OVE
20d90 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d RFLOW2 && pgno==
20da0 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 ovfl ){.
20db0 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 next = iGuess;.
20dc0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
20dd0 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d TE_DONE;. }
20de0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
20df0 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65 78 f.. assert( nex
20e00 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 t==0 || rc==SQLI
20e10 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 TE_DONE );. if(
20e20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
20e30 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 {. rc = btree
20e40 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 GetPage(pBt, ovf
20e50 6c 2c 20 26 70 50 61 67 65 2c 20 28 70 70 50 61 l, &pPage, (ppPa
20e60 67 65 3d 3d 30 29 20 3f 20 50 41 47 45 52 5f 47 ge==0) ? PAGER_G
20e70 45 54 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 30 29 ET_READONLY : 0)
20e80 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
20e90 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
20ea0 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 Page==0 );. i
20eb0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
20ec0 20 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d ){. next =
20ed0 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d get4byte(pPage-
20ee0 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 >aData);. }.
20ef0 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 }.. *pPgnoNext
20f00 20 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 = next;. if( p
20f10 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 pPage ){. *pp
20f20 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 Page = pPage;.
20f30 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 }else{. relea
20f40 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
20f50 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d }. return (rc=
20f60 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 =SQLITE_DONE ? S
20f70 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a QLITE_OK : rc);.
20f80 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 }../*.** Copy da
20f90 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
20fa0 20 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 to a page, or f
20fb0 72 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 rom a page to a
20fc0 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 buffer..**.** pP
20fd0 61 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e ayload is a poin
20fe0 74 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 ter to data stor
20ff0 65 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 ed on database p
21000 61 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 age pDbPage..**
21010 49 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 If argument eOp
21020 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e is false, then n
21030 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 Byte bytes of da
21040 74 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a ta are copied.**
21050 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 from pPayload t
21060 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 o the buffer poi
21070 6e 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e nted at by pBuf.
21080 20 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c If eOp is true,
21090 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 .** then sqlite3
210a0 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 PagerWrite() is
210b0 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 called on pDbPag
210c0 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 e and nByte byte
210d0 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 s.** of data are
210e0 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 copied from the
210f0 20 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 buffer pBuf to
21100 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 pPayload..**.**
21110 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
21120 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 urned on success
21130 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 , otherwise an e
21140 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 rror code..*/.st
21150 61 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 atic int copyPay
21160 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 load(. void *pP
21170 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 ayload,
21180 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
21190 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 page data */. v
211a0 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
211b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
211c0 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f ter to buffer */
211d0 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 . int nByte,
211e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
211f0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
21200 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 to copy */. int
21210 20 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 eOp,
21220 20 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 /* 0 -> c
21230 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 opy from page, 1
21240 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 -> copy to page
21250 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 */. DbPage *pD
21260 62 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 bPage
21270 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 /* Page containi
21280 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 ng pPayload */.)
21290 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 {. if( eOp ){.
212a0 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 /* Copy data
212b0 66 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 from buffer to p
212c0 61 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 age (a write ope
212d0 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 ration) */. i
212e0 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 nt rc = sqlite3P
212f0 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 agerWrite(pDbPag
21300 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
21310 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
21320 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
21330 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 }. memcpy(p
21340 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e Payload, pBuf, n
21350 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Byte);. }else{.
21360 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 /* Copy data
21370 20 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 from page to bu
21380 66 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 ffer (a read ope
21390 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d ration) */. m
213a0 65 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 emcpy(pBuf, pPay
213b0 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 load, nByte);.
213c0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
213d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
213e0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
213f0 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 used to read or
21400 6f 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 overwrite payloa
21410 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a d information.**
21420 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 for the entry t
21430 68 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 hat the pCur cur
21440 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
21450 74 6f 2e 20 54 68 65 20 65 4f 70 0a 2a 2a 20 61 to. The eOp.** a
21460 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 rgument is inter
21470 70 72 65 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 preted as follow
21480 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 30 3a 20 54 68 s:.**.** 0: Th
21490 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 e operation is a
214a0 20 72 65 61 64 2e 20 50 6f 70 75 6c 61 74 65 20 read. Populate
214b0 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 61 63 the overflow cac
214c0 68 65 2e 0a 2a 2a 20 20 20 31 3a 20 54 68 65 20 he..** 1: The
214d0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 20 77 operation is a w
214e0 72 69 74 65 2e 20 50 6f 70 75 6c 61 74 65 20 74 rite. Populate t
214f0 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 61 63 68 he overflow cach
21500 65 2e 0a 2a 2a 20 20 20 32 3a 20 54 68 65 20 6f e..** 2: The o
21510 70 65 72 61 74 69 6f 6e 20 69 73 20 61 20 72 65 peration is a re
21520 61 64 2e 20 44 6f 20 6e 6f 74 20 70 6f 70 75 6c ad. Do not popul
21530 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ate the overflow
21540 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 cache..**.** A
21550 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20 62 total of "amt" b
21560 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f 72 ytes are read or
21570 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e 69 written beginni
21580 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a ng at "offset"..
21590 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64 20 ** Data is read
215a0 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20 62 to or from the b
215b0 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a uffer pBuf..**.*
215c0 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 * The content be
215d0 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 69 74 ing read or writ
215e0 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 61 72 ten might appear
215f0 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61 67 on the main pag
21600 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 74 74 e.** or be scatt
21610 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 ered out on mult
21620 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 iple overflow pa
21630 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ges..**.** If th
21640 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 e current cursor
21650 20 65 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20 entry uses one
21660 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 or more overflow
21670 20 70 61 67 65 73 20 61 6e 64 20 74 68 65 0a 2a pages and the.*
21680 2a 20 65 4f 70 20 61 72 67 75 6d 65 6e 74 20 69 * eOp argument i
21690 73 20 6e 6f 74 20 32 2c 20 74 68 69 73 20 66 75 s not 2, this fu
216a0 6e 63 74 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 63 nction may alloc
216b0 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 61 6e ate space for an
216c0 64 20 6c 61 7a 69 6c 79 20 0a 2a 2a 20 70 6f 70 d lazily .** pop
216d0 75 6c 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 ulates the overf
216e0 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
216f0 63 68 65 20 61 72 72 61 79 20 28 42 74 43 75 72 che array (BtCur
21700 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 sor.aOverflow).
21710 0a 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 63 .** Subsequent c
21720 61 6c 6c 73 20 75 73 65 20 74 68 69 73 20 63 61 alls use this ca
21730 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b che to make seek
21740 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c ing to the suppl
21750 69 65 64 20 6f 66 66 73 65 74 20 0a 2a 2a 20 6d ied offset .** m
21760 6f 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a ore efficient..*
21770 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 *.** Once an ove
21780 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
21790 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 cache has been a
217a0 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 llocated, it may
217b0 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 be.** invalidat
217c0 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ed if some other
217d0 20 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 cursor writes t
217e0 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 o the same table
217f0 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 , or if.** the c
21800 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 ursor is moved t
21810 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f o a different ro
21820 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c w. Additionally,
21830 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a in auto-vacuum.
21840 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c ** mode, the fol
21850 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 lowing events ma
21860 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 y invalidate an
21870 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
21880 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 st cache..**.**
21890 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 * An increment
218a0 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 al vacuum,.**
218b0 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 * A commit in au
218c0 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 to_vacuum="full"
218d0 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 mode,.** * Cr
218e0 65 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 eating a table (
218f0 6d 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 may require movi
21900 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ng an overflow p
21910 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 age)..*/.static
21920 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 int accessPayloa
21930 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 d(. BtCursor *p
21940 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 Cur, /* Cur
21950 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
21960 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 entry to read fr
21970 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 om */. u32 offs
21980 65 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et, /*
21990 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 Begin reading th
219a0 69 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c is far into payl
219b0 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 oad */. u32 amt
219c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
219d0 20 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 Read this many
219e0 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 bytes */. unsig
219f0 6e 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 ned char *pBuf,
21a00 2f 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 /* Write the byt
21a10 65 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 es into this buf
21a20 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f fer */ . int eO
21a30 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f p /
21a40 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 * zero to read.
21a50 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 non-zero to writ
21a60 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 e. */.){. unsig
21a70 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f ned char *aPaylo
21a80 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ad;. int rc = S
21a90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
21aa0 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 iIdx = 0;. MemP
21ab0 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 age *pPage = pCu
21ac0 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
21ad0 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74 72 65 65 iPage]; /* Btree
21ae0 20 70 61 67 65 20 6f 66 20 63 75 72 72 65 6e 74 page of current
21af0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 74 53 68 entry */. BtSh
21b00 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 ared *pBt = pCur
21b10 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 ->pBt;
21b20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 /* Btree
21b30 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c this cursor bel
21b40 6f 6e 67 73 20 74 6f 20 2a 2f 0a 23 69 66 64 65 ongs to */.#ifde
21b50 66 20 53 51 4c 49 54 45 5f 44 49 52 45 43 54 5f f SQLITE_DIRECT_
21b60 4f 56 45 52 46 4c 4f 57 5f 52 45 41 44 0a 20 20 OVERFLOW_READ.
21b70 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20 unsigned char *
21b80 63 6f 6e 73 74 20 70 42 75 66 53 74 61 72 74 20 const pBufStart
21b90 3d 20 70 42 75 66 3b 0a 20 20 69 6e 74 20 62 45 = pBuf;. int bE
21ba0 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nd;
21bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21bc0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 /* True if r
21bd0 65 61 64 69 6e 67 20 74 6f 20 65 6e 64 20 6f 66 eading to end of
21be0 20 64 61 74 61 20 2a 2f 0a 23 65 6e 64 69 66 0a data */.#endif.
21bf0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
21c00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
21c10 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
21c20 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
21c30 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
21c40 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
21c50 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
21c60 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
21c70 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
21c80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4f 70 );. assert( eOp
21c90 21 3d 32 20 7c 7c 20 6f 66 66 73 65 74 3d 3d 30 !=2 || offset==0
21ca0 20 29 3b 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 ); /* Always
21cb0 20 73 74 61 72 74 20 66 72 6f 6d 20 62 65 67 69 start from begi
21cc0 6e 6e 69 6e 67 20 66 6f 72 20 65 4f 70 3d 3d 32 nning for eOp==2
21cd0 20 2a 2f 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e */.. getCellIn
21ce0 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 fo(pCur);. aPay
21cf0 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 load = pCur->inf
21d00 6f 2e 70 50 61 79 6c 6f 61 64 3b 0a 23 69 66 64 o.pPayload;.#ifd
21d10 65 66 20 53 51 4c 49 54 45 5f 44 49 52 45 43 54 ef SQLITE_DIRECT
21d20 5f 4f 56 45 52 46 4c 4f 57 5f 52 45 41 44 0a 20 _OVERFLOW_READ.
21d30 20 62 45 6e 64 20 3d 20 6f 66 66 73 65 74 2b 61 bEnd = offset+a
21d40 6d 74 3d 3d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e mt==pCur->info.n
21d50 50 61 79 6c 6f 61 64 3b 0a 23 65 6e 64 69 66 0a Payload;.#endif.
21d60 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 assert( offset
21d70 2b 61 6d 74 20 3c 3d 20 70 43 75 72 2d 3e 69 6e +amt <= pCur->in
21d80 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 0a fo.nPayload );..
21d90 20 20 69 66 28 20 26 61 50 61 79 6c 6f 61 64 5b if( &aPayload[
21da0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
21db0 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61 44 61 l] > &pPage->aDa
21dc0 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 ta[pBt->usableSi
21dd0 7a 65 5d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 ze] ){. /* Tr
21de0 79 69 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 ying to read or
21df0 77 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 write past the e
21e00 6e 64 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 nd of the data i
21e10 73 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 s an error */.
21e20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
21e30 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
21e40 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 }.. /* Check if
21e50 20 64 61 74 61 20 6d 75 73 74 20 62 65 20 72 65 data must be re
21e60 61 64 2f 77 72 69 74 74 65 6e 20 74 6f 2f 66 72 ad/written to/fr
21e70 6f 6d 20 74 68 65 20 62 74 72 65 65 20 70 61 67 om the btree pag
21e80 65 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 e itself. */. i
21e90 66 28 20 6f 66 66 73 65 74 3c 70 43 75 72 2d 3e f( offset<pCur->
21ea0 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 info.nLocal ){.
21eb0 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a int a = amt;.
21ec0 20 20 20 20 69 66 28 20 61 2b 6f 66 66 73 65 74 if( a+offset
21ed0 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 >pCur->info.nLoc
21ee0 61 6c 20 29 7b 0a 20 20 20 20 20 20 61 20 3d 20 al ){. a =
21ef0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
21f00 6c 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 l - offset;.
21f10 7d 0a 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 }. rc = copyP
21f20 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 ayload(&aPayload
21f30 5b 6f 66 66 73 65 74 5d 2c 20 70 42 75 66 2c 20 [offset], pBuf,
21f40 61 2c 20 28 65 4f 70 20 26 20 30 78 30 31 29 2c a, (eOp & 0x01),
21f50 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 pPage->pDbPage)
21f60 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 ;. offset = 0
21f70 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b ;. pBuf += a;
21f80 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 . amt -= a;.
21f90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 }else{. offs
21fa0 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f et -= pCur->info
21fb0 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 .nLocal;. }..
21fc0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
21fd0 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 K && amt>0 ){.
21fe0 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c const u32 ovfl
21ff0 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
22000 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 leSize - 4; /*
22010 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 Bytes content pe
22020 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 r ovfl page */.
22030 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 Pgno nextPage
22040 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20 ;.. nextPage
22050 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 61 79 = get4byte(&aPay
22060 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e load[pCur->info.
22070 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 20 20 20 20 2f nLocal]);.. /
22080 2a 20 49 66 20 74 68 65 20 42 74 43 75 72 73 6f * If the BtCurso
22090 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 68 61 r.aOverflow[] ha
220a0 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 s not been alloc
220b0 61 74 65 64 2c 20 61 6c 6c 6f 63 61 74 65 20 69 ated, allocate i
220c0 74 20 6e 6f 77 2e 0a 20 20 20 20 2a 2a 20 45 78 t now.. ** Ex
220d0 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c cept, do not all
220e0 6f 63 61 74 65 20 61 4f 76 65 72 66 6c 6f 77 5b ocate aOverflow[
220f0 5d 20 66 6f 72 20 65 4f 70 3d 3d 32 2e 0a 20 20 ] for eOp==2..
22100 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
22110 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 aOverflow[] arra
22120 79 20 69 73 20 73 69 7a 65 64 20 61 74 20 6f 6e y is sized at on
22130 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 e entry for each
22140 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 overflow page.
22150 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 76 65 ** in the ove
22160 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 rflow chain. The
22170 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
22180 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c the first overfl
22190 6f 77 20 70 61 67 65 20 69 73 0a 20 20 20 20 2a ow page is. *
221a0 2a 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 76 65 * stored in aOve
221b0 72 66 6c 6f 77 5b 30 5d 2c 20 65 74 63 2e 20 41 rflow[0], etc. A
221c0 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 value of 0 in t
221d0 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 he aOverflow[] a
221e0 72 72 61 79 0a 20 20 20 20 2a 2a 20 6d 65 61 6e rray. ** mean
221f0 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e 6f 77 6e s "not yet known
22200 22 20 28 74 68 65 20 63 61 63 68 65 20 69 73 20 " (the cache is
22210 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 lazily populated
22220 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ).. */. if
22230 28 20 65 4f 70 21 3d 32 20 26 26 20 28 70 43 75 ( eOp!=2 && (pCu
22240 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 20 42 54 r->curFlags & BT
22250 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 3d 3d 30 CF_ValidOvfl)==0
22260 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f ){. int nO
22270 76 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 vfl = (pCur->inf
22280 6f 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d o.nPayload-pCur-
22290 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 >info.nLocal+ovf
222a0 6c 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a lSize-1)/ovflSiz
222b0 65 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76 e;. if( nOv
222c0 66 6c 3e 70 43 75 72 2d 3e 6e 4f 76 66 6c 41 6c fl>pCur->nOvflAl
222d0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 50 loc ){. P
222e0 67 6e 6f 20 2a 61 4e 65 77 20 3d 20 28 50 67 6e gno *aNew = (Pgn
222f0 6f 2a 29 73 71 6c 69 74 65 33 44 62 52 65 61 6c o*)sqlite3DbReal
22300 6c 6f 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 loc(.
22310 20 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 pCur->pBtree->d
22320 62 2c 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c b, pCur->aOverfl
22330 6f 77 2c 20 6e 4f 76 66 6c 2a 32 2a 73 69 7a 65 ow, nOvfl*2*size
22340 6f 66 28 50 67 6e 6f 29 0a 20 20 20 20 20 20 20 of(Pgno).
22350 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
22360 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 aNew==0 ){.
22370 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
22380 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
22390 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
223a0 20 70 43 75 72 2d 3e 6e 4f 76 66 6c 41 6c 6c 6f pCur->nOvflAllo
223b0 63 20 3d 20 6e 4f 76 66 6c 2a 32 3b 0a 20 20 20 c = nOvfl*2;.
223c0 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 pCur->aOv
223d0 65 72 66 6c 6f 77 20 3d 20 61 4e 65 77 3b 0a 20 erflow = aNew;.
223e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
223f0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
22400 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
22410 20 20 20 20 6d 65 6d 73 65 74 28 70 43 75 72 2d memset(pCur-
22420 3e 61 4f 76 65 72 66 6c 6f 77 2c 20 30 2c 20 6e >aOverflow, 0, n
22430 4f 76 66 6c 2a 73 69 7a 65 6f 66 28 50 67 6e 6f Ovfl*sizeof(Pgno
22440 29 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 ));. pCur
22450 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 ->curFlags |= BT
22460 43 46 5f 56 61 6c 69 64 4f 76 66 6c 3b 0a 20 20 CF_ValidOvfl;.
22470 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
22480 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 /* If the overf
22490 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
224a0 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c che has been all
224b0 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 ocated and the.
224c0 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 ** entry for
224d0 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72 the first requir
224e0 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ed overflow page
224f0 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a is valid, skip.
22500 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 ** directly
22510 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
22520 20 20 69 66 28 20 28 70 43 75 72 2d 3e 63 75 72 if( (pCur->cur
22530 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c Flags & BTCF_Val
22540 69 64 4f 76 66 6c 29 21 3d 30 0a 20 20 20 20 20 idOvfl)!=0.
22550 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c && pCur->aOverfl
22560 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 ow[offset/ovflSi
22570 7a 65 5d 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ze]. ){.
22580 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f iIdx = (offset/
22590 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 ovflSize);.
225a0 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 nextPage = pCur
225b0 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
225c0 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 ];. offset
225d0 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 = (offset%ovflSi
225e0 7a 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ze);. }..
225f0 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 for( ; rc==SQLIT
22600 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 E_OK && amt>0 &&
22610 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b nextPage; iIdx+
22620 2b 29 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 +){.. /* If
22630 20 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c required, popul
22640 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ate the overflow
22650 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
22660 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 . */. if( (
22670 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
22680 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c 29 BTCF_ValidOvfl)
22690 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 !=0 ){. a
226a0 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 ssert(!pCur->aOv
226b0 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 erflow[iIdx] ||
226c0 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
226d0 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 iIdx]==nextPage)
226e0 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e ;. pCur->
226f0 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 aOverflow[iIdx]
22700 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20 = nextPage;.
22710 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6f }.. if( o
22720 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 ffset>=ovflSize
22730 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
22740 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f e only reason to
22750 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 read this page
22760 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 is to obtain the
22770 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
22780 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
22790 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
227a0 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e overflow chain.
227b0 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 The page.
227c0 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 ** data is not
227d0 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 required. So fi
227e0 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 rst try to looku
227f0 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 p the overflow.
22800 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c ** page-l
22810 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e ist cache, if an
22820 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 y, then fall bac
22830 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 k to the getOver
22840 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 flowPage().
22850 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a ** function..
22860 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
22870 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 ** Note that
22880 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 the aOverflow[]
22890 61 72 72 61 79 20 6d 75 73 74 20 62 65 20 61 6c array must be al
228a0 6c 6f 63 61 74 65 64 20 62 65 63 61 75 73 65 20 located because
228b0 65 4f 70 21 3d 32 0a 20 20 20 20 20 20 20 20 2a eOp!=2. *
228c0 2a 20 68 65 72 65 2e 20 20 49 66 20 65 4f 70 3d * here. If eOp=
228d0 3d 32 2c 20 74 68 65 6e 20 6f 66 66 73 65 74 3d =2, then offset=
228e0 3d 30 20 61 6e 64 20 74 68 69 73 20 62 72 61 6e =0 and this bran
228f0 63 68 20 69 73 20 6e 65 76 65 72 20 74 61 6b 65 ch is never take
22900 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 n.. */.
22910 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 4f assert( eO
22920 70 21 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20 p!=2 );.
22930 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 75 assert( pCur->cu
22940 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 61 rFlags & BTCF_Va
22950 6c 69 64 4f 76 66 6c 20 29 3b 0a 20 20 20 20 20 lidOvfl );.
22960 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 if( pCur->aOv
22970 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 erflow[iIdx+1] )
22980 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 {. next
22990 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 Page = pCur->aOv
229a0 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a erflow[iIdx+1];.
229b0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
229c0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 rc = ge
229d0 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 tOverflowPage(pB
229e0 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 t, nextPage, 0,
229f0 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 &nextPage);.
22a00 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6f 66 }. of
22a10 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 fset -= ovflSize
22a20 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
22a30 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 /* Need t
22a40 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 o read this page
22a50 20 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f properly. It co
22a60 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 ntains some of t
22a70 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 he. ** ra
22a80 6e 67 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 nge of data that
22a90 20 69 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 is being read (
22aa0 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 eOp==0) or writt
22ab0 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 en (eOp!=0)..
22ac0 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 */.#ifdef S
22ad0 51 4c 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 QLITE_DIRECT_OVE
22ae0 52 46 4c 4f 57 5f 52 45 41 44 0a 20 20 20 20 20 RFLOW_READ.
22af0 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 sqlite3_file
22b00 2a 66 64 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 *fd;.#endif.
22b10 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b int a = amt;
22b20 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 20 2b . if( a +
22b30 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 offset > ovflSi
22b40 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ze ){.
22b50 61 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f a = ovflSize - o
22b60 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 7d ffset;. }
22b70 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
22b80 44 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 5f DIRECT_OVERFLOW_
22b90 52 45 41 44 0a 20 20 20 20 20 20 20 20 2f 2a 20 READ. /*
22ba0 49 66 20 61 6c 6c 20 74 68 65 20 66 6f 6c 6c 6f If all the follo
22bb0 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 20 wing are true:.
22bc0 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
22bd0 20 20 2a 2a 20 20 20 31 29 20 74 68 69 73 20 69 ** 1) this i
22be0 73 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 s a read operati
22bf0 6f 6e 2c 20 61 6e 64 20 0a 20 20 20 20 20 20 20 on, and .
22c00 20 2a 2a 20 20 20 32 29 20 64 61 74 61 20 69 73 ** 2) data is
22c10 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 74 required from t
22c20 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 he start of this
22c30 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 overflow page,
22c40 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 and. **
22c50 20 33 29 20 74 68 65 20 64 61 74 61 62 61 73 65 3) the database
22c60 20 69 73 20 66 69 6c 65 2d 62 61 63 6b 65 64 2c is file-backed,
22c70 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 and. **
22c80 20 20 34 29 20 74 68 65 72 65 20 69 73 20 6e 6f 4) there is no
22c90 20 6f 70 65 6e 20 77 72 69 74 65 2d 74 72 61 6e open write-tran
22ca0 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 saction, and.
22cb0 20 20 20 20 20 2a 2a 20 20 20 35 29 20 74 68 65 ** 5) the
22cc0 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
22cd0 20 61 20 57 41 4c 20 64 61 74 61 62 61 73 65 2c a WAL database,
22ce0 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 36 29 . ** 6)
22cf0 20 61 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20 74 all data from t
22d00 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 he page is being
22d10 20 72 65 61 64 2e 0a 20 20 20 20 20 20 20 20 2a read.. *
22d20 2a 20 20 20 37 29 20 61 74 20 6c 65 61 73 74 20 * 7) at least
22d30 34 20 62 79 74 65 73 20 68 61 76 65 20 61 6c 72 4 bytes have alr
22d40 65 61 64 79 20 62 65 65 6e 20 72 65 61 64 20 69 eady been read i
22d50 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 nto the output b
22d60 75 66 66 65 72 20 0a 20 20 20 20 20 20 20 20 2a uffer . *
22d70 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 *. ** the
22d80 6e 20 64 61 74 61 20 63 61 6e 20 62 65 20 72 65 n data can be re
22d90 61 64 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d ad directly from
22da0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
22db0 6c 65 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 le into the.
22dc0 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 62 75 ** output bu
22dd0 66 66 65 72 2c 20 62 79 70 61 73 73 69 6e 67 20 ffer, bypassing
22de0 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 61 the page-cache a
22df0 6c 74 6f 67 65 74 68 65 72 2e 20 54 68 69 73 20 ltogether. This
22e00 73 70 65 65 64 73 0a 20 20 20 20 20 20 20 20 2a speeds. *
22e10 2a 20 75 70 20 6c 6f 61 64 69 6e 67 20 6c 61 72 * up loading lar
22e20 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 ge records that
22e30 73 70 61 6e 20 6d 61 6e 79 20 6f 76 65 72 66 6c span many overfl
22e40 6f 77 20 70 61 67 65 73 2e 0a 20 20 20 20 20 20 ow pages..
22e50 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 */. if(
22e60 20 28 65 4f 70 26 30 78 30 31 29 3d 3d 30 20 20 (eOp&0x01)==0
22e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22e90 20 20 20 20 2f 2a 20 28 31 29 20 2a 2f 0a 20 20 /* (1) */.
22ea0 20 20 20 20 20 20 20 26 26 20 6f 66 66 73 65 74 && offset
22eb0 3d 3d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 ==0
22ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
22ee0 28 32 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 (2) */.
22ef0 26 26 20 28 62 45 6e 64 20 7c 7c 20 61 3d 3d 6f && (bEnd || a==o
22f00 76 66 6c 53 69 7a 65 29 20 20 20 20 20 20 20 20 vflSize)
22f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22f20 20 20 20 20 20 20 2f 2a 20 28 36 29 20 2a 2f 0a /* (6) */.
22f30 20 20 20 20 20 20 20 20 20 26 26 20 70 42 74 2d && pBt-
22f40 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
22f50 54 52 41 4e 53 5f 52 45 41 44 20 20 20 20 20 20 TRANS_READ
22f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
22f70 2a 20 28 34 29 20 2a 2f 0a 20 20 20 20 20 20 20 * (4) */.
22f80 20 20 26 26 20 28 66 64 20 3d 20 73 71 6c 69 74 && (fd = sqlit
22f90 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 74 2d e3PagerFile(pBt-
22fa0 3e 70 50 61 67 65 72 29 29 2d 3e 70 4d 65 74 68 >pPager))->pMeth
22fb0 6f 64 73 20 20 20 20 20 2f 2a 20 28 33 29 20 2a ods /* (3) *
22fc0 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 42 /. && pB
22fd0 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
22fe0 5b 31 39 5d 3d 3d 30 78 30 31 20 20 20 20 20 20 [19]==0x01
22ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23000 20 2f 2a 20 28 35 29 20 2a 2f 0a 20 20 20 20 20 /* (5) */.
23010 20 20 20 20 26 26 20 26 70 42 75 66 5b 2d 34 5d && &pBuf[-4]
23020 3e 3d 70 42 75 66 53 74 61 72 74 20 20 20 20 20 >=pBufStart
23030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 28 37 29 /* (7)
23050 20 2a 2f 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 */. ){.
23060 20 20 20 20 20 20 20 20 20 75 38 20 61 53 61 76 u8 aSav
23070 65 5b 34 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 e[4];.
23080 75 38 20 2a 61 57 72 69 74 65 20 3d 20 26 70 42 u8 *aWrite = &pB
23090 75 66 5b 2d 34 5d 3b 0a 20 20 20 20 20 20 20 20 uf[-4];.
230a0 20 20 61 73 73 65 72 74 28 20 61 57 72 69 74 65 assert( aWrite
230b0 3e 3d 70 42 75 66 53 74 61 72 74 20 29 3b 20 20 >=pBufStart );
230c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
230d0 20 20 20 20 20 20 20 2f 2a 20 68 65 6e 63 65 20 /* hence
230e0 28 37 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 (7) */.
230f0 20 6d 65 6d 63 70 79 28 61 53 61 76 65 2c 20 61 memcpy(aSave, a
23100 57 72 69 74 65 2c 20 34 29 3b 0a 20 20 20 20 20 Write, 4);.
23110 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
23120 33 4f 73 52 65 61 64 28 66 64 2c 20 61 57 72 69 3OsRead(fd, aWri
23130 74 65 2c 20 61 2b 34 2c 20 28 69 36 34 29 70 42 te, a+4, (i64)pB
23140 74 2d 3e 70 61 67 65 53 69 7a 65 2a 28 6e 65 78 t->pageSize*(nex
23150 74 50 61 67 65 2d 31 29 29 3b 0a 20 20 20 20 20 tPage-1));.
23160 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 nextPage =
23170 67 65 74 34 62 79 74 65 28 61 57 72 69 74 65 29 get4byte(aWrite)
23180 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 ;. memc
23190 70 79 28 61 57 72 69 74 65 2c 20 61 53 61 76 65 py(aWrite, aSave
231a0 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 , 4);. }e
231b0 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 lse.#endif..
231c0 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 {.
231d0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
231e0 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
231f0 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
23200 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ire(pBt->pPager,
23210 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44 62 50 nextPage, &pDbP
23220 61 67 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 age,.
23230 20 20 20 28 28 65 4f 70 26 30 78 30 31 29 3d 3d ((eOp&0x01)==
23240 30 20 3f 20 50 41 47 45 52 5f 47 45 54 5f 52 45 0 ? PAGER_GET_RE
23250 41 44 4f 4e 4c 59 20 3a 20 30 29 0a 20 20 20 20 ADONLY : 0).
23260 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
23270 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
23280 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
23290 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 aPayload = s
232a0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
232b0 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 ta(pDbPage);.
232c0 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 nextPag
232d0 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61 e = get4byte(aPa
232e0 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20 yload);.
232f0 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 rc = copyPay
23300 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f load(&aPayload[o
23310 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 ffset+4], pBuf,
23320 61 2c 20 28 65 4f 70 26 30 78 30 31 29 2c 20 70 a, (eOp&0x01), p
23330 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
23340 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
23350 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b rUnref(pDbPage);
23360 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 66 . off
23370 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 set = 0;.
23380 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
23390 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b amt -= a;
233a0 0a 20 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d . pBuf +=
233b0 20 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 a;. }.
233c0 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d }. }.. if( rc=
233d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d =SQLITE_OK && am
233e0 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 t>0 ){. retur
233f0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
23400 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 _BKPT;. }. ret
23410 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
23420 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 Read part of th
23430 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65 64 e key associated
23440 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 with cursor pCu
23450 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 r. Exactly.** "
23460 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 amt" bytes will
23470 62 65 20 74 72 61 6e 73 66 65 72 72 65 64 20 69 be transferred i
23480 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 nto pBuf[]. The
23490 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 transfer.** beg
234a0 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e ins at "offset".
234b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 .**.** The calle
234c0 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 r must ensure th
234d0 61 74 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 at pCur is point
234e0 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 ing to a valid r
234f0 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 ow.** in the tab
23500 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e le..**.** Return
23510 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
23520 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f ccess or an erro
23530 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 r code if anythi
23540 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 ng goes.** wrong
23550 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 . An error is r
23560 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 eturned if "offs
23570 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 et+amt" is large
23580 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 r than.** the av
23590 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e ailable payload.
235a0 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
235b0 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 treeKey(BtCursor
235c0 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 *pCur, u32 offs
235d0 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 et, u32 amt, voi
235e0 64 20 2a 70 42 75 66 29 7b 0a 20 20 61 73 73 65 d *pBuf){. asse
235f0 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
23600 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
23610 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
23620 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
23630 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ID );. assert(
23640 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 pCur->iPage>=0 &
23650 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 & pCur->apPage[p
23660 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 Cur->iPage] );.
23670 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
23680 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
23690 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ]<pCur->apPage[p
236a0 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
236b0 6c 6c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 61 ll );. return a
236c0 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 ccessPayload(pCu
236d0 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 r, offset, amt,
236e0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 (unsigned char*)
236f0 70 42 75 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a pBuf, 0);.}../*.
23700 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 ** Read part of
23710 74 68 65 20 64 61 74 61 20 61 73 73 6f 63 69 61 the data associa
23720 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 ted with cursor
23730 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a pCur. Exactly.*
23740 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 * "amt" bytes wi
23750 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 ll be transfered
23760 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 into pBuf[]. T
23770 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 he transfer.** b
23780 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 egins at "offset
23790 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 "..**.** Return
237a0 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
237b0 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 cess or an error
237c0 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e code if anythin
237d0 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
237e0 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 An error is re
237f0 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 turned if "offse
23800 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 t+amt" is larger
23810 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 than.** the ava
23820 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a ilable payload..
23830 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 */.int sqlite3Bt
23840 72 65 65 44 61 74 61 28 42 74 43 75 72 73 6f 72 reeData(BtCursor
23850 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 *pCur, u32 offs
23860 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 et, u32 amt, voi
23870 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 d *pBuf){. int
23880 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c rc;..#ifndef SQL
23890 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
238a0 42 0a 20 20 69 66 20 28 20 70 43 75 72 2d 3e 65 B. if ( pCur->e
238b0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
238c0 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 VALID ){. ret
238d0 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 urn SQLITE_ABORT
238e0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
238f0 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
23900 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
23910 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 ;. rc = restore
23920 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
23930 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d Cur);. if( rc==
23940 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
23950 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
23960 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
23970 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 73 65 72 LID );. asser
23980 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d t( pCur->iPage>=
23990 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 0 && pCur->apPag
239a0 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 e[pCur->iPage] )
239b0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
239c0 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
239d0 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 iPage]<pCur->apP
239e0 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
239f0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 ->nCell );. r
23a00 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 c = accessPayloa
23a10 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 d(pCur, offset,
23a20 61 6d 74 2c 20 70 42 75 66 2c 20 30 29 3b 0a 20 amt, pBuf, 0);.
23a30 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
23a40 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
23a50 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 a pointer to pay
23a60 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e load information
23a70 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 from the entry
23a80 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 that the .** pCu
23a90 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e r cursor is poin
23aa0 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f ting to. The po
23ab0 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 inter is to the
23ac0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 beginning of.**
23ad0 74 68 65 20 6b 65 79 20 69 66 20 69 6e 64 65 78 the key if index
23ae0 20 62 74 72 65 65 73 20 28 70 50 61 67 65 2d 3e btrees (pPage->
23af0 69 6e 74 4b 65 79 3d 3d 30 29 20 61 6e 64 20 69 intKey==0) and i
23b00 73 20 74 68 65 20 64 61 74 61 20 66 6f 72 0a 2a s the data for.*
23b10 2a 20 74 61 62 6c 65 20 62 74 72 65 65 73 20 28 * table btrees (
23b20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d 3d 31 pPage->intKey==1
23b30 29 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ). The number of
23b40 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 bytes of availa
23b50 62 6c 65 0a 2a 2a 20 6b 65 79 2f 64 61 74 61 20 ble.** key/data
23b60 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
23b70 2a 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 *pAmt. If *pAmt
23b80 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 76 61 ==0, then the va
23b90 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 lue.** returned
23ba0 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 76 61 will not be a va
23bb0 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a lid pointer..**.
23bc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
23bd0 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 is an optimizati
23be0 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f on. It is commo
23bf0 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 n for the entire
23c00 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61 key.** and data
23c10 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 6c to fit on the l
23c20 6f 63 61 6c 20 70 61 67 65 20 61 6e 64 20 66 6f ocal page and fo
23c30 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 6e 6f r there to be no
23c40 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 overflow.** pag
23c50 65 73 2e 20 20 57 68 65 6e 20 74 68 61 74 20 69 es. When that i
23c60 73 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 74 69 s so, this routi
23c70 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 ne can be used t
23c80 6f 20 61 63 63 65 73 73 20 74 68 65 0a 2a 2a 20 o access the.**
23c90 6b 65 79 20 61 6e 64 20 64 61 74 61 20 77 69 74 key and data wit
23ca0 68 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f hout making a co
23cb0 70 79 2e 20 20 49 66 20 74 68 65 20 6b 65 79 20 py. If the key
23cc0 61 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 69 6c and/or data spil
23cd0 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 ls.** onto overf
23ce0 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 65 6e 20 low pages, then
23cf0 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 accessPayload()
23d00 6d 75 73 74 20 62 65 20 75 73 65 64 20 74 6f 20 must be used to
23d10 72 65 61 73 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 reassemble.** th
23d20 65 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 20 63 e key/data and c
23d30 6f 70 79 20 69 74 20 69 6e 74 6f 20 61 20 70 72 opy it into a pr
23d40 65 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 eallocated buffe
23d50 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 r..**.** The poi
23d60 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 62 79 nter returned by
23d70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f this routine lo
23d80 6f 6b 73 20 64 69 72 65 63 74 6c 79 20 69 6e 74 oks directly int
23d90 6f 20 74 68 65 20 63 61 63 68 65 64 0a 2a 2a 20 o the cached.**
23da0 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
23db0 62 61 73 65 2e 20 20 54 68 65 20 64 61 74 61 20 base. The data
23dc0 6d 69 67 68 74 20 63 68 61 6e 67 65 20 6f 72 20 might change or
23dd0 6d 6f 76 65 20 74 68 65 20 6e 65 78 74 20 74 69 move the next ti
23de0 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 65 20 me.** any btree
23df0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
23e00 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e d..*/.static con
23e10 73 74 20 76 6f 69 64 20 2a 66 65 74 63 68 50 61 st void *fetchPa
23e20 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f yload(. BtCurso
23e30 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a r *pCur, /*
23e40 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
23e50 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 to entry to rea
23e60 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 d from */. u32
23e70 2a 70 41 6d 74 20 20 20 20 20 20 20 20 20 20 20 *pAmt
23e80 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 /* Write the nu
23e90 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
23ea0 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a e bytes here */.
23eb0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 ){. assert( pCu
23ec0 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 50 r!=0 && pCur->iP
23ed0 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e age>=0 && pCur->
23ee0 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
23ef0 67 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ge]);. assert(
23f00 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
23f10 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
23f20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
23f30 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
23f40 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
23f50 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
23f60 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
23f70 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
23f80 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
23f90 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
23fa0 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
23fb0 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
23fc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
23fd0 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3e 30 20 29 ->info.nSize>0 )
23fe0 3b 0a 20 20 2a 70 41 6d 74 20 3d 20 70 43 75 72 ;. *pAmt = pCur
23ff0 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 ->info.nLocal;.
24000 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 return (void*)p
24010 43 75 72 2d 3e 69 6e 66 6f 2e 70 50 61 79 6c 6f Cur->info.pPaylo
24020 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f ad;.}.../*.** Fo
24030 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 r the entry that
24040 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 cursor pCur is
24050 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e point to, return
24060 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65 as.** many byte
24070 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20 s of the key or
24080 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69 data as are avai
24090 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 lable on the loc
240a0 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 al.** b-tree pag
240b0 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75 e. Write the nu
240c0 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
240d0 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41 e bytes into *pA
240e0 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f mt..**.** The po
240f0 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69 inter returned i
24100 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68 s ephemeral. Th
24110 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d e key/data may m
24120 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73 ove.** or be des
24130 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65 troyed on the ne
24140 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42 xt call to any B
24150 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a tree routine,.**
24160 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 including calls
24170 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65 from other thre
24180 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 ads against the
24190 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48 same cache..** H
241a0 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e ence, a mutex on
241b0 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68 the BtShared sh
241c0 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69 ould be held pri
241d0 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a or to calling.**
241e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
241f0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
24200 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67 nes is used to g
24210 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20 et quick access
24220 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a to key and data.
24230 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e ** in the common
24240 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f case where no o
24250 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 verflow pages ar
24260 65 20 75 73 65 64 2e 0a 2a 2f 0a 63 6f 6e 73 74 e used..*/.const
24270 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 void *sqlite3Bt
24280 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 reeKeyFetch(BtCu
24290 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 rsor *pCur, u32
242a0 2a 70 41 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e *pAmt){. return
242b0 20 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 fetchPayload(pC
242c0 75 72 2c 20 70 41 6d 74 29 3b 0a 7d 0a 63 6f 6e ur, pAmt);.}.con
242d0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
242e0 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42 BtreeDataFetch(B
242f0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
24300 33 32 20 2a 70 41 6d 74 29 7b 0a 20 20 72 65 74 32 *pAmt){. ret
24310 75 72 6e 20 66 65 74 63 68 50 61 79 6c 6f 61 64 urn fetchPayload
24320 28 70 43 75 72 2c 20 70 41 6d 74 29 3b 0a 7d 0a (pCur, pAmt);.}.
24330 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
24340 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 cursor down to
24350 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 a new child page
24360 2e 20 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 . The newPgno a
24370 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a rgument is the.*
24380 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 * page number of
24390 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 the child page
243a0 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2a 0a 2a to move to..**.*
243b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
243c0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 returns SQLITE_C
243d0 4f 52 52 55 50 54 20 69 66 20 74 68 65 20 70 61 ORRUPT if the pa
243e0 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 20 ge-header flags
243f0 66 69 65 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 field of.** the
24400 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 64 new child page d
24410 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 oes not match th
24420 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 e flags field of
24430 20 74 68 65 20 70 61 72 65 6e 74 20 28 69 2e 65 the parent (i.e
24440 2e 0a 2a 2a 20 69 66 20 61 6e 20 69 6e 74 6b 65 ..** if an intke
24450 79 20 70 61 67 65 20 61 70 70 65 61 72 73 20 74 y page appears t
24460 6f 20 62 65 20 74 68 65 20 70 61 72 65 6e 74 20 o be the parent
24470 6f 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 of a non-intkey
24480 70 61 67 65 2c 20 6f 72 0a 2a 2a 20 76 69 63 65 page, or.** vice
24490 2d 76 65 72 73 61 29 2e 0a 2a 2f 0a 73 74 61 74 -versa)..*/.stat
244a0 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68 69 ic int moveToChi
244b0 6c 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ld(BtCursor *pCu
244c0 72 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f 29 7b r, u32 newPgno){
244d0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
244e0 20 69 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 i = pCur->iPage
244f0 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 ;. MemPage *pNe
24500 77 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 wPage;. BtShare
24510 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 d *pBt = pCur->p
24520 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 Bt;.. assert( c
24530 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
24540 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
24550 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
24560 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
24570 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
24580 3e 69 50 61 67 65 3c 42 54 43 55 52 53 4f 52 5f >iPage<BTCURSOR_
24590 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 20 61 MAX_DEPTH );. a
245a0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 ssert( pCur->iPa
245b0 67 65 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 ge>=0 );. if( p
245c0 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 Cur->iPage>=(BTC
245d0 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d URSOR_MAX_DEPTH-
245e0 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 1) ){. return
245f0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
24600 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d BKPT;. }. rc =
24610 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
24620 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 pBt, newPgno, &p
24630 4e 65 77 50 61 67 65 2c 0a 20 20 20 20 20 20 20 NewPage,.
24640 20 20 20 20 20 20 20 20 28 70 43 75 72 2d 3e 63 (pCur->c
24650 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 57 urFlags & BTCF_W
24660 72 69 74 65 46 6c 61 67 29 3d 3d 30 20 3f 20 50 riteFlag)==0 ? P
24670 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c AGER_GET_READONL
24680 59 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 72 63 Y : 0);. if( rc
24690 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
246a0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 pCur->apPage[i+1
246b0 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 ] = pNewPage;.
246c0 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d pCur->aiIdx[i+1]
246d0 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 = 0;. pCur->iP
246e0 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e age++;.. pCur->
246f0 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
24700 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 pCur->curFlags
24710 20 26 3d 20 7e 28 42 54 43 46 5f 56 61 6c 69 64 &= ~(BTCF_Valid
24720 4e 4b 65 79 7c 42 54 43 46 5f 56 61 6c 69 64 4f NKey|BTCF_ValidO
24730 76 66 6c 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 vfl);. if( pNew
24740 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c Page->nCell<1 ||
24750 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 pNewPage->intKe
24760 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b y!=pCur->apPage[
24770 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 i]->intKey ){.
24780 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
24790 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
247a0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
247b0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f E_OK;.}..#if 0./
247c0 2a 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e *.** Page pParen
247d0 74 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c t is an internal
247e0 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 (non-leaf) tree
247f0 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 page. This func
24800 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 tion .** asserts
24810 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 that page numbe
24820 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 r iChild is the
24830 6c 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 left-child if th
24840 65 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c e iIdx'th.** cel
24850 6c 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e l in page pParen
24860 74 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 t. Or, if iIdx i
24870 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 s equal to the t
24880 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a otal number of.*
24890 2a 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 * cells in pPare
248a0 6e 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 nt, that page nu
248b0 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 mber iChild is t
248c0 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f he right-child o
248d0 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a f.** the page..*
248e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 /.static void as
248f0 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 sertParentIndex(
24900 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 MemPage *pParent
24910 2c 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f , int iIdx, Pgno
24920 20 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 iChild){. asse
24930 72 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e rt( iIdx<=pParen
24940 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 t->nCell );. if
24950 28 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d ( iIdx==pParent-
24960 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 >nCell ){. as
24970 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 sert( get4byte(&
24980 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 pParent->aData[p
24990 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 Parent->hdrOffse
249a0 74 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b t+8])==iChild );
249b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
249c0 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 sert( get4byte(f
249d0 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c indCell(pParent,
249e0 20 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 iIdx))==iChild
249f0 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 );. }.}.#else.#
24a00 20 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 define assertP
24a10 61 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a arentIndex(x,y,z
24a20 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ) .#endif../*.**
24a30 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
24a40 20 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e up to the paren
24a50 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 t page..**.** pC
24a60 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 ur->idx is set t
24a70 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 o the cell index
24a80 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
24a90 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f he pointer.** to
24aa0 20 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 the page we are
24ab0 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 coming from. I
24ac0 66 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 f we are coming
24ad0 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 from the.** righ
24ae0 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 t-most child pag
24af0 65 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 e then pCur->idx
24b00 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d is set to one m
24b10 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 ore than.** the
24b20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 largest cell ind
24b30 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ex..*/.static vo
24b40 69 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 id moveToParent(
24b50 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
24b60 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
24b70 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
24b80 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
24b90 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
24ba0 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
24bb0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 ssert( pCur->iPa
24bc0 67 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 ge>0 );. assert
24bd0 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
24be0 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 0a Cur->iPage] );..
24bf0 20 20 2f 2a 20 55 50 44 41 54 45 3a 20 49 74 20 /* UPDATE: It
24c00 69 73 20 61 63 74 75 61 6c 6c 79 20 70 6f 73 73 is actually poss
24c10 69 62 6c 65 20 66 6f 72 20 74 68 65 20 63 6f 6e ible for the con
24c20 64 69 74 69 6f 6e 20 74 65 73 74 65 64 20 62 79 dition tested by
24c30 20 74 68 65 20 61 73 73 65 72 74 0a 20 20 2a 2a the assert. **
24c40 20 62 65 6c 6f 77 20 74 6f 20 62 65 20 75 6e 74 below to be unt
24c50 72 75 65 20 69 66 20 74 68 65 20 64 61 74 61 62 rue if the datab
24c60 61 73 65 20 66 69 6c 65 20 69 73 20 63 6f 72 72 ase file is corr
24c70 75 70 74 2e 20 54 68 69 73 20 63 61 6e 20 6f 63 upt. This can oc
24c80 63 75 72 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 cur if. ** one
24c90 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 64 69 66 cursor has modif
24ca0 69 65 64 20 70 61 67 65 20 70 50 61 72 65 6e 74 ied page pParent
24cb0 20 77 68 69 6c 65 20 61 20 72 65 66 65 72 65 6e while a referen
24cc0 63 65 20 74 6f 20 69 74 20 69 73 20 68 65 6c 64 ce to it is held
24cd0 20 0a 20 20 2a 2a 20 62 79 20 61 20 73 65 63 6f . ** by a seco
24ce0 6e 64 20 63 75 72 73 6f 72 2e 20 57 68 69 63 68 nd cursor. Which
24cf0 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
24d00 20 69 66 20 61 20 73 69 6e 67 6c 65 20 70 61 67 if a single pag
24d10 65 20 69 73 20 6c 69 6e 6b 65 64 0a 20 20 2a 2a e is linked. **
24d20 20 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61 6e 20 into more than
24d30 6f 6e 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 one b-tree struc
24d40 74 75 72 65 20 69 6e 20 61 20 63 6f 72 72 75 70 ture in a corrup
24d50 74 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a t database. */.
24d60 23 69 66 20 30 0a 20 20 61 73 73 65 72 74 50 61 #if 0. assertPa
24d70 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 70 rentIndex(. p
24d80 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
24d90 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 ->iPage-1], .
24da0 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
24db0 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 r->iPage-1], .
24dc0 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 pCur->apPage[p
24dd0 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 6e Cur->iPage]->pgn
24de0 6f 0a 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 o. );.#endif.
24df0 74 65 73 74 63 61 73 65 28 20 70 43 75 72 2d 3e testcase( pCur->
24e00 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
24e10 65 2d 31 5d 20 3e 20 70 43 75 72 2d 3e 61 70 50 e-1] > pCur->apP
24e20 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d age[pCur->iPage-
24e30 31 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 1]->nCell );..
24e40 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
24e50 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
24e60 50 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e Page]);. pCur->
24e70 69 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d iPage--;. pCur-
24e80 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
24e90 0a 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 . pCur->curFlag
24ea0 73 20 26 3d 20 7e 28 42 54 43 46 5f 56 61 6c 69 s &= ~(BTCF_Vali
24eb0 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 6c 69 64 dNKey|BTCF_Valid
24ec0 4f 76 66 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Ovfl);.}../*.**
24ed0 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
24ee0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
24ef0 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 root page of its
24f00 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
24f10 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
24f20 74 61 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 table has a virt
24f30 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 ual root page, t
24f40 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
24f50 73 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 s moved to point
24f60 0a 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 .** to the virtu
24f70 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 al root page ins
24f80 74 65 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 tead of the actu
24f90 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 al root page. A
24fa0 74 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 table has a.** v
24fb0 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 irtual root page
24fc0 20 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c when the actual
24fd0 20 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 root page conta
24fe0 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 ins no cells and
24ff0 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 a .** single ch
25000 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 ild page. This c
25010 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 an only happen w
25020 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f ith the table ro
25030 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a oted at page 1..
25040 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 **.** If the b-t
25050 72 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73 ree structure is
25060 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 empty, the curs
25070 6f 72 20 73 74 61 74 65 20 69 73 20 73 65 74 20 or state is set
25080 74 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e to .** CURSOR_IN
25090 56 41 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 VALID. Otherwise
250a0 2c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 , the cursor is
250b0 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 set to point to
250c0 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c the first.** cel
250d0 6c 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 l located on the
250e0 20 72 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 root (or virtua
250f0 6c 20 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 l root) page and
25100 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 the cursor stat
25110 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 e.** is set to C
25120 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a URSOR_VALID..**.
25130 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 ** If this funct
25140 69 6f 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 ion returns succ
25150 65 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 essfully, it may
25160 20 62 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 be assumed that
25170 20 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 the.** page-hea
25180 64 65 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 der flags indica
25190 74 65 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 te that the [vir
251a0 74 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 tual] root-page
251b0 69 73 20 74 68 65 20 65 78 70 65 63 74 65 64 20 is the expected
251c0 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 .** kind of b-tr
251d0 65 65 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 ee page (i.e. if
251e0 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 when opening th
251f0 65 20 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c e cursor the cal
25200 6c 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 ler did not.** s
25210 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f pecify a KeyInfo
25220 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66 structure the f
25230 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74 lags byte is set
25240 20 74 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 to 0x05 or 0x0D
25250 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 ,.** indicating
25260 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 a table b-tree,
25270 6f 72 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 or if the caller
25280 20 64 69 64 20 73 70 65 63 69 66 79 20 61 20 4b did specify a K
25290 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 eyInfo .** struc
252a0 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 ture the flags b
252b0 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 yte is set to 0x
252c0 30 32 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 02 or 0x0A, indi
252d0 63 61 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a cating an index.
252e0 2a 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 ** b-tree)..*/.s
252f0 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
25300 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 Root(BtCursor *p
25310 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 Cur){. MemPage
25320 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 *pRoot;. int rc
25330 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
25340 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
25350 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
25360 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 );. assert( CUR
25370 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 55 SOR_INVALID < CU
25380 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
25390 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 );. assert( CU
253a0 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 43 RSOR_VALID < C
253b0 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
253c0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 K );. assert( C
253d0 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20 URSOR_FAULT >
253e0 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
253f0 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
25400 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 ->eState>=CURSOR
25410 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b 0a _REQUIRESEEK ){.
25420 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 if( pCur->eS
25430 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
25440 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 LT ){. asse
25450 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 rt( pCur->skipNe
25460 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b xt!=SQLITE_OK );
25470 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 . return pC
25480 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 ur->skipNext;.
25490 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
254a0 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
254b0 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 pCur);. }.. if
254c0 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 ( pCur->iPage>=0
254d0 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 ){. while( p
254e0 43 75 72 2d 3e 69 50 61 67 65 20 29 20 72 65 6c Cur->iPage ) rel
254f0 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
25500 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
25510 65 2d 2d 5d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 e--]);. }else i
25520 66 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f f( pCur->pgnoRoo
25530 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 75 72 t==0 ){. pCur
25540 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
25550 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 72 R_INVALID;. r
25560 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
25570 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
25580 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
25590 65 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e e(pCur->pBtree->
255a0 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 pBt, pCur->pgnoR
255b0 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 oot, &pCur->apPa
255c0 67 65 5b 30 5d 2c 0a 20 20 20 20 20 20 20 20 20 ge[0],.
255d0 20 20 20 20 20 20 20 20 28 70 43 75 72 2d 3e 63 (pCur->c
255e0 75 72 46 6c 61 67 73 20 26 20 42 54 43 46 5f 57 urFlags & BTCF_W
255f0 72 69 74 65 46 6c 61 67 29 3d 3d 30 20 3f 20 50 riteFlag)==0 ? P
25600 41 47 45 52 5f 47 45 54 5f 52 45 41 44 4f 4e 4c AGER_GET_READONL
25610 59 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 Y : 0);. if(
25620 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
25630 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 . pCur->eSt
25640 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
25650 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75 ALID;. retu
25660 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
25670 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 pCur->iPage = 0
25680 3b 0a 20 20 7d 0a 20 20 70 52 6f 6f 74 20 3d 20 ;. }. pRoot =
25690 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b pCur->apPage[0];
256a0 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 . assert( pRoot
256b0 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 ->pgno==pCur->pg
256c0 6e 6f 52 6f 6f 74 20 29 3b 0a 0a 20 20 2f 2a 20 noRoot );.. /*
256d0 49 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 If pCur->pKeyInf
256e0 6f 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 o is not NULL, t
256f0 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 hen the caller t
25700 68 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 hat opened this
25710 63 75 72 73 6f 72 0a 20 20 2a 2a 20 65 78 70 65 cursor. ** expe
25720 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 cted to open it
25730 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 on an index b-tr
25740 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ee. Otherwise, i
25750 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 f pKeyInfo is.
25760 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61 6c ** NULL, the cal
25770 6c 65 72 20 65 78 70 65 63 74 73 20 61 20 74 61 ler expects a ta
25780 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20 74 ble b-tree. If t
25790 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 63 his is not the c
257a0 61 73 65 2c 0a 20 20 2a 2a 20 72 65 74 75 72 6e ase,. ** return
257b0 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 an SQLITE_CORRU
257c0 50 54 20 65 72 72 6f 72 2e 20 0a 20 20 2a 2a 0a PT error. . **.
257d0 20 20 2a 2a 20 45 61 72 6c 69 65 72 20 76 65 72 ** Earlier ver
257e0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
257f0 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 69 assumed that thi
25800 73 20 74 65 73 74 20 63 6f 75 6c 64 20 6e 6f 74 s test could not
25810 20 66 61 69 6c 0a 20 20 2a 2a 20 69 66 20 74 68 fail. ** if th
25820 65 20 72 6f 6f 74 20 70 61 67 65 20 77 61 73 20 e root page was
25830 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 77 already loaded w
25840 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
25850 6e 20 77 61 73 20 63 61 6c 6c 65 64 20 28 69 2e n was called (i.
25860 65 2e 0a 20 20 2a 2a 20 69 66 20 70 43 75 72 2d e.. ** if pCur-
25870 3e 69 50 61 67 65 3e 3d 30 29 2e 20 42 75 74 20 >iPage>=0). But
25880 74 68 69 73 20 69 73 20 6e 6f 74 20 73 6f 20 69 this is not so i
25890 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
258a0 73 20 63 6f 72 72 75 70 74 65 64 20 0a 20 20 2a s corrupted . *
258b0 2a 20 69 6e 20 73 75 63 68 20 61 20 77 61 79 20 * in such a way
258c0 74 68 61 74 20 70 61 67 65 20 70 52 6f 6f 74 20 that page pRoot
258d0 69 73 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 61 is linked into a
258e0 20 73 65 63 6f 6e 64 20 62 2d 74 72 65 65 20 74 second b-tree t
258f0 61 62 6c 65 20 0a 20 20 2a 2a 20 28 6f 72 20 74 able . ** (or t
25900 68 65 20 66 72 65 65 6c 69 73 74 29 2e 20 20 2a he freelist). *
25910 2f 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f /. assert( pRoo
25920 74 2d 3e 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 t->intKey==1 ||
25930 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 3d 3d 30 pRoot->intKey==0
25940 20 29 3b 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d );. if( pRoot-
25950 3e 69 73 49 6e 69 74 3d 3d 30 20 7c 7c 20 28 70 >isInit==0 || (p
25960 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 Cur->pKeyInfo==0
25970 29 21 3d 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 )!=pRoot->intKey
25980 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
25990 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
259a0 50 54 3b 0a 20 20 7d 0a 0a 20 20 70 43 75 72 2d PT;. }.. pCur-
259b0 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 >aiIdx[0] = 0;.
259c0 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
259d0 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 e = 0;. pCur->c
259e0 75 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 urFlags &= ~(BTC
259f0 46 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f 56 61 F_AtLast|BTCF_Va
25a00 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 6c lidNKey|BTCF_Val
25a10 69 64 4f 76 66 6c 29 3b 0a 0a 20 20 69 66 28 20 idOvfl);.. if(
25a20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 20 29 pRoot->nCell>0 )
25a30 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 {. pCur->eSta
25a40 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 te = CURSOR_VALI
25a50 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 D;. }else if( !
25a60 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 pRoot->leaf ){.
25a70 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b Pgno subpage;
25a80 0a 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e . if( pRoot->
25a90 70 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e pgno!=1 ) return
25aa0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
25ab0 42 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 BKPT;. subpag
25ac0 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 e = get4byte(&pR
25ad0 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 oot->aData[pRoot
25ae0 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
25af0 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 . pCur->eStat
25b00 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 e = CURSOR_VALID
25b10 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 ;. rc = moveT
25b20 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 oChild(pCur, sub
25b30 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a page);. }else{.
25b40 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
25b50 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
25b60 44 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 D;. }. return
25b70 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 rc;.}../*.** Mov
25b80 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 e the cursor dow
25b90 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f n to the left-mo
25ba0 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 st leaf entry be
25bb0 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 neath the.** ent
25bc0 72 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 ry to which it i
25bd0 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
25be0 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ting..**.** The
25bf0 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 left-most leaf i
25c00 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 s the one with t
25c10 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 he smallest key
25c20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 - the first.** i
25c30 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
25c40 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
25c50 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 moveToLeftmost(
25c60 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
25c70 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 . Pgno pgno;.
25c80 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
25c90 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 OK;. MemPage *p
25ca0 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
25cb0 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
25cc0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
25cd0 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
25ce0 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
25cf0 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d );. while( rc==
25d00 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 SQLITE_OK && !(p
25d10 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
25d20 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
25d30 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 )->leaf ){. a
25d40 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
25d50 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
25d60 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
25d70 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 pgno = get4b
25d80 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 yte(findCell(pPa
25d90 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b ge, pCur->aiIdx[
25da0 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a pCur->iPage]));.
25db0 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
25dc0 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 hild(pCur, pgno)
25dd0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
25de0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 c;.}../*.** Move
25df0 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e the cursor down
25e00 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f to the right-mo
25e10 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 st leaf entry be
25e20 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 neath the.** pag
25e30 65 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 e to which it is
25e40 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
25e50 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 ing. Notice the
25e60 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 difference.** b
25e70 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 etween moveToLef
25e80 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 tmost() and move
25e90 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 ToRightmost().
25ea0 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 moveToLeftmost()
25eb0 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 .** finds the le
25ec0 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 ft-most entry be
25ed0 6e 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 neath the *entry
25ee0 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f * whereas moveTo
25ef0 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 Rightmost().** f
25f00 69 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d inds the right-m
25f10 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 ost entry beneat
25f20 68 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a h the *page*..**
25f30 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f .** The right-mo
25f40 73 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 st entry is the
25f50 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 one with the lar
25f60 67 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c gest key - the l
25f70 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 ast.** key in as
25f80 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a cending order..*
25f90 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 /.static int mov
25fa0 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 eToRightmost(BtC
25fb0 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
25fc0 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 Pgno pgno;. int
25fd0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
25fe0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
25ff0 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 e = 0;.. assert
26000 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
26010 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
26020 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
26030 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
26040 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 21 28 70 );. while( !(p
26050 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
26060 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
26070 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 )->leaf ){. p
26080 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 gno = get4byte(&
26090 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
260a0 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
260b0 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 );. pCur->aiI
260c0 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
260d0 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
260e0 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
260f0 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 hild(pCur, pgno)
26100 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
26110 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
26120 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
26130 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 ->iPage] = pPage
26140 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 61 73 73 ->nCell-1;. ass
26150 65 72 74 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e ert( pCur->info.
26160 6e 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 61 73 nSize==0 );. as
26170 73 65 72 74 28 20 28 70 43 75 72 2d 3e 63 75 72 sert( (pCur->cur
26180 46 6c 61 67 73 20 26 20 42 54 43 46 5f 56 61 6c Flags & BTCF_Val
26190 69 64 4e 4b 65 79 29 3d 3d 30 20 29 3b 0a 20 20 idNKey)==0 );.
261a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
261b0 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 ;.}../* Move the
261c0 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 cursor to the f
261d0 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 irst entry in th
261e0 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e e table. Return
261f0 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e SQLITE_OK.** on
26200 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a success. Set *
26210 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 pRes to 0 if the
26220 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 cursor actually
26230 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 points to somet
26240 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a hing.** or set *
26250 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 pRes to 1 if the
26260 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e table is empty.
26270 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 .*/.int sqlite3B
26280 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 73 treeFirst(BtCurs
26290 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
262a0 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
262b0 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
262c0 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
262d0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
262e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
262f0 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e d(pCur->pBtree->
26300 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
26310 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 rc = moveToRoot(
26320 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d pCur);. if( rc=
26330 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
26340 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
26350 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
26360 49 44 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ID ){. asse
26370 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f rt( pCur->pgnoRo
26380 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 ot==0 || pCur->a
26390 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
263a0 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a e]->nCell==0 );.
263b0 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b *pRes = 1;
263c0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
263d0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
263e0 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
263f0 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a ge]->nCell>0 );.
26400 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b *pRes = 0;
26410 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 . rc = move
26420 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 ToLeftmost(pCur)
26430 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
26440 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d turn rc;.}../* M
26450 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 ove the cursor t
26460 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
26470 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 in the table.
26480 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
26490 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 .** on success.
264a0 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 Set *pRes to 0
264b0 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 if the cursor ac
264c0 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f tually points to
264d0 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 something.** or
264e0 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 set *pRes to 1
264f0 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
26500 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 empty..*/.int sq
26510 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 lite3BtreeLast(B
26520 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
26530 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 nt *pRes){. int
26540 20 72 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 rc;. . assert(
26550 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
26560 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
26570 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
26580 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
26590 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
265a0 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
265b0 20 63 75 72 73 6f 72 20 61 6c 72 65 61 64 79 20 cursor already
265c0 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 points to the la
265d0 73 74 20 65 6e 74 72 79 2c 20 74 68 69 73 20 69 st entry, this i
265e0 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 s a no-op. */.
265f0 69 66 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 if( CURSOR_VALID
26600 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 26 ==pCur->eState &
26610 26 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 & (pCur->curFlag
26620 73 20 26 20 42 54 43 46 5f 41 74 4c 61 73 74 29 s & BTCF_AtLast)
26630 21 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 !=0 ){.#ifdef SQ
26640 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f LITE_DEBUG. /
26650 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 72 * This block ser
26660 76 65 73 20 74 6f 20 61 73 73 65 72 74 28 29 20 ves to assert()
26670 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
26680 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f 69 6e really does poin
26690 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 t . ** to the
266a0 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 last entry in t
266b0 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a 20 20 he b-tree. */.
266c0 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f int ii;. fo
266d0 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72 2d r(ii=0; ii<pCur-
266e0 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 >iPage; ii++){.
266f0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
26700 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d 70 43 r->aiIdx[ii]==pC
26710 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d 2d 3e ur->apPage[ii]->
26720 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 nCell );. }.
26730 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
26740 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
26750 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 ge]==pCur->apPag
26760 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
26770 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 20 20 61 nCell-1 );. a
26780 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
26790 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
267a0 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e 64 69 66 ->leaf );.#endif
267b0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
267c0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 TE_OK;. }.. rc
267d0 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
267e0 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ur);. if( rc==S
267f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
26800 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c if( CURSOR_INVAL
26810 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
26820 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
26830 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 ( pCur->pgnoRoot
26840 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 ==0 || pCur->apP
26850 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
26860 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 ->nCell==0 );.
26870 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 *pRes = 1;.
26880 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
26890 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
268a0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
268b0 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 ID );. *pRe
268c0 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 s = 0;. rc
268d0 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 = moveToRightmos
268e0 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 69 t(pCur);. i
268f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
26900 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 ){. pCur
26910 2d 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 ->curFlags |= BT
26920 43 46 5f 41 74 4c 61 73 74 3b 0a 20 20 20 20 20 CF_AtLast;.
26930 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
26940 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 26 pCur->curFlags &
26950 3d 20 7e 42 54 43 46 5f 41 74 4c 61 73 74 3b 0a = ~BTCF_AtLast;.
26960 20 20 20 20 20 20 7d 0a 20 20 20 0a 20 20 20 20 }. .
26970 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
26980 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 c;.}../* Move th
26990 65 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 e cursor so that
269a0 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e it points to an
269b0 20 65 6e 74 72 79 20 6e 65 61 72 20 74 68 65 20 entry near the
269c0 6b 65 79 20 0a 2a 2a 20 73 70 65 63 69 66 69 65 key .** specifie
269d0 64 20 62 79 20 70 49 64 78 4b 65 79 20 6f 72 20 d by pIdxKey or
269e0 69 6e 74 4b 65 79 2e 20 20 20 52 65 74 75 72 6e intKey. Return
269f0 20 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e a success code.
26a00 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b 45 .**.** For INTKE
26a10 59 20 74 61 62 6c 65 73 2c 20 74 68 65 20 69 6e Y tables, the in
26a20 74 4b 65 79 20 70 61 72 61 6d 65 74 65 72 20 69 tKey parameter i
26a30 73 20 75 73 65 64 2e 20 20 70 49 64 78 4b 65 79 s used. pIdxKey
26a40 20 0a 2a 2a 20 6d 75 73 74 20 62 65 20 4e 55 4c .** must be NUL
26a50 4c 2e 20 20 46 6f 72 20 69 6e 64 65 78 20 74 61 L. For index ta
26a60 62 6c 65 73 2c 20 70 49 64 78 4b 65 79 20 69 73 bles, pIdxKey is
26a70 20 75 73 65 64 20 61 6e 64 20 69 6e 74 4b 65 79 used and intKey
26a80 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a .** is ignored..
26a90 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 78 61 63 **.** If an exac
26aa0 74 20 6d 61 74 63 68 20 69 73 20 6e 6f 74 20 66 t match is not f
26ab0 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 ound, then the c
26ac0 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 0a ursor is always.
26ad0 2a 2a 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 ** left pointing
26ae0 20 61 74 20 61 20 6c 65 61 66 20 70 61 67 65 20 at a leaf page
26af0 77 68 69 63 68 20 77 6f 75 6c 64 20 68 6f 6c 64 which would hold
26b00 20 74 68 65 20 65 6e 74 72 79 20 69 66 20 69 74 the entry if it
26b10 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 65 6e 74 .** were present
26b20 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 69 . The cursor mi
26b30 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 ght point to an
26b40 65 6e 74 72 79 20 74 68 61 74 20 63 6f 6d 65 73 entry that comes
26b50 0a 2a 2a 20 62 65 66 6f 72 65 20 6f 72 20 61 66 .** before or af
26b60 74 65 72 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a ter the key..**.
26b70 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 69 73 ** An integer is
26b80 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 written into *p
26b90 52 65 73 20 77 68 69 63 68 20 69 73 20 74 68 65 Res which is the
26ba0 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 63 6f result of.** co
26bb0 6d 70 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20 mparing the key
26bc0 77 69 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 with the entry t
26bd0 6f 20 77 68 69 63 68 20 74 68 65 20 63 75 72 73 o which the curs
26be0 6f 72 20 69 73 20 0a 2a 2a 20 70 6f 69 6e 74 69 or is .** pointi
26bf0 6e 67 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e 67 ng. The meaning
26c00 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 of the integer
26c10 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 written into.**
26c20 2a 70 52 65 73 20 69 73 20 61 73 20 66 6f 6c 6c *pRes is as foll
26c30 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a ows:.**.** *
26c40 70 52 65 73 3c 30 20 20 20 20 20 20 54 68 65 20 pRes<0 The
26c50 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
26c60 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
26c70 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 try that.**
26c80 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 is
26c90 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e 74 smaller than int
26ca0 4b 65 79 2f 70 49 64 78 4b 65 79 20 6f 72 20 69 Key/pIdxKey or i
26cb0 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
26cc0 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 mpty.**
26cd0 20 20 20 20 20 20 20 20 20 61 6e 64 20 74 68 65 and the
26ce0 20 63 75 72 73 6f 72 20 69 73 20 74 68 65 72 65 cursor is there
26cf0 66 6f 72 65 20 6c 65 66 74 20 70 6f 69 6e 74 20 fore left point
26d00 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a to nothing..**.*
26d10 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d 30 20 20 * *pRes==0
26d20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 The cursor is
26d30 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
26d40 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a t an entry that.
26d50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
26d60 20 20 20 20 65 78 61 63 74 6c 79 20 6d 61 74 63 exactly matc
26d70 68 65 73 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b hes intKey/pIdxK
26d80 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 ey..**.** *p
26d90 52 65 73 3e 30 20 20 20 20 20 20 54 68 65 20 63 Res>0 The c
26da0 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f ursor is left po
26db0 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 inting at an ent
26dc0 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 ry that.**
26dd0 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 6c is l
26de0 61 72 67 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 arger than intKe
26df0 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2f y/pIdxKey..**.*/
26e00 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
26e10 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
26e20 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
26e30 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 r, /* T
26e40 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 he cursor to be
26e50 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 moved */. Unpac
26e60 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b kedRecord *pIdxK
26e70 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ey, /* Unpacked
26e80 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 index key */. i
26e90 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 64 intKey,
26ea0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
26eb0 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e able key */. in
26ec0 74 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 t biasRight,
26ed0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
26ee0 65 2c 20 62 69 61 73 20 74 68 65 20 73 65 61 72 e, bias the sear
26ef0 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 ch to the high e
26f00 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 nd */. int *pRe
26f10 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
26f20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 /* Write search
26f30 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f results here */
26f40 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
26f50 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 20 78 52 RecordCompare xR
26f60 65 63 6f 72 64 43 6f 6d 70 61 72 65 3b 0a 0a 20 ecordCompare;..
26f70 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
26f80 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
26f90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
26fa0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
26fb0 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
26fc0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
26fd0 73 65 72 74 28 20 70 52 65 73 20 29 3b 0a 20 20 sert( pRes );.
26fe0 61 73 73 65 72 74 28 20 28 70 49 64 78 4b 65 79 assert( (pIdxKey
26ff0 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 ==0)==(pCur->pKe
27000 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 yInfo==0) );..
27010 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 /* If the cursor
27020 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73 69 is already posi
27030 74 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70 6f tioned at the po
27040 69 6e 74 20 77 65 20 61 72 65 20 74 72 79 69 6e int we are tryin
27050 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 g. ** to move t
27060 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 o, then just ret
27070 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e urn without doin
27080 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 g any work */.
27090 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
270a0 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 ==CURSOR_VALID &
270b0 26 20 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 & (pCur->curFlag
270c0 73 20 26 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b s & BTCF_ValidNK
270d0 65 79 29 21 3d 30 0a 20 20 20 26 26 20 70 43 75 ey)!=0. && pCu
270e0 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
270f0 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20 69 tKey . ){. i
27100 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b f( pCur->info.nK
27110 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ey==intKey ){.
27120 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
27130 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
27140 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
27150 20 69 66 28 20 28 70 43 75 72 2d 3e 63 75 72 46 if( (pCur->curF
27160 6c 61 67 73 20 26 20 42 54 43 46 5f 41 74 4c 61 lags & BTCF_AtLa
27170 73 74 29 21 3d 30 20 26 26 20 70 43 75 72 2d 3e st)!=0 && pCur->
27180 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 info.nKey<intKey
27190 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 ){. *pRes
271a0 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 = -1;. retu
271b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
271c0 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 }. }.. if( p
271d0 49 64 78 4b 65 79 20 29 7b 0a 20 20 20 20 78 52 IdxKey ){. xR
271e0 65 63 6f 72 64 43 6f 6d 70 61 72 65 20 3d 20 73 ecordCompare = s
271f0 71 6c 69 74 65 33 56 64 62 65 46 69 6e 64 43 6f qlite3VdbeFindCo
27200 6d 70 61 72 65 28 70 49 64 78 4b 65 79 29 3b 0a mpare(pIdxKey);.
27210 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 65 72 72 pIdxKey->err
27220 43 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 61 73 Code = 0;. as
27230 73 65 72 74 28 20 70 49 64 78 4b 65 79 2d 3e 64 sert( pIdxKey->d
27240 65 66 61 75 6c 74 5f 72 63 3d 3d 31 20 0a 20 20 efault_rc==1 .
27250 20 20 20 20 20 20 20 7c 7c 20 70 49 64 78 4b 65 || pIdxKe
27260 79 2d 3e 64 65 66 61 75 6c 74 5f 72 63 3d 3d 30 y->default_rc==0
27270 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 49 . || pI
27280 64 78 4b 65 79 2d 3e 64 65 66 61 75 6c 74 5f 72 dxKey->default_r
27290 63 3d 3d 2d 31 0a 20 20 20 20 29 3b 0a 20 20 7d c==-1. );. }
272a0 65 6c 73 65 7b 0a 20 20 20 20 78 52 65 63 6f 72 else{. xRecor
272b0 64 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 2f 2a dCompare = 0; /*
272c0 20 41 6c 6c 20 6b 65 79 73 20 61 72 65 20 69 6e All keys are in
272d0 74 65 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 0a 20 tegers */. }..
272e0 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 rc = moveToRoot
272f0 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
27300 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
27310 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 c;. }. assert(
27320 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d pCur->pgnoRoot=
27330 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 =0 || pCur->apPa
27340 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 ge[pCur->iPage]
27350 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
27360 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c r->pgnoRoot==0 |
27370 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 | pCur->apPage[p
27380 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 Cur->iPage]->isI
27390 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nit );. assert(
273a0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
273b0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c URSOR_INVALID ||
273c0 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
273d0 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
273e0 6c 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 43 75 l>0 );. if( pCu
273f0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
27400 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 R_INVALID ){.
27410 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 *pRes = -1;.
27420 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 assert( pCur->p
27430 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 gnoRoot==0 || pC
27440 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
27450 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d >iPage]->nCell==
27460 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 );. return
27470 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
27480 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
27490 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
274a0 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 || pIdxKey );.
274b0 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e for(;;){. in
274c0 74 20 6c 77 72 2c 20 75 70 72 2c 20 69 64 78 2c t lwr, upr, idx,
274d0 20 63 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c c;. Pgno chl
274e0 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 dPg;. MemPage
274f0 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
27500 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
27510 67 65 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43 65 ge];. u8 *pCe
27520 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
27530 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
27540 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72 65 Pointer to curre
27550 6e 74 20 63 65 6c 6c 20 69 6e 20 70 50 61 67 65 nt cell in pPage
27560 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 */.. /* pPag
27570 65 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 e->nCell must be
27580 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 greater than ze
27590 72 6f 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 ro. If this is t
275a0 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 he root-page.
275b0 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 ** the cursor w
275c0 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 ould have been I
275d0 4e 56 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 NVALID above and
275e0 20 74 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f this for(;;) lo
275f0 6f 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 op. ** not ru
27600 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f n. If this is no
27610 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c t the root-page,
27620 20 74 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f then the moveTo
27630 43 68 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a Child() routine.
27640 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 ** would hav
27650 65 20 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 e already detect
27660 65 64 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e ed db corruption
27670 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 . Similarly, pPa
27680 67 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 ge must. ** b
27690 65 20 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 e the right kind
276a0 20 28 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 (index or table
276b0 29 20 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 ) of b-tree page
276c0 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 . Otherwise.
276d0 2a 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 ** a moveToChild
276e0 28 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 () or moveToRoot
276f0 28 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 () call would ha
27700 76 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 ve detected corr
27710 75 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 uption. */.
27720 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
27730 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 Cell>0 );. as
27740 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 sert( pPage->int
27750 4b 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 Key==(pIdxKey==0
27760 29 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 ) );. lwr = 0
27770 3b 0a 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 ;. upr = pPag
27780 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 e->nCell-1;.
27790 61 73 73 65 72 74 28 20 62 69 61 73 52 69 67 68 assert( biasRigh
277a0 74 3d 3d 30 20 7c 7c 20 62 69 61 73 52 69 67 68 t==0 || biasRigh
277b0 74 3d 3d 31 20 29 3b 0a 20 20 20 20 69 64 78 20 t==1 );. idx
277c0 3d 20 75 70 72 3e 3e 28 31 2d 62 69 61 73 52 69 = upr>>(1-biasRi
277d0 67 68 74 29 3b 20 2f 2a 20 69 64 78 20 3d 20 62 ght); /* idx = b
277e0 69 61 73 52 69 67 68 74 20 3f 20 75 70 72 20 3a iasRight ? upr :
277f0 20 28 6c 77 72 2b 75 70 72 29 2f 32 3b 20 2a 2f (lwr+upr)/2; */
27800 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
27810 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
27820 28 75 31 36 29 69 64 78 3b 0a 20 20 20 20 69 66 (u16)idx;. if
27830 28 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 ( xRecordCompare
27840 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 ==0 ){. for
27850 28 3b 3b 29 7b 0a 20 20 20 20 20 20 20 20 69 36 (;;){. i6
27860 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 4 nCellKey;.
27870 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
27880 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 Cell(pPage, idx)
27890 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 + pPage->childP
278a0 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 trSize;.
278b0 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
278c0 79 4c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 yLeaf ){.
278d0 20 20 20 77 68 69 6c 65 28 20 30 78 38 30 20 3c while( 0x80 <
278e0 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 20 29 7b 0a = *(pCell++) ){.
278f0 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
27900 70 43 65 6c 6c 3e 3d 70 50 61 67 65 2d 3e 61 44 pCell>=pPage->aD
27910 61 74 61 45 6e 64 20 29 20 72 65 74 75 72 6e 20 ataEnd ) return
27920 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
27930 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 7d KPT;. }
27940 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
27950 20 20 20 67 65 74 56 61 72 69 6e 74 28 70 43 65 getVarint(pCe
27960 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43 65 6c 6c ll, (u64*)&nCell
27970 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 69 66 Key);. if
27980 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 ( nCellKey<intKe
27990 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c y ){. l
279a0 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20 20 20 wr = idx+1;.
279b0 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 if( lwr>up
279c0 72 20 29 7b 20 63 20 3d 20 2d 31 3b 20 62 72 65 r ){ c = -1; bre
279d0 61 6b 3b 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 ak; }. }e
279e0 6c 73 65 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 lse if( nCellKey
279f0 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 >intKey ){.
27a00 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d 31 upr = idx-1
27a10 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
27a20 6c 77 72 3e 75 70 72 20 29 7b 20 63 20 3d 20 2b lwr>upr ){ c = +
27a30 31 3b 20 62 72 65 61 6b 3b 20 7d 0a 20 20 20 20 1; break; }.
27a40 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
27a50 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 assert( nCe
27a60 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 3b llKey==intKey );
27a70 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d . pCur-
27a80 3e 63 75 72 46 6c 61 67 73 20 7c 3d 20 42 54 43 >curFlags |= BTC
27a90 46 5f 56 61 6c 69 64 4e 4b 65 79 3b 0a 20 20 20 F_ValidNKey;.
27aa0 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 pCur->inf
27ab0 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 o.nKey = nCellKe
27ac0 79 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 y;. pCu
27ad0 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
27ae0 50 61 67 65 5d 20 3d 20 28 75 31 36 29 69 64 78 Page] = (u16)idx
27af0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
27b00 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
27b10 20 20 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 lwr
27b20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 = idx;.
27b30 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 6e goto moveto_n
27b40 65 78 74 5f 6c 61 79 65 72 3b 0a 20 20 20 20 20 ext_layer;.
27b50 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
27b60 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
27b70 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 0;. r
27b80 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
27b90 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
27ba0 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
27bb0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
27bc0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 }. ass
27bd0 65 72 74 28 20 6c 77 72 2b 75 70 72 3e 3d 30 20 ert( lwr+upr>=0
27be0 29 3b 0a 20 20 20 20 20 20 20 20 69 64 78 20 3d );. idx =
27bf0 20 28 6c 77 72 2b 75 70 72 29 3e 3e 31 3b 20 20 (lwr+upr)>>1;
27c00 2f 2a 20 69 64 78 20 3d 20 28 6c 77 72 2b 75 70 /* idx = (lwr+up
27c10 72 29 2f 32 3b 20 2a 2f 0a 20 20 20 20 20 20 7d r)/2; */. }
27c20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
27c30 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 for(;;){.
27c40 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 int nCell;.
27c50 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 pCell = fi
27c60 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 ndCell(pPage, id
27c70 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c x) + pPage->chil
27c80 64 50 74 72 53 69 7a 65 3b 0a 0a 20 20 20 20 20 dPtrSize;..
27c90 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 /* The maximu
27ca0 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 67 65 m supported page
27cb0 2d 73 69 7a 65 20 69 73 20 36 35 35 33 36 20 62 -size is 65536 b
27cc0 79 74 65 73 2e 20 54 68 69 73 20 6d 65 61 6e 73 ytes. This means
27cd0 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a that. **
27ce0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
27cf0 62 65 72 20 6f 66 20 72 65 63 6f 72 64 20 62 79 ber of record by
27d00 74 65 73 20 73 74 6f 72 65 64 20 6f 6e 20 61 6e tes stored on an
27d10 20 69 6e 64 65 78 20 42 2d 54 72 65 65 0a 20 20 index B-Tree.
27d20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 ** page is
27d30 20 6c 65 73 73 20 74 68 61 6e 20 31 36 33 38 34 less than 16384
27d40 20 62 79 74 65 73 20 61 6e 64 20 6d 61 79 20 62 bytes and may b
27d50 65 20 73 74 6f 72 65 64 20 61 73 20 61 20 32 2d e stored as a 2-
27d60 62 79 74 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 byte. **
27d70 76 61 72 69 6e 74 2e 20 54 68 69 73 20 69 6e 66 varint. This inf
27d80 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 ormation is used
27d90 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61 to attempt to a
27da0 76 6f 69 64 20 70 61 72 73 69 6e 67 20 0a 20 20 void parsing .
27db0 20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 ** the ent
27dc0 69 72 65 20 63 65 6c 6c 20 62 79 20 63 68 65 63 ire cell by chec
27dd0 6b 69 6e 67 20 66 6f 72 20 74 68 65 20 63 61 73 king for the cas
27de0 65 73 20 77 68 65 72 65 20 74 68 65 20 72 65 63 es where the rec
27df0 6f 72 64 20 69 73 20 0a 20 20 20 20 20 20 20 20 ord is .
27e00 2a 2a 20 73 74 6f 72 65 64 20 65 6e 74 69 72 65 ** stored entire
27e10 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 62 2d ly within the b-
27e20 74 72 65 65 20 70 61 67 65 20 62 79 20 69 6e 73 tree page by ins
27e30 70 65 63 74 69 6e 67 20 74 68 65 20 66 69 72 73 pecting the firs
27e40 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 32 20 t . ** 2
27e50 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 65 6c bytes of the cel
27e60 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 l.. */.
27e70 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 43 nCell = pC
27e80 65 6c 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 20 20 ell[0];.
27e90 69 66 28 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 if( nCell<=pPage
27ea0 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61 ->max1bytePayloa
27eb0 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f d ){. /
27ec0 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 * This branch ru
27ed0 6e 73 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 ns if the record
27ee0 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 74 -size field of t
27ef0 68 65 20 63 65 6c 6c 20 69 73 20 61 0a 20 20 20 he cell is a.
27f00 20 20 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 ** single
27f10 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64 byte varint and
27f20 20 74 68 65 20 72 65 63 6f 72 64 20 66 69 74 73 the record fits
27f30 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 entirely on the
27f40 20 6d 61 69 6e 0a 20 20 20 20 20 20 20 20 20 20 main.
27f50 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 ** b-tree page.
27f60 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74 65 */. te
27f70 73 74 63 61 73 65 28 20 70 43 65 6c 6c 2b 6e 43 stcase( pCell+nC
27f80 65 6c 6c 2b 31 3d 3d 70 50 61 67 65 2d 3e 61 44 ell+1==pPage->aD
27f90 61 74 61 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 ataEnd );.
27fa0 20 20 20 20 63 20 3d 20 78 52 65 63 6f 72 64 43 c = xRecordC
27fb0 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 ompare(nCell, (v
27fc0 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 oid*)&pCell[1],
27fd0 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 pIdxKey);.
27fe0 20 20 7d 65 6c 73 65 20 69 66 28 20 21 28 70 43 }else if( !(pC
27ff0 65 6c 6c 5b 31 5d 20 26 20 30 78 38 30 29 20 0a ell[1] & 0x80) .
28000 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6e 43 && (nC
28010 65 6c 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 30 78 ell = ((nCell&0x
28020 37 66 29 3c 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 7f)<<7) + pCell[
28030 31 5d 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 1])<=pPage->maxL
28040 6f 63 61 6c 0a 20 20 20 20 20 20 20 20 29 7b 0a ocal. ){.
28050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
28060 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 record-size fie
28070 6c 64 20 69 73 20 61 20 32 20 62 79 74 65 20 76 ld is a 2 byte v
28080 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 arint and the re
28090 63 6f 72 64 20 0a 20 20 20 20 20 20 20 20 20 20 cord .
280a0 2a 2a 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 ** fits entirely
280b0 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 on the main b-t
280c0 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 ree page. */.
280d0 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
280e0 28 20 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b 32 3d ( pCell+nCell+2=
280f0 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e 64 =pPage->aDataEnd
28100 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 );. c
28110 3d 20 78 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 = xRecordCompare
28120 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 (nCell, (void*)&
28130 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 pCell[2], pIdxKe
28140 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
28150 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
28160 54 68 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 The record flows
28170 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f over onto one o
28180 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 r more overflow
28190 70 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 pages. In.
281a0 20 20 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 ** this case
281b0 20 74 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 the whole cell
281c0 6e 65 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 needs to be pars
281d0 65 64 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c ed, a buffer all
281e0 6f 63 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 ocated.
281f0 20 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 ** and accessPa
28200 79 6c 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 yload() used to
28210 72 65 74 72 69 65 76 65 20 74 68 65 20 72 65 63 retrieve the rec
28220 6f 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 ord into the.
28230 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 ** buffer
28240 20 62 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f before VdbeReco
28250 72 64 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 rdCompare() can
28260 62 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 be called. */.
28270 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 void *pC
28280 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 ellKey;.
28290 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 u8 * const pCe
282a0 6c 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d llBody = pCell -
282b0 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
282c0 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 Size;.
282d0 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
282e0 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f r(pPage, pCellBo
282f0 64 79 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 dy, &pCur->info)
28300 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c ;. nCel
28310 6c 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 l = (int)pCur->i
28320 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 nfo.nKey;.
28330 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 pCellKey = s
28340 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 qlite3Malloc( nC
28350 65 6c 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ell );.
28360 20 69 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 if( pCellKey==0
28370 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
28380 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
28390 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 M;. g
283a0 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 oto moveto_finis
283b0 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 h;. }.
283c0 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 pCur->a
283d0 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
283e0 5d 20 3d 20 28 75 31 36 29 69 64 78 3b 0a 20 20 ] = (u16)idx;.
283f0 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 rc = acc
28400 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c essPayload(pCur,
28410 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 0, nCell, (unsi
28420 67 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c gned char*)pCell
28430 4b 65 79 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 Key, 2);.
28440 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
28450 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
28460 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b _free(pCellKey);
28470 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
28480 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b o moveto_finish;
28490 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
284a0 20 20 20 20 20 20 20 63 20 3d 20 78 52 65 63 6f c = xReco
284b0 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c rdCompare(nCell,
284c0 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b pCellKey, pIdxK
284d0 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 ey);. s
284e0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c qlite3_free(pCel
284f0 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d lKey);. }
28500 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
28510 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 70 . (p
28520 49 64 78 4b 65 79 2d 3e 65 72 72 43 6f 64 65 21 IdxKey->errCode!
28530 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 =SQLITE_CORRUPT
28540 7c 7c 20 63 3d 3d 30 29 0a 20 20 20 20 20 20 20 || c==0).
28550 20 20 26 26 20 28 70 49 64 78 4b 65 79 2d 3e 65 && (pIdxKey->e
28560 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 4e rrCode!=SQLITE_N
28570 4f 4d 45 4d 20 7c 7c 20 70 43 75 72 2d 3e 70 42 OMEM || pCur->pB
28580 74 72 65 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 tree->db->malloc
28590 46 61 69 6c 65 64 29 0a 20 20 20 20 20 20 20 20 Failed).
285a0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 );. if( c
285b0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <0 ){.
285c0 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20 20 lwr = idx+1;.
285d0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 }else if( c
285e0 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 >0 ){.
285f0 75 70 72 20 3d 20 69 64 78 2d 31 3b 0a 20 20 20 upr = idx-1;.
28600 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
28610 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63 3d assert( c=
28620 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 =0 );.
28630 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
28640 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
28650 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 _OK;. p
28660 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
28670 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 69 >iPage] = (u16)i
28680 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 dx;. if
28690 28 20 70 49 64 78 4b 65 79 2d 3e 65 72 72 43 6f ( pIdxKey->errCo
286a0 64 65 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 de ) rc = SQLITE
286b0 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 20 20 _CORRUPT;.
286c0 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f goto moveto_
286d0 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 finish;.
286e0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 77 }. if( lw
286f0 72 3e 75 70 72 20 29 20 62 72 65 61 6b 3b 0a 20 r>upr ) break;.
28700 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c assert( l
28710 77 72 2b 75 70 72 3e 3d 30 20 29 3b 0a 20 20 20 wr+upr>=0 );.
28720 20 20 20 20 20 69 64 78 20 3d 20 28 6c 77 72 2b idx = (lwr+
28730 75 70 72 29 3e 3e 31 3b 20 20 2f 2a 20 69 64 78 upr)>>1; /* idx
28740 20 3d 20 28 6c 77 72 2b 75 70 72 29 2f 32 20 2a = (lwr+upr)/2 *
28750 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a /. }. }.
28760 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 3d assert( lwr=
28770 3d 75 70 72 2b 31 20 7c 7c 20 28 70 50 61 67 65 =upr+1 || (pPage
28780 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 ->intKey && !pPa
28790 67 65 2d 3e 6c 65 61 66 29 20 29 3b 0a 20 20 20 ge->leaf) );.
287a0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
287b0 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 isInit );. if
287c0 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
287d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
287e0 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
287f0 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 >iPage]<pCur->ap
28800 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
28810 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 ]->nCell );.
28820 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
28830 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 ur->iPage] = (u1
28840 36 29 69 64 78 3b 0a 20 20 20 20 20 20 2a 70 52 6)idx;. *pR
28850 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 es = c;. rc
28860 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
28870 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f goto moveto_
28880 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 6d 6f finish;. }.mo
28890 76 65 74 6f 5f 6e 65 78 74 5f 6c 61 79 65 72 3a veto_next_layer:
288a0 0a 20 20 20 20 69 66 28 20 6c 77 72 3e 3d 70 50 . if( lwr>=pP
288b0 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 age->nCell ){.
288c0 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 chldPg = get
288d0 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
288e0 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
288f0 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65 fset+8]);. }e
28900 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 lse{. chldP
28910 67 20 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e g = get4byte(fin
28920 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77 72 dCell(pPage, lwr
28930 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 ));. }. pC
28940 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
28950 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 iPage] = (u16)lw
28960 72 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 r;. rc = move
28970 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 63 68 ToChild(pCur, ch
28980 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 ldPg);. if( r
28990 63 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 6d c ) break;. }.m
289a0 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 oveto_finish:.
289b0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
289c0 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 = 0;. pCur->cu
289d0 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 rFlags &= ~(BTCF
289e0 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f _ValidNKey|BTCF_
289f0 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 72 65 ValidOvfl);. re
28a00 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
28a10 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
28a20 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
28a30 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 not pointing at
28a40 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 an entry of the
28a50 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 table..**.** TRU
28a60 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e E will be return
28a70 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 ed after a call
28a80 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e to sqlite3BtreeN
28a90 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70 ext() moves.** p
28aa0 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74 ast the last ent
28ab0 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ry in the table
28ac0 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 or sqlite3BtreeP
28ad0 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 74 rev() moves past
28ae0 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 6e .** the first en
28af0 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 6c try. TRUE is al
28b00 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 so returned if t
28b10 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
28b20 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 y..*/.int sqlite
28b30 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 3BtreeEof(BtCurs
28b40 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 or *pCur){. /*
28b50 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 TODO: What if th
28b60 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 e cursor is in C
28b70 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
28b80 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 K but all table
28b90 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 entries. ** hav
28ba0 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 e been deleted?
28bb0 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 This API will ne
28bc0 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 ed to change to
28bd0 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
28be0 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c code. ** as wel
28bf0 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e l as the boolean
28c00 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 result value..
28c10 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 */. return (CU
28c20 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 RSOR_VALID!=pCur
28c30 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a ->eState);.}../*
28c40 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 .** Advance the
28c50 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 cursor to the ne
28c60 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 xt entry in the
28c70 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a database. If.**
28c80 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e successful then
28c90 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 set *pRes=0. I
28ca0 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 f the cursor.**
28cb0 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e was already poin
28cc0 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 ting to the last
28cd0 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
28ce0 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a tabase before.**
28cf0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
28d00 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 s called, then s
28d10 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2a 0a 2a et *pRes=1..**.*
28d20 2a 20 54 68 65 20 6d 61 69 6e 20 65 6e 74 72 79 * The main entry
28d30 20 70 6f 69 6e 74 20 69 73 20 73 71 6c 69 74 65 point is sqlite
28d40 33 42 74 72 65 65 4e 65 78 74 28 29 2e 20 20 54 3BtreeNext(). T
28d50 68 61 74 20 72 6f 75 74 69 6e 65 20 69 73 20 6f hat routine is o
28d60 70 74 69 6d 69 7a 65 64 0a 2a 2a 20 66 6f 72 20 ptimized.** for
28d70 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 the common case
28d80 6f 66 20 6d 65 72 65 6c 79 20 69 6e 63 72 65 6d of merely increm
28d90 65 6e 74 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 enting the cell
28da0 63 6f 75 6e 74 65 72 20 42 74 43 75 72 73 6f 72 counter BtCursor
28db0 2e 61 69 49 64 78 0a 2a 2a 20 74 6f 20 74 68 65 .aiIdx.** to the
28dc0 20 6e 65 78 74 20 63 65 6c 6c 20 6f 6e 20 74 68 next cell on th
28dd0 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 2e 20 e current page.
28de0 20 54 68 65 20 28 73 6c 6f 77 65 72 29 20 62 74 The (slower) bt
28df0 72 65 65 4e 65 78 74 28 29 20 68 65 6c 70 65 72 reeNext() helper
28e00 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 .** routine is c
28e10 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 20 69 73 alled when it is
28e20 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 6d 6f necessary to mo
28e30 76 65 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e ve to a differen
28e40 74 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 6f 20 t page or.** to
28e50 72 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 73 restore the curs
28e60 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 or..**.** The ca
28e70 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 77 lling function w
28e80 69 6c 6c 20 73 65 74 20 2a 70 52 65 73 20 74 6f ill set *pRes to
28e90 20 30 20 6f 72 20 31 2e 20 20 54 68 65 20 69 6e 0 or 1. The in
28ea0 69 74 69 61 6c 20 2a 70 52 65 73 20 76 61 6c 75 itial *pRes valu
28eb0 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 31 20 69 e.** will be 1 i
28ec0 66 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 69 f the cursor bei
28ed0 6e 67 20 73 74 65 70 70 65 64 20 63 6f 72 72 65 ng stepped corre
28ee0 73 70 6f 6e 64 73 20 74 6f 20 61 6e 20 53 51 4c sponds to an SQL
28ef0 20 69 6e 64 65 78 20 61 6e 64 0a 2a 2a 20 69 66 index and.** if
28f00 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f this routine co
28f10 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 73 6b uld have been sk
28f20 69 70 70 65 64 20 69 66 20 74 68 61 74 20 53 51 ipped if that SQ
28f30 4c 20 69 6e 64 65 78 20 68 61 64 20 62 65 65 6e L index had been
28f40 0a 2a 2a 20 61 20 75 6e 69 71 75 65 20 69 6e 64 .** a unique ind
28f50 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 ex. Otherwise t
28f60 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 68 he caller will h
28f70 61 76 65 20 73 65 74 20 2a 70 52 65 73 20 74 6f ave set *pRes to
28f80 20 7a 65 72 6f 2e 0a 2a 2a 20 5a 65 72 6f 20 69 zero..** Zero i
28f90 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 s the common cas
28fa0 65 2e 20 54 68 65 20 62 74 72 65 65 20 69 6d 70 e. The btree imp
28fb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 66 lementation is f
28fc0 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 0a 2a ree to use the.*
28fd0 2a 20 69 6e 69 74 69 61 6c 20 2a 70 52 65 73 20 * initial *pRes
28fe0 76 61 6c 75 65 20 61 73 20 61 20 68 69 6e 74 20 value as a hint
28ff0 74 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f to improve perfo
29000 72 6d 61 6e 63 65 2c 20 62 75 74 20 74 68 65 20 rmance, but the
29010 63 75 72 72 65 6e 74 0a 2a 2a 20 53 51 4c 69 74 current.** SQLit
29020 65 20 62 74 72 65 65 20 69 6d 70 6c 65 6d 65 6e e btree implemen
29030 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 2e tation does not.
29040 20 28 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 (Note that the
29050 63 6f 6d 64 62 32 20 62 74 72 65 65 0a 2a 2a 20 comdb2 btree.**
29060 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 implementation d
29070 6f 65 73 20 75 73 65 20 74 68 69 73 20 68 69 6e oes use this hin
29080 74 2c 20 68 6f 77 65 76 65 72 2e 29 0a 2a 2f 0a t, however.).*/.
29090 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 4e 4f static SQLITE_NO
290a0 49 4e 4c 49 4e 45 20 69 6e 74 20 62 74 72 65 65 INLINE int btree
290b0 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 Next(BtCursor *p
290c0 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b Cur, int *pRes){
290d0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
290e0 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 idx;. MemPage
290f0 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 *pPage;.. asser
29100 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
29110 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
29120 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 ssert( pCur->ski
29130 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 43 75 72 pNext==0 || pCur
29140 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
29150 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
29160 72 74 28 20 2a 70 52 65 73 3d 3d 30 20 29 3b 0a rt( *pRes==0 );.
29170 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
29180 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te!=CURSOR_VALID
29190 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
291a0 28 70 43 75 72 2d 3e 63 75 72 46 6c 61 67 73 20 (pCur->curFlags
291b0 26 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 66 6c & BTCF_ValidOvfl
291c0 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d )==0 );. rc =
291d0 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
291e0 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
291f0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
29200 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
29210 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
29220 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 if( CURSOR_INV
29230 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
29240 74 65 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 te ){. *pRe
29250 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 s = 1;. ret
29260 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
29270 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 }. if( pCu
29280 72 2d 3e 73 6b 69 70 4e 65 78 74 20 29 7b 0a 20 r->skipNext ){.
29290 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
292a0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
292b0 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d R_VALID || pCur-
292c0 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
292d0 53 4b 49 50 4e 45 58 54 20 29 3b 0a 20 20 20 20 SKIPNEXT );.
292e0 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
292f0 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 CURSOR_VALID;.
29300 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 73 if( pCur->s
29310 6b 69 70 4e 65 78 74 3e 30 20 29 7b 0a 20 20 20 kipNext>0 ){.
29320 20 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e pCur->skipN
29330 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 ext = 0;.
29340 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
29350 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
29360 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 pCur->skipNext
29370 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a = 0;. }. }..
29380 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
29390 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
293a0 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 ge];. idx = ++p
293b0 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
293c0 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 >iPage];. asser
293d0 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 t( pPage->isInit
293e0 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
293f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
29400 73 20 63 6f 72 72 75 70 74 2c 20 69 74 20 69 73 s corrupt, it is
29410 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 possible for th
29420 65 20 76 61 6c 75 65 20 6f 66 20 69 64 78 20 0a e value of idx .
29430 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c ** to be inval
29440 69 64 20 68 65 72 65 2e 20 54 68 69 73 20 63 61 id here. This ca
29450 6e 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 n only occur if
29460 61 20 73 65 63 6f 6e 64 20 63 75 72 73 6f 72 20 a second cursor
29470 6d 6f 64 69 66 69 65 73 0a 20 20 2a 2a 20 74 68 modifies. ** th
29480 65 20 70 61 67 65 20 77 68 69 6c 65 20 63 75 72 e page while cur
29490 73 6f 72 20 70 43 75 72 20 69 73 20 68 6f 6c 64 sor pCur is hold
294a0 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 20 ing a reference
294b0 74 6f 20 69 74 2e 20 57 68 69 63 68 20 63 61 6e to it. Which can
294c0 0a 20 20 2a 2a 20 6f 6e 6c 79 20 68 61 70 70 65 . ** only happe
294d0 6e 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 n if the databas
294e0 65 20 69 73 20 63 6f 72 72 75 70 74 20 69 6e 20 e is corrupt in
294f0 73 75 63 68 20 61 20 77 61 79 20 61 73 20 74 6f such a way as to
29500 20 6c 69 6e 6b 20 74 68 65 0a 20 20 2a 2a 20 70 link the. ** p
29510 61 67 65 20 69 6e 74 6f 20 6d 6f 72 65 20 74 68 age into more th
29520 61 6e 20 6f 6e 65 20 62 2d 74 72 65 65 20 73 74 an one b-tree st
29530 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 74 65 ructure. */. te
29540 73 74 63 61 73 65 28 20 69 64 78 3e 70 50 61 67 stcase( idx>pPag
29550 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 69 e->nCell );.. i
29560 66 28 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e f( idx>=pPage->n
29570 43 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 Cell ){. if(
29580 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
29590 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
295a0 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 oChild(pCur, get
295b0 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
295c0 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
295d0 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 fset+8]));.
295e0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
295f0 20 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 rc;. retur
29600 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 n moveToLeftmost
29610 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (pCur);. }.
29620 20 20 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 do{. if(
29630 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 pCur->iPage==0 )
29640 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 {. *pRes
29650 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 = 1;. pCu
29660 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
29670 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
29680 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
29690 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
296a0 20 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 moveToParent
296b0 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 (pCur);. pP
296c0 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
296d0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
296e0 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 . }while( pCu
296f0 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
29700 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 Page]>=pPage->nC
29710 65 6c 6c 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ell );. if( p
29720 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a Page->intKey ){.
29730 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c return sql
29740 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 ite3BtreeNext(pC
29750 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d ur, pRes);. }
29760 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 else{. retu
29770 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
29780 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 }. }. if( pP
29790 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
297a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
297b0 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
297c0 72 65 74 75 72 6e 20 6d 6f 76 65 54 6f 4c 65 66 return moveToLef
297d0 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 7d tmost(pCur);. }
297e0 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 .}.int sqlite3Bt
297f0 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 reeNext(BtCursor
29800 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 *pCur, int *pRe
29810 73 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 s){. MemPage *p
29820 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Page;. assert(
29830 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
29840 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
29850 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 rt( pRes!=0 );.
29860 20 61 73 73 65 72 74 28 20 2a 70 52 65 73 3d 3d assert( *pRes==
29870 30 20 7c 7c 20 2a 70 52 65 73 3d 3d 31 20 29 3b 0 || *pRes==1 );
29880 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
29890 3e 73 6b 69 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 >skipNext==0 ||
298a0 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCur->eState!=CU
298b0 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
298c0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
298d0 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 63 75 = 0;. pCur->cu
298e0 72 46 6c 61 67 73 20 26 3d 20 7e 28 42 54 43 46 rFlags &= ~(BTCF
298f0 5f 56 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f _ValidNKey|BTCF_
29900 56 61 6c 69 64 4f 76 66 6c 29 3b 0a 20 20 2a 70 ValidOvfl);. *p
29910 52 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 Res = 0;. if( p
29920 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Cur->eState!=CUR
29930 53 4f 52 5f 56 41 4c 49 44 20 29 20 72 65 74 75 SOR_VALID ) retu
29940 72 6e 20 62 74 72 65 65 4e 65 78 74 28 70 43 75 rn btreeNext(pCu
29950 72 2c 20 70 52 65 73 29 3b 0a 20 20 70 50 61 67 r, pRes);. pPag
29960 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
29970 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
29980 20 69 66 28 20 28 2b 2b 70 43 75 72 2d 3e 61 69 if( (++pCur->ai
29990 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
299a0 29 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 )>=pPage->nCell
299b0 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 ){. pCur->aiI
299c0 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d dx[pCur->iPage]-
299d0 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 -;. return bt
299e0 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 reeNext(pCur, pR
299f0 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 es);. }. if( p
29a00 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
29a10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
29a20 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
29a30 20 72 65 74 75 72 6e 20 6d 6f 76 65 54 6f 4c 65 return moveToLe
29a40 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 ftmost(pCur);.
29a50 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 }.}../*.** Step
29a60 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
29a70 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 e back to the pr
29a80 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 evious entry in
29a90 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 the database. I
29aa0 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 f.** successful
29ab0 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 then set *pRes=0
29ac0 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
29ad0 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 .** was already
29ae0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
29af0 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 first entry in t
29b00 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f he database befo
29b10 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 re.** this routi
29b20 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 ne was called, t
29b30 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e hen set *pRes=1.
29b40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 69 6e 20 .**.** The main
29b50 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 73 entry point is s
29b60 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 qlite3BtreePrevi
29b70 6f 75 73 28 29 2e 20 20 54 68 61 74 20 72 6f 75 ous(). That rou
29b80 74 69 6e 65 20 69 73 20 6f 70 74 69 6d 69 7a 65 tine is optimize
29b90 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6d d.** for the com
29ba0 6d 6f 6e 20 63 61 73 65 20 6f 66 20 6d 65 72 65 mon case of mere
29bb0 6c 79 20 64 65 63 72 65 6d 65 6e 74 69 6e 67 20 ly decrementing
29bc0 74 68 65 20 63 65 6c 6c 20 63 6f 75 6e 74 65 72 the cell counter
29bd0 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64 78 0a BtCursor.aiIdx.
29be0 2a 2a 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f ** to the previo
29bf0 75 73 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 63 us cell on the c
29c00 75 72 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 urrent page. Th
29c10 65 20 28 73 6c 6f 77 65 72 29 20 62 74 72 65 65 e (slower) btree
29c20 50 72 65 76 69 6f 75 73 28 29 0a 2a 2a 20 68 65 Previous().** he
29c30 6c 70 65 72 20 72 6f 75 74 69 6e 65 20 69 73 20 lper routine is
29c40 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 20 69 called when it i
29c50 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 6d s necessary to m
29c60 6f 76 65 20 74 6f 20 61 20 64 69 66 66 65 72 65 ove to a differe
29c70 6e 74 20 70 61 67 65 0a 2a 2a 20 6f 72 20 74 6f nt page.** or to
29c80 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 restore the cur
29c90 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 sor..**.** The c
29ca0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
29cb0 77 69 6c 6c 20 73 65 74 20 2a 70 52 65 73 20 74 will set *pRes t
29cc0 6f 20 30 20 6f 72 20 31 2e 20 20 54 68 65 20 69 o 0 or 1. The i
29cd0 6e 69 74 69 61 6c 20 2a 70 52 65 73 20 76 61 6c nitial *pRes val
29ce0 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 31 20 ue.** will be 1
29cf0 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 if the cursor be
29d00 69 6e 67 20 73 74 65 70 70 65 64 20 63 6f 72 72 ing stepped corr
29d10 65 73 70 6f 6e 64 73 20 74 6f 20 61 6e 20 53 51 esponds to an SQ
29d20 4c 20 69 6e 64 65 78 20 61 6e 64 0a 2a 2a 20 69 L index and.** i
29d30 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 f this routine c
29d40 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 73 ould have been s
29d50 6b 69 70 70 65 64 20 69 66 20 74 68 61 74 20 53 kipped if that S
29d60 51 4c 20 69 6e 64 65 78 20 68 61 64 20 62 65 65 QL index had bee
29d70 6e 0a 2a 2a 20 61 20 75 6e 69 71 75 65 20 69 6e n.** a unique in
29d80 64 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 dex. Otherwise
29d90 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 the caller will
29da0 68 61 76 65 20 73 65 74 20 2a 70 52 65 73 20 74 have set *pRes t
29db0 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 5a 65 72 6f 20 o zero..** Zero
29dc0 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 is the common ca
29dd0 73 65 2e 20 54 68 65 20 62 74 72 65 65 20 69 6d se. The btree im
29de0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
29df0 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 0a free to use the.
29e00 2a 2a 20 69 6e 69 74 69 61 6c 20 2a 70 52 65 73 ** initial *pRes
29e10 20 76 61 6c 75 65 20 61 73 20 61 20 68 69 6e 74 value as a hint
29e20 20 74 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 66 to improve perf
29e30 6f 72 6d 61 6e 63 65 2c 20 62 75 74 20 74 68 65 ormance, but the
29e40 20 63 75 72 72 65 6e 74 0a 2a 2a 20 53 51 4c 69 current.** SQLi
29e50 74 65 20 62 74 72 65 65 20 69 6d 70 6c 65 6d 65 te btree impleme
29e60 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 ntation does not
29e70 2e 20 28 4e 6f 74 65 20 74 68 61 74 20 74 68 65 . (Note that the
29e80 20 63 6f 6d 64 62 32 20 62 74 72 65 65 0a 2a 2a comdb2 btree.**
29e90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
29ea0 64 6f 65 73 20 75 73 65 20 74 68 69 73 20 68 69 does use this hi
29eb0 6e 74 2c 20 68 6f 77 65 76 65 72 2e 29 0a 2a 2f nt, however.).*/
29ec0 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 4e .static SQLITE_N
29ed0 4f 49 4e 4c 49 4e 45 20 69 6e 74 20 62 74 72 65 OINLINE int btre
29ee0 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 ePrevious(BtCurs
29ef0 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
29f00 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
29f10 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
29f20 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
29f30 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
29f40 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
29f50 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 61 73 pRes!=0 );. as
29f60 73 65 72 74 28 20 2a 70 52 65 73 3d 3d 30 20 29 sert( *pRes==0 )
29f70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
29f80 2d 3e 73 6b 69 70 4e 65 78 74 3d 3d 30 20 7c 7c ->skipNext==0 ||
29f90 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCur->eState!=C
29fa0 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
29fb0 20 61 73 73 65 72 74 28 20 28 70 43 75 72 2d 3e assert( (pCur->
29fc0 63 75 72 46 6c 61 67 73 20 26 20 28 42 54 43 46 curFlags & (BTCF
29fd0 5f 41 74 4c 61 73 74 7c 42 54 43 46 5f 56 61 6c _AtLast|BTCF_Val
29fe0 69 64 4f 76 66 6c 7c 42 54 43 46 5f 56 61 6c 69 idOvfl|BTCF_Vali
29ff0 64 4e 4b 65 79 29 29 3d 3d 30 20 29 3b 0a 20 20 dNKey))==0 );.
2a000 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 6e assert( pCur->in
2a010 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 fo.nSize==0 );.
2a020 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
2a030 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
2a040 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 73 74 ){. rc = rest
2a050 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
2a060 6e 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 n(pCur);. if(
2a070 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
2a080 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
2a090 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 c;. }. if(
2a0a0 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d CURSOR_INVALID=
2a0b0 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b =pCur->eState ){
2a0c0 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 . *pRes = 1
2a0d0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
2a0e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
2a0f0 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b if( pCur->sk
2a100 69 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 ipNext ){.
2a110 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
2a120 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
2a130 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 ID || pCur->eSta
2a140 74 65 3d 3d 43 55 52 53 4f 52 5f 53 4b 49 50 4e te==CURSOR_SKIPN
2a150 45 58 54 20 29 3b 0a 20 20 20 20 20 20 70 43 75 EXT );. pCu
2a160 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
2a170 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 OR_VALID;.
2a180 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 if( pCur->skipNe
2a190 78 74 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 xt<0 ){.
2a1a0 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d pCur->skipNext =
2a1b0 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 0;. retu
2a1c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
2a1d0 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72 }. pCur
2a1e0 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a ->skipNext = 0;.
2a1f0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 }. }.. pPa
2a200 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
2a210 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
2a220 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
2a230 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 >isInit );. if(
2a240 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
2a250 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 . int idx = p
2a260 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
2a270 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 >iPage];. rc
2a280 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
2a290 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e ur, get4byte(fin
2a2a0 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 dCell(pPage, idx
2a2b0 29 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 )));. if( rc
2a2c0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
2a2d0 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 rc = moveToRigh
2a2e0 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 7d tmost(pCur);. }
2a2f0 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 else{. while(
2a300 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
2a310 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20 29 7b 0a r->iPage]==0 ){.
2a320 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e if( pCur->
2a330 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 iPage==0 ){.
2a340 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
2a350 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
2a360 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 D;. *pRes
2a370 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 = 1;. re
2a380 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
2a390 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f }. mo
2a3a0 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 veToParent(pCur)
2a3b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
2a3c0 72 74 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e rt( pCur->info.n
2a3d0 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 Size==0 );. a
2a3e0 73 73 65 72 74 28 20 28 70 43 75 72 2d 3e 63 75 ssert( (pCur->cu
2a3f0 72 46 6c 61 67 73 20 26 20 28 42 54 43 46 5f 56 rFlags & (BTCF_V
2a400 61 6c 69 64 4e 4b 65 79 7c 42 54 43 46 5f 56 61 alidNKey|BTCF_Va
2a410 6c 69 64 4f 76 66 6c 29 29 3d 3d 30 20 29 3b 0a lidOvfl))==0 );.
2a420 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
2a430 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b [pCur->iPage]--;
2a440 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 . pPage = pCu
2a450 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
2a460 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 iPage];. if(
2a470 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 pPage->intKey &&
2a480 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
2a490 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
2a4a0 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
2a4b0 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 (pCur, pRes);.
2a4c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
2a4d0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
2a4e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
2a4f0 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 n rc;.}.int sqli
2a500 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
2a510 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
2a520 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 61 int *pRes){. a
2a530 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
2a540 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
2a550 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73 21 . assert( pRes!
2a560 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
2a570 2a 70 52 65 73 3d 3d 30 20 7c 7c 20 2a 70 52 65 *pRes==0 || *pRe
2a580 73 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 s==1 );. assert
2a590 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 ( pCur->skipNext
2a5a0 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 ==0 || pCur->eSt
2a5b0 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
2a5c0 44 20 29 3b 0a 20 20 2a 70 52 65 73 20 3d 20 30 D );. *pRes = 0
2a5d0 3b 0a 20 20 70 43 75 72 2d 3e 63 75 72 46 6c 61 ;. pCur->curFla
2a5e0 67 73 20 26 3d 20 7e 28 42 54 43 46 5f 41 74 4c gs &= ~(BTCF_AtL
2a5f0 61 73 74 7c 42 54 43 46 5f 56 61 6c 69 64 4f 76 ast|BTCF_ValidOv
2a600 66 6c 7c 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 fl|BTCF_ValidNKe
2a610 79 29 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f y);. pCur->info
2a620 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 .nSize = 0;. if
2a630 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d ( pCur->eState!=
2a640 43 55 52 53 4f 52 5f 56 41 4c 49 44 0a 20 20 20 CURSOR_VALID.
2a650 7c 7c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 || pCur->aiIdx[p
2a660 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 0a 20 Cur->iPage]==0.
2a670 20 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 || pCur->apPag
2a680 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
2a690 6c 65 61 66 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 leaf==0. ){.
2a6a0 20 72 65 74 75 72 6e 20 62 74 72 65 65 50 72 65 return btreePre
2a6b0 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73 vious(pCur, pRes
2a6c0 29 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 );. }. pCur->a
2a6d0 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
2a6e0 5d 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ]--;. return SQ
2a6f0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
2a700 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
2a710 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 page from the d
2a720 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
2a730 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 .** The new page
2a740 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 is marked as di
2a750 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 rty. (In other
2a760 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 words, sqlite3Pa
2a770 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 gerWrite().** ha
2a780 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 s already been c
2a790 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 alled on the new
2a7a0 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 page.) The new
2a7b0 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a page has also.*
2a7c0 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 * been reference
2a7d0 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e d and the callin
2a7e0 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 g routine is res
2a7f0 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c ponsible for cal
2a800 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 ling.** sqlite3P
2a810 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 agerUnref() on t
2a820 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e he new page when
2a830 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a it is done..**.
2a840 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
2a850 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 returned on succ
2a860 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 ess. Any other
2a870 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 return value ind
2a880 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 icates.** an err
2a890 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64 or. *ppPage and
2a8a0 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65 *pPgno are unde
2a8b0 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65 fined in the eve
2a8c0 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a nt of an error..
2a8d0 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 ** Do not invoke
2a8e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
2a8f0 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20 ef() on *ppPage
2a900 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 if an error is r
2a910 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
2a920 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 f the "nearby" p
2a930 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 arameter is not
2a940 30 2c 20 74 68 65 6e 20 61 6e 20 65 66 66 6f 72 0, then an effor
2a950 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a t is made to .**
2a960 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 locate a page c
2a970 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 lose to the page
2a980 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 number "nearby"
2a990 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
2a9a0 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 sed in an.** att
2a9b0 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c empt to keep rel
2a9c0 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 ated pages close
2a9d0 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 to each other i
2a9e0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
2a9f0 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e ile,.** which in
2aa00 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 turn can make d
2aa10 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66 atabase access f
2aa20 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 aster..**.** If
2aa30 74 68 65 20 65 4d 6f 64 65 20 70 61 72 61 6d 65 the eMode parame
2aa40 74 65 72 20 69 73 20 42 54 41 4c 4c 4f 43 5f 45 ter is BTALLOC_E
2aa50 58 41 43 54 20 61 6e 64 20 74 68 65 20 6e 65 61 XACT and the nea
2aa60 72 62 79 20 70 61 67 65 20 65 78 69 73 74 73 0a rby page exists.
2aa70 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 ** anywhere on t
2aa80 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 he free-list, th
2aa90 65 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 en it is guarant
2aaa0 65 65 64 20 74 6f 20 62 65 20 72 65 74 75 72 6e eed to be return
2aab0 65 64 2e 20 20 49 66 0a 2a 2a 20 65 4d 6f 64 65 ed. If.** eMode
2aac0 20 69 73 20 42 54 41 4c 4c 4f 43 5f 4c 54 20 74 is BTALLOC_LT t
2aad0 68 65 6e 20 74 68 65 20 70 61 67 65 20 72 65 74 hen the page ret
2aae0 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 6c 65 urned will be le
2aaf0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
2ab00 0a 2a 2a 20 74 6f 20 6e 65 61 72 62 79 20 69 66 .** to nearby if
2ab10 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 20 65 any such page e
2ab20 78 69 73 74 73 2e 20 20 49 66 20 65 4d 6f 64 65 xists. If eMode
2ab30 20 69 73 20 42 54 41 4c 4c 4f 43 5f 41 4e 59 20 is BTALLOC_ANY
2ab40 74 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61 72 then there.** ar
2ab50 65 20 6e 6f 20 72 65 73 74 72 69 63 74 69 6f 6e e no restriction
2ab60 73 20 6f 6e 20 77 68 69 63 68 20 70 61 67 65 20 s on which page
2ab70 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
2ab80 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 static int alloc
2ab90 61 74 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 ateBtreePage(.
2aba0 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
2abb0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
2abc0 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ree */. MemPage
2abd0 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 20 **ppPage,
2abe0 2f 2a 20 53 74 6f 72 65 20 70 6f 69 6e 74 65 72 /* Store pointer
2abf0 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 to the allocate
2ac00 64 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 d page here */.
2ac10 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 20 20 Pgno *pPgno,
2ac20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 /* Store
2ac30 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
2ac40 20 68 65 72 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 here */. Pgno
2ac50 6e 65 61 72 62 79 2c 20 20 20 20 20 20 20 20 20 nearby,
2ac60 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 /* Search for
2ac70 61 20 70 61 67 65 20 6e 65 61 72 20 74 68 69 73 a page near this
2ac80 20 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 65 4d 6f one */. u8 eMo
2ac90 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 de
2aca0 20 2f 2a 20 42 54 41 4c 4c 4f 43 5f 45 58 41 43 /* BTALLOC_EXAC
2acb0 54 2c 20 42 54 41 4c 4c 4f 43 5f 4c 54 2c 20 6f T, BTALLOC_LT, o
2acc0 72 20 42 54 41 4c 4c 4f 43 5f 41 4e 59 20 2a 2f r BTALLOC_ANY */
2acd0 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
2ace0 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b Page1;. int rc;
2acf0 0a 20 20 75 33 32 20 6e 3b 20 20 20 20 20 2f 2a . u32 n; /*
2ad00 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
2ad10 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
2ad20 20 2a 2f 0a 20 20 75 33 32 20 6b 3b 20 20 20 20 */. u32 k;
2ad30 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 /* Number of le
2ad40 61 76 65 73 20 6f 6e 20 74 68 65 20 74 72 75 6e aves on the trun
2ad50 6b 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 k of the freelis
2ad60 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a t */. MemPage *
2ad70 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 pTrunk = 0;. Me
2ad80 6d 50 61 67 65 20 2a 70 50 72 65 76 54 72 75 6e mPage *pPrevTrun
2ad90 6b 20 3d 20 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 k = 0;. Pgno mx
2ada0 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 Page; /* Tot
2adb0 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 al size of the d
2adc0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
2add0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
2ade0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
2adf0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
2ae00 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 42 54 ssert( eMode==BT
2ae10 41 4c 4c 4f 43 5f 41 4e 59 20 7c 7c 20 28 6e 65 ALLOC_ANY || (ne
2ae20 61 72 62 79 3e 30 20 26 26 20 49 66 4e 6f 74 4f arby>0 && IfNotO
2ae30 6d 69 74 41 56 28 70 42 74 2d 3e 61 75 74 6f 56 mitAV(pBt->autoV
2ae40 61 63 75 75 6d 29 29 20 29 3b 0a 20 20 70 50 61 acuum)) );. pPa
2ae50 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 ge1 = pBt->pPage
2ae60 31 3b 0a 20 20 6d 78 50 61 67 65 20 3d 20 62 74 1;. mxPage = bt
2ae70 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 reePagecount(pBt
2ae80 29 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 );. n = get4byt
2ae90 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
2aea0 5b 33 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 [36]);. testcas
2aeb0 65 28 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 e( n==mxPage-1 )
2aec0 3b 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 ;. if( n>=mxPag
2aed0 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
2aee0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
2aef0 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e KPT;. }. if( n
2af00 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 >0 ){. /* The
2af10 72 65 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 re are pages on
2af20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 the freelist. R
2af30 65 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 euse one of thos
2af40 65 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 e pages. */.
2af50 50 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 Pgno iTrunk;.
2af60 20 75 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d u8 searchList =
2af70 20 30 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 0; /* If the fr
2af80 65 65 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 ee-list must be
2af90 73 65 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 searched for 'ne
2afa0 61 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 arby' */. .
2afb0 20 20 2f 2a 20 49 66 20 65 4d 6f 64 65 3d 3d 42 /* If eMode==B
2afc0 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 61 6e 64 TALLOC_EXACT and
2afd0 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 a query of the
2afe0 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 pointer-map.
2aff0 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 ** shows that th
2b000 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 e page 'nearby'
2b010 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 is somewhere on
2b020 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 the free-list, t
2b030 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 hen. ** the e
2b040 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 ntire-list will
2b050 62 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 be searched for
2b060 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a that page.. *
2b070 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
2b080 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
2b090 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d . if( eMode==
2b0a0 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 20 29 7b BTALLOC_EXACT ){
2b0b0 0a 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 . if( nearb
2b0c0 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 y<=mxPage ){.
2b0d0 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 u8 eType;.
2b0e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
2b0f0 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 earby>0 );.
2b100 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
2b110 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 autoVacuum );.
2b120 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
2b130 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 pGet(pBt, nearby
2b140 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 , &eType, 0);.
2b150 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
2b160 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
2b170 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
2b180 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
2b190 20 20 20 20 20 20 20 20 20 20 73 65 61 72 63 68 search
2b1a0 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 List = 1;.
2b1b0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
2b1c0 7d 65 6c 73 65 20 69 66 28 20 65 4d 6f 64 65 3d }else if( eMode=
2b1d0 3d 42 54 41 4c 4c 4f 43 5f 4c 45 20 29 7b 0a 20 =BTALLOC_LE ){.
2b1e0 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 searchList
2b1f0 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 = 1;. }.#endi
2b200 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d f.. /* Decrem
2b210 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 ent the free-lis
2b220 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 t count by 1. Se
2b230 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 t iTrunk to the
2b240 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 index of the.
2b250 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c ** first free-l
2b260 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 ist trunk page.
2b270 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e iPrevTrunk is in
2b280 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a itially 1.. *
2b290 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
2b2a0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
2b2b0 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
2b2c0 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
2b2d0 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62 rn rc;. put4b
2b2e0 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
2b2f0 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 ta[36], n-1);..
2b300 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 /* The code w
2b310 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 ithin this loop
2b320 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 is run only once
2b330 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c if the 'searchL
2b340 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20 ist' variable.
2b350 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 ** is not true
2b360 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
2b370 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 runs once for ea
2b380 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e ch trunk-page on
2b390 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 the. ** free
2b3a0 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20 -list until the
2b3b0 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 page 'nearby' is
2b3c0 20 6c 6f 63 61 74 65 64 20 28 65 4d 6f 64 65 3d located (eMode=
2b3d0 3d 42 54 41 4c 4c 4f 43 5f 45 58 41 43 54 29 0a =BTALLOC_EXACT).
2b3e0 20 20 20 20 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 ** or until
2b3f0 61 20 70 61 67 65 20 6c 65 73 73 20 74 68 61 6e a page less than
2b400 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63 'nearby' is loc
2b410 61 74 65 64 20 28 65 4d 6f 64 65 3d 3d 42 54 41 ated (eMode==BTA
2b420 4c 4c 4f 43 5f 4c 54 29 0a 20 20 20 20 2a 2f 0a LLOC_LT). */.
2b430 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 do {. p
2b440 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 PrevTrunk = pTru
2b450 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 nk;. if( pP
2b460 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 revTrunk ){.
2b470 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 iTrunk = get
2b480 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 4byte(&pPrevTrun
2b490 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 k->aData[0]);.
2b4a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
2b4b0 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 iTrunk = get4
2b4c0 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 byte(&pPage1->aD
2b4d0 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 ata[32]);.
2b4e0 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 }. testcase
2b4f0 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 ( iTrunk==mxPage
2b500 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 54 );. if( iT
2b510 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 0a 20 runk>mxPage ){.
2b520 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
2b530 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
2b540 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
2b550 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
2b560 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 GetPage(pBt, iTr
2b570 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 unk, &pTrunk, 0)
2b580 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
2b590 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
2b5a0 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 pTrunk = 0;.
2b5b0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
2b5c0 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
2b5d0 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
2b5e0 72 74 28 20 70 54 72 75 6e 6b 21 3d 30 20 29 3b rt( pTrunk!=0 );
2b5f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
2b600 54 72 75 6e 6b 2d 3e 61 44 61 74 61 21 3d 30 20 Trunk->aData!=0
2b610 29 3b 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 );.. k = ge
2b620 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
2b630 61 44 61 74 61 5b 34 5d 29 3b 20 2f 2a 20 23 20 aData[4]); /* #
2b640 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 69 of leaves on thi
2b650 73 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a s trunk page */.
2b660 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 if( k==0 &
2b670 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b & !searchList ){
2b680 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
2b690 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 trunk has no lea
2b6a0 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74 ves and the list
2b6b0 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 is not being se
2b6c0 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20 arched. .
2b6d0 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74 ** So extract t
2b6e0 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74 he trunk page it
2b6f0 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20 self and use it
2b700 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 as the newly .
2b710 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 ** allocat
2b720 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 ed page */.
2b730 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76 assert( pPrev
2b740 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 Trunk==0 );.
2b750 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
2b760 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
2b770 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
2b780 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
2b790 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
2b7a0 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
2b7b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2b7c0 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 *pPgno = iTru
2b7d0 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 nk;. memc
2b7e0 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
2b7f0 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
2b800 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
2b810 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
2b820 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
2b830 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 pTrunk = 0;.
2b840 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f TRACE(("ALLO
2b850 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d CATE: %d trunk -
2b860 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c %d free pages l
2b870 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 eft\n", *pPgno,
2b880 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c n-1));. }el
2b890 73 65 20 69 66 28 20 6b 3e 28 75 33 32 29 28 70 se if( k>(u32)(p
2b8a0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 Bt->usableSize/4
2b8b0 20 2d 20 32 29 20 29 7b 0a 20 20 20 20 20 20 20 - 2) ){.
2b8c0 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 /* Value of k i
2b8d0 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 s out of range.
2b8e0 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 Database corrup
2b8f0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 tion */.
2b900 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
2b910 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
2b920 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
2b930 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 ate_page;.#ifnde
2b940 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
2b950 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 7d TOVACUUM. }
2b960 65 6c 73 65 20 69 66 28 20 73 65 61 72 63 68 4c else if( searchL
2b970 69 73 74 20 0a 20 20 20 20 20 20 20 20 20 20 20 ist .
2b980 20 26 26 20 28 6e 65 61 72 62 79 3d 3d 69 54 72 && (nearby==iTr
2b990 75 6e 6b 20 7c 7c 20 28 69 54 72 75 6e 6b 3c 6e unk || (iTrunk<n
2b9a0 65 61 72 62 79 20 26 26 20 65 4d 6f 64 65 3d 3d earby && eMode==
2b9b0 42 54 41 4c 4c 4f 43 5f 4c 45 29 29 20 0a 20 20 BTALLOC_LE)) .
2b9c0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ){. /
2b9d0 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 62 65 * The list is be
2b9e0 69 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e 64 ing searched and
2b9f0 20 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65 this trunk page
2ba00 20 69 73 20 74 68 65 20 70 61 67 65 0a 20 20 20 is the page.
2ba10 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 ** to alloc
2ba20 61 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 ate, regardless
2ba30 6f 66 20 77 68 65 74 68 65 72 20 69 74 20 68 61 of whether it ha
2ba40 73 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 s leaves..
2ba50 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 2a 70 50 */. *pP
2ba60 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 gno = iTrunk;.
2ba70 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
2ba80 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
2ba90 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a searchList = 0;.
2baa0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
2bab0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
2bac0 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
2bad0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
2bae0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
2baf0 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
2bb00 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
2bb10 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 if( k==0
2bb20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
2bb30 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a !pPrevTrunk ){.
2bb40 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
2bb50 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
2bb60 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
2bb70 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
2bb80 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
2bb90 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
2bba0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
2bbb0 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 e(pPrevTrunk->pD
2bbc0 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
2bbd0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
2bbe0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
2bbf0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
2bc00 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
2bc10 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
2bc20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
2bc30 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
2bc40 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e ta[0], &pTrunk->
2bc50 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
2bc60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2bc70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
2bc80 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 /* The trunk
2bc90 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 page is required
2bca0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 by the caller b
2bcb0 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a ut it contains .
2bcc0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 ** poi
2bcd0 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 nters to free-li
2bce0 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 st leaves. The f
2bcf0 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 irst leaf become
2bd00 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 s a trunk.
2bd10 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 ** page in t
2bd20 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 his case..
2bd30 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
2bd40 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 MemPage *pNewTr
2bd50 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 unk;. P
2bd60 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 gno iNewTrunk =
2bd70 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b get4byte(&pTrunk
2bd80 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 ->aData[8]);.
2bd90 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 if( iNewT
2bda0 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a runk>mxPage ){ .
2bdb0 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
2bdc0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
2bdd0 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 BKPT;.
2bde0 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
2bdf0 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
2be00 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
2be10 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 testcase( iNewTr
2be20 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 unk==mxPage );.
2be30 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 rc = bt
2be40 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
2be50 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 iNewTrunk, &pNew
2be60 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 Trunk, 0);.
2be70 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
2be80 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2be90 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
2bea0 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
2beb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2bec0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
2bed0 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 PagerWrite(pNewT
2bee0 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
2bef0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
2bf00 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
2bf10 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 relea
2bf20 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b sePage(pNewTrunk
2bf30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 );. g
2bf40 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
2bf50 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 _page;.
2bf60 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d }. mem
2bf70 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e cpy(&pNewTrunk->
2bf80 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e aData[0], &pTrun
2bf90 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b k->aData[0], 4);
2bfa0 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
2bfb0 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e yte(&pNewTrunk->
2bfc0 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a aData[4], k-1);.
2bfd0 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
2bfe0 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 (&pNewTrunk->aDa
2bff0 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e ta[8], &pTrunk->
2c000 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 aData[12], (k-1)
2c010 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 *4);. r
2c020 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 eleasePage(pNewT
2c030 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 runk);.
2c040 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b if( !pPrevTrunk
2c050 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2c060 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
2c070 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
2c080 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
2c090 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
2c0a0 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 put4byte(&pPage1
2c0b0 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 ->aData[32], iNe
2c0c0 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 wTrunk);.
2c0d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2c0e0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
2c0f0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 72 e3PagerWrite(pPr
2c100 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 evTrunk->pDbPage
2c110 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
2c120 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
2c130 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
2c140 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
2c150 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
2c160 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
2c170 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 e(&pPrevTrunk->a
2c180 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 Data[0], iNewTru
2c190 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d nk);. }
2c1a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2c1b0 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 pTrunk = 0;.
2c1c0 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 TRACE(("A
2c1d0 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e LLOCATE: %d trun
2c1e0 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 k - %d free page
2c1f0 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e s left\n", *pPgn
2c200 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 o, n-1));.#endif
2c210 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
2c220 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 k>0 ){.
2c230 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 /* Extract a lea
2c240 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b f from the trunk
2c250 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 */. u32
2c260 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 closest;.
2c270 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 Pgno iPage;.
2c280 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
2c290 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 ar *aData = pTru
2c2a0 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 nk->aData;.
2c2b0 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 if( nearby>0
2c2c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 ){. u32
2c2d0 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6c i;. cl
2c2e0 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 osest = 0;.
2c2f0 20 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d if( eMode==
2c300 42 54 41 4c 4c 4f 43 5f 4c 45 20 29 7b 0a 20 20 BTALLOC_LE ){.
2c310 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d for(i=
2c320 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 0; i<k; i++){.
2c330 20 20 20 20 20 20 20 20 20 20 20 20 69 50 61 67 iPag
2c340 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 e = get4byte(&aD
2c350 61 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a 20 20 20 ata[8+i*4]);.
2c360 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 if( i
2c370 50 61 67 65 3c 3d 6e 65 61 72 62 79 20 29 7b 0a Page<=nearby ){.
2c380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c390 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 closest = i;.
2c3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 bre
2c3b0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ak;.
2c3c0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
2c3d0 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
2c3e0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 e{. i
2c3f0 6e 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 nt dist;.
2c400 20 20 20 20 20 64 69 73 74 20 3d 20 73 71 6c 69 dist = sqli
2c410 74 65 33 41 62 73 49 6e 74 33 32 28 67 65 74 34 te3AbsInt32(get4
2c420 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d 29 20 byte(&aData[8])
2c430 2d 20 6e 65 61 72 62 79 29 3b 0a 20 20 20 20 20 - nearby);.
2c440 20 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 for(i=1;
2c450 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 i<k; i++){.
2c460 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 32 20 int d2
2c470 3d 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 = sqlite3AbsInt3
2c480 32 28 67 65 74 34 62 79 74 65 28 26 61 44 61 74 2(get4byte(&aDat
2c490 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 61 72 a[8+i*4]) - near
2c4a0 62 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 by);.
2c4b0 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29 if( d2<dist )
2c4c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
2c4d0 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 closest = i;.
2c4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
2c4f0 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 ist = d2;.
2c500 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2c510 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2c520 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
2c530 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f e{. clo
2c540 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 sest = 0;.
2c550 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 }.. iPa
2c560 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 ge = get4byte(&a
2c570 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 Data[8+closest*4
2c580 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 ]);. test
2c590 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 case( iPage==mxP
2c5a0 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 age );. i
2c5b0 66 28 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 f( iPage>mxPage
2c5c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
2c5d0 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
2c5e0 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 _BKPT;.
2c5f0 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
2c600 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
2c610 20 7d 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 }. testc
2c620 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 ase( iPage==mxPa
2c630 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ge );. if
2c640 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 0a 20 ( !searchList .
2c650 20 20 20 20 20 20 20 20 7c 7c 20 28 69 50 61 67 || (iPag
2c660 65 3d 3d 6e 65 61 72 62 79 20 7c 7c 20 28 69 50 e==nearby || (iP
2c670 61 67 65 3c 6e 65 61 72 62 79 20 26 26 20 65 4d age<nearby && eM
2c680 6f 64 65 3d 3d 42 54 41 4c 4c 4f 43 5f 4c 45 29 ode==BTALLOC_LE)
2c690 29 20 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 ) . ){.
2c6a0 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43 6f int noCo
2c6b0 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 20 ntent;.
2c6c0 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b *pPgno = iPage;
2c6d0 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 43 45 . TRACE
2c6e0 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 (("ALLOCATE: %d
2c6f0 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20 25 was leaf %d of %
2c700 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20 d on trunk %d".
2c710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c720 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65 20 ": %d more free
2c730 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 pages\n",.
2c740 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e *pPgn
2c750 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c o, closest+1, k,
2c760 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e pTrunk->pgno, n
2c770 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 -1));.
2c780 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
2c790 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
2c7a0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
2c7b0 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
2c7c0 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
2c7d0 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ge;. if
2c7e0 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b ( closest<k-1 ){
2c7f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d . mem
2c800 63 70 79 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f cpy(&aData[8+clo
2c810 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b sest*4], &aData[
2c820 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 20 4+k*4], 4);.
2c830 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2c840 20 20 70 75 74 34 62 79 74 65 28 26 61 44 61 74 put4byte(&aDat
2c850 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 a[4], k-1);.
2c860 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 20 noContent
2c870 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 43 6f = !btreeGetHasCo
2c880 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67 6e ntent(pBt, *pPgn
2c890 6f 29 3f 20 50 41 47 45 52 5f 47 45 54 5f 4e 4f o)? PAGER_GET_NO
2c8a0 43 4f 4e 54 45 4e 54 20 3a 20 30 3b 0a 20 20 20 CONTENT : 0;.
2c8b0 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 rc = btre
2c8c0 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 eGetPage(pBt, *p
2c8d0 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 6e 6f Pgno, ppPage, no
2c8e0 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 Content);.
2c8f0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
2c900 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
2c910 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
2c920 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 3PagerWrite((*pp
2c930 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b Page)->pDbPage);
2c940 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
2c950 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
2c960 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
2c970 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 releasePage(*ppP
2c980 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
2c990 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
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 30 3b 0a 20 20 20 20 20 20 List = 0;.
2c9c0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
2c9d0 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
2c9e0 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 revTrunk);.
2c9f0 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b pPrevTrunk = 0;
2ca00 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 73 65 61 . }while( sea
2ca10 72 63 68 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c rchList );. }el
2ca20 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 se{. /* There
2ca30 20 61 72 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e are no pages on
2ca40 20 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20 73 the freelist, s
2ca50 6f 20 61 70 70 65 6e 64 20 61 20 6e 65 77 20 70 o append a new p
2ca60 61 67 65 20 74 6f 20 74 68 65 0a 20 20 20 20 2a age to the. *
2ca70 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 * database image
2ca80 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
2ca90 4e 6f 72 6d 61 6c 6c 79 2c 20 6e 65 77 20 70 61 Normally, new pa
2caa0 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ges allocated by
2cab0 20 74 68 69 73 20 62 6c 6f 63 6b 20 63 61 6e 20 this block can
2cac0 62 65 20 72 65 71 75 65 73 74 65 64 20 66 72 6f be requested fro
2cad0 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 m the. ** pag
2cae0 65 72 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 er layer with th
2caf0 65 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 e 'no-content' f
2cb00 6c 61 67 20 73 65 74 2e 20 54 68 69 73 20 70 72 lag set. This pr
2cb10 65 76 65 6e 74 73 20 74 68 65 20 70 61 67 65 72 events the pager
2cb20 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 . ** from try
2cb30 69 6e 67 20 74 6f 20 72 65 61 64 20 74 68 65 20 ing to read the
2cb40 70 61 67 65 73 20 63 6f 6e 74 65 6e 74 20 66 72 pages content fr
2cb50 6f 6d 20 64 69 73 6b 2e 20 48 6f 77 65 76 65 72 om disk. However
2cb60 2c 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 , if the. **
2cb70 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
2cb80 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 ion has already
2cb90 72 75 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 run one or more
2cba0 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 incremental-vacu
2cbb0 75 6d 0a 20 20 20 20 2a 2a 20 73 74 65 70 73 2c um. ** steps,
2cbc0 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 77 then the page w
2cbd0 65 20 61 72 65 20 61 62 6f 75 74 20 74 6f 20 61 e are about to a
2cbe0 6c 6c 6f 63 61 74 65 20 6d 61 79 20 63 6f 6e 74 llocate may cont
2cbf0 61 69 6e 20 63 6f 6e 74 65 6e 74 0a 20 20 20 20 ain content.
2cc00 2a 2a 20 74 68 61 74 20 69 73 20 72 65 71 75 69 ** that is requi
2cc10 72 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 red in the event
2cc20 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 of a rollback.
2cc30 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 64 6f In this case, do
2cc40 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20 . ** not set
2cc50 74 68 65 20 6e 6f 2d 63 6f 6e 74 65 6e 74 20 66 the no-content f
2cc60 6c 61 67 2e 20 54 68 69 73 20 63 61 75 73 65 73 lag. This causes
2cc70 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 6c 6f the pager to lo
2cc80 61 64 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 0a 20 ad and journal.
2cc90 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e ** the curren
2cca0 74 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 62 t page content b
2ccb0 65 66 6f 72 65 20 6f 76 65 72 77 72 69 74 69 6e efore overwritin
2ccc0 67 20 69 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 g it.. **.
2ccd0 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 ** Note that th
2cce0 65 20 70 61 67 65 72 20 77 69 6c 6c 20 6e 6f 74 e pager will not
2ccf0 20 61 63 74 75 61 6c 6c 79 20 61 74 74 65 6d 70 actually attemp
2cd00 74 20 74 6f 20 6c 6f 61 64 20 6f 72 20 6a 6f 75 t to load or jou
2cd10 72 6e 61 6c 20 0a 20 20 20 20 2a 2a 20 63 6f 6e rnal . ** con
2cd20 74 65 6e 74 20 66 6f 72 20 61 6e 79 20 70 61 67 tent for any pag
2cd30 65 20 74 68 61 74 20 72 65 61 6c 6c 79 20 64 6f e that really do
2cd40 65 73 20 6c 69 65 20 70 61 73 74 20 74 68 65 20 es lie past the
2cd50 65 6e 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 end of the datab
2cd60 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 ase. ** file
2cd70 6f 6e 20 64 69 73 6b 2e 20 53 6f 20 74 68 65 20 on disk. So the
2cd80 65 66 66 65 63 74 73 20 6f 66 20 64 69 73 61 62 effects of disab
2cd90 6c 69 6e 67 20 74 68 65 20 6e 6f 2d 63 6f 6e 74 ling the no-cont
2cda0 65 6e 74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ent optimization
2cdb0 0a 20 20 20 20 2a 2a 20 68 65 72 65 20 61 72 65 . ** here are
2cdc0 20 63 6f 6e 66 69 6e 65 64 20 74 6f 20 74 68 6f confined to tho
2cdd0 73 65 20 70 61 67 65 73 20 74 68 61 74 20 6c 69 se pages that li
2cde0 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 65 6e e between the en
2cdf0 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 d of the. **
2ce00 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 61 database image a
2ce10 6e 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 nd the end of th
2ce20 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
2ce30 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 . */. int
2ce40 62 4e 6f 43 6f 6e 74 65 6e 74 20 3d 20 28 30 3d bNoContent = (0=
2ce50 3d 49 66 4e 6f 74 4f 6d 69 74 41 56 28 70 42 74 =IfNotOmitAV(pBt
2ce60 2d 3e 62 44 6f 54 72 75 6e 63 61 74 65 29 29 3f ->bDoTruncate))?
2ce70 20 50 41 47 45 52 5f 47 45 54 5f 4e 4f 43 4f 4e PAGER_GET_NOCON
2ce80 54 45 4e 54 3a 30 3b 0a 0a 20 20 20 20 72 63 20 TENT:0;.. rc
2ce90 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
2cea0 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
2ceb0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
2cec0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
2ced0 63 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 c;. pBt->nPag
2cee0 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 42 74 e++;. if( pBt
2cef0 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e 47 ->nPage==PENDING
2cf00 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
2cf10 29 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0a ) pBt->nPage++;.
2cf20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2cf30 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
2cf40 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
2cf50 6f 56 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 oVacuum && PTRMA
2cf60 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 42 P_ISPAGE(pBt, pB
2cf70 74 2d 3e 6e 50 61 67 65 29 20 29 7b 0a 20 20 20 t->nPage) ){.
2cf80 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 /* If *pPgno
2cf90 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f 69 6e refers to a poin
2cfa0 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c ter-map page, al
2cfb0 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77 20 70 locate two new p
2cfc0 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 ages. ** at
2cfd0 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
2cfe0 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 file instead of
2cff0 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 one. The first a
2d000 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a 20 20 llocated page.
2d010 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 ** becomes a
2d020 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 new pointer-map
2d030 20 70 61 67 65 2c 20 74 68 65 20 73 65 63 6f 6e page, the secon
2d040 64 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 d is used by the
2d050 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a caller.. *
2d060 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 /. MemPage
2d070 2a 70 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 *pPg = 0;.
2d080 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
2d090 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 : %d from end of
2d0a0 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d file (pointer-m
2d0b0 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 70 42 74 ap page)\n", pBt
2d0c0 2d 3e 6e 50 61 67 65 29 29 3b 0a 20 20 20 20 20 ->nPage));.
2d0d0 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 50 assert( pBt->nP
2d0e0 61 67 65 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 age!=PENDING_BYT
2d0f0 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 E_PAGE(pBt) );.
2d100 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
2d110 65 74 50 61 67 65 28 70 42 74 2c 20 70 42 74 2d etPage(pBt, pBt-
2d120 3e 6e 50 61 67 65 2c 20 26 70 50 67 2c 20 62 4e >nPage, &pPg, bN
2d130 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 oContent);.
2d140 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2d150 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
2d160 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
2d170 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67 rite(pPg->pDbPag
2d180 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 e);. rele
2d190 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20 asePage(pPg);.
2d1a0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
2d1b0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
2d1c0 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 pBt->nPage
2d1d0 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 ++;. if( pB
2d1e0 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e t->nPage==PENDIN
2d1f0 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
2d200 20 29 7b 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b ){ pBt->nPage++
2d210 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ; }. }.#endif
2d220 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 32 38 . put4byte(28
2d230 20 2b 20 28 75 38 2a 29 70 42 74 2d 3e 70 50 61 + (u8*)pBt->pPa
2d240 67 65 31 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d ge1->aData, pBt-
2d250 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 2a 70 50 >nPage);. *pP
2d260 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 50 61 67 65 gno = pBt->nPage
2d270 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a ;.. assert( *
2d280 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
2d290 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
2d2a0 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
2d2b0 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 etPage(pBt, *pPg
2d2c0 6e 6f 2c 20 70 70 50 61 67 65 2c 20 62 4e 6f 43 no, ppPage, bNoC
2d2d0 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 ontent);. if(
2d2e0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
2d2f0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
2d300 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 3PagerWrite((*pp
2d310 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b Page)->pDbPage);
2d320 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
2d330 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2d340 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 releasePage(*ppP
2d350 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 age);. }.
2d360 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
2d370 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 : %d from end of
2d380 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f file\n", *pPgno
2d390 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 ));. }.. asser
2d3a0 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 t( *pPgno!=PENDI
2d3b0 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
2d3c0 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 ) );..end_alloca
2d3d0 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 te_page:. relea
2d3e0 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a sePage(pTrunk);.
2d3f0 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
2d400 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 revTrunk);. if(
2d410 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
2d420 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
2d430 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
2d440 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 nt((*ppPage)->pD
2d450 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 bPage)>1 ){.
2d460 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 releasePage(*p
2d470 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 2a 70 pPage);. *p
2d480 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 pPage = 0;.
2d490 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
2d4a0 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
2d4b0 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 }. (*ppPage)
2d4c0 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
2d4d0 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 }else{. *ppPa
2d4e0 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 ge = 0;. }. as
2d4f0 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
2d500 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 _OK || sqlite3Pa
2d510 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 28 gerIswriteable((
2d520 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 *ppPage)->pDbPag
2d530 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e) );. return r
2d540 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
2d550 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
2d560 64 20 74 6f 20 61 64 64 20 70 61 67 65 20 69 50 d to add page iP
2d570 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
2d580 61 73 65 20 66 69 6c 65 20 66 72 65 65 2d 6c 69 ase file free-li
2d590 73 74 2e 20 0a 2a 2a 20 49 74 20 69 73 20 61 73 st. .** It is as
2d5a0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 70 sumed that the p
2d5b0 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
2d5c0 64 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 dy a part of the
2d5d0 20 66 72 65 65 2d 6c 69 73 74 2e 0a 2a 2a 0a 2a free-list..**.*
2d5e0 2a 20 54 68 65 20 76 61 6c 75 65 20 70 61 73 73 * The value pass
2d5f0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
2d600 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
2d610 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 70 s function is op
2d620 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 66 20 74 68 tional..** If th
2d630 65 20 63 61 6c 6c 65 72 20 68 61 70 70 65 6e 73 e caller happens
2d640 20 74 6f 20 68 61 76 65 20 61 20 70 6f 69 6e 74 to have a point
2d650 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 er to the MemPag
2d660 65 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 72 e object .** cor
2d670 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 responding to pa
2d680 67 65 20 69 50 61 67 65 20 68 61 6e 64 79 2c 20 ge iPage handy,
2d690 69 74 20 6d 61 79 20 70 61 73 73 20 69 74 20 61 it may pass it a
2d6a0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 76 61 6c s the second val
2d6b0 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ue. .** Otherwis
2d6c0 65 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 4e e, it may pass N
2d6d0 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ULL..**.** If a
2d6e0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d pointer to a Mem
2d6f0 50 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 70 Page object is p
2d700 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
2d710 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a ond argument,.**
2d720 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 63 its reference c
2d730 6f 75 6e 74 20 69 73 20 6e 6f 74 20 61 6c 74 65 ount is not alte
2d740 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 red by this func
2d750 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
2d760 69 6e 74 20 66 72 65 65 50 61 67 65 32 28 42 74 int freePage2(Bt
2d770 53 68 61 72 65 64 20 2a 70 42 74 2c 20 4d 65 6d Shared *pBt, Mem
2d780 50 61 67 65 20 2a 70 4d 65 6d 50 61 67 65 2c 20 Page *pMemPage,
2d790 50 67 6e 6f 20 69 50 61 67 65 29 7b 0a 20 20 4d Pgno iPage){. M
2d7a0 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d emPage *pTrunk =
2d7b0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
2d7c0 20 20 20 2f 2a 20 46 72 65 65 2d 6c 69 73 74 20 /* Free-list
2d7d0 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 trunk page */.
2d7e0 50 67 6e 6f 20 69 54 72 75 6e 6b 20 3d 20 30 3b Pgno iTrunk = 0;
2d7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d800 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
2d810 65 72 20 6f 66 20 66 72 65 65 2d 6c 69 73 74 20 er of free-list
2d820 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 20 0a 20 trunk page */ .
2d830 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 MemPage *pPage1
2d840 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 20 = pBt->pPage1;
2d850 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 72 65 /* Local re
2d860 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 ference to page
2d870 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 1 */. MemPage *
2d880 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 pPage;
2d890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
2d8a0 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 2e 20 ge being freed.
2d8b0 4d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a May be NULL. */.
2d8c0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
2d8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d8e0 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
2d8f0 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 Code */. int nF
2d900 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ree;
2d910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2d920 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 Initial number
2d930 6f 66 20 70 61 67 65 73 20 6f 6e 20 66 72 65 65 of pages on free
2d940 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 -list */.. asse
2d950 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
2d960 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
2d970 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
2d980 69 50 61 67 65 3e 31 20 29 3b 0a 20 20 61 73 73 iPage>1 );. ass
2d990 65 72 74 28 20 21 70 4d 65 6d 50 61 67 65 20 7c ert( !pMemPage |
2d9a0 7c 20 70 4d 65 6d 50 61 67 65 2d 3e 70 67 6e 6f | pMemPage->pgno
2d9b0 3d 3d 69 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 ==iPage );.. if
2d9c0 28 20 70 4d 65 6d 50 61 67 65 20 29 7b 0a 20 20 ( pMemPage ){.
2d9d0 20 20 70 50 61 67 65 20 3d 20 70 4d 65 6d 50 61 pPage = pMemPa
2d9e0 67 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ge;. sqlite3P
2d9f0 61 67 65 72 52 65 66 28 70 50 61 67 65 2d 3e 70 agerRef(pPage->p
2da00 44 62 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 DbPage);. }else
2da10 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 62 74 {. pPage = bt
2da20 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 reePageLookup(pB
2da30 74 2c 20 69 50 61 67 65 29 3b 0a 20 20 7d 0a 0a t, iPage);. }..
2da40 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 /* Increment t
2da50 68 65 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 he free page cou
2da60 6e 74 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a nt on pPage1 */.
2da70 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2da80 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d gerWrite(pPage1-
2da90 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
2daa0 20 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 rc ) goto freep
2dab0 61 67 65 5f 6f 75 74 3b 0a 20 20 6e 46 72 65 65 age_out;. nFree
2dac0 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
2dad0 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b ge1->aData[36]);
2dae0 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 . put4byte(&pPa
2daf0 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 ge1->aData[36],
2db00 6e 46 72 65 65 2b 31 29 3b 0a 0a 20 20 69 66 28 nFree+1);.. if(
2db10 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 pBt->btsFlags &
2db20 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 BTS_SECURE_DELE
2db30 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 TE ){. /* If
2db40 74 68 65 20 73 65 63 75 72 65 5f 64 65 6c 65 74 the secure_delet
2db50 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 e option is enab
2db60 6c 65 64 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a led, then. **
2db70 20 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76 always fully ov
2db80 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 erwrite deleted
2db90 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 information with
2dba0 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 zeros.. */.
2dbb0 20 20 20 69 66 28 20 28 21 70 50 61 67 65 20 26 if( (!pPage &
2dbc0 26 20 28 28 72 63 20 3d 20 62 74 72 65 65 47 65 & ((rc = btreeGe
2dbd0 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65 tPage(pBt, iPage
2dbe0 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 , &pPage, 0))!=0
2dbf0 29 20 29 0a 20 20 20 20 20 7c 7c 20 20 20 20 20 ) ). ||
2dc00 20 20 20 20 20 20 20 28 28 72 63 20 3d 20 73 71 ((rc = sq
2dc10 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
2dc20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 pPage->pDbPage))
2dc30 21 3d 30 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 !=0). ){.
2dc40 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
2dc50 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d out;. }. m
2dc60 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 emset(pPage->aDa
2dc70 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 ta, 0, pPage->pB
2dc80 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 t->pageSize);.
2dc90 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 }.. /* If the d
2dca0 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 atabase supports
2dcb0 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 auto-vacuum, wr
2dcc0 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 ite an entry in
2dcd0 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
2dce0 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 ** to indicate
2dcf0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 that the page i
2dd00 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 s free.. */. i
2dd10 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
2dd20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 ){. ptrmapPut
2dd30 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 (pBt, iPage, PTR
2dd40 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c MAP_FREEPAGE, 0,
2dd50 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 &rc);. if( r
2dd60 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 c ) goto freepag
2dd70 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a e_out;. }.. /*
2dd80 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 Now manipulate
2dd90 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 the actual datab
2dda0 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 ase free-list st
2ddb0 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 ructure. There a
2ddc0 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 re two. ** poss
2ddd0 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 ibilities. If th
2dde0 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 e free-list is c
2ddf0 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 urrently empty,
2de00 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74 0a or if the first.
2de10 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 ** trunk page
2de20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 in the free-list
2de30 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 is full, then t
2de40 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 his page will be
2de50 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 come a. ** new
2de60 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 free-list trunk
2de70 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c page. Otherwise,
2de80 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 it will become
2de90 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 a leaf of the.
2dea0 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ** first trunk p
2deb0 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 age in the curre
2dec0 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 nt free-list. Th
2ded0 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 is block tests i
2dee0 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 f it. ** is pos
2def0 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 sible to add the
2df00 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 page as a new f
2df10 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 ree-list leaf..
2df20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 */. if( nFree!
2df30 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c =0 ){. u32 nL
2df40 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 eaf;
2df50 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
2df60 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 umber of leaf ce
2df70 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 lls on trunk pag
2df80 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b e */.. iTrunk
2df90 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
2dfa0 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b ge1->aData[32]);
2dfb0 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
2dfc0 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 etPage(pBt, iTru
2dfd0 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b nk, &pTrunk, 0);
2dfe0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
2dff0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2e000 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 goto freepage_ou
2e010 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c t;. }.. nL
2e020 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 eaf = get4byte(&
2e030 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d pTrunk->aData[4]
2e040 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
2e050 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 Bt->usableSize>3
2e060 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 2 );. if( nLe
2e070 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 af > (u32)pBt->u
2e080 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 sableSize/4 - 2
2e090 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
2e0a0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
2e0b0 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 T;. goto fr
2e0c0 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 eepage_out;.
2e0d0 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 }. if( nLeaf
2e0e0 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 < (u32)pBt->usab
2e0f0 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a leSize/4 - 8 ){.
2e100 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 /* In this
2e110 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 72 case there is r
2e120 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b oom on the trunk
2e130 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 page to insert
2e140 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a the page. *
2e150 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 * being freed as
2e160 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 a new leaf..
2e170 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e **. ** N
2e180 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75 ote that the tru
2e190 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 nk page is not r
2e1a0 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c eally full until
2e1b0 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
2e1c0 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 ** usableSize
2e1d0 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 /4 - 2 entries,
2e1e0 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 not usableSize/4
2e1f0 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 - 8 entries as
2e200 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a we have. **
2e210 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 coded. But due
2e220 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 to a coding err
2e230 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f or in versions o
2e240 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 f SQLite prior t
2e250 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 o. ** 3.6.0
2e260 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 , databases with
2e270 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 freelist trunk
2e280 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f pages holding mo
2e290 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a re than. **
2e2a0 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
2e2b0 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 8 entries will b
2e2c0 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f e reported as co
2e2d0 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 rrupt. In order
2e2e0 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 . ** to mai
2e2f0 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 ntain backwards
2e300 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 compatibility wi
2e310 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e th older version
2e320 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 s of SQLite,.
2e330 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f ** we will co
2e340 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 ntinue to restri
2e350 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ct the number of
2e360 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 entries to usab
2e370 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 leSize/4 - 8.
2e380 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 ** for now.
2e390 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e At some point in
2e3a0 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 the future (onc
2e3b0 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 e everyone has u
2e3c0 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a pgraded. **
2e3d0 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 to 3.6.0 or lat
2e3e0 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f er) we should co
2e3f0 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 nsider fixing th
2e400 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 e conditional ab
2e410 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 ove. ** to
2e420 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 read "usableSize
2e430 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 /4-2" instead of
2e440 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 "usableSize/4-8
2e450 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ".. */.
2e460 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2e470 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d gerWrite(pTrunk-
2e480 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
2e490 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2e4a0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 OK ){. pu
2e4b0 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
2e4c0 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b aData[4], nLeaf+
2e4d0 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 1);. put4
2e4e0 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
2e4f0 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 ata[8+nLeaf*4],
2e500 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 iPage);.
2e510 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 42 if( pPage && (pB
2e520 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
2e530 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 S_SECURE_DELETE)
2e540 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
2e550 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
2e560 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 tWrite(pPage->pD
2e570 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
2e580 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 }. rc = b
2e590 74 72 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e treeSetHasConten
2e5a0 74 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 t(pBt, iPage);.
2e5b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 41 }. TRA
2e5c0 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 CE(("FREE-PAGE:
2e5d0 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b %d leaf on trunk
2e5e0 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 page %d\n",pPag
2e5f0 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e e->pgno,pTrunk->
2e600 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 67 6f pgno));. go
2e610 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
2e620 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
2e630 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 If control flow
2e640 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c s to this point,
2e650 20 74 68 65 6e 20 69 74 20 77 61 73 20 6e 6f 74 then it was not
2e660 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 possible to add
2e670 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 the. ** the pa
2e680 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 ge being freed a
2e690 73 20 61 20 6c 65 61 66 20 70 61 67 65 20 6f 66 s a leaf page of
2e6a0 20 74 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b the first trunk
2e6b0 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
2e6c0 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 69 62 6c 79 t.. ** Possibly
2e6d0 20 62 65 63 61 75 73 65 20 74 68 65 20 66 72 65 because the fre
2e6e0 65 2d 6c 69 73 74 20 69 73 20 65 6d 70 74 79 2c e-list is empty,
2e6f0 20 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 63 or possibly bec
2e700 61 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 66 ause the . ** f
2e710 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 irst trunk in th
2e720 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 e free-list is f
2e730 75 6c 6c 2e 20 45 69 74 68 65 72 20 77 61 79 2c ull. Either way,
2e740 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 the page being
2e750 66 72 65 65 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 freed. ** will
2e760 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 66 become the new f
2e770 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 irst trunk page
2e780 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 in the free-list
2e790 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
2e7a0 67 65 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f ge==0 && SQLITE_
2e7b0 4f 4b 21 3d 28 72 63 20 3d 20 62 74 72 65 65 47 OK!=(rc = btreeG
2e7c0 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 etPage(pBt, iPag
2e7d0 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 20 29 e, &pPage, 0)) )
2e7e0 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 {. goto freep
2e7f0 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 72 age_out;. }. r
2e800 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
2e810 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
2e820 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 Page);. if( rc!
2e830 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
2e840 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
2e850 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 75 74 34 62 out;. }. put4b
2e860 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 yte(pPage->aData
2e870 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 20 70 75 74 , iTrunk);. put
2e880 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
2e890 61 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 20 70 75 ata[4], 0);. pu
2e8a0 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
2e8b0 61 44 61 74 61 5b 33 32 5d 2c 20 69 50 61 67 65 aData[32], iPage
2e8c0 29 3b 0a 20 20 54 52 41 43 45 28 28 22 46 52 45 );. TRACE(("FRE
2e8d0 45 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 74 E-PAGE: %d new t
2e8e0 72 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 63 runk page replac
2e8f0 69 6e 67 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 ing %d\n", pPage
2e900 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 6e 6b 29 29 ->pgno, iTrunk))
2e910 3b 0a 0a 66 72 65 65 70 61 67 65 5f 6f 75 74 3a ;..freepage_out:
2e920 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a . if( pPage ){.
2e930 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 pPage->isIni
2e940 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 6c t = 0;. }. rel
2e950 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
2e960 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
2e970 54 72 75 6e 6b 29 3b 0a 20 20 72 65 74 75 72 6e Trunk);. return
2e980 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f rc;.}.static vo
2e990 69 64 20 66 72 65 65 50 61 67 65 28 4d 65 6d 50 id freePage(MemP
2e9a0 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
2e9b0 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 *pRC){. if( (*p
2e9c0 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 RC)==SQLITE_OK )
2e9d0 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 66 72 65 {. *pRC = fre
2e9e0 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 42 ePage2(pPage->pB
2e9f0 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 2d t, pPage, pPage-
2ea00 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f >pgno);. }.}../
2ea10 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 *.** Free any ov
2ea20 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 erflow pages ass
2ea30 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
2ea40 20 67 69 76 65 6e 20 43 65 6c 6c 2e 20 20 57 72 given Cell. Wr
2ea50 69 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 61 6c ite the.** local
2ea60 20 43 65 6c 6c 20 73 69 7a 65 20 28 74 68 65 20 Cell size (the
2ea70 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
2ea80 6f 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 on the original
2ea90 70 61 67 65 2c 20 6f 6d 69 74 74 69 6e 67 0a 2a page, omitting.*
2eaa0 2a 20 6f 76 65 72 66 6c 6f 77 29 20 69 6e 74 6f * overflow) into
2eab0 20 2a 70 6e 53 69 7a 65 2e 0a 2a 2f 0a 73 74 61 *pnSize..*/.sta
2eac0 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c tic int clearCel
2ead0 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 l(. MemPage *pP
2eae0 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a age, /*
2eaf0 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 The page that c
2eb00 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 65 6c 6c ontains the Cell
2eb10 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
2eb20 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 2f har *pCell, /
2eb30 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
2eb40 74 68 65 20 43 65 6c 6c 20 2a 2f 0a 20 20 75 31 the Cell */. u1
2eb50 36 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 6 *pnSize
2eb60 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
2eb70 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
2eb80 43 65 6c 6c 20 68 65 72 65 20 2a 2f 0a 29 7b 0a Cell here */.){.
2eb90 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
2eba0 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
2ebb0 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
2ebc0 20 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a Pgno ovflPgno;.
2ebd0 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 int rc;. int
2ebe0 6e 4f 76 66 6c 3b 0a 20 20 75 33 32 20 6f 76 66 nOvfl;. u32 ovf
2ebf0 6c 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73 lPageSize;.. as
2ec00 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
2ec10 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
2ec20 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
2ec30 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
2ec40 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
2ec50 20 26 69 6e 66 6f 29 3b 0a 20 20 2a 70 6e 53 69 &info);. *pnSi
2ec60 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b ze = info.nSize;
2ec70 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 . if( info.iOve
2ec80 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 rflow==0 ){.
2ec90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
2eca0 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f ; /* No overflo
2ecb0 77 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 w pages. Return
2ecc0 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
2ecd0 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 ything */. }.
2ece0 69 66 28 20 70 43 65 6c 6c 2b 69 6e 66 6f 2e 69 if( pCell+info.i
2ecf0 4f 76 65 72 66 6c 6f 77 2b 33 20 3e 20 70 50 61 Overflow+3 > pPa
2ed00 67 65 2d 3e 61 44 61 74 61 2b 70 50 61 67 65 2d ge->aData+pPage-
2ed10 3e 6d 61 73 6b 50 61 67 65 20 29 7b 0a 20 20 20 >maskPage ){.
2ed20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
2ed30 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 20 2f 2a ORRUPT_BKPT; /*
2ed40 20 43 65 6c 6c 20 65 78 74 65 6e 64 73 20 70 61 Cell extends pa
2ed50 73 74 20 65 6e 64 20 6f 66 20 70 61 67 65 20 2a st end of page *
2ed60 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f /. }. ovflPgno
2ed70 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
2ed80 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
2ed90 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 w]);. assert( p
2eda0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e Bt->usableSize >
2edb0 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 4 );. ovflPage
2edc0 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
2edd0 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f leSize - 4;. nO
2ede0 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 vfl = (info.nPay
2edf0 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 load - info.nLoc
2ee00 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a al + ovflPageSiz
2ee10 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 e - 1)/ovflPageS
2ee20 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f ize;. assert( o
2ee30 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f vflPgno==0 || nO
2ee40 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 vfl>0 );. while
2ee50 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 ( nOvfl-- ){.
2ee60 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b Pgno iNext = 0;
2ee70 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f . MemPage *pO
2ee80 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 vfl = 0;. if(
2ee90 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f ovflPgno<2 || o
2eea0 76 66 6c 50 67 6e 6f 3e 62 74 72 65 65 50 61 67 vflPgno>btreePag
2eeb0 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 ecount(pBt) ){.
2eec0 20 20 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f 74 /* 0 is not
2eed0 20 61 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 a legal page nu
2eee0 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 31 20 mber and page 1
2eef0 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 20 20 cannot be an .
2ef00 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 ** overflow
2ef10 70 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 20 page. Therefore
2ef20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f 72 if ovflPgno<2 or
2ef30 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
2ef40 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 the . ** f
2ef50 69 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ile the database
2ef60 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 must be corrupt
2ef70 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 . */. retur
2ef80 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
2ef90 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
2efa0 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a 20 20 if( nOvfl ){.
2efb0 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 rc = getOver
2efc0 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76 flowPage(pBt, ov
2efd0 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 flPgno, &pOvfl,
2efe0 26 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 69 &iNext);. i
2eff0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
2f000 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 c;. }.. if
2f010 28 20 28 20 70 4f 76 66 6c 20 7c 7c 20 28 28 70 ( ( pOvfl || ((p
2f020 4f 76 66 6c 20 3d 20 62 74 72 65 65 50 61 67 65 Ovfl = btreePage
2f030 4c 6f 6f 6b 75 70 28 70 42 74 2c 20 6f 76 66 6c Lookup(pBt, ovfl
2f040 50 67 6e 6f 29 29 21 3d 30 29 20 29 0a 20 20 20 Pgno))!=0) ).
2f050 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 && sqlite3Page
2f060 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 4f rPageRefcount(pO
2f070 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 21 3d 31 vfl->pDbPage)!=1
2f080 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a . ){. /*
2f090 20 54 68 65 72 65 20 69 73 20 6e 6f 20 72 65 61 There is no rea
2f0a0 73 6f 6e 20 61 6e 79 20 63 75 72 73 6f 72 20 73 son any cursor s
2f0b0 68 6f 75 6c 64 20 68 61 76 65 20 61 6e 20 6f 75 hould have an ou
2f0c0 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 tstanding refere
2f0d0 6e 63 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f nce . ** to
2f0e0 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
2f0f0 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 e belonging to a
2f100 20 63 65 6c 6c 20 74 68 61 74 20 69 73 20 62 65 cell that is be
2f110 69 6e 67 20 64 65 6c 65 74 65 64 2f 75 70 64 61 ing deleted/upda
2f120 74 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 53 6f ted.. ** So
2f130 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74 73 if there exists
2f140 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 72 more than one r
2f150 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 69 73 eference to this
2f160 20 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 0a page, then it .
2f170 20 20 20 20 20 20 2a 2a 20 6d 75 73 74 20 6e 6f ** must no
2f180 74 20 72 65 61 6c 6c 79 20 62 65 20 61 6e 20 6f t really be an o
2f190 76 65 72 66 6c 6f 77 20 70 61 67 65 20 61 6e 64 verflow page and
2f1a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 the database mu
2f1b0 73 74 20 62 65 20 63 6f 72 72 75 70 74 2e 20 0a st be corrupt. .
2f1c0 20 20 20 20 20 20 2a 2a 20 49 74 20 69 73 20 68 ** It is h
2f1d0 65 6c 70 66 75 6c 20 74 6f 20 64 65 74 65 63 74 elpful to detect
2f1e0 20 74 68 69 73 20 62 65 66 6f 72 65 20 63 61 6c this before cal
2f1f0 6c 69 6e 67 20 66 72 65 65 50 61 67 65 32 28 29 ling freePage2()
2f200 2c 20 61 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 , as . ** f
2f210 72 65 65 50 61 67 65 32 28 29 20 6d 61 79 20 7a reePage2() may z
2f220 65 72 6f 20 74 68 65 20 70 61 67 65 20 63 6f 6e ero the page con
2f230 74 65 6e 74 73 20 69 66 20 73 65 63 75 72 65 2d tents if secure-
2f240 64 65 6c 65 74 65 20 6d 6f 64 65 20 69 73 0a 20 delete mode is.
2f250 20 20 20 20 20 2a 2a 20 65 6e 61 62 6c 65 64 2e ** enabled.
2f260 20 49 66 20 74 68 69 73 20 27 6f 76 65 72 66 6c If this 'overfl
2f270 6f 77 27 20 70 61 67 65 20 68 61 70 70 65 6e 73 ow' page happens
2f280 20 74 6f 20 62 65 20 61 20 70 61 67 65 20 74 68 to be a page th
2f290 61 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 at the. **
2f2a0 63 61 6c 6c 65 72 20 69 73 20 69 74 65 72 61 74 caller is iterat
2f2b0 69 6e 67 20 74 68 72 6f 75 67 68 20 6f 72 20 75 ing through or u
2f2c0 73 69 6e 67 20 69 6e 20 73 6f 6d 65 20 6f 74 68 sing in some oth
2f2d0 65 72 20 77 61 79 2c 20 74 68 69 73 0a 20 20 20 er way, this.
2f2e0 20 20 20 2a 2a 20 63 61 6e 20 62 65 20 70 72 6f ** can be pro
2f2f0 62 6c 65 6d 61 74 69 63 2e 0a 20 20 20 20 20 20 blematic..
2f300 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 */. rc = SQ
2f310 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
2f320 54 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 T;. }else{.
2f330 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 rc = freePag
2f340 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f e2(pBt, pOvfl, o
2f350 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 0a vflPgno);. }.
2f360 0a 20 20 20 20 69 66 28 20 70 4f 76 66 6c 20 29 . if( pOvfl )
2f370 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 {. sqlite3P
2f380 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c 2d agerUnref(pOvfl-
2f390 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d >pDbPage);. }
2f3a0 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
2f3b0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6f 76 66 turn rc;. ovf
2f3c0 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a 20 lPgno = iNext;.
2f3d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
2f3e0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
2f3f0 43 72 65 61 74 65 20 74 68 65 20 62 79 74 65 20 Create the byte
2f400 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 74 6f sequence used to
2f410 20 72 65 70 72 65 73 65 6e 74 20 61 20 63 65 6c represent a cel
2f420 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 0a l on page pPage.
2f430 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 61 ** and write tha
2f440 74 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 t byte sequence
2f450 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f into pCell[]. O
2f460 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 verflow pages ar
2f470 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 e.** allocated a
2f480 6e 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 nd filled in as
2f490 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 20 necessary. The
2f4a0 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 calling procedur
2f4b0 65 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 e.** is responsi
2f4c0 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 ble for making s
2f4d0 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 20 73 ure sufficient s
2f4e0 70 61 63 65 20 68 61 73 20 62 65 65 6e 20 61 6c pace has been al
2f4f0 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 located.** for p
2f500 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f Cell[]..**.** No
2f510 74 65 20 74 68 61 74 20 70 43 65 6c 6c 20 64 6f te that pCell do
2f520 65 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 es not necessary
2f530 20 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 need to point t
2f540 6f 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 o the pPage->aDa
2f550 74 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 ta.** area. pCe
2f560 6c 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 ll might point t
2f570 6f 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 o some temporary
2f580 20 73 74 6f 72 61 67 65 2e 20 20 54 68 65 20 63 storage. The c
2f590 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 ell will.** be c
2f5a0 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20 74 68 onstructed in th
2f5b0 69 73 20 74 65 6d 70 6f 72 61 72 79 20 61 72 65 is temporary are
2f5c0 61 20 74 68 65 6e 20 63 6f 70 69 65 64 20 69 6e a then copied in
2f5d0 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a to pPage->aData.
2f5e0 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 ** later..*/.sta
2f5f0 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 tic int fillInCe
2f600 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ll(. MemPage *p
2f610 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
2f620 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
2f630 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
2f640 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 he cell */. uns
2f650 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c igned char *pCel
2f660 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 l, /* C
2f670 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 omplete text of
2f680 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f the cell */. co
2f690 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
2f6a0 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 i64 nKey, /*
2f6b0 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e The key */. con
2f6c0 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 st void *pData,i
2f6d0 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 nt nData, /* T
2f6e0 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 he data */. int
2f6f0 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 nZero,
2f700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
2f710 78 74 72 61 20 7a 65 72 6f 20 62 79 74 65 73 20 xtra zero bytes
2f720 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 44 61 to append to pDa
2f730 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 ta */. int *pnS
2f740 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ize
2f750 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
2f760 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 20 2a cell size here *
2f770 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c /.){. int nPayl
2f780 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 oad;. const u8
2f790 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 *pSrc;. int nSr
2f7a0 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 c, n, rc;. int
2f7b0 73 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d spaceLeft;. Mem
2f7c0 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b Page *pOvfl = 0;
2f7d0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 . MemPage *pToR
2f7e0 65 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e elease = 0;. un
2f7f0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 72 signed char *pPr
2f800 69 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 ior;. unsigned
2f810 63 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a char *pPayload;.
2f820 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
2f830 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
2f840 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 Pgno pgnoOvfl =
2f850 30 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 65 72 0;. int nHeader
2f860 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
2f870 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
2f880 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
2f890 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 x) );.. /* pPag
2f8a0 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 e is not necessa
2f8b0 72 69 6c 79 20 77 72 69 74 65 61 62 6c 65 20 73 rily writeable s
2f8c0 69 6e 63 65 20 70 43 65 6c 6c 20 6d 69 67 68 74 ince pCell might
2f8d0 20 62 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 be auxiliary.
2f8e0 2a 2a 20 62 75 66 66 65 72 20 73 70 61 63 65 20 ** buffer space
2f8f0 74 68 61 74 20 69 73 20 73 65 70 61 72 61 74 65 that is separate
2f900 20 66 72 6f 6d 20 74 68 65 20 70 50 61 67 65 20 from the pPage
2f910 62 75 66 66 65 72 20 61 72 65 61 20 2a 2f 0a 20 buffer area */.
2f920 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c 3c 70 assert( pCell<p
2f930 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 Page->aData || p
2f940 43 65 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e 61 44 Cell>=&pPage->aD
2f950 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a ata[pBt->pageSiz
2f960 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c e]. |
2f970 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 | sqlite3PagerIs
2f980 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
2f990 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
2f9a0 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 68 /* Fill in the h
2f9b0 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 65 61 eader. */. nHea
2f9c0 64 65 72 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 der = pPage->chi
2f9d0 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 6e 50 61 ldPtrSize;. nPa
2f9e0 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b 20 yload = nData +
2f9f0 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50 61 nZero;. if( pPa
2fa00 67 65 2d 3e 69 6e 74 4b 65 79 4c 65 61 66 20 29 ge->intKeyLeaf )
2fa10 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d {. nHeader +=
2fa20 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 70 43 putVarint32(&pC
2fa30 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 50 ell[nHeader], nP
2fa40 61 79 6c 6f 61 64 29 3b 0a 20 20 7d 65 6c 73 65 ayload);. }else
2fa50 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 44 {. assert( nD
2fa60 61 74 61 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 ata==0 );. as
2fa70 73 65 72 74 28 20 6e 5a 65 72 6f 3d 3d 30 20 29 sert( nZero==0 )
2fa80 3b 0a 20 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 ;. }. nHeader
2fa90 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 += putVarint(&pC
2faa0 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 ell[nHeader], *(
2fab0 75 36 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 0a u64*)&nKey);. .
2fac0 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
2fad0 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 20 2a 2f payload size */
2fae0 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e . if( pPage->in
2faf0 74 4b 65 79 20 29 7b 0a 20 20 20 20 70 53 72 63 tKey ){. pSrc
2fb00 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 6e 53 = pData;. nS
2fb10 72 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 rc = nData;.
2fb20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 65 6c nData = 0;. }el
2fb30 73 65 7b 20 0a 20 20 20 20 69 66 28 20 4e 45 56 se{ . if( NEV
2fb40 45 52 28 6e 4b 65 79 3e 30 78 37 66 66 66 66 66 ER(nKey>0x7fffff
2fb50 66 66 20 7c 7c 20 70 4b 65 79 3d 3d 30 29 20 29 ff || pKey==0) )
2fb60 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
2fb70 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
2fb80 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 PT;. }. nP
2fb90 61 79 6c 6f 61 64 20 3d 20 28 69 6e 74 29 6e 4b ayload = (int)nK
2fba0 65 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 ey;. pSrc = p
2fbb0 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 Key;. nSrc =
2fbc0 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 (int)nKey;. }.
2fbd0 20 69 66 28 20 6e 50 61 79 6c 6f 61 64 3c 3d 70 if( nPayload<=p
2fbe0 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 Page->maxLocal )
2fbf0 7b 0a 20 20 20 20 6e 20 3d 20 6e 48 65 61 64 65 {. n = nHeade
2fc00 72 20 2b 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 r + nPayload;.
2fc10 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 33 testcase( n==3
2fc20 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
2fc30 28 20 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 69 66 ( n==4 );. if
2fc40 28 20 6e 3c 34 20 29 20 6e 20 3d 20 34 3b 0a 20 ( n<4 ) n = 4;.
2fc50 20 20 20 2a 70 6e 53 69 7a 65 20 3d 20 6e 3b 0a *pnSize = n;.
2fc60 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 spaceLeft =
2fc70 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 50 nPayload;. pP
2fc80 72 69 6f 72 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 rior = pCell;.
2fc90 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6d }else{. int m
2fca0 6e 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f n = pPage->minLo
2fcb0 63 61 6c 3b 0a 20 20 20 20 6e 20 3d 20 6d 6e 20 cal;. n = mn
2fcc0 2b 20 28 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 6e + (nPayload - mn
2fcd0 29 20 25 20 28 70 50 61 67 65 2d 3e 70 42 74 2d ) % (pPage->pBt-
2fce0 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 >usableSize - 4)
2fcf0 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
2fd00 6e 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 n==pPage->maxLoc
2fd10 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 al );. testca
2fd20 73 65 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 6d 61 se( n==pPage->ma
2fd30 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 xLocal+1 );.
2fd40 69 66 28 20 6e 20 3e 20 70 50 61 67 65 2d 3e 6d if( n > pPage->m
2fd50 61 78 4c 6f 63 61 6c 20 29 20 6e 20 3d 20 6d 6e axLocal ) n = mn
2fd60 3b 0a 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 ;. spaceLeft
2fd70 3d 20 6e 3b 0a 20 20 20 20 2a 70 6e 53 69 7a 65 = n;. *pnSize
2fd80 20 3d 20 6e 20 2b 20 6e 48 65 61 64 65 72 20 2b = n + nHeader +
2fd90 20 34 3b 0a 20 20 20 20 70 50 72 69 6f 72 20 3d 4;. pPrior =
2fda0 20 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 2b &pCell[nHeader+
2fdb0 6e 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 79 6c 6f n];. }. pPaylo
2fdc0 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 61 ad = &pCell[nHea
2fdd0 64 65 72 5d 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 der];.. /* At t
2fde0 68 69 73 20 70 6f 69 6e 74 20 76 61 72 69 61 62 his point variab
2fdf0 6c 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 65 les should be se
2fe00 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 t as follows:.
2fe10 2a 2a 0a 20 20 2a 2a 20 20 20 6e 50 61 79 6c 6f **. ** nPaylo
2fe20 61 64 20 20 20 20 20 20 20 20 20 20 20 54 6f 74 ad Tot
2fe30 61 6c 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 20 al payload size
2fe40 69 6e 20 62 79 74 65 73 0a 20 20 2a 2a 20 20 20 in bytes. **
2fe50 70 50 61 79 6c 6f 61 64 20 20 20 20 20 20 20 20 pPayload
2fe60 20 20 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 Begin writing
2fe70 20 70 61 79 6c 6f 61 64 20 68 65 72 65 0a 20 20 payload here.
2fe80 2a 2a 20 20 20 73 70 61 63 65 4c 65 66 74 20 20 ** spaceLeft
2fe90 20 20 20 20 20 20 20 20 53 70 61 63 65 20 61 76 Space av
2fea0 61 69 6c 61 62 6c 65 20 61 74 20 70 50 61 79 6c ailable at pPayl
2feb0 6f 61 64 2e 20 20 49 66 20 6e 50 61 79 6c 6f 61 oad. If nPayloa
2fec0 64 3e 73 70 61 63 65 4c 65 66 74 2c 0a 20 20 2a d>spaceLeft,. *
2fed0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
2fee0 20 20 20 20 20 20 20 74 68 61 74 20 6d 65 61 6e that mean
2fef0 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 73 s content must s
2ff00 70 69 6c 6c 20 69 6e 74 6f 20 6f 76 65 72 66 6c pill into overfl
2ff10 6f 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 20 ow pages.. **
2ff20 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 20 *pnSize
2ff30 20 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20 Size of the
2ff40 6c 6f 63 61 6c 20 63 65 6c 6c 20 28 6e 6f 74 20 local cell (not
2ff50 63 6f 75 6e 74 69 6e 67 20 6f 76 65 72 66 6c 6f counting overflo
2ff60 77 20 70 61 67 65 73 29 0a 20 20 2a 2a 20 20 20 w pages). **
2ff70 70 50 72 69 6f 72 20 20 20 20 20 20 20 20 20 20 pPrior
2ff80 20 20 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 Where to writ
2ff90 65 20 74 68 65 20 70 67 6e 6f 20 6f 66 20 74 68 e the pgno of th
2ffa0 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 e first overflow
2ffb0 20 70 61 67 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 page. **. **
2ffc0 55 73 65 20 61 20 63 61 6c 6c 20 74 6f 20 62 74 Use a call to bt
2ffd0 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
2ffe0 29 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 ) to verify that
2fff0 20 74 68 65 20 76 61 6c 75 65 73 20 61 62 6f 76 the values abov
30000 65 0a 20 20 2a 2a 20 77 65 72 65 20 63 6f 6d 70 e. ** were comp
30010 75 74 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a uted correctly..
30020 20 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f */.#if SQLITE_
30030 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20 43 65 DEBUG. {. Ce
30040 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 llInfo info;.
30050 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
30060 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
30070 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 &info);. ass
30080 65 72 74 28 20 6e 48 65 61 64 65 72 3d 28 69 6e ert( nHeader=(in
30090 74 29 28 69 6e 66 6f 2e 70 50 61 79 6c 6f 61 64 t)(info.pPayload
300a0 20 2d 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20 20 - pCell) );.
300b0 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b assert( info.nK
300c0 65 79 3d 3d 6e 4b 65 79 20 29 3b 0a 20 20 20 20 ey==nKey );.
300d0 61 73 73 65 72 74 28 20 2a 70 6e 53 69 7a 65 20 assert( *pnSize
300e0 3d 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 20 29 3b == info.nSize );
300f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 70 61 . assert( spa
30100 63 65 4c 65 66 74 20 3d 3d 20 69 6e 66 6f 2e 6e ceLeft == info.n
30110 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20 61 73 73 Local );. ass
30120 65 72 74 28 20 70 50 72 69 6f 72 20 3d 3d 20 26 ert( pPrior == &
30130 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 pCell[info.iOver
30140 66 6c 6f 77 5d 20 29 3b 0a 20 20 7d 0a 23 65 6e flow] );. }.#en
30150 64 69 66 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 dif.. /* Write
30160 74 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 74 6f the payload into
30170 20 74 68 65 20 6c 6f 63 61 6c 20 43 65 6c 6c 20 the local Cell
30180 61 6e 64 20 61 6e 79 20 65 78 74 72 61 20 69 6e and any extra in
30190 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
301a0 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 50 s */. while( nP
301b0 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20 ayload>0 ){.
301c0 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30 if( spaceLeft==0
301d0 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){.#ifndef SQLI
301e0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
301f0 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 UM. Pgno pg
30200 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f noPtrmap = pgnoO
30210 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 vfl; /* Overflow
30220 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 page pointer-ma
30230 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a p entry page */.
30240 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 if( pBt->a
30250 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
30260 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 do{.
30270 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 pgnoOvfl++;.
30280 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 } while(
30290 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 . PTRMA
302a0 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 P_ISPAGE(pBt, pg
302b0 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f noOvfl) || pgnoO
302c0 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 vfl==PENDING_BYT
302d0 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 E_PAGE(pBt) .
302e0 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a );. }.
302f0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 #endif. rc
30300 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
30310 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c age(pBt, &pOvfl,
30320 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f &pgnoOvfl, pgno
30330 4f 76 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 Ovfl, 0);.#ifnde
30340 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
30350 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f TOVACUUM. /
30360 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
30370 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d e supports auto-
30380 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 vacuum, and the
30390 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 second or subseq
303a0 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 uent. ** ov
303b0 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 erflow page is b
303c0 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 eing allocated,
303d0 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 add an entry to
303e0 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
303f0 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 ** for tha
30400 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 t page now. .
30410 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 **. ** I
30420 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
30430 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 rst overflow pag
30440 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 e, then write a
30450 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 partial entry .
30460 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 ** to the p
30470 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 ointer-map. If w
30480 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 e write nothing
30490 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d to this pointer-
304a0 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 map slot,.
304b0 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 ** then the opti
304c0 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 mistic overflow
304d0 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 chain processing
304e0 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a in clearCell().
304f0 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 ** may mis
30500 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 75 6e interpret the un
30510 69 6e 69 74 69 61 6c 69 7a 65 64 20 76 61 6c 75 initialized valu
30520 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 es and delete th
30530 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 e. ** wrong
30540 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
30550 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 database..
30560 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 */. if( pBt
30570 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 ->autoVacuum &&
30580 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
30590 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 79 70 . u8 eTyp
305a0 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f e = (pgnoPtrmap?
305b0 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
305c0 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 :PTRMAP_OVERFLOW
305d0 31 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 1);. ptrm
305e0 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f apPut(pBt, pgnoO
305f0 76 66 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f vfl, eType, pgno
30600 50 74 72 6d 61 70 2c 20 26 72 63 29 3b 0a 20 20 Ptrmap, &rc);.
30610 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
30620 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
30630 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 ePage(pOvfl);.
30640 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
30650 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 #endif. if(
30660 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 rc ){. r
30670 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 eleasePage(pToRe
30680 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 lease);.
30690 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
306a0 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
306b0 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f pToRelease is no
306c0 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 t zero than pPri
306d0 6f 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 or points into t
306e0 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 he data area.
306f0 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 ** of pToRele
30700 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 ase. Make sure
30710 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 pToRelease is st
30720 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a ill writeable. *
30730 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 /. assert(
30740 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c pToRelease==0 ||
30750 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
30760 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 riteable(pToRele
30770 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ase->pDbPage) );
30780 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 .. /* If pP
30790 72 69 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 rior is part of
307a0 74 68 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 the data area of
307b0 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b pPage, then mak
307c0 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 e sure pPage.
307d0 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 ** is still w
307e0 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 riteable */.
307f0 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 assert( pPrior
30800 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c <pPage->aData ||
30810 20 70 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d pPrior>=&pPage-
30820 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 >aData[pBt->page
30830 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 Size].
30840 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 || sqlite3Page
30850 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
30860 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
30870 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 . put4byte(
30880 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c pPrior, pgnoOvfl
30890 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 );. release
308a0 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
308b0 3b 0a 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 ;. pToRelea
308c0 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 se = pOvfl;.
308d0 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c pPrior = pOvfl
308e0 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 ->aData;. p
308f0 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 ut4byte(pPrior,
30900 30 29 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 0);. pPaylo
30910 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 ad = &pOvfl->aDa
30920 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 ta[4];. spa
30930 63 65 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 ceLeft = pBt->us
30940 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 ableSize - 4;.
30950 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79 }. n = nPay
30960 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e load;. if( n>
30970 73 70 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20 spaceLeft ) n =
30980 73 70 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20 spaceLeft;..
30990 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 /* If pToRelease
309a0 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 is not zero tha
309b0 6e 20 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74 n pPayload point
309c0 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 s into the data
309d0 61 72 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70 area. ** of p
309e0 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 ToRelease. Make
309f0 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 sure pToRelease
30a00 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 is still writea
30a10 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 ble. */. asse
30a20 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d rt( pToRelease==
30a30 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 0 || sqlite3Page
30a40 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f rIswriteable(pTo
30a50 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 Release->pDbPage
30a60 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 ) );.. /* If
30a70 70 50 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74 pPayload is part
30a80 20 6f 66 20 74 68 65 20 64 61 74 61 20 61 72 65 of the data are
30a90 61 20 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e a of pPage, then
30aa0 20 6d 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 make sure pPage
30ab0 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c . ** is still
30ac0 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 writeable */.
30ad0 20 20 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f assert( pPaylo
30ae0 61 64 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 ad<pPage->aData
30af0 7c 7c 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 || pPayload>=&pP
30b00 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
30b10 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 pageSize].
30b20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
30b30 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
30b40 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
30b50 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72 );.. if( nSr
30b60 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 c>0 ){. if(
30b70 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 n>nSrc ) n = nS
30b80 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 rc;. assert
30b90 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 ( pSrc );.
30ba0 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c memcpy(pPayload,
30bb0 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d pSrc, n);. }
30bc0 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 else{. mems
30bd0 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 et(pPayload, 0,
30be0 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 n);. }. nP
30bf0 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 ayload -= n;.
30c00 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a pPayload += n;.
30c10 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 pSrc += n;.
30c20 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 nSrc -= n;.
30c30 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e spaceLeft -= n
30c40 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d ;. if( nSrc==
30c50 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 0 ){. nSrc
30c60 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 = nData;. p
30c70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 Src = pData;.
30c80 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 }. }. release
30c90 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
30ca0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
30cb0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
30cc0 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 emove the i-th c
30cd0 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 ell from pPage.
30ce0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 This routine ef
30cf0 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 fects pPage only
30d00 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f ..** The cell co
30d10 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 ntent is not fre
30d20 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 ed or deallocate
30d30 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 d. It is assume
30d40 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 d that.** the ce
30d50 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 ll content has b
30d60 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 een copied somep
30d70 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 lace else. This
30d80 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a routine just.**
30d90 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 removes the ref
30da0 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 erence to the ce
30db0 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a ll from pPage..*
30dc0 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 *.** "sz" must b
30dd0 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
30de0 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c bytes in the cel
30df0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 l..*/.static voi
30e00 64 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 d dropCell(MemPa
30e10 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 ge *pPage, int i
30e20 64 78 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 dx, int sz, int
30e30 2a 70 52 43 29 7b 0a 20 20 75 33 32 20 70 63 3b *pRC){. u32 pc;
30e40 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
30e50 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 et to cell conte
30e60 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 nt of cell being
30e70 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 deleted */. u8
30e80 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a *data; /*
30e90 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f pPage->aData */
30ea0 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 . u8 *ptr;
30eb0 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f /* Used to mo
30ec0 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 ve bytes around
30ed0 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f within data[] */
30ee0 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
30ef0 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e /* The return
30f00 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 code */. int h
30f10 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 dr; /* Be
30f20 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 ginning of the h
30f30 65 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 eader. 0 most p
30f40 61 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 ages. 100 page
30f50 31 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 1 */.. if( *pRC
30f60 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 ) return;.. as
30f70 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 sert( idx>=0 &&
30f80 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c idx<pPage->nCell
30f90 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a );. assert( sz
30fa0 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 ==cellSize(pPage
30fb0 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 , idx) );. asse
30fc0 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
30fd0 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
30fe0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
30ff0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
31000 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
31010 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
31020 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 ;. data = pPage
31030 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d ->aData;. ptr =
31040 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 &pPage->aCellId
31050 78 5b 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d x[2*idx];. pc =
31060 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a get2byte(ptr);.
31070 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 hdr = pPage->h
31080 64 72 4f 66 66 73 65 74 3b 0a 20 20 74 65 73 74 drOffset;. test
31090 63 61 73 65 28 20 70 63 3d 3d 67 65 74 32 62 79 case( pc==get2by
310a0 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
310b0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
310c0 70 63 2b 73 7a 3d 3d 70 50 61 67 65 2d 3e 70 42 pc+sz==pPage->pB
310d0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b t->usableSize );
310e0 0a 20 20 69 66 28 20 70 63 20 3c 20 28 75 33 32 . if( pc < (u32
310f0 29 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b )get2byte(&data[
31100 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 7a hdr+5]) || pc+sz
31110 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 > pPage->pBt->u
31120 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
31130 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 *pRC = SQLITE_C
31140 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
31150 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72 return;. }. r
31160 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 50 c = freeSpace(pP
31170 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20 age, pc, sz);.
31180 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 if( rc ){. *p
31190 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 RC = rc;. ret
311a0 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 urn;. }. pPage
311b0 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 6d 65 6d ->nCell--;. mem
311c0 6d 6f 76 65 28 70 74 72 2c 20 70 74 72 2b 32 2c move(ptr, ptr+2,
311d0 20 32 2a 28 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2*(pPage->nCell
311e0 20 2d 20 69 64 78 29 29 3b 0a 20 20 70 75 74 32 - idx));. put2
311f0 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
31200 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 ], pPage->nCell)
31210 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 ;. pPage->nFree
31220 20 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 += 2;.}../*.**
31230 49 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c Insert a new cel
31240 6c 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 l on pPage at ce
31250 6c 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 ll index "i". p
31260 43 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 Cell points to t
31270 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 he.** content of
31280 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a the cell..**.**
31290 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e If the cell con
312a0 74 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e tent will fit on
312b0 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 the page, then
312c0 70 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 put it there. I
312d0 66 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 f it.** will not
312e0 20 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 fit, then make
312f0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 a copy of the ce
31300 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 ll content into
31310 70 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d pTemp if.** pTem
31320 70 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 p is not null.
31330 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 Regardless of pT
31340 65 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 emp, allocate a
31350 6e 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 new entry.** in
31360 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 5d 20 pPage->apOvfl[]
31370 61 6e 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e and make it poin
31380 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f t to the cell co
31390 6e 74 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a ntent (either.**
313a0 20 69 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 in pTemp or the
313b0 20 6f 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 original pCell)
313c0 20 61 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 and also record
313d0 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 its index. .**
313e0 41 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 Allocating a new
313f0 20 65 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d entry in pPage-
31400 3e 61 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 >aCell[] implies
31410 20 74 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d that .** pPage-
31420 3e 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e >nOverflow is in
31430 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 73 74 cremented..*/.st
31440 61 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 atic void insert
31450 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 Cell(. MemPage
31460 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 *pPage, /* Pag
31470 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 e into which we
31480 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 are copying */.
31490 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 int i,
314a0 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 /* New cell b
314b0 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 20 ecomes the i-th
314c0 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 cell of the page
314d0 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
314e0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 /* Conte
314f0 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63 65 nt of the new ce
31500 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 ll */. int sz,
31510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
31520 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e es of content in
31530 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a pCell */. u8 *
31540 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a pTemp, /*
31550 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73 70 Temp storage sp
31560 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 ace for pCell, i
31570 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 50 67 f needed */. Pg
31580 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 no iChild,
31590 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 /* If non-zero,
315a0 72 65 70 6c 61 63 65 20 66 69 72 73 74 20 34 20 replace first 4
315b0 62 79 74 65 73 20 77 69 74 68 20 74 68 69 73 20 bytes with this
315c0 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a value */. int *
315d0 70 52 43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pRC /*
315e0 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 72 Read and write r
315f0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
31600 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
31610 20 69 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f idx = 0; /
31620 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 * Where to write
31630 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e new cell conten
31640 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 t in data[] */.
31650 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 int j;
31660 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
31670 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b er */. int end;
31680 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
31690 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 st byte past the
316a0 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 last cell point
316b0 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a er in data[] */.
316c0 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 int ins;
316d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
316e0 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 data[] where new
316f0 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 cell pointer is
31700 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 inserted */. i
31710 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 nt cellOffset;
31720 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 /* Address of f
31730 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 irst cell pointe
31740 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 r in data[] */.
31750 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 u8 *data;
31760 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e /* The conten
31770 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 t of the whole p
31780 61 67 65 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 age */.. if( *p
31790 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 RC ) return;..
317a0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
317b0 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b i<=pPage->nCell+
317c0 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
317d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 58 );. assert( MX
317e0 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 _CELL(pPage->pBt
317f0 29 3c 3d 31 30 39 32 31 20 29 3b 0a 20 20 61 73 )<=10921 );. as
31800 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 sert( pPage->nCe
31810 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 ll<=MX_CELL(pPag
31820 65 2d 3e 70 42 74 29 20 7c 7c 20 43 4f 52 52 55 e->pBt) || CORRU
31830 50 54 5f 44 42 20 29 3b 0a 20 20 61 73 73 65 72 PT_DB );. asser
31840 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
31850 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28 70 low<=ArraySize(p
31860 50 61 67 65 2d 3e 61 70 4f 76 66 6c 29 20 29 3b Page->apOvfl) );
31870 0a 20 20 61 73 73 65 72 74 28 20 41 72 72 61 79 . assert( Array
31880 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 70 4f 76 Size(pPage->apOv
31890 66 6c 29 3d 3d 41 72 72 61 79 53 69 7a 65 28 70 fl)==ArraySize(p
318a0 50 61 67 65 2d 3e 61 69 4f 76 66 6c 29 20 29 3b Page->aiOvfl) );
318b0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
318c0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
318d0 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
318e0 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 63 65 6c );. /* The cel
318f0 6c 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c l should normall
31900 79 20 62 65 20 73 69 7a 65 64 20 63 6f 72 72 65 y be sized corre
31910 63 74 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 ctly. However,
31920 77 68 65 6e 20 6d 6f 76 69 6e 67 20 61 0a 20 20 when moving a.
31930 2a 2a 20 6d 61 6c 66 6f 72 6d 65 64 20 63 65 6c ** malformed cel
31940 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 20 70 61 l from a leaf pa
31950 67 65 20 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f ge to an interio
31960 72 20 70 61 67 65 2c 20 69 66 20 74 68 65 20 63 r page, if the c
31970 65 6c 6c 20 73 69 7a 65 0a 20 20 2a 2a 20 77 61 ell size. ** wa
31980 6e 74 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20 nted to be less
31990 74 68 61 6e 20 34 20 62 75 74 20 67 6f 74 20 72 than 4 but got r
319a0 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20 6f ounded up to 4 o
319b0 6e 20 74 68 65 20 6c 65 61 66 2c 20 74 68 65 6e n the leaf, then
319c0 20 73 69 7a 65 0a 20 20 2a 2a 20 6d 69 67 68 74 size. ** might
319d0 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 38 20 be less than 8
319e0 28 6c 65 61 66 2d 73 69 7a 65 20 2b 20 70 6f 69 (leaf-size + poi
319f0 6e 74 65 72 29 20 6f 6e 20 74 68 65 20 69 6e 74 nter) on the int
31a00 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 48 65 6e erior node. Hen
31a10 63 65 0a 20 20 2a 2a 20 74 68 65 20 74 65 72 6d ce. ** the term
31a20 20 61 66 74 65 72 20 74 68 65 20 7c 7c 20 69 6e after the || in
31a30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
31a40 73 73 65 72 74 28 29 2e 20 2a 2f 0a 20 20 61 73 ssert(). */. as
31a50 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 sert( sz==cellSi
31a60 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 zePtr(pPage, pCe
31a70 6c 6c 29 20 7c 7c 20 28 73 7a 3d 3d 38 20 26 26 ll) || (sz==8 &&
31a80 20 69 43 68 69 6c 64 3e 30 29 20 29 3b 0a 20 20 iChild>0) );.
31a90 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 if( pPage->nOver
31aa0 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 flow || sz+2>pPa
31ab0 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 ge->nFree ){.
31ac0 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20 if( pTemp ){.
31ad0 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 memcpy(pTemp
31ae0 2c 20 70 43 65 6c 6c 2c 20 73 7a 29 3b 0a 20 20 , pCell, sz);.
31af0 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d pCell = pTem
31b00 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 p;. }. if(
31b10 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 iChild ){.
31b20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c put4byte(pCell,
31b30 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 7d 0a iChild);. }.
31b40 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e j = pPage->n
31b50 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 Overflow++;.
31b60 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 29 28 assert( j<(int)(
31b70 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 70 sizeof(pPage->ap
31b80 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 61 Ovfl)/sizeof(pPa
31b90 67 65 2d 3e 61 70 4f 76 66 6c 5b 30 5d 29 29 20 ge->apOvfl[0]))
31ba0 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 70 );. pPage->ap
31bb0 4f 76 66 6c 5b 6a 5d 20 3d 20 70 43 65 6c 6c 3b Ovfl[j] = pCell;
31bc0 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 69 4f 76 . pPage->aiOv
31bd0 66 6c 5b 6a 5d 20 3d 20 28 75 31 36 29 69 3b 0a fl[j] = (u16)i;.
31be0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
31bf0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
31c00 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
31c10 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
31c20 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
31c30 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 72 {. *pRC = r
31c40 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b c;. return;
31c50 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
31c60 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
31c70 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
31c80 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
31c90 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e data = pPage->
31ca0 61 44 61 74 61 3b 0a 20 20 20 20 63 65 6c 6c 4f aData;. cellO
31cb0 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 ffset = pPage->c
31cc0 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 65 ellOffset;. e
31cd0 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 nd = cellOffset
31ce0 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c + 2*pPage->nCell
31cf0 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c ;. ins = cell
31d00 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20 Offset + 2*i;.
31d10 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 53 rc = allocateS
31d20 70 61 63 65 28 70 50 61 67 65 2c 20 73 7a 2c 20 pace(pPage, sz,
31d30 26 69 64 78 29 3b 0a 20 20 20 20 69 66 28 20 72 &idx);. if( r
31d40 63 20 29 7b 20 2a 70 52 43 20 3d 20 72 63 3b 20 c ){ *pRC = rc;
31d50 72 65 74 75 72 6e 3b 20 7d 0a 20 20 20 20 2f 2a return; }. /*
31d60 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 53 70 61 The allocateSpa
31d70 63 65 28 29 20 72 6f 75 74 69 6e 65 20 67 75 61 ce() routine gua
31d80 72 61 6e 74 65 65 73 20 74 68 65 20 66 6f 6c 6c rantees the foll
31d90 6f 77 69 6e 67 20 74 77 6f 20 70 72 6f 70 65 72 owing two proper
31da0 74 69 65 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 ties. ** if i
31db0 74 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 t returns succes
31dc0 73 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 s */. assert(
31dd0 20 69 64 78 20 3e 3d 20 65 6e 64 2b 32 20 29 3b idx >= end+2 );
31de0 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
31df0 2b 73 7a 20 3c 3d 20 28 69 6e 74 29 70 50 61 67 +sz <= (int)pPag
31e00 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
31e10 7a 65 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d ze );. pPage-
31e20 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 >nCell++;. pP
31e30 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75 age->nFree -= (u
31e40 31 36 29 28 32 20 2b 20 73 7a 29 3b 0a 20 20 20 16)(2 + sz);.
31e50 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 69 64 memcpy(&data[id
31e60 78 5d 2c 20 70 43 65 6c 6c 2c 20 73 7a 29 3b 0a x], pCell, sz);.
31e70 20 20 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 if( iChild )
31e80 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 {. put4byte
31e90 28 26 64 61 74 61 5b 69 64 78 5d 2c 20 69 43 68 (&data[idx], iCh
31ea0 69 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ild);. }.
31eb0 6d 65 6d 6d 6f 76 65 28 26 64 61 74 61 5b 69 6e memmove(&data[in
31ec0 73 2b 32 5d 2c 20 26 64 61 74 61 5b 69 6e 73 5d s+2], &data[ins]
31ed0 2c 20 65 6e 64 2d 69 6e 73 29 3b 0a 20 20 20 20 , end-ins);.
31ee0 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 put2byte(&data[i
31ef0 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 ns], idx);. p
31f00 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 ut2byte(&data[pP
31f10 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 age->hdrOffset+3
31f20 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 ], pPage->nCell)
31f30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
31f40 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
31f50 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
31f60 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
31f70 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
31f80 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e cell may contain
31f90 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
31fa0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 overflow page.
31fb0 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 If so, write.
31fc0 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 ** the entry
31fd0 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 for the overflow
31fe0 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 page into the p
31ff0 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 ointer map..
32000 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 */. ptrma
32010 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 pPutOvflPtr(pPag
32020 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a e, pCell, pRC);.
32030 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
32040 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 72 61 79 20 .}../*.** Array
32050 61 70 43 65 6c 6c 5b 5d 20 63 6f 6e 74 61 69 6e apCell[] contain
32060 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 6e 43 s pointers to nC
32070 65 6c 6c 20 62 2d 74 72 65 65 20 70 61 67 65 20 ell b-tree page
32080 63 65 6c 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73 cells. The .** s
32090 7a 43 65 6c 6c 5b 5d 20 61 72 72 61 79 20 63 6f zCell[] array co
320a0 6e 74 61 69 6e 73 20 74 68 65 20 73 69 7a 65 20 ntains the size
320b0 69 6e 20 62 79 74 65 73 20 6f 66 20 65 61 63 68 in bytes of each
320c0 20 63 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 cell. This func
320d0 74 69 6f 6e 0a 2a 2a 20 72 65 70 6c 61 63 65 73 tion.** replaces
320e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e the current con
320f0 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 70 50 tents of page pP
32100 67 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 g with the conte
32110 6e 74 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 0a nts of the cell.
32120 2a 2a 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 ** array..**.**
32130 53 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c Some of the cell
32140 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6d 61 s in apCell[] ma
32150 79 20 63 75 72 72 65 6e 74 6c 79 20 62 65 20 73 y currently be s
32160 74 6f 72 65 64 20 69 6e 20 70 50 67 2e 20 54 68 tored in pPg. Th
32170 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 is.** function w
32180 6f 72 6b 73 20 61 72 6f 75 6e 64 20 70 72 6f 62 orks around prob
32190 6c 65 6d 73 20 63 61 75 73 65 64 20 62 79 20 74 lems caused by t
321a0 68 69 73 20 62 79 20 6d 61 6b 69 6e 67 20 61 20 his by making a
321b0 63 6f 70 79 20 6f 66 20 61 6e 79 20 0a 2a 2a 20 copy of any .**
321c0 73 75 63 68 20 63 65 6c 6c 73 20 62 65 66 6f 72 such cells befor
321d0 65 20 6f 76 65 72 77 72 69 74 69 6e 67 20 74 68 e overwriting th
321e0 65 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2a 0a e page data..**.
321f0 2a 2a 20 54 68 65 20 4d 65 6d 50 61 67 65 2e 6e ** The MemPage.n
32200 46 72 65 65 20 66 69 65 6c 64 20 69 73 20 69 6e Free field is in
32210 76 61 6c 69 64 61 74 65 64 20 62 79 20 74 68 69 validated by thi
32220 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 74 20 69 s function. It i
32230 73 20 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e s the .** respon
32240 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 sibility of the
32250 63 61 6c 6c 65 72 20 74 6f 20 73 65 74 20 69 74 caller to set it
32260 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 73 correctly..*/.s
32270 74 61 74 69 63 20 76 6f 69 64 20 72 65 62 75 69 tatic void rebui
32280 6c 64 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 ldPage(. MemPag
32290 65 20 2a 70 50 67 2c 20 20 20 20 20 20 20 20 20 e *pPg,
322a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 64 69 /* Edi
322b0 74 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 t this page */.
322c0 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 int nCell,
322d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
322e0 20 2f 2a 20 46 69 6e 61 6c 20 6e 75 6d 62 65 72 /* Final number
322f0 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 70 61 67 of cells on pag
32300 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 e */. u8 **apCe
32310 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
32320 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
32330 6f 66 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 75 31 of cells */. u1
32340 36 20 2a 73 7a 43 65 6c 6c 20 20 20 20 20 20 20 6 *szCell
32350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
32360 20 41 72 72 61 79 20 6f 66 20 63 65 6c 6c 20 73 Array of cell s
32370 69 7a 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e izes */.){. con
32380 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50 67 st int hdr = pPg
32390 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 ->hdrOffset;
323a0 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
323b0 6f 66 20 68 65 61 64 65 72 20 6f 6e 20 70 50 67 of header on pPg
323c0 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 */. u8 * const
323d0 20 61 44 61 74 61 20 3d 20 70 50 67 2d 3e 61 44 aData = pPg->aD
323e0 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f ata; /
323f0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 * Pointer to dat
32400 61 20 66 6f 72 20 70 50 67 20 2a 2f 0a 20 20 63 a for pPg */. c
32410 6f 6e 73 74 20 69 6e 74 20 75 73 61 62 6c 65 53 onst int usableS
32420 69 7a 65 20 3d 20 70 50 67 2d 3e 70 42 74 2d 3e ize = pPg->pBt->
32430 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 75 38 usableSize;. u8
32440 20 2a 20 63 6f 6e 73 74 20 70 45 6e 64 20 3d 20 * const pEnd =
32450 26 61 44 61 74 61 5b 75 73 61 62 6c 65 53 69 7a &aData[usableSiz
32460 65 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 75 e];. int i;. u
32470 38 20 2a 70 43 65 6c 6c 70 74 72 20 3d 20 70 50 8 *pCellptr = pP
32480 67 2d 3e 61 43 65 6c 6c 49 64 78 3b 0a 20 20 75 g->aCellIdx;. u
32490 38 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69 74 65 8 *pTmp = sqlite
324a0 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 3PagerTempSpace(
324b0 70 50 67 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 pPg->pBt->pPager
324c0 29 3b 0a 20 20 75 38 20 2a 70 44 61 74 61 3b 0a );. u8 *pData;.
324d0 0a 20 20 69 20 3d 20 67 65 74 32 62 79 74 65 28 . i = get2byte(
324e0 26 61 44 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a &aData[hdr+5]);.
324f0 20 20 6d 65 6d 63 70 79 28 26 70 54 6d 70 5b 69 memcpy(&pTmp[i
32500 5d 2c 20 26 61 44 61 74 61 5b 69 5d 2c 20 75 73 ], &aData[i], us
32510 61 62 6c 65 53 69 7a 65 20 2d 20 69 29 3b 0a 0a ableSize - i);..
32520 20 20 70 44 61 74 61 20 3d 20 70 45 6e 64 3b 0a pData = pEnd;.
32530 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
32540 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 ll; i++){. u8
32550 20 2a 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c *pCell = apCell
32560 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 65 [i];. if( pCe
32570 6c 6c 3e 61 44 61 74 61 20 26 26 20 70 43 65 6c ll>aData && pCel
32580 6c 3c 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 l<pEnd ){.
32590 70 43 65 6c 6c 20 3d 20 26 70 54 6d 70 5b 70 43 pCell = &pTmp[pC
325a0 65 6c 6c 20 2d 20 61 44 61 74 61 5d 3b 0a 20 20 ell - aData];.
325b0 20 20 7d 0a 20 20 20 20 70 44 61 74 61 20 2d 3d }. pData -=
325c0 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 szCell[i];.
325d0 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 70 43 memcpy(pData, pC
325e0 65 6c 6c 2c 20 73 7a 43 65 6c 6c 5b 69 5d 29 3b ell, szCell[i]);
325f0 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 43 . put2byte(pC
32600 65 6c 6c 70 74 72 2c 20 28 70 44 61 74 61 20 2d ellptr, (pData -
32610 20 61 44 61 74 61 29 29 3b 0a 20 20 20 20 70 43 aData));. pC
32620 65 6c 6c 70 74 72 20 2b 3d 20 32 3b 0a 20 20 20 ellptr += 2;.
32630 20 61 73 73 65 72 74 28 20 73 7a 43 65 6c 6c 5b assert( szCell[
32640 69 5d 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 i]==cellSizePtr(
32650 70 50 67 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20 pPg, pCell) );.
32660 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 70 50 67 }.. /* The pPg
32670 2d 3e 6e 46 72 65 65 20 66 69 65 6c 64 20 69 73 ->nFree field is
32680 20 6e 6f 77 20 73 65 74 20 69 6e 63 6f 72 72 65 now set incorre
32690 63 74 6c 79 2e 20 54 68 65 20 63 61 6c 6c 65 72 ctly. The caller
326a0 20 77 69 6c 6c 20 66 69 78 20 69 74 2e 20 2a 2f will fix it. */
326b0 0a 20 20 70 50 67 2d 3e 6e 43 65 6c 6c 20 3d 20 . pPg->nCell =
326c0 6e 43 65 6c 6c 3b 0a 20 20 70 50 67 2d 3e 6e 4f nCell;. pPg->nO
326d0 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 verflow = 0;..
326e0 70 75 74 32 62 79 74 65 28 26 61 44 61 74 61 5b put2byte(&aData[
326f0 68 64 72 2b 31 5d 2c 20 30 29 3b 0a 20 20 70 75 hdr+1], 0);. pu
32700 74 32 62 79 74 65 28 26 61 44 61 74 61 5b 68 64 t2byte(&aData[hd
32710 72 2b 33 5d 2c 20 70 50 67 2d 3e 6e 43 65 6c 6c r+3], pPg->nCell
32720 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 61 );. put2byte(&a
32730 44 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 44 61 Data[hdr+5], pDa
32740 74 61 20 2d 20 61 44 61 74 61 29 3b 0a 20 20 61 ta - aData);. a
32750 44 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 78 Data[hdr+7] = 0x
32760 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 72 00;.}../*.** Arr
32770 61 79 20 61 70 43 65 6c 6c 5b 5d 20 63 6f 6e 74 ay apCell[] cont
32780 61 69 6e 73 20 6e 43 65 6c 6c 20 70 6f 69 6e 74 ains nCell point
32790 65 72 73 20 74 6f 20 62 2d 74 72 65 65 20 63 65 ers to b-tree ce
327a0 6c 6c 73 2e 20 41 72 72 61 79 20 73 7a 43 65 6c lls. Array szCel
327b0 6c 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 l.** contains th
327c0 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
327d0 6f 66 20 65 61 63 68 20 73 75 63 68 20 63 65 6c of each such cel
327e0 6c 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e l. This function
327f0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 0a 2a 2a attempts to .**
32800 20 61 64 64 20 74 68 65 20 63 65 6c 6c 73 20 73 add the cells s
32810 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 72 72 tored in the arr
32820 61 79 20 74 6f 20 70 61 67 65 20 70 50 67 2e 20 ay to page pPg.
32830 49 66 20 69 74 20 63 61 6e 6e 6f 74 20 28 62 65 If it cannot (be
32840 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 61 cause .** the pa
32850 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 64 ge needs to be d
32860 65 66 72 61 67 6d 65 6e 74 65 64 20 62 65 66 6f efragmented befo
32870 72 65 20 74 68 65 20 63 65 6c 6c 73 20 77 69 6c re the cells wil
32880 6c 20 66 69 74 29 2c 20 6e 6f 6e 2d 7a 65 72 6f l fit), non-zero
32890 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e .** is returned.
328a0 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 Otherwise, if t
328b0 68 65 20 63 65 6c 6c 73 20 61 72 65 20 61 64 64 he cells are add
328c0 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ed successfully,
328d0 20 7a 65 72 6f 20 69 73 0a 2a 2a 20 72 65 74 75 zero is.** retu
328e0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 rned..**.** Argu
328f0 6d 65 6e 74 20 70 43 65 6c 6c 70 74 72 20 70 6f ment pCellptr po
32900 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
32910 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 63 t entry in the c
32920 65 6c 6c 2d 70 6f 69 6e 74 65 72 20 61 72 72 61 ell-pointer arra
32930 79 0a 2a 2a 20 28 70 61 72 74 20 6f 66 20 70 61 y.** (part of pa
32940 67 65 20 70 50 67 29 20 74 6f 20 70 6f 70 75 6c ge pPg) to popul
32950 61 74 65 2e 20 41 66 74 65 72 20 63 65 6c 6c 20 ate. After cell
32960 61 70 43 65 6c 6c 5b 30 5d 20 69 73 20 77 72 69 apCell[0] is wri
32970 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 70 tten to the.** p
32980 61 67 65 20 62 6f 64 79 2c 20 61 20 31 36 2d 62 age body, a 16-b
32990 69 74 20 6f 66 66 73 65 74 20 69 73 20 77 72 69 it offset is wri
329a0 74 74 65 6e 20 74 6f 20 70 43 65 6c 6c 70 74 72 tten to pCellptr
329b0 2e 20 41 6e 64 20 73 6f 20 6f 6e 2c 20 66 6f 72 . And so on, for
329c0 20 65 61 63 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e each.** cell in
329d0 20 74 68 65 20 61 72 72 61 79 2e 20 49 74 20 69 the array. It i
329e0 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 s the responsibi
329f0 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c lity of the call
32a00 65 72 20 74 6f 20 65 6e 73 75 72 65 0a 2a 2a 20 er to ensure.**
32a10 74 68 61 74 20 69 74 20 69 73 20 73 61 66 65 20 that it is safe
32a20 74 6f 20 6f 76 65 72 77 72 69 74 65 20 74 68 69 to overwrite thi
32a30 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 65 s part of the ce
32a40 6c 6c 2d 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll-pointer array
32a50 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ..**.** When thi
32a60 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
32a70 6c 6c 65 64 2c 20 2a 70 70 44 61 74 61 20 70 6f lled, *ppData po
32a80 69 6e 74 73 20 74 6f 20 74 68 65 20 73 74 61 72 ints to the star
32a90 74 20 6f 66 20 74 68 65 20 0a 2a 2a 20 63 6f 6e t of the .** con
32aa0 74 65 6e 74 20 61 72 65 61 20 6f 6e 20 70 61 67 tent area on pag
32ab0 65 20 70 50 67 2e 20 49 66 20 74 68 65 20 73 69 e pPg. If the si
32ac0 7a 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e ze of the conten
32ad0 74 20 61 72 65 61 20 69 73 20 65 78 74 65 6e 64 t area is extend
32ae0 65 64 2c 0a 2a 2a 20 2a 70 70 44 61 74 61 20 69 ed,.** *ppData i
32af0 73 20 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69 s updated to poi
32b00 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 20 73 74 nt to the new st
32b10 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 art of the conte
32b20 6e 74 20 61 72 65 61 0a 2a 2a 20 62 65 66 6f 72 nt area.** befor
32b30 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a e returning..**.
32b40 2a 2a 20 46 69 6e 61 6c 6c 79 2c 20 61 72 67 75 ** Finally, argu
32b50 6d 65 6e 74 20 70 42 65 67 69 6e 20 70 6f 69 6e ment pBegin poin
32b60 74 73 20 74 6f 20 74 68 65 20 62 79 74 65 20 69 ts to the byte i
32b70 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f mmediately follo
32b80 77 69 6e 67 20 74 68 65 0a 2a 2a 20 65 6e 64 20 wing the.** end
32b90 6f 66 20 74 68 65 20 73 70 61 63 65 20 72 65 71 of the space req
32ba0 75 69 72 65 64 20 62 79 20 74 68 69 73 20 70 61 uired by this pa
32bb0 67 65 20 66 6f 72 20 74 68 65 20 63 65 6c 6c 2d ge for the cell-
32bc0 70 6f 69 6e 74 65 72 20 61 72 65 61 20 28 66 6f pointer area (fo
32bd0 72 0a 2a 2a 20 61 6c 6c 20 63 65 6c 6c 73 20 2d r.** all cells -
32be0 20 6e 6f 74 20 6a 75 73 74 20 74 68 6f 73 65 20 not just those
32bf0 69 6e 73 65 72 74 65 64 20 62 79 20 74 68 65 20 inserted by the
32c00 63 75 72 72 65 6e 74 20 63 61 6c 6c 29 2e 20 49 current call). I
32c10 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a f the content.**
32c20 20 61 72 65 61 20 6d 75 73 74 20 62 65 20 65 78 area must be ex
32c30 74 65 6e 64 65 64 20 74 6f 20 62 65 66 6f 72 65 tended to before
32c40 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 6f this point in o
32c50 72 64 65 72 20 74 6f 20 61 63 63 6f 6d 6f 64 61 rder to accomoda
32c60 74 65 20 61 6c 6c 0a 2a 2a 20 63 65 6c 6c 73 20 te all.** cells
32c70 69 6e 20 61 70 43 65 6c 6c 5b 5d 2c 20 74 68 65 in apCell[], the
32c80 6e 20 74 68 65 20 63 65 6c 6c 73 20 64 6f 20 6e n the cells do n
32c90 6f 74 20 66 69 74 20 61 6e 64 20 6e 6f 6e 2d 7a ot fit and non-z
32ca0 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e ero is returned.
32cb0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
32cc0 61 67 65 49 6e 73 65 72 74 41 72 72 61 79 28 0a ageInsertArray(.
32cd0 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 2c 20 MemPage *pPg,
32ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32cf0 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 61 64 64 /* Page to add
32d00 20 63 65 6c 6c 73 20 74 6f 20 2a 2f 0a 20 20 75 cells to */. u
32d10 38 20 2a 70 42 65 67 69 6e 2c 20 20 20 20 20 20 8 *pBegin,
32d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
32d30 2a 20 45 6e 64 20 6f 66 20 63 65 6c 6c 2d 70 6f * End of cell-po
32d40 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 inter array */.
32d50 20 75 38 20 2a 2a 70 70 44 61 74 61 2c 20 20 20 u8 **ppData,
32d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32d70 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 61 67 65 /* IN/OUT: Page
32d80 20 63 6f 6e 74 65 6e 74 20 2d 61 72 65 61 20 70 content -area p
32d90 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a ointer */. u8 *
32da0 70 43 65 6c 6c 70 74 72 2c 20 20 20 20 20 20 20 pCellptr,
32db0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
32dc0 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c 2d 70 ointer to cell-p
32dd0 6f 69 6e 74 65 72 20 61 72 65 61 20 2a 2f 0a 20 ointer area */.
32de0 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 int nCell,
32df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32e00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 /* Number of ce
32e10 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 70 50 lls to add to pP
32e20 67 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 g */. u8 **apCe
32e30 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
32e40 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
32e50 6f 66 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 75 31 of cells */. u1
32e60 36 20 2a 73 7a 43 65 6c 6c 20 20 20 20 20 20 20 6 *szCell
32e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
32e80 20 41 72 72 61 79 20 6f 66 20 63 65 6c 6c 20 73 Array of cell s
32e90 69 7a 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 izes */.){. int
32ea0 20 69 3b 0a 20 20 75 38 20 2a 61 44 61 74 61 20 i;. u8 *aData
32eb0 3d 20 70 50 67 2d 3e 61 44 61 74 61 3b 0a 20 20 = pPg->aData;.
32ec0 75 38 20 2a 70 44 61 74 61 20 3d 20 2a 70 70 44 u8 *pData = *ppD
32ed0 61 74 61 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 ata;. const int
32ee0 20 62 46 72 65 65 6c 69 73 74 20 3d 20 61 44 61 bFreelist = aDa
32ef0 74 61 5b 31 5d 20 7c 7c 20 61 44 61 74 61 5b 32 ta[1] || aData[2
32f00 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 43 4f 52 ];. assert( COR
32f10 52 55 50 54 5f 44 42 20 7c 7c 20 70 50 67 2d 3e RUPT_DB || pPg->
32f20 68 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 hdrOffset==0 );
32f30 20 20 20 2f 2a 20 4e 65 76 65 72 20 63 61 6c 6c /* Never call
32f40 65 64 20 6f 6e 20 70 61 67 65 20 31 20 2a 2f 0a ed on page 1 */.
32f50 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
32f60 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e ll; i++){. in
32f70 74 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 69 5d t sz = szCell[i]
32f80 3b 0a 20 20 20 20 75 38 20 2a 70 53 6c 6f 74 3b ;. u8 *pSlot;
32f90 0a 20 20 20 20 69 66 28 20 62 46 72 65 65 6c 69 . if( bFreeli
32fa0 73 74 3d 3d 30 20 7c 7c 20 28 70 53 6c 6f 74 20 st==0 || (pSlot
32fb0 3d 20 70 61 67 65 46 69 6e 64 53 6c 6f 74 28 70 = pageFindSlot(p
32fc0 50 67 2c 20 73 7a 2c 20 30 2c 20 30 29 29 3d 3d Pg, sz, 0, 0))==
32fd0 30 20 29 7b 0a 20 20 20 20 20 20 70 44 61 74 61 0 ){. pData
32fe0 20 2d 3d 20 73 7a 3b 0a 20 20 20 20 20 20 69 66 -= sz;. if
32ff0 28 20 70 44 61 74 61 3c 70 42 65 67 69 6e 20 29 ( pData<pBegin )
33000 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
33010 20 70 53 6c 6f 74 20 3d 20 70 44 61 74 61 3b 0a pSlot = pData;.
33020 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
33030 28 70 53 6c 6f 74 2c 20 61 70 43 65 6c 6c 5b 69 (pSlot, apCell[i
33040 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 70 75 74 32 ], sz);. put2
33050 62 79 74 65 28 70 43 65 6c 6c 70 74 72 2c 20 28 byte(pCellptr, (
33060 70 53 6c 6f 74 20 2d 20 61 44 61 74 61 29 29 3b pSlot - aData));
33070 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2b 3d . pCellptr +=
33080 20 32 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 61 74 2;. }. *ppDat
33090 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 72 65 74 a = pData;. ret
330a0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
330b0 41 72 72 61 79 20 61 70 43 65 6c 6c 5b 5d 20 63 Array apCell[] c
330c0 6f 6e 74 61 69 6e 73 20 6e 43 65 6c 6c 20 70 6f ontains nCell po
330d0 69 6e 74 65 72 73 20 74 6f 20 62 2d 74 72 65 65 inters to b-tree
330e0 20 63 65 6c 6c 73 2e 20 41 72 72 61 79 20 73 7a cells. Array sz
330f0 43 65 6c 6c 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e Cell .** contain
33100 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 s the size in by
33110 74 65 73 20 6f 66 20 65 61 63 68 20 73 75 63 68 tes of each such
33120 20 63 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 cell. This func
33130 74 69 6f 6e 20 61 64 64 73 20 74 68 65 0a 2a 2a tion adds the.**
33140 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 space associate
33150 64 20 77 69 74 68 20 65 61 63 68 20 63 65 6c 6c d with each cell
33160 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 74 68 in the array th
33170 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 at is currently
33180 73 74 6f 72 65 64 20 0a 2a 2a 20 77 69 74 68 69 stored .** withi
33190 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 70 50 n the body of pP
331a0 67 20 74 6f 20 74 68 65 20 70 50 67 20 66 72 65 g to the pPg fre
331b0 65 2d 6c 69 73 74 2e 20 54 68 65 20 63 65 6c 6c e-list. The cell
331c0 2d 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 6f 74 -pointers and ot
331d0 68 65 72 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 her.** fields of
331e0 20 74 68 65 20 70 61 67 65 20 61 72 65 20 6e 6f the page are no
331f0 74 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a t updated..**.**
33200 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
33210 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c eturns the total
33220 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 number of cells
33230 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 added to the fr
33240 65 65 2d 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 ee-list..*/.stat
33250 69 63 20 69 6e 74 20 70 61 67 65 46 72 65 65 41 ic int pageFreeA
33260 72 72 61 79 28 0a 20 20 4d 65 6d 50 61 67 65 20 rray(. MemPage
33270 2a 70 50 67 2c 20 20 20 20 20 20 20 20 20 20 20 *pPg,
33280 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
33290 74 6f 20 65 64 69 74 20 2a 2f 0a 20 20 69 6e 74 to edit */. int
332a0 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 nCell,
332b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
332c0 43 65 6c 6c 73 20 74 6f 20 64 65 6c 65 74 65 20 Cells to delete
332d0 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c */. u8 **apCell
332e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
332f0 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
33300 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 75 31 36 20 cells */. u16
33310 2a 73 7a 43 65 6c 6c 20 20 20 20 20 20 20 20 20 *szCell
33320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
33330 72 72 61 79 20 6f 66 20 63 65 6c 6c 20 73 69 7a rray of cell siz
33340 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 20 es */.){. u8 *
33350 63 6f 6e 73 74 20 61 44 61 74 61 20 3d 20 70 50 const aData = pP
33360 67 2d 3e 61 44 61 74 61 3b 0a 20 20 75 38 20 2a g->aData;. u8 *
33370 20 63 6f 6e 73 74 20 70 45 6e 64 20 3d 20 26 61 const pEnd = &a
33380 44 61 74 61 5b 70 50 67 2d 3e 70 42 74 2d 3e 75 Data[pPg->pBt->u
33390 73 61 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 75 38 sableSize];. u8
333a0 20 2a 20 63 6f 6e 73 74 20 70 53 74 61 72 74 20 * const pStart
333b0 3d 20 26 61 44 61 74 61 5b 70 50 67 2d 3e 68 64 = &aData[pPg->hd
333c0 72 4f 66 66 73 65 74 20 2b 20 38 20 2b 20 70 50 rOffset + 8 + pP
333d0 67 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 5d g->childPtrSize]
333e0 3b 0a 20 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 ;. int nRet = 0
333f0 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 75 38 20 ;. int i;. u8
33400 2a 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 6e *pFree = 0;. in
33410 74 20 73 7a 46 72 65 65 20 3d 20 30 3b 0a 0a 20 t szFree = 0;..
33420 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
33430 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 l; i++){. u8
33440 2a 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b *pCell = apCell[
33450 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 65 6c i];. if( pCel
33460 6c 3e 3d 70 53 74 61 72 74 20 26 26 20 70 43 65 l>=pStart && pCe
33470 6c 6c 3c 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 ll<pEnd ){.
33480 20 69 6e 74 20 73 7a 20 3d 20 73 7a 43 65 6c 6c int sz = szCell
33490 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 [i];. if( p
334a0 46 72 65 65 21 3d 28 70 43 65 6c 6c 20 2b 20 73 Free!=(pCell + s
334b0 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 z) ){. if
334c0 28 20 70 46 72 65 65 20 29 20 66 72 65 65 53 70 ( pFree ) freeSp
334d0 61 63 65 28 70 50 67 2c 20 70 46 72 65 65 20 2d ace(pPg, pFree -
334e0 20 61 44 61 74 61 2c 20 73 7a 46 72 65 65 29 3b aData, szFree);
334f0 0a 20 20 20 20 20 20 20 20 70 46 72 65 65 20 3d . pFree =
33500 20 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 20 20 pCell;.
33510 73 7a 46 72 65 65 20 3d 20 73 7a 3b 0a 20 20 20 szFree = sz;.
33520 20 20 20 20 20 69 66 28 20 70 46 72 65 65 2b 73 if( pFree+s
33530 7a 3e 70 45 6e 64 20 29 20 72 65 74 75 72 6e 20 z>pEnd ) return
33540 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 0;. }else{.
33550 20 20 20 20 20 20 20 20 70 46 72 65 65 20 3d 20 pFree =
33560 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 20 20 73 pCell;. s
33570 7a 46 72 65 65 20 2b 3d 20 73 7a 3b 0a 20 20 20 zFree += sz;.
33580 20 20 20 7d 0a 20 20 20 20 20 20 6e 52 65 74 2b }. nRet+
33590 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 +;. }. }. i
335a0 66 28 20 70 46 72 65 65 20 29 20 66 72 65 65 53 f( pFree ) freeS
335b0 70 61 63 65 28 70 50 67 2c 20 70 46 72 65 65 20 pace(pPg, pFree
335c0 2d 20 61 44 61 74 61 2c 20 73 7a 46 72 65 65 29 - aData, szFree)
335d0 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b ;. return nRet;
335e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 50 .}../*.** The pP
335f0 67 2d 3e 6e 46 72 65 65 20 66 69 65 6c 64 20 69 g->nFree field i
33600 73 20 69 6e 76 61 6c 69 64 20 77 68 65 6e 20 74 s invalid when t
33610 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
33620 75 72 6e 73 2e 20 49 74 20 69 73 20 74 68 65 0a urns. It is the.
33630 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 ** responsibilit
33640 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 y of the caller
33650 74 6f 20 73 65 74 20 69 74 20 63 6f 72 72 65 63 to set it correc
33660 74 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tly..*/.static v
33670 6f 69 64 20 65 64 69 74 50 61 67 65 28 0a 20 20 oid editPage(.
33680 4d 65 6d 50 61 67 65 20 2a 70 50 67 2c 20 20 20 MemPage *pPg,
33690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
336a0 2f 2a 20 45 64 69 74 20 74 68 69 73 20 70 61 67 /* Edit this pag
336b0 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6c 64 2c e */. int iOld,
336c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
336d0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
336e0 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 63 75 of first cell cu
336f0 72 72 65 6e 74 6c 79 20 6f 6e 20 70 61 67 65 20 rrently on page
33700 2a 2f 0a 20 20 69 6e 74 20 69 4e 65 77 2c 20 20 */. int iNew,
33710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33720 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
33730 20 6e 65 77 20 66 69 72 73 74 20 63 65 6c 6c 20 new first cell
33740 6f 6e 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 on page */. int
33750 20 6e 4e 65 77 2c 20 20 20 20 20 20 20 20 20 20 nNew,
33760 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
33770 46 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Final number of
33780 63 65 6c 6c 73 20 6f 6e 20 70 61 67 65 20 2a 2f cells on page */
33790 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 . u8 **apCell,
337a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
337b0 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 /* Array of c
337c0 65 6c 6c 73 20 2a 2f 0a 20 20 75 31 36 20 2a 73 ells */. u16 *s
337d0 7a 43 65 6c 6c 20 20 20 20 20 20 20 20 20 20 20 zCell
337e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
337f0 61 79 20 6f 66 20 63 65 6c 6c 20 73 69 7a 65 73 ay of cell sizes
33800 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 20 63 6f */.){. u8 * co
33810 6e 73 74 20 61 44 61 74 61 20 3d 20 70 50 67 2d nst aData = pPg-
33820 3e 61 44 61 74 61 3b 0a 20 20 63 6f 6e 73 74 20 >aData;. const
33830 69 6e 74 20 68 64 72 20 3d 20 70 50 67 2d 3e 68 int hdr = pPg->h
33840 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 38 20 2a drOffset;. u8 *
33850 70 42 65 67 69 6e 20 3d 20 26 70 50 67 2d 3e 61 pBegin = &pPg->a
33860 43 65 6c 6c 49 64 78 5b 6e 4e 65 77 20 2a 20 32 CellIdx[nNew * 2
33870 5d 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d ];. int nCell =
33880 20 70 50 67 2d 3e 6e 43 65 6c 6c 3b 20 20 20 20 pPg->nCell;
33890 20 20 20 2f 2a 20 43 65 6c 6c 73 20 73 74 6f 72 /* Cells stor
338a0 65 64 20 6f 6e 20 70 50 67 20 2a 2f 0a 20 20 75 ed on pPg */. u
338b0 38 20 2a 70 44 61 74 61 3b 0a 20 20 75 38 20 2a 8 *pData;. u8 *
338c0 70 43 65 6c 6c 70 74 72 3b 0a 20 20 69 6e 74 20 pCellptr;. int
338d0 69 3b 0a 20 20 69 6e 74 20 69 4f 6c 64 45 6e 64 i;. int iOldEnd
338e0 20 3d 20 69 4f 6c 64 20 2b 20 70 50 67 2d 3e 6e = iOld + pPg->n
338f0 43 65 6c 6c 20 2b 20 70 50 67 2d 3e 6e 4f 76 65 Cell + pPg->nOve
33900 72 66 6c 6f 77 3b 0a 20 20 69 6e 74 20 69 4e 65 rflow;. int iNe
33910 77 45 6e 64 20 3d 20 69 4e 65 77 20 2b 20 6e 4e wEnd = iNew + nN
33920 65 77 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ew;..#ifdef SQLI
33930 54 45 5f 44 45 42 55 47 0a 20 20 75 38 20 2a 70 TE_DEBUG. u8 *p
33940 54 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 Tmp = sqlite3Pag
33950 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 67 2d erTempSpace(pPg-
33960 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 >pBt->pPager);.
33970 20 6d 65 6d 63 70 79 28 70 54 6d 70 2c 20 61 44 memcpy(pTmp, aD
33980 61 74 61 2c 20 70 50 67 2d 3e 70 42 74 2d 3e 75 ata, pPg->pBt->u
33990 73 61 62 6c 65 53 69 7a 65 29 3b 0a 23 65 6e 64 sableSize);.#end
339a0 69 66 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 if.. /* Remove
339b0 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 73 cells from the s
339c0 74 61 72 74 20 61 6e 64 20 65 6e 64 20 6f 66 20 tart and end of
339d0 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 66 the page */. if
339e0 28 20 69 4f 6c 64 3c 69 4e 65 77 20 29 7b 0a 20 ( iOld<iNew ){.
339f0 20 20 20 69 6e 74 20 6e 53 68 69 66 74 20 3d 20 int nShift =
33a00 70 61 67 65 46 72 65 65 41 72 72 61 79 28 0a 20 pageFreeArray(.
33a10 20 20 20 20 20 20 20 70 50 67 2c 20 69 4e 65 77 pPg, iNew
33a20 2d 69 4f 6c 64 2c 20 26 61 70 43 65 6c 6c 5b 69 -iOld, &apCell[i
33a30 4f 6c 64 5d 2c 20 26 73 7a 43 65 6c 6c 5b 69 4f Old], &szCell[iO
33a40 6c 64 5d 0a 20 20 20 20 29 3b 0a 20 20 20 20 6d ld]. );. m
33a50 65 6d 6d 6f 76 65 28 70 50 67 2d 3e 61 43 65 6c emmove(pPg->aCel
33a60 6c 49 64 78 2c 20 26 70 50 67 2d 3e 61 43 65 6c lIdx, &pPg->aCel
33a70 6c 49 64 78 5b 6e 53 68 69 66 74 2a 32 5d 2c 20 lIdx[nShift*2],
33a80 6e 43 65 6c 6c 2a 32 29 3b 0a 20 20 20 20 6e 43 nCell*2);. nC
33a90 65 6c 6c 20 2d 3d 20 6e 53 68 69 66 74 3b 0a 20 ell -= nShift;.
33aa0 20 7d 0a 20 20 69 66 28 20 69 4e 65 77 45 6e 64 }. if( iNewEnd
33ab0 20 3c 20 69 4f 6c 64 45 6e 64 20 29 7b 0a 20 20 < iOldEnd ){.
33ac0 20 20 6e 43 65 6c 6c 20 2d 3d 20 70 61 67 65 46 nCell -= pageF
33ad0 72 65 65 41 72 72 61 79 28 0a 20 20 20 20 20 20 reeArray(.
33ae0 20 20 70 50 67 2c 20 69 4f 6c 64 45 6e 64 2d 69 pPg, iOldEnd-i
33af0 4e 65 77 45 6e 64 2c 20 26 61 70 43 65 6c 6c 5b NewEnd, &apCell[
33b00 69 4e 65 77 45 6e 64 5d 2c 20 26 73 7a 43 65 6c iNewEnd], &szCel
33b10 6c 5b 69 4e 65 77 45 6e 64 5d 0a 20 20 20 20 29 l[iNewEnd]. )
33b20 3b 0a 20 20 7d 0a 0a 20 20 70 44 61 74 61 20 3d ;. }.. pData =
33b30 20 26 61 44 61 74 61 5b 67 65 74 32 62 79 74 65 &aData[get2byte
33b40 28 26 61 44 61 74 61 5b 68 64 72 2b 35 5d 29 5d (&aData[hdr+5])]
33b50 3b 0a 20 20 69 66 28 20 70 44 61 74 61 3c 70 42 ;. if( pData<pB
33b60 65 67 69 6e 20 29 20 67 6f 74 6f 20 65 64 69 74 egin ) goto edit
33b70 70 61 67 65 5f 66 61 69 6c 3b 0a 0a 20 20 2f 2a page_fail;.. /*
33b80 20 41 64 64 20 63 65 6c 6c 73 20 74 6f 20 74 68 Add cells to th
33b90 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 70 e start of the p
33ba0 61 67 65 20 2a 2f 0a 20 20 69 66 28 20 69 4e 65 age */. if( iNe
33bb0 77 3c 69 4f 6c 64 20 29 7b 0a 20 20 20 20 69 6e w<iOld ){. in
33bc0 74 20 6e 41 64 64 20 3d 20 69 4f 6c 64 2d 69 4e t nAdd = iOld-iN
33bd0 65 77 3b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 ew;. pCellptr
33be0 20 3d 20 70 50 67 2d 3e 61 43 65 6c 6c 49 64 78 = pPg->aCellIdx
33bf0 3b 0a 20 20 20 20 6d 65 6d 6d 6f 76 65 28 26 70 ;. memmove(&p
33c00 43 65 6c 6c 70 74 72 5b 6e 41 64 64 2a 32 5d 2c Cellptr[nAdd*2],
33c10 20 70 43 65 6c 6c 70 74 72 2c 20 6e 43 65 6c 6c pCellptr, nCell
33c20 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 70 61 67 *2);. if( pag
33c30 65 49 6e 73 65 72 74 41 72 72 61 79 28 0a 20 20 eInsertArray(.
33c40 20 20 20 20 20 20 20 20 70 50 67 2c 20 70 42 65 pPg, pBe
33c50 67 69 6e 2c 20 26 70 44 61 74 61 2c 20 70 43 65 gin, &pData, pCe
33c60 6c 6c 70 74 72 2c 0a 20 20 20 20 20 20 20 20 20 llptr,.
33c70 20 6e 41 64 64 2c 20 26 61 70 43 65 6c 6c 5b 69 nAdd, &apCell[i
33c80 4e 65 77 5d 2c 20 26 73 7a 43 65 6c 6c 5b 69 4e New], &szCell[iN
33c90 65 77 5d 0a 20 20 20 20 29 20 29 20 67 6f 74 6f ew]. ) ) goto
33ca0 20 65 64 69 74 70 61 67 65 5f 66 61 69 6c 3b 0a editpage_fail;.
33cb0 20 20 20 20 6e 43 65 6c 6c 20 2b 3d 20 6e 41 64 nCell += nAd
33cc0 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 d;. }.. /* Add
33cd0 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 any overflow ce
33ce0 6c 6c 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 lls */. for(i=0
33cf0 3b 20 69 3c 70 50 67 2d 3e 6e 4f 76 65 72 66 6c ; i<pPg->nOverfl
33d00 6f 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e ow; i++){. in
33d10 74 20 69 43 65 6c 6c 20 3d 20 28 69 4f 6c 64 20 t iCell = (iOld
33d20 2b 20 70 50 67 2d 3e 61 69 4f 76 66 6c 5b 69 5d + pPg->aiOvfl[i]
33d30 29 20 2d 20 69 4e 65 77 3b 0a 20 20 20 20 69 66 ) - iNew;. if
33d40 28 20 69 43 65 6c 6c 3e 3d 30 20 26 26 20 69 43 ( iCell>=0 && iC
33d50 65 6c 6c 3c 6e 4e 65 77 20 29 7b 0a 20 20 20 20 ell<nNew ){.
33d60 20 20 75 38 20 2a 70 43 65 6c 6c 70 74 72 20 3d u8 *pCellptr =
33d70 20 26 70 50 67 2d 3e 61 43 65 6c 6c 49 64 78 5b &pPg->aCellIdx[
33d80 69 43 65 6c 6c 20 2a 20 32 5d 3b 0a 20 20 20 20 iCell * 2];.
33d90 20 20 6d 65 6d 6d 6f 76 65 28 26 70 43 65 6c 6c memmove(&pCell
33da0 70 74 72 5b 32 5d 2c 20 70 43 65 6c 6c 70 74 72 ptr[2], pCellptr
33db0 2c 20 28 6e 43 65 6c 6c 20 2d 20 69 43 65 6c 6c , (nCell - iCell
33dc0 29 20 2a 20 32 29 3b 0a 20 20 20 20 20 20 6e 43 ) * 2);. nC
33dd0 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 ell++;. if(
33de0 20 70 61 67 65 49 6e 73 65 72 74 41 72 72 61 79 pageInsertArray
33df0 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 (. pP
33e00 67 2c 20 70 42 65 67 69 6e 2c 20 26 70 44 61 74 g, pBegin, &pDat
33e10 61 2c 20 70 43 65 6c 6c 70 74 72 2c 0a 20 20 20 a, pCellptr,.
33e20 20 20 20 20 20 20 20 20 20 31 2c 20 26 61 70 43 1, &apC
33e30 65 6c 6c 5b 69 43 65 6c 6c 20 2b 20 69 4e 65 77 ell[iCell + iNew
33e40 5d 2c 20 26 73 7a 43 65 6c 6c 5b 69 43 65 6c 6c ], &szCell[iCell
33e50 20 2b 20 69 4e 65 77 5d 0a 20 20 20 20 20 20 29 + iNew]. )
33e60 20 29 20 67 6f 74 6f 20 65 64 69 74 70 61 67 65 ) goto editpage
33e70 5f 66 61 69 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d _fail;. }. }
33e80 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 63 65 .. /* Append ce
33e90 6c 6c 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f lls to the end o
33ea0 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 f the page */.
33eb0 70 43 65 6c 6c 70 74 72 20 3d 20 26 70 50 67 2d pCellptr = &pPg-
33ec0 3e 61 43 65 6c 6c 49 64 78 5b 6e 43 65 6c 6c 2a >aCellIdx[nCell*
33ed0 32 5d 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e 2];. if( pageIn
33ee0 73 65 72 74 41 72 72 61 79 28 0a 20 20 20 20 20 sertArray(.
33ef0 20 20 20 70 50 67 2c 20 70 42 65 67 69 6e 2c 20 pPg, pBegin,
33f00 26 70 44 61 74 61 2c 20 70 43 65 6c 6c 70 74 72 &pData, pCellptr
33f10 2c 0a 20 20 20 20 20 20 20 20 6e 4e 65 77 2d 6e ,. nNew-n
33f20 43 65 6c 6c 2c 20 26 61 70 43 65 6c 6c 5b 69 4e Cell, &apCell[iN
33f30 65 77 2b 6e 43 65 6c 6c 5d 2c 20 26 73 7a 43 65 ew+nCell], &szCe
33f40 6c 6c 5b 69 4e 65 77 2b 6e 43 65 6c 6c 5d 0a 20 ll[iNew+nCell].
33f50 20 29 20 29 20 67 6f 74 6f 20 65 64 69 74 70 61 ) ) goto editpa
33f60 67 65 5f 66 61 69 6c 3b 0a 0a 20 20 70 50 67 2d ge_fail;.. pPg-
33f70 3e 6e 43 65 6c 6c 20 3d 20 6e 4e 65 77 3b 0a 20 >nCell = nNew;.
33f80 20 70 50 67 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 pPg->nOverflow
33f90 3d 20 30 3b 0a 0a 20 20 70 75 74 32 62 79 74 65 = 0;.. put2byte
33fa0 28 26 61 44 61 74 61 5b 68 64 72 2b 33 5d 2c 20 (&aData[hdr+3],
33fb0 70 50 67 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 pPg->nCell);. p
33fc0 75 74 32 62 79 74 65 28 26 61 44 61 74 61 5b 68 ut2byte(&aData[h
33fd0 64 72 2b 35 5d 2c 20 70 44 61 74 61 20 2d 20 61 dr+5], pData - a
33fe0 44 61 74 61 29 3b 0a 0a 23 69 66 64 65 66 20 53 Data);..#ifdef S
33ff0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f QLITE_DEBUG. fo
34000 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 20 26 26 r(i=0; i<nNew &&
34010 20 21 43 4f 52 52 55 50 54 5f 44 42 3b 20 69 2b !CORRUPT_DB; i+
34020 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c +){. u8 *pCel
34030 6c 20 3d 20 61 70 43 65 6c 6c 5b 69 2b 69 4e 65 l = apCell[i+iNe
34040 77 5d 3b 0a 20 20 20 20 69 6e 74 20 69 4f 66 66 w];. int iOff
34050 20 3d 20 67 65 74 32 62 79 74 65 28 26 70 50 67 = get2byte(&pPg
34060 2d 3e 61 43 65 6c 6c 49 64 78 5b 69 2a 32 5d 29 ->aCellIdx[i*2])
34070 3b 0a 20 20 20 20 69 66 28 20 70 43 65 6c 6c 3e ;. if( pCell>
34080 3d 61 44 61 74 61 20 26 26 20 70 43 65 6c 6c 3c =aData && pCell<
34090 26 61 44 61 74 61 5b 70 50 67 2d 3e 70 42 74 2d &aData[pPg->pBt-
340a0 3e 75 73 61 62 6c 65 53 69 7a 65 5d 20 29 7b 0a >usableSize] ){.
340b0 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 26 70 pCell = &p
340c0 54 6d 70 5b 70 43 65 6c 6c 20 2d 20 61 44 61 74 Tmp[pCell - aDat
340d0 61 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 a];. }. as
340e0 73 65 72 74 28 20 30 3d 3d 6d 65 6d 63 6d 70 28 sert( 0==memcmp(
340f0 70 43 65 6c 6c 2c 20 26 61 44 61 74 61 5b 69 4f pCell, &aData[iO
34100 66 66 5d 2c 20 73 7a 43 65 6c 6c 5b 69 2b 69 4e ff], szCell[i+iN
34110 65 77 5d 29 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 ew]) );. }.#end
34120 69 66 0a 0a 20 20 72 65 74 75 72 6e 3b 0a 20 65 if.. return;. e
34130 64 69 74 70 61 67 65 5f 66 61 69 6c 3a 0a 20 20 ditpage_fail:.
34140 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 65 64 69 /* Unable to edi
34150 74 20 74 68 69 73 20 70 61 67 65 2e 20 52 65 62 t this page. Reb
34160 75 69 6c 64 20 69 74 20 66 72 6f 6d 20 73 63 72 uild it from scr
34170 61 74 63 68 20 69 6e 73 74 65 61 64 2e 20 2a 2f atch instead. */
34180 0a 20 20 72 65 62 75 69 6c 64 50 61 67 65 28 70 . rebuildPage(p
34190 50 67 2c 20 6e 4e 65 77 2c 20 26 61 70 43 65 6c Pg, nNew, &apCel
341a0 6c 5b 69 4e 65 77 5d 2c 20 26 73 7a 43 65 6c 6c l[iNew], &szCell
341b0 5b 69 4e 65 77 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a [iNew]);.}../*.*
341c0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
341d0 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 parameters deter
341e0 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 mine how many ad
341f0 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 jacent pages get
34200 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 involved.** in
34210 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 a balancing oper
34220 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 ation. NN is th
34230 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 e number of neig
34240 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
34250 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 side.** of the p
34260 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63 69 age that partici
34270 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 pate in the bala
34280 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e ncing operation.
34290 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 NB is the.** t
342a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
342b0 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69 63 ages that partic
342c0 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 ipate, including
342d0 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67 65 the target page
342e0 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 and.** NN neigh
342f0 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 bors on either s
34300 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d ide..**.** The m
34310 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 inimum value of
34320 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 NN is 1 (of cour
34330 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 se). Increasing
34340 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 NN above 1.** (
34350 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 to 2 or 3) gives
34360 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 a modest improv
34370 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 ement in SELECT
34380 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f and DELETE perfo
34390 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 rmance.** in exc
343a0 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 hange for a larg
343b0 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 er degradation i
343c0 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 n INSERT and UPD
343d0 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e ATE performance.
343e0 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 .** The value of
343f0 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 NN appears to g
34400 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65 73 ive the best res
34410 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f ults overall..*/
34420 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 .#define NN 1
34430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
34440 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 ber of neighbors
34450 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 on either side
34460 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 of pPage */.#def
34470 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 ine NB (NN*2+1)
34480 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 /* Total pa
34490 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 ges involved in
344a0 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a the balance */..
344b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
344c0 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 OMIT_QUICKBALANC
344d0 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 E./*.** This ver
344e0 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28 sion of balance(
344f0 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 6f ) handles the co
34500 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 73 mmon special cas
34510 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77 e where.** a new
34520 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 20 entry is being
34530 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 20 inserted on the
34540 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 6e extreme right-en
34550 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 65 d of the.** tree
34560 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 , in other words
34570 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 65 , when the new e
34580 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65 ntry will become
34590 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 the largest.**
345a0 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 entry in the tre
345b0 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 e..**.** Instead
345c0 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62 61 of trying to ba
345d0 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 68 lance the 3 righ
345e0 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 65 t-most leaf page
345f0 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 61 s, just add.** a
34600 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 65 new page to the
34610 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
34620 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e 65 and put the one
34630 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a 2a new entry in.**
34640 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 69 that page. Thi
34650 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 67 s leaves the rig
34660 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 74 ht side of the t
34670 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a 20 ree somewhat.**
34680 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 74 unbalanced. But
34690 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 77 odds are that w
346a0 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 e will be insert
346b0 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 0a ing new entries.
346c0 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 6f ** at the end so
346d0 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 6f on afterwards so
346e0 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 the nearly empt
346f0 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 63 y page will quic
34700 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e 20 kly.** fill up.
34710 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a 0a On average..**.
34720 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 20 ** pPage is the
34730 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 leaf page which
34740 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 is the right-mos
34750 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 72 t page in the tr
34760 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 69 ee..** pParent i
34770 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 70 s its parent. p
34780 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 Page must have a
34790 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f 77 single overflow
347a0 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 20 entry.** which
347b0 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 68 is also the righ
347c0 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e 20 t-most entry on
347d0 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 the page..**.**
347e0 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 The pSpace buffe
347f0 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f r is used to sto
34800 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 re a temporary c
34810 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69 64 opy of the divid
34820 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74 20 er.** cell that
34830 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 64 will be inserted
34840 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 53 into pParent. S
34850 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 uch a cell consi
34860 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62 79 sts of a 4.** by
34870 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 te page number f
34880 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61 72 ollowed by a var
34890 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 iable length int
348a0 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a 2a eger. In other.*
348b0 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73 74 * words, at most
348c0 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63 65 13 bytes. Hence
348d0 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 the pSpace buff
348e0 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a 2a er must be at.**
348f0 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73 20 least 13 bytes
34900 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 in size..*/.stat
34910 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71 ic int balance_q
34920 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70 50 uick(MemPage *pP
34930 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20 2a arent, MemPage *
34940 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61 63 pPage, u8 *pSpac
34950 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a e){. BtShared *
34960 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61 67 const pBt = pPag
34970 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42 2d e->pBt; /* B-
34980 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a 2f Tree Database */
34990 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 . MemPage *pNew
349a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
349b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c 79 /* Newly
349c0 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 allocated page
349d0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
349e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
349f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
34a00 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50 67 urn Code */. Pg
34a10 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20 20 no pgnoNew;
34a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34a30 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
34a40 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20 20 r of pNew */..
34a50 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
34a60 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
34a70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
34a80 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
34a90 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
34aa0 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 le(pParent->pDbP
34ab0 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
34ac0 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
34ad0 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 ow==1 );.. /* T
34ae0 68 69 73 20 65 72 72 6f 72 20 63 6f 6e 64 69 74 his error condit
34af0 69 6f 6e 20 69 73 20 6e 6f 77 20 63 61 75 67 68 ion is now caugh
34b00 74 20 70 72 69 6f 72 20 74 6f 20 72 65 61 63 68 t prior to reach
34b10 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
34b20 6e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 n */. if( pPage
34b30 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 ->nCell==0 ) ret
34b40 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
34b50 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20 41 PT_BKPT;.. /* A
34b60 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
34b70 67 65 2e 20 54 68 69 73 20 70 61 67 65 20 77 69 ge. This page wi
34b80 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72 69 ll become the ri
34b90 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20 0a ght-sibling of .
34ba0 20 20 2a 2a 20 70 50 61 67 65 2e 20 4d 61 6b 65 ** pPage. Make
34bb0 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
34bc0 20 77 72 69 74 61 62 6c 65 2c 20 73 6f 20 74 68 writable, so th
34bd0 61 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 at the new divid
34be0 65 72 20 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61 79 er cell. ** may
34bf0 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 49 66 be inserted. If
34c00 20 62 6f 74 68 20 74 68 65 73 65 20 6f 70 65 72 both these oper
34c10 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63 65 ations are succe
34c20 73 73 66 75 6c 2c 20 70 72 6f 63 65 65 64 2e 0a ssful, proceed..
34c30 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f */. rc = allo
34c40 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
34c50 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e t, &pNew, &pgnoN
34c60 65 77 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 69 66 ew, 0, 0);.. if
34c70 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
34c80 29 7b 0a 0a 20 20 20 20 75 38 20 2a 70 4f 75 74 ){.. u8 *pOut
34c90 20 3d 20 26 70 53 70 61 63 65 5b 34 5d 3b 0a 20 = &pSpace[4];.
34ca0 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 70 u8 *pCell = p
34cb0 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 30 5d 3b Page->apOvfl[0];
34cc0 0a 20 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 . u16 szCell
34cd0 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 = cellSizePtr(pP
34ce0 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 age, pCell);.
34cf0 20 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 u8 *pStop;..
34d00 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
34d10 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
34d20 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 (pNew->pDbPage)
34d30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
34d40 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d Page->aData[0]==
34d50 28 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f (PTF_INTKEY|PTF_
34d60 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 LEAFDATA|PTF_LEA
34d70 46 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 F) );. zeroPa
34d80 67 65 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 ge(pNew, PTF_INT
34d90 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 KEY|PTF_LEAFDATA
34da0 7c 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 |PTF_LEAF);.
34db0 72 65 62 75 69 6c 64 50 61 67 65 28 70 4e 65 77 rebuildPage(pNew
34dc0 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a , 1, &pCell, &sz
34dd0 43 65 6c 6c 29 3b 0a 20 20 20 20 70 4e 65 77 2d Cell);. pNew-
34de0 3e 6e 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 >nFree = pBt->us
34df0 61 62 6c 65 53 69 7a 65 20 2d 20 70 4e 65 77 2d ableSize - pNew-
34e00 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2d 20 32 20 >cellOffset - 2
34e10 2d 20 73 7a 43 65 6c 6c 3b 0a 0a 20 20 20 20 2f - szCell;.. /
34e20 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
34e30 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
34e40 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 base, update the
34e50 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 pointer map.
34e60 20 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 ** with entries
34e70 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 for the new pag
34e80 65 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 e, and any point
34e90 65 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 er from the .
34ea0 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 ** cell on the
34eb0 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 page to an overf
34ec0 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 65 69 74 low page. If eit
34ed0 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 her of these.
34ee0 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 ** operations f
34ef0 61 69 6c 73 2c 20 74 68 65 20 72 65 74 75 72 6e ails, the return
34f00 20 63 6f 64 65 20 69 73 20 73 65 74 2c 20 62 75 code is set, bu
34f10 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 t the contents.
34f20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 ** of the par
34f30 65 6e 74 20 70 61 67 65 20 61 72 65 20 73 74 69 ent page are sti
34f40 6c 6c 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 ll manipulated b
34f50 79 20 74 68 68 20 63 6f 64 65 20 62 65 6c 6f 77 y thh code below
34f60 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 69 73 .. ** That is
34f70 20 4f 6b 2c 20 61 74 20 74 68 69 73 20 70 6f 69 Ok, at this poi
34f80 6e 74 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 nt the parent pa
34f90 67 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 ge is guaranteed
34fa0 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61 to. ** be ma
34fb0 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 52 rked as dirty. R
34fc0 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f eturning an erro
34fd0 72 20 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 r code will caus
34fe0 65 20 61 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 e a. ** rollb
34ff0 61 63 6b 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79 ack, undoing any
35000 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 74 6f changes made to
35010 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
35020 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
35030 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
35040 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 . ptrmapPut
35050 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 (pBt, pgnoNew, P
35060 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 TRMAP_BTREE, pPa
35070 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 rent->pgno, &rc)
35080 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 ;. if( szCe
35090 6c 6c 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 ll>pNew->minLoca
350a0 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 l ){. ptr
350b0 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e mapPutOvflPtr(pN
350c0 65 77 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b ew, pCell, &rc);
350d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
350e0 20 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 . /* Create
350f0 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 74 a divider cell t
35100 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 o insert into pP
35110 61 72 65 6e 74 2e 20 54 68 65 20 64 69 76 69 64 arent. The divid
35120 65 72 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 er cell. ** c
35130 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 2d 62 onsists of a 4-b
35140 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 yte page number
35150 28 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 (the page number
35160 20 6f 66 20 70 50 61 67 65 29 20 61 6e 64 0a 20 of pPage) and.
35170 20 20 20 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 ** a variable
35180 20 6c 65 6e 67 74 68 20 6b 65 79 20 76 61 6c 75 length key valu
35190 65 20 28 77 68 69 63 68 20 6d 75 73 74 20 62 65 e (which must be
351a0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 the same value
351b0 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 as the. ** la
351c0 72 67 65 73 74 20 6b 65 79 20 6f 6e 20 70 50 61 rgest key on pPa
351d0 67 65 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ge).. **.
351e0 2a 2a 20 54 6f 20 66 69 6e 64 20 74 68 65 20 6c ** To find the l
351f0 61 72 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65 argest key value
35200 20 6f 6e 20 70 50 61 67 65 2c 20 66 69 72 73 74 on pPage, first
35210 20 66 69 6e 64 20 74 68 65 20 72 69 67 68 74 2d find the right-
35220 6d 6f 73 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c most . ** cel
35230 6c 20 6f 6e 20 70 50 61 67 65 2e 20 54 68 65 20 l on pPage. The
35240 66 69 72 73 74 20 74 77 6f 20 66 69 65 6c 64 73 first two fields
35250 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 20 61 72 of this cell ar
35260 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 e the . ** re
35270 63 6f 72 64 2d 6c 65 6e 67 74 68 20 28 61 20 76 cord-length (a v
35280 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 ariable length i
35290 6e 74 65 67 65 72 20 61 74 20 6d 6f 73 74 20 33 nteger at most 3
352a0 32 2d 62 69 74 73 20 69 6e 20 73 69 7a 65 29 0a 2-bits in size).
352b0 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b ** and the k
352c0 65 79 20 76 61 6c 75 65 20 28 61 20 76 61 72 69 ey value (a vari
352d0 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 able length inte
352e0 67 65 72 2c 20 6d 61 79 20 68 61 76 65 20 61 6e ger, may have an
352f0 79 20 76 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a y value).. **
35300 20 54 68 65 20 66 69 72 73 74 20 6f 66 20 74 68 The first of th
35310 65 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f e while(...) loo
35320 70 73 20 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f ps below skips o
35330 76 65 72 20 74 68 65 20 72 65 63 6f 72 64 2d 6c ver the record-l
35340 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20 66 69 65 ength. ** fie
35350 6c 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 77 ld. The second w
35360 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 hile(...) loop c
35370 6f 70 69 65 73 20 74 68 65 20 6b 65 79 20 76 61 opies the key va
35380 6c 75 65 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 lue from the.
35390 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 ** cell on pPag
353a0 65 20 69 6e 74 6f 20 74 68 65 20 70 53 70 61 63 e into the pSpac
353b0 65 20 62 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f e buffer.. */
353c0 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e . pCell = fin
353d0 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 dCell(pPage, pPa
353e0 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 ge->nCell-1);.
353f0 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c pStop = &pCell
35400 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 [9];. while(
35410 28 2a 28 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 (*(pCell++)&0x80
35420 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 ) && pCell<pStop
35430 20 29 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 );. pStop =
35440 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 &pCell[9];. w
35450 68 69 6c 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b hile( ((*(pOut++
35460 29 20 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 ) = *(pCell++))&
35470 30 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 0x80) && pCell<p
35480 53 74 6f 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 Stop );.. /*
35490 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 64 Insert the new d
354a0 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f ivider cell into
354b0 20 70 50 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 pParent. */.
354c0 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 insertCell(pPar
354d0 65 6e 74 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 ent, pParent->nC
354e0 65 6c 6c 2c 20 70 53 70 61 63 65 2c 20 28 69 6e ell, pSpace, (in
354f0 74 29 28 70 4f 75 74 2d 70 53 70 61 63 65 29 2c t)(pOut-pSpace),
35500 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
35510 30 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 0, pPage->pgno,
35520 26 72 63 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 &rc);.. /* Se
35530 74 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c t the right-chil
35540 64 20 70 6f 69 6e 74 65 72 20 6f 66 20 70 50 61 d pointer of pPa
35550 72 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f rent to point to
35560 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a the new page. *
35570 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 /. put4byte(&
35580 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 pParent->aData[p
35590 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 Parent->hdrOffse
355a0 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a t+8], pgnoNew);.
355b0 20 20 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 . /* Releas
355c0 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 e the reference
355d0 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e to the new page.
355e0 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65 50 */. releaseP
355f0 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a age(pNew);. }..
35600 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 return rc;.}.#
35610 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
35620 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 OMIT_QUICKBALANC
35630 45 20 2a 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a E */..#if 0./*.*
35640 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
35650 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62 does not contrib
35660 75 74 65 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 ute anything to
35670 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 the operation of
35680 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69 SQLite..** it i
35690 73 20 73 6f 6d 65 74 69 6d 65 73 20 61 63 74 69 s sometimes acti
356a0 76 61 74 65 64 20 74 65 6d 70 6f 72 61 72 69 6c vated temporaril
356b0 79 20 77 68 69 6c 65 20 64 65 62 75 67 67 69 6e y while debuggin
356c0 67 20 63 6f 64 65 20 72 65 73 70 6f 6e 73 69 62 g code responsib
356d0 6c 65 20 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69 le .** for setti
356e0 6e 67 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 ng pointer-map e
356f0 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ntries..*/.stati
35700 63 20 69 6e 74 20 70 74 72 6d 61 70 43 68 65 63 c int ptrmapChec
35710 6b 50 61 67 65 73 28 4d 65 6d 50 61 67 65 20 2a kPages(MemPage *
35720 2a 61 70 50 61 67 65 2c 20 69 6e 74 20 6e 50 61 *apPage, int nPa
35730 67 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b ge){. int i, j;
35740 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 . for(i=0; i<nP
35750 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 age; i++){. P
35760 67 6e 6f 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b gno n;. u8 e;
35770 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 . MemPage *pP
35780 61 67 65 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b age = apPage[i];
35790 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
357a0 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
357b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
357c0 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 ge->isInit );..
357d0 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 for(j=0; j<pP
357e0 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 age->nCell; j++)
357f0 7b 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f {. CellInfo
35800 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 75 38 20 info;. u8
35810 2a 7a 3b 0a 20 20 20 20 20 0a 20 20 20 20 20 20 *z;. .
35820 7a 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 z = findCell(pPa
35830 67 65 2c 20 6a 29 3b 0a 20 20 20 20 20 20 62 74 ge, j);. bt
35840 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
35850 70 50 61 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 pPage, z, &info)
35860 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f ;. if( info
35870 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
35880 20 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 Pgno ovfl
35890 3d 20 67 65 74 34 62 79 74 65 28 26 7a 5b 69 6e = get4byte(&z[in
358a0 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a fo.iOverflow]);.
358b0 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 ptrmapGe
358c0 74 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c t(pBt, ovfl, &e,
358d0 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 &n);. as
358e0 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e sert( n==pPage->
358f0 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 pgno && e==PTRMA
35900 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20 P_OVERFLOW1 );.
35910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
35920 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
35930 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 63 68 . Pgno ch
35940 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 7a ild = get4byte(z
35950 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 );. ptrma
35960 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c pGet(pBt, child,
35970 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 &e, &n);.
35980 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 assert( n==pPa
35990 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 ge->pgno && e==P
359a0 54 52 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 TRMAP_BTREE );.
359b0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
359c0 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
359d0 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 f ){. Pgno
359e0 63 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 child = get4byte
359f0 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
35a00 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
35a10 38 5d 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 8]);. ptrma
35a20 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c pGet(pBt, child,
35a30 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 &e, &n);.
35a40 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 assert( n==pPage
35a50 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 ->pgno && e==PTR
35a60 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 MAP_BTREE );.
35a70 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
35a80 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 1;.}.#endif../*.
35a90 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
35aa0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79 is used to copy
35ab0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
35ac0 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 the b-tree node
35ad0 20 73 74 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 stored .** on p
35ae0 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 age pFrom to pag
35af0 65 20 70 54 6f 2e 20 49 66 20 70 61 67 65 20 70 e pTo. If page p
35b00 46 72 6f 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c From was not a l
35b10 65 61 66 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a eaf page, then.*
35b20 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 * the pointer-ma
35b30 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61 p entries for ea
35b40 63 68 20 63 68 69 6c 64 20 70 61 67 65 20 61 72 ch child page ar
35b50 65 20 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 e updated so tha
35b60 74 20 74 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20 t the.** parent
35b70 70 61 67 65 20 73 74 6f 72 65 64 20 69 6e 20 74 page stored in t
35b80 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
35b90 73 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 s page pTo. If p
35ba0 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a From contained.*
35bb0 2a 20 61 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 * any cells with
35bc0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 overflow page p
35bd0 6f 69 6e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 ointers, then th
35be0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
35bf0 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 pointer.** map e
35c00 6e 74 72 69 65 73 20 61 72 65 20 61 6c 73 6f 20 ntries are also
35c10 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 updated so that
35c20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
35c30 69 73 20 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a is page pTo..**.
35c40 2a 2a 20 49 66 20 70 46 72 6f 6d 20 69 73 20 63 ** If pFrom is c
35c50 75 72 72 65 6e 74 6c 79 20 63 61 72 72 79 69 6e urrently carryin
35c60 67 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 g any overflow c
35c70 65 6c 6c 73 20 28 65 6e 74 72 69 65 73 20 69 6e ells (entries in
35c80 20 74 68 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e the.** MemPage.
35c90 61 70 4f 76 66 6c 5b 5d 20 61 72 72 61 79 29 2c apOvfl[] array),
35ca0 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 63 6f they are not co
35cb0 70 69 65 64 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a pied to pTo. .**
35cc0 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 .** Before retur
35cd0 6e 69 6e 67 2c 20 70 61 67 65 20 70 54 6f 20 69 ning, page pTo i
35ce0 73 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 s reinitialized
35cf0 75 73 69 6e 67 20 62 74 72 65 65 49 6e 69 74 50 using btreeInitP
35d00 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 age()..**.** The
35d10 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 66 20 performance of
35d20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
35d30 20 6e 6f 74 20 63 72 69 74 69 63 61 6c 2e 20 49 not critical. I
35d40 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 t is only used b
35d50 79 20 0a 2a 2a 20 74 68 65 20 62 61 6c 61 6e 63 y .** the balanc
35d60 65 5f 73 68 61 6c 6c 6f 77 65 72 28 29 20 61 6e e_shallower() an
35d70 64 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 d balance_deeper
35d80 28 29 20 70 72 6f 63 65 64 75 72 65 73 2c 20 6e () procedures, n
35d90 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 77 68 69 either of.** whi
35da0 63 68 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 66 ch are called of
35db0 74 65 6e 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c ten under normal
35dc0 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a circumstances..
35dd0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
35de0 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 4d opyNodeContent(M
35df0 65 6d 50 61 67 65 20 2a 70 46 72 6f 6d 2c 20 4d emPage *pFrom, M
35e00 65 6d 50 61 67 65 20 2a 70 54 6f 2c 20 69 6e 74 emPage *pTo, int
35e10 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a *pRC){. if( (*
35e20 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 pRC)==SQLITE_OK
35e30 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 ){. BtShared
35e40 2a 20 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 46 * const pBt = pF
35e50 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20 20 75 38 rom->pBt;. u8
35e60 20 2a 20 63 6f 6e 73 74 20 61 46 72 6f 6d 20 3d * const aFrom =
35e70 20 70 46 72 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 pFrom->aData;.
35e80 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 54 u8 * const aT
35e90 6f 20 3d 20 70 54 6f 2d 3e 61 44 61 74 61 3b 0a o = pTo->aData;.
35ea0 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 46 int const iF
35eb0 72 6f 6d 48 64 72 20 3d 20 70 46 72 6f 6d 2d 3e romHdr = pFrom->
35ec0 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 hdrOffset;. i
35ed0 6e 74 20 63 6f 6e 73 74 20 69 54 6f 48 64 72 20 nt const iToHdr
35ee0 3d 20 28 28 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 = ((pTo->pgno==1
35ef0 29 20 3f 20 31 30 30 20 3a 20 30 29 3b 0a 20 20 ) ? 100 : 0);.
35f00 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e int rc;. in
35f10 74 20 69 44 61 74 61 3b 0a 20 20 0a 20 20 0a 20 t iData;. . .
35f20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d assert( pFrom
35f30 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 ->isInit );.
35f40 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 6e assert( pFrom->n
35f50 46 72 65 65 3e 3d 69 54 6f 48 64 72 20 29 3b 0a Free>=iToHdr );.
35f60 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 32 assert( get2
35f70 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f byte(&aFrom[iFro
35f80 6d 48 64 72 2b 35 5d 29 20 3c 3d 20 28 69 6e 74 mHdr+5]) <= (int
35f90 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 )pBt->usableSize
35fa0 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f );. . /* Co
35fb0 70 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f py the b-tree no
35fc0 64 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 de content from
35fd0 70 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 page pFrom to pa
35fe0 67 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 ge pTo. */. i
35ff0 44 61 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 Data = get2byte(
36000 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b &aFrom[iFromHdr+
36010 35 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 5]);. memcpy(
36020 26 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 &aTo[iData], &aF
36030 72 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d rom[iData], pBt-
36040 3e 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 >usableSize-iDat
36050 61 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 a);. memcpy(&
36060 61 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 aTo[iToHdr], &aF
36070 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 rom[iFromHdr], p
36080 46 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 From->cellOffset
36090 20 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c + 2*pFrom->nCel
360a0 6c 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 l);. . /* Re
360b0 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 initialize page
360c0 70 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 pTo so that the
360d0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
360e0 4d 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 MemPage structur
360f0 65 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 e. ** match t
36100 68 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 he new data. The
36110 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
36120 6f 66 20 70 54 6f 20 63 61 6e 20 61 63 74 75 61 of pTo can actua
36130 6c 6c 79 20 66 61 69 6c 20 75 6e 64 65 72 0a 20 lly fail under.
36140 20 20 20 2a 2a 20 66 61 69 72 6c 79 20 6f 62 73 ** fairly obs
36150 63 75 72 65 20 63 69 72 63 75 6d 73 74 61 6e 63 cure circumstanc
36160 65 73 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 es, even though
36170 69 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 it is a copy of
36180 69 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20 20 initialized .
36190 20 2a 2a 20 70 61 67 65 20 70 46 72 6f 6d 2e 0a ** page pFrom..
361a0 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e */. pTo->
361b0 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 isInit = 0;.
361c0 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 rc = btreeInitPa
361d0 67 65 28 70 54 6f 29 3b 0a 20 20 20 20 69 66 28 ge(pTo);. if(
361e0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
361f0 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 72 {. *pRC = r
36200 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b c;. return;
36210 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
36220 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 If this is an a
36230 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
36240 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 ase, update the
36250 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
36260 69 65 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 ies. ** for a
36270 6e 79 20 62 2d 74 72 65 65 20 6f 72 20 6f 76 65 ny b-tree or ove
36280 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 rflow pages that
36290 20 70 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69 6e pTo now contain
362a0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 74 s the pointers t
362b0 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 o.. */. if
362c0 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 ( ISAUTOVACUUM )
362d0 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 73 {. *pRC = s
362e0 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 etChildPtrmaps(p
362f0 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d To);. }. }.}
36300 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
36310 74 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 tine redistribut
36320 65 73 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 es cells on the
36330 69 50 61 72 65 6e 74 49 64 78 27 74 68 20 63 68 iParentIdx'th ch
36340 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 0a 2a ild of pParent.*
36350 2a 20 28 68 65 72 65 61 66 74 65 72 20 22 74 68 * (hereafter "th
36360 65 20 70 61 67 65 22 29 20 61 6e 64 20 75 70 20 e page") and up
36370 74 6f 20 32 20 73 69 62 6c 69 6e 67 73 20 73 6f to 2 siblings so
36380 20 74 68 61 74 20 61 6c 6c 20 70 61 67 65 73 20 that all pages
36390 68 61 76 65 20 61 62 6f 75 74 20 74 68 65 0a 2a have about the.*
363a0 2a 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f 66 * same amount of
363b0 20 66 72 65 65 20 73 70 61 63 65 2e 20 55 73 75 free space. Usu
363c0 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 73 69 ally a single si
363d0 62 6c 69 6e 67 20 6f 6e 20 65 69 74 68 65 72 20 bling on either
363e0 73 69 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 70 side of the.** p
363f0 61 67 65 20 61 72 65 20 75 73 65 64 20 69 6e 20 age are used in
36400 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 the balancing, t
36410 68 6f 75 67 68 20 62 6f 74 68 20 73 69 62 6c 69 hough both sibli
36420 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 ngs might come f
36430 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65 20 rom one.** side
36440 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 74 if the page is t
36450 68 65 20 66 69 72 73 74 20 6f 72 20 6c 61 73 74 he first or last
36460 20 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 child of its pa
36470 72 65 6e 74 2e 20 49 66 20 74 68 65 20 70 61 67 rent. If the pag
36480 65 20 0a 2a 2a 20 68 61 73 20 66 65 77 65 72 20 e .** has fewer
36490 74 68 61 6e 20 32 20 73 69 62 6c 69 6e 67 73 20 than 2 siblings
364a0 28 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 (something which
364b0 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
364c0 20 69 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 if the page.**
364d0 69 73 20 61 20 72 6f 6f 74 20 70 61 67 65 20 6f is a root page o
364e0 72 20 61 20 63 68 69 6c 64 20 6f 66 20 61 20 72 r a child of a r
364f0 6f 6f 74 20 70 61 67 65 29 20 74 68 65 6e 20 61 oot page) then a
36500 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69 62 ll available sib
36510 6c 69 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63 69 lings.** partici
36520 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 pate in the bala
36530 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ncing..**.** The
36540 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 number of sibli
36550 6e 67 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 ngs of the page
36560 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 might be increas
36570 65 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 ed or decreased
36580 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 by .** one or tw
36590 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 o in an effort t
365a0 6f 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 o keep pages nea
365b0 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 rly full but not
365c0 20 6f 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a 0a over full. .**.
365d0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 ** Note that whe
365e0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
365f0 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f s called, some o
36600 66 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 74 f the cells on t
36610 68 65 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 he page.** might
36620 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 not actually be
36630 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 stored in MemPa
36640 67 65 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69 73 ge.aData[]. This
36650 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 can happen.** i
36660 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 f the page is ov
36670 65 72 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f 75 erfull. This rou
36680 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 tine ensures tha
36690 74 20 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c 6f t all cells allo
366a0 63 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 cated.** to the
366b0 70 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 page and its sib
366c0 6c 69 6e 67 73 20 66 69 74 20 69 6e 74 6f 20 4d lings fit into M
366d0 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 20 62 emPage.aData[] b
366e0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
366f0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f .**.** In the co
36700 75 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e urse of balancin
36710 67 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 g the page and i
36720 74 73 20 73 69 62 6c 69 6e 67 73 2c 20 63 65 6c ts siblings, cel
36730 6c 73 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 73 ls may be.** ins
36740 65 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 72 65 erted into or re
36750 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 moved from the p
36760 61 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 72 arent page (pPar
36770 65 6e 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a 2a ent). Doing so.*
36780 2a 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 20 * may cause the
36790 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 62 parent page to b
367a0 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f ecome overfull o
367b0 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66 20 r underfull. If
367c0 74 68 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73 2c this.** happens,
367d0 20 69 74 20 69 73 20 74 68 65 20 72 65 73 70 6f it is the respo
367e0 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 nsibility of the
367f0 20 63 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f 6b caller to invok
36800 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a e the correct.**
36810 20 62 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 balancing routi
36820 6e 65 20 74 6f 20 66 69 78 20 74 68 69 73 20 70 ne to fix this p
36830 72 6f 62 6c 65 6d 20 28 73 65 65 20 74 68 65 20 roblem (see the
36840 62 61 6c 61 6e 63 65 28 29 20 72 6f 75 74 69 6e balance() routin
36850 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 e). .**.** If th
36860 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 is routine fails
36870 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c for any reason,
36880 20 69 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 it might leave
36890 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
368a0 69 6e 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 in a corrupted s
368b0 74 61 74 65 2e 20 53 6f 20 69 66 20 74 68 69 73 tate. So if this
368c0 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 routine fails,
368d0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f the database sho
368e0 75 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 uld.** be rolled
368f0 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 back..**.** The
36900 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 third argument
36910 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
36920 2c 20 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 73 , aOvflSpace, is
36930 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a a pointer to a.
36940 2a 2a 20 62 75 66 66 65 72 20 62 69 67 20 65 6e ** buffer big en
36950 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e 65 ough to hold one
36960 20 70 61 67 65 2e 20 49 66 20 77 68 69 6c 65 20 page. If while
36970 69 6e 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 20 inserting cells
36980 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 0a into the parent.
36990 2a 2a 20 70 61 67 65 20 28 70 50 61 72 65 6e 74 ** page (pParent
369a0 29 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 ) the parent pag
369b0 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 e becomes overfu
369c0 6c 6c 2c 20 74 68 69 73 20 62 75 66 66 65 72 20 ll, this buffer
369d0 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 is.** used to st
369e0 6f 72 65 20 74 68 65 20 70 61 72 65 6e 74 27 73 ore the parent's
369f0 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e overflow cells.
36a00 20 42 65 63 61 75 73 65 20 74 68 69 73 20 66 75 Because this fu
36a10 6e 63 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a 2a nction inserts.*
36a20 2a 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 66 * a maximum of f
36a30 6f 75 72 20 64 69 76 69 64 65 72 20 63 65 6c 6c our divider cell
36a40 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e s into the paren
36a50 74 20 70 61 67 65 2c 20 61 6e 64 20 74 68 65 20 t page, and the
36a60 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65 20 maximum.** size
36a70 6f 66 20 61 20 63 65 6c 6c 20 73 74 6f 72 65 64 of a cell stored
36a80 20 77 69 74 68 69 6e 20 61 6e 20 69 6e 74 65 72 within an inter
36a90 6e 61 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77 61 nal node is alwa
36aa0 79 73 20 6c 65 73 73 20 74 68 61 6e 20 31 2f 34 ys less than 1/4
36ab0 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2d .** of the page-
36ac0 73 69 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c 53 size, the aOvflS
36ad0 70 61 63 65 5b 5d 20 62 75 66 66 65 72 20 69 73 pace[] buffer is
36ae0 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 guaranteed to b
36af0 65 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 e large.** enoug
36b00 68 20 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66 6c h for all overfl
36b10 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 ow cells..**.**
36b20 49 66 20 61 4f 76 66 6c 53 70 61 63 65 20 69 73 If aOvflSpace is
36b30 20 73 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20 70 set to a null p
36b40 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e ointer, this fun
36b50 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a ction returns .*
36b60 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a * SQLITE_NOMEM..
36b70 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f */.#if defined(_
36b80 4d 53 43 5f 56 45 52 29 20 26 26 20 5f 4d 53 43 MSC_VER) && _MSC
36b90 5f 56 45 52 20 3e 3d 20 31 37 30 30 20 26 26 20 _VER >= 1700 &&
36ba0 64 65 66 69 6e 65 64 28 5f 4d 5f 41 52 4d 29 0a defined(_M_ARM).
36bb0 23 70 72 61 67 6d 61 20 6f 70 74 69 6d 69 7a 65 #pragma optimize
36bc0 28 22 22 2c 20 6f 66 66 29 0a 23 65 6e 64 69 66 ("", off).#endif
36bd0 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
36be0 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d nce_nonroot(. M
36bf0 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c emPage *pParent,
36c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
36c10 2a 20 50 61 72 65 6e 74 20 70 61 67 65 20 6f 66 * Parent page of
36c20 20 73 69 62 6c 69 6e 67 73 20 62 65 69 6e 67 20 siblings being
36c30 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 69 6e balanced */. in
36c40 74 20 69 50 61 72 65 6e 74 49 64 78 2c 20 20 20 t iParentIdx,
36c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
36c60 20 49 6e 64 65 78 20 6f 66 20 22 74 68 65 20 70 Index of "the p
36c70 61 67 65 22 20 69 6e 20 70 50 61 72 65 6e 74 20 age" in pParent
36c80 2a 2f 0a 20 20 75 38 20 2a 61 4f 76 66 6c 53 70 */. u8 *aOvflSp
36c90 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ace,
36ca0 20 20 20 20 20 2f 2a 20 70 61 67 65 2d 73 69 7a /* page-siz
36cb0 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
36cc0 20 66 6f 72 20 70 61 72 65 6e 74 20 6f 76 66 6c for parent ovfl
36cd0 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 6f 74 */. int isRoot
36ce0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
36cf0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
36d00 20 70 50 61 72 65 6e 74 20 69 73 20 61 20 72 6f pParent is a ro
36d10 6f 74 2d 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ot-page */. int
36d20 20 62 42 75 6c 6b 20 20 20 20 20 20 20 20 20 20 bBulk
36d30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
36d40 54 72 75 65 20 69 66 20 74 68 69 73 20 63 61 6c True if this cal
36d50 6c 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 62 l is part of a b
36d60 75 6c 6b 20 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 ulk load */.){.
36d70 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 BtShared *pBt;
36d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
36d90 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74 61 62 The whole datab
36da0 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 ase */. int nCe
36db0 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ll = 0;
36dc0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
36dd0 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 of cells in apCe
36de0 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d ll[] */. int nM
36df0 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 20 20 axCells = 0;
36e00 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 /* Alloca
36e10 74 65 64 20 73 69 7a 65 20 6f 66 20 61 70 43 65 ted size of apCe
36e20 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 72 6f ll, szCell, aFro
36e30 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 m. */. int nNew
36e40 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
36e50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
36e60 66 20 70 61 67 65 73 20 69 6e 20 61 70 4e 65 77 f pages in apNew
36e70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6c 64 [] */. int nOld
36e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
36e90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
36ea0 66 20 70 61 67 65 73 20 69 6e 20 61 70 4f 6c 64 f pages in apOld
36eb0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a [] */. int i, j
36ec0 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 , k;
36ed0 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
36ee0 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e nters */. int n
36ef0 78 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 xDiv;
36f00 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
36f10 64 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 divider slot in
36f20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d pParent->aCell[]
36f30 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 */. int rc = S
36f40 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
36f50 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e /* The return
36f60 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6c code */. u16 l
36f70 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 eafCorrection;
36f80 20 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 /* 4 if
36f90 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e pPage is a leaf.
36fa0 20 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 0 if not */.
36fb0 69 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 int leafData;
36fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
36fd0 54 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 True if pPage is
36fe0 20 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 a leaf of a LEA
36ff0 46 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 FDATA tree */.
37000 69 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b int usableSpace;
37010 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
37020 42 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 Bytes in pPage b
37030 65 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 eyond the header
37040 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c */. int pageFl
37050 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
37060 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 /* Value of p
37070 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a Page->aData[0] *
37080 2f 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c /. int subtotal
37090 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
370a0 20 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 /* Subtotal of
370b0 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f bytes in cells o
370c0 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 n one page */.
370d0 69 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b int iSpace1 = 0;
370e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
370f0 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 First unused byt
37100 65 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a e of aSpace1[] *
37110 2f 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 70 61 /. int iOvflSpa
37120 63 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ce = 0;
37130 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 /* First unused
37140 20 62 79 74 65 20 6f 66 20 61 4f 76 66 6c 53 70 byte of aOvflSp
37150 61 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 ace[] */. int s
37160 7a 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 zScratch;
37170 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
37180 6f 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 of scratch memor
37190 79 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 y requested */.
371a0 20 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b MemPage *apOld[
371b0 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a NB]; /*
371c0 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f pPage and up to
371d0 20 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f two siblings */
371e0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 . MemPage *apNe
371f0 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 w[NB+2];
37200 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 /* pPage and up
37210 74 6f 20 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 to NB siblings a
37220 66 74 65 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a fter balancing *
37230 2f 0a 20 20 75 38 20 2a 70 52 69 67 68 74 3b 20 /. u8 *pRight;
37240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
37250 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 /* Location in
37260 70 61 72 65 6e 74 20 6f 66 20 72 69 67 68 74 2d parent of right-
37270 73 69 62 6c 69 6e 67 20 70 6f 69 6e 74 65 72 20 sibling pointer
37280 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b 4e */. u8 *apDiv[N
37290 42 2d 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 B-1];
372a0 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65 6c /* Divider cel
372b0 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f ls in pParent */
372c0 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 . int cntNew[NB
372d0 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 +2];
372e0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65 6c /* Index in aCel
372f0 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74 65 l[] of cell afte
37300 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 r i-th page */.
37310 20 69 6e 74 20 63 6e 74 4f 6c 64 5b 4e 42 2b 32 int cntOld[NB+2
37320 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ]; /*
37330 20 4f 6c 64 20 69 6e 64 65 78 20 69 6e 20 61 43 Old index in aC
37340 65 6c 6c 5b 5d 20 61 66 74 65 72 20 69 2d 74 68 ell[] after i-th
37350 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 page */. int s
37360 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 zNew[NB+2];
37370 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 /* Combi
37380 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c ned size of cell
37390 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 s place on i-th
373a0 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 page */. u8 **a
373b0 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 pCell = 0;
373c0 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 /* All ce
373d0 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 lls begin balanc
373e0 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 ed */. u16 *szC
373f0 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ell;
37400 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 /* Local si
37410 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 ze of all cells
37420 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 in apCell[] */.
37430 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20 u8 *aSpace1;
37440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
37450 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70 69 65 Space for copie
37460 73 20 6f 66 20 64 69 76 69 64 65 72 73 20 63 65 s of dividers ce
37470 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 lls */. Pgno pg
37480 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
37490 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 76 61 /* Temp va
374a0 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70 61 67 r to store a pag
374b0 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 20 e number in */.
374c0 20 75 38 20 61 62 44 6f 6e 65 5b 4e 42 2b 32 5d u8 abDone[NB+2]
374d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
374e0 20 54 72 75 65 20 61 66 74 65 72 20 69 27 74 68 True after i'th
374f0 20 6e 65 77 20 70 61 67 65 20 69 73 20 70 6f 70 new page is pop
37500 75 6c 61 74 65 64 20 2a 2f 0a 20 20 50 67 6e 6f ulated */. Pgno
37510 20 61 50 67 6e 6f 5b 4e 42 2b 32 5d 3b 20 20 20 aPgno[NB+2];
37520 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
37530 20 6e 75 6d 62 65 72 73 20 6f 66 20 6e 65 77 20 numbers of new
37540 70 61 67 65 73 20 62 65 66 6f 72 65 20 73 68 75 pages before shu
37550 66 66 6c 69 6e 67 20 2a 2f 0a 20 20 75 31 36 20 ffling */. u16
37560 61 50 67 46 6c 61 67 73 5b 4e 42 2b 32 5d 3b 20 aPgFlags[NB+2];
37570 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 /* flag
37580 73 20 66 69 65 6c 64 20 6f 66 20 6e 65 77 20 70 s field of new p
37590 61 67 65 73 20 62 65 66 6f 72 65 20 73 68 75 66 ages before shuf
375a0 66 6c 69 6e 67 20 2a 2f 0a 0a 20 20 6d 65 6d 73 fling */.. mems
375b0 65 74 28 61 62 44 6f 6e 65 2c 20 30 2c 20 73 69 et(abDone, 0, si
375c0 7a 65 6f 66 28 61 62 44 6f 6e 65 29 29 3b 0a 20 zeof(abDone));.
375d0 20 70 42 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e pBt = pParent->
375e0 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
375f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
37600 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
37610 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
37620 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
37630 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 le(pParent->pDbP
37640 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a 20 age) );..#if 0.
37650 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 TRACE(("BALANCE
37660 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 20 : begin page %d
37670 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20 child of %d\n",
37680 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61 pPage->pgno, pPa
37690 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23 65 rent->pgno));.#e
376a0 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74 68 ndif.. /* At th
376b0 69 73 20 70 6f 69 6e 74 20 70 50 61 72 65 6e 74 is point pParent
376c0 20 6d 61 79 20 68 61 76 65 20 61 74 20 6d 6f 73 may have at mos
376d0 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20 63 t one overflow c
376e0 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a 2a ell. And if. **
376f0 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 63 this overflow c
37700 65 6c 6c 20 69 73 20 70 72 65 73 65 6e 74 2c 20 ell is present,
37710 69 74 20 6d 75 73 74 20 62 65 20 74 68 65 20 63 it must be the c
37720 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20 69 ell with . ** i
37730 6e 64 65 78 20 69 50 61 72 65 6e 74 49 64 78 2e ndex iParentIdx.
37740 20 54 68 69 73 20 73 63 65 6e 61 72 69 6f 20 63 This scenario c
37750 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 65 6e 20 omes about when
37760 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 this function.
37770 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 28 69 6e ** is called (in
37780 64 69 72 65 63 74 6c 79 29 20 66 72 6f 6d 20 73 directly) from s
37790 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
377a0 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 e().. */. asse
377b0 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 rt( pParent->nOv
377c0 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 erflow==0 || pPa
377d0 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d rent->nOverflow=
377e0 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =1 );. assert(
377f0 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
37800 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 ow==0 || pParent
37810 2d 3e 61 69 4f 76 66 6c 5b 30 5d 3d 3d 69 50 61 ->aiOvfl[0]==iPa
37820 72 65 6e 74 49 64 78 20 29 3b 0a 0a 20 20 69 66 rentIdx );.. if
37830 28 20 21 61 4f 76 66 6c 53 70 61 63 65 20 29 7b ( !aOvflSpace ){
37840 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
37850 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 TE_NOMEM;. }..
37860 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 69 62 /* Find the sib
37870 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 62 61 ling pages to ba
37880 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 6c 6f 63 61 lance. Also loca
37890 74 65 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 te the cells in
378a0 70 50 61 72 65 6e 74 20 0a 20 20 2a 2a 20 74 68 pParent . ** th
378b0 61 74 20 64 69 76 69 64 65 20 74 68 65 20 73 69 at divide the si
378c0 62 6c 69 6e 67 73 2e 20 41 6e 20 61 74 74 65 6d blings. An attem
378d0 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66 69 pt is made to fi
378e0 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f nd NN siblings o
378f0 6e 20 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 73 n . ** either s
37900 69 64 65 20 6f 66 20 70 50 61 67 65 2e 20 4d 6f ide of pPage. Mo
37910 72 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 re siblings are
37920 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 taken from one s
37930 69 64 65 2c 20 68 6f 77 65 76 65 72 2c 20 0a 20 ide, however, .
37940 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 ** if there are
37950 20 66 65 77 65 72 20 74 68 61 6e 20 4e 4e 20 73 fewer than NN s
37960 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68 65 20 6f iblings on the o
37970 74 68 65 72 20 73 69 64 65 2e 20 49 66 20 70 50 ther side. If pP
37980 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20 4e arent. ** has N
37990 42 20 6f 72 20 66 65 77 65 72 20 63 68 69 6c 64 B or fewer child
379a0 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68 69 ren then all chi
379b0 6c 64 72 65 6e 20 6f 66 20 70 50 61 72 65 6e 74 ldren of pParent
379c0 20 61 72 65 20 74 61 6b 65 6e 2e 20 20 0a 20 20 are taken. .
379d0 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f **. ** This loo
379e0 70 20 61 6c 73 6f 20 64 72 6f 70 73 20 74 68 65 p also drops the
379f0 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 66 divider cells f
37a00 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 rom the parent p
37a10 61 67 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 77 age. This. ** w
37a20 61 79 2c 20 74 68 65 20 72 65 6d 61 69 6e 64 65 ay, the remainde
37a30 72 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f r of the functio
37a40 6e 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 n does not have
37a50 74 6f 20 64 65 61 6c 20 77 69 74 68 20 61 6e 79 to deal with any
37a60 0a 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 63 . ** overflow c
37a70 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 72 65 ells in the pare
37a80 6e 74 20 70 61 67 65 2c 20 73 69 6e 63 65 20 69 nt page, since i
37a90 66 20 61 6e 79 20 65 78 69 73 74 65 64 20 74 68 f any existed th
37aa0 65 79 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 76 ey will. ** hav
37ab0 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 e already been r
37ac0 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 emoved.. */. i
37ad0 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 = pParent->nOve
37ae0 72 66 6c 6f 77 20 2b 20 70 50 61 72 65 6e 74 2d rflow + pParent-
37af0 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66 28 20 69 3c >nCell;. if( i<
37b00 32 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 2 ){. nxDiv =
37b10 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
37b20 20 61 73 73 65 72 74 28 20 62 42 75 6c 6b 3d 3d assert( bBulk==
37b30 30 20 7c 7c 20 62 42 75 6c 6b 3d 3d 31 20 29 3b 0 || bBulk==1 );
37b40 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e 74 . if( iParent
37b50 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 Idx==0 ){
37b60 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 .
37b70 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 nxDiv = 0;.
37b80 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e }else if( iParen
37b90 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 tIdx==i ){.
37ba0 20 6e 78 44 69 76 20 3d 20 69 2d 32 2b 62 42 75 nxDiv = i-2+bBu
37bb0 6c 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 lk;. }else{.
37bc0 20 20 20 20 20 61 73 73 65 72 74 28 20 62 42 75 assert( bBu
37bd0 6c 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 6e lk==0 );. n
37be0 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64 xDiv = iParentId
37bf0 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 x-1;. }. i
37c00 20 3d 20 32 2d 62 42 75 6c 6b 3b 0a 20 20 7d 0a = 2-bBulk;. }.
37c10 20 20 6e 4f 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 nOld = i+1;.
37c20 69 66 28 20 28 69 2b 6e 78 44 69 76 2d 70 50 61 if( (i+nxDiv-pPa
37c30 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 rent->nOverflow)
37c40 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c ==pParent->nCell
37c50 20 29 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d ){. pRight =
37c60 20 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 &pParent->aData
37c70 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 [pParent->hdrOff
37c80 73 65 74 2b 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b set+8];. }else{
37c90 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 66 69 . pRight = fi
37ca0 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
37cb0 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d i+nxDiv-pParent-
37cc0 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 7d >nOverflow);. }
37cd0 0a 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 . pgno = get4by
37ce0 74 65 28 70 52 69 67 68 74 29 3b 0a 20 20 77 68 te(pRight);. wh
37cf0 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 72 63 ile( 1 ){. rc
37d00 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
37d10 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26 61 70 e(pBt, pgno, &ap
37d20 4f 6c 64 5b 69 5d 2c 20 30 29 3b 0a 20 20 20 20 Old[i], 0);.
37d30 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
37d40 6d 65 6d 73 65 74 28 61 70 4f 6c 64 2c 20 30 2c memset(apOld, 0,
37d50 20 28 69 2b 31 29 2a 73 69 7a 65 6f 66 28 4d 65 (i+1)*sizeof(Me
37d60 6d 50 61 67 65 2a 29 29 3b 0a 20 20 20 20 20 20 mPage*));.
37d70 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
37d80 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 anup;. }.
37d90 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20 31 2b 61 nMaxCells += 1+a
37da0 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b 61 pOld[i]->nCell+a
37db0 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66 6c pOld[i]->nOverfl
37dc0 6f 77 3b 0a 20 20 20 20 69 66 28 20 28 69 2d 2d ow;. if( (i--
37dd0 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 )==0 ) break;..
37de0 20 20 20 69 66 28 20 69 2b 6e 78 44 69 76 3d 3d if( i+nxDiv==
37df0 70 50 61 72 65 6e 74 2d 3e 61 69 4f 76 66 6c 5b pParent->aiOvfl[
37e00 30 5d 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 0] && pParent->n
37e10 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
37e20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 70 50 61 apDiv[i] = pPa
37e30 72 65 6e 74 2d 3e 61 70 4f 76 66 6c 5b 30 5d 3b rent->apOvfl[0];
37e40 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 . pgno = ge
37e50 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 t4byte(apDiv[i])
37e60 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d ;. szNew[i]
37e70 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
37e80 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d Parent, apDiv[i]
37e90 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 );. pParent
37ea0 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->nOverflow = 0;
37eb0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
37ec0 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e apDiv[i] = fin
37ed0 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 dCell(pParent, i
37ee0 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e +nxDiv-pParent->
37ef0 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 nOverflow);.
37f00 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 pgno = get4byt
37f10 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 e(apDiv[i]);.
37f20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 szNew[i] = ce
37f30 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e llSizePtr(pParen
37f40 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 t, apDiv[i]);..
37f50 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 /* Drop the
37f60 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 70 cell from the p
37f70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44 69 arent page. apDi
37f80 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e 74 v[i] still point
37f90 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 s to. ** th
37fa0 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 68 e cell within th
37fb0 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 74 e parent, even t
37fc0 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 65 65 hough it has bee
37fd0 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 20 n dropped..
37fe0 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66 65 ** This is safe
37ff0 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 69 6e because droppin
38000 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 g a cell only ov
38010 65 72 77 72 69 74 65 73 20 74 68 65 20 66 69 72 erwrites the fir
38020 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 st. ** four
38030 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61 6e bytes of it, an
38040 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 d this function
38050 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 68 does not need th
38060 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a e first. **
38070 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 74 four bytes of t
38080 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e he divider cell.
38090 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 So the pointer
380a0 69 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a 20 is safe to use.
380b0 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f 6e ** later on
380c0 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 . . **.
380d0 20 20 20 2a 2a 20 42 75 74 20 6e 6f 74 20 69 66 ** But not if
380e0 20 77 65 20 61 72 65 20 69 6e 20 73 65 63 75 72 we are in secur
380f0 65 2d 64 65 6c 65 74 65 20 6d 6f 64 65 2e 20 49 e-delete mode. I
38100 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 n secure-delete
38110 6d 6f 64 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 mode,. ** t
38120 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f he dropCell() ro
38130 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 utine will overw
38140 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 rite the entire
38150 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 cell with zeroes
38160 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 .. ** In th
38170 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 is case, tempora
38180 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65 rily copy the ce
38190 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 ll into the aOvf
381a0 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a lSpace[]. *
381b0 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c * buffer. It wil
381c0 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20 l be copied out
381d0 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 again as soon as
381e0 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75 the aSpace[] bu
381f0 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 ffer. ** is
38200 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a allocated. */.
38210 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 62 if( pBt->b
38220 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 53 45 tsFlags & BTS_SE
38230 43 55 52 45 5f 44 45 4c 45 54 45 20 29 7b 0a 20 CURE_DELETE ){.
38240 20 20 20 20 20 20 20 69 6e 74 20 69 4f 66 66 3b int iOff;
38250 0a 0a 20 20 20 20 20 20 20 20 69 4f 66 66 20 3d .. iOff =
38260 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 SQLITE_PTR_TO_I
38270 4e 54 28 61 70 44 69 76 5b 69 5d 29 20 2d 20 53 NT(apDiv[i]) - S
38280 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 QLITE_PTR_TO_INT
38290 28 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 29 (pParent->aData)
382a0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 69 ;. if( (i
382b0 4f 66 66 2b 73 7a 4e 65 77 5b 69 5d 29 3e 28 69 Off+szNew[i])>(i
382c0 6e 74 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 nt)pBt->usableSi
382d0 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ze ){.
382e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
382f0 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
38300 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f 6c 64 memset(apOld
38310 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a 65 6f , 0, (i+1)*sizeo
38320 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0a 20 20 f(MemPage*));.
38330 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c goto bal
38340 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
38350 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
38360 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 memcpy(&a
38370 4f 76 66 6c 53 70 61 63 65 5b 69 4f 66 66 5d 2c OvflSpace[iOff],
38380 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 apDiv[i], szNew
38390 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 [i]);.
383a0 61 70 44 69 76 5b 69 5d 20 3d 20 26 61 4f 76 66 apDiv[i] = &aOvf
383b0 6c 53 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d lSpace[apDiv[i]-
383c0 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 3b pParent->aData];
383d0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
383e0 20 7d 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c }. dropCel
383f0 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 l(pParent, i+nxD
38400 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 iv-pParent->nOve
38410 72 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c rflow, szNew[i],
38420 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d &rc);. }. }
38430 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 .. /* Make nMax
38440 43 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 Cells a multiple
38450 20 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 of 4 in order t
38460 6f 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 o preserve 8-byt
38470 65 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 e. ** alignment
38480 20 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 */. nMaxCe