xref: /sqlite-3.40.0/src/test_btree.c (revision 85e9e22b)
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.6 2008/07/15 00:27:35 drh 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=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->pPage;
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->idx,
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   int cnt, idx;
87   MemPage *pPage = pCur->pPage;
88   BtCursor tmpCur;
89   int rc;
90 
91   if( pCur->eState==CURSOR_REQUIRESEEK ){
92     rc = sqlite3BtreeRestoreCursorPosition(pCur);
93     if( rc!=SQLITE_OK ){
94       return rc;
95     }
96   }
97 
98   assert( pPage->isInit );
99   sqlite3BtreeGetTempCursor(pCur, &tmpCur);
100   while( upCnt-- ){
101     sqlite3BtreeMoveToParent(&tmpCur);
102   }
103   pPage = tmpCur.pPage;
104   aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
105   assert( aResult[0]==pPage->pgno );
106   aResult[1] = tmpCur.idx;
107   aResult[2] = pPage->nCell;
108   if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
109     sqlite3BtreeParseCell(tmpCur.pPage, tmpCur.idx, &tmpCur.info);
110     aResult[3] = tmpCur.info.nSize;
111     aResult[6] = tmpCur.info.nData;
112     aResult[7] = tmpCur.info.nHeader;
113     aResult[8] = tmpCur.info.nLocal;
114   }else{
115     aResult[3] = 0;
116     aResult[6] = 0;
117     aResult[7] = 0;
118     aResult[8] = 0;
119   }
120   aResult[4] = pPage->nFree;
121   cnt = 0;
122   idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
123   while( idx>0 && idx<pPage->pBt->usableSize ){
124     cnt++;
125     idx = get2byte(&pPage->aData[idx]);
126   }
127   aResult[5] = cnt;
128   if( pPage->pParent==0 || sqlite3BtreeIsRootPage(pPage) ){
129     aResult[9] = 0;
130   }else{
131     aResult[9] = pPage->pParent->pgno;
132   }
133   if( tmpCur.info.iOverflow ){
134     aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
135   }else{
136     aResult[10] = 0;
137   }
138   sqlite3BtreeReleaseTempCursor(&tmpCur);
139   return SQLITE_OK;
140 }
141