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) {
76 struct CtdlMessage *msg;
80 struct cdbdata *cdb_bucket;
84 lprintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
85 (op ? "adding" : "removing") , msgnum
88 msg = CtdlFetchMessage(msgnum, 1);
89 if (msg == NULL) return;
91 if (msg->cm_fields['M'] != NULL) {
92 wordbreaker(msg->cm_fields['M'], &num_tokens, &tokens);
97 for (i=0; i<num_tokens; ++i) {
99 /* Add the message to the relevant token bucket */
101 /* FIXME lock the file */
102 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i], sizeof(int));
103 if (cdb_bucket == NULL) {
104 cdb_bucket = malloc(sizeof(struct cdbdata));
106 cdb_bucket->ptr = NULL;
110 num_msgs = cdb_bucket->len / sizeof(long);
113 if (op == 1) { /* add to index */
115 cdb_bucket->ptr = realloc(cdb_bucket->ptr, num_msgs*sizeof(long) );
116 msgs = (long *) cdb_bucket->ptr;
117 msgs[num_msgs - 1] = msgnum;
120 if (op == 0) { /* remove from index */
122 msgs = (long *) cdb_bucket->ptr;
123 for (j=0; j<num_msgs; ++j) {
124 if (msgs[j] == msgnum) {
125 memmove(&msgs[j], &msgs[j+1],
126 ((num_msgs - j - 1)*sizeof(long)));
133 /* sort and purge dups */
134 if ( (op == 1) && (num_msgs > 1) ) {
135 msgs = (long *) cdb_bucket->ptr;
136 qsort(msgs, num_msgs, sizeof(long), longcmp);
137 for (j=0; j<(num_msgs-1); ++j) {
138 if (msgs[j] == msgs[j+1]) {
139 memmove(&msgs[j], &msgs[j+1],
140 ((num_msgs - j - 1)*sizeof(long)));
146 cdb_store(CDB_FULLTEXT, &tokens[i], sizeof(int),
147 msgs, (num_msgs*sizeof(long)) );
149 cdb_free(cdb_bucket);
151 /* FIXME unlock the file */
161 * Add a message to the list of those to be indexed.
163 void ft_index_msg(long msgnum, void *userdata) {
165 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
167 if (ft_num_msgs > ft_num_alloc) {
168 ft_num_alloc += 1024;
169 ft_newmsgs = realloc(ft_newmsgs,
170 (ft_num_alloc * sizeof(long)));
172 ft_newmsgs[ft_num_msgs - 1] = msgnum;
178 * Scan a room for messages to index.
180 void ft_index_room(struct ctdlroom *qrbuf, void *data)
182 getroom(&CC->room, qrbuf->QRname);
183 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
188 * Begin the fulltext indexing process. (Called as an EVT_TIMER event)
190 void do_fulltext_indexing(void) {
192 static time_t last_index = 0L;
195 * Make sure we don't run the indexer too frequently.
196 * FIXME move the setting into config
198 if ( (time(NULL) - last_index) < 300L) {
203 * Check to see whether the fulltext index is up to date; if there
204 * are no messages to index, don't waste any more time trying.
206 lprintf(CTDL_DEBUG, "CitControl.MMhighest = %ld\n", CitControl.MMhighest);
207 lprintf(CTDL_DEBUG, "CitControl.MMfulltext = %ld\n", CitControl.MMfulltext);
208 if (CitControl.MMfulltext >= CitControl.MMhighest) {
213 lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
216 * If we've switched wordbreaker modules, burn the index and start
217 * over. FIXME write this...
219 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
220 lprintf(CTDL_INFO, "(re)initializing full text index\n");
221 cdb_trunc(CDB_FULLTEXT);
222 CitControl.MMfulltext = 0L;
227 * Now go through each room and find messages to index.
229 ft_newhighest = CitControl.MMhighest;
230 ForEachRoom(ft_index_room, NULL); /* load all msg pointers */
232 if (ft_num_msgs > 0) {
233 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
234 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
235 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
236 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
237 ((ft_num_msgs - i - 1)*sizeof(long)));
242 /* Here it is ... do each message! */
243 for (i=0; i<ft_num_msgs; ++i) {
244 ft_index_message(ft_newmsgs[i], 1);
253 /* Save our place so we don't have to do this again */
254 CitControl.MMfulltext = ft_newhighest;
255 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
257 last_index = time(NULL);
259 lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
265 * API call to perform searches.
266 * (This one does the "all of these words" search.)
267 * Caller is responsible for freeing the message list.
269 void ft_search(int *fts_num_msgs, long **fts_msgs, char *search_string) {
273 struct cdbdata *cdb_bucket;
276 int num_all_msgs = 0;
277 long *all_msgs = NULL;
278 int num_ret_msgs = 0;
279 int num_ret_alloc = 0;
280 long *ret_msgs = NULL;
282 wordbreaker(search_string, &num_tokens, &tokens);
283 if (num_tokens > 0) {
284 for (i=0; i<num_tokens; ++i) {
286 /* search for tokens[i] */
287 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tokens[i], sizeof(int));
288 if (cdb_bucket != NULL) {
289 num_msgs = cdb_bucket->len / sizeof(long);
290 msgs = (long *)cdb_bucket->ptr;
292 num_all_msgs += num_msgs;
293 if (num_all_msgs > 0) {
294 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
295 memcpy(&all_msgs[num_all_msgs - num_msgs], msgs,
296 num_msgs*sizeof(long) );
299 cdb_free(cdb_bucket);
304 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
307 * At this point, if a message appears num_tokens times in the
308 * list, then it contains all of the search tokens.
310 if (num_all_msgs >= num_tokens) for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
311 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
314 if (num_ret_msgs > num_ret_alloc) {
316 ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
318 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
326 *fts_num_msgs = num_ret_msgs;
327 *fts_msgs = ret_msgs;
332 * Tentative form of a search command
334 void cmd_srch(char *argbuf) {
338 char search_string[256];
340 if (CtdlAccessCheck(ac_logged_in)) return;
341 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
342 ft_search(&num_msgs, &msgs, search_string);
344 cprintf("%d %d msgs match all search words:\n",
345 LISTING_FOLLOWS, num_msgs);
347 for (i=0; i<num_msgs; ++i) {
348 cprintf("%ld\n", msgs[i]);
351 if (msgs != NULL) free(msgs);
356 /*****************************************************************************/
358 char *serv_fulltext_init(void)
360 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
361 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");