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