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)
41 #error Neither <db.h> nor <db3/db.h> was found by configure. Install db3-devel.
47 #include "dynloader.h"
48 #include "citserver.h"
50 #include "sysdep_decls.h"
52 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
53 static DB_ENV *dbenv; /* The DB environment (global) */
55 struct cdbtsd { /* Thread-specific DB stuff */
56 DB_TXN *tid; /* Transaction handle */
57 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
60 static pthread_key_t tsdkey;
62 #define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
63 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
65 /* just a little helper function */
66 static void txabort(DB_TXN *tid) {
67 int ret = txn_abort(tid);
70 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
75 /* this one is even more helpful than the last. */
76 static void txcommit(DB_TXN *tid) {
77 int ret = txn_commit(tid, 0);
80 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
85 /* are you sensing a pattern yet? */
86 static void txbegin(DB_TXN **tid) {
87 int ret = txn_begin(dbenv, NULL, tid, 0);
90 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
95 static void cclose(DBC *cursor) {
98 if ((ret = cursor->c_close(cursor))) {
99 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
104 static void bailIfCursor(DBC **cursors, const char *msg)
108 for (i = 0; i < MAXCDB; i++)
109 if (cursors[i] != NULL)
111 lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
116 static void check_handles(void *arg) {
118 struct cdbtsd *tsd = (struct cdbtsd *)arg;
120 bailIfCursor(tsd->cursors, "in check_handles");
122 if (tsd->tid != NULL) {
123 lprintf(1, "cdb_*: transaction still in progress!");
129 static void dest_tsd(void *arg) {
137 * Ensure that we have a key for thread-specific data. We don't
138 * put anything in here that Citadel cares about; this is just database
139 * related stuff like cursors and transactions.
141 * This should be called immediately after startup by any thread which wants
142 * to use database calls, except for whatever thread calls open_databases.
144 void cdb_allocate_tsd(void) {
147 if (pthread_getspecific(tsdkey) != NULL)
150 tsd = mallok(sizeof *tsd);
154 memset(tsd->cursors, 0, sizeof tsd->cursors);
155 pthread_setspecific(tsdkey, tsd);
158 void cdb_free_tsd(void) {
159 dest_tsd(pthread_getspecific(tsdkey));
160 pthread_setspecific(tsdkey, NULL);
163 void cdb_check_handles(void) {
164 check_handles(pthread_getspecific(tsdkey));
169 * Reclaim unused space in the databases. We need to do each one of
170 * these discretely, rather than in a loop.
172 * This is a stub function in the Sleepycat DB backend, because there is no
173 * such API call available.
175 void defrag_databases(void)
182 * Cull the database logs
184 static void cdb_cull_logs(void) {
189 lprintf(5, "Database log file cull started.\n");
193 /* Get the list of names. */
194 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
195 if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
196 #elif DB_VERSION_MAJOR >= 4
197 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
199 if ((ret = log_archive(dbenv, &list, flags)) != 0) {
201 lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
205 /* Print the list of names. */
207 for (file = list; *file != NULL; ++file) {
208 lprintf(9, "Deleting log: %s\n", *file);
214 lprintf(5, "Database log file cull ended.\n");
219 * Request a checkpoint of the database.
221 static void cdb_checkpoint(void) {
223 static time_t last_cull = 0L;
225 #if DB_VERSION_MAJOR >= 4
226 ret = dbenv->txn_checkpoint(dbenv,
228 ret = txn_checkpoint(dbenv,
230 MAX_CHECKPOINT_KBYTES,
231 MAX_CHECKPOINT_MINUTES,
233 if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
234 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
238 if (ret == DB_INCOMPLETE) {
239 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
242 /* Cull the logs if we haven't done so for 24 hours */
243 if ((time(NULL) - last_cull) > 86400L) {
244 last_cull = time(NULL);
251 * Open the various databases we'll be using. Any database which
252 * does not exist should be created. Note that we don't need an S_DATABASE
253 * critical section here, because there aren't any active threads manipulating
254 * the database yet -- and besides, it causes problems on BSDI.
256 void open_databases(void)
260 char dbfilename[SIZ];
263 lprintf(9, "cdb_*: open_databases() starting\n");
265 * Silently try to create the database subdirectory. If it's
266 * already there, no problem.
268 system("exec mkdir data 2>/dev/null");
270 lprintf(9, "cdb_*: Setting up DB environment\n");
271 db_env_set_func_yield(sched_yield);
272 ret = db_env_create(&dbenv, 0);
274 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
277 dbenv->set_errpfx(dbenv, "citserver");
280 * We want to specify the shared memory buffer pool cachesize,
281 * but everything else is the default.
283 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
285 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
286 dbenv->close(dbenv, 0);
290 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
291 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
292 dbenv->close(dbenv, 0);
296 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
297 DB_INIT_LOCK|DB_THREAD;
298 ret = dbenv->open(dbenv, "./data", flags, 0);
300 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
301 dbenv->close(dbenv, 0);
305 lprintf(7, "cdb_*: Starting up DB\n");
307 for (i = 0; i < MAXCDB; ++i) {
309 /* Create a database handle */
310 ret = db_create(&dbp[i], dbenv, 0);
312 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
317 /* Arbitrary names for our tables -- we reference them by
318 * number, so we don't have string names for them.
320 sprintf(dbfilename, "cdb.%02x", i);
322 ret = dbp[i]->open(dbp[i],
329 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
334 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
335 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
340 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
341 lprintf(9, "cdb_*: open_databases() finished\n");
346 * Close all of the db database files we've opened. This can be done
347 * in a loop, since it's just a bunch of closes.
349 void close_databases(void)
356 #if DB_VERSION_MAJOR >= 4
357 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
359 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
361 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
365 for (a = 0; a < MAXCDB; ++a) {
366 lprintf(7, "cdb_*: Closing database %d\n", a);
367 ret = dbp[a]->close(dbp[a], 0);
369 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
375 /* Close the handle. */
376 ret = dbenv->close(dbenv, 0);
378 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
384 * Store a piece of data. Returns 0 if the operation was successful. If a
385 * key already exists it should be overwritten.
387 int cdb_store(int cdb,
388 void *ckey, int ckeylen,
389 void *cdata, int cdatalen)
396 memset(&dkey, 0, sizeof(DBT));
397 memset(&ddata, 0, sizeof(DBT));
400 ddata.size = cdatalen;
405 ret = dbp[cdb]->put(dbp[cdb], /* db */
406 MYTID, /* transaction ID */
412 lprintf(1, "cdb_store(%d): %s\n", cdb,
421 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
426 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
427 tid, /* transaction ID */
432 if (ret == DB_LOCK_DEADLOCK)
439 lprintf(1, "cdb_store(%d): %s\n", cdb,
454 * Delete a piece of data. Returns 0 if the operation was successful.
456 int cdb_delete(int cdb, void *key, int keylen)
463 memset(&dkey, 0, sizeof dkey);
469 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
472 lprintf(1, "cdb_delete(%d): %s\n", cdb,
474 if (ret != DB_NOTFOUND)
480 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
485 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
486 && ret != DB_NOTFOUND)
488 if (ret == DB_LOCK_DEADLOCK)
495 lprintf(1, "cdb_delete(%d): %s\n", cdb,
508 static DBC *localcursor(int cdb)
513 if (MYCURSORS[cdb] == NULL)
514 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
516 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
520 lprintf(1, "localcursor: %s\n", db_strerror(ret));
529 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
530 * a struct cdbdata which it is the caller's responsibility to free later on
531 * using the cdb_free() routine.
533 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
536 struct cdbdata *tempcdb;
540 memset(&dkey, 0, sizeof(DBT));
546 memset(&dret, 0, sizeof(DBT));
547 dret.flags = DB_DBT_MALLOC;
548 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
556 memset(&dret, 0, sizeof(DBT));
557 dret.flags = DB_DBT_MALLOC;
559 curs = localcursor(cdb);
561 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
564 while (ret == DB_LOCK_DEADLOCK);
568 if ((ret != 0) && (ret != DB_NOTFOUND))
570 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
574 if (ret != 0) return NULL;
575 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
579 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
583 tempcdb->len = dret.size;
584 tempcdb->ptr = dret.data;
590 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
591 * more complex stuff with other database managers in the future).
593 void cdb_free(struct cdbdata *cdb)
599 void cdb_close_cursor(int cdb)
601 if (MYCURSORS[cdb] != NULL)
602 cclose(MYCURSORS[cdb]);
604 MYCURSORS[cdb] = NULL;
608 * Prepare for a sequential search of an entire database.
609 * (There is guaranteed to be no more than one traversal in
610 * progress per thread at any given time.)
612 void cdb_rewind(int cdb)
616 if (MYCURSORS[cdb] != NULL)
617 cclose(MYCURSORS[cdb]);
620 * Now initialize the cursor
622 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
624 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
631 * Fetch the next item in a sequential search. Returns a pointer to a
632 * cdbdata structure, or NULL if we've hit the end.
634 struct cdbdata *cdb_next_item(int cdb)
637 struct cdbdata *cdbret;
640 /* Initialize the key/data pair so the flags aren't set. */
641 memset(&key, 0, sizeof(key));
642 memset(&data, 0, sizeof(data));
643 data.flags = DB_DBT_MALLOC;
645 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
646 &key, &data, DB_NEXT);
649 if (ret != DB_NOTFOUND) {
650 lprintf(1, "cdb_next_item(%d): %s\n",
651 cdb, db_strerror(ret));
654 cclose(MYCURSORS[cdb]);
655 MYCURSORS[cdb] = NULL;
656 return NULL; /* presumably, end of file */
659 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
660 cdbret->len = data.size;
661 cdbret->ptr = data.data;
669 * Truncate (delete every record)
671 void cdb_trunc(int cdb) {
672 /* FIXME this needs to be implemented */
677 * Transaction-based stuff. I'm writing this as I bake cookies...
680 void cdb_begin_transaction(void) {
682 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
686 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
693 void cdb_end_transaction(void) {
696 for (i = 0; i < MAXCDB; i++)
697 if (MYCURSORS[i] != NULL) {
698 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
699 cclose(MYCURSORS[i]);
705 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");