mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-07 02:42:48 +03:00
Add the --help and --all options to the wordcount test utility.
FossilOrigin-Name: 18baeadfc89f6252e38dbc22904b11c5b56347ee
This commit is contained in:
17
manifest
17
manifest
@@ -1,5 +1,5 @@
|
|||||||
C Fix\sa\scrash\sthat\scould\soccur\sfollowing\san\sOOM\sin\sthe\sgroup_concat()\sfunction\nif\sthe\ssecond\sargument\sis\san\sSQLITE_BLOB\svalue.
|
C Add\sthe\s--help\sand\s--all\soptions\sto\sthe\swordcount\stest\sutility.
|
||||||
D 2016-12-30T17:40:14.373
|
D 2016-12-31T21:55:23.877
|
||||||
F Makefile.in 41bd4cad981487345c4a84081074bcdb876e4b2e
|
F Makefile.in 41bd4cad981487345c4a84081074bcdb876e4b2e
|
||||||
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
|
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
|
||||||
F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da
|
F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da
|
||||||
@@ -1449,7 +1449,7 @@ F test/without_rowid3.test 2724c787a51a5dce09d078453a758117b4b728f1
|
|||||||
F test/without_rowid4.test 4e08bcbaee0399f35d58b5581881e7a6243d458a
|
F test/without_rowid4.test 4e08bcbaee0399f35d58b5581881e7a6243d458a
|
||||||
F test/without_rowid5.test 89b1c587bd92a0590e440da33e7666bf4891572a
|
F test/without_rowid5.test 89b1c587bd92a0590e440da33e7666bf4891572a
|
||||||
F test/without_rowid6.test 1f99644e6508447fb050f73697350c7ceca3392e
|
F test/without_rowid6.test 1f99644e6508447fb050f73697350c7ceca3392e
|
||||||
F test/wordcount.c 97856eec21fd00d77da720007b1888c383f63dcf
|
F test/wordcount.c 06efb84b7c48a4973c2c24ea06c93d00bce24389
|
||||||
F test/zeroblob.test 3857870fe681b8185654414a9bccfde80b62a0fa
|
F test/zeroblob.test 3857870fe681b8185654414a9bccfde80b62a0fa
|
||||||
F test/zerodamage.test e59a56443d6298ecf7435f618f0b27654f0c849e
|
F test/zerodamage.test e59a56443d6298ecf7435f618f0b27654f0c849e
|
||||||
F tool/GetFile.cs a15e08acb5dd7539b75ba23501581d7c2b462cb5
|
F tool/GetFile.cs a15e08acb5dd7539b75ba23501581d7c2b462cb5
|
||||||
@@ -1541,7 +1541,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
|||||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||||
P a0971e713682a73d8c7c20511db256c20d2f6388
|
P 14d855d2b2b5b3485e0673d11405db7266b34c6d
|
||||||
R b2119513a85656f5ab1ba46bed3bca22
|
R fe34c48679c60f27af86fde5a781f642
|
||||||
U dan
|
T *branch * wordcount-enhancement
|
||||||
Z b8d94ab9819b45ed8f5f9b27f33e84f7
|
T *sym-wordcount-enhancement *
|
||||||
|
T -sym-trunk *
|
||||||
|
U drh
|
||||||
|
Z 8bab216aa01a50924e317f1b569b8185
|
||||||
|
@@ -1 +1 @@
|
|||||||
14d855d2b2b5b3485e0673d11405db7266b34c6d
|
18baeadfc89f6252e38dbc22904b11c5b56347ee
|
472
test/wordcount.c
472
test/wordcount.c
@@ -13,24 +13,6 @@
|
|||||||
**
|
**
|
||||||
** Option:
|
** Option:
|
||||||
**
|
**
|
||||||
** --without-rowid Use a WITHOUT ROWID table to store the words.
|
|
||||||
** --insert Use INSERT mode (the default)
|
|
||||||
** --replace Use REPLACE mode
|
|
||||||
** --select Use SELECT mode
|
|
||||||
** --update Use UPDATE mode
|
|
||||||
** --delete Use DELETE mode
|
|
||||||
** --query Use QUERY mode
|
|
||||||
** --nocase Add the NOCASE collating sequence to the words.
|
|
||||||
** --trace Enable sqlite3_trace() output.
|
|
||||||
** --summary Show summary information on the collected data.
|
|
||||||
** --stats Show sqlite3_status() results at the end.
|
|
||||||
** --pagesize NNN Use a page size of NNN
|
|
||||||
** --cachesize NNN Use a cache size of NNN
|
|
||||||
** --commit NNN Commit after every NNN operations
|
|
||||||
** --nosync Use PRAGMA synchronous=OFF
|
|
||||||
** --journal MMMM Use PRAGMA journal_mode=MMMM
|
|
||||||
** --timer Time the operation of this program
|
|
||||||
** --tag NAME Tag all output using NAME. Use only stdout.
|
|
||||||
**
|
**
|
||||||
** Modes:
|
** Modes:
|
||||||
**
|
**
|
||||||
@@ -81,8 +63,36 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "sqlite3.h"
|
#include "sqlite3.h"
|
||||||
|
#ifndef _WIN32
|
||||||
|
# include <unistd.h>
|
||||||
|
#else
|
||||||
|
# include <io.h>
|
||||||
|
#endif
|
||||||
#define ISALPHA(X) isalpha((unsigned char)(X))
|
#define ISALPHA(X) isalpha((unsigned char)(X))
|
||||||
|
|
||||||
|
const char zHelp[] =
|
||||||
|
"Usage: wordcount [OPTIONS] DATABASE [INPUT]\n"
|
||||||
|
" --all Repeat the test for all test modes\n"
|
||||||
|
" --cachesize NNN Use a cache size of NNN\n"
|
||||||
|
" --commit NNN Commit after every NNN operations\n"
|
||||||
|
" --delete Use DELETE mode\n"
|
||||||
|
" --insert Use INSERT mode (the default)\n"
|
||||||
|
" --journal MMMM Use PRAGMA journal_mode=MMMM\n"
|
||||||
|
" --nocase Add the NOCASE collating sequence to the words.\n"
|
||||||
|
" --nosync Use PRAGMA synchronous=OFF\n"
|
||||||
|
" --pagesize NNN Use a page size of NNN\n"
|
||||||
|
" --query Use QUERY mode\n"
|
||||||
|
" --replace Use REPLACE mode\n"
|
||||||
|
" --select Use SELECT mode\n"
|
||||||
|
" --stats Show sqlite3_status() results at the end.\n"
|
||||||
|
" --summary Show summary information on the collected data.\n"
|
||||||
|
" --tag NAME Tag all output using NAME. Use only stdout.\n"
|
||||||
|
" --timer Time the operation of this program\n"
|
||||||
|
" --trace Enable sqlite3_trace() output.\n"
|
||||||
|
" --update Use UPDATE mode\n"
|
||||||
|
" --without-rowid Use a WITHOUT ROWID table to store the words.\n"
|
||||||
|
;
|
||||||
|
|
||||||
/* Output tag */
|
/* Output tag */
|
||||||
char *zTag = "--";
|
char *zTag = "--";
|
||||||
|
|
||||||
@@ -110,6 +120,12 @@ static void fatal_error(const char *zMsg, ...){
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Print a usage message and quit */
|
||||||
|
static void usage(void){
|
||||||
|
printf("%s",zHelp);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
/* The sqlite3_trace() callback function */
|
/* The sqlite3_trace() callback function */
|
||||||
static void traceCallback(void *NotUsed, const char *zSql){
|
static void traceCallback(void *NotUsed, const char *zSql){
|
||||||
printf("%s;\n", zSql);
|
printf("%s;\n", zSql);
|
||||||
@@ -189,7 +205,6 @@ static void checksumFinalize(sqlite3_context *context){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Define operating modes */
|
/* Define operating modes */
|
||||||
#define MODE_INSERT 0
|
#define MODE_INSERT 0
|
||||||
#define MODE_REPLACE 1
|
#define MODE_REPLACE 1
|
||||||
@@ -197,12 +212,50 @@ static void checksumFinalize(sqlite3_context *context){
|
|||||||
#define MODE_UPDATE 3
|
#define MODE_UPDATE 3
|
||||||
#define MODE_DELETE 4
|
#define MODE_DELETE 4
|
||||||
#define MODE_QUERY 5
|
#define MODE_QUERY 5
|
||||||
|
#define MODE_COUNT 6
|
||||||
|
#define MODE_ALL (-1)
|
||||||
|
|
||||||
|
/* Mode names */
|
||||||
|
static const char *azMode[] = {
|
||||||
|
"--insert",
|
||||||
|
"--replace",
|
||||||
|
"--select",
|
||||||
|
"--update",
|
||||||
|
"--delete",
|
||||||
|
"--query"
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Determine if another iteration of the test is required. Return true
|
||||||
|
** if so. Return zero if all iterations have finished.
|
||||||
|
*/
|
||||||
|
static int allLoop(
|
||||||
|
int iMode, /* The selected test mode */
|
||||||
|
int *piLoopCnt, /* Iteration loop counter */
|
||||||
|
int *piMode2, /* The test mode to use on the next iteration */
|
||||||
|
int *pUseWithoutRowid /* Whether or not to use --without-rowid */
|
||||||
|
){
|
||||||
|
int i;
|
||||||
|
if( iMode!=MODE_ALL ){
|
||||||
|
if( *piLoopCnt ) return 0;
|
||||||
|
*piMode2 = iMode;
|
||||||
|
*piLoopCnt = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if( (*piLoopCnt)>=MODE_COUNT*2 ) return 0;
|
||||||
|
i = (*piLoopCnt)++;
|
||||||
|
*pUseWithoutRowid = i&1;
|
||||||
|
*piMode2 = i>>1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv){
|
||||||
const char *zFileToRead = 0; /* Input file. NULL for stdin */
|
const char *zFileToRead = 0; /* Input file. NULL for stdin */
|
||||||
const char *zDbName = 0; /* Name of the database file to create */
|
const char *zDbName = 0; /* Name of the database file to create */
|
||||||
int useWithoutRowid = 0; /* True for --without-rowid */
|
int useWithoutRowid = 0; /* True for --without-rowid */
|
||||||
int iMode = MODE_INSERT; /* One of MODE_xxxxx */
|
int iMode = MODE_INSERT; /* One of MODE_xxxxx */
|
||||||
|
int iMode2; /* Mode to use for current --all iteration */
|
||||||
|
int iLoopCnt = 0; /* Which iteration when running --all */
|
||||||
int useNocase = 0; /* True for --nocase */
|
int useNocase = 0; /* True for --nocase */
|
||||||
int doTrace = 0; /* True for --trace */
|
int doTrace = 0; /* True for --trace */
|
||||||
int showStats = 0; /* True for --stats */
|
int showStats = 0; /* True for --stats */
|
||||||
@@ -226,7 +279,8 @@ int main(int argc, char **argv){
|
|||||||
int iCur, iHiwtr; /* Statistics values, current and "highwater" */
|
int iCur, iHiwtr; /* Statistics values, current and "highwater" */
|
||||||
FILE *pTimer = stderr; /* Output channel for the timer */
|
FILE *pTimer = stderr; /* Output channel for the timer */
|
||||||
sqlite3_int64 sumCnt = 0; /* Sum in QUERY mode */
|
sqlite3_int64 sumCnt = 0; /* Sum in QUERY mode */
|
||||||
sqlite3_int64 startTime;
|
sqlite3_int64 startTime; /* Time of start */
|
||||||
|
sqlite3_int64 totalTime = 0; /* Total time */
|
||||||
char zInput[2000]; /* A single line of input */
|
char zInput[2000]; /* A single line of input */
|
||||||
|
|
||||||
/* Process command-line arguments */
|
/* Process command-line arguments */
|
||||||
@@ -248,6 +302,9 @@ int main(int argc, char **argv){
|
|||||||
iMode = MODE_DELETE;
|
iMode = MODE_DELETE;
|
||||||
}else if( strcmp(z,"query")==0 ){
|
}else if( strcmp(z,"query")==0 ){
|
||||||
iMode = MODE_QUERY;
|
iMode = MODE_QUERY;
|
||||||
|
}else if( strcmp(z,"all")==0 ){
|
||||||
|
iMode = MODE_ALL;
|
||||||
|
showTimer = -99;
|
||||||
}else if( strcmp(z,"nocase")==0 ){
|
}else if( strcmp(z,"nocase")==0 ){
|
||||||
useNocase = 1;
|
useNocase = 1;
|
||||||
}else if( strcmp(z,"trace")==0 ){
|
}else if( strcmp(z,"trace")==0 ){
|
||||||
@@ -274,23 +331,30 @@ int main(int argc, char **argv){
|
|||||||
}else if( strcmp(z,"tag")==0 && i<argc-1 ){
|
}else if( strcmp(z,"tag")==0 && i<argc-1 ){
|
||||||
zTag = argv[++i];
|
zTag = argv[++i];
|
||||||
pTimer = stdout;
|
pTimer = stdout;
|
||||||
|
}else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){
|
||||||
|
usage();
|
||||||
}else{
|
}else{
|
||||||
fatal_error("unknown option: %s\n", argv[i]);
|
fatal_error("unknown option: \"%s\"\n"
|
||||||
|
"Use --help for a list of options\n",
|
||||||
|
argv[i]);
|
||||||
}
|
}
|
||||||
}else if( zDbName==0 ){
|
}else if( zDbName==0 ){
|
||||||
zDbName = argv[i];
|
zDbName = argv[i];
|
||||||
}else if( zFileToRead==0 ){
|
}else if( zFileToRead==0 ){
|
||||||
zFileToRead = argv[i];
|
zFileToRead = argv[i];
|
||||||
}else{
|
}else{
|
||||||
fatal_error("surplus argument: %s\n", argv[i]);
|
fatal_error("surplus argument: \"%s\"\n", argv[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( zDbName==0 ){
|
if( zDbName==0 ){
|
||||||
fatal_error("Usage: %s [--options] DATABASE [INPUTFILE]\n", argv[0]);
|
usage();
|
||||||
}
|
}
|
||||||
startTime = realTime();
|
startTime = realTime();
|
||||||
|
|
||||||
/* Open the database and the input file */
|
/* Open the database and the input file */
|
||||||
|
if( zDbName[0] && strcmp(zDbName,":memory:")!=0 ){
|
||||||
|
unlink(zDbName);
|
||||||
|
}
|
||||||
if( sqlite3_open(zDbName, &db) ){
|
if( sqlite3_open(zDbName, &db) ){
|
||||||
fatal_error("Cannot open database file: %s\n", zDbName);
|
fatal_error("Cannot open database file: %s\n", zDbName);
|
||||||
}
|
}
|
||||||
@@ -300,6 +364,9 @@ int main(int argc, char **argv){
|
|||||||
fatal_error("Could not open input file \"%s\"\n", zFileToRead);
|
fatal_error("Could not open input file \"%s\"\n", zFileToRead);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
if( iMode==MODE_ALL ){
|
||||||
|
fatal_error("The --all mode cannot be used with stdin\n");
|
||||||
|
}
|
||||||
in = stdin;
|
in = stdin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,189 +389,214 @@ int main(int argc, char **argv){
|
|||||||
sqlite3_free(zSql);
|
sqlite3_free(zSql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iLoopCnt = 0;
|
||||||
/* Construct the "wordcount" table into which to put the words */
|
while( allLoop(iMode, &iLoopCnt, &iMode2, &useWithoutRowid) ){
|
||||||
if( sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0) ){
|
/* Delete prior content in --all mode */
|
||||||
fatal_error("Could not start a transaction\n");
|
if( iMode==MODE_ALL ){
|
||||||
}
|
if( sqlite3_exec(db, "DROP TABLE IF EXISTS wordcount; VACUUM;",0,0,0) ){
|
||||||
zSql = sqlite3_mprintf(
|
fatal_error("Could not clean up prior iteration\n");
|
||||||
"CREATE TABLE IF NOT EXISTS wordcount(\n"
|
}
|
||||||
" word TEXT PRIMARY KEY COLLATE %s,\n"
|
startTime = realTime();
|
||||||
" cnt INTEGER\n"
|
rewind(in);
|
||||||
")%s",
|
}
|
||||||
useNocase ? "nocase" : "binary",
|
|
||||||
useWithoutRowid ? " WITHOUT ROWID" : ""
|
/* Construct the "wordcount" table into which to put the words */
|
||||||
);
|
if( sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0) ){
|
||||||
if( zSql==0 ) fatal_error("out of memory\n");
|
fatal_error("Could not start a transaction\n");
|
||||||
rc = sqlite3_exec(db, zSql, 0, 0, 0);
|
}
|
||||||
if( rc ) fatal_error("Could not create the wordcount table: %s.\n",
|
zSql = sqlite3_mprintf(
|
||||||
sqlite3_errmsg(db));
|
"CREATE TABLE IF NOT EXISTS wordcount(\n"
|
||||||
sqlite3_free(zSql);
|
" word TEXT PRIMARY KEY COLLATE %s,\n"
|
||||||
|
" cnt INTEGER\n"
|
||||||
/* Prepare SQL statements that will be needed */
|
")%s",
|
||||||
if( iMode==MODE_QUERY ){
|
useNocase ? "nocase" : "binary",
|
||||||
rc = sqlite3_prepare_v2(db,
|
useWithoutRowid ? " WITHOUT ROWID" : ""
|
||||||
"SELECT cnt FROM wordcount WHERE word=?1",
|
);
|
||||||
-1, &pSelect, 0);
|
if( zSql==0 ) fatal_error("out of memory\n");
|
||||||
if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n",
|
rc = sqlite3_exec(db, zSql, 0, 0, 0);
|
||||||
sqlite3_errmsg(db));
|
if( rc ) fatal_error("Could not create the wordcount table: %s.\n",
|
||||||
}
|
|
||||||
if( iMode==MODE_SELECT ){
|
|
||||||
rc = sqlite3_prepare_v2(db,
|
|
||||||
"SELECT 1 FROM wordcount WHERE word=?1",
|
|
||||||
-1, &pSelect, 0);
|
|
||||||
if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n",
|
|
||||||
sqlite3_errmsg(db));
|
|
||||||
rc = sqlite3_prepare_v2(db,
|
|
||||||
"INSERT INTO wordcount(word,cnt) VALUES(?1,1)",
|
|
||||||
-1, &pInsert, 0);
|
|
||||||
if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n",
|
|
||||||
sqlite3_errmsg(db));
|
sqlite3_errmsg(db));
|
||||||
}
|
sqlite3_free(zSql);
|
||||||
if( iMode==MODE_SELECT || iMode==MODE_UPDATE || iMode==MODE_INSERT ){
|
|
||||||
rc = sqlite3_prepare_v2(db,
|
/* Prepare SQL statements that will be needed */
|
||||||
"UPDATE wordcount SET cnt=cnt+1 WHERE word=?1",
|
if( iMode2==MODE_QUERY ){
|
||||||
-1, &pUpdate, 0);
|
rc = sqlite3_prepare_v2(db,
|
||||||
if( rc ) fatal_error("Could not prepare the UPDATE statement: %s\n",
|
"SELECT cnt FROM wordcount WHERE word=?1",
|
||||||
sqlite3_errmsg(db));
|
-1, &pSelect, 0);
|
||||||
}
|
if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n",
|
||||||
if( iMode==MODE_INSERT ){
|
sqlite3_errmsg(db));
|
||||||
rc = sqlite3_prepare_v2(db,
|
}
|
||||||
"INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,1)",
|
if( iMode2==MODE_SELECT ){
|
||||||
-1, &pInsert, 0);
|
rc = sqlite3_prepare_v2(db,
|
||||||
if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n",
|
"SELECT 1 FROM wordcount WHERE word=?1",
|
||||||
sqlite3_errmsg(db));
|
-1, &pSelect, 0);
|
||||||
}
|
if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n",
|
||||||
if( iMode==MODE_UPDATE ){
|
sqlite3_errmsg(db));
|
||||||
rc = sqlite3_prepare_v2(db,
|
rc = sqlite3_prepare_v2(db,
|
||||||
"INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,0)",
|
"INSERT INTO wordcount(word,cnt) VALUES(?1,1)",
|
||||||
-1, &pInsert, 0);
|
-1, &pInsert, 0);
|
||||||
if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n",
|
if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n",
|
||||||
sqlite3_errmsg(db));
|
sqlite3_errmsg(db));
|
||||||
}
|
}
|
||||||
if( iMode==MODE_REPLACE ){
|
if( iMode2==MODE_SELECT || iMode2==MODE_UPDATE || iMode2==MODE_INSERT ){
|
||||||
rc = sqlite3_prepare_v2(db,
|
rc = sqlite3_prepare_v2(db,
|
||||||
|
"UPDATE wordcount SET cnt=cnt+1 WHERE word=?1",
|
||||||
|
-1, &pUpdate, 0);
|
||||||
|
if( rc ) fatal_error("Could not prepare the UPDATE statement: %s\n",
|
||||||
|
sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
if( iMode2==MODE_INSERT ){
|
||||||
|
rc = sqlite3_prepare_v2(db,
|
||||||
|
"INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,1)",
|
||||||
|
-1, &pInsert, 0);
|
||||||
|
if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n",
|
||||||
|
sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
if( iMode2==MODE_UPDATE ){
|
||||||
|
rc = sqlite3_prepare_v2(db,
|
||||||
|
"INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,0)",
|
||||||
|
-1, &pInsert, 0);
|
||||||
|
if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n",
|
||||||
|
sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
if( iMode2==MODE_REPLACE ){
|
||||||
|
rc = sqlite3_prepare_v2(db,
|
||||||
"REPLACE INTO wordcount(word,cnt)"
|
"REPLACE INTO wordcount(word,cnt)"
|
||||||
"VALUES(?1,coalesce((SELECT cnt FROM wordcount WHERE word=?1),0)+1)",
|
"VALUES(?1,coalesce((SELECT cnt FROM wordcount WHERE word=?1),0)+1)",
|
||||||
-1, &pInsert, 0);
|
-1, &pInsert, 0);
|
||||||
if( rc ) fatal_error("Could not prepare the REPLACE statement: %s\n",
|
if( rc ) fatal_error("Could not prepare the REPLACE statement: %s\n",
|
||||||
sqlite3_errmsg(db));
|
sqlite3_errmsg(db));
|
||||||
}
|
}
|
||||||
if( iMode==MODE_DELETE ){
|
if( iMode2==MODE_DELETE ){
|
||||||
rc = sqlite3_prepare_v2(db,
|
rc = sqlite3_prepare_v2(db,
|
||||||
"DELETE FROM wordcount WHERE word=?1",
|
"DELETE FROM wordcount WHERE word=?1",
|
||||||
-1, &pDelete, 0);
|
-1, &pDelete, 0);
|
||||||
if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n",
|
if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n",
|
||||||
sqlite3_errmsg(db));
|
sqlite3_errmsg(db));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Process the input file */
|
/* Process the input file */
|
||||||
while( fgets(zInput, sizeof(zInput), in) ){
|
while( fgets(zInput, sizeof(zInput), in) ){
|
||||||
for(i=0; zInput[i]; i++){
|
for(i=0; zInput[i]; i++){
|
||||||
if( !ISALPHA(zInput[i]) ) continue;
|
if( !ISALPHA(zInput[i]) ) continue;
|
||||||
for(j=i+1; ISALPHA(zInput[j]); j++){}
|
for(j=i+1; ISALPHA(zInput[j]); j++){}
|
||||||
|
|
||||||
/* Found a new word at zInput[i] that is j-i bytes long.
|
/* Found a new word at zInput[i] that is j-i bytes long.
|
||||||
** Process it into the wordcount table. */
|
** Process it into the wordcount table. */
|
||||||
if( iMode==MODE_DELETE ){
|
if( iMode2==MODE_DELETE ){
|
||||||
sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC);
|
sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
if( sqlite3_step(pDelete)!=SQLITE_DONE ){
|
if( sqlite3_step(pDelete)!=SQLITE_DONE ){
|
||||||
fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db));
|
fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db));
|
||||||
}
|
|
||||||
sqlite3_reset(pDelete);
|
|
||||||
}else if( iMode==MODE_SELECT ){
|
|
||||||
sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC);
|
|
||||||
rc = sqlite3_step(pSelect);
|
|
||||||
sqlite3_reset(pSelect);
|
|
||||||
if( rc==SQLITE_ROW ){
|
|
||||||
sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC);
|
|
||||||
if( sqlite3_step(pUpdate)!=SQLITE_DONE ){
|
|
||||||
fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db));
|
|
||||||
}
|
}
|
||||||
sqlite3_reset(pUpdate);
|
sqlite3_reset(pDelete);
|
||||||
}else if( rc==SQLITE_DONE ){
|
}else if( iMode2==MODE_SELECT ){
|
||||||
|
sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
|
rc = sqlite3_step(pSelect);
|
||||||
|
sqlite3_reset(pSelect);
|
||||||
|
if( rc==SQLITE_ROW ){
|
||||||
|
sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
|
if( sqlite3_step(pUpdate)!=SQLITE_DONE ){
|
||||||
|
fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
sqlite3_reset(pUpdate);
|
||||||
|
}else if( rc==SQLITE_DONE ){
|
||||||
|
sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
|
if( sqlite3_step(pInsert)!=SQLITE_DONE ){
|
||||||
|
fatal_error("Insert failed: %s\n", sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
sqlite3_reset(pInsert);
|
||||||
|
}else{
|
||||||
|
fatal_error("SELECT failed: %s\n", sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
}else if( iMode2==MODE_QUERY ){
|
||||||
|
sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
|
if( sqlite3_step(pSelect)==SQLITE_ROW ){
|
||||||
|
sumCnt += sqlite3_column_int64(pSelect, 0);
|
||||||
|
}
|
||||||
|
sqlite3_reset(pSelect);
|
||||||
|
}else{
|
||||||
sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC);
|
sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
if( sqlite3_step(pInsert)!=SQLITE_DONE ){
|
if( sqlite3_step(pInsert)!=SQLITE_DONE ){
|
||||||
fatal_error("Insert failed: %s\n", sqlite3_errmsg(db));
|
fatal_error("INSERT failed: %s\n", sqlite3_errmsg(db));
|
||||||
}
|
}
|
||||||
sqlite3_reset(pInsert);
|
sqlite3_reset(pInsert);
|
||||||
}else{
|
if( iMode2==MODE_UPDATE
|
||||||
fatal_error("SELECT failed: %s\n", sqlite3_errmsg(db));
|
|| (iMode2==MODE_INSERT && sqlite3_changes(db)==0)
|
||||||
}
|
){
|
||||||
}else if( iMode==MODE_QUERY ){
|
sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC);
|
||||||
sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC);
|
if( sqlite3_step(pUpdate)!=SQLITE_DONE ){
|
||||||
if( sqlite3_step(pSelect)==SQLITE_ROW ){
|
fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db));
|
||||||
sumCnt += sqlite3_column_int64(pSelect, 0);
|
}
|
||||||
}
|
sqlite3_reset(pUpdate);
|
||||||
sqlite3_reset(pSelect);
|
|
||||||
}else{
|
|
||||||
sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC);
|
|
||||||
if( sqlite3_step(pInsert)!=SQLITE_DONE ){
|
|
||||||
fatal_error("INSERT failed: %s\n", sqlite3_errmsg(db));
|
|
||||||
}
|
|
||||||
sqlite3_reset(pInsert);
|
|
||||||
if( iMode==MODE_UPDATE
|
|
||||||
|| (iMode==MODE_INSERT && sqlite3_changes(db)==0)
|
|
||||||
){
|
|
||||||
sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC);
|
|
||||||
if( sqlite3_step(pUpdate)!=SQLITE_DONE ){
|
|
||||||
fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db));
|
|
||||||
}
|
}
|
||||||
sqlite3_reset(pUpdate);
|
}
|
||||||
|
i = j-1;
|
||||||
|
|
||||||
|
/* Increment the operation counter. Do a COMMIT if it is time. */
|
||||||
|
nOp++;
|
||||||
|
if( commitInterval>0 && (nOp%commitInterval)==0 ){
|
||||||
|
sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i = j-1;
|
}
|
||||||
|
sqlite3_exec(db, "COMMIT", 0, 0, 0);
|
||||||
/* Increment the operation counter. Do a COMMIT if it is time. */
|
sqlite3_finalize(pInsert); pInsert = 0;
|
||||||
nOp++;
|
sqlite3_finalize(pUpdate); pUpdate = 0;
|
||||||
if( commitInterval>0 && (nOp%commitInterval)==0 ){
|
sqlite3_finalize(pSelect); pSelect = 0;
|
||||||
sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0);
|
sqlite3_finalize(pDelete); pDelete = 0;
|
||||||
|
|
||||||
|
if( iMode2==MODE_QUERY && iMode!=MODE_ALL ){
|
||||||
|
printf("%s sum of cnt: %lld\n", zTag, sumCnt);
|
||||||
|
rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1,
|
||||||
|
&pSelect, 0);
|
||||||
|
if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){
|
||||||
|
printf("%s double-check: %lld\n", zTag,sqlite3_column_int64(pSelect,0));
|
||||||
|
}
|
||||||
|
sqlite3_finalize(pSelect);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if( showTimer ){
|
||||||
|
sqlite3_int64 elapseTime = realTime() - startTime;
|
||||||
|
totalTime += elapseTime;
|
||||||
|
fprintf(pTimer, "%3d.%03d wordcount", (int)(elapseTime/1000),
|
||||||
|
(int)(elapseTime%1000));
|
||||||
|
if( iMode==MODE_ALL ){
|
||||||
|
fprintf(pTimer, " %s%s\n", azMode[iMode2],
|
||||||
|
useWithoutRowid? " --without-rowid" : "");
|
||||||
|
}else{
|
||||||
|
for(i=1; i<argc; i++) if( i!=showTimer ) fprintf(pTimer," %s",argv[i]);
|
||||||
|
fprintf(pTimer, "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
sqlite3_exec(db, "COMMIT", 0, 0, 0);
|
if( showSummary ){
|
||||||
if( zFileToRead ) fclose(in);
|
sqlite3_create_function(db, "checksum", -1, SQLITE_UTF8, 0,
|
||||||
sqlite3_finalize(pInsert);
|
0, checksumStep, checksumFinalize);
|
||||||
sqlite3_finalize(pUpdate);
|
sqlite3_exec(db,
|
||||||
sqlite3_finalize(pSelect);
|
"SELECT 'count(*): ', count(*) FROM wordcount;\n"
|
||||||
sqlite3_finalize(pDelete);
|
"SELECT 'sum(cnt): ', sum(cnt) FROM wordcount;\n"
|
||||||
|
"SELECT 'max(cnt): ', max(cnt) FROM wordcount;\n"
|
||||||
if( iMode==MODE_QUERY ){
|
"SELECT 'avg(cnt): ', avg(cnt) FROM wordcount;\n"
|
||||||
printf("%s sum of cnt: %lld\n", zTag, sumCnt);
|
"SELECT 'sum(cnt=1):', sum(cnt=1) FROM wordcount;\n"
|
||||||
rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1,
|
"SELECT 'top 10: ', group_concat(word, ', ') FROM "
|
||||||
&pSelect, 0);
|
"(SELECT word FROM wordcount ORDER BY cnt DESC, word LIMIT 10);\n"
|
||||||
if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){
|
"SELECT 'checksum: ', checksum(word, cnt) FROM "
|
||||||
printf("%s double-check: %lld\n", zTag, sqlite3_column_int64(pSelect, 0));
|
"(SELECT word, cnt FROM wordcount ORDER BY word);\n"
|
||||||
|
"PRAGMA integrity_check;\n",
|
||||||
|
printResult, 0, 0);
|
||||||
}
|
}
|
||||||
sqlite3_finalize(pSelect);
|
} /* End the --all loop */
|
||||||
|
|
||||||
|
/* Close the input file after the last read */
|
||||||
|
if( zFileToRead ) fclose(in);
|
||||||
|
|
||||||
|
/* In --all mode, so the total time */
|
||||||
|
if( iMode==MODE_ALL && showTimer ){
|
||||||
|
fprintf(pTimer, "%3d.%03d wordcount --all\n", (int)(totalTime/1000),
|
||||||
|
(int)(totalTime%1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if( showTimer ){
|
|
||||||
sqlite3_int64 elapseTime = realTime() - startTime;
|
|
||||||
fprintf(pTimer, "%3d.%03d wordcount", (int)(elapseTime/1000),
|
|
||||||
(int)(elapseTime%1000));
|
|
||||||
for(i=1; i<argc; i++) if( i!=showTimer ) fprintf(pTimer, " %s", argv[i]);
|
|
||||||
fprintf(pTimer, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if( showSummary ){
|
|
||||||
sqlite3_create_function(db, "checksum", -1, SQLITE_UTF8, 0,
|
|
||||||
0, checksumStep, checksumFinalize);
|
|
||||||
sqlite3_exec(db,
|
|
||||||
"SELECT 'count(*): ', count(*) FROM wordcount;\n"
|
|
||||||
"SELECT 'sum(cnt): ', sum(cnt) FROM wordcount;\n"
|
|
||||||
"SELECT 'max(cnt): ', max(cnt) FROM wordcount;\n"
|
|
||||||
"SELECT 'avg(cnt): ', avg(cnt) FROM wordcount;\n"
|
|
||||||
"SELECT 'sum(cnt=1):', sum(cnt=1) FROM wordcount;\n"
|
|
||||||
"SELECT 'top 10: ', group_concat(word, ', ') FROM "
|
|
||||||
"(SELECT word FROM wordcount ORDER BY cnt DESC, word LIMIT 10);\n"
|
|
||||||
"SELECT 'checksum: ', checksum(word, cnt) FROM "
|
|
||||||
"(SELECT word, cnt FROM wordcount ORDER BY word);\n"
|
|
||||||
"PRAGMA integrity_check;\n",
|
|
||||||
printResult, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Database connection statistics printed after both prepared statements
|
/* Database connection statistics printed after both prepared statements
|
||||||
** have been finalized */
|
** have been finalized */
|
||||||
if( showStats ){
|
if( showStats ){
|
||||||
|
Reference in New Issue
Block a user