extensions/sqlite/sqlite-source/btreeInt.h
author David Anderson <dvander@alliedmods.net>
Sun Oct 28 05:48:06 2007 +0000 (2007-10-28)
changeset 1645 27307df4d711
parent 1203 633c25ce6405
permissions -rw-r--r--
updated to sqlite-3.5.1 now that it's stable
used new shared cache functionality (yay)
dvander@1203
     1
/*
dvander@1203
     2
** 2004 April 6
dvander@1203
     3
**
dvander@1203
     4
** The author disclaims copyright to this source code.  In place of
dvander@1203
     5
** a legal notice, here is a blessing:
dvander@1203
     6
**
dvander@1203
     7
**    May you do good and not evil.
dvander@1203
     8
**    May you find forgiveness for yourself and forgive others.
dvander@1203
     9
**    May you share freely, never taking more than you give.
dvander@1203
    10
**
dvander@1203
    11
*************************************************************************
dvander@1203
    12
** $Id$
dvander@1203
    13
**
dvander@1203
    14
** This file implements a external (disk-based) database using BTrees.
dvander@1203
    15
** For a detailed discussion of BTrees, refer to
dvander@1203
    16
**
dvander@1203
    17
**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
dvander@1203
    18
**     "Sorting And Searching", pages 473-480. Addison-Wesley
dvander@1203
    19
**     Publishing Company, Reading, Massachusetts.
dvander@1203
    20
**
dvander@1203
    21
** The basic idea is that each page of the file contains N database
dvander@1203
    22
** entries and N+1 pointers to subpages.
dvander@1203
    23
**
dvander@1203
    24
**   ----------------------------------------------------------------
dvander@1203
    25
**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
dvander@1203
    26
**   ----------------------------------------------------------------
dvander@1203
    27
**
dvander@1203
    28
** All of the keys on the page that Ptr(0) points to have values less
dvander@1203
    29
** than Key(0).  All of the keys on page Ptr(1) and its subpages have
dvander@1203
    30
** values greater than Key(0) and less than Key(1).  All of the keys
dvander@1203
    31
** on Ptr(N) and its subpages have values greater than Key(N-1).  And
dvander@1203
    32
** so forth.
dvander@1203
    33
**
dvander@1203
    34
** Finding a particular key requires reading O(log(M)) pages from the 
dvander@1203
    35
** disk where M is the number of entries in the tree.
dvander@1203
    36
**
dvander@1203
    37
** In this implementation, a single file can hold one or more separate 
dvander@1203
    38
** BTrees.  Each BTree is identified by the index of its root page.  The
dvander@1203
    39
** key and data for any entry are combined to form the "payload".  A
dvander@1203
    40
** fixed amount of payload can be carried directly on the database
dvander@1203
    41
** page.  If the payload is larger than the preset amount then surplus
dvander@1203
    42
** bytes are stored on overflow pages.  The payload for an entry
dvander@1203
    43
** and the preceding pointer are combined to form a "Cell".  Each 
dvander@1203
    44
** page has a small header which contains the Ptr(N) pointer and other
dvander@1203
    45
** information such as the size of key and data.
dvander@1203
    46
**
dvander@1203
    47
** FORMAT DETAILS
dvander@1203
    48
**
dvander@1203
    49
** The file is divided into pages.  The first page is called page 1,
dvander@1203
    50
** the second is page 2, and so forth.  A page number of zero indicates
dvander@1203
    51
** "no such page".  The page size can be anything between 512 and 65536.
dvander@1203
    52
** Each page can be either a btree page, a freelist page or an overflow
dvander@1203
    53
** page.
dvander@1203
    54
**
dvander@1203
    55
** The first page is always a btree page.  The first 100 bytes of the first
dvander@1203
    56
** page contain a special header (the "file header") that describes the file.
dvander@1203
    57
** The format of the file header is as follows:
dvander@1203
    58
**
dvander@1203
    59
**   OFFSET   SIZE    DESCRIPTION
dvander@1203
    60
**      0      16     Header string: "SQLite format 3\000"
dvander@1203
    61
**     16       2     Page size in bytes.  
dvander@1203
    62
**     18       1     File format write version
dvander@1203
    63
**     19       1     File format read version
dvander@1203
    64
**     20       1     Bytes of unused space at the end of each page
dvander@1203
    65
**     21       1     Max embedded payload fraction
dvander@1203
    66
**     22       1     Min embedded payload fraction
dvander@1203
    67
**     23       1     Min leaf payload fraction
dvander@1203
    68
**     24       4     File change counter
dvander@1203
    69
**     28       4     Reserved for future use
dvander@1203
    70
**     32       4     First freelist page
dvander@1203
    71
**     36       4     Number of freelist pages in the file
dvander@1203
    72
**     40      60     15 4-byte meta values passed to higher layers
dvander@1203
    73
**
dvander@1203
    74
** All of the integer values are big-endian (most significant byte first).
dvander@1203
    75
**
dvander@1203
    76
** The file change counter is incremented when the database is changed
dvander@1203
    77
** This counter allows other processes to know when the file has changed
dvander@1203
    78
** and thus when they need to flush their cache.
dvander@1203
    79
**
dvander@1203
    80
** The max embedded payload fraction is the amount of the total usable
dvander@1203
    81
** space in a page that can be consumed by a single cell for standard
dvander@1203
    82
** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
dvander@1203
    83
** is to limit the maximum cell size so that at least 4 cells will fit
dvander@1203
    84
** on one page.  Thus the default max embedded payload fraction is 64.
dvander@1203
    85
**
dvander@1203
    86
** If the payload for a cell is larger than the max payload, then extra
dvander@1203
    87
** payload is spilled to overflow pages.  Once an overflow page is allocated,
dvander@1203
    88
** as many bytes as possible are moved into the overflow pages without letting
dvander@1203
    89
** the cell size drop below the min embedded payload fraction.
dvander@1203
    90
**
dvander@1203
    91
** The min leaf payload fraction is like the min embedded payload fraction
dvander@1203
    92
** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
dvander@1203
    93
** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
dvander@1203
    94
** not specified in the header.
dvander@1203
    95
**
dvander@1203
    96
** Each btree pages is divided into three sections:  The header, the
dvander@1203
    97
** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
dvander@1203
    98
** file header that occurs before the page header.
dvander@1203
    99
**
dvander@1203
   100
**      |----------------|
dvander@1203
   101
**      | file header    |   100 bytes.  Page 1 only.
dvander@1203
   102
**      |----------------|
dvander@1203
   103
**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
dvander@1203
   104
**      |----------------|
dvander@1203
   105
**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
dvander@1203
   106
**      | array          |   |  Grows downward
dvander@1203
   107
**      |                |   v
dvander@1203
   108
**      |----------------|
dvander@1203
   109
**      | unallocated    |
dvander@1203
   110
**      | space          |
dvander@1203
   111
**      |----------------|   ^  Grows upwards
dvander@1203
   112
**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
dvander@1203
   113
**      | area           |   |  and free space fragments.
dvander@1203
   114
**      |----------------|
dvander@1203
   115
**
dvander@1203
   116
** The page headers looks like this:
dvander@1203
   117
**
dvander@1203
   118
**   OFFSET   SIZE     DESCRIPTION
dvander@1203
   119
**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
dvander@1203
   120
**      1       2      byte offset to the first freeblock
dvander@1203
   121
**      3       2      number of cells on this page
dvander@1203
   122
**      5       2      first byte of the cell content area
dvander@1203
   123
**      7       1      number of fragmented free bytes
dvander@1203
   124
**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
dvander@1203
   125
**
dvander@1203
   126
** The flags define the format of this btree page.  The leaf flag means that
dvander@1203
   127
** this page has no children.  The zerodata flag means that this page carries
dvander@1203
   128
** only keys and no data.  The intkey flag means that the key is a integer
dvander@1203
   129
** which is stored in the key size entry of the cell header rather than in
dvander@1203
   130
** the payload area.
dvander@1203
   131
**
dvander@1203
   132
** The cell pointer array begins on the first byte after the page header.
dvander@1203
   133
** The cell pointer array contains zero or more 2-byte numbers which are
dvander@1203
   134
** offsets from the beginning of the page to the cell content in the cell
dvander@1203
   135
** content area.  The cell pointers occur in sorted order.  The system strives
dvander@1203
   136
** to keep free space after the last cell pointer so that new cells can
dvander@1203
   137
** be easily added without having to defragment the page.
dvander@1203
   138
**
dvander@1203
   139
** Cell content is stored at the very end of the page and grows toward the
dvander@1203
   140
** beginning of the page.
dvander@1203
   141
**
dvander@1203
   142
** Unused space within the cell content area is collected into a linked list of
dvander@1203
   143
** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
dvander@1203
   144
** to the first freeblock is given in the header.  Freeblocks occur in
dvander@1203
   145
** increasing order.  Because a freeblock must be at least 4 bytes in size,
dvander@1203
   146
** any group of 3 or fewer unused bytes in the cell content area cannot
dvander@1203
   147
** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
dvander@1203
   148
** a fragment.  The total number of bytes in all fragments is recorded.
dvander@1203
   149
** in the page header at offset 7.
dvander@1203
   150
**
dvander@1203
   151
**    SIZE    DESCRIPTION
dvander@1203
   152
**      2     Byte offset of the next freeblock
dvander@1203
   153
**      2     Bytes in this freeblock
dvander@1203
   154
**
dvander@1203
   155
** Cells are of variable length.  Cells are stored in the cell content area at
dvander@1203
   156
** the end of the page.  Pointers to the cells are in the cell pointer array
dvander@1203
   157
** that immediately follows the page header.  Cells is not necessarily
dvander@1203
   158
** contiguous or in order, but cell pointers are contiguous and in order.
dvander@1203
   159
**
dvander@1203
   160
** Cell content makes use of variable length integers.  A variable
dvander@1203
   161
** length integer is 1 to 9 bytes where the lower 7 bits of each 
dvander@1203
   162
** byte are used.  The integer consists of all bytes that have bit 8 set and
dvander@1203
   163
** the first byte with bit 8 clear.  The most significant byte of the integer
dvander@1203
   164
** appears first.  A variable-length integer may not be more than 9 bytes long.
dvander@1203
   165
** As a special case, all 8 bytes of the 9th byte are used as data.  This
dvander@1203
   166
** allows a 64-bit integer to be encoded in 9 bytes.
dvander@1203
   167
**
dvander@1203
   168
**    0x00                      becomes  0x00000000
dvander@1203
   169
**    0x7f                      becomes  0x0000007f
dvander@1203
   170
**    0x81 0x00                 becomes  0x00000080
dvander@1203
   171
**    0x82 0x00                 becomes  0x00000100
dvander@1203
   172
**    0x80 0x7f                 becomes  0x0000007f
dvander@1203
   173
**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
dvander@1203
   174
**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
dvander@1203
   175
**
dvander@1203
   176
** Variable length integers are used for rowids and to hold the number of
dvander@1203
   177
** bytes of key and data in a btree cell.
dvander@1203
   178
**
dvander@1203
   179
** The content of a cell looks like this:
dvander@1203
   180
**
dvander@1203
   181
**    SIZE    DESCRIPTION
dvander@1203
   182
**      4     Page number of the left child. Omitted if leaf flag is set.
dvander@1203
   183
**     var    Number of bytes of data. Omitted if the zerodata flag is set.
dvander@1203
   184
**     var    Number of bytes of key. Or the key itself if intkey flag is set.
dvander@1203
   185
**      *     Payload
dvander@1203
   186
**      4     First page of the overflow chain.  Omitted if no overflow
dvander@1203
   187
**
dvander@1203
   188
** Overflow pages form a linked list.  Each page except the last is completely
dvander@1203
   189
** filled with data (pagesize - 4 bytes).  The last page can have as little
dvander@1203
   190
** as 1 byte of data.
dvander@1203
   191
**
dvander@1203
   192
**    SIZE    DESCRIPTION
dvander@1203
   193
**      4     Page number of next overflow page
dvander@1203
   194
**      *     Data
dvander@1203
   195
**
dvander@1203
   196
** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
dvander@1203
   197
** file header points to the first in a linked list of trunk page.  Each trunk
dvander@1203
   198
** page points to multiple leaf pages.  The content of a leaf page is
dvander@1203
   199
** unspecified.  A trunk page looks like this:
dvander@1203
   200
**
dvander@1203
   201
**    SIZE    DESCRIPTION
dvander@1203
   202
**      4     Page number of next trunk page
dvander@1203
   203
**      4     Number of leaf pointers on this page
dvander@1203
   204
**      *     zero or more pages numbers of leaves
dvander@1203
   205
*/
dvander@1203
   206
