1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Simplify the b-tree logic by taking advantage of the fact that all b-trees

are either intkey+leafdata or zerodata. (CVS 5431)

FossilOrigin-Name: 29d3bfd7c9a68078385354394052612bf812859b
This commit is contained in:
drh
2008-07-17 18:39:57 +00:00
parent b912f3e9e4
commit 44845221ed
4 changed files with 39 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
C Fix\sthe\stest\sharness\sso\sthat\sit\sdoes\snot\stry\sto\slink\sagainst\nsqlite3_mutex_alloc()\sif\scompiled\swith\s-DSQLITE_THREADSAFE=0.\s(CVS\s5430) C Simplify\sthe\sb-tree\slogic\sby\staking\sadvantage\sof\sthe\sfact\sthat\sall\sb-trees\nare\seither\sintkey+leafdata\sor\szerodata.\s(CVS\s5431)
D 2008-07-17T17:34:20 D 2008-07-17T18:39:58
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in a03f7cb4f7ad50bc53a788c6c544430e81f95de4 F Makefile.in a03f7cb4f7ad50bc53a788c6c544430e81f95de4
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -96,9 +96,9 @@ F src/attach.c b18ba42c77f7d3941f5d23d2ca20fa1d841a4e91
F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627
F src/bitvec.c 95c86bd18d8fedf0533f5af196192546e10a7e7d F src/bitvec.c 95c86bd18d8fedf0533f5af196192546e10a7e7d
F src/btmutex.c 709cad2cdca0afd013f0f612363810e53f59ec53 F src/btmutex.c 709cad2cdca0afd013f0f612363810e53f59ec53
F src/btree.c 5dcc47a35d12bc8584d7c89ed113d24fb9fbc2ca F src/btree.c c618d62fb41642216f8fc5be403c36f32e1b25d6
F src/btree.h 03256ed7ee42b5ecacbe887070b0f8249e7d069d F src/btree.h 03256ed7ee42b5ecacbe887070b0f8249e7d069d
F src/btreeInt.h a6a5ffab12fa2c15392b85242cd5568371949046 F src/btreeInt.h 2aa6dc53b1801bb22815a5be22e03be121f3bd8f
F src/build.c bac7233d984be3805aaa41cf500f7ee12dc97249 F src/build.c bac7233d984be3805aaa41cf500f7ee12dc97249
F src/callback.c aa492a0ad8c2d454edff9fb8a57fae13743cf71d F src/callback.c aa492a0ad8c2d454edff9fb8a57fae13743cf71d
F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c
@@ -608,7 +608,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
P 08fe49f62f8a91c36ad4bb9a1d8c89806c37f600 P 26a203d894edd0091ac60862956e42d22167011e
R 514cbf07ace43083faff0e77954a2fe0 R 0aa24510df7c6bac75839ea22cde6884
U drh U drh
Z 0a72bbba17dc41aabdaed96d70dac8a1 Z 2661cde038f2c435549bd085c583b89a

View File

