drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ** 2007 May 05 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Code for testing the btree.c module in SQLite. This code |
| 13 | ** is not included in the SQLite library. It is used for automated |
| 14 | ** testing of the SQLite library. |
| 15 | ** |
| 16 | ** $Id: test_btree.c,v 1.1 2007/05/05 18:39:25 drh Exp $ |
| 17 | */ |
| 18 | #include "btreeInt.h" |
| 19 | #include <tcl.h> |
| 20 | |
| 21 | /* |
| 22 | ** Print a disassembly of the given page on standard output. This routine |
| 23 | ** is used for debugging and testing only. |
| 24 | */ |
| 25 | static int btreePageDump( |
| 26 | BtShared *pBt, /* The Btree to be dumped */ |
| 27 | int pgno, /* The page to be dumped */ |
| 28 | int recursive, /* True to decend into child pages */ |
| 29 | MemPage *pParent /* Parent page */ |
| 30 | ){ |
| 31 | int rc; |
| 32 | MemPage *pPage; |
| 33 | int i, j, c; |
| 34 | int nFree; |
| 35 | u16 idx; |
| 36 | int hdr; |
| 37 | int nCell; |
| 38 | int isInit; |
| 39 | unsigned char *data; |
| 40 | char range[20]; |
| 41 | unsigned char payload[20]; |
| 42 | |
| 43 | rc = sqlite3BtreeGetPage(pBt, (Pgno)pgno, &pPage, 0); |
| 44 | isInit = pPage->isInit; |
| 45 | if( pPage->isInit==0 ){ |
| 46 | sqlite3BtreeInitPage(pPage, pParent); |
| 47 | } |
| 48 | if( rc ){ |
| 49 | return rc; |
| 50 | } |
| 51 | hdr = pPage->hdrOffset; |
| 52 | data = pPage->aData; |
| 53 | c = data[hdr]; |
| 54 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 55 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
| 56 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
| 57 | pPage->leaf = (c & PTF_LEAF)!=0; |
| 58 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 59 | nCell = get2byte(&data[hdr+3]); |
| 60 | sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
| 61 | data[hdr], data[hdr+7], |
| 62 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
| 63 | assert( hdr == (pgno==1 ? 100 : 0) ); |
| 64 | idx = hdr + 12 - pPage->leaf*4; |
| 65 | for(i=0; i<nCell; i++){ |
| 66 | CellInfo info; |
| 67 | Pgno child; |
| 68 | unsigned char *pCell; |
| 69 | int sz; |
| 70 | int addr; |
| 71 | |
| 72 | addr = get2byte(&data[idx + 2*i]); |
| 73 | pCell = &data[addr]; |
| 74 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 75 | sz = info.nSize; |
| 76 | sqlite3_snprintf(sizeof(range),range,"%d..%d", addr, addr+sz-1); |
| 77 | if( pPage->leaf ){ |
| 78 | child = 0; |
| 79 | }else{ |
| 80 | child = get4byte(pCell); |
| 81 | } |
| 82 | sz = info.nData; |
| 83 | if( !pPage->intKey ) sz += info.nKey; |
| 84 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
| 85 | memcpy(payload, &pCell[info.nHeader], sz); |
| 86 | for(j=0; j<sz; j++){ |
| 87 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 88 | } |
| 89 | payload[sz] = 0; |
| 90 | sqlite3DebugPrintf( |
| 91 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 92 | i, range, child, info.nKey, info.nData, payload |
| 93 | ); |
| 94 | } |
| 95 | if( !pPage->leaf ){ |
| 96 | sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); |
| 97 | } |
| 98 | nFree = 0; |
| 99 | i = 0; |
| 100 | idx = get2byte(&data[hdr+1]); |
| 101 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
| 102 | int sz = get2byte(&data[idx+2]); |
| 103 | sqlite3_snprintf(sizeof(range),range,"%d..%d", idx, idx+sz-1); |
| 104 | nFree += sz; |
| 105 | sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
| 106 | i, range, sz, nFree); |
| 107 | idx = get2byte(&data[idx]); |
| 108 | i++; |
| 109 | } |
| 110 | if( idx!=0 ){ |
| 111 | sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); |
| 112 | } |
| 113 | if( recursive && !pPage->leaf ){ |
| 114 | for(i=0; i<nCell; i++){ |
| 115 | unsigned char *pCell = sqlite3BtreeFindCell(pPage, i); |
| 116 | btreePageDump(pBt, get4byte(pCell), 1, pPage); |
| 117 | idx = get2byte(pCell); |
| 118 | } |
| 119 | btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage); |
| 120 | } |
| 121 | pPage->isInit = isInit; |
| 122 | sqlite3PagerUnref(pPage->pDbPage); |
| 123 | fflush(stdout); |
| 124 | return SQLITE_OK; |
| 125 | } |
| 126 | int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){ |
| 127 | return btreePageDump(p->pBt, pgno, recursive, 0); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** Usage: sqlite3_shared_cache_report |
| 132 | ** |
| 133 | ** Return a list of file that are shared and the number of |
| 134 | ** references to each file. |
| 135 | */ |
| 136 | int sqlite3BtreeSharedCacheReport( |
| 137 | void * clientData, |
| 138 | Tcl_Interp *interp, |
| 139 | int objc, |
| 140 | Tcl_Obj *CONST objv[] |
| 141 | ){ |
| 142 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 143 | const ThreadData *pTd = sqlite3ThreadDataReadOnly(); |
| 144 | if( pTd->useSharedData ){ |
| 145 | BtShared *pBt; |
| 146 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 147 | for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){ |
| 148 | const char *zFile = sqlite3PagerFilename(pBt->pPager); |
| 149 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1)); |
| 150 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef)); |
| 151 | } |
| 152 | Tcl_SetObjResult(interp, pRet); |
| 153 | } |
| 154 | #endif |
| 155 | return TCL_OK; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | ** Print debugging information about all cursors to standard output. |
| 160 | */ |
| 161 | void sqlite3BtreeCursorList(Btree *p){ |
| 162 | BtCursor *pCur; |
| 163 | BtShared *pBt = p->pBt; |
| 164 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 165 | MemPage *pPage = pCur->pPage; |
| 166 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
| 167 | sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", |
| 168 | pCur, pCur->pgnoRoot, zMode, |
| 169 | pPage ? pPage->pgno : 0, pCur->idx, |
| 170 | (pCur->eState==CURSOR_VALID) ? "" : " eof" |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | ** Fill aResult[] with information about the entry and page that the |
| 178 | ** cursor is pointing to. |
| 179 | ** |
| 180 | ** aResult[0] = The page number |
| 181 | ** aResult[1] = The entry number |
| 182 | ** aResult[2] = Total number of entries on this page |
| 183 | ** aResult[3] = Cell size (local payload + header) |
| 184 | ** aResult[4] = Number of free bytes on this page |
| 185 | ** aResult[5] = Number of free blocks on the page |
| 186 | ** aResult[6] = Total payload size (local + overflow) |
| 187 | ** aResult[7] = Header size in bytes |
| 188 | ** aResult[8] = Local payload size |
| 189 | ** aResult[9] = Parent page number |
| 190 | ** aResult[10]= Page number of the first overflow page |
| 191 | ** |
| 192 | ** This routine is used for testing and debugging only. |
| 193 | */ |
| 194 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ |
| 195 | int cnt, idx; |
| 196 | MemPage *pPage = pCur->pPage; |
| 197 | BtCursor tmpCur; |
| 198 | |
| 199 | int rc = sqlite3BtreeRestoreOrClearCursorPosition(pCur); |
| 200 | if( rc!=SQLITE_OK ){ |
| 201 | return rc; |
| 202 | } |
| 203 | |
| 204 | assert( pPage->isInit ); |
| 205 | sqlite3BtreeGetTempCursor(pCur, &tmpCur); |
| 206 | while( upCnt-- ){ |
| 207 | sqlite3BtreeMoveToParent(&tmpCur); |
| 208 | } |
| 209 | pPage = tmpCur.pPage; |
| 210 | aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage); |
| 211 | assert( aResult[0]==pPage->pgno ); |
| 212 | aResult[1] = tmpCur.idx; |
| 213 | aResult[2] = pPage->nCell; |
| 214 | if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){ |
| 215 | sqlite3BtreeParseCell(tmpCur.pPage, tmpCur.idx, &tmpCur.info); |
| 216 | aResult[3] = tmpCur.info.nSize; |
| 217 | aResult[6] = tmpCur.info.nData; |
| 218 | aResult[7] = tmpCur.info.nHeader; |
| 219 | aResult[8] = tmpCur.info.nLocal; |
| 220 | }else{ |
| 221 | aResult[3] = 0; |
| 222 | aResult[6] = 0; |
| 223 | aResult[7] = 0; |
| 224 | aResult[8] = 0; |
| 225 | } |
| 226 | aResult[4] = pPage->nFree; |
| 227 | cnt = 0; |
| 228 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
| 229 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
| 230 | cnt++; |
| 231 | idx = get2byte(&pPage->aData[idx]); |
| 232 | } |
| 233 | aResult[5] = cnt; |
| 234 | if( pPage->pParent==0 || sqlite3BtreeIsRootPage(pPage) ){ |
| 235 | aResult[9] = 0; |
| 236 | }else{ |
| 237 | aResult[9] = pPage->pParent->pgno; |
| 238 | } |
| 239 | if( tmpCur.info.iOverflow ){ |
| 240 | aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]); |
| 241 | }else{ |
| 242 | aResult[10] = 0; |
| 243 | } |
| 244 | sqlite3BtreeReleaseTempCursor(&tmpCur); |
| 245 | return SQLITE_OK; |
| 246 | } |