Add output query words in sql mode, add most frequent wrods to filter of query-lex
[ftsbench.git] / pgdriver.c
index 206c097..adf329f 100644 (file)
@@ -1,3 +1,32 @@
+/*
+ * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the names of any co-contributors
+ *        may be used to endorse or promote products derived from this software
+ *        without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #endif /* HAVE_SYS_POLL_H */
 #endif /* HAVE_POLL */
 
+#ifndef INFTIM
+#define INFTIM (-1)
+#endif
+
 typedef enum SocketState {
     SS_NONE = 0,
        SS_READ,
@@ -81,7 +114,7 @@ pgflush(ftsPG *db) {
                /* success write, waits for read */
                db->state = SS_READ;
        } else {
-               fprintf(stderr, "PQflush failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "PQflush failed: %s\n", PQerrorMessage(db->conn));
                exit(1);
        }
 }
@@ -128,7 +161,7 @@ waitResult(ftsPG *db) {
                if ( pfd.events ) {
                        int ret = poll( &pfd, 1, INFTIM);
                        if ( ret<0 ) {
-                               fprintf(stderr,"poll failed: %s", strerror(errno));
+                               fprintf(stderr,"poll failed: %s\n", strerror(errno));
                                exit(1);
                        }
 
@@ -145,7 +178,7 @@ waitResult(ftsPG *db) {
 
        while ( (res = PQgetResult(db->conn))!= NULL ) {
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "Execution of prepared statement failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
                PQclear(res);
@@ -178,15 +211,13 @@ execQuery(ftsDB* adb, char ** words, int flags) {
 
                db->flags = flags;
 
-               if ( flags & FLG_FUNC )
-                       sprintf(buf, "SELECT count(*) FROM ftsbench WHERE to_tsvector(body) @@ to_tsquery( $1 ::text );");
-               else
-                       sprintf(buf, "SELECT count(*) FROM ftsbench WHERE fts @@ to_tsquery( $1 ::text );");
+               sprintf(buf, "SELECT count(*) FROM ftsbench WHERE %s @@ to_tsquery( $1 ::text );",
+                       ( flags & FLG_FUNC ) ? "to_tsvector(body)" : "fts" );
 
                res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
                
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "PREPARE SELECT command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
                PQclear(res);
@@ -229,7 +260,13 @@ execQuery(ftsDB* adb, char ** words, int flags) {
                        fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
+       } else if ( PQntuples(res) == 1 ) {
+               db->db.nres += atoi( PQgetvalue(res,0,0) );
+       } else {
+               fprintf(stderr,"Bad PQntuples %d\n", PQntuples(res));
+               exit(1);
        }
+       
        PQclear(res);
 
        db->emptyquery = 0;
@@ -248,7 +285,7 @@ startCreateScheme(ftsDB* adb, int flags) {
 
        res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "DROP TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
@@ -262,14 +299,14 @@ startCreateScheme(ftsDB* adb, int flags) {
 
        res = PQexec(db->conn, buf);
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
 
        res = PQexec(db->conn, "BEGIN;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
@@ -286,14 +323,14 @@ finishCreateScheme(ftsDB* adb) {
                waitResult(db);
                
                if ( PQsetnonblocking(db->conn, 0) != 0 ) {
-                       fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
        }
 
        res = PQexec(db->conn, "COMMIT;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
@@ -308,31 +345,27 @@ finishCreateScheme(ftsDB* adb) {
                        sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( fts );", 
                                                (db->flags & FLG_GIST) ? "GiST" : "GIN" );
 
-               printf("(create index, ");
-               fflush(stdout);
+               report("(create index, ");
 
                res = PQexec(db->conn, buf);
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "CREATE INDEX command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
                                exit(1);
                }
                PQclear(res);
-       } else {
-               printf("(");
-               fflush(stdout);
-       }
+       } else 
+               report("(");
 
-       printf("vacuum");
-       fflush(stdout);
+       report("vacuum");
 
        res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "VACUUM ANALYZE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
                exit(1);
        }
        PQclear(res);
-       printf(") ");
-       fflush(stdout);
+
+       report(") ");
 
        return;
 }
@@ -355,13 +388,13 @@ InsertRow(ftsDB* adb, int id, char *txt) {
                                                 2, NULL );
                
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "PREPARE INSERT command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
                PQclear(res);
 
                if ( PQsetnonblocking(db->conn, 1) != 0 ) {
-                       fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
        } else {
@@ -375,7 +408,7 @@ InsertRow(ftsDB* adb, int id, char *txt) {
        if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
                                                 2, paramValues,
                                                 paramLengths, paramFormats, 0) == 0 ) {
-                fprintf(stderr, "PQsendQueryPrepared failed: %s", PQerrorMessage(db->conn));
+                fprintf(stderr, "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
                 exit(1);
        }
 
@@ -384,6 +417,13 @@ InsertRow(ftsDB* adb, int id, char *txt) {
        db->db.nquery++;
 }
 
+static void 
+Close(ftsDB* adb) {
+       ftsPG *db = (ftsPG*)adb;
+
+       PQfinish(db->conn);
+}
+
 ftsDB* 
 PGInit(char * connstr) {
        ftsPG   *db = (ftsPG*)malloc(sizeof(ftsPG));
@@ -395,7 +435,7 @@ PGInit(char * connstr) {
        db->conn = PQconnectdb(conninfo);
 
        if (PQstatus(db->conn) != CONNECTION_OK) {
-               fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(db->conn));
                exit(1);
        }
 
@@ -405,6 +445,7 @@ PGInit(char * connstr) {
        db->db.startCreateScheme = startCreateScheme;
        db->db.finishCreateScheme = finishCreateScheme;
        db->db.InsertRow = InsertRow;
+       db->db.Close = Close;
        db->socket = PQsocket(db->conn);
        if ( db->socket < 0 ) {
                fprintf(stderr,"Socket error\n");