Add close() call
[ftsbench.git] / mysqldriver.c
1 /*
2  * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <time.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <netinet/in.h>
38
39 #include <mysql.h>
40 #include "ftsbench.h"
41
42 typedef struct ftsMY {
43         ftsDB   db;
44         MYSQL   *conn;
45         int     flags;
46         MYSQL_STMT              *prepareStmt;
47         StringBuf       b;
48 } ftsMY;
49
50 static void
51 execQuery(ftsDB *adb, char **words, int flags) {
52         ftsMY *db = (ftsMY*)adb;
53         static MYSQL_BIND       data;
54         MYSQL_RES       *res;
55
56         if ( db->prepareStmt == NULL ) {
57                 db->prepareStmt = mysql_stmt_init(db->conn);
58                 if ( db->prepareStmt == NULL ) {
59                         fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
60                         exit(1);
61                 }
62
63 #define SEARCH_QUERY "SELECT count(*) FROM ftsbench WHERE MATCH(body) AGAINST ( ? IN BOOLEAN MODE);"
64                 if ( mysql_stmt_prepare( db->prepareStmt, SEARCH_QUERY, strlen(SEARCH_QUERY) ) != 0 ) {
65                         fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
66                         exit(1);
67                 }
68
69                 if ( mysql_stmt_param_count(db->prepareStmt) != 1 ) {
70                         fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
71                         exit(1);
72                 }
73
74                 memset(&data, 0, sizeof(MYSQL_BIND));
75                 data.buffer_type = MYSQL_TYPE_BLOB;
76                 data.length = (unsigned long*)&(db->b.strlen);
77
78                 db->flags = flags;
79         }
80
81     db->b.strlen = 0;
82                  
83         while( *words ) {
84                 if ( db->flags & FLG_OR)
85                         sb_add(&db->b, " ", 1);
86                 else
87                         sb_add(&db->b, " +", 2);
88
89                 sb_add(&db->b, *words, -1);
90                 words++;
91         }
92
93         data.buffer = db->b.str;
94         data.buffer_length = db->b.strlen;
95
96         if ( mysql_stmt_bind_param(db->prepareStmt, &data) ) {
97                 fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
98                 exit(1);
99         }
100
101         if ( mysql_stmt_execute( db->prepareStmt ) ) {
102                 fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
103                 exit(1);
104         }
105
106         res = mysql_store_result(db->conn);
107         if ( res == NULL ) {
108                 if  ( mysql_errno(db->conn)!=0 ) {
109                         fprintf(stderr,"mysql_store_result failed: %s\n", mysql_error(db->conn));
110                         exit(1);
111                 }
112         } else
113                 mysql_free_result(res);
114
115         pthread_mutex_lock(&(db->db.nqueryMutex));
116         db->db.nquery ++;
117         pthread_mutex_unlock(&(db->db.nqueryMutex));
118 }
119
120 static void
121 startCreateScheme(ftsDB *adb, int flags) {
122         ftsMY *db = (ftsMY*)adb;
123
124         db->flags = flags;
125         if ( flags & FLG_FUNC )
126                 printf("Flag 'func' is ignored by MySQL\n");
127
128         if ( flags & (FLG_GIN | FLG_GIST) )
129                 printf("MySQL doesn't distinguish 'gin' and 'gist' flags\n");
130
131         if ( mysql_query(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;")!= 0 ) {
132                 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
133                 exit(1);
134         }
135
136         if ( mysql_query(db->conn, "CREATE TABLE ftsbench (id int not null, body text) ENGINE MyISAM;")!= 0 ) {
137                 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
138                 exit(1);
139         }
140 }
141
142 static void
143 finishCreateScheme(ftsDB *adb) {
144         ftsMY *db = (ftsMY*)adb;
145
146         if  ( db->flags & (FLG_GIN | FLG_GIST) ) {
147                 printf("(create index, ");
148                 fflush(stdout);
149
150                 if ( mysql_query(db->conn, "CREATE FULLTEXT INDEX fts ON ftsbench (body);")!= 0 ) {
151                         fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
152                         exit(1);
153                 }
154         } else {
155                 printf("(");
156                 fflush(stdout);
157         }
158
159         printf("optimize");
160         fflush(stdout);
161         if ( mysql_query(db->conn, "OPTIMIZE TABLE ftsbench;")!= 0 ) {
162                 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
163                 exit(1);
164         }
165
166         printf(") ");
167         fflush(stdout);
168 }
169
170 static void
171 InsertRow(ftsDB *adb, int id, char *txt) {
172         ftsMY *db = (ftsMY*)adb;
173         static MYSQL_BIND       data[2];
174         static unsigned long    txtlen;
175
176         if ( db->prepareStmt == NULL ) {
177                 db->prepareStmt = mysql_stmt_init(db->conn);
178                 if ( db->prepareStmt == NULL ) {
179                         fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
180                         exit(1);
181                 }
182
183 #define INSERT_QUERY "INSERT INTO ftsbench (id, body) VALUES ( ? , ? );"
184                 if ( mysql_stmt_prepare( db->prepareStmt, INSERT_QUERY, strlen(INSERT_QUERY) ) != 0 ) {
185                         fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
186                         exit(1);
187                 }
188
189                 if ( mysql_stmt_param_count(db->prepareStmt) != 2 ) {
190                         fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
191                         exit(1);
192                 }
193
194                 memset(data, 0, sizeof(data));
195                 data[0].buffer_type = MYSQL_TYPE_LONG;
196                 data[1].buffer_type = MYSQL_TYPE_BLOB;
197                 data[1].length = &txtlen;
198
199         }
200
201         data[0].buffer = &id;
202
203         txtlen = (unsigned long) strlen(txt);
204         data[1].buffer = txt;
205         data[1].buffer_length = txtlen;
206
207         if ( mysql_stmt_bind_param(db->prepareStmt, data) ) {
208                 fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
209                 exit(1);
210         }
211
212         if ( mysql_stmt_execute( db->prepareStmt ) ) {
213                 fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
214                 exit(1);
215         }
216 }
217
218 static void
219 Close(ftsDB* adb) {  
220     ftsMY *db = (ftsMY*)adb;
221
222         mysql_close(db->conn);
223 }
224
225 ftsDB* 
226 MYInit(char * connstr) {
227         ftsMY   *db = (ftsMY*)malloc(sizeof(ftsMY));
228
229         memset(db,0,sizeof(ftsMY));
230
231         db->conn = mysql_init(NULL);
232
233         if ( !mysql_real_connect(db->conn, NULL, NULL, NULL, connstr, 0, NULL, 0) ) {
234                 fprintf(stderr,"mysql_real_connect failed: %s\n", mysql_error(db->conn));
235                 exit(1);
236         }
237
238         db->db.execQuery = execQuery;
239         db->db.startCreateScheme = startCreateScheme;
240         db->db.finishCreateScheme = finishCreateScheme;
241         db->db.InsertRow = InsertRow;
242         db->db.Close = Close;
243         
244         return (ftsDB*)db;
245 }