]> code.citadel.org Git - citadel.git/blob - citadel/serv_fulltext.c
*** empty log message ***
[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         struct CtdlMessage *msg;
77         int num_tokens = 0;
78         int *tokens = NULL;
79         int i, j;
80         struct cdbdata *cdb_bucket;
81         int num_msgs;
82         long *msgs;
83
84         lprintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
85                 (op ? "adding" : "removing") , msgnum
86         );
87
88         msg = CtdlFetchMessage(msgnum, 1);
89         if (msg == NULL) return;
90
91         if (msg->cm_fields['M'] != NULL) {
92                 wordbreaker(msg->cm_fields['M'], &num_tokens, &tokens);
93         }
94         CtdlFreeMessage(msg);
95
96         if (num_tokens > 0) {
97                 for (i=0; i<num_tokens; ++i) {
98
99                         /* Add the message to the relevant token bucket */
100
101                         /* FIXME lock the file */
102                         cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i], sizeof(int));
103                         if (cdb_bucket == NULL) {
104                                 cdb_bucket = malloc(sizeof(struct cdbdata));
105                                 cdb_bucket->len = 0;
106                                 cdb_bucket->ptr = NULL;
107                                 num_msgs = 0;
108                         }
109                         else {
110                                 num_msgs = cdb_bucket->len / sizeof(long);
111                         }
112
113                         if (op == 1) {  /* add to index */
114                                 ++num_msgs;
115                                 cdb_bucket->ptr = realloc(cdb_bucket->ptr, num_msgs*sizeof(long) );
116                                 msgs = (long *) cdb_bucket->ptr;
117                                 msgs[num_msgs - 1] = msgnum;
118                         }
119
120                         if (op == 0) {  /* remove from index */
121                                 /* FIXME do this */
122                         }
123
124                         /* sort and purge dups */
125                         if (num_msgs > 1) {
126                                 qsort(msgs, num_msgs, sizeof(long), longcmp);
127                                 for (j=0; j<(num_msgs-1); ++j) {
128                                         if (msgs[j] == msgs[j+1]) {
129                                                 memmove(&msgs[j], &msgs[j+1],
130                                                         ((num_msgs - j)*sizeof(long)));
131                                                 --num_msgs;
132                                         }
133                                 }
134                         }
135
136                         cdb_store(CDB_FULLTEXT, &tokens[i], sizeof(int),
137                                 msgs, (num_msgs*sizeof(long)) );
138
139                         cdb_free(cdb_bucket);
140
141                         /* FIXME unlock the file */
142                 }
143
144                 free(tokens);
145         }
146 }
147
148
149
150 /*
151  * Add a message to the list of those to be indexed.
152  */
153 void ft_index_msg(long msgnum, void *userdata) {
154
155         if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
156                 ++ft_num_msgs;
157                 if (ft_num_msgs > ft_num_alloc) {
158                         ft_num_alloc += 1024;
159                         ft_newmsgs = realloc(ft_newmsgs,
160                                 (ft_num_alloc * sizeof(long)));
161                 }
162                 ft_newmsgs[ft_num_msgs - 1] = msgnum;
163         }
164
165 }
166
167 /*
168  * Scan a room for messages to index.
169  */
170 void ft_index_room(struct ctdlroom *qrbuf, void *data)
171 {
172         getroom(&CC->room, qrbuf->QRname);
173         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
174 }
175
176
177 /*
178  * Begin the fulltext indexing process.  (Called as an EVT_TIMER event)
179  */
180 void do_fulltext_indexing(void) {
181         int i;
182         static time_t last_index = 0L;
183
184         /*
185          * Make sure we don't run the indexer too frequently.
186          * FIXME move the setting into config
187          */
188         if ( (time(NULL) - last_index) < 300L) {
189                 return;
190         }
191
192         /*
193          * Check to see whether the fulltext index is up to date; if there
194          * are no messages to index, don't waste any more time trying.
195          */
196         lprintf(CTDL_DEBUG, "CitControl.MMhighest  = %ld\n", CitControl.MMhighest);
197         lprintf(CTDL_DEBUG, "CitControl.MMfulltext = %ld\n", CitControl.MMfulltext);
198         if (CitControl.MMfulltext >= CitControl.MMhighest) {
199                 /* nothing to do! */
200                 return;
201         }
202
203         lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
204         
205         /*
206          * If we've switched wordbreaker modules, burn the index and start
207          * over.  FIXME write this...
208          */
209         if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
210                 lprintf(CTDL_INFO, "(re)initializing full text index\n");
211                 cdb_trunc(CDB_FULLTEXT);
212                 CitControl.MMfulltext = 0L;
213                 put_control();
214         }
215
216         /*
217          * Now go through each room and find messages to index.
218          */
219         ft_newhighest = CitControl.MMhighest;
220         ForEachRoom(ft_index_room, NULL);       /* load all msg pointers */
221
222         if (ft_num_msgs > 0) {
223                 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
224                 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
225                         if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
226                                 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
227                                         ((ft_num_msgs - i)*sizeof(long)));
228                                 --ft_num_msgs;
229                         }
230                 }
231
232                 /* Here it is ... do each message! */
233                 for (i=0; i<ft_num_msgs; ++i) {
234                         ft_index_message(ft_newmsgs[i], 1);
235                 }
236
237                 free(ft_newmsgs);
238                 ft_num_msgs = 0;
239                 ft_num_alloc = 0;
240                 ft_newmsgs = NULL;
241         }
242
243         /* Save our place so we don't have to do this again */
244         CitControl.MMfulltext = ft_newhighest;
245         CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
246         put_control();
247         last_index = time(NULL);
248
249         lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
250         return;
251 }
252
253
254 /*
255  * API call to perform searches.
256  * (This one does the "all of these words" search.)
257  * Caller is responsible for freeing the message list.
258  */
259 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
260         int num_tokens = 0;
261         int *tokens = NULL;
262         int i, j;
263         struct cdbdata *cdb_bucket;
264         int num_msgs;
265         long *msgs;
266         int num_all_msgs = 0;
267         long *all_msgs = NULL;
268         int num_ret_msgs = 0;
269         int num_ret_alloc = 0;
270         long *ret_msgs = NULL;
271
272         wordbreaker(search_string, &num_tokens, &tokens);
273         if (num_tokens > 0) {
274                 for (i=0; i<num_tokens; ++i) {
275
276                         /* search for tokens[i] */
277                         cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i], sizeof(int));
278                         if (cdb_bucket != NULL) {
279                                 num_msgs = cdb_bucket->len / sizeof(long);
280                                 msgs = (long *)cdb_bucket->ptr;
281
282                                 num_all_msgs += num_msgs;
283                                 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
284                                 memcpy(&all_msgs[num_all_msgs - num_msgs], msgs, num_msgs*sizeof(long) );
285
286                                 cdb_free(cdb_bucket);
287                         }
288
289                 }
290                 free(tokens);
291                 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
292
293                 /*
294                  * At this point, if a message appears num_tokens times in the
295                  * list, then it contains all of the search tokens.
296                  */
297                 if (num_all_msgs >= num_tokens) for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
298                         if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
299
300                                 ++num_ret_msgs;
301                                 if (num_ret_msgs > num_ret_alloc) {
302                                         num_ret_alloc += 64;
303                                         ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
304                                         ret_msgs[num_ret_msgs - 1] = all_msgs[j];
305                                 }
306
307                         }
308                 }
309
310                 free(all_msgs);
311         }
312         
313         *fts_num_msgs = num_ret_msgs;
314         *fts_msgs = ret_msgs;
315 }
316
317
318 /*
319  * Tentative form of a search command
320  */
321 void cmd_srch(char *argbuf) {
322         int num_msgs = 0;
323         long *msgs = NULL;
324         int i;
325         char search_string[256];
326
327         if (CtdlAccessCheck(ac_logged_in)) return;
328         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
329         ft_search(&num_msgs, &msgs, search_string);
330
331         cprintf("%d %d msgs match all search words:\n", LISTING_FOLLOWS, num_msgs);
332         if (num_msgs > 0) {
333                 for (i=0; i<num_msgs; ++i) {
334                         cprintf("%ld\n", msgs[i]);
335                 }
336         }
337         if (msgs != NULL) free(msgs);
338         cprintf("000\n");
339 }
340
341
342 /*****************************************************************************/
343
344 char *serv_fulltext_init(void)
345 {
346         CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
347         CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
348         return "$Id$";
349 }