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