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