#include "sqliteInt.h"
dvander@1203
   207
#include "pager.h"
dvander@1203
   208
#include "btree.h"
dvander@1203
   209
#include "os.h"
dvander@1203
   210
#include <assert.h>
dvander@1203
   211
dvander@1203
   212
/* Round up a number to the next larger multiple of 8.  This is used
dvander@1203
   213
** to force 8-byte alignment on 64-bit architectures.
dvander@1203
   214
*/
dvander@1203
   215
#define ROUND8(x)   ((x+7)&~7)
dvander@1203
   216
dvander@1203
   217
dvander@1203
   218
/* The following value is the maximum cell size assuming a maximum page
dvander@1203
   219
** size give above.
dvander@1203
   220
*/
dvander@1203
   221
#define MX_CELL_SIZE(pBt)  (pBt->pageSize-8)
dvander@1203
   222
dvander@1203
   223
/* The maximum number of cells on a single page of the database.  This
dvander@1203
   224
** assumes a minimum cell size of 3 bytes.  Such small cells will be
dvander@1203
   225
** exceedingly rare, but they are possible.
dvander@1203
   226
*/
dvander@1203
   227
#define MX_CELL(pBt) ((pBt->pageSize-8)/3)
dvander@1203
   228
dvander@1203
   229
/* Forward declarations */
dvander@1203
   230
