]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/fulltext/serv_fulltext.c
Removed double-fetch-room in the indexer.
[citadel.git] / citadel / server / modules / fulltext / serv_fulltext.c
1 // This module handles fulltext indexing of the message base.
2 //
3 // Copyright (c) 2005-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
7
8 #include "../../sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <time.h>
18 #include <sys/wait.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <libcitadel.h>
22 #include "../../citadel_defs.h"
23 #include "../../server.h"
24 #include "../../citserver.h"
25 #include "../../support.h"
26 #include "../../config.h"
27 #include "../../room_ops.h"
28 #include "../../database.h"
29 #include "../../msgbase.h"
30 #include "../../control.h"
31 #include "serv_fulltext.h"
32 #include "ft_wordbreaker.h"
33 #include "../../threads.h"
34 #include "../../context.h"
35 #include "../../ctdl_module.h"
36
37 // Compare function
38 int longcmp(const void *rec1, const void *rec2) {
39         long i1, i2;
40
41         i1 = *(const long *)rec1;
42         i2 = *(const long *)rec2;
43
44         if (i1 > i2) return(1);
45         if (i1 < i2) return(-1);
46         return(0);
47 }
48
49
50                 //if (ftc_msgs[i] != NULL) {
51                         //cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i], (ftc_num_msgs[i] * sizeof(long)));
52
53
54
55 // Index or de-index a message.  (op == 1 to index, 0 to de-index)
56 void ft_index_message(long msgnum, int op) {
57         int i, j;
58         Array *tokens_in_this_message = NULL;
59         struct cdbdata cdb_bucket;
60         StrBuf *msgtext;
61         char *txt;
62         int tok;
63         struct CtdlMessage *msg = NULL;
64
65         if (msgnum == 0) return;
66
67         msg = CtdlFetchMessage(msgnum, 1);
68         if (msg == NULL) {
69                 syslog(LOG_ERR, "fulltext: ft_index_message() could not load msg %ld", msgnum);
70                 return;
71         }
72
73         if (!CM_IsEmpty(msg, eSuppressIdx)) {
74                 syslog(LOG_DEBUG, "fulltext: ft_index_message() excluded msg %ld", msgnum);
75                 CM_Free(msg);
76                 return;
77         }
78
79         syslog(LOG_DEBUG, "fulltext: ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
80
81         // Output the message as text before indexing it, so we don't end up indexing a bunch of encoded base64, etc.
82         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
83         CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
84         CM_Free(msg);
85         msgtext = CC->redirect_buffer;
86         CC->redirect_buffer = NULL;
87         if (msgtext != NULL) {
88                 syslog(LOG_DEBUG, "fulltext: wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
89         }
90         txt = SmashStrBuf(&msgtext);
91         tokens_in_this_message = wordbreaker(txt);
92         free(txt);
93
94         syslog(LOG_DEBUG, "fulltext: %sindexing message %ld [%d tokens]",
95                 (op ? "" : "de"),
96                 msgnum,
97                 array_len(tokens_in_this_message)
98         );
99
100         if (array_len(tokens_in_this_message) > 0) {
101                 for (i=0; i<array_len(tokens_in_this_message); ++i) {
102
103                         // Identify the bucket which we will be modifying
104                         memcpy(&tok, array_get_element_at(tokens_in_this_message, i), sizeof(int));
105         
106                         // fetch the bucket
107                         // cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
108                         // if op == 1 , add this msgnum to the record.
109                         // if op == 0 , remove this msgnum from the record.
110                         // FIXME do this
111                         // FIXME then write it back to disk
112                 }
113         }
114         array_free(tokens_in_this_message);
115 }
116
117
118
119 Array *messages_to_be_indexed = NULL;
120 long highest_msg_already_indexed = 0;
121 long highest_msg_to_be_indexed = 0;
122
123
124 // Scan a room for messages to index.
125 void ft_index_room(struct ctdlroom *qrbuf, void *data) {
126         if (server_shutting_down) {
127                 return;
128         }
129
130         int num_msgs = 0;
131         long *msglist;
132         int i;
133         num_msgs = CtdlFetchMsgList(qrbuf->QRnumber, &msglist);
134         if (msglist != NULL) {
135                 for (i=0; i<num_msgs; ++i) {
136                         if (
137                                 (msglist[i] > 0)
138                                 && (msglist[i] > highest_msg_already_indexed)
139                                 && (msglist[i] <= highest_msg_to_be_indexed)
140                            ) {
141                                 array_append(messages_to_be_indexed, &msglist[i]);
142                         }
143                 }
144                 free(msglist);
145         }
146 }
147
148
149 // Begin the fulltext indexing process.
150 void do_fulltext_indexing(void) {
151         int i;
152         static time_t last_progress = 0L;
153         static int is_running = 0;
154         if (is_running) return;         // Concurrency check - only one can run 
155         is_running = 1;
156
157         // Don't do this if the site doesn't have it enabled.
158         if (!CtdlGetConfigInt("c_enable_fulltext")) {
159                 return;
160         }
161         syslog(LOG_DEBUG, "fulltext: indexing started");
162
163         // If we've switched wordbreaker modules, burn the index and start over.
164         begin_critical_section(S_CONTROL);
165         if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
166                 syslog(LOG_DEBUG, "fulltext: wb ver on disk = %d, code ver = %d",
167                         CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
168                 );
169                 syslog(LOG_INFO, "fulltext: (re)initializing index");
170                 cdb_trunc(CDB_FULLTEXT);
171                 CtdlSetConfigLong("MMfulltext", 0);
172         }
173         end_critical_section(S_CONTROL);
174
175         // Silently return if our fulltext index is up to date with new messages.
176         if ((CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))) {
177                 return;         // nothing to do!
178         }
179
180         highest_msg_already_indexed = CtdlGetConfigLong("MMfulltext");
181         highest_msg_to_be_indexed = CtdlGetConfigLong("MMhighest");
182         messages_to_be_indexed = array_new(sizeof(long));
183
184         // Now go through each room and find messages to index.
185         CtdlForEachRoom(ft_index_room, NULL);                           // load all msg pointers
186         array_sort(messages_to_be_indexed, longcmp);                    // sort them
187
188         // Here it is ... do each message!
189         long msgnum = 0;
190         long prev_msgnum = 0;
191         for (i=0; i<array_len(messages_to_be_indexed); ++i) {
192                 memcpy(&msgnum, array_get_element_at(messages_to_be_indexed, i), sizeof(long));
193
194                 if (msgnum != prev_msgnum) {                            // careful to avoid dupes
195                         ft_index_message(msgnum, 1);
196                 }
197
198                 prev_msgnum = msgnum;
199         }
200
201         array_free(messages_to_be_indexed);
202
203 #if 0
204         syslog(LOG_DEBUG,
205                 "fulltext: indexed %d of %d messages (%d%%)", i, ft_num_msgs, ((i*100) / ft_num_msgs));
206         last_progress = time(NULL);
207
208         // Check to see if we need to quit early
209         if (server_shutting_down) {
210                 syslog(LOG_DEBUG, "fulltext: indexer quitting early");
211                 ft_newhighest = ft_newmsgs[i];
212                 break;
213         }
214
215         begin_critical_section(S_CONTROL);
216         CtdlSetConfigLong("MMfulltext", ft_newhighest);
217         CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
218         end_critical_section(S_CONTROL);
219 #endif
220
221         syslog(LOG_DEBUG, "fulltext: indexing finished");
222         is_running = 0;
223         return;
224 }
225
226
227 // API call to perform searches.
228 // (This one does the "all of these words" search.)
229 // Caller is responsible for freeing the message list.
230 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
231         Array *t = NULL;
232         int i, j;
233         struct cdbdata cdb_bucket;
234         int num_all_msgs = 0;
235         long *all_msgs = NULL;
236         int num_ret_msgs = 0;
237         int num_ret_alloc = 0;
238         long *ret_msgs = NULL;
239         int tok;
240
241 #if 0
242         t = wordbreaker(search_string);
243         if (array_len(t) > 0) {
244                 for (i=0; i<array_len(t); ++i) {
245
246                         // search for tokens[i]
247                         memcpy(&tok, array_get_element_at(t, i), sizeof(int));
248
249                         // fetch the bucket, Liza
250                         if (ftc_msgs[tok] == NULL) {
251                                 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
252                                 if (cdb_bucket.ptr != NULL) {
253                                         ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
254                                         ftc_msgs[tok] = (long *) malloc(cdb_bucket.len);
255                                         memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
256                                 }
257                                 else {
258                                         ftc_num_msgs[tok] = 0;
259                                         ftc_msgs[tok] = malloc(sizeof(long));
260                                 }
261                         }
262
263                         num_all_msgs += ftc_num_msgs[tok];
264                         if (num_all_msgs > 0) {
265                                 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
266                                 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]], ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
267                         }
268
269                 }
270                 array_free(t);
271                 if (all_msgs != NULL) {
272                         qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
273
274                         // At this point, if a message appears array_len(t) times in the
275                         // list, then it contains all of the search tokens.
276                         if (num_all_msgs >= array_len(t))
277                                 for (j=0; j<(num_all_msgs-array_len(t)+1); ++j) {
278                                         if (all_msgs[j] == all_msgs[j+array_len(t)-1]) {
279                                                 ++num_ret_msgs;
280                                                 if (num_ret_msgs > num_ret_alloc) {
281                                                         num_ret_alloc += 64;
282                                                         ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
283                                                 }
284                                                 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
285                                                 
286                                         }
287                                 }
288                         free(all_msgs);
289                 }
290         }
291
292         *fts_num_msgs = num_ret_msgs;
293         *fts_msgs = ret_msgs;
294 #endif
295 }
296
297
298 // This search command is for diagnostic purposes and may be removed or replaced.
299 void cmd_srch(char *argbuf) {
300         int num_msgs = 0;
301         long *msgs = NULL;
302         int i;
303         char search_string[256];
304
305         if (CtdlAccessCheck(ac_logged_in)) return;
306
307         if (!CtdlGetConfigInt("c_enable_fulltext")) {
308                 cprintf("%d Full text index is not enabled on this server.\n", ERROR + CMD_NOT_SUPPORTED);
309                 return;
310         }
311
312         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
313         ft_search(&num_msgs, &msgs, search_string);
314
315         cprintf("%d %d msgs match all search words:\n", LISTING_FOLLOWS, num_msgs);
316         if (num_msgs > 0) {
317                 for (i=0; i<num_msgs; ++i) {
318                         cprintf("%ld\n", msgs[i]);
319                 }
320         }
321         if (msgs != NULL) free(msgs);
322         cprintf("000\n");
323 }
324
325
326
327 void ft_delete_remove(char *room, long msgnum) {
328         if (room) return;
329         
330         // Remove from fulltext index
331         if (CtdlGetConfigInt("c_enable_fulltext")) {
332                 ft_index_message(msgnum, 0);
333         }
334 }
335
336
337 // Initialization function, called from modules_init.c
338 char *ctdl_module_init_fulltext(void) {
339         if (!threading) {
340                 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
341                 CtdlRegisterDeleteHook(ft_delete_remove);
342                 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
343                 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER, PRIO_CLEANUP + 300);
344         }
345         // return our module name for the log
346         return "fulltext";
347 }