blob: 2a41068e5f9bd775a9f58343ffd0496601a2a437 [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"
drh5c4d9702001-08-20 00:33:58 +000018#include "tcl.h"
19#include <stdlib.h>
20#include <string.h>
21
mistachkine84d8d32013-04-29 03:09:10 +000022extern const char *sqlite3ErrName(int);
drh5c4d9702001-08-20 00:33:58 +000023
24/*
drha2451e22007-08-20 23:50:24 +000025** A bogus sqlite3 connection structure for use in the btree
26** tests.
27*/
28static sqlite3 sDb;
29static int nRefSqlite3 = 0;
30
31/*
drh75c014c2010-08-30 15:02:28 +000032** Usage: btree_open FILENAME NCACHE
drh5c4d9702001-08-20 00:33:58 +000033**
34** Open a new database
35*/
36static int btree_open(
37 void *NotUsed,
38 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
39 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +000040 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +000041){
42 Btree *pBt;
drh75c014c2010-08-30 15:02:28 +000043 int rc, nCache;
drh5c4d9702001-08-20 00:33:58 +000044 char zBuf[100];
drhc02a43a2012-01-10 23:18:38 +000045 int n;
46 char *zFilename;
drh75c014c2010-08-30 15:02:28 +000047 if( argc!=3 ){
drh5c4d9702001-08-20 00:33:58 +000048 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh23e11ca2004-05-04 17:27:28 +000049 " FILENAME NCACHE FLAGS\"", 0);
drh5c4d9702001-08-20 00:33:58 +000050 return TCL_ERROR;
51 }
drh23e11ca2004-05-04 17:27:28 +000052 if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
drha2451e22007-08-20 23:50:24 +000053 nRefSqlite3++;
54 if( nRefSqlite3==1 ){
55 sDb.pVfs = sqlite3_vfs_find(0);
danielk197759f8c082008-06-18 17:09:10 +000056 sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
drh27641702007-08-22 02:56:42 +000057 sqlite3_mutex_enter(sDb.mutex);
drha2451e22007-08-20 23:50:24 +000058 }
drh83cc1392012-04-19 18:04:28 +000059 n = (int)strlen(argv[1]);
drhc02a43a2012-01-10 23:18:38 +000060 zFilename = sqlite3_malloc( n+2 );
61 if( zFilename==0 ) return TCL_ERROR;
62 memcpy(zFilename, argv[1], n+1);
63 zFilename[n+1] = 0;
64 rc = sqlite3BtreeOpen(sDb.pVfs, zFilename, &sDb, &pBt, 0,
drh33f4e022007-09-03 15:19:34 +000065 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
drhc02a43a2012-01-10 23:18:38 +000066 sqlite3_free(zFilename);
drh5c4d9702001-08-20 00:33:58 +000067 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +000068 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +000069 return TCL_ERROR;
70 }
drh90f5ecb2004-07-22 01:19:35 +000071 sqlite3BtreeSetCacheSize(pBt, nCache);
drhfe63d1c2004-09-08 20:13:04 +000072 sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pBt);
drh5c4d9702001-08-20 00:33:58 +000073 Tcl_AppendResult(interp, zBuf, 0);
74 return TCL_OK;
75}
76
77/*
78** Usage: btree_close ID
79**
80** Close the given database.
81*/
82static int btree_close(
83 void *NotUsed,
84 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
85 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +000086 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +000087){
88 Btree *pBt;
89 int rc;
90 if( argc!=2 ){
91 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
92 " ID\"", 0);
93 return TCL_ERROR;
94 }
drhe8f52c52008-07-12 14:52:20 +000095 pBt = sqlite3TestTextToPtr(argv[1]);
drh23e11ca2004-05-04 17:27:28 +000096 rc = sqlite3BtreeClose(pBt);
drh5c4d9702001-08-20 00:33:58 +000097 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +000098 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +000099 return TCL_ERROR;
100 }
drha2451e22007-08-20 23:50:24 +0000101 nRefSqlite3--;
102 if( nRefSqlite3==0 ){
drh27641702007-08-22 02:56:42 +0000103 sqlite3_mutex_leave(sDb.mutex);
drha2451e22007-08-20 23:50:24 +0000104 sqlite3_mutex_free(sDb.mutex);
105 sDb.mutex = 0;
drha2451e22007-08-20 23:50:24 +0000106 sDb.pVfs = 0;
107 }
drh5c4d9702001-08-20 00:33:58 +0000108 return TCL_OK;
109}
110
drh27641702007-08-22 02:56:42 +0000111
drh5c4d9702001-08-20 00:33:58 +0000112/*
113** Usage: btree_begin_transaction ID
114**
115** Start a new transaction
116*/
117static int btree_begin_transaction(
118 void *NotUsed,
119 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
120 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000121 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000122){
123 Btree *pBt;
124 int rc;
125 if( argc!=2 ){
126 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
127 " ID\"", 0);
128 return TCL_ERROR;
129 }
drhe8f52c52008-07-12 14:52:20 +0000130 pBt = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000131 sqlite3BtreeEnter(pBt);
danielk197740b38dc2004-06-26 08:38:24 +0000132 rc = sqlite3BtreeBeginTrans(pBt, 1);
drhff0587c2007-08-29 17:43:19 +0000133 sqlite3BtreeLeave(pBt);
drh5c4d9702001-08-20 00:33:58 +0000134 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +0000135 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000136 return TCL_ERROR;
137 }
138 return TCL_OK;
139}
140
141/*
drh5c4d9702001-08-20 00:33:58 +0000142** Usage: btree_pager_stats ID
143**
144** Returns pager statistics
145*/
146static int btree_pager_stats(
147 void *NotUsed,
148 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
149 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000150 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000151){
152 Btree *pBt;
153 int i;
154 int *a;
155
156 if( argc!=2 ){
157 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
158 " ID\"", 0);
159 return TCL_ERROR;
160 }
drhe8f52c52008-07-12 14:52:20 +0000161 pBt = sqlite3TestTextToPtr(argv[1]);
danielk1977f3c62652007-08-30 08:27:39 +0000162
163 /* Normally in this file, with a b-tree handle opened using the
164 ** [btree_open] command it is safe to call sqlite3BtreeEnter() directly.
165 ** But this function is sometimes called with a btree handle obtained
166 ** from an open SQLite connection (using [btree_from_db]). In this case
167 ** we need to obtain the mutex for the controlling SQLite handle before
168 ** it is safe to call sqlite3BtreeEnter().
169 */
drhe5fe6902007-12-07 18:55:28 +0000170 sqlite3_mutex_enter(pBt->db->mutex);
danielk1977f3c62652007-08-30 08:27:39 +0000171
drh27641702007-08-22 02:56:42 +0000172 sqlite3BtreeEnter(pBt);
danielk19773b8a05f2007-03-19 17:44:26 +0000173 a = sqlite3PagerStats(sqlite3BtreePager(pBt));
danielk197742741be2005-01-08 12:42:39 +0000174 for(i=0; i<11; i++){
drh5c4d9702001-08-20 00:33:58 +0000175 static char *zName[] = {
176 "ref", "page", "max", "size", "state", "err",
danielk197742741be2005-01-08 12:42:39 +0000177 "hit", "miss", "ovfl", "read", "write"
drh5c4d9702001-08-20 00:33:58 +0000178 };
179 char zBuf[100];
180 Tcl_AppendElement(interp, zName[i]);
drhfe63d1c2004-09-08 20:13:04 +0000181 sqlite3_snprintf(sizeof(zBuf), zBuf,"%d",a[i]);
drh5c4d9702001-08-20 00:33:58 +0000182 Tcl_AppendElement(interp, zBuf);
183 }
drh27641702007-08-22 02:56:42 +0000184 sqlite3BtreeLeave(pBt);
danielk1977f3c62652007-08-30 08:27:39 +0000185
186 /* Release the mutex on the SQLite handle that controls this b-tree */
drhe5fe6902007-12-07 18:55:28 +0000187 sqlite3_mutex_leave(pBt->db->mutex);
drh5c4d9702001-08-20 00:33:58 +0000188 return TCL_OK;
189}
190
191/*
drhecdc7532001-09-23 02:35:53 +0000192** Usage: btree_cursor ID TABLENUM WRITEABLE
drh5c4d9702001-08-20 00:33:58 +0000193**
194** Create a new cursor. Return the ID for the cursor.
195*/
196static int btree_cursor(
197 void *NotUsed,
198 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
199 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000200 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000201){
202 Btree *pBt;
203 int iTable;
204 BtCursor *pCur;
dan93a696f2010-01-07 11:27:30 +0000205 int rc = SQLITE_OK;
drhecdc7532001-09-23 02:35:53 +0000206 int wrFlag;
drh5c4d9702001-08-20 00:33:58 +0000207 char zBuf[30];
208
drhecdc7532001-09-23 02:35:53 +0000209 if( argc!=4 ){
drh5c4d9702001-08-20 00:33:58 +0000210 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drhecdc7532001-09-23 02:35:53 +0000211 " ID TABLENUM WRITEABLE\"", 0);
drh5c4d9702001-08-20 00:33:58 +0000212 return TCL_ERROR;
213 }
drhe8f52c52008-07-12 14:52:20 +0000214 pBt = sqlite3TestTextToPtr(argv[1]);
drh5c4d9702001-08-20 00:33:58 +0000215 if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR;
drhecdc7532001-09-23 02:35:53 +0000216 if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR;
danfd261ec2015-10-22 20:54:33 +0000217 if( wrFlag ) wrFlag = BTREE_WRCSR;
danielk1977cd3e8f72008-03-25 09:47:35 +0000218 pCur = (BtCursor *)ckalloc(sqlite3BtreeCursorSize());
219 memset(pCur, 0, sqlite3BtreeCursorSize());
dan6a75c8a2015-10-30 20:54:25 +0000220 sqlite3_mutex_enter(pBt->db->mutex);
drhff0587c2007-08-29 17:43:19 +0000221 sqlite3BtreeEnter(pBt);
dan93a696f2010-01-07 11:27:30 +0000222#ifndef SQLITE_OMIT_SHARED_CACHE
dan6b513642015-10-26 16:31:18 +0000223 rc = sqlite3BtreeLockTable(pBt, iTable, !!wrFlag);
dan93a696f2010-01-07 11:27:30 +0000224#endif
danielk197796d48e92009-06-29 06:00:37 +0000225 if( rc==SQLITE_OK ){
226 rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, 0, pCur);
227 }
drhff0587c2007-08-29 17:43:19 +0000228 sqlite3BtreeLeave(pBt);
dan6a75c8a2015-10-30 20:54:25 +0000229 sqlite3_mutex_leave(pBt->db->mutex);
drh5c4d9702001-08-20 00:33:58 +0000230 if( rc ){
danielk1977cd3e8f72008-03-25 09:47:35 +0000231 ckfree((char *)pCur);
mistachkine84d8d32013-04-29 03:09:10 +0000232 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000233 return TCL_ERROR;
234 }
drhfe63d1c2004-09-08 20:13:04 +0000235 sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur);
drh5c4d9702001-08-20 00:33:58 +0000236 Tcl_AppendResult(interp, zBuf, 0);
237 return SQLITE_OK;
238}
239
240/*
241** Usage: btree_close_cursor ID
242**
243** Close a cursor opened using btree_cursor.
244*/
245static int btree_close_cursor(
246 void *NotUsed,
247 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
248 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000249 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000250){
251 BtCursor *pCur;
drhff0587c2007-08-29 17:43:19 +0000252 Btree *pBt;
drh5c4d9702001-08-20 00:33:58 +0000253 int rc;
254
255 if( argc!=2 ){
256 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
257 " ID\"", 0);
258 return TCL_ERROR;
259 }
drhe8f52c52008-07-12 14:52:20 +0000260 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000261 pBt = pCur->pBtree;
dan6a75c8a2015-10-30 20:54:25 +0000262 sqlite3_mutex_enter(pBt->db->mutex);
drhff0587c2007-08-29 17:43:19 +0000263 sqlite3BtreeEnter(pBt);
drh23e11ca2004-05-04 17:27:28 +0000264 rc = sqlite3BtreeCloseCursor(pCur);
drhff0587c2007-08-29 17:43:19 +0000265 sqlite3BtreeLeave(pBt);
dan6a75c8a2015-10-30 20:54:25 +0000266 sqlite3_mutex_leave(pBt->db->mutex);
danielk1977cd3e8f72008-03-25 09:47:35 +0000267 ckfree((char *)pCur);
drh5c4d9702001-08-20 00:33:58 +0000268 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000269 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000270 return TCL_ERROR;
271 }
272 return SQLITE_OK;
273}
274
275/*
drh5c4d9702001-08-20 00:33:58 +0000276** Usage: btree_next ID
277**
drh2dcc9aa2002-12-04 13:40:25 +0000278** Move the cursor to the next entry in the table. Return 0 on success
279** or 1 if the cursor was already on the last entry in the table or if
280** the table is empty.
drh5c4d9702001-08-20 00:33:58 +0000281*/
282static int btree_next(
283 void *NotUsed,
284 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
285 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000286 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000287){
288 BtCursor *pCur;
289 int rc;
drh0de8c112002-07-06 16:32:14 +0000290 int res = 0;
291 char zBuf[100];
drh5c4d9702001-08-20 00:33:58 +0000292
293 if( argc!=2 ){
294 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
295 " ID\"", 0);
296 return TCL_ERROR;
297 }
drhe8f52c52008-07-12 14:52:20 +0000298 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000299 sqlite3BtreeEnter(pCur->pBtree);
drh4fc0fb42004-05-07 02:26:28 +0000300 rc = sqlite3BtreeNext(pCur, &res);
drhff0587c2007-08-29 17:43:19 +0000301 sqlite3BtreeLeave(pCur->pBtree);
drh5c4d9702001-08-20 00:33:58 +0000302 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000303 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000304 return TCL_ERROR;
305 }
drhfe63d1c2004-09-08 20:13:04 +0000306 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
drh0de8c112002-07-06 16:32:14 +0000307 Tcl_AppendResult(interp, zBuf, 0);
308 return SQLITE_OK;
309}
310
311/*
312** Usage: btree_first ID
313**
drh2dcc9aa2002-12-04 13:40:25 +0000314** Move the cursor to the first entry in the table. Return 0 if the
315** cursor was left point to something and 1 if the table is empty.
drh0de8c112002-07-06 16:32:14 +0000316*/
317static int btree_first(
318 void *NotUsed,
319 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
320 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000321 const char **argv /* Text of each argument */
drh0de8c112002-07-06 16:32:14 +0000322){
323 BtCursor *pCur;
324 int rc;
325 int res = 0;
326 char zBuf[100];
327
328 if( argc!=2 ){
329 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
330 " ID\"", 0);
331 return TCL_ERROR;
332 }
drhe8f52c52008-07-12 14:52:20 +0000333 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000334 sqlite3BtreeEnter(pCur->pBtree);
drh4fc0fb42004-05-07 02:26:28 +0000335 rc = sqlite3BtreeFirst(pCur, &res);
drhff0587c2007-08-29 17:43:19 +0000336 sqlite3BtreeLeave(pCur->pBtree);
drh0de8c112002-07-06 16:32:14 +0000337 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000338 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh0de8c112002-07-06 16:32:14 +0000339 return TCL_ERROR;
340 }
drhfe63d1c2004-09-08 20:13:04 +0000341 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
drh0de8c112002-07-06 16:32:14 +0000342 Tcl_AppendResult(interp, zBuf, 0);
drh5c4d9702001-08-20 00:33:58 +0000343 return SQLITE_OK;
344}
345
346/*
drhc39e0002004-05-07 23:50:57 +0000347** Usage: btree_eof ID
348**
349** Return TRUE if the given cursor is not pointing at a valid entry.
350** Return FALSE if the cursor does point to a valid entry.
351*/
352static int btree_eof(
353 void *NotUsed,
354 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
355 int argc, /* Number of arguments */
356 const char **argv /* Text of each argument */
357){
358 BtCursor *pCur;
drhff0587c2007-08-29 17:43:19 +0000359 int rc;
drhc39e0002004-05-07 23:50:57 +0000360 char zBuf[50];
361
362 if( argc!=2 ){
363 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
364 " ID\"", 0);
365 return TCL_ERROR;
366 }
drhe8f52c52008-07-12 14:52:20 +0000367 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000368 sqlite3BtreeEnter(pCur->pBtree);
369 rc = sqlite3BtreeEof(pCur);
370 sqlite3BtreeLeave(pCur->pBtree);
371 sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc);
drhc39e0002004-05-07 23:50:57 +0000372 Tcl_AppendResult(interp, zBuf, 0);
373 return SQLITE_OK;
374}
375
376/*
drh0de8c112002-07-06 16:32:14 +0000377** Usage: btree_payload_size ID
378**
379** Return the number of bytes of payload
380*/
381static int btree_payload_size(
382 void *NotUsed,
383 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
384 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000385 const char **argv /* Text of each argument */
drh0de8c112002-07-06 16:32:14 +0000386){
387 BtCursor *pCur;
drh4fc0fb42004-05-07 02:26:28 +0000388 int n2;
drha34b6762004-05-07 13:30:42 +0000389 u64 n1;
drh0de8c112002-07-06 16:32:14 +0000390 char zBuf[50];
391
392 if( argc!=2 ){
393 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
394 " ID\"", 0);
395 return TCL_ERROR;
396 }
drhe8f52c52008-07-12 14:52:20 +0000397 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000398 sqlite3BtreeEnter(pCur->pBtree);
danielk197730548662009-07-09 05:07:37 +0000399
400 /* The cursor may be in "require-seek" state. If this is the case, the
401 ** call to BtreeDataSize() will fix it. */
402 sqlite3BtreeDataSize(pCur, (u32*)&n2);
shanecbcadd42009-07-09 03:20:46 +0000403 if( pCur->apPage[pCur->iPage]->intKey ){
drha34b6762004-05-07 13:30:42 +0000404 n1 = 0;
405 }else{
drh03d847e2005-12-09 20:21:58 +0000406 sqlite3BtreeKeySize(pCur, (i64*)&n1);
drha34b6762004-05-07 13:30:42 +0000407 }
drhff0587c2007-08-29 17:43:19 +0000408 sqlite3BtreeLeave(pCur->pBtree);
drhfe63d1c2004-09-08 20:13:04 +0000409 sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", (int)(n1+n2));
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*/
428static int btree_varint_test(
429 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*/
512static int btree_from_db(
513 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**
550** Return true if the B-Tree is in-memory.
551*/
552static int btree_ismemdb(
553 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;
560
561 if( argc!=2 ){
562 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
563 " ID\"", 0);
564 return TCL_ERROR;
565 }
drhe8f52c52008-07-12 14:52:20 +0000566 pBt = sqlite3TestTextToPtr(argv[1]);
danielk197717b90b52008-06-06 11:11:25 +0000567 sqlite3_mutex_enter(pBt->db->mutex);
568 sqlite3BtreeEnter(pBt);
569 res = sqlite3PagerIsMemdb(sqlite3BtreePager(pBt));
570 sqlite3BtreeLeave(pBt);
571 sqlite3_mutex_leave(pBt->db->mutex);
572 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(res));
573 return SQLITE_OK;
574}
575
danielk197730548662009-07-09 05:07:37 +0000576/*
577** usage: btree_set_cache_size ID NCACHE
578**
579** Set the size of the cache used by btree $ID.
580*/
581static int btree_set_cache_size(
582 void *NotUsed,
583 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
584 int argc, /* Number of arguments */
585 const char **argv /* Text of each argument */
586){
587 int nCache;
588 Btree *pBt;
589
590 if( argc!=3 ){
591 Tcl_AppendResult(
592 interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", 0);
593 return TCL_ERROR;
594 }
595 pBt = sqlite3TestTextToPtr(argv[1]);
596 if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
597
598 sqlite3_mutex_enter(pBt->db->mutex);
599 sqlite3BtreeEnter(pBt);
600 sqlite3BtreeSetCacheSize(pBt, nCache);
601 sqlite3BtreeLeave(pBt);
602 sqlite3_mutex_leave(pBt->db->mutex);
603 return TCL_OK;
604}
605
dan6b513642015-10-26 16:31:18 +0000606/*
607** usage: btree_insert CSR ?KEY? VALUE
608**
609** Set the size of the cache used by btree $ID.
610*/
611static int btree_insert(
612 ClientData clientData,
613 Tcl_Interp *interp,
614 int objc,
615 Tcl_Obj *const objv[]
616){
617 BtCursor *pCur;
618 int rc;
dan6b513642015-10-26 16:31:18 +0000619 void *pKey = 0;
620 int nKey = 0;
621 void *pData = 0;
622 int nData = 0;
623
624 if( objc!=4 && objc!=3 ){
625 Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE");
626 return TCL_ERROR;
627 }
628
629 if( objc==4 ){
630 if( Tcl_GetIntFromObj(interp, objv[2], &nKey) ) return TCL_ERROR;
631 pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &nData);
632 }else{
633 pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &nKey);
634 }
635 pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
636
dan6a75c8a2015-10-30 20:54:25 +0000637 sqlite3_mutex_enter(pCur->pBtree->db->mutex);
dan6b513642015-10-26 16:31:18 +0000638 sqlite3BtreeEnter(pCur->pBtree);
dan433d5aa2015-10-26 16:34:13 +0000639 rc = sqlite3BtreeInsert(pCur, pKey, nKey, pData, nData, 0, 0, 0);
dan6b513642015-10-26 16:31:18 +0000640 sqlite3BtreeLeave(pCur->pBtree);
dan6a75c8a2015-10-30 20:54:25 +0000641 sqlite3_mutex_leave(pCur->pBtree->db->mutex);
dan6b513642015-10-26 16:31:18 +0000642
643 Tcl_ResetResult(interp);
644 if( rc ){
645 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
646 return TCL_ERROR;
647 }
648 return TCL_OK;
649}
danielk197730548662009-07-09 05:07:37 +0000650
danielk19771ad7f642005-01-20 05:24:32 +0000651
danielk197742741be2005-01-08 12:42:39 +0000652/*
drh5c4d9702001-08-20 00:33:58 +0000653** Register commands with the TCL interpreter.
654*/
655int Sqlitetest3_Init(Tcl_Interp *interp){
drhc2eef3b2002-08-31 18:53:06 +0000656 static struct {
657 char *zName;
658 Tcl_CmdProc *xProc;
659 } aCmd[] = {
660 { "btree_open", (Tcl_CmdProc*)btree_open },
661 { "btree_close", (Tcl_CmdProc*)btree_close },
662 { "btree_begin_transaction", (Tcl_CmdProc*)btree_begin_transaction },
drhc2eef3b2002-08-31 18:53:06 +0000663 { "btree_pager_stats", (Tcl_CmdProc*)btree_pager_stats },
drhc2eef3b2002-08-31 18:53:06 +0000664 { "btree_cursor", (Tcl_CmdProc*)btree_cursor },
665 { "btree_close_cursor", (Tcl_CmdProc*)btree_close_cursor },
drhc2eef3b2002-08-31 18:53:06 +0000666 { "btree_next", (Tcl_CmdProc*)btree_next },
drhc39e0002004-05-07 23:50:57 +0000667 { "btree_eof", (Tcl_CmdProc*)btree_eof },
drhc2eef3b2002-08-31 18:53:06 +0000668 { "btree_payload_size", (Tcl_CmdProc*)btree_payload_size },
669 { "btree_first", (Tcl_CmdProc*)btree_first },
drh6d2fb152004-05-14 16:50:06 +0000670 { "btree_varint_test", (Tcl_CmdProc*)btree_varint_test },
danielk197742741be2005-01-08 12:42:39 +0000671 { "btree_from_db", (Tcl_CmdProc*)btree_from_db },
danielk197730548662009-07-09 05:07:37 +0000672 { "btree_ismemdb", (Tcl_CmdProc*)btree_ismemdb },
673 { "btree_set_cache_size", (Tcl_CmdProc*)btree_set_cache_size }
drhc2eef3b2002-08-31 18:53:06 +0000674 };
675 int i;
676
677 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
678 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
679 }
danielk1977312d6b32004-06-29 13:18:23 +0000680
dan6b513642015-10-26 16:31:18 +0000681 Tcl_CreateObjCommand(interp, "btree_insert", btree_insert, 0, 0);
682
drh5c4d9702001-08-20 00:33:58 +0000683 return TCL_OK;
684}