Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Further btree updates. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
6548088566cbe39d84b12f42be18d18b |
User & Date: | dan 2013-09-25 18:46:13.104 |
Context
2013-09-25
| ||
19:40 | Add a bugfix for b-tree deletes. check-in: 88c8fd489d user: dan tags: trunk | |
18:46 | Further btree updates. check-in: 6548088566 user: dan tags: trunk | |
2013-09-24
| ||
20:39 | Further progress on b-treee backend. check-in: c954958697 user: dan tags: trunk | |
Changes
Changes to src/bt_main.c.
︙ | ︙ | |||
152 153 154 155 156 157 158 | }else{ btCsrSetup(db, pCsr); } return rc; } | < < < < < < < < > > > > > > > > > > > > < < > > > > > > > > | 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 | }else{ btCsrSetup(db, pCsr); } return rc; } static void btCsrReset(bt_cursor *pCsr){ int i; for(i=0; i<pCsr->nPg; i++){ sqlite4BtPageRelease(pCsr->apPage[i]); } pCsr->nPg = 0; } int sqlite4BtCsrClose(bt_cursor *pCsr){ if( pCsr ){ btCsrReset(pCsr); sqlite4_free(pCsr->pDb->pEnv, pCsr); } return SQLITE4_OK; } void *sqlite4BtCsrExtra(bt_cursor *pCsr){ return (void*)&pCsr[1]; } /* ** Set pCsr->apPage[pCsr->nPg] to a reference to database page pgno. */ static int btCsrDescend(bt_cursor *pCsr, u32 pgno){ int rc; if( pCsr->nPg>=BT_MAX_DEPTH ){ rc = btErrorBkpt(SQLITE4_CORRUPT); }else{ rc = sqlite4BtPageGet(pCsr->pDb->pPager, pgno, &pCsr->apPage[pCsr->nPg]); if( rc==SQLITE4_OK ){ pCsr->nPg++; } } return rc; } static int btCsrAscend(bt_cursor *pCsr){ if( pCsr->nPg>0 ){ pCsr->nPg--; sqlite4BtPageRelease(pCsr->apPage[pCsr->nPg]); } return (pCsr->nPg==0 ? SQLITE4_NOTFOUND : SQLITE4_OK); } static int btCellCount(const u8 *aData, int nData){ return (int)btGetU16(&aData[nData-2]); } static int btFreeOffset(const u8 *aData, int nData){ return (int)btGetU16(&aData[nData-4]); |
︙ | ︙ | |||
211 212 213 214 215 216 217 218 219 220 221 222 223 224 | ** Return a pointer to the big-endian u16 field that contains the ** pointer to cell iCell. */ static u8* btCellPtrFind(u8 *aData, int nData, int iCell){ return &aData[nData - 4 - iCell*2 - 2]; } /* ** This function seeks the cursor as required for either sqlite4BtCsrFirst() ** (if parameter bLast is false) or sqlite4BtCsrLast() (if bLast is true). */ static int btCsrEnd(bt_cursor *pCsr, int bLast){ const int pgsz = sqlite4BtPagerPagesize(pCsr->pDb->pPager); | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | ** Return a pointer to the big-endian u16 field that contains the ** pointer to cell iCell. */ static u8* btCellPtrFind(u8 *aData, int nData, int iCell){ return &aData[nData - 4 - iCell*2 - 2]; } /* ** Parameters aData and nData describe a buffer containing an internal ** b-tree node. The page number of the iCell'th leftmost child page ** is returned. */ static u32 btChildPgno(u8 *aData, int nData, int iCell){ u32 pgno; /* Return value */ int nCell = btCellCount(aData, nData); if( iCell>=nCell ){ pgno = btGetU32(&aData[1]); }else{ int nKey; u8 *pCell = btCellFind(aData, nData, iCell); pCell += sqlite4BtVarintGet32(pCell, &nKey); pCell += nKey; sqlite4BtVarintGet32(pCell, (int*)&pgno); } return pgno; } #ifndef NDEBUG static void printPage(u8 *aData, int nData){ int i; int nCell = (int)btCellCount(aData, nData); printf("nCell=%d\n", nCell); printf("iFree=%d\n", (int)btFreeOffset(aData, nData)); printf("flags=%d\n", (int)btFlags(aData, nData)); printf("cell offsets:"); for(i=0; i<nCell; i++){ printf(" %d", btCellFind(aData, nData, i) - aData); } printf("\n"); } #endif /* ** This function seeks the cursor as required for either sqlite4BtCsrFirst() ** (if parameter bLast is false) or sqlite4BtCsrLast() (if bLast is true). */ static int btCsrEnd(bt_cursor *pCsr, int bLast){ const int pgsz = sqlite4BtPagerPagesize(pCsr->pDb->pPager); |
︙ | ︙ | |||
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | int iHi; /* pK/nK is <= than cell iHi */ int iLo; /* pK/nK is > than cell (iLo-1) */ int res; /* Result of comparison */ u8 *aData = (u8*)sqlite4BtPageData(pCsr->apPage[pCsr->nPg-1]); iLo = 0; iHi = nCell = btCellCount(aData, pgsz); while( iHi>iLo ){ int iTst = (iHi+iLo)/2; /* Cell to compare to pK/nK */ rc = btCellKeyCompare(pCsr, aData, pgsz, iTst, pK, nK, &res); if( rc!=SQLITE4_OK || res==0 ){ /* Cell iTst is EQUAL to pK/nK */ iHi = iLo = iTst; }else if( res<0 ){ /* Cell iTst is SMALLER than pK/nK */ iLo = iTst+1; }else{ /* Cell iTst is LARGER than pK/nK */ iHi = iTst; } } | > > > > | > > > > > > > > > | > | 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 | int iHi; /* pK/nK is <= than cell iHi */ int iLo; /* pK/nK is > than cell (iLo-1) */ int res; /* Result of comparison */ u8 *aData = (u8*)sqlite4BtPageData(pCsr->apPage[pCsr->nPg-1]); iLo = 0; iHi = nCell = btCellCount(aData, pgsz); if( nCell==0 ){ rc = SQLITE4_NOTFOUND; break; } while( iHi>iLo ){ int iTst = (iHi+iLo)/2; /* Cell to compare to pK/nK */ rc = btCellKeyCompare(pCsr, aData, pgsz, iTst, pK, nK, &res); if( rc!=SQLITE4_OK || res==0 ){ /* Cell iTst is EQUAL to pK/nK */ iHi = iLo = iTst; }else if( res<0 ){ /* Cell iTst is SMALLER than pK/nK */ iLo = iTst+1; }else{ /* Cell iTst is LARGER than pK/nK */ iHi = iTst; } } if( rc!=SQLITE4_OK ) break; assert( iHi==iLo ); pCsr->aiCell[pCsr->nPg-1] = iHi; if( aData[0] & BT_PGFLAGS_INTERNAL ){ if( iHi==nCell ){ pgno = btGetU32(&aData[1]); }else{ u8 *pCell; int nByte; pCell = btCellFind(aData, pgsz, 0); pCell += sqlite4BtVarintGet32(pCell, &nByte); pCell += nByte; sqlite4BtVarintGet32(pCell, (int*)&pgno); } }else{ pgno = 0; if( res!=0 ){ assert( BT_SEEK_LEFAST<0 && BT_SEEK_LE<0 ); if( eSeek<0 ){ rc = sqlite4BtCsrPrev(pCsr); if( rc==SQLITE4_OK ) rc = SQLITE4_INEXACT; }else if( eSeek==BT_SEEK_EQ || iHi==nCell ){ rc = SQLITE4_NOTFOUND; }else{ rc = SQLITE4_INEXACT; } } } } } return rc; } |
︙ | ︙ | |||
379 380 381 382 383 384 385 386 387 388 389 390 | /* ** Position cursor pCsr to point to the largest key in the database. */ int sqlite4BtCsrLast(bt_cursor *pCsr){ return btCsrEnd(pCsr, 1); } /* ** Advance to the next entry in the tree. */ int sqlite4BtCsrNext(bt_cursor *pCsr){ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < | > > > < | > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > | 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 | /* ** Position cursor pCsr to point to the largest key in the database. */ int sqlite4BtCsrLast(bt_cursor *pCsr){ return btCsrEnd(pCsr, 1); } /* ** This function does the work of both sqlite4BtCsrNext() (if parameter ** bNext is true) and Pref() (if bNext is false). */ static int btCsrStep(bt_cursor *pCsr, int bNext){ const int pgsz = sqlite4BtPagerPagesize(pCsr->pDb->pPager); int rc = SQLITE4_OK; int bRequireDescent = 0; while( rc==SQLITE4_OK ){ int iPg = pCsr->nPg-1; int iCell = pCsr->aiCell[iPg]; if( bNext ){ u8 *aData = (u8*)sqlite4BtPageData(pCsr->apPage[iPg]); int nCell = btCellCount(aData, pgsz); assert( bRequireDescent==0 || bRequireDescent==1 ); if( iCell<(nCell+bRequireDescent-1) ){ pCsr->aiCell[iPg]++; break; } }else{ if( pCsr->aiCell[iPg]>0 ){ pCsr->aiCell[iPg]--; break; } } rc = btCsrAscend(pCsr); bRequireDescent = 1; } if( bRequireDescent && rc==SQLITE4_OK ){ u32 pgno; /* Child page number */ u8 *aData = (u8*)sqlite4BtPageData(pCsr->apPage[pCsr->nPg-1]); pgno = btChildPgno(aData, pgsz, pCsr->aiCell[pCsr->nPg-1]); while( 1 ){ rc = btCsrDescend(pCsr, pgno); if( rc!=SQLITE4_OK ){ break; }else{ int nCell; aData = (u8*)sqlite4BtPageData(pCsr->apPage[pCsr->nPg-1]); nCell = btCellCount(aData, pgsz); if( btFlags(aData, pgsz) & BT_PGFLAGS_INTERNAL ){ pCsr->aiCell[pCsr->nPg-1] = (bNext ? 0 : nCell); pgno = btChildPgno(aData, pgsz, pCsr->aiCell[pCsr->nPg-1]); }else{ pCsr->aiCell[pCsr->nPg-1] = (bNext ? 0 : nCell-1); break; } } } } return rc; } /* ** Advance to the next entry in the tree. */ int sqlite4BtCsrNext(bt_cursor *pCsr){ return btCsrStep(pCsr, 1); } /* ** Retreat to the previous entry in the tree. */ int sqlite4BtCsrPrev(bt_cursor *pCsr){ return btCsrStep(pCsr, 0); } int sqlite4BtCsrValid(bt_cursor *pCsr){ assert( 0 ); return 0; } int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK){ const int pgsz = sqlite4BtPagerPagesize(pCsr->pDb->pPager); u8 *aData; u8 *pCell; int iCell = pCsr->aiCell[pCsr->nPg-1]; int nK; aData = (u8*)sqlite4BtPageData(pCsr->apPage[pCsr->nPg-1]); pCell = btCellFind(aData, pgsz, iCell); pCell += sqlite4BtVarintGet32(pCell, &nK); if( nK==0 ){ assert( 0 ); }else{ *ppK = pCell; *pnK = nK; } return SQLITE4_OK; } int sqlite4BtCsrData( bt_cursor *pCsr, /* Cursor handle */ int iOffset, /* Offset of requested data */ int nByte, /* Bytes requested (or -ve for all avail.) */ const void **ppV, /* OUT: Pointer to data buffer */ int *pnV /* OUT: Size of data buffer in bytes */ ){ const int pgsz = sqlite4BtPagerPagesize(pCsr->pDb->pPager); u8 *aData; u8 *pCell; int iCell = pCsr->aiCell[pCsr->nPg-1]; int nK; int nV; aData = (u8*)sqlite4BtPageData(pCsr->apPage[pCsr->nPg-1]); pCell = btCellFind(aData, pgsz, iCell); pCell += sqlite4BtVarintGet32(pCell, &nK); assert( nK!=0 ); pCell += nK; pCell += sqlite4BtVarintGet32(pCell, &nV); assert( nV!=0 ); *ppV = pCell; *pnV = nV; return SQLITE4_OK; } static int btInsertIntoLeaf( bt_cursor *pCsr, const void *pK, int nK, const void *pV, int nV |
︙ | ︙ | |||
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 | u8 *aFrom = &aData[pgsz-4-iCell*2]; memmove(aFrom, &aFrom[-2], (nCell-iCell) * 2); } btPutU16(btCellPtrFind(aData, pgsz, iCell), iWrite); /* Write the cell itself */ iWrite += sqlite4BtVarintPut32(&aData[iWrite], nK); iWrite += nK; iWrite += sqlite4BtVarintPut32(&aData[iWrite], nV); iWrite += nV; btPutU16(&aData[pgsz-4], iWrite); } }else{ assert( 0 ); } return rc; } /* ** Insert a new key/value pair or replace an existing one. */ int sqlite4BtReplace(bt_db *db, const void *pK, int nK, const void *pV, int nV){ int rc = SQLITE4_OK; bt_cursor csr; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | u8 *aFrom = &aData[pgsz-4-iCell*2]; memmove(aFrom, &aFrom[-2], (nCell-iCell) * 2); } btPutU16(btCellPtrFind(aData, pgsz, iCell), iWrite); /* Write the cell itself */ iWrite += sqlite4BtVarintPut32(&aData[iWrite], nK); memcpy(&aData[iWrite], pK, nK); iWrite += nK; iWrite += sqlite4BtVarintPut32(&aData[iWrite], nV); memcpy(&aData[iWrite], pV, nV); iWrite += nV; btPutU16(&aData[pgsz-4], iWrite); } }else{ assert( 0 ); } return rc; } static int btRemoveFromLeaf(bt_cursor *pCsr){ const int pgsz = sqlite4BtPagerPagesize(pCsr->pDb->pPager); int rc = SQLITE4_OK; /* Return code */ BtPage *pLeaf; /* Leaf page */ pLeaf = pCsr->apPage[pCsr->nPg-1]; rc = sqlite4BtPageWrite(pLeaf); if( rc==SQLITE4_OK ){ u8 *aData; /* Page buffer */ int nCell; /* Number of cells initially on this page */ int iDel; /* Index of cell to delete */ iDel = pCsr->aiCell[pCsr->nPg-1]; aData = (u8*)sqlite4BtPageData(pLeaf); nCell = btCellCount(aData, pgsz); if( iDel<(nCell-1) ){ u8 *aTo = btCellPtrFind(aData, pgsz, nCell-2); u8 *aFrom = btCellPtrFind(aData, pgsz, nCell-1); memmove(aTo, aFrom, 2*(nCell-iDel-1)); } /* Decrease cell count */ btPutU16(&aData[pgsz-2], nCell-1); } return rc; } /* ** Insert a new key/value pair or replace an existing one. */ int sqlite4BtReplace(bt_db *db, const void *pK, int nK, const void *pV, int nV){ int rc = SQLITE4_OK; bt_cursor csr; |
︙ | ︙ | |||
508 509 510 511 512 513 514 | rc = btInsertIntoLeaf(&csr, pK, nK, pV, nV); } return rc; } int sqlite4BtDelete(bt_cursor *pCsr){ | < | < < < < < < < < | 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 | rc = btInsertIntoLeaf(&csr, pK, nK, pV, nV); } return rc; } int sqlite4BtDelete(bt_cursor *pCsr){ return btRemoveFromLeaf(pCsr); } int sqlite4BtSetCookie(bt_db *db, unsigned int iVal){ return sqlite4BtPagerSetCookie(db->pPager, iVal); } int sqlite4BtGetCookie(bt_db *db, unsigned int *piVal){ return sqlite4BtPagerGetCookie(db->pPager, piVal); } |
Changes to src/bt_pager.c.
︙ | ︙ | |||
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | memset(p, 0, nByte); p->pEnv = pEnv; p->pVfs = sqlite4BtEnvDefault(); *pp = p; return SQLITE4_OK; } /* ** Close a pager database handle. */ int sqlite4BtPagerClose(BtPager *p){ if( p->pFd ){ p->pVfs->xClose(p->pFd); } btHashClear(p); sqlite4_free(p->pEnv, p->zFile); sqlite4_free(p->pEnv, p); return SQLITE4_OK; } /* | > > > > > > > > > > > > > > > > > > | 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 | memset(p, 0, nByte); p->pEnv = pEnv; p->pVfs = sqlite4BtEnvDefault(); *pp = p; return SQLITE4_OK; } static void btFreePage(BtPager *p, BtPage *pPg){ if( pPg ){ sqlite4_free(p->pEnv, pPg->aData); sqlite4_free(p->pEnv, pPg); } } /* ** Close a pager database handle. */ int sqlite4BtPagerClose(BtPager *p){ int i; if( p->pFd ){ p->pVfs->xClose(p->pFd); } for(i=0; i<p->hash.nHash; i++){ BtPage *pPg; BtPage *pNext; for(pPg=p->hash.aHash[i]; pPg; pPg=pNext){ pNext = pPg->pNextHash; btFreePage(p, pPg); } } btHashClear(p); sqlite4_free(p->pEnv, p->zFile); sqlite4_free(p->pEnv, p); return SQLITE4_OK; } /* |
︙ | ︙ | |||
310 311 312 313 314 315 316 | pRet = 0; } *ppPg = pRet; return rc; } | < < < < < < < | 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | pRet = 0; } *ppPg = pRet; return rc; } /* ** Roll back the current write transaction. */ static int btRollbackTransaction(BtPager *p){ int rc = SQLITE4_OK; BtPage *pPg; BtPage *pNext; |
︙ | ︙ |
Changes to test/simple3.test.
︙ | ︙ | |||
19 20 21 22 23 24 25 | do_test 1.0 { sqlite4 db file:test.db?kv=bt db close } {} do_test 1.1 { sqlite4 db file:test.db?kv=bt } {} | > | | > > | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | do_test 1.0 { sqlite4 db file:test.db?kv=bt db close } {} do_test 1.1 { sqlite4 db file:test.db?kv=bt } {} do_execsql_test 1.2 { CREATE TABLE t1(a, b) } do_execsql_test 1.3 { SELECT * FROM sqlite_master; } {table t1 t1 2 {CREATE TABLE t1(a, b)}} finish_test |