typedef struct MemPage MemPage;
dvander@1203
   231
typedef struct BtLock BtLock;
dvander@1203
   232
dvander@1203
   233
/*
dvander@1203
   234
** This is a magic string that appears at the beginning of every
dvander@1203
   235
** SQLite database in order to identify the file as a real database.
dvander@1203
   236
**
dvander@1203
   237
** You can change this value at compile-time by specifying a
dvander@1203
   238
** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
dvander@1203
   239
** header must be exactly 16 bytes including the zero-terminator so
dvander@1203
   240
** the string itself should be 15 characters long.  If you change
dvander@1203
   241
** the header, then your custom library will not be able to read 
dvander@1203
   242
** databases generated by the standard tools and the standard tools
dvander@1203
   243
** will not be able to read databases created by your custom library.
dvander@1203
   244
*/
dvander@1203
   245
#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
dvander@1203
   246
#  define SQLITE_FILE_HEADER "SQLite format 3"
dvander@1203
   247
#endif
dvander@1203
   248
dvander@1203
   249
/*
dvander@1203
   250
** Page type flags.  An ORed combination of these flags appear as the
dvander@1645
   251
** first byte of on-disk image of every BTree page.
dvander@1203
   252
*/
dvander@1203
   253
#define PTF_INTKEY    0x01
dvander@1203
   254
