4 * This module handles fulltext indexing of the message base.
17 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
39 #include "serv_extensions.h"
45 #include "serv_fulltext.h"
46 #include "ft_wordbreaker.h"
49 long ft_newhighest = 0L;
50 long *ft_newmsgs = NULL;
58 int longcmp(const void *rec1, const void *rec2) {
61 i1 = *(const long *)rec1;
62 i2 = *(const long *)rec2;
64 if (i1 > i2) return(1);
65 if (i1 < i2) return(-1);
73 * Index or de-index a message. (op == 1 to index, 0 to de-index)
75 void ft_index_message(long msgnum, int op) {
79 struct cdbdata *cdb_bucket;
84 lprintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
85 (op ? "adding" : "removing") , msgnum
88 /* Output the message as text before indexing it, so we don't end up
89 * indexing a bunch of encoded base64, etc.
91 CC->redirect_buffer = malloc(SIZ);
93 CC->redirect_alloc = SIZ;
94 CtdlOutputMsg(msgnum, MT_CITADEL, HEADERS_ALL, 0, 1);
95 msgtext = CC->redirect_buffer;
96 CC->redirect_buffer = NULL;
98 CC->redirect_alloc = 0;
99 lprintf(CTDL_DEBUG, "Wordbreaking message %ld...\n", msgnum);
100 wordbreaker(msgtext, &num_tokens, &tokens);
103 lprintf(CTDL_DEBUG, "Indexing message %ld...\n", msgnum);
104 if (num_tokens > 0) {
105 for (i=0; i<num_tokens; ++i) {
107 /* Add the message to the relevant token bucket */
109 /* FIXME lock the file */
110 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i], sizeof(int));
111 if (cdb_bucket == NULL) {
112 cdb_bucket = malloc(sizeof(struct cdbdata));
114 cdb_bucket->ptr = NULL;
118 num_msgs = cdb_bucket->len / sizeof(long);
121 if (op == 1) { /* add to index */
123 cdb_bucket->ptr = realloc(cdb_bucket->ptr, num_msgs*sizeof(long) );
124 msgs = (long *) cdb_bucket->ptr;
125 msgs[num_msgs - 1] = msgnum;
128 if (op == 0) { /* remove from index */
130 msgs = (long *) cdb_bucket->ptr;
131 for (j=0; j<num_msgs; ++j) {
132 if (msgs[j] == msgnum) {
133 memmove(&msgs[j], &msgs[j+1], ((num_msgs - j - 1)*sizeof(long)));
141 /* sort and purge dups
143 * This whole section is commented out because it's
144 * no longer needed -- since the tokenizer already
145 * does a merge/purge on the tokens it returns, and
146 * we're guaranteed to always be indexing a message
147 * with a number higher than any already in the index.
149 if ( (op == 1) && (num_msgs > 1) ) {
150 msgs = (long *) cdb_bucket->ptr;
151 qsort(msgs, num_msgs, sizeof(long), longcmp);
152 for (j=0; j<(num_msgs-1); ++j) {
153 if (msgs[j] == msgs[j+1]) {
154 memmove(&msgs[j], &msgs[j+1], ((num_msgs - j - 1)*sizeof(long)));
162 cdb_store(CDB_FULLTEXT, &tokens[i], sizeof(int),
163 msgs, (num_msgs*sizeof(long)) );
165 cdb_free(cdb_bucket);
167 /* FIXME unlock the file */
177 * Add a message to the list of those to be indexed.
179 void ft_index_msg(long msgnum, void *userdata) {
181 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
183 if (ft_num_msgs > ft_num_alloc) {
184 ft_num_alloc += 1024;
185 ft_newmsgs = realloc(ft_newmsgs,
186 (ft_num_alloc * sizeof(long)));
188 ft_newmsgs[ft_num_msgs - 1] = msgnum;
194 * Scan a room for messages to index.
196 void ft_index_room(struct ctdlroom *qrbuf, void *data)
198 getroom(&CC->room, qrbuf->QRname);
199 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
204 * Begin the fulltext indexing process. (Called as an EVT_TIMER event)
206 void do_fulltext_indexing(void) {
208 static time_t last_index = 0L;
209 static time_t last_progress = 0L;
212 * Don't do this if the site doesn't have it enabled.
214 if (!config.c_enable_fulltext) {
219 * Make sure we don't run the indexer too frequently.
220 * FIXME move the setting into config
222 if ( (time(NULL) - last_index) < 300L) {
227 * Check to see whether the fulltext index is up to date; if there
228 * are no messages to index, don't waste any more time trying.
230 if (CitControl.MMfulltext >= CitControl.MMhighest) {
231 return; /* nothing to do! */
234 lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
237 * If we've switched wordbreaker modules, burn the index and start
238 * over. FIXME write this...
240 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
241 lprintf(CTDL_INFO, "(re)initializing full text index\n");
242 cdb_trunc(CDB_FULLTEXT);
243 CitControl.MMfulltext = 0L;
248 * Now go through each room and find messages to index.
250 ft_newhighest = CitControl.MMhighest;
251 ForEachRoom(ft_index_room, NULL); /* load all msg pointers */
253 if (ft_num_msgs > 0) {
254 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
255 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
256 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
257 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
258 ((ft_num_msgs - i - 1)*sizeof(long)));
264 /* Here it is ... do each message! */
265 for (i=0; i<ft_num_msgs; ++i) {
266 if ((time(NULL) - last_progress) > 10) {
268 "Indexed %d of %d messages (%d%%)\n",
270 ((i*100) / ft_num_msgs)
272 last_progress = time(NULL);
274 ft_index_message(ft_newmsgs[i], 1);
283 /* Save our place so we don't have to do this again */
284 CitControl.MMfulltext = ft_newhighest;
285 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
287 last_index = time(NULL);
289 lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
295 * API call to perform searches.
296 * (This one does the "all of these words" search.)
297 * Caller is responsible for freeing the message list.
299 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
303 struct cdbdata *cdb_bucket;
306 int num_all_msgs = 0;
307 long *all_msgs = NULL;
308 int num_ret_msgs = 0;
309 int num_ret_alloc = 0;
310 long *ret_msgs = NULL;
312 wordbreaker(search_string, &num_tokens, &tokens);
313 if (num_tokens > 0) {
314 for (i=0; i<num_tokens; ++i) {
316 /* search for tokens[i] */
317 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i],
319 if (cdb_bucket != NULL) {
320 num_msgs = cdb_bucket->len / sizeof(long);
321 msgs = (long *)cdb_bucket->ptr;
323 num_all_msgs += num_msgs;
324 if (num_all_msgs > 0) {
325 all_msgs = realloc(all_msgs,
326 num_all_msgs*sizeof(long) );
327 memcpy(&all_msgs[num_all_msgs-num_msgs],
328 msgs, num_msgs*sizeof(long) );
331 cdb_free(cdb_bucket);
336 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
339 * At this point, if a message appears num_tokens times in the
340 * list, then it contains all of the search tokens.
342 if (num_all_msgs >= num_tokens)
343 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
344 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
347 if (num_ret_msgs > num_ret_alloc) {
349 ret_msgs = realloc(ret_msgs,
350 (num_ret_alloc*sizeof(long)) );
352 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
360 *fts_num_msgs = num_ret_msgs;
361 *fts_msgs = ret_msgs;
366 * Tentative form of a search command
368 void cmd_srch(char *argbuf) {
372 char search_string[256];
374 if (CtdlAccessCheck(ac_logged_in)) return;
376 if (!config.c_enable_fulltext) {
377 cprintf("%d Full text index is not enabled on this server.\n",
378 ERROR + CMD_NOT_SUPPORTED);
381 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
382 ft_search(&num_msgs, &msgs, search_string);
384 cprintf("%d %d msgs match all search words:\n",
385 LISTING_FOLLOWS, num_msgs);
387 for (i=0; i<num_msgs; ++i) {
388 cprintf("%ld\n", msgs[i]);
391 if (msgs != NULL) free(msgs);
396 /*****************************************************************************/
398 char *serv_fulltext_init(void)
400 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
401 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");