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\n", 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\n", 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\n", 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 sprintf(buf, "SELECT count(*) FROM ftsbench WHERE %s @@ to_tsquery( $1 ::text );",
215 ( flags & FLG_FUNC ) ? "to_tsvector(body)" : "fts" );
217 res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
219 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
220 fprintf(stderr, "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
234 sb_add(&db->b, (db->flags & FLG_OR) ? " | '" : " & '", 4);
236 sb_add(&db->b, "'", 1);
240 sb_add(&db->b, "'", 1);
241 sb_add(&db->b, ptr, 1);
244 sb_add(&db->b, "'", 1);
250 paramValues[0] = db->b.str;
252 res = PQexecPrepared( db->conn, "search_ftsbench",
256 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
257 /* skip error ' all words are a stop word' for GIN index -
258 result is empty, in any case */
259 if ( checkEmptyQuery(db, res) == 0 ) {
260 fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
267 pthread_mutex_lock(&(db->db.nqueryMutex));
269 pthread_mutex_unlock(&(db->db.nqueryMutex));
273 startCreateScheme(ftsDB* adb, int flags) {
274 ftsPG *db = (ftsPG*)adb;
280 res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
281 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
282 fprintf(stderr, "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
287 if ( flags & FLG_FUNC )
288 sprintf(buf,"CREATE TABLE ftsbench( id int not null, body text );");
290 sprintf(buf,"CREATE TABLE ftsbench( id int not null, body text, fts tsvector ); "
291 "CREATE TRIGGER tsvectorupdate BEFORE UPDATE OR INSERT ON ftsbench "
292 "FOR EACH ROW EXECUTE PROCEDURE tsearch2(fts, body);" );
294 res = PQexec(db->conn, buf);
295 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
296 fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
301 res = PQexec(db->conn, "BEGIN;");
302 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
303 fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
312 finishCreateScheme(ftsDB* adb) {
313 ftsPG *db = (ftsPG*)adb;
316 if ( db->db.nquery > 0 ) {
319 if ( PQsetnonblocking(db->conn, 0) != 0 ) {
320 fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
325 res = PQexec(db->conn, "COMMIT;");
326 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
327 fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
332 if ( (db->flags & (FLG_GIST | FLG_GIN)) != 0 ) {
335 if ( db->flags & FLG_FUNC )
336 sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( to_tsvector(body) );",
337 (db->flags & FLG_GIST) ? "GiST" : "GIN" );
339 sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( fts );",
340 (db->flags & FLG_GIST) ? "GiST" : "GIN" );
342 printf("(create index, ");
345 res = PQexec(db->conn, buf);
346 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
347 fprintf(stderr, "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
359 res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
360 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
361 fprintf(stderr, "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
373 InsertRow(ftsDB* adb, int id, char *txt) {
374 ftsPG *db = (ftsPG*)adb;
376 const char *paramValues[2];
377 uint32_t binaryIntVal;
378 int paramLengths[] = {sizeof(binaryIntVal), 0};
379 int paramFormats[] = {1, 0};
381 if ( db->db.nquery == 0 ) {
384 res = PQprepare( db->conn, "insert_ftsbench",
385 "INSERT INTO ftsbench (id, body) VALUES ( $1 ::int4, $2 ::text);",
388 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
389 fprintf(stderr, "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
394 if ( PQsetnonblocking(db->conn, 1) != 0 ) {
395 fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
402 binaryIntVal = htonl((uint32_t) id);
403 paramValues[0] = (char*)&binaryIntVal;
404 paramValues[1] = txt;
406 if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
408 paramLengths, paramFormats, 0) == 0 ) {
409 fprintf(stderr, "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
419 PGInit(char * connstr) {
420 ftsPG *db = (ftsPG*)malloc(sizeof(ftsPG));
423 memset(db,0,sizeof(ftsPG));
425 sprintf(conninfo, "dbname=%s", connstr);
426 db->conn = PQconnectdb(conninfo);
428 if (PQstatus(db->conn) != CONNECTION_OK) {
429 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(db->conn));
433 db->origreceiver = PQsetNoticeReceiver(db->conn, NoticeReceiver, (void *)db);
435 db->db.execQuery = execQuery;
436 db->db.startCreateScheme = startCreateScheme;
437 db->db.finishCreateScheme = finishCreateScheme;
438 db->db.InsertRow = InsertRow;
439 db->socket = PQsocket(db->conn);
440 if ( db->socket < 0 ) {
441 fprintf(stderr,"Socket error\n");