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;
55 int ftc_num_msgs[65536];
56 long *ftc_msgs[65536];
62 int longcmp(const void *rec1, const void *rec2) {
65 i1 = *(const long *)rec1;
66 i2 = *(const long *)rec2;
68 if (i1 > i2) return(1);
69 if (i1 < i2) return(-1);
74 * Flush our index cache out to disk.
76 void ft_flush_cache(void) {
78 time_t last_update = 0;
80 for (i=0; i<65536; ++i) {
81 if ((time(NULL) - last_update) >= 10) {
83 "Flushing index cache to disk (%d%% complete)\n",
86 last_update = time(NULL);
88 if (ftc_msgs[i] != NULL) {
89 cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i],
90 (ftc_num_msgs[i] * sizeof(long)));
96 lprintf(CTDL_INFO, "Flushed index cache to disk (100%% complete)\n");
101 * Index or de-index a message. (op == 1 to index, 0 to de-index)
103 void ft_index_message(long msgnum, int op) {
107 struct cdbdata *cdb_bucket;
111 lprintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
112 (op ? "adding" : "removing") , msgnum
115 /* Output the message as text before indexing it, so we don't end up
116 * indexing a bunch of encoded base64, etc.
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);
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) {
134 /* Add the message to the relevant token bucket */
136 /* search for tokens[i] */
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);
150 ftc_num_msgs[tok] = 0;
151 ftc_msgs[tok] = malloc(sizeof(long));
156 if (op == 1) { /* add to index */
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;
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)));
176 lprintf(CTDL_ALERT, "Invalid token %d !!\n", tok);
187 * Add a message to the list of those to be indexed.
189 void ft_index_msg(long msgnum, void *userdata) {
191 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
193 if (ft_num_msgs > ft_num_alloc) {
194 ft_num_alloc += 1024;
195 ft_newmsgs = realloc(ft_newmsgs,
196 (ft_num_alloc * sizeof(long)));
198 ft_newmsgs[ft_num_msgs - 1] = msgnum;
204 * Scan a room for messages to index.
206 void ft_index_room(struct ctdlroom *qrbuf, void *data)
208 getroom(&CC->room, qrbuf->QRname);
209 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
214 * Begin the fulltext indexing process. (Called as an EVT_TIMER event)
216 void do_fulltext_indexing(void) {
218 static time_t last_index = 0L;
219 static time_t last_progress = 0L;
222 * Don't do this if the site doesn't have it enabled.
224 if (!config.c_enable_fulltext) {
229 * Make sure we don't run the indexer too frequently.
230 * FIXME move the setting into config
232 if ( (time(NULL) - last_index) < 300L) {
237 * Check to see whether the fulltext index is up to date; if there
238 * are no messages to index, don't waste any more time trying.
240 if (CitControl.MMfulltext >= CitControl.MMhighest) {
241 return; /* nothing to do! */
244 lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
247 * If we've switched wordbreaker modules, burn the index and start
250 begin_critical_section(S_CONTROL);
251 lprintf(CTDL_DEBUG, "wb ver on disk = %d, code ver = %d\n",
252 CitControl.fulltext_wordbreaker, FT_WORDBREAKER_ID);
253 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
254 lprintf(CTDL_INFO, "(re)initializing full text index\n");
255 cdb_trunc(CDB_FULLTEXT);
256 CitControl.MMfulltext = 0L;
259 end_critical_section(S_CONTROL);
262 * Now go through each room and find messages to index.
264 ft_newhighest = CitControl.MMhighest;
265 ForEachRoom(ft_index_room, NULL); /* load all msg pointers */
267 if (ft_num_msgs > 0) {
268 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
269 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
270 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
271 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
272 ((ft_num_msgs - i - 1)*sizeof(long)));
278 /* Here it is ... do each message! */
279 for (i=0; i<ft_num_msgs; ++i) {
280 if ((time(NULL) - last_progress) > 10) {
282 "Indexed %d of %d messages (%d%%)\n",
284 ((i*100) / ft_num_msgs)
286 last_progress = time(NULL);
288 ft_index_message(ft_newmsgs[i], 1);
290 /* Check to see if we need to quit early */
292 lprintf(CTDL_DEBUG, "Indexer quitting early\n");
293 ft_newhighest = ft_newmsgs[i];
297 /* Check to see if we have to maybe flush to disk */
298 if (i >= FT_MAX_CACHE) {
299 lprintf(CTDL_DEBUG, "Time to flush.\n");
300 ft_newhighest = ft_newmsgs[i];
312 /* Save our place so we don't have to do this again */
314 begin_critical_section(S_CONTROL);
315 CitControl.MMfulltext = ft_newhighest;
316 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
318 end_critical_section(S_CONTROL);
319 last_index = time(NULL);
321 lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
326 * Main loop for the indexer thread.
328 void *indexer_thread(void *arg) {
329 struct CitContext indexerCC;
331 lprintf(CTDL_DEBUG, "indexer_thread() initializing\n");
333 memset(&indexerCC, 0, sizeof(struct CitContext));
334 indexerCC.internal_pgm = 1;
335 indexerCC.cs_pid = 0;
336 pthread_setspecific(MyConKey, (void *)&indexerCC );
340 while (!time_to_die) {
341 do_fulltext_indexing();
345 lprintf(CTDL_DEBUG, "indexer_thread() exiting\n");
352 * API call to perform searches.
353 * (This one does the "all of these words" search.)
354 * Caller is responsible for freeing the message list.
356 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
360 struct cdbdata *cdb_bucket;
361 int num_all_msgs = 0;
362 long *all_msgs = NULL;
363 int num_ret_msgs = 0;
364 int num_ret_alloc = 0;
365 long *ret_msgs = NULL;
368 wordbreaker(search_string, &num_tokens, &tokens);
369 if (num_tokens > 0) {
370 for (i=0; i<num_tokens; ++i) {
372 /* search for tokens[i] */
375 /* fetch the bucket, Liza */
376 if (ftc_msgs[tok] == NULL) {
377 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
378 if (cdb_bucket != NULL) {
379 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
380 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
381 cdb_bucket->ptr = NULL;
382 cdb_free(cdb_bucket);
385 ftc_num_msgs[tok] = 0;
386 ftc_msgs[tok] = malloc(sizeof(long));
390 num_all_msgs += ftc_num_msgs[tok];
391 if (num_all_msgs > 0) {
392 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
393 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]],
394 ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
399 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
402 * At this point, if a message appears num_tokens times in the
403 * list, then it contains all of the search tokens.
405 if (num_all_msgs >= num_tokens)
406 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
407 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
410 if (num_ret_msgs > num_ret_alloc) {
412 ret_msgs = realloc(ret_msgs,
413 (num_ret_alloc*sizeof(long)) );
415 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
423 *fts_num_msgs = num_ret_msgs;
424 *fts_msgs = ret_msgs;
429 * This search command is for diagnostic purposes and may be removed or replaced.
431 void cmd_srch(char *argbuf) {
435 char search_string[256];
437 if (CtdlAccessCheck(ac_logged_in)) return;
439 if (!config.c_enable_fulltext) {
440 cprintf("%d Full text index is not enabled on this server.\n",
441 ERROR + CMD_NOT_SUPPORTED);
445 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
446 ft_search(&num_msgs, &msgs, search_string);
448 cprintf("%d %d msgs match all search words:\n",
449 LISTING_FOLLOWS, num_msgs);
451 for (i=0; i<num_msgs; ++i) {
452 cprintf("%ld\n", msgs[i]);
455 if (msgs != NULL) free(msgs);
460 * Zero out our index cache.
462 void initialize_ft_cache(void) {
463 memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
464 memset(ftc_msgs, 0, (65536 * sizeof(long *)));
468 /*****************************************************************************/
470 char *serv_fulltext_init(void)
472 initialize_ft_cache();
473 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");