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