xref: /sqlite-3.40.0/src/test_btree.c (revision 71d5d2cd)
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.8 2008/09/29 11:49:48 danielk1977 Exp $
17 */
18 #include "btreeInt.h"
19 #include <tcl.h>
20 
21 /*
22 ** Usage: sqlite3_shared_cache_report
23 **
24 ** Return a list of file that are shared and the number of
25 ** references to each file.
26 */
27 int sqlite3BtreeSharedCacheReport(
28   void * clientData,
29   Tcl_Interp *interp,
30   int objc,
31   Tcl_Obj *CONST objv[]
32 ){
33 #ifndef SQLITE_OMIT_SHARED_CACHE
34   extern BtShared *sqlite3SharedCacheList;
35   BtShared *pBt;
36   Tcl_Obj *pRet = Tcl_NewObj();
37   for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
38     const char *zFile = sqlite3PagerFilename(pBt->pPager);
39     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
40     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
41   }
42   Tcl_SetObjResult(interp, pRet);
43 #endif
44   return TCL_OK;
45 }
46 
47 /*
48 ** Print debugging information about all cursors to standard output.
49 */
50 void sqlite3BtreeCursorList(Btree *p){
51 #ifdef SQLITE_DEBUG
52   BtCursor *pCur;
53   BtShared *pBt = p->pBt;
54   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
55     MemPage *pPage = pCur->apPage[pCur->iPage];
56     char *zMode = pCur->wrFlag ? "rw" : "ro";
57     sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
58        pCur, pCur->pgnoRoot, zMode,
59        pPage ? pPage->pgno : 0, pCur->aiIdx[pCur->iPage],
60        (pCur->eState==CURSOR_VALID) ? "" : " eof"
61     );
62   }
63 #endif
64 }
65 
66 
67 /*
68 ** Fill aResult[] with information about the entry and page that the
69 ** cursor is pointing to.
70 **
71 **   aResult[0] =  The page number
72 **   aResult[1] =  The entry number
73 **   aResult[2] =  Total number of entries on this page
74 **   aResult[3] =  Cell size (local payload + header)
75 **   aResult[4] =  Number of free bytes on this page
76 **   aResult[5] =  Number of free blocks on the page
77 **   aResult[6] =  Total payload size (local + overflow)
78 **   aResult[7] =  Header size in bytes
79 **   aResult[8] =  Local payload size
80 **   aResult[9] =  Parent page number
81 **   aResult[10]=  Page number of the first overflow page
82 **
83 ** This routine is used for testing and debugging only.
84 */
85 int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
86 #if 0
87   int cnt, idx;
88   MemPage *pPage = pCur->apPage[pCur->iPage];
89   BtCursor tmpCur;
90   int rc;
91 
92   if( pCur->eState==CURSOR_REQUIRESEEK ){
93     rc = sqlite3BtreeRestoreCursorPosition(pCur);
94     if( rc!=SQLITE_OK ){
95       return rc;
96     }
97   }
98 
99   assert( pPage->isInit );
100   sqlite3BtreeGetTempCursor(pCur, &tmpCur);
101   while( upCnt-- ){
102     sqlite3BtreeMoveToParent(&tmpCur);
103   }
104   pPage = tmpCur.pPage;
105   aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
106   assert( aResult[0]==pPage->pgno );
107   aResult[1] = tmpCur.idx;
108   aResult[2] = pPage->nCell;
109   if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
110     sqlite3BtreeParseCell(tmpCur.pPage, tmpCur.idx, &tmpCur.info);
111     aResult[3] = tmpCur.info.nSize;
112     aResult[6] = tmpCur.info.nData;
113     aResult[7] = tmpCur.info.nHeader;
114     aResult[8] = tmpCur.info.nLocal;
115   }else{
116     aResult[3] = 0;
117     aResult[6] = 0;
118     aResult[7] = 0;
119     aResult[8] = 0;
120   }
121   aResult[4] = pPage->nFree;
122   cnt = 0;
123   idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
124   while( idx>0 && idx<pPage->pBt->usableSize ){
125     cnt++;
126     idx = get2byte(&pPage->aData[idx]);
127   }
128   aResult[5] = cnt;
129   if( pPage->pParent==0 || sqlite3BtreeIsRootPage(pPage) ){
130     aResult[9] = 0;
131   }else{
132     aResult[9] = pPage->pParent->pgno;
133   }
134   if( tmpCur.info.iOverflow ){
135     aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
136   }else{
137     aResult[10] = 0;
138   }
139   sqlite3BtreeReleaseTempCursor(&tmpCur);
140 #endif
141   return SQLITE_OK;
142 }
143