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_DB3_DB_H)
40 #elif defined(HAVE_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"
52 #include "sysdep_decls.h"
54 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
55 static DB_ENV *dbenv; /* The DB environment (global) */
57 struct cdbtsd { /* Thread-specific DB stuff */
58 DB_TXN *tid; /* Transaction handle */
59 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
62 static pthread_key_t tsdkey;
64 #define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
65 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
67 /* just a little helper function */
68 static void txabort(DB_TXN *tid) {
69 int ret = txn_abort(tid);
72 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
77 /* this one is even more helpful than the last. */
78 static void txcommit(DB_TXN *tid) {
79 int ret = txn_commit(tid, 0);
82 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
87 /* are you sensing a pattern yet? */
88 static void txbegin(DB_TXN **tid) {
89 int ret = txn_begin(dbenv, NULL, tid, 0);
92 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
97 static void cclose(DBC *cursor) {
100 if ((ret = cursor->c_close(cursor))) {
101 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
106 static void bailIfCursor(DBC **cursors, const char *msg)
110 for (i = 0; i < MAXCDB; i++)
111 if (cursors[i] != NULL)
113 lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
118 static void check_handles(void *arg) {
120 struct cdbtsd *tsd = (struct cdbtsd *)arg;
122 bailIfCursor(tsd->cursors, "in check_handles");
124 if (tsd->tid != NULL) {
125 lprintf(1, "cdb_*: transaction still in progress!");
131 static void dest_tsd(void *arg) {
139 * Ensure that we have a key for thread-specific data. We don't
140 * put anything in here that Citadel cares about; this is just database
141 * related stuff like cursors and transactions.
143 * This should be called immediately after startup by any thread which wants
144 * to use database calls, except for whatever thread calls open_databases.
146 void cdb_allocate_tsd(void) {
149 if (pthread_getspecific(tsdkey) != NULL)
152 tsd = mallok(sizeof *tsd);
156 memset(tsd->cursors, 0, sizeof tsd->cursors);
157 pthread_setspecific(tsdkey, tsd);
160 void cdb_free_tsd(void) {
161 dest_tsd(pthread_getspecific(tsdkey));
162 pthread_setspecific(tsdkey, NULL);
165 void cdb_check_handles(void) {
166 check_handles(pthread_getspecific(tsdkey));
171 * Reclaim unused space in the databases. We need to do each one of
172 * these discretely, rather than in a loop.
174 * This is a stub function in the Sleepycat DB backend, because there is no
175 * such API call available.
177 void defrag_databases(void)
184 * Cull the database logs
186 static void cdb_cull_logs(void) {
191 lprintf(5, "Database log file cull started.\n");
195 /* Get the list of names. */
196 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
197 if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
198 #elif DB_VERSION_MAJOR >= 4
199 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
201 if ((ret = log_archive(dbenv, &list, flags)) != 0) {
203 lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
207 /* Print the list of names. */
209 for (file = list; *file != NULL; ++file) {
210 lprintf(9, "Deleting log: %s\n", *file);
216 lprintf(5, "Database log file cull ended.\n");
221 * Request a checkpoint of the database.
223 static void cdb_checkpoint(void) {
225 static time_t last_cull = 0L;
227 #if DB_VERSION_MAJOR >= 4
228 ret = dbenv->txn_checkpoint(dbenv,
230 ret = txn_checkpoint(dbenv,
232 MAX_CHECKPOINT_KBYTES,
233 MAX_CHECKPOINT_MINUTES,
235 if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
236 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
240 if (ret == DB_INCOMPLETE) {
241 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
244 /* Cull the logs if we haven't done so for 24 hours */
245 if ((time(NULL) - last_cull) > 86400L) {
246 last_cull = time(NULL);
253 * Open the various databases we'll be using. Any database which
254 * does not exist should be created. Note that we don't need an S_DATABASE
255 * critical section here, because there aren't any active threads manipulating
256 * the database yet -- and besides, it causes problems on BSDI.
258 void open_databases(void)
262 char dbfilename[SIZ];
265 lprintf(9, "cdb_*: open_databases() starting\n");
267 * Silently try to create the database subdirectory. If it's
268 * already there, no problem.
270 system("exec mkdir data 2>/dev/null");
272 lprintf(9, "cdb_*: Setting up DB environment\n");
273 db_env_set_func_yield(sched_yield);
274 ret = db_env_create(&dbenv, 0);
276 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
279 dbenv->set_errpfx(dbenv, "citserver");
282 * We want to specify the shared memory buffer pool cachesize,
283 * but everything else is the default.
285 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
287 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
288 dbenv->close(dbenv, 0);
292 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
293 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
294 dbenv->close(dbenv, 0);
298 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
299 DB_INIT_LOCK|DB_THREAD;
300 ret = dbenv->open(dbenv, "./data", flags, 0);
302 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
303 dbenv->close(dbenv, 0);
307 lprintf(7, "cdb_*: Starting up DB\n");
309 for (i = 0; i < MAXCDB; ++i) {
311 /* Create a database handle */
312 ret = db_create(&dbp[i], dbenv, 0);
314 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
319 /* Arbitrary names for our tables -- we reference them by
320 * number, so we don't have string names for them.
322 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
324 ret = dbp[i]->open(dbp[i],
331 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
336 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
337 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
342 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
343 lprintf(9, "cdb_*: open_databases() finished\n");
348 * Close all of the db database files we've opened. This can be done
349 * in a loop, since it's just a bunch of closes.
351 void close_databases(void)
358 #if DB_VERSION_MAJOR >= 4
359 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
361 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
363 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
367 for (a = 0; a < MAXCDB; ++a) {
368 lprintf(7, "cdb_*: Closing database %d\n", a);
369 ret = dbp[a]->close(dbp[a], 0);
371 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
377 /* Close the handle. */
378 ret = dbenv->close(dbenv, 0);
380 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
386 * Store a piece of data. Returns 0 if the operation was successful. If a
387 * key already exists it should be overwritten.
389 int cdb_store(int cdb,
390 void *ckey, int ckeylen,
391 void *cdata, int cdatalen)
398 memset(&dkey, 0, sizeof(DBT));
399 memset(&ddata, 0, sizeof(DBT));
402 ddata.size = cdatalen;
407 ret = dbp[cdb]->put(dbp[cdb], /* db */
408 MYTID, /* transaction ID */
414 lprintf(1, "cdb_store(%d): %s\n", cdb,
423 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
428 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
429 tid, /* transaction ID */
434 if (ret == DB_LOCK_DEADLOCK)
441 lprintf(1, "cdb_store(%d): %s\n", cdb,
456 * Delete a piece of data. Returns 0 if the operation was successful.
458 int cdb_delete(int cdb, void *key, int keylen)
465 memset(&dkey, 0, sizeof dkey);
471 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
474 lprintf(1, "cdb_delete(%d): %s\n", cdb,
476 if (ret != DB_NOTFOUND)
482 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
487 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
488 && ret != DB_NOTFOUND)
490 if (ret == DB_LOCK_DEADLOCK)
497 lprintf(1, "cdb_delete(%d): %s\n", cdb,
510 static DBC *localcursor(int cdb)
515 if (MYCURSORS[cdb] == NULL)
516 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
518 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
522 lprintf(1, "localcursor: %s\n", db_strerror(ret));
531 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
532 * a struct cdbdata which it is the caller's responsibility to free later on
533 * using the cdb_free() routine.
535 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
538 struct cdbdata *tempcdb;
542 memset(&dkey, 0, sizeof(DBT));
548 memset(&dret, 0, sizeof(DBT));
549 dret.flags = DB_DBT_MALLOC;
550 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
558 memset(&dret, 0, sizeof(DBT));
559 dret.flags = DB_DBT_MALLOC;
561 curs = localcursor(cdb);
563 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
566 while (ret == DB_LOCK_DEADLOCK);
570 if ((ret != 0) && (ret != DB_NOTFOUND))
572 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
576 if (ret != 0) return NULL;
577 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
581 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
585 tempcdb->len = dret.size;
586 tempcdb->ptr = dret.data;
592 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
593 * more complex stuff with other database managers in the future).
595 void cdb_free(struct cdbdata *cdb)
601 void cdb_close_cursor(int cdb)
603 if (MYCURSORS[cdb] != NULL)
604 cclose(MYCURSORS[cdb]);
606 MYCURSORS[cdb] = NULL;
610 * Prepare for a sequential search of an entire database.
611 * (There is guaranteed to be no more than one traversal in
612 * progress per thread at any given time.)
614 void cdb_rewind(int cdb)
618 if (MYCURSORS[cdb] != NULL)
619 cclose(MYCURSORS[cdb]);
622 * Now initialize the cursor
624 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
626 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
633 * Fetch the next item in a sequential search. Returns a pointer to a
634 * cdbdata structure, or NULL if we've hit the end.
636 struct cdbdata *cdb_next_item(int cdb)
639 struct cdbdata *cdbret;
642 /* Initialize the key/data pair so the flags aren't set. */
643 memset(&key, 0, sizeof(key));
644 memset(&data, 0, sizeof(data));
645 data.flags = DB_DBT_MALLOC;
647 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
648 &key, &data, DB_NEXT);
651 if (ret != DB_NOTFOUND) {
652 lprintf(1, "cdb_next_item(%d): %s\n",
653 cdb, db_strerror(ret));
656 cclose(MYCURSORS[cdb]);
657 MYCURSORS[cdb] = NULL;
658 return NULL; /* presumably, end of file */
661 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
662 cdbret->len = data.size;
663 cdbret->ptr = data.data;
671 * Transaction-based stuff. I'm writing this as I bake cookies...
674 void cdb_begin_transaction(void) {
676 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
680 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
687 void cdb_end_transaction(void) {
690 for (i = 0; i < MAXCDB; i++)
691 if (MYCURSORS[i] != NULL) {
692 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
693 cclose(MYCURSORS[i]);
699 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
709 * Truncate (delete every record)
711 void cdb_trunc(int cdb)
719 ret = dbp[cdb]->truncate(dbp[cdb], /* db */
720 MYTID, /* transaction ID */
721 &count, /* #rows that were deleted */
725 lprintf(1, "cdb_truncate(%d): %s\n", cdb,
732 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
737 if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
738 tid, /* transaction ID */
739 &count, /* #rows deleted */
742 if (ret == DB_LOCK_DEADLOCK)
749 lprintf(1, "cdb_truncate(%d): %s\n", cdb,