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