4 * Sleepycat (Berkeley) DB driver for Citadel/UX
8 /*****************************************************************************
9 Tunable configuration parameters for the Sleepycat DB back end
10 *****************************************************************************/
12 /* Citadel will checkpoint the db at the end of every session, but only if
13 * the specified number of kilobytes has been written, or if the specified
14 * number of minutes has passed, since the last checkpoint.
16 #define MAX_CHECKPOINT_KBYTES 256
17 #define MAX_CHECKPOINT_MINUTES 15
19 /*****************************************************************************/
32 #include <sys/types.h>
39 #include "dynloader.h"
40 #include "citserver.h"
42 #include "sysdep_decls.h"
44 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
45 static DB_ENV *dbenv; /* The DB environment (global) */
47 struct cdbtsd { /* Thread-specific DB stuff */
48 DB_TXN *tid; /* Transaction handle */
49 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
52 static pthread_key_t tsdkey;
54 #define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
55 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
57 /* just a little helper function */
58 static void txabort(DB_TXN *tid) {
59 int ret = txn_abort(tid);
62 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
67 /* this one is even more helpful than the last. */
68 static void txcommit(DB_TXN *tid) {
69 int ret = txn_commit(tid, 0);
72 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
77 /* are you sensing a pattern yet? */
78 static void txbegin(DB_TXN **tid) {
79 int ret = txn_begin(dbenv, NULL, tid, 0);
82 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
87 static void cclose(DBC *cursor) {
90 if ((ret = cursor->c_close(cursor))) {
91 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
96 static void bailIfCursor(DBC **cursors, const char *msg)
100 for (i = 0; i < MAXCDB; i++)
101 if (cursors[i] != NULL)
103 lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
108 static void check_handles(void *arg) {
110 struct cdbtsd *tsd = (struct cdbtsd *)arg;
112 bailIfCursor(tsd->cursors, "in check_handles");
114 if (tsd->tid != NULL) {
115 lprintf(1, "cdb_*: transaction still in progress!");
121 static void dest_tsd(void *arg) {
129 * Ensure that we have a key for thread-specific data. We don't
130 * put anything in here that Citadel cares about; this is just database
131 * related stuff like cursors and transactions.
133 * This should be called immediately after startup by any thread which wants
134 * to use database calls, except for whatever thread calls open_databases.
136 void cdb_allocate_tsd(void) {
139 if (pthread_getspecific(tsdkey) != NULL)
142 tsd = mallok(sizeof *tsd);
146 memset(tsd->cursors, 0, sizeof tsd->cursors);
147 pthread_setspecific(tsdkey, tsd);
150 void cdb_free_tsd(void) {
151 dest_tsd(pthread_getspecific(tsdkey));
152 pthread_setspecific(tsdkey, NULL);
155 void cdb_check_handles(void) {
156 check_handles(pthread_getspecific(tsdkey));
161 * Reclaim unused space in the databases. We need to do each one of
162 * these discretely, rather than in a loop.
164 * This is a stub function in the Sleepycat DB backend, because there is no
165 * such API call available.
167 void defrag_databases(void)
174 * Cull the database logs
176 static void cdb_cull_logs(void) {
182 lprintf(5, "Database log file cull started.\n");
184 dp = opendir("data");
185 if (dp == NULL) return;
187 while (d = readdir(dp), d != NULL) {
188 if (!strncasecmp(d->d_name, "log.", 4)) {
189 sprintf(filename, "./data/%s", d->d_name);
190 stat(filename, &statbuf);
191 if ((time(NULL) - statbuf.st_mtime) > 432000L) {
192 lprintf(5, "%s ... deleted\n", filename);
196 lprintf(5, "%s ... kept\n", filename);
203 lprintf(5, "Database log file cull ended.\n");
208 * Request a checkpoint of the database.
210 static void cdb_checkpoint(void) {
212 static time_t last_cull = 0L;
214 ret = txn_checkpoint(dbenv,
215 MAX_CHECKPOINT_KBYTES,
216 MAX_CHECKPOINT_MINUTES,
218 if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
219 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
223 if (ret == DB_INCOMPLETE) {
224 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
227 /* Cull the logs if we haven't done so for 24 hours */
228 if ((time(NULL) - last_cull) > 86400L) {
229 last_cull = time(NULL);
236 * Open the various databases we'll be using. Any database which
237 * does not exist should be created. Note that we don't need an S_DATABASE
238 * critical section here, because there aren't any active threads manipulating
239 * the database yet -- and besides, it causes problems on BSDI.
241 void open_databases(void)
245 char dbfilename[SIZ];
248 lprintf(9, "cdb_*: open_databases() starting\n");
250 * Silently try to create the database subdirectory. If it's
251 * already there, no problem.
253 system("exec mkdir data 2>/dev/null");
255 lprintf(9, "cdb_*: Setting up DB environment\n");
256 db_env_set_func_yield(sched_yield);
257 ret = db_env_create(&dbenv, 0);
259 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
262 dbenv->set_errpfx(dbenv, "citserver");
265 * We want to specify the shared memory buffer pool cachesize,
266 * but everything else is the default.
268 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
270 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
271 dbenv->close(dbenv, 0);
275 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
276 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
277 dbenv->close(dbenv, 0);
281 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
282 DB_INIT_LOCK|DB_THREAD;
283 ret = dbenv->open(dbenv, "./data", flags, 0);
285 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
286 dbenv->close(dbenv, 0);
290 lprintf(7, "cdb_*: Starting up DB\n");
292 for (i = 0; i < MAXCDB; ++i) {
294 /* Create a database handle */
295 ret = db_create(&dbp[i], dbenv, 0);
297 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
302 /* Arbitrary names for our tables -- we reference them by
303 * number, so we don't have string names for them.
305 sprintf(dbfilename, "cdb.%02x", i);
307 ret = dbp[i]->open(dbp[i],
314 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
319 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
320 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
325 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
326 lprintf(9, "cdb_*: open_databases() finished\n");
331 * Close all of the db database files we've opened. This can be done
332 * in a loop, since it's just a bunch of closes.
334 void close_databases(void)
341 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
342 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
346 for (a = 0; a < MAXCDB; ++a) {
347 lprintf(7, "cdb_*: Closing database %d\n", a);
348 ret = dbp[a]->close(dbp[a], 0);
350 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
356 /* Close the handle. */
357 ret = dbenv->close(dbenv, 0);
359 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
365 * Store a piece of data. Returns 0 if the operation was successful. If a
366 * key already exists it should be overwritten.
368 int cdb_store(int cdb,
369 void *ckey, int ckeylen,
370 void *cdata, int cdatalen)
377 memset(&dkey, 0, sizeof(DBT));
378 memset(&ddata, 0, sizeof(DBT));
381 ddata.size = cdatalen;
386 ret = dbp[cdb]->put(dbp[cdb], /* db */
387 MYTID, /* transaction ID */
393 lprintf(1, "cdb_store(%d): %s\n", cdb,
402 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
407 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
408 tid, /* transaction ID */
413 if (ret == DB_LOCK_DEADLOCK)
420 lprintf(1, "cdb_store(%d): %s\n", cdb,
435 * Delete a piece of data. Returns 0 if the operation was successful.
437 int cdb_delete(int cdb, void *key, int keylen)
444 memset(&dkey, 0, sizeof dkey);
450 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
453 lprintf(1, "cdb_delete(%d): %s\n", cdb,
455 if (ret != DB_NOTFOUND)
461 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
466 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
467 && ret != DB_NOTFOUND)
469 if (ret == DB_LOCK_DEADLOCK)
476 lprintf(1, "cdb_delete(%d): %s\n", cdb,
489 static DBC *localcursor(int cdb)
494 if (MYCURSORS[cdb] == NULL)
495 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
497 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
501 lprintf(1, "localcursor: %s\n", db_strerror(ret));
510 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
511 * a struct cdbdata which it is the caller's responsibility to free later on
512 * using the cdb_free() routine.
514 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
517 struct cdbdata *tempcdb;
521 memset(&dkey, 0, sizeof(DBT));
527 memset(&dret, 0, sizeof(DBT));
528 dret.flags = DB_DBT_MALLOC;
529 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
537 memset(&dret, 0, sizeof(DBT));
538 dret.flags = DB_DBT_MALLOC;
540 curs = localcursor(cdb);
542 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
545 while (ret == DB_LOCK_DEADLOCK);
549 if ((ret != 0) && (ret != DB_NOTFOUND))
551 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
555 if (ret != 0) return NULL;
556 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
560 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
564 tempcdb->len = dret.size;
565 tempcdb->ptr = dret.data;
571 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
572 * more complex stuff with other database managers in the future).
574 void cdb_free(struct cdbdata *cdb)
580 void cdb_close_cursor(int cdb)
582 if (MYCURSORS[cdb] != NULL)
583 cclose(MYCURSORS[cdb]);
585 MYCURSORS[cdb] = NULL;
589 * Prepare for a sequential search of an entire database.
590 * (There is guaranteed to be no more than one traversal in
591 * progress per thread at any given time.)
593 void cdb_rewind(int cdb)
597 if (MYCURSORS[cdb] != NULL)
598 cclose(MYCURSORS[cdb]);
601 * Now initialize the cursor
603 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
605 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
612 * Fetch the next item in a sequential search. Returns a pointer to a
613 * cdbdata structure, or NULL if we've hit the end.
615 struct cdbdata *cdb_next_item(int cdb)
618 struct cdbdata *cdbret;
621 /* Initialize the key/data pair so the flags aren't set. */
622 memset(&key, 0, sizeof(key));
623 memset(&data, 0, sizeof(data));
624 data.flags = DB_DBT_MALLOC;
626 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
627 &key, &data, DB_NEXT);
630 if (ret != DB_NOTFOUND) {
631 lprintf(1, "cdb_next_item(%d): %s\n",
632 cdb, db_strerror(ret));
635 cclose(MYCURSORS[cdb]);
636 MYCURSORS[cdb] = NULL;
637 return NULL; /* presumably, end of file */
640 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
641 cdbret->len = data.size;
642 cdbret->ptr = data.data;
649 * Transaction-based stuff. I'm writing this as I bake cookies...
652 void cdb_begin_transaction(void) {
654 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
658 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
665 void cdb_end_transaction(void) {
668 for (i = 0; i < MAXCDB; i++)
669 if (MYCURSORS[i] != NULL) {
670 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
671 cclose(MYCURSORS[i]);
677 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");