Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | All BTree code is in place. Now we just have to make it work. (CVS 225) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
d4be4709ee32bab6e78104861ed4e02d |
User & Date: | drh 2001-06-10 19:56:59.000 |
Context
2001-06-22
| ||
19:15 | The BTree code compiles and links now, but it does not work yet. (CVS 226) (check-in: b31c49021c user: drh tags: trunk) | |
2001-06-10
| ||
19:56 | All BTree code is in place. Now we just have to make it work. (CVS 225) (check-in: d4be4709ee user: drh tags: trunk) | |
2001-06-08
| ||
00:25 | documentation update (CVS 224) (check-in: d1e211fad9 user: drh tags: trunk) | |
Changes
Changes to src/btree.c.
︙ | ︙ | |||
17 18 19 20 21 22 23 | ** Boston, MA 02111-1307, USA. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ************************************************************************* | | | 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ** Boston, MA 02111-1307, USA. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ************************************************************************* ** $Id: btree.c,v 1.12 2001/06/10 19:56:59 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. |
︙ | ︙ | |||
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include "pager.h" #include "btree.h" #include <assert.h> /* ** Primitive data types. u32 must be 4 bytes and u16 must be 2 bytes. ** Change these typedefs when porting to new architectures. */ typedef unsigned int u32; typedef unsigned short int u16; typedef unsigned char u8; /* ** Forward declarations of structures used only in this file. */ | > > | 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include "pager.h" #include "btree.h" #include <assert.h> /* ** Primitive data types. u32 must be 4 bytes and u16 must be 2 bytes. ** The uptr type must be big enough to hold a pointer. ** Change these typedefs when porting to new architectures. */ typedef unsigned int uptr; typedef unsigned int u32; typedef unsigned short int u16; typedef unsigned char u8; /* ** Forward declarations of structures used only in this file. */ |
︙ | ︙ | |||
124 125 126 127 128 129 130 | }; /* ** Each database page has a header that is an instance of this ** structure. ** ** PageHdr.firstFree is 0 if there is no free space on this page. | | | | | | | 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 | }; /* ** Each database page has a header that is an instance of this ** structure. ** ** PageHdr.firstFree is 0 if there is no free space on this page. ** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a ** FreeBlk structure that describes the first block of free space. ** All free space is defined by a linked list of FreeBlk structures. ** ** Data is stored in a linked list of Cell structures. PageHdr.firstCell ** is the index into MemPage.u.aDisk[] of the first cell on the page. The ** Cells are kept in sorted order. ** ** A Cell contains all information about a database entry and a pointer ** to a child page that contains other entries less than itself. In ** other words, the i-th Cell contains both Ptr(i) and Key(i). The ** right-most pointer of the page is contained in PageHdr.rightChild. */ struct PageHdr { Pgno rightChild; /* Child page that comes after all cells on this page */ u16 firstCell; /* Index in MemPage.u.aDisk[] of the first cell */ u16 firstFree; /* Index in MemPage.u.aDisk[] of the first free block */ }; /* ** Entries on a page of the database are called "Cells". Each Cell ** has a header and data. This structure defines the header. The ** key and data (collectively the "payload") follow this header on ** the database page. ** ** A definition of the complete Cell structure is given below. The ** header for the cell must be defined separately in order to do some ** of the sizing #defines that follow. */ struct CellHdr { Pgno leftChild; /* Child page that comes before this cell */ u16 nKey; /* Number of bytes in the key */ u16 iNext; /* Index in MemPage.u.aDisk[] of next cell in sorted order */ u32 nData; /* Number of bytes of data */ } /* ** The minimum size of a complete Cell. The Cell must contain a header ** and at least 4 bytes of payload. */ |
︙ | ︙ | |||
210 211 212 213 214 215 216 | ** Free space on a page is remembered using a linked list of the FreeBlk ** structures. Space on a database page is allocated in increments of ** at least 4 bytes and is always aligned to a 4-byte boundry. The ** linked list of FreeBlks is always kept in order by address. */ struct FreeBlk { u16 iSize; /* Number of bytes in this block of free space */ | | | | | | > > > > > > > > > > | > > | > | | 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 | ** Free space on a page is remembered using a linked list of the FreeBlk ** structures. Space on a database page is allocated in increments of ** at least 4 bytes and is always aligned to a 4-byte boundry. The ** linked list of FreeBlks is always kept in order by address. */ struct FreeBlk { u16 iSize; /* Number of bytes in this block of free space */ u16 iNext; /* Index in MemPage.u.aDisk[] of the next free block */ }; /* ** The number of bytes of payload that will fit on a single overflow page. */ #define OVERFLOW_SIZE (SQLITE_PAGE_SIZE-sizeof(Pgno)) /* ** When the key and data for a single entry in the BTree will not fit in ** the MX_LOACAL_PAYLOAD bytes of space available on the database page, ** then all extra bytes are written to a linked list of overflow pages. ** Each overflow page is an instance of the following structure. ** ** Unused pages in the database are also represented by instances of ** the OverflowPage structure. The PageOne.freeList field is the ** page number of the first page in a linked list of unused database ** pages. */ struct OverflowPage { Pgno iNext; char aPayload[OVERFLOW_SIZE]; }; /* ** For every page in the database file, an instance of the following structure ** is stored in memory. The u.aDisk[] array contains the raw bits read from ** the disk. The rest is auxiliary information that held in memory only. The ** auxiliary info is only valid for regular database pages - it is not ** used for overflow pages and pages on the freelist. ** ** Of particular interest in the auxiliary info is the apCell[] entry. Each ** apCell[] entry is a pointer to a Cell structure in u.aDisk[]. The cells are ** put in this array so that they can be accessed in constant time, rather ** than in linear time which would be needed if we had to walk the linked ** list on every access. ** ** Note that apCell[] contains enough space to hold up to two more Cells ** than can possibly fit on one page. In the steady state, every apCell[] ** points to memory inside u.aDisk[]. But in the middle of an insert ** operation, some apCell[] entries may temporarily point to data space ** outside of u.aDisk[]. This is a transient situation that is quickly ** resolved. But while it is happening, it is possible for a database ** page to hold as many as two more cells than it might otherwise hold. ** The extra too 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 { union { char aDisk[SQLITE_PAGE_SIZE]; /* Page data stored on disk */ PageHdr hdr; /* Overlay page header */ } u; int isInit; /* True if auxiliary data is initialized */ MemPage *pParent; /* The parent of this page. NULL for root */ int nFree; /* Number of free bytes in u.aDisk[] */ int nCell; /* Number of entries on this page */ int isOverfull; /* Some apCell[] points outside u.aDisk[] */ Cell *apCell[MX_CELL+2]; /* All data entires in sorted order */ } /* ** 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. */ |
︙ | ︙ | |||
286 287 288 289 290 291 292 | /* ** 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. */ struct BtCursor { Btree *pBt; /* The Btree to which this cursor belongs */ | | | 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | /* ** 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. */ struct BtCursor { Btree *pBt; /* The Btree to which this cursor belongs */ BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ Pgno pgnoRoot; /* The root page of this tree */ MemPage *pPage; /* Page that contains the entry */ u16 idx; /* Index of the entry in pPage->apCell[] */ u8 bSkipNext; /* sqliteBtreeNext() is no-op if true */ u8 iMatch; /* compare result from last sqliteBtreeMoveto() */ }; |
︙ | ︙ | |||
318 319 320 321 322 323 324 | /* ** 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){ | | < | | | | | | | | | | | | | | | | | > | | | | | | 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 | /* ** 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; FreeBlk *pFBlk; char newPage[SQLITE_PAGE_SIZE]; pc = sizeof(PageHdr); pPage->u.hdr.firstCell = pc; memcpy(newPage, pPage->u.aDisk, pc); for(i=0; i<pPage->nCell; i++){ Cell *pCell = &pPage->apCell[i]; n = cellSize(pCell); pCell->h.iNext = i<pPage->nCell-1 ? pc + n : 0; memcpy(&newPage[pc], pCell, n); pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc]; pc += n; } assert( pPage->nFree==SQLITE_PAGE_SIZE-pc ); memcpy(pPage->u.aDisk, newPage, pc); pFBlk = &pPage->u.aDisk[pc]; pFBlk->iSize = SQLITE_PAGE_SIZE - pc; pFBlk->iNext = 0; pPage->u.hdr.firstFree = pc; memset(&pFBlk[1], 0, SQLITE_PAGE_SIZE - pc - sizeof(FreeBlk)); } /* ** Allocate nByte bytes of space on a page. nByte must be a ** multiple of 4. ** ** Return the index into pPage->u.aDisk[] 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. */ static int allocateSpace(MemPage *pPage, int nByte){ FreeBlk *p; u16 *pIdx; int start; assert( nByte==ROUNDUP(nByte) ); if( pPage->nFree<nByte || pPage->isOverfull ) return 0; pIdx = &pPage->u.hdr.firstFree; p = (FreeBlk*)&pPage->u.aDisk[*pIdx]; while( p->iSize<nByte ){ if( p->iNext==0 ){ defragmentPage(pPage); pIdx = &pPage->u.hdr.firstFree; }else{ pIdx = &p->iNext; } p = (FreeBlk*)&pPage->u.aDisk[*pIdx]; } if( p->iSize==nByte ){ start = *pIdx; *pIdx = p->iNext; }else{ start = *pIdx; FreeBlk *pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte]; pNew->iNext = p->iNext; pNew->iSize = p->iSize - nByte; *pIdx = start + nByte; } pPage->nFree -= nByte; return start; } /* ** Return a section of the MemPage.u.aDisk[] to the freelist. ** The first byte of the new free block is pPage->u.aDisk[start] ** and the size of the block is "size" bytes. Size must be ** a multiple of 4. ** ** 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; u16 *pIdx, idx; FreeBlk *pFBlk; FreeBlk *pNew; FreeBlk *pNext; assert( size == ROUNDUP(size) ); assert( start == ROUNDUP(start) ); pIdx = &pPage->u.hdr.firstFree; idx = *pIdx; while( idx!=0 && idx<start ){ pFBlk = (FreeBlk*)&pPage->u.aDisk[idx]; if( idx + pFBlk->iSize == start ){ pFBlk->iSize += size; if( idx + pFBlk->iSize == pFBlk->iNext ){ pNext = (FreeBlk*)&pPage->u.aDisk[pFblk->iNext]; pFBlk->iSize += pNext->iSize; pFBlk->iNext = pNext->iNext; } pPage->nFree += size; return; } pIdx = &pFBlk->iNext; idx = *pIdx; } pNew = (FreeBlk*)&pPage->u.aDisk[start]; if( idx != end ){ pNew->iSize = size; pNew->iNext = idx; }else{ pNext = (FreeBlk*)&pPage->u.aDisk[idx]; pNew->iSize = size + pNext->iSize; pNew->iNext = pNext->iNext; } *pIdx = start; pPage->nFree += size; } |
︙ | ︙ | |||
450 451 452 453 454 455 456 | ** Return SQLITE_OK on success. If we see that the page does ** not contained 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(MemPage *pPage, Pgno pgnoThis, MemPage *pParent){ | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | 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 | ** Return SQLITE_OK on success. If we see that the page does ** not contained 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(MemPage *pPage, Pgno pgnoThis, MemPage *pParent){ int idx; /* An index into pPage->u.aDisk[] */ Cell *pCell; /* A pointer to a Cell in pPage->u.aDisk[] */ FreeBlk *pFBlk; /* A pointer to a free block in pPage->u.aDisk[] */ int sz; /* The size of a Cell in bytes */ int freeSpace; /* Amount of free space on the page */ if( pPage->pParent ){ assert( pPage->pParent==pParent ); return SQLITE_OK; } if( pParent ){ pPage->pParent = pParent; sqlitepager_ref(pParent); } if( pPage->isInit ) return SQLITE_OK; pPage->isInit = 1; pPage->nCell = 0; freeSpace = SQLITE_PAGE_SIZE - sizeof(PageHdr); idx = pPage->u.hdr.firstCell; while( idx!=0 ){ if( idx>SQLITE_PAGE_SIZE-MN_CELL_SIZE ) goto page_format_error; if( idx<sizeof(PageHdr) ) goto page_format_error; pCell = (Cell*)&pPage->u.aDisk[idx]; sz = cellSize(pCell); if( idx+sz > SQLITE_PAGE_SIZE ) goto page_format_error; freeSpace -= sz; pPage->apCell[pPage->nCell++] = pCell; idx = pCell->h.iNext; } pPage->nFree = 0; idx = pPage->u.hdr.firstFree; while( idx!=0 ){ if( idx>SQLITE_PAGE_SIZE-sizeof(FreeBlk) ) goto page_format_error; if( idx<sizeof(PageHdr) ) goto page_format_error; pFBlk = (FreeBlk*)&pPage->u.aDisk[idx]; pPage->nFree += pFBlk->iSize; if( pFBlk->iNext <= idx ) goto page_format_error; idx = pFBlk->iNext; } if( pPage->nCell==0 && pPage->nFree==0 ){ /* As a special case, an uninitialized root page appears to be ** an empty database */ return SQLITE_OK; } if( pPage->nFree!=freeSpace ) goto page_format_error; return SQLITE_OK; page_format_error: return SQLITE_CORRUPT; } /* ** Set up a raw page so that it looks like a database page holding ** no entries. */ static void zeroPage(MemPage *pPage){ PageHdr *pHdr; FreeBlk *pFBlk; memset(pPage, 0, SQLITE_PAGE_SIZE); pHdr = &pPage->u.hdr; pHdr->firstCell = 0; pHdr->firstFree = sizeof(*pHdr); pFBlk = (FreeBlk*)&pHdr[1]; pFBlk->iNext = 0; pFBlk->iSize = SQLITE_PAGE_SIZE - sizeof(*pHdr); } |
︙ | ︙ | |||
753 754 755 756 757 758 759 | if( rc!=SQLITE_OK ){ goto create_cursor_exception; } rc = initPage(pCur->pPage, pCur->pgnoRoot, 0); if( rc!=SQLITE_OK ){ goto create_cursor_exception; } | | > > < < | 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 | if( rc!=SQLITE_OK ){ goto create_cursor_exception; } rc = initPage(pCur->pPage, pCur->pgnoRoot, 0); if( rc!=SQLITE_OK ){ goto create_cursor_exception; } pCur->pBt = pBt; pCur->idx = 0; pCur->pNext = pBt->pCursor; if( pCur->pNext ){ pCur->pNext->pPrev = pCur; } pCur->pPrev = 0; pBt->pCursor = pCur; *ppCur = pCur; return SQLITE_OK; create_cursor_exception: *ppCur = 0; if( pCur ){ if( pCur->pPage ) sqlitepager_unref(pCur->pPage); |
︙ | ︙ | |||
798 799 800 801 802 803 804 | sqliteFree(pCur); } /* ** Make a temporary cursor by filling in the fields of pTempCur. ** The temporary cursor is not on the cursor list for the Btree. */ | | | | 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 | sqliteFree(pCur); } /* ** Make a temporary cursor by filling in the fields of pTempCur. ** The temporary cursor is not on the cursor list for the Btree. */ static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ memcpy(pTempCur, pCur, sizeof(*pCur)); pTempCur->pNext = 0; pTempCur->pPrev = 0; sqlitepager_ref(pTempCur->pPage); } /* ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() ** function above. */ static void releaseTempCursor(BtCursor *pCur){ sqlitepager_unref(pCur->pPage); } /* ** Set *pSize to the number of bytes of key in the entry the ** cursor currently points to. Always return SQLITE_OK. ** Failure is not possible. If the cursor is not currently |
︙ | ︙ | |||
871 872 873 874 875 876 877 | } while( amt>0 && nextPage ){ OverflowPage *pOvfl; rc = sqlitepager_get(pCur->pBt->pPager, nextPage, &pOvfl); if( rc!=0 ){ return rc; } | | | 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 | } while( amt>0 && nextPage ){ OverflowPage *pOvfl; rc = sqlitepager_get(pCur->pBt->pPager, nextPage, &pOvfl); if( rc!=0 ){ return rc; } nextPage = pOvfl->iNext; if( offset<OVERFLOW_SIZE ){ int a = amt; if( a + offset > OVERFLOW_SIZE ){ a = OVERFLOW_SIZE - offset; } memcpy(zBuf, &pOvfl->aPayload[offset], a); amt -= a; |
︙ | ︙ | |||
1005 1006 1007 1008 1009 1010 1011 | if( nextPage==0 ){ return SQLITE_CORRUPT; } rc = sqlitepager_get(pCur->pBt->pPager, nextPage, &pOvfl); if( rc ){ return rc; } | | | 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 | if( nextPage==0 ){ return SQLITE_CORRUPT; } rc = sqlitepager_get(pCur->pBt->pPager, nextPage, &pOvfl); if( rc ){ return rc; } nextPage = pOvfl->iNext; n = nKey; if( n>OVERFLOW_SIZE ){ n = OVERFLOW_SIZE; } c = memcmp(pOvfl->aPayload, pKey, n); sqlitepager_unref(pOvfl); if( c!=0 ){ |
︙ | ︙ | |||
1151 1152 1153 1154 1155 1156 1157 | lwr = pCur->idx+1; }else{ upr = pCur->idx-1; } } assert( lwr==upr+1 ); if( lwr>=pPage->nCell ){ | | | 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 | lwr = pCur->idx+1; }else{ upr = pCur->idx-1; } } assert( lwr==upr+1 ); if( lwr>=pPage->nCell ){ chldPg = pPage->u.hdr.rightChild; }else{ chldPg = pPage->apCell[lwr]->h.leftChild; } if( chldPg==0 ){ pCur->iMatch = c; if( pRes ) *pRes = c; return SQLITE_OK; |
︙ | ︙ | |||
1181 1182 1183 1184 1185 1186 1187 | if( pCur->bSkipNext ){ pCur->bSkipNext = 0; if( pRes ) *pRes = 0; return SQLITE_OK; } pCur->idx++; if( pCur->idx>=pCur->pPage->nCell ){ | | | | 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 | if( pCur->bSkipNext ){ pCur->bSkipNext = 0; if( pRes ) *pRes = 0; return SQLITE_OK; } pCur->idx++; if( pCur->idx>=pCur->pPage->nCell ){ if( pPage->u.hdr.rightChild ){ rc = moveToChild(pCur, pPage->u.hdr.rightChild); if( rc ) return rc; rc = moveToLeftmost(pCur); if( rc ) return rc; if( pRes ) *pRes = 0; return SQLITE_OK; } do{ |
︙ | ︙ | |||
1232 1233 1234 1235 1236 1237 1238 | rc = sqlitepager_get(pBt->pPager, pPage1->freeList, &pOvfl); if( rc ) return rc; rc = sqlitepager_write(pOvfl); if( rc ){ sqlitepager_unref(pOvfl); return rc; } | | | 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 | rc = sqlitepager_get(pBt->pPager, pPage1->freeList, &pOvfl); if( rc ) return rc; rc = sqlitepager_write(pOvfl); if( rc ){ sqlitepager_unref(pOvfl); return rc; } pPage1->freeList = pOvfl->iNext; *ppPage = (MemPage*)pOvfl; }else{ *pPgno = sqlitepager_pagecount(pBt->pPager); rc = sqlitepager_get(pBt->pPager, *pPgno, ppPage); if( rc ) return rc; rc = sqlitepager_write(*ppPage); } |
︙ | ︙ | |||
1275 1276 1277 1278 1279 1280 1281 | needOvflUnref = 1; } rc = sqlitepager_write(pOvfl); if( rc ){ if( needOvflUnref ) sqlitepager_unref(pOvfl); return rc; } | | | 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 | needOvflUnref = 1; } rc = sqlitepager_write(pOvfl); if( rc ){ if( needOvflUnref ) sqlitepager_unref(pOvfl); return rc; } pOvfl->iNext = pPage1->freeList; pPage1->freeList = pgno; memset(pOvfl->aPayload, 0, OVERFLOW_SIZE); pPage->isInit = 0; assert( pPage->pParent==0 ); rc = sqlitepager_unref(pOvfl); return rc; } |
︙ | ︙ | |||
1302 1303 1304 1305 1306 1307 1308 | return SQLITE_OK; } ovfl = pCell->ovfl; pCell->ovfl = 0; while( ovfl ){ rc = sqlitepager_get(pPager, ovfl, &pOvfl); if( rc ) return rc; | | | 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 | return SQLITE_OK; } ovfl = pCell->ovfl; pCell->ovfl = 0; while( ovfl ){ rc = sqlitepager_get(pPager, ovfl, &pOvfl); if( rc ) return rc; nextOvfl = pOvfl->iNext; rc = freePage(pBt, pOvfl, ovfl); if( rc ) return rc; ovfl = nextOvfl; sqlitepager_unref(pOvfl); } return SQLITE_OK; } |
︙ | ︙ | |||
1350 1351 1352 1353 1354 1355 1356 | if( rc ){ *pNext = 0; clearCell(pBt, pCell); return rc; } spaceLeft = OVERFLOW_SIZE; pSpace = pOvfl->aPayload; | | | 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 | if( rc ){ *pNext = 0; clearCell(pBt, pCell); return rc; } spaceLeft = OVERFLOW_SIZE; pSpace = pOvfl->aPayload; pNextPg = &pOvfl->iNext; } n = nPayload; if( n>spaceLeft ) n = spaceLeft; memcpy(pSpace, pPayload, n); nPayload -= n; if( nPayload==0 && pData ){ pPayload = pData; |
︙ | ︙ | |||
1399 1400 1401 1402 1403 1404 1405 | ** another. */ static void reparentChildPages(Pager *pPager, Page *pPage){ int i; for(i=0; i<pPage->nCell; i++){ reparentPage(pPager, pPage->apCell[i]->leftChild, pPage); } | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > > > > > > > > > | | > > > | | | > > > > > > > > | > | > | > | | > | < < < < | > | > | > > | | > > | | > > | | > | < | < > | | < < < < < | > | > | | | > | | < > > > > > | > > > > > > | | | | < | | | | < < < < | > > > | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > | > > | > > > > | > > > > | > > | | | > | | > | < < < | < < | < < < | < < | < < < < < < < | < < < < < < | < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < > | | < < < < < < | < | < > > > < < < < | < < < < > | < < | < < < < < < > > | < > < > | > | | < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < | < | | < < | < < < < | < < < | < < | < < < | < < | < < | < < < < < < < | < > | < < < < < < < < < < | < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < | > | < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < | < < < | < < < < < < < < | > | > | > | | < < < < | < < < < < < < < < < < | < < < < < < < < < < < < < < < | < < < < < < < < < | < < < < | | < | < < | < < < < < < < < < < < < < | < < < < < < | 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 | ** another. */ static void reparentChildPages(Pager *pPager, Page *pPage){ int i; for(i=0; i<pPage->nCell; i++){ reparentPage(pPager, pPage->apCell[i]->leftChild, pPage); } reparentPage(pPager, pPage->u.hdr.rightChild, pPage); } /* ** Remove the i-th cell from pPage. This routine effects pPage only. ** The cell content is not freed or deallocated. It is assumed that ** the cell content has been copied someplace else. This routine just ** removes the reference to the cell from pPage. ** ** "sz" must be the number of bytes in the cell. ** ** Do not bother maintaining the integrity of the linked list of Cells. ** Only pPage->apCell[] is important. The relinkCellList() routine ** will be called soon after this routine in order to rebuild the ** linked list. */ static void dropCell(MemPage *pPage, int i, int sz){ int j; assert( i>=0 && i<pPage->nCell ); assert( sz==cellSize(pPage->apCell[i]); freeSpace(pPage, idx, sz); for(j=i, j<pPage->nCell-2; j++){ pPage->apCell[j] = pPage->apCell[j+1]; } pPage->nCell--; } /* ** Insert a new cell on pPage at cell index "i". pCell points to the ** content of the cell. ** ** If the cell content will fit on the page, then put it there. If it ** will not fit, then just make pPage->apCell[i] point to the content ** and set pPage->isOverfull. ** ** Do not bother maintaining the integrity of the linked list of Cells. ** Only pPage->apCell[] is important. The relinkCellList() routine ** will be called soon after this routine in order to rebuild the ** linked list. */ static void insertCell(MemPage *pPage, int i, Cell *pCell, int sz){ int idx, j; assert( i>=0 && i<=pPage->nCell ); assert( sz==cellSize(pCell) ); for(j=pPage->nCell; j>i; j--){ pPage->apCell[j] = pPage->apCell[j-1]; } pPage->nCell++; idx = allocateSpace(pPage, sz); if( idx<=0 ){ pPage->isOverfull = 1; pPage->apCell[i] = pCell; }else{ memcpy(&pPage->u.aDisk[idx], pCell, sz); pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx]); } } /* ** Rebuild the linked list of cells on a page so that the cells ** occur in the order specified by pPage->apCell[]. Invoke this ** routine once to repair damage after one or more invocations ** of either insertCell() or dropCell(). */ static void relinkCellList(MemPage *pPage){ int i; u16 *pIdx; pIdx = &pPage->u.hdr.firstCell; for(i=0; i<pPage->nCell; i++){ int idx = ((uptr)pPage->apCell[i]) - (uptr)pPage; *pIdx = idx; pIdx = &pPage->apCell[i]->h.iNext; } *pIdx = 0; } /* ** Make a copy of the contents of pFrom into pTo. The pFrom->apCell[] ** pointers that point intto pFrom->u.aDisk[] must be adjusted to point ** intto pTo->u.aDisk[] instead. But some pFrom->apCell[] entries might ** not point to pFrom->u.aDisk[]. Those are unchanged. */ static void copyPage(MemPage *pTo, MemPage *pFrom){ uptr from, to; int i; memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_PAGE_SIZE); pTo->pParent = pFrom->pParent; pTo->isInit = 1; pTo->nCell = pFrom->nCell; pTo->nFree = pFrom->nFree; pTo->isOverfull = pFrom->isOverfull; to = (unsigned int)pTo; from = (unsigned int)pFrom; for(i=0; i<pTo->nCell; i++){ uptr addr = (uptr)(pFrom->apCell[i]); if( addr>from && addr<from+SQLITE_PAGE_SIZE ){ *((uptr*)&pTo->apCell[i]) = addr + to - from; } } } /* ** This routine redistributes Cells on pPage and up to two siblings ** of pPage so that all pages have about the same amount of free space. ** Usually one sibling on either side of pPage is used in the balancing, ** though both siblings might come from one side if pPage is the first ** or last child of its parent. If pPage has fewer than two siblings ** (something which can only happen if pPage is the root page or a ** child of root) then all available siblings participate in the balancing. ** ** The number of siblings of pPage might be increased or decreased by ** one in order to keep all pages between 2/3 and completely full. If ** pPage is the root page, then the depth of the tree might be increased ** or decreased by one, as necessary, to keep the root page from being ** overfull or empty. ** ** This routine calls relinkCellList() on its input page regardless of ** whether or not it does any real balancing. Client routines will typically ** invoke insertCell() or dropCell() before calling this routine, so we ** need to call relinkCellList() to clean up the mess that those other ** routines left behind. ** ** pCur is left pointing to the same cell as when this routine was called ** event if that cell gets moved to a different page. pCur may be NULL. ** ** Note that when this routine is called, some of the Cells on pPage ** might not actually be stored in pPage->u.aDisk[]. This can happen ** if the page is overfull. Part of the job of this routine is to ** make sure all Cells for pPage once again fit in pPage->u.aDisk[]. ** ** If this routine fails for any reason, it means the database may have ** been left in a corrupted state and should be rolled back. */ static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){ MemPage *pParent; /* The parent of pPage */ MemPage *apOld[3]; /* pPage and up to two siblings */ Pgno pgnoOld[3]; /* Page numbers for each page in apOld[] */ MemPage *apNew[4]; /* pPage and up to 3 siblings after balancing */ Pgno pgnoNew[4]; /* Page numbers for each page in apNew[] */ int idxDiv[3]; /* Indices of divider cells in pParent */ Cell *apDiv[3]; /* Divider cells in pParent */ int nCell; /* Number of cells in apCell[] */ int nOld; /* Number of pages in apOld[] */ int nNew; /* Number of pages in apNew[] */ int perPage; /* Approximate number of bytes per page */ int nDiv; /* Number of cells in apDiv[] */ int i, j, k; /* Loop counters */ int idx; /* Index of pPage in pParent->apCell[] */ int nxDiv; /* Next divider slot in pParent->apCell[] */ int rc; /* The return code */ int iCur; /* apCell[iCur] is the cell of the cursor */ int usedPerPage; /* Memory needed for each page */ int freePerPage; /* Average free space per page */ Cell *apCell[MX_CELL*3+5]; /* All cells from pages being balanceed */ int szCell[MX_CELL*3+5]; /* Local size of all cells */ Cell aTemp[2]; /* Temporary holding area for apDiv[] */ MemPage aOld[3]; /* Temporary copies of pPage and its siblings */ /* ** Return without doing any work if pPage is neither overfull nor ** underfull. */ if( !pPage->isOverfull && pPage->nFree<SQLITE_PAGE_SIZE/2 ){ relinkCellList(pPage); return SQLITE_OK; } /* ** Find the parent of the page to be balanceed. ** If there is no parent, it means this page is the root page and ** special rules apply. */ pParent = pPage->pParent; if( pParent==0 ){ Pgno pgnoChild; Page *pChild; if( pPage->nCell==0 ){ if( pPage->u.hdr.rightChild ){ /* ** The root page is empty. Copy the one child page ** into the root page and return. This reduces the depth ** of the BTree by one. */ rc = sqlitepager_write(pPage); if( rc ) return rc; pgnoChild = pPage->u.hdr.rightChild; rc = sqlitepager_get(pBt, pgnoChild, &pChild); if( rc ) return rc; memcpy(pPage, pChild, SQLITE_PAGE_SIZE); pPage->isInit = 0; initPage(pPage, sqlitepager_pagenumber(pPage), 0); reparentChildPages(pBt->pPager, pPage); freePage(pBt, pChild, pgnoChild); sqlitepager_unref(pChild); } return SQLITE_OK; } if( !pPage->isOverfull ){ /* It is OK for the root page to be less than half full. */ relinkCellList(pPage); return SQLITE_OK; } /* ** If we get to here, it means the root page is overfull. ** When this happens, Create a new child page and copy the ** contents of the root into the child. Then make the root ** page an empty page with rightChild pointing to the new ** child. Then fall thru to the code below which will cause ** the overfull child page to be split. */ rc = sqlitepager_write(pPage); if( rc ) return rc; rc = allocatePage(pBt, &pChild, &pgnoChild); if( rc ) return rc; copyPage(pChild, pPage); pChild->pParent = pPage; pChild->isOverfull = 1; if( pCur ){ sqlitepager_ref(pChild); sqlitepager_unref(pCur->pPage); pCur->pPage = pChild; } zeroPage(pPage); pPage->u.hdr.rightChild = pgnoChild; pParent = pPage; pPage = pChild; }else{ rc = sqlitepager_write(pPage); if( rc ) return rc; } /* ** Find the Cell in the parent page whose h.leftChild points back ** to pPage. The "idx" variable is the index of that cell. If pPage ** is the rightmost child of pParent then set idx to pParent->nCell */ idx = -1; pgno = sqlitepager_pagenumber(pPage); for(i=0; i<pParent->nCell; i++){ if( pParent->apCell[i]->h.leftChild==pgno ){ idx = i; break; } } if( idx<0 && pPage->u.hdr.rightChild==pgno ){ idx = pPage->nCell; } if( idx<0 ){ return SQLITE_CORRUPT; } /* ** Initialize variables so that it will be safe to jump ** directory to balance_cleanup at any moment. */ nOld = nNew = 0; sqlitepager_ref(pParent); /* ** Find sibling pages to pPage and the Cells in pParent that divide ** the siblings. An attempt is made to find one sibling on either ** side of pPage. Both siblings are taken from one side, however, if ** pPage is either the first or last child of its parent. If pParent ** has 3 or fewer children then all children of pParent are taken. */ if( idx==pParent->nCell ){ nxDiv = idx - 2; }else{ nxDiv = idx - 1; } if( nxDiv<0 ) nxDiv = 0; nDiv = 0; for(i=0, k=nxDiv; i<3; i++, k++){ if( k<pParent->nCell ){ idxDiv[i] = k; apDiv[i] = pParent->apCell[k]; nDiv++; pgnoOld[i] = apDiv[i]->h.leftChild; }else if( k==pParent->nCell ){ pgnoOld[i] = pParent->rightChild; }else{ break; } rc = sqlitepager_get(pBt, pgnoOld[i], &apOld[i]); if( rc ) goto balance_cleanup; nOld++; } /* ** Set iCur to be the index in apCell[] of the cell that the cursor ** is pointing to. We will need this later on in order to keep the ** cursor pointing at the same cell. */ if( pCur ){ iCur = pCur->idx; for(i=0; idxDiv[i]<idx; i++){ iCur += apOld[i]->nCell + 1; } sqlitepager_unref(pCur->pPage); pCur->pPage = 0; } /* ** Make copies of the content of pPage and its siblings into aOld[]. ** The rest of this function will use data from the copies rather ** that the original pages since the original pages will be in the ** process of being overwritten. */ for(i=0; i<nOld; i++){ copyPage(&aOld[i], apOld[i]); rc = freePage(pBt, apOld[i], pgnoOld[i]); if( rc ) goto balance_cleanup; apOld[i] = &aOld[i]; } /* ** Load pointers to all cells on sibling pages and the divider cells ** into the local apCell[] array. Make copies of the divider cells ** into aTemp[] and remove the the divider Cells from pParent. */ nCell = 0; for(i=0; i<nOld; i++){ MemPage *pOld = apOld[i]; for(j=0; j<pOld->nCell; j++){ apCell[nCell] = pOld->apCell[j]; szCell[nCell] = cellSize(apCell[nCell]); nCell++; } if( i<nOld-1 ){ szCell[nCell] = cellSize(apDiv[i]); memcpy(aTemp[i], apDiv[i], szCell[nCell]); apCell[nCell] = &aTemp[i]; dropCell(pParent, nxDiv, szCell[nCell]); assert( apCell[nCell]->h.leftChild==pgnoOld[i] ); apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild; nCell++; } } /* ** Estimate the number of pages needed. Record this number in "k" ** for now. It will get transferred to nNew as we allocate the ** new pages. */ totalSize = 0; for(i=0; i<nCell; i++){ totalSize += szCell[i]; } k = (totalSize + (SQLITE_PAGE_SIZE - sizeof(PageHdr) - 1)) / (SQLITE_PAGE_SIZE - sizeof(PageHdr)); usedPerPage = (totalSize+k-1)/k; freePerPage = SQLITE_PAGE_SIZE - usedPerPage; /* ** Allocate new pages */ for(i=0; i<k; i++){ rc = allocatePage(pBt, &apNew[i], &pgnoNew[i]); if( rc ) goto balance_cleanup; nNew++; zeroPage(apNew[i]); } /* ** Evenly distribute the data in apCell[] across the new pages. ** Insert divider cells into pParent as necessary. */ j = 0; for(i=0; i<nNew; i++){ MemPage *pNew = apNew[i]; while( j<nCell && pNew->nFree<freePerPage && szCell[j]<=pNew->nFree ){ if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; } insertCell(pNew, pNew->nCell, apCell[j], szCell[j]); j++; } assert( !pNew->isOverfull ); relinkCellList(pNew); if( i<nNew-1 && j<nCell ){ pNew->u.hdr.rightChild = apCell[j]->h.leftChild; apCell[j]->h.leftChild = pgnoNew[i]; if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; } insertCell(pParent, nxDiv, apCell[j], szCell[j]); j++; nxDiv++; } } apNew[nNew-1]->u.hdr.rightChild = apOld[nOld-1]->u.hdr.rightChild; if( nxDiv==pParent->nCell ){ pParent->u.hdr.rightChild = pgnoNew[nNew-1]; }else{ pParent->apCell[nxDiv]->h.leftChild = pgnoNew[nNew-1]; } if( pCur ){ assert( pCur->pPage!=0 ); sqlitepager_ref(pCur->pPage); } /* ** Reparent children of all cells. */ for(i=0; i<nNew; i++){ reparentChildPages(pBt->pPager, apNew[i]); } reparentChildPages(pBt->pPager, pParent); /* ** balance the parent page. */ rc = balance(pBt, pParent, 0); /* ** Cleanup before returning. */ balance_cleanup: for(i=0; i<nOld; i++){ sqlitepager_unref(apOld[i]); } for(i=0; i<nNew; i++){ sqlitepager_unref(apNew[i]); } if( pCur && pCur->pPage==0 ){ pCur->pPage = pParent; pCur->idx = 0; }else{ sqlitepager_unref(pParent); } return rc; } /* ** Insert a new record into the BTree. The key is given by (pKey,nKey) ** and the data is given by (pData,nData). The cursor is used only to ** define what database the record should be inserted into. The cursor ** is left pointing at the new record. */ int sqliteBtreeInsert( BtCursor *pCur, /* Insert data into the table of this cursor */ void *pKey, int nKey, /* The key of the new record */ void *pData, int nData /* The data of the new record */ ){ Cell newCell; int rc; int loc; int szNew; MemPage *pPage; Btree *pBt = pCur->pBt; if( !pCur->pBt->inTrans ){ return SQLITE_ERROR; /* Must start a transaction first */ } rc = sqliteBtreeMoveto(pCur, pKey, nKey, &loc); if( rc ) return rc; pPage = pCur->pPage; rc = sqlitepager_write(pPage); if( rc ) return rc; rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData); if( rc ) return rc; szNew = cellSize(&newCell); if( loc==0 ){ newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild; rc = clearCell(pBt, pPage->apCell[pCur->idx]); if( rc ) return rc; dropCell(pPage, pCur->idx, cellSize(pPage->apCell[pCur->idx])); }else if( loc>0 ){ assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */ pCur->idx++; }else{ assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */ } insertCell(pPage, pCur->idx, &newCell, cellSize(&newCell)); rc = balance(pCur->pBt, pPage, pCur); return rc; } /* ** Delete the entry that the cursor is pointing to. ** ** The cursor is left pointing at either the next or the previous |
︙ | ︙ | |||
2093 2094 2095 2096 2097 2098 2099 | } if( pCur->idx >= pPage->nCell ){ return SQLITE_ERROR; /* The cursor is not pointing to anything */ } rc = sqlitepager_write(pPage); if( rc ) return rc; pCell = pPage->apCell[pCur->idx]; | > > > | > | > | < | < < | > > | | | < < < < < | | > > | > | < < > > > > | < | 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 | } if( pCur->idx >= pPage->nCell ){ return SQLITE_ERROR; /* The cursor is not pointing to anything */ } rc = sqlitepager_write(pPage); if( rc ) return rc; pCell = pPage->apCell[pCur->idx]; pgnoChild = pCell->h.leftChild; clearCell(pCell); dropCell(pPage, pCur->idx, cellSize(pCell)); if( pgnoChild ){ /* ** If the entry we just deleted is not a leaf, then we've left a ** whole in an internal page. We have to fill the whole by moving ** in a page from a leaf. The next Cell after the one just deleted ** is guaranteed to exist and to be a leaf so we can use it. */ BtCursor leafCur; Cell *pNext; int szNext; getTempCursor(pCur, &leafCur); rc = sqliteBtreeNext(&leafCur, 0); if( rc!=SQLITE_OK ){ return SQLITE_CORRUPT; } pNext = leafCur.pPage->apCell[leafCur.idx] szNext = cellSize(pNext); insertCell(pPage, pCur->idx, pNext, szNext); rc = balance(pCur->pBt, pPage, pCur); if( rc ) return rc; pCur->bSkipNext = 1; dropCell(leafCur.pPage, leafCur.idx, szNext); rc = balance(pCur->pBt, leafCur.pPage, 0); releaseTempCur(&leafCur); }else{ rc = balance(pCur->pBt, pPage, pCur); pCur->bSkipNext = 1; } return rc; } /* ** Create a new BTree in the same file. Write into *piTable the index ** of the root page of the new table. */ |
︙ | ︙ | |||
2157 2158 2159 2160 2161 2162 2163 | int rc; int i; Cell *pCell; int idx; rc = sqlitepager_get(pBt->pPager, pgno, &pPage); if( rc ) return rc; | | | | 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 | int rc; int i; Cell *pCell; int idx; rc = sqlitepager_get(pBt->pPager, pgno, &pPage); if( rc ) return rc; idx = pPage->u.hdr.firstCell; while( idx>0 ){ pCell = (Cell*)&pPage->u.aDisk[idx]; idx = pCell->h.iNext; if( pCell->h.leftChild ){ rc = clearDatabasePage(pBt, pCell->h.leftChild); if( rc ) return rc; } rc = clearCell(pCell); if( rc ) return rc; |
︙ | ︙ |