]> code.citadel.org Git - citadel.git/blob - citadel/serv_fulltext.c
* Began writing all the "glue" for the indexer.
[citadel.git] / citadel / serv_fulltext.c
1 /*
2  * $Id$
3  *
4  * This module handles fulltext indexing of the message base.
5  *
6  */
7
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "serv_extensions.h"
40 #include "database.h"
41 #include "msgbase.h"
42 #include "control.h"
43 #include "room_ops.h"
44 #include "tools.h"
45 #include "serv_fulltext.h"
46 #include "ft_wordbreaker.h"
47
48
49 long ft_newhighest = 0L;
50 long *ft_newmsgs = NULL;
51 int ft_num_msgs = 0;
52 int ft_num_alloc = 0;
53
54
55 void ft_index_msg(long msgnum, void *userdata) {
56
57         if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
58                 ++ft_num_msgs;
59                 if (ft_num_msgs > ft_num_alloc) {
60                         ft_num_alloc += 1024;
61                         ft_newmsgs = realloc(ft_newmsgs, (ft_num_alloc * sizeof(long)));
62                 }
63                 ft_newmsgs[ft_num_msgs - 1] = msgnum;
64         }
65
66 }
67
68 /*
69  * Scan a room for messages to index.
70  */
71 void ft_index_room(struct ctdlroom *qrbuf, void *data)
72 {
73         getroom(&CC->room, qrbuf->QRname);
74         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, ft_index_msg, NULL);
75 }
76
77
78 /*
79  * Compare function
80  */
81 int longcmp(const void *rec1, const void *rec2) {
82         long i1, i2;
83
84         i1 = *(const long *)rec1;
85         i2 = *(const long *)rec2;
86
87         if (i1 > i2) return(1);
88         if (i1 < i2) return(-1);
89         return(0);
90 }
91
92
93
94 void do_fulltext_indexing(void) {
95         int i;
96
97         lprintf(CTDL_DEBUG, "do_fulltext_indexing() started\n");
98
99         /*
100          * Check to see whether the fulltext index is up to date; if there
101          * are no messages to index, don't waste any more time trying.
102          */
103         lprintf(CTDL_DEBUG, "CitControl.MMhighest  = %ld\n", CitControl.MMhighest);
104         lprintf(CTDL_DEBUG, "CitControl.MMfulltext = %ld\n", CitControl.MMfulltext);
105         if (CitControl.MMfulltext >= CitControl.MMhighest) {
106                 lprintf(CTDL_DEBUG, "Nothing to do!\n");
107                 return;
108         }
109         
110         /*
111          * Make sure we don't run the indexer too frequently.
112          * FIXME write this...
113          */
114
115         /*
116          * If we've switched wordbreaker modules, burn the index and start
117          * over.  FIXME write this...
118          */
119
120         /*
121          * Now go through each room and find messages to index.
122          */
123         ft_newhighest = CitControl.MMhighest;
124         ForEachRoom(ft_index_room, NULL);                               /* merge ptrs */
125
126         if (ft_num_msgs > 0) {
127                 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);  /* sort */
128                 if (i>1) for (i=0; i<(ft_num_msgs-1); ++i) {            /* purge dups */
129                         if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
130                                 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1], ((ft_num_msgs - i)*sizeof(long)));
131                                 --ft_num_msgs;
132                         }
133                 }
134
135                 /* Here it is ... do each message! */
136                 for (i=0; i<ft_num_msgs; ++i) {
137                         lprintf(CTDL_DEBUG, "FIXME INDEX %ld\n", ft_newmsgs[i]);
138                 }
139
140                 free(ft_newmsgs);
141                 ft_num_msgs = 0;
142                 ft_num_alloc = 0;
143         }
144
145         lprintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
146         return;
147 }
148
149
150 /*****************************************************************************/
151
152 char *serv_fulltext_init(void)
153 {
154         CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
155         return "$Id$";
156 }