blob: 6b4bfedbd983ec5e9ab9f0d9bfe1db3f68edcfd7 [file] [log] [blame]
drh5c4d9702001-08-20 00:33:58 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh5c4d9702001-08-20 00:33:58 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh5c4d9702001-08-20 00:33:58 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh5c4d9702001-08-20 00:33:58 +000010**
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.
drh5c4d9702001-08-20 00:33:58 +000015*/
16#include "sqliteInt.h"
danielk1977a1644fd2007-08-29 12:31:25 +000017#include "btreeInt.h"
mistachkin52b1dbb2016-07-28 14:37:04 +000018#if defined(INCLUDE_SQLITE_TCL_H)
19# include "sqlite_tcl.h"
20#else
21# include "tcl.h"
22#endif
drh5c4d9702001-08-20 00:33:58 +000023#include <stdlib.h>
24#include <string.h>
25
mistachkine84d8d32013-04-29 03:09:10 +000026extern const char *sqlite3ErrName(int);
drh5c4d9702001-08-20 00:33:58 +000027
28/*
drha2451e22007-08-20 23:50:24 +000029** A bogus sqlite3 connection structure for use in the btree
30** tests.
31*/
32static sqlite3 sDb;
33static int nRefSqlite3 = 0;
34
35/*
drh75c014c2010-08-30 15:02:28 +000036** Usage: btree_open FILENAME NCACHE
drh5c4d9702001-08-20 00:33:58 +000037**
38** Open a new database
39*/
mistachkin7617e4a2016-07-28 17:11:20 +000040static int SQLITE_TCLAPI btree_open(
drh5c4d9702001-08-20 00:33:58 +000041 void *NotUsed,
42 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
43 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +000044 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +000045){
46 Btree *pBt;
drh75c014c2010-08-30 15:02:28 +000047 int rc, nCache;
drh5c4d9702001-08-20 00:33:58 +000048 char zBuf[100];
drhc02a43a2012-01-10 23:18:38 +000049 int n;
50 char *zFilename;
drh75c014c2010-08-30 15:02:28 +000051 if( argc!=3 ){
drh5c4d9702001-08-20 00:33:58 +000052 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh23e11ca2004-05-04 17:27:28 +000053 " FILENAME NCACHE FLAGS\"", 0);
drh5c4d9702001-08-20 00:33:58 +000054 return TCL_ERROR;
55 }
drh23e11ca2004-05-04 17:27:28 +000056 if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
drha2451e22007-08-20 23:50:24 +000057 nRefSqlite3++;
58 if( nRefSqlite3==1 ){
59 sDb.pVfs = sqlite3_vfs_find(0);
danielk197759f8c082008-06-18 17:09:10 +000060 sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
drh27641702007-08-22 02:56:42 +000061 sqlite3_mutex_enter(sDb.mutex);
drha2451e22007-08-20 23:50:24 +000062 }
drh83cc1392012-04-19 18:04:28 +000063 n = (int)strlen(argv[1]);
drhc02a43a2012-01-10 23:18:38 +000064 zFilename = sqlite3_malloc( n+2 );
65 if( zFilename==0 ) return TCL_ERROR;
66 memcpy(zFilename, argv[1], n+1);
67 zFilename[n+1] = 0;
68 rc = sqlite3BtreeOpen(sDb.pVfs, zFilename, &sDb, &pBt, 0,
drh33f4e022007-09-03 15:19:34 +000069 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
drhc02a43a2012-01-10 23:18:38 +000070 sqlite3_free(zFilename);
drh5c4d9702001-08-20 00:33:58 +000071 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +000072 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +000073 return TCL_ERROR;
74 }
drh90f5ecb2004-07-22 01:19:35 +000075 sqlite3BtreeSetCacheSize(pBt, nCache);
drhfe63d1c2004-09-08 20:13:04 +000076 sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pBt);
drh5c4d9702001-08-20 00:33:58 +000077 Tcl_AppendResult(interp, zBuf, 0);
78 return TCL_OK;
79}
80
81/*
82** Usage: btree_close ID
83**
84** Close the given database.
85*/
mistachkin7617e4a2016-07-28 17:11:20 +000086static int SQLITE_TCLAPI btree_close(
drh5c4d9702001-08-20 00:33:58 +000087 void *NotUsed,
88 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
89 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +000090 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +000091){
92 Btree *pBt;
93 int rc;
94 if( argc!=2 ){
95 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
96 " ID\"", 0);
97 return TCL_ERROR;
98 }
drhe8f52c52008-07-12 14:52:20 +000099 pBt = sqlite3TestTextToPtr(argv[1]);
drh23e11ca2004-05-04 17:27:28 +0000100 rc = sqlite3BtreeClose(pBt);
drh5c4d9702001-08-20 00:33:58 +0000101 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +0000102 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000103 return TCL_ERROR;
104 }
drha2451e22007-08-20 23:50:24 +0000105 nRefSqlite3--;
106 if( nRefSqlite3==0 ){
drh27641702007-08-22 02:56:42 +0000107 sqlite3_mutex_leave(sDb.mutex);
drha2451e22007-08-20 23:50:24 +0000108 sqlite3_mutex_free(sDb.mutex);
109 sDb.mutex = 0;
drha2451e22007-08-20 23:50:24 +0000110 sDb.pVfs = 0;
111 }
drh5c4d9702001-08-20 00:33:58 +0000112 return TCL_OK;
113}
114
drh27641702007-08-22 02:56:42 +0000115
drh5c4d9702001-08-20 00:33:58 +0000116/*
117** Usage: btree_begin_transaction ID
118**
119** Start a new transaction
120*/
mistachkin7617e4a2016-07-28 17:11:20 +0000121static int SQLITE_TCLAPI btree_begin_transaction(
drh5c4d9702001-08-20 00:33:58 +0000122 void *NotUsed,
123 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
124 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000125 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000126){
127 Btree *pBt;
128 int rc;
129 if( argc!=2 ){
130 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
131 " ID\"", 0);
132 return TCL_ERROR;
133 }
drhe8f52c52008-07-12 14:52:20 +0000134 pBt = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000135 sqlite3BtreeEnter(pBt);
danielk197740b38dc2004-06-26 08:38:24 +0000136 rc = sqlite3BtreeBeginTrans(pBt, 1);
drhff0587c2007-08-29 17:43:19 +0000137 sqlite3BtreeLeave(pBt);
drh5c4d9702001-08-20 00:33:58 +0000138 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +0000139 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000140 return TCL_ERROR;
141 }
142 return TCL_OK;
143}
144
145/*
drh5c4d9702001-08-20 00:33:58 +0000146** Usage: btree_pager_stats ID
147**
148** Returns pager statistics
149*/
mistachkin7617e4a2016-07-28 17:11:20 +0000150static int SQLITE_TCLAPI btree_pager_stats(
drh5c4d9702001-08-20 00:33:58 +0000151 void *NotUsed,
152 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
153 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000154 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000155){
156 Btree *pBt;
157 int i;
158 int *a;
159
160 if( argc!=2 ){
161 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
162 " ID\"", 0);
163 return TCL_ERROR;
164 }
drhe8f52c52008-07-12 14:52:20 +0000165 pBt = sqlite3TestTextToPtr(argv[1]);
danielk1977f3c62652007-08-30 08:27:39 +0000166
167 /* Normally in this file, with a b-tree handle opened using the
168 ** [btree_open] command it is safe to call sqlite3BtreeEnter() directly.
169 ** But this function is sometimes called with a btree handle obtained
170 ** from an open SQLite connection (using [btree_from_db]). In this case
171 ** we need to obtain the mutex for the controlling SQLite handle before
172 ** it is safe to call sqlite3BtreeEnter().
173 */
drhe5fe6902007-12-07 18:55:28 +0000174 sqlite3_mutex_enter(pBt->db->mutex);
danielk1977f3c62652007-08-30 08:27:39 +0000175
drh27641702007-08-22 02:56:42 +0000176 sqlite3BtreeEnter(pBt);
danielk19773b8a05f2007-03-19 17:44:26 +0000177 a = sqlite3PagerStats(sqlite3BtreePager(pBt));
danielk197742741be2005-01-08 12:42:39 +0000178 for(i=0; i<11; i++){
drh5c4d9702001-08-20 00:33:58 +0000179 static char *zName[] = {
180 "ref", "page", "max", "size", "state", "err",
danielk197742741be2005-01-08 12:42:39 +0000181 "hit", "miss", "ovfl", "read", "write"
drh5c4d9702001-08-20 00:33:58 +0000182 };
183 char zBuf[100];
184 Tcl_AppendElement(interp, zName[i]);
drhfe63d1c2004-09-08 20:13:04 +0000185 sqlite3_snprintf(sizeof(zBuf), zBuf,"%d",a[i]);
drh5c4d9702001-08-20 00:33:58 +0000186 Tcl_AppendElement(interp, zBuf);
187 }
drh27641702007-08-22 02:56:42 +0000188 sqlite3BtreeLeave(pBt);
danielk1977f3c62652007-08-30 08:27:39 +0000189
190 /* Release the mutex on the SQLite handle that controls this b-tree */
drhe5fe6902007-12-07 18:55:28 +0000191 sqlite3_mutex_leave(pBt->db->mutex);
drh5c4d9702001-08-20 00:33:58 +0000192 return TCL_OK;
193}
194
195/*
drhecdc7532001-09-23 02:35:53 +0000196** Usage: btree_cursor ID TABLENUM WRITEABLE
drh5c4d9702001-08-20 00:33:58 +0000197**
198** Create a new cursor. Return the ID for the cursor.
199*/
mistachkin7617e4a2016-07-28 17:11:20 +0000200static int SQLITE_TCLAPI btree_cursor(
drh5c4d9702001-08-20 00:33:58 +0000201 void *NotUsed,
202 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
203 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000204 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000205){
206 Btree *pBt;
207 int iTable;
208 BtCursor *pCur;
dan93a696f2010-01-07 11:27:30 +0000209 int rc = SQLITE_OK;
drhecdc7532001-09-23 02:35:53 +0000210 int wrFlag;
drh5c4d9702001-08-20 00:33:58 +0000211 char zBuf[30];
212
drhecdc7532001-09-23 02:35:53 +0000213 if( argc!=4 ){
drh5c4d9702001-08-20 00:33:58 +0000214 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drhecdc7532001-09-23 02:35:53 +0000215 " ID TABLENUM WRITEABLE\"", 0);
drh5c4d9702001-08-20 00:33:58 +0000216 return TCL_ERROR;
217 }
drhe8f52c52008-07-12 14:52:20 +0000218 pBt = sqlite3TestTextToPtr(argv[1]);
drh5c4d9702001-08-20 00:33:58 +0000219 if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR;
drhecdc7532001-09-23 02:35:53 +0000220 if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR;
danfd261ec2015-10-22 20:54:33 +0000221 if( wrFlag ) wrFlag = BTREE_WRCSR;
danielk1977cd3e8f72008-03-25 09:47:35 +0000222 pCur = (BtCursor *)ckalloc(sqlite3BtreeCursorSize());
223 memset(pCur, 0, sqlite3BtreeCursorSize());
dan6a75c8a2015-10-30 20:54:25 +0000224 sqlite3_mutex_enter(pBt->db->mutex);
drhff0587c2007-08-29 17:43:19 +0000225 sqlite3BtreeEnter(pBt);
dan93a696f2010-01-07 11:27:30 +0000226#ifndef SQLITE_OMIT_SHARED_CACHE
dan6b513642015-10-26 16:31:18 +0000227 rc = sqlite3BtreeLockTable(pBt, iTable, !!wrFlag);
dan93a696f2010-01-07 11:27:30 +0000228#endif
danielk197796d48e92009-06-29 06:00:37 +0000229 if( rc==SQLITE_OK ){
230 rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, 0, pCur);
231 }
drhff0587c2007-08-29 17:43:19 +0000232 sqlite3BtreeLeave(pBt);
dan6a75c8a2015-10-30 20:54:25 +0000233 sqlite3_mutex_leave(pBt->db->mutex);
drh5c4d9702001-08-20 00:33:58 +0000234 if( rc ){
danielk1977cd3e8f72008-03-25 09:47:35 +0000235 ckfree((char *)pCur);
mistachkine84d8d32013-04-29 03:09:10 +0000236 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000237 return TCL_ERROR;
238 }
drhfe63d1c2004-09-08 20:13:04 +0000239 sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur);
drh5c4d9702001-08-20 00:33:58 +0000240 Tcl_AppendResult(interp, zBuf, 0);
241 return SQLITE_OK;
242}
243
244/*
245** Usage: btree_close_cursor ID
246**
247** Close a cursor opened using btree_cursor.
248*/
mistachkin7617e4a2016-07-28 17:11:20 +0000249static int SQLITE_TCLAPI btree_close_cursor(
drh5c4d9702001-08-20 00:33:58 +0000250 void *NotUsed,
251 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
252 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000253 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000254){
255 BtCursor *pCur;
256 int rc;
257
258 if( argc!=2 ){
259 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
260 " ID\"", 0);
261 return TCL_ERROR;
262 }
drhe8f52c52008-07-12 14:52:20 +0000263 pCur = sqlite3TestTextToPtr(argv[1]);
drh2eb22af2016-09-10 19:51:40 +0000264#if SQLITE_THREADSAFE>0
265 {
266 Btree *pBt = pCur->pBtree;
267 sqlite3_mutex_enter(pBt->db->mutex);
268 sqlite3BtreeEnter(pBt);
269 rc = sqlite3BtreeCloseCursor(pCur);
270 sqlite3BtreeLeave(pBt);
271 sqlite3_mutex_leave(pBt->db->mutex);
272 }
273#else
drh23e11ca2004-05-04 17:27:28 +0000274 rc = sqlite3BtreeCloseCursor(pCur);
drh2eb22af2016-09-10 19:51:40 +0000275#endif
danielk1977cd3e8f72008-03-25 09:47:35 +0000276 ckfree((char *)pCur);
drh5c4d9702001-08-20 00:33:58 +0000277 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000278 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000279 return TCL_ERROR;
280 }
281 return SQLITE_OK;
282}
283
284/*
drh5c4d9702001-08-20 00:33:58 +0000285** Usage: btree_next ID
286**
drh2dcc9aa2002-12-04 13:40:25 +0000287** Move the cursor to the next entry in the table. Return 0 on success
288** or 1 if the cursor was already on the last entry in the table or if
289** the table is empty.
drh5c4d9702001-08-20 00:33:58 +0000290*/
mistachkin7617e4a2016-07-28 17:11:20 +0000291static int SQLITE_TCLAPI btree_next(
drh5c4d9702001-08-20 00:33:58 +0000292 void *NotUsed,
293 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
294 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000295 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000296){
297 BtCursor *pCur;
298 int rc;
drh0de8c112002-07-06 16:32:14 +0000299 int res = 0;
300 char zBuf[100];
drh5c4d9702001-08-20 00:33:58 +0000301
302 if( argc!=2 ){
303 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
304 " ID\"", 0);
305 return TCL_ERROR;
306 }
drhe8f52c52008-07-12 14:52:20 +0000307 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000308 sqlite3BtreeEnter(pCur->pBtree);
drh4fc0fb42004-05-07 02:26:28 +0000309 rc = sqlite3BtreeNext(pCur, &res);
drhff0587c2007-08-29 17:43:19 +0000310 sqlite3BtreeLeave(pCur->pBtree);
drh5c4d9702001-08-20 00:33:58 +0000311 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000312 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000313 return TCL_ERROR;
314 }
drhfe63d1c2004-09-08 20:13:04 +0000315 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
drh0de8c112002-07-06 16:32:14 +0000316 Tcl_AppendResult(interp, zBuf, 0);
317 return SQLITE_OK;
318}
319
320/*
321** Usage: btree_first ID
322**
drh2dcc9aa2002-12-04 13:40:25 +0000323** Move the cursor to the first entry in the table. Return 0 if the
324** cursor was left point to something and 1 if the table is empty.
drh0de8c112002-07-06 16:32:14 +0000325*/
mistachkin7617e4a2016-07-28 17:11:20 +0000326static int SQLITE_TCLAPI btree_first(
drh0de8c112002-07-06 16:32:14 +0000327 void *NotUsed,
328 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
329 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000330 const char **argv /* Text of each argument */
drh0de8c112002-07-06 16:32:14 +0000331){
332 BtCursor *pCur;
333 int rc;
334 int res = 0;
335 char zBuf[100];
336
337 if( argc!=2 ){
338 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
339 " ID\"", 0);
340 return TCL_ERROR;
341 }
drhe8f52c52008-07-12 14:52:20 +0000342 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000343 sqlite3BtreeEnter(pCur->pBtree);
drh4fc0fb42004-05-07 02:26:28 +0000344 rc = sqlite3BtreeFirst(pCur, &res);
drhff0587c2007-08-29 17:43:19 +0000345 sqlite3BtreeLeave(pCur->pBtree);
drh0de8c112002-07-06 16:32:14 +0000346 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000347 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh0de8c112002-07-06 16:32:14 +0000348 return TCL_ERROR;
349 }
drhfe63d1c2004-09-08 20:13:04 +0000350 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
drh0de8c112002-07-06 16:32:14 +0000351 Tcl_AppendResult(interp, zBuf, 0);
drh5c4d9702001-08-20 00:33:58 +0000352 return SQLITE_OK;
353}
354
355/*
drhc39e0002004-05-07 23:50:57 +0000356** Usage: btree_eof ID
357**
358** Return TRUE if the given cursor is not pointing at a valid entry.
359** Return FALSE if the cursor does point to a valid entry.
360*/
mistachkin7617e4a2016-07-28 17:11:20 +0000361static int SQLITE_TCLAPI btree_eof(
drhc39e0002004-05-07 23:50:57 +0000362 void *NotUsed,
363 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
364 int argc, /* Number of arguments */
365 const char **argv /* Text of each argument */
366){
367 BtCursor *pCur;
drhff0587c2007-08-29 17:43:19 +0000368 int rc;
drhc39e0002004-05-07 23:50:57 +0000369 char zBuf[50];
370
371 if( argc!=2 ){
372 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
373 " ID\"", 0);
374 return TCL_ERROR;
375 }
drhe8f52c52008-07-12 14:52:20 +0000376 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000377 sqlite3BtreeEnter(pCur->pBtree);
378 rc = sqlite3BtreeEof(pCur);
379 sqlite3BtreeLeave(pCur->pBtree);
380 sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc);
drhc39e0002004-05-07 23:50:57 +0000381 Tcl_AppendResult(interp, zBuf, 0);
382 return SQLITE_OK;
383}
384
385/*
drh0de8c112002-07-06 16:32:14 +0000386** Usage: btree_payload_size ID
387**
388** Return the number of bytes of payload
389*/
mistachkin7617e4a2016-07-28 17:11:20 +0000390static int SQLITE_TCLAPI btree_payload_size(
drh0de8c112002-07-06 16:32:14 +0000391 void *NotUsed,
392 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
393 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000394 const char **argv /* Text of each argument */
drh0de8c112002-07-06 16:32:14 +0000395){
396 BtCursor *pCur;
drha7c90c42016-06-04 20:37:10 +0000397 u32 n;
drh0de8c112002-07-06 16:32:14 +0000398 char zBuf[50];
399
400 if( argc!=2 ){
401 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
402 " ID\"", 0);
403 return TCL_ERROR;
404 }
drhe8f52c52008-07-12 14:52:20 +0000405 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000406 sqlite3BtreeEnter(pCur->pBtree);
drha7c90c42016-06-04 20:37:10 +0000407 n = sqlite3BtreePayloadSize(pCur);
drhff0587c2007-08-29 17:43:19 +0000408 sqlite3BtreeLeave(pCur->pBtree);
drha7c90c42016-06-04 20:37:10 +0000409 sqlite3_snprintf(sizeof(zBuf),zBuf, "%u", n);
drh0de8c112002-07-06 16:32:14 +0000410 Tcl_AppendResult(interp, zBuf, 0);
drh0de8c112002-07-06 16:32:14 +0000411 return SQLITE_OK;
412}
413
414/*
drh6d2fb152004-05-14 16:50:06 +0000415** usage: varint_test START MULTIPLIER COUNT INCREMENT
416**
shane3f8d5cf2008-04-24 19:15:09 +0000417** This command tests the putVarint() and getVarint()
drh6d2fb152004-05-14 16:50:06 +0000418** routines, both for accuracy and for speed.
419**
shane3f8d5cf2008-04-24 19:15:09 +0000420** An integer is written using putVarint() and read back with
421** getVarint() and varified to be unchanged. This repeats COUNT
drh6d2fb152004-05-14 16:50:06 +0000422** times. The first integer is START*MULTIPLIER. Each iteration
423** increases the integer by INCREMENT.
424**
425** This command returns nothing if it works. It returns an error message
426** if something goes wrong.
427*/
mistachkin7617e4a2016-07-28 17:11:20 +0000428static int SQLITE_TCLAPI btree_varint_test(
drh6d2fb152004-05-14 16:50:06 +0000429 void *NotUsed,
430 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
431 int argc, /* Number of arguments */
432 const char **argv /* Text of each argument */
433){
434 u32 start, mult, count, incr;
435 u64 in, out;
drhd8820e82004-05-18 15:57:42 +0000436 int n1, n2, i, j;
drh6d2fb152004-05-14 16:50:06 +0000437 unsigned char zBuf[100];
438 if( argc!=5 ){
439 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
440 " START MULTIPLIER COUNT INCREMENT\"", 0);
441 return TCL_ERROR;
442 }
443 if( Tcl_GetInt(interp, argv[1], (int*)&start) ) return TCL_ERROR;
444 if( Tcl_GetInt(interp, argv[2], (int*)&mult) ) return TCL_ERROR;
445 if( Tcl_GetInt(interp, argv[3], (int*)&count) ) return TCL_ERROR;
446 if( Tcl_GetInt(interp, argv[4], (int*)&incr) ) return TCL_ERROR;
447 in = start;
448 in *= mult;
drh7da5fcb2012-03-30 14:59:43 +0000449 for(i=0; i<(int)count; i++){
drhd8820e82004-05-18 15:57:42 +0000450 char zErr[200];
shane3f8d5cf2008-04-24 19:15:09 +0000451 n1 = putVarint(zBuf, in);
drh6d2fb152004-05-14 16:50:06 +0000452 if( n1>9 || n1<1 ){
drh65545b52015-01-19 00:35:53 +0000453 sqlite3_snprintf(sizeof(zErr), zErr,
454 "putVarint returned %d - should be between 1 and 9", n1);
drhd8820e82004-05-18 15:57:42 +0000455 Tcl_AppendResult(interp, zErr, 0);
drh6d2fb152004-05-14 16:50:06 +0000456 return TCL_ERROR;
457 }
shane3f8d5cf2008-04-24 19:15:09 +0000458 n2 = getVarint(zBuf, &out);
drh6d2fb152004-05-14 16:50:06 +0000459 if( n1!=n2 ){
drh65545b52015-01-19 00:35:53 +0000460 sqlite3_snprintf(sizeof(zErr), zErr,
461 "putVarint returned %d and getVarint returned %d", n1, n2);
drhd8820e82004-05-18 15:57:42 +0000462 Tcl_AppendResult(interp, zErr, 0);
drh6d2fb152004-05-14 16:50:06 +0000463 return TCL_ERROR;
464 }
465 if( in!=out ){
drh65545b52015-01-19 00:35:53 +0000466 sqlite3_snprintf(sizeof(zErr), zErr,
467 "Wrote 0x%016llx and got back 0x%016llx", in, out);
drhd8820e82004-05-18 15:57:42 +0000468 Tcl_AppendResult(interp, zErr, 0);
drh6d2fb152004-05-14 16:50:06 +0000469 return TCL_ERROR;
470 }
drh9d213ef2004-06-30 04:02:11 +0000471 if( (in & 0xffffffff)==in ){
472 u32 out32;
shane3f8d5cf2008-04-24 19:15:09 +0000473 n2 = getVarint32(zBuf, out32);
drh9d213ef2004-06-30 04:02:11 +0000474 out = out32;
475 if( n1!=n2 ){
drh65545b52015-01-19 00:35:53 +0000476 sqlite3_snprintf(sizeof(zErr), zErr,
477 "putVarint returned %d and GetVarint32 returned %d",
drh9d213ef2004-06-30 04:02:11 +0000478 n1, n2);
479 Tcl_AppendResult(interp, zErr, 0);
480 return TCL_ERROR;
481 }
482 if( in!=out ){
drh65545b52015-01-19 00:35:53 +0000483 sqlite3_snprintf(sizeof(zErr), zErr,
484 "Wrote 0x%016llx and got back 0x%016llx from GetVarint32",
drh9d213ef2004-06-30 04:02:11 +0000485 in, out);
486 Tcl_AppendResult(interp, zErr, 0);
487 return TCL_ERROR;
488 }
489 }
drhd8820e82004-05-18 15:57:42 +0000490
491 /* In order to get realistic timings, run getVarint 19 more times.
492 ** This is because getVarint is called about 20 times more often
493 ** than putVarint.
494 */
495 for(j=0; j<19; j++){
shane3f8d5cf2008-04-24 19:15:09 +0000496 getVarint(zBuf, &out);
drhd8820e82004-05-18 15:57:42 +0000497 }
drh6d2fb152004-05-14 16:50:06 +0000498 in += incr;
499 }
500 return TCL_OK;
501}
drh9b171272004-05-08 02:03:22 +0000502
503/*
danielk197742741be2005-01-08 12:42:39 +0000504** usage: btree_from_db DB-HANDLE
505**
506** This command returns the btree handle for the main database associated
507** with the database-handle passed as the argument. Example usage:
508**
509** sqlite3 db test.db
510** set bt [btree_from_db db]
511*/
mistachkin7617e4a2016-07-28 17:11:20 +0000512static int SQLITE_TCLAPI btree_from_db(
danielk197742741be2005-01-08 12:42:39 +0000513 void *NotUsed,
514 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
515 int argc, /* Number of arguments */
516 const char **argv /* Text of each argument */
517){
518 char zBuf[100];
519 Tcl_CmdInfo info;
520 sqlite3 *db;
521 Btree *pBt;
danielk197763c64f32007-05-17 14:45:12 +0000522 int iDb = 0;
danielk197742741be2005-01-08 12:42:39 +0000523
danielk197763c64f32007-05-17 14:45:12 +0000524 if( argc!=2 && argc!=3 ){
danielk197742741be2005-01-08 12:42:39 +0000525 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk197763c64f32007-05-17 14:45:12 +0000526 " DB-HANDLE ?N?\"", 0);
danielk197742741be2005-01-08 12:42:39 +0000527 return TCL_ERROR;
528 }
529
530 if( 1!=Tcl_GetCommandInfo(interp, argv[1], &info) ){
531 Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"", 0);
532 return TCL_ERROR;
533 }
danielk197763c64f32007-05-17 14:45:12 +0000534 if( argc==3 ){
535 iDb = atoi(argv[2]);
536 }
537
danielk197742741be2005-01-08 12:42:39 +0000538 db = *((sqlite3 **)info.objClientData);
539 assert( db );
540
danielk197763c64f32007-05-17 14:45:12 +0000541 pBt = db->aDb[iDb].pBt;
danielk197742741be2005-01-08 12:42:39 +0000542 sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", pBt);
543 Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
544 return TCL_OK;
545}
546
danielk197717b90b52008-06-06 11:11:25 +0000547/*
548** Usage: btree_ismemdb ID
549**
dan9131ab92016-04-06 18:20:51 +0000550** Return true if the B-Tree is currently stored entirely in memory.
danielk197717b90b52008-06-06 11:11:25 +0000551*/
mistachkin7617e4a2016-07-28 17:11:20 +0000552static int SQLITE_TCLAPI btree_ismemdb(
danielk197717b90b52008-06-06 11:11:25 +0000553 void *NotUsed,
554 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
555 int argc, /* Number of arguments */
556 const char **argv /* Text of each argument */
557){
558 Btree *pBt;
559 int res;
dan9131ab92016-04-06 18:20:51 +0000560 sqlite3_file *pFile;
danielk197717b90b52008-06-06 11:11:25 +0000561
562 if( argc!=2 ){
563 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
564 " ID\"", 0);
565 return TCL_ERROR;
566 }
drhe8f52c52008-07-12 14:52:20 +0000567 pBt = sqlite3TestTextToPtr(argv[1]);
danielk197717b90b52008-06-06 11:11:25 +0000568 sqlite3_mutex_enter(pBt->db->mutex);
569 sqlite3BtreeEnter(pBt);
dan9131ab92016-04-06 18:20:51 +0000570 pFile = sqlite3PagerFile(sqlite3BtreePager(pBt));
571 res = (pFile->pMethods==0);
danielk197717b90b52008-06-06 11:11:25 +0000572 sqlite3BtreeLeave(pBt);
573 sqlite3_mutex_leave(pBt->db->mutex);
574 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(res));
575 return SQLITE_OK;
576}
577
danielk197730548662009-07-09 05:07:37 +0000578/*
579** usage: btree_set_cache_size ID NCACHE
580**
581** Set the size of the cache used by btree $ID.
582*/
mistachkin7617e4a2016-07-28 17:11:20 +0000583static int SQLITE_TCLAPI btree_set_cache_size(
danielk197730548662009-07-09 05:07:37 +0000584 void *NotUsed,
585 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
586 int argc, /* Number of arguments */
587 const char **argv /* Text of each argument */
588){
589 int nCache;
590 Btree *pBt;
591
592 if( argc!=3 ){
593 Tcl_AppendResult(
594 interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", 0);
595 return TCL_ERROR;
596 }
597 pBt = sqlite3TestTextToPtr(argv[1]);
598 if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
599
600 sqlite3_mutex_enter(pBt->db->mutex);
601 sqlite3BtreeEnter(pBt);
602 sqlite3BtreeSetCacheSize(pBt, nCache);
603 sqlite3BtreeLeave(pBt);
604 sqlite3_mutex_leave(pBt->db->mutex);
605 return TCL_OK;
606}
607
dan6b513642015-10-26 16:31:18 +0000608/*
609** usage: btree_insert CSR ?KEY? VALUE
610**
611** Set the size of the cache used by btree $ID.
612*/
mistachkin7617e4a2016-07-28 17:11:20 +0000613static int SQLITE_TCLAPI btree_insert(
dan6b513642015-10-26 16:31:18 +0000614 ClientData clientData,
615 Tcl_Interp *interp,
616 int objc,
617 Tcl_Obj *const objv[]
618){
619 BtCursor *pCur;
620 int rc;
drh8eeb4462016-05-21 20:03:42 +0000621 BtreePayload x;
dan6b513642015-10-26 16:31:18 +0000622
623 if( objc!=4 && objc!=3 ){
624 Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE");
625 return TCL_ERROR;
626 }
627
drh8eeb4462016-05-21 20:03:42 +0000628 memset(&x, 0, sizeof(x));
dan6b513642015-10-26 16:31:18 +0000629 if( objc==4 ){
drh8eeb4462016-05-21 20:03:42 +0000630 if( Tcl_GetIntFromObj(interp, objv[2], &rc) ) return TCL_ERROR;
631 x.nKey = rc;
632 x.pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &x.nData);
dan6b513642015-10-26 16:31:18 +0000633 }else{
drh8eeb4462016-05-21 20:03:42 +0000634 x.pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &rc);
635 x.nKey = rc;
dan6b513642015-10-26 16:31:18 +0000636 }
637 pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
638
dan6a75c8a2015-10-30 20:54:25 +0000639 sqlite3_mutex_enter(pCur->pBtree->db->mutex);
dan6b513642015-10-26 16:31:18 +0000640 sqlite3BtreeEnter(pCur->pBtree);
drh8eeb4462016-05-21 20:03:42 +0000641 rc = sqlite3BtreeInsert(pCur, &x, 0, 0);
dan6b513642015-10-26 16:31:18 +0000642 sqlite3BtreeLeave(pCur->pBtree);
dan6a75c8a2015-10-30 20:54:25 +0000643 sqlite3_mutex_leave(pCur->pBtree->db->mutex);
dan6b513642015-10-26 16:31:18 +0000644
645 Tcl_ResetResult(interp);
646 if( rc ){
647 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
648 return TCL_ERROR;
649 }
650 return TCL_OK;
651}
danielk197730548662009-07-09 05:07:37 +0000652
danielk19771ad7f642005-01-20 05:24:32 +0000653
danielk197742741be2005-01-08 12:42:39 +0000654/*
drh5c4d9702001-08-20 00:33:58 +0000655** Register commands with the TCL interpreter.
656*/
657int Sqlitetest3_Init(Tcl_Interp *interp){
drhc2eef3b2002-08-31 18:53:06 +0000658 static struct {
659 char *zName;
660 Tcl_CmdProc *xProc;
661 } aCmd[] = {
662 { "btree_open", (Tcl_CmdProc*)btree_open },
663 { "btree_close", (Tcl_CmdProc*)btree_close },
664 { "btree_begin_transaction", (Tcl_CmdProc*)btree_begin_transaction },
drhc2eef3b2002-08-31 18:53:06 +0000665 { "btree_pager_stats", (Tcl_CmdProc*)btree_pager_stats },
drhc2eef3b2002-08-31 18:53:06 +0000666 { "btree_cursor", (Tcl_CmdProc*)btree_cursor },
667 { "btree_close_cursor", (Tcl_CmdProc*)btree_close_cursor },
drhc2eef3b2002-08-31 18:53:06 +0000668 { "btree_next", (Tcl_CmdProc*)btree_next },
drhc39e0002004-05-07 23:50:57 +0000669 { "btree_eof", (Tcl_CmdProc*)btree_eof },
drhc2eef3b2002-08-31 18:53:06 +0000670 { "btree_payload_size", (Tcl_CmdProc*)btree_payload_size },
671 { "btree_first", (Tcl_CmdProc*)btree_first },
drh6d2fb152004-05-14 16:50:06 +0000672 { "btree_varint_test", (Tcl_CmdProc*)btree_varint_test },
danielk197742741be2005-01-08 12:42:39 +0000673 { "btree_from_db", (Tcl_CmdProc*)btree_from_db },
danielk197730548662009-07-09 05:07:37 +0000674 { "btree_ismemdb", (Tcl_CmdProc*)btree_ismemdb },
675 { "btree_set_cache_size", (Tcl_CmdProc*)btree_set_cache_size }
drhc2eef3b2002-08-31 18:53:06 +0000676 };
677 int i;
678
679 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
680 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
681 }
danielk1977312d6b32004-06-29 13:18:23 +0000682
dan6b513642015-10-26 16:31:18 +0000683 Tcl_CreateObjCommand(interp, "btree_insert", btree_insert, 0, 0);
684
drh5c4d9702001-08-20 00:33:58 +0000685 return TCL_OK;
686}