2 * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
37 #include <netinet/in.h>
45 #ifdef HAVE_SYS_POLL_H
48 #error Not defined HAVE_POLL_H or HAVE_SYS_POLL_H
49 #endif /* HAVE_SYS_POLL_H */
50 #endif /* HAVE_POLL */
56 typedef enum SocketState {
64 typedef enum ResStatus {
69 typedef struct ftsPG {
73 PQnoticeReceiver origreceiver;
84 NoticeReceiver(void *arg, const PGresult *res) {
85 ftsPG *db = (ftsPG*) arg;
89 if ( strcmp(PQresultErrorField(res,PG_DIAG_SQLSTATE), "00000") == 0 ) {
90 char *msg = PQresultErrorMessage(res);
92 if ( strstr(msg, "does not exist, skipping")!=NULL ) {
93 /* skip 'NOTICE: table "wow" does not exist, skipping' */
96 if ( strstr(msg, "query contains only stopword(s) or doesn't contain lexeme(s), ignored")!=NULL ) {
102 db->origreceiver(db, res);
109 flushres=PQflush(db->conn);
110 if ( flushres > 0 ) {
111 /* need more write */
112 db->state = SS_WRITE;
113 } else if ( flushres == 0 ) {
114 /* success write, waits for read */
117 fprintf(stderr, "PQflush failed: %s", PQerrorMessage(db->conn));
123 checkStatus(ftsPG* db) {
124 switch( db->state ) {
129 return RS_INPROGRESS;
131 PQconsumeInput(db->conn);
132 if ( PQisBusy(db->conn) != 0 ) {
134 return RS_INPROGRESS;
139 fprintf(stderr,"Should not be here!\n");
148 waitResult(ftsPG *db) {
154 pfd.events = pfd.revents = 0;
155 if ( db->state == SS_READ ) {
157 } else if ( db->state == SS_WRITE ) {
158 pfd.events = POLLOUT;
162 int ret = poll( &pfd, 1, INFTIM);
164 fprintf(stderr,"poll failed: %s", strerror(errno));
168 if ( pfd.revents & (POLLHUP | POLLNVAL | POLLERR) ) {
169 fprintf(stderr,"Poll report about socket error\n");
171 } else if ( pfd.revents & POLLIN ) {
172 db->state = SS_READYREAD;
173 } else if ( pfd.revents & POLLOUT ) {
174 db->state = SS_READYWRITE;
177 } while( checkStatus(db) != RS_OK );
179 while ( (res = PQgetResult(db->conn))!= NULL ) {
180 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
181 fprintf(stderr, "Execution of prepared statement failed: %s", PQerrorMessage(db->conn));
189 checkEmptyQuery(ftsPG *db, PGresult *res) {
191 db->emptyquery > 0 &&
192 PQresultStatus(res) == PGRES_FATAL_ERROR &&
193 strcmp(PQresultErrorField(res,PG_DIAG_SQLSTATE), "0A000") == 0 && /* FEATURE_NOT_SUPPORTED */
194 strstr(PQresultErrorField(res, PG_DIAG_SOURCE_FILE), "ginscan.c") != NULL
202 execQuery(ftsDB* adb, char ** words, int flags) {
203 ftsPG *db = (ftsPG*)adb;
204 const char *paramValues[1];
208 if ( db->prepared == 0 ) {
214 if ( flags & FLG_FUNC )
215 sprintf(buf, "SELECT count(*) FROM ftsbench WHERE to_tsvector(body) @@ to_tsquery( $1 ::text );");
217 sprintf(buf, "SELECT count(*) FROM ftsbench WHERE fts @@ to_tsquery( $1 ::text );");
219 res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
221 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
222 fprintf(stderr, "PREPARE SELECT command failed: %s", PQerrorMessage(db->conn));
236 sb_add(&db->b, (db->flags & FLG_OR) ? " | '" : " & '", 4);
238 sb_add(&db->b, "'", 1);
242 sb_add(&db->b, "'", 1);
243 sb_add(&db->b, ptr, 1);
246 sb_add(&db->b, "'", 1);
252 paramValues[0] = db->b.str;
254 res = PQexecPrepared( db->conn, "search_ftsbench",
258 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
259 /* skip error ' all words are a stop word' for GIN index -
260 result is empty, in any case */
261 if ( checkEmptyQuery(db, res) == 0 ) {
262 fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
269 pthread_mutex_lock(&(db->db.nqueryMutex));
271 pthread_mutex_unlock(&(db->db.nqueryMutex));
275 startCreateScheme(ftsDB* adb, int flags) {
276 ftsPG *db = (ftsPG*)adb;
282 res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
283 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
284 fprintf(stderr, "DROP TABLE command failed: %s", PQerrorMessage(db->conn));
289 if ( flags & FLG_FUNC )
290 sprintf(buf,"CREATE TABLE ftsbench( id int not null, body text );");
292 sprintf(buf,"CREATE TABLE ftsbench( id int not null, body text, fts tsvector ); "
293 "CREATE TRIGGER tsvectorupdate BEFORE UPDATE OR INSERT ON ftsbench "
294 "FOR EACH ROW EXECUTE PROCEDURE tsearch2(fts, body);" );
296 res = PQexec(db->conn, buf);
297 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
298 fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
303 res = PQexec(db->conn, "BEGIN;");
304 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
305 fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
314 finishCreateScheme(ftsDB* adb) {
315 ftsPG *db = (ftsPG*)adb;
318 if ( db->db.nquery > 0 ) {
321 if ( PQsetnonblocking(db->conn, 0) != 0 ) {
322 fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
327 res = PQexec(db->conn, "COMMIT;");
328 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
329 fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
334 if ( (db->flags & (FLG_GIST | FLG_GIN)) != 0 ) {
337 if ( db->flags & FLG_FUNC )
338 sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( to_tsvector(body) );",
339 (db->flags & FLG_GIST) ? "GiST" : "GIN" );
341 sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( fts );",
342 (db->flags & FLG_GIST) ? "GiST" : "GIN" );
344 printf("(create index, ");
347 res = PQexec(db->conn, buf);
348 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
349 fprintf(stderr, "CREATE INDEX command failed: %s", PQerrorMessage(db->conn));
361 res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
362 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
363 fprintf(stderr, "VACUUM ANALYZE command failed: %s", PQerrorMessage(db->conn));
375 InsertRow(ftsDB* adb, int id, char *txt) {
376 ftsPG *db = (ftsPG*)adb;
378 const char *paramValues[2];
379 uint32_t binaryIntVal;
380 int paramLengths[] = {sizeof(binaryIntVal), 0};
381 int paramFormats[] = {1, 0};
383 if ( db->db.nquery == 0 ) {
386 res = PQprepare( db->conn, "insert_ftsbench",
387 "INSERT INTO ftsbench (id, body) VALUES ( $1 ::int4, $2 ::text);",
390 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
391 fprintf(stderr, "PREPARE INSERT command failed: %s", PQerrorMessage(db->conn));
396 if ( PQsetnonblocking(db->conn, 1) != 0 ) {
397 fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
404 binaryIntVal = htonl((uint32_t) id);
405 paramValues[0] = (char*)&binaryIntVal;
406 paramValues[1] = txt;
408 if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
410 paramLengths, paramFormats, 0) == 0 ) {
411 fprintf(stderr, "PQsendQueryPrepared failed: %s", PQerrorMessage(db->conn));
421 PGInit(char * connstr) {
422 ftsPG *db = (ftsPG*)malloc(sizeof(ftsPG));
425 memset(db,0,sizeof(ftsPG));
427 sprintf(conninfo, "dbname=%s", connstr);
428 db->conn = PQconnectdb(conninfo);
430 if (PQstatus(db->conn) != CONNECTION_OK) {
431 fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(db->conn));
435 db->origreceiver = PQsetNoticeReceiver(db->conn, NoticeReceiver, (void *)db);
437 db->db.execQuery = execQuery;
438 db->db.startCreateScheme = startCreateScheme;
439 db->db.finishCreateScheme = finishCreateScheme;
440 db->db.InsertRow = InsertRow;
441 db->socket = PQsocket(db->conn);
442 if ( db->socket < 0 ) {
443 fprintf(stderr,"Socket error\n");