]> 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  * Index or de-index a message.  (op == 1 to index, 0 to de-index)
57  */
58 void ft_index_message(long msgnum, int op) {
59         struct CtdlMessage *msg;
60         int num_tokens = 0;
61         int *tokens = NULL;
62         int i;
63         struct cdbdata *cdb_bucket;
64         int num_msgs;
65
66         msg = CtdlFetchMessage(msgnum, 1);
67         if (msg == NULL) return;
68
69         if (msg->cm_fields['M'] != NULL) {
70                 wordbreaker(msg->cm_fields['M'], &num_tokens, &tokens);
71         }
72         CtdlFreeMessage(msg);
73
74         if (num_tokens > 0) {
75                 for (i=0; i<num_tokens; ++i) {
76
77                         /* Add the message to the relevant token bucket */
78                         lprintf(CTDL_DEBUG, "msg %ld, token %d\n", msgnum, tokens[i]);
79
80                         /* FIXME lock the file */
81                         cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i], sizeof(long));
82                         if (cdb_bucket == NULL) {
83                                 cdb_bucket = malloc(sizeof(struct cdbdata));
84                                 cdb_bucket->len = 0;
85                                 cdb_bucket->ptr = malloc(sizeof(long));
86                         }
87                         num_msgs = cdb_bucket->len / sizeof(long);
88
89                         /* FIXME finish this */
90
91                         cdb_free(cdb_bucket);
92
93                         /* FIXME unlock the file */
94                 }
95
96                 free(tokens);
97         }
98 }
99
100
101
102 /*
103  * Add a message to the list of those to be indexed.
104  */
105 void ft_index_msg(long msgnum, void *userdata) {
106
107         if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
108                 ++ft_num_msgs;
109                 if (ft_num_msgs > ft_num_alloc) {
110                         ft_num_alloc += 1024;
111                         ft_newmsgs = realloc(ft_newmsgs,
112                                 (ft_num_alloc * sizeof(long)));
113                 }
114                 ft_newmsgs[ft_num_msgs - 1] = msgnum;
115         }
116
117 }
118
119 /*
120  * Scan a room for messages to index.
121  */
122 void ft_index_room(struct ctdlroom *qrbuf, void *data)
123 {
124         getroom(&CC->room, qrbuf->QRname);
125         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
126 }
127
128
129 /*
130  * Compare function
131  */
132 int longcmp(const void *rec1, const void *rec2) {
133         long i1, i2;
134
135         i1 = *(const long *)rec1;
136         i2 = *(const long *)rec2;
137
138         if (i1 > i2) return(1);
139         if (i1 < i2) return(-1);
140         return(0);
141 }
142
143
144 /*
145  * Begin the fulltext indexing process.  (Called as an EVT_TIMER event)
146  */
147 void do_fulltext_indexing(void) {
148         int i;
149         static time_t last_index = 0L;
150
151         /*
152          * Make sure we don't run the indexer too frequently.
153          * FIXME move the setting into config
154          */
155         if ( (time(NULL) - last_index) < 300L) {
156                 return;
157         }
158
159         /*
160          * Check to see whether the fulltext index is up to date; if there
161          * are no messages to index, don't waste any more time trying.
162          */
163         lprintf(CTDL_DEBUG, "CitControl.MMhighest  = %ld\n",
164                 CitControl.MMhighest);
165         lprintf(CTDL_DEBUG, "CitControl.MMfulltext = %ld\n",
166                 CitControl.MMfulltext);
167         if (CitControl.MMfulltext >= CitControl.MMhighest) {
168                 /* nothing to do! */
169                 return;
170         }
171
172         lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
173         
174         /*
175          * If we've switched wordbreaker modules, burn the index and start
176          * over.  FIXME write this...
177          */
178         if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
179                 lprintf(CTDL_INFO, "(re)initializing full text index\n");
180                 cdb_trunc(CDB_FULLTEXT);
181                 CitControl.MMfulltext = 0L;
182                 put_control();
183         }
184
185         /*
186          * Now go through each room and find messages to index.
187          */
188         ft_newhighest = CitControl.MMhighest;
189         ForEachRoom(ft_index_room, NULL);       /* load all msg pointers */
190
191         if (ft_num_msgs > 0) {
192                 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
193                 if (i>1) for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
194                         if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
195                                 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
196                                         ((ft_num_msgs - i)*sizeof(long)));
197                                 --ft_num_msgs;
198                         }
199                 }
200
201                 /* Here it is ... do each message! */
202                 for (i=0; i<ft_num_msgs; ++i) {
203                         ft_index_message(ft_newmsgs[i], 1);
204                 }
205
206                 free(ft_newmsgs);
207                 ft_num_msgs = 0;
208                 ft_num_alloc = 0;
209                 ft_newmsgs = NULL;
210         }
211
212         lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
213         return;
214 }
215
216
217 /*****************************************************************************/
218
219 char *serv_fulltext_init(void)
220 {
221         CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
222         return "$Id$";
223 }