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