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