blob: ef9eb35cba1f0f0eae6c5f10e7a8e5ded4e845c4 [file] [log] [blame]
drh16a9b832007-05-05 18:39:25 +00001/*
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**
drh7a7364c2007-05-08 11:27:15 +000016** $Id: test_btree.c,v 1.2 2007/05/08 11:27:16 drh Exp $
drh16a9b832007-05-05 18:39:25 +000017*/
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*/
25static 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}
126int 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*/
136int 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*/
161void 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*/
194int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
195 int cnt, idx;
196 MemPage *pPage = pCur->pPage;
197 BtCursor tmpCur;
drh7a7364c2007-05-08 11:27:15 +0000198 int rc;
drh16a9b832007-05-05 18:39:25 +0000199
drh7a7364c2007-05-08 11:27:15 +0000200 if( pCur->eState==CURSOR_REQUIRESEEK ){
201 rc = sqlite3BtreeRestoreOrClearCursorPosition(pCur);
202 if( rc!=SQLITE_OK ){
203 return rc;
204 }
drh16a9b832007-05-05 18:39:25 +0000205 }
206
207 assert( pPage->isInit );
208 sqlite3BtreeGetTempCursor(pCur, &tmpCur);
209 while( upCnt-- ){
210 sqlite3BtreeMoveToParent(&tmpCur);
211 }
212 pPage = tmpCur.pPage;
213 aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
214 assert( aResult[0]==pPage->pgno );
215 aResult[1] = tmpCur.idx;
216 aResult[2] = pPage->nCell;
217 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
218 sqlite3BtreeParseCell(tmpCur.pPage, tmpCur.idx, &tmpCur.info);
219 aResult[3] = tmpCur.info.nSize;
220 aResult[6] = tmpCur.info.nData;
221 aResult[7] = tmpCur.info.nHeader;
222 aResult[8] = tmpCur.info.nLocal;
223 }else{
224 aResult[3] = 0;
225 aResult[6] = 0;
226 aResult[7] = 0;
227 aResult[8] = 0;
228 }
229 aResult[4] = pPage->nFree;
230 cnt = 0;
231 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
232 while( idx>0 && idx<pPage->pBt->usableSize ){
233 cnt++;
234 idx = get2byte(&pPage->aData[idx]);
235 }
236 aResult[5] = cnt;
237 if( pPage->pParent==0 || sqlite3BtreeIsRootPage(pPage) ){
238 aResult[9] = 0;
239 }else{
240 aResult[9] = pPage->pParent->pgno;
241 }
242 if( tmpCur.info.iOverflow ){
243 aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
244 }else{
245 aResult[10] = 0;
246 }
247 sqlite3BtreeReleaseTempCursor(&tmpCur);
248 return SQLITE_OK;
249}