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;
56 * Index or de-index a message. (op == 1 to index, 0 to de-index)
58 void ft_index_message(long msgnum, int op) {
59 struct CtdlMessage *msg;
64 msg = CtdlFetchMessage(msgnum, 1);
65 if (msg == NULL) return;
67 if (msg->cm_fields['M'] != NULL) {
68 wordbreaker(msg->cm_fields['M'], &num_tokens, &tokens);
73 for (i=0; i<num_tokens; ++i) {
74 /* FIXME do something with this */
75 lprintf(CTDL_DEBUG, "msg %ld, token %d\n",
85 * Add a message to the list of those to be indexed.
87 void ft_index_msg(long msgnum, void *userdata) {
89 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
91 if (ft_num_msgs > ft_num_alloc) {
93 ft_newmsgs = realloc(ft_newmsgs,
94 (ft_num_alloc * sizeof(long)));
96 ft_newmsgs[ft_num_msgs - 1] = msgnum;
102 * Scan a room for messages to index.
104 void ft_index_room(struct ctdlroom *qrbuf, void *data)
106 getroom(&CC->room, qrbuf->QRname);
107 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
114 int longcmp(const void *rec1, const void *rec2) {
117 i1 = *(const long *)rec1;
118 i2 = *(const long *)rec2;
120 if (i1 > i2) return(1);
121 if (i1 < i2) return(-1);
127 * Begin the fulltext indexing process. (Called as an EVT_TIMER event)
129 void do_fulltext_indexing(void) {
132 lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
135 * Check to see whether the fulltext index is up to date; if there
136 * are no messages to index, don't waste any more time trying.
138 lprintf(CTDL_DEBUG, "CitControl.MMhighest = %ld\n",
139 CitControl.MMhighest);
140 lprintf(CTDL_DEBUG, "CitControl.MMfulltext = %ld\n",
141 CitControl.MMfulltext);
142 if (CitControl.MMfulltext >= CitControl.MMhighest) {
143 lprintf(CTDL_DEBUG, "Nothing to do!\n");
148 * Make sure we don't run the indexer too frequently.
149 * FIXME write this...
153 * If we've switched wordbreaker modules, burn the index and start
154 * over. FIXME write this...
158 * Now go through each room and find messages to index.
160 ft_newhighest = CitControl.MMhighest;
161 ForEachRoom(ft_index_room, NULL); /* load all msg pointers */
163 if (ft_num_msgs > 0) {
164 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
165 if (i>1) for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
166 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
167 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
168 ((ft_num_msgs - i)*sizeof(long)));
173 /* Here it is ... do each message! */
174 for (i=0; i<ft_num_msgs; ++i) {
175 ft_index_message(ft_newmsgs[i], 1);
184 lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
189 /*****************************************************************************/
191 char *serv_fulltext_init(void)
193 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);