FILE *fp;
int c , cnt = 0;
- if ((fp = fopen(filename, "r")) == NULL) {
- fprintf(stderr,"Cannot open %s\n", filename);
- exit(1);
- }
+ if ((fp = fopen(filename, "r")) == NULL)
+ fatal("Cannot open %s\n", filename);
+
while ((c = getc(fp)) != EOF)
if (c == '\n')
cnt += 1;
FILE *fp;
int i , temp_cfreq = 0;
- if ((fp = fopen(filename, "r")) == NULL) {
- fprintf(stderr,"Cannot open %s\n", filename);
- exit(1);
- }
+ if ((fp = fopen(filename, "r")) == NULL)
+ fatal("Cannot open %s\n", filename);
+
a = (struct word_info *)malloc(no_of_words * sizeof(struct word_info));
- if (!a) {
- fprintf(stderr,"Can't allocate %d bytes\n", no_of_words * sizeof(struct word_info));
- exit(1);
- }
+ if (!a)
+ fatal("Can't allocate %d bytes\n", no_of_words * sizeof(struct word_info));
+
for (i = 0; i < no_of_words; i++) {
fscanf(fp, "%s", buf); /* store word in temporary buffer */
a[i].word = strdup(buf);
- if (!a[i].word) {
- fprintf(stderr,"strdup failed\n");
- exit(1);
- }
+ if (!a[i].word)
+ fatal("strdup failed\n");
fscanf(fp, "%d", &a[i].freq);
a[i].word_len = strlen(a[i].word);
a[i].cum_freq = temp_cfreq;
FILE *fp;
int i , temp_cfreq = 0;
- if ((fp = fopen(filename, "r")) == NULL) {
- fprintf(stderr,"Cannot open %s\n", filename);
- exit(1);
- }
+ if ((fp = fopen(filename, "r")) == NULL)
+ fatal("Cannot open %s\n", filename);
+
d = (struct doc_info *)malloc(no_of_docs * sizeof(struct doc_info));
+ if ( !d )
+ fatal("Can't allocate %d bytes\n", no_of_docs * sizeof(struct doc_info));
for (i = 0; i < no_of_docs; i++) {
fscanf(fp, "%d", &d[i].doc_len);
fscanf(fp, "%d", &d[i].doc_freq);
void
finnegan_init(char *lex_file, char *doc_file, int quiet) {
- if ( isInited ) {
- fprintf(stderr,"finnegan is already inited\n");
- exit(1);
- }
+ if ( isInited )
+ fatal("finnegan is already inited\n");
if (!quiet) {
printf("Initialize text generator with:\n");
" -l LEXFILE - file with words and its frequents (default gendata/lex)\n"
" -g GAMMAFILE - file with doc's length distribution (default \n"
" gendata/gamma-lens)\n"
- " -l FLGAS - options for db's schema (see below)\n"
+ " -l FLAGS - options for db's schema (see below)\n"
" -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
" statement\n"
" -q - do not print progress message\n",
" -l LEXFILE - file with words and its frequents (default gendata/query-lex)\n"
" -g GAMMAFILE - file with doc's length distribution (default \n"
" gendata/query-lens)\n"
- " -l FLGAS - options for db's schema (see below)\n"
+ " -l FLAGS - options for db's schema (see below)\n"
" -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
" statement\n"
" -r - row mode: timing every query\n"
if ( DBDesc[i].init )
return DBDesc[i].rdbms;
} else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
- if ( DBDesc[i].init == NULL ) {
- fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
- exit(1);
- }
+ if ( DBDesc[i].init == NULL )
+ fatal("Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
+
return DBDesc[i].rdbms;
}
}
- fprintf(stderr,"Can't find a RDBMS\n");
- exit(1);
+ fatal("Can't find a RDBMS\n");
return NULLSQL;
}
if ( strcasestr(flg,"or") )
flags |= FLG_OR;
- if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
- fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
- exit(1);
- }
- if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
- fprintf(stderr,"AND and OR flags are mutually exclusive\n");
- exit(1);
- } else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
+ if ( (flags & FLG_GIST) && (flags & FLG_GIN) )
+ fatal("GIN and GiST flags are mutually exclusive\n");
+ if ( (flags & FLG_AND) && (flags & FLG_OR) )
+ fatal("AND and OR flags are mutually exclusive\n");
+ else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
flags |= FLG_AND;
return flags;
ftsDB **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
int i;
- if (!dbs) {
- fprintf(stderr,"Not enough mwmory\n");
- exit(1);
- }
+ if (!dbs)
+ fatal("Not enough mwmory\n");
for(i=0;i<n;i++) {
dbs[i] = DBDesc[rdbms].init(connstr);
fflush(stdout);
}
+void
+fatal(const char *format, ...) {
+ va_list args;
+
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+
+ fflush(stderr);
+
+ exit(1);
+}
+
+
extern char *optarg;
int
pthread_mutex_lock( &mutexFinish );
for(i=0;i<nClients;i++) {
- if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
- fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
- exit(1);
- }
+ if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 )
+ fatal("pthread_create failed: %s\n", strerror(errno));
}
for(;;) {
sleepTo.tv_sec = time(NULL) + 1;
res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
- if ( !(res == ETIMEDOUT || res == 0) ) {
- fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
- exit(1);
- }
+ if ( !(res == ETIMEDOUT || res == 0) )
+ fatal("pthread_cond_timedwait failed: %s\n", strerror(errno));
}
elapsed = elapsedtime(&begin);
pthread_mutex_unlock( &mutexFinish );
/* main part */
void report(const char *format, ...);
+void fatal(const char *format, ...);
typedef struct ftsDB {
void (*execQuery)(struct ftsDB*, char **, int);
void (*startCreateScheme)(struct ftsDB*, int);
if ( db->prepareStmt == NULL ) {
db->prepareStmt = mysql_stmt_init(db->conn);
- if ( db->prepareStmt == NULL ) {
- fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( db->prepareStmt == NULL )
+ fatal("mysql_stmt_init failed: %s\n", mysql_error(db->conn));
#define SEARCH_QUERY "SELECT count(*) FROM ftsbench WHERE MATCH(body) AGAINST ( ? IN BOOLEAN MODE);"
- if ( mysql_stmt_prepare( db->prepareStmt, SEARCH_QUERY, strlen(SEARCH_QUERY) ) != 0 ) {
- fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_prepare( db->prepareStmt, SEARCH_QUERY, strlen(SEARCH_QUERY) ) != 0 )
+ fatal("mysql_stmt_init failed: %s\n", mysql_error(db->conn));
- if ( mysql_stmt_param_count(db->prepareStmt) != 1 ) {
- fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
- exit(1);
- }
+ if ( mysql_stmt_param_count(db->prepareStmt) != 1 )
+ fatal("mysql_stmt_param_count: invalid parameter count\n");
memset(&datain, 0, sizeof(MYSQL_BIND));
datain.buffer_type = MYSQL_TYPE_BLOB;
datain.buffer = db->b.str;
datain.buffer_length = db->b.strlen;
- if ( mysql_stmt_bind_param(db->prepareStmt, &datain) ) {
- fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_bind_param(db->prepareStmt, &datain) )
+ fatal("mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
res = mysql_stmt_result_metadata(db->prepareStmt);
- if ( !res ) {
- fprintf(stderr,"mysql_stmt_result_metadata failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( !res )
+ fatal("mysql_stmt_result_metadata failed: %s\n", mysql_error(db->conn));
- if ( mysql_stmt_execute( db->prepareStmt ) ) {
- fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_execute( db->prepareStmt ) )
+ fatal("mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
- if (mysql_stmt_bind_result( db->prepareStmt, &dataout)) {
- fprintf(stderr,"mysql_stmt_bind_result failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if (mysql_stmt_bind_result( db->prepareStmt, &dataout))
+ fatal("mysql_stmt_bind_result failed: %s\n", mysql_error(db->conn));
- if ( mysql_stmt_store_result( db->prepareStmt ) ) {
- fprintf(stderr,"mysql_stmt_store_result failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_store_result( db->prepareStmt ) )
+ fatal("mysql_stmt_store_result failed: %s\n", mysql_error(db->conn));
if ( !mysql_stmt_fetch( db->prepareStmt) ) {
db->db.nres += intout;
} else {
- fprintf(stderr,"mysql_stmt_fetch returns void result\n");
- exit(1);
+ fatal("mysql_stmt_fetch returns void result\n");
}
mysql_free_result(res);
if ( flags & (FLG_GIN | FLG_GIST) )
report("MySQL doesn't distinguish 'gin' and 'gist' flags\n");
- if ( mysql_query(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;")!= 0 ) {
- fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_query(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;")!= 0 )
+ fatal("mysql_query failed: %s\n", mysql_error(db->conn));
- if ( mysql_query(db->conn, "CREATE TABLE ftsbench (id int not null, body text) ENGINE MyISAM;")!= 0 ) {
- fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_query(db->conn, "CREATE TABLE ftsbench (id int not null, body text) ENGINE MyISAM;")!= 0 )
+ fatal("mysql_query failed: %s\n", mysql_error(db->conn));
}
static void
if ( db->flags & (FLG_GIN | FLG_GIST) ) {
report("(create index, ");
- if ( mysql_query(db->conn, "CREATE FULLTEXT INDEX fts ON ftsbench (body);")!= 0 ) {
- fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_query(db->conn, "CREATE FULLTEXT INDEX fts ON ftsbench (body);")!= 0 )
+ fatal("mysql_query failed: %s\n", mysql_error(db->conn));
} else
report("(");
report("optimize");
- if ( mysql_query(db->conn, "OPTIMIZE TABLE ftsbench;")!= 0 ) {
- fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_query(db->conn, "OPTIMIZE TABLE ftsbench;")!= 0 )
+ fatal("mysql_query failed: %s\n", mysql_error(db->conn));
report(") ");
}
if ( db->prepareStmt == NULL ) {
db->prepareStmt = mysql_stmt_init(db->conn);
- if ( db->prepareStmt == NULL ) {
- fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( db->prepareStmt == NULL )
+ fatal("mysql_stmt_init failed: %s\n", mysql_error(db->conn));
#define INSERT_QUERY "INSERT INTO ftsbench (id, body) VALUES ( ? , ? );"
- if ( mysql_stmt_prepare( db->prepareStmt, INSERT_QUERY, strlen(INSERT_QUERY) ) != 0 ) {
- fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_prepare( db->prepareStmt, INSERT_QUERY, strlen(INSERT_QUERY) ) != 0 )
+ fatal("mysql_stmt_init failed: %s\n", mysql_error(db->conn));
- if ( mysql_stmt_param_count(db->prepareStmt) != 2 ) {
- fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
- exit(1);
- }
+ if ( mysql_stmt_param_count(db->prepareStmt) != 2 )
+ fatal("mysql_stmt_param_count: invalid parameter count\n");
memset(data, 0, sizeof(data));
data[0].buffer_type = MYSQL_TYPE_LONG;
data[1].buffer = txt;
data[1].buffer_length = txtlen;
- if ( mysql_stmt_bind_param(db->prepareStmt, data) ) {
- fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_bind_param(db->prepareStmt, data) )
+ fatal("mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
- if ( mysql_stmt_execute( db->prepareStmt ) ) {
- fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( mysql_stmt_execute( db->prepareStmt ) )
+ fatal("mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
}
static void
db->conn = mysql_init(NULL);
- if ( !mysql_real_connect(db->conn, NULL, NULL, NULL, connstr, 0, NULL, 0) ) {
- fprintf(stderr,"mysql_real_connect failed: %s\n", mysql_error(db->conn));
- exit(1);
- }
+ if ( !mysql_real_connect(db->conn, NULL, NULL, NULL, connstr, 0, NULL, 0) )
+ fatal("mysql_real_connect failed: %s\n", mysql_error(db->conn));
db->db.execQuery = execQuery;
db->db.startCreateScheme = startCreateScheme;
/* success write, waits for read */
db->state = SS_READ;
} else {
- fprintf(stderr, "PQflush failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
+ fatal( "PQflush failed: %s\n", PQerrorMessage(db->conn));
}
}
break;
case SS_NONE:
default:
- fprintf(stderr,"Should not be here!\n");
- exit(1);
+ fatal( "Should not be here!\n");
break;
}
if ( pfd.events ) {
int ret = poll( &pfd, 1, INFTIM);
- if ( ret<0 ) {
- fprintf(stderr,"poll failed: %s\n", strerror(errno));
- exit(1);
- }
+ if ( ret<0 )
+ fatal("poll failed: %s\n", strerror(errno));
- if ( pfd.revents & (POLLHUP | POLLNVAL | POLLERR) ) {
- fprintf(stderr,"Poll report about socket error\n");
- exit(1);
+ if ( pfd.revents & (POLLHUP | POLLNVAL | POLLERR) ) {
+ fatal("Poll report about socket error\n");
} else if ( pfd.revents & POLLIN ) {
db->state = SS_READYREAD;
} else if ( pfd.revents & POLLOUT ) {
} while( checkStatus(db) != RS_OK );
while ( (res = PQgetResult(db->conn))!= NULL ) {
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
}
}
res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
db->prepared = 1;
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
/* skip error ' all words are a stop word' for GIN index -
result is empty, in any case */
- if ( checkEmptyQuery(db, res) == 0 ) {
- fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if ( checkEmptyQuery(db, res) == 0 )
+ fatal( "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
} else if ( PQntuples(res) == 1 ) {
db->db.nres += atoi( PQgetvalue(res,0,0) );
} else {
- fprintf(stderr,"Bad PQntuples %d\n", PQntuples(res));
- exit(1);
+ fatal("Bad PQntuples %d\n", PQntuples(res));
}
PQclear(res);
db->flags = flags;
res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
if ( flags & FLG_FUNC )
"FOR EACH ROW EXECUTE PROCEDURE tsearch2(fts, body);" );
res = PQexec(db->conn, buf);
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
res = PQexec(db->conn, "BEGIN;");
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
return;
if ( db->db.nquery > 0 ) {
waitResult(db);
- if ( PQsetnonblocking(db->conn, 0) != 0 ) {
- fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if ( PQsetnonblocking(db->conn, 0) != 0 )
+ fatal( "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
}
res = PQexec(db->conn, "COMMIT;");
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
if ( (db->flags & (FLG_GIST | FLG_GIN)) != 0 ) {
report("(create index, ");
res = PQexec(db->conn, buf);
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
} else
report("(");
report("vacuum");
res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
report(") ");
"INSERT INTO ftsbench (id, body) VALUES ( $1 ::int4, $2 ::text);",
2, NULL );
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
- if ( PQsetnonblocking(db->conn, 1) != 0 ) {
- fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if ( PQsetnonblocking(db->conn, 1) != 0 )
+ fatal( "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
} else {
waitResult(db);
}
if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
2, paramValues,
- paramLengths, paramFormats, 0) == 0 ) {
- fprintf(stderr, "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ paramLengths, paramFormats, 0) == 0 )
+ fatal( "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
pgflush(db);
sprintf(conninfo, "dbname=%s", connstr);
db->conn = PQconnectdb(conninfo);
- if (PQstatus(db->conn) != CONNECTION_OK) {
- fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQstatus(db->conn) != CONNECTION_OK)
+ fatal( "Connection to database failed: %s\n", PQerrorMessage(db->conn));
db->origreceiver = PQsetNoticeReceiver(db->conn, NoticeReceiver, (void *)db);
db->db.InsertRow = InsertRow;
db->db.Close = Close;
db->socket = PQsocket(db->conn);
- if ( db->socket < 0 ) {
- fprintf(stderr,"Socket error\n");
- exit(1);
- }
+ if ( db->socket < 0 )
+ fatal("Socket error\n");
return (ftsDB*)db;
}
b->str = (char*)realloc( b->str, sizeof(char) * b->length );
}
- if (!b->str) {
- fprintf(stderr,"Not enough memory (%d bytes)\n", b->length);
- exit(1);
- }
+ if (!b->str)
+ fatal("Not enough memory (%d bytes)\n", b->length);
}
memcpy(b->str + b->strlen, s, length);