blob: e29399321ecbb9df0917114f1af69e90335ea06e [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**
danielk197771d5d2c2008-09-29 11:49:47 +000016** $Id: test_btree.c,v 1.8 2008/09/29 11:49:48 danielk1977 Exp $
drh16a9b832007-05-05 18:39:25 +000017*/
18#include "btreeInt.h"
19#include <tcl.h>
20
21/*
drh16a9b832007-05-05 18:39:25 +000022** 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*/
27int sqlite3BtreeSharedCacheReport(
28 void * clientData,
29 Tcl_Interp *interp,
30 int objc,
31 Tcl_Obj *CONST objv[]
32){
33#ifndef SQLITE_OMIT_SHARED_CACHE
drhe53831d2007-08-17 01:14:38 +000034 extern BtShared *sqlite3SharedCacheList;
35 BtShared *pBt;
36 Tcl_Obj *pRet = Tcl_NewObj();
drh78f82d12008-09-02 00:52:52 +000037 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
drhe53831d2007-08-17 01:14:38 +000038 const char *zFile = sqlite3PagerFilename(pBt->pPager);
39 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
40 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
drh16a9b832007-05-05 18:39:25 +000041 }
drhe53831d2007-08-17 01:14:38 +000042 Tcl_SetObjResult(interp, pRet);
drh16a9b832007-05-05 18:39:25 +000043#endif
44 return TCL_OK;
45}
46
47/*
48** Print debugging information about all cursors to standard output.
49*/
50void sqlite3BtreeCursorList(Btree *p){
drh85e9e222008-07-15 00:27:34 +000051#ifdef SQLITE_DEBUG
drh16a9b832007-05-05 18:39:25 +000052 BtCursor *pCur;
53 BtShared *pBt = p->pBt;
54 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
danielk197771d5d2c2008-09-29 11:49:47 +000055 MemPage *pPage = pCur->apPage[pCur->iPage];
drh16a9b832007-05-05 18:39:25 +000056 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,
danielk197771d5d2c2008-09-29 11:49:47 +000059 pPage ? pPage->pgno : 0, pCur->aiIdx[pCur->iPage],
drh16a9b832007-05-05 18:39:25 +000060 (pCur->eState==CURSOR_VALID) ? "" : " eof"
61 );
62 }
drh85e9e222008-07-15 00:27:34 +000063#endif
drh16a9b832007-05-05 18:39:25 +000064}
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*/
85int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
danielk197771d5d2c2008-09-29 11:49:47 +000086#if 0
drh16a9b832007-05-05 18:39:25 +000087 int cnt, idx;
danielk197771d5d2c2008-09-29 11:49:47 +000088 MemPage *pPage = pCur->apPage[pCur->iPage];
drh16a9b832007-05-05 18:39:25 +000089 BtCursor tmpCur;
drh7a7364c2007-05-08 11:27:15 +000090 int rc;
drh16a9b832007-05-05 18:39:25 +000091
drh7a7364c2007-05-08 11:27:15 +000092 if( pCur->eState==CURSOR_REQUIRESEEK ){
drha3460582008-07-11 21:02:53 +000093 rc = sqlite3BtreeRestoreCursorPosition(pCur);
drh7a7364c2007-05-08 11:27:15 +000094 if( rc!=SQLITE_OK ){
95 return rc;
96 }
drh16a9b832007-05-05 18:39:25 +000097 }
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);
danielk197771d5d2c2008-09-29 11:49:47 +0000140#endif
drh16a9b832007-05-05 18:39:25 +0000141 return SQLITE_OK;
142}