aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/src/test_btree.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/src/test_btree.c')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/src/test_btree.c247
1 files changed, 247 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/src/test_btree.c b/libraries/sqlite/unix/sqlite-3.5.1/src/test_btree.c
new file mode 100644
index 0000000..3647a5b
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/src/test_btree.c
@@ -0,0 +1,247 @@
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.3 2007/08/17 01:14:39 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*/
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 extern BtShared *sqlite3SharedCacheList;
144 BtShared *pBt;
145 Tcl_Obj *pRet = Tcl_NewObj();
146 for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
147 const char *zFile = sqlite3PagerFilename(pBt->pPager);
148 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
149 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
150 }
151 Tcl_SetObjResult(interp, pRet);
152#endif
153 return TCL_OK;
154}
155
156/*
157** Print debugging information about all cursors to standard output.
158*/
159void sqlite3BtreeCursorList(Btree *p){
160 BtCursor *pCur;
161 BtShared *pBt = p->pBt;
162 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
163 MemPage *pPage = pCur->pPage;
164 char *zMode = pCur->wrFlag ? "rw" : "ro";
165 sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
166 pCur, pCur->pgnoRoot, zMode,
167 pPage ? pPage->pgno : 0, pCur->idx,
168 (pCur->eState==CURSOR_VALID) ? "" : " eof"
169 );
170 }
171}
172
173
174/*
175** Fill aResult[] with information about the entry and page that the
176** cursor is pointing to.
177**
178** aResult[0] = The page number
179** aResult[1] = The entry number
180** aResult[2] = Total number of entries on this page
181** aResult[3] = Cell size (local payload + header)
182** aResult[4] = Number of free bytes on this page
183** aResult[5] = Number of free blocks on the page
184** aResult[6] = Total payload size (local + overflow)
185** aResult[7] = Header size in bytes
186** aResult[8] = Local payload size
187** aResult[9] = Parent page number
188** aResult[10]= Page number of the first overflow page
189**
190** This routine is used for testing and debugging only.
191*/
192int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
193 int cnt, idx;
194 MemPage *pPage = pCur->pPage;
195 BtCursor tmpCur;
196 int rc;
197
198 if( pCur->eState==CURSOR_REQUIRESEEK ){
199 rc = sqlite3BtreeRestoreOrClearCursorPosition(pCur);
200 if( rc!=SQLITE_OK ){
201 return rc;
202 }
203 }
204
205 assert( pPage->isInit );
206 sqlite3BtreeGetTempCursor(pCur, &tmpCur);
207 while( upCnt-- ){
208 sqlite3BtreeMoveToParent(&tmpCur);
209 }
210 pPage = tmpCur.pPage;
211 aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
212 assert( aResult[0]==pPage->pgno );
213 aResult[1] = tmpCur.idx;
214 aResult[2] = pPage->nCell;
215 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
216 sqlite3BtreeParseCell(tmpCur.pPage, tmpCur.idx, &tmpCur.info);
217 aResult[3] = tmpCur.info.nSize;
218 aResult[6] = tmpCur.info.nData;
219 aResult[7] = tmpCur.info.nHeader;
220 aResult[8] = tmpCur.info.nLocal;
221 }else{
222 aResult[3] = 0;
223 aResult[6] = 0;
224 aResult[7] = 0;
225 aResult[8] = 0;
226 }
227 aResult[4] = pPage->nFree;
228 cnt = 0;
229 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
230 while( idx>0 && idx<pPage->pBt->usableSize ){
231 cnt++;
232 idx = get2byte(&pPage->aData[idx]);
233 }
234 aResult[5] = cnt;
235 if( pPage->pParent==0 || sqlite3BtreeIsRootPage(pPage) ){
236 aResult[9] = 0;
237 }else{
238 aResult[9] = pPage->pParent->pgno;
239 }
240 if( tmpCur.info.iOverflow ){
241 aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
242 }else{
243 aResult[10] = 0;
244 }
245 sqlite3BtreeReleaseTempCursor(&tmpCur);
246 return SQLITE_OK;
247}