4 * Sleepycat (Berkeley) DB driver for Citadel/UX
8 /*****************************************************************************
9 Tunable configuration parameters for the Sleepycat DB back end
10 *****************************************************************************/
12 /* Set to 1 for transaction-based database logging. This is recommended for
13 * safe recovery in the event of system or application failure.
15 #define TRANSACTION_BASED 1
17 /* Citadel will checkpoint the db at the end of every session, but only if
18 * the specified number of kilobytes has been written, or if the specified
19 * number of minutes has passed, since the last checkpoint.
21 #define MAX_CHECKPOINT_KBYTES 0
22 #define MAX_CHECKPOINT_MINUTES 15
24 /*****************************************************************************/
37 #include "citserver.h"
39 #include "sysdep_decls.h"
40 #include "dynloader.h"
42 DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
43 DB_ENV *dbenv; /* The DB environment (global) */
45 struct cdbssd { /* Session-specific DB stuff */
46 DBC *cursor; /* Cursor, for traversals... */
47 DB_TXN *tid; /* Transaction ID */
50 struct cdbssd *ssd_arr = NULL;
52 #define MYCURSOR ssd_arr[CC->cs_pid].cursor
53 #define MYTID ssd_arr[CC->cs_pid].tid
56 * Ensure that we have enough space for session-specific data. We don't
57 * put anything in here that Citadel cares about; this is just database
58 * related stuff like cursors and transactions.
60 void cdb_allocate_ssd(void) {
62 * Make sure we have a cursor allocated for this session
65 lprintf(9, "num_ssd before realloc = %d\n", num_ssd);
66 if (num_ssd <= CC->cs_pid) {
67 num_ssd = CC->cs_pid + 1;
68 if (ssd_arr == NULL) {
69 ssd_arr = (struct cdbssd *)
70 mallok((sizeof(struct cdbssd) * num_ssd));
72 ssd_arr = (struct cdbssd *)
73 reallok(ssd_arr, (sizeof(struct cdbssd) * num_ssd));
76 lprintf(9, "num_ssd after realloc = %d\n", num_ssd);
81 * Reclaim unused space in the databases. We need to do each one of
82 * these discretely, rather than in a loop.
84 * This is a stub function in the Sleepycat DB backend, because there is no
85 * such API call available.
87 void defrag_databases(void)
95 * Request a checkpoint of the database.
97 void cdb_checkpoint(void) {
100 ret = txn_checkpoint(dbenv,
101 MAX_CHECKPOINT_KBYTES,
102 MAX_CHECKPOINT_MINUTES,
105 lprintf(1, "txn_checkpoint: %s\n", db_strerror(ret));
111 * Open the various databases we'll be using. Any database which
112 * does not exist should be created. Note that we don't need an S_DATABASE
113 * critical section here, because there aren't any active threads manipulating
114 * the database yet -- and besides, it causes problems on BSDI.
116 void open_databases(void)
120 char dbfilename[256];
124 * Silently try to create the database subdirectory. If it's
125 * already there, no problem.
127 system("exec mkdir data 2>/dev/null");
129 lprintf(9, "Setting up DB environment\n");
130 ret = db_env_create(&dbenv, 0);
132 lprintf(1, "db_env_create: %s\n", db_strerror(ret));
135 dbenv->set_errpfx(dbenv, "citserver");
138 * We want to specify the shared memory buffer pool cachesize,
139 * but everything else is the default.
141 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
143 lprintf(1, "set_cachesize: %s\n", db_strerror(ret));
144 dbenv->close(dbenv, 0);
149 * We specify DB_PRIVATE but not DB_INIT_LOCK or DB_THREAD, even
150 * though this is a multithreaded application. Since Citadel does all
151 * database access in S_DATABASE critical sections, access to the db
152 * is serialized already, so don't bother the database manager with
153 * it. Besides, it locks up when we do it that way.
155 #ifdef TRANSACTION_BASED
156 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN;
158 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE;
160 ret = dbenv->open(dbenv, "./data", flags, 0);
162 lprintf(1, "dbenv->open: %s\n", db_strerror(ret));
163 dbenv->close(dbenv, 0);
167 lprintf(7, "Starting up DB\n");
169 for (i = 0; i < MAXCDB; ++i) {
171 /* Create a database handle */
172 ret = db_create(&dbp[i], dbenv, 0);
174 lprintf(1, "db_create: %s\n", db_strerror(ret));
179 /* Arbitrary names for our tables -- we reference them by
180 * number, so we don't have string names for them.
182 sprintf(dbfilename, "cdb.%02x", i);
184 ret = dbp[i]->open(dbp[i],
191 lprintf(1, "db_open[%d]: %s\n", i, db_strerror(ret));
197 CtdlRegisterSessionHook(cdb_allocate_ssd, EVT_START);
198 #ifdef TRANSACTION_BASED
199 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
205 * Close all of the gdbm database files we've opened. This can be done
206 * in a loop, since it's just a bunch of closes.
208 void close_databases(void)
213 begin_critical_section(S_DATABASE);
214 for (a = 0; a < MAXCDB; ++a) {
215 lprintf(7, "Closing database %d\n", a);
216 ret = dbp[a]->close(dbp[a], 0);
218 lprintf(1, "db_close: %s\n", db_strerror(ret));
225 /* Close the handle. */
226 ret = dbenv->close(dbenv, 0);
228 lprintf(1, "DBENV->close: %s\n", db_strerror(ret));
232 end_critical_section(S_DATABASE);
238 * Store a piece of data. Returns 0 if the operation was successful. If a
239 * key already exists it should be overwritten.
241 int cdb_store(int cdb,
242 void *ckey, int ckeylen,
243 void *cdata, int cdatalen)
249 memset(&dkey, 0, sizeof(DBT));
250 memset(&ddata, 0, sizeof(DBT));
253 ddata.size = cdatalen;
256 begin_critical_section(S_DATABASE);
257 ret = dbp[cdb]->put(dbp[cdb], /* db */
258 MYTID, /* transaction ID */
262 end_critical_section(S_DATABASE);
264 lprintf(1, "cdb_store: %s\n", db_strerror(ret));
272 * Delete a piece of data. Returns 0 if the operation was successful.
274 int cdb_delete(int cdb, void *key, int keylen)
283 begin_critical_section(S_DATABASE);
284 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
285 end_critical_section(S_DATABASE);
294 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
295 * a struct cdbdata which it is the caller's responsibility to free later on
296 * using the cdb_free() routine.
298 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
301 struct cdbdata *tempcdb;
305 memset(&dkey, 0, sizeof(DBT));
306 memset(&dret, 0, sizeof(DBT));
309 dret.flags = DB_DBT_MALLOC;
311 begin_critical_section(S_DATABASE);
312 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
313 end_critical_section(S_DATABASE);
314 if ((ret != 0) && (ret != DB_NOTFOUND)) {
315 lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
317 if (ret != 0) return NULL;
318 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
319 if (tempcdb == NULL) {
320 lprintf(2, "Cannot allocate memory!\n");
322 tempcdb->len = dret.size;
323 tempcdb->ptr = dret.data;
329 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
330 * more complex stuff with other database managers in the future).
332 void cdb_free(struct cdbdata *cdb)
340 * Prepare for a sequential search of an entire database.
341 * (There is guaranteed to be no more than one traversal in
342 * progress per session at any given time.)
344 void cdb_rewind(int cdb)
349 * Now initialize the cursor
351 begin_critical_section(S_DATABASE);
352 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
354 lprintf(1, "db_cursor: %s\n", db_strerror(ret));
356 end_critical_section(S_DATABASE);
361 * Fetch the next item in a sequential search. Returns a pointer to a
362 * cdbdata structure, or NULL if we've hit the end.
364 struct cdbdata *cdb_next_item(int cdb)
367 struct cdbdata *cdbret;
370 /* Initialize the key/data pair so the flags aren't set. */
371 memset(&key, 0, sizeof(key));
372 memset(&data, 0, sizeof(data));
373 data.flags = DB_DBT_MALLOC;
375 begin_critical_section(S_DATABASE);
376 ret = MYCURSOR->c_get(MYCURSOR,
377 &key, &data, DB_NEXT);
378 end_critical_section(S_DATABASE);
380 if (ret) return NULL; /* presumably, end of file */
382 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
383 cdbret->len = data.size;
384 cdbret->ptr = data.data;
391 * Transaction-based stuff. I'm writing this as I bake cookies...
394 void cdb_begin_transaction(void) {
396 #ifdef TRANSACTION_BASED
397 txn_begin(dbenv, NULL, &MYTID, 0);
403 void cdb_end_transaction(void) {
404 #ifdef TRANSACTION_BASED
405 txn_commit(MYTID, 0);