#define PTF_ZERODATA  0x02
dvander@1203
   255
#define PTF_LEAFDATA  0x04
dvander@1203
   256
#define PTF_LEAF      0x08
dvander@1203
   257
dvander@1203
   258
/*
dvander@1203
   259
** As each page of the file is loaded into memory, an instance of the following
dvander@1203
   260
** structure is appended and initialized to zero.  This structure stores
dvander@1203
   261
** information about the page that is decoded from the raw file page.
dvander@1203
   262
**
dvander@1203
   263
** The pParent field points back to the parent page.  This allows us to
dvander@1203
   264
** walk up the BTree from any leaf to the root.  Care must be taken to
dvander@1203
   265
** unref() the parent page pointer when this page is no longer referenced.
dvander@1203
   266
** The pageDestructor() routine handles that chore.
dvander@1645
   267
**
dvander@1645
   268
** Access to all fields of this structure is controlled by the mutex
dvander@1645
   269
** stored in MemPage.pBt->mutex.
dvander@1203
   270
*/
dvander@1203
   271
struct MemPage {
dvander@1203
   272
  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
dvander@1203
   273
  u8 idxShift;         /* True if Cell indices have changed */
dvander@1203
   274
  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
dvander@1203
   275
  u8 intKey;           /* True if intkey flag is set */
dvander@1203
   276
  u8 leaf;             /* True if leaf flag is set */
dvander@1203
   277
  u8 zeroData;         /* True if table stores keys only */
dvander@1203
   278
  u8 leafData;         /* True if tables stores data on leaves only */
dvander@1203
   279
  u8 hasData;          /* True if this page stores data */
dvander@1203
   280
  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
dvander@1203
   281
  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
dvander@1645
   282
  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
dvander@1645
   283
  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
dvander@1203
   284
  u16 cellOffset;      /* Index in aData of first cell pointer */
dvander@1203
   285
  u16 idxParent;       /* Index in parent of this node */
dvander@1203
   286
  u16 nFree;           /* Number of free bytes on the page */
dvander@1203
   287
  u16 nCell;           /* Number of cells on this page, local and ovfl */
dvander@1203
   288
  struct _OvflCell {   /* Cells that will not fit on aData[] */
dvander@1203
   289
    u8 *pCell;          /* Pointers to the body of the overflow cell */
dvander@1203
   290
    u16 idx;            /* Insert this cell before idx-th non-overflow cell */
dvander@1203
   291
  } aOvfl[5];
dvander@1645
   292
  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
dvander@1645
   293
  u8 *aData;           /* Pointer to disk image of the page data */
dvander@1203
   294
  DbPage *pDbPage;     /* Pager page handle */
dvander@1203
   295
  Pgno pgno;           /* Page number for this page */
dvander@1203
   296
  MemPage *pParent;    /* The parent of this page.  NULL for root */
dvander@1203
   297
};
dvander@1203
   298
dvander@1203
   299
/*
dvander@1203
   300
** The in-memory image of a disk page has the auxiliary information appended
dvander@1203
   301
** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
dvander@1203
   302
** that extra information.
dvander@1203
   303
*/
dvander@1203
   304
#define EXTRA_SIZE sizeof(MemPage)
dvander@1203
   305
dvander@1645
   306
/* A Btree handle
dvander@1645
   307
**
dvander@1645
   308
** A database connection contains a pointer to an instance of
dvander@1645
   309
** this object for every database file that it has open.  This structure
dvander@1645
   310
** is opaque to the database connection.  The database connection cannot
dvander@1645
   311
** see the internals of this structure and only deals with pointers to
dvander@1645
   312
** this structure.
dvander@1645
   313
**
dvander@1645
   314
** For some database files, the same underlying database cache might be 
dvander@1645
   315
** shared between multiple connections.  In that case, each contection
dvander@1645
   316
** has it own pointer to this object.  But each instance of this object
dvander@1645
   317
** points to the same BtShared object.  The database cache and the
dvander@1645
   318
** schema associated with the database file are all contained within
dvander@1645
   319
** the BtShared object.
dvander@1645
   320
**
dvander@1645
   321
** All fields in this structure are accessed under sqlite3.mutex.
dvander@1645
   322
** The pBt pointer itself may not be changed while there exists cursors 
dvander@1645
   323
** in the referenced BtShared that point back to this Btree since those
dvander@1645
   324
** cursors have to do go through this Btree to find their BtShared and
dvander@1645
   325
** they often do so without holding sqlite3.mutex.
dvander@1645
   326
*/
dvander@1203
   327
