4 * Index messages by EUID per room.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
34 #include "serv_extensions.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
46 #include "euidindex.h"
49 * The structure of an EUID *index* is:
51 * |----room_number----|----------EUID-------------|
52 * (sizeof long) (actual length of euid)
55 * The structure of an EUID *record* is:
57 * |-----msg_number----|----room_number----|----------EUID-------------|
58 * (sizeof long) (sizeof long) (actual length of euid)
62 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
65 struct cdbdata *cdb_euid;
68 lprintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
70 key_len = strlen(euid) + sizeof(long) + 1;
71 key = malloc(key_len);
72 memcpy(key, &qrbuf->QRnumber, sizeof(long));
73 strcpy(&key[sizeof(long)], euid);
75 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
78 if (cdb_euid == NULL) {
82 /* The first (sizeof long) of the record is what we're
83 * looking for. Throw away the rest.
85 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
88 lprintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
92 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
98 lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
100 key_len = strlen(euid) + sizeof(long) + 1;
101 key = malloc(key_len);
102 memcpy(key, &qrbuf->QRnumber, sizeof(long));
103 strcpy(&key[sizeof(long)], euid);
105 data_len = sizeof(long) + key_len;
106 data = malloc(data_len);
108 memcpy(data, &msgnum, sizeof(long));
109 memcpy(&data[sizeof(long)], key, key_len);
111 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
119 * Called by rebuild_euid_index_for_room() to index one message.
121 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
122 struct CtdlMessage *msg;
124 msg = CtdlFetchMessage(msgnum, 0);
125 if (msg == NULL) return;
126 if (msg->cm_fields['E'] != NULL) {
127 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
129 CtdlFreeMessage(msg);
133 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
134 static struct RoomProcList *rplist = NULL;
135 struct RoomProcList *ptr;
138 /* Lazy programming here. Call this function as a ForEachRoom backend
139 * in order to queue up the room names, or call it with a null room
140 * to make it do the processing.
143 ptr = (struct RoomProcList *)
144 malloc(sizeof (struct RoomProcList));
145 if (ptr == NULL) return;
147 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
153 while (rplist != NULL) {
154 if (getroom(&qr, rplist->name) == 0) {
155 lprintf(CTDL_DEBUG, "Rebuilding EUID index for <%s>\n", rplist->name);
156 usergoto(rplist->name, 0, 0, NULL, NULL);
157 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, rebuild_euid_index_for_msg, NULL);
160 rplist = rplist->next;
167 * Globally rebuild the EUID indices in every room.
169 void rebuild_euid_index(void) {
170 cdb_trunc(CDB_EUIDINDEX); /* delete the old indices */
171 ForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate the room names */
172 rebuild_euid_index_for_room(NULL, NULL); /* now do indexing on them */