* Finished (mostly) the Sleepycat DB backend ... added transaction logging
[citadel.git] / citadel / database.c
1 /*
2  * $Id$
3  *
4  * GDBM database driver for Citadel/UX
5  *
6  */
7
8 /*
9  * Note that each call to a GDBM function is wrapped in an S_DATABASE critical
10  * section.  This is done because GDBM is not threadsafe.  This is the ONLY
11  * place in the entire Citadel server where any code enters two different
12  * classes of critical sections at the same time; this is why the GDBM calls
13  * are *tightly* wrapped in S_DATABASE.  Opening multiple concurrent critical
14  * sections elsewhere in the code can, and probably will, cause deadlock
15  * conditions to occur.  (Deadlock is bad.  Eliminate.)
16  */
17
18 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <errno.h>
26 #ifdef HAVE_GDBM_H
27 #include <gdbm.h>
28 #endif
29 #include "citadel.h"
30 #include "server.h"
31 #include "database.h"
32 #include "sysdep_decls.h"
33
34
35 /*
36  * This array holds one gdbm handle for each Citadel database.
37  */
38 GDBM_FILE gdbms[MAXCDB];
39
40 /*
41  * We also keep these around, for sequential searches (one per session slot)
42  */
43 int max_keys = 0;
44 datum *dtkey;
45
46
47 /*
48  * Reclaim unused space in the databases.  We need to do each one of
49  * these discretely, rather than in a loop.
50  */
51 void defrag_databases(void)
52 {
53
54         /* defrag the message base */
55         lprintf(7, "Defragmenting message base\n");
56         begin_critical_section(S_MSGMAIN);
57         begin_critical_section(S_DATABASE);
58         gdbm_reorganize(gdbms[CDB_MSGMAIN]);
59         end_critical_section(S_DATABASE);
60         end_critical_section(S_MSGMAIN);
61
62         /* defrag the user file, mailboxes, and user/room relationships */
63         lprintf(7, "Defragmenting user file\n");
64         begin_critical_section(S_USERSUPP);
65         begin_critical_section(S_DATABASE);
66         gdbm_reorganize(gdbms[CDB_USERSUPP]);
67         gdbm_reorganize(gdbms[CDB_VISIT]);
68         end_critical_section(S_DATABASE);
69         end_critical_section(S_USERSUPP);
70
71         /* defrag the room files and message lists */
72         lprintf(7, "Defragmenting room files and message lists\n");
73         begin_critical_section(S_QUICKROOM);
74         begin_critical_section(S_DATABASE);
75         gdbm_reorganize(gdbms[CDB_QUICKROOM]);
76         gdbm_reorganize(gdbms[CDB_MSGLISTS]);
77         end_critical_section(S_DATABASE);
78         end_critical_section(S_QUICKROOM);
79
80         /* defrag the floor table */
81         lprintf(7, "Defragmenting floor table\n");
82         begin_critical_section(S_FLOORTAB);
83         begin_critical_section(S_DATABASE);
84         gdbm_reorganize(gdbms[CDB_FLOORTAB]);
85         end_critical_section(S_DATABASE);
86         end_critical_section(S_FLOORTAB);
87 }
88
89
90 /*
91  * Open the various gdbm databases we'll be using.  Any database which
92  * does not exist should be created.
93  */
94 void open_databases(void)
95 {
96         lprintf(7, "%s\n", gdbm_version);
97
98         /*
99          * Silently try to create the database subdirectory.  If it's
100          * already there, no problem.
101          */
102         system("exec mkdir data 2>/dev/null");
103
104         /* a critical section is unnecessary, as this function is called before
105            any other threads are created. and it causes problems on BSDI.
106
107            begin_critical_section(S_DATABASE);
108
109          */
110
111         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
112                                        GDBM_WRCREAT, 0600, NULL);
113         if (gdbms[CDB_MSGMAIN] == NULL) {
114                 lprintf(2, "Cannot open msgmain: %s\n",
115                         gdbm_strerror(gdbm_errno));
116                 exit(1);
117         }
118         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
119                                         GDBM_WRCREAT, 0600, NULL);
120         if (gdbms[CDB_USERSUPP] == NULL) {
121                 lprintf(2, "Cannot open usersupp: %s\n",
122                         gdbm_strerror(gdbm_errno));
123                 exit(1);
124         }
125         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
126                                      GDBM_WRCREAT, 0600, NULL);
127         if (gdbms[CDB_VISIT] == NULL) {
128                 lprintf(2, "Cannot open visit file: %s\n",
129                         gdbm_strerror(gdbm_errno));
130                 exit(1);
131         }
132         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
133                                          GDBM_WRCREAT, 0600, NULL);
134         if (gdbms[CDB_QUICKROOM] == NULL) {
135                 lprintf(2, "Cannot open quickroom: %s\n",
136                         gdbm_strerror(gdbm_errno));
137                 exit(1);
138         }
139         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
140                                         GDBM_WRCREAT, 0600, NULL);
141         if (gdbms[CDB_FLOORTAB] == NULL) {
142                 lprintf(2, "Cannot open floortab: %s\n",
143                         gdbm_strerror(gdbm_errno));
144                 exit(1);
145         }
146         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
147                                         GDBM_WRCREAT, 0600, NULL);
148         if (gdbms[CDB_MSGLISTS] == NULL) {
149                 lprintf(2, "Cannot open msglists: %s\n",
150                         gdbm_strerror(gdbm_errno));
151                 exit(1);
152         }
153         /*
154            end_critical_section(S_DATABASE);
155          */
156
157 }
158
159
160 /*
161  * Close all of the gdbm database files we've opened.  This can be done
162  * in a loop, since it's just a bunch of closes.
163  */
164 void close_databases(void)
165 {
166         int a;
167
168         begin_critical_section(S_DATABASE);
169         for (a = 0; a < MAXCDB; ++a) {
170                 lprintf(7, "Closing database %d\n", a);
171                 gdbm_close(gdbms[a]);
172         }
173         end_critical_section(S_DATABASE);
174
175         for (a = 0; a < max_keys; ++a) {
176                 if (dtkey[a].dptr != NULL) {
177                         phree(dtkey[a].dptr);
178                 }
179         }
180
181 }
182
183
184 /*
185  * Store a piece of data.  Returns 0 if the operation was successful.  If a
186  * datum already exists it should be overwritten.
187  */
188 int cdb_store(int cdb,
189               void *key, int keylen,
190               void *data, int datalen)
191 {
192
193         datum dkey, ddata;
194         int retval;
195
196         dkey.dsize = keylen;
197         dkey.dptr = key;
198         ddata.dsize = datalen;
199         ddata.dptr = data;
200
201         begin_critical_section(S_DATABASE);
202         retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
203         end_critical_section(S_DATABASE);
204         if (retval < 0) {
205                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
206                 return (-1);
207         }
208         return (0);
209 }
210
211
212 /*
213  * Delete a piece of data.  Returns 0 if the operation was successful.
214  */
215 int cdb_delete(int cdb, void *key, int keylen)
216 {
217
218         datum dkey;
219         int retval;
220
221         dkey.dsize = keylen;
222         dkey.dptr = key;
223
224         begin_critical_section(S_DATABASE);
225         retval = gdbm_delete(gdbms[cdb], dkey);
226         end_critical_section(S_DATABASE);
227         return (retval);
228
229 }
230
231
232
233
234 /*
235  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
236  * a struct cdbdata which it is the caller's responsibility to free later on
237  * using the cdb_free() routine.
238  */
239 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
240 {
241
242         struct cdbdata *tempcdb;
243         datum dkey, dret;
244
245         dkey.dsize = keylen;
246         dkey.dptr = key;
247
248         begin_critical_section(S_DATABASE);
249         dret = gdbm_fetch(gdbms[cdb], dkey);
250         end_critical_section(S_DATABASE);
251         if (dret.dptr == NULL) {
252                 return NULL;
253         }
254         tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
255         if (tempcdb == NULL) {
256                 lprintf(2, "Cannot allocate memory!\n");
257         }
258         tempcdb->len = dret.dsize;
259         tempcdb->ptr = dret.dptr;
260         return (tempcdb);
261 }
262
263
264 /*
265  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
266  * more complex stuff with other database managers in the future).
267  */
268 void cdb_free(struct cdbdata *cdb)
269 {
270         phree(cdb->ptr);
271         phree(cdb);
272 }
273
274
275 /* 
276  * Prepare for a sequential search of an entire database.  (In the gdbm model,
277  * we do this by keeping an array dtkey[] of "the next" key for each session
278  * that is open.  There is guaranteed to be no more than one traversal in
279  * progress per session at any given time.)
280  */
281 void cdb_rewind(int cdb)
282 {
283
284         while (max_keys <= CC->cs_pid) {
285                 ++max_keys;
286                 if (dtkey == NULL) {
287                         dtkey = (datum *)
288                             mallok((sizeof(datum) * max_keys));
289                 } else {
290                         dtkey = (datum *)
291                             reallok(dtkey, (sizeof(datum) * max_keys));
292                 }
293                 dtkey[max_keys - 1].dsize = 0;
294                 dtkey[max_keys - 1].dptr = NULL;
295         }
296
297         if (dtkey[CC->cs_pid].dptr != NULL) {
298                 phree(dtkey[CC->cs_pid].dptr);
299         }
300         begin_critical_section(S_DATABASE);
301         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
302         end_critical_section(S_DATABASE);
303 }
304
305
306 /*
307  * Fetch the next item in a sequential search.  Returns a pointer to a 
308  * cdbdata structure, or NULL if we've hit the end.
309  */
310 struct cdbdata *cdb_next_item(int cdb)
311 {
312         datum dret;
313         struct cdbdata *cdbret;
314         void *ptr = NULL;
315
316
317         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
318                 return NULL;
319         }
320         begin_critical_section(S_DATABASE);
321         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
322         end_critical_section(S_DATABASE);
323         if (dret.dptr == NULL) {        /* bad read */
324                 phree(dtkey[CC->cs_pid].dptr);
325                 return NULL;
326         }
327         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
328         cdbret->len = dret.dsize;
329         cdbret->ptr = dret.dptr;
330
331         ptr = dtkey[CC->cs_pid].dptr;
332         begin_critical_section(S_DATABASE);
333         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
334         end_critical_section(S_DATABASE);
335
336         if (ptr != NULL) {      /* Free the previous key. */
337                 free(ptr);
338         }
339
340         return (cdbret);
341 }
342
343
344
345 /*
346  * GDBM doesn't support transaction-based logging.  Stub out these functions.
347  */
348
349 void cdb_begin_transaction(void) {
350 }
351
352 void cdb_end_transaction(void) {
353 }