* The full text indexer now runs in its own dedicated thread instead of
[citadel.git] / citadel / serv_fulltext.c
1 /*
2  * $Id$
3  *
4  * This module handles fulltext indexing of the message base.
5  *
6  */
7
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "serv_extensions.h"
40 #include "database.h"
41 #include "msgbase.h"
42 #include "control.h"
43 #include "room_ops.h"
44 #include "tools.h"
45 #include "serv_fulltext.h"
46 #include "ft_wordbreaker.h"
47
48
49 long ft_newhighest = 0L;
50 long *ft_newmsgs = NULL;
51 int ft_num_msgs = 0;
52 int ft_num_alloc = 0;
53
54
55 /*
56  * Compare function
57  */
58 int longcmp(const void *rec1, const void *rec2) {
59         long i1, i2;
60
61         i1 = *(const long *)rec1;
62         i2 = *(const long *)rec2;
63
64         if (i1 > i2) return(1);
65         if (i1 < i2) return(-1);
66         return(0);
67 }
68
69
70
71
72 /*
73  * Index or de-index a message.  (op == 1 to index, 0 to de-index)
74  */
75 void ft_index_message(long msgnum, int op) {
76         int num_tokens = 0;
77         int *tokens = NULL;
78         int i, j;
79         struct cdbdata *cdb_bucket;
80         int num_msgs;
81         long *msgs;
82         char *msgtext;
83
84         lprintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
85                 (op ? "adding" : "removing") , msgnum
86         );
87
88         /* Output the message as text before indexing it, so we don't end up
89          * indexing a bunch of encoded base64, etc.
90          */
91         CC->redirect_buffer = malloc(SIZ);
92         CC->redirect_len = 0;
93         CC->redirect_alloc = SIZ;
94         CtdlOutputMsg(msgnum, MT_CITADEL, HEADERS_ALL, 0, 1);
95         msgtext = CC->redirect_buffer;
96         CC->redirect_buffer = NULL;
97         CC->redirect_len = 0;
98         CC->redirect_alloc = 0;
99         lprintf(CTDL_DEBUG, "Wordbreaking message %ld...\n", msgnum);
100         wordbreaker(msgtext, &num_tokens, &tokens);
101         free(msgtext);
102
103         lprintf(CTDL_DEBUG, "Indexing message %ld...\n", msgnum);
104         if (num_tokens > 0) {
105                 for (i=0; i<num_tokens; ++i) {
106
107                         /* Add the message to the relevant token bucket */
108
109                         /* FIXME do we need to lock the file here? */
110                         cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i],
111                                                                 sizeof(int));
112                         if (cdb_bucket == NULL) {
113                                 cdb_bucket = malloc(sizeof(struct cdbdata));
114                                 cdb_bucket->len = 0;
115                                 cdb_bucket->ptr = NULL;
116                                 num_msgs = 0;
117                         }
118                         else {
119                                 num_msgs = cdb_bucket->len / sizeof(long);
120                         }
121
122                         if (op == 1) {  /* add to index */
123                                 ++num_msgs;
124                                 cdb_bucket->ptr = realloc(cdb_bucket->ptr,
125                                                         num_msgs*sizeof(long));
126                                 msgs = (long *) cdb_bucket->ptr;
127                                 msgs[num_msgs - 1] = msgnum;
128                         }
129
130                         if (op == 0) {  /* remove from index */
131                                 if (num_msgs >= 1) {
132                                 msgs = (long *) cdb_bucket->ptr;
133                                         for (j=0; j<num_msgs; ++j) {
134                                                 if (msgs[j] == msgnum) {
135                                                         memmove(&msgs[j], &msgs[j+1], ((num_msgs - j - 1)*sizeof(long)));
136                                                         --num_msgs;
137                                                         --j;
138                                                 }
139                                         }
140                                 }
141                         }
142
143                         cdb_store(CDB_FULLTEXT, &tokens[i], sizeof(int),
144                                 msgs, (num_msgs*sizeof(long)) );
145
146                         cdb_free(cdb_bucket);
147
148                         /* FIXME do we need to unlock the file here? */
149                 }
150
151                 free(tokens);
152         }
153 }
154
155
156
157 /*
158  * Add a message to the list of those to be indexed.
159  */
160 void ft_index_msg(long msgnum, void *userdata) {
161
162         if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
163                 ++ft_num_msgs;
164                 if (ft_num_msgs > ft_num_alloc) {
165                         ft_num_alloc += 1024;
166                         ft_newmsgs = realloc(ft_newmsgs,
167                                 (ft_num_alloc * sizeof(long)));
168                 }
169                 ft_newmsgs[ft_num_msgs - 1] = msgnum;
170         }
171
172 }
173
174 /*
175  * Scan a room for messages to index.
176  */
177 void ft_index_room(struct ctdlroom *qrbuf, void *data)
178 {
179         getroom(&CC->room, qrbuf->QRname);
180         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
181 }
182
183
184 /*
185  * Begin the fulltext indexing process.  (Called as an EVT_TIMER event)
186  */
187 void do_fulltext_indexing(void) {
188         int i;
189         static time_t last_index = 0L;
190         static time_t last_progress = 0L;
191
192         /*
193          * Don't do this if the site doesn't have it enabled.
194          */
195         if (!config.c_enable_fulltext) {
196                 return;
197         }
198
199         /*
200          * Make sure we don't run the indexer too frequently.
201          * FIXME move the setting into config
202          */
203         if ( (time(NULL) - last_index) < 300L) {
204                 return;
205         }
206
207         /*
208          * Check to see whether the fulltext index is up to date; if there
209          * are no messages to index, don't waste any more time trying.
210          */
211         if (CitControl.MMfulltext >= CitControl.MMhighest) {
212                 return;         /* nothing to do! */
213         }
214
215         lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
216         
217         /*
218          * If we've switched wordbreaker modules, burn the index and start
219          * over.
220          */
221         if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
222                 lprintf(CTDL_INFO, "(re)initializing full text index\n");
223                 cdb_trunc(CDB_FULLTEXT);
224                 CitControl.MMfulltext = 0L;
225                 put_control();
226         }
227
228         /*
229          * Now go through each room and find messages to index.
230          */
231         ft_newhighest = CitControl.MMhighest;
232         ForEachRoom(ft_index_room, NULL);       /* load all msg pointers */
233
234         if (ft_num_msgs > 0) {
235                 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
236                 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
237                         if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
238                                 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
239                                         ((ft_num_msgs - i - 1)*sizeof(long)));
240                                 --ft_num_msgs;
241                                 --i;
242                         }
243                 }
244
245                 /* Here it is ... do each message! */
246                 for (i=0; i<ft_num_msgs; ++i) {
247                         if ((time(NULL) - last_progress) > 10) {
248                                 lprintf(CTDL_DEBUG,
249                                         "Indexed %d of %d messages (%d%%)\n",
250                                                 i, ft_num_msgs,
251                                                 ((i*100) / ft_num_msgs)
252                                 );
253                                 last_progress = time(NULL);
254                         }
255                         ft_index_message(ft_newmsgs[i], 1);
256
257                         /* Check to see if we need to quit early */
258                         if (time_to_die) {
259                                 lprintf(CTDL_DEBUG, "Indexer quitting early\n");
260                                 ft_newhighest = ft_newmsgs[i];
261                                 break;
262                         }
263
264                 }
265
266                 free(ft_newmsgs);
267                 ft_num_msgs = 0;
268                 ft_num_alloc = 0;
269                 ft_newmsgs = NULL;
270         }
271
272         /* Save our place so we don't have to do this again */
273         CitControl.MMfulltext = ft_newhighest;
274         CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
275         put_control();
276         last_index = time(NULL);
277
278         lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
279         return;
280 }
281
282 /*
283  * Main loop for the indexer thread.
284  */
285 void *indexer_thread(void *arg) {
286         struct CitContext indexerCC;
287
288         lprintf(CTDL_DEBUG, "indexer_thread() initializing\n");
289
290         memset(&indexerCC, 0, sizeof(struct CitContext));
291         indexerCC.internal_pgm = 1;
292         indexerCC.cs_pid = 0;
293         pthread_setspecific(MyConKey, (void *)&indexerCC );
294
295         cdb_allocate_tsd();
296
297         while (!time_to_die) {
298                 do_fulltext_indexing();
299                 sleep(1);
300         }
301
302         lprintf(CTDL_DEBUG, "indexer_thread() exiting\n");
303         pthread_exit(NULL);
304 }
305
306
307
308 /*
309  * API call to perform searches.
310  * (This one does the "all of these words" search.)
311  * Caller is responsible for freeing the message list.
312  */
313 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
314         int num_tokens = 0;
315         int *tokens = NULL;
316         int i, j;
317         struct cdbdata *cdb_bucket;
318         int num_msgs;
319         long *msgs;
320         int num_all_msgs = 0;
321         long *all_msgs = NULL;
322         int num_ret_msgs = 0;
323         int num_ret_alloc = 0;
324         long *ret_msgs = NULL;
325
326         wordbreaker(search_string, &num_tokens, &tokens);
327         if (num_tokens > 0) {
328                 for (i=0; i<num_tokens; ++i) {
329
330                         /* search for tokens[i] */
331                         cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i],
332                                                                 sizeof(int));
333                         if (cdb_bucket != NULL) {
334                                 num_msgs = cdb_bucket->len / sizeof(long);
335                                 msgs = (long *)cdb_bucket->ptr;
336
337                                 num_all_msgs += num_msgs;
338                                 if (num_all_msgs > 0) {
339                                         all_msgs = realloc(all_msgs,
340                                                 num_all_msgs*sizeof(long) );
341                                         memcpy(&all_msgs[num_all_msgs-num_msgs],
342                                                 msgs, num_msgs*sizeof(long) );
343                                 }
344
345                                 cdb_free(cdb_bucket);
346                         }
347
348                 }
349                 free(tokens);
350                 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
351
352                 /*
353                  * At this point, if a message appears num_tokens times in the
354                  * list, then it contains all of the search tokens.
355                  */
356                 if (num_all_msgs >= num_tokens)
357                    for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
358                         if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
359
360                                 ++num_ret_msgs;
361                                 if (num_ret_msgs > num_ret_alloc) {
362                                         num_ret_alloc += 64;
363                                         ret_msgs = realloc(ret_msgs,
364                                                 (num_ret_alloc*sizeof(long)) );
365                                 }
366                                 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
367
368                         }
369                 }
370
371                 free(all_msgs);
372         }
373         
374         *fts_num_msgs = num_ret_msgs;
375         *fts_msgs = ret_msgs;
376 }
377
378
379 /*
380  * Tentative form of a search command
381  */
382 void cmd_srch(char *argbuf) {
383         int num_msgs = 0;
384         long *msgs = NULL;
385         int i;
386         char search_string[256];
387
388         if (CtdlAccessCheck(ac_logged_in)) return;
389
390         if (!config.c_enable_fulltext) {
391                 cprintf("%d Full text index is not enabled on this server.\n",
392                         ERROR + CMD_NOT_SUPPORTED);
393         }
394
395         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
396         ft_search(&num_msgs, &msgs, search_string);
397
398         cprintf("%d %d msgs match all search words:\n",
399                 LISTING_FOLLOWS, num_msgs);
400         if (num_msgs > 0) {
401                 for (i=0; i<num_msgs; ++i) {
402                         cprintf("%ld\n", msgs[i]);
403                 }
404         }
405         if (msgs != NULL) free(msgs);
406         cprintf("000\n");
407 }
408
409
410 /*****************************************************************************/
411
412 char *serv_fulltext_init(void)
413 {
414         CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
415         return "$Id$";
416 }