8 #include <netinet/in.h>
16 #ifdef HAVE_SYS_POLL_H
19 #error Not defined HAVE_POLL_H or HAVE_SYS_POLL_H
20 #endif /* HAVE_SYS_POLL_H */
21 #endif /* HAVE_POLL */
23 typedef enum SocketState {
31 typedef enum ResStatus {
36 typedef struct ftsPG {
40 PQnoticeReceiver origreceiver;
51 NoticeReceiver(void *arg, const PGresult *res) {
52 ftsPG *db = (ftsPG*) arg;
56 if ( strcmp(PQresultErrorField(res,PG_DIAG_SQLSTATE), "00000") == 0 ) {
57 char *msg = PQresultErrorMessage(res);
59 if ( strstr(msg, "does not exist, skipping")!=NULL ) {
60 /* skip 'NOTICE: table "wow" does not exist, skipping' */
63 if ( strstr(msg, "query contains only stopword(s) or doesn't contain lexeme(s), ignored")!=NULL ) {
69 db->origreceiver(db, res);
76 flushres=PQflush(db->conn);
80 } else if ( flushres == 0 ) {
81 /* success write, waits for read */
84 fprintf(stderr, "PQflush failed: %s", PQerrorMessage(db->conn));
90 checkStatus(ftsPG* db) {
98 PQconsumeInput(db->conn);
99 if ( PQisBusy(db->conn) != 0 ) {
101 return RS_INPROGRESS;
106 fprintf(stderr,"Should not be here!\n");
115 waitResult(ftsPG *db) {
121 pfd.events = pfd.revents = 0;
122 if ( db->state == SS_READ ) {
124 } else if ( db->state == SS_WRITE ) {
125 pfd.events = POLLOUT;
129 int ret = poll( &pfd, 1, INFTIM);
131 fprintf(stderr,"poll failed: %s", strerror(errno));
135 if ( pfd.revents & (POLLHUP | POLLNVAL | POLLERR) ) {
136 fprintf(stderr,"Poll report about socket error\n");
138 } else if ( pfd.revents & POLLIN ) {
139 db->state = SS_READYREAD;
140 } else if ( pfd.revents & POLLOUT ) {
141 db->state = SS_READYWRITE;
144 } while( checkStatus(db) != RS_OK );
146 while ( (res = PQgetResult(db->conn))!= NULL ) {
147 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
148 fprintf(stderr, "Execution of prepared statement failed: %s", PQerrorMessage(db->conn));
156 checkEmptyQuery(ftsPG *db, PGresult *res) {
158 db->emptyquery > 0 &&
159 PQresultStatus(res) == PGRES_FATAL_ERROR &&
160 strcmp(PQresultErrorField(res,PG_DIAG_SQLSTATE), "0A000") == 0 && /* FEATURE_NOT_SUPPORTED */
161 strstr(PQresultErrorField(res, PG_DIAG_SOURCE_FILE), "ginscan.c") != NULL
169 execQuery(ftsDB* adb, char ** words, int flags) {
170 ftsPG *db = (ftsPG*)adb;
171 const char *paramValues[1];
175 if ( db->prepared == 0 ) {
181 if ( flags & FLG_FUNC )
182 sprintf(buf, "SELECT count(*) FROM ftsbench WHERE to_tsvector(body) @@ to_tsquery( $1 ::text );");
184 sprintf(buf, "SELECT count(*) FROM ftsbench WHERE fts @@ to_tsquery( $1 ::text );");
186 res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
188 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
189 fprintf(stderr, "PREPARE SELECT command failed: %s", PQerrorMessage(db->conn));
203 sb_add(&db->b, (db->flags & FLG_OR) ? " | '" : " & '", 4);
205 sb_add(&db->b, "'", 1);
209 sb_add(&db->b, "'", 1);
210 sb_add(&db->b, ptr, 1);
213 sb_add(&db->b, "'", 1);
219 paramValues[0] = db->b.str;
221 res = PQexecPrepared( db->conn, "search_ftsbench",
225 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
226 /* skip error ' all words are a stop word' for GIN index -
227 result is empty, in any case */
228 if ( checkEmptyQuery(db, res) == 0 ) {
229 fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
236 pthread_mutex_lock(&(db->db.nqueryMutex));
238 pthread_mutex_unlock(&(db->db.nqueryMutex));
242 startCreateScheme(ftsDB* adb, int flags) {
243 ftsPG *db = (ftsPG*)adb;
249 res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
250 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
251 fprintf(stderr, "DROP TABLE command failed: %s", PQerrorMessage(db->conn));
256 if ( flags & FLG_FUNC )
257 sprintf(buf,"CREATE TABLE ftsbench( id int not null, body text );");
259 sprintf(buf,"CREATE TABLE ftsbench( id int not null, body text, fts tsvector ); "
260 "CREATE TRIGGER tsvectorupdate BEFORE UPDATE OR INSERT ON ftsbench "
261 "FOR EACH ROW EXECUTE PROCEDURE tsearch2(fts, body);" );
263 res = PQexec(db->conn, buf);
264 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
265 fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
270 res = PQexec(db->conn, "BEGIN;");
271 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
272 fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
281 finishCreateScheme(ftsDB* adb) {
282 ftsPG *db = (ftsPG*)adb;
285 if ( db->db.nquery > 0 ) {
288 if ( PQsetnonblocking(db->conn, 0) != 0 ) {
289 fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
294 res = PQexec(db->conn, "COMMIT;");
295 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
296 fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
301 if ( (db->flags & (FLG_GIST | FLG_GIN)) != 0 ) {
304 if ( db->flags & FLG_FUNC )
305 sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( to_tsvector(body) );",
306 (db->flags & FLG_GIST) ? "GiST" : "GIN" );
308 sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( fts );",
309 (db->flags & FLG_GIST) ? "GiST" : "GIN" );
311 printf("(create index, ");
314 res = PQexec(db->conn, buf);
315 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
316 fprintf(stderr, "CREATE INDEX command failed: %s", PQerrorMessage(db->conn));
328 res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
329 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
330 fprintf(stderr, "VACUUM ANALYZE command failed: %s", PQerrorMessage(db->conn));
342 InsertRow(ftsDB* adb, int id, char *txt) {
343 ftsPG *db = (ftsPG*)adb;
345 const char *paramValues[2];
346 uint32_t binaryIntVal;
347 int paramLengths[] = {sizeof(binaryIntVal), 0};
348 int paramFormats[] = {1, 0};
350 if ( db->db.nquery == 0 ) {
353 res = PQprepare( db->conn, "insert_ftsbench",
354 "INSERT INTO ftsbench (id, body) VALUES ( $1 ::int4, $2 ::text);",
357 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
358 fprintf(stderr, "PREPARE INSERT command failed: %s", PQerrorMessage(db->conn));
363 if ( PQsetnonblocking(db->conn, 1) != 0 ) {
364 fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
371 binaryIntVal = htonl((uint32_t) id);
372 paramValues[0] = (char*)&binaryIntVal;
373 paramValues[1] = txt;
375 if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
377 paramLengths, paramFormats, 0) == 0 ) {
378 fprintf(stderr, "PQsendQueryPrepared failed: %s", PQerrorMessage(db->conn));
388 PGInit(char * connstr) {
389 ftsPG *db = (ftsPG*)malloc(sizeof(ftsPG));
392 memset(db,0,sizeof(ftsPG));
394 sprintf(conninfo, "dbname=%s", connstr);
395 db->conn = PQconnectdb(conninfo);
397 if (PQstatus(db->conn) != CONNECTION_OK) {
398 fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(db->conn));
402 db->origreceiver = PQsetNoticeReceiver(db->conn, NoticeReceiver, (void *)db);
404 db->db.execQuery = execQuery;
405 db->db.startCreateScheme = startCreateScheme;
406 db->db.finishCreateScheme = finishCreateScheme;
407 db->db.InsertRow = InsertRow;
408 db->socket = PQsocket(db->conn);
409 if ( db->socket < 0 ) {
410 fprintf(stderr,"Socket error\n");