Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: third_party/sqlite/src/src/btreeInt.h

Issue 1610963002: Import SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/sqlite/src/src/btree.c ('k') | third_party/sqlite/src/src/build.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2004 April 6 2 ** 2004 April 6
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 /* The maximum number of cells on a single page of the database. This 224 /* The maximum number of cells on a single page of the database. This
225 ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself 225 ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
226 ** plus 2 bytes for the index to the cell in the page header). Such 226 ** plus 2 bytes for the index to the cell in the page header). Such
227 ** small cells will be rare, but they are possible. 227 ** small cells will be rare, but they are possible.
228 */ 228 */
229 #define MX_CELL(pBt) ((pBt->pageSize-8)/6) 229 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
230 230
231 /* Forward declarations */ 231 /* Forward declarations */
232 typedef struct MemPage MemPage; 232 typedef struct MemPage MemPage;
233 typedef struct BtLock BtLock; 233 typedef struct BtLock BtLock;
234 typedef struct CellInfo CellInfo;
234 235
235 /* 236 /*
236 ** This is a magic string that appears at the beginning of every 237 ** This is a magic string that appears at the beginning of every
237 ** SQLite database in order to identify the file as a real database. 238 ** SQLite database in order to identify the file as a real database.
238 ** 239 **
239 ** You can change this value at compile-time by specifying a 240 ** You can change this value at compile-time by specifying a
240 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The 241 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
241 ** header must be exactly 16 bytes including the zero-terminator so 242 ** header must be exactly 16 bytes including the zero-terminator so
242 ** the string itself should be 15 characters long. If you change 243 ** the string itself should be 15 characters long. If you change
243 ** the header, then your custom library will not be able to read 244 ** the header, then your custom library will not be able to read
(...skipping 29 matching lines...) Expand all
273 struct MemPage { 274 struct MemPage {
274 u8 isInit; /* True if previously initialized. MUST BE FIRST! */ 275 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
275 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ 276 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
276 u8 intKey; /* True if table b-trees. False for index b-trees */ 277 u8 intKey; /* True if table b-trees. False for index b-trees */
277 u8 intKeyLeaf; /* True if the leaf of an intKey table */ 278 u8 intKeyLeaf; /* True if the leaf of an intKey table */
278 u8 noPayload; /* True if internal intKey page (thus w/o data) */ 279 u8 noPayload; /* True if internal intKey page (thus w/o data) */
279 u8 leaf; /* True if a leaf page */ 280 u8 leaf; /* True if a leaf page */
280 u8 hdrOffset; /* 100 for page 1. 0 otherwise */ 281 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
281 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ 282 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
282 u8 max1bytePayload; /* min(maxLocal,127) */ 283 u8 max1bytePayload; /* min(maxLocal,127) */
284 u8 bBusy; /* Prevent endless loops on corrupt database files */
283 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ 285 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
284 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ 286 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
285 u16 cellOffset; /* Index in aData of first cell pointer */ 287 u16 cellOffset; /* Index in aData of first cell pointer */
286 u16 nFree; /* Number of free bytes on the page */ 288 u16 nFree; /* Number of free bytes on the page */
287 u16 nCell; /* Number of cells on this page, local and ovfl */ 289 u16 nCell; /* Number of cells on this page, local and ovfl */
288 u16 maskPage; /* Mask for page offset */ 290 u16 maskPage; /* Mask for page offset */
289 u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th 291 u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th
290 ** non-overflow cell */ 292 ** non-overflow cell */
291 u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ 293 u8 *apOvfl[5]; /* Pointers to the body of overflow cells */
292 BtShared *pBt; /* Pointer to BtShared that this page is part of */ 294 BtShared *pBt; /* Pointer to BtShared that this page is part of */
293 u8 *aData; /* Pointer to disk image of the page data */ 295 u8 *aData; /* Pointer to disk image of the page data */
294 u8 *aDataEnd; /* One byte past the end of usable data */ 296 u8 *aDataEnd; /* One byte past the end of usable data */
295 u8 *aCellIdx; /* The cell index area */ 297 u8 *aCellIdx; /* The cell index area */
298 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
296 DbPage *pDbPage; /* Pager page handle */ 299 DbPage *pDbPage; /* Pager page handle */
300 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
301 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
297 Pgno pgno; /* Page number for this page */ 302 Pgno pgno; /* Page number for this page */
298 }; 303 };
299 304
300 /* 305 /*
301 ** The in-memory image of a disk page has the auxiliary information appended 306 ** The in-memory image of a disk page has the auxiliary information appended
302 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold 307 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
303 ** that extra information. 308 ** that extra information.
304 */ 309 */
305 #define EXTRA_SIZE sizeof(MemPage) 310 #define EXTRA_SIZE sizeof(MemPage)
306 311
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 ** in the referenced BtShared that point back to this Btree since those 347 ** in the referenced BtShared that point back to this Btree since those
343 ** cursors have to go through this Btree to find their BtShared and 348 ** cursors have to go through this Btree to find their BtShared and
344 ** they often do so without holding sqlite3.mutex. 349 ** they often do so without holding sqlite3.mutex.
345 */ 350 */
346 struct Btree { 351 struct Btree {
347 sqlite3 *db; /* The database connection holding this btree */ 352 sqlite3 *db; /* The database connection holding this btree */
348 BtShared *pBt; /* Sharable content of this btree */ 353 BtShared *pBt; /* Sharable content of this btree */
349 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ 354 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
350 u8 sharable; /* True if we can share pBt with another db */ 355 u8 sharable; /* True if we can share pBt with another db */
351 u8 locked; /* True if db currently has pBt locked */ 356 u8 locked; /* True if db currently has pBt locked */
357 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
352 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ 358 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
353 int nBackup; /* Number of backup operations reading this btree */ 359 int nBackup; /* Number of backup operations reading this btree */
360 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
354 Btree *pNext; /* List of other sharable Btrees from the same db */ 361 Btree *pNext; /* List of other sharable Btrees from the same db */
355 Btree *pPrev; /* Back pointer of the same list */ 362 Btree *pPrev; /* Back pointer of the same list */
356 #ifndef SQLITE_OMIT_SHARED_CACHE 363 #ifndef SQLITE_OMIT_SHARED_CACHE
357 BtLock lock; /* Object used to lock page 1 */ 364 BtLock lock; /* Object used to lock page 1 */
358 #endif 365 #endif
359 }; 366 };
360 367
361 /* 368 /*
362 ** Btree.inTrans may take one of the following values. 369 ** Btree.inTrans may take one of the following values.
363 ** 370 **
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 BtCursor *pCursor; /* A list of all open cursors */ 417 BtCursor *pCursor; /* A list of all open cursors */
411 MemPage *pPage1; /* First page of the database */ 418 MemPage *pPage1; /* First page of the database */
412 u8 openFlags; /* Flags to sqlite3BtreeOpen() */ 419 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
413 #ifndef SQLITE_OMIT_AUTOVACUUM 420 #ifndef SQLITE_OMIT_AUTOVACUUM
414 u8 autoVacuum; /* True if auto-vacuum is enabled */ 421 u8 autoVacuum; /* True if auto-vacuum is enabled */
415 u8 incrVacuum; /* True if incr-vacuum is enabled */ 422 u8 incrVacuum; /* True if incr-vacuum is enabled */
416 u8 bDoTruncate; /* True to truncate db on commit */ 423 u8 bDoTruncate; /* True to truncate db on commit */
417 #endif 424 #endif
418 u8 inTransaction; /* Transaction state */ 425 u8 inTransaction; /* Transaction state */
419 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ 426 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
427 #ifdef SQLITE_HAS_CODEC
428 u8 optimalReserve; /* Desired amount of reserved space per page */
429 #endif
420 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ 430 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
421 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ 431 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
422 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ 432 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
423 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ 433 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
424 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ 434 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
425 u32 pageSize; /* Total number of bytes on a page */ 435 u32 pageSize; /* Total number of bytes on a page */
426 u32 usableSize; /* Number of usable bytes on each page */ 436 u32 usableSize; /* Number of usable bytes on each page */
427 int nTransaction; /* Number of open transactions (read + write) */ 437 int nTransaction; /* Number of open transactions (read + write) */
428 u32 nPage; /* Number of pages in the database */ 438 u32 nPage; /* Number of pages in the database */
429 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ 439 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
(...skipping 18 matching lines...) Expand all
448 #define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */ 458 #define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */
449 #define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */ 459 #define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */
450 #define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */ 460 #define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */
451 #define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */ 461 #define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */
452 462
453 /* 463 /*
454 ** An instance of the following structure is used to hold information 464 ** An instance of the following structure is used to hold information
455 ** about a cell. The parseCellPtr() function fills in this structure 465 ** about a cell. The parseCellPtr() function fills in this structure
456 ** based on information extract from the raw disk page. 466 ** based on information extract from the raw disk page.
457 */ 467 */
458 typedef struct CellInfo CellInfo;
459 struct CellInfo { 468 struct CellInfo {
460 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ 469 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
461 u8 *pPayload; /* Pointer to the start of payload */ 470 u8 *pPayload; /* Pointer to the start of payload */
462 u32 nPayload; /* Bytes of payload */ 471 u32 nPayload; /* Bytes of payload */
463 u16 nLocal; /* Amount of payload held locally, not on overflow */ 472 u16 nLocal; /* Amount of payload held locally, not on overflow */
464 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
465 u16 nSize; /* Size of the cell content on the main b-tree page */ 473 u16 nSize; /* Size of the cell content on the main b-tree page */
466 }; 474 };
467 475
468 /* 476 /*
469 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than 477 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
470 ** this will be declared corrupt. This value is calculated based on a 478 ** this will be declared corrupt. This value is calculated based on a
471 ** maximum database size of 2^31 pages a minimum fanout of 2 for a 479 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
472 ** root-node and 3 for all other internal nodes. 480 ** root-node and 3 for all other internal nodes.
473 ** 481 **
474 ** If a tree that appears to be taller than this is encountered, it is 482 ** If a tree that appears to be taller than this is encountered, it is
(...skipping 16 matching lines...) Expand all
491 ** found at self->pBt->mutex. 499 ** found at self->pBt->mutex.
492 ** 500 **
493 ** skipNext meaning: 501 ** skipNext meaning:
494 ** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. 502 ** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op.
495 ** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. 503 ** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op.
496 ** eState==FAULT: Cursor fault with skipNext as error code. 504 ** eState==FAULT: Cursor fault with skipNext as error code.
497 */ 505 */
498 struct BtCursor { 506 struct BtCursor {
499 Btree *pBtree; /* The Btree to which this cursor belongs */ 507 Btree *pBtree; /* The Btree to which this cursor belongs */
500 BtShared *pBt; /* The BtShared this cursor points to */ 508 BtShared *pBt; /* The BtShared this cursor points to */
501 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ 509 BtCursor *pNext; /* Forms a linked list of all cursors */
502 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
503 Pgno *aOverflow; /* Cache of overflow page locations */ 510 Pgno *aOverflow; /* Cache of overflow page locations */
504 CellInfo info; /* A parse of the cell we are pointing at */ 511 CellInfo info; /* A parse of the cell we are pointing at */
505 i64 nKey; /* Size of pKey, or last integer key */ 512 i64 nKey; /* Size of pKey, or last integer key */
506 void *pKey; /* Saved key that was cursor last known position */ 513 void *pKey; /* Saved key that was cursor last known position */
507 Pgno pgnoRoot; /* The root page of this tree */ 514 Pgno pgnoRoot; /* The root page of this tree */
508 int nOvflAlloc; /* Allocated size of aOverflow[] array */ 515 int nOvflAlloc; /* Allocated size of aOverflow[] array */
509 int skipNext; /* Prev() is noop if negative. Next() is noop if positive. 516 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
510 ** Error code if eState==CURSOR_FAULT */ 517 ** Error code if eState==CURSOR_FAULT */
511 u8 curFlags; /* zero or more BTCF_* flags defined below */ 518 u8 curFlags; /* zero or more BTCF_* flags defined below */
519 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
512 u8 eState; /* One of the CURSOR_XXX constants (see below) */ 520 u8 eState; /* One of the CURSOR_XXX constants (see below) */
513 u8 hints; /* As configured by CursorSetHints() */ 521 u8 hints; /* As configured by CursorSetHints() */
514 i16 iPage; /* Index of current page in apPage */ 522 /* All fields above are zeroed when the cursor is allocated. See
523 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
524 ** initialized. */
525 i8 iPage; /* Index of current page in apPage */
526 u8 curIntKey; /* Value of apPage[0]->intKey */
527 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
528 void *padding1; /* Make object size a multiple of 16 */
515 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ 529 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
516 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ 530 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
517 }; 531 };
518 532
519 /* 533 /*
520 ** Legal values for BtCursor.curFlags 534 ** Legal values for BtCursor.curFlags
521 */ 535 */
522 #define BTCF_WriteFlag 0x01 /* True if a write cursor */ 536 #define BTCF_WriteFlag 0x01 /* True if a write cursor */
523 #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ 537 #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
524 #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ 538 #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
525 #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ 539 #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
526 #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ 540 #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
541 #define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
527 542
528 /* 543 /*
529 ** Potential values for BtCursor.eState. 544 ** Potential values for BtCursor.eState.
530 ** 545 **
531 ** CURSOR_INVALID: 546 ** CURSOR_INVALID:
532 ** Cursor does not point to a valid entry. This can happen (for example) 547 ** Cursor does not point to a valid entry. This can happen (for example)
533 ** because the table is empty or because BtreeCursorFirst() has not been 548 ** because the table is empty or because BtreeCursorFirst() has not been
534 ** called. 549 ** called.
535 ** 550 **
536 ** CURSOR_VALID: 551 ** CURSOR_VALID:
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 BtShared *pBt; /* The tree being checked out */ 674 BtShared *pBt; /* The tree being checked out */
660 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ 675 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
661 u8 *aPgRef; /* 1 bit per page in the db (see above) */ 676 u8 *aPgRef; /* 1 bit per page in the db (see above) */
662 Pgno nPage; /* Number of pages in the database */ 677 Pgno nPage; /* Number of pages in the database */
663 int mxErr; /* Stop accumulating errors when this reaches zero */ 678 int mxErr; /* Stop accumulating errors when this reaches zero */
664 int nErr; /* Number of messages written to zErrMsg so far */ 679 int nErr; /* Number of messages written to zErrMsg so far */
665 int mallocFailed; /* A memory allocation error has occurred */ 680 int mallocFailed; /* A memory allocation error has occurred */
666 const char *zPfx; /* Error message prefix */ 681 const char *zPfx; /* Error message prefix */
667 int v1, v2; /* Values for up to two %d fields in zPfx */ 682 int v1, v2; /* Values for up to two %d fields in zPfx */
668 StrAccum errMsg; /* Accumulate the error message text here */ 683 StrAccum errMsg; /* Accumulate the error message text here */
684 u32 *heap; /* Min-heap used for analyzing cell coverage */
669 }; 685 };
670 686
671 /* 687 /*
672 ** Routines to read or write a two- and four-byte big-endian integer values. 688 ** Routines to read or write a two- and four-byte big-endian integer values.
673 */ 689 */
674 #define get2byte(x) ((x)[0]<<8 | (x)[1]) 690 #define get2byte(x) ((x)[0]<<8 | (x)[1])
675 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) 691 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
676 #define get4byte sqlite3Get4byte 692 #define get4byte sqlite3Get4byte
677 #define put4byte sqlite3Put4byte 693 #define put4byte sqlite3Put4byte
694
695 /*
696 ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
697 ** two-byte aligned address. get2bytea() is only used for accessing the
698 ** cell addresses in a btree header.
699 */
700 #if SQLITE_BYTEORDER==4321
701 # define get2byteAligned(x) (*(u16*)(x))
702 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
703 && GCC_VERSION>=4008000
704 # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
705 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
706 && defined(_MSC_VER) && _MSC_VER>=1300
707 # define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
708 #else
709 # define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
710 #endif
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/btree.c ('k') | third_party/sqlite/src/src/build.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698