* The size constant "256" which shows up everywhere as a buffer size has now
[citadel.git] / citadel / database_sleepycat.c
1 /*
2  * $Id$
3  *
4  * Sleepycat (Berkeley) DB driver for Citadel/UX
5  *
6  */
7
8 /*****************************************************************************/
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <time.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <signal.h>
20 #include <db.h>
21 #include "citadel.h"
22 #include "server.h"
23 #include "citserver.h"
24 #include "database.h"
25 #include "sysdep_decls.h"
26 #include "dynloader.h"
27
28 DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
29 DB_ENV *dbenv;                  /* The DB environment (global) */
30
31 struct cdbssd {                 /* Session-specific DB stuff */
32         DBC *cursor;            /* Cursor, for traversals... */
33 };
34
35 struct cdbssd *ssd_arr = NULL;
36 int num_ssd = 0;
37
38 #define MYCURSOR        ssd_arr[CC->cs_pid].cursor
39 #define MYTID           NULL
40
41 /*
42  * Ensure that we have enough space for session-specific data.  We don't
43  * put anything in here that Citadel cares about; this is just database
44  * related stuff like cursors.
45  */
46 void cdb_allocate_ssd(void) {
47         /*
48          * Make sure we have a cursor allocated for this session
49          */
50
51         lprintf(9, "num_ssd before realloc = %d\n", num_ssd);
52         if (num_ssd <= CC->cs_pid) {
53                 num_ssd = CC->cs_pid + 1;
54                 if (ssd_arr == NULL) {
55                         ssd_arr = (struct cdbssd *)
56                             mallok((sizeof(struct cdbssd) * num_ssd));
57                 } else {
58                         ssd_arr = (struct cdbssd *)
59                             reallok(ssd_arr, (sizeof(struct cdbssd) * num_ssd));
60                 }
61         }
62         lprintf(9, "num_ssd  after realloc = %d\n", num_ssd);
63 }
64
65
66 /*
67  * Reclaim unused space in the databases.  We need to do each one of
68  * these discretely, rather than in a loop.
69  *
70  * This is a stub function in the Sleepycat DB backend, because there is no
71  * such API call available.
72  */
73 void defrag_databases(void)
74 {
75         /* do nothing */
76 }
77
78
79
80 /*
81  * Open the various databases we'll be using.  Any database which
82  * does not exist should be created.  Note that we don't need an S_DATABASE
83  * critical section here, because there aren't any active threads manipulating
84  * the database yet -- and besides, it causes problems on BSDI.
85  */
86 void open_databases(void)
87 {
88         int ret;
89         int i;
90         char dbfilename[SIZ];
91         u_int32_t flags = 0;
92
93         lprintf(9, "open_databases() starting\n");
94         /*
95          * Silently try to create the database subdirectory.  If it's
96          * already there, no problem.
97          */
98         system("exec mkdir data 2>/dev/null");
99
100         lprintf(9, "Setting up DB environment\n");
101         ret = db_env_create(&dbenv, 0);
102         if (ret) {
103                 lprintf(1, "db_env_create: %s\n", db_strerror(ret));
104                 exit(ret);
105         }
106         dbenv->set_errpfx(dbenv, "citserver");
107
108         /*
109          * We want to specify the shared memory buffer pool cachesize,
110          * but everything else is the default.
111          */
112         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
113         if (ret) {
114                 lprintf(1, "set_cachesize: %s\n", db_strerror(ret));
115                 dbenv->close(dbenv, 0);
116                 exit(ret);
117         }
118
119         /*
120          * We specify DB_PRIVATE but not DB_INIT_LOCK or DB_THREAD, even
121          * though this is a multithreaded application.  Since Citadel does all
122          * database access in S_DATABASE critical sections, access to the db
123          * is serialized already, so don't bother the database manager with
124          * it.  Besides, it locks up when we do it that way.
125          */
126         flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_LOG;
127         /* flags |= DB_INIT_LOCK | DB_THREAD; */
128         ret = dbenv->open(dbenv, "./data", flags, 0);
129         if (ret) {
130                 lprintf(1, "dbenv->open: %s\n", db_strerror(ret));
131                 dbenv->close(dbenv, 0);
132                 exit(ret);
133         }
134
135         lprintf(7, "Starting up DB\n");
136
137         for (i = 0; i < MAXCDB; ++i) {
138
139                 /* Create a database handle */
140                 ret = db_create(&dbp[i], dbenv, 0);
141                 if (ret) {
142                         lprintf(1, "db_create: %s\n", db_strerror(ret));
143                         exit(ret);
144                 }
145
146
147                 /* Arbitrary names for our tables -- we reference them by
148                  * number, so we don't have string names for them.
149                  */
150                 sprintf(dbfilename, "cdb.%02x", i);
151
152                 ret = dbp[i]->open(dbp[i],
153                                 dbfilename,
154                                 NULL,
155                                 DB_BTREE,
156                                 DB_CREATE,
157                                 0600);
158                 if (ret) {
159                         lprintf(1, "db_open[%d]: %s\n", i, db_strerror(ret));
160                         exit(ret);
161                 }
162         }
163
164         cdb_allocate_ssd();
165         CtdlRegisterSessionHook(cdb_allocate_ssd, EVT_START);
166         lprintf(9, "open_databases() finished\n");
167 }
168
169
170 /*
171  * Close all of the gdbm database files we've opened.  This can be done
172  * in a loop, since it's just a bunch of closes.
173  */
174 void close_databases(void)
175 {
176         int a;
177         int ret;
178
179         begin_critical_section(S_DATABASE);
180         for (a = 0; a < MAXCDB; ++a) {
181                 lprintf(7, "Closing database %d\n", a);
182                 ret = dbp[a]->close(dbp[a], 0);
183                 if (ret) {
184                         lprintf(1, "db_close: %s\n", db_strerror(ret));
185                 }
186                 
187         }
188
189
190
191         /* Close the handle. */
192         ret = dbenv->close(dbenv, 0);
193         if (ret) {
194                 lprintf(1, "DBENV->close: %s\n", db_strerror(ret));
195         }
196
197
198         end_critical_section(S_DATABASE);
199
200 }
201
202
203 /*
204  * Store a piece of data.  Returns 0 if the operation was successful.  If a
205  * key already exists it should be overwritten.
206  */
207 int cdb_store(int cdb,
208               void *ckey, int ckeylen,
209               void *cdata, int cdatalen)
210 {
211
212         DBT dkey, ddata;
213         int ret;
214
215         memset(&dkey, 0, sizeof(DBT));
216         memset(&ddata, 0, sizeof(DBT));
217         dkey.size = ckeylen;
218         dkey.data = ckey;
219         ddata.size = cdatalen;
220         ddata.data = cdata;
221
222         begin_critical_section(S_DATABASE);
223         ret = dbp[cdb]->put(dbp[cdb],           /* db */
224                                 MYTID,          /* transaction ID */
225                                 &dkey,          /* key */
226                                 &ddata,         /* data */
227                                 0);             /* flags */
228         end_critical_section(S_DATABASE);
229         if (ret) {
230                 lprintf(1, "cdb_store(%d): %s\n", cdb, db_strerror(ret));
231                 return (-1);
232         }
233         return (0);
234 }
235
236
237 /*
238  * Delete a piece of data.  Returns 0 if the operation was successful.
239  */
240 int cdb_delete(int cdb, void *key, int keylen)
241 {
242
243         DBT dkey;
244         int ret;
245
246         dkey.size = keylen;
247         dkey.data = key;
248
249         begin_critical_section(S_DATABASE);
250         ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
251         end_critical_section(S_DATABASE);
252         return (ret);
253
254 }
255
256
257
258
259 /*
260  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
261  * a struct cdbdata which it is the caller's responsibility to free later on
262  * using the cdb_free() routine.
263  */
264 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
265 {
266
267         struct cdbdata *tempcdb;
268         DBT dkey, dret;
269         int ret;
270
271         memset(&dkey, 0, sizeof(DBT));
272         memset(&dret, 0, sizeof(DBT));
273         dkey.size = keylen;
274         dkey.data = key;
275         dret.flags = DB_DBT_MALLOC;
276
277         begin_critical_section(S_DATABASE);
278         ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
279         end_critical_section(S_DATABASE);
280         if ((ret != 0) && (ret != DB_NOTFOUND)) {
281                 lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
282         }
283         if (ret != 0) return NULL;
284         tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
285         if (tempcdb == NULL) {
286                 lprintf(2, "Cannot allocate memory!\n");
287         }
288         tempcdb->len = dret.size;
289         tempcdb->ptr = dret.data;
290         return (tempcdb);
291 }
292
293
294 /*
295  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
296  * more complex stuff with other database managers in the future).
297  */
298 void cdb_free(struct cdbdata *cdb)
299 {
300         phree(cdb->ptr);
301         phree(cdb);
302 }
303
304
305 /* 
306  * Prepare for a sequential search of an entire database.
307  * (There is guaranteed to be no more than one traversal in
308  * progress per session at any given time.)
309  */
310 void cdb_rewind(int cdb)
311 {
312         int ret = 0;
313
314         /*
315          * Now initialize the cursor
316          */
317         begin_critical_section(S_DATABASE);
318         ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
319         if (ret) {
320                 lprintf(1, "db_cursor: %s\n", db_strerror(ret));
321         }
322         end_critical_section(S_DATABASE);
323 }
324
325
326 /*
327  * Fetch the next item in a sequential search.  Returns a pointer to a 
328  * cdbdata structure, or NULL if we've hit the end.
329  */
330 struct cdbdata *cdb_next_item(int cdb)
331 {
332         DBT key, data;
333         struct cdbdata *cdbret;
334         int ret = 0;
335
336         /* Initialize the key/data pair so the flags aren't set. */
337         memset(&key, 0, sizeof(key));
338         memset(&data, 0, sizeof(data));
339         data.flags = DB_DBT_MALLOC;
340
341         begin_critical_section(S_DATABASE);
342         ret = MYCURSOR->c_get(MYCURSOR,
343                 &key, &data, DB_NEXT);
344         end_critical_section(S_DATABASE);
345         
346         if (ret) return NULL;           /* presumably, end of file */
347
348         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
349         cdbret->len = data.size;
350         cdbret->ptr = data.data;
351
352         return (cdbret);
353 }
354