Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Begin modifying the BTree code for the new version-3 file format. This is a work-in-progress. As of this check-in, SQLite will not build. (CVS 1306) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
ce0bbd3a7159e12c86c5cde6571d6668 |
User & Date: | drh 2004-04-23 23:43:10.000 |
Context
2004-04-26
| ||
14:10 | Pager tests working. (CVS 1308) (check-in: 910067a200 user: drh tags: trunk) | |
2004-04-23
| ||
23:43 | Begin modifying the BTree code for the new version-3 file format. This is a work-in-progress. As of this check-in, SQLite will not build. (CVS 1306) (check-in: ce0bbd3a71 user: drh tags: trunk) | |
23:38 | Add an extra assert() to lemon.c to make debugging easier. Ticket #692. (CVS 1305) (check-in: 818bdba5dc user: drh tags: trunk) | |
Changes
Changes to src/btree.c.
1 | /* | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /* ** 2004 April 6 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** $Id: btree.c,v 1.104 2004/04/23 23:43:10 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to ** ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: ** "Sorting And Searching", pages 473-480. Addison-Wesley ** Publishing Company, Reading, Massachusetts. |
︙ | ︙ | |||
32 33 34 35 36 37 38 | ** so forth. ** ** Finding a particular key requires reading O(log(M)) pages from the ** disk where M is the number of entries in the tree. ** ** In this implementation, a single file can hold one or more separate ** BTrees. Each BTree is identified by the index of its root page. The | | | | | | | > > > > > > > > > | > > > > > > > > | > > > | > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < | < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < | < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | | | > > > > > | | | | > | | 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | ** so forth. ** ** Finding a particular key requires reading O(log(M)) pages from the ** disk where M is the number of entries in the tree. ** ** In this implementation, a single file can hold one or more separate ** BTrees. Each BTree is identified by the index of its root page. The ** key and data for any entry are combined to form the "payload". A ** fixed amount of payload can be carried directly on the database ** page. If the payload is larger than the preset amount then surplus ** bytes are stored on overflow pages. The payload for an entry ** and the preceding pointer are combined to form a "Cell". Each ** page has a small header which contains the Ptr(N+1) pointer and other ** information such as the size of key and data. ** ** FORMAT DETAILS ** ** The file is divided into pages. The first page is called page 1, ** the second is page 2, and so forth. A page number of zero indicates ** "no such page". The page size can be anything between 512 and 65536. ** Each page can be either a btree page, a freelist page or an overflow ** page. ** ** The first page is always a btree page. The first 100 bytes of the first ** page contain a special header that describes the file. The format ** of that header is as follows: ** ** OFFSET SIZE DESCRIPTION ** 0 16 Header string: "SQLite version 3" ** 16 2 Page size in bytes. ** 18 1 File format write version ** 19 1 File format read version ** 20 2 Bytes of unused space at the end of each page ** 22 2 Maximum allowed local payload per entry ** 24 8 File change counter ** 32 4 First freelist page ** 36 4 Number of freelist pages in the file ** 40 60 15 4-byte meta values passed to higher layers ** ** All of the integer values are big-endian (most significant byte first). ** The file change counter is incremented every time the database is changed. ** This allows other processes to know when the file has changed and thus ** when they need to flush their cache. ** ** Each btree page begins with a header described below. Note that the ** header for page one begins at byte 100. For all other btree pages, the ** header begins on byte zero. ** ** OFFSET SIZE DESCRIPTION ** 0 1 Flags. 01: leaf, 02: zerodata, 04: intkey, F8: type ** 1 2 byte offset to the first freeblock ** 3 2 byte offset to the first cell ** 5 1 number of fragmented free bytes ** 6 4 Right child (the Ptr(N+1) value). Omitted if leaf ** ** The flags define the format of this btree page. The leaf flag means that ** this page has no children. The zerodata flag means that this page carries ** only keys and no data. The intkey flag means that the key is a single ** variable length integer at the beginning of the payload. ** ** A variable-length integer is 1 to 9 bytes where the lower 7 bits of each ** byte are used. The integer consists of all bytes that have bit 8 set and ** the first byte with bit 8 clear. Unlike fixed-length values, variable- ** length integers are little-endian. Examples: ** ** 0x00 becomes 0x00000000 ** 0x1b becomes 0x0000001b ** 0x9b 0x4a becomes 0x00000dca ** 0x80 0x1b becomes 0x0000001b ** 0xf8 0xac 0xb1 0x91 0x01 becomes 0x12345678 ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 ** ** Variable length integers are used for rowids and to hold the number of ** bytes of key and data in a btree cell. ** ** Unused space within a btree page is collected into a linked list of ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset ** to the first freeblock is given in the header. Freeblocks occur in ** increasing order. Because a freeblock is 4 bytes in size, the minimum ** size allocation on a btree page is 4 bytes. Because a freeblock must be ** at least 4 bytes in size, any group of 3 or fewer unused bytes cannot ** exist on the freeblock chain. The total number of such fragmented bytes ** is recorded in the page header at offset 5. ** ** SIZE DESCRIPTION ** 2 Byte offset of the next freeblock ** 2 Bytes in this freeblock ** ** Cells are of variable length. The first cell begins on the byte defined ** in the page header. Cells do not necessarily occur in order - they can ** skip around on the page. ** ** SIZE DESCRIPTION ** 2 Byte offset of the next cell. 0 if this is the last cell ** 4 Page number of the left child. Omitted if leaf flag is set. ** var Number of bytes of data. Omitted if the zerodata flag is set. ** var Number of bytes of key. Omitted if the intkey flag is set. ** * Payload ** 4 First page of the overflow chain. Omitted if no overflow ** ** Overflow pages form a linked list. Each page except the last is completely ** filled with data (pagesize - 4 bytes). The last page can have as little ** as 1 byte of data. ** ** SIZE DESCRIPTION ** 4 Page number of next overflow page ** * Data ** ** Freelist pages come in two subtypes: trunk pages and leaf pages. The ** file header points to first in a linked list of trunk page. Each trunk ** page points to multiple leaf pages. The content of a leaf page is ** unspecified. A trunk page looks like this: ** ** SIZE DESCRIPTION ** 4 Page number of next trunk page ** 4 Number of leaf pointers on this page ** * zero or more pages numbers of leaves */ #include "sqliteInt.h" #include "pager.h" #include "btree.h" #include <assert.h> /* Forward declarations */ static BtOps sqliteBtreeOps; static BtCursorOps sqliteBtreeCursorOps; /* ** This is a magic string that appears at the beginning of every ** SQLite database in order to identify the file as a real database. ** 0123456789 123456 */ static const char zMagicHeader[] = "SQLite version 3"; /* ** Page type flags */ #define PTF_LEAF 0x01 #define PTF_ZERODATA 0x02 #define PTF_INTKEY 0x04 /* ** As each page of the file is loaded into memory, an instance of the following ** structure is appended and initialized to zero. This structure stores ** information about the page that is decoded from the raw file page. ** The extra two entries in apCell[] are an allowance for this situation. ** ** The pParent field points back to the parent page. This allows us to ** walk up the BTree from any leaf to the root. Care must be taken to ** unref() the parent page pointer when this page is no longer referenced. ** The pageDestructor() routine handles that chore. */ struct MemPage { struct BTree *pBt; /* Pointer back to BTree structure */ unsigned char *aData; /* Pointer back to the start of the page */ u8 idxShift; /* True if Cell indices have changed */ u8 isOverfull; /* Some aCell[] points outside u.aDisk[] */ u8 intKey; /* True if intkey flag is set */ u8 leaf; /* True if leaf flag is set */ u8 zeroData; /* True if zero data flag is set */ u8 hdrOffset; /* 100 for page 1. 0 otherwise */ Pgno pgno; /* Page number for this page */ MemPage *pParent; /* The parent of this page. NULL for root */ int idxParent; /* Index in pParent->apCell[] of this node */ int nFree; /* Number of free bytes on the page */ int nCell; /* Number of entries on this page */ unsigned char **aCell; /* Pointer to start of each cell */ }; /* ** The in-memory image of a disk page has the auxiliary information appended ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold ** that extra information. */ #define EXTRA_SIZE sizeof(Mempage) /* ** Everything we need to know about an open database */ struct Btree { BtOps *pOps; /* Function table */ Pager *pPager; /* The page cache */ BtCursor *pCursor; /* A list of all open cursors */ MemPage *page1; /* First page of the database */ u8 inTrans; /* True if a transaction is in progress */ u8 inCkpt; /* True if there is a checkpoint on the transaction */ u8 readOnly; /* True if the underlying file is readonly */ int pageSize; /* Number of usable bytes on each page */ int maxLocal; /* Maximum local payload */ }; typedef Btree Bt; /* ** A cursor is a pointer to a particular entry in the BTree. ** The entry is identified by its MemPage and the index in ** MemPage.apCell[] of the entry. |
︙ | ︙ | |||
381 382 383 384 385 386 387 | #define SKIP_PREV 2 /* The next sqliteBtreePrevious() is a no-op */ #define SKIP_INVALID 3 /* Calls to Next() and Previous() are invalid */ /* Forward declarations */ static int fileBtreeCloseCursor(BtCursor *pCur); /* | > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > | | | > > > > > > > > > > > | < | | > > | | | > > > > > > > > > | > > > > > > | | > > | | | | | | | > > | | | < < > | < | > > | < > | | | > > > > | > | > | < > > | < > > | | | > | | | > > > | < < | | > > | < | > | > > > > > > | | > > | > | > | | | | < | < < > > > > > < < < < < > | > | | | | < | | > > > > > | > > > > > | > > > > > > > > > > > > | | | > | < | | | | | > | | | > > > | | < > > | > | < > > > > > > > > > > > > > > > > > > > > | > | | < > > > | < > | < > > > > > > | | < < > > | > > > > > | | > > > | > | < | > > > > | > > > > > | > | > | | < | > > | > > | < | | > > | | | | > > > > > | > > | | > | > > > | > > > > | > | < < < < > > | < > | | | > | < > | < < | < < < | | > > | | | | | | > | > > | | | | | | > | | < < > > > > | | | > > | 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | #define SKIP_PREV 2 /* The next sqliteBtreePrevious() is a no-op */ #define SKIP_INVALID 3 /* Calls to Next() and Previous() are invalid */ /* Forward declarations */ static int fileBtreeCloseCursor(BtCursor *pCur); /* ** Read or write a two-, four-, and eight-byte integer values */ static u32 get2byte(unsigned char *p){ return (p[0]<<8) | p[1]; } static u32 get4byte(unsigned char *p){ return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; } static u64 get4byte(unsigned char *p){ u64 v = get4byte(p); return (v<<32) | get4byte(&p[4]); } static void put2byte(unsigned char *p, u32 v){ p[0] = v>>8; p[1] = v; } static void put4byte(unsigned char *p, u32 v){ p[0] = v>>24; p[1] = v>>16; p[2] = v>>8; p[3] = v; } static void put8byte(unsigned char *p, u64 v){ put4byte(&p[4], v>>32); put4byte(p, v); } /* ** Read a variable-length integer. Store the result in *pResult. ** Return the number of bytes in the integer. */ static unsigned int getVarint(unsigned char *p, u64 *pResult){ u64 x = p[0] & 0x7f; int n = 0; while( (p[n++]&0x80)!=0 ){ x |= (p[n]&0x7f)<<(n*7); } *pResult = x; return n; } /* ** Write a variable length integer with value v into p[]. Return ** the number of bytes written. */ static unsigned int putVarint(unsigned char *p, u64 v){ int i = 0; do{ p[i++] = v & 0x7f; v >>= 7; }while( v!=0 ); p[i-1] |= 0x80; return i; } /* ** Compute the total number of bytes that a Cell needs on the main ** database page. The number returned includes the Cell header, ** local payload storage, and the pointer to overflow pages (if ** applicable). Additional space allocated on overflow pages ** is NOT included in the value returned from this routine. */ static int cellSize(MemPage *pPage, unsigned char *pCell){ int n, nPayload; u64 nData, nKey; int maxPayload; if( pPage->leaf ){ n = 2; }else{ n = 6; } if( pPage->zeroData ){ nData = 0; }else{ n += getVarint(&pCell[n], &nData); } if( pPage->intKey ){ u64 dummy; nKey = getVarint(&pCell[n], &dummy); }else{ n += getVarint(pCell, &nKey); } nPayload = nKey + nData; maxPayload = pPage->pBt->maxPayload; if( nPayload>maxPayload ){ nPayload = maxPayload + 4; } return n + nPayload; } /* ** Defragment the page given. All Cells are moved to the ** beginning of the page and all free space is collected ** into one big FreeBlk at the end of the page. */ static void defragmentPage(MemPage *pPage){ int pc, i, n; int start, hdr; int leftover; unsigned char *oldPage; unsigned char newPage[SQLITE_PAGE_SIZE]; assert( sqlitepager_iswriteable(pPage->aData) ); assert( pPage->pBt!=0 ); assert( pPage->pageSize <= SQLITE_PAGE_SIZE ); oldPage = pPage->aData; hdr = pPage->hdrOffset; ptr = 3+hdr; n = 6+hdr; if( !pPage->leaf ){ n += 4; } start = n; pc = get2byte(&oldPage[ptr]); i = 0; while( pc>0 ){ assert( n<pPage->pageSize ); size = cellSize(pPage, &oldPage[pc]); memcpy(&newPage[n], &oldPage[pc], size); put2byte(&newPage[ptr],n); pPage->aCell[i] = &oldPage[n]; n += size; ptr = pc; pc = get2byte(&oldPage[pc]); } leftover = pPage->pageSize - n; assert( leftover>=0 ); assert( pPage->nFree==leftover ); if( leftover<4 ){ oldPage[hdr+5] = leftover; leftover = 0; n = pPage->pageSize; } memcpy(&oldPage[start], &newPage[start], n-start); if( leftover==0 ){ put2byte(&oldPage[hdr+3], 0); }else if( leftover>=4 ){ put2byte(&oldPage[hdr+3], n); put2byte(&oldPage[n], 0); put2byte(&oldPage[n+2], leftover); memset(&oldPage[n+4], 0, leftover-4); } } /* ** Allocate nByte bytes of space on a page. If nByte is less than ** 4 it is rounded up to 4. ** ** Return the index into pPage->aData[] of the first byte of ** the new allocation. Or return 0 if there is not enough free ** space on the page to satisfy the allocation request. ** ** If the page contains nBytes of free space but does not contain ** nBytes of contiguous free space, then this routine automatically ** calls defragementPage() to consolidate all free space before ** allocating the new chunk. ** ** Algorithm: Carve a piece off of the first freeblock that is ** nByte in size or that larger. */ static int allocateSpace(MemPage *pPage, int nByte){ int ptr, pc, hdr; int size; unsigned char *data; #ifndef NDEBUG int cnt = 0; #endif data = pPage->aData; assert( sqlitepager_iswriteable(data) ); assert( pPage->pBt ); if( nByte<4 ) nByte = 4; if( pPage->nFree<nByte || pPage->isOverfull ) return 0; hdr = pPage->hdrOffset; if( data[hdr+5]>=252 ){ defragmentPage(pPage); } ptr = hdr+1; pc = get2byte(&data[ptr]); assert( ptr<pc ); assert( pc<=pPage->pageSize-4 ); while( (size = get2byte(&data[pc+2])<nByte ){ ptr = pc; pc = get2byte(&data[ptr]); assert( pc<=pPage->pageSize-4 ); assert( pc>=ptr+size+4 || pc==0 ); if( pc==0 ){ assert( (cnt++)==0 ); defragmentPage(pPage); assert( data[hdr+5]==0 ); ptr = pPage->hdrOffset+1; pc = get2byte(&data[ptr]); } } assert( pc>0 && size>=nByte ); assert( pc+size<=pPage->pageSize ); if( size>nByte+4 ){ put2byte(&data[ptr], pc+nByte); put2byte(&data[pc+size], get2byte(&data[pc])); put2byte(&data[pc+size+2], size-nByte); }else{ put2byte(&data[ptr], get2byte(&data[pc])); data[hdr+5] += size-nByte; } pPage->nFree -= nByte; assert( pPage->nFree>=0 ); return pc; } /* ** Return a section of the pPage->aData to the freelist. ** The first byte of the new free block is pPage->aDisk[start] ** and the size of the block is "size" bytes. ** ** Most of the effort here is involved in coalesing adjacent ** free blocks into a single big free block. */ static void freeSpace(MemPage *pPage, int start, int size){ int end = start + size; /* End of the segment being freed */ int ptr, pbegin, pend; #ifndef NDEBUG int tsize = 0; /* Total size of all freeblocks */ #endif unsigned char *data = pPage->aData; assert( pPage->pBt!=0 ); assert( sqlitepager_iswriteable(data) ); assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); assert( end<=pPage->pBt->pageSize ); if( size<4 ) size = 4; /* Add the space back into the linked list of freeblocks */ ptr = pPage->hdrOffset + 1; while( (pbegin = get2byte(&data[ptr]))<start && pbegin>0 ){ assert( pbegin<=pPage->pBt->pageSize-4 ); assert( pbegin>ptr ); ptr = pbegin; } assert( pbegin<=pPage->pBt->pageSize-4 ); assert( pbegin>ptr || pbegin==0 ); put2bytes(&data[ptr], start); put2bytes(&data[start], pbegin); put2bytes(&data[start+2], size); pPage->nFree += size; /* Coalesce adjacent free blocks */ ptr = pPage->hdrOffset + 1; while( (pbegin = get2byte(&data[ptr]))>0 ){ int pnext, psize; assert( pbegin>ptr ); assert( pbegin<pPage->pBt->pageSize-4 ); pnext = get2byte(&data[pbegin]); psize = get2byte(&data[pbegin+2]); if( pbegin + psize + 3 >= pnext && pnext>0 ){ int frag = pnext - (pbegin+psize); assert( frag<=data[pPage->hdrOffset+5] ); data[pPage->hdrOffset+5] -= frag; put2byte(&data[pbegin], get2byte(&data[pnext])); put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); }else{ assert( (tsize += psize)>0 ); ptr = pbegin; } } assert( tsize+data[pPage->hdrOffset+5]==pPage->nFree ); } /* ** The following is the default comparison function for (non-integer) ** keys in the btrees. This function returns negative, zero, or ** positive if the first key is less than, equal to, or greater than ** the second. ** */ static int keyComp( void *userData, int nKey1, const unsigned char *aKey1, int nKey2, const unsigned char *aKey2, ){ KeyClass *pKeyClass = (KeyClass*)userData; i1 = i2 = 0; for(i1=i2=0; pKeyClass!=0; pKeyClass=pKeyClass->pNext){ if( varint32(aKey1, &i1, nKey1, &n1) ) goto bad_key; if( varint32(aKey2, &i2, nKey2, &n2) ) goto bad_key; if( n1==0 ){ if( n2>0 ) return -1; /* both values are NULL. consider them equal for sorting purposes. */ }else if( n2==0 ){ /* right value is NULL but the left value is not. right comes first */ return +1; }else if( n1<=5 ){ if( n2>5 ) return -1; /* both values are numbers. sort them numerically */ ... }else if( n2<=5 ){ /* right value is numeric and left is TEXT or BLOB. right comes first */ return +1; }else if( n1<12 || n2<12 ){ /* bad coding for either the left or the right value */ goto bad_key; }else if( (n1&0x01)==0 ){ if( n2&0x01)!=0 ) return -1; /* both values are BLOB. use memcmp() */ n1 = (n1-12)/2; n2 = (n2-12)/2; if( i1+n1>nKey1 || i2+n2>nKey2 ) goto bad_key; c = memcmp(&aKey1[i1], &aKey2[i2], n1<n2 ? n1 : n2); if( c!=0 ){ return c | 1; } if( n1!=n2 ){ return (n1-n2) | 1; } i1 += n1; i2 += n2; }else if( n2&0x01)!=0 ){ /* right value if BLOB and left is TEXT. BLOB comes first */ return +1; }else{ /* both values are TEXT. use the supplied comparison function */ n1 = (n1-13)/2; n2 = (n2-13)/2; if( i1+n1>nKey1 || i2+n2>nKey2 ) goto bad_key; c = pKeyClass->xCompare(pKeyClass->pUser, n1, &aKey1[i1], n2, &aKey2[i2]); if( c!=0 ){ return c | 1; } i1 += n1; i2 += n2; } } return 0; bad_key: return 1; } /* ** Initialize the auxiliary information for a disk block. ** ** The pParent parameter must be a pointer to the MemPage which ** is the parent of the page being initialized. The root of a ** BTree has no parent and so for that page, pParent==NULL. ** ** Return SQLITE_OK on success. If we see that the page does ** not contain a well-formed database page, then return ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not ** guarantee that the page is well-formed. It only shows that ** we failed to detect any corruption. */ static int initPage( Bt *pBt, /* The Btree */ unsigned char *data, /* Start of the data for the page */ Pgno pgnoThis, /* The page number */ MemPage *pParent /* The parent. Might be NULL */ ){ MemPage *pPage; int c, pc, i; int sumCell = 0; /* Total size of all cells */ unsigned char *data; pPage = (MemPage*)&aData[pBt->pageSize]; if( pPage->pParent ){ assert( pPage->pParent==pParent ); return SQLITE_OK; } if( pParent ){ pPage->pParent = pParent; sqlitepager_ref(pParent->aData); } if( pPage->pBt!=0 ) return SQLITE_OK; pPage->pBt = pBt; pPage->nCell = 0; pPage->pgno = pgnoThis; pPage->hdrOffset = hdr = pgnoThis==1 ? 100 : 0; c = data[pPage->hdrOffset]; pPage->intKey = (c & PTF_INTKEY)!=0; pPage->zeroData = (c & PTF_ZERODATA)!=0; pPage->leaf = (c & PTF_INTKEY)!=0; /* Initialize the cell count and cell pointers */ pc = get2byte(&data[hdr+3]); while( pc>0 ){ if( pc>=pBt->pageSize ) return SQLITE_CORRUPT; if( pPage->nCell>pBt->pageSize ) return SQLITE_CORRUPT; pPage->nCell++; pc = get2byte(&data[pc]); } pPage->aCell = sqlite_malloc( sizeof(pPage->aCell[0])*pPage->nCell ); if( pPage->aCell==0 ){ return SQLITE_NOMEM; } pc = get2byte(&data[hdr+3]); for(i=0; pc>0; i++){ pPage->aCell[i] = &data[pc]; pc = get2byte(&data[pc]); sumCell += cellSize(pPage, &data[pc]); } /* Compute the total free space on the page */ pPage->nFree = data[hdr+5]; pc = get2byte(&data[hdr+1]); while( pc>0 ){ int next, size; if( pc>=pBt->pageSize ) return SQLITE_CORRUPT; next = get2byte(&data[pc]); size = get2byte(&data[pc+2]); if( next>0 && next<=pc+size+3 ) return SQLITE_CURRUPT; pPage->nFree += size; pc = next; } if( pPage->nFree>=pBt->pageSize ) return SQLITE_CORRUPT; /* Sanity check: Cells and freespace and header must sum to the size ** a page. */ if( sumCell+pPage->nFree+hdr+10-pPage->leaf*4 != pBt->pageSize ){ return CORRUPT; } return SQLITE_OK; } /* ** Set up a raw page so that it looks like a database page holding ** no entries. */ static void zeroPage(MemPage *pPage, int flags){ unsigned char *data = pPage->aData; Btree *pBt = pPage->pBt; int hdr = pPage->pgno==1 ? 100 : 0; int first; assert( sqlitepager_iswriteable(data) ); memset(&data[hdr], 0, pBt->pageSize - hdr); data[hdr] = flags; first = hdr + 6 + 4*((flags&0x01)!=0); put2byte(&data[hdr+1], first); put2byte(&data[first+2], pBt->pageSize - first); sqliteFree(pPage->aCell); pPage->aCell = 0; pPage->nCell = 0; pPage->nFree = pBt->pageSize - first; pPage->intKey = (flags & PTF_INTKEY)!=0; pPage->leaf = (flags & PTF_LEAF)!=0; pPage->zeroData = (flags & PTF_ZERODATA)!=0; pPage->hdrOffset = hdr; } /* ** This routine is called when the reference count for a page ** reaches zero. We need to unref the pParent pointer when that ** happens. */ static void pageDestructor(void *pData){ MemPage *pPage = (MemPage*)&((char*)pData)[SQLITE_PAGE_SIZE]; if( pPage->pParent ){ MemPage *pParent = pPage->pParent; pPage->pParent = 0; sqlitepager_unref(pParent->aData); } sqliteFree(pPage->aCell); pPage->aCell = 0; } /* ** Open a new database. ** ** Actually, this routine just sets up the internal data structures ** for accessing the database. We do not open the database file |
︙ | ︙ | |||
687 688 689 690 691 692 693 694 695 696 | int rc; /* ** The following asserts make sure that structures used by the btree are ** the right size. This is to guard against size changes that result ** when compiling on a different architecture. */ assert( sizeof(u32)==4 ); assert( sizeof(u16)==2 ); assert( sizeof(Pgno)==4 ); | > < < < < < | 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | int rc; /* ** The following asserts make sure that structures used by the btree are ** the right size. This is to guard against size changes that result ** when compiling on a different architecture. */ assert( sizeof(u64)==8 ); assert( sizeof(u32)==4 ); assert( sizeof(u16)==2 ); assert( sizeof(Pgno)==4 ); assert( sizeof(ptr)==sizeof(char*) ); assert( sizeof(uptr)==sizeof(ptr) ); pBt = sqliteMalloc( sizeof(*pBt) ); if( pBt==0 ){ *ppBtree = 0; return SQLITE_NOMEM; |
︙ | ︙ | |||
717 718 719 720 721 722 723 724 725 726 727 728 729 730 | return rc; } sqlitepager_set_destructor(pBt->pPager, pageDestructor); pBt->pCursor = 0; pBt->page1 = 0; pBt->readOnly = sqlitepager_isreadonly(pBt->pPager); pBt->pOps = &sqliteBtreeOps; *ppBtree = pBt; return SQLITE_OK; } /* ** Close an open database and invalidate all cursors. */ | > > | 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | return rc; } sqlitepager_set_destructor(pBt->pPager, pageDestructor); pBt->pCursor = 0; pBt->page1 = 0; pBt->readOnly = sqlitepager_isreadonly(pBt->pPager); pBt->pOps = &sqliteBtreeOps; pBt->pageSize = SQLITE_PAGE_SIZE; pBt->maxLocal = (SQLITE_PAGE_SIZE-10)/4-12; *ppBtree = pBt; return SQLITE_OK; } /* ** Close an open database and invalidate all cursors. */ |
︙ | ︙ | |||
778 779 780 781 782 783 784 785 | ** well-formed database file, then SQLITE_CORRUPT is returned. ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM ** is returned if we run out of memory. SQLITE_PROTOCOL is returned ** if there is a locking protocol violation. */ static int lockBtree(Btree *pBt){ int rc; if( pBt->page1 ) return SQLITE_OK; | > | < | < | > | | | | | > > | < < < < < < < | < | > | < < | < > | < | 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | ** well-formed database file, then SQLITE_CORRUPT is returned. ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM ** is returned if we run out of memory. SQLITE_PROTOCOL is returned ** if there is a locking protocol violation. */ static int lockBtree(Btree *pBt){ int rc; unsigned char *data; if( pBt->page1 ) return SQLITE_OK; rc = sqlitepager_get(pBt->pPager, 1, (void**)&data); if( rc!=SQLITE_OK ) return rc; /* Do some checking to help insure the file we opened really is ** a valid database file. */ if( sqlitepager_pagecount(pBt->pPager)>0 ){ if( memcmp(data, zMagicHeader, 16)!=0 ){ rc = SQLITE_NOTADB; goto page1_init_failed; } /*** TBD: Other header checks such as page size ****/ } pBt->page1 = (MemPage*)&data[SQLITE_PAGE_SIZE]; return rc; page1_init_failed: sqlitepager_unref(pBt->data); pBt->page1 = 0; return rc; } /* ** If there are no outstanding cursors and we are not in the middle ** of a transaction but there is a read lock on the database, then ** this routine unrefs the first page of the database file which ** has the effect of releasing the read lock. ** ** If there are any outstanding cursors, this routine is a no-op. ** ** If there is a transaction in progress, this routine is a no-op. */ static void unlockBtreeIfUnused(Btree *pBt){ if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){ sqlitepager_unref(pBt->page1->aData); pBt->page1 = 0; pBt->inTrans = 0; pBt->inCkpt = 0; } } /* ** Create a new database by initializing the first page of the ** file. */ static int newDatabase(Btree *pBt){ MemPage *pP1; unsigned char *data; int rc; if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK; pP1 = pBt->page1; assert( pP1!=0 ); data = pP1->aData; rc = sqlitepager_write(data); if( rc ) return rc; memcpy(data, zMagicHeader, sizeof(zMagicHeader)); assert( sizeof(zMagicHeader)==16 ); put2byte(&data[16], SQLITE_PAGE_SIZE); data[18] = 1; data[19] = 1; put2byte(&data[22], (SQLITE_PAGE_SIZE-10)/4-12); zeroPage(pP1, PTF_INTKEY|PTF_LEAF); return SQLITE_OK; } /* ** Attempt to start a new transaction. ** ** A transaction must be started before attempting any changes |
︙ | ︙ | |||
923 924 925 926 927 928 929 | int rc; BtCursor *pCur; if( pBt->inTrans==0 ) return SQLITE_OK; pBt->inTrans = 0; pBt->inCkpt = 0; rc = pBt->readOnly ? SQLITE_OK : sqlitepager_rollback(pBt->pPager); for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ | | | 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | int rc; BtCursor *pCur; if( pBt->inTrans==0 ) return SQLITE_OK; pBt->inTrans = 0; pBt->inCkpt = 0; rc = pBt->readOnly ? SQLITE_OK : sqlitepager_rollback(pBt->pPager); for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ if( pCur->pPage && pCur->pPage->pBt==0 ){ sqlitepager_unref(pCur->pPage); pCur->pPage = 0; } } unlockBtreeIfUnused(pBt); return rc; } |
︙ | ︙ |