Don't automatically defrag databases on shutdown
[citadel.git] / citadel / database.c
1 /*
2  * This file contains a set of abstractions that allow Citadel to plug into any
3  * record manager or database system for its data store.
4  */
5
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <pthread.h>
15 #include <gdbm.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include "database.h"
19 #include "sysdep_decls.h"
20
21
22 /*
23  * This array holds one gdbm handle for each Citadel database.
24  */
25 GDBM_FILE gdbms[MAXCDB];
26
27 /*
28  * We also keep these around, for sequential searches... (one per 
29  * session.  Maybe there's a better way?)
30  */
31 #define MAXKEYS 256
32 datum dtkey[MAXKEYS];
33
34
35 /*
36  * Reclaim unused space in the databases.  We need to do each one of
37  * these discretely, rather than in a loop.
38  */
39 void defrag_databases(void) {
40
41         /* defrag the message base */
42         begin_critical_section(S_MSGMAIN);
43         gdbm_reorganize(gdbms[CDB_MSGMAIN]);
44         end_critical_section(S_MSGMAIN);
45
46         /* defrag the user file, mailboxes, and user/room relationships */
47         begin_critical_section(S_USERSUPP);
48         gdbm_reorganize(gdbms[CDB_USERSUPP]);
49         gdbm_reorganize(gdbms[CDB_MAILBOXES]);
50         gdbm_reorganize(gdbms[CDB_VISIT]);
51         end_critical_section(S_USERSUPP);
52
53         /* defrag the room files and message lists */
54         begin_critical_section(S_QUICKROOM);
55         gdbm_reorganize(gdbms[CDB_QUICKROOM]);
56         gdbm_reorganize(gdbms[CDB_MSGLISTS]);
57         end_critical_section(S_QUICKROOM);
58
59         /* defrag the floor table */
60         begin_critical_section(S_FLOORTAB);
61         gdbm_reorganize(gdbms[CDB_FLOORTAB]);
62         end_critical_section(S_FLOORTAB);
63         }
64
65
66 /*
67  * Open the various gdbm databases we'll be using.  Any database which
68  * does not exist should be created.
69  */
70 void open_databases(void) {
71         int a;
72
73         /*
74          * Silently try to create the database subdirectory.  If it's
75          * already there, no problem.
76          */
77         system("exec mkdir data 2>/dev/null");
78
79         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
80                 GDBM_WRCREAT, 0600, NULL);
81         if (gdbms[CDB_MSGMAIN] == NULL) {
82                 lprintf(2, "Cannot open msgmain: %s\n",
83                         gdbm_strerror(gdbm_errno));
84                 }
85
86         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
87                 GDBM_WRCREAT, 0600, NULL);
88         if (gdbms[CDB_USERSUPP] == NULL) {
89                 lprintf(2, "Cannot open usersupp: %s\n",
90                         gdbm_strerror(gdbm_errno));
91                 }
92
93         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
94                 GDBM_WRCREAT, 0600, NULL);
95         if (gdbms[CDB_VISIT] == NULL) {
96                 lprintf(2, "Cannot open visit file: %s\n",
97                         gdbm_strerror(gdbm_errno));
98                 }
99
100         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
101                 GDBM_WRCREAT, 0600, NULL);
102         if (gdbms[CDB_QUICKROOM] == NULL) {
103                 lprintf(2, "Cannot open quickroom: %s\n",
104                         gdbm_strerror(gdbm_errno));
105                 }
106
107         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
108                 GDBM_WRCREAT, 0600, NULL);
109         if (gdbms[CDB_FLOORTAB] == NULL) {
110                 lprintf(2, "Cannot open floortab: %s\n",
111                         gdbm_strerror(gdbm_errno));
112                 }
113
114         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
115                 GDBM_WRCREAT, 0600, NULL);
116         if (gdbms[CDB_MSGLISTS] == NULL) {
117                 lprintf(2, "Cannot open msglists: %s\n",
118                         gdbm_strerror(gdbm_errno));
119                 }
120
121         gdbms[CDB_MAILBOXES] = gdbm_open("data/mailboxes.gdbm", 0,
122                 GDBM_WRCREAT, 0600, NULL);
123         if (gdbms[CDB_MAILBOXES] == NULL) {
124                 lprintf(2, "Cannot open mailboxes: %s\n",
125                         gdbm_strerror(gdbm_errno));
126                 }
127
128         for (a=0; a<MAXKEYS; ++a) {
129                 dtkey[a].dsize = 0;
130                 dtkey[a].dptr = NULL;
131                 }
132
133         }
134
135
136 /*
137  * Close all of the gdbm database files we've opened.  This can be done
138  * in a loop, since it's just a bunch of closes.
139  */
140 void close_databases(void) {
141         int a;
142
143         /* Hmm... we should decide when would be a good time to defrag.
144          * Server shutdowns might be an opportune time.
145         defrag_databases(); */
146
147         for (a=0; a<MAXCDB; ++a) {
148                 lprintf(7, "Closing database %d\n", a);
149                 gdbm_close(gdbms[a]);
150                 }
151
152         for (a=0; a<MAXKEYS; ++a) {
153                 if (dtkey[a].dptr != NULL) {
154                         free(dtkey[a].dptr);
155                         }
156                 }
157
158         }
159
160
161 /*
162  * Store a piece of data.  Returns 0 if the operation was successful.  If a
163  * datum already exists it should be overwritten.
164  */
165 int cdb_store(int cdb,
166                 void *key, int keylen,
167                 void *data, int datalen) {
168
169         datum dkey, ddata;
170
171         dkey.dsize = keylen;
172         dkey.dptr = key;
173         ddata.dsize = datalen;
174         ddata.dptr = data;
175
176         if ( gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE) < 0 ) {
177                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
178                 return(-1);
179                 }
180
181         return(0);
182         }
183
184
185 /*
186  * Delete a piece of data.  Returns 0 if the operation was successful.
187  */
188 int cdb_delete(int cdb, void *key, int keylen) {
189
190         datum dkey;
191
192         dkey.dsize = keylen;
193         dkey.dptr = key;
194
195         return(gdbm_delete(gdbms[cdb], dkey));
196
197         }
198
199
200
201
202 /*
203  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
204  * a struct cdbdata which it is the caller's responsibility to free later on
205  * using the cdb_free() routine.
206  */
207 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
208         
209         struct cdbdata *tempcdb;
210         datum dkey, dret;
211         
212         dkey.dsize = keylen;
213         dkey.dptr = key;
214
215         dret = gdbm_fetch(gdbms[cdb], dkey);
216         if (dret.dptr == NULL) {
217                 return NULL;
218                 }
219
220         tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
221         if (tempcdb == NULL) {
222                 lprintf(2, "Cannot allocate memory!\n");
223                 }
224
225         tempcdb->len = dret.dsize;
226         tempcdb->ptr = dret.dptr;
227         return(tempcdb);
228         }
229
230
231 /*
232  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
233  * more complex stuff with other database managers in the future).
234  */
235 void cdb_free(struct cdbdata *cdb) {
236         free(cdb->ptr);
237         free(cdb);
238         }
239
240
241 /* 
242  * Prepare for a sequential search of an entire database.  (In the gdbm model,
243  * we do this by keeping an array dtkey[] of "the next" key for each session
244  * that is open.  There is guaranteed to be no more than one traversal in
245  * progress per session at any given time.)
246  */
247 void cdb_rewind(int cdb) {
248
249         if (dtkey[CC->cs_pid].dptr != NULL) {
250                 free(dtkey[CC->cs_pid].dptr);
251                 }
252
253         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
254         }
255
256
257 /*
258  * Fetch the next item in a sequential search.  Returns a pointer to a 
259  * cdbdata structure, or NULL if we've hit the end.
260  */
261 struct cdbdata *cdb_next_item(int cdb) {
262         datum dret;
263         struct cdbdata *cdbret;
264
265         
266         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
267                 return NULL;
268                 }
269
270         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
271         if (dret.dptr == NULL) {        /* bad read */
272                 free(dtkey[CC->cs_pid].dptr);
273                 return NULL;
274                 }
275
276         cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
277         cdbret->len = dret.dsize;
278         cdbret->ptr = dret.dptr;
279
280         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
281         return(cdbret);
282         }