1 /* 2 ** This C program extracts all "words" from an input document and adds them 3 ** to an SQLite database. A "word" is any contiguous sequence of alphabetic 4 ** characters. All digits, punctuation, and whitespace characters are 5 ** word separators. The database stores a single entry for each distinct 6 ** word together with a count of the number of occurrences of that word. 7 ** A fresh database is created automatically on each run. 8 ** 9 ** wordcount DATABASE INPUTFILE 10 ** 11 ** The INPUTFILE name can be omitted, in which case input it taken from 12 ** standard input. 13 ** 14 ** Option: 15 ** 16 ** --without-rowid Use a WITHOUT ROWID table to store the words. 17 ** --insert Use INSERT mode (the default) 18 ** --replace Use REPLACE mode 19 ** --select Use SELECT mode 20 ** --update Use UPDATE mode 21 ** --delete Use DELETE mode 22 ** --query Use QUERY mode 23 ** --nocase Add the NOCASE collating sequence to the words. 24 ** --trace Enable sqlite3_trace() output. 25 ** --summary Show summary information on the collected data. 26 ** --stats Show sqlite3_status() results at the end. 27 ** --pagesize NNN Use a page size of NNN 28 ** --cachesize NNN Use a cache size of NNN 29 ** --commit NNN Commit after every NNN operations 30 ** --nosync Use PRAGMA synchronous=OFF 31 ** --journal MMMM Use PRAGMA journal_mode=MMMM 32 ** --timer Time the operation of this program 33 ** --tag NAME Tag all output using NAME. Use only stdout. 34 ** 35 ** Modes: 36 ** 37 ** Insert mode means: 38 ** (1) INSERT OR IGNORE INTO wordcount VALUES($new,1) 39 ** (2) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new -- if (1) is a noop 40 ** 41 ** Update mode means: 42 ** (1) INSERT OR IGNORE INTO wordcount VALUES($new,0) 43 ** (2) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new 44 ** 45 ** Replace mode means: 46 ** (1) REPLACE INTO wordcount 47 ** VALUES($new,ifnull((SELECT cnt FROM wordcount WHERE word=$new),0)+1); 48 ** 49 ** Select mode means: 50 ** (1) SELECT 1 FROM wordcount WHERE word=$new 51 ** (2) INSERT INTO wordcount VALUES($new,1) -- if (1) returns nothing 52 ** (3) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new --if (1) return TRUE 53 ** 54 ** Delete mode means: 55 ** (1) DELETE FROM wordcount WHERE word=$new 56 ** 57 ** Query mode means: 58 ** (1) SELECT cnt FROM wordcount WHERE word=$new 59 ** 60 ** Note that delete mode and query mode are only useful for preexisting 61 ** databases. The wordcount table is created using IF NOT EXISTS so this 62 ** utility can be run multiple times on the same database file. The 63 ** --without-rowid, --nocase, and --pagesize parameters are only effective 64 ** when creating a new database and are harmless no-ops on preexisting 65 ** databases. 66 ** 67 ****************************************************************************** 68 ** 69 ** Compile as follows: 70 ** 71 ** gcc -I. wordcount.c sqlite3.c -ldl -lpthreads 72 ** 73 ** Or: 74 ** 75 ** gcc -I. -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ 76 ** wordcount.c sqlite3.c 77 */ 78 #include <stdio.h> 79 #include <string.h> 80 #include <ctype.h> 81 #include <stdlib.h> 82 #include <stdarg.h> 83 #include "sqlite3.h" 84 #define ISALPHA(X) isalpha((unsigned char)(X)) 85 86 /* Output tag */ 87 char *zTag = "--"; 88 89 /* Return the current wall-clock time */ 90 static sqlite3_int64 realTime(void){ 91 static sqlite3_vfs *clockVfs = 0; 92 sqlite3_int64 t; 93 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 94 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ 95 clockVfs->xCurrentTimeInt64(clockVfs, &t); 96 }else{ 97 double r; 98 clockVfs->xCurrentTime(clockVfs, &r); 99 t = (sqlite3_int64)(r*86400000.0); 100 } 101 return t; 102 } 103 104 /* Print an error message and exit */ 105 static void fatal_error(const char *zMsg, ...){ 106 va_list ap; 107 va_start(ap, zMsg); 108 vfprintf(stderr, zMsg, ap); 109 va_end(ap); 110 exit(1); 111 } 112 113 /* The sqlite3_trace() callback function */ 114 static void traceCallback(void *NotUsed, const char *zSql){ 115 printf("%s;\n", zSql); 116 } 117 118 /* An sqlite3_exec() callback that prints results on standard output, 119 ** each column separated by a single space. */ 120 static int printResult(void *NotUsed, int nArg, char **azArg, char **azNm){ 121 int i; 122 printf("%s", zTag); 123 for(i=0; i<nArg; i++){ 124 printf(" %s", azArg[i] ? azArg[i] : "(null)"); 125 } 126 printf("\n"); 127 return 0; 128 } 129 130 131 /* 132 ** Add one character to a hash 133 */ 134 static void addCharToHash(unsigned int *a, unsigned char x){ 135 if( a[0]<4 ){ 136 a[1] = (a[1]<<8) | x; 137 a[0]++; 138 }else{ 139 a[2] = (a[2]<<8) | x; 140 a[0]++; 141 if( a[0]==8 ){ 142 a[3] += a[1] + a[4]; 143 a[4] += a[2] + a[3]; 144 a[0] = a[1] = a[2] = 0; 145 } 146 } 147 } 148 149 /* 150 ** Compute the final hash value. 151 */ 152 static void finalHash(unsigned int *a, char *z){ 153 a[3] += a[1] + a[4] + a[0]; 154 a[4] += a[2] + a[3]; 155 sqlite3_snprintf(17, z, "%08x%08x", a[3], a[4]); 156 } 157 158 159 /* 160 ** Implementation of a checksum() aggregate SQL function 161 */ 162 static void checksumStep( 163 sqlite3_context *context, 164 int argc, 165 sqlite3_value **argv 166 ){ 167 const unsigned char *zVal; 168 int nVal, i, j; 169 unsigned int *a; 170 a = (unsigned*)sqlite3_aggregate_context(context, sizeof(unsigned int)*5); 171 172 if( a ){ 173 for(i=0; i<argc; i++){ 174 nVal = sqlite3_value_bytes(argv[i]); 175 zVal = (const unsigned char*)sqlite3_value_text(argv[i]); 176 if( zVal ) for(j=0; j<nVal; j++) addCharToHash(a, zVal[j]); 177 addCharToHash(a, '|'); 178 } 179 addCharToHash(a, '\n'); 180 } 181 } 182 static void checksumFinalize(sqlite3_context *context){ 183 unsigned int *a; 184 char zResult[24]; 185 a = sqlite3_aggregate_context(context, 0); 186 if( a ){ 187 finalHash(a, zResult); 188 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); 189 } 190 } 191 192 193 /* Define operating modes */ 194 #define MODE_INSERT 0 195 #define MODE_REPLACE 1 196 #define MODE_SELECT 2 197 #define MODE_UPDATE 3 198 #define MODE_DELETE 4 199 #define MODE_QUERY 5 200 201 int main(int argc, char **argv){ 202 const char *zFileToRead = 0; /* Input file. NULL for stdin */ 203 const char *zDbName = 0; /* Name of the database file to create */ 204 int useWithoutRowid = 0; /* True for --without-rowid */ 205 int iMode = MODE_INSERT; /* One of MODE_xxxxx */ 206 int useNocase = 0; /* True for --nocase */ 207 int doTrace = 0; /* True for --trace */ 208 int showStats = 0; /* True for --stats */ 209 int showSummary = 0; /* True for --summary */ 210 int showTimer = 0; /* True for --timer */ 211 int cacheSize = 0; /* Desired cache size. 0 means default */ 212 int pageSize = 0; /* Desired page size. 0 means default */ 213 int commitInterval = 0; /* How often to commit. 0 means never */ 214 int noSync = 0; /* True for --nosync */ 215 const char *zJMode = 0; /* Journal mode */ 216 int nOp = 0; /* Operation counter */ 217 int i, j; /* Loop counters */ 218 sqlite3 *db; /* The SQLite database connection */ 219 char *zSql; /* Constructed SQL statement */ 220 sqlite3_stmt *pInsert = 0; /* The INSERT statement */ 221 sqlite3_stmt *pUpdate = 0; /* The UPDATE statement */ 222 sqlite3_stmt *pSelect = 0; /* The SELECT statement */ 223 sqlite3_stmt *pDelete = 0; /* The DELETE statement */ 224 FILE *in; /* The open input file */ 225 int rc; /* Return code from an SQLite interface */ 226 int iCur, iHiwtr; /* Statistics values, current and "highwater" */ 227 FILE *pTimer = stderr; /* Output channel for the timer */ 228 sqlite3_int64 sumCnt = 0; /* Sum in QUERY mode */ 229 sqlite3_int64 startTime; 230 char zInput[2000]; /* A single line of input */ 231 232 /* Process command-line arguments */ 233 for(i=1; i<argc; i++){ 234 const char *z = argv[i]; 235 if( z[0]=='-' ){ 236 do{ z++; }while( z[0]=='-' ); 237 if( strcmp(z,"without-rowid")==0 ){ 238 useWithoutRowid = 1; 239 }else if( strcmp(z,"replace")==0 ){ 240 iMode = MODE_REPLACE; 241 }else if( strcmp(z,"select")==0 ){ 242 iMode = MODE_SELECT; 243 }else if( strcmp(z,"insert")==0 ){ 244 iMode = MODE_INSERT; 245 }else if( strcmp(z,"update")==0 ){ 246 iMode = MODE_UPDATE; 247 }else if( strcmp(z,"delete")==0 ){ 248 iMode = MODE_DELETE; 249 }else if( strcmp(z,"query")==0 ){ 250 iMode = MODE_QUERY; 251 }else if( strcmp(z,"nocase")==0 ){ 252 useNocase = 1; 253 }else if( strcmp(z,"trace")==0 ){ 254 doTrace = 1; 255 }else if( strcmp(z,"nosync")==0 ){ 256 noSync = 1; 257 }else if( strcmp(z,"stats")==0 ){ 258 showStats = 1; 259 }else if( strcmp(z,"summary")==0 ){ 260 showSummary = 1; 261 }else if( strcmp(z,"timer")==0 ){ 262 showTimer = i; 263 }else if( strcmp(z,"cachesize")==0 && i<argc-1 ){ 264 i++; 265 cacheSize = atoi(argv[i]); 266 }else if( strcmp(z,"pagesize")==0 && i<argc-1 ){ 267 i++; 268 pageSize = atoi(argv[i]); 269 }else if( strcmp(z,"commit")==0 && i<argc-1 ){ 270 i++; 271 commitInterval = atoi(argv[i]); 272 }else if( strcmp(z,"journal")==0 && i<argc-1 ){ 273 zJMode = argv[++i]; 274 }else if( strcmp(z,"tag")==0 && i<argc-1 ){ 275 zTag = argv[++i]; 276 pTimer = stdout; 277 }else{ 278 fatal_error("unknown option: %s\n", argv[i]); 279 } 280 }else if( zDbName==0 ){ 281 zDbName = argv[i]; 282 }else if( zFileToRead==0 ){ 283 zFileToRead = argv[i]; 284 }else{ 285 fatal_error("surplus argument: %s\n", argv[i]); 286 } 287 } 288 if( zDbName==0 ){ 289 fatal_error("Usage: %s [--options] DATABASE [INPUTFILE]\n", argv[0]); 290 } 291 startTime = realTime(); 292 293 /* Open the database and the input file */ 294 if( sqlite3_open(zDbName, &db) ){ 295 fatal_error("Cannot open database file: %s\n", zDbName); 296 } 297 if( zFileToRead ){ 298 in = fopen(zFileToRead, "rb"); 299 if( in==0 ){ 300 fatal_error("Could not open input file \"%s\"\n", zFileToRead); 301 } 302 }else{ 303 in = stdin; 304 } 305 306 /* Set database connection options */ 307 if( doTrace ) sqlite3_trace(db, traceCallback, 0); 308 if( pageSize ){ 309 zSql = sqlite3_mprintf("PRAGMA page_size=%d", pageSize); 310 sqlite3_exec(db, zSql, 0, 0, 0); 311 sqlite3_free(zSql); 312 } 313 if( cacheSize ){ 314 zSql = sqlite3_mprintf("PRAGMA cache_size=%d", cacheSize); 315 sqlite3_exec(db, zSql, 0, 0, 0); 316 sqlite3_free(zSql); 317 } 318 if( noSync ) sqlite3_exec(db, "PRAGMA synchronous=OFF", 0, 0, 0); 319 if( zJMode ){ 320 zSql = sqlite3_mprintf("PRAGMA journal_mode=%s", zJMode); 321 sqlite3_exec(db, zSql, 0, 0, 0); 322 sqlite3_free(zSql); 323 } 324 325 326 /* Construct the "wordcount" table into which to put the words */ 327 if( sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0) ){ 328 fatal_error("Could not start a transaction\n"); 329 } 330 zSql = sqlite3_mprintf( 331 "CREATE TABLE IF NOT EXISTS wordcount(\n" 332 " word TEXT PRIMARY KEY COLLATE %s,\n" 333 " cnt INTEGER\n" 334 ")%s", 335 useNocase ? "nocase" : "binary", 336 useWithoutRowid ? " WITHOUT ROWID" : "" 337 ); 338 if( zSql==0 ) fatal_error("out of memory\n"); 339 rc = sqlite3_exec(db, zSql, 0, 0, 0); 340 if( rc ) fatal_error("Could not create the wordcount table: %s.\n", 341 sqlite3_errmsg(db)); 342 sqlite3_free(zSql); 343 344 /* Prepare SQL statements that will be needed */ 345 if( iMode==MODE_QUERY ){ 346 rc = sqlite3_prepare_v2(db, 347 "SELECT cnt FROM wordcount WHERE word=?1", 348 -1, &pSelect, 0); 349 if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n", 350 sqlite3_errmsg(db)); 351 } 352 if( iMode==MODE_SELECT ){ 353 rc = sqlite3_prepare_v2(db, 354 "SELECT 1 FROM wordcount WHERE word=?1", 355 -1, &pSelect, 0); 356 if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n", 357 sqlite3_errmsg(db)); 358 rc = sqlite3_prepare_v2(db, 359 "INSERT INTO wordcount(word,cnt) VALUES(?1,1)", 360 -1, &pInsert, 0); 361 if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", 362 sqlite3_errmsg(db)); 363 } 364 if( iMode==MODE_SELECT || iMode==MODE_UPDATE || iMode==MODE_INSERT ){ 365 rc = sqlite3_prepare_v2(db, 366 "UPDATE wordcount SET cnt=cnt+1 WHERE word=?1", 367 -1, &pUpdate, 0); 368 if( rc ) fatal_error("Could not prepare the UPDATE statement: %s\n", 369 sqlite3_errmsg(db)); 370 } 371 if( iMode==MODE_INSERT ){ 372 rc = sqlite3_prepare_v2(db, 373 "INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,1)", 374 -1, &pInsert, 0); 375 if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", 376 sqlite3_errmsg(db)); 377 } 378 if( iMode==MODE_UPDATE ){ 379 rc = sqlite3_prepare_v2(db, 380 "INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,0)", 381 -1, &pInsert, 0); 382 if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", 383 sqlite3_errmsg(db)); 384 } 385 if( iMode==MODE_REPLACE ){ 386 rc = sqlite3_prepare_v2(db, 387 "REPLACE INTO wordcount(word,cnt)" 388 "VALUES(?1,coalesce((SELECT cnt FROM wordcount WHERE word=?1),0)+1)", 389 -1, &pInsert, 0); 390 if( rc ) fatal_error("Could not prepare the REPLACE statement: %s\n", 391 sqlite3_errmsg(db)); 392 } 393 if( iMode==MODE_DELETE ){ 394 rc = sqlite3_prepare_v2(db, 395 "DELETE FROM wordcount WHERE word=?1", 396 -1, &pDelete, 0); 397 if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n", 398 sqlite3_errmsg(db)); 399 } 400 401 /* Process the input file */ 402 while( fgets(zInput, sizeof(zInput), in) ){ 403 for(i=0; zInput[i]; i++){ 404 if( !ISALPHA(zInput[i]) ) continue; 405 for(j=i+1; ISALPHA(zInput[j]); j++){} 406 407 /* Found a new word at zInput[i] that is j-i bytes long. 408 ** Process it into the wordcount table. */ 409 if( iMode==MODE_DELETE ){ 410 sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC); 411 if( sqlite3_step(pDelete)!=SQLITE_DONE ){ 412 fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db)); 413 } 414 sqlite3_reset(pDelete); 415 }else if( iMode==MODE_SELECT ){ 416 sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC); 417 rc = sqlite3_step(pSelect); 418 sqlite3_reset(pSelect); 419 if( rc==SQLITE_ROW ){ 420 sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC); 421 if( sqlite3_step(pUpdate)!=SQLITE_DONE ){ 422 fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db)); 423 } 424 sqlite3_reset(pUpdate); 425 }else if( rc==SQLITE_DONE ){ 426 sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC); 427 if( sqlite3_step(pInsert)!=SQLITE_DONE ){ 428 fatal_error("Insert failed: %s\n", sqlite3_errmsg(db)); 429 } 430 sqlite3_reset(pInsert); 431 }else{ 432 fatal_error("SELECT failed: %s\n", sqlite3_errmsg(db)); 433 } 434 }else if( iMode==MODE_QUERY ){ 435 sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC); 436 if( sqlite3_step(pSelect)==SQLITE_ROW ){ 437 sumCnt += sqlite3_column_int64(pSelect, 0); 438 } 439 sqlite3_reset(pSelect); 440 }else{ 441 sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC); 442 if( sqlite3_step(pInsert)!=SQLITE_DONE ){ 443 fatal_error("INSERT failed: %s\n", sqlite3_errmsg(db)); 444 } 445 sqlite3_reset(pInsert); 446 if( iMode==MODE_UPDATE 447 || (iMode==MODE_INSERT && sqlite3_changes(db)==0) 448 ){ 449 sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC); 450 if( sqlite3_step(pUpdate)!=SQLITE_DONE ){ 451 fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db)); 452 } 453 sqlite3_reset(pUpdate); 454 } 455 } 456 i = j-1; 457 458 /* Increment the operation counter. Do a COMMIT if it is time. */ 459 nOp++; 460 if( commitInterval>0 && (nOp%commitInterval)==0 ){ 461 sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0); 462 } 463 } 464 } 465 sqlite3_exec(db, "COMMIT", 0, 0, 0); 466 if( zFileToRead ) fclose(in); 467 sqlite3_finalize(pInsert); 468 sqlite3_finalize(pUpdate); 469 sqlite3_finalize(pSelect); 470 sqlite3_finalize(pDelete); 471 472 if( iMode==MODE_QUERY ){ 473 printf("%s sum of cnt: %lld\n", zTag, sumCnt); 474 rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1, 475 &pSelect, 0); 476 if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){ 477 printf("%s double-check: %lld\n", zTag, sqlite3_column_int64(pSelect, 0)); 478 } 479 sqlite3_finalize(pSelect); 480 } 481 482 483 if( showTimer ){ 484 sqlite3_int64 elapseTime = realTime() - startTime; 485 fprintf(pTimer, "%3d.%03d wordcount", (int)(elapseTime/1000), 486 (int)(elapseTime%1000)); 487 for(i=1; i<argc; i++) if( i!=showTimer ) fprintf(pTimer, " %s", argv[i]); 488 fprintf(pTimer, "\n"); 489 } 490 491 if( showSummary ){ 492 sqlite3_create_function(db, "checksum", -1, SQLITE_UTF8, 0, 493 0, checksumStep, checksumFinalize); 494 sqlite3_exec(db, 495 "SELECT 'count(*): ', count(*) FROM wordcount;\n" 496 "SELECT 'sum(cnt): ', sum(cnt) FROM wordcount;\n" 497 "SELECT 'max(cnt): ', max(cnt) FROM wordcount;\n" 498 "SELECT 'avg(cnt): ', avg(cnt) FROM wordcount;\n" 499 "SELECT 'sum(cnt=1):', sum(cnt=1) FROM wordcount;\n" 500 "SELECT 'top 10: ', group_concat(word, ', ') FROM " 501 "(SELECT word FROM wordcount ORDER BY cnt DESC, word LIMIT 10);\n" 502 "SELECT 'checksum: ', checksum(word, cnt) FROM " 503 "(SELECT word, cnt FROM wordcount ORDER BY word);\n" 504 "PRAGMA integrity_check;\n", 505 printResult, 0, 0); 506 } 507 508 /* Database connection statistics printed after both prepared statements 509 ** have been finalized */ 510 if( showStats ){ 511 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, 0); 512 printf("%s Lookaside Slots Used: %d (max %d)\n", zTag, iCur,iHiwtr); 513 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, 0); 514 printf("%s Successful lookasides: %d\n", zTag, iHiwtr); 515 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHiwtr,0); 516 printf("%s Lookaside size faults: %d\n", zTag, iHiwtr); 517 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHiwtr,0); 518 printf("%s Lookaside OOM faults: %d\n", zTag, iHiwtr); 519 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, 0); 520 printf("%s Pager Heap Usage: %d bytes\n", zTag, iCur); 521 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); 522 printf("%s Page cache hits: %d\n", zTag, iCur); 523 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); 524 printf("%s Page cache misses: %d\n", zTag, iCur); 525 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); 526 printf("%s Page cache writes: %d\n", zTag, iCur); 527 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, 0); 528 printf("%s Schema Heap Usage: %d bytes\n", zTag, iCur); 529 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, 0); 530 printf("%s Statement Heap Usage: %d bytes\n", zTag, iCur); 531 } 532 533 sqlite3_close(db); 534 535 /* Global memory usage statistics printed after the database connection 536 ** has closed. Memory usage should be zero at this point. */ 537 if( showStats ){ 538 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, 0); 539 printf("%s Memory Used (bytes): %d (max %d)\n", zTag,iCur,iHiwtr); 540 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, 0); 541 printf("%s Outstanding Allocations: %d (max %d)\n",zTag,iCur,iHiwtr); 542 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, 0); 543 printf("%s Pcache Overflow Bytes: %d (max %d)\n",zTag,iCur,iHiwtr); 544 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, 0); 545 printf("%s Scratch Overflow Bytes: %d (max %d)\n",zTag,iCur,iHiwtr); 546 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, 0); 547 printf("%s Largest Allocation: %d bytes\n",zTag,iHiwtr); 548 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, 0); 549 printf("%s Largest Pcache Allocation: %d bytes\n",zTag,iHiwtr); 550 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, 0); 551 printf("%s Largest Scratch Allocation: %d bytes\n",zTag,iHiwtr); 552 } 553 return 0; 554 } 555