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>
38 #elif defined(HAVE_DB4_DB_H)
40 #elif defined(HAVE_DB3_DB_H)
43 #error Neither <db.h> nor <db3/db.h> was found by configure. Install db3-devel.
49 #include "dynloader.h"
50 #include "citserver.h"
53 #include "sysdep_decls.h"
55 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
56 static DB_ENV *dbenv; /* The DB environment (global) */
58 struct cdbtsd { /* Thread-specific DB stuff */
59 DB_TXN *tid; /* Transaction handle */
60 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
63 static pthread_key_t tsdkey;
65 #define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
66 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
68 /* just a little helper function */
69 static void txabort(DB_TXN *tid) {
70 int ret = txn_abort(tid);
73 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
78 /* this one is even more helpful than the last. */
79 static void txcommit(DB_TXN *tid) {
80 int ret = txn_commit(tid, 0);
83 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
88 /* are you sensing a pattern yet? */
89 static void txbegin(DB_TXN **tid) {
90 int ret = txn_begin(dbenv, NULL, tid, 0);
93 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
98 static void cclose(DBC *cursor) {
101 if ((ret = cursor->c_close(cursor))) {
102 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
107 static void bailIfCursor(DBC **cursors, const char *msg)
111 for (i = 0; i < MAXCDB; i++)
112 if (cursors[i] != NULL)
114 lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
119 static void check_handles(void *arg) {
121 struct cdbtsd *tsd = (struct cdbtsd *)arg;
123 bailIfCursor(tsd->cursors, "in check_handles");
125 if (tsd->tid != NULL) {
126 lprintf(1, "cdb_*: transaction still in progress!");
132 static void dest_tsd(void *arg) {
140 * Ensure that we have a key for thread-specific data. We don't
141 * put anything in here that Citadel cares about; this is just database
142 * related stuff like cursors and transactions.
144 * This should be called immediately after startup by any thread which wants
145 * to use database calls, except for whatever thread calls open_databases.
147 void cdb_allocate_tsd(void) {
150 if (pthread_getspecific(tsdkey) != NULL)
153 tsd = mallok(sizeof *tsd);
157 memset(tsd->cursors, 0, sizeof tsd->cursors);
158 pthread_setspecific(tsdkey, tsd);
161 void cdb_free_tsd(void) {
162 dest_tsd(pthread_getspecific(tsdkey));
163 pthread_setspecific(tsdkey, NULL);
166 void cdb_check_handles(void) {
167 check_handles(pthread_getspecific(tsdkey));
172 * Reclaim unused space in the databases. We need to do each one of
173 * these discretely, rather than in a loop.
175 * This is a stub function in the Sleepycat DB backend, because there is no
176 * such API call available.
178 void defrag_databases(void)
185 * Cull the database logs
187 static void cdb_cull_logs(void) {
193 lprintf(5, "Database log file cull started.\n");
197 /* Get the list of names. */
198 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
199 if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
200 #elif DB_VERSION_MAJOR >= 4
201 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
203 if ((ret = log_archive(dbenv, &list, flags)) != 0) {
205 lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
209 /* Print the list of names. */
211 for (file = list; *file != NULL; ++file) {
212 lprintf(9, "Deleting log: %s\n", *file);
215 snprintf(errmsg, sizeof(errmsg),
216 " ** ERROR **\n \n \n "
217 "Citadel was unable to delete the "
218 "database log file '%s' because of the "
219 "following error:\n \n %s\n \n"
220 " This log file is no longer in use "
221 "and may be safely deleted.\n",
224 aide_message(errmsg);
230 lprintf(5, "Database log file cull ended.\n");
235 * Request a checkpoint of the database.
237 static void cdb_checkpoint(void) {
239 static time_t last_cull = 0L;
241 #if DB_VERSION_MAJOR >= 4
242 ret = dbenv->txn_checkpoint(dbenv,
244 ret = txn_checkpoint(dbenv,
246 MAX_CHECKPOINT_KBYTES,
247 MAX_CHECKPOINT_MINUTES,
249 if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
250 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
254 if (ret == DB_INCOMPLETE) {
255 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
258 /* Cull the logs if we haven't done so for 24 hours */
259 if ((time(NULL) - last_cull) > 86400L) {
260 last_cull = time(NULL);
267 * Open the various databases we'll be using. Any database which
268 * does not exist should be created. Note that we don't need an S_DATABASE
269 * critical section here, because there aren't any active threads manipulating
270 * the database yet -- and besides, it causes problems on BSDI.
272 void open_databases(void)
276 char dbfilename[SIZ];
279 lprintf(9, "cdb_*: open_databases() starting\n");
281 * Silently try to create the database subdirectory. If it's
282 * already there, no problem.
284 system("exec mkdir data 2>/dev/null");
286 lprintf(9, "cdb_*: Setting up DB environment\n");
287 db_env_set_func_yield(sched_yield);
288 ret = db_env_create(&dbenv, 0);
290 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
293 dbenv->set_errpfx(dbenv, "citserver");
296 * We want to specify the shared memory buffer pool cachesize,
297 * but everything else is the default.
299 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
301 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
302 dbenv->close(dbenv, 0);
306 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
307 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
308 dbenv->close(dbenv, 0);
312 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
313 DB_INIT_LOCK|DB_THREAD;
314 ret = dbenv->open(dbenv, "./data", flags, 0);
316 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
317 dbenv->close(dbenv, 0);
321 lprintf(7, "cdb_*: Starting up DB\n");
323 for (i = 0; i < MAXCDB; ++i) {
325 /* Create a database handle */
326 ret = db_create(&dbp[i], dbenv, 0);
328 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
333 /* Arbitrary names for our tables -- we reference them by
334 * number, so we don't have string names for them.
336 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
338 ret = dbp[i]->open(dbp[i],
345 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
350 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
351 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
356 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
357 lprintf(9, "cdb_*: open_databases() finished\n");
362 * Close all of the db database files we've opened. This can be done
363 * in a loop, since it's just a bunch of closes.
365 void close_databases(void)
372 #if DB_VERSION_MAJOR >= 4
373 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
375 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
377 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
381 for (a = 0; a < MAXCDB; ++a) {
382 lprintf(7, "cdb_*: Closing database %d\n", a);
383 ret = dbp[a]->close(dbp[a], 0);
385 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
391 /* Close the handle. */
392 ret = dbenv->close(dbenv, 0);
394 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
400 * Store a piece of data. Returns 0 if the operation was successful. If a
401 * key already exists it should be overwritten.
403 int cdb_store(int cdb,
404 void *ckey, int ckeylen,
405 void *cdata, int cdatalen)
412 memset(&dkey, 0, sizeof(DBT));
413 memset(&ddata, 0, sizeof(DBT));
416 ddata.size = cdatalen;
421 ret = dbp[cdb]->put(dbp[cdb], /* db */
422 MYTID, /* transaction ID */
428 lprintf(1, "cdb_store(%d): %s\n", cdb,
437 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
442 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
443 tid, /* transaction ID */
448 if (ret == DB_LOCK_DEADLOCK)
455 lprintf(1, "cdb_store(%d): %s\n", cdb,
470 * Delete a piece of data. Returns 0 if the operation was successful.
472 int cdb_delete(int cdb, void *key, int keylen)
479 memset(&dkey, 0, sizeof dkey);
485 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
488 lprintf(1, "cdb_delete(%d): %s\n", cdb,
490 if (ret != DB_NOTFOUND)
496 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
501 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
502 && ret != DB_NOTFOUND)
504 if (ret == DB_LOCK_DEADLOCK)
511 lprintf(1, "cdb_delete(%d): %s\n", cdb,
524 static DBC *localcursor(int cdb)
529 if (MYCURSORS[cdb] == NULL)
530 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
532 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
536 lprintf(1, "localcursor: %s\n", db_strerror(ret));
545 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
546 * a struct cdbdata which it is the caller's responsibility to free later on
547 * using the cdb_free() routine.
549 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
552 struct cdbdata *tempcdb;
556 memset(&dkey, 0, sizeof(DBT));
562 memset(&dret, 0, sizeof(DBT));
563 dret.flags = DB_DBT_MALLOC;
564 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
572 memset(&dret, 0, sizeof(DBT));
573 dret.flags = DB_DBT_MALLOC;
575 curs = localcursor(cdb);
577 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
580 while (ret == DB_LOCK_DEADLOCK);
584 if ((ret != 0) && (ret != DB_NOTFOUND))
586 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
590 if (ret != 0) return NULL;
591 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
595 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
599 tempcdb->len = dret.size;
600 tempcdb->ptr = dret.data;
606 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
607 * more complex stuff with other database managers in the future).
609 void cdb_free(struct cdbdata *cdb)
615 void cdb_close_cursor(int cdb)
617 if (MYCURSORS[cdb] != NULL)
618 cclose(MYCURSORS[cdb]);
620 MYCURSORS[cdb] = NULL;
624 * Prepare for a sequential search of an entire database.
625 * (There is guaranteed to be no more than one traversal in
626 * progress per thread at any given time.)
628 void cdb_rewind(int cdb)
632 if (MYCURSORS[cdb] != NULL)
633 cclose(MYCURSORS[cdb]);
636 * Now initialize the cursor
638 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
640 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
647 * Fetch the next item in a sequential search. Returns a pointer to a
648 * cdbdata structure, or NULL if we've hit the end.
650 struct cdbdata *cdb_next_item(int cdb)
653 struct cdbdata *cdbret;
656 /* Initialize the key/data pair so the flags aren't set. */
657 memset(&key, 0, sizeof(key));
658 memset(&data, 0, sizeof(data));
659 data.flags = DB_DBT_MALLOC;
661 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
662 &key, &data, DB_NEXT);
665 if (ret != DB_NOTFOUND) {
666 lprintf(1, "cdb_next_item(%d): %s\n",
667 cdb, db_strerror(ret));
670 cclose(MYCURSORS[cdb]);
671 MYCURSORS[cdb] = NULL;
672 return NULL; /* presumably, end of file */
675 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
676 cdbret->len = data.size;
677 cdbret->ptr = data.data;
685 * Transaction-based stuff. I'm writing this as I bake cookies...
688 void cdb_begin_transaction(void) {
690 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
694 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
701 void cdb_end_transaction(void) {
704 for (i = 0; i < MAXCDB; i++)
705 if (MYCURSORS[i] != NULL) {
706 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
707 cclose(MYCURSORS[i]);
713 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
723 * Truncate (delete every record)
725 void cdb_trunc(int cdb)
729 #if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 2
735 lprintf(1, "cdb_trunc must not be called in a transaction.\n");
740 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
742 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
747 /* Initialize the key/data pair so the flags aren't set. */
748 memset(&key, 0, sizeof(key));
749 memset(&data, 0, sizeof(data));
753 ret = dbp[cdb]->cursor(dbp[cdb], tid, &MYCURSORS[cdb], 0);
756 lprintf(1, "cdb_trunc: db_cursor: %s\n", db_strerror(ret));
760 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
761 &key, &data, DB_NEXT);
764 cclose(MYCURSORS[cdb]);
766 if (ret == DB_LOCK_DEADLOCK)
769 if (ret == DB_NOTFOUND)
772 lprintf(1, "cdb_trunc: c_get: %s\n", db_strerror(ret));
776 ret = MYCURSORS[cdb]->c_del(MYCURSORS[cdb], 0);
779 cclose(MYCURSORS[cdb]);
781 if (ret == DB_LOCK_DEADLOCK)
784 lprintf(1, "cdb_trunc: c_del: %s\n", db_strerror(ret));
788 cclose(MYCURSORS[cdb]);
795 if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
796 tid, /* transaction ID */
797 &count, /* #rows deleted */
800 if (ret == DB_LOCK_DEADLOCK)
807 lprintf(1, "cdb_truncate(%d): %s\n", cdb,