4 * This module handles fulltext indexing of the message base.
7 * Copyright (c) 2005-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
59 #include "serv_fulltext.h"
60 #include "ft_wordbreaker.h"
63 #include "ctdl_module.h"
67 long ft_newhighest = 0L;
68 long *ft_newmsgs = NULL;
73 int ftc_num_msgs[65536];
74 long *ftc_msgs[65536];
80 int longcmp(const void *rec1, const void *rec2) {
83 i1 = *(const long *)rec1;
84 i2 = *(const long *)rec2;
86 if (i1 > i2) return(1);
87 if (i1 < i2) return(-1);
92 * Flush our index cache out to disk.
94 void ft_flush_cache(void) {
96 time_t last_update = 0;
98 for (i=0; i<65536; ++i) {
99 if ((time(NULL) - last_update) >= 10) {
100 CtdlLogPrintf(CTDL_INFO,
101 "Flushing index cache to disk (%d%% complete)\n",
104 last_update = time(NULL);
106 if (ftc_msgs[i] != NULL) {
107 cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i],
108 (ftc_num_msgs[i] * sizeof(long)));
114 CtdlLogPrintf(CTDL_INFO, "Flushed index cache to disk (100%% complete)\n");
119 * Index or de-index a message. (op == 1 to index, 0 to de-index)
121 void ft_index_message(long msgnum, int op) {
125 struct cdbdata *cdb_bucket;
129 CtdlLogPrintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
130 (op ? "adding" : "removing") , msgnum
133 /* Output the message as text before indexing it, so we don't end up
134 * indexing a bunch of encoded base64, etc.
136 CC->redirect_buffer = malloc(SIZ);
137 CC->redirect_len = 0;
138 CC->redirect_alloc = SIZ;
139 CtdlOutputMsg(msgnum, MT_CITADEL, HEADERS_ALL, 0, 1, NULL, 0);
140 msgtext = CC->redirect_buffer;
141 CC->redirect_buffer = NULL;
142 CC->redirect_len = 0;
143 CC->redirect_alloc = 0;
144 CtdlLogPrintf(CTDL_DEBUG, "Wordbreaking message %ld...\n", msgnum);
145 wordbreaker(msgtext, &num_tokens, &tokens);
148 CtdlLogPrintf(CTDL_DEBUG, "Indexing message %ld [%d tokens]\n", msgnum, num_tokens);
149 if (num_tokens > 0) {
150 for (i=0; i<num_tokens; ++i) {
152 /* Add the message to the relevant token bucket */
154 /* search for tokens[i] */
157 if ( (tok >= 0) && (tok <= 65535) ) {
158 /* fetch the bucket, Liza */
159 if (ftc_msgs[tok] == NULL) {
160 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
161 if (cdb_bucket != NULL) {
162 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
163 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
164 cdb_bucket->ptr = NULL;
165 cdb_free(cdb_bucket);
168 ftc_num_msgs[tok] = 0;
169 ftc_msgs[tok] = malloc(sizeof(long));
174 if (op == 1) { /* add to index */
176 ftc_msgs[tok] = realloc(ftc_msgs[tok],
177 ftc_num_msgs[tok]*sizeof(long));
178 ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
181 if (op == 0) { /* remove from index */
182 if (ftc_num_msgs[tok] >= 1) {
183 for (j=0; j<ftc_num_msgs[tok]; ++j) {
184 if (ftc_msgs[tok][j] == msgnum) {
185 memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
194 CtdlLogPrintf(CTDL_ALERT, "Invalid token %d !!\n", tok);
205 * Add a message to the list of those to be indexed.
207 void ft_index_msg(long msgnum, void *userdata) {
209 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
211 if (ft_num_msgs > ft_num_alloc) {
212 ft_num_alloc += 1024;
213 ft_newmsgs = realloc(ft_newmsgs,
214 (ft_num_alloc * sizeof(long)));
216 ft_newmsgs[ft_num_msgs - 1] = msgnum;
222 * Scan a room for messages to index.
224 void ft_index_room(struct ctdlroom *qrbuf, void *data)
226 if (CtdlThreadCheckStop())
229 getroom(&CC->room, qrbuf->QRname);
230 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
235 * Begin the fulltext indexing process. (Called as an EVT_TIMER event)
237 void do_fulltext_indexing(void) {
239 static time_t last_index = 0L;
240 static time_t last_progress = 0L;
241 time_t run_time = 0L;
242 time_t end_time = 0L;
245 * Don't do this if the site doesn't have it enabled.
247 if (!config.c_enable_fulltext) {
252 * Make sure we don't run the indexer too frequently.
253 * FIXME move the setting into config
256 * The thread sleeps for 300 seconds so we don't need this here any more
258 if ( (time(NULL) - last_index) < 300L) {
264 * Check to see whether the fulltext index is up to date; if there
265 * are no messages to index, don't waste any more time trying.
267 if ((CitControl.MMfulltext >= CitControl.MMhighest) && (CitControl.fulltext_wordbreaker == FT_WORDBREAKER_ID)) {
268 return; /* nothing to do! */
271 run_time = time(NULL);
272 CtdlLogPrintf(CTDL_DEBUG, "do_fulltext_indexing() started (%ld)\n", run_time);
275 * If we've switched wordbreaker modules, burn the index and start
278 begin_critical_section(S_CONTROL);
279 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
280 CtdlLogPrintf(CTDL_DEBUG, "wb ver on disk = %d, code ver = %d\n",
281 CitControl.fulltext_wordbreaker, FT_WORDBREAKER_ID);
282 CtdlLogPrintf(CTDL_INFO, "(re)initializing full text index\n");
283 cdb_trunc(CDB_FULLTEXT);
284 CitControl.MMfulltext = 0L;
287 end_critical_section(S_CONTROL);
290 * Now go through each room and find messages to index.
292 ft_newhighest = CitControl.MMhighest;
293 ForEachRoom(ft_index_room, NULL); /* load all msg pointers */
295 if (ft_num_msgs > 0) {
296 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
297 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
298 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
299 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
300 ((ft_num_msgs - i - 1)*sizeof(long)));
306 /* Here it is ... do each message! */
307 for (i=0; i<ft_num_msgs; ++i) {
308 if (time(NULL) != last_progress) {
309 CtdlLogPrintf(CTDL_DEBUG,
310 "Indexed %d of %d messages (%d%%)\n",
312 ((i*100) / ft_num_msgs)
314 last_progress = time(NULL);
316 ft_index_message(ft_newmsgs[i], 1);
318 /* Check to see if we need to quit early */
319 if (CtdlThreadCheckStop()) {
320 CtdlLogPrintf(CTDL_DEBUG, "Indexer quitting early\n");
321 ft_newhighest = ft_newmsgs[i];
325 /* Check to see if we have to maybe flush to disk */
326 if (i >= FT_MAX_CACHE) {
327 CtdlLogPrintf(CTDL_DEBUG, "Time to flush.\n");
328 ft_newhighest = ft_newmsgs[i];
339 end_time = time(NULL);
341 if (CtdlThreadCheckStop())
344 CtdlLogPrintf(CTDL_DEBUG, "do_fulltext_indexing() duration (%ld)\n", end_time - run_time);
346 /* Save our place so we don't have to do this again */
348 begin_critical_section(S_CONTROL);
349 CitControl.MMfulltext = ft_newhighest;
350 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
352 end_critical_section(S_CONTROL);
353 last_index = time(NULL);
355 CtdlLogPrintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
360 * Main loop for the indexer thread.
362 void *indexer_thread(void *arg) {
363 struct CitContext indexerCC;
365 CtdlLogPrintf(CTDL_DEBUG, "indexer_thread() initializing\n");
367 CtdlFillSystemContext(&indexerCC, "indexer");
368 citthread_setspecific(MyConKey, (void *)&indexerCC );
370 while (!CtdlThreadCheckStop()) {
371 do_fulltext_indexing();
372 CtdlThreadSleep(300);
375 CtdlLogPrintf(CTDL_DEBUG, "indexer_thread() exiting\n");
382 * API call to perform searches.
383 * (This one does the "all of these words" search.)
384 * Caller is responsible for freeing the message list.
386 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
390 struct cdbdata *cdb_bucket;
391 int num_all_msgs = 0;
392 long *all_msgs = NULL;
393 int num_ret_msgs = 0;
394 int num_ret_alloc = 0;
395 long *ret_msgs = NULL;
398 wordbreaker(search_string, &num_tokens, &tokens);
399 if (num_tokens > 0) {
400 for (i=0; i<num_tokens; ++i) {
402 /* search for tokens[i] */
405 /* fetch the bucket, Liza */
406 if (ftc_msgs[tok] == NULL) {
407 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
408 if (cdb_bucket != NULL) {
409 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
410 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
411 cdb_bucket->ptr = NULL;
412 cdb_free(cdb_bucket);
415 ftc_num_msgs[tok] = 0;
416 ftc_msgs[tok] = malloc(sizeof(long));
420 num_all_msgs += ftc_num_msgs[tok];
421 if (num_all_msgs > 0) {
422 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
423 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]],
424 ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
429 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
432 * At this point, if a message appears num_tokens times in the
433 * list, then it contains all of the search tokens.
435 if (num_all_msgs >= num_tokens)
436 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
437 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
440 if (num_ret_msgs > num_ret_alloc) {
442 ret_msgs = realloc(ret_msgs,
443 (num_ret_alloc*sizeof(long)) );
445 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
453 *fts_num_msgs = num_ret_msgs;
454 *fts_msgs = ret_msgs;
459 * This search command is for diagnostic purposes and may be removed or replaced.
461 void cmd_srch(char *argbuf) {
465 char search_string[256];
467 if (CtdlAccessCheck(ac_logged_in)) return;
469 if (!config.c_enable_fulltext) {
470 cprintf("%d Full text index is not enabled on this server.\n",
471 ERROR + CMD_NOT_SUPPORTED);
475 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
476 ft_search(&num_msgs, &msgs, search_string);
478 cprintf("%d %d msgs match all search words:\n",
479 LISTING_FOLLOWS, num_msgs);
481 for (i=0; i<num_msgs; ++i) {
482 cprintf("%ld\n", msgs[i]);
485 if (msgs != NULL) free(msgs);
490 * Zero out our index cache.
492 void initialize_ft_cache(void) {
493 memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
494 memset(ftc_msgs, 0, (65536 * sizeof(long *)));
498 void ft_delete_remove(char *room, long msgnum)
502 /* Remove from fulltext index */
503 if (config.c_enable_fulltext) {
504 ft_index_message(msgnum, 0);
508 /*****************************************************************************/
510 CTDL_MODULE_INIT(fulltext)
514 initialize_ft_cache();
515 initialize_noise_words();
516 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
517 CtdlRegisterDeleteHook(ft_delete_remove);
518 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
519 CtdlRegisterCleanupHook(noise_word_cleanup);
523 CtdlThreadCreate("Indexer", CTDLTHREAD_BIGSTACK, indexer_thread, NULL);
525 /* return our Subversion id for the Log */