@@ -1 +1 @@
26a203d894edd0091ac60862956e42d22167011e 29d3bfd7c9a68078385354394052612bf812859b

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give. ** May you share freely, never taking more than you give.
** **
************************************************************************* *************************************************************************
** $Id: btree.c,v 1.483 2008/07/16 18:17:56 danielk1977 Exp $ ** $Id: btree.c,v 1.484 2008/07/17 18:39:58 drh Exp $
** **
** This file implements a external (disk-based) database using BTrees. ** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information. ** See the header comment on "btreeInt.h" for additional information.
@@ -871,27 +871,38 @@ static void freeSpace(MemPage *pPage, int start, int size){
/* /*
** Decode the flags byte (the first byte of the header) for a page ** Decode the flags byte (the first byte of the header) for a page
** and initialize fields of the MemPage structure accordingly. ** and initialize fields of the MemPage structure accordingly.
**
** Only the following combinations are supported. Anything different
** indicates a corrupt database files:
**
** PTF_ZERODATA
** PTF_ZERODATA | PTF_LEAF
** PTF_LEAFDATA | PTF_INTKEY
** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
*/ */
static void decodeFlags(MemPage *pPage, int flagByte){ static int decodeFlags(MemPage *pPage, int flagByte){
BtShared *pBt; /* A copy of pPage->pBt */ BtShared *pBt; /* A copy of pPage->pBt */
assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); assert( sqlite3_mutex_held(pPage->pBt->mutex) );
pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; pPage->leaf = flagByte>>3; assert( PTF_LEAF == 1<<3 );
pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; flagByte &= ~PTF_LEAF;
pPage->leaf = (flagByte & PTF_LEAF)!=0; pPage->childPtrSize = 4-4*pPage->leaf;
pPage->childPtrSize = 4*(pPage->leaf==0);
pBt = pPage->pBt; pBt = pPage->pBt;
if( flagByte & PTF_LEAFDATA ){ if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
pPage->leafData = 1; pPage->intKey = 1;
pPage->hasData = pPage->leaf;
pPage->maxLocal = pBt->maxLeaf; pPage->maxLocal = pBt->maxLeaf;
pPage->minLocal = pBt->minLeaf; pPage->minLocal = pBt->minLeaf;
}else{ }else if( flagByte==PTF_ZERODATA ){
pPage->leafData = 0; pPage->intKey = 0;
pPage->hasData = 0;
pPage->maxLocal = pBt->maxLocal; pPage->maxLocal = pBt->maxLocal;
pPage->minLocal = pBt->minLocal; pPage->minLocal = pBt->minLocal;
}else{
return SQLITE_CORRUPT_BKPT;
} }
pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); return SQLITE_OK;
} }
/* /*
@@ -941,7 +952,7 @@ int sqlite3BtreeInitPage(
} }
hdr = pPage->hdrOffset; hdr = pPage->hdrOffset;
data = pPage->aData; data = pPage->aData;
decodeFlags(pPage, data[hdr]); if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
pPage->nOverflow = 0; pPage->nOverflow = 0;
pPage->idxShift = 0; pPage->idxShift = 0;
usableSize = pBt->usableSize; usableSize = pBt->usableSize;
@@ -3757,7 +3768,7 @@ int sqlite3BtreeMoveto(
} }
if( c==0 ){ if( c==0 ){
pCur->info.nKey = nCellKey; pCur->info.nKey = nCellKey;
if( pPage->leafData && !pPage->leaf ){ if( pPage->intKey && !pPage->leaf ){
lwr = pCur->idx; lwr = pCur->idx;
upr = lwr - 1; upr = lwr - 1;
break; break;
@@ -3884,7 +3895,7 @@ int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
pPage = pCur->pPage; pPage = pCur->pPage;
}while( pCur->idx>=pPage->nCell ); }while( pCur->idx>=pPage->nCell );
*pRes = 0; *pRes = 0;
if( pPage->leafData ){ if( pPage->intKey ){
rc = sqlite3BtreeNext(pCur, pRes); rc = sqlite3BtreeNext(pCur, pRes);
}else{ }else{
rc = SQLITE_OK; rc = SQLITE_OK;
@@ -3951,7 +3962,7 @@ int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
pCur->idx--; pCur->idx--;
pCur->info.nSize = 0; pCur->info.nSize = 0;
pCur->validNKey = 0; pCur->validNKey = 0;
if( pPage->leafData && !pPage->leaf ){ if( pPage->intKey && !pPage->leaf ){
rc = sqlite3BtreePrevious(pCur, pRes); rc = sqlite3BtreePrevious(pCur, pRes);
}else{ }else{
rc = SQLITE_OK; rc = SQLITE_OK;
@@ -4946,12 +4957,12 @@ static int balance_nonroot(MemPage *pPage){
*/ */
if( pPage->leaf && if( pPage->leaf &&
pPage->intKey && pPage->intKey &&
pPage->leafData &&
pPage->nOverflow==1 && pPage->nOverflow==1 &&
pPage->aOvfl[0].idx==pPage->nCell && pPage->aOvfl[0].idx==pPage->nCell &&
pPage->pParent->pgno!=1 && pPage->pParent->pgno!=1 &&
get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
){ ){
assert( pPage->intKey );
/* /*
** TODO: Check the siblings to the left of pPage. It may be that ** TODO: Check the siblings to the left of pPage. It may be that
** they are not full and no new page is required. ** they are not full and no new page is required.
@@ -5095,7 +5106,7 @@ static int balance_nonroot(MemPage *pPage){
*/ */
nCell = 0; nCell = 0;
leafCorrection = pPage->leaf*4; leafCorrection = pPage->leaf*4;
leafData = pPage->leafData && pPage->leaf; leafData = pPage->hasData;
for(i=0; i<nOld; i++){ for(i=0; i<nOld; i++){
MemPage *pOld = apCopy[i]; MemPage *pOld = apCopy[i];
int limit = pOld->nCell+pOld->nOverflow; int limit = pOld->nCell+pOld->nOverflow;
@@ -5772,7 +5783,7 @@ int sqlite3BtreeInsert(
pPage = pCur->pPage; pPage = pCur->pPage;
assert( pPage->intKey || nKey>=0 ); assert( pPage->intKey || nKey>=0 );
assert( pPage->leaf || !pPage->leafData ); assert( pPage->leaf || !pPage->intKey );
TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
pCur->pgnoRoot, nKey, nData, pPage->pgno, pCur->pgnoRoot, nKey, nData, pPage->pgno,
loc==0 ? "overwrite" : "new entry")); loc==0 ? "overwrite" : "new entry"));
@@ -5888,7 +5899,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){
unsigned char *pNext; unsigned char *pNext;
int notUsed; int notUsed;
unsigned char *tempCell = 0; unsigned char *tempCell = 0;
assert( !pPage->leafData ); assert( !pPage->intKey );
sqlite3BtreeGetTempCursor(pCur, &leafCur); sqlite3BtreeGetTempCursor(pCur, &leafCur);
rc = sqlite3BtreeNext(&leafCur, &notUsed); rc = sqlite3BtreeNext(&leafCur, &notUsed);
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give. ** May you share freely, never taking more than you give.
** **
************************************************************************* *************************************************************************
** $Id: btreeInt.h,v 1.26 2008/07/12 14:52:20 drh Exp $ ** $Id: btreeInt.h,v 1.27 2008/07/17 18:39:58 drh Exp $
** **
** This file implements a external (disk-based) database using BTrees. ** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to ** For a detailed discussion of BTrees, refer to
@@ -275,8 +275,6 @@ struct MemPage {
u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
u8 intKey; /* True if intkey flag is set */ u8 intKey; /* True if intkey flag is set */
u8 leaf; /* True if leaf flag is set */ u8 leaf; /* True if leaf flag is set */
u8 zeroData; /* True if table stores keys only */
u8 leafData; /* True if tables stores data on leaves only */
u8 hasData; /* True if this page stores data */ u8 hasData; /* True if this page stores data */
u8 hdrOffset; /* 100 for page 1. 0 otherwise */ u8 hdrOffset; /* 100 for page 1. 0 otherwise */
u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */