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 46 6f 72 g BTrees..** For
01c0: 20 61 20 64 65 74 61 69 6c 65 64 20 64 69 73 63 a detailed disc
01d0: 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 65 65 73 ussion of BTrees
01e0: 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a , refer to.**.**
01f0: 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 4b Donald E. K
0200: 6e 75 74 68 2c 20 54 48 45 20 41 52 54 20 4f 46 nuth, THE ART OF
0210: 20 43 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 41 COMPUTER PROGRA
0220: 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 3a MMING, Volume 3:
0230: 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 6e 67 .** "Sorting
0240: 20 41 6e 64 20 53 65 61 72 63 68 69 6e 67 22 2c And Searching",
0250: 20 70 61 67 65 73 20 34 37 33 2d 34 38 30 2e 20 pages 473-480.
0260: 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a Addison-Wesley.*
0270: 2a 20 20 20 20 20 50 75 62 6c 69 73 68 69 6e 67 * Publishing
0280: 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 6e Company, Readin
0290: 67 2c 20 4d 61 73 73 61 63 68 75 73 65 74 74 73 g, Massachusetts
02a0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 ..**.** The basi
02b0: 63 20 69 64 65 61 20 69 73 20 74 68 61 74 20 65 c idea is that e
02c0: 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 ach page of the
02d0: 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e 20 file contains N
02e0: 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74 72 database.** entr
02f0: 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69 6e ies and N+1 poin
0300: 74 65 72 73 20 74 6f 20 73 75 62 70 61 67 65 73 ters to subpages
0310: 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d ..**.** ------
0320: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0330: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0340: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 ----------.**
0360: 7c 20 20 50 74 72 28 30 29 20 7c 20 4b 65 79 28 | Ptr(0) | Key(
0370: 30 29 20 7c 20 50 74 72 28 31 29 20 7c 20 4b 65 0) | Ptr(1) | Ke
0380: 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 y(1) | ... | Key
0390: 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29 20 7c (N-1) | Ptr(N) |
03a0: 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
03b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
03c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
03d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
03e0: 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c ------.**.** All
03f0: 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 of the keys on
0400: 74 68 65 20 70 61 67 65 20 74 68 61 74 20 50 74 the page that Pt
0410: 72 28 30 29 20 70 6f 69 6e 74 73 20 74 6f 20 68 r(0) points to h
0420: 61 76 65 20 76 61 6c 75 65 73 20 6c 65 73 73 0a ave values less.
0430: 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 29 2e 20 ** than Key(0).
0440: 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 All of the keys
0450: 20 6f 6e 20 70 61 67 65 20 50 74 72 28 31 29 20 on page Ptr(1)
0460: 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 and its subpages
0470: 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 have.** values
0480: 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 greater than Key
0490: 28 30 29 20 61 6e 64 20 6c 65 73 73 20 74 68 61 (0) and less tha
04a0: 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20 6f n Key(1). All o
04b0: 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e f the keys.** on
04c0: 20 50 74 72 28 4e 29 20 61 6e 64 20 69 74 73 20 Ptr(N) and its
04d0: 73 75 62 70 61 67 65 73 20 68 61 76 65 20 76 61 subpages have va
04e0: 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 68 61 lues greater tha
04f0: 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e 64 n Key(N-1). And
0500: 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a .** so forth..**
0510: 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 61 .** Finding a pa
0520: 72 74 69 63 75 6c 61 72 20 6b 65 79 20 72 65 71 rticular key req
0530: 75 69 72 65 73 20 72 65 61 64 69 6e 67 20 4f 28 uires reading O(
0540: 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 20 66 72 log(M)) pages fr
0550: 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b 20 om the .** disk
0560: 77 68 65 72 65 20 4d 20 69 73 20 74 68 65 20 6e where M is the n
0570: 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
0580: 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a in the tree..**
0590: 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c .** In this impl
05a0: 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73 69 ementation, a si
05b0: 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e 20 68 6f ngle file can ho
05c0: 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 ld one or more s
05d0: 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 54 72 65 eparate .** BTre
05e0: 65 73 2e 20 20 45 61 63 68 20 42 54 72 65 65 20 es. Each BTree
05f0: 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 is identified by
0600: 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 69 74 the index of it
0610: 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 s root page. Th
0620: 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 e.** key and dat
0630: 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 72 79 20 a for any entry
0640: 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 are combined to
0650: 66 6f 72 6d 20 74 68 65 20 22 70 61 79 6c 6f 61 form the "payloa
0660: 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64 20 d". A.** fixed
0670: 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 amount of payloa
0680: 64 20 63 61 6e 20 62 65 20 63 61 72 72 69 65 64 d can be carried
0690: 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 74 68 65 directly on the
06a0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 database.** pag
06b0: 65 2e 20 20 49 66 20 74 68 65 20 70 61 79 6c 6f e. If the paylo
06c0: 61 64 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 ad is larger tha
06d0: 6e 20 74 68 65 20 70 72 65 73 65 74 20 61 6d 6f n the preset amo
06e0: 75 6e 74 20 74 68 65 6e 20 73 75 72 70 6c 75 73 unt then surplus
06f0: 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 20 73 74 .** bytes are st
0700: 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 ored on overflow
0710: 20 70 61 67 65 73 2e 20 20 54 68 65 20 70 61 79 pages. The pay
0720: 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e 74 72 load for an entr
0730: 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 65 y.** and the pre
0740: 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 72 20 61 ceding pointer a
0750: 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 re combined to f
0760: 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20 45 orm a "Cell". E
0770: 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 68 61 73 ach .** page has
0780: 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 20 a small header
0790: 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 which contains t
07a0: 68 65 20 50 74 72 28 4e 29 20 70 6f 69 6e 74 65 he Ptr(N) pointe
07b0: 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20 69 r and other.** i
07c0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68 20 nformation such
07d0: 61 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 6b as the size of k
07e0: 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a ey and data..**.
07f0: 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 41 49 4c ** FORMAT DETAIL
0800: 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 S.**.** The file
0810: 20 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f is divided into
0820: 20 70 61 67 65 73 2e 20 20 54 68 65 20 66 69 72 pages. The fir
0830: 73 74 20 70 61 67 65 20 69 73 20 63 61 6c 6c 65 st page is calle
0840: 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68 65 d page 1,.** the
0850: 20 73 65 63 6f 6e 64 20 69 73 20 70 61 67 65 20 second is page
0860: 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 2, and so forth.
0870: 20 20 41 20 70 61 67 65 20 6e 75 6d 62 65 72 20 A page number
0880: 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 65 of zero indicate
0890: 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 61 s.** "no such pa
08a0: 67 65 22 2e 20 20 54 68 65 20 70 61 67 65 20 73 ge". The page s
08b0: 69 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 20 70 ize can be any p
08c0: 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 ower of 2 betwee
08d0: 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e n 512 and 65536.
08e0: 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63 61 .** Each page ca
08f0: 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 62 74 n be either a bt
0900: 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 65 65 ree page, a free
0910: 6c 69 73 74 20 70 61 67 65 2c 20 61 6e 20 6f 76 list page, an ov
0920: 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 erflow.** page,
0930: 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 or a pointer-map
0940: 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 page..**.** The
0950: 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20 61 first page is a
0960: 6c 77 61 79 73 20 61 20 62 74 72 65 65 20 70 61 lways a btree pa
0970: 67 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 ge. The first 1
0980: 30 30 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 00 bytes of the
0990: 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f first.** page co
09a0: 6e 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 ntain a special
09b0: 68 65 61 64 65 72 20 28 74 68 65 20 22 66 69 6c header (the "fil
09c0: 65 20 68 65 61 64 65 72 22 29 20 74 68 61 74 20 e header") that
09d0: 64 65 73 63 72 69 62 65 73 20 74 68 65 20 66 69 describes the fi
09e0: 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 le..** The forma
09f0: 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 t of the file he
0a00: 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f ader is as follo
0a10: 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 ws:.**.** OFFS
0a20: 45 54 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 ET SIZE DES
0a30: 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
0a40: 20 30 20 20 20 20 20 20 31 36 20 20 20 20 20 48 0 16 H
0a50: 65 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 eader string: "S
0a60: 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 QLite format 3\0
0a70: 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 00".** 16
0a80: 20 20 20 20 32 20 20 20 20 20 50 61 67 65 20 73 2 Page s
0a90: 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 28 ize in bytes. (
0aa0: 31 20 6d 65 61 6e 73 20 36 35 35 33 36 29 0a 2a 1 means 65536).*
0ab0: 2a 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31 * 18 1
0ac0: 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 File format
0ad0: 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a write version.*
0ae0: 2a 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31 * 19 1
0af0: 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 File format
0b00: 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a read version.**
0b10: 20 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 1
0b20: 20 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 Bytes of unu
0b30: 73 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65 sed space at the
0b40: 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 end of each pag
0b50: 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 e.** 21
0b60: 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 1 Max embe
0b70: 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 dded payload fra
0b80: 63 74 69 6f 6e 20 28 6d 75 73 74 20 62 65 20 36 ction (must be 6
0b90: 34 29 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 20 4).** 22
0ba0: 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 62 1 Min emb
0bb0: 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
0bc0: 61 63 74 69 6f 6e 20 28 6d 75 73 74 20 62 65 20 action (must be
0bd0: 33 32 29 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 32).** 23
0be0: 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 1 Min le
0bf0: 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 af payload fract
0c00: 69 6f 6e 20 28 6d 75 73 74 20 62 65 20 33 32 29 ion (must be 32)
0c10: 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20 20 20 .** 24
0c20: 20 34 20 20 20 20 20 46 69 6c 65 20 63 68 61 6e 4 File chan
0c30: 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 ge counter.**
0c40: 20 20 32 38 20 20 20 20 20 20 20 34 20 20 20 20 28 4
0c50: 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 Reserved for fu
0c60: 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 ture use.**
0c70: 33 32 20 20 20 20 20 20 20 34 20 20 20 20 20 46 32 4 F
0c80: 69 72 73 74 20 66 72 65 65 6c 69 73 74 20 70 61 irst freelist pa
0c90: 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20 20 20 ge.** 36
0ca0: 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 4 Number
0cb0: 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 of freelist page
0cc0: 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a s in the file.**
0cd0: 20 20 20 20 20 34 30 20 20 20 20 20 20 36 30 20 40 60
0ce0: 20 20 20 20 31 35 20 34 2d 62 79 74 65 20 6d 65 15 4-byte me
0cf0: 74 61 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 ta values passed
0d00: 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79 65 72 to higher layer
0d10: 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 34 30 20 20 s.**.** 40
0d20: 20 20 20 20 20 34 20 20 20 20 20 53 63 68 65 6d 4 Schem
0d30: 61 20 63 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20 20 a cookie.**
0d40: 34 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 44 4 F
0d50: 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 ile format of sc
0d60: 68 65 6d 61 20 6c 61 79 65 72 0a 2a 2a 20 20 20 hema layer.**
0d70: 20 20 34 38 20 20 20 20 20 20 20 34 20 20 20 20 48 4
0d80: 20 53 69 7a 65 20 6f 66 20 70 61 67 65 20 63 61 Size of page ca
0d90: 63 68 65 0a 2a 2a 20 20 20 20 20 35 32 20 20 20 che.** 52
0da0: 20 20 20 20 34 20 20 20 20 20 4c 61 72 67 65 73 4 Larges
0db0: 74 20 72 6f 6f 74 2d 70 61 67 65 20 28 61 75 74 t root-page (aut
0dc0: 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d 29 0a 2a o/incr_vacuum).*
0dd0: 2a 20 20 20 20 20 35 36 20 20 20 20 20 20 20 34 * 56 4
0de0: 20 20 20 20 20 31 3d 55 54 46 2d 38 20 32 3d 55 1=UTF-8 2=U
0df0: 54 46 31 36 6c 65 20 33 3d 55 54 46 31 36 62 65 TF16le 3=UTF16be
0e00: 0a 2a 2a 20 20 20 20 20 36 30 20 20 20 20 20 20 .** 60
0e10: 20 34 20 20 20 20 20 55 73 65 72 20 76 65 72 73 4 User vers
0e20: 69 6f 6e 0a 2a 2a 20 20 20 20 20 36 34 20 20 20 ion.** 64
0e30: 20 20 20 20 34 20 20 20 20 20 49 6e 63 72 65 6d 4 Increm
0e40: 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 ental vacuum mod
0e50: 65 0a 2a 2a 20 20 20 20 20 36 38 20 20 20 20 20 e.** 68
0e60: 20 20 34 20 20 20 20 20 41 70 70 6c 69 63 61 74 4 Applicat
0e70: 69 6f 6e 2d 49 44 0a 2a 2a 20 20 20 20 20 37 32 ion-ID.** 72
0e80: 20 20 20 20 20 20 32 30 20 20 20 20 20 75 6e 75 20 unu
0e90: 73 65 64 0a 2a 2a 20 20 20 20 20 39 32 20 20 20 sed.** 92
0ea0: 20 20 20 20 34 20 20 20 20 20 54 68 65 20 76 65 4 The ve
0eb0: 72 73 69 6f 6e 2d 76 61 6c 69 64 2d 66 6f 72 20 rsion-valid-for
0ec0: 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 39 36 number.** 96
0ed0: 20 20 20 20 20 20 20 34 20 20 20 20 20 53 51 4c 4 SQL
0ee0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
0ef0: 45 52 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 ER.**.** All of
0f00: 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 the integer valu
0f10: 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 61 es are big-endia
0f20: 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 n (most signific
0f30: 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 2e ant byte first).
0f40: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 .**.** The file
0f50: 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 change counter i
0f60: 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 s incremented wh
0f70: 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
0f80: 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 is changed.** Th
0f90: 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 is counter allow
0fa0: 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 s other processe
0fb0: 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 s to know when t
0fc0: 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e he file has chan
0fd0: 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 ged.** and thus
0fe0: 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 74 when they need t
0ff0: 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 61 o flush their ca
1000: 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d che..**.** The m
1010: 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c ax embedded payl
1020: 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 oad fraction is
1030: 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 the amount of th
1040: 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a e total usable.*
1050: 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 67 * space in a pag
1060: 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f e that can be co
1070: 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e 67 nsumed by a sing
1080: 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e le cell for stan
1090: 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 dard.** B-tree (
10a0: 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 61 non-LEAFDATA) ta
10b0: 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 6f bles. A value o
10c0: 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 25 f 255 means 100%
10d0: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a . The default.*
10e0: 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 * is to limit th
10f0: 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 e maximum cell s
1100: 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 6c ize so that at l
1110: 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 6c east 4 cells wil
1120: 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 l fit.** on one
1130: 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 20 page. Thus the
1140: 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 default max embe
1150: 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 dded payload fra
1160: 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a ction is 64..**.
1170: 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 ** If the payloa
1180: 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 d for a cell is
1190: 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
11a0: 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 max payload, the
11b0: 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f n extra.** paylo
11c0: 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f ad is spilled to
11d0: 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
11e0: 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c Once an overfl
11f0: 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 ow page is alloc
1200: 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 ated,.** as many
1210: 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 62 bytes as possib
1220: 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 le are moved int
1230: 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 o the overflow p
1240: 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 74 ages without let
1250: 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c ting.** the cell
1260: 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 size drop below
1270: 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 the min embedde
1280: 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
1290: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 on..**.** The mi
12a0: 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 n leaf payload f
12b0: 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 raction is like
12c0: 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 the min embedded
12d0: 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
12e0: 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 n.** except that
12f0: 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 6c it applies to l
1300: 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c eaf nodes in a L
1310: 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 54 EAFDATA tree. T
1320: 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 he maximum.** pa
1330: 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 66 yload fraction f
1340: 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 or a LEAFDATA tr
1350: 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 30 ee is always 100
1360: 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 69 % (or 255) and i
1370: 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 t.** not specifi
1380: 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ed in the header
1390: 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 ..**.** Each btr
13a0: 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76 69 ee pages is divi
13b0: 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 73 ded into three s
13c0: 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 ections: The he
13d0: 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c ader, the.** cel
13e0: 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 2c l pointer array,
13f0: 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f and the cell co
1400: 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 67 ntent area. Pag
1410: 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 31 e 1 also has a 1
1420: 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 00-byte.** file
1430: 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75 header that occu
1440: 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 61 rs before the pa
1450: 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a ge header..**.**
1460: 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------
1470: 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 -------|.**
1480: 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 20 | file header
1490: 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 2e | 100 bytes.
14a0: 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a Page 1 only..*
14b0: 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
14c0: 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
14d0: 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 20 | page header
14e0: 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 66 | 8 bytes f
14f0: 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 62 or leaves. 12 b
1500: 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 6f ytes for interio
1510: 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 r nodes.**
1520: 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------------
1530: 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c -|.** | cel
1540: 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 l pointer |
1550: 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20 63 | 2 bytes per c
1560: 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 ell. Sorted ord
1570: 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 er..** | ar
1580: 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 20 ray |
1590: 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 | Grows downwa
15a0: 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 rd.** |
15b0: 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 |
15c0: 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d v.** |-----
15d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 -----------|.**
15e0: 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 | unallocat
15f0: 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 ed |.**
1600: 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20 20 | space
1610: 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d |.** |----
1620: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 ------------|
1630: 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 73 ^ Grows upwards
1640: 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 .** | cell
1650: 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 content | |
1660: 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 Arbitrary order
1670: 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 69 interspersed wi
1680: 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a th freeblocks..*
1690: 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 20 * | area
16a0: 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 61 | | a
16b0: 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66 72 nd free space fr
16c0: 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 agments..**
16d0: 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
16e0: 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 --|.**.** The pa
16f0: 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 ge headers looks
1700: 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
1710: 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a * OFFSET SIZ
1720: 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49 4f E DESCRIPTIO
1730: 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 N.** 0
1740: 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 20 1 Flags.
1750: 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 1: intkey, 2: ze
1760: 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 rodata, 4: leafd
1770: 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 ata, 8: leaf.**
1780: 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 20 1 2
1790: 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 20 byte offset
17a0: 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 to the first fre
17b0: 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 eblock.** 3
17c0: 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 75 2 nu
17d0: 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e mber of cells on
17e0: 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 this page.**
17f0: 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20 20 5 2
1800: 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 first byte of
1810: 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
1820: 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 area.** 7
1830: 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 6d 1 num
1840: 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 ber of fragmente
1850: 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 d free bytes.**
1860: 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20 20 8 4
1870: 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 20 Right child
1880: 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c 75 (the Ptr(N) valu
1890: 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 e). Omitted on
18a0: 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 leaves..**.** Th
18b0: 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 74 e flags define t
18c0: 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 he format of thi
18d0: 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 s btree page. T
18e0: 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 he leaf flag mea
18f0: 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 ns that.** this
1900: 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 6c page has no chil
1910: 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 dren. The zerod
1920: 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 ata flag means t
1930: 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63 61 hat this page ca
1940: 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 rries.** only ke
1950: 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 ys and no data.
1960: 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 The intkey flag
1970: 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
1980: 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 key is an intege
1990: 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 r.** which is st
19a0: 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 ored in the key
19b0: 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 size entry of th
19c0: 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 e cell header ra
19d0: 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 ther than in.**
19e0: 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 the payload area
19f0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c ..**.** The cell
1a00: 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 pointer array b
1a10: 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 egins on the fir
1a20: 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 st byte after th
1a30: 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a e page header..*
1a40: 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 * The cell point
1a50: 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e er array contain
1a60: 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 s zero or more 2
1a70: 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 -byte numbers wh
1a80: 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 ich are.** offse
1a90: 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 ts from the begi
1aa0: 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 nning of the pag
1ab0: 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f e to the cell co
1ac0: 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c ntent in the cel
1ad0: 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 l.** content are
1ae0: 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 a. The cell poi
1af0: 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 nters occur in s
1b00: 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 orted order. Th
1b10: 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 e system strives
1b20: 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 .** to keep free
1b30: 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 space after the
1b40: 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 last cell point
1b50: 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 er so that new c
1b60: 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 ells can.** be e
1b70: 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 asily added with
1b80: 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 out having to de
1b90: 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 fragment the pag
1ba0: 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f e..**.** Cell co
1bb0: 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 ntent is stored
1bc0: 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 at the very end
1bd0: 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 of the page and
1be0: 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 grows toward the
1bf0: 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 .** beginning of
1c00: 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
1c10: 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 Unused space wi
1c20: 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f thin the cell co
1c30: 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f ntent area is co
1c40: 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c llected into a l
1c50: 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a inked list of.**
1c60: 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 freeblocks. Ea
1c70: 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 ch freeblock is
1c80: 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
1c90: 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 in size. The b
1ca0: 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f yte offset.** to
1cb0: 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 the first freeb
1cc0: 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e lock is given in
1cd0: 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 the header. Fr
1ce0: 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 eeblocks occur i
1cf0: 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 n.** increasing
1d00: 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 order. Because
1d10: 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 a freeblock must
1d20: 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 be at least 4 b
1d30: 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a ytes in size,.**
1d40: 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 any group of 3
1d50: 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 or fewer unused
1d60: 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c bytes in the cel
1d70: 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 l content area c
1d80: 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f annot.** exist o
1d90: 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 n the freeblock
1da0: 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 chain. A group
1db0: 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 of 3 or fewer fr
1dc0: 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c ee bytes is call
1dd0: 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 ed.** a fragment
1de0: 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d . The total num
1df0: 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1e00: 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 all fragments is
1e10: 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e recorded..** in
1e20: 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
1e30: 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a at offset 7..**
1e40: 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 .** SIZE D
1e50: 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 ESCRIPTION.**
1e60: 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 2 Byte of
1e70: 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 fset of the next
1e80: 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 freeblock.**
1e90: 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69 2 Bytes i
1ea0: 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b n this freeblock
1eb0: 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 .**.** Cells are
1ec0: 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e of variable len
1ed0: 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 gth. Cells are
1ee0: 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 stored in the ce
1ef0: 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
1f00: 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 at.** the end of
1f10: 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e the page. Poin
1f20: 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c ters to the cell
1f30: 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c s are in the cel
1f40: 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a l pointer array.
1f50: 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 ** that immediat
1f60: 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 ely follows the
1f70: 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 page header. Ce
1f80: 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 lls is not neces
1f90: 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 sarily.** contig
1fa0: 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 uous or in order
1fb0: 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 , but cell point
1fc0: 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f ers are contiguo
1fd0: 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e us and in order.
1fe0: 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 .**.** Cell cont
1ff0: 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 ent makes use of
2000: 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
2010: 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 integers. A va
2020: 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 riable.** length
2030: 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f integer is 1 to
2040: 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74 9 bytes where t
2050: 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 he lower 7 bits
2060: 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 of each .** byte
2070: 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 are used. The
2080: 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 integer consists
2090: 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 of all bytes th
20a0: 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65 at have bit 8 se
20b0: 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 t and.** the fir
20c0: 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74 st byte with bit
20d0: 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 8 clear. The m
20e0: 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 ost significant
20f0: 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 byte of the inte
2100: 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 ger.** appears f
2110: 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c irst. A variabl
2120: 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
2130: 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 may not be more
2140: 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f than 9 bytes lo
2150: 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 ng..** As a spec
2160: 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 ial case, all 8
2170: 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 bytes of the 9th
2180: 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61 byte are used a
2190: 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a s data. This.**
21a0: 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 allows a 64-bit
21b0: 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 integer to be e
21c0: 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 ncoded in 9 byte
21d0: 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 s..**.** 0x00
21e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21f0: 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
2200: 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 x00000000.**
2210: 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 0x7f
2220: 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
2230: 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a s 0x0000007f.**
2240: 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20 0x81 0x00
2250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
2260: 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 comes 0x0000008
2270: 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 0.** 0x82 0x0
2280: 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
2290: 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
22a0: 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 00100.** 0x80
22b0: 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 0x7f
22c0: 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
22d0: 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 x0000007f.**
22e0: 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30 0x8a 0x91 0xd1 0
22f0: 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 xac 0x78 become
2300: 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a s 0x12345678.**
2310: 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78 0x81 0x81 0x
2320: 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65 81 0x81 0x01 be
2330: 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 comes 0x1020408
2340: 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 1.**.** Variable
2350: 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 length integers
2360: 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f are used for ro
2370: 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 wids and to hold
2380: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
2390: 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 * bytes of key a
23a0: 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 nd data in a btr
23b0: 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 ee cell..**.** T
23c0: 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
23d0: 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 cell looks like
23e0: 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
23f0: 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
2400: 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
2410: 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
2420: 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 the left child.
2430: 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 Omitted if leaf
2440: 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 flag is set..**
2450: 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 var Numbe
2460: 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 r of bytes of da
2470: 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 ta. Omitted if t
2480: 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 he zerodata flag
2490: 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 is set..**
24a0: 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 var Number of
24b0: 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f bytes of key. O
24c0: 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 r the key itself
24d0: 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 if intkey flag
24e0: 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 is set..**
24f0: 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a * Payload.**
2500: 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
2510: 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 t page of the ov
2520: 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f erflow chain. O
2530: 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 mitted if no ove
2540: 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 rflow.**.** Over
2550: 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 flow pages form
2560: 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 a linked list.
2570: 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74 Each page except
2580: 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d the last is com
2590: 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 pletely.** fille
25a0: 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67 d with data (pag
25b0: 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 esize - 4 bytes)
25c0: 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 . The last page
25d0: 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 can have as lit
25e0: 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 tle.** as 1 byte
25f0: 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 of data..**.**
2600: 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
2610: 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
2620: 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
2630: 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f of next overflo
2640: 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a w page.** *
2650: 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 Data.**.**
2660: 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 Freelist pages c
2670: 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 ome in two subty
2680: 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 pes: trunk pages
2690: 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e and leaf pages.
26a0: 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 The.** file he
26b0: 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 ader points to t
26c0: 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 he first in a li
26d0: 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 nked list of tru
26e0: 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 nk page. Each t
26f0: 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 runk.** page poi
2700: 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 nts to multiple
2710: 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 leaf pages. The
2720: 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 content of a le
2730: 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e af page is.** un
2740: 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 specified. A tr
2750: 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c unk page looks l
2760: 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
2770: 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
2780: 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
2790: 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
27a0: 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 of next trunk p
27b0: 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 age.** 4
27c0: 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 Number of leaf
27d0: 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 pointers on thi
27e0: 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a s page.** *
27f0: 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 zero or mor
2800: 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 e pages numbers
2810: 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 23 69 6e of leaves.*/.#in
2820: 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 6e 74 clude "sqliteInt
2830: 2e 68 22 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c .h".../* The fol
2840: 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 73 20 lowing value is
2850: 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c the maximum cell
2860: 20 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 size assuming a
2870: 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a maximum page.**
2880: 20 73 69 7a 65 20 67 69 76 65 20 61 62 6f 76 65 size give above
2890: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
28a0: 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 CELL_SIZE(pBt)
28b0: 28 28 69 6e 74 29 28 70 42 74 2d 3e 70 61 67 65 ((int)(pBt->page
28c0: 53 69 7a 65 2d 38 29 29 0a 0a 2f 2a 20 54 68 65 Size-8))../* The
28d0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
28e0: 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 of cells on a si
28f0: 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 ngle page of the
2900: 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
2910: 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 .** assumes a mi
2920: 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 nimum cell size
2930: 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20 62 of 6 bytes (4 b
2940: 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c ytes for the cel
2950: 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 l itself.** plus
2960: 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 2 bytes for the
2970: 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 index to the ce
2980: 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 ll in the page h
2990: 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a eader). Such.**
29a0: 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c small cells wil
29b0: 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 l be rare, but t
29c0: 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 hey are possible
29d0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
29e0: 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d CELL(pBt) ((pBt-
29f0: 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a >pageSize-8)/6).
2a00: 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c ./* Forward decl
2a10: 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 arations */.type
2a20: 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 def struct MemPa
2a30: 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 ge MemPage;.type
2a40: 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 def struct BtLoc
2a50: 6b 20 42 74 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 k BtLock;.typede
2a60: 66 20 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 f struct CellInf
2a70: 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 74 79 70 65 o CellInfo;.type
2a80: 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 def struct Btree
2a90: 50 74 72 6d 61 70 20 42 74 72 65 65 50 74 72 6d Ptrmap BtreePtrm
2aa0: 61 70 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ap;../*.** This
2ab0: 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69 6e is a magic strin
2ac0: 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61 g that appears a
2ad0: 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
2ae0: 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 of every.** SQLi
2af0: 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 6f te database in o
2b00: 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66 79 rder to identify
2b10: 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20 72 the file as a r
2b20: 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a eal database..**
2b30: 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 6e .** You can chan
2b40: 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 ge this value at
2b50: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 79 compile-time by
2b60: 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a 2a specifying a.**
2b70: 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 -DSQLITE_FILE_H
2b80: 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 EADER="..." on t
2b90: 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d he compiler comm
2ba0: 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a 2a and-line. The.*
2bb0: 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62 65 * header must be
2bc0: 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74 65 exactly 16 byte
2bd0: 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 s including the
2be0: 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 zero-terminator
2bf0: 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 so.** the string
2c00: 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 62 itself should b
2c10: 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73 20 e 15 characters
2c20: 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 68 long. If you ch
2c30: 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 ange.** the head
2c40: 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 75 er, then your cu
2c50: 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 6c stom library wil
2c60: 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f l not be able to
2c70: 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 61 read .** databa
2c80: 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 ses generated by
2c90: 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f the standard to
2ca0: 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61 6e ols and the stan
2cb0: 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 dard tools.** wi
2cc0: 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 ll not be able t
2cd0: 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 73 o read databases
2ce0: 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75 72 created by your
2cf0: 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 2e custom library.
2d00: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
2d10: 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 2f TE_FILE_HEADER /
2d20: 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33 34 * 123456789 1234
2d30: 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 56 */.# define
2d40: 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 SQLITE_FILE_HEAD
2d50: 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 ER "SQLite forma
2d60: 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a t 3".#endif../*.
2d70: 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c 61 ** Page type fla
2d80: 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f 6d gs. An ORed com
2d90: 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 bination of thes
2da0: 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20 61 e flags appear a
2db0: 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 62 s the.** first b
2dc0: 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 yte of on-disk i
2dd0: 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42 54 mage of every BT
2de0: 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 ree page..*/.#de
2df0: 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 20 fine PTF_INTKEY
2e00: 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65 20 0x01.#define
2e10: 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 78 PTF_ZERODATA 0x
2e20: 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 02.#define PTF_L
2e30: 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23 64 EAFDATA 0x04.#d
2e40: 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 20 efine PTF_LEAF
2e50: 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 0x08../*.**
2e60: 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
2e70: 68 69 73 20 6f 62 6a 65 63 74 20 73 74 6f 72 65 his object store
2e80: 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 s information ab
2e90: 6f 75 74 20 65 61 63 68 20 61 20 73 69 6e 67 6c out each a singl
2ea0: 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 e database.** pa
2eb0: 67 65 20 74 68 61 74 20 68 61 73 20 62 65 65 6e ge that has been
2ec0: 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d loaded into mem
2ed0: 6f 72 79 2e 20 20 54 68 65 20 69 6e 66 6f 72 6d ory. The inform
2ee0: 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 6f 62 ation in this ob
2ef0: 6a 65 63 74 0a 2a 2a 20 69 73 20 64 65 72 69 76 ject.** is deriv
2f00: 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 ed from the raw
2f10: 6f 6e 2d 64 69 73 6b 20 70 61 67 65 20 63 6f 6e on-disk page con
2f20: 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 65 tent..**.** As e
2f30: 61 63 68 20 64 61 74 61 62 61 73 65 20 70 61 67 ach database pag
2f40: 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f e is loaded into
2f50: 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 70 61 67 memory, the pag
2f60: 65 72 20 61 6c 6c 6f 63 61 74 73 20 61 6e 0a 2a er allocats an.*
2f70: 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
2f80: 69 73 20 6f 62 6a 65 63 74 20 61 6e 64 20 7a 65 is object and ze
2f90: 72 6f 73 20 74 68 65 20 66 69 72 73 74 20 38 20 ros the first 8
2fa0: 62 79 74 65 73 2e 20 20 28 54 68 69 73 20 69 73 bytes. (This is
2fb0: 20 74 68 65 0a 2a 2a 20 22 65 78 74 72 61 22 20 the.** "extra"
2fc0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f information asso
2fd0: 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 ciated with each
2fe0: 20 70 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 page of the pag
2ff0: 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 er.).**.** Acces
3000: 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 s to all fields
3010: 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
3020: 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 e is controlled
3030: 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 by the mutex.**
3040: 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 stored in MemPag
3050: 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f e.pBt->mutex..*/
3060: 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 .struct MemPage
3070: 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 {. u8 isInit;
3080: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
3090: 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 if previously i
30a0: 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 nitialized. MUST
30b0: 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 BE FIRST! */.
30c0: 75 38 20 62 42 75 73 79 3b 20 20 20 20 20 20 20 u8 bBusy;
30d0: 20 20 20 20 20 2f 2a 20 50 72 65 76 65 6e 74 20 /* Prevent
30e0: 65 6e 64 6c 65 73 73 20 6c 6f 6f 70 73 20 6f 6e endless loops on
30f0: 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
3100: 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38 20 e files */. u8
3110: 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 intKey;
3120: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 61 62 /* True if tab
3130: 6c 65 20 62 2d 74 72 65 65 73 2e 20 20 46 61 6c le b-trees. Fal
3140: 73 65 20 66 6f 72 20 69 6e 64 65 78 20 62 2d 74 se for index b-t
3150: 72 65 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 74 rees */. u8 int
3160: 4b 65 79 4c 65 61 66 3b 20 20 20 20 20 20 20 2f KeyLeaf; /
3170: 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 6c 65 * True if the le
3180: 61 66 20 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20 af of an intKey
3190: 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 table */. Pgno
31a0: 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
31b0: 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 /* Page number f
31c0: 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a or this page */.
31d0: 20 20 2f 2a 20 4f 6e 6c 79 20 74 68 65 20 66 69 /* Only the fi
31e0: 72 73 74 20 38 20 62 79 74 65 73 20 28 61 62 6f rst 8 bytes (abo
31f0: 76 65 29 20 61 72 65 20 7a 65 72 6f 65 64 20 62 ve) are zeroed b
3200: 79 20 70 61 67 65 72 2e 63 20 77 68 65 6e 20 61 y pager.c when a
3210: 20 6e 65 77 20 70 61 67 65 0a 20 20 2a 2a 20 69 new page. ** i
3220: 73 20 61 6c 6c 6f 63 61 74 65 64 2e 20 41 6c 6c s allocated. All
3230: 20 66 69 65 6c 64 73 20 74 68 61 74 20 66 6f 6c fields that fol
3240: 6c 6f 77 20 6d 75 73 74 20 62 65 20 69 6e 69 74 low must be init
3250: 69 61 6c 69 7a 65 64 20 62 65 66 6f 72 65 20 75 ialized before u
3260: 73 65 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b se */. u8 leaf;
3270: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3280: 54 72 75 65 20 69 66 20 61 20 6c 65 61 66 20 70 True if a leaf p
3290: 61 67 65 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f age */. u8 hdrO
32a0: 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
32b0: 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e 100 for page 1.
32c0: 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0 otherwise */
32d0: 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69 . u8 childPtrSi
32e0: 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20 ze; /* 0 if
32f0: 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c leaf==1. 4 if l
3300: 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 38 20 6d eaf==0 */. u8 m
3310: 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 3b 20 ax1bytePayload;
3320: 20 2f 2a 20 6d 69 6e 28 6d 61 78 4c 6f 63 61 6c /* min(maxLocal
3330: 2c 31 32 37 29 20 2a 2f 0a 20 20 75 38 20 6e 4f ,127) */. u8 nO
3340: 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 verflow;
3350: 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 /* Number of ove
3360: 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 rflow cell bodie
3370: 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a s in aCell[] */.
3380: 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 u16 maxLocal;
3390: 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
33a0: 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f f BtShared.maxLo
33b0: 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e cal or BtShared.
33c0: 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 maxLeaf */. u16
33d0: 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 minLocal;
33e0: 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 /* Copy of BtS
33f0: 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f hared.minLocal o
3400: 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 r BtShared.minLe
3410: 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c af */. u16 cell
3420: 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 Offset; /*
3430: 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f Index in aData o
3440: 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 f first cell poi
3450: 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 nter */. u16 nF
3460: 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f ree; /
3470: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 * Number of free
3480: 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 bytes on the pa
3490: 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c ge */. u16 nCel
34a0: 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l; /*
34b0: 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 Number of cells
34c0: 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f on this page, lo
34d0: 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a cal and ovfl */.
34e0: 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 u16 maskPage;
34f0: 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 /* Mask f
3500: 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a or page offset *
3510: 2f 0a 20 20 75 31 36 20 61 69 4f 76 66 6c 5b 34 /. u16 aiOvfl[4
3520: 5d 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 ]; /* Inse
3530: 72 74 20 74 68 65 20 69 2d 74 68 20 6f 76 65 72 rt the i-th over
3540: 66 6c 6f 77 20 63 65 6c 6c 20 62 65 66 6f 72 65 flow cell before
3550: 20 74 68 65 20 61 69 4f 76 66 6c 2d 74 68 0a 20 the aiOvfl-th.
3560: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3570: 20 20 20 20 20 20 2a 2a 20 6e 6f 6e 2d 6f 76 65 ** non-ove
3580: 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 rflow cell */.
3590: 75 38 20 2a 61 70 4f 76 66 6c 5b 34 5d 3b 20 20 u8 *apOvfl[4];
35a0: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 /* Pointers
35b0: 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 to the body of
35c0: 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2a overflow cells *
35d0: 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
35e0: 74 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e t; /* Poin
35f0: 74 65 72 20 74 6f 20 42 74 53 68 61 72 65 64 20 ter to BtShared
3600: 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 69 that this page i
3610: 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 75 s part of */. u
3620: 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 8 *aData;
3630: 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
3640: 6f 20 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 o disk image of
3650: 74 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f the page data */
3660: 0a 20 20 75 38 20 2a 61 44 61 74 61 45 6e 64 3b . u8 *aDataEnd;
3670: 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 /* One b
3680: 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 yte past the end
3690: 20 6f 66 20 75 73 61 62 6c 65 20 64 61 74 61 20 of usable data
36a0: 2a 2f 0a 20 20 75 38 20 2a 61 43 65 6c 6c 49 64 */. u8 *aCellId
36b0: 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 x; /* The
36c0: 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 72 65 61 cell index area
36d0: 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 4f */. u8 *aDataO
36e0: 66 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 fst; /* Sa
36f0: 6d 65 20 61 73 20 61 44 61 74 61 20 66 6f 72 20 me as aData for
3700: 6c 65 61 76 65 73 2e 20 20 61 44 61 74 61 2b 34 leaves. aData+4
3710: 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 2a 2f for interior */
3720: 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
3730: 67 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 72 ge; /* Pager
3740: 20 70 61 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a page handle */.
3750: 20 20 75 31 36 20 28 2a 78 43 65 6c 6c 53 69 7a u16 (*xCellSiz
3760: 65 29 28 4d 65 6d 50 61 67 65 2a 2c 75 38 2a 29 e)(MemPage*,u8*)
3770: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
3780: 20 63 65 6c 6c 53 69 7a 65 50 74 72 20 6d 65 74 cellSizePtr met
3790: 68 6f 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a hod */. void (*
37a0: 78 50 61 72 73 65 43 65 6c 6c 29 28 4d 65 6d 50 xParseCell)(MemP
37b0: 61 67 65 2a 2c 75 38 2a 2c 43 65 6c 6c 49 6e 66 age*,u8*,CellInf
37c0: 6f 2a 29 3b 20 2f 2a 20 62 74 72 65 65 50 61 72 o*); /* btreePar
37d0: 73 65 43 65 6c 6c 20 6d 65 74 68 6f 64 20 2a 2f seCell method */
37e0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
37f0: 4f 4d 49 54 5f 43 4f 4e 43 55 52 52 45 4e 54 0a OMIT_CONCURRENT.
3800: 20 20 75 33 32 20 70 67 6e 6f 52 6f 6f 74 3b 20 u32 pgnoRoot;
3810: 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 /* Root p
3820: 61 67 65 20 6f 66 20 62 2d 74 72 65 65 20 74 68 age of b-tree th
3830: 61 74 20 74 68 69 73 20 70 61 67 65 20 62 65 6c at this page bel
3840: 6f 6e 67 73 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 ongs to */.#endi
3850: 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 f.};../*.** A li
3860: 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 nked list of the
3870: 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
3880: 74 75 72 65 73 20 69 73 20 73 74 6f 72 65 64 20 tures is stored
3890: 61 74 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 at BtShared.pLoc
38a0: 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 k..** Locks are
38b0: 61 64 64 65 64 20 28 6f 72 20 75 70 67 72 61 64 added (or upgrad
38c0: 65 64 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 ed from READ_LOC
38d0: 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 K to WRITE_LOCK)
38e0: 20 77 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a when a cursor .
38f0: 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 ** is opened on
3900: 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 the table with r
3910: 6f 6f 74 20 70 61 67 65 20 42 74 53 68 61 72 65 oot page BtShare
3920: 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 d.iTable. Locks
3930: 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 are removed.** f
3940: 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68 rom this list wh
3950: 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e en a transaction
3960: 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 is committed or
3970: 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 rolled back, or
3980: 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 when.** a btree
3990: 20 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 handle is close
39a0: 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c d..*/.struct BtL
39b0: 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 ock {. Btree *p
39c0: 42 74 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a Btree; /*
39d0: 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f Btree handle ho
39e0: 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 lding this lock
39f0: 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 */. Pgno iTable
3a00: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f ; /* Ro
3a10: 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ot page of table
3a20: 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 */. u8 eLock;
3a30: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
3a40: 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 EAD_LOCK or WRIT
3a50: 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f E_LOCK */. BtLo
3a60: 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 ck *pNext;
3a70: 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 /* Next in BtS
3a80: 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 hared.pLock list
3a90: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 */.};../* Candi
3aa0: 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 date values for
3ab0: 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a BtLock.eLock */.
3ac0: 23 64 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 #define READ_LOC
3ad0: 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 K 1.#define
3ae0: 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a WRITE_LOCK 2.
3af0: 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64 ./* A Btree hand
3b00: 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 le.**.** A datab
3b10: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
3b20: 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
3b30: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
3b40: 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 of.** this obje
3b50: 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 ct for every dat
3b60: 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 abase file that
3b70: 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 it has open. Th
3b80: 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
3b90: 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65 is opaque to the
3ba0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
3bb0: 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62 tion. The datab
3bc0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
3bd0: 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 annot.** see the
3be0: 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 internals of th
3bf0: 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 is structure and
3c00: 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 only deals with
3c10: 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
3c20: 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
3c30: 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 **.** For some d
3c40: 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74 atabase files, t
3c50: 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 he same underlyi
3c60: 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68 ng database cach
3c70: 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 e might be .** s
3c80: 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75 hared between mu
3c90: 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f ltiple connectio
3ca0: 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 ns. In that cas
3cb0: 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 e, each connecti
3cc0: 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e on.** has it own
3cd0: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
3ce0: 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65 s object. But e
3cf0: 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ach instance of
3d00: 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 this object.** p
3d10: 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d oints to the sam
3d20: 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
3d30: 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 t. The database
3d40: 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a cache and the.*
3d50: 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 * schema associa
3d60: 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 ted with the dat
3d70: 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 abase file are a
3d80: 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 ll contained wit
3d90: 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 hin.** the BtSha
3da0: 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a red object..**.*
3db0: 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 * All fields in
3dc0: 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 this structure a
3dd0: 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 re accessed unde
3de0: 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e r sqlite3.mutex.
3df0: 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e .** The pBt poin
3e00: 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e ter itself may n
3e10: 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 ot be changed wh
3e20: 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73 ile there exists
3e30: 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 cursors .** in
3e40: 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 the referenced B
3e50: 74 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69 tShared that poi
3e60: 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 nt back to this
3e70: 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73 Btree since thos
3e80: 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 e.** cursors hav
3e90: 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 e to go through
3ea0: 74 68 69 73 20 42 74 72 65 65 20 74 6f 20 66 69 this Btree to fi
3eb0: 6e 64 20 74 68 65 69 72 20 42 74 53 68 61 72 65 nd their BtShare
3ec0: 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66 d and.** they of
3ed0: 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 ten do so withou
3ee0: 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65 t holding sqlite
3ef0: 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 3.mutex..*/.stru
3f00: 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73 71 6c ct Btree {. sql
3f10: 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
3f20: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
3f30: 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 connection holdi
3f40: 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f ng this btree */
3f50: 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
3f60: 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61 62 6c ; /* Sharabl
3f70: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 e content of thi
3f80: 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 38 20 s btree */. u8
3f90: 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 inTrans;
3fa0: 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 /* TRANS_NONE, T
3fb0: 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54 52 41 RANS_READ or TRA
3fc0: 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20 75 38 NS_WRITE */. u8
3fd0: 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20 20 20 sharable;
3fe0: 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63 /* True if we c
3ff0: 61 6e 20 73 68 61 72 65 20 70 42 74 20 77 69 74 an share pBt wit
4000: 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a h another db */.
4010: 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 u8 locked;
4020: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
4030: 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 db currently has
4040: 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 pBt locked */.
4050: 20 75 38 20 68 61 73 49 6e 63 72 62 6c 6f 62 43 u8 hasIncrblobC
4060: 75 72 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 74 ur; /* True if t
4070: 68 65 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 here are one or
4080: 6d 6f 72 65 20 49 6e 63 72 62 6c 6f 62 20 63 75 more Incrblob cu
4090: 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 77 rsors */. int w
40a0: 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a antToLock; /*
40b0: 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
40c0: 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 d calls to sqlit
40d0: 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a e3BtreeEnter() *
40e0: 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b /. int nBackup;
40f0: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
4100: 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61 of backup opera
4110: 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68 tions reading th
4120: 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 33 is btree */. u3
4130: 32 20 69 44 61 74 61 56 65 72 73 69 6f 6e 3b 20 2 iDataVersion;
4140: 20 2f 2a 20 43 6f 6d 62 69 6e 65 73 20 77 69 74 /* Combines wit
4150: 68 20 70 42 74 2d 3e 70 50 61 67 65 72 2d 3e 69 h pBt->pPager->i
4160: 44 61 74 61 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 DataVersion */.
4170: 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 Btree *pNext;
4180: 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f /* List of o
4190: 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74 ther sharable Bt
41a0: 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 rees from the sa
41b0: 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 me db */. Btree
41c0: 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a *pPrev; /*
41d0: 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 Back pointer of
41e0: 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a the same list *
41f0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
4200: 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
4210: 48 45 0a 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b HE. BtLock lock
4220: 3b 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 ; /* Objec
4230: 74 20 75 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70 t used to lock p
4240: 61 67 65 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a age 1 */.#endif.
4250: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e };../*.** Btree.
4260: 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 inTrans may take
4270: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
4280: 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a owing values..**
4290: 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 .** If the share
42a0: 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69 6f 6e d-data extension
42b0: 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 is enabled, the
42c0: 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 re may be multip
42d0: 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 le users.** of t
42e0: 68 65 20 42 74 72 65 65 20 73 74 72 75 63 74 75 he Btree structu
42f0: 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 re. At most one
4300: 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f 70 65 of these may ope
4310: 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 n a write transa
4320: 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e ction,.** but an
4330: 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 61 76 y number may hav
4340: 65 20 61 63 74 69 76 65 20 72 65 61 64 20 74 72 e active read tr
4350: 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 ansactions..*/.#
4360: 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e define TRANS_NON
4370: 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54 52 41 E 0.#define TRA
4380: 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65 66 69 NS_READ 1.#defi
4390: 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45 20 32 ne TRANS_WRITE 2
43a0: 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
43b0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
43c0: 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 ct represents a
43d0: 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
43e0: 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 file..** .** A s
43f0: 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 ingle database f
4400: 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 75 73 ile can be in us
4410: 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 e at the same ti
4420: 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 me by two.** or
4430: 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f more database co
4440: 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e nnections. When
4450: 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e two or more con
4460: 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 nections are.**
4470: 73 68 61 72 69 6e 67 20 74 68 65 20 73 61 6d 65 sharing the same
4480: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
4490: 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 each connection
44a0: 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 has it own.** pr
44b0: 69 76 61 74 65 20 42 74 72 65 65 20 6f 62 6a 65 ivate Btree obje
44c0: 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 ct for the file
44d0: 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 6f 73 and each of thos
44e0: 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74 73 0a e Btrees points.
44f0: 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 ** to this one B
4500: 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 tShared object.
4510: 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 69 BtShared.nRef i
4520: 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a s the number of.
4530: 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 ** connections c
4540: 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 urrently sharing
4550: 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 this database f
4560: 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 ile..**.** Field
4570: 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 s in this struct
4580: 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 ure are accessed
4590: 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 61 under the BtSha
45a0: 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 red.mutex.** mut
45b0: 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72 20 6e ex, except for n
45c0: 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20 77 68 Ref and pNext wh
45d0: 69 63 68 20 61 72 65 20 61 63 63 65 73 73 65 64 ich are accessed
45e0: 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c under the.** gl
45f0: 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 obal SQLITE_MUTE
4600: 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 X_STATIC_MASTER
4610: 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50 61 67 mutex. The pPag
4620: 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 er field.** may
4630: 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 not be modified
4640: 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69 74 69 once it is initi
4650: 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 ally set as long
4660: 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 as nRef>0..** T
4670: 68 65 20 70 53 63 68 65 6d 61 20 66 69 65 6c 64 he pSchema field
4680: 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e 63 65 may be set once
4690: 20 75 6e 64 65 72 20 42 74 53 68 61 72 65 64 2e under BtShared.
46a0: 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 mutex and.** the
46b0: 72 65 61 66 74 65 72 20 69 73 20 75 6e 63 68 61 reafter is uncha
46c0: 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 nged as long as
46d0: 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 nRef>0..**.** is
46e0: 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 Pending:.**.**
46f0: 20 49 66 20 61 20 42 74 53 68 61 72 65 64 20 63 If a BtShared c
4700: 6c 69 65 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f lient fails to o
4710: 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f btain a write-lo
4720: 63 6b 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 ck on a database
4730: 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28 62 65 63 .** table (bec
4740: 61 75 73 65 20 74 68 65 72 65 20 65 78 69 73 74 ause there exist
4750: 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 s one or more re
4760: 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 ad-locks on the
4770: 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65 table),.** the
4780: 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 65 6e shared-cache en
4790: 74 65 72 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f ters 'pending-lo
47a0: 63 6b 27 20 73 74 61 74 65 20 61 6e 64 20 69 73 ck' state and is
47b0: 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20 Pending is.**
47c0: 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a set to true..**.
47d0: 2a 2a 20 20 20 54 68 65 20 73 68 61 72 65 64 2d ** The shared-
47e0: 63 61 63 68 65 20 6c 65 61 76 65 73 20 74 68 65 cache leaves the
47f0: 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 'pending lock'
4800: 73 74 61 74 65 20 77 68 65 6e 20 65 69 74 68 65 state when eithe
4810: 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f r of.** the fo
4820: 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a llowing occur:.*
4830: 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54 68 65 20 *.** 1) The
4840: 63 75 72 72 65 6e 74 20 77 72 69 74 65 72 20 28 current writer (
4850: 42 74 53 68 61 72 65 64 2e 70 57 72 69 74 65 72 BtShared.pWriter
4860: 29 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 ) concludes its
4870: 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a transaction, OR.
4880: 2a 2a 20 20 20 20 20 32 29 20 54 68 65 20 6e 75 ** 2) The nu
4890: 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 mber of locks he
48a0: 6c 64 20 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e ld by other conn
48b0: 65 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f ections drops to
48c0: 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 zero..**.** w
48d0: 68 69 6c 65 20 69 6e 20 74 68 65 20 27 70 65 6e hile in the 'pen
48e0: 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 ding-lock' state
48f0: 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 , no connection
4900: 6d 61 79 20 73 74 61 72 74 20 61 20 6e 65 77 0a may start a new.
4910: 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
4920: 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 ..**.** This f
4930: 65 61 74 75 72 65 20 69 73 20 69 6e 63 6c 75 64 eature is includ
4940: 65 64 20 74 6f 20 68 65 6c 70 20 70 72 65 76 65 ed to help preve
4950: 6e 74 20 77 72 69 74 65 72 2d 73 74 61 72 76 61 nt writer-starva
4960: 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tion..*/.struct
4970: 42 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67 BtShared {. Pag
4980: 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 er *pPager;
4990: 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63 /* The page c
49a0: 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ache */. sqlite
49b0: 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 3 *db;
49c0: 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
49d0: 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 ection currently
49e0: 20 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65 using this Btre
49f0: 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 e */. BtCursor
4a00: 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 *pCursor; /*
4a10: 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 A list of all op
4a20: 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 en cursors */.
4a30: 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
4a40: 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 /* First p
4a50: 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
4a60: 61 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 65 6e ase */. u8 open
4a70: 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f Flags; /
4a80: 2a 20 46 6c 61 67 73 20 74 6f 20 73 71 6c 69 74 * Flags to sqlit
4a90: 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f e3BtreeOpen() */
4aa0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
4ab0: 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
4ac0: 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b u8 autoVacuum;
4ad0: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
4ae0: 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 if auto-vacuum i
4af0: 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 s enabled */. u
4b00: 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 8 incrVacuum;
4b10: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
4b20: 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65 incr-vacuum is e
4b30: 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 62 nabled */. u8 b
4b40: 44 6f 54 72 75 6e 63 61 74 65 3b 20 20 20 20 20 DoTruncate;
4b50: 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 75 /* True to tru
4b60: 6e 63 61 74 65 20 64 62 20 6f 6e 20 63 6f 6d 6d ncate db on comm
4b70: 69 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 it */.#endif. u
4b80: 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 8 inTransaction;
4b90: 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 /* Transact
4ba0: 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 75 ion state */. u
4bb0: 38 20 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61 8 max1bytePayloa
4bc0: 64 3b 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 d; /* Maximum
4bd0: 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 63 65 first byte of ce
4be0: 6c 6c 20 66 6f 72 20 61 20 31 2d 62 79 74 65 20 ll for a 1-byte
4bf0: 70 61 79 6c 6f 61 64 20 2a 2f 0a 23 69 66 64 65 payload */.#ifde
4c00: 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 f SQLITE_HAS_COD
4c10: 45 43 0a 20 20 75 38 20 6f 70 74 69 6d 61 6c 52 EC. u8 optimalR
4c20: 65 73 65 72 76 65 3b 20 20 20 20 2f 2a 20 44 65 eserve; /* De
4c30: 73 69 72 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 sired amount of
4c40: 72 65 73 65 72 76 65 64 20 73 70 61 63 65 20 70 reserved space p
4c50: 65 72 20 70 61 67 65 20 2a 2f 0a 23 65 6e 64 69 er page */.#endi
4c60: 66 0a 20 20 75 31 36 20 62 74 73 46 6c 61 67 73 f. u16 btsFlags
4c70: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f ; /* Boo
4c80: 6c 65 61 6e 20 70 61 72 61 6d 65 74 65 72 73 2e lean parameters.
4c90: 20 20 53 65 65 20 42 54 53 5f 2a 20 6d 61 63 72 See BTS_* macr
4ca0: 6f 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 31 os below */. u1
4cb0: 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 6 maxLocal;
4cc0: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
4cd0: 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 ocal payload in
4ce0: 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 non-LEAFDATA tab
4cf0: 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e les */. u16 min
4d00: 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f Local; /
4d10: 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Minimum local
4d20: 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c payload in non-L
4d30: 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a EAFDATA tables *
4d40: 2f 0a 20 20 75 31 36 20 6d 61 78 4c 65 61 66 3b /. u16 maxLeaf;
4d50: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
4d60: 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f imum local paylo
4d70: 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 ad in a LEAFDATA
4d80: 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 table */. u16
4d90: 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 minLeaf;
4da0: 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 /* Minimum loc
4db0: 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 al payload in a
4dc0: 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a LEAFDATA table *
4dd0: 2f 0a 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 /. u32 pageSize
4de0: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
4df0: 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
4e00: 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a es on a page */.
4e10: 20 20 75 33 32 20 75 73 61 62 6c 65 53 69 7a 65 u32 usableSize
4e20: 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
4e30: 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 r of usable byte
4e40: 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a s on each page *
4e50: 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 /. int nTransac
4e60: 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d tion; /* Num
4e70: 62 65 72 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e ber of open tran
4e80: 73 61 63 74 69 6f 6e 73 20 28 72 65 61 64 20 2b sactions (read +
4e90: 20 77 72 69 74 65 29 20 2a 2f 0a 20 20 75 33 32 write) */. u32
4ea0: 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
4eb0: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
4ec0: 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
4ed0: 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 abase */. void
4ee0: 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 *pSchema;
4ef0: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 /* Pointer to s
4f00: 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 pace allocated b
4f10: 79 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 y sqlite3BtreeSc
4f20: 68 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 hema() */. void
4f30: 20 28 2a 78 46 72 65 65 53 63 68 65 6d 61 29 28 (*xFreeSchema)(
4f40: 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 void*); /* Dest
4f50: 72 75 63 74 6f 72 20 66 6f 72 20 42 74 53 68 61 ructor for BtSha
4f60: 72 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 red.pSchema */.
4f70: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
4f80: 6d 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 mutex; /* Non-re
4f90: 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 72 65 cursive mutex re
4fa0: 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 quired to access
4fb0: 20 74 68 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a this object */.
4fc0: 20 20 42 69 74 76 65 63 20 2a 70 48 61 73 43 6f Bitvec *pHasCo
4fd0: 6e 74 65 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f ntent; /* Set o
4fe0: 66 20 70 61 67 65 73 20 6d 6f 76 65 64 20 74 6f f pages moved to
4ff0: 20 66 72 65 65 2d 6c 69 73 74 20 74 68 69 73 20 free-list this
5000: 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
5010: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
5020: 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
5030: 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
5040: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5050: 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
5060: 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
5070: 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
5080: 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 *pNext; /*
5090: 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f Next on a list o
50a0: 66 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 f sharable BtSha
50b0: 72 65 64 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 red structs */.
50c0: 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 BtLock *pLock;
50d0: 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
50e0: 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 f locks held on
50f0: 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 this shared-btre
5100: 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 e struct */. Bt
5110: 72 65 65 20 2a 70 57 72 69 74 65 72 3b 20 20 20 ree *pWriter;
5120: 20 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74 /* Btree wit
5130: 68 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e h currently open
5140: 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
5150: 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 on */.#endif. u
5160: 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 8 *pTmpSpace;
5170: 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 /* Temp spa
5180: 63 65 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f ce sufficient to
5190: 20 68 6f 6c 64 20 61 20 73 69 6e 67 6c 65 20 63 hold a single c
51a0: 65 6c 6c 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ell */.#ifndef S
51b0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 55 QLITE_OMIT_CONCU
51c0: 52 52 45 4e 54 0a 20 20 42 74 72 65 65 50 74 72 RRENT. BtreePtr
51d0: 6d 61 70 20 2a 70 4d 61 70 3b 0a 23 65 6e 64 69 map *pMap;.#endi
51e0: 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f f.};../*.** Allo
51f0: 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 42 wed values for B
5200: 74 53 68 61 72 65 64 2e 62 74 73 46 6c 61 67 73 tShared.btsFlags
5210: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 53 5f .*/.#define BTS_
5220: 52 45 41 44 5f 4f 4e 4c 59 20 20 20 20 20 20 20 READ_ONLY
5230: 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 6e 64 0x0001 /* Und
5240: 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20 erlying file is
5250: 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 readonly */.#def
5260: 69 6e 65 20 42 54 53 5f 50 41 47 45 53 49 5a 45 ine BTS_PAGESIZE
5270: 5f 46 49 58 45 44 20 20 20 30 78 30 30 30 32 20 _FIXED 0x0002
5280: 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 63 /* Page size c
5290: 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 an no longer be
52a0: 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 changed */.#defi
52b0: 6e 65 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 ne BTS_SECURE_DE
52c0: 4c 45 54 45 20 20 20 20 30 78 30 30 30 34 20 20 LETE 0x0004
52d0: 20 2f 2a 20 50 52 41 47 4d 41 20 73 65 63 75 72 /* PRAGMA secur
52e0: 65 5f 64 65 6c 65 74 65 20 69 73 20 65 6e 61 62 e_delete is enab
52f0: 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 led */.#define B
5300: 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 TS_INITIALLY_EMP
5310: 54 59 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 TY 0x0008 /*
5320: 44 61 74 61 62 61 73 65 20 77 61 73 20 65 6d 70 Database was emp
5330: 74 79 20 61 74 20 74 72 61 6e 73 20 73 74 61 72 ty at trans star
5340: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 53 t */.#define BTS
5350: 5f 4e 4f 5f 57 41 4c 20 20 20 20 20 20 20 20 20 _NO_WAL
5360: 20 20 30 78 30 30 31 30 20 20 20 2f 2a 20 44 6f 0x0010 /* Do
5370: 20 6e 6f 74 20 6f 70 65 6e 20 77 72 69 74 65 2d not open write-
5380: 61 68 65 61 64 2d 6c 6f 67 20 66 69 6c 65 73 20 ahead-log files
5390: 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 53 5f 45 */.#define BTS_E
53a0: 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 XCLUSIVE
53b0: 30 78 30 30 32 30 20 20 20 2f 2a 20 70 57 72 69 0x0020 /* pWri
53c0: 74 65 72 20 68 61 73 20 61 6e 20 65 78 63 6c 75 ter has an exclu
53d0: 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 23 64 65 sive lock */.#de
53e0: 66 69 6e 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 fine BTS_PENDING
53f0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 34 30 0x0040
5400: 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 66 6f /* Waiting fo
5410: 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20 74 6f 20 r read-locks to
5420: 63 6c 65 61 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 clear */../*.**
5430: 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
5440: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
5450: 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 ucture is used t
5460: 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 o hold informati
5470: 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 on.** about a ce
5480: 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 65 43 65 ll. The parseCe
5490: 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 69 6f 6e llPtr() function
54a0: 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 fills in this s
54b0: 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 tructure.** base
54c0: 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e d on information
54d0: 20 65 78 74 72 61 63 74 20 66 72 6f 6d 20 74 68 extract from th
54e0: 65 20 72 61 77 20 64 69 73 6b 20 70 61 67 65 2e e raw disk page.
54f0: 0a 2a 2f 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 .*/.struct CellI
5500: 6e 66 6f 20 7b 0a 20 20 69 36 34 20 6e 4b 65 79 nfo {. i64 nKey
5510: 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 ; /* The ke
5520: 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 y for INTKEY tab
5530: 6c 65 73 2c 20 6f 72 20 6e 50 61 79 6c 6f 61 64 les, or nPayload
5540: 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 otherwise */.
5550: 75 38 20 2a 70 50 61 79 6c 6f 61 64 3b 20 20 2f u8 *pPayload; /
5560: 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
5570: 20 73 74 61 72 74 20 6f 66 20 70 61 79 6c 6f 61 start of payloa
5580: 64 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c d */. u32 nPayl
5590: 6f 61 64 3b 20 20 2f 2a 20 42 79 74 65 73 20 6f oad; /* Bytes o
55a0: 66 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 f payload */. u
55b0: 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 16 nLocal; /*
55c0: 20 41 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f Amount of paylo
55d0: 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 2c ad held locally,
55e0: 20 6e 6f 74 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 not on overflow
55f0: 20 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b */. u16 nSize;
5600: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
5610: 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
5620: 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 on the main b-t
5630: 72 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a ree page */.};..
5640: 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 /*.** Maximum de
5650: 70 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 pth of an SQLite
5660: 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 B-Tree structur
5670: 65 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 e. Any B-Tree de
5680: 65 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 eper than.** thi
5690: 73 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 s will be declar
56a0: 65 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 ed corrupt. This
56b0: 20 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c value is calcul
56c0: 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a ated based on a.
56d0: 2a 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 ** maximum datab
56e0: 61 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 ase size of 2^31
56f0: 20 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d pages a minimum
5700: 20 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 fanout of 2 for
5710: 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 a.** root-node
5720: 61 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 and 3 for all ot
5730: 68 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 her internal nod
5740: 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 es..**.** If a t
5750: 72 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 ree that appears
5760: 20 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 to be taller th
5770: 61 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 an this is encou
5780: 6e 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a ntered, it is.**
5790: 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
57a0: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f e database is co
57b0: 72 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e rrupt..*/.#defin
57c0: 65 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 e BTCURSOR_MAX_D
57d0: 45 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 EPTH 20../*.** A
57e0: 20 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 cursor is a poi
57f0: 6e 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 nter to a partic
5800: 75 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 ular entry withi
5810: 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a n a particular.*
5820: 2a 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 * b-tree within
5830: 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
5840: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 .**.** The entry
5850: 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 is identified b
5860: 79 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e y its MemPage an
5870: 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a d the index in.*
5880: 2a 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b * MemPage.aCell[
5890: 5d 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a ] of the entry..
58a0: 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 **.** A single d
58b0: 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e atabase file can
58c0: 20 62 65 20 73 68 61 72 65 64 20 62 79 20 74 77 be shared by tw
58d0: 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 o more database
58e0: 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 connections,.**
58f0: 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e 6e but cursors cann
5900: 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 45 ot be shared. E
5910: 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 73 ach cursor is as
5920: 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a sociated with a.
5930: 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 ** particular da
5940: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
5950: 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 43 n identified BtC
5960: 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e ursor.pBtree.db.
5970: 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e .**.** Fields in
5980: 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
5990: 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 are accessed und
59a0: 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e er the BtShared.
59b0: 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61 mutex.** found a
59c0: 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74 t self->pBt->mut
59d0: 65 78 2e 20 0a 2a 2a 0a 2a 2a 20 73 6b 69 70 4e ex. .**.** skipN
59e0: 65 78 74 20 6d 65 61 6e 69 6e 67 3a 0a 2a 2a 20 ext meaning:.**
59f0: 20 20 20 65 53 74 61 74 65 3d 3d 53 4b 49 50 4e eState==SKIPN
5a00: 45 58 54 20 26 26 20 73 6b 69 70 4e 65 78 74 3e EXT && skipNext>
5a10: 30 3a 20 20 4e 65 78 74 20 73 71 6c 69 74 65 33 0: Next sqlite3
5a20: 42 74 72 65 65 4e 65 78 74 28 29 20 69 73 20 6e BtreeNext() is n
5a30: 6f 2d 6f 70 2e 0a 2a 2a 20 20 20 20 65 53 74 61 o-op..** eSta
5a40: 74 65 3d 3d 53 4b 49 50 4e 45 58 54 20 26 26 20 te==SKIPNEXT &&
5a50: 73 6b 69 70 4e 65 78 74 3c 30 3a 20 20 4e 65 78 skipNext<0: Nex
5a60: 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 t sqlite3BtreePr
5a70: 65 76 69 6f 75 73 28 29 20 69 73 20 6e 6f 2d 6f evious() is no-o
5a80: 70 2e 0a 2a 2a 20 20 20 20 65 53 74 61 74 65 3d p..** eState=
5a90: 3d 46 41 55 4c 54 3a 20 20 20 20 20 20 20 20 20 =FAULT:
5aa0: 20 20 20 20 20 20 20 20 20 20 43 75 72 73 6f 72 Cursor
5ab0: 20 66 61 75 6c 74 20 77 69 74 68 20 73 6b 69 70 fault with skip
5ac0: 4e 65 78 74 20 61 73 20 65 72 72 6f 72 20 63 6f Next as error co
5ad0: 64 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 de..*/.struct Bt
5ae0: 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 65 65 Cursor {. Btree
5af0: 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 *pBtree;
5b00: 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 /* The Btre
5b10: 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 e to which this
5b20: 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 2a cursor belongs *
5b30: 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
5b40: 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
5b50: 20 54 68 65 20 42 74 53 68 61 72 65 64 20 74 68 The BtShared th
5b60: 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 is cursor points
5b70: 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f to */. BtCurso
5b80: 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 r *pNext;
5b90: 20 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c 69 /* Forms a li
5ba0: 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c nked list of all
5bb0: 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 50 67 cursors */. Pg
5bc0: 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 no *aOverflow;
5bd0: 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 /* Cache
5be0: 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 of overflow pag
5bf0: 65 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 e locations */.
5c00: 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 CellInfo info;
5c10: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 /* A
5c20: 70 61 72 73 65 20 6f 66 20 74 68 65 20 63 65 6c parse of the cel
5c30: 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69 6e l we are pointin
5c40: 67 20 61 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b g at */. i64 nK
5c50: 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ey;
5c60: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 /* Size of p
5c70: 4b 65 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 Key, or last int
5c80: 65 67 65 72 20 6b 65 79 20 2a 2f 0a 20 20 76 6f eger key */. vo
5c90: 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 id *pKey;
5ca0: 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 /* Saved
5cb0: 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 75 key that was cu
5cc0: 72 73 6f 72 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 rsor last known
5cd0: 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 position */. Pg
5ce0: 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 no pgnoRoot;
5cf0: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
5d00: 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73 oot page of this
5d10: 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 6e tree */. int n
5d20: 4f 76 66 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 20 OvflAlloc;
5d30: 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 /* Allocate
5d40: 64 20 73 69 7a 65 20 6f 66 20 61 4f 76 65 72 66 d size of aOverf
5d50: 6c 6f 77 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 low[] array */.
5d60: 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b 20 20 int skipNext;
5d70: 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73 20 6e /* Prev() is n
5d80: 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76 65 2e oop if negative.
5d90: 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f 70 20 Next() is noop
5da0: 69 66 20 70 6f 73 69 74 69 76 65 2e 0a 20 20 20 if positive..
5db0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5dc0: 2a 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 69 66 ** Error code if
5dd0: 20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f eState==CURSOR_
5de0: 46 41 55 4c 54 20 2a 2f 0a 20 20 75 38 20 63 75 FAULT */. u8 cu
5df0: 72 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 rFlags;
5e00: 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 6f 72 20 /* zero or
5e10: 6d 6f 72 65 20 42 54 43 46 5f 2a 20 66 6c 61 67 more BTCF_* flag
5e20: 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 s defined below
5e30: 2a 2f 0a 20 20 75 38 20 63 75 72 50 61 67 65 72 */. u8 curPager
5e40: 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f Flags; /
5e50: 2a 20 46 6c 61 67 73 20 74 6f 20 73 65 6e 64 20 * Flags to send
5e60: 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 to sqlite3PagerG
5e70: 65 74 28 29 20 2a 2f 0a 20 20 75 38 20 65 53 74 et() */. u8 eSt
5e80: 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ate;
5e90: 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 /* One of th
5ea0: 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e e CURSOR_XXX con
5eb0: 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f stants (see belo
5ec0: 77 29 20 2a 2f 0a 20 20 75 38 20 68 69 6e 74 73 w) */. u8 hints
5ed0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5ee0: 20 20 2f 2a 20 41 73 20 63 6f 6e 66 69 67 75 72 /* As configur
5ef0: 65 64 20 62 79 20 43 75 72 73 6f 72 53 65 74 48 ed by CursorSetH
5f00: 69 6e 74 73 28 29 20 2a 2f 0a 20 20 2f 2a 20 41 ints() */. /* A
5f10: 6c 6c 20 66 69 65 6c 64 73 20 61 62 6f 76 65 20 ll fields above
5f20: 61 72 65 20 7a 65 72 6f 65 64 20 77 68 65 6e 20 are zeroed when
5f30: 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c the cursor is al
5f40: 6c 6f 63 61 74 65 64 2e 20 20 53 65 65 0a 20 20 located. See.
5f50: 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ** sqlite3BtreeC
5f60: 75 72 73 6f 72 5a 65 72 6f 28 29 2e 20 20 46 69 ursorZero(). Fi
5f70: 65 6c 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 elds that follow
5f80: 20 6d 75 73 74 20 62 65 20 6d 61 6e 75 61 6c 6c must be manuall
5f90: 79 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a y. ** initializ
5fa0: 65 64 2e 20 2a 2f 0a 20 20 69 38 20 69 50 61 67 ed. */. i8 iPag
5fb0: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
5fc0: 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
5fd0: 75 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61 urrent page in a
5fe0: 70 50 61 67 65 20 2a 2f 0a 20 20 75 38 20 63 75 pPage */. u8 cu
5ff0: 72 49 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20 rIntKey;
6000: 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 /* Value of
6010: 20 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b apPage[0]->intK
6020: 65 79 20 2a 2f 0a 20 20 75 31 36 20 69 78 3b 20 ey */. u16 ix;
6030: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6040: 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 /* Current ind
6050: 65 78 20 66 6f 72 20 61 70 50 61 67 65 5b 69 50 ex for apPage[iP
6060: 61 67 65 5d 20 2a 2f 0a 20 20 75 31 36 20 61 69 age] */. u16 ai
6070: 49 64 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 Idx[BTCURSOR_MAX
6080: 5f 44 45 50 54 48 2d 31 5d 3b 20 20 20 20 20 2f _DEPTH-1]; /
6090: 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 * Current index
60a0: 69 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a in apPage[i] */.
60b0: 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f struct KeyInfo
60c0: 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 20 *pKeyInfo;
60d0: 20 20 20 20 20 20 20 2f 2a 20 41 72 67 20 70 61 /* Arg pa
60e0: 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 ssed to comparis
60f0: 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 on function */.
6100: 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61 67 65 MemPage *apPage
6110: 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 [BTCURSOR_MAX_DE
6120: 50 54 48 5d 3b 20 2f 2a 20 50 61 67 65 73 20 66 PTH]; /* Pages f
6130: 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63 75 72 72 rom root to curr
6140: 65 6e 74 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a ent page */.};..
6150: 2f 2a 0a 2a 2a 20 4c 65 67 61 6c 20 76 61 6c 75 /*.** Legal valu
6160: 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e es for BtCursor.
6170: 63 75 72 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 curFlags.*/.#def
6180: 69 6e 65 20 42 54 43 46 5f 57 72 69 74 65 46 6c ine BTCF_WriteFl
6190: 61 67 20 20 20 20 30 78 30 31 20 20 20 2f 2a 20 ag 0x01 /*
61a0: 54 72 75 65 20 69 66 20 61 20 77 72 69 74 65 20 True if a write
61b0: 63 75 72 73 6f 72 20 2a 2f 0a 23 64 65 66 69 6e cursor */.#defin
61c0: 65 20 42 54 43 46 5f 56 61 6c 69 64 4e 4b 65 79 e BTCF_ValidNKey
61d0: 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 54 72 0x02 /* Tr
61e0: 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 ue if info.nKey
61f0: 69 73 20 76 61 6c 69 64 20 2a 2f 0a 23 64 65 66 is valid */.#def
6200: 69 6e 65 20 42 54 43 46 5f 56 61 6c 69 64 4f 76 ine BTCF_ValidOv
6210: 66 6c 20 20 20 20 30 78 30 34 20 20 20 2f 2a 20 fl 0x04 /*
6220: 54 72 75 65 20 69 66 20 61 4f 76 65 72 66 6c 6f True if aOverflo
6230: 77 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 23 64 w is valid */.#d
6240: 65 66 69 6e 65 20 42 54 43 46 5f 41 74 4c 61 73 efine BTCF_AtLas
6250: 74 20 20 20 20 20 20 20 30 78 30 38 20 20 20 2f t 0x08 /
6260: 2a 20 43 75 72 73 6f 72 20 69 73 20 70 6f 69 6e * Cursor is poin
6270: 74 69 6e 67 20 6f 74 20 74 68 65 20 6c 61 73 74 ting ot the last
6280: 20 65 6e 74 72 79 20 2a 2f 0a 23 64 65 66 69 6e entry */.#defin
6290: 65 20 42 54 43 46 5f 49 6e 63 72 62 6c 6f 62 20 e BTCF_Incrblob
62a0: 20 20 20 20 30 78 31 30 20 20 20 2f 2a 20 54 72 0x10 /* Tr
62b0: 75 65 20 69 66 20 61 6e 20 69 6e 63 72 65 6d 65 ue if an increme
62c0: 6e 74 61 6c 20 49 2f 4f 20 68 61 6e 64 6c 65 20 ntal I/O handle
62d0: 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 46 5f */.#define BTCF_
62e0: 4d 75 6c 74 69 70 6c 65 20 20 20 20 20 30 78 32 Multiple 0x2
62f0: 30 20 20 20 2f 2a 20 4d 61 79 62 65 20 61 6e 6f 0 /* Maybe ano
6300: 74 68 65 72 20 63 75 72 73 6f 72 20 6f 6e 20 74 ther cursor on t
6310: 68 65 20 73 61 6d 65 20 62 74 72 65 65 20 2a 2f he same btree */
6320: 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 ../*.** Potentia
6330: 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 43 l values for BtC
6340: 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a ursor.eState..**
6350: 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c .** CURSOR_INVAL
6360: 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 ID:.** Cursor
6370: 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e 74 20 74 does not point t
6380: 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e o a valid entry.
6390: 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e This can happen
63a0: 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 0a (for example) .
63b0: 2a 2a 20 20 20 62 65 63 61 75 73 65 20 74 68 65 ** because the
63c0: 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 table is empty
63d0: 6f 72 20 62 65 63 61 75 73 65 20 42 74 72 65 65 or because Btree
63e0: 43 75 72 73 6f 72 46 69 72 73 74 28 29 20 68 61 CursorFirst() ha
63f0: 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 20 20 s not been.**
6400: 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 called..**.** CU
6410: 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 RSOR_VALID:.**
6420: 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 Cursor points t
6430: 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e o a valid entry.
6440: 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74 getPayload() et
6450: 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 c. may be called
6460: 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 53 ..**.** CURSOR_S
6470: 4b 49 50 4e 45 58 54 3a 0a 2a 2a 20 20 20 43 75 KIPNEXT:.** Cu
6480: 72 73 6f 72 20 69 73 20 76 61 6c 69 64 20 65 78 rsor is valid ex
6490: 63 65 70 74 20 74 68 61 74 20 74 68 65 20 43 75 cept that the Cu
64a0: 72 73 6f 72 2e 73 6b 69 70 4e 65 78 74 20 66 69 rsor.skipNext fi
64b0: 65 6c 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 0a eld is non-zero.
64c0: 2a 2a 20 20 20 69 6e 64 69 63 61 74 69 6e 67 20 ** indicating
64d0: 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 73 71 that the next sq
64e0: 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 29 lite3BtreeNext()
64f0: 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 or sqlite3Btree
6500: 50 72 65 76 69 6f 75 73 28 29 0a 2a 2a 20 20 20 Previous().**
6510: 6f 70 65 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64 operation should
6520: 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a be a no-op..**.
6530: 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 ** CURSOR_REQUIR
6540: 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 ESEEK:.** The
6550: 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 table that this
6560: 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
6570: 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 d on still exist
6580: 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 s, but has been
6590: 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 .** modified s
65a0: 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 ince the cursor
65b0: 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 was last used. T
65c0: 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 he cursor positi
65d0: 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 on is saved.**
65e0: 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 in variables Bt
65f0: 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 Cursor.pKey and
6600: 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 BtCursor.nKey. W
6610: 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 hen a cursor is
6620: 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 in .** this st
6630: 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 ate, restoreCurs
6640: 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e orPosition() can
6650: 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 be called to at
6660: 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 tempt to.** se
6670: 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ek the cursor to
6680: 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 the saved posit
6690: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f ion..**.** CURSO
66a0: 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 6e R_FAULT:.** An
66b0: 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 unrecoverable e
66c0: 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 rror (an I/O err
66d0: 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 or or a malloc f
66e0: 61 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 ailure) has occu
66f0: 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 rred.** on a d
6700: 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 ifferent connect
6710: 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 ion that shares
6720: 74 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 the BtShared cac
6730: 68 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 he with this.**
6740: 20 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 cursor. The e
6750: 72 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 rror has left th
6760: 65 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e e cache in an in
6770: 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 consistent state
6780: 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e ..** Do nothin
6790: 67 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 g else with this
67a0: 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 cursor. Any at
67b0: 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 tempt to use the
67c0: 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f cursor.** sho
67d0: 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 uld return the e
67e0: 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 rror code stored
67f0: 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 in BtCursor.ski
6800: 70 4e 65 78 74 0a 2a 2f 0a 23 64 65 66 69 6e 65 pNext.*/.#define
6810: 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 CURSOR_INVALID
6820: 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 0.#def
6830: 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 ine CURSOR_VALID
6840: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 1.#
6850: 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 53 4b define CURSOR_SK
6860: 49 50 4e 45 58 54 20 20 20 20 20 20 20 20 20 20 IPNEXT
6870: 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 2.#define CURSOR
6880: 5f 52 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 _REQUIRESEEK
6890: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 43 55 52 3.#define CUR
68a0: 53 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 SOR_FAULT
68b0: 20 20 20 20 20 20 34 0a 0a 2f 2a 20 0a 2a 2a 20 4../* .**
68c0: 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 The database pag
68d0: 65 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 e the PENDING_BY
68e0: 54 45 20 6f 63 63 75 70 69 65 73 2e 20 54 68 69 TE occupies. Thi
68f0: 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72 20 s page is never
6900: 75 73 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e used..*/.# defin
6910: 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 e PENDING_BYTE_P
6920: 41 47 45 28 70 42 74 29 20 50 41 47 45 52 5f 4d AGE(pBt) PAGER_M
6930: 4a 5f 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a J_PGNO(pBt)../*.
6940: 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 ** These macros
6950: 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 define the locat
6960: 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 ion of the point
6970: 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 er-map entry for
6980: 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 a .** database
6990: 70 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 page. The first
69a0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 argument to each
69b0: 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
69c0: 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 f usable.** byte
69d0: 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f s on each page o
69e0: 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 f the database (
69f0: 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54 68 65 often 1024). The
6a00: 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a second is the.*
6a10: 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f * page number to
6a20: 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 look up in the
6a30: 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a pointer map..**.
6a40: 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ** PTRMAP_PAGENO
6a50: 20 72 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 returns the dat
6a60: 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 abase page numbe
6a70: 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 r of the pointer
6a80: 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 -map.** page tha
6a90: 74 20 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 t stores the req
6aa0: 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 uired pointer. P
6ab0: 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 TRMAP_PTROFFSET
6ac0: 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f returns.** the o
6ad0: 66 66 73 65 74 20 6f 66 20 74 68 65 20 72 65 71 ffset of the req
6ae0: 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79 uested map entry
6af0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
6b00: 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 73 gno argument pas
6b10: 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 sed to PTRMAP_PA
6b20: 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 GENO is a pointe
6b30: 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 r-map page,.** t
6b40: 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 75 hen pgno is retu
6b50: 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d rned. So (pgno==
6b60: 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 PTRMAP_PAGENO(pg
6b70: 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 sz, pgno)) can b
6b80: 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 73 e.** used to tes
6b90: 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61 20 70 t if pgno is a p
6ba0: 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e ointer-map page.
6bb0: 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 20 69 PTRMAP_ISPAGE i
6bc0: 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 mplements.** thi
6bd0: 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 s test..*/.#defi
6be0: 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ne PTRMAP_PAGENO
6bf0: 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d (pBt, pgno) ptrm
6c00: 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 apPageno(pBt, pg
6c10: 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d no).#define PTRM
6c20: 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70 67 70 AP_PTROFFSET(pgp
6c30: 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35 2a trmap, pgno) (5*
6c40: 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d 31 (pgno-pgptrmap-1
6c50: 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 )).#define PTRMA
6c60: 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 P_ISPAGE(pBt, pg
6c70: 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41 47 45 no) (PTRMAP_PAGE
6c80: 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f 29 29 NO((pBt),(pgno))
6c90: 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a ==(pgno))../*.**
6ca0: 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
6cb0: 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61 62 is a lookup tab
6cc0: 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 le that identifi
6cd0: 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 es the parent pa
6ce0: 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 63 ge for.** each c
6cf0: 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74 68 65 hild page in the
6d00: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
6d10: 20 54 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 The parent page
6d20: 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 is the page tha
6d30: 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 t.** contains a
6d40: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
6d50: 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70 61 67 hild. Every pag
6d60: 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
6d70: 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 e contains.** 0
6d80: 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61 67 65 or 1 parent page
6d90: 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63 6f 6e s. (In this con
6da0: 74 65 78 74 20 27 64 61 74 61 62 61 73 65 20 70 text 'database p
6db0: 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a 20 74 age' refers.** t
6dc0: 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 o any page that
6dd0: 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 is not part of t
6de0: 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
6df0: 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20 70 6f tself.) Each po
6e00: 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 inter map.** ent
6e10: 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 ry consists of a
6e20: 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27 74 79 single byte 'ty
6e30: 70 65 27 20 61 6e 64 20 61 20 34 20 62 79 74 65 pe' and a 4 byte
6e40: 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d parent page num
6e50: 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52 4d ber..** The PTRM
6e60: 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66 69 65 AP_XXX identifie
6e70: 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74 68 65 rs below are the
6e80: 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a 2a valid types..**
6e90: 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 .** The purpose
6ea0: 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d of the pointer m
6eb0: 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c 69 74 ap is to facilit
6ec0: 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73 20 66 y moving pages f
6ed0: 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 rom one.** posit
6ee0: 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 20 ion in the file
6ef0: 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20 70 61 to another as pa
6f00: 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75 75 6d rt of autovacuum
6f10: 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65 0a 2a . When a page.*
6f20: 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68 65 20 * is moved, the
6f30: 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73 20 70 pointer in its p
6f40: 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 75 70 arent must be up
6f50: 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 dated to point t
6f60: 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 o the.** new loc
6f70: 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69 6e ation. The poin
6f80: 74 65 72 20 6d 61 70 20 69 73 20 75 73 65 64 20 ter map is used
6f90: 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 70 61 to locate the pa
6fa0: 72 65 6e 74 20 70 61 67 65 20 71 75 69 63 6b 6c rent page quickl
6fb0: 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f y..**.** PTRMAP_
6fc0: 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20 64 61 ROOTPAGE: The da
6fd0: 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 tabase page is a
6fe0: 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65 20 root-page. The
6ff0: 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e page-number is n
7000: 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ot.**
7010: 20 20 20 20 20 20 20 75 73 65 64 20 69 6e 20 74 used in t
7020: 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
7030: 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 3a PTRMAP_FREEPAGE:
7040: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
7050: 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65 64 20 ge is an unused
7060: 28 66 72 65 65 29 20 70 61 67 65 2e 20 54 68 65 (free) page. The
7070: 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a page-number .**
7080: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7090: 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e is not used in
70a0: 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a this case..**.*
70b0: 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f * PTRMAP_OVERFLO
70c0: 57 31 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 W1: The database
70d0: 20 70 61 67 65 20 69 73 20 74 68 65 20 66 69 72 page is the fir
70e0: 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 st page in a lis
70f0: 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 t of .**
7100: 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 overf
7110: 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 low pages. The p
7120: 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65 6e 74 age number ident
7130: 69 66 69 65 73 20 74 68 65 20 70 61 67 65 20 74 ifies the page t
7140: 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 hat.**
7150: 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e contain
7160: 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 s the cell with
7170: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 a pointer to thi
7180: 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e s overflow page.
7190: 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 .**.** PTRMAP_OV
71a0: 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64 61 74 ERFLOW2: The dat
71b0: 61 62 61 73 65 20 70 61 67 65 20 69 73 20 74 68 abase page is th
71c0: 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74 65 e second or late
71d0: 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 r page in a list
71e0: 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
71f0: 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f overflo
7200: 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 w pages. The pag
7210: 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 e-number identif
7220: 69 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 ies the previous
7230: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
7240: 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 74 68 page in th
7250: 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 e overflow page
7260: 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d list..**.** PTRM
7270: 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20 64 61 AP_BTREE: The da
7280: 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 tabase page is a
7290: 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65 20 non-root btree
72a0: 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 6e page. The page n
72b0: 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 umber.**
72c0: 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 69 65 identifie
72d0: 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 s the parent pag
72e0: 65 20 69 6e 20 74 68 65 20 62 74 72 65 65 2e 0a e in the btree..
72f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 */.#define PTRMA
7300: 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23 64 65 P_ROOTPAGE 1.#de
7310: 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52 45 45 fine PTRMAP_FREE
7320: 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65 20 50 PAGE 2.#define P
7330: 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 TRMAP_OVERFLOW1
7340: 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 3.#define PTRMAP
7350: 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23 64 65 _OVERFLOW2 4.#de
7360: 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 45 fine PTRMAP_BTRE
7370: 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68 20 E 5../* A bunch
7380: 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74 of assert() stat
7390: 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63 6b 20 ements to check
73a0: 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
73b0: 73 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 0a state variables.
73c0: 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70 20 28 ** of handle p (
73d0: 74 79 70 65 20 42 74 72 65 65 2a 29 20 61 72 65 type Btree*) are
73e0: 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 internally cons
73f0: 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 istent..*/.#defi
7400: 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 ne btreeIntegrit
7410: 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72 74 28 y(p) \. assert(
7420: 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 p->pBt->inTrans
7430: 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f action!=TRANS_NO
7440: 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 NE || p->pBt->nT
7450: 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29 3b ransaction==0 );
7460: 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e \. assert( p->
7470: 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
7480: 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20 29 on>=p->inTrans )
7490: 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 ; .../*.** The I
74a0: 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 SAUTOVACUUM macr
74b0: 6f 20 69 73 20 75 73 65 64 20 77 69 74 68 69 6e o is used within
74c0: 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 balance_nonroot
74d0: 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a () to determine.
74e0: 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ** if the databa
74f0: 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f se supports auto
7500: 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 -vacuum or not.
7510: 42 65 63 61 75 73 65 20 69 74 20 69 73 20 75 73 Because it is us
7520: 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e 20 ed.** within an
7530: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
7540: 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 is an argument t
7550: 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f 20 o another macro
7560: 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c 6f .** (sqliteMallo
7570: 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e 6f 74 cRaw), it is not
7580: 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 possible to use
7590: 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f 6d conditional com
75a0: 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c pilation..** So,
75b0: 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 64 this macro is d
75c0: 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a efined instead..
75d0: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
75e0: 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
75f0: 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 .#define ISAUTOV
7600: 41 43 55 55 4d 20 30 0a 23 65 6c 73 65 0a 23 64 ACUUM 0.#else.#d
7610: 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 efine ISAUTOVACU
7620: 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 UM (pBt->autoVac
7630: 75 75 6d 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 uum).#endif..#if
7640: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7650: 43 4f 4e 43 55 52 52 45 4e 54 0a 23 20 64 65 66 CONCURRENT.# def
7660: 69 6e 65 20 49 53 43 4f 4e 43 55 52 52 45 4e 54 ine ISCONCURRENT
7670: 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 0.#else.# defin
7680: 65 20 49 53 43 4f 4e 43 55 52 52 45 4e 54 20 28 e ISCONCURRENT (
7690: 70 42 74 2d 3e 70 4d 61 70 21 3d 30 29 0a 23 65 pBt->pMap!=0).#e
76a0: 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 52 45 ndif..#define RE
76b0: 51 55 49 52 45 5f 50 54 52 4d 41 50 20 28 49 53 QUIRE_PTRMAP (IS
76c0: 41 55 54 4f 56 41 43 55 55 4d 20 7c 7c 20 49 53 AUTOVACUUM || IS
76d0: 43 4f 4e 43 55 52 52 45 4e 54 29 0a 0a 2f 2a 0a CONCURRENT)../*.
76e0: 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
76f0: 65 20 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 e is passed arou
7700: 6e 64 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 nd through all t
7710: 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 he sanity checki
7720: 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 ng routines.** i
7730: 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
7740: 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c track of some gl
7750: 6f 62 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 obal state infor
7760: 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 mation..**.** Th
7770: 65 20 61 52 65 66 5b 5d 20 61 72 72 61 79 20 69 e aRef[] array i
7780: 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 6f 20 74 s allocated so t
7790: 68 61 74 20 74 68 65 72 65 20 69 73 20 31 20 62 hat there is 1 b
77a0: 69 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 it for each page
77b0: 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 in.** the datab
77c0: 61 73 65 2e 20 41 73 20 74 68 65 20 69 6e 74 65 ase. As the inte
77d0: 67 72 69 74 79 2d 63 68 65 63 6b 20 70 72 6f 63 grity-check proc
77e0: 65 65 64 73 2c 20 66 6f 72 20 65 61 63 68 20 70 eeds, for each p
77f0: 61 67 65 20 75 73 65 64 20 69 6e 0a 2a 2a 20 74 age used in.** t
7800: 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 he database the
7810: 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
7820: 74 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 61 t is set. This a
7830: 6c 6c 6f 77 73 20 69 6e 74 65 67 72 69 74 79 2d llows integrity-
7840: 63 68 65 63 6b 20 74 6f 20 0a 2a 2a 20 64 65 74 check to .** det
7850: 65 63 74 20 70 61 67 65 73 20 74 68 61 74 20 61 ect pages that a
7860: 72 65 20 75 73 65 64 20 74 77 69 63 65 20 61 6e re used twice an
7870: 64 20 6f 72 70 68 61 6e 65 64 20 70 61 67 65 73 d orphaned pages
7880: 20 28 62 6f 74 68 20 6f 66 20 77 68 69 63 68 20 (both of which
7890: 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20 63 6f 72 .** indicate cor
78a0: 72 75 70 74 69 6f 6e 29 2e 0a 2a 2f 0a 74 79 70 ruption)..*/.typ
78b0: 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 edef struct Inte
78c0: 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72 69 74 grityCk Integrit
78d0: 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74 65 yCk;.struct Inte
78e0: 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74 53 68 grityCk {. BtSh
78f0: 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 2f 2a ared *pBt; /*
7900: 20 54 68 65 20 74 72 65 65 20 62 65 69 6e 67 20 The tree being
7910: 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 checked out */.
7920: 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 Pager *pPager;
7930: 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 /* The associ
7940: 61 74 65 64 20 70 61 67 65 72 2e 20 20 41 6c 73 ated pager. Als
7950: 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62 79 20 o accessible by
7960: 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a 20 pBt->pPager */.
7970: 20 75 38 20 2a 61 50 67 52 65 66 3b 20 20 20 20 u8 *aPgRef;
7980: 20 20 20 2f 2a 20 31 20 62 69 74 20 70 65 72 20 /* 1 bit per
7990: 70 61 67 65 20 69 6e 20 74 68 65 20 64 62 20 28 page in the db (
79a0: 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 see above) */.
79b0: 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 Pgno nPage;
79c0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
79d0: 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
79e0: 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 base */. int mx
79f0: 45 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 Err; /* S
7a00: 74 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 top accumulating
7a10: 20 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 69 errors when thi
7a20: 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 2a s reaches zero *
7a30: 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 /. int nErr;
7a40: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
7a50: 6f 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 of messages writ
7a60: 74 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 ten to zErrMsg s
7a70: 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d o far */. int m
7a80: 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 allocFailed; /*
7a90: 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 A memory allocat
7aa0: 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 ion error has oc
7ab0: 63 75 72 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 curred */. cons
7ac0: 74 20 63 68 61 72 20 2a 7a 50 66 78 3b 20 2f 2a t char *zPfx; /*
7ad0: 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 Error message p
7ae0: 72 65 66 69 78 20 2a 2f 0a 20 20 69 6e 74 20 76 refix */. int v
7af0: 31 2c 20 76 32 3b 20 20 20 20 20 20 20 2f 2a 20 1, v2; /*
7b00: 56 61 6c 75 65 73 20 66 6f 72 20 75 70 20 74 6f Values for up to
7b10: 20 74 77 6f 20 25 64 20 66 69 65 6c 64 73 20 69 two %d fields i
7b20: 6e 20 7a 50 66 78 20 2a 2f 0a 20 20 53 74 72 41 n zPfx */. StrA
7b30: 63 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a ccum errMsg; /*
7b40: 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 Accumulate the
7b50: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 error message te
7b60: 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 75 33 32 xt here */. u32
7b70: 20 2a 68 65 61 70 3b 20 20 20 20 20 20 20 20 2f *heap; /
7b80: 2a 20 4d 69 6e 2d 68 65 61 70 20 75 73 65 64 20 * Min-heap used
7b90: 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 63 65 for analyzing ce
7ba0: 6c 6c 20 63 6f 76 65 72 61 67 65 20 2a 2f 0a 7d ll coverage */.}
7bb0: 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 ;../*.** Routine
7bc0: 73 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 s to read or wri
7bd0: 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f te a two- and fo
7be0: 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 ur-byte big-endi
7bf0: 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 an integer value
7c00: 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 s..*/.#define ge
7c10: 74 32 62 79 74 65 28 78 29 20 20 20 28 28 78 29 t2byte(x) ((x)
7c20: 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 [0]<<8 | (x)[1])
7c30: 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 74 .#define put2byt
7c40: 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d e(p,v) ((p)[0] =
7c50: 20 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 (u8)((v)>>8), (
7c60: 70 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 p)[1] = (u8)(v))
7c70: 0a 23 64 65 66 69 6e 65 20 67 65 74 34 62 79 74 .#define get4byt
7c80: 65 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 e sqlite3Get4byt
7c90: 65 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62 79 e.#define put4by
7ca0: 74 65 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 te sqlite3Put4by
7cb0: 74 65 0a 0a 2f 2a 0a 2a 2a 20 67 65 74 32 62 79 te../*.** get2by
7cc0: 74 65 41 6c 69 67 6e 65 64 28 29 2c 20 75 6e 6c teAligned(), unl
7cd0: 69 6b 65 20 67 65 74 32 62 79 74 65 28 29 2c 20 ike get2byte(),
7ce0: 72 65 71 75 69 72 65 73 20 74 68 61 74 20 69 74 requires that it
7cf0: 73 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 s argument point
7d00: 20 74 6f 20 61 0a 2a 2a 20 74 77 6f 2d 62 79 74 to a.** two-byt
7d10: 65 20 61 6c 69 67 6e 65 64 20 61 64 64 72 65 73 e aligned addres
7d20: 73 2e 20 20 67 65 74 32 62 79 74 65 61 28 29 20 s. get2bytea()
7d30: 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 is only used for
7d40: 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 0a 2a accessing the.*
7d50: 2a 20 63 65 6c 6c 20 61 64 64 72 65 73 73 65 73 * cell addresses
7d60: 20 69 6e 20 61 20 62 74 72 65 65 20 68 65 61 64 in a btree head
7d70: 65 72 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 er..*/.#if SQLIT
7d80: 45 5f 42 59 54 45 4f 52 44 45 52 3d 3d 34 33 32 E_BYTEORDER==432
7d90: 31 0a 23 20 64 65 66 69 6e 65 20 67 65 74 32 62 1.# define get2b
7da0: 79 74 65 41 6c 69 67 6e 65 64 28 78 29 20 20 28 yteAligned(x) (
7db0: 2a 28 75 31 36 2a 29 28 78 29 29 0a 23 65 6c 69 *(u16*)(x)).#eli
7dc0: 66 20 53 51 4c 49 54 45 5f 42 59 54 45 4f 52 44 f SQLITE_BYTEORD
7dd0: 45 52 3d 3d 31 32 33 34 20 26 26 20 47 43 43 5f ER==1234 && GCC_
7de0: 56 45 52 53 49 4f 4e 3e 3d 34 30 30 38 30 30 30 VERSION>=4008000
7df0: 0a 23 20 64 65 66 69 6e 65 20 67 65 74 32 62 79 .# define get2by
7e00: 74 65 41 6c 69 67 6e 65 64 28 78 29 20 20 5f 5f teAligned(x) __
7e10: 62 75 69 6c 74 69 6e 5f 62 73 77 61 70 31 36 28 builtin_bswap16(
7e20: 2a 28 75 31 36 2a 29 28 78 29 29 0a 23 65 6c 69 *(u16*)(x)).#eli
7e30: 66 20 53 51 4c 49 54 45 5f 42 59 54 45 4f 52 44 f SQLITE_BYTEORD
7e40: 45 52 3d 3d 31 32 33 34 20 26 26 20 4d 53 56 43 ER==1234 && MSVC
7e50: 5f 56 45 52 53 49 4f 4e 3e 3d 31 33 30 30 0a 23 _VERSION>=1300.#
7e60: 20 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 define get2byte
7e70: 41 6c 69 67 6e 65 64 28 78 29 20 20 5f 62 79 74 Aligned(x) _byt
7e80: 65 73 77 61 70 5f 75 73 68 6f 72 74 28 2a 28 75 eswap_ushort(*(u
7e90: 31 36 2a 29 28 78 29 29 0a 23 65 6c 73 65 0a 23 16*)(x)).#else.#
7ea0: 20 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 define get2byte
7eb0: 41 6c 69 67 6e 65 64 28 78 29 20 20 28 28 78 29 Aligned(x) ((x)
7ec0: 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 [0]<<8 | (x)[1])
7ed0: 0a 23 65 6e 64 69 66 0a .#endif.