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>
33 #include <libcitadel.h>
36 #include "citserver.h"
43 #include "serv_fulltext.h"
44 #include "ft_wordbreaker.h"
47 #include "ctdl_module.h"
51 long ft_newhighest = 0L;
52 long *ft_newmsgs = NULL;
57 int ftc_num_msgs[65536];
58 long *ftc_msgs[65536];
64 int longcmp(const void *rec1, const void *rec2) {
67 i1 = *(const long *)rec1;
68 i2 = *(const long *)rec2;
70 if (i1 > i2) return(1);
71 if (i1 < i2) return(-1);
76 * Flush our index cache out to disk.
78 void ft_flush_cache(void) {
80 time_t last_update = 0;
82 for (i=0; i<65536; ++i) {
83 if ((time(NULL) - last_update) >= 10) {
85 "Flushing index cache to disk (%d%% complete)\n",
88 last_update = time(NULL);
90 if (ftc_msgs[i] != NULL) {
91 cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i],
92 (ftc_num_msgs[i] * sizeof(long)));
98 lprintf(CTDL_INFO, "Flushed index cache to disk (100%% complete)\n");
103 * Index or de-index a message. (op == 1 to index, 0 to de-index)
105 void ft_index_message(long msgnum, int op) {
109 struct cdbdata *cdb_bucket;
113 lprintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
114 (op ? "adding" : "removing") , msgnum
117 /* Output the message as text before indexing it, so we don't end up
118 * indexing a bunch of encoded base64, etc.
120 CC->redirect_buffer = malloc(SIZ);
121 CC->redirect_len = 0;
122 CC->redirect_alloc = SIZ;
123 CtdlOutputMsg(msgnum, MT_CITADEL, HEADERS_ALL, 0, 1, NULL);
124 msgtext = CC->redirect_buffer;
125 CC->redirect_buffer = NULL;
126 CC->redirect_len = 0;
127 CC->redirect_alloc = 0;
128 lprintf(CTDL_DEBUG, "Wordbreaking message %ld...\n", msgnum);
129 wordbreaker(msgtext, &num_tokens, &tokens);
132 lprintf(CTDL_DEBUG, "Indexing message %ld [%d tokens]\n", msgnum, num_tokens);
133 if (num_tokens > 0) {
134 for (i=0; i<num_tokens; ++i) {
136 /* Add the message to the relevant token bucket */
138 /* search for tokens[i] */
141 if ( (tok >= 0) && (tok <= 65535) ) {
142 /* fetch the bucket, Liza */
143 if (ftc_msgs[tok] == NULL) {
144 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
145 if (cdb_bucket != NULL) {
146 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
147 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
148 cdb_bucket->ptr = NULL;
149 cdb_free(cdb_bucket);
152 ftc_num_msgs[tok] = 0;
153 ftc_msgs[tok] = malloc(sizeof(long));
158 if (op == 1) { /* add to index */
160 ftc_msgs[tok] = realloc(ftc_msgs[tok],
161 ftc_num_msgs[tok]*sizeof(long));
162 ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
165 if (op == 0) { /* remove from index */
166 if (ftc_num_msgs[tok] >= 1) {
167 for (j=0; j<ftc_num_msgs[tok]; ++j) {
168 if (ftc_msgs[tok][j] == msgnum) {
169 memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
178 lprintf(CTDL_ALERT, "Invalid token %d !!\n", tok);
189 * Add a message to the list of those to be indexed.
191 void ft_index_msg(long msgnum, void *userdata) {
193 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
195 if (ft_num_msgs > ft_num_alloc) {
196 ft_num_alloc += 1024;
197 ft_newmsgs = realloc(ft_newmsgs,
198 (ft_num_alloc * sizeof(long)));
200 ft_newmsgs[ft_num_msgs - 1] = msgnum;
206 * Scan a room for messages to index.
208 void ft_index_room(struct ctdlroom *qrbuf, void *data)
210 getroom(&CC->room, qrbuf->QRname);
211 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
216 * Begin the fulltext indexing process. (Called as an EVT_TIMER event)
218 void do_fulltext_indexing(void) {
220 static time_t last_index = 0L;
221 static time_t last_progress = 0L;
222 time_t run_time = 0L;
223 time_t end_time = 0L;
226 * Don't do this if the site doesn't have it enabled.
228 if (!config.c_enable_fulltext) {
233 * Make sure we don't run the indexer too frequently.
234 * FIXME move the setting into config
237 * The thread sleeps for 300 seconds so we don't need this here any more
239 if ( (time(NULL) - last_index) < 300L) {
245 * Check to see whether the fulltext index is up to date; if there
246 * are no messages to index, don't waste any more time trying.
248 if (CitControl.MMfulltext >= CitControl.MMhighest) {
249 return; /* nothing to do! */
252 run_time = time(NULL);
253 lprintf(CTDL_DEBUG, "do_fulltext_indexing() started (%ld)\n", run_time);
256 * If we've switched wordbreaker modules, burn the index and start
259 begin_critical_section(S_CONTROL);
260 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
261 lprintf(CTDL_DEBUG, "wb ver on disk = %d, code ver = %d\n",
262 CitControl.fulltext_wordbreaker, FT_WORDBREAKER_ID);
263 lprintf(CTDL_INFO, "(re)initializing full text index\n");
264 cdb_trunc(CDB_FULLTEXT);
265 CitControl.MMfulltext = 0L;
268 end_critical_section(S_CONTROL);
271 * Now go through each room and find messages to index.
273 ft_newhighest = CitControl.MMhighest;
274 ForEachRoom(ft_index_room, NULL); /* load all msg pointers */
276 if (ft_num_msgs > 0) {
277 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
278 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
279 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
280 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
281 ((ft_num_msgs - i - 1)*sizeof(long)));
287 /* Here it is ... do each message! */
288 for (i=0; i<ft_num_msgs; ++i) {
289 if (time(NULL) != last_progress) {
291 "Indexed %d of %d messages (%d%%)\n",
293 ((i*100) / ft_num_msgs)
295 last_progress = time(NULL);
297 ft_index_message(ft_newmsgs[i], 1);
299 /* Check to see if we need to quit early */
300 if (CtdlThreadCheckStop()) {
301 lprintf(CTDL_DEBUG, "Indexer quitting early\n");
302 ft_newhighest = ft_newmsgs[i];
306 /* Check to see if we have to maybe flush to disk */
307 if (i >= FT_MAX_CACHE) {
308 lprintf(CTDL_DEBUG, "Time to flush.\n");
309 ft_newhighest = ft_newmsgs[i];
320 end_time = time(NULL);
321 lprintf(CTDL_DEBUG, "do_fulltext_indexing() duration (%ld)\n", end_time - run_time);
323 /* Save our place so we don't have to do this again */
325 begin_critical_section(S_CONTROL);
326 CitControl.MMfulltext = ft_newhighest;
327 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
329 end_critical_section(S_CONTROL);
330 last_index = time(NULL);
332 lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
337 * Main loop for the indexer thread.
339 void *indexer_thread(void *arg) {
340 struct CitContext indexerCC;
342 lprintf(CTDL_DEBUG, "indexer_thread() initializing\n");
344 memset(&indexerCC, 0, sizeof(struct CitContext));
345 indexerCC.internal_pgm = 1;
346 indexerCC.cs_pid = 0;
347 pthread_setspecific(MyConKey, (void *)&indexerCC );
351 while (!CtdlThreadCheckStop()) {
352 do_fulltext_indexing();
353 CtdlThreadSleep(300);
356 lprintf(CTDL_DEBUG, "indexer_thread() exiting\n");
363 * API call to perform searches.
364 * (This one does the "all of these words" search.)
365 * Caller is responsible for freeing the message list.
367 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
371 struct cdbdata *cdb_bucket;
372 int num_all_msgs = 0;
373 long *all_msgs = NULL;
374 int num_ret_msgs = 0;
375 int num_ret_alloc = 0;
376 long *ret_msgs = NULL;
379 wordbreaker(search_string, &num_tokens, &tokens);
380 if (num_tokens > 0) {
381 for (i=0; i<num_tokens; ++i) {
383 /* search for tokens[i] */
386 /* fetch the bucket, Liza */
387 if (ftc_msgs[tok] == NULL) {
388 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
389 if (cdb_bucket != NULL) {
390 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
391 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
392 cdb_bucket->ptr = NULL;
393 cdb_free(cdb_bucket);
396 ftc_num_msgs[tok] = 0;
397 ftc_msgs[tok] = malloc(sizeof(long));
401 num_all_msgs += ftc_num_msgs[tok];
402 if (num_all_msgs > 0) {
403 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
404 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]],
405 ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
410 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
413 * At this point, if a message appears num_tokens times in the
414 * list, then it contains all of the search tokens.
416 if (num_all_msgs >= num_tokens)
417 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
418 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
421 if (num_ret_msgs > num_ret_alloc) {
423 ret_msgs = realloc(ret_msgs,
424 (num_ret_alloc*sizeof(long)) );
426 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
434 *fts_num_msgs = num_ret_msgs;
435 *fts_msgs = ret_msgs;
440 * This search command is for diagnostic purposes and may be removed or replaced.
442 void cmd_srch(char *argbuf) {
446 char search_string[256];
448 if (CtdlAccessCheck(ac_logged_in)) return;
450 if (!config.c_enable_fulltext) {
451 cprintf("%d Full text index is not enabled on this server.\n",
452 ERROR + CMD_NOT_SUPPORTED);
456 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
457 ft_search(&num_msgs, &msgs, search_string);
459 cprintf("%d %d msgs match all search words:\n",
460 LISTING_FOLLOWS, num_msgs);
462 for (i=0; i<num_msgs; ++i) {
463 cprintf("%ld\n", msgs[i]);
466 if (msgs != NULL) free(msgs);
471 * Zero out our index cache.
473 void initialize_ft_cache(void) {
474 memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
475 memset(ftc_msgs, 0, (65536 * sizeof(long *)));
479 void ft_delete_remove(char *room, long msgnum)
483 /* Remove from fulltext index */
484 if (config.c_enable_fulltext) {
485 ft_index_message(msgnum, 0);
489 /*****************************************************************************/
491 CTDL_MODULE_INIT(fulltext)
495 initialize_ft_cache();
496 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
497 CtdlRegisterDeleteHook(ft_delete_remove);
498 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
502 CtdlThreadCreate("indexer", CTDLTHREAD_BIGSTACK, indexer_thread, NULL);
504 /* return our Subversion id for the Log */