struct Btree {
dvander@1645
   328
  sqlite3 *pSqlite;  /* The database connection holding this btree */
dvander@1645
   329
  BtShared *pBt;     /* Sharable content of this btree */
dvander@1645
   330
  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
dvander@1645
   331
  u8 sharable;       /* True if we can share pBt with other pSqlite */
dvander@1645
   332
  u8 locked;         /* True if pSqlite currently has pBt locked */
dvander@1645
   333
  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
dvander@1645
   334
  Btree *pNext;      /* List of other sharable Btrees from the same pSqlite */
dvander@1645
   335
  Btree *pPrev;      /* Back pointer of the same list */
dvander@1203
   336
};
dvander@1203
   337
dvander@1203
   338
/*
dvander@1203
   339
** Btree.inTrans may take one of the following values.
dvander@1203
   340
**
dvander@1203
   341
** If the shared-data extension is enabled, there may be multiple users
dvander@1203
   342
** of the Btree structure. At most one of these may open a write transaction,
dvander@1645
   343
** but any number may have active read transactions.
dvander@1203
   344
*/
dvander@1203
   345
#define TRANS_NONE  0
dvander@1203
   346
#define TRANS_READ  1
dvander@1203
   347
#define TRANS_WRITE 2
dvander@1203
   348
dvander@1203
   349
/*
dvander@1645
   350
** An instance of this object represents a single database file.
dvander@1645
   351
** 
dvander@1645
   352
** A single database file can be in use as the same time by two
dvander@1645
   353
** or more database connections.  When two or more connections are
dvander@1645
   354
** sharing the same database file, each connection has it own
dvander@1645
   355
** private Btree object for the file and each of those Btrees points
dvander@1645
   356
** to this one BtShared object.  BtShared.nRef is the number of
dvander@1645
   357
** connections currently sharing this database file.
dvander@1645
   358
**
dvander@1645
   359
** Fields in this structure are accessed under the BtShared.mutex
dvander@1645
   360
** mutex, except for nRef and pNext which are accessed under the
dvander@1645
   361
** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
dvander@1645
   362
** may not be modified once it is initially set as long as nRef>0.
dvander@1645
   363
** The pSchema field may be set once under BtShared.mutex and
dvander@1645
   364
** thereafter is unchanged as long as nRef>0.
dvander@1203
   365
*/
dvander@1203
   366
struct BtShared {
dvander@1203
   367
  Pager *pPager;        /* The page cache */
dvander@1203
   368
  BtCursor *pCursor;    /* A list of all open cursors */
dvander@1203
   369
  MemPage *pPage1;      /* First page of the database */
dvander@1203
   370
  u8 inStmt;            /* True if we are in a statement subtransaction */
dvander@1203
   371
  u8 readOnly;          /* True if the underlying file is readonly */
dvander@1203
   372
  u8 maxEmbedFrac;      /* Maximum payload as % of total page size */
dvander@1203
   373
  u8 minEmbedFrac;      /* Minimum payload as % of total page size */
dvander@1203
   374
  u8 minLeafFrac;       /* Minimum leaf payload as % of total page size */
dvander@1203
   375
  u8 pageSizeFixed;     /* True if the page size can no longer be changed */
dvander@1203
   376
#ifndef SQLITE_OMIT_AUTOVACUUM
dvander@1203
   377
  u8 autoVacuum;        /* True if auto-vacuum is enabled */
dvander@1203
   378
  u8 incrVacuum;        /* True if incr-vacuum is enabled */
dvander@1203
   379
  Pgno nTrunc;          /* Non-zero if the db will be truncated (incr vacuum) */
dvander@1203
   380
#endif
dvander@1203
   381
  u16 pageSize;         /* Total number of bytes on a page */
dvander@1203
   382
  u16 usableSize;       /* Number of usable bytes on each page */
dvander@1203
   383
  int maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
dvander@1203
   384
  int minLocal;         /* Minimum local payload in non-LEAFDATA tables */
dvander@1203
   385
  int maxLeaf;          /* Maximum local payload in a LEAFDATA table */
dvander@1203
   386
  int minLeaf;          /* Minimum local payload in a LEAFDATA table */
dvander@1203
   387
  BusyHandler *pBusyHandler;   /* Callback for when there is lock contention */
dvander@1203
   388
  u8 inTransaction;     /* Transaction state */
dvander@1203
   389
  int nTransaction;     /* Number of open transactions (read + write) */
dvander@1203
   390
  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
dvander@1203
   391
  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
dvander@1645
   392
  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
dvander@1203
   393
#ifndef SQLITE_OMIT_SHARED_CACHE
dvander@1645
   394
  int nRef;             /* Number of references to this structure */
dvander@1645
   395
  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
dvander@1203
   396
  BtLock *pLock;        /* List of locks held on this shared-btree struct */
dvander@1203
   397
#endif
dvander@1203
   398
};
dvander@1203
   399
dvander@1203
   400
/*
dvander@1203
   401
** An instance of the following structure is used to hold information
dvander@1203
   402
** about a cell.  The parseCellPtr() function fills in this structure
dvander@1203
   403
** based on information extract from the raw disk page.
dvander@1203
   404
*/
dvander@1203
   405
typedef struct CellInfo CellInfo;
dvander@1203
   406
struct CellInfo {
dvander@1203
   407
  u8 *pCell;     /* Pointer to the start of cell content */
dvander@1203
   408
  i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
dvander@1203
   409
  u32 nData;     /* Number of bytes of data */
dvander@1203
   410
  u32 nPayload;  /* Total amount of payload */
dvander@1203
   411
  u16 nHeader;   /* Size of the cell content header in bytes */
dvander@1203
   412
  u16 nLocal;    /* Amount of payload held locally */
dvander@1203
   413
  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
dvander@1203
   414
  u16 nSize;     /* Size of the cell content on the main b-tree page */
dvander@1203
   415
};
dvander@1203
   416
dvander@1203
   417
/*
dvander@1645
   418
** A cursor is a pointer to a particular entry within a particular
dvander@1645
   419
** b-tree within a database file.
dvander@1645
   420
**
dvander@1203
   421
** The entry is identified by its MemPage and the index in
dvander@1203
   422
** MemPage.aCell[] of the entry.
dvander@1645
   423
**
dvander@1645
   424
** When a single database file can shared by two more database connections,
dvander@1645
   425
** but cursors cannot be shared.  Each cursor is associated with a
dvander@1645
   426
** particular database connection identified BtCursor.pBtree.pSqlite.
dvander@1645
   427
**
dvander@1645
   428
** Fields in this structure are accessed under the BtShared.mutex
dvander@1645
   429
** found at self->pBt->mutex. 
dvander@1203
   430
*/
dvander@1203
   431
struct BtCursor {
dvander@1203
   432
  Btree *pBtree;            /* The Btree to which this cursor belongs */
dvander@1645
   433
  BtShared *pBt;            /* The BtShared this cursor points to */
dvander@1203
   434
  BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
dvander@1203
   435
  int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
dvander@1203
   436
  void *pArg;               /* First arg to xCompare() */
dvander@1203
   437
  Pgno pgnoRoot;            /* The root page of this tree */
dvander@1203
   438
  MemPage *pPage;           /* Page that contains the entry */
dvander@1203
   439
  int idx;                  /* Index of the entry in pPage->aCell[] */
dvander@1203
   440
  CellInfo info;            /* A parse of the cell we are pointing at */
dvander@1203
   441
  u8 wrFlag;                /* True if writable */
dvander@1203
   442
  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
dvander@1203
   443
  void *pKey;      /* Saved key that was cursor's last known position */
dvander@1203
   444
  i64 nKey;        /* Size of pKey, or last integer key */
dvander@1203
   445
  int skip;        /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
dvander@1203
   446
#ifndef SQLITE_OMIT_INCRBLOB
dvander@1203
   447
  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
dvander@1203
   448
  Pgno *aOverflow;          /* Cache of overflow page locations */
dvander@1203
   449
#endif
dvander@1203
   450
};
dvander@1203
   451
dvander@1203
   452
/*
dvander@1203
   453
** Potential values for BtCursor.eState.
dvander@1203
   454
**
dvander@1203
   455
** CURSOR_VALID:
dvander@1203
   456
**   Cursor points to a valid entry. getPayload() etc. may be called.
dvander@1203
   457
**
dvander@1203
   458
** CURSOR_INVALID:
dvander@1203
   459
**   Cursor does not point to a valid entry. This can happen (for example) 
dvander@1203
   460
**   because the table is empty or because BtreeCursorFirst() has not been
dvander@1203
   461
**   called.
dvander@1203
   462
**
dvander@1203
   463
** CURSOR_REQUIRESEEK:
dvander@1203
   464
**   The table that this cursor was opened on still exists, but has been 
dvander@1203
   465
**   modified since the cursor was last used. The cursor position is saved
dvander@1203
   466
**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
dvander@1203
   467
**   this state, restoreOrClearCursorPosition() can be called to attempt to
dvander@1203
   468
**   seek the cursor to the saved position.
dvander@1645
   469
**
dvander@1645
   470
** CURSOR_FAULT:
dvander@1645
   471
**   A unrecoverable error (an I/O error or a malloc failure) has occurred
dvander@1645
   472
**   on a different connection that shares the BtShared cache with this
dvander@1645
   473
**   cursor.  The error has left the cache in an inconsistent state.
dvander@1645
   474
**   Do nothing else with this cursor.  Any attempt to use the cursor
dvander@1645
   475
**   should return the error code stored in BtCursor.skip
dvander@1203
   476
*/
dvander@1203
   477
#define CURSOR_INVALID           0
dvander@1203
   478
#define CURSOR_VALID             1
dvander@1203
   479
#define CURSOR_REQUIRESEEK       2
dvander@1645
   480
#define CURSOR_FAULT             3
dvander@1203
   481
dvander@1203
   482
/*
dvander@1203
   483
** The TRACE macro will print high-level status information about the
dvander@1203
   484
** btree operation when the global variable sqlite3_btree_trace is
dvander@1203
   485
** enabled.
dvander@1203
   486
*/
dvander@1203
   487
#if SQLITE_TEST
dvander@1203
   488
# define TRACE(X)   if( sqlite3_btree_trace ){ printf X; fflush(stdout); }
dvander@1203
   489
#else
dvander@1203
   490
# define TRACE(X)
dvander@1203
   491
#endif
dvander@1203
   492
dvander@1203
   493
/*
dvander@1203
   494
** Routines to read and write variable-length integers.  These used to
dvander@1203
   495
** be defined locally, but now we use the varint routines in the util.c
dvander@1203
   496
** file.
dvander@1203
   497
*/
dvander@1203
   498
#define getVarint    sqlite3GetVarint
dvander@1203
   499
#define getVarint32(A,B)  ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
dvander@1203
   500
#define putVarint    sqlite3PutVarint
dvander@1203
   501
dvander@1203
   502
/* The database page the PENDING_BYTE occupies. This page is never used.
dvander@1203
   503
** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
dvander@1203
   504
** should possibly be consolidated (presumably in pager.h).
dvander@1203
   505
**
dvander@1203
   506
** If disk I/O is omitted (meaning that the database is stored purely
dvander@1203
   507
** in memory) then there is no pending byte.
dvander@1203
   508
*/
dvander@1203
   509
#ifdef SQLITE_OMIT_DISKIO
dvander@1203
   510
# define PENDING_BYTE_PAGE(pBt)  0x7fffffff
dvander@1203
   511
#else
dvander@1203
   512
# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
dvander@1203
   513
#endif
dvander@1203
   514
dvander@1203
   515
/*
dvander@1203
   516
** A linked list of the following structures is stored at BtShared.pLock.
dvander@1203
   517
** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
dvander@1203
   518
** is opened on the table with root page BtShared.iTable. Locks are removed
dvander@1203
   519
** from this list when a transaction is committed or rolled back, or when
dvander@1203
   520
** a btree handle is closed.
dvander@1203
   521
*/
dvander@1203
   522
struct BtLock {
dvander@1203
   523
  Btree *pBtree;        /* Btree handle holding this lock */
dvander@1203
   524
  Pgno iTable;          /* Root page of table */
dvander@1203
   525
  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
dvander@1203
   526
  BtLock *pNext;        /* Next in BtShared.pLock list */
dvander@1203
   527
};
dvander@1203
   528
dvander@1203
   529
/* Candidate values for BtLock.eLock */
dvander@1203
   530
#define READ_LOCK     1
dvander@1203
   531
#define WRITE_LOCK    2
dvander@1203
   532
dvander@1203
   533
/*
dvander@1203
   534
** These macros define the location of the pointer-map entry for a 
dvander@1203
   535
** database page. The first argument to each is the number of usable
dvander@1203
   536
** bytes on each page of the database (often 1024). The second is the
dvander@1203
   537
** page number to look up in the pointer map.
dvander@1203
   538
**
dvander@1203
   539
** PTRMAP_PAGENO returns the database page number of the pointer-map
dvander@1203
   540
** page that stores the required pointer. PTRMAP_PTROFFSET returns
dvander@1203
   541
** the offset of the requested map entry.
dvander@1203
   542
**
dvander@1203
   543
** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
dvander@1203
   544
** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
dvander@1203
   545
** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
dvander@1203
   546
** this test.
dvander@1203
   547
*/
dvander@1203
   548
#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
dvander@1203
   549
#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
dvander@1203
   550
#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
dvander@1203
   551
dvander@1203
   552
/*
dvander@1203
   553
** The pointer map is a lookup table that identifies the parent page for
dvander@1203
   554
** each child page in the database file.  The parent page is the page that
dvander@1203
   555
** contains a pointer to the child.  Every page in the database contains
dvander@1203
   556
** 0 or 1 parent pages.  (In this context 'database page' refers
dvander@1203
   557
** to any page that is not part of the pointer map itself.)  Each pointer map
dvander@1203
   558
** entry consists of a single byte 'type' and a 4 byte parent page number.
dvander@1203
   559
** The PTRMAP_XXX identifiers below are the valid types.
dvander@1203
   560
**
dvander@1203
   561
** The purpose of the pointer map is to facility moving pages from one
dvander@1203
   562
** position in the file to another as part of autovacuum.  When a page
dvander@1203
   563
** is moved, the pointer in its parent must be updated to point to the
dvander@1203
   564
** new location.  The pointer map is used to locate the parent page quickly.
dvander@1203
   565
**
dvander@1203
   566
** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
dvander@1203
   567
**                  used in this case.
dvander@1203
   568
**
dvander@1203
   569
** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
dvander@1203
   570
**                  is not used in this case.
dvander@1203
   571
**
dvander@1203
   572
** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
dvander@1203
   573
**                   overflow pages. The page number identifies the page that
dvander@1203
   574
**                   contains the cell with a pointer to this overflow page.
dvander@1203
   575
**
dvander@1203
   576
** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
dvander@1203
   577
**                   overflow pages. The page-number identifies the previous
dvander@1203
   578
**                   page in the overflow page list.
dvander@1203
   579
**
dvander@1203
   580
** PTRMAP_BTREE: The database page is a non-root btree page. The page number
dvander@1203
   581
**               identifies the parent page in the btree.
dvander@1203
   582
*/
dvander@1203
   583
#define PTRMAP_ROOTPAGE 1
dvander@1203
   584
#define PTRMAP_FREEPAGE 2
dvander@1203
   585
#define PTRMAP_OVERFLOW1 3
dvander@1203
   586
#define PTRMAP_OVERFLOW2 4
dvander@1203
   587
#define PTRMAP_BTREE 5
dvander@1203
   588
dvander@1203
   589
/* A bunch of assert() statements to check the transaction state variables
dvander@1203
   590
** of handle p (type Btree*) are internally consistent.
dvander@1203
   591
*/
dvander@1203
   592
#define btreeIntegrity(p) \
dvander@1203
   593
  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
dvander@1203
   594
  assert( p->pBt->inTransaction>=p->inTrans ); 
dvander@1203
   595
dvander@1203
   596
dvander@1203
   597
/*
dvander@1203
   598
** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
dvander@1203
   599
** if the database supports auto-vacuum or not. Because it is used
dvander@1203
   600
** within an expression that is an argument to another macro 
dvander@1203
   601
** (sqliteMallocRaw), it is not possible to use conditional compilation.
dvander@1203
   602
** So, this macro is defined instead.
dvander@1203
   603
*/
dvander@1203
   604
#ifndef SQLITE_OMIT_AUTOVACUUM
dvander@1203
   605
#define ISAUTOVACUUM (pBt->autoVacuum)
dvander@1203
   606
#else
dvander@1203
   607
#define ISAUTOVACUUM 0
dvander@1203
   608
#endif
dvander@1203
   609
dvander@1203
   610
dvander@1203
   611
/*
dvander@1203
   612
** This structure is passed around through all the sanity checking routines
dvander@1203
   613
** in order to keep track of some global state information.
dvander@1203
   614
*/
dvander@1203
   615
typedef struct IntegrityCk IntegrityCk;
dvander@1203
   616
struct IntegrityCk {
dvander@1203
   617
  BtShared *pBt;    /* The tree being checked out */
dvander@1203
   618
  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
dvander@1203
   619
  int nPage;        /* Number of pages in the database */
dvander@1203
   620
  int *anRef;       /* Number of times each page is referenced */
dvander@1203
   621
  int mxErr;        /* Stop accumulating errors when this reaches zero */
dvander@1203
   622
  char *zErrMsg;    /* An error message.  NULL if no errors seen. */
dvander@1203
   623
  int nErr;         /* Number of messages written to zErrMsg so far */
dvander@1203
   624
};
dvander@1203
   625
dvander@1203
   626
/*
dvander@1203
   627
** Read or write a two- and four-byte big-endian integer values.
dvander@1203
   628
*/
dvander@1203
   629
#define get2byte(x)   ((x)[0]<<8 | (x)[1])
dvander@1203
   630
#define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v))
dvander@1203
   631
#define get4byte sqlite3Get4byte
dvander@1203
   632
#define put4byte sqlite3Put4byte
dvander@1203
   633
dvander@1203
   634
/*
dvander@1203
   635
** Internal routines that should be accessed by the btree layer only.
dvander@1203
   636
*/
dvander@1203
   637
int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
dvander@1203
   638
int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent);
dvander@1203
   639
void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
dvander@1203
   640
void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
dvander@1645
   641
#ifdef SQLITE_TEST
dvander@1203
   642
u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell);
dvander@1645
   643
#endif
dvander@1203
   644
int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur);
dvander@1203
   645
void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
dvander@1203
   646
void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
dvander@1203
   647
int sqlite3BtreeIsRootPage(MemPage *pPage);
dvander@1203
   648
void sqlite3BtreeMoveToParent(BtCursor